88 collectAssistantCitations ,
99} from "@t3tools/shared/assistantCitations" ;
1010import { shallow } from "zustand/vanilla/shallow" ;
11+ import { claudeResumeCompactionAge } from "@t3tools/shared/claudeCompaction" ;
1112import type { HandoffTimelineRun } from "@t3tools/client-runtime/handoff" ;
1213import { renderCodexDirectivesForCopy } from "@t3tools/shared/codexMarkdownDirectives" ;
1314import {
@@ -589,8 +590,8 @@ type MessagesTimelineRowContent =
589590 label : string ;
590591 tokenCounts : ReturnType < typeof contextCompactionTokenCounts > ;
591592 active : boolean ;
592- /** T3 Code compacted an idle thread on its own ; absent for mid-turn and typed compactions. */
593- automatic ?: boolean ;
593+ /** Why T3 Code compacted without being asked ; absent for typed and provider compactions. */
594+ note ?: string ;
594595 }
595596 | {
596597 kind : "message" ;
@@ -1577,23 +1578,38 @@ export function deriveMessagesTimelineRows(input: {
15771578 activeWorkRow !== null || latestToolFailed ? activeToolEntries . map ( ( entry ) => entry . id ) : [ ] ,
15781579 ) ;
15791580 // A bare `/compact` turn is the compaction itself: one compaction row stands in for its prompt
1580- // and working indicator, from the moment the turn starts.
1581- const compactionRuns = new Map < RunId , { automatic : boolean ; hasItem : boolean } > ( ) ;
1581+ // and working indicator, from the moment the turn starts. Likewise an answered "compact before
1582+ // continuing?" question folds into the compaction it asked for.
1583+ const compactionRuns = new Map < RunId , { note : string | undefined ; hasItem : boolean } > ( ) ;
1584+ const compactedRunIds = new Set < RunId > ( ) ;
1585+ const resumeCompactionAgeByRunId = new Map < RunId , string > ( ) ;
15821586 for ( const entry of timelineEntries ) {
15831587 if (
15841588 entry . kind === "message" &&
15851589 entry . message . runId !== null &&
15861590 isCompactionCommandMessage ( entry . message )
15871591 ) {
15881592 compactionRuns . set ( entry . message . runId , {
1589- automatic : isIdleCompactionMessage ( entry . message ) ,
1593+ note : isIdleCompactionMessage ( entry . message ) ? "Automatic before cache expiry" : undefined ,
15901594 hasItem : false ,
15911595 } ) ;
1592- } else if ( timelineEntryIsCompaction ( entry ) && entry . kind === "work" && entry . entry . runId ) {
1593- const run = compactionRuns . get ( entry . entry . runId ) ;
1594- if ( run ) run . hasItem = true ;
1596+ } else if ( entry . kind === "work" && entry . entry . runId ) {
1597+ if ( timelineEntryIsCompaction ( entry ) ) {
1598+ compactedRunIds . add ( entry . entry . runId ) ;
1599+ const run = compactionRuns . get ( entry . entry . runId ) ;
1600+ if ( run ) run . hasItem = true ;
1601+ }
1602+ const age = entry . entry . questionAnswer
1603+ ? claudeResumeCompactionAge ( entry . entry . questionAnswer )
1604+ : null ;
1605+ if ( age !== null ) resumeCompactionAgeByRunId . set ( entry . entry . runId , age ) ;
15951606 }
15961607 }
1608+ const compactionNote = ( runId : RunId | null | undefined ) => {
1609+ if ( runId == null ) return undefined ;
1610+ const age = resumeCompactionAgeByRunId . get ( runId ) ;
1611+ return compactionRuns . get ( runId ) ?. note ?? ( age ? `On return after ${ age } idle` : undefined ) ;
1612+ } ;
15971613 const activeRunIsCompaction =
15981614 input . isWorking && unsettledRunId !== null && compactionRuns . has ( unsettledRunId ) ;
15991615 const appendWorkingRow = ( ) => {
@@ -1688,10 +1704,11 @@ export function deriveMessagesTimelineRows(input: {
16881704 const compactionRunId = timelineEntry . entry . runId ;
16891705 const compactionRun =
16901706 compactionRunId == null ? undefined : compactionRuns . get ( compactionRunId ) ;
1707+ const note = compactionNote ( compactionRunId ) ;
16911708 nextRows . push ( {
16921709 kind : "context-compaction" ,
16931710 id : compactionRun ? `compaction:${ compactionRunId } ` : timelineEntry . id ,
1694- ...( compactionRun ?. automatic ? { automatic : true } : { } ) ,
1711+ ...( note ? { note } : { } ) ,
16951712 createdAt : timelineEntry . createdAt ,
16961713 label : timelineEntry . entry . label ,
16971714 tokenCounts :
@@ -1701,6 +1718,16 @@ export function deriveMessagesTimelineRows(input: {
17011718 continue ;
17021719 }
17031720
1721+ if (
1722+ timelineEntry . kind === "work" &&
1723+ timelineEntry . entry . runId != null &&
1724+ compactedRunIds . has ( timelineEntry . entry . runId ) &&
1725+ timelineEntry . entry . questionAnswer !== undefined &&
1726+ claudeResumeCompactionAge ( timelineEntry . entry . questionAnswer ) !== null
1727+ ) {
1728+ continue ;
1729+ }
1730+
17041731 if ( timelineEntry . kind === "work" ) {
17051732 if (
17061733 ( timelineEntry . entry . runId != null && failedRunIds . has ( timelineEntry . entry . runId ) ) ||
@@ -1926,7 +1953,7 @@ export function deriveMessagesTimelineRows(input: {
19261953 label : active ? "Compacting context" : "Context compaction" ,
19271954 tokenCounts : null ,
19281955 active,
1929- ...( compactionRun . automatic ? { automatic : true } : { } ) ,
1956+ ...( compactionRun . note ? { note : compactionRun . note } : { } ) ,
19301957 } ) ;
19311958 }
19321959 continue ;
@@ -2322,7 +2349,7 @@ function isRowUnchanged(a: MessagesTimelineRow, b: MessagesTimelineRow): boolean
23222349 a . createdAt === bc . createdAt &&
23232350 a . label === bc . label &&
23242351 a . active === bc . active &&
2325- a . automatic === bc . automatic
2352+ a . note === bc . note
23262353 ) ;
23272354 }
23282355
0 commit comments