Skip to content

Commit 4e0c626

Browse files
wtfsayoclaude
authored andcommitted
fix(devin): system prompt in #2, tool-error flag, Gemini schemas, history overflow
- Send the leading system text as GetChatMessage #2 instead of folding it into the first user prompt. Live on swe-1-6 with a ~6.7k-token system prompt the turn-2 cache ratio is unchanged (6688/6715 vs 7072/7097) and the prompt is smaller; swe-1-6, swe-2-medium, gemini and claude all obey #2. - Mark failed tool results with ChatMessagePrompt #9 tool_result_is_error instead of an in-band "ERROR:" prefix. - Rewrite JSON-Schema type arrays to anyOf in tool parameters for Gemini uids, which Cognition refuses with invalid_argument on every turn. - Surface a pre-output invalid_argument on a history near or past the model's input window as context_length_exceeded so Codex compacts; small requests with the same code stay a plain 400. - Correct the #15 (CortexTrajectoryReference), #17 prompt_id and #22 execution_id comments. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> (cherry picked from commit 29688bf)
1 parent 0ca17f3 commit 4e0c626

9 files changed

Lines changed: 384 additions & 39 deletions

File tree

‎scripts/test-layout/layout.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@
746746
"destination-policy-resolved.test.ts": "routing",
747747
"devin-adapter-reset-wait.test.ts": "providers",
748748
"devin-adapter.test.ts": "providers",
749+
"devin-chat-wire-fixes.test.ts": "providers",
749750
"devin-cli-authmode-migration.test.ts": "providers",
750751
"devin-effort-ladder.test.ts": "providers",
751752
"devin-family-resolution.test.ts": "providers",

‎src/adapters/devin.ts‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { buildNonOpenAIToolCatalogNudgeForTools } from "./tool-catalog-nudge";
1717
import { DEVIN_DEFAULT_API_SERVER, resolveDevinApiServer } from "../oauth/devin";
1818
import { isProviderIssuedThinkingSignature } from "../responses/reasoning-envelope";
1919
import { SendBudgetExhaustedError } from "../lib/upstream-retry";
20+
import { devinContextOverflowEvent, isDevinHistoryOverflow } from "./devin/context-overflow";
2021

