@@ -60,7 +60,7 @@ function makeOrchestrationLayer() {
6060 ) ,
6161 OrchestrationProjectionSnapshotQueryLive ,
6262 ) . pipe (
63- Layer . provide ( ThreadBackgroundLiveness . layer ) ,
63+ Layer . provideMerge ( ThreadBackgroundLiveness . layer ) ,
6464 Layer . provide ( ThreadPlanProgress . layer ) ,
6565 Layer . provide ( OrchestrationEventStoreLive ) ,
6666 Layer . provideMerge ( OrchestrationCommandReceiptRepositoryLive ) ,
@@ -224,6 +224,7 @@ describe("OrchestrationEngine", () => {
224224 } satisfies OrchestrationProjectionPipelineShape ) ,
225225 ) ,
226226 Layer . provide ( Layer . succeed ( OrchestrationEventStore , eventStore ) ) ,
227+ Layer . provide ( ThreadBackgroundLiveness . layer ) ,
227228 Layer . provide ( OrchestrationCommandReceiptRepositoryLive ) ,
228229 Layer . provide ( SqlitePersistenceMemory ) ,
229230 Layer . provideMerge ( NodeServices . layer ) ,
@@ -322,15 +323,17 @@ describe("OrchestrationEngine", () => {
322323 ) ;
323324
324325 effectIt . effect (
325- "rejects same-millisecond thread changes without blocking on unrelated threads" ,
326+ "rejects persisted changes and live background work without blocking unrelated threads" ,
326327 ( ) =>
327328 Effect . gen ( function * ( ) {
328329 yield * TestClock . setTime ( Date . parse ( now ( ) ) ) ;
329330 const engine = yield * OrchestrationEngineService ;
330331 const snapshots = yield * ProjectionSnapshotQuery ;
332+ const backgroundLiveness = yield * ThreadBackgroundLiveness . ThreadBackgroundLivenessService ;
331333 const projectId = ProjectId . make ( "project-auto-settle-guard" ) ;
332334 const guardedThreadId = ThreadId . make ( "thread-auto-settle-guarded" ) ;
333335 const unrelatedThreadId = ThreadId . make ( "thread-auto-settle-unrelated" ) ;
336+ const liveThreadId = ThreadId . make ( "thread-auto-settle-live" ) ;
334337
335338 yield * engine . dispatch ( {
336339 type : "project.create" ,
@@ -340,7 +343,7 @@ describe("OrchestrationEngine", () => {
340343 workspaceRoot : "/tmp/project-auto-settle-guard" ,
341344 createdAt : now ( ) ,
342345 } ) ;
343- for ( const threadId of [ guardedThreadId , unrelatedThreadId ] ) {
346+ for ( const threadId of [ guardedThreadId , unrelatedThreadId , liveThreadId ] ) {
344347 yield * engine . dispatch ( {
345348 type : "thread.create" ,
346349 commandId : CommandId . make ( `cmd-create-${ threadId } ` ) ,
@@ -385,6 +388,43 @@ describe("OrchestrationEngine", () => {
385388 . pipe ( Effect . flip ) ;
386389 expect ( staleError . _tag ) . toBe ( "OrchestrationCommandInvariantError" ) ;
387390
391+ const livenessSnapshotSequence = yield * engine . latestSequence ;
392+ for ( const [ taskType , expectedLiveness ] of [
393+ [ "subagent" , "working" ] ,
394+ [ "local_bash" , "monitoring" ] ,
395+ ] as const ) {
396+ backgroundLiveness . recordTaskLiveness ( {
397+ threadId : liveThreadId ,
398+ taskId : `task-${ expectedLiveness } ` ,
399+ taskType,
400+ status : undefined ,
401+ kind : "started" ,
402+ } ) ;
403+ expect ( backgroundLiveness . getThreadBackgroundLiveness ( liveThreadId ) ) . toBe (
404+ expectedLiveness ,
405+ ) ;
406+ expect ( yield * engine . latestSequence ) . toBe ( livenessSnapshotSequence ) ;
407+
408+ const livenessError = yield * engine
409+ . dispatch ( {
410+ type : "thread.auto-settle" ,
411+ commandId : CommandId . make ( `cmd-auto-settle-${ expectedLiveness } ` ) ,
412+ threadId : liveThreadId ,
413+ snapshotSequence : livenessSnapshotSequence ,
414+ } )
415+ . pipe ( Effect . flip ) ;
416+ expect ( livenessError . _tag ) . toBe ( "OrchestrationCommandInvariantError" ) ;
417+ expect ( yield * engine . latestSequence ) . toBe ( livenessSnapshotSequence ) ;
418+ backgroundLiveness . clearThreadLiveness ( liveThreadId ) ;
419+ }
420+
421+ yield * engine . dispatch ( {
422+ type : "thread.auto-settle" ,
423+ commandId : CommandId . make ( "cmd-auto-settle-after-liveness-cleared" ) ,
424+ threadId : liveThreadId ,
425+ snapshotSequence : livenessSnapshotSequence ,
426+ } ) ;
427+
388428 const freshSnapshotSequence = yield * engine . latestSequence ;
389429 yield * engine . dispatch ( {
390430 type : "thread.meta.update" ,
@@ -403,6 +443,9 @@ describe("OrchestrationEngine", () => {
403443 expect (
404444 settled . threads . find ( ( thread ) => thread . id === guardedThreadId ) ?. settledOverride ,
405445 ) . toBe ( "settled" ) ;
446+ expect ( settled . threads . find ( ( thread ) => thread . id === liveThreadId ) ?. settledOverride ) . toBe (
447+ "settled" ,
448+ ) ;
406449 } ) . pipe ( Effect . provide ( makeOrchestrationLayer ( ) ) ) ,
407450 ) ;
408451
0 commit comments