Repository navigation
fix(codex): stop background exec completions from replaying full history into a fresh thread - #184206
Conversation
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
✅ Maintainer security changes approvedA maintainer approved this revision with an explicit security approval comment.
A later push requires a fresh approval comment for an external contributor's PR. |
|
Codex review: blocked before merge. What this changesThis PR stabilizes session tool policy, skill declarations, and WebChat context so background command completions reuse the bound Codex thread. Example: A WebChat user starts a background exec command and receives its completion.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Blocked before merge - 1 item remains This PR addresses a current-main completion-thread bug with useful live evidence and no identified correctness findings; current-head maintainer security approval remains required. Priority: P2 Before merge
FindingsNone. Agent review detailsHow this fits togetherBackground exec completions enter ordinary session admission, which prepares current tool authority and conversation context before dispatching the turn to the Codex harness. flowchart TD
A[Background exec completion] --> B[Captured session target]
B --> C[Ordinary session admission]
C --> D[Host tool policy and skill declarations]
D --> E[Codex thread compatibility]
E --> F[Bound native thread]
D --> G[Current authority checks before effects]
Technical reviewBest possible solution: Retain the focused session-stability repair with host-owned policy lifetime and current authority checks at execution. Do we have a high-confidence way to reproduce the issue? Current-main source retains all three destabilizing paths, and the contributor supplies a discriminating stock-versus-patched Gateway/app-server trace; no target code was executed during this review. Is this the best way to solve the issue? The repair uses the existing caller, event-target, host-policy, and skill-authoring owners, removes the superseded CLI helper, and preserves genuinely restricted turns rather than weakening Codex isolation. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 1ebc46e1a52f. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
PR surfaceSource +92, Tests +289, Other +3. Total +384 across 25 files. Added test files: 3. View PR surface stats
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment HistoryReview history (6 earlier review cycles)
Reviewed November 9, 2026, 9:59 PM ET / November 10, 2026, 02:59 UTC (Revision 7). |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
…vers The same-session, restricted-sender, and cross-session decisions are asserted through admission and an actual write in the final-effect table, so the extra target-field assertions only restated them. Document the toolPolicyScope option's contract where it is declared. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
toolPolicyScope is no longer a public tool-factory option. Host policy preparation records the attempt's scope, and the host's tool construction supplies it, so a plugin cannot claim conversation lifetime through options. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… test-line allocation
Retain expired invocation authority and explicitly reject workspace execution while restoring the remembered schema. Move unchanged inherited tool policy composition to its existing owner and infer the host capability return type to meet module line limits.
e780ca2 to
447a020
Compare
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Review update: the merge conflict is resolved in 663ead5b0269, and GitHub confirms this head is mergeable. The follow-up moves the unchanged WebChat route regression table into Validation on the exact candidate tree in secretless AWS: 36 tests passed across the context, WebChat-route, and session-event-permissions suites; formatting and targeted lint passed. Independent review found no additional code blockers. New-head CI, CodeQL, CodeQL Critical Quality, and OpenGrep passed. The remaining observed gate is current-head maintainer security approval; the aggregate |
Keep both regression groups mergeable without replaying unrelated main history. The WebChat route table is unchanged; no production behavior changes. Co-authored-by: sallyom <[email protected]>
|
/allow-security-sensitive-change |
|
Rationale for the security approval on Sender/group-restricted and turn-scoped producers do not receive this exemption. Cross-session completions and explicit source ceilings retain their restrictions. |
|
Merged via squash.
|
…ory into a fresh thread (openclaw#184206) * fix(codex): keep background exec completion wakes on the bound thread A background exec completion wake started a fresh native thread with the full session history instead of continuing the bound Codex thread. Three differences between the wake and the user turn each forced that: - The default session-event source policy capped every follow-up at the producer's tool list, so the wake ran as a turn-scoped restricted turn. A cap from a conversation-scoped producer that no group, per-DM or sender policy narrowed is now session-scoped and is skipped for follow-ups into the same session. Request-derived policy keeps the cap. - skill_workshop exposed its personal-library schema only on human turns. The embedded/harness path now reuses the stable skill authoring helper the CLI runner already had (openclaw#172026): internal turns publish the session's last human shape with an expired grant. Expired follow-up grants no longer replace the remembered shape. - An internal session stores no delivery route, so a wake on its own WebChat route dropped the session chat type and the conversation line in the developer instructions changed. * fix(codex): keep the session-scoped cap decision with the caller owner and prove its final effects Decide the session-scoped marking inside createCodingToolsGatewayCaller so agent-tools.ts and the session-event contract return to their base size, share the CLI stable skill-authoring instance from its module so prepare.ts keeps its line count, and replace the mocked caller test with one that observes the real caller identity. Add a production-boundary test showing that a same-session follow-up is uncapped only when the caller marked it, while a restricted sender, a cross-session destination, and authority revoked before the write all stay denied. * test(codex): drop the target-field cap test the final-effect table covers The same-session, restricted-sender, and cross-session decisions are asserted through admission and an actual write in the final-effect table, so the extra target-field assertions only restated them. Document the toolPolicyScope option's contract where it is declared. * fix(codex): keep tool-policy lifetime behind host tool construction toolPolicyScope is no longer a public tool-factory option. Host policy preparation records the attempt's scope, and the host's tool construction supplies it, so a plugin cannot claim conversation lifetime through options. * test(codex): allocate completion authority regression coverage * test: track Skill Workshop's move to its bundled plugin and the exact test-line allocation * fix(codex): preserve the human tool shape on expired follow-ups Retain expired invocation authority and explicitly reject workspace execution while restoring the remembered schema. Move unchanged inherited tool policy composition to its existing owner and infer the host capability return type to meet module line limits. * test: separate WebChat route regressions from recovery coverage Keep both regression groups mergeable without replaying unrelated main history. The WebChat route table is unchanged; no production behavior changes. Co-authored-by: sallyom <[email protected]> --------- Co-authored-by: Marvinthebored <[email protected]> Co-authored-by: sallyom <[email protected]>
Related: #183286, #179363, #172026, #178982
What Problem This Solves
When a background
execfinishes in a Codex-backed WebChat session, the completion wake does not continue the session's bound native thread. It starts a fresh transient thread, replays the full session history into it uncached, and does it again for the follow-up turn. On a long session every completion wake costs a full uncached history read. In one observed session, four completion wakes each sent about 190k uncached input tokens.This finishes, for event wakes, what #183286 started: tool declarations belong to the session, not to the turn. #183286 kept seven native Codex thread, plugin, and supervision declarations stable across owner and completion turns. A completion wake still differed from the user turn in three other ways, and each one alone forces a new thread:
skill_workshopschema. The personal-library schema is published only on human turns that carry an authoring grant. A wake publishes a narrower schema, the dynamic tool catalog fingerprint changes, and the bound thread rotates. The Claude CLI runner already fixed this in fix(claude-cli): stop cache-prefix churn after child completion #172026 (resolveStableCliSkillAuthoring); the embedded/harness path never used it.buildSessionEventContextnever matched the wake's own WebChat route and dropped the session chat type. The wake's developer instructions then lacked the "You are in a WebChat direct conversation" line, the live thread config fingerprint changed, and Codex had to cold-resume the thread.Why This Change Was Made
Each layer is fixed where the difference starts, without widening authority:
createCodingToolsGatewayCallermarks the caller identity session-scoped when the policy scope isconversationand no caller-supplied source policy exists, andsession-event-targetskips it only when the follow-up's destination is the same session. The mark is not set when group, per-DM, or sender policy narrowed the producer (groupPolicy/senderPolicy). Those come from the inbound request, the wake cannot rebuild them, and the cap keeps carrying them. The caller-inheritance merge keeps the mark only for an inherited owner of the same agent and session that has no narrower cap of its own. Cross-session follow-ups, sender-narrowed producers, and turn-scoped producers keep the cap exactly as before.pluginHarnessToolPolicyScope,resolvePluginHarnessToolPolicies). Host policy preparation now records that value in the attempt's host capability, and the host's tool construction (createToolSurfaceAsync, which is how Codex builds every dynamic tool) passes it to the internal tool options. A plugin cannot set it through tool options: a plugin-suppliedtoolPolicyScopeis overwritten by the host value, and when the host has none the cap stays in place (fail-closed).skill_workshop(B). The fix(claude-cli): stop cache-prefix churn after child completion #172026 helper moves fromcli-runner/tosrc/agents/stable-skill-authoring.tsas a per-runtime factory and is also applied at the embedded attempt dispatch. Internal turns publish the session's last human shape with an expired grant: the declaration matches, personal-library calls fail withAUTHORITY_EXPIRED, and workspace calls keep the ordinary no-grant rule (unsandboxed runs only). Grants that are already expired (transcript follow-ups, stranded-reply recovery) carryexpired: true, publish the remembered human shape without replacing it, and continue refusing personal and workspace authoring.channel: "webchat",to: <sessionKey>, the shapechat.sendand subagent announce already use) keeps the session chat type. Events routed to any other session still get none.Out of scope, noted for follow-up: when a thread's config does change (for example a model switch), Codex can take longer than OpenClaw's 2-second unload budget to close a code-mode thread after
thread/unsubscribe(measured 0.7 s, 2.9 s, and 10.6 s in this harness). The cold resume then fails with "Codex did not confirm unloading its previous configuration". With this change, completion wakes no longer take that path.AI-assisted: written with Claude Code. Every claim below was checked by running it.
User Impact
execcompletion wakes in Codex-backed sessions continue the bound thread with native code mode on. The wake reads the session prefix from cache instead of replaying the full history uncached, and the next user turn stays on the same thread.skill_workshopcalls that rely on that default now returnAUTHORITY_EXPIREDinstead of running against the workspace. Explicittarget=workspacecalls keep the no-grant rule.Evidence
The explicit-expired-grant regression fails before the repair (1 failed, 3 passed) and passes afterward (4 passed). It checks the remembered workspace schema, rejected personal invocation, rejected workspace execution, and the unchanged remembered shape.
Regression tests, each shown failing without its fix and passing with it:
host-capability.tool-policy-scope.test.ts(new): host-prepared scope reaches tool construction; a plugin-suppliedtoolPolicyScope: "conversation"does notsession-event-permissions.test.ts"bounds a same-session background completion's final effects: same-session"writeOffered: false, expectedtrue(the producer cap is kept)agent-tools.session-event-cap.test.ts"a sender narrowed by toolsBySender" (realcreateOpenClawCodingTools, real caller identity, no mock)expected true to be undefinedwhen the sender check is removedstable-skill-authoring.test.ts"keeps the human shape when a follow-up passes an expired grant"session-event-context.test.ts"keeps the session chat type only for $route"agent-tools.prefix-stability.test.ts"offers the human turn's skill_workshop definition on an internal turn"Authority controls around the same-session exemption
session-event-permissions.test.ts"bounds a same-session background completion's final effects" runs the production path: a realcaptureSessionEventTargetForHostunder a caller identity, the realenqueueSessionEventForHostadmission, the real harness host capabilities and managed tool surface, and a real file write. Each row captures a producer whose ceiling lists onlyread:same-sessionwriteofferedrestricted-sendersenderPolicyorgroupPolicynarrowed the turn; see the caller test above)writecross-sessionwriterevoked-before-writeread-onlyafter the tool surface is builtwriteoffered, call refused (authority/permission error)Result: 4 passed. With
session-event-target.tsrestored to its base version, thesame-sessionrow fails (writeOffered: false).Expired personal-library calls:
agent-tools.prefix-stability.test.tsbuilds the real coding tools for an internal turn and showsskill_librarywithtarget: "personal"rejected with "fresh message" (AUTHORITY_EXPIRED).stable-skill-authoring.test.tsshows an expired grant refuses personal-library calls and never replaces the remembered human shape; the sandboxed-run refusal for workspace calls is unchanged code from #172026 and is not re-tested here.What this does not show: the embedded dispatch wiring (
resolveStableEmbeddedSkillAuthoringatrun-attempt-dispatch.ts) is a single call covered by the two tests above through the same factory, not by a dispatch-level test.Touched suites: the files above plus
gateway-caller-context.test.tsandagent-tools.delegated-authority.test.tspass. The new production-boundary rows pass 4 of 4.tsgocore and thesrctest shards: clean.oxfmton the repository: clean.check:test-mock-exportsandcheck:max-lines-ratchetagainst the base commit: OK (the PR adds no mock factory and no line toagent-tools.tsorcli-runner/prepare.ts).Live run against a real Codex app-server (codex 0.162.1), an isolated gateway, and
chat.send. Script: user turn, a turn that startsexecwithbackground=true, the completion wake, then a user turn. Wire capture of the app-server JSON-RPC, base4fa442e3327:The live trace was captured with commit
591bd2b60aaon base4fa442e3327.Stock: each wake turn starts a fresh thread with 48 dynamic tools and 0 cached tokens. Patched: one thread with 10 dynamic tools for the whole session. The wake turns are plain
turn/startcalls on the bound thread, reading 99% of their input from cache, with nothread/resumeorthread/unsubscribein the wire log. The next user turn stays on the same thread. (The stock run's next user turn did not reach the app-server before the harness stopped, so it is not shown.)What this evidence does not show: the harness covers a WebChat direct session with no group, per-DM, or sender policy. Sessions with those policies are covered by the unit tests above, not by a live run.
🤖 Generated with Claude Code