Skip to content

bt-daemon: import through the daemon and remove the delivery ledger - #291

Merged
Stephen Belanger (Qard) merged 6 commits into
mainfrom
design/import-through-daemon
Nov 16, 2026
Merged

Stephen Belanger (Qard) merged 6 commits into
mainfrom
design/import-through-daemon

Conversation

@Qard

@Qard Stephen Belanger (Qard) commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Second PR in the stack that splits #276. Its base was #289, which has since merged into main.

Why

On main, bt trace import translates and delivers in its own process, while the daemon delivers live sessions. The two coordinate through the destination-scoped delivery ledger. That ledger has its own NDJSON log, a JSON checkpoint, a SQLite origins index outside session storage, and OS file locks shared between processes. Once the daemon is the only process that delivers, none of that is needed.

Changes

  • Import is a daemon client. It starts or upgrades the daemon the same way hook capture does, then sends each synthesized lifecycle event with event.log over a single connection.
    • import.finish waits for the session's routes to deliver and reports their root span and distinct span count. With finalize: false, it only flushes, which is what --attach does when it detaches.
    • import.reset restarts translation from fresh state when the transcript was rewritten. It's stored as a Reset control in the journal, so replay resets the translator at the same point.
  • Ledger removed.
    • The delivery_ledger module, its origins index and its use in the consumer are gone.
    • Translation already stamps each op's origin from stored origins.
    • Duplicate tags are now removed while routed rows are prepared (unique_tags).
    • GC deletes old delivery-ledger directories.
  • Legacy delivery checkpoints.
    • A journal from a daemon that predates the SpanOp journal can carry a delivery_checkpoint. Translation now advances that route's head past the output the checkpoint covered, and discards the origins rebuilt for those spans.
    • Previously, only the ledger's list of completed spans stopped those spans from being re-delivered with the new daemon's provenance.
  • Each import run is isolated. A run captures into its own daemon-side session: the native session id plus a per-run suffix, with its own journal, storage, translator and delivery. Translation, span metadata, sinks and span plugins all use the native session id. A run therefore reproduces the native session's deterministic span ids, and delivers every span to its destination, even one that already received the session elsewhere.
  • Trade-off. Repeating an import re-delivers its rows under the same ids. An import older than what a destination already holds can replace newer content on the spans they share. On main, the ledger prevented that by remembering each destination's completed spans.

Tests

  • The import tests in replay.rs, cursor_pipeline.rs, distributed_tracing.rs, muse_attach.rs and root_provenance.rs now run against a daemon (tests/support/import_daemon.rs). Their ledger-state assertions are removed.
  • legacy_acknowledged_spans_receive_late_updates_without_inventing_origins covers the legacy checkpoint fix.
  • importing_into_another_destination_delivers_the_whole_session and repeating_a_claude_import_reuses_its_span_ids cover the two review findings. Both fail without isolated runs. Four Muse import tests from main now check identity (same span ids, one trace) rather than "nothing re-sent".
  • 715 tests passed and 14 were ignored. Clippy is clean with all features and with default features, and cargo fmt --check passes.

🤖 Generated with Claude Code

Every translator is now serializable, and AgentTranslator requires a new
TranslatorState trait with a blanket implementation for serde types. A
translator's saved state is everything it still needs from events it has
consumed to translate later ones, so a later pipeline can persist it
instead of replaying those events.

Sink-only row fields that wire JSON omits are preserved through a shared
SinkFields codec, which the stored SpanOp codec now also uses. serde_json
round-trips floats exactly so restored state matches what was saved.
OpenCode v2 message parts the translator does not model are kept as raw
JSON rather than rejected.

A test resumes every agent's native stream from saved state after each
event and requires output identical to an uninterrupted run; the shared
native streams now include an OpenCode v2 session.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Transcript import is now a daemon client. It starts or upgrades the daemon
as hook capture does, captures every synthesized event over one connection,
and finishes each session with import.finish, which flushes or finalizes
its routes and reports their root span and distinct span count. A rewritten
transcript restarts translation with import.reset, a Reset control stored
in the journal so replay resets the translator at the same point. Imports
and hooks share one pipeline.

