Repository navigation
fix(agents): pin the session tool surface across turns - #176167
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. |
|
Codex review: needs maintainer review before merge. What this changesThis PR persists session tool definitions across turns while preserving current execution restrictions and hosted-search policy. Example: A background exec finishes and triggers another turn in the same session.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness✅ Ready for maintainer review This PR fixes the remaining session-event tool-prefix churn without restoring revoked executors. No actionable patch defect remains, and the maintainer’s documented inherited-CI override resolves the previous review’s outstanding request. Priority: P2 Before mergeNone. FindingsNone. Agent review detailsHow this fits togetherThe embedded runner combines current tool executors with transcript-backed session definitions, then passes the resulting surface through tool adapters to provider requests. flowchart TD
A[Current conversation policy] --> B[Current tool assembly]
B --> C[Session tool pin]
D[Transcript checkpoint] --> C
C --> D
C --> E[Provider request]
E --> F[Current executor or refusal]
Technical reviewBest possible solution: Keep the tool pin in the existing transcript-backed prompt owner, with current executors, explicit refusals, and policy-driven invalidation. Do we have a high-confidence way to reproduce the issue? Current main rebuilds the event tool surface from a narrower allowlist, and the supplied real-provider before/after results demonstrate the resulting cache miss; this review executed no target code. Is this the best way to solve the issue? The change follows the established stable-prefix contract and reuses both the prompt checkpoint owner and execution gate, with no unnecessary parallel policy owner. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 9463f49a6d24. Provenance checked
TestingProof path: shipped entry point. SecurityNone. EvidenceWhat I checked:
Likely related people:
PR surfaceSource +209, Tests +318, Other -1. Total +526 across 16 files. Added test files: 0. View PR surface stats
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 HistoryReview history (9 earlier review cycles; latest 8 shown)
Reviewed October 19, 2026, 1:05 AM ET / 05:05 UTC (Revision 10). |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Maintainer override: the red checks on this head come from
CI run: https://github.com/openclaw/openclaw/actions/runs/39192319951. Every other required check passes. |
|
@clawsweeper re-review |
|
🦞👀 Re-review progress:
|
|
Landed as 1f7b2f1.
|
What Problem This Solves
Fixes: the provider prompt cache misses completely on session-event turns, for example the exec-exit notification after a background
exec. On real OpenAI that turn read 0 cached tokens, and the next turn missed part of its prefix again.The tool block changed on that turn in three ways. Each one alone breaks the cached prefix:
automations,conversations_*).exec,processandsessions_sendchange, because they name sibling tools (applyToolAvailabilityDescriptions) and those siblings are missing.web_searchdrops out of the request. The saved event allowlist holds only function tools, so the hosted tool's runtime gate saw noweb_searchentry.Why This Change Was Made
#167698 already pins the system prompt per session in the transcript-backed prompt series. This extends the same owner to the tool block:
prepareSessionToolSurface,session-prompt-state.ts). A turn that builds fewer tools still sends the pinned definitions. A call to a tool this turn did not build is refused throughgateAgentToolExecution(fix: prompt cache misses on completion, heartbeat and non-owner turns #175309), so authority is unchanged.tools, the agent'stools) changes, so configured removals leave the request. Only a digest of that config is stored.openclaw.system-promptcheckpoint entry. Routes without in-history prompt updates write a tools-only checkpoint. No schema change.web_search. The saved session-event allowlist keeps it when the producing turn's full conversation policy allowed it (isConversationToolAllowed: profile, group, sender, sandbox, subagent and runtime caps). An event turn cannot gainweb_searchthat its producer did not have.OPENCLAW_PROMPT_CACHE_ASSERT=1. It now also throws on tool-block changes within a session, except a pin change the pin declared, and names the turn (trigger, provenance, reply mode).User Impact
Background and event turns reuse the cached prefix. No change to which tools a turn may run.
Evidence
Real OpenAI (
gpt-6-sol, direct toapi.openai.com) through the Gateway and a Telegram mock-user scenario: two user turns, image generation, a subagent spawn, a backgroundexec, a system event and a closing user turn. Cached share of prompt tokens per request, from the Gateway's usage records:The first request of each session and each new subagent is cold (0%) on both sides.
Hosted-search authority at the final provider request (same real-OpenAI setup, request tool lists from
OPENCLAW_DEBUG_MODEL_PAYLOAD=tools). The producing turn starts a backgroundexecthat sleeps 90 s. While it sleeps, the Gateway config getstools.deny: ["web_search"](hot reload):web_searchexeccall, before the denyA producer that sender policy denies search (
tools.toolsBySender: {"*": {deny: ["web_search"]}}) sends 51 tools without hostedweb_searchon every request. That includes its exec-exit event, which reads 93.5% cached.The queued event does not keep hosted search after a revocation. The policy change also starts a new pin, which costs one cache miss, as designed. The allowed case is the exec-exit row in the cache table above: 52 tools,
web_searchsent.Unit tests (
session-prompt-state.test.tsand others):A background turn keeps the user turn's definitions and refuses a missing tool.
The pin survives a restart, a prompt-series restart during admission and a system-prompt-only checkpoint.
A route or policy change starts a new pin.
A sibling-only description change keeps the pinned text.
A permission refresh uses the new executors.
The assert admits one declared pin change.
The session-event allowlist keeps hosted
web_search.Compaction retires the pin instead of carrying it into the new history.
A checkpoint written before this change (no
toolskey) still restores the system-prompt series, and a new pin starts from it.Hosted search: a producer whose policy denies
web_searchsaves noweb_searchentry. A saved entry still yields to the current policy when the event turn sends its request (resolveWebSearchToolPolicywithtools.deny).Each test for a fix fails without that fix. The pre-change checkpoint test and the two hosted-search policy tests cover behavior that already holds.
E2E: compaction hot-reload passes on the final head. On the previous head, background-exec restart, session resume restart, restored-requester settle, heartbeat session routing, subagent current-requester and the SQLite flip proof all pass (14/14). Earlier commits on this branch also passed the compaction hot-reload, image-generation, silent-reply, chat-tools and approval suites. One test expectation changed (
gateway-session-resume-restart): a staleexeccall after recovery restricted the session is still refused and its effect still never happens. The refusal now comes from the pinned gate, so the wording differs.Upgrade:
pnpm test:docker:published-upgrade-survivorfrom the currentlatest([email protected]) passes on this head (881 s, no failures). An earlier run from[email protected]passed as well (915 s). The lane installs the published release with seeded config, sessions and transcripts, then updates in place to this branch. The Gateway, config and stored state survive.Harness fix: the SQLite flip-proof helper now skips compressed (
event_zstd) transcript rows, which the larger checkpoint entry produces.Merge danger
Two-way door for behavior: a revert stops pinning, and older code ignores the
toolsfield in checkpoint entries. Blast radius: the tool block of every embedded-runner request, and the allowlist stored with background exec and session events.