xonecas.io
64875
Mc

mini-coder / commit 18fa45f

fix: resolve Codex autonomy issues via dual-anchored system prompt and payload tracing

Sean2026-03-01 18fa45f1400db83a17fe4f2d19f20fc6d4bccf8d parent 0a0f0a9

3 files changed

codex-lazy-fix.md+76−0
@@ -0,0 +1,76 @@
1+# Codex Autonomy Issues & Fix Analysis
2+
3+## Behaviours
4+When using `zen/gpt-5.3-codex` as the agent, the model consistently exhibits "lazy" or permission-seeking behaviour. Specifically:
5+1. **Initial Compliance**: It starts by reading files or globbing the directory.
6+2. **Immediate Stall**: Instead of executing edits or implementing the plan, it outputs a multi-paragraph text explaining what it *plans* to do and ends the turn.
7+3. **Permission Seeking**: It explicitly asks the user for permission (e.g., "Reply **'proceed'** and I'll start implementing batch 1").
8+4. **Ralph Mode Incompatibility**: In `/ralph` mode, the agent loops continuously. Because it restarts with a fresh context on each loop and stalls after gathering context, it never actually writes any files. It just loops through the same read-and-plan phase until it hits the max iteration limit.
9+5. **Model Differences**: Both Claude and Gemini models do not exhibit this behaviour. They are not subjected to the same conversational RLHF that pushes the model to ask the user to double check its work.
10+
11+## Root Cause Analysis
12+An analysis of both OpenAI's open-source `codex-rs` client and `opencode` source code reveals that Codex models (like `gpt-5.3-codex`) are highly RLHF-tuned for safety and collaborative pair-programming. By default, the model prefers to break tasks into chunks and explicitly ask for sign-off.
13+
14+To override this, the model requires three things which `mini-coder` was failing to provide correctly:
15+
16+### 1. Dual-Anchored System Prompts (`system` + `instructions`)
17+`mini-coder` implemented a check `useInstructions` that placed the system prompt into the `instructions` field of the `/v1/responses` API payload. However, doing so stripped the `system` role message from the conversation context (`input` array).
18+
19+By looking at `opencode` and `codex-rs`, they both ensure that the context array *also* contains the system prompt:
20+- `opencode` maps its environment variables and system instructions to `role: "system"` (or `role: "developer"`) inside `input.messages`, **while also** passing behavioral instructions to the `instructions` field in the API payload.
21+- `codex-rs` directly injects `role: "developer"` into the message list (as seen in `codex-rs/core/src/compact.rs` and their memory tracing implementations).
22+
23+Without the `system` / `developer` message anchored at the start of the `input` array, the AI SDK and the model deprioritized the standalone `instructions` field, allowing the model's base permission-seeking behaviors to take over.
24+
25+### 2. Explicit "Do Not Ask" Directives
26+Both `opencode` and `codex-rs` employ heavy anti-permission prompts.
27+- **Opencode** (`session/prompt/codex_header.txt`):
28+ > "- Default: do the work without asking questions... Never ask permission questions like 'Should I proceed?' or 'Do you want me to run tests?'; proceed with the most reasonable option and mention what you did."
29+- **Codex-RS** (`core/templates/model_instructions/gpt-5.2-codex_instructions_template.md`):
30+ > "Persist until the task is fully handled end-to-end within the current turn whenever feasible: do not stop at analysis or partial fixes; carry changes through implementation, verification, and a clear explanation of outcomes unless the user explicitly pauses or redirects you."
31+
32+`mini-coder` introduced `CODEX_AUTONOMY` in a previous commit, but because of Issue #1, it was never adequately anchored in the `input` array.
33+
34+## Evidence & Tests
35+We introduced a fetch wrapper interceptor in `src/llm-api/providers.ts` that logs the full outbound API requests to `~/.config/mini-coder/api.log`.
36+
37+A test script `test-turn.ts` running a dummy turn showed the exact payload generated by the AI SDK before our fix:
38+```json
39+ "body": {
40+ "model": "gpt-5.3-codex",
41+ "input": [
42+ {
43+ "role": "user",
44+ "content": [
45+ { "type": "input_text", "text": "hello" }
46+ ]
47+ }
48+ ],
49+ "store": false,
50+ "instructions": "You are a test agent.",
51+ ...
52+```
53+```json
54+ "body": {
55+ "model": "gpt-5.3-codex",
56+ "input": [
57+ {
58+ "role": "developer",
59+ "content": "You are mini-coder, a small and fast CLI coding agent... [CODEX_AUTONOMY directives]"
60+ },
61+ {
62+ "role": "user",
63+ "content": [
64+ { "type": "input_text", "text": "hello" }
65+ ]
66+ }
67+ ],
68+ "instructions": "You are mini-coder, a small and fast CLI coding agent... [CODEX_AUTONOMY directives]"
69+ }
70+```
71+This perfectly mirrors the behavior seen in `opencode` and `codex-rs`.
72+
73+## Actions Taken
74+1. Added an `api.log` request interceptor in `providers.ts` to capture and inspect the exact JSON payloads sent to the OpenAI/AI SDK endpoints.
75+2. Cloned and analyzed both `opencode` and `codex` repos to observe how they communicate with `gpt-5.*` codex endpoints.
76+3. Updated `src/llm-api/turn.ts` so `system: systemPrompt` is *always* passed to the AI SDK, guaranteeing a `developer` message anchors the `input` array, even when `instructions` is also used.
src/llm-api/providers.ts+47−4
@@ -5,8 +5,38 @@ import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
55 import type { LanguageModel } from "ai";
66 import { createOllama } from "ollama-ai-provider";
77
8+import { logApiEvent } from "./api-log.ts";
89 // ─── Zen endpoint constants ────────────────────────────────────────────────────
910
11+function getFetchWithLogging(): typeof fetch {
12+ const customFetch = async (
13+ input: Parameters<typeof fetch>[0],
14+ init?: Parameters<typeof fetch>[1],
15+ ): Promise<Response> => {
16+ if (init?.body) {
17+ try {
18+ const bodyStr = init.body.toString();
19+ const bodyJson = JSON.parse(bodyStr);
20+ logApiEvent("Provider Request", {
21+ url: input.toString(),
22+ method: init.method,
23+ headers: init.headers,
24+ body: bodyJson,
25+ });
26+ } catch {
27+ logApiEvent("Provider Request", {
28+ url: input.toString(),
29+ method: init.method,
30+ headers: init.headers,
31+ body: init.body,
32+ });
33+ }
34+ }
35+ return fetch(input, init);
36+ };
37+ return customFetch as unknown as typeof fetch;
38+}
39+
1040 const ZEN_BASE = "https://opencode.ai/zen/v1";
1141
1242 // Zen endpoint routing — matched in order, fallthrough to OpenAI-compatible
@@ -35,6 +65,7 @@ function zenAnthropic() {
3565 if (!_zenAnthropic) {
3666 // @ai-sdk/anthropic appends /messages automatically — baseURL is the v1 root
3767 _zenAnthropic = createAnthropic({
68+ fetch: getFetchWithLogging(),
3869 apiKey: getZenApiKey(),
3970 baseURL: ZEN_BASE,
4071 });
@@ -46,6 +77,7 @@ function zenOpenAI() {
4677 if (!_zenOpenAI) {
4778 // @ai-sdk/openai appends /responses automatically — baseURL is the v1 root
4879 _zenOpenAI = createOpenAI({
80+ fetch: getFetchWithLogging(),
4981 apiKey: getZenApiKey(),
5082 baseURL: ZEN_BASE,
5183 });
@@ -57,6 +89,7 @@ function zenGoogle() {
5789 if (!_zenGoogle) {
5890 // @ai-sdk/google constructs its own path; we pass the base up to /models
5991 _zenGoogle = createGoogleGenerativeAI({
92+ fetch: getFetchWithLogging(),
6093 apiKey: getZenApiKey(),
6194 baseURL: ZEN_BASE,
6295 });
@@ -68,6 +101,7 @@ function zenCompat() {
68101 if (!_zenCompat) {
69102 // @ai-sdk/openai-compatible appends /chat/completions — baseURL is the v1 root
70103 _zenCompat = createOpenAICompatible({
104+ fetch: getFetchWithLogging(),
71105 name: "zen-compat",
72106 apiKey: getZenApiKey(),
73107 baseURL: ZEN_BASE,
@@ -86,7 +120,10 @@ function directAnthropic() {
86120 if (!_directAnthropic) {
87121 const key = process.env.ANTHROPIC_API_KEY;
88122 if (!key) throw new Error("ANTHROPIC_API_KEY is not set");
89- _directAnthropic = createAnthropic({ apiKey: key });
123+ _directAnthropic = createAnthropic({
124+ fetch: getFetchWithLogging(),
125+ apiKey: key,
126+ });
90127 }
91128 return _directAnthropic;
92129 }
@@ -95,7 +132,7 @@ function directOpenAI() {
95132 if (!_directOpenAI) {
96133 const key = process.env.OPENAI_API_KEY;
97134 if (!key) throw new Error("OPENAI_API_KEY is not set");
98- _directOpenAI = createOpenAI({ apiKey: key });
135+ _directOpenAI = createOpenAI({ fetch: getFetchWithLogging(), apiKey: key });
99136 }
100137 return _directOpenAI;
101138 }
@@ -104,7 +141,10 @@ function directGoogle() {
104141 if (!_directGoogle) {
105142 const key = process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
106143 if (!key) throw new Error("GOOGLE_API_KEY or GEMINI_API_KEY is not set");
107- _directGoogle = createGoogleGenerativeAI({ apiKey: key });
144+ _directGoogle = createGoogleGenerativeAI({
145+ fetch: getFetchWithLogging(),
146+ apiKey: key,
147+ });
108148 }
109149 return _directGoogle;
110150 }
@@ -207,7 +247,10 @@ export function resolveModel(modelString: string): LanguageModel {
207247
208248 case "ollama": {
209249 const baseURL = process.env.OLLAMA_BASE_URL ?? "http://localhost:11434";
210- const ollamaProvider = createOllama({ baseURL });
250+ const ollamaProvider = createOllama({
251+ fetch: getFetchWithLogging(),
252+ baseURL,
253+ });
211254 // ollama-ai-provider returns LanguageModelV1; cast to LanguageModel
212255 return ollamaProvider(modelId) as unknown as LanguageModel;
213256 }
src/llm-api/turn.ts+1−1
@@ -159,7 +159,7 @@ export async function* runTurn(options: {
159159 return undefined;
160160 },
161161
162- ...(systemPrompt && !useInstructions ? { system: systemPrompt } : {}),
162+ ...(systemPrompt ? { system: systemPrompt } : {}),
163163 ...(useInstructions
164164 ? {
165165 providerOptions: {