Skip to content

fix(agents): keep the system prompt stable when task suggestions are not granted - #197893

Merged
obviyus merged 1 commit into
mainfrom
fix/prompt-cache-task-suggestion-grant
Nov 13, 2026
Merged

obviyus merged 1 commit into
mainfrom
fix/prompt-cache-task-suggestion-grant

Conversation

@obviyus

@obviyus obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

A Control UI (or TUI) session renders one system prompt for the user's own turns and a different one for turns that arrive from elsewhere: sessions_send from another session, subagent settle, cron, wakes. Every alternation re-writes the whole conversation at Anthropic cache-write price.

On a production gateway the [prompt-cache] detector repeatedly reported systemPromptSuffix changes in the Tooling and Session routing sections on trigger=user provenance=inter_session:* turns. A fix that merged earlier for completion wakes (run-granted skill_library) covered part of the Tooling difference; it did not cover this one.

Root cause

suggest_task and dismiss_task are offered only when the turn's requesting client is an operator UI with operator.admin and the task-suggestions capability:

  • src/gateway/server-methods/chat-send-request.ts (supportsTaskSuggestions) and src/gateway/agent-turn/agent-run-execution-phase.ts set taskSuggestionDeliveryMode: "gateway" from the client connection.
  • src/agents/openclaw-tools.ts adds the two tools only in that mode.

A turn without that client (inter-session, cron, wake) does not have the tools. The cached system prompt changed in two places:

  1. The deferred-tool directory in Tooling lost the suggest_task / dismiss_task rows.
  2. Session routing (src/agents/delegation-guidance.ts) lost its suggest_task bullet.

Fix

Use the mechanism that already exists for run-granted tools:

  • Both tools are marked runGranted in core-tool-facts.ts, like skill_library. The cached directory omits them, and the turn that holds the grant lists them in its own runtime context ("Deferred tools granted for this turn"). The flag also stops "unavailable core tool" allowlist warnings for them, which is correct because they are absent on some turns by design.
  • Session routing no longer renders the suggest_task bullet. The tool's own description already carries that guidance (records a card only; not for explicit requests to start a session).

Which turns get the tools does not change. The Codex app-server thread prompt, which renders its own delegation section, is not changed.

Proof

Real route, before vs after. Isolated throwaway Gateway (scratch state, no channels), real Anthropic-compatible provider route, cacheRetention: short. A logging proxy recorded the provider's own cache_read_input_tokens / cache_creation_input_tokens and the system-prompt digest. User turns were sent as an operator UI client (TUI identity: operator.admin + task-suggestions, through chat.send), the same gate the Control UI passes. Target session: UI turn, UI turn, then another session's sessions_send into it, then a UI turn.

target-session request before: read / write after: read / write
UI turn 1 11,640 / 19,589 11,640 / 19,623
UI turn 2 12,785 / 19,411 12,921 / 19,445
sessions_send turn 12,785 / 20,310 32,366 / 1,097
UI turn 3 32,196 / 2,065 33,463 / 1,307
  • Before: 2 system prompts in the session. The sessions_send turn's prompt differed exactly by the two suggest_task/dismiss_task directory rows and the Session routing bullet (diffed from captured bodies), and re-wrote 20.3k tokens.
  • After: 1 system prompt for every target-session request. UI turns still list both tools in their own runtime context. The sessions_send turn does not.

Separate from this change, the second request of a fresh session (UI turn 2 above) re-wrote ~19k in both arms. I reproduced it with plain UI turns and no inter-session input, and found no byte difference in system, tools, parameters or message prefix. It happens once per session, on this route only. I did not investigate it further here.

Focused tests:

  • tool-search-directory.test.ts: the cached directory is identical with or without each run-granted tool (skill_library, suggest_task, dismiss_task). The grant is listed separately.
  • delegation-guidance.test.ts: Session routing renders the same with or without the task-suggestion grant.

Not covered

Skills-only suffix changes in production also happen on ordinary user turns (8 of 15 with no provenance). They track environment changes (skill catalog edits and the remote-node availability line inside the Skills section), not the trigger. Separate follow-up.

…not granted

suggest_task and dismiss_task are offered only when the turn's requesting client is an operator UI with the task-suggestions capability. Inter-session turns (sessions_send, subagent settle), cron and wakes in the same session have no such client, so the cached deferred-tool directory and the Session routing section changed between these turns and user turns. Each alternation re-wrote the whole conversation.

Both tools are now run-granted, like skill_library: the cached directory omits them and the granting turn lists them in its own runtime context. Session routing no longer renders a suggest_task bullet; the tool description already carries that guidance. Who receives the tools does not change.

Co-authored-by: obviyus <[email protected]>
@clawsweeper

clawsweeper Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

@obviyus

obviyus commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

@clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Nov 12, 2026
@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. labels Nov 12, 2026
@clawsweeper

clawsweeper Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Keep task-suggestion grants out of the cached system prompt and advertise them in the granting turn’s runtime context.

Example: An operator UI conversation receives a sessions_send message from another session.

  • Before: The reported probe changed system prompts and wrote 20,310 cache tokens for the inter-session turn.
  • After: The reported probe retained one system prompt and wrote 1,097 cache tokens for that turn.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with source-supported behavior and relevant real-Gateway evidence; current-head CI is still running.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The supplied provider-output table exercises operator chat.send, sessions_send delivery, and return to an operator turn through an isolated Gateway, reporting one stable system prompt after the fix and continued grant discovery only on UI turns.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Internal messages entering an operator UI conversation can invalidate its prompt cache and cause unnecessary cache-write charges.
Reason: The maintainer-authored direction restores the documented stable-prefix contract without expanding capability or adding configuration.

Merge readiness

✅ Ready for maintainer review

Keep open. This maintainer-authored PR fixes a current prompt-cache defect using existing machinery; no actionable correctness or security findings were identified.

Priority: P2
Reviewed head: 72fb253d0ec75ed48ca6d9e9838fc3e56a701345

Before merge

None.

Findings

None.

Tests

  • Missing end-to-end proof: The PR does not explicitly document base-red/head-green execution of the added regression cases.
Agent review details

How this fits together

Gateway client capabilities determine task-suggestion availability; agent prompt assembly separates stable discovery text from turn-specific grants before sending requests to the provider.

flowchart TD
  A[UI or inter-session input] --> B[Gateway capability selection]
  B --> C[Agent tool catalog]
  C --> D[Stable system prompt]
  C --> E[Turn grant context]
  D --> F[Provider request]
  E --> F
Loading

Technical review

Best possible solution:

Use the existing run-granted metadata and turn-context owner while preserving task-suggestion access and tool-local guidance.

Do we have a high-confidence way to reproduce the issue?

Current-main source shows grant-dependent directory rows and routing guidance, and the PR supplies a matching Gateway A/B reproduction; no target code was executed during this review.

Is this the best way to solve the issue?

The patch is the smallest complete repair for the demonstrated task-suggestion churn: it reuses grant handling, removes the varying embedded guidance, and preserves the tool description and generic renderer used by other callers.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 00b62dd84752.

Provenance checked

  • Run-granted discovery and its regression coverage keeps the original intent (a3d5baa: Keep completion-wake prompts stable by moving requester-granted discovery into the granting turn.)
  • Embedded Session routing suggestion guidance changes intended behavior with a stated reason (159e4a5: Distinguish independent session creation, supervised helpers, and proactive suggestion cards; the later automation refactor carried this guidance forward.)
  • Canonical core-tool metadata keeps the original intent (6ee2a38: Separate canonical tool identities and policy facts from presentation descriptions while retaining existing catalog behavior.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

PR surface

Source -1, Tests +21. Total +20 across 4 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 2 4 5 -1
Tests 2 30 9 +21
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 4 34 14 +20

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Repeated prompt-cache invalidation increases provider work and cost in otherwise functioning conversations.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

Rating scale

6/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.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 12, 2026, 3:02 PM ET / 20:02 UTC.

@obviyus
obviyus merged commit da21556 into main Nov 13, 2026
138 checks passed
@obviyus
obviyus deleted the fix/prompt-cache-task-suggestion-grant branch November 13, 2026 03:49
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 13, 2026
RomneyDa added a commit that referenced this pull request Nov 16, 2026
…not granted (#197893)

Co-authored-by: obviyus <[email protected]>
(cherry picked from commit da21556)
Co-authored-by: RomneyDa <[email protected]>
Backport-adaptation: Test existing suggest_task/dismiss_task grants; beta.4 intentionally lacks the unrelated skill_library feature.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant