@@ -42,6 +42,7 @@ import * as Stream from "effect/Stream";
4242import * as TestClock from "effect/testing/TestClock" ;
4343import * as SqlClient from "effect/sql/SqlClient" ;
4444
45+ import { createAttachmentId } from "../attachmentStore.ts" ;
4546import * as CheckpointStore from "../checkpointing/CheckpointStore.ts" ;
4647import * as GitWorkflow from "../git/GitWorkflowService.ts" ;
4748import * as ServerConfig from "../config.ts" ;
@@ -716,6 +717,181 @@ it.layer(layerProjectDeletionTest)("project deletion during thread commands", (i
716717 ) ;
717718 } ) ,
718719 ) ;
720+
721+ it . effect . each ( [ "fork" , "delete" ] as const ) (
722+ "preserves page ownership when %s commits before a concurrent fork or deletion" ,
723+ ( first ) =>
724+ Effect . gen ( function * ( ) {
725+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
726+ const eventSink = yield * EventSink . EventSinkV2 ;
727+ const executor = yield * ThreadCommandExecutor . ThreadCommandExecutor ;
728+ const now = yield * DateTime . now ;
729+ const sourceThreadId = ThreadId . make ( `fork-delete-source:${ first } ` ) ;
730+ const targetThreadId = ThreadId . make ( `fork-delete-target:${ first } ` ) ;
731+ const runId = RunId . make ( `fork-delete-run:${ first } ` ) ;
732+ const forkCommandId = CommandId . make ( `fork-delete-fork:${ first } ` ) ;
733+ const deleteCommandId = CommandId . make ( `fork-delete-delete:${ first } ` ) ;
734+ const attachmentId = createAttachmentId ( sourceThreadId , "html" ) ! ;
735+ yield * orchestrator . dispatch ( {
736+ type : "thread.create" ,
737+ commandId : CommandId . make ( `fork-delete-create:${ first } ` ) ,
738+ threadId : sourceThreadId ,
739+ projectId : ProjectId . make ( `fork-delete-project:${ first } ` ) ,
740+ title : "Page source" ,
741+ modelSelection,
742+ runtimeMode : "full-access" ,
743+ interactionMode : "default" ,
744+ branch : null ,
745+ worktreePath : null ,
746+ createdBy : "user" ,
747+ creationSource : "web" ,
748+ } ) ;
749+ yield * eventSink . write ( {
750+ events : [
751+ {
752+ id : EventId . make ( `fork-delete-run:${ first } ` ) ,
753+ type : "run.created" ,
754+ threadId : sourceThreadId ,
755+ runId,
756+ occurredAt : now ,
757+ payload : {
758+ id : runId ,
759+ threadId : sourceThreadId ,
760+ ordinal : 1 ,
761+ providerInstanceId : modelSelection . instanceId ,
762+ modelSelection,
763+ providerThreadId : null ,
764+ userMessageId : MessageId . make ( `fork-delete-message:${ first } ` ) ,
765+ rootNodeId : null ,
766+ activeAttemptId : null ,
767+ status : "completed" ,
768+ requestedAt : now ,
769+ startedAt : now ,
770+ completedAt : now ,
771+ checkpointId : null ,
772+ contextHandoffId : null ,
773+ } ,
774+ } ,
775+ {
776+ id : EventId . make ( `fork-delete-page:${ first } ` ) ,
777+ type : "turn-item.updated" ,
778+ threadId : sourceThreadId ,
779+ runId,
780+ occurredAt : now ,
781+ payload : {
782+ id : TurnItemId . make ( `fork-delete-page:${ first } ` ) ,
783+ threadId : sourceThreadId ,
784+ runId,
785+ nodeId : null ,
786+ providerThreadId : null ,
787+ providerTurnId : null ,
788+ nativeItemRef : null ,
789+ parentItemId : null ,
790+ ordinal : 1 ,
791+ status : "completed" ,
792+ title : "Shared page" ,
793+ startedAt : now ,
794+ completedAt : now ,
795+ updatedAt : now ,
796+ type : "dynamic_tool" ,
797+ toolName : "mcp__t3-code__html_render" ,
798+ input : { } ,
799+ output : { htmlRender : { attachmentId, title : "Shared page" , height : 300 } } ,
800+ } ,
801+ } ,
802+ ] ,
803+ } ) ;
804+ const firstReady = yield * Deferred . make < void > ( ) ;
805+ const releaseFirst = yield * Deferred . make < void > ( ) ;
806+ const secondQueued = yield * Deferred . make < void > ( ) ;
807+ const cleanupIds : Array < string > = [ ] ;
808+ const commitCommand = eventSink . commitCommand ;
809+ const commitSpy = vi . spyOn ( eventSink , "commitCommand" ) . mockImplementation ( ( input ) => {
810+ if ( input . commandId === deleteCommandId ) {
811+ for ( const effect of input . effects ?? [ ] ) {
812+ if ( effect . request . type === "attachment.cleanup" )
813+ cleanupIds . push ( ...effect . request . attachmentIds ) ;
814+ }
815+ }
816+ return input . commandId === ( first === "fork" ? forkCommandId : deleteCommandId )
817+ ? Deferred . succeed ( firstReady , undefined ) . pipe (
818+ Effect . andThen ( Deferred . await ( releaseFirst ) ) ,
819+ Effect . andThen ( commitCommand ( input ) ) ,
820+ )
821+ : commitCommand ( input ) ;
822+ } ) ;
823+ const withDeletionLock = executor . withDeletionLock ;
824+ let lockRequests = 0 ;
825+ const observeLock : ThreadCommandExecutor . ThreadCommandExecutor [ "Service" ] [ "withDeletionLock" ] =
826+ ( effect ) =>
827+ ++ lockRequests === 2
828+ ? Deferred . succeed ( secondQueued , undefined ) . pipe (
829+ Effect . andThen ( withDeletionLock ( effect ) ) ,
830+ )
831+ : withDeletionLock ( effect ) ;
832+ const lockSpy = vi . spyOn ( executor , "withDeletionLock" ) . mockImplementation ( observeLock ) ;
833+ yield * Effect . gen ( function * ( ) {
834+ const fork = orchestrator
835+ . dispatch ( {
836+ type : "thread.fork" ,
837+ commandId : forkCommandId ,
838+ sourceThreadId,
839+ targetThreadId,
840+ sourcePoint : { type : "run" , runId } ,
841+ createdBy : "user" ,
842+ creationSource : "web" ,
843+ } )
844+ . pipe ( Effect . result ) ;
845+ const deletion = orchestrator
846+ . dispatch ( {
847+ type : "thread.delete" ,
848+ commandId : deleteCommandId ,
849+ threadId : sourceThreadId ,
850+ } )
851+ . pipe ( Effect . result ) ;
852+ const firstFiber = yield * ( first === "fork" ? fork : deletion ) . pipe (
853+ Effect . forkChild ( { startImmediately : true } ) ,
854+ ) ;
855+ yield * Effect . raceFirst (
856+ Deferred . await ( firstReady ) ,
857+ Fiber . join ( firstFiber ) . pipe ( Effect . andThen ( Effect . die ( "Missing commit barrier" ) ) ) ,
858+ ) ;
859+ const secondFiber = yield * ( first === "fork" ? deletion : fork ) . pipe (
860+ Effect . forkChild ( { startImmediately : true } ) ,
861+ ) ;
862+ yield * Effect . raceFirst ( Deferred . await ( secondQueued ) , Fiber . await ( secondFiber ) ) ;
863+ yield * Deferred . succeed ( releaseFirst , undefined ) ;
864+ const firstResult = yield * Fiber . join ( firstFiber ) ;
865+ const secondResult = yield * Fiber . join ( secondFiber ) ;
866+ assert . equal ( firstResult . _tag , "Success" ) ;
867+ if ( first === "fork" ) {
868+ assert . equal ( secondResult . _tag , "Success" ) ;
869+ assert . deepEqual ( cleanupIds , [ ] ) ;
870+ const forkProjection = yield * orchestrator . getThreadProjection ( targetThreadId ) ;
871+ assert . isNull ( forkProjection . thread . deletedAt ) ;
872+ assert . isTrue (
873+ forkProjection . visibleTurnItems . some (
874+ ( row ) => row . item . id === TurnItemId . make ( `fork-delete-page:${ first } ` ) ,
875+ ) ,
876+ ) ;
877+ } else {
878+ assert . equal ( secondResult . _tag , "Failure" ) ;
879+ assert . deepEqual ( cleanupIds , [ attachmentId ] ) ;
880+ const target = yield * orchestrator
881+ . getThreadProjection ( targetThreadId )
882+ . pipe ( Effect . result ) ;
883+ assert . equal ( target . _tag , "Failure" ) ;
884+ }
885+ } ) . pipe (
886+ Effect . ensuring (
887+ Effect . sync ( ( ) => {
888+ commitSpy . mockRestore ( ) ;
889+ lockSpy . mockRestore ( ) ;
890+ } ) ,
891+ ) ,
892+ ) ;
893+ } ) ,
894+ ) ;
719895} ) ;
720896
721897const layerSharedApplicationDataPlaneTest = Layer . mergeAll (
0 commit comments