11import * as NodeServices from "@effect/platform-node/NodeServices" ;
22import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
3- import { assert , it } from "@effect/vitest" ;
3+ import { assert , it , vi } from "@effect/vitest" ;
44import {
55 AuthSessionId ,
66 CommandId ,
@@ -42,6 +42,7 @@ import * as Stream from "effect/Stream";
4242import * as TestClock from "effect/testing/TestClock" ;
4343import * as Scope from "effect/Scope" ;
4444import * as Exit from "effect/Exit" ;
45+ import * as Fiber from "effect/Fiber" ;
4546import { Headers } from "effect/http" ;
4647import { Rpc , RpcGroup , RpcMessage , RpcServer } from "effect/rpc" ;
4748import * as SqlClient from "effect/sql/SqlClient" ;
@@ -59,6 +60,7 @@ import * as CheckpointDiffQuery from "./checkpointing/CheckpointDiffQuery.ts";
5960import * as CheckpointStore from "./checkpointing/CheckpointStore.ts" ;
6061import { CheckpointTurnRangeUnavailableError } from "./checkpointing/Errors.ts" ;
6162import * as CommandOutputHub from "./orchestration-v2/CommandOutputHub.ts" ;
63+ import * as CommandReceiptStore from "./orchestration-v2/CommandReceiptStore.ts" ;
6264import * as CodexInstallation from "./provider/CodexInstallation.ts" ;
6365import * as DeviceService from "./device/DeviceService.ts" ;
6466import * as DirectEndpoints from "./environment/DirectEndpoints.ts" ;
@@ -621,6 +623,10 @@ it.effect.each(["family", "worktree"] as const)(
621623 yield * projection . apply ( create ( id , parent ) ) ;
622624 }
623625 let keepDuringRead = false ;
626+ let checkLockOrder = false ;
627+ const readerHeld = yield * Deferred . make < void > ( ) ;
628+ const receiptHasStorage = yield * Deferred . make < void > ( ) ;
629+ const lockCycle = yield * Deferred . make < void > ( ) ;
624630 const responses =
625631 yield * Queue . unbounded <
626632 Exit . Exit <
@@ -679,9 +685,40 @@ it.effect.each(["family", "worktree"] as const)(
679685 Layer . mock ( ManagedProjectFolders . ManagedProjectFolders ) ( {
680686 namedProjectsRoot : process . cwd ( ) ,
681687 } ) ,
688+ Layer . succeed ( TemporaryThreadStorage . TemporaryThreadStores , {
689+ ...stores ,
690+ getShellSnapshot : ( options ) =>
691+ stores . getShellSnapshot ( options ) . pipe (
692+ Effect . tap ( ( ) => {
693+ if ( ! keepDuringRead ) return Effect . void ;
694+ keepDuringRead = false ;
695+ return storage
696+ . keep ( {
697+ threadId : ThreadId . make ( "racing" ) ,
698+ commandId : CommandId . make ( "keep-racing" ) ,
699+ } )
700+ . pipe (
701+ Effect . mapError (
702+ ( cause ) =>
703+ new ProjectionStore . ProjectionStoreReadError ( {
704+ threadId : ThreadId . make ( "racing" ) ,
705+ cause,
706+ } ) ,
707+ ) ,
708+ ) ;
709+ } ) ,
710+ ) ,
711+ } ) ,
682712 Layer . mock ( ThreadManagement . ThreadManagementService ) ( {
683713 readShellSnapshot : ( options ) =>
684- projection . readShellSnapshot ( options ) . pipe (
714+ Effect . gen ( function * ( ) {
715+ if ( checkLockOrder ) {
716+ yield * Deferred . succeed ( readerHeld , undefined ) ;
717+ yield * Deferred . await ( receiptHasStorage ) ;
718+ yield * Deferred . succeed ( lockCycle , undefined ) ;
719+ }
720+ return yield * projection . readShellSnapshot ( options ) ;
721+ } ) . pipe (
685722 Effect . map ( ( decode ) =>
686723 decode . pipe (
687724 Effect . tap ( ( ) => {
@@ -784,6 +821,30 @@ it.effect.each(["family", "worktree"] as const)(
784821 ? [ "racing-child" ]
785822 : [ "private" , "private-child" , "racing" , "racing-child" , "saved" , "saved-child" ] ,
786823 ) ;
824+ checkLockOrder = true ;
825+ const familyRead = yield * read ( true ) . pipe ( Effect . forkChild ) ;
826+ const acquiredReader = yield * Effect . raceFirst (
827+ Deferred . await ( readerHeld ) . pipe ( Effect . as ( true ) ) ,
828+ Fiber . join ( familyRead ) . pipe ( Effect . as ( false ) ) ,
829+ ) ;
830+ const getReceipt = stores . volatile . applicationReceipts . getByCommandId ;
831+ using _receipt = vi
832+ . spyOn ( stores . volatile . applicationReceipts , "getByCommandId" )
833+ . mockImplementation ( ( input ) =>
834+ Deferred . succeed ( receiptHasStorage , undefined ) . pipe ( Effect . andThen ( getReceipt ( input ) ) ) ,
835+ ) ;
836+ const receiptRead = yield * Context . get ( services , CommandReceiptStore . CommandReceiptStoreV2 )
837+ . getByCommandId ( CommandId . make ( "lock-order-receipt" ) )
838+ . pipe ( Effect . forkChild ) ;
839+ if ( acquiredReader ) yield * Deferred . await ( receiptHasStorage ) ;
840+ const completed = yield * Effect . raceFirst (
841+ Effect . all ( [ Fiber . join ( familyRead ) , Fiber . join ( receiptRead ) ] ) . pipe ( Effect . as ( true ) ) ,
842+ Deferred . await ( lockCycle ) . pipe ( Effect . as ( false ) ) ,
843+ ) ;
844+ assert . isTrue (
845+ completed ,
846+ "Shell and receipt reads must finish without a storage/SQL lock cycle" ,
847+ ) ;
787848 } ) . pipe ( Effect . scoped ) ,
788849) ;
789850
0 commit comments