With the daemon the only process that delivers, the delivery ledger and its
cross-process locks, NDJSON log, checkpoint and origins index are gone.
Translation already stamps stored origins; tags are deduplicated while
routed rows are prepared. Legacy ledger directories are deleted. A legacy
delivery checkpoint now advances its route's head past the output it
covered and discards origins rebuilt for those spans, which the ledger
previously did by remembering completed spans.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-16T10:54:21.981358Z a6483b2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6483b236b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment on lines +253 to +255
env.route = Some(self.route.clone());
env.config = None;
self.client.log(&env).await?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Replay historical Codex output for a newly selected destination

When a Codex session has already been delivered through this daemon, importing it into another project or experiment now reuses the existing source translator. That translator retains its transcript offsets, so the imported lifecycle events do not regenerate the historical turn, model, or tool rows. Meanwhile, SpanJournal::prune deletes acknowledged operations and subscribe starts the new route at the publication floor; observe_history only restores correlation metadata, not sink output. Consequently, the import can report success while the new destination receives only late root updates instead of the session. Preserve or explicitly regenerate the historical output when importing into a new route.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 611c4da. Each import run now captures into its own daemon-side session and translates the native session from fresh state under the native session's identity. A new destination receives the whole session, and a repeated import reproduces the original span ids. Covered by importing_into_another_destination_delivers_the_whole_session and repeating_a_claude_import_reuses_its_span_ids in tests/replay.rs, which both fail without the change.

Self::emit_translator_batches(live, ops).await?;
env.route = Some(self.route.clone());
env.config = None;
self.client.log(&env).await?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Make repeated Claude imports reuse their original span identities

Importing the same completed Claude transcript twice while the daemon remains running now feeds every synthetic UserPromptSubmit into the same existing translator. ClaudeTranslator::open_turn increments turn_count unconditionally, and ensure_root increments root_generation after the earlier SessionEnd, so the second import creates a new trace and new turn IDs for the same historical work rather than updating the original spans. Previously, each import started with a fresh translator and therefore reproduced the original deterministic IDs. Add replay-aware identity handling so repeated imports, including imports after live capture, do not duplicate historical turns and traces.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 611c4da. Each import run now captures into its own daemon-side session and translates the native session from fresh state under the native session's identity. A new destination receives the whole session, and a repeated import reproduces the original span ids. Covered by importing_into_another_destination_delivers_the_whole_session and repeating_a_claude_import_reuses_its_span_ids in tests/replay.rs, which both fail without the change.

Stephen Belanger (Qard) added a commit that referenced this pull request Nov 16, 2026
This branch already contained its own translator-state and import changes,
so conflicts take this branch's side. Keeps #289's save-and-restore test
for every agent alongside this branch's recovery tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Match the crate's convention: derives name Serialize and Deserialize
through a use serde import rather than serde:: paths.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sign/import-through-daemon

# Conflicts:
#	bt-daemon/src/transcript_import/pipeline.rs
Imports shared the live session's translator, so importing a session into a
new destination delivered almost none of its history, and repeating a Claude
import opened new turns and a new trace for the same work.

Each import run now captures into its own daemon-side session (the native id
with a per-run suffix) with its own journal, storage and delivery, while
translation, span metadata, sinks and span plugins use the native session id.
A run therefore reproduces the native session's deterministic ids and
delivers every span to its destination. Repeating an import re-delivers its
rows under the same ids; an older import may replace newer content on spans
it shares with what a destination already holds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stephen Belanger (Qard) added a commit that referenced this pull request Nov 16, 2026
Each import run's translator uses the native session's namespace here too,
while its events and ops live in the run's own session storage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Base automatically changed from design/translator-state to main November 16, 2026 14:57
…-daemon

# Conflicts:
#	bt-daemon/src/transcript_import/pipeline.rs
@Qard
Stephen Belanger (Qard) merged commit c4e25c9 into main Nov 16, 2026
29 checks passed
@Qard
Stephen Belanger (Qard) deleted the design/import-through-daemon branch November 16, 2026 15:58
Stephen Belanger (Qard) added a commit that referenced this pull request Nov 16, 2026
#291 landed as c4e25c9, whose tree matches the #291 head this branch already
merged, so every conflict keeps this branch's side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants