@@ -16,9 +16,84 @@ import { ThreadManagementService } from "../orchestration-v2/ThreadManagementSer
1616import { ProviderRegistry } from "../provider/Services/ProviderRegistry.ts" ;
1717import { ScheduledTaskService } from "../scheduledTasks/ScheduledTaskService.ts" ;
1818import type { McpInvocationScope } from "./McpInvocationContext.ts" ;
19- import { layer , OrchestratorMcpService } from "./OrchestratorMcpService.ts" ;
19+ import * as OrchestratorMcpService from "./OrchestratorMcpService.ts" ;
2020
2121describe ( "OrchestratorMcpService" , ( ) => {
22+ it . effect ( "retries terminal acknowledgement with a fresh command id" , ( ) =>
23+ Effect . gen ( function * ( ) {
24+ const parentThreadId = ThreadId . make ( "thread:mcp-ack-parent" ) ;
25+ const childThreadId = ThreadId . make ( "thread:mcp-ack-child" ) ;
26+ const childRunId = RunId . make ( "run:mcp-ack-child" ) ;
27+ const taskId = NodeId . make ( "node:mcp-ack-task" ) ;
28+ const acknowledgementCommandIds = yield * Ref . make < ReadonlyArray < string > > ( [ ] ) ;
29+ const acknowledgementAttempts = yield * Ref . make ( 0 ) ;
30+ const parentProjection = {
31+ thread : { id : parentThreadId } ,
32+ runs : [ ] ,
33+ contextTransfers : [ ] ,
34+ subagents : [
35+ {
36+ id : taskId ,
37+ threadId : parentThreadId ,
38+ origin : "app_owned" ,
39+ childThreadId,
40+ driver : "codex" ,
41+ model : "gpt-5.6-terra" ,
42+ result : "terminal result" ,
43+ completionDelivery : { state : "pending" } ,
44+ } ,
45+ ] ,
46+ } as unknown as OrchestrationV2ThreadProjection ;
47+ const childProjection = {
48+ thread : { id : childThreadId } ,
49+ runs : [ { id : childRunId , ordinal : 1 , status : "completed" } ] ,
50+ contextTransfers : [ ] ,
51+ } as unknown as OrchestrationV2ThreadProjection ;
52+ const dependencies = Layer . mergeAll (
53+ NodeServices . layer ,
54+ Layer . mock ( ThreadManagementService ) ( {
55+ getThreadProjection : ( threadId ) =>
56+ Effect . succeed ( threadId === parentThreadId ? parentProjection : childProjection ) ,
57+ dispatch : ( command ) =>
58+ Ref . update ( acknowledgementCommandIds , ( commandIds ) => [
59+ ...commandIds ,
60+ String ( command . commandId ) ,
61+ ] ) . pipe (
62+ Effect . andThen ( Ref . updateAndGet ( acknowledgementAttempts , ( count ) => count + 1 ) ) ,
63+ Effect . flatMap ( ( attempt ) =>
64+ attempt === 1
65+ ? Effect . fail ( new Error ( "simulated acknowledgement failure" ) as never )
66+ : Effect . succeed ( { } as never ) ,
67+ ) ,
68+ ) ,
69+ } ) ,
70+ Layer . mock ( ProviderRegistry ) ( { getProviders : Effect . succeed ( [ ] ) } ) ,
71+ Layer . mock ( ScheduledTaskService ) ( { } ) ,
72+ ) ;
73+ const scope : McpInvocationScope = {
74+ environmentId : EnvironmentId . make ( "environment:mcp-ack" ) ,
75+ threadId : parentThreadId ,
76+ providerSessionId : "provider-session:mcp-ack" ,
77+ providerInstanceId : ProviderInstanceId . make ( "codex" ) ,
78+ capabilities : new Set ( [ "orchestration" ] ) ,
79+ issuedAt : 1 ,
80+ } ;
81+
82+ yield * Effect . gen ( function * ( ) {
83+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
84+ const error = yield * service . taskStatus ( scope , taskId ) . pipe ( Effect . flip ) ;
85+ assert . equal ( error . code , "orchestration_error" ) ;
86+
87+ const result = yield * service . taskStatus ( scope , taskId ) ;
88+ assert . equal ( result . status , "completed" ) ;
89+ assert . equal ( result . summary , "terminal result" ) ;
90+ const commandIds = yield * Ref . get ( acknowledgementCommandIds ) ;
91+ assert . equal ( commandIds . length , 2 ) ;
92+ assert . notEqual ( commandIds [ 0 ] , commandIds [ 1 ] ) ;
93+ } ) . pipe ( Effect . provide ( OrchestratorMcpService . layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
94+ } ) ,
95+ ) ;
96+
2297 it . effect ( "does not dispose delivery when a nonterminal task has no active child run" , ( ) =>
2398 Effect . gen ( function * ( ) {
2499 const parentThreadId = ThreadId . make ( "thread:mcp-cancel-parent" ) ;
@@ -70,13 +145,13 @@ describe("OrchestratorMcpService", () => {
70145 } ;
71146
72147 yield * Effect . gen ( function * ( ) {
73- const service = yield * OrchestratorMcpService ;
148+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
74149 const error = yield * service
75150 . cancelTask ( scope , { taskId, clientRequestId : "cancel-unstarted-task" } )
76151 . pipe ( Effect . flip ) ;
77152 assert . equal ( error . code , "task_not_cancellable" ) ;
78153 assert . deepEqual ( yield * Ref . get ( dispatched ) , [ ] ) ;
79- } ) . pipe ( Effect . provide ( layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
154+ } ) . pipe ( Effect . provide ( OrchestratorMcpService . layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
80155 } ) ,
81156 ) ;
82157
@@ -132,7 +207,7 @@ describe("OrchestratorMcpService", () => {
132207 } ;
133208
134209 yield * Effect . gen ( function * ( ) {
135- const service = yield * OrchestratorMcpService ;
210+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
136211 const error = yield * service
137212 . cancelTask ( scope , { taskId, clientRequestId : "cancel-failed-task" } )
138213 . pipe ( Effect . flip ) ;
@@ -141,7 +216,7 @@ describe("OrchestratorMcpService", () => {
141216 ( yield * Ref . get ( dispatched ) ) . map ( ( command ) => ( command as { type : string } ) . type ) ,
142217 [ "run.interrupt" ] ,
143218 ) ;
144- } ) . pipe ( Effect . provide ( layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
219+ } ) . pipe ( Effect . provide ( OrchestratorMcpService . layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
145220 } ) ,
146221 ) ;
147222
@@ -201,7 +276,7 @@ describe("OrchestratorMcpService", () => {
201276 } ;
202277
203278 yield * Effect . gen ( function * ( ) {
204- const service = yield * OrchestratorMcpService ;
279+ const service = yield * OrchestratorMcpService . OrchestratorMcpService ;
205280 const result = yield * service . cancelTask ( scope , {
206281 taskId,
207282 clientRequestId : "cancel-dispose-failed-task" ,
@@ -211,7 +286,7 @@ describe("OrchestratorMcpService", () => {
211286 ( yield * Ref . get ( dispatched ) ) . map ( ( command ) => ( command as { type : string } ) . type ) ,
212287 [ "run.interrupt" , "delegated_task.completion-delivery.dispose" ] ,
213288 ) ;
214- } ) . pipe ( Effect . provide ( layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
289+ } ) . pipe ( Effect . provide ( OrchestratorMcpService . layer . pipe ( Layer . provide ( dependencies ) ) ) ) ;
215290 } ) ,
216291 ) ;
217292} ) ;
0 commit comments