2122
/**
2223
* Combine two usage frames from one turn by keeping the larger count per field.
@@ -519,16 +520,11 @@ function mapOneMessage(message: OcxMessage): ChatHistoryItem | undefined {
519520
};
520521
}
521522
if (message.role === "toolResult") {
522-
const wireContent = mapOcxContentToWire(message.content);
523-
const toolContent = message.isError
524-
? (typeof wireContent === "string"
525-
? `ERROR: ${wireContent}`
526-
: [{ type: "text", text: "ERROR:" } as ContentPart, ...wireContent])
527-
: wireContent;
528523
return {
529524
role: "tool",
530-
content: toolContent,
525+
content: mapOcxContentToWire(message.content),
531526
tool_call_id: message.toolCallId,
527+
...(message.isError ? { is_error: true } : {}),
532528
};
533529
}
534530
return undefined;
@@ -695,6 +691,11 @@ export function createDevinAdapter(
695691
let openToolId: string | undefined;
696692
let usage: OcxUsage | undefined;
697693
let stopReason: string | undefined;
694+
// Kept outside the try so the catch can tell an oversized history from a bad request.
695+
let producedOutput = false;
696+
let maxInputTokens: number | undefined;
697+
let messages: ChatHistoryItem[] = [];
698+
let tools: ToolDef[] | undefined;
698699

699700
const closeOpenTool = () => {
700701
if (!openToolId) return;
@@ -704,6 +705,8 @@ export function createDevinAdapter(
704705

705706
try {
706707
// Read the selected UID's catalog row, not the picker's collapsed base.
708+
messages = mapOcxMessagesToDevin(parsed);
709+
tools = mapOcxToolsToDevin(parsed.context.tools);
707710
const maxOutputTokens = resolveDevinMaxOutputTokens(
708711
provider, modelUid, parsed.options.maxOutputTokens, catalog?.byUid.get(modelUid),
709712
);
@@ -718,8 +721,8 @@ export function createDevinAdapter(
718721
apiServerUrl: host,
719722
modelUid,
720723
catalog,
721-
messages: mapOcxMessagesToDevin(parsed),
722-
tools: mapOcxToolsToDevin(parsed.context.tools),
724+
messages,
725+
tools,
723726
cascadeId,
724727
completionOpts: {
725728
...(maxOutputTokens !== undefined ? { maxOutputTokens } : {}),
@@ -746,6 +749,7 @@ export function createDevinAdapter(
746749
emit({ type: "error", message: DEVIN_CLIENT_CLOSED_MESSAGE, status: 499, retryable: false, ...(usage ? { usage } : {}) });
747750
return;
748751
}
752+
if (event.kind === "text" || event.kind === "reasoning" || event.kind === "tool_call_start") producedOutput = true;
749753
if (event.kind === "text") {
750754
closeOpenTool();
751755
if (event.text) emit({ type: "text_delta", text: event.text });
@@ -818,6 +822,12 @@ export function createDevinAdapter(
818822
// The Responses boundary already maps this local refusal to its structured 429 code.
819823
// Converting it to an adapter event would make it an ordinary untyped upstream error.
820824
if (error instanceof SendBudgetExhaustedError) throw error;
825+
if (error instanceof CloudChatError && isDevinHistoryOverflow({
826+
code: error.code, producedOutput, modelUid, contextWindow: maxInputTokens, messages, tools,
827+
})) {
828+
emit({ ...devinContextOverflowEvent(), ...(usage ? { usage } : {}) });
829+
return;
830+
}
821831
const message = error instanceof CloudChatError
822832
? ("Devin cloud error" + (error.code ? " " + error.code : "") + ": " + error.message)
823833
: error instanceof Error ? error.message : String(error);

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { getCachedCatalog, ModelNotAvailableError, type CacheEntry } from './cat
3939
import { anySignal, cancelBodyOnAbort } from '../../../lib/abort.js';
4040
import { parseRetryAfterFromMessage } from '../../../lib/retry-delay.js';
4141
import { resolveDevinApiBaseUrl } from '../../../oauth/devin/api-base.js';
42+
import { normalizeDevinToolParameters } from './tool-schema.js';
4243

4344
/**
4445
* Connect-RPC streaming inactivity timeout. If the cloud sends zero bytes
@@ -175,6 +176,7 @@ export function allocateCascadeId(): string {
175176
* #3 prompt: string (text content)
176177
* #4 num_tokens: int (rough estimate)
177178
* #5 safe_for_code_telemetry: bool (1 = ok to log)
179+
* #9 tool_result_is_error: bool (tool prompts only)
178180
* #10 images: repeated ImageData (multimodal)
179181
* #11 thinking: string (assistant reasoning, replayed)
180182
* #12 signature: string (opaque attestation for #11)
@@ -218,6 +220,7 @@ function encodeChatMessagePrompt(
218220
thinking?: string;
219221
signature?: string;
220222
signatureType?: string;
223+
isError?: boolean;
221224
},
222225
): Buffer {
223226
const textParts = content.filter((p): p is { type: 'text'; text: string } => p.type === 'text');
@@ -234,6 +237,9 @@ function encodeChatMessagePrompt(
234237
if (opts?.toolCallId) {
235238
parts.push(encodeString(7, opts.toolCallId));
236239
}
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.
242+
if (opts?.isError) parts.push(encodeVarintField(9, 1));
237243
// Assistant message with tool_calls: encode each as a ChatToolCall.
238244
if (opts?.toolCalls && opts.toolCalls.length > 0) {
239245
for (const tc of opts.toolCalls) {
@@ -257,21 +263,16 @@ function encodeChatMessagePrompt(
257263
const SOURCE_BY_ROLE: Record<string, number> = {
258264
user: 1,
259265
assistant: 2,
260-
// NOTE: do not send source=3 (SYSTEM) directly — the Codeium chat backend
261-
// returns "third-party model provider is experiencing issues" when any
262-
// ChatMessagePrompt has source=SYSTEM. The captured LS upstream traffic
263-
// shows the IDE inlines system context into the *user* prompt (source=1)
264-
// wrapped in <additional_metadata>...</additional_metadata>. We collapse
265-
// role:'system' messages into the next user turn before building the
266-
// proto — see `collapseSystemIntoUser` below.
266+
// Never sent as a prompt source: the leading system text goes in request #2,
267+
// and a later system message is collapsed into the next user turn below.
267268
system: 1,
268269
tool: 4,
269270
};
270271

271272
/**
272-
* Collapse OpenAI-style messages so all `role:'system'` entries are inlined
273-
* into the immediately-following user message, matching the wire format the
274-
* IDE uses. Cognition's chat backend rejects raw role=system entries.
273+
* Collapse `role:'system'` entries that follow the conversation start into the
274+
* immediately-following user message. The leading run of system messages never
275+
* reaches here; it is the request's #2 system prompt.
275276
*
276277
* [{system: "S1"}, {system: "S2"}, {user: "U1"}, {assistant: "A1"}, {user: "U2"}]
277278
*
@@ -428,6 +429,8 @@ export interface ChatHistoryItem {
428429
thinking?: string;
429430
signature?: string;
430431
signature_type?: string;
432+
/** For `role: 'tool'` only — the tool failed. Encoded as ChatMessagePrompt #9. */
433+
is_error?: boolean;
431434
}
432435

