Skip to content

Commit d84edae

Browse files
wtfsayoclaude
andcommitted
fix(devin): measured overflow boundary, per-type Gemini branches, tool-error marker
- Overflow: binary-searched live on swe-1-6 (200k window) the refusal sits at ~200-203k real tokens for prose and JSON alike, and a 32000 output cap does not move it. Characters per token ran 1.33-5.53 across samples, so the chars/4 estimate is replaced by a word-piece count (1.00-1.54x real) at 95% of the window: every sample at the window is caught, none at 60% is. - Gemini schemas: `{type:[T,"null"], ...}` becomes anyOf branches that carry the type-specific keywords (items, properties, ...); an existing anyOf is folded in rather than nested under allOf. Draft-7 `dependencies` is walked as a schema map with name lists left as data. - Tool errors keep the in-band ERROR: marker beside #9: with a neutral result flagged as an error only gemini reported a failure; swe-1-6, gpt-6-sol-low and gpt-5-6-luna-low read it as success. - A request with only system text keeps it as a user prompt instead of sending no prompts. - Docs: describe the context_length_exceeded reclassification beside the allowDevinInvalidArgument recovery option. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 29688bf commit d84edae

8 files changed

Lines changed: 162 additions & 42 deletions

File tree

‎docs-site/src/content/docs/guides/codex-integration.md‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,14 @@ change sign-in, the conversation's ordinary model, Codex's provider ID, or the d
504504

505505
Recovery does not replay after cancellation, semantic output, tool side effects, an exhausted
506506
send budget, or an authentication, admission or policy refusal. Generic `400` errors do not
507-
enable fallback. The separately opted-in Devin `invalid_argument` case applies only to an
508-
identified compaction failure from that adapter. The emergency attempt shares the original
509-
request's send budget and never starts a second recovery attempt.
507+
enable fallback. Devin answers an oversized history with an opaque pre-output
508+
`invalid_argument`; when the request's estimated size is at or near the model's input window,
509+
the adapter reports it as `context_length_exceeded` instead, so Codex compacts on an ordinary
510+
turn and a failed compaction qualifies as a context overflow without any Devin-specific option.
511+
A smaller request that gets the same code stays a plain `400`. The separately opted-in
512+
`allowDevinInvalidArgument` case covers only those remaining `invalid_argument` failures, and
513+
only on an identified compaction request. The emergency attempt shares the original request's
514+
send budget and never starts a second recovery attempt.
510515

511516
Native encrypted compaction is outside this recovery path: its original error is retained.
512517
There is no automatic local truncation mode. A response being accepted is not proof that a

‎src/adapters/devin.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,19 @@ function mapOneMessage(message: OcxMessage): ChatHistoryItem | undefined {
452452
};
453453
}
454454
if (message.role === "toolResult") {
455+
// #9 alone is not enough: live, with a neutral "hello world" result flagged
456+
// as an error, only gemini-3-8-flash reported a failure; swe-1-6,
457+
// gpt-6-sol-low and gpt-5-6-luna-low read it as success. So the flag rides
458+
// with the in-band marker rather than replacing it.
459+
const wireContent = mapOcxContentToWire(message.content);
460+
const toolContent = message.isError
461+
? (typeof wireContent === "string"
462+
? `ERROR: ${wireContent}`
463+
: [{ type: "text", text: "ERROR:" } as ContentPart, ...wireContent])
464+
: wireContent;
455465
return {
456466
role: "tool",
457-
content: mapOcxContentToWire(message.content),
467+
content: toolContent,
458468
tool_call_id: message.toolCallId,
459469
...(message.isError ? { is_error: true } : {}),
460470
};
@@ -761,7 +771,7 @@ export function createDevinAdapter(
761771
// Converting it to an adapter event would make it an ordinary untyped upstream error.
762772
if (error instanceof SendBudgetExhaustedError) throw error;
763773
if (error instanceof CloudChatError && isDevinHistoryOverflow({
764-
code: error.code, producedOutput, modelUid, contextWindow: maxInputTokens, messages, tools,
774+
code: error.code, producedOutput, contextWindow: maxInputTokens, messages, tools,
765775
})) {
766776
emit({ ...devinContextOverflowEvent(), ...(usage ? { usage } : {}) });
767777
return;

‎src/adapters/devin/cloud-direct/chat.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ function encodeChatMessagePrompt(
237237
if (opts?.toolCallId) {
238238
parts.push(encodeString(7, opts.toolCallId));
239239
}
240-
// Accepted live on a tool prompt, and the model reported the failure from the
241-
// flag alone, so a failed tool result needs no in-band text marker.
240+
// Accepted live on a tool prompt. Only some models act on it, so the adapter
241+
// also keeps an in-band marker in the text.
242242
if (opts?.isError) parts.push(encodeVarintField(9, 1));
243243
// Assistant message with tool_calls: encode each as a ChatToolCall.
244244
if (opts?.toolCalls && opts.toolCalls.length > 0) {
@@ -672,8 +672,10 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
672672
// from cache in #2, against 7072 of 7097 when the same text was collapsed
673673
// into the first user prompt, so the cache ratio is unchanged and the prompt
674674
// is smaller. The model obeyed an instruction given only in #2.
675+
// A request with only system text keeps it as a user prompt: #2 alone would
676+
// leave the request with no prompt at all.
675677
const firstNonSystem = args.messages.findIndex((m) => m.role !== 'system');
676-
const leadingSystem = firstNonSystem === -1 ? args.messages : args.messages.slice(0, firstNonSystem);
678+
const leadingSystem = firstNonSystem === -1 ? [] : args.messages.slice(0, firstNonSystem);
677679
const systemPrompt = leadingSystem
678680
.map((m) => normalizeContent(m.content)
679681
.filter((p): p is { type: 'text'; text: string } => p.type === 'text')

‎src/adapters/devin/cloud-direct/tool-schema.ts‎

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,75 @@ export function isDevinGeminiModelUid(modelUid: string): boolean {
1818
/** Keys whose values are instance data, not subschemas. */
1919
const DATA_KEYS = new Set(['enum', 'const', 'default', 'examples', 'example']);
2020
/** Keys whose values map arbitrary names (which may be "enum" or "default") to subschemas. */
21-
const SCHEMA_MAP_KEYS = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas']);
21+
const SCHEMA_MAP_KEYS = new Set(['properties', 'patternProperties', '$defs', 'definitions', 'dependentSchemas', 'dependencies']);
22+
/** Keys that describe the whole node and stay on it rather than moving into each branch. */
23+
const ANNOTATION_KEYS = new Set(['description', 'title', 'default', 'examples', 'example', '$comment', 'deprecated']);
2224

2325
function rewriteMap(map: unknown): unknown {
2426
if (!map || typeof map !== 'object' || Array.isArray(map)) return map;
25-
return Object.fromEntries(Object.entries(map).map(([name, schema]) => [name, rewrite(schema)]));
27+
// A draft-7 `dependencies` entry may be a list of property names, which is data.
28+
return Object.fromEntries(Object.entries(map).map(([name, schema]) => [name, Array.isArray(schema) ? schema : rewrite(schema)]));
29+
}
30+
31+
type Schema = Record<string, unknown>;
32+
const isSchema = (value: unknown): value is Schema => !!value && typeof value === 'object' && !Array.isArray(value);
33+
34+
/**
35+
* `{type: [T, "null"], ...rest}` becomes `{anyOf: [{...rest, type: T}, {type: "null"}]}`,
36+
* so type-specific keywords (items, properties, ...) stay attached to their type.
37+
* An existing anyOf is folded in branch by branch rather than nested under allOf.
38+
*/
39+
function splitTypeArray(node: Schema, types: unknown[]): Schema {
40+
const annotations: Schema = {};
41+
const rest: Schema = {};
42+
for (const [key, value] of Object.entries(node)) {
43+
if (key === 'type' || key === 'anyOf') continue;
44+
Object.defineProperty(ANNOTATION_KEYS.has(key) ? annotations : rest, key, { value, enumerable: true, writable: true, configurable: true });
45+
}
46+
const concrete = types.filter((type) => type !== 'null');
47+
const allowsNull = concrete.length < types.length;
48+
const existing = Array.isArray(node.anyOf) ? node.anyOf : undefined;
49+
const branches: unknown[] = [];
50+
let nullReachable = allowsNull && !existing;
51+
if (!existing) {
52+
for (const type of concrete) branches.push({ ...rest, type });
53+
} else {
54+
for (const branch of existing) {
55+
if (!isSchema(branch)) continue;
56+
if (branch.type === undefined) {
57+
for (const type of concrete) branches.push({ ...rest, ...branch, type });
58+
if (allowsNull) nullReachable = true;
59+
} else if (branch.type === 'null') {
60+
if (allowsNull) nullReachable = true;
61+
} else if (concrete.includes(branch.type)) {
62+
branches.push({ ...rest, ...branch });
63+
}
64+
}
65+
}
66+
if (nullReachable) branches.push({ type: 'null' });
67+
// Nothing satisfies both unions; keep the existing anyOf rather than emit an empty one.
68+
if (branches.length === 0) return { ...annotations, ...rest, anyOf: existing };
69+
if (branches.length === 1 && isSchema(branches[0])) return { ...annotations, ...branches[0] };
70+
return { ...annotations, anyOf: branches };
2671
}
2772

2873
function rewrite(node: unknown): unknown {
2974
if (Array.isArray(node)) return node.map(rewrite);
30-
if (!node || typeof node !== 'object') return node;
75+
if (!isSchema(node)) return node;
3176
// fromEntries defines own properties, so a "__proto__" key stays data.
32-
const out: Record<string, unknown> = Object.fromEntries(
33-
Object.entries(node as Record<string, unknown>).map(([key, value]) => [
77+
const out: Schema = Object.fromEntries(
78+
Object.entries(node).map(([key, value]) => [
3479
key,
3580
DATA_KEYS.has(key) ? value : SCHEMA_MAP_KEYS.has(key) ? rewriteMap(value) : rewrite(value),
3681
]),
3782
);
3883
if (!Array.isArray(out.type)) return out;
3984
const types = out.type as unknown[];
40-
delete out.type;
41-
if (types.length === 1) return { ...out, type: types[0] };
42-
if (types.length === 0) return out;
43-
const union = types.map((type) => ({ type }));
44-
if (out.anyOf === undefined) return { ...out, anyOf: union };
45-
// Both constraints must hold, so an existing anyOf is kept beside the new one.
46-
const allOf = Array.isArray(out.allOf) ? out.allOf : [];
47-
return { ...out, allOf: [...allOf, { anyOf: union }] };
85+
if (types.length === 0) {
86+
delete out.type;
87+
return out;
88+
}
89+
return splitTypeArray(out, types);
4890
}
4991

5092
/** Rewrite type arrays for Gemini uids; every other model gets the schema unchanged. */

‎src/adapters/devin/context-overflow.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@
77
* full and never compacts, so every later turn dead-ends the same way. Only a
88
* request large against the model's window is reclassified; a small request
99
* with the same code stays an ordinary 400.
10+
*
11+
* Boundary, binary-searched live on swe-1-6 (200k catalog window, 8192 output):
12+
* accepted at 199,186 prompt tokens of prose and 200,345 of JSON, refused at
13+
* ~203k of either. Raising the output cap to 32000 did not move it (195,571
14+
* input tokens still accepted), so the window is not reduced by the output
15+
* reservation and the threshold is the window itself.
1016
*/
1117
import type { AdapterEvent } from "../../types";
12-
import { estimateTokens } from "../../lib/token-estimate";
1318
import type { ChatHistoryItem, ToolDef } from "./cloud-direct";
1419

15-
/** Share of the window the estimate must reach; the chars-per-token estimate can run ~25% low. */
16-
const WINDOW_SHARE = 0.8;
20+
/**
21+
* Share of the window the estimate must reach. Characters per real token ran
22+
* from 1.33 (Korean) through 2.31 (JSON) and 4.23 (source code) to 5.53
23+
* (prose), so no character ratio separates "over the window" from "60% of it".
24+
* The word-piece count below read 1.00x to 1.54x of the real count on those
25+
* samples: at 0.95 every sample at the window is caught and none at 60% is.
26+
*/
27+
const WINDOW_SHARE = 0.95;
28+
/** One token per short letter run, 1-3 digit group, or other visible character. */
29+
const WORD_PIECE = /[A-Z]?[a-z]{1,8}|[A-Z]{1,8}(?![a-z])|\d{1,3}|\S/g;
1730
/** Text size that counts as oversized when the model's window is unknown. */
1831
const UNKNOWN_WINDOW_CHARS = 512 * 1024;
1932

@@ -36,15 +49,16 @@ function requestText(messages: ChatHistoryItem[], tools: ToolDef[] | undefined):
3649
export function isDevinHistoryOverflow(input: {
3750
code: string | undefined;
3851
producedOutput: boolean;
39-
modelUid: string;
4052
contextWindow: number | undefined;
4153
messages: ChatHistoryItem[];
4254
tools: ToolDef[] | undefined;
4355
}): boolean {
4456
if (input.code !== "invalid_argument" || input.producedOutput) return false;
4557
const text = requestText(input.messages, input.tools);
4658
if (!input.contextWindow) return text.length >= UNKNOWN_WINDOW_CHARS;
47-
return estimateTokens(text, input.modelUid) >= input.contextWindow * WINDOW_SHARE;
59+
let pieces = 0;
60+
for (const _ of text.matchAll(WORD_PIECE)) pieces++;
61+
return pieces >= input.contextWindow * WINDOW_SHARE;
4862
}
4963

5064
/** Same terminal shape the Kiro adapter uses, which Codex reads as "context full, compact". */

‎structure/providers-and-adapters.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ rewrite rules and the routed-id settlement.
127127
| `src/adapters/declaration-carrier.ts`, `src/adapters/input-media-guard.ts` | Default-deny allowlists for constraints the normalized request carries but a wire may not be able to express: `tools[*].allowed_callers`, which fences a tool off from callers, and inline document bytes. Both are refused with a 400 at the single guard every registered adapter passes through, rather than left to each adapter, because an adapter that never learned about the carrier rebuilds without it and answers normally. `allowed_callers` reaches the `anthropic` wire; document bytes reach `anthropic`, `openai-chat` and `google`; the `openai-responses` wire is exempt from the whole guard because it forwards the original body. Adding an `AdapterWire` member makes the omission visible in these lists instead of at a customer's upstream. The unrestricted `["direct"]` caller default is not a restriction. |
128128
| `src/adapters/azure.ts` | Azure OpenAI bridge. |
129129
| `src/adapters/cursor.ts`, `src/adapters/cursor/` | Cursor protobuf transport: discovery, request builder, event decoding, MCP, thread continuity, native-exec policy. |
130-
| `src/adapters/devin.ts`, `src/adapters/devin/cloud-direct/` | Devin runTurn transport over Cognition Connect-RPC. `GetChatMessage` uses the Responses provider executor and shared physical-send budget; catalog, JWT, and `src/web-search/devin-executor.ts` native search support RPCs remain outside inference-send accounting. Provider-stated pre-output 429 reset delays are surfaced immediately by default, releasing shared active-turn capacity. A positive `OPENCODEX_DEVIN_STATED_RESET_WAIT_MS` explicitly enables bounded waiting and up to two replays on standalone turns, which hold that capacity until completion or cancellation. Combo children bypass that wait and surface a pre-output 429 so the next target can run. During an opted-in standalone wait, safe heartbeats commit the response preflight and keep the stream's stall watchdog fed. Invalid values fail closed to the immediate-refusal behavior. A recorded tenant host is used only for the stored account whose credential owns the transmitted key, searched in the configured provider id and then its deprecated alias; a configured, forwarded, or unmatched key uses the configured base URL or the US default. Native search previews the current route by effective adapter without mutating combo selection state, pins one admitted active-account snapshot for the request, and calls `GetWebSearchResults`, so it starts no CLI or second model. The leading system text is sent as `GetChatMessage` #2 rather than folded into the first user prompt, a failed tool result sets ChatMessagePrompt #9, and Gemini uids have JSON-Schema type arrays rewritten to `anyOf` in tool parameters. A pre-output `invalid_argument` on a history estimated at 80% or more of the selected UID's input window (512 KiB of text when no window is known) is surfaced as `context_length_exceeded` so Codex compacts; a small request with the same code stays a plain 400. |
130+
| `src/adapters/devin.ts`, `src/adapters/devin/cloud-direct/` | Devin runTurn transport over Cognition Connect-RPC. `GetChatMessage` uses the Responses provider executor and shared physical-send budget; catalog, JWT, and `src/web-search/devin-executor.ts` native search support RPCs remain outside inference-send accounting. Provider-stated pre-output 429 reset delays are surfaced immediately by default, releasing shared active-turn capacity. A positive `OPENCODEX_DEVIN_STATED_RESET_WAIT_MS` explicitly enables bounded waiting and up to two replays on standalone turns, which hold that capacity until completion or cancellation. Combo children bypass that wait and surface a pre-output 429 so the next target can run. During an opted-in standalone wait, safe heartbeats commit the response preflight and keep the stream's stall watchdog fed. Invalid values fail closed to the immediate-refusal behavior. A recorded tenant host is used only for the stored account whose credential owns the transmitted key, searched in the configured provider id and then its deprecated alias; a configured, forwarded, or unmatched key uses the configured base URL or the US default. Native search previews the current route by effective adapter without mutating combo selection state, pins one admitted active-account snapshot for the request, and calls `GetWebSearchResults`, so it starts no CLI or second model. The leading system text is sent as `GetChatMessage` #2 rather than folded into the first user prompt, a failed tool result sets ChatMessagePrompt #9 and keeps its in-band `ERROR:` marker (most models ignore the flag alone), and Gemini uids have JSON-Schema type arrays split into per-type `anyOf` branches in tool parameters. A pre-output `invalid_argument` on a history whose word-piece estimate reaches 95% of the selected UID's input window (512 KiB of text when no window is known) is surfaced as `context_length_exceeded` so Codex compacts; a small request with the same code stays a plain 400. |
131131
| `src/adapters/kiro.ts` and `src/adapters/kiro/` | Kiro event/tool/thinking/truncation/retry handling, including an egress-aware completion fallback, fixed public HTTP 5xx text, and closed-set status/code diagnostics. The original path is a facade over leaves for wire identity, reasoning, conversation state, token estimation, payload assembly, streaming, and the adapter. |
132132
| `src/adapters/mimo-free.ts` | Mimo Free transport (client identity + JWT). Concurrent requests share one JWT bootstrap bound only to its timeout; each request stops waiting on its own abort without cancelling the others. |
133133
| `src/adapters/command-code.ts`, `src/adapters/command-code-tool-text.ts`, `src/adapters/command-code-restored-schema.ts` | Command Code OAuth NDJSON translation. For every `xiaomi/mimo-` model, text, native calls, reasoning, and terminal decisions share one byte-bounded queue with linear queue visits. Markup is deduplicated against matching native calls; text-only restoration requires one contiguous text run, a clean finish, a declared tool, and arguments validated against supported schema constraints. A parameter-free (freeform) block may omit `</function>` but must end with `</tool_call>`; parameter blocks keep the canonical close. Markup appended after prose in the same delta is split off at the marker and held like a block that opens with `<tool_call>`; a marker split across deltas after prose is still released as text. Native, reasoning, and other intervening events interrupt a still-probing block but leave a held block held in arrival order, and the queued byte bound still flushes an unresolved envelope as text. An envelope the strict parser rejects but that opens with `<tool_call>`, closes with `</tool_call>`, and names a declared function is dropped when a native call for that same function arrives and on a clean finish; markup that parses but fits no supported schema is still released as text. Regex patterns, other unsupported constraints, and abnormal finishes fail closed. `tests/providers/command-code-tool-text-prose-split.test.ts` covers the split, the interleaved-event hold, and both drop paths. |

0 commit comments

Comments
 (0)