feat: split system prompt into static/dynamic blocks with per-block cache_control#1330
Merged
Conversation
…ache_control
Split buildSystemPrompt to return SystemPromptBlock[] instead of string,
separating static content (base prompt, instructions, tool policy, tone)
from dynamic content (permission mode, language, env info, auto memory,
MEMORY.md). For Claude models, callAgent maps cacheable blocks with
cache_control: {type: 'ephemeral'} and non-cacheable blocks without it,
so dynamic content changes don't invalidate the static cache.
transformMessagesForExplicitCache idempotency check prevents double-marking.
- Add SystemPromptBlock interface to prompts/index.ts
- Widen CallAgentOptions.systemPrompt to string | SystemPromptBlock[]
- Update 9 existing tests across 3 files for array-based assertions
- Add 4 block structure tests in prompts.test.ts
- Add 3 SystemPromptBlock[] input tests in aiService.cacheControl.test.ts
- Update spec 019 with user story 5, FR-008, edge cases 7-8
- All 235 test files pass (3150 tests)
Follow the agent.noParams.test.ts pattern to avoid CI environment issues: - Add workdir: /tmp/test-system-prompt to isolate from CI filesystem - Add apiKey: test-key for proper agent initialization - Re-create AIManager after Agent.create() to pick up mock ToolManager - Register mock ConfigurationService in container - Clear mockCallAgent call history before sendMessage to ensure mock.calls[0] captures the sendMessage call, not any init call The CI test process was producing zero output (crash/hang), likely due to real MemoryService reading actual files from process.cwd() in CI.
Align with Claude Code: permission mode is handled purely at the tool execution layer (PermissionManager), not in the system prompt. This prevents dynamic block changes when mode switches, preserving prompt cache.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Split the system prompt into static/dynamic blocks with per-block
cache_control, mirroring Claude Code's approach where static prompt content getscache_controland survives cache invalidation, while dynamic content does not.Changes
Source
packages/agent-sdk/src/prompts/index.ts: AddedSystemPromptBlockinterface{ text: string; cacheable: boolean }. ChangedbuildSystemPromptto returnSystemPromptBlock[]instead ofstring, splitting into:cacheable: true): base prompt + DOING_TASKS + EXECUTING_ACTIONS + TOOL_POLICY + OUTPUT_EFFICIENCY + TONE_AND_STYLEcacheable: false): permission mode + language + env info + auto memory + MEMORY.mdpackages/agent-sdk/src/services/aiService.ts: WidenedCallAgentOptions.systemPrompttostring | SystemPromptBlock[].callAgentmaps blocks to content parts withcache_controlon cacheable blocks only for Claude models; joins to string for non-Claude models. ExistingtransformMessagesForExplicitCacheidempotency check prevents double-marking.Tests
prompts.test.tsSystemPromptBlock[]input tests inaiService.cacheControl.test.tsagent.systemPrompt.test.ts,aiManager.plan.test.ts,aiManager.test.tsfor array-based assertionsSpec
specs/019-prompt-cache-control.mdwith user story 5, FR-008, edge cases 7-8, andSystemPromptBlockkey entityWhy
Previously, the entire system prompt was a single string. Any change to dynamic content (date, MEMORY.md, permission mode) invalidated the entire cache. By separating static and dynamic content into independent blocks, the static block's cache survives dynamic content changes.