@@ -4,6 +4,7 @@ import { assert, it } from "@effect/vitest";
44import {
55 CommandId ,
66 ProviderInstanceId ,
7+ RunId ,
78 ThreadId ,
89 type OrchestrationV2Command ,
910 type OrchestrationV2StoredEvent ,
@@ -130,30 +131,35 @@ const fixture = Effect.gen(function* () {
130131 const dispatched = yield * Ref . make < ReadonlyArray < OrchestrationV2Command > > ( [ ] ) ;
131132 const threads = ThreadManagementService . of ( {
132133 getThreadRecords : ( id : ThreadId ) =>
133- Ref . get ( archived ) . pipe (
134- Effect . flatMap ( ( ids ) =>
135- [ owner , peer , crewMember , foreign ] . includes ( id )
136- ? Ref . get ( ownerPlan ) . pipe (
137- Effect . map ( ( plan ) => {
138- const result = projection ( id , ids . has ( id ) ) ;
139- return {
140- ...result ,
141- thread : {
142- ...result . thread ,
143- interactionMode :
144- plan && id === owner ? ( "plan" as const ) : ( "default" as const ) ,
145- } ,
146- } ;
147- } ) ,
148- )
149- : Effect . fail (
150- new OrchestratorProjectionError ( {
151- threadId : id ,
152- cause : new ProjectionStoreThreadNotFoundError ( { threadId : id } ) ,
153- } ) ,
154- ) ,
155- ) ,
156- ) ,
134+ Effect . gen ( function * ( ) {
135+ if ( ! [ owner , peer , crewMember , foreign ] . includes ( id ) )
136+ return yield * new OrchestratorProjectionError ( {
137+ threadId : id ,
138+ cause : new ProjectionStoreThreadNotFoundError ( { threadId : id } ) ,
139+ } ) ;
140+ const result = projection ( id , ( yield * Ref . get ( archived ) ) . has ( id ) ) ;
141+ const plan = yield * Ref . get ( ownerPlan ) ;
142+ const notice = ( yield * Ref . get ( dispatched ) ) . findLast (
143+ ( item ) => item . type === "message.dispatch" && item . threadId === id ,
144+ ) ;
145+ return {
146+ ...result ,
147+ runs :
148+ notice ?. type === "message.dispatch"
149+ ? [
150+ {
151+ id : RunId . make ( "run:" + notice . messageId ) ,
152+ userMessageId : notice . messageId ,
153+ status : "running" ,
154+ } ,
155+ ]
156+ : [ ] ,
157+ thread : {
158+ ...result . thread ,
159+ interactionMode : plan && id === owner ? ( "plan" as const ) : ( "default" as const ) ,
160+ } ,
161+ } ;
162+ } ) ,
157163 dispatch : ( command : OrchestrationV2Command ) =>
158164 Effect . gen ( function * ( ) {
159165 if ( yield * Ref . get ( fault ) )
@@ -789,12 +795,31 @@ it.effect(
789795 }
790796 }
791797 assert . equal (
792- ( yield * Ref . get ( dispatched ) ) . filter ( ( item ) => item . type === "thread.stop " ) . length ,
798+ ( yield * Ref . get ( dispatched ) ) . filter ( ( item ) => item . type === "run.interrupt " ) . length ,
793799 4 ,
794800 ) ;
795801 } ) . pipe ( Effect . scoped , Effect . provide ( TestLayer ) ) ,
796802) ;
797803
804+ it . effect ( "boot recovers an archive followed by unarchive before the notifier saw it" , ( ) =>
805+ Effect . gen ( function * ( ) {
806+ const { start, relay, store, sql } = yield * fixture ;
807+ const run = yield * start ( "original" , peerId ) ;
808+ for ( const [ index , type ] of [ "thread.archived" , "thread.unarchived" ] . entries ( ) ) {
809+ yield * sql `INSERT INTO orchestration_events
810+ (event_id, aggregate_kind, stream_id, stream_version, event_type, occurred_at,
811+ actor_kind, payload_json, metadata_json, application_event_version)
812+ VALUES (${ type } , 'thread', ${ owner } , ${ index + 1 } , ${ type } , ${ run . createdAt } ,
813+ 'system', '{}', '{}', 2)` ;
814+ }
815+ yield * relay . reconcileOwners ( ) ;
816+ assert . equal ( ( yield * store . runById ( run . runId ) ) ?. cancelReason , "owner_archived" ) ;
817+ const replacement = yield * start ( "replacement" , peerId ) ;
818+ yield * relay . reconcileOwners ( ) ;
819+ assert . equal ( ( yield * store . runById ( replacement . runId ) ) ?. status , "active" ) ;
820+ } ) . pipe ( Effect . scoped , Effect . provide ( TestLayer ) ) ,
821+ ) ;
822+
798823it . effect ( "a delayed archive reconciles its owner without cancelling a replacement run" , ( ) =>
799824 Effect . gen ( function * ( ) {
800825 const { start, store, relay, root, sql, dispatched } = yield * fixture ;
@@ -808,7 +833,7 @@ it.effect("a delayed archive reconciles its owner without cancelling a replaceme
808833 yield * sql `UPDATE orchestration_v2_projection_threads SET archived_at = ${ at } WHERE thread_id = ${ owner } ` ;
809834 assert . equal ( ( yield * store . current ( owner , original . runId ) ) . status , "cancelled" ) ;
810835 yield * sql `UPDATE orchestration_v2_projection_threads SET archived_at = NULL WHERE thread_id = ${ owner } ` ;
811- const replacement = yield * start ( "replacement" ) ;
836+ const replacement = yield * start ( "replacement" , peerId ) ;
812837 const unrelated = yield * store . start ( foreign , root , "review" , "unrelated" ) ;
813838 yield * sql `UPDATE orchestration_v2_projection_threads SET archived_at = ${ at } WHERE thread_id = ${ foreign } ` ;
814839
@@ -822,8 +847,10 @@ it.effect("a delayed archive reconciles its owner without cancelling a replaceme
822847 assert . equal ( ( yield * store . runById ( unrelated . runId ) ) ?. status , "active" ) ;
823848 assert . equal ( ( yield * store . runById ( original . runId ) ) ?. holderStop ?. status , "already_idle" ) ;
824849 assert . equal (
825- ( yield * Ref . get ( dispatched ) ) . filter ( ( item ) => item . type === "thread.stop" ) . length ,
826- 1 ,
850+ ( yield * Ref . get ( dispatched ) ) . filter (
851+ ( item ) => item . type === "thread.stop" || item . type === "run.interrupt" ,
852+ ) . length ,
853+ 0 ,
827854 ) ;
828855 } ) . pipe ( Effect . scoped , Effect . provide ( TestLayer ) ) ,
829856) ;
0 commit comments