GeezerVison is showing github.com — fix(adapters,server): output budgets, adapter queue memory, and per-k… · lidge-jun/opencodex@447ac22
File treeExpand file treeCollapse file tree Open diff view settings devlog/_plan/260920_meaning_preservation_batch
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +128Lines changed: 128 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Lane E — output budgets, queue memory, per-key policy | | 2 | + | | 3 | +Status: OPEN. Branch `codex/260920-lane-e-budgets-key-policy` against `dev`, one pull request. | | 4 | +Covers phase 2 bundles 10, 11 and 12 from [010_phase2.md](010_phase2.md). | | 5 | + | | 6 | +## What each bundle turned out to be | | 7 | + | | 8 | +### 10 — Devin output budget and the history ceiling | | 9 | + | | 10 | +Two defects, not one. The adapter forwarded only a caller-supplied | | 11 | +`max_output_tokens`, and Codex never sends one, so every `devin/*` turn was capped at | | 12 | +the cloud-direct encoder's 8192 fallback however the provider was configured. The | | 13 | +escape hatch was closed too: no OAuth preset declares `defaultMaxOutputTokens` or | | 14 | +`modelMaxOutputTokens`, so the delete-when-preset-undefined branch in | | 15 | +`applyOAuthPresetCatalog` was the only branch either field ever took and a | | 16 | +hand-edited value was gone before the next startup finished. Both had to move, or | | 17 | +wiring the adapter alone would have been unreachable in practice. | | 18 | + | | 19 | +The resolver reads the caller's explicit value, then the configured per-model cap, | | 20 | +then the provider default, then nothing — leaving the encoder fallback. It never | | 21 | +reads `contextWindow` or `modelContextWindows`: CompletionConfiguration #2 is the | | 22 | +output cap and #3 is the context window, and collapsing them would ask Cognition to | | 23 | +generate a whole window of output. | | 24 | + | | 25 | +The history ceiling is the other half and stays a separate quantity. #5189 is | | 26 | +carried with attribution: it derives the coding-agent projected-history bound from | | 27 | +the declared context window in characters. That bounds replayed history memory; | | 28 | +nothing there decides how long a reply may run. | | 29 | + | | 30 | +No retry change was needed. Source review of `stated-reset-retry.ts` and | | 31 | +`upstream-retry.ts` confirms an upstream `incomplete / max_output_tokens` is a | | 32 | +successful streaming response that has already emitted events, so it matches none | | 33 | +of the replay conditions. The repeated identical attempts in #5190 are the client's. | | 34 | + | | 35 | +### 11 — adapter event queue memory | | 36 | + | | 37 | +PR #5182 had the right idea and the wrong number. Its 1 MiB aggregate default | | 38 | +aborts a legitimate turn: a synchronous producer fills the queue before its | | 39 | +consumer is scheduled, and the image loop does exactly that with over a million | | 40 | +one-character deltas that coalesce into roughly 1.2 MB of retained text. Its own CI | | 41 | +proved it, which is why it sits at `CHANGES_REQUESTED`. | | 42 | + | | 43 | +The work is carried with attribution and reshaped around two budgets rather than | | 44 | +one, because a stalled consumer and a malformed event are different failures and an | | 45 | +operator reading the terminal error should learn which happened. Accounting is now | | 46 | +exact by construction: each queued item records what it was charged, so a merge | | 47 | +pays only for appended text, a refused event is priced before anything is retained | | 48 | +and never charged, and the terminal record explaining a refusal is admitted past | | 49 | +the budget it reports but still charged and released. `retainedCodeUnits()` exposes | | 50 | +the counter so the regressions assert it reaches zero rather than inferring it from | | 51 | +an abort that happened to fire. | | 52 | + | | 53 | +Retention is measured by a bounded walk of own enumerable properties rather than a | | 54 | +per-variant table. A table would be exhaustive over `AdapterEvent`, which is the | | 55 | +union class `AGENTS.md` records: a member added on another branch would silently | | 56 | +stop being counted. | | 57 | + | | 58 | +### 12 — per-admission-key model and provider scope | | 59 | + | | 60 | +The security question is where the check goes, not what it compares. A scope | | 61 | +evaluated against the client's string authorizes one destination and reaches | | 62 | +another, because alias resolution, policy and combo selection, subagent fallback | | 63 | +and compaction override all rewrite that string. So the scope names destinations | | 64 | +and is applied to the resolved route. | | 65 | + | | 66 | +On the Responses path every route produced by the request — direct name, alias, | | 67 | +policy, combo child, shadow-intercept target and both subagent-fallback re-routes — | | 68 | +passes through one capture point, which is where the check sits. Chat and Messages | | 69 | +translate into that path; their native lanes and the compaction route send without | | 70 | +re-entering it, so each applies the same predicate itself. `/v1/models` filters by | | 71 | +the same predicate, and that filter is explicitly not the boundary. | | 72 | + | | 73 | +A malformed scope drops the key rather than degrading to `undefined` like every | | 74 | +other field on the record, because degrading a permission field reads as "allowed | | 75 | +everything". | | 76 | + | | 77 | +Out of scope and deliberately not started: Redis, a full multi-tenant conversion, | | 78 | +and any budget or RPM/TPM system. | | 79 | + | | 80 | +#### What the scope does not cover, stated rather than implied | | 81 | + | | 82 | +An adversarial review of the branch found authenticated data-plane endpoints that | | 83 | +spend provider quota without resolving a model through the router, so the scope | | 84 | +does not reach them: | | 85 | + | | 86 | +- `/v1/images/generations` and `/v1/images/edits`, | | 87 | +- `/v1/audio/transcriptions` and its streaming form, | | 88 | +- `/v1/live`, `/v1/realtime/calls` and the standalone realtime sockets, | | 89 | +- the non-account-qualified branch of `/v1/alpha/search`, which forwards the caller's | | 90 | + model to a search sidecar without routing it. | | 91 | + | | 92 | +The account-qualified search branch does route a model and is checked. The rest | | 93 | +need a destination definition this lane does not own — an image or audio endpoint | | 94 | +has a fixed-purpose model rather than a routed one — and inventing one here would | | 95 | +be the multi-tenant expansion this batch rules out. They are recorded so the | | 96 | +contract is not read as broader than it is. | | 97 | + | | 98 | +The review also found that resolving an OpenAI virtual model rewrites | | 99 | +`route.modelId` to the wire id after the initial check. That one was a real hole in | | 100 | +the stated contract and is fixed: the settled route is re-checked after | | 101 | +normalization, so the id that is billed is the id that was authorized. | | 102 | + | | 103 | +## Verification | | 104 | + | | 105 | +Static source review plus exact-head hosted CI. No local suite, individual test, | | 106 | +typecheck, build, install or live `ocx` execution was run — those are NOT RUN, not | | 107 | +passing. | | 108 | + | | 109 | +Union-defect classes checked before pushing. No file in the touched set carries a | | 110 | +`file-size-baseline.json` cap; the largest, `src/oauth/index.ts` and | | 111 | +`src/server/index/serve-options.ts`, stay under the 2000-line new-file threshold. | | 112 | +The two new test files are registered in both `scripts/test-layout/layout.json` and | | 113 | +`tests/fixtures/test-layout-expected.json`. Nothing here restates a count or | | 114 | +enumerates a union. | | 115 | + | | 116 | +## Ownership | | 117 | + | | 118 | +Lane E owns the adapter event queue budget and the Devin and coding-agent limits. | | 119 | +Retry classification — `sendCount`, the send budget, the stage and cause vocabulary — | | 120 | +is lane C's and is untouched. | | 121 | + | | 122 | +## Carried work | | 123 | + | | 124 | +- #5182 (luvs01) — adapter event queue backlog budget. | | 125 | +- #5189 (mdwsk88) — coding-agent projected-history ceiling. | | 126 | + | | 127 | +Both carry a `Co-authored-by` trailer in the branch commit. Neither original pull | | 128 | +request is closed here; the coordinator handles that after this lane lands. |
Collapse file Copy file name to clipboardExpand all lines: scripts/test-layout/layout.json +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1566 | 1566 | "web-search-sidecar-429.test.ts": "web-search", | 1567 | 1567 | "management-google-tool-schema-policy.test.ts": "server", | 1568 | 1568 | "codex-shim-destroyed-probe.test.ts": "codex-integration", | 1569 | | - "client-runtime.test.ts": "clients" | | 1569 | + "client-runtime.test.ts": "clients", | | 1570 | + "devin-output-budget.test.ts": "providers", | | 1571 | + "api-key-model-scope.test.ts": "server" | 1570 | 1572 | }, | 1571 | 1573 | "migrated": [ | 1572 | 1574 | "adapters", | |
Collapse file Copy file name to clipboardExpand all lines: src/adapters/coding-agent/protocol.ts +28-3Lines changed: 28 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,6 +21,30 @@ export const MAX_STREAM_LINE_BYTES = 8 * 1024 * 1024; | 21 | 21 | export const MAX_STREAM_TOTAL_BYTES = 64 * 1024 * 1024; | 22 | 22 | /** Hard ceiling on projected conversation history text (characters) to prevent runaway memory. */ | 23 | 23 | export const MAX_PROJECTED_HISTORY_CHARS = 200_000; | | 24 | +/** | | 25 | + * Chars-per-token ratio for deriving the projected-history ceiling from the model context | | 26 | + * window. Sits between the English/code (~4 chars per token) and CJK (~1.5) extremes: the | | 27 | + * ceiling is a runaway-memory bound and a coarse guard against cutting history the window can | | 28 | + * hold, not a token accounting - the caller-side compaction line stays the token authority. | | 29 | + */ | | 30 | +const PROJECTED_HISTORY_CHARS_PER_TOKEN = 3; | | 31 | +/** Absolute ceiling on a window-derived history cap, so runaway metadata cannot unbound stdin. */ | | 32 | +const MAX_PROJECTED_HISTORY_DERIVED_CHARS = 4_000_000; | | 33 | + | | 34 | +/** | | 35 | + * Projected-history character ceiling for a turn, derived from the declared model context | | 36 | + * window. A missing or non-finite window keeps the legacy flat cap, and the derivation never | | 37 | + * lowers the cap below it: small windows change nothing, while large windows scale (a 1M-token | | 38 | + * model keeps 3M characters) until the hard ceiling. The flat 200k cap predates window | | 39 | + * metadata and cut long replays to roughly 50k-130k tokens of content regardless of the model. | | 40 | + */ | | 41 | +export function projectedHistoryCharLimit(contextWindowTokens: number | undefined): number { | | 42 | + if (typeof contextWindowTokens !== "number" || !Number.isFinite(contextWindowTokens) || contextWindowTokens <= 0) { | | 43 | + return MAX_PROJECTED_HISTORY_CHARS; | | 44 | + } | | 45 | + const derived = contextWindowTokens * PROJECTED_HISTORY_CHARS_PER_TOKEN; | | 46 | + return Math.min(Math.max(derived, MAX_PROJECTED_HISTORY_CHARS), MAX_PROJECTED_HISTORY_DERIVED_CHARS); | | 47 | +} | 24 | 48 |
| 25 | 49 | export class CodingAgentStreamLimitError extends Error { | 26 | 50 | constructor(message: string) { | @@ -395,7 +419,7 @@ export function buildSystemPrompt(parsed: OcxParsedRequest): string | undefined | 395 | 419 | * prior conversation turns are structured as bounded context text with tool results as text, | 396 | 420 | * clearly demarcated from the current user request. Codex retains tool control; vendor tools are never invoked. | 397 | 421 | */ | 398 | | -export function buildConversationInput(parsed: OcxParsedRequest): string[] { | | 422 | +export function buildConversationInput(parsed: OcxParsedRequest, options: { maxHistoryChars?: number } = {}): string[] { | 399 | 423 | const nonDev = parsed.context.messages.filter(m => m.role !== "developer"); | 400 | 424 | if (nonDev.length === 0) { | 401 | 425 | return [JSON.stringify({ type: "user", message: { role: "user", content: [{ type: "text", text: "" }] } })]; | @@ -475,10 +499,11 @@ export function buildConversationInput(parsed: OcxParsedRequest): string[] { | 475 | 499 |
| 476 | 500 | const imageBlocks: WireContentPart[] = [...historyImageBlocks, ...currentImageBlocks]; | 477 | 501 |
| | 502 | + const maxHistoryChars = options.maxHistoryChars ?? MAX_PROJECTED_HISTORY_CHARS; | 478 | 503 | let historyText = historyMessages.map(formatMessageForHistory).filter(Boolean).join("\n\n"); | 479 | | - if (historyText.length > MAX_PROJECTED_HISTORY_CHARS) { | | 504 | + if (historyText.length > maxHistoryChars) { | 480 | 505 | historyText = `[Earlier conversation history truncated for length...]\n\n` + | 481 | | - historyText.slice(historyText.length - MAX_PROJECTED_HISTORY_CHARS); | | 506 | + historyText.slice(historyText.length - maxHistoryChars); | 482 | 507 | } | 483 | 508 |
| 484 | 509 | const combinedText = `Prior conversation context:\n\n${historyText}\n\nCurrent user request:\n\n${currentRequestText}`; | |
Collapse file Copy file name to clipboardExpand all lines: src/adapters/coding-agent/turn.ts +10-2Lines changed: 10 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { execFileSync, spawn as nodeSpawn, type ChildProcess, type SpawnOptions } from "node:child_process"; | 2 | 2 | import type { AdapterEvent, OcxParsedRequest, OcxProviderConfig } from "../../types"; | 3 | 3 | import { commandInvocation } from "../../lib/win-exec"; | | 4 | +import { modelRecordValue } from "../../reasoning-effort"; | 4 | 5 | import type { IncomingMeta } from "../base"; | 5 | | -import { buildConversationInput, CodingAgentProtocolError, mapStreamMessageToEvents, readJsonLines, type StreamParseState } from "./protocol"; | | 6 | +import { buildConversationInput, CodingAgentProtocolError, mapStreamMessageToEvents, projectedHistoryCharLimit, readJsonLines, type StreamParseState } from "./protocol"; | 6 | 7 | import { resolveCodingAgentBinary, resolveProfileByBaseUrl, type CodingAgentProviderProfile, type WhichFn } from "./profile"; | 7 | 8 |
| 8 | 9 | /** Injectable spawn for tests; production uses node:child_process. */ | @@ -264,7 +265,14 @@ export async function runCodingAgentTurn(input: CodingAgentTurnInput): Promise<v | 264 | 265 | const stdin = child.stdin; | 265 | 266 | if (stdin) { | 266 | 267 | stdin.on("error", () => { /* EPIPE if the CLI exits early; surfaced via close/stderr */ }); | 267 | | - for (const line of buildConversationInput(parsed)) stdin.write(`${line}\n`); | | 268 | + // The projected history scales with the model context window on the routed provider row | | 269 | + // (catalog and config metadata merged): a 1M-token model keeps 3M characters of replay | | 270 | + // where the flat cap cut it near 50k-130k tokens of content. Absent metadata keeps the | | 271 | + // flat cap. | | 272 | + const historyCharLimit = projectedHistoryCharLimit( | | 273 | + modelRecordValue(provider.modelContextWindows, parsed.modelId) ?? provider.contextWindow, | | 274 | + ); | | 275 | + for (const line of buildConversationInput(parsed, { maxHistoryChars: historyCharLimit })) stdin.write(`${line}\n`); | 268 | 276 | stdin.end(); | 269 | 277 | } | 270 | 278 | const stdout = child.stdout; | |
Collapse file Copy file name to clipboardExpand all lines: src/adapters/devin.ts +72-23Lines changed: 72 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -192,6 +192,35 @@ async function resolveWireModelUid( | 192 | 192 | */ | 193 | 193 | export const resolveWireModelUidForTests = resolveWireModelUid; | 194 | 194 |
| | 195 | +const positiveTokenCount = (value: unknown): number | undefined => | | 196 | + typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined; | | 197 | + | | 198 | +/** | | 199 | + * Read a per-model token count for the exact UID selected for this turn. | | 200 | + * | | 201 | + * Tries the selected UID and then its collapsed base id, preferring the | | 202 | + * canonical spelling and accepting dotted or case-folded saved hints — the same | | 203 | + * normalization the inference request applies to the model id. Where several | | 204 | + * spellings match one id, the smallest wins: a ceiling stated twice is | | 205 | + * satisfied by the lower statement. | | 206 | + */ | | 207 | +function devinModelTokenHint( | | 208 | + record: Record<string, number> | undefined, | | 209 | + modelUid: string, | | 210 | +): number | undefined { | | 211 | + if (!record) return undefined; | | 212 | + for (const id of [modelUid, collapseDevinModelUid(modelUid)]) { | | 213 | + const exact = Object.hasOwn(record, id) ? positiveTokenCount(record[id]) : undefined; | | 214 | + if (exact !== undefined) return exact; | | 215 | + const matches = Object.entries(record) | | 216 | + .filter(([key]) => normalizeDevinModelId(key).toLowerCase() === id.toLowerCase()) | | 217 | + .map(([, value]) => positiveTokenCount(value)) | | 218 | + .filter((value): value is number => value !== undefined); | | 219 | + if (matches.length > 0) return Math.min(...matches); | | 220 | + } | | 221 | + return undefined; | | 222 | +} | | 223 | + | 195 | 224 | /** | 196 | 225 | * Resolve the INPUT ceiling for the exact UID selected for this turn. Catalog | 197 | 226 | * ClientModelConfig #18 and CompletionConfiguration #3 both carry input tokens; | @@ -204,33 +233,50 @@ function resolveDevinMaxInputTokens( | 204 | 233 | modelUid: string, | 205 | 234 | liveWindow?: number, | 206 | 235 | ): number | undefined { | 207 | | - const positive = (value: unknown): number | undefined => | 208 | | - typeof value === "number" && Number.isSafeInteger(value) && value > 0 ? value : undefined; | 209 | | - const baseId = collapseDevinModelUid(modelUid); | 210 | | - const configured = (record: Record<string, number> | undefined): number | undefined => { | 211 | | - if (!record) return undefined; | 212 | | - for (const id of [modelUid, baseId]) { | 213 | | - // Prefer the canonical spelling; retain dotted/case-folded saved hints, | 214 | | - // matching the model-id normalization used for the inference request. | 215 | | - const exact = Object.hasOwn(record, id) ? positive(record[id]) : undefined; | 216 | | - if (exact !== undefined) return exact; | 217 | | - const matches = Object.entries(record) | 218 | | - .filter(([key]) => normalizeDevinModelId(key).toLowerCase() === id.toLowerCase()) | 219 | | - .map(([, value]) => positive(value)) | 220 | | - .filter((value): value is number => value !== undefined); | 221 | | - if (matches.length > 0) return Math.min(...matches); | 222 | | - } | 223 | | - return undefined; | 224 | | - }; | 225 | | - const contextHint = configured(provider.modelContextWindows) ?? positive(provider.contextWindow); | 226 | | - const inputHint = configured(provider.modelMaxInputTokens); | 227 | | - const ceilings = [positive(liveWindow), contextHint, inputHint] | | 236 | + const contextHint = devinModelTokenHint(provider.modelContextWindows, modelUid) | | 237 | + ?? positiveTokenCount(provider.contextWindow); | | 238 | + const inputHint = devinModelTokenHint(provider.modelMaxInputTokens, modelUid); | | 239 | + const ceilings = [positiveTokenCount(liveWindow), contextHint, inputHint] | 228 | 240 | .filter((value): value is number => value !== undefined); | 229 | 241 | return ceilings.length > 0 ? Math.min(...ceilings) : undefined; | 230 | 242 | } | 231 | 243 |
| 232 | | -/** Pure test seam; runtime uses the same resolver immediately before dispatch. */ | | 244 | +/** | | 245 | + * Resolve the OUTPUT ceiling for this turn, highest authority first: | | 246 | + * | | 247 | + * 1. the caller's explicit `max_output_tokens`, forwarded unchanged — an | | 248 | + * explicit cap is a request, so a small one is never widened into a | | 249 | + * configured larger one; | | 250 | + * 2. the configured per-model cap (`modelMaxOutputTokens`), read through the | | 251 | + * same UID-aware hint lookup the input ceiling uses; | | 252 | + * 3. the provider-wide `defaultMaxOutputTokens`; | | 253 | + * 4. undefined, which leaves the cloud-direct encoder's own 8192 fallback in | | 254 | + * place for a provider that configured nothing. | | 255 | + * | | 256 | + * This is NOT the history ceiling, and the two must not collapse into one | | 257 | + * number. CompletionConfiguration #2 is the output cap and #3 is the context | | 258 | + * window, so feeding a context window into this resolver would ask Cognition to | | 259 | + * generate a whole window's worth of output. Nothing here reads | | 260 | + * `contextWindow` or `modelContextWindows` for that reason. | | 261 | + * | | 262 | + * Step 1 keeps the caller's raw value rather than `positiveTokenCount`: the | | 263 | + * inbound parser owns what a caller may send, and re-filtering here would | | 264 | + * silently promote a rejected value to a configured cap the caller never asked | | 265 | + * for. | | 266 | + */ | | 267 | +function resolveDevinMaxOutputTokens( | | 268 | + provider: OcxProviderConfig, | | 269 | + modelUid: string, | | 270 | + requested: number | undefined, | | 271 | +): number | undefined { | | 272 | + if (typeof requested === "number") return requested; | | 273 | + return devinModelTokenHint(provider.modelMaxOutputTokens, modelUid) | | 274 | + ?? positiveTokenCount(provider.defaultMaxOutputTokens); | | 275 | +} | | 276 | + | | 277 | +/** Pure test seams; runtime uses the same resolvers immediately before dispatch. */ | 233 | 278 | export const resolveDevinMaxInputTokensForTests = resolveDevinMaxInputTokens; | | 279 | +export const resolveDevinMaxOutputTokensForTests = resolveDevinMaxOutputTokens; | 234 | 280 |
| 235 | 281 | export class DevinMissingCredentialError extends Error { | 236 | 282 | constructor() { | @@ -590,6 +636,9 @@ export function createDevinAdapter( | 590 | 636 | const maxInputTokens = resolveDevinMaxInputTokens( | 591 | 637 | provider, modelUid, catalog?.byUid.get(modelUid)?.contextWindow, | 592 | 638 | ); | | 639 | + const maxOutputTokens = resolveDevinMaxOutputTokens( | | 640 | + provider, modelUid, parsed.options.maxOutputTokens, | | 641 | + ); | 593 | 642 | // The reset-retry wrapper waits out a 429 that states its own recovery | 594 | 643 | // delay ("limit will reset in 35 seconds") and replays the identical | 595 | 644 | // request — but only while zero events have been yielded, so a | @@ -606,7 +655,7 @@ export function createDevinAdapter( | 606 | 655 | // input hint used to force every model through the 128k default. | 607 | 656 | completionOpts: { | 608 | 657 | ...(maxInputTokens !== undefined ? { maxInputTokens } : {}), | 609 | | - ...(typeof parsed.options.maxOutputTokens === "number" ? { maxOutputTokens: parsed.options.maxOutputTokens } : {}), | | 658 | + ...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}), | 610 | 659 | ...(typeof parsed.options.temperature === "number" ? { temperature: parsed.options.temperature } : {}), | 611 | 660 | ...(typeof parsed.options.topP === "number" ? { topP: parsed.options.topP } : {}), | 612 | 661 | }, | |
|