Skip to content

Commit 796b07f

Browse files
wtfsayoclaude
andcommitted
fix(devin): hold the signed attempt's reasoning until its outcome is known
The fallback yielded the signed attempt's reasoning and signature before it knew whether Cognition would refuse the turn, so a successful unsigned retry left the client holding the refused attempt's signature, which the next turn would replay against the retry's thinking. When a fallback is possible, the signed attempt's events are now held until its first visible output or a clean finish, and discarded when the retry starts. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 073ef93 commit 796b07f

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎src/adapters/devin.ts‎

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { AdapterEvent, OcxAssistantMessage, OcxContentPart, OcxMessage, Ocx
1010
import { namespacedToolName } from "../types";
1111
import type { IncomingMeta, ProviderAdapter } from "./base";
1212
import { streamChatEventsWithResetRetry, devinStatedResetWaitMs, allocateCascadeId, CloudChatError, type ChatHistoryItem, type ToolDef } from "./devin/cloud-direct";
13-
import type { ContentPart } from "./devin/cloud-direct/chat";
13+
import type { CloudChatEvent, ContentPart } from "./devin/cloud-direct/chat";
1414
import { getCachedCatalog, type CacheEntry } from "./devin/cloud-direct/catalog";
1515
import { collapseDevinModelUid } from "./devin/live-models";
1616
import { buildNonOpenAIToolCatalogNudgeForTools } from "./tool-catalog-nudge";
@@ -660,18 +660,36 @@ export function createDevinAdapter(
660660
},
661661
});
662662
async function* withSignatureFallback() {
663-
let produced = false;
663+
if (!unsignedMessages) {
664+
yield* request(signedMessages);
665+
return;
666+
}
667+
// Events from the signed attempt are held until its outcome is known: a refusal
668+
// after reasoning would otherwise leave the client with the refused attempt's
669+
// reasoning and signature, and the next turn would replay that signature against
670+
// the retry's thinking.
671+
const held: CloudChatEvent[] = [];
672+
let visible = false;
664673
try {
665674
for await (const event of request(signedMessages)) {
666675
// Only visible output makes a retry unsafe. Live, the refusal often lands after the
667676
// model has streamed its reasoning, its signature and a finish frame, and nothing else.
668-
produced ||= event.kind === "text" || event.kind === "tool_call_start" || event.kind === "tool_call_args";
669-
yield event;
677+
if (!visible && (event.kind === "text" || event.kind === "tool_call_start" || event.kind === "tool_call_args")) {
678+
visible = true;
679+
yield* held.splice(0);
680+
}
681+
if (visible) yield event;
682+
else held.push(event);
670683
}
671684
} catch (error) {
672-
if (!unsignedMessages || produced || !(error instanceof CloudChatError && error.code === "invalid_argument")) throw error;
685+
if (visible || !(error instanceof CloudChatError && error.code === "invalid_argument")) {
686+
yield* held.splice(0);
687+
throw error;
688+
}
673689
yield* request(unsignedMessages);
690+
return;
674691
}
692+
yield* held.splice(0);
675693
}
676694
for await (const event of withSignatureFallback()) {
677695
if (incoming.abortSignal?.aborted) {

‎tests/providers/devin-anthropic-signature-fallback.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe("Devin Anthropic signature fallback", () => {
2424
const previousFetch = globalThis.fetch;
2525
let home = "";
2626
let requests: Buffer[] = [];
27-
let responses: Array<"refuse" | "ok" | "text-then-refuse" | "reasoning-then-refuse"> = [];
27+
let responses: Array<"refuse" | "ok" | "text-then-refuse" | "reasoning-then-refuse" | "reasoning-then-ok"> = [];
2828

2929
const frame = (body: Buffer, flags = 0) => {
3030
const header = Buffer.alloc(5);
@@ -73,6 +73,7 @@ describe("Devin Anthropic signature fallback", () => {
7373
: next === "text-then-refuse" ? Buffer.concat([frame(encodeString(3, "partial")), refusal])
7474
// The live shape: reasoning, its signature and a finish frame, then the refusal trailer.
7575
: next === "reasoning-then-refuse" ? Buffer.concat([frame(Buffer.concat([encodeString(9, "thinking"), encodeString(10, "EpcBNew"), encodeString(21, "anthropic"), encodeVarintField(5, 2)])), refusal])
76+
: next === "reasoning-then-ok" ? Buffer.concat([frame(Buffer.concat([encodeString(9, "thinking"), encodeString(10, "EpcBNew"), encodeString(21, "anthropic")])), ok])
7677
: ok;
7778
return new Response(body, { headers: { "content-type": "application/connect+proto" } });
7879
}) as typeof fetch;
@@ -96,13 +97,26 @@ describe("Devin Anthropic signature fallback", () => {
9697
expect(events).toContainEqual({ type: "text_delta", text: "ok" });
9798
});
9899

99-
test("a refusal after reasoning alone is still retried", async () => {
100+
test("a refusal after reasoning alone is still retried, and the refused attempt's reasoning never reaches the client", async () => {
100101
responses = ["reasoning-then-refuse", "ok"];
101102
const events = await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");
102103
expect(requests).toHaveLength(2);
103104
expect(assistantSignature(requests[1]!).signature).toBeUndefined();
104105
expect(events.some(e => e.type === "error")).toBe(false);
105106
expect(events).toContainEqual({ type: "text_delta", text: "ok" });
107+
// The refused attempt streamed "thinking" and signature EpcBNew; neither may leak into the turn.
108+
expect(events.some(e => e.type === "thinking_delta")).toBe(false);
109+
expect(events.some(e => e.type === "thinking_signature")).toBe(false);
110+
});
111+
112+
test("an accepted signed turn still delivers its held reasoning", async () => {
113+
responses = ["reasoning-then-ok"];
114+
const events = await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");
115+
expect(requests).toHaveLength(1);
116+
const kinds = events.map(e => e.type);
117+
expect(kinds).toContain("thinking_delta");
118+
expect(events).toContainEqual({ type: "thinking_signature", signature: encodeDevinSignature("EpcBNew", "anthropic") });
119+
expect(kinds.indexOf("thinking_delta")).toBeLessThan(kinds.indexOf("text_delta"));
106120
});
107121

108122
test("an accepted signed Claude turn is sent once, signature included", async () => {

0 commit comments

Comments
 (0)