Repository navigation
Conversation
…ve prompt cache The gateway transiently decorated the CURRENT user message with a timestamp prefix (injectTimestamp, issue #3658) plus a content array-wrap before sending it to the model, but did not persist that decoration. As soon as the message aged into history, its bare stored form differed from the decorated form the model had seen — its wire bytes changed between turns. On every full-resend transport (anthropic-messages, openai-completions, local OpenAI-compat) this byte-instability invalidated the prompt cache from that message to the end of the prompt on every single user turn. This change single-sources the per-message timestamp stamp at the LLM boundary. normalizeMessagesForLlmBoundary now stamps every user turn — current AND historical — from each message's own `timestamp`, so the serialization a message produces is identical whether it is the active turn or a historical one. The transient inject-at-send-time call sites are removed, and the content form is canonicalized so a text-only user turn serializes byte-identically current vs historical. Storage stays bare; the stamp is applied only at the boundary. Net effect: byte-stable history across turns, so the prompt cache is no longer invalidated on each user turn. As a bonus, models now see true per-message historical timestamps rather than only the current turn's. Verified live on the wire: the same user message is byte-identical as the active turn and after it has aged into history. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Codex review: needs maintainer review before merge. Reviewed June 6, 2026, 2:17 PM ET / 18:17 UTC. Summary PR surface: Source +360, Tests +585. Total +945 across 17 files. Reproducibility: yes. Current source shows the current user turn is stamped before runtime while transcript storage remains bare, and the PR body supplies live cache/wire evidence; I did not rerun the paid/live provider scenario locally. Review metrics: 1 noteworthy metric.
Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land the single LLM-boundary stamping design only after maintainers explicitly accept the prompt-compatibility change and the latest head has fresh green validation for the touched agent/gateway surfaces. Do we have a high-confidence way to reproduce the issue? Yes. Current source shows the current user turn is stamped before runtime while transcript storage remains bare, and the PR body supplies live cache/wire evidence; I did not rerun the paid/live provider scenario locally. Is this the best way to solve the issue? Unclear until maintainers accept the compatibility tradeoff. If changing historical prompt bytes is acceptable, the LLM boundary is the right owner because it is the shared provider serialization point. AGENTS.md: found and applied where relevant. Codex review notes: model gpt-5.5, reasoning high; reviewed against 59365959d3b0. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source +360, Tests +585. Total +945 across 17 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
|
Merged via squash.
Thanks @Marvinthebored! |
AI-assisted PR (Claude). Marked per CONTRIBUTING; human-run real-behavior proof from a live setup is below.
Summary
What problem does this PR solve? — The gateway decorates the current user message (a
[timestamp]prefix from #3658 + a content-block wrap) but does not persist the decoration. When that message ages into history it reverts to its bare stored string, so its wire bytes change and the provider prompt cache is invalidated from that message to the end of the prompt — on every new user turn.Why now? — On
anthropic-messagesthis re-writes the suffix at the 1.25× cache-write premium every turn and never collects the 0.1× reads, so it costs more than not caching at all. It also hitsopenai-completionsand local vLLM/llama.cpp/Ollama (slower prefill). Full analysis: #90810.Intended outcome — Every user message serializes byte-identically whether it is the current turn or history, so the cached prefix is reused across turns.
Out of scope —
openai-chatgpt-responses(already immune via server-side continuation); the separatecache_controlmechanisms in #86063 and the merged tool-loop fix #89488.Success looks like — same user message is byte-identical as current turn and as history;
cache_readtracks the growing prefix whilecache_writecollapses to the new delta.Reviewer focus — (1) the boundary stamp is deterministic per message and never mutates on aging; (2) content-form canonicalization is applied only to text-only turns and does not alter attachment/image turns that legitimately need block arrays.
Linked context
Closes #90810
Related #86063 (different mechanism —
cache_controlplacement / metadata strip), #89488 (merged, separate slice), #3658 (the feature this regresses from; kept intact).Requested by a maintainer or owner? — No; filed proactively with the issue above.
How it works
normalizeMessagesForLlmBoundarynow stamps every user turn (current and historical) from that message's own storedtimestamp. Storage stays bare; the stamp is deterministic, so bytes never change as a message ages.content(matching how history is stored/replayed); attachment/image turns keep their block arrays.injectTimestampcall sites (chat.ts,agent.ts,tui/embedded-backend.ts,server-restart-sentinel.ts) are removed in favor of the single boundary stamp.This keeps #3658 intact (same prefix-on-the-message format small local models parse reliably) and, as a bonus, the model now sees every message's timestamp — real historical time awareness, not just "now".
Real behavior proof (required for external PRs)
claude-opus-4-8over an Anthropic-Messages-compatible provider, plus a transport-level wire capture on an OpenAI-compatible endpoint.usagefields (input / cache_read / cache_write) and cross-checked them token-for-token against the provider's own billing dashboard. Separately captured two consecutive outbound request bodies and diffed the previous user message's serialization.cache_readclimbed with the conversation 83,300 → 85,789 whilecache_writewas the new delta only (27–713 tokens; write-share ~26% → ~1.4%). Reconciliation was token-exact vs the provider dashboard on every sampled turn (e.g. read 83,882 + write 470 = 84,352 cached). After a >5-min TTL gap, a single full re-write then reads resumed — impossible under the bug. Wire diff: the previous user message is now byte-identical as current turn and as history (before: decorated block"content":[{…}]vs bare string"content":"…").cache_readwas pinned at the static system+tools floor (~53,875) with the suffix re-billed ascache_creationevery turn (worst case ~131k tokens re-written in one tool-heavy turn).openai-completions; onanthropic-messagesit is inferred (shared boundary serialization) but not separately wire-captured, because that path also carriescache_control. (2) the fullpnpm build && pnpm check && pnpm testsuite was not run end-to-end in this environment; targeted typechecks + the new regression test were run (below). (3) nocodex reviewrun.Tests and validation
tsgocore typecheck (clean) andtsgocore test typecheck (clean) on the rebased branch.src/agents/embedded-agent-runner/run/attempt.llm-boundary.cache-stability.test.ts— asserts the same user turn serializes byte-identically as current vs historical at the LLM boundary.attempt.llm-boundarysuite was extended to cover the boundary stamp.Risk checklist
[timestamp]prefix at the LLM boundary (previously only the current turn). This is the Feature: Gateway should inject timestamps for TUI/web messages #3658 format, applied consistently.Current review state
Investigated and implemented with AI assistance (Claude). cc @Peetiegonzalez