@@ -6522,6 +6522,113 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
65226522 } ) ,
65236523 ) ;
65246524
6525+ it . effect (
6526+ "reads the shell of a thread with a long finished run history without scanning it" ,
6527+ ( ) =>
6528+ Effect . gen ( function * ( ) {
6529+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
6530+ const sql = yield * SqlClient . SqlClient ;
6531+ const suffix = "shell-long-history" ;
6532+ const threadId = ThreadId . make ( `thread:${ suffix } ` ) ;
6533+ const now = yield * DateTime . now ;
6534+ yield * projectionStore . apply ( {
6535+ id : EventId . make ( `event:${ suffix } :thread-created` ) ,
6536+ type : "thread.created" ,
6537+ threadId,
6538+ occurredAt : now ,
6539+ payload : {
6540+ createdBy : "user" ,
6541+ creationSource : "web" ,
6542+ id : threadId ,
6543+ projectId : ProjectId . make ( `project:${ suffix } ` ) ,
6544+ title : "Long finished history" ,
6545+ providerInstanceId,
6546+ modelSelection,
6547+ runtimeMode : "full-access" ,
6548+ interactionMode : "default" ,
6549+ branch : null ,
6550+ worktreePath : null ,
6551+ activeProviderThreadId : null ,
6552+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
6553+ forkedFrom : null ,
6554+ createdAt : now ,
6555+ updatedAt : now ,
6556+ archivedAt : null ,
6557+ settledOverride : null ,
6558+ settledAt : null ,
6559+ lastVisitedAt : null ,
6560+ deletedAt : null ,
6561+ } ,
6562+ } ) ;
6563+ for ( let ordinal = 1 ; ordinal <= 3 ; ordinal ++ ) {
6564+ const runId = RunId . make ( `run:${ suffix } :${ ordinal } ` ) ;
6565+ const rootNodeId = NodeId . make ( `node:${ suffix } :${ ordinal } ` ) ;
6566+ yield * projectionStore . apply ( {
6567+ id : EventId . make ( `event:${ suffix } :run-${ ordinal } ` ) ,
6568+ type : "run.created" ,
6569+ threadId,
6570+ runId,
6571+ nodeId : rootNodeId ,
6572+ driver,
6573+ providerInstanceId,
6574+ occurredAt : now ,
6575+ payload : {
6576+ id : runId ,
6577+ threadId,
6578+ ordinal,
6579+ providerInstanceId,
6580+ modelSelection,
6581+ providerThreadId : null ,
6582+ userMessageId : MessageId . make ( `message:${ suffix } :${ ordinal } ` ) ,
6583+ rootNodeId,
6584+ activeAttemptId : null ,
6585+ status : "completed" as const ,
6586+ requestedAt : now ,
6587+ startedAt : now ,
6588+ completedAt : now ,
6589+ checkpointId : null ,
6590+ contextHandoffId : null ,
6591+ } ,
6592+ } ) ;
6593+ }
6594+
6595+ const { statements, tracer } = traceSqlStatements ( ) ;
6596+ const shell = yield * projectionStore
6597+ . getThreadShell ( threadId )
6598+ . pipe ( Effect . withTracer ( tracer ) ) ;
6599+ assert . isNotNull ( shell ) ;
6600+ const statement = statements . find ( ( query ) => query . includes ( "AS activity_run_status" ) ) ;
6601+ assert . isDefined ( statement ) ;
6602+ const plan = yield * sql . unsafe < {
6603+ readonly id : number ;
6604+ readonly parent : number ;
6605+ readonly detail : string ;
6606+ } > ( `EXPLAIN QUERY PLAN ${ statement } ` ) ;
6607+ // With no live run, a lookup that walks the ordinal index reads every run the thread has had.
6608+ const liveRunLookups = plan . filter ( ( row ) =>
6609+ row . detail . startsWith ( "SEARCH r USING INDEX orchestration_v2_projection_runs_" ) ,
6610+ ) ;
6611+ assert . isAbove ( liveRunLookups . length , 0 ) ;
6612+ for ( const lookup of liveRunLookups ) {
6613+ assert . include (
6614+ lookup . detail ,
6615+ "orchestration_v2_projection_runs_thread_status_idx (thread_id=? AND status=?)" ,
6616+ ) ;
6617+ }
6618+ // The newest run needs no sort, but the run that ended last must come from an ordered index.
6619+ const candidateLookups = plan . filter ( ( row ) =>
6620+ row . detail . startsWith ( "SEARCH candidate USING INDEX orchestration_v2_projection_runs_" ) ,
6621+ ) ;
6622+ assert . isAbove ( candidateLookups . length , 0 ) ;
6623+ const sortedCandidates = plan . filter (
6624+ ( row ) =>
6625+ row . detail === "USE TEMP B-TREE FOR ORDER BY" &&
6626+ candidateLookups . some ( ( lookup ) => lookup . parent === row . parent ) ,
6627+ ) ;
6628+ assert . deepEqual ( sortedCandidates , [ ] ) ;
6629+ } ) ,
6630+ ) ;
6631+
65256632 it . effect ( "selects the latest waiting secret only from active runs" , ( ) =>
65266633 Effect . gen ( function * ( ) {
65276634 const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments