11import { mkdtempSync } from "node:fs" ;
22import { tmpdir } from "node:os" ;
33import { 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" ;
55import { createDevinAdapter } from "../../src/adapters/devin" ;
66import { setCachedCatalogForTests } from "../../src/adapters/devin/cloud-direct/catalog" ;
77import { 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" ;
99import { encodeDevinSignature } from "../../src/adapters/devin/reasoning-signature" ;
1010import { createTranslatorBudget } from "../../src/lib/translator-budget" ;
1111import { 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