Skip to content

fix(agent-runtime): send the GPT-6.2 Codex system prompt inside input for prompt caching - #2428

Merged
vastsa merged 2 commits into
vastsa:mainfrom
SeanLiu326:fix/codex-gpt62-cacheable-instructions
Nov 21, 2026
Merged

vastsa merged 2 commits into
vastsa:mainfrom
SeanLiu326:fix/codex-gpt62-cacheable-instructions

Conversation

@SeanLiu326

Copy link
Copy Markdown
Contributor

Fixes #2419.

What

The Codex adapter sends the system prompt as the top-level instructions field. 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 plain openai-responses path never had the problem (it sends the system prompt as a leading developer message inside input).

The change

An innermost onPayload hook (codex-cacheable-instructions.ts) rewrites the body for openai-codex models whose id starts with gpt-6.2:

  • the system prompt becomes the first input item as a {type: "message", role: "developer"} message (the same shape OpenCode's fix uses),
  • the top-level instructions field is dropped outright (an empty string would tell the model the turn is instruction-less),
  • everything else — 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 onPayload before its WebSocket/SSE branch, so both transports ride the rewrite; the extension before_provider_request chain 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). Full agent-runtime suite: 2057 passed / 1 skipped; tsc --noEmit clean.

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.

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.
@vastsa

vastsa commented Nov 20, 2026

Copy link
Copy Markdown
Owner

The rewrite itself looks right (provider/id gating, idempotence, and WS continuation keep working because getCachedWebSocketInputDelta compares input prefixes). But it is only applied to main turns (runtime.ts:2283). Delegated subagents (subagent-model-binding.ts:67-127) run multi-turn loops with a fixed system prompt on the same codex adapter and still send it as top-level instructions, so the uncached-prompt cost from #2419 remains on that path. Compaction (compaction-request.ts:58) and one-shot (one-shot-complete.ts:103) are also uncovered.

Please apply withCodexCacheableInstructions on those request paths as well, and add a test that goes through at least one runtime/subagent stream path to show the payload is rewritten. The branch is also behind main; please merge the latest main when updating.

@aiuo-codepilot aiuo-codepilot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@vastsa
vastsa merged commit 76e0a27 into vastsa:main Nov 21, 2026
5 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

Development

Successfully merging this pull request may close these issues.

[Bug] OpenAI (ChatGPT Plus/Pro) 登录下 GPT-6.2 Sol / Astra 的系统提示无法复用缓存

2 participants