@@ -29,6 +29,7 @@ import * as Queue from "effect/Queue";
2929import * as Ref from "effect/Ref" ;
3030import * as Schedule from "effect/Schedule" ;
3131import * as Stream from "effect/Stream" ;
32+ import * as Tracer from "effect/Tracer" ;
3233import * as SubscriptionRef from "effect/SubscriptionRef" ;
3334import type * as SqlClient from "effect/sql/SqlClient" ;
3435import { McpSchema , McpServer } from "effect/ai" ;
@@ -845,15 +846,155 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th
845846 ) ;
846847 expect ( Option . isSome ( offered ) ) . toBe ( true ) ;
847848
848- const status = yield * a . call ( "task_status" , { taskId : delegated . taskId } ) ;
849+ const messageReads : Array < string > = [ ] ;
850+ const tracer = Tracer . make ( {
851+ span ( options ) {
852+ const span = new Tracer . NativeSpan ( options ) ;
853+ const end = span . end . bind ( span ) ;
854+ span . end = ( endTime , exit ) => {
855+ end ( endTime , exit ) ;
856+ const query = span . attributes . get ( "db.query.text" ) ;
857+ if (
858+ typeof query === "string" &&
859+ / S E L E C T [ \s \S ] * F R O M o r c h e s t r a t i o n _ v 2 _ p r o j e c t i o n _ m e s s a g e s / i. test ( query )
860+ ) {
861+ messageReads . push ( query ) ;
862+ }
863+ } ;
864+ return span ;
865+ } ,
866+ } ) ;
867+ const status = yield * a
868+ . call ( "task_status" , { taskId : delegated . taskId } )
869+ . pipe ( Effect . withTracer ( tracer ) ) ;
849870 expect ( status . structuredContent ) . toMatchObject ( {
850871 status : "completed" ,
851872 summary : "All 412 tests pass on the box." ,
852873 } ) ;
874+ // Delivery validation still reads messages; choosing the observer does not need another read.
875+ expect ( messageReads ) . toHaveLength ( 2 ) ;
876+ expect ( messageReads . filter ( ( query ) => ! / \b m e s s a g e _ i d \s + I N \b / i. test ( query ) ) ) . toHaveLength ( 1 ) ;
877+ const acknowledged = yield * a . projections . getThreadRecords ( parentThreadId , [ "subagents" ] , {
878+ subagentIds : [ delegated . taskId ] ,
879+ } ) ;
880+ expect ( acknowledged . subagents [ 0 ] ?. completionDelivery ) . toEqual ( {
881+ state : "acknowledged" ,
882+ observedByRunId : parentRunId ,
883+ } ) ;
853884 } ) ,
854885 ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
855886) ;
856887
888+ it . effect . each ( [ "no active parent run" , "a newer run owned by another provider" ] as const ) (
889+ "preserves remote observation guards with %s" ,
890+ ( observer ) =>
891+ Effect . scoped (
892+ Effect . gen ( function * ( ) {
893+ const thread = yield * makeBoxThread ;
894+ const b = yield * serveBox ( thread ) ;
895+ const offers = yield * SubscriptionRef . make <
896+ ReadonlyArray < ProviderContinuationRequests . ProviderContinuationRequest >
897+ > ( [ ] ) ;
898+ const database = yield * SqlitePersistence . layerMemory . pipe ( Layer . build ) ;
899+ const a = yield * makeLaptop ( database , offers ) ;
900+ yield * linkTo ( a . links , b , "full-access" ) ;
901+ yield * seedParent ( a ) ;
902+ const delegated = yield * delegateToBox ( a ) ;
903+
904+ yield * Ref . set ( thread . reply , "Remote result remains available." ) ;
905+ yield * Ref . set ( thread . status , "completed" ) ;
906+ yield * Deferred . succeed ( thread . finished , undefined ) ;
907+ yield * taskResult ( a , delegated . taskId ) ;
908+ yield * SubscriptionRef . changes ( offers ) . pipe (
909+ Stream . filter ( ( seen ) => seen . some ( ( offer ) => offer . threadId === parentThreadId ) ) ,
910+ Stream . runHead ,
911+ ) ;
912+
913+ const parent = yield * a . projections . getThreadRecords (
914+ parentThreadId ,
915+ [ "runs" , "subagents" ] ,
916+ {
917+ subagentIds : [ delegated . taskId ] ,
918+ } ,
919+ ) ;
920+ expect ( parent . subagents [ 0 ] ?. completionDelivery ?. state ) . not . toBe ( "acknowledged" ) ;
921+ const parentRun = parent . runs . find ( ( run ) => run . id === parentRunId ) ;
922+ if ( parentRun === undefined ) return yield * Effect . die ( "The parent run was not seeded." ) ;
923+ const now = yield * DateTime . now ;
924+ const otherInstanceId = ProviderInstanceId . make ( "codex-other" ) ;
925+ const observingRun : OrchestrationV2Run =
926+ observer === "no active parent run"
927+ ? { ...parentRun , status : "completed" , completedAt : now }
928+ : {
929+ id : RunId . make ( "run:laptop-other-provider" ) ,
930+ threadId : parentThreadId ,
931+ ordinal : parentRun . ordinal + 1 ,
932+ providerInstanceId : otherInstanceId ,
933+ modelSelection : { instanceId : otherInstanceId , model : "gpt-5.4" } ,
934+ providerThreadId : null ,
935+ userMessageId : MessageId . make ( "message:laptop-other-provider" ) ,
936+ rootNodeId : null ,
937+ activeAttemptId : null ,
938+ status : "preparing" ,
939+ requestedAt : now ,
940+ startedAt : null ,
941+ completedAt : null ,
942+ checkpointId : null ,
943+ contextHandoffId : null ,
944+ } ;
945+ yield * a . sink . write ( {
946+ commandId : CommandId . make ( "command:change-remote-result-observer" ) ,
947+ events : [
948+ {
949+ id : EventId . make ( "event:change-remote-result-observer" ) ,
950+ type : "run.updated" ,
951+ threadId : parentThreadId ,
952+ runId : observingRun . id ,
953+ providerInstanceId : observingRun . providerInstanceId ,
954+ occurredAt : now ,
955+ payload : observingRun ,
956+ } ,
957+ ] ,
958+ } ) ;
959+
960+ const status = yield * a . call ( "task_status" , { taskId : delegated . taskId } ) ;
961+ if ( observer === "no active parent run" ) {
962+ expect ( status . isError ) . toBe ( true ) ;
963+ expect ( status . content ) . toEqual ( [
964+ {
965+ type : "text" ,
966+ text : JSON . stringify ( {
967+ _tag : "OrchestratorMcpFailure" ,
968+ code : "parent_not_active" ,
969+ message : "The calling provider no longer owns an active thread run." ,
970+ } ) ,
971+ } ,
972+ ] ) ;
973+ const unacknowledged = yield * a . projections . getThreadRecords (
974+ parentThreadId ,
975+ [ "subagents" ] ,
976+ { subagentIds : [ delegated . taskId ] } ,
977+ ) ;
978+ expect ( unacknowledged . subagents [ 0 ] ?. result ) . toBe ( "Remote result remains available." ) ;
979+ expect ( unacknowledged . subagents [ 0 ] ?. completionDelivery ?. state ) . not . toBe ( "acknowledged" ) ;
980+ return ;
981+ }
982+ expect ( status . isError , JSON . stringify ( status . content ) ) . not . toBe ( true ) ;
983+ expect ( status . structuredContent ) . toMatchObject ( {
984+ status : "completed" ,
985+ summary : "Remote result remains available." ,
986+ } ) ;
987+ const acknowledged = yield * a . projections . getThreadRecords ( parentThreadId , [ "subagents" ] , {
988+ subagentIds : [ delegated . taskId ] ,
989+ } ) ;
990+ expect ( acknowledged . subagents [ 0 ] ?. completionDelivery ) . toEqual ( {
991+ state : "acknowledged" ,
992+ observedByRunId : null ,
993+ } ) ;
994+ } ) ,
995+ ) . pipe ( Effect . provide ( NodeHttpServer . layerTest ) ) ,
996+ ) ;
997+
857998it . effect (
858999 "with the beta off here, a task already running there still ends, and no new one starts" ,
8591000 ( ) =>
0 commit comments