Repository navigation
fix(agent-runtime): send the GPT-6.2 Codex system prompt inside input for prompt caching - #2428
Conversation
GPT-6.2 stopped computing prompt-cache prefixes over the top-level `instructions` field, so the Codex adapter's whole system prompt was re-billed as uncached input on every turn of a session while the prompt never changed (issue vastsa#2419). The plain openai-responses path already sends the system prompt as a leading developer message inside input, which is exactly the shape OpenCode's fix moved the Codex path to. An innermost payload hook rewrites the body for openai-codex models whose id starts with gpt-6.2: the instructions become the first input item as a developer message and the top-level field is dropped. The rewrite is idempotent, leaves GPT-6.1 and earlier on the cached instructions field, and pi-ai applies onPayload before the WebSocket and SSE branches, so both transports ride it. The extension before_provider_request chain wraps it and sees the rewritten body.
|
The rewrite itself looks right (provider/id gating, idempotence, and WS continuation keep working because Please apply |
There was a problem hiding this comment.
AIUO CodePilot · Preliminary review
Diff-only preliminary review of #2428: the hook moves Codex `instructions` into a leading `developer` message for `gpt-6.2` models. One likely behavioral defect: when a caller `onPayload` rewrites to a replacement body lacking `instructions`, the fallback discards the caller's rewrite and returns the original body, contrary to the wrapper's stated contract. Verification limits: no repo/execution access; line numbers inferred from patch context.
- medium
packages/agent\-runtime/src/codex\-cacheable\-instructions\.ts:60: `codexCacheableInstructions(rewritten ?? payload, payloadModel) ?? rewritten ?? payload` falls back to `payload` whenever the rewrite is a no-op. If a caller `onPayload` intentionally replaced the body with one that has no top-level `instructions` (e.g. it already moved the prompt into `input`), the rewrite returns undefined and the wrapper returns the *original* pre-hook payload, silently discarding the caller's rewrite and reverting any other fields it changed. The wrapper should return `rewritten ?? payload` in the no-op case (i.e. only use the rewrite result when defined).
Evidence:return codexCacheableInstructions\(rewritten ?? payload, payloadModel\) ?? rewritten ?? payload;
Head: 4b83167
Base: 95df2e0
CI: success; mergeable: true.
Validation: code/tests NOT EXECUTED. Diff-only preliminary analysis; not approval. Human review and repository gates remain required.
Fixes #2419.
What
The Codex adapter sends the system prompt as the top-level
instructionsfield. GPT-6.2 no longer computes prompt-cache prefixes over that field, so an unchanged project/system prompt was re-billed as uncached input on every turn. GPT-6.1 and earlier still cache it, and the plainopenai-responsespath never had the problem (it sends the system prompt as a leadingdevelopermessage insideinput).The change
An innermost
onPayloadhook (codex-cacheable-instructions.ts) rewrites the body foropenai-codexmodels whose id starts withgpt-6.2:inputitem as a{type: "message", role: "developer"}message (the same shape OpenCode's fix uses),instructionsfield is dropped outright (an empty string would tell the model the turn is instruction-less),prompt_cache_key, tools, reasoning includes — stays untouched, and the rewrite is idempotent: once upstream moves the prompt itself, the hook sees no top-level field and returns undefined.pi-ai applies
onPayloadbefore its WebSocket/SSE branch, so both transports ride the rewrite; the extensionbefore_provider_requestchain wraps it and sees the rewritten body. GPT-6.1-and-earlier models and every non-codex provider return the original options object untouched.Tests
codex-cacheable-instructions.test.ts(8 cases): the gpt-6.2 rewrite (developer message first, field gone, other fields intact), gpt-6.1/gpt-6/gpt-5.6 untouched, non-codex providers untouched even on a matching id, idempotence, missing-prompt no-op, and the wrapper's ordering (caller hook runs, rewrite still lands when the caller declines). Fullagent-runtimesuite: 2057 passed / 1 skipped;tsc --noEmitclean.Validation note: the reporter's live A/B (GPT-6.2 vs GPT-6.1 cache hits) needs a ChatGPT-login account; this PR pins the request shape with the reporter's offline method (a captured codex body) plus the wrapper contract.