@@ -35,13 +35,18 @@ import { formatSubagentDisplayTitle } from "./subagentDisplay.ts";
3535const ACTIVITY_RUN_STATUSES = new Set ( [ "preparing" , "starting" , "running" , "waiting" ] ) ;
3636const INTERRUPTIBLE_RUN_STATUSES = new Set ( [ "preparing" , "starting" , "running" ] ) ;
3737
38+ type ThreadActivityOptions = { readonly includeIdleCompaction ?: boolean } ;
39+
3840function latestMatchingRun (
3941 projection : OrchestrationV2ThreadProjection ,
4042 predicate : ( run : OrchestrationV2ThreadProjection [ "runs" ] [ number ] ) => boolean ,
43+ { includeIdleCompaction = false } : ThreadActivityOptions = { } ,
4144) : OrchestrationV2ThreadProjection [ "runs" ] [ number ] | null {
4245 return projection . runs . reduce < OrchestrationV2ThreadProjection [ "runs" ] [ number ] | null > (
4346 ( latest , candidate ) =>
44- predicate ( candidate ) && ( latest === null || candidate . ordinal > latest . ordinal )
47+ ( includeIdleCompaction || candidate . idleCompactionOfRunId === undefined ) &&
48+ predicate ( candidate ) &&
49+ ( latest === null || candidate . ordinal > latest . ordinal )
4550 ? candidate
4651 : latest ,
4752 null ,
@@ -104,13 +109,18 @@ export function deriveLatestThreadRun(
104109 * Returns the run that owns live provider work, falling back to the newest run
105110 * once the thread is idle. A newer queued run must not make an older executing
106111 * run look settled in clients that render per-run activity.
112+ * Open chat controls opt into idle compaction so it can still be stopped.
107113 */
108114export function deriveThreadActivityRun (
109115 projection : OrchestrationV2ThreadProjection ,
116+ options : ThreadActivityOptions = { } ,
110117) : ThreadRunSummary | null {
111118 const run =
112- latestMatchingRun ( projection , ( candidate ) => ACTIVITY_RUN_STATUSES . has ( candidate . status ) ) ??
113- presentedLatestRun ( projection ) ;
119+ latestMatchingRun (
120+ projection ,
121+ ( candidate ) => ACTIVITY_RUN_STATUSES . has ( candidate . status ) ,
122+ options ,
123+ ) ?? presentedLatestRun ( projection ) ;
114124 return run === null ? null : summarizeThreadRun ( projection , run ) ;
115125}
116126
@@ -230,16 +240,19 @@ export function formatProviderSubagentStatus(
230240
231241export function deriveThreadRuntime (
232242 projection : OrchestrationV2ThreadProjection ,
243+ options : ThreadActivityOptions = { } ,
233244) : ThreadRuntimeSummary | null {
234245 const latestRun = deriveLatestThreadRun ( projection ) ;
235246 const providerSession = projection . providerSessions . findLast (
236247 ( session ) => session . providerInstanceId === projection . thread . providerInstanceId ,
237248 ) ;
238249 const usageLimitedRun = presentedUsageLimitRun ( projection ) ;
239250 const latestRunProjection = presentedLatestRun ( projection ) ;
240- const activityRun = deriveThreadActivityRun ( projection ) ;
241- const liveActivityRun = latestMatchingRun ( projection , ( run ) =>
242- ACTIVITY_RUN_STATUSES . has ( run . status ) ,
251+ const activityRun = deriveThreadActivityRun ( projection , options ) ;
252+ const liveActivityRun = latestMatchingRun (
253+ projection ,
254+ ( run ) => ACTIVITY_RUN_STATUSES . has ( run . status ) ,
255+ options ,
243256 ) ;
244257 // Same rule as the shell runtime: only background work that holds the
245258 // completion parks the thread at idle; a dev server left running does not.
@@ -262,7 +275,8 @@ export function deriveThreadRuntime(
262275 return null ;
263276 }
264277 const activeRunId =
265- latestMatchingRun ( projection , ( run ) => INTERRUPTIBLE_RUN_STATUSES . has ( run . status ) ) ?. id ?? null ;
278+ latestMatchingRun ( projection , ( run ) => INTERRUPTIBLE_RUN_STATUSES . has ( run . status ) , options )
279+ ?. id ?? null ;
266280 return {
267281 status : usageLimitedRun
268282 ? "failed"
0 commit comments