Repository navigation
Keep replayed Codex usage snapshots out of unknown turn ids - #1804
SawyerHood merged 2 commits into
Conversation
|
🚨 SLOP COP 🚨 · I am SlopCop. I am reviewing this pull request now. I will check security, code quality, architecture, performance, and the relevant test path with parallel reviewers. |
| // Context-window usage is session state and may be thread-scoped; token | ||
| // usage is turn-only and, on resume, duplicates the snapshot bb already | ||
| // persisted for that turn, so drop it. | ||
| if ( |
There was a problem hiding this comment.
🚨 slopcop/review — Increase the host daemon protocol version for this event-stream change.
This code drops the token event and changes the context event scope. Repository policy requires a protocol increase when message meaning changes. Version 132 also documents an equivalent bridge event suppression change. Update the protocol constant and its contract test.
There was a problem hiding this comment.
Done: bumped HOST_DAEMON_PROTOCOL_VERSION to 134 with a lineage note in protocol.ts and updated the contract test (4ec5864).
|
|
||
| if (event.type === "turn/started" && event.scope.kind === "turn") { | ||
| session.openCodexTurnIds.add(event.scope.turnId); | ||
| session.startedCodexTurnIds.add(event.scope.turnId); |
There was a problem hiding this comment.
🚨 slopcop/review — Bound the new turn-ID state.
The set keeps every started turn ID until the session ends. An active session can therefore grow with the total turn count. A construction-phase marker would avoid retained history and limit replay handling to resume or fork construction.
There was a problem hiding this comment.
Done: replaced the set with a construction-phase boolean (awaitingReplayedUsage), set for resume/fork sessions and cleared at the first turn/started, so no per-turn state is retained (4ec5864). Also added a fork replay path to the fake app-server and a fork regression test.
There was a problem hiding this comment.
🚨 SLOP COP 🚨 · review
Plain English summary
This pull request stops old Codex usage data from attaching to a turn that BB never started.
It keeps context usage at the thread level. It also drops duplicate token data for the old turn.
Findings
-
Blocking: Increase
HOST_DAEMON_PROTOCOL_VERSIONand update its contract test.The bridge now drops one daemon event and changes the scope of another event. This change alters the daemon-to-server event stream.
The repository policy requires a protocol increase for this type of message meaning change. Version 132 documents a similar bridge event suppression change.
-
Non-blocking: Bound
startedCodexTurnIds.The set keeps each turn ID until the session ends. An active session can therefore use more memory with each turn.
A construction-phase marker would remove this history. It would also limit replay handling to resume and fork construction.
-
Non-blocking: Add a fork replay test.
The new test covers resume. The fake fork path does not send the replay that the code also claims to handle.
Architecture
The database already drops both invalid usage snapshots. That rule remains a good final safety guard.
The database cannot replace this bridge logic without losing the context snapshot. Provider scope normalization belongs in the provider bridge.
I found no other security issue or important CPU cost.
Validation
- The focused bridge replay test passed.
- The provider package type check passed through Turbo.
git diff --checkpassed.- GitHub CI is green.
- Browser testing does not apply because this change has no user interface or HTTP route.
I left a comment-only review, as required by this rule. The protocol increase must occur before merge.
Codex app-server replays the rollout's last-turn token usage on thread/resume and thread/fork, scoped to the previous turn's Codex id. The bridge re-mints turn ids per session, so the replayed snapshot named a bb turn id that never had a turn/started, and the server dropped it as an orphan thread-state snapshot (#1727). The bridge now tracks the Codex turn ids it emitted turn/started for. A usage snapshot for any other turn is a replay: the context-window usage goes out thread-scoped (allowed by the scope policy) and the turn-only token usage is dropped. The server-side orphan drop stays as the safety net. Fixes #1727 Co-Authored-By: Claude <[email protected]>
- Replace the per-turn id set with a construction-phase marker that is set for thread/resume and thread/fork sessions and cleared at the session's first turn/started, so state does not grow with turn count. - Fake app-server: thread/fork of a usage-replay-* thread replays the source turn's usage under the new thread id, like codex does; add a fork regression test. - Bump HOST_DAEMON_PROTOCOL_VERSION to 134: the bridge now drops one daemon event and re-scopes another on the daemon->server stream. Co-Authored-By: Claude <[email protected]>
f0c4ebc to
4ec5864
Compare
…ailed turn (#1807) ## What was wrong Pi has a `keepRecentTokens` floor (20k tokens by default). When a session has nothing before that floor, `/compact` refuses before it calls the model with `Compaction failed: Nothing to compact (session too small)`. Pi reports that refusal through the same `compaction_end.errorMessage` field as a real failure. The pi event translator (`packages/agent-runtime/src/pi/event-translation.ts`) turned every manual `compaction_end` error into `turn/completed status=failed`. The server maps a failed root turn to `run.failed`, so the thread moved to `error`, the composer showed "Retry by sending a follow-up message", and `bb thread tell` (steer mode) answered `HTTP 409: Thread is not active`. Nothing was wrong with the session. Report: https://get-bb.github.io/reports/issues/1721.html ## What changed - `packages/agent-runtime/src/pi/event-translation.ts`: a manual `compaction_end` with pi's pre-flight refusal messages (`Nothing to compact (session too small)`, `Already compacted`) is a no-op. The translator emits `provider/warning { summary: "Context compaction skipped", details: <pi message> }` and a normal `turn/completed status=completed`. Real failures (for example `Summarization failed: ...`) still emit `status=failed`. Manual compaction still owns its maintenance turn. - `packages/thread-view/src/build-event-projection.ts`, `operation-projection.ts`: a `provider/warning` inside a compaction turn settles the pending compaction row as `Context compaction skipped` with the warning detail. The row was otherwise pending forever, because no `thread/compacted` or `item/completed` follows a refusal. The warning is not rendered a second time as a standalone row. - Tests: `event-translation.test.ts` gets the no-op case for both refusal messages and the pinned failure case now uses a real failure message. `timeline-interruption.test.ts` gets the projection case (with a `providerWarning` factory in the harness). ## Deviation from the report The report suggested `provider/error{message:"Context compaction skipped"}` as the cheapest way to close the compaction row. That renders a red "Provider error" row and an error-status compaction row, which is not informational. This PR uses `provider/warning` instead and settles the row with a `completed` status. No wire contract changed, so `HOST_DAEMON_PROTOCOL_VERSION` is unchanged. ## Update after SlopCop review - Added the `compaction-skipped` provider warning category (`packages/domain`). The pi translator emits it for a refused manual compaction, and the thread view settles the pending compaction row **only** for that category. An unrelated warning inside a compaction turn keeps its own row and leaves the compaction pending (new test `keeps a pending compaction and renders an unrelated warning in the same turn`, fails before / passes after). - Bumped `HOST_DAEMON_PROTOCOL_VERSION` to 135 (134 was taken by #1804) (plus the contract test), because the daemon event stream and the warning category enum changed. - Regenerated `packages/plugin-sdk/bundled-types` for the new enum member. - Verified: `turbo typecheck` for domain, agent-runtime, thread-view, host-daemon-contract, plugin-sdk, server, app — 7 successful; `turbo test` for domain (145), host-daemon-contract (52), thread-view (378), agent-runtime (320) — all pass. ## How I verified - Before the change: the two new translator tests failed (`status: "failed"` with the pi error), and the new thread-view test failed (row stayed `pending`). - `pnpm exec turbo run typecheck --filter=@bb/agent-runtime --filter=@bb/thread-view` — 2 successful. - `pnpm exec turbo run test --filter=@bb/agent-runtime --filter=@bb/thread-view` — agent-runtime 320 passed, thread-view 377 passed. Fixes #1721 > AGENT GENERATED: by Claude Opus 5 --------- Co-authored-by: Claude <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
… unknown turn ids Main commit 644df1c fixed this in the codex bridge's thread/event emission, which this branch rewrote to thread/delta. The session's awaitingReplayedUsage field and its construction set-site survived the rebase; the consumer did not. Re-implemented in sendThreadDeltas: while the session awaits replayed usage, a usage.exact delta (codex's replayed thread-state snapshot) is dropped — its token usage duplicates what bb persisted — and re-emitted as a contextWindow delta that assembles thread-scoped (the fresh session has no current or last turn); the first turn.open clears the flag. The fake app-server's replay support and the protocol-version bump merged cleanly; the regression test is ported to the delta assembly harness (ids compared within one assembled snapshot because assembler entropy is fresh per pass). Co-Authored-By: Claude Fable 5 <[email protected]>
What was wrong
codex app-serverreplays the rollout's last-turn token usage right afterthread/resumeandthread/fork, scoped to the previous turn's Codex turn id, before any new turn starts. The Codex bridge stamps every turn scope with a per-session id prefix (bt<entropy>-<serial>-), so that replayed snapshot went out under a bb turn id the server had never seen aturn/startedfor (resume: same Codex turn, different prefix; fork: the parent's turn). The server correctly classifies that as an orphan thread-state snapshot and drops it, and logsDropped orphan thread-state snapshot with no stored turn/startedtwice on every Codex resume (unarchive, daemon restart, idle reap) and on the first turn of every fork. Investigation report: https://get-bb.github.io/reports/issues/1727.htmlWhat changed
plugins/provider-codex/src/bridge/bridge.ts: each session tracks the Codex turn ids it has emittedturn/startedfor. When athread/tokenUsage/updatedorthread/contextWindowUsage/updatednames any other turn (only resume/fork replays do that), the bridge emits the context-window usage thread-scoped ({kind:"thread"}, allowed by the domain scope policy and what the ACP bridge already does) and drops the token usage (turn-only by policy; on resume it duplicates the persisted snapshot, on fork it is the parent's cumulative total).plugins/provider-codex/src/bridge/fake-codex-app-server.mjs:thread/resumefor ausage-replay-*provider thread id replays last-turn usage like the real app-server (opt-in fixture behavior).plugins/provider-codex/src/bridge/bridge.resume-usage-replay.test.ts: new regression test that drives two bridge sessions (resume, turn, release, resume) and asserts the replayed token usage is dropped and the replayed context-window usage is thread-scoped.The server-side orphan drop is unchanged; it stays as the safety net. No wire changes, so no
HOST_DAEMON_PROTOCOL_VERSIONbump. No deviation from the report's proposed fix.Verification
pnpm exec vitest run src/bridge/bridge.resume-usage-replay.test.ts(inplugins/provider-codex) withbridge.tsreset toorigin/main: 2 failed (resume:expected 3 to be 2; fork: replayed token usageto have a length of +0 but got 1). With the change: 2 passed.pnpm exec turbo run typecheck test --filter=bb-plugin-provider-codex --filter=@bb/host-daemon-contract --filter=@bb/host-daemon --force: all pass (codex plugin 15 files / 167 tests; host-daemon-contract 52 tests; host-daemon 47 files).Fixes #1727