@@ -188,6 +188,54 @@ describe("terminal guard", () => {
188188 expect ( actual . at ( - 1 ) ) . toMatchObject ( { usage : { inputTokens : 30 , outputTokens : 5 , totalTokens : 35 } } ) ;
189189 } ) ;
190190
191+
192+ // A heartbeat is adapter liveness, not turn content. The openai-chat adapter emits one per
193+ // tool-call delta while it buffers, so retaining them here would let a single large argument
194+ // payload grow `seen` without bound — and `seen` is what both the continuation analysis and
195+ // the rebuilt request read. Passing them through unretained is what the empty-completion
196+ // guard already does.
197+ // A heartbeat is adapter liveness, not turn content. The openai-chat adapter emits one per
198+ // tool-call delta while it buffers, so retaining them would grow the guard's record without
199+ // bound on a large argument payload. `analyzeTerminalTurn` and `buildContinuationRequest`
200+ // both read that record, so pin the contract on the pure functions that consume it plus the
201+ // observable passthrough.
202+ test ( "a retained heartbeat would corrupt the continuation record" , ( ) => {
203+ const clean : AdapterEvent [ ] = [
204+ { type : "text_delta" , text : "我接下来会修改相关文件。" } ,
205+ ] ;
206+ const padded : AdapterEvent [ ] = [
207+ { type : "text_delta" , text : "我接下来会修改相关文件。" } ,
208+ ...Array . from ( { length : 50 } , ( ) => ( { type : "heartbeat" } ) as AdapterEvent ) ,
209+ ] ;
210+ const request = parsed ( "继续检查" ) ;
211+ // The guard must not let liveness markers change what the continuation decides or sends.
212+ expect ( analyzeTerminalTurn ( request , padded ) . assistantText )
213+ . toBe ( analyzeTerminalTurn ( request , clean ) . assistantText ) ;
214+ expect ( JSON . stringify ( buildContinuationRequest ( request , padded ) . context . messages ) )
215+ . toBe ( JSON . stringify ( buildContinuationRequest ( request , clean ) . context . messages ) ) ;
216+ } ) ;
217+
218+ test ( "heartbeats reach the consumer so the bridge watchdog stays armed" , async ( ) => {
219+ const actual : AdapterEvent [ ] = [ ] ;
220+ for await ( const event of guardTerminalEventStream ( {
221+ parsed : parsed ( "继续检查" ) ,
222+ firstEvents : ( async function * ( ) {
223+ yield { type : "text_delta" , text : "我接下来会修改相关文件。" } as AdapterEvent ;
224+ for ( let i = 0 ; i < 50 ; i ++ ) yield { type : "heartbeat" } as AdapterEvent ;
225+ yield { type : "tool_call_start" , id : "call_1" , name : "exec_command" } as AdapterEvent ;
226+ yield { type : "tool_call_end" } as AdapterEvent ;
227+ yield { type : "done" , usage : { inputTokens : 10 , outputTokens : 2 } } as AdapterEvent ;
228+ } ) ( ) ,
229+ continuation : ( ) => ( async function * ( ) {
230+ yield { type : "done" } as AdapterEvent ;
231+ } ) ( ) ,
232+ adapterName : "openai-chat" ,
233+ } ) ) actual . push ( event ) ;
234+
235+ expect ( actual . filter ( event => event . type === "heartbeat" ) ) . toHaveLength ( 50 ) ;
236+ expect ( actual . filter ( event => event . type === "done" ) ) . toHaveLength ( 1 ) ;
237+ } ) ;
238+
191239 test ( "stops after the configured continuation bound" , async ( ) => {
192240 let continuations = 0 ;
193241 const actual : AdapterEvent [ ] = [ ] ;
0 commit comments