Repository navigation
perf(session): reuse the provider's prompt cache across a project's sessions - #1066
Merged
Merged
Conversation
Providers cache a prompt only up to its first difference. The <env> block names the session (its ID, scratch folder, connected folders, budget) and sat ~4K tokens into the system prompt, so a new session re-read everything after it: the rest of the system prompt and, for chat templates that render tools after the system text (DeepSeek, Qwen, Kimi via OpenRouter), all ~9K tokens of tool definitions. Claude's only system cache marker sat after it, so a new Claude session reused nothing. The environment is now joined to the start of the first user message (SystemPrompt.withEnvironment), keeping a plain-text message a plain string. The system prompt and tools are byte-identical across a project's sessions; the environment is rebuilt the same each step, so a session's own prefix stays stable. Compaction's shared path carries the same environment to keep its prefix, and the context estimate still counts it. Measured on the captured request (same provider): a new session's first call reused 0-24% of the prompt with the old layout and 99% with the session lines in the user message (DeepSeek); Claude reused nothing before.
…session facts The previous commit moved the whole <env> block, rules included, into the first user message as plain text. Rules there carry less weight than system instructions, and nothing marked them as coming from OpenScience. SystemPrompt.environment now returns two parts. The system prompt keeps the rules and project facts: model, project, access mode, platform, date and knowledge cutoff, and the rules about folders, Results and paths, plus a line telling the agent that this session's details arrive in a <session-context> block from OpenScience, not from the user. That block, on the first user message, carries only the session's facts: Session ID, working folder or scratch, connected folders and the harness env lines. It is not a <system-reminder>, which stays reserved for harness instructions in the system prompt (enforced by the delegation and native-provider tests). Checked with real prompts on Claude Haiku, Kimi K3 and DeepSeek flash: no path leak in a greeting, the real session ID and folder when asked, the access mode stated correctly, and new files in Session scratch as the rules say (DeepSeek: 10/14 vs 7/14 before the move; it is inconsistent either way). New sessions in one project read ~14.6K cached tokens on their first call.
…onment parts
Review findings on the environment split:
- The budget, acceptance and deliverables lines carried instructions ("keep
improving it", "must pass", "produce anything else the request asks for")
and sat in the session block on the user message. The env.lines hook now
has a `rules` output: each unit puts its figures in `lines` (session
block) and the instruction about them in `rules` (system prompt), the
latter only when the unit is active.
- SystemPrompt.environment returns a named { rules, session } instead of a
positional pair, and takes the units' { lines, rules }; withEnvironment is
now withSessionContext, and compaction's remembered field sessionContext,
since they carry only the session's facts.
- Stale test title and comments ("compute stays in <env>") and the <env>
fixture in the helper tests now match the <session-context> block.
The <session-context> block was rebuilt on every request from state that
changes during a session (connected folders, a new deadline), so a change
silently rewrote the first user message: the agent's earliest message
changed under it and the whole conversation was re-read uncached.
The block as first built is now kept per session (SessionTraceStore
sessionContext.sent) and always sent on the first user message, so the
cached prefix never changes. When a freshly built block differs from the
last one told, it is told once as a durable harness message ("Session
details changed. These replace the ones at the start of the conversation:")
and recorded as latest, the same way a change in the offered tools is
announced (Toolset.notice).
…a restart HarnessState lives in memory. The acceptance contract and the deliverables are read only from the session's first request, in that message's hook, so a process that restarted mid-session never read them again: their session details, their rules and their end-of-turn checks silently disappeared. Each unit now marks its state seeded, from the first request's own hook as before or, failing that, from the stored transcript the first time its env.lines or loop.before_finish hook runs (firstRequest: the first user message with text the person typed; none for a worker), like the cost unit reseeds spend.
The block on the first user message is kept as first sent, and a later change is told once as a durable notice. Compaction replaces the head that block rode, and may summarize the notice away, so the conversation would again start with the original details (an old folder list or budget). The kept block now records the message it rides (head). When the request's first user message is a different one, as after compaction, the current details are recorded and sent; the new prefix is written fresh anyway. The summary request itself still rides the turn's own block, so its prefix is unchanged.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
KB (KB-syntheticsciences)
marked this pull request as ready for review
November 15, 2026 16:07
KB (KB-syntheticsciences)
requested review from
Aayam Bansal (aayambansal) and
Ishaan Gangwani (ishaan1124)
as code owners
November 15, 2026 16:07
KB (KB-syntheticsciences)
removed request for
Aayam Bansal (aayambansal) and
Ishaan Gangwani (ishaan1124)
November 15, 2026 16:08
# Conflicts: # CHANGELOG.md # backend/cli/src/session/prompt.ts
KB (KB-syntheticsciences)
requested a review
from Aayam Bansal (aayambansal)
November 15, 2026 16:58
3 of 4 tasks
This branch was successfully deployed
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.
What does this PR do, and why?
A new session got no prompt-cache reuse from the model provider on its first call. The system prompt carried per-session details (session ID, working or scratch folder, connected folders, budget, checks), so its bytes differed between sessions, and the provider's cache missed on everything after them, tool definitions included.
This PR keeps the system prompt and tool definitions identical across a project's sessions and moves the session's own facts into a labelled
<session-context>block on the first message.env.linesrulesoutput.SystemPrompt.environment()now returns{ rules, session }.SessionTraceStore.sessionContext(sent,latest,head). Later prompts resend the stored copy, so the cached prefix doesn't move.harness/first-request.ts).Measured with real prompts:
Linked issue
Performance work, no issue.
How did you verify it?
util/lock-coordination.test.ts).main.Checklist
bun run check: format, typecheck, UI, workspace, SDK and docs tests locally; backend through deep CI./tooling/repo/generate.tsnot needed (no server route changes)docs/notes/opencode-core-for-science.mdupdated