@@ -4259,6 +4259,129 @@ it.layer(TestLayer)("ProjectionStoreV2", (it) => {
42594259 } ) ,
42604260 ) ;
42614261
4262+ it . effect (
4263+ "projects each outstanding monitor by name across snapshots and live shell updates" ,
4264+ ( ) =>
4265+ Effect . gen ( function * ( ) {
4266+ const store = yield * ProjectionStore . ProjectionStoreV2 ;
4267+ const sql = yield * SqlClient . SqlClient ;
4268+ const threadId = ThreadId . make ( "thread:monitor-labels" ) ;
4269+ const now = yield * DateTime . now ;
4270+ const nowIso = DateTime . formatIso ( now ) ;
4271+ yield * store . apply ( {
4272+ id : EventId . make ( "event:monitor-labels:thread" ) ,
4273+ type : "thread.created" ,
4274+ threadId,
4275+ occurredAt : now ,
4276+ payload : {
4277+ createdBy : "user" ,
4278+ creationSource : "web" ,
4279+ id : threadId ,
4280+ projectId : ProjectId . make ( "project:monitor-labels" ) ,
4281+ title : "Watch the build" ,
4282+ providerInstanceId,
4283+ modelSelection,
4284+ runtimeMode : "full-access" ,
4285+ interactionMode : "default" ,
4286+ branch : null ,
4287+ worktreePath : null ,
4288+ activeProviderThreadId : null ,
4289+ lineage : { parentThreadId : null , relationshipToParent : null , rootThreadId : threadId } ,
4290+ forkedFrom : null ,
4291+ createdAt : now ,
4292+ updatedAt : now ,
4293+ archivedAt : null ,
4294+ settledOverride : null ,
4295+ settledAt : null ,
4296+ lastVisitedAt : null ,
4297+ deletedAt : null ,
4298+ } ,
4299+ } ) ;
4300+ for ( const [ id , status , label ] of [
4301+ [ "a-build" , "active" , 'Build "release" finishes' ] ,
4302+ [ "b-checks" , "triggered" , "PR checks pass" ] ,
4303+ [ "c-cancelled" , "cancelled" , "Old wait" ] ,
4304+ [ "d-delivered" , "delivered" , "Finished wait" ] ,
4305+ [ "e-failed" , "failed" , "Failed wait" ] ,
4306+ ] ) {
4307+ yield * sql `
4308+ INSERT INTO thread_monitors (
4309+ monitor_id, thread_id, label, condition_type, continuation_mode,
4310+ status, created_at, updated_at
4311+ ) VALUES (${ id } , ${ threadId } , ${ label } , 'signal', 'record-only',
4312+ ${ status } , ${ nowIso } , ${ nowIso } )
4313+ ` ;
4314+ }
4315+ const expected = [
4316+ {
4317+ taskId : "thread-monitor:a-build" ,
4318+ kind : "monitor" as const ,
4319+ description : 'Build "release" finishes' ,
4320+ } ,
4321+ {
4322+ taskId : "thread-monitor:b-checks" ,
4323+ kind : "monitor" as const ,
4324+ description : "PR checks pass" ,
4325+ } ,
4326+ ] ;
4327+ const snapshot = ( yield * store . getShellSnapshot ( ) ) . threads . find ( ( t ) => t . id === threadId ) ;
4328+ const live = yield * store . getThreadShell ( threadId ) ;
4329+ assert . deepEqual ( snapshot ?. pendingBackgroundTasks , expected ) ;
4330+ assert . deepEqual ( live ?. pendingBackgroundTasks , expected ) ;
4331+ assert . equal ( live ?. backgroundLiveness , "monitoring" ) ;
4332+
4333+ const runId = RunId . make ( "run:monitor-labels" ) ;
4334+ yield * store . apply ( {
4335+ id : EventId . make ( "event:monitor-labels:run" ) ,
4336+ type : "run.created" ,
4337+ threadId,
4338+ runId,
4339+ driver,
4340+ providerInstanceId,
4341+ occurredAt : now ,
4342+ payload : {
4343+ id : runId ,
4344+ threadId,
4345+ ordinal : 1 ,
4346+ providerInstanceId,
4347+ modelSelection,
4348+ providerThreadId : null ,
4349+ userMessageId : MessageId . make ( "message:monitor-labels" ) ,
4350+ rootNodeId : null ,
4351+ activeAttemptId : null ,
4352+ status : "running" ,
4353+ requestedAt : now ,
4354+ startedAt : now ,
4355+ completedAt : null ,
4356+ checkpointId : null ,
4357+ contextHandoffId : null ,
4358+ } ,
4359+ } ) ;
4360+ const working = yield * store . getThreadShell ( threadId ) ;
4361+ assert . deepEqual ( working ?. pendingBackgroundTasks , [ ] ) ;
4362+ assert . equal ( working ?. backgroundLiveness , "monitoring" ) ;
4363+
4364+ const activeRun = ( yield * store . getThreadProjection ( threadId ) ) . runs [ 0 ] ! ;
4365+ yield * store . apply ( {
4366+ id : EventId . make ( "event:monitor-labels:completed" ) ,
4367+ type : "run.updated" ,
4368+ threadId,
4369+ runId,
4370+ occurredAt : now ,
4371+ payload : { ...activeRun , status : "completed" , completedAt : now } ,
4372+ } ) ;
4373+ yield * sql `UPDATE thread_monitors SET status = 'cancelled'
4374+ WHERE monitor_id = 'a-build'` ;
4375+ const remaining = yield * store . getThreadShell ( threadId ) ;
4376+ assert . deepEqual ( remaining ?. pendingBackgroundTasks , expected . slice ( 1 ) ) ;
4377+ yield * sql `UPDATE thread_monitors SET status = 'delivered'
4378+ WHERE monitor_id = 'b-checks'` ;
4379+ const finished = yield * store . getThreadShell ( threadId ) ;
4380+ assert . deepEqual ( finished ?. pendingBackgroundTasks , [ ] ) ;
4381+ assert . equal ( finished ?. backgroundLiveness , null ) ;
4382+ } ) ,
4383+ ) ;
4384+
42624385 it . effect ( "dates a completion released by held background work at the release" , ( ) =>
42634386 Effect . gen ( function * ( ) {
42644387 const store = yield * ProjectionStore . ProjectionStoreV2 ;
0 commit comments