433436
/**
@@ -535,8 +538,8 @@ interface BuildArgs {
535538
messages: ChatHistoryItem[];
536539
cascadeId: string;
537540
/**
538-
* GetChatMessageRequest #22. Optional because it is omitted on a first turn;
539-
* the working client only reuses one across a later tool loop.
541+
* GetChatMessageRequest #17 prompt_id. Optional because it is omitted on a
542+
* first turn; the working client only reuses one across a later tool loop.
540543
*/
541544
promptId?: string;
542545
sessionId: string;
@@ -639,7 +642,7 @@ export function sanitizeToolDescriptionForCognitionForTests(description: string)
639642
return sanitizeToolDescriptionForCognition(description);
640643
}
641644

642-
function encodeToolDef(tool: ToolDef): Buffer {
645+
function encodeToolDef(tool: ToolDef, modelUid: string): Buffer {
643646
const rawDesc = sanitizeToolDescriptionForCognition(tool.description ?? '');
644647
const desc =
645648
rawDesc.length > MAX_TOOL_DESC_LEN
@@ -648,7 +651,7 @@ function encodeToolDef(tool: ToolDef): Buffer {
648651
return Buffer.concat([
649652
encodeString(1, tool.name),
650653
encodeString(2, desc),
651-
encodeString(3, JSON.stringify(tool.parameters ?? {})),
654+
encodeString(3, JSON.stringify(normalizeDevinToolParameters(modelUid, tool.parameters ?? {}))),
652655
]);
653656
}
654657

@@ -667,9 +670,20 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
667670
cloudChatShape: true,
668671
});
669672

670-
// System messages must be inlined into the user turn (Cognition cloud
671-
// rejects source=3). See `collapseSystemIntoUser` for the format.
672-
const collapsed = collapseSystemIntoUser(args.messages);
673+
// The leading system messages become request #2. Measured live on swe-1-6
674+
// with a ~6.7k-token system prompt: turn 2 read 6688 of 6715 prompt tokens
675+
// from cache in #2, against 7072 of 7097 when the same text was collapsed
676+
// into the first user prompt, so the cache ratio is unchanged and the prompt
677+
// is smaller. The model obeyed an instruction given only in #2.
678+
const firstNonSystem = args.messages.findIndex((m) => m.role !== 'system');
679+
const leadingSystem = firstNonSystem === -1 ? args.messages : args.messages.slice(0, firstNonSystem);
680+
const systemPrompt = leadingSystem
681+
.map((m) => normalizeContent(m.content)
682+
.filter((p): p is { type: 'text'; text: string } => p.type === 'text')
683+
.map((p) => p.text).join('\n'))
684+
.filter(Boolean)
685+
.join('\n\n');
686+
const collapsed = collapseSystemIntoUser(args.messages.slice(leadingSystem.length));
673687
const promptParts = collapsed.map((m) =>
674688
encodeMessage(
675689
3,
@@ -686,6 +700,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
686700
thinking: m.role === 'assistant' ? m.thinking : undefined,
687701
signature: m.role === 'assistant' ? m.signature : undefined,
688702
signatureType: m.role === 'assistant' ? m.signature_type : undefined,
703+
isError: m.role === 'tool' ? m.is_error : undefined,
689704
},
690705
),
691706
),
@@ -694,7 +709,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
694709
const completion = encodeCompletionConfiguration(args.completionOpts ?? {});
695710

696711
const toolParts: Buffer[] = (args.tools ?? []).map((t) =>
697-
encodeMessage(10, encodeToolDef(t)),
712+
encodeMessage(10, encodeToolDef(t, args.modelUid)),
698713
);
699714

700715
// Field layout from mitm capture of the LS:
@@ -704,15 +719,15 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
704719
// #8 completion_configuration
705720
// #10 tools (repeated ChatToolDefinition)
706721
// #13 prompt_cache_options
722+
// #15 CortexTrajectoryReference
707723
// #16 cascade_id (string)
724+
// #17 prompt_id (string)
708725
// #21 chat_model_uid (string)
709-
// #22 prompt_id (string)
726+
// #22 execution_id (string)
710727
return Buffer.concat([
711728
encodeMessage(1, metadata),
712-
// #2 system_prompt is always written, empty when the caller had none. The
713-
// system turn is separately collapsed into the first user message because
714-
// source=SYSTEM is refused; this field is the one the wire expects here.
715-
encodeString(2, ''),
729+
// #2 system_prompt is always written, empty when the caller had none.
730+
encodeString(2, systemPrompt),
716731
...promptParts,
717732
encodeVarintField(7, args.requestType ?? 5),
718733
encodeMessage(8, completion),
@@ -724,7 +739,7 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
724739
// it and records real savings; sending it unconditionally matches both the
725740
// native client and CLIProxyAPIPlus, which places it outside its tools gate.
726741
encodeMessage(13, encodeVarintField(1, PROMPT_CACHE_EPHEMERAL)),
727-
// #15 session model config: { id, turn, 4 }. Present on every verified
742+
// #15 CortexTrajectoryReference: { id, 1, 4 }. Present on every verified
728743
// request.
729744
encodeMessage(15, Buffer.concat([
730745
encodeString(1, crypto.randomUUID()),
@@ -734,9 +749,9 @@ function buildGetChatMessageRequest(args: BuildArgs): Buffer {
734749
encodeString(16, args.cascadeId),
735750
encodeVarintField(20, 1),
736751
encodeString(21, args.modelUid),
737-
// #22 is deliberately omitted. It is a user-exchange id that only appears
738-
// from the second turn onward and is reused across that turn's tool loop; a
739-
// fresh per-request uuid matches neither shape.
752+
// #17 prompt_id is deliberately omitted. It is a user-exchange id that only
753+
// appears from the second turn onward and is reused across that turn's tool
754+
// loop; a fresh per-request uuid matches neither shape.
740755
]);
741756
}
742757

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Tool-schema compatibility for Gemini models behind GetChatMessage.
3+
*
4+
* Measured live on gemini-3-8-flash-medium: any schema node whose `type` is a
5+
* JSON-Schema type array (`["string", "null"]`) is refused with an opaque
6+
* `invalid_argument` on every turn, while the same union spelled as
7+
* `anyOf: [{type: "string"}, {type: "null"}]` is accepted, as are `$schema`,
8+
* `additionalProperties: false`, `const`, and `$ref`/`$defs`. Claude models
9+
* accept type arrays, so only the Gemini family is rewritten, and only that one
10+
* keyword: the full Google subset sanitizer would strip keywords this backend
11+
* accepts.
12+
*/
13+
14+
export function isDevinGeminiModelUid(modelUid: string): boolean {
15+
return /^gemini-/i.test(modelUid) || /^MODEL_GOOGLE_GEMINI_/i.test(modelUid);
16+
}
17+
18+
/** Keys whose values are instance data, not subschemas. */
19+
const DATA_KEYS = new Set(['enum', 'const', 'default', 'examples', 'example']);
20+
/** 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']);
22+
23+
function rewriteMap(map: unknown): unknown {
24+
if (!map || typeof map !== 'object' || Array.isArray(map)) return map;
25+
return Object.fromEntries(Object.entries(map).map(([name, schema]) => [name, rewrite(schema)]));
26+
}
27+
28+
function rewrite(node: unknown): unknown {
29+
if (Array.isArray(node)) return node.map(rewrite);
30+
if (!node || typeof node !== 'object') return node;
31+
// 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]) => [
34+
key,
35+
DATA_KEYS.has(key) ? value : SCHEMA_MAP_KEYS.has(key) ? rewriteMap(value) : rewrite(value),
36+
]),
37+
);
38+
if (!Array.isArray(out.type)) return out;
39+
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 }] };
48+
}
49+
50+
/** Rewrite type arrays for Gemini uids; every other model gets the schema unchanged. */
51+
export function normalizeDevinToolParameters(modelUid: string, parameters: unknown): unknown {
52+
return isDevinGeminiModelUid(modelUid) ? rewrite(parameters) : parameters;
53+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Recognise an oversized Devin history behind Cognition's opaque refusal.
3+
*
4+
* Measured live on swe-1-6 (200k context): a 1.2 MB or 3 MB history is refused
5+
* after ~11s with `invalid_argument` and no output, the same code a bad tool
6+
* schema gets. Passed through as a plain 400, Codex never learns its context is
7+
* full and never compacts, so every later turn dead-ends the same way. Only a
8+
* request large against the model's window is reclassified; a small request
9+
* with the same code stays an ordinary 400.
10+
*/
11+
import type { AdapterEvent } from "../../types";
12+
import { estimateTokens } from "../../lib/token-estimate";
13+
import type { ChatHistoryItem, ToolDef } from "./cloud-direct";
14+
15+
/** Share of the window the estimate must reach; the chars-per-token estimate can run ~25% low. */
16+
const WINDOW_SHARE = 0.8;
17+
/** Text size that counts as oversized when the model's window is unknown. */
18+
const UNKNOWN_WINDOW_CHARS = 512 * 1024;
19+
20+
export const DEVIN_CONTEXT_OVERFLOW_MESSAGE =
21+
"Devin rejected this turn and its history is at or past the model's context window. Compact the conversation or start a new session.";
22+
23+
/** Text the model would tokenize; image bytes are excluded so a screenshot cannot look like a long history. */
24+
function requestText(messages: ChatHistoryItem[], tools: ToolDef[] | undefined): string {
25+
const parts: string[] = [];
26+
for (const m of messages) {
27+
if (typeof m.content === "string") parts.push(m.content);
28+
else for (const p of m.content) if (p.type === "text") parts.push(p.text);
29+
for (const call of m.tool_calls ?? []) parts.push(call.arguments);
30+
if (m.thinking) parts.push(m.thinking);
31+
}
32+
for (const tool of tools ?? []) parts.push(tool.description, JSON.stringify(tool.parameters ?? {}));
33+
return parts.join("\n");
34+
}
35+
36+
export function isDevinHistoryOverflow(input: {
37+
code: string | undefined;
38+
producedOutput: boolean;
39+
modelUid: string;
40+
contextWindow: number | undefined;
41+
messages: ChatHistoryItem[];
42+
tools: ToolDef[] | undefined;
43+
}): boolean {
44+
if (input.code !== "invalid_argument" || input.producedOutput) return false;
45+
const text = requestText(input.messages, input.tools);
46+
if (!input.contextWindow) return text.length >= UNKNOWN_WINDOW_CHARS;
47+
return estimateTokens(text, input.modelUid) >= input.contextWindow * WINDOW_SHARE;
48+
}
49+
50+
/** Same terminal shape the Kiro adapter uses, which Codex reads as "context full, compact". */
51+
export function devinContextOverflowEvent(): Extract<AdapterEvent, { type: "error" }> {
52+
return {
53+
type: "error",
54+
message: DEVIN_CONTEXT_OVERFLOW_MESSAGE,
55+
status: 400,
56+
errorType: "invalid_request_error",
57+
code: "context_length_exceeded",
58+
retryable: false,
59+
};
60+
}

0 commit comments

Comments
 (0)