Skip to content

fix(agent-runtime): keep the context budget reminder out of the prefix - #2464

Merged
vastsa merged 3 commits into
mainfrom
fix/context-budget-prefix-cache
Nov 20, 2026
Merged

vastsa merged 3 commits into
mainfrom
fix/context-budget-prefix-cache

Conversation

@vastsa

@vastsa vastsa commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Fixes #2458

Root cause

On pi 1.5.2 the agent loop builds each request from normalizeContext({ messages }); context.systemPrompt is no longer read, so the systemPrompt append the issue points at is dead code. The real prefix rewrite is the trailing context_budget system section: every adapter without supportsMidConvoSystemMessages (the default for custom OpenAI-compatible endpoints, e.g. Strata / llama-server) runs collapseSystemMessages, which folds all system messages into the leading system message. The 72-token reminder therefore lands at token 0 and invalidates the whole prefix cache.

Fix

  • Models that keep mid-conversation system messages in place: unchanged, still a context_budget section that expires at the next checkpoint.
  • All other models: the reminder is a request-only user message at the tail (same path as steering); it is not persisted and not carried into the next run.
  • Drop the dead systemPrompt append.

Tests

  • New: the collapsed request's leading system message is identical before and after the reminder (fails on old code), and the mid-conversation path keeps the section.
  • packages/agent-runtime: tsc --noEmit passes, vitest run 2062/2062 passes.

Not changed: the SILENT_TURN_NUDGE rerun still rewrites the runtime section; it is a rare one-shot recovery path and is left for a separate change.

pi-ai folds every system message into the leading system message for
adapters without mid-conversation system support, which is the default
for custom OpenAI-compatible endpoints. The context_budget section
therefore rewrote the request head, so a 72-token reminder discarded a
200k-token prefix cache (TTFT 5 s -> 184 s on a local endpoint).

Such models now receive the reminder as a request-only user message at
the tail; models that keep system messages in place keep the existing
section. The legacy systemPrompt append is dropped: the agent loop has
ignored context.systemPrompt since the transcript-based pi 1.5 core.

fixes #2458
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Deploying pi-desktop with  Cloudflare Pages  Cloudflare Pages

Latest commit: f985f41
Status: ✅  Deploy successful!
Preview URL: https://e59b5a5b.pi-desktop.pages.dev
Branch Preview URL: https://fix-context-budget-prefix-ca.pi-desktop.pages.dev

View logs

The source contract still expected the reminder to be a system section
unconditionally. It now asserts the split by mid-conversation support
and that the systemPrompt append is gone.

refs #2458
@vastsa
vastsa merged commit 31a8293 into main Nov 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant