Repository navigation
Description
Bug type
Performance regression — prompt cache hit rate degradation
Summary
In long conversational sessions, DeepSeek KV cache hit rate alternates between ~20% and ~99% on consecutive API calls within the same user message turn. This wastes 15M input tokens per long session ($27 at DeepSeek pricing).
Root cause traced to CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES (SOUL.md, IDENTITY.md, USER.md) being injected as developer instructions differently between full-turn calls and tool-continuation calls, creating two different message prefixes that prevent cache reuse.
Affected versions
- First observed: 2026.6.5
- Still present: 2026.6.10 (latest)
Mechanism (code trace)
In run-attempt-*.js:
// Line 211-215: These files are marked as turn-scoped
const CODEX_TURN_SCOPED_WORKSPACE_DEVELOPER_CONTEXT_BASENAMES = new Set([
"identity.md",
"soul.md",
"user.md"
]);
// Line 8285-8291: Turn collaboration developer instructions differ between calls
const buildCodexTurnCollaborationDeveloperInstructions = () =>
buildTurnCollaborationMode(params, {
turnScopedDeveloperInstructions: workspaceBootstrapContext.turnScopedDeveloperInstructions,
...
}).settings.developer_instructions ?? void 0;
const buildRenderedCodexDeveloperInstructions = () =>
joinPresentSections(promptBuild.developerInstructions,
buildCodexTurnCollaborationDeveloperInstructions());buildTurnCollaborationMode() returns different developer instructions for full-turn calls vs tool-continuation calls. These instructions are injected at/near the front of the messages array, so the two call types produce different prefixes → DeepSeek KV cache cannot serve the conversation history on full-turn calls.
Observed pattern
Every consecutive API call within the same user message alternates:
Turn N (full call): hit=20.0% in=108k cache=27k ⚠️ only system prompt cached
Turn N+1 (tool continuation): hit=99.9% in=132 cache=138k ✅ full history cached
Turn N+2 (full call): hit=19.0% in=118k cache=27k ⚠️ prefix mismatch again
Turn N+3 (tool continuation): hit=99.9% in=26 cache=149k ✅
...repeats for 200+ turns
Impact
- Session-wide cache hit rate: 51% (should be 90%+)
- Wasted tokens: ~15M input tokens per long session
- Cost: ~$27 per session at DeepSeek rates
- Latency: full context re-computation on every user message
Reproduction
- OpenClaw 2026.6.x with deepseek-v4-pro
- Have SOUL.md, IDENTITY.md, USER.md in workspace
- Start a conversation and let it run for 50+ turns with tool calls
- Observe usage.cacheRead alternating between near-zero and near-total on consecutive API calls
Session evidence
Full turn-by-turn analysis performed on session bb84ac8e (340 turns). Data available at ~/.openclaw/agents/main/sessions/.
Related issues
- [Bug]: Codex app-server rewrites prompt on tool-call continuation turns, busting OpenAI prompt cache mid-turn (cache ratio 93% → 47%) #84110 — Same pattern (prompt rewrite on tool-continuation turns) but for Codex app-server
- [Bug]: 2026.6.9 re-busts prompt prefix caching — per-run :run:<UUID> injected into the system-prompt "Runtime:" line (regression of #43148) #96677 — Per-run UUID injection breaking prefix cache (different mechanism, same symptom class)
- DeepSeek cache hit rate <10% after 6.x upgrade - boundary-aware caching breaks prefix matching #94518 — DeepSeek cache hit rate <10% after 6.x (likely umbrella for multiple mechanisms)
- [Bug]: Active memory injection breaks prompt cache hit rate (99.9% → 22%) #91223 — Active memory injection breaks prompt cache hit rate
- Prompt-cache prefix churn on OpenAI models: per-turn dynamic injections (message tool hint + volatile system-prompt section) defeat automatic prefix caching #95610 — Per-turn dynamic injections defeating prefix caching
Suggested fix
Make buildTurnCollaborationMode return byte-identical developer instructions for full-turn and tool-continuation calls within the same user turn, OR move turn-scoped instructions to a position in the messages array that does not affect prefix matching (e.g., after the conversation history instead of before it).
Metadata
Metadata
Assignees
Labels
Type
Fields
Priority
Activity
clawsweeper commentedon Jun 25, 2026
Codex review: keeping this open for maintainer follow-up; there is still a little grit to resolve. Reviewed June 25, 2026, 8:31 AM ET / 12:31 UTC.
Summary
Keep open: current main and v2026.6.10 still contain the reported Codex app-server path that moves SOUL.md, IDENTITY.md, and USER.md into turn-scoped collaboration developer instructions, and the related DeepSeek prompt-cache trackers do not cover this exact Codex turn-scoped mechanism.
Reproducibility: Do we have a high-confidence way to reproduce the issue? Not fully; current source reproduces the claimed placement of SOUL.md, IDENTITY.md, and USER.md into Codex turn collaboration developer instructions, but the specific DeepSeek cache oscillation still needs live or redacted session-log proof.
Ways to help us reproduce this
Next step
The source-level mechanism is plausible, but the fix requires maintainer judgment about Codex collaboration-mode semantics and live DeepSeek cache validation.
Review details
Best possible solution:
Keep this as the canonical tracker for the turn-scoped Codex workspace-instruction cache mechanism until maintainers either stabilize the model-visible prefix with live provider proof or explicitly fold this mechanism into a broader DeepSeek cache fix.
Do we have a high-confidence way to reproduce the issue?
Do we have a high-confidence way to reproduce the issue? Not fully; current source reproduces the claimed placement of SOUL.md, IDENTITY.md, and USER.md into Codex turn collaboration developer instructions, but the specific DeepSeek cache oscillation still needs live or redacted session-log proof.
Is this the best way to solve the issue?
Is this the best way to solve the issue? Unclear; byte-stabilizing Codex's model-visible prefix is the right target, but moving developer instructions after history may conflict with the Codex collaboration-mode contract.
AGENTS.md: found and applied where relevant.
Remaining risk / open question:
Codex review notes: model internal, reasoning high; reviewed against 4ecb45bf7729.
Label changes
Label changes:
P2: The report describes a costly provider prompt-cache regression with a plausible source path but without a live current-main reproduction in this review.impact:session-state: The affected surface is model-visible turn context and collaboration developer instruction state across Codex turns.impact:auth-provider: The user-visible impact is provider/model prompt-cache behavior on DeepSeek-compatible routing.issue-rating: 🐚 platinum hermit: Current issue advisory state selects this label.clawsweeper:needs-live-repro: Current issue advisory state selects this label.clawsweeper:no-new-fix-pr: Current issue advisory state selects this label.clawsweeper:needs-maintainer-review: Current issue advisory state selects this label.clawsweeper:needs-product-decision: Current issue advisory state selects this label.Label justifications:
P2: The report describes a costly provider prompt-cache regression with a plausible source path but without a live current-main reproduction in this review.impact:session-state: The affected surface is model-visible turn context and collaboration developer instruction state across Codex turns.impact:auth-provider: The user-visible impact is provider/model prompt-cache behavior on DeepSeek-compatible routing.Evidence reviewed
Acceptance criteria:
What I checked:
extensions/codex/src/app-server/attempt-context.ts:34, 4ecb45bf7729)extensions/codex/src/app-server/run-attempt.ts:2444, 4ecb45bf7729)extensions/codex/src/app-server/run-attempt.test.ts:2738, 4ecb45bf7729)extensions/codex/src/app-server/attempt-context.ts:34, aa69b12d0086)../codex/codex-rs/docs/codex_mcp_interface.md:87, c38b2e9ba69c)../codex/codex-rs/core/src/context_manager/updates.rs:56, c38b2e9ba69c)Likely related people:
extensions/codex/src/app-server/attempt-context.ts,extensions/codex/src/app-server/run-attempt.ts)extensions/codex/src/app-server/attempt-context.ts,extensions/codex/src/app-server/thread-lifecycle.ts,extensions/codex/src/app-server/run-attempt.ts)extensions/codex/src/app-server/attempt-context.ts)extensions/codex/src/app-server/run-attempt.ts,extensions/codex/src/app-server/thread-lifecycle.ts)How this review workflow works
@clawsweeper re-reviewcomments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.@clawsweeper re-reviewor@clawsweeper re-runon an open PR or issue to request a fresh review only.@clawsweeper reviewto request a fresh review only.@clawsweeper autofix,@clawsweeper automerge,@clawsweeper fix ci, or@clawsweeper address review.@clawsweeper explainto ask for more context, or@clawsweeper stopto stop active automation.openclaw-barnacle commentedon Jul 10, 2026
This issue has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.
clawsweeper commentedon Jul 10, 2026
ClawSweeper status: review started.
I am starting a fresh review of this issue: [Bug]: Turn-scoped developer instructions (SOUL.md/IDENTITY.md/USER.md) create different message prefixes between full-turn and tool-continuation calls, breaking DeepSeek KV cache (20%/99% oscillation) This is item 1/1 in the current shard. Shard 16/32.
This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.
Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.
openclaw-barnacle commentedon Jul 18, 2026
Closing due to inactivity.
If this is still an issue, please retry on the latest OpenClaw release and share updated details.
If you are absolutely sure it still happens on the latest release, open a new issue with fresh steps to reproduce.