@@ -2374,55 +2374,170 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
23742374 } ) ,
23752375 ) ;
23762376
2377- it . effect ( "uses indexed shell reads and batches item counts " , ( ) =>
2377+ it . effect ( "looks up shell stop requests through the interrupt request index " , ( ) =>
23782378 Effect . gen ( function * ( ) {
23792379 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
23802380 const sql = yield * SqlClient . SqlClient ;
2381- const { projectId, activeThreadId, forkThreadId } =
2382- yield * assertProjectScopedShells ( "sql-shell-item-counts" ) ;
2381+ const { projectId } = yield * assertProjectScopedShells ( "sql-shell-stop-index" ) ;
23832382 const { statements, tracer } = traceSqlStatements ( ) ;
2384-
2385- const snapshot = yield * projectionStore
2383+ yield * projectionStore
23862384 . getShellSnapshot ( { projectId, location : "active" } )
23872385 . pipe ( Effect . withTracer ( tracer ) ) ;
2388- assert . sameMembers (
2389- snapshot . threads . map ( ( thread ) => thread . id ) ,
2390- [ activeThreadId , forkThreadId ] ,
2391- ) ;
23922386
23932387 const threadQuery = statements . find ( ( statement ) =>
2394- statement . includes ( "AS forked_from_run_source_thread_id " ) ,
2388+ statement . includes ( "AS latest_run_stop_requested " ) ,
23952389 ) ;
23962390 assert . isDefined ( threadQuery ) ;
2397- assert . isFalse ( threadQuery . includes ( "GROUP BY i.run_id" ) ) ;
2398- const threadPlan = ( yield * sql . unsafe < { readonly detail : string } > (
2391+ const stopRequestLookups = ( yield * sql . unsafe < { readonly detail : string } > (
23992392 `EXPLAIN QUERY PLAN ${ threadQuery } ` ,
2400- ) ) . map ( ( row ) => row . detail ) ;
2401- const stopRequestLookups = threadPlan . filter ( ( detail ) =>
2402- detail . startsWith ( "SEARCH stop_request " ) ,
2403- ) ;
2404- assert . lengthOf ( stopRequestLookups , 2 ) ;
2393+ ) )
2394+ . map ( ( row ) => row . detail )
2395+ . filter ( ( detail ) => detail . startsWith ( "SEARCH stop_request " ) ) ;
2396+ assert . isNotEmpty ( stopRequestLookups ) ;
24052397 for ( const lookup of stopRequestLookups ) {
24062398 assert . include ( lookup , "orchestration_v2_projection_turn_items_stop_request_idx" ) ;
24072399 }
2408-
2409- const countQuery = statements . find (
2410- ( statement ) =>
2411- statement . includes ( "COUNT(*) AS item_count" ) &&
2412- statement . includes ( "AS runless_item_count" ) ,
2413- ) ;
2414- assert . isDefined ( countQuery ) ;
2415- const plan = ( yield * sql . unsafe < { readonly detail : string } > (
2416- `EXPLAIN QUERY PLAN ${ countQuery } ` ,
2417- ) ) . map ( ( row ) => row . detail ) ;
2418- assert . isTrue (
2419- plan . some ( ( detail ) =>
2420- detail . includes ( "orchestration_v2_projection_turn_items_thread_run_idx" ) ,
2421- ) ,
2422- ) ;
24232400 } ) ,
24242401 ) ;
24252402
2403+ it . effect (
2404+ "counts each shell's own runless and live-run items, skipping rolled back and missing runs" ,
2405+ ( ) =>
2406+ Effect . gen ( function * ( ) {
2407+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
2408+ const now = yield * DateTime . now ;
2409+ const projectId = ProjectId . make ( "project:shell-item-counts" ) ;
2410+ const busyThreadId = ThreadId . make ( "thread:shell-item-counts:busy" ) ;
2411+ const emptyThreadId = ThreadId . make ( "thread:shell-item-counts:empty" ) ;
2412+ const otherThreadId = ThreadId . make ( "thread:shell-item-counts:other" ) ;
2413+ for ( const threadId of [ busyThreadId , emptyThreadId , otherThreadId ] ) {
2414+ yield * projectionStore . apply ( {
2415+ id : EventId . make ( `event:${ threadId } :created` ) ,
2416+ type : "thread.created" ,
2417+ threadId,
2418+ occurredAt : now ,
2419+ payload : {
2420+ createdBy : "user" ,
2421+ creationSource : "web" ,
2422+ id : threadId ,
2423+ projectId,
2424+ title : threadId ,
2425+ providerInstanceId,
2426+ modelSelection,
2427+ runtimeMode : "full-access" ,
2428+ interactionMode : "default" ,
2429+ branch : null ,
2430+ worktreePath : null ,
2431+ activeProviderThreadId : null ,
2432+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
2433+ forkedFrom : null ,
2434+ createdAt : now ,
2435+ updatedAt : now ,
2436+ archivedAt : null ,
2437+ settledOverride : null ,
2438+ settledAt : null ,
2439+ lastVisitedAt : null ,
2440+ deletedAt : null ,
2441+ } ,
2442+ } ) ;
2443+ }
2444+ const applyRun = (
2445+ threadId : ThreadId ,
2446+ suffix : string ,
2447+ ordinal : number ,
2448+ rolledBack : boolean ,
2449+ ) => {
2450+ const runId = RunId . make ( `run:shell-item-counts:${ suffix } ` ) ;
2451+ const rootNodeId = NodeId . make ( `node:shell-item-counts:${ suffix } ` ) ;
2452+ return projectionStore
2453+ . apply ( {
2454+ id : EventId . make ( `event:shell-item-counts:${ suffix } :run` ) ,
2455+ type : "run.created" ,
2456+ threadId,
2457+ runId,
2458+ nodeId : rootNodeId ,
2459+ driver,
2460+ occurredAt : now ,
2461+ payload : {
2462+ id : runId ,
2463+ threadId,
2464+ ordinal,
2465+ providerInstanceId,
2466+ modelSelection,
2467+ providerThreadId : null ,
2468+ userMessageId : MessageId . make ( `message:shell-item-counts:${ suffix } :user` ) ,
2469+ rootNodeId,
2470+ activeAttemptId : null ,
2471+ status : rolledBack ? "rolled_back" : "completed" ,
2472+ requestedAt : now ,
2473+ startedAt : now ,
2474+ completedAt : now ,
2475+ checkpointId : null ,
2476+ contextHandoffId : null ,
2477+ } ,
2478+ } )
2479+ . pipe ( Effect . as ( runId ) ) ;
2480+ } ;
2481+ let ordinal = 0 ;
2482+ const applyItem = ( threadId : ThreadId , runId : RunId | null ) => {
2483+ ordinal += 1 ;
2484+ return projectionStore . apply ( {
2485+ id : EventId . make ( `event:shell-item-counts:item:${ ordinal } ` ) ,
2486+ type : "turn-item.updated" ,
2487+ threadId,
2488+ ...( runId === null ? { } : { runId } ) ,
2489+ occurredAt : now ,
2490+ payload : {
2491+ id : TurnItemId . make ( `turn-item:shell-item-counts:${ ordinal } ` ) ,
2492+ threadId,
2493+ runId,
2494+ nodeId : null ,
2495+ providerThreadId : null ,
2496+ providerTurnId : null ,
2497+ nativeItemRef : null ,
2498+ parentItemId : null ,
2499+ ordinal,
2500+ status : "completed" ,
2501+ title : null ,
2502+ startedAt : now ,
2503+ completedAt : now ,
2504+ updatedAt : now ,
2505+ type : "assistant_message" ,
2506+ messageId : MessageId . make ( `message:shell-item-counts:${ ordinal } ` ) ,
2507+ text : `item ${ ordinal } ` ,
2508+ streaming : false ,
2509+ } ,
2510+ } ) ;
2511+ } ;
2512+
2513+ const completedRunId = yield * applyRun ( busyThreadId , "busy-completed" , 1 , false ) ;
2514+ const rolledBackRunId = yield * applyRun ( busyThreadId , "busy-rolled-back" , 2 , true ) ;
2515+ const otherRunId = yield * applyRun ( otherThreadId , "other" , 1 , false ) ;
2516+ yield * applyItem ( busyThreadId , null ) ;
2517+ yield * applyItem ( busyThreadId , null ) ;
2518+ yield * applyItem ( busyThreadId , completedRunId ) ;
2519+ yield * applyItem ( busyThreadId , completedRunId ) ;
2520+ yield * applyItem ( busyThreadId , rolledBackRunId ) ;
2521+ yield * applyItem ( busyThreadId , RunId . make ( "run:shell-item-counts:missing" ) ) ;
2522+ yield * applyItem ( otherThreadId , otherRunId ) ;
2523+
2524+ const snapshot = yield * projectionStore . getShellSnapshot ( { projectId } ) ;
2525+ const expected = [
2526+ [ busyThreadId , 4 ] ,
2527+ [ emptyThreadId , 0 ] ,
2528+ [ otherThreadId , 1 ] ,
2529+ ] as const ;
2530+ for ( const [ threadId , itemCount ] of expected ) {
2531+ const shell = yield * projectionStore . getThreadShell ( threadId ) ;
2532+ assert . deepEqual (
2533+ snapshot . threads . find ( ( thread ) => thread . id === threadId ) ,
2534+ shell ?? undefined ,
2535+ ) ;
2536+ assert . equal ( shell ?. itemCount , itemCount ) ;
2537+ }
2538+ } ) ,
2539+ ) ;
2540+
24262541 it . effect ( "does not treat visited or marked-unread state as thread activity" , ( ) =>
24272542 Effect . gen ( function * ( ) {
24282543 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments