xonecas.io
64875
Mc

mini-coder / commit 19a9755

feat: implement audit recommendations

- derive maxOutputTokens from model-info cache instead of hardcoding 16384
  - add max_output_tokens column to model_capabilities (DB_VERSION 6)
  - parse limit.output from models.dev payload in model-info-normalize
  - propagate through ModelCapabilityRow, RuntimeCapability, ModelInfo
  - expose via getMaxOutputTokens() in model-info.ts
  - use in buildStreamTextRequest with 16384 fallback
  - add test coverage for limit.output parsing

- add stream normalization comment to turn-execution.ts explaining
  why StreamToolCallTracker and StreamTextPhaseTracker exist

- upgrade WAL checkpoint from PASSIVE to TRUNCATE at startup to keep
  the WAL file size near zero between sessions

- fix shell output truncation to snap to the last newline boundary
  instead of cutting mid-byte, preventing broken last lines

xonecas2026-03-21 19a9755612566d112c997471b168fa75e5c4196b parent 1977015

10 files changed

docs/KNOWN_ISSUES.md+0−2
@@ -7,12 +7,10 @@
77 - `supportsThinking()` trusts models.dev flag too broadly — sends adaptive thinking to models that reject it (e.g. `claude-haiku-4-5` via Zen returns 400).
88 - AI SDK expands `claude-3-5-haiku` → dated variant that Zen doesn't serve (404).
99 - Shell tool max-timeout leaves the parent terminal in a broken state.
10-- Truncated tool commands cut mid-argument (e.g. `--new '{…`). Truncate at argument boundaries instead.
1110
1211 ## Features
1312
1413 - Conversation summary on max context instead of just an error with `/new` suggestion.
15-- Derive `maxOutputTokens` from model info instead of hardcoding 16384.
1614
1715 ## Refactors
1816
src/llm-api/model-info-cache.ts+6−0
@@ -12,6 +12,7 @@ import {
1212 export interface ModelInfo {
1313 canonicalModelId: string | null;
1414 contextWindow: number | null;
15+ maxOutputTokens: number | null;
1516 reasoning: boolean;
1617 }
1718
@@ -26,6 +27,7 @@ export interface LiveModel {
2627 interface RuntimeCapability {
2728 canonicalModelId: string;
2829 contextWindow: number | null;
30+ maxOutputTokens: number | null;
2931 reasoning: boolean;
3032 sourceProvider: string | null;
3133 }
@@ -91,6 +93,7 @@ export function buildRuntimeCache(
9193 capabilitiesByCanonical.set(canonical, {
9294 canonicalModelId: canonical,
9395 contextWindow: row.context_window,
96+ maxOutputTokens: row.max_output_tokens,
9497 reasoning: row.reasoning === 1,
9598 sourceProvider: row.source_provider,
9699 });
@@ -146,6 +149,7 @@ function resolveFromProviderRow(
146149 return {
147150 canonicalModelId: capability.canonicalModelId,
148151 contextWindow: capability.contextWindow ?? row.contextWindow,
152+ maxOutputTokens: capability.maxOutputTokens,
149153 reasoning: capability.reasoning,
150154 };
151155 }
@@ -153,6 +157,7 @@ function resolveFromProviderRow(
153157 return {
154158 canonicalModelId: row.canonicalModelId,
155159 contextWindow: row.contextWindow,
160+ maxOutputTokens: null,
156161 reasoning: false,
157162 };
158163 }
@@ -179,6 +184,7 @@ export function resolveModelInfoInCache(
179184 return {
180185 canonicalModelId: capability.canonicalModelId,
181186 contextWindow: capability.contextWindow,
187+ maxOutputTokens: capability.maxOutputTokens,
182188 reasoning: capability.reasoning,
183189 };
184190 }
src/llm-api/model-info-normalize.ts+13−0
@@ -4,6 +4,7 @@ import { isRecord } from "./history/shared.ts";
44 interface ModelsDevEntry {
55 canonicalModelId: string;
66 contextWindow: number | null;
7+ maxOutputTokens: number | null;
78 reasoning: boolean;
89 sourceProvider: string;
910 rawJson: string | null;
@@ -35,6 +36,14 @@ function parseContextWindow(model: Record<string, unknown>): number | null {
3536 return Math.max(0, Math.trunc(context));
3637 }
3738
39+function parseMaxOutputTokens(model: Record<string, unknown>): number | null {
40+ const limit = model.limit;
41+ if (!isRecord(limit)) return null;
42+ const output = limit.output;
43+ if (typeof output !== "number" || !Number.isFinite(output)) return null;
44+ return Math.max(0, Math.trunc(output));
45+}
46+
3847 export function parseModelsDevCapabilities(
3948 payload: unknown,
4049 updatedAt: number,
@@ -55,6 +64,7 @@ export function parseModelsDevCapabilities(
5564 const canonicalModelId = normalizeModelId(explicitId);
5665 if (!canonicalModelId) continue;
5766 const contextWindow = parseContextWindow(modelValue);
67+ const maxOutputTokens = parseMaxOutputTokens(modelValue);
5868 const reasoning = modelValue.reasoning === true;
5969 const rawJson = JSON.stringify(modelValue);
6070 const prev = merged.get(canonicalModelId);
@@ -62,6 +72,7 @@ export function parseModelsDevCapabilities(
6272 merged.set(canonicalModelId, {
6373 canonicalModelId,
6474 contextWindow,
75+ maxOutputTokens,
6576 reasoning,
6677 sourceProvider: provider,
6778 rawJson,
@@ -71,6 +82,7 @@ export function parseModelsDevCapabilities(
7182 merged.set(canonicalModelId, {
7283 canonicalModelId,
7384 contextWindow: prev.contextWindow ?? contextWindow,
85+ maxOutputTokens: prev.maxOutputTokens ?? maxOutputTokens,
7486 reasoning: prev.reasoning || reasoning,
7587 sourceProvider: prev.sourceProvider,
7688 rawJson: prev.rawJson ?? rawJson,
@@ -81,6 +93,7 @@ export function parseModelsDevCapabilities(
8193 return Array.from(merged.values()).map((entry) => ({
8294 canonical_model_id: entry.canonicalModelId,
8395 context_window: entry.contextWindow,
96+ max_output_tokens: entry.maxOutputTokens,
8497 reasoning: entry.reasoning ? 1 : 0,
8598 source_provider: entry.sourceProvider,
8699 raw_json: entry.rawJson,
src/llm-api/model-info.test.ts+25−0
@@ -99,6 +99,7 @@ describe("parseModelsDevCapabilities", () => {
9999 {
100100 canonical_model_id: "gemini-2.5-pro",
101101 context_window: 128,
102+ max_output_tokens: null,
102103 reasoning: 1,
103104 source_provider: "google",
104105 raw_json: JSON.stringify({ id: "models/models/Gemini-2.5-Pro" }),
@@ -107,6 +108,24 @@ describe("parseModelsDevCapabilities", () => {
107108 ]);
108109 });
109110
111+ test("parses max_output_tokens from limit.output", () => {
112+ const rows = parseModelsDevCapabilities(
113+ {
114+ openai: {
115+ models: {
116+ "gpt-5.2": {
117+ id: "gpt-5.2",
118+ limit: { context: 400_000, output: 32_768 },
119+ },
120+ },
121+ },
122+ },
123+ 123,
124+ );
125+ expect(rows[0]?.max_output_tokens).toBe(32_768);
126+ expect(rows[0]?.context_window).toBe(400_000);
127+ });
128+
110129 test("sanitizes invalid context window values", () => {
111130 const rows = parseModelsDevCapabilities(
112131 {
@@ -169,6 +188,7 @@ describe("resolveModelInfoFromRows", () => {
169188 {
170189 canonical_model_id: "gpt-5.2",
171190 context_window: 400_000,
191+ max_output_tokens: 16_384,
172192 reasoning: 1,
173193 source_provider: "openai",
174194 raw_json: null,
@@ -212,6 +232,7 @@ describe("resolveModelInfoFromRows", () => {
212232 ).toEqual({
213233 canonicalModelId: "gpt-5.2",
214234 contextWindow: 400_000,
235+ maxOutputTokens: 16_384,
215236 reasoning: true,
216237 });
217238
@@ -220,6 +241,7 @@ describe("resolveModelInfoFromRows", () => {
220241 ).toEqual({
221242 canonicalModelId: null,
222243 contextWindow: 32_768,
244+ maxOutputTokens: null,
223245 reasoning: false,
224246 });
225247 });
@@ -238,6 +260,7 @@ describe("resolveModelInfoFromRows", () => {
238260
239261 expect(openaiInfo).toEqual(zenInfo);
240262 expect(openaiInfo?.contextWindow).toBe(400_000);
263+ expect(openaiInfo?.maxOutputTokens).toBe(16_384);
241264 expect(openaiInfo?.reasoning).toBe(true);
242265 });
243266
@@ -248,6 +271,7 @@ describe("resolveModelInfoFromRows", () => {
248271 {
249272 canonical_model_id: "gpt-5.2",
250273 context_window: null,
274+ max_output_tokens: null,
251275 reasoning: 1,
252276 source_provider: "openai",
253277 raw_json: null,
@@ -270,6 +294,7 @@ describe("resolveModelInfoFromRows", () => {
270294 expect(info).toEqual({
271295 canonicalModelId: "gpt-5.2",
272296 contextWindow: 200_000,
297+ maxOutputTokens: null,
273298 reasoning: true,
274299 });
275300 });
src/llm-api/model-info.ts+4−0
@@ -264,6 +264,10 @@ export function getContextWindow(modelString: string): number | null {
264264 return resolveModelInfo(modelString)?.contextWindow ?? null;
265265 }
266266
267+export function getMaxOutputTokens(modelString: string): number | null {
268+ return resolveModelInfo(modelString)?.maxOutputTokens ?? null;
269+}
270+
267271 export function supportsThinking(modelString: string): boolean {
268272 return resolveModelInfo(modelString)?.reasoning ?? false;
269273 }
src/llm-api/turn-execution.ts+6−0
@@ -1,3 +1,9 @@
1+// Stream normalization strategy:
2+// Providers emit tool calls and text in inconsistent ways. StreamToolCallTracker
3+// collects fragmented tool-call deltas into complete calls before yielding events,
4+// and synthesises missing tool-call IDs (OpenAI streaming sometimes omits them).
5+// StreamTextPhaseTracker filters out non-reasoning "commentary" text phases that
6+// some providers (OpenAI) emit alongside tool calls, preventing duplicate output.
17 import type { FlexibleSchema, StepResult } from "ai";
28 import { dynamicTool, jsonSchema, type streamText } from "ai";
39 import { normalizeUnknownError } from "./error-utils.ts";
src/llm-api/turn-request.ts+2−1
@@ -1,5 +1,6 @@
11 import type { streamText } from "ai";
22
3+import { getMaxOutputTokens } from "./model-info.ts";
34 import { isAnthropicModelFamily } from "./model-routing.ts";
45 import type { ThinkingEffort } from "./provider-options.ts";
56 import {
@@ -79,7 +80,7 @@ export function buildStreamTextRequest(
7980
8081 return {
8182 model: input.model,
82- maxOutputTokens: 16384,
83+ maxOutputTokens: getMaxOutputTokens(input.modelString) ?? 16384,
8384 messages: input.prepared.messages,
8485 tools: input.toolSet,
8586 stopWhen: continueUntilModelStops,
src/session/db/connection.ts+5−4
@@ -13,7 +13,7 @@ function getDbPath(): string {
1313 return join(dir, "sessions.db");
1414 }
1515
16-const DB_VERSION = 5;
16+const DB_VERSION = 6;
1717
1818 const SCHEMA = `
1919 CREATE TABLE IF NOT EXISTS sessions (
@@ -65,6 +65,7 @@ const SCHEMA = `
6565 CREATE TABLE IF NOT EXISTS model_capabilities (
6666 canonical_model_id TEXT PRIMARY KEY,
6767 context_window INTEGER,
68+ max_output_tokens INTEGER,
6869 reasoning INTEGER NOT NULL,
6970 source_provider TEXT,
7071 raw_json TEXT,
@@ -208,9 +209,9 @@ export function pruneOldData(): void {
208209 });
209210 }
210211
211- // Passive checkpoint keeps startup opportunistic: if another process is using
212- // the database, we skip shrinking the WAL instead of failing the whole CLI.
212+ // TRUNCATE checkpoint shrinks the WAL file to zero after writes are applied.
213+ // Falls back gracefully if another process holds a read lock (busy-safe).
213214 runBestEffortMaintenance(() => {
214- db.exec("PRAGMA wal_checkpoint(PASSIVE);");
215+ db.exec("PRAGMA wal_checkpoint(TRUNCATE);");
215216 });
216217 }
src/session/db/model-info-repo.ts+5−2
@@ -3,6 +3,7 @@ import { getDb } from "./connection.ts";
33 export interface ModelCapabilityRow {
44 canonical_model_id: string;
55 context_window: number | null;
6+ max_output_tokens: number | null;
67 reasoning: number;
78 source_provider: string | null;
89 raw_json: string | null;
@@ -24,7 +25,7 @@ export function listModelCapabilities(): ModelCapabilityRow[] {
2425 .query<
2526 ModelCapabilityRow,
2627 []
27- >("SELECT canonical_model_id, context_window, reasoning, source_provider, raw_json, updated_at FROM model_capabilities")
28+ >("SELECT canonical_model_id, context_window, max_output_tokens, reasoning, source_provider, raw_json, updated_at FROM model_capabilities")
2829 .all();
2930 }
3031
@@ -34,11 +35,12 @@ export function replaceModelCapabilities(rows: ModelCapabilityRow[]): void {
3435 `INSERT INTO model_capabilities (
3536 canonical_model_id,
3637 context_window,
38+ max_output_tokens,
3739 reasoning,
3840 source_provider,
3941 raw_json,
4042 updated_at
41- ) VALUES (?, ?, ?, ?, ?, ?)`,
43+ ) VALUES (?, ?, ?, ?, ?, ?, ?)`,
4244 );
4345 const run = db.transaction(() => {
4446 db.run("DELETE FROM model_capabilities");
@@ -46,6 +48,7 @@ export function replaceModelCapabilities(rows: ModelCapabilityRow[]): void {
4648 insertStmt.run(
4749 row.canonical_model_id,
4850 row.context_window,
51+ row.max_output_tokens,
4952 row.reasoning,
5053 row.source_provider,
5154 row.raw_json,
src/tools/shell.ts+4−1
@@ -110,7 +110,10 @@ export async function runShellCommand(input: ShellInput): Promise<ShellOutput> {
110110 if (!value) continue;
111111
112112 if (totalBytes + value.length > MAX_OUTPUT_BYTES) {
113- chunks.push(value.slice(0, MAX_OUTPUT_BYTES - totalBytes));
113+ const partial = value.slice(0, MAX_OUTPUT_BYTES - totalBytes);
114+ // Snap to the last newline so we never cut mid-line.
115+ const lastNl = partial.lastIndexOf(10); // 0x0a = "\n"
116+ chunks.push(lastNl >= 0 ? partial.slice(0, lastNl + 1) : partial);
114117 truncated = true;
115118 reader.cancel().catch(() => {});
116119 break;