Skip to content

Commit 24b94d2

Browse files
fix(devin): keep data fields byte-exact under blocklist sanitization
Review on #6281: extending the sanitizer to ChatMessagePrompt text, thinking (#11), and tool-call arguments rewrote literal content — patch bodies, exact needles, quoted file bytes — so a replay could describe a different command than the one run, or ask the model to edit a different string. Restrict the sanitizer to instruction surfaces (tool descriptions and the #2 system prompt) and let data fields pass through verbatim; if the cloud still refuses, the caller sees the upstream permission_denied. Tests now pin both halves: instruction surfaces are rewritten, user text/thinking/arguments stay literal. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent d45fa9b commit 24b94d2

3 files changed

Lines changed: 40 additions & 20 deletions

File tree

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

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,7 @@ function encodeChatToolCall(tc: { id: string; name: string; arguments: string })
207207
return Buffer.concat([
208208
encodeString(1, tc.id),
209209
encodeString(2, tc.name),
210-
// Replayed tool-call arguments are model-generated text that can carry a
211-
// blocklisted phrase (e.g. a patch quoting the Codex prompt). The rewrite
212-
// is a plain string substitution containing no JSON-breaking characters,
213-
// so the arguments stay valid JSON.
214-
encodeString(3, sanitizeTextForCognition(tc.arguments)),
210+
encodeString(3, tc.arguments),
215211
]);
216212
}
217213

@@ -229,7 +225,7 @@ function encodeChatMessagePrompt(
229225
): Buffer {
230226
const textParts = content.filter((p): p is { type: 'text'; text: string } => p.type === 'text');
231227
const imageParts = content.filter((p): p is { type: 'image'; mimeType: string; base64Data: string; caption?: string } => p.type === 'image');
232-
const joined = sanitizeTextForCognition(textParts.map((p) => p.text).join('\n'));
228+
const joined = textParts.map((p) => p.text).join('\n');
233229
const parts: Buffer[] = [
234230
// #1 message_id. The verified turn-1 capture stamps one on every prompt.
235231
encodeString(1, crypto.randomUUID()),
@@ -258,7 +254,7 @@ function encodeChatMessagePrompt(
258254
// reasoning model restarted its chain on every turn of a tool loop. Two
259255
// independent clients of the same service write it here: #11 thinking,
260256
// #12 signature, #18 signature_type on the assistant prompt.
261-
if (opts?.thinking) parts.push(encodeString(11, sanitizeTextForCognition(opts.thinking)));
257+
if (opts?.thinking) parts.push(encodeString(11, opts.thinking));
262258
if (opts?.signature) parts.push(encodeString(12, opts.signature));
263259
if (opts?.signatureType) parts.push(encodeString(18, opts.signatureType));
264260
return Buffer.concat(parts);
@@ -634,13 +630,15 @@ const MAX_TOOL_DESC_LEN = 6998;
634630
*
635631
* Scope note: the first entries only ever appeared in tool descriptions, but
636632
* this one lives in request #2 (the system prompt) and could equally appear
637-
* in conversation history — e.g. a tool result that returns a file quoting
638-
* the Codex prompt. The sanitizer therefore runs on every text the adapter
639-
* puts on the wire: tool descriptions, the #2 system prompt, each
640-
* ChatMessagePrompt's joined text, replayed thinking (#11), and tool-call
641-
* arguments (#6.3). Rewriting content the model quoted is
642-
* semantic-preserving, the same trade-off the tool-description rewrites
643-
* already accepted.
633+
* Scope note: the sanitizer only ever touches *instruction surfaces* —
634+
* tool descriptions and the #2 system prompt, where a meaning-preserving
635+
* reword loses nothing. It deliberately does NOT touch data fields:
636+
* message text (#3), replayed thinking (#11), and tool-call arguments
637+
* (#6.3) carry literal content (patches, exact needles, quoted file bytes)
638+
* where a rewrite would silently change what the model did or sees. If a
639+
* blocklisted phrase reaches the cloud inside one of those, the request is
640+
* refused and the caller sees the upstream `permission_denied` — which is
641+
* the correct failure, better than corrupting the data.
644642
*/
645643
const COGNITION_BLOCKLIST_REWRITES: ReadonlyArray<[RegExp, string]> = [
646644
[/\bTakes a task_id parameter identifying the task\b/g, "Accepts a task_id parameter identifying the task"],

‎structure/providers-and-adapters.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ rewrite rules and the routed-id settlement.
183183
| `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. |
184184
| `src/adapters/azure.ts` | Azure OpenAI bridge. |
185185
| `src/adapters/cursor.ts`, `src/adapters/cursor/` | Cursor protobuf transport: discovery, request builder, event decoding, MCP, thread continuity, native-exec policy. |
186-
| `src/adapters/devin.ts`, `src/adapters/devin/cloud-direct/` | Devin runTurn transport over Cognition Connect-RPC. Assistant reasoning replays as ChatMessagePrompt #11 thinking, #12 signature and #18 signature type (`src/adapters/devin/reasoning-signature.ts`): the #10/#21 pair arrives after the visible answer and becomes its own signature-only reasoning item, so a single unsigned thinking block plus exactly one signature-only block is replayed as one signed prompt, and a signature-only turn (GPT, Gemini) is replayed rather than dropped. An Anthropic signature is replayed, but because the streamed thinking is a summary the signature may not cover, a turn Cognition refuses with `invalid_argument` before any visible output (reasoning alone does not count) is retried once with Anthropic signatures withheld and the thinking text kept. `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 wire model UID comes from the catalog's family metadata (`ClientModelConfig` #23/#30/#31): a family id with no effort anchors on the family's default member and selects the nearest enabled rung, rounding up first; an effort moves only the effort axis, to the lowest rung at or above it (else the highest below) while Fast Mode, 1M Context and the other axes stay at the anchor's values unless the caller asked for `fast` or a `1m` value; not lowering a requested effort outranks keeping those axes (an unranked member counts as lower), a disabled row the caller named is kept when the request still selects it so the preflight names that refusal, and resolution never leaves the family. Rows without family metadata fall back to suffix resolution, whose variant scan matches the collapsed base rather than a string prefix. With no caller or configured output cap, the selected row's catalog `maxOutputTokens` fills CompletionConfiguration #2; #3 is `max_newlines` and is sent at a fixed value, never a context window. The leading system text is sent as `GetChatMessage` #2, a failed tool result sets ChatMessagePrompt #9 and keeps an in-band `ERROR:` marker, and Gemini uids have JSON-Schema type arrays split into per-type `anyOf` branches in tool parameters while preserving outer enum/const, boolean constraints (including `not`, `oneOf`, and `allOf`) on null, and existing null-branch restrictions. A pre-output `invalid_argument` on a history whose word-piece estimate, using the sanitized and truncated tool descriptions actually sent on the wire, reaches 95% of the selected UID's catalog input window, capped by configured provider and model limits (512 KiB of text only 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. Known limit: a malformed schema on a history already at or above that 95% threshold is also reported as overflow because the upstream returns the same `invalid_argument`. The `COGNITION_BLOCKLIST_REWRITES` sanitizer rewrites exact phrases Cognition refuses with `permission_denied`; it runs on tool descriptions and on every other string the request carries — the #2 system prompt, each ChatMessagePrompt's joined text, replayed thinking (#11), and tool-call arguments — because a trigger can reach the cloud outside tool descriptions (the Codex `<permissions instructions>` escalation boilerplate is the recorded case). |
186+
| `src/adapters/devin.ts`, `src/adapters/devin/cloud-direct/` | Devin runTurn transport over Cognition Connect-RPC. Assistant reasoning replays as ChatMessagePrompt #11 thinking, #12 signature and #18 signature type (`src/adapters/devin/reasoning-signature.ts`): the #10/#21 pair arrives after the visible answer and becomes its own signature-only reasoning item, so a single unsigned thinking block plus exactly one signature-only block is replayed as one signed prompt, and a signature-only turn (GPT, Gemini) is replayed rather than dropped. An Anthropic signature is replayed, but because the streamed thinking is a summary the signature may not cover, a turn Cognition refuses with `invalid_argument` before any visible output (reasoning alone does not count) is retried once with Anthropic signatures withheld and the thinking text kept. `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 wire model UID comes from the catalog's family metadata (`ClientModelConfig` #23/#30/#31): a family id with no effort anchors on the family's default member and selects the nearest enabled rung, rounding up first; an effort moves only the effort axis, to the lowest rung at or above it (else the highest below) while Fast Mode, 1M Context and the other axes stay at the anchor's values unless the caller asked for `fast` or a `1m` value; not lowering a requested effort outranks keeping those axes (an unranked member counts as lower), a disabled row the caller named is kept when the request still selects it so the preflight names that refusal, and resolution never leaves the family. Rows without family metadata fall back to suffix resolution, whose variant scan matches the collapsed base rather than a string prefix. With no caller or configured output cap, the selected row's catalog `maxOutputTokens` fills CompletionConfiguration #2; #3 is `max_newlines` and is sent at a fixed value, never a context window. The leading system text is sent as `GetChatMessage` #2, a failed tool result sets ChatMessagePrompt #9 and keeps an in-band `ERROR:` marker, and Gemini uids have JSON-Schema type arrays split into per-type `anyOf` branches in tool parameters while preserving outer enum/const, boolean constraints (including `not`, `oneOf`, and `allOf`) on null, and existing null-branch restrictions. A pre-output `invalid_argument` on a history whose word-piece estimate, using the sanitized and truncated tool descriptions actually sent on the wire, reaches 95% of the selected UID's catalog input window, capped by configured provider and model limits (512 KiB of text only 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. Known limit: a malformed schema on a history already at or above that 95% threshold is also reported as overflow because the upstream returns the same `invalid_argument`. The `COGNITION_BLOCKLIST_REWRITES` sanitizer rewrites exact phrases Cognition refuses with `permission_denied`; it runs only on instruction surfaces — tool descriptions and the #2 system prompt — while message text, replayed thinking (#11), and tool-call arguments stay byte-exact, because a rewrite there would silently change literal content such as patches or quoted file bytes; a trigger inside a data field surfaces as the upstream `permission_denied` instead (the Codex `<permissions instructions>` escalation boilerplate is the recorded #2 case). |
187187
| `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. |
188188
| `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. |
189189
| `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 bare text block, 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, including a marker in a later delta of the same text block, is held as a tail that can never mint a call: possible trailing marker prefixes stay in the same byte-accounted probe across deltas and release as text on a mismatch, boundary or end; a same-content native call strips a completed envelope as an echo, and anything else releases as presentation text so quoted examples stay inert. A tail waits only while a native input it could echo is open, counting the first later input of its own tool, so it is released as soon as those close without a match. 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 prose boundary, the interleaved-event hold, and both drop paths. |

‎tests/providers/devin-adapter.test.ts‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,12 @@ describe("devin adapter", () => {
319319
expect(sanitizeTextForCognitionForTests(trigger.toUpperCase()))
320320
.toContain("whether to allow the action");
321321

322-
// The phrase must be gone from every field of the encoded request, not
323-
// just tool descriptions: it reaches the cloud in #2 (system prompt) when
324-
// Codex injects it, and in a #3 prompt when a tool result or pasted file
325-
// quotes it.
322+
// The phrase is rewritten on instruction surfaces — #2 (system prompt)
323+
// and tool descriptions — but data fields must stay byte-exact: a user
324+
// message, replayed thinking, or tool-call arguments quoting the phrase
325+
// are literal content (patches, exact needles) where a rewrite would
326+
// silently change what the model did. If the cloud still refuses such a
327+
// request, the caller sees the upstream permission_denied.
326328
const sys = `<permissions instructions>\n- Include a short question ${trigger}. e.g. "Do it?"\n</permissions>`;
327329
const req = buildGetChatMessageRequestForTests({
328330
apiKey: "k",
@@ -344,8 +346,28 @@ describe("devin adapter", () => {
344346
],
345347
tools: [{ name: "codex_escalation", description: trigger, parameters: { type: "object" } }],
346348
});
347-
expect(req.includes(Buffer.from(trigger, "utf8"))).toBe(false);
349+
// Instruction surfaces rewritten...
348350
expect(req.includes(Buffer.from("asking the user whether to allow the action", "utf8"))).toBe(true);
351+
// ...but data fields preserve the literal bytes (user text, thinking,
352+
// tool-call arguments all still carry the verbatim trigger).
353+
expect(req.includes(Buffer.from(`here is a quote: ${trigger}`, "utf8"))).toBe(true);
354+
expect(req.includes(Buffer.from(`the prompt says: ${trigger}`, "utf8"))).toBe(true);
355+
expect(req.includes(Buffer.from(`{"patch":"${trigger}"}`, "utf8"))).toBe(true);
356+
357+
// A request where the ONLY trigger carrier is a tool description must
358+
// come out clean — pins encodeToolDef specifically.
359+
const toolOnly = buildGetChatMessageRequestForTests({
360+
apiKey: "k",
361+
modelUid: "swe-2",
362+
cascadeId: "c",
363+
sessionId: "s",
364+
requestId: 1n,
365+
triggerId: "t",
366+
messages: [{ role: "user", content: "hi" }],
367+
tools: [{ name: "codex_escalation", description: trigger, parameters: { type: "object" } }],
368+
});
369+
expect(toolOnly.includes(Buffer.from(trigger, "utf8"))).toBe(false);
370+
expect(toolOnly.includes(Buffer.from("whether to allow the action", "utf8"))).toBe(true);
349371
});
350372

351373
test("the catalog parser reads the per-account context window", () => {

0 commit comments

Comments
 (0)