openclaw/docs/reference
Ayaan Zaidi d4131afa00
fix(active-memory): recalls on claude-cli never reuse the prompt cache (#160768)
Closes #160672

## What Problem This Solves

Fixes: every Active Memory recall on the `claude-cli` runtime writes the whole OpenClaw system prompt into Claude's prompt cache again, even for two recalls of the same agent seconds apart.

## User Impact

User impact: repeated Active Memory recalls of the same agent on `claude-cli` now send a byte-identical system prompt, so the recall's first API call can read it from Claude's cache instead of paying for a 10k+ token cache write each time. Normal `claude-cli` turns are unchanged.

## Why This Change Was Made

Each recall gets a new session key (`<parent>:active-memory:<per-run hash>`) so its session entry stays unique. The Runtime line rendered that key into the system prompt, and Claude CLI receives the whole system prompt as one `appendSystemPrompt`, so the per-run key forced a full cache rewrite. The recall's session key is the only value that differed between two recalls.

One-shot CLI dispatch runs (today only Active Memory recall) now carry the Runtime facts line in their only user turn, through the same `UserPromptSubmit` context Claude CLI already uses for other per-turn facts. This reuses the existing relocatable Runtime region that Chat Completions routes already move into the first user message. The recall model still sees its agent, session, model and channel. Recall session keys, storage and cleanup are unchanged. Normal resumable CLI turns keep the Runtime line in the system prompt.

Thanks @ndakota79 for the detailed cache-counter report and cause analysis.

No overlap with Pash/Sarah changes.

## Evidence

Isolated Gateway (task-owned home, state, config, port) with agents `alpha` and `beta` on `agentRuntime: claude-cli`, Active Memory `mode: always`, and a fake `claude` executable that records the `initialize.appendSystemPrompt` and the `UserPromptSubmit` context it receives. Two `chat.send` turns on `agent:alpha:main` about 60 s apart, then one on `agent:beta:main`.

- Base (`7f0781faa67`): the two alpha recalls' system prompts differ in exactly one line:
  ```
  -Runtime: agent=alpha | session=agent:alpha:main:active-memory:e10ad4c9dc5e | ...
  +Runtime: agent=alpha | session=agent:alpha:main:active-memory:9945b3fb179b | ...
  ```
- Candidate: both alpha recalls send the same system prompt (14,210 bytes, sha256 `f7a45b89f4e84e5f…`). Each recall's user turn carries its own `Runtime: agent=alpha | session=agent:alpha:main:active-memory:<hash> | ... | channel=webchat | ...`. The candidate recall prompt equals the base recall prompt minus the Runtime line.
- Controls: the beta recall still gets its own prompt (its working directory differs) and its own `agent=beta` Runtime facts in the turn. The alpha and beta main (non-recall) turns send the same system prompt and user turn as on base, with the Runtime line still in the system prompt.
- Regression: `src/agents/cli-runner/prepare.test.ts` "keeps per-run helper session identities out of the reusable system prompt" fails on base (system prompts differ) and passes with the fix. `prepare.test.ts` (194), `cli-backend-dispatch.test.ts` (40) and `helpers.system-prompt.test.ts` (15) pass.
- Test cost: the new test runs in 136 ms. `pnpm test src/agents/cli-runner/prepare.test.ts --maxWorkers=1`: 194 passed, 83.3 s wall (Vitest 65.9 s); the same command filtered to the new test: 23.1 s wall (Vitest 5.8 s).

Not verified: a real Claude Code run. The proof checks the bytes OpenClaw sends to the CLI (the `initialize` system prompt and the `UserPromptSubmit` response); it does not show Claude Code's cache counters. The Runtime facts use the same `UserPromptSubmit` additional-context path that already carries `before_prompt_build` hook context (including Active Memory's own recalled context) on `claude-cli` turns.

Co-authored-by: Ayaan Zaidi <hi@obviy.us>
2026-09-29 04:52:53 +05:30
..
database-schemas improve: keep Gateway responsive while compaction loads history (#160314) 2026-09-28 15:53:43 -07:00
full-release-validation chore(release): retire the internal Tideclaw alpha release track (#160261) 2026-09-28 10:01:49 -07:00
session-management-compaction fix(compaction): prevent fallback from replaying previously compacted history (#160211) 2026-09-28 23:43:45 +05:30
templates improve(agents): stop agents refusing or arguing over requested work (#157447) 2026-09-24 16:16:21 -05:00
test chore(crabbox): require Crabbox 0.67.0 (#160560) 2026-09-28 11:29:35 -07:00
AGENTS.default.md improve(agents): stop agents refusing or arguing over requested work (#157447) 2026-09-24 16:16:21 -05:00
api-usage-costs.md docs: fix verified accuracy findings in channels, cli, nodes, and reference (#143950) 2026-09-10 18:58:15 +08:00
credits.md docs: fix and reciprocate cross-page links across install, help, reference, start, and web (#143773) 2026-09-10 15:35:25 +09:00
database-schemas.md fix(state): observe foreign commits on the next database read (#156824) 2026-09-24 01:15:39 +00:00
device-models.md feat(ios): render Mermaid diagrams in native chat (#135470) 2026-09-01 17:42:01 -07:00
full-release-validation.md fix(ci): preserve first failures through release validation 2026-09-23 18:27:19 -07:00
memory-config.md fix(memory): avoid unnecessary reindexing for patterned extra paths (#158998) 2026-09-27 15:13:10 +05:30
openclaw-ai.md docs: fix 12 concrete defects from the ux audit remainder (#144128) 2026-09-25 17:28:14 +08:00
prompt-caching.md fix(active-memory): recalls on claude-cli never reuse the prompt cache (#160768) 2026-09-29 04:52:53 +05:30
pull-request-review-flow.md fix(scripts): prevent host tooling drift from blocking PR workflows (#152078) 2026-09-18 14:34:18 -07:00
release-performance-sweep.md
RELEASING.md chore(release): retire the internal Tideclaw alpha release track (#160261) 2026-09-28 10:01:49 -07:00
rich-output-protocol.md fix: resend inbound attachments referenced from chat history (#158720) 2026-09-26 21:02:02 +05:30
rpc.md
secret-placeholder-conventions.md docs: fix 12 concrete defects from the ux audit remainder (#144128) 2026-09-25 17:28:14 +08:00
secretref-credential-surface.md refactor: retire the TaskFlow Webhooks plugin (#158225) 2026-09-25 20:29:15 -07:00
secretref-user-supplied-credentials-matrix.json refactor: retire the TaskFlow Webhooks plugin (#158225) 2026-09-25 20:29:15 -07:00
session-management-compaction.md docs(reference): split the session management deep dive by reader job (#143082) 2026-09-09 21:03:41 +09:00
test.md docs(reference): split the testing reference by reader job (#141221) 2026-09-09 02:15:05 +08:00
token-use.md fix(codex): restore installed skills with catalog-backed models (#140422) 2026-09-24 14:59:15 -07:00
transcript-hygiene.md feat: refresh plugin tools within active agent conversations (#145648) 2026-09-12 03:53:24 -07:00