mirror of
https://github.com/openclaw/openclaw.git
synced 2026-10-03 17:53:39 +00:00
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> |
||
|---|---|---|
| .. | ||
| database-schemas | ||
| full-release-validation | ||
| session-management-compaction | ||
| templates | ||
| test | ||
| AGENTS.default.md | ||
| api-usage-costs.md | ||
| credits.md | ||
| database-schemas.md | ||
| device-models.md | ||
| full-release-validation.md | ||
| memory-config.md | ||
| openclaw-ai.md | ||
| prompt-caching.md | ||
| pull-request-review-flow.md | ||
| release-performance-sweep.md | ||
| RELEASING.md | ||
| rich-output-protocol.md | ||
| rpc.md | ||
| secret-placeholder-conventions.md | ||
| secretref-credential-surface.md | ||
| secretref-user-supplied-credentials-matrix.json | ||
| session-management-compaction.md | ||
| test.md | ||
| token-use.md | ||
| transcript-hygiene.md | ||