Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
fix(agent-runtime): send the GPT-6.2 Codex system prompt inside input
liuzixiang1dcb045c84be
diff --git a/packages/agent-runtime/src/codex-cacheable-instructions.test.ts b/packages/agent-runtime/src/codex-cacheable-instructions.test.tsnew file mode 100644index 000000000..793a4e94b--- /dev/null+++ b/packages/agent-runtime/src/codex-cacheable-instructions.test.ts@@ -0,0 +1,89 @@+import type { Model } from "@earendil-works/pi-ai";+import { describe, expect, it } from "vitest";+import {+ codexCacheableInstructions,+ withCodexCacheableInstructions,+} from "./codex-cacheable-instructions.js";++const codexModel = (id: string, provider = "openai-codex") =>+ ({ id, provider, api: "openai-codex-responses", baseUrl: "https://chatgpt.com/backend-api" }) as Model<any>;++const codexBody = () => ({+ model: "gpt-6.2-sol",+ store: false,+ stream: true,+ instructions: "You are PI, a careful desktop agent.",+ input: [{ role: "user", content: [{ type: "input_text", text: "hello" }] }],+ prompt_cache_key: "session-1",+});++describe("codexCacheableInstructions (#2419)", () => {+ it("moves a gpt-6.2 codex system prompt into input as a leading developer message", () => {+ const rewritten = codexCacheableInstructions(codexBody(), codexModel("gpt-6.2-sol")) as Record<string, unknown>;++ expect(rewritten).toBeDefined();+ expect(rewritten.instructions).toBeUndefined();+ const input = rewritten.input as Array<Record<string, unknown>>;+ expect(input).toHaveLength(2);+ expect(input[0]).toEqual({+ type: "message",+ role: "developer",+ content: [{ type: "input_text", text: "You are PI, a careful desktop agent." }],+ });+ expect(input[1]).toEqual(codexBody().input[0]);+ // Everything else the adapter built stays untouched.+ expect(rewritten.prompt_cache_key).toBe("session-1");+ expect(rewritten.model).toBe("gpt-6.2-sol");+ });++ it("leaves gpt-6.1 and earlier codex models on the instructions field", () => {+ expect(codexCacheableInstructions(codexBody(), codexModel("gpt-6.1-sol"))).toBeUndefined();+ expect(codexCacheableInstructions(codexBody(), codexModel("gpt-6-astra"))).toBeUndefined();+ expect(codexCacheableInstructions(codexBody(), codexModel("gpt-5.6-luna"))).toBeUndefined();+ });++ it("leaves non-codex providers alone even on a gpt-6.2 id", () => {+ expect(codexCacheableInstructions(codexBody(), codexModel("gpt-6.2-sol", "openai-responses"))).toBeUndefined();+ });++ it("is a no-op once the prompt already lives in input", () => {+ const first = codexCacheableInstructions(codexBody(), codexModel("gpt-6.2-sol")) as Record<string, unknown>;+ expect(codexCacheableInstructions(first, codexModel("gpt-6.2-sol"))).toBeUndefined();+ });++ it("does nothing when the adapter sent no system prompt", () => {+ const body = codexBody();+ delete (body as Record<string, unknown>).instructions;+ expect(codexCacheableInstructions(body, codexModel("gpt-6.2-sol"))).toBeUndefined();+ });+});++describe("withCodexCacheableInstructions", () => {+ it("rewrites the payload for a gpt-6.2 codex model and runs the caller hook", async () => {+ const callerOrder: string[] = [];+ const options = {+ onPayload: async (payload: unknown) => {+ callerOrder.push("caller");+ return (payload as Record<string, unknown>).marked === true ? payload : undefined;+ },+ };+ const wrapped = withCodexCacheableInstructions(options as never, codexModel("gpt-6.2-sol"));+ const body = { ...codexBody(), marked: true };+ const result = (await wrapped.onPayload?.(body, codexModel("gpt-6.2-sol"))) as Record<string, unknown>;+ expect(callerOrder).toEqual(["caller"]);+ expect(result.instructions).toBeUndefined();+ expect((result.input as Array<unknown>)[0]).toMatchObject({ role: "developer" });+ });++ it("returns the original options untouched for other models", () => {+ const options = {};+ expect(withCodexCacheableInstructions(options as never, codexModel("gpt-6.1-sol"))).toBe(options);+ expect(withCodexCacheableInstructions(options as never, codexModel("gpt-6.2-sol", "openai-responses"))).toBe(options);+ });++ it("still rewrites when the caller hook declines", async () => {+ const wrapped = withCodexCacheableInstructions({} as never, codexModel("gpt-6.2-sol"));+ const result = (await wrapped.onPayload?.(codexBody(), codexModel("gpt-6.2-sol"))) as Record<string, unknown>;+ expect(result.instructions).toBeUndefined();+ });+});diff --git a/packages/agent-runtime/src/codex-cacheable-instructions.ts b/packages/agent-runtime/src/codex-cacheable-instructions.tsnew file mode 100644index 000000000..4a481d1a5--- /dev/null+++ b/packages/agent-runtime/src/codex-cacheable-instructions.ts@@ -0,0 +1,63 @@+import type { Model, SimpleStreamOptions } from "@earendil-works/pi-ai";++/**+ * Move the Codex system prompt out of the top-level `instructions` field.+ *+ * GPT-6.2 stopped computing prompt-cache prefixes over the `instructions`+ * field: the adapter puts the whole system prompt there, so every turn of a+ * session re-bills it as uncached input even though it never changes+ * (issue #2419). GPT-6.1 and earlier still cache it, and the plain+ * `openai-responses` path already sends the system prompt as a leading+ * `developer` message inside `input`, so only the codex adapter on a+ * `gpt-6.2` model gets rewritten. The rewrite is idempotent: once the prompt+ * lives in `input`, the top-level field is gone and the hook returns+ * undefined, which keeps upstream's eventual fix a no-op here.+ */+const GPT62_PREFIX = "gpt-6.2";++export function codexCacheableInstructions(payload: unknown, model: Model<any>): unknown | undefined {+ if (!payload || typeof payload !== "object" || Array.isArray(payload)) return undefined;+ if (model.provider !== "openai-codex") return undefined;+ if (!String(model.id).startsWith(GPT62_PREFIX)) return undefined;+ const record = payload as Record<string, unknown>;+ const instructions = record.instructions;+ if (typeof instructions !== "string" || instructions.length === 0) return undefined;+ const input = Array.isArray(record.input) ? record.input : [];+ const rest = { ...record };+ // The field is dropped outright, matching the OpenCode fix: the Codex+ // backend rejects nothing about a missing `instructions`, while an empty+ // string would ask the model to treat the turn as instruction-less.+ delete rest.instructions;+ return {+ ...rest,+ input: [+ {+ type: "message",+ role: "developer",+ content: [{ type: "input_text", text: instructions }],+ },+ ...input,+ ],+ };+}++/**+ * Innermost payload hook: the extension chain above it must see the+ * rewritten body, and pi-ai applies `onPayload` before the WebSocket/SSE+ * branch, so both transports ride the same rewrite.+ */+export function withCodexCacheableInstructions(+ options: SimpleStreamOptions,+ model: Model<any>,+): SimpleStreamOptions {+ if (model.provider !== "openai-codex" || !String(model.id).startsWith(GPT62_PREFIX)) {+ return options;+ }+ return {+ ...options,+ onPayload: async (payload, payloadModel) => {+ const rewritten = await options.onPayload?.(payload, payloadModel);+ return codexCacheableInstructions(rewritten ?? payload, payloadModel) ?? rewritten ?? payload;+ },+ };+}diff --git a/packages/agent-runtime/src/runtime.ts b/packages/agent-runtime/src/runtime.tsindex aae9c51ae..8671addb2 100644--- a/packages/agent-runtime/src/runtime.ts+++ b/packages/agent-runtime/src/runtime.ts@@ -283,6 +283,7 @@ import {openCodeEndpointFromProvider,withOpenCodeSessionHeaders,} from "./opencode-session-headers.js";+import { withCodexCacheableInstructions } from "./codex-cacheable-instructions.js";import { withFastModeOptions } from "./fast-mode.js";import { withSessionAffinityOptions } from "./session-affinity.js";import { withCompactionRequestHeaders } from "./compaction-request.js";@@ -2280,7 +2281,10 @@ Do not invent objections or turn speculative risks into blockers. Stop when the// one-shot requests build their own options and keep the default.const hookedOptions = withFastModeOptions(withSessionAffinityOptions(- this.withExtensionProviderHooks(requestOptions, m),+ this.withExtensionProviderHooks(+ withCodexCacheableInstructions(requestOptions, m),+ m,+ ),{provider: this.provider,api: m.api,