Repository navigation
fix(agents): keep the session system prompt stable across completion wakes - #197835
Conversation
…wakes An exec or child completion wake runs as a host system turn with its producer's tool ceiling in toolsAllow. Any explicit toolsAllow switched the prompt to minimal mode, so the wake rebuilt a different system prompt for the same session (no execution bias, memory, docs, routing or output sections). The provider then missed the cache right after the tool block and rewrote the whole conversation on every user/wake alternation. A host system turn now keeps the session's prompt mode; its ceiling still gates calls. Run-granted core tools (skill_library) exist only on turns whose requester granted them, so the cached deferred-tool directory omits them and the granting turn lists them in its runtime context instead. Co-authored-by: obviyus <[email protected]>
|
🦞👀 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. |
|
Codex review: needs maintainer review before merge. What this changesThis PR preserves session prompt mode during completion wakes and moves run-granted deferred-tool discovery into the current turn’s context. Example: An Anthropic-backed conversation resumes after a 10-second background exec completes.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open: this PR addresses a current prompt-cache defect with useful live evidence and no actionable findings; exact-head CI remains pending. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherGateway-local and worker turns feed session facts, tool catalogs, and current-turn instructions into shared prompt preparation, which produces the system prompt and runtime context sent to the model provider. flowchart TD
A[User turn or completion wake] --> B[Session and tool preparation]
B --> C[Shared system prompt preparation]
B --> D[Worker tool projection]
D --> C
C --> E[Stable system prompt]
C --> F[Current turn runtime context]
E --> G[Model provider request]
F --> G
Technical reviewBest possible solution: Keep prompt presentation session-stable through the existing preparation owners while retaining current-turn tool gates and grant discovery. Do we have a high-confidence way to reproduce the issue? Current-main source shows the finite producer ceiling selecting minimal mode, and the contributor supplies a matching live before/after Gateway trace; this review ran no reproduction. Is this the best way to solve the issue? The patch uses the existing host-turn predicate, directory renderer, runtime-context carrier, and worker projection without adding persistence or competing authority. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 74999886c2f9. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +74, Tests +65. Total +139 across 9 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. Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. LabelsLabel changes: No label 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 (2 earlier review cycles)
Reviewed November 12, 2026, 12:57 PM ET / 17:57 UTC (Revision 3). |
… collisions Remote worker turns render the deferred directory from a projection whose catalog is disposed before prompt preparation, so the run-granted listing that replaces the cached directory row was never emitted. The worker projection now renders the listing before cleanup and carries it into prompt preparation, including refresh. When another catalog source shares a run-granted tool's name, or the entry requires an exact id, the listing now names the exact catalog id instead of a bare name that tool lookup rejects as ambiguous. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Keep this PR to the host-system-turn prompt-mode fix. Moving run-granted tools out of the cached deferred directory needs worker projection and name-collision handling; it returns as a separate change. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
…on fixes Reinstate the run-granted tool listing, with remote worker projection carrying the per-turn listing and colliding names listed by exact catalog id. Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
Co-authored-by: obviyus <[email protected]>
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
…wakes (openclaw#197835) Completion wakes kept their producer's tool ceiling and forced the minimal prompt, so one session alternated between two system prompts and lost its Anthropic prompt cache on every wake. Host system turns now keep the session prompt mode; their tool ceiling still gates calls. Run-granted tools such as skill_library move out of the cached deferred-tool directory into the granted turn's own context, including remote worker turns, with exact catalog IDs when names collide. Real route (cacheRetention short): 2 system prompts -> 1 per session; first wake cache write 16,379 -> 987; session cache writes 39.5k -> 21.8k. Co-authored-by: obviyus <[email protected]>
…wakes (#197835) Completion wakes kept their producer's tool ceiling and forced the minimal prompt, so one session alternated between two system prompts and lost its Anthropic prompt cache on every wake. Host system turns now keep the session prompt mode; their tool ceiling still gates calls. Run-granted tools such as skill_library move out of the cached deferred-tool directory into the granted turn's own context, including remote worker turns, with exact catalog IDs when names collide. Real route (cacheRetention short): 2 system prompts -> 1 per session; first wake cache write 16,379 -> 987; session cache writes 39.5k -> 21.8k. Co-authored-by: obviyus <[email protected]> (cherry picked from commit a3d5baa) Co-authored-by: RomneyDa <[email protected]> Backport-adaptation: Minimal runGranted fact/resolver from 3c8129e and prompt-only host-event classifier from 8100c60. No skill_library feature or cron authority migration. Group fixtures adapted with subsequent 197893.
Problem
The system prompt of one session changes between turns, depending on what started the turn. Anthropic prompt caching keys on an exact prefix, so each change discards the cache for the whole conversation. Long sessions (several hundred thousand tokens) then re-write the full prefix at cache-write price.
On a production gateway over 3 days (
anthropic-messages,cacheRetention: short), the existing[prompt-cache] cache read droppeddetector attributed most breaks tosystemPrompt/systemPromptSuffixdigest changes. The largest group wastrigger=event provenance=internal_system:exec(background exec completion wakes): the stable prefix and almost every suffix section changed at once.Root cause
toolsAllow(session-event-target.ts,session-event-handoff.ts).prepareEmbeddedAttemptSystemPrompttreats any restrictivetoolsAllowas a narrowly scoped run and forcespromptMode: "minimal". A wake therefore renders a different prompt for the same session: no Execution Bias, Bootstrap/Project Context changes, no Memory Recall, Documentation, Session routing, Output Directives, UI Presentation or messaging guidance. The tool definitions were already kept stable for these turns (resolveTurnToolSurface); only the prompt mode was not.skill_libraryis a run-granted core tool: it exists only on turns whose requester granted it. A system-started wake of the same session does not have it, so the "Available deferred-schema tools" list inside the Tooling section differed by one row between user turns and wakes.Fix
trigger=eventwithinternal_systemprovenance, the existingisHostSystemTurn) keeps the session's prompt mode. Its tool ceiling still gates calls exactly as before. Narrowly scoped runs that are not host wakes (crontoolsAllow, plugin commands, etc.) keep the minimal prompt.openclaw:core:skill_library), sotool_describe/dispatch_actionresolve it.Proof
Real route, before vs after. Isolated throwaway Gateway (scratch state, no channels), real Anthropic-compatible provider route,
cacheRetention: short, the same configured scratch workspace for both arms. A logging proxy recorded the provider's owncache_read_input_tokens/cache_creation_input_tokensand the request system-prompt digest. One session per arm: user turn that starts a 10 s background exec → exec completion wake → user turn → wake → final user turn.cache read dropped 29595 -> 12157 … trigger=event provenance=internal_system:execwith the same section signature seen in production.[prompt-cache]drop warnings.The cost saving at production scale is an extrapolation from this mechanism. I measured it only on this short probe session, not on a production deployment.
Focused tests:
attempt-system-prompt.test.ts: a completion wake with an inherited tool ceiling renders the user-turn prompt byte for byte. A non-wake restricted run still renders the minimal prompt.tool-search-directory.test.ts: the cached directory is identical with or without the run grant, and the grant is listed separately. With a colliding client tool, the listing gives an exact id thatToolSearchRuntime.describeresolves.worker-gateway-tool-runtime.test.ts: the worker projection leavesskill_libraryout of the cached directory and returns it in the per-turn listing.The real-route numbers above were measured before the worker and collision fixes. Those fixes change no prompt bytes on the measured embedded path.
Not covered (separate follow-ups)
systemPromptSuffixchanges (Tooling / Session routing / Skills) oninter_sessionuser turns (sessions_send, subagent settle). The Tooling part matches the run-granted directory row fixed here. The Session routing and Skills differences were not reproduced or measured in this PR.historyRewrite: message N (assistant) differs … content.length, content[k].type/id/name/arguments). An earlier assistant message gains or changes tool-call blocks after it was already sent. This is often together withreplayed thinking dropped … prefix_binding_mismatch, and sometimes with changedenvelope.api/provider/model(a fallback route). This change does not address it.