Repository navigation
fix(codex): keep managed relay context in the cached prefix - #206554
Conversation
The managed inference relay put skills, shared persona, memory and session project guidance in each request's top-level instructions and disabled the native thread developer carrier. Models that never cache instructions (GPT-6.2 Sol/Astra) then lost that tail on every warm request. Stable context now always rides the refreshable thread developer section; the relay keeps only the personal USER.md overlay, cron policy, plugin system context and per-turn conversation text.
|
🦞👀 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. |
|
Proof for #206421 (observed, no secrets): Residual: the fix leaves ~420 uncached tokens per warm request on gpt-6.2-sol (the relay's per-turn conversation instructions and tool bindings in |
|
Codex review: needs changes before merge. What this changesThis PR moves shared Codex context into cacheable thread developer instructions while retaining personal overlays and turn context in the parent-only relay. Example: A managed Codex turn makes five sequential tool calls with a loaded skills catalog.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 1 item remains This PR addresses a demonstrated caching defect with a focused production change, but it leaves a snapshot consumer asserting the old instruction layout and failing CI. Reviewed head: Before merge
Findings
Agent review detailsHow this fits togetherOpenClaw prepares workspace and skill instructions, then the Codex adapter divides them between native thread developer input and parent-only relay instructions before provider dispatch. flowchart LR
A[Workspace and skills] --> B[Context preparation]
B --> C[Shared thread instructions]
B --> D[Personal overlay]
E[Current turn context] --> F[Parent only relay]
D --> F
C --> G[Codex provider request]
F --> G
Technical reviewBest possible solution: Keep the existing thread-carrier repair and update the snapshot consumer to verify shared persona there while retaining personal-overlay isolation. Do we have a high-confidence way to reproduce the issue? Current-main source confirms the uncached relay placement, and the reporter's official-release comparison plus supplied base/candidate request evidence establish a strong reproduction path without this review executing target code. Is this the best way to solve the issue? Reusing the existing refreshable thread owner is a small, coherent repair with an explicitly accepted inheritance tradeoff; the retained snapshot contract must follow that cutover. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against c47c5251d1db. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceAcceptance criteria:
What I checked:
Likely related people:
PR surfaceSource -3, Tests +192, Docs +6. Total +195 across 23 files. Added test files: 0. 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. Review metrics
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 Reviewed November 27, 2026, 5:02 AM ET / 10:02 UTC. |
- Leave the refreshable skills/persona/memory section out of the thread config fingerprint, so a changed section no longer forces a cold resume that protected background work refuses. - Record the delivered section for persistent live threads too, so the existing warm handoff and post-compaction restore deliver edits; ephemeral lifetime keys off generic policy.
|
Review round 1 P1 proof (protected thread + skill edit), head fe38f61 vs previous head 8fd3970. Observed, no secrets: |
# Conflicts: # test/fixtures/agents/prompt-snapshots/codex-runtime-happy-path/discord-group-codex-message-tool.md.diff
…thread - A persona edit or removal now arrives as a refresh handoff on the live thread, not a cold resume; the latest section carries the new persona and nothing after it carries the old one.
|
Rejected review findings: round 2 on This is an owner decision, and it has been made. On 2026-11-27 Ayaan delegated product calls on obvious bugs ("if you think anything is a really, really obvious bug, feel free to make the product decision there after consulting Fable 5.2"). We consulted Fable 5.2 and decided to keep the reversal of #142276's parent-only delivery:
No code change follows from this finding. |
|
Final-head proof on Scripted relay lane (vitest-source; real bundled Codex 0.172.0 app-server and relay; synthetic upstream records the request bodies;
Live cache (the reporter's five-tool-call turn; per-request
Uncached input per turn: main 51.8k, this PR 24.0k, Live P1, managed relay (persistent thread; on turn 1 the model starts
|
…tale - Native compaction rebuilds from the section a persistent thread was started or resumed with and can sample its continuation before any host handoff, so an in-place refresh could let a withdrawn persona or skill return for one request. - Reuse now cold-resumes an unprotected persistent thread whose native section differs from the current one (idle edit, or the first turn after protected work ends); protected threads keep the in-place handoff instead of refusing. - Tests: native compaction continuation on a persistent thread carries the withdrawn catalog's removal; protected edit refreshes in place, then cold-resumes once the listener ends; the external WebSocket persona test asserts the cold resume again.
|
Proof for Change: on reuse, a persistent thread is cold-resumed when its native section differs from the current one and nothing protects it ( Tests (vitest-source; red side = the same test files copied onto
Affected codex suites on Live (gpt-6.2-sol, dist build of
Remaining gap (stated in the body): if a section is edited while protected work runs and the thread compacts before that work ends, one continuation request can still see the previous section. It heals on the first unprotected turn. |
- An idle persistent thread cold-resumes for a changed section; protected and incognito threads take the injected handoff, and the compaction limitations apply only to those.
… copy is stale - Native compaction can sample its continuation from the thread's native section before any injected handoff. On the managed relay, while that native section differs from the current one (an edited incognito thread, or a protected persistent thread awaiting its cold resume), request instructions also carry the current section, as main did on every request. - The fallback stops once the native copy matches; unedited threads never pay for it, and the section never holds the personal overlay. - Test: managed incognito edit, withdrawal and compaction; every relayed request in the edited turns, including the continuation after compaction, carries the current catalog and the personal overlay once.
|
Proof for
|
|
Landed in cf0c99c. With the managed Codex app-server, skills, shared persona and memory now go in the native thread's developer instructions instead of every request's top-level |
…#206554) Root cause: extensions/codex/src/app-server/thread-lifecycle-run.ts:154 dropped the native thread developer carrier whenever the managed inference relay existed, and extensions/codex/src/app-server/run-attempt-turn-request.ts:293 put skills, shared persona, memory and session project guidance into the relay text that inference-context.ts:271 adds to every request's top-level `instructions`. GPT-6.2 Sol and Astra never cache `instructions`, so every warm request in a turn re-paid that whole block. Fixes openclaw#206421 Thanks @brandon-julio-t for the precise report (managed vs `appServer.command` A/B with per-request usage), and @NaturalMatchaKing for the A/B evidence in openclaw#204136. ## What changed - Skills, shared persona (`SOUL.md`, `IDENTITY.md`, workspace-root `USER.md`), memory guidance and session project guidance now always ride the existing refreshable thread developer section, on managed connections too. Codex sends that as a `developer` input item, so it sits in the cached prefix, and later requests in a turn only append. - The relay keeps only parent-only text: the selected personal `users/<id>/USER.md` overlay, cron policy, plugin `before_prompt_build` system context, and per-turn conversation instructions. Per-turn plugin context stays off the thread on relay connections (`turnRefreshableInstructions` split; still thread-carried when there is no relay, as before). - `workspace-context.ts` renders the personal-only persona with the same renderer; the codex context plumbs `personalPersonaInstructions` instead of the full persona. - Edits to that section no longer refuse a thread kept alive by background work. Before this, a skill, persona or memory edit made the next message cold-resume the thread, and a thread kept alive by a running background command refused with "still has background work… stop its running work and retry". `appServer.command` and other non-relay connections already failed this way on main; managed connections would have started to with the carrier move. Now the refreshable section is left out of the thread configuration fingerprint, and persistent live threads keep the same in-memory record incognito threads already had of the section the thread was natively given (`nativeRefreshableInstructions`) and the one last delivered. On reuse (`thread-lifecycle-warm.ts`): - Idle thread, section changed: cold resume, as on main, so native configuration carries the current section and compaction rebuilds from it. On managed connections this costs one uncached turn per edit. - Protected thread (running background command or child), section changed: the existing appended developer handoff instead of the refusal; the existing post-compaction restore re-delivers it. - Self-heal: the first later turn where the thread is no longer protected and its native section still differs from the current one cold-resumes. - Managed relay fallback: while a thread's native section differs from the current one (an edited incognito thread, or a protected persistent thread waiting for its cold resume), the relay also sends the current section in request `instructions`, as main did for every request, so a post-compaction continuation sees it. It stops when the native copy matches again (after the self-heal cold resume); unedited threads never pay for it. The section never holds the selected personal overlay, so that is still sent once (`renderCodexRelayRefreshableFallback`, thread-policy.ts). No new field, flag or persisted state: the comparison uses the in-memory record, and ephemeral lifetime is keyed off the generic policy that only ephemeral records carry. - Remaining narrow gap, only on connections without a relay (`appServer.command`): an edit made while background work keeps the thread protected, followed by a native compaction before that work ends, can let one continuation request see the previous section. On main those threads refuse the next turn outright. Codex samples the continuation right after compaction, before injected developer messages are drained (`codex-rs/core/src/session/turn.rs` pending-input drain is deferred after auto-compaction; `session/inject.rs` queues injections into the active turn), and the host restore lands on the request after it. It heals on the first unprotected turn. Not taken: (B) carrying the section in turn-level `collaborationMode.settings.developer_instructions`, because a model catalog's default-mode override wins over caller text, it replaces the built-in Default-mode text and collides with OpenClaw's cron collaboration instructions, and native children do not inherit it; (D) asking Codex to drain injected developer messages before the post-compaction continuation, or to accept developer instructions in thread settings, needs a Codex dependency bump. - Report estimate counts the stable section on the thread in both modes. - No model-facing wording changed. Layout change: a user with a personal overlay gets two `## OpenClaw Agent Soul` blocks (shared in the thread, personal in the relay). - `inference-dispatch.ts` untouched (openclaw#206475 size recovery kept). ## What remains: 97.2% vs 99.5% Live gpt-6.2-sol against main `2553b7e25179`, the reporter's five-tool-call turn. Two runs per arm on `2ca1ebe03be6` plus one per arm on `a286631217b9` (the stale-section cold resume does not touch an unedited turn; earlier heads had three runs per arm with the same results). Warm-request cache hit and per-request `input/cached` come from the Codex rollout: | arm | warm hit | uncached input per warm request | previous request's input not cached | uncached input per turn | |---|---|---|---|---| | main, managed relay | 70.6–70.7% | ~6,150–6,200 tokens | 6,054–6,100 tokens | 51.5–51.8k | | this PR, managed relay | 97.2–97.3% | ~570 tokens | 421 tokens | 23.9–24.0k | | `appServer.command` (no relay) | 99.5% | ~100 tokens | 3 tokens | 20.5–20.6k | The remaining ~420 tokens per warm request are the relay's per-turn conversation instructions and tool bindings, which still go in top-level `instructions` and are never cached on these models. They are per-turn facts, so moving them is a separate change (for example to turn `additionalContext`); this PR does not move them. ## Earlier attempt openclaw#141270 (@mbelinky) aimed at the same carrier: workspace persona and memory on the thread developer instructions. This PR is a smaller change at the relay/lifecycle owners and does not build on it. ## Deliberate decision reversed This reverses openclaw#142276's parent-only delivery for shared text: native Codex subagents now inherit shared persona, skills and memory guidance through `developer_instructions` (`codex-rs/core/src/agent/child_config.rs` `inherited_developer_instructions`). Reasoning: that is already the behavior of every connection without a relay (openclaw#162156 called it intentional); native delegation is off wherever session tools are denied; the original openclaw#142276 problem (catalog collaboration text replacing caller instructions) is solved by thread developer instructions; and keeping the split costs every managed user about 2.2x the uncached input on a five-tool-call turn (51.8k vs 24.0k tokens; about 11x per warm request). The personal overlay stays parent-only (openclaw#162156 privacy boundary kept). A child created after an in-place edit inherits the thread's creation-time section, the same limitation incognito threads already document. ## Evidence Proof comments: openclaw#206554 (comment) (cache and request bodies), openclaw#206554 (comment) (background-work P1), openclaw#206554 (comment) (`2ca1ebe03be6`: scripted lane, live cache, live managed P1), openclaw#206554 (comment) (`a286631217b9`: stale-section cold resume, compaction and self-heal regressions, live P1 on both routes, cache), openclaw#206554 (comment) (`76341cb30f58`: managed relay fallback regression, live managed P1). Child-inheritance decision: openclaw#206554 (comment) - Scripted lane (real managed bundled codex 0.172.0 app-server, real relay, synthetic Responses upstream recording bodies): `run-attempt.skills.native.test.ts` "keeps managed skills in the cached native prefix across a multi-request turn" is red on main (skills in `instructions` on every frame) and green here (marker once in a `developer` input item, each frame's input extends the previous one byte-for-byte). - Background-work regression: `run-attempt.native-background-outcome.test.ts` "refreshes edited skills on a protected thread, then cold-resumes once it ends" (managed relay and `appServer.command`): the protected edit arrives as one handoff with no refusal, and after the listener ends the next turn unsubscribes and cold-resumes with the edited catalog. Red on `8fd3970192d6` (refusal) and on `2ca1ebe03be6` (no self-heal); green here. - Compaction regression: `run-attempt.skills.native.test.ts` "keeps a withdrawn catalog withdrawn after native compaction rebuilds the persistent thread" (real Codex binary, pre-sampling auto-compaction): the continuation request after compaction no longer carries the withdrawn creation catalog. Red on `2ca1ebe03be6`, green here; the incognito case keeps its documented one-request reversion. - Real Gateway + `openclaw agent`, gpt-6.2-sol, a native-shell `sleep` listener started on turn 1, a skill installed with `openclaw skills install`, then turn 2: previous head refuses on both routes; this PR answers with the new skill on both, through a warm refresh handoff, with turn 2's first request cached up to the previous input. After a Gateway restart, the cold resume carries the current section. ## Merge danger Two-way door: no stored state, schema or config changes; a revert restores the previous layout. On first turn after upgrade each managed thread gets one cold resume (Gateway restart) that installs the section, as before. Restricted threads on connections without a relay rotate once after upgrade because their stored fingerprint included the section; afterwards edits cold-resume them instead of rotating them. Blast radius: Codex harness thread lifecycle and relay instructions. Co-authored-by: Ayaan Zaidi <[email protected]>
Root cause: extensions/codex/src/app-server/thread-lifecycle-run.ts:154 dropped the native thread developer carrier whenever the managed inference relay existed, and extensions/codex/src/app-server/run-attempt-turn-request.ts:293 put skills, shared persona, memory and session project guidance into the relay text that inference-context.ts:271 adds to every request's top-level
instructions. GPT-6.2 Sol and Astra never cacheinstructions, so every warm request in a turn re-paid that whole block.Fixes #206421
Thanks @brandon-julio-t for the precise report (managed vs
appServer.commandA/B with per-request usage), and @NaturalMatchaKing for the A/B evidence in #204136.What changed
Skills, shared persona (
SOUL.md,IDENTITY.md, workspace-rootUSER.md), memory guidance and session project guidance now always ride the existing refreshable thread developer section, on managed connections too. Codex sends that as adeveloperinput item, so it sits in the cached prefix, and later requests in a turn only append.The relay keeps only parent-only text: the selected personal
users/<id>/USER.mdoverlay, cron policy, pluginbefore_prompt_buildsystem context, and per-turn conversation instructions. Per-turn plugin context stays off the thread on relay connections (turnRefreshableInstructionssplit; still thread-carried when there is no relay, as before).workspace-context.tsrenders the personal-only persona with the same renderer; the codex context plumbspersonalPersonaInstructionsinstead of the full persona.Edits to that section no longer refuse a thread kept alive by background work. Before this, a skill, persona or memory edit made the next message cold-resume the thread, and a thread kept alive by a running background command refused with "still has background work… stop its running work and retry".
appServer.commandand other non-relay connections already failed this way on main; managed connections would have started to with the carrier move. Now the refreshable section is left out of the thread configuration fingerprint, and persistent live threads keep the same in-memory record incognito threads already had of the section the thread was natively given (nativeRefreshableInstructions) and the one last delivered. On reuse (thread-lifecycle-warm.ts):instructions, as main did for every request, so a post-compaction continuation sees it. It stops when the native copy matches again (after the self-heal cold resume); unedited threads never pay for it. The section never holds the selected personal overlay, so that is still sent once (renderCodexRelayRefreshableFallback, thread-policy.ts).No new field, flag or persisted state: the comparison uses the in-memory record, and ephemeral lifetime is keyed off the generic policy that only ephemeral records carry.
Remaining narrow gap, only on connections without a relay (
appServer.command): an edit made while background work keeps the thread protected, followed by a native compaction before that work ends, can let one continuation request see the previous section. On main those threads refuse the next turn outright. Codex samples the continuation right after compaction, before injected developer messages are drained (codex-rs/core/src/session/turn.rspending-input drain is deferred after auto-compaction;session/inject.rsqueues injections into the active turn), and the host restore lands on the request after it. It heals on the first unprotected turn. Not taken: (B) carrying the section in turn-levelcollaborationMode.settings.developer_instructions, because a model catalog's default-mode override wins over caller text, it replaces the built-in Default-mode text and collides with OpenClaw's cron collaboration instructions, and native children do not inherit it; (D) asking Codex to drain injected developer messages before the post-compaction continuation, or to accept developer instructions in thread settings, needs a Codex dependency bump.Report estimate counts the stable section on the thread in both modes.
No model-facing wording changed. Layout change: a user with a personal overlay gets two
## OpenClaw Agent Soulblocks (shared in the thread, personal in the relay).inference-dispatch.tsuntouched (fix: recover Codex chats after oversized requests #206475 size recovery kept).What remains: 97.2% vs 99.5%
Live gpt-6.2-sol against main
712db11dfc9e, the reporter's five-tool-call turn. Two runs per arm on2ca1ebe03be6plus one per arm ona286631217b9(the stale-section cold resume does not touch an unedited turn; earlier heads had three runs per arm with the same results). Warm-request cache hit and per-requestinput/cachedcome from the Codex rollout:appServer.command(no relay)The remaining ~420 tokens per warm request are the relay's per-turn conversation instructions and tool bindings, which still go in top-level
instructionsand are never cached on these models. They are per-turn facts, so moving them is a separate change (for example to turnadditionalContext); this PR does not move them.Earlier attempt
#141270 (@mbelinky) aimed at the same carrier: workspace persona and memory on the thread developer instructions. This PR is a smaller change at the relay/lifecycle owners and does not build on it.
Deliberate decision reversed
This reverses #142276's parent-only delivery for shared text: native Codex subagents now inherit shared persona, skills and memory guidance through
developer_instructions(codex-rs/core/src/agent/child_config.rsinherited_developer_instructions). Reasoning: that is already the behavior of every connection without a relay (#162156 called it intentional); native delegation is off wherever session tools are denied; the original #142276 problem (catalog collaboration text replacing caller instructions) is solved by thread developer instructions; and keeping the split costs every managed user about 2.2x the uncached input on a five-tool-call turn (51.8k vs 24.0k tokens; about 11x per warm request). The personal overlay stays parent-only (#162156 privacy boundary kept). A child created after an in-place edit inherits the thread's creation-time section, the same limitation incognito threads already document.Evidence
Proof comments: #206554 (comment) (cache and request bodies), #206554 (comment) (background-work P1), #206554 (comment) (
2ca1ebe03be6: scripted lane, live cache, live managed P1), #206554 (comment) (a286631217b9: stale-section cold resume, compaction and self-heal regressions, live P1 on both routes, cache), #206554 (comment) (76341cb30f58: managed relay fallback regression, live managed P1). Child-inheritance decision: #206554 (comment)run-attempt.skills.native.test.ts"keeps managed skills in the cached native prefix across a multi-request turn" is red on main (skills ininstructionson every frame) and green here (marker once in adeveloperinput item, each frame's input extends the previous one byte-for-byte).run-attempt.native-background-outcome.test.ts"refreshes edited skills on a protected thread, then cold-resumes once it ends" (managed relay andappServer.command): the protected edit arrives as one handoff with no refusal, and after the listener ends the next turn unsubscribes and cold-resumes with the edited catalog. Red on8fd3970192d6(refusal) and on2ca1ebe03be6(no self-heal); green here.run-attempt.skills.native.test.ts"keeps a withdrawn catalog withdrawn after native compaction rebuilds the persistent thread" (real Codex binary, pre-sampling auto-compaction): the continuation request after compaction no longer carries the withdrawn creation catalog. Red on2ca1ebe03be6, green here; the incognito case keeps its documented one-request reversion.openclaw agent, gpt-6.2-sol, a native-shellsleeplistener started on turn 1, a skill installed withopenclaw skills install, then turn 2: previous head refuses on both routes; this PR answers with the new skill on both, through a warm refresh handoff, with turn 2's first request cached up to the previous input. After a Gateway restart, the cold resume carries the current section.Merge danger
Two-way door: no stored state, schema or config changes; a revert restores the previous layout. On first turn after upgrade each managed thread gets one cold resume (Gateway restart) that installs the section, as before. Restricted threads on connections without a relay rotate once after upgrade because their stored fingerprint included the section; afterwards edits cold-resume them instead of rotating them. Blast radius: Codex harness thread lifecycle and relay instructions.