@@ -12,8 +12,12 @@ import { normalizeMessage } from '@/lib/copilot/chat/persisted-message'
1212import {
1313 MothershipStreamV1CompletionStatus ,
1414 MothershipStreamV1EventType ,
15+ MothershipStreamV1RunKind ,
1516 MothershipStreamV1SessionKind ,
17+ MothershipStreamV1SpanLifecycleEvent ,
18+ MothershipStreamV1SpanPayloadKind ,
1619 MothershipStreamV1TextChannel ,
20+ MothershipStreamV1ToolOutcome ,
1721} from '@/lib/copilot/generated/mothership-stream-v1'
1822import type { StreamBatchEvent } from '@/lib/copilot/request/session/types'
1923
@@ -229,6 +233,80 @@ describe('buildEffectiveChatTranscript', () => {
229233 ] )
230234 } )
231235
236+ it ( 'pairs a scoped compaction inside the owning subagent during stream replay' , ( ) => {
237+ const scope = {
238+ lane : 'subagent' as const ,
239+ parentToolCallId : 'tc-workflow' ,
240+ spanId : 'span-workflow' ,
241+ parentSpanId : 'span-superagent' ,
242+ agentId : 'superagent' ,
243+ }
244+ const stream = { streamId : 'stream-1' }
245+ const result = buildEffectiveChatTranscript ( {
246+ messages : [ buildUserMessage ( 'stream-1' , 'Hello' ) ] ,
247+ activeStreamId : 'stream-1' ,
248+ streamSnapshot : {
249+ events : [
250+ toBatchEvent ( 1 , {
251+ v : 1 ,
252+ seq : 1 ,
253+ ts : '2026-04-15T12:00:01.000Z' ,
254+ type : MothershipStreamV1EventType . span ,
255+ stream,
256+ scope,
257+ payload : {
258+ kind : MothershipStreamV1SpanPayloadKind . subagent ,
259+ event : MothershipStreamV1SpanLifecycleEvent . start ,
260+ agent : 'workflow' ,
261+ data : { tool_call_id : 'tc-workflow' } ,
262+ } ,
263+ } ) ,
264+ toBatchEvent ( 2 , {
265+ v : 1 ,
266+ seq : 2 ,
267+ ts : '2026-04-15T12:00:02.000Z' ,
268+ type : MothershipStreamV1EventType . run ,
269+ stream,
270+ scope,
271+ payload : { kind : MothershipStreamV1RunKind . compaction_start } ,
272+ } ) ,
273+ toBatchEvent ( 3 , {
274+ v : 1 ,
275+ seq : 3 ,
276+ ts : '2026-04-15T12:00:03.000Z' ,
277+ type : MothershipStreamV1EventType . run ,
278+ stream,
279+ scope,
280+ payload : {
281+ kind : MothershipStreamV1RunKind . compaction_done ,
282+ data : { summary_chars : 42 } ,
283+ } ,
284+ } ) ,
285+ ] ,
286+ previewSessions : [ ] ,
287+ status : 'active' ,
288+ } ,
289+ } )
290+
291+ const compactions = result [ 1 ] ?. contentBlocks ?. filter (
292+ ( block ) => block . type === MothershipStreamV1EventType . tool
293+ )
294+ expect ( compactions ) . toHaveLength ( 1 )
295+ expect ( compactions ?. [ 0 ] ) . toEqual (
296+ expect . objectContaining ( {
297+ parentToolCallId : 'tc-workflow' ,
298+ spanId : 'span-workflow' ,
299+ parentSpanId : 'span-superagent' ,
300+ toolCall : expect . objectContaining ( {
301+ id : 'compaction_2' ,
302+ name : 'context_compaction' ,
303+ calledBy : 'workflow' ,
304+ state : MothershipStreamV1ToolOutcome . success ,
305+ } ) ,
306+ } )
307+ )
308+ } )
309+
232310 it ( 'materializes a cancelled assistant tail when the stream ends before persistence' , ( ) => {
233311 const result = buildEffectiveChatTranscript ( {
234312 messages : [ buildUserMessage ( 'stream-1' , 'Hello' ) ] ,
0 commit comments