Skip to content

Commit 37848f4

Browse files
wtfsayoclaude
andcommitted
fix(devin): heartbeat while signed reasoning is held, and keep the refused attempt's usage
- While the signed attempt's events are held, a plain heartbeat goes out at most every 15 seconds, so a long reasoning phase does not trip the bridge's upstream stall deadline. The held reasoning and signature stay held until the retry decision. - The refused attempt was processed, so its final usage is added to every usage frame of the unsigned retry (frames are cumulative per request) instead of being dropped with its reasoning. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 796b07f commit 37848f4

2 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎src/adapters/devin.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@ export function mergeDevinUsage(previous: OcxUsage, next: OcxUsage): OcxUsage {
5454
*/
5555
const DEVIN_CLIENT_CLOSED_MESSAGE = "client closed request";
5656

57+
/** Below the bridge's upstream stall deadline, so held reasoning never reads as a stall. */
58+
const HELD_REASONING_HEARTBEAT_MS = 15_000;
59+
60+
type DevinUsageEvent = Extract<CloudChatEvent, { kind: "usage" }>;
61+
62+
/** The retry's cumulative usage plus the refused attempt's final counts. */
63+
function addDevinUsage(event: DevinUsageEvent, prior: DevinUsageEvent): DevinUsageEvent {
64+
const sum = (a?: number, b?: number) => (a === undefined && b === undefined ? undefined : (a ?? 0) + (b ?? 0));
65+
const out: DevinUsageEvent = { ...event };
66+
for (const key of ["promptTokens", "completionTokens", "totalTokens", "cachedInputTokens", "cacheCreationInputTokens", "reasoningTokens"] as const) {
67+
const value = sum(event[key], prior[key]);
68+
if (value !== undefined) out[key] = value;
69+
}
70+
return out;
71+
}
72+
5773
/** Map a cloud-direct failure onto the structured fields the error event carries. */
5874
export function devinErrorClassification(error: unknown): { status?: number; errorType?: string; retryable?: boolean } {
5975
const status = error instanceof CloudChatError ? error.status : undefined;
@@ -669,7 +685,11 @@ export function createDevinAdapter(
669685
// reasoning and signature, and the next turn would replay that signature against
670686
// the retry's thinking.
671687
const held: CloudChatEvent[] = [];
688+
// Usage is still real: the refused attempt was processed, so its final counts are
689+
// added to every usage frame of the retry (frames are cumulative per request).
690+
let refusedUsage: Extract<CloudChatEvent, { kind: "usage" }> | undefined;
672691
let visible = false;
692+
let lastHeartbeat = Date.now();
673693
try {
674694
for await (const event of request(signedMessages)) {
675695
// Only visible output makes a retry unsafe. Live, the refusal often lands after the
@@ -678,15 +698,26 @@ export function createDevinAdapter(
678698
visible = true;
679699
yield* held.splice(0);
680700
}
681-
if (visible) yield event;
682-
else held.push(event);
701+
if (visible) {
702+
yield event;
703+
continue;
704+
}
705+
held.push(event);
706+
if (event.kind === "usage") refusedUsage = event;
707+
// Held reasoning must not look like a stalled upstream to the bridge.
708+
if (Date.now() - lastHeartbeat >= HELD_REASONING_HEARTBEAT_MS) {
709+
lastHeartbeat = Date.now();
710+
emit({ type: "heartbeat" });
711+
}
683712
}
684713
} catch (error) {
685714
if (visible || !(error instanceof CloudChatError && error.code === "invalid_argument")) {
686715
yield* held.splice(0);
687716
throw error;
688717
}
689-
yield* request(unsignedMessages);
718+
for await (const event of request(unsignedMessages)) {
719+
yield event.kind === "usage" && refusedUsage ? addDevinUsage(event, refusedUsage) : event;
720+
}
690721
return;
691722
}
692723
yield* held.splice(0);

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { mkdtempSync } from "node:fs";
22
import { tmpdir } from "node:os";
33
import { join } from "node:path";
4-
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
4+
import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test";
55
import { createDevinAdapter } from "../../src/adapters/devin";
66
import { setCachedCatalogForTests } from "../../src/adapters/devin/cloud-direct/catalog";
77
import { devinCacheIdentity, invalidateSessionIdentity } from "../../src/adapters/devin/cloud-direct/chat";
8-
import { encodeString, encodeVarintField, iterFields } from "../../src/adapters/devin/cloud-direct/wire";
8+
import { encodeMessage, encodeString, encodeVarintField, iterFields } from "../../src/adapters/devin/cloud-direct/wire";
99
import { encodeDevinSignature } from "../../src/adapters/devin/reasoning-signature";
1010
import { createTranslatorBudget } from "../../src/lib/translator-budget";
1111
import { encodeReasoningEnvelope } from "../../src/responses/reasoning-envelope";
@@ -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" | "reasoning-then-ok"> = [];
27+
let responses: Array<"refuse" | "ok" | "text-then-refuse" | "reasoning-then-refuse" | "reasoning-then-ok" | "usage-reasoning-then-refuse" | "usage-ok"> = [];
2828

2929
const frame = (body: Buffer, flags = 0) => {
3030
const header = Buffer.alloc(5);
@@ -74,6 +74,9 @@ describe("Devin Anthropic signature fallback", () => {
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])
7676
: next === "reasoning-then-ok" ? Buffer.concat([frame(Buffer.concat([encodeString(9, "thinking"), encodeString(10, "EpcBNew"), encodeString(21, "anthropic")])), ok])
77+
// ModelUsageStats (#7) arrives with the reasoning, before the refusal trailer.
78+
: next === "usage-reasoning-then-refuse" ? Buffer.concat([frame(Buffer.concat([encodeMessage(7, Buffer.concat([encodeVarintField(2, 1000), encodeVarintField(3, 40)])), encodeString(9, "thinking")])), frame(encodeString(9, " more")), refusal])
79+
: next === "usage-ok" ? Buffer.concat([frame(Buffer.concat([encodeMessage(7, Buffer.concat([encodeVarintField(2, 1100), encodeVarintField(3, 20)])), encodeString(3, "ok"), encodeVarintField(5, 2)])), frame(Buffer.from("{}"), 2)])
7780
: ok;
7881
return new Response(body, { headers: { "content-type": "application/connect+proto" } });
7982
}) as typeof fetch;
@@ -119,6 +122,31 @@ describe("Devin Anthropic signature fallback", () => {
119122
expect(kinds.indexOf("thinking_delta")).toBeLessThan(kinds.indexOf("text_delta"));
120123
});
121124

125+
test("the refused attempt's usage is added to the retry's", async () => {
126+
responses = ["usage-reasoning-then-refuse", "usage-ok"];
127+
const events = await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");
128+
expect(requests).toHaveLength(2);
129+
const done = events.find(e => e.type === "done") as { usage?: { inputTokens?: number; outputTokens?: number } } | undefined;
130+
expect(done?.usage?.inputTokens).toBe(2100);
131+
expect(done?.usage?.outputTokens).toBe(60);
132+
});
133+
134+
test("held reasoning emits heartbeats, never the held events", async () => {
135+
// Each clock read advances 20s, so every held frame is past the heartbeat interval.
136+
let clock = Date.now();
137+
const now = spyOn(Date, "now").mockImplementation(() => (clock += 20_000));
138+
try {
139+
responses = ["usage-reasoning-then-refuse", "ok"];
140+
const events = await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");
141+
const kinds = events.map(e => e.type);
142+
expect(kinds.filter(k => k === "heartbeat").length).toBeGreaterThan(0);
143+
expect(kinds).not.toContain("thinking_delta");
144+
expect(kinds.indexOf("heartbeat")).toBeLessThan(kinds.indexOf("text_delta"));
145+
} finally {
146+
now.mockRestore();
147+
}
148+
});
149+
122150
test("an accepted signed Claude turn is sent once, signature included", async () => {
123151
responses = ["ok"];
124152
await run(encodeDevinSignature("EpcBClaude", "anthropic"), "claude-opus-5-5-medium");

0 commit comments

Comments
 (0)