@@ -633,6 +633,94 @@ describe("applyThreadDetailEvent", () => {
633633 expect ( result . thread . activities [ 0 ] ?. id ) . toBe ( "activity-0" ) ;
634634 }
635635 } ) ;
636+
637+ it ( "replaces earlier resolvable context-window updates for the same turn" , ( ) => {
638+ const contextWindowActivity = ( id : string , sequence : number , usedTokens : unknown ) => ( {
639+ id : EventId . make ( id ) ,
640+ tone : "info" as const ,
641+ kind : "context-window.updated" ,
642+ summary : "Context window updated" ,
643+ payload : { usedTokens } ,
644+ turnId : TurnId . make ( "turn-1" ) ,
645+ sequence,
646+ createdAt : "2026-04-01T11:00:00.000Z" ,
647+ } ) ;
648+ const otherTurnActivity = contextWindowActivity ( "activity-other-turn" , 2 , 500 ) ;
649+ const existingActivities = [
650+ contextWindowActivity ( "activity-cw-1" , 1 , 1_000 ) ,
651+ { ...otherTurnActivity , turnId : TurnId . make ( "turn-0" ) } ,
652+ // Malformed row (no usedTokens): must survive, and must not be
653+ // treated as the latest value by consumers.
654+ contextWindowActivity ( "activity-cw-malformed" , 3 , undefined ) ,
655+ contextWindowActivity ( "activity-cw-2" , 4 , 2_000 ) ,
656+ ] ;
657+
658+ const result = applyThreadDetailEvent (
659+ { ...baseThread , activities : existingActivities } ,
660+ {
661+ ...baseEventFields ,
662+ sequence : 20 ,
663+ occurredAt : "2026-04-01T11:02:00.000Z" ,
664+ aggregateKind : "thread" ,
665+ aggregateId : ThreadId . make ( "thread-1" ) ,
666+ type : "thread.activity-appended" ,
667+ payload : {
668+ threadId : ThreadId . make ( "thread-1" ) ,
669+ activity : contextWindowActivity ( "activity-cw-3" , 5 , 3_000 ) ,
670+ } ,
671+ } ,
672+ ) ;
673+
674+ expect ( result . kind ) . toBe ( "updated" ) ;
675+ if ( result . kind === "updated" ) {
676+ const ids = result . thread . activities . map ( ( activity ) => activity . id ) ;
677+ // Same-turn resolvable rows collapse to the newest; the other turn's
678+ // row and the malformed row are untouched.
679+ expect ( ids ) . toEqual ( [ "activity-other-turn" , "activity-cw-malformed" , "activity-cw-3" ] ) ;
680+ }
681+ } ) ;
682+
683+ it ( "does not collapse context-window history for a malformed update" , ( ) => {
684+ const resolvable = {
685+ id : EventId . make ( "activity-cw-resolvable" ) ,
686+ tone : "info" as const ,
687+ kind : "context-window.updated" ,
688+ summary : "Context window updated" ,
689+ payload : { usedTokens : 1_000 } ,
690+ turnId : TurnId . make ( "turn-1" ) ,
691+ sequence : 1 ,
692+ createdAt : "2026-04-01T11:00:00.000Z" ,
693+ } ;
694+
695+ const result = applyThreadDetailEvent (
696+ { ...baseThread , activities : [ resolvable ] } ,
697+ {
698+ ...baseEventFields ,
699+ sequence : 21 ,
700+ occurredAt : "2026-04-01T11:03:00.000Z" ,
701+ aggregateKind : "thread" ,
702+ aggregateId : ThreadId . make ( "thread-1" ) ,
703+ type : "thread.activity-appended" ,
704+ payload : {
705+ threadId : ThreadId . make ( "thread-1" ) ,
706+ activity : {
707+ ...resolvable ,
708+ id : EventId . make ( "activity-cw-broken" ) ,
709+ payload : { usedTokens : Number . NaN } ,
710+ sequence : 2 ,
711+ } ,
712+ } ,
713+ } ,
714+ ) ;
715+
716+ expect ( result . kind ) . toBe ( "updated" ) ;
717+ if ( result . kind === "updated" ) {
718+ // The resolvable row must survive so consumers can still derive a
719+ // usage value by walking backwards past the malformed row.
720+ const ids = result . thread . activities . map ( ( activity ) => activity . id ) ;
721+ expect ( ids ) . toEqual ( [ "activity-cw-resolvable" , "activity-cw-broken" ] ) ;
722+ }
723+ } ) ;
636724 } ) ;
637725
638726 describe ( "thread.turn-diff-completed" , ( ) => {
0 commit comments