Repository navigation
fix(gateway): the turn after a subagent completion misses the prompt cache - #181673
Conversation
The Gateway agent turn skipped persisting the user turn of a subagent completion handoff, while the turn's hidden runtime context was still saved after it. The next request then replayed a different history and lost the prompt cache. Persist the turn through the existing path with its inter_session provenance and display: false, as the steering path already does, and remove the unused helper.
|
🦞👀 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 changesPersist subagent completion prompts as hidden transcript turns so subsequent model requests preserve their history prefix. Example: An idle parent receives a subagent completion, then its owner sends another message.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review Keep open for landing: this PR fixes a remaining history-prefix defect, and Peter’s explicit approval resolves the previous review’s retention-policy blocker. No actionable patch defects remain. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe Gateway agent-turn owner admits completion input into the existing transcript recorder; model replay consumes the stored turn, while chat projection hides internal completion messages. flowchart LR
A[Subagent completion] --> B[Gateway agent turn]
B --> C[Existing transcript recorder]
C --> D[Hidden transcript turn]
D --> E[Model history replay]
D --> F[Chat display filter]
Technical reviewBest possible solution: Land the approved suppression removal through the existing recorder, preserving hidden display and explicit caller suppression. Do we have a high-confidence way to reproduce the issue? Current-main source still suppresses the completion user turn, matching the supplied before-fix Gateway trace; no target code was executed during this read-only review. Is this the best way to solve the issue? This is the smallest complete fix: remove the special suppression, reuse existing hidden-input persistence, and retain one regression that reads the stored transcript. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 4846765d3e20. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource -25, Tests -6. Total -31 across 4 files. Added test files: 0. View PR surface stats
Review metrics
Root-cause clusterRelationship: Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. 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 HistoryReviewed October 26, 2026, 12:17 AM ET / 04:17 UTC (Revision 3). |
…nscript Replace the mocked agentCommand call-shape assertion with a regression at the user-turn owner: a subagent completion handoff prepared through prepareAgentRunUserTurn is appended to the session transcript as a hidden inter_session user row.
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Maintainer decision (Peter, 2026-10-25): approved. Ordinary subagent-completion prompts may be retained as hidden ( |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Merged via squash.
|
…cache (openclaw#181673) * fix(gateway): keep subagent completion prompts in model history The Gateway agent turn skipped persisting the user turn of a subagent completion handoff, while the turn's hidden runtime context was still saved after it. The next request then replayed a different history and lost the prompt cache. Persist the turn through the existing path with its inter_session provenance and display: false, as the steering path already does, and remove the unused helper. * test(gateway): prove subagent completion prompts reach the stored transcript Replace the mocked agentCommand call-shape assertion with a regression at the user-turn owner: a subagent completion handoff prepared through prepareAgentRunUserTurn is appended to the session transcript as a hidden inter_session user row.
…ielding (#314) A parent agent that spawns sub-agents and calls sessions_yield is idle while the children run. When a child finishes, the gateway agent method runs a parent turn with the child result. That turn did not save its prompt to the parent transcript. The model saw the result once, in that turn only. On the next wake the result was gone, so the parent waited for children that had already finished and never sent its final answer. The suppression came from upstream "fix: hide subagent announce handoff prompts" (openclaw#79618). Its goal was to hide the handoff prompt in chat history. The chat display projection already hides user rows with subagent_announce provenance, so dropping the transcript row is not needed for that goal. Upstream removed the same suppression in openclaw#181673. Remove the automatic suppression for sub-agent completion handoffs. The completion turn now saves a user row with inter_session / subagent_announce provenance that carries the child result. Callers that pass suppressPromptPersistence explicitly keep their behavior. Trade-off: when a completion delivery stays pending, for example in a session with no deliverable channel, the agent steering queue replays it on each wake, and a sessions_yield abort returns the leased items to the queue. That replay is now saved in each wake row too. A TODO marks the release on the yield abort; fixing it changes the steering lease rules.
Closes #181672
Related: #150081
Related: #151962
What Problem This Solves
Fixes: when a subagent completes while its requester is idle, the completion turn's user message reaches the model but is never saved, so the next owner turn replays a different history and misses the prompt cache from that point.
User Impact
User impact: after a subagent completion, the next turn reuses the cached prefix through the completion turn, and the stored history keeps the completion turn whole instead of a runtime-context message with no turn before it. Chat history does not change: the completion's user message stays hidden.
Why This Change Was Made
The Gateway
agentmethod suppressed prompt persistence for subagent completion handoffs (shouldSuppressAgentPromptPersistence). #79618 added it so the handoff prompt ([Inter-session message] … sourceTool=subagent_announce) would neither show in WebChat andchat.historynor be written to the raw transcript. This PR deliberately brings back the transcript row and keeps the display part: the row is saved withdisplay: falseand itsinter_session/subagent_announceprovenance, chat display hides such rows by provenance, and the older row filter from that PR stays. Since then the runtime context travels in a separate hidden carrier saved right after this turn, and the transcript hygiene rules (#137273) keep hidden internal input in model history so replay keeps the request prefix. Without the user turn, the saved carrier has no turn before it.The completion turn now takes the existing persistence path that subagent settle wakes (
subagent_settle) and private completions already use: the user turn is saved withdisplay: falseand its provenance, and, like those turns, it is durably admitted before it runs. The unused helper is removed.Evidence
Tested at
e1201d7891e(baseupstream/main53f8d4f7f57). The Gateway runs below are at1bab55aad8d; the later commit changes tests only.Real Gateway: the built Gateway on each side, in an isolated HOME and state directory with the network off and a synthetic token, a local recording OpenAI-compatible Chat Completions provider, and a local IRC server. The owner sends 9 direct messages; turns 4 and 7 make the agent spawn a subagent, and each completion runs a parent turn through the Gateway
agentmethod. Each parent request's message history (system prompt ignored, subagent requests skipped) is compared with the previous parent request's.msg[i]indexes the request'smessages, counting the system message as 0;lostTokis the history not reused, estimated as characters / 4.Before (base
53f8d4f7f57):After (this PR):
Observed result: on the base, request 8 continues with the completion's runtime context at
msg[15]because the completion's user message was never stored (same at request 14); 2 of the 12 parent requests that follow another miss the prefix. With this PR the stored transcript has a hidden user row (display: false,inter_session/subagent_announce) before the runtime-context entry, and all 12 extend the previous request.chat.historyfor the session after the run returns 28 messages on both sides with the same role and content for each (ids, including child session keys, and timestamps normalized; metadata such as sequence numbers and usage not compared); neither shows the completion's user message.With #180608 merged locally on top, all 12 parent requests that follow another also keep the system prompt and tools unchanged, so the whole request extends the previous one. This PR does not depend on it.
Regression test:
src/gateway/agent-turn/agent-run-settle-continuation.native.test.tsprepares a subagent completion handoff (subagent_announceprovenance,task_completionevent) throughprepareAgentRunUserTurnwith the real transcript recorder and session store, persists it, and reads the stored transcript: one user row withdisplay: falseand the handoff's provenance. On the base it fails (expected prepared recorder to be defined: the prompt is suppressed, so no recorder exists); with this PR it passes. The other 16 tests in the file pass on both. It replaces the test from #79618 inagent.media-and-routing.test-utils.ts, which asserted the suppression on the mocked command call; its remaining checks (model state preserved for internal callers, the inter-session message header) are covered by sibling tests there.Checks:
pnpm test <file> --maxWorkers=1for both changed test files, thesrc/gateway/agent-turntests and the Gateway suites that coversubagent_announcesessions, history and display;tsgocore and the gateway-methods and gateway-other test projects;oxfmt --checkandoxlinton the changed files. The broad suites run in this PR's CI. The redauto-responsecheck is the Barnacle workflow failing withTypeError: Cannot read properties of undefined (reading 'owner')inscripts/github/barnacle-auto-response.mjs; the same failure appears on unrelated PRs and issues at the same time (for example https://github.com/openclaw/openclaw/actions/runs/39968677427), so it is not caused by this change.--maxWorkers=1wallsrc/gateway/agent-turn/agent-run-settle-continuation.native.test.tschecks-node-changed-compact-large-15(job)src/gateway/server-methods/agent.test.ts(loadsagent.media-and-routing.test-utils.ts)Simplify pass: removes
shouldSuppressAgentPromptPersistenceand its three imports; the suppression requested explicitly by callers stays. Against the merge base: production +2/−27, tests +42/−48 (one test moved to the owner suite, no new test file).Not covered: real provider cache counters (the local provider reports none; the claim is about request prefixes), CLI-backend requesters, restart recovery of a completion turn, the live Control UI view during the turn (only
chat.historyafter the run was compared), and private completions (unchanged; they already saved their turn).