Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OrchestratorMcpService.test.ts

787 lines30.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import {
4 EnvironmentId,
5 NodeId,
6 ProjectId,
7 ProviderDriverKind,
8 ProviderInstanceId,
9 RunId,
10 ThreadId,
11 type OrchestrationV2ThreadProjection,
12 type ServerProvider,
13} from "@t3tools/contracts";
14import * as Effect from "effect/Effect";
15import * as Layer from "effect/Layer";
16import * as Ref from "effect/Ref";
17​
18import type { ProviderAdapterV2Shape } from "../orchestration-v2/ProviderAdapter.ts";
19import {
20 ProviderAdapterRegistryLookupError,
21 ProviderAdapterRegistryV2,
22} from "../orchestration-v2/ProviderAdapterRegistry.ts";
23import { ThreadManagementService } from "../orchestration-v2/ThreadManagementService.ts";
24import { ProviderRegistry } from "../provider/Services/ProviderRegistry.ts";
25import { buildUnavailableProviderSnapshot } from "../provider/unavailableProviderSnapshot.ts";
26import { ScheduledTaskService } from "../scheduledTasks/ScheduledTaskService.ts";
27import type { McpInvocationScope } from "./McpInvocationContext.ts";
28import * as OrchestratorMcpService from "./OrchestratorMcpService.ts";
29​
30describe("OrchestratorMcpService", () => {
31 it.effect("retries terminal acknowledgement with a fresh command id", () =>
32 Effect.gen(function* () {
33 const parentThreadId = ThreadId.make("thread:mcp-ack-parent");
34 const childThreadId = ThreadId.make("thread:mcp-ack-child");
35 const childRunId = RunId.make("run:mcp-ack-child");
36 const taskId = NodeId.make("node:mcp-ack-task");
37 const acknowledgementCommandIds = yield* Ref.make<ReadonlyArray<string>>([]);
38 const acknowledgementAttempts = yield* Ref.make(0);
39 const parentProjection = {
40 thread: { id: parentThreadId },
41 runs: [],
42 contextTransfers: [],
43 subagents: [
44 {
45 id: taskId,
46 threadId: parentThreadId,
47 origin: "app_owned",
48 childThreadId,
49 driver: "codex",
50 model: "gpt-5.6-terra",
51 result: "terminal result",
52 completionDelivery: { state: "pending" },
53 },
54 ],
55 } as unknown as OrchestrationV2ThreadProjection;
56 const childProjection = {
57 thread: { id: childThreadId },
58 runs: [{ id: childRunId, ordinal: 1, status: "completed" }],
59 contextTransfers: [],
60 messages: [],
61 subagents: [],
62 providerThreads: [],
63 } as unknown as OrchestrationV2ThreadProjection;
64 let hasNestedWork = true;
65 const dependencies = Layer.mergeAll(
66 NodeServices.layer,
67 Layer.mock(ThreadManagementService)({
68 getThreadProjection: (threadId) =>
69 Effect.succeed(
70 threadId === parentThreadId
71 ? hasNestedWork
72 ? {
73 ...parentProjection,
74 subagents: parentProjection.subagents.map((task) => ({
75 ...task,
76 result: null,
77 status: "running" as const,
78 })),
79 }
80 : parentProjection
81 : hasNestedWork
82 ? {
83 ...childProjection,
84 subagents: [
85 { ...parentProjection.subagents[0]!, status: "running" as const },
86 ],
87 }
88 : childProjection,
89 ),
90 dispatch: (command) =>
91 Ref.update(acknowledgementCommandIds, (commandIds) => [
92 ...commandIds,
93 String(command.commandId),
94 ]).pipe(
95 Effect.andThen(Ref.updateAndGet(acknowledgementAttempts, (count) => count + 1)),
96 Effect.flatMap((attempt) =>
97 attempt === 1
98 ? Effect.fail(new Error("simulated acknowledgement failure") as never)
99 : Effect.succeed({} as never),
100 ),
101 ),
102 }),
103 Layer.mock(ProviderRegistry)({ getProviders: Effect.succeed([]) }),
104 Layer.mock(ProviderAdapterRegistryV2)({ list: () => Effect.succeed([]) }),
105 Layer.mock(ScheduledTaskService)({}),
106 );
107 const scope: McpInvocationScope = {
108 environmentId: EnvironmentId.make("environment:mcp-ack"),
109 threadId: parentThreadId,
110 providerSessionId: "provider-session:mcp-ack",
111 providerInstanceId: ProviderInstanceId.make("codex"),
112 capabilities: new Set(["orchestration"]),
113 issuedAt: 1,
114 };
115​
116 yield* Effect.gen(function* () {
117 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
118 const pending = yield* service.taskStatus(scope, taskId);
119 assert.equal(pending.status, "running");
120 assert.equal(pending.workState, "waiting_for_children");
121 assert.isNull(pending.summary);
122 assert.equal(yield* Ref.get(acknowledgementAttempts), 0);
123 hasNestedWork = false;
124 const error = yield* service.taskStatus(scope, taskId).pipe(Effect.flip);
125 assert.equal(error.code, "orchestration_error");
126​
127 const result = yield* service.taskStatus(scope, taskId);
128 assert.equal(result.status, "completed");
129 assert.equal(result.summary, "terminal result");
130 const commandIds = yield* Ref.get(acknowledgementCommandIds);
131 assert.equal(commandIds.length, 2);
132 assert.notEqual(commandIds[0], commandIds[1]);
133 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
134 }),
135 );
136​
137 it.effect("does not dispose delivery when a nonterminal task has no active child run", () =>
138 Effect.gen(function* () {
139 const parentThreadId = ThreadId.make("thread:mcp-cancel-parent");
140 const childThreadId = ThreadId.make("thread:mcp-cancel-child");
141 const taskId = NodeId.make("node:mcp-cancel-task");
142 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
143 const parentProjection = {
144 thread: { id: parentThreadId },
145 runs: [],
146 contextTransfers: [],
147 subagents: [
148 {
149 id: taskId,
150 threadId: parentThreadId,
151 origin: "app_owned",
152 childThreadId,
153 driver: "codex",
154 model: "gpt-5.6-terra",
155 result: null,
156 completionDelivery: { state: "pending" },
157 },
158 ],
159 } as unknown as OrchestrationV2ThreadProjection;
160 const childProjection = {
161 thread: { id: childThreadId },
162 runs: [],
163 contextTransfers: [],
164 messages: [],
165 subagents: [],
166 providerThreads: [],
167 } as unknown as OrchestrationV2ThreadProjection;
168 const dependencies = Layer.mergeAll(
169 NodeServices.layer,
170 Layer.mock(ThreadManagementService)({
171 getThreadProjection: (threadId) =>
172 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
173 dispatch: (command) =>
174 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
175 Effect.as({} as never),
176 ),
177 }),
178 Layer.mock(ProviderRegistry)({ getProviders: Effect.succeed([]) }),
179 Layer.mock(ProviderAdapterRegistryV2)({ list: () => Effect.succeed([]) }),
180 Layer.mock(ScheduledTaskService)({}),
181 );
182 const scope: McpInvocationScope = {
183 environmentId: EnvironmentId.make("environment:mcp-cancel"),
184 threadId: parentThreadId,
185 providerSessionId: "provider-session:mcp-cancel",
186 providerInstanceId: ProviderInstanceId.make("codex"),
187 capabilities: new Set(["orchestration"]),
188 issuedAt: 1,
189 };
190​
191 yield* Effect.gen(function* () {
192 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
193 const error = yield* service
194 .cancelTask(scope, { taskId, clientRequestId: "cancel-unstarted-task" })
195 .pipe(Effect.flip);
196 assert.equal(error.code, "task_not_cancellable");
197 assert.deepEqual(yield* Ref.get(dispatched), []);
198 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
199 }),
200 );
201​
202 it.effect("does not dispose delivery when the child interrupt fails", () =>
203 Effect.gen(function* () {
204 const parentThreadId = ThreadId.make("thread:mcp-cancel-failed-parent");
205 const childThreadId = ThreadId.make("thread:mcp-cancel-failed-child");
206 const childRunId = RunId.make("run:mcp-cancel-failed-child");
207 const taskId = NodeId.make("node:mcp-cancel-failed-task");
208 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
209 const parentProjection = {
210 thread: { id: parentThreadId },
211 runs: [],
212 contextTransfers: [],
213 subagents: [
214 {
215 id: taskId,
216 threadId: parentThreadId,
217 origin: "app_owned",
218 childThreadId,
219 driver: "codex",
220 model: "gpt-5.6-terra",
221 result: null,
222 completionDelivery: { state: "pending" },
223 },
224 ],
225 } as unknown as OrchestrationV2ThreadProjection;
226 const childProjection = {
227 thread: { id: childThreadId },
228 runs: [{ id: childRunId, status: "running" }],
229 contextTransfers: [],
230 messages: [],
231 subagents: [],
232 providerThreads: [],
233 } as unknown as OrchestrationV2ThreadProjection;
234 const dependencies = Layer.mergeAll(
235 NodeServices.layer,
236 Layer.mock(ThreadManagementService)({
237 getThreadProjection: (threadId) =>
238 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
239 dispatch: (command) =>
240 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
241 Effect.andThen(Effect.fail(new Error("simulated interrupt failure") as never)),
242 ),
243 }),
244 Layer.mock(ProviderRegistry)({ getProviders: Effect.succeed([]) }),
245 Layer.mock(ProviderAdapterRegistryV2)({ list: () => Effect.succeed([]) }),
246 Layer.mock(ScheduledTaskService)({}),
247 );
248 const scope: McpInvocationScope = {
249 environmentId: EnvironmentId.make("environment:mcp-cancel-failed"),
250 threadId: parentThreadId,
251 providerSessionId: "provider-session:mcp-cancel-failed",
252 providerInstanceId: ProviderInstanceId.make("codex"),
253 capabilities: new Set(["orchestration"]),
254 issuedAt: 1,
255 };
256​
257 yield* Effect.gen(function* () {
258 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
259 const error = yield* service
260 .cancelTask(scope, { taskId, clientRequestId: "cancel-failed-task" })
261 .pipe(Effect.flip);
262 assert.equal(error.code, "task_not_cancellable");
263 assert.deepEqual(
264 (yield* Ref.get(dispatched)).map((command) => (command as { type: string }).type),
265 ["run.interrupt"],
266 );
267 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
268 }),
269 );
270​
271 it.effect("returns cancel requested when post-interrupt disposal fails", () =>
272 Effect.gen(function* () {
273 const parentThreadId = ThreadId.make("thread:mcp-cancel-dispose-failed-parent");
274 const childThreadId = ThreadId.make("thread:mcp-cancel-dispose-failed-child");
275 const childRunId = RunId.make("run:mcp-cancel-dispose-failed-child");
276 const taskId = NodeId.make("node:mcp-cancel-dispose-failed-task");
277 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
278 const parentProjection = {
279 thread: { id: parentThreadId },
280 runs: [],
281 contextTransfers: [],
282 subagents: [
283 {
284 id: taskId,
285 threadId: parentThreadId,
286 origin: "app_owned",
287 childThreadId,
288 driver: "codex",
289 model: "gpt-5.6-terra",
290 result: null,
291 completionDelivery: { state: "pending" },
292 },
293 ],
294 } as unknown as OrchestrationV2ThreadProjection;
295 const childProjection = {
296 thread: { id: childThreadId },
297 runs: [{ id: childRunId, status: "running" }],
298 contextTransfers: [],
299 messages: [],
300 subagents: [],
301 providerThreads: [],
302 } as unknown as OrchestrationV2ThreadProjection;
303 const dependencies = Layer.mergeAll(
304 NodeServices.layer,
305 Layer.mock(ThreadManagementService)({
306 getThreadProjection: (threadId) =>
307 Effect.succeed(threadId === parentThreadId ? parentProjection : childProjection),
308 dispatch: (command) =>
309 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
310 Effect.andThen(
311 command.type === "delegated_task.completion-delivery.dispose"
312 ? Effect.fail(new Error("simulated disposal failure") as never)
313 : Effect.succeed({} as never),
314 ),
315 ),
316 }),
317 Layer.mock(ProviderRegistry)({ getProviders: Effect.succeed([]) }),
318 Layer.mock(ProviderAdapterRegistryV2)({ list: () => Effect.succeed([]) }),
319 Layer.mock(ScheduledTaskService)({}),
320 );
321 const scope: McpInvocationScope = {
322 environmentId: EnvironmentId.make("environment:mcp-cancel-dispose-failed"),
323 threadId: parentThreadId,
324 providerSessionId: "provider-session:mcp-cancel-dispose-failed",
325 providerInstanceId: ProviderInstanceId.make("codex"),
326 capabilities: new Set(["orchestration"]),
327 issuedAt: 1,
328 };
329​
330 yield* Effect.gen(function* () {
331 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
332 const result = yield* service.cancelTask(scope, {
333 taskId,
334 clientRequestId: "cancel-dispose-failed-task",
335 });
336 assert.equal(result.status, "cancel_requested");
337 assert.deepEqual(
338 (yield* Ref.get(dispatched)).map((command) => (command as { type: string }).type),
339 ["run.interrupt", "delegated_task.completion-delivery.dispose"],
340 );
341 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
342 }),
343 );
344});
345​
346describe("OrchestratorMcpService provider resolution", () => {
347 const parentThreadId = ThreadId.make("thread:mcp-providers-parent");
348 const childThreadId = ThreadId.make("thread:mcp-providers-child");
349 const parentRunId = RunId.make("run:mcp-providers-parent");
350 const parentNodeId = NodeId.make("node:mcp-providers-root");
351 const taskId = NodeId.make("node:mcp-providers-task");
352 const projectId = ProjectId.make("project:mcp-providers");
353 const codexInstanceId = ProviderInstanceId.make("codex");
354 const antigravityInstanceId = ProviderInstanceId.make("antigravity");
355​
356 const scope: McpInvocationScope = {
357 environmentId: EnvironmentId.make("environment:mcp-providers"),
358 threadId: parentThreadId,
359 providerSessionId: "provider-session:mcp-providers",
360 providerInstanceId: codexInstanceId,
361 capabilities: new Set(["orchestration"]),
362 issuedAt: 1,
363 };
364​
365 const providerSnapshot = (input: {
366 readonly instanceId: ProviderInstanceId;
367 readonly driver: ProviderDriverKind;
368 readonly model?: string;
369 readonly enabled?: boolean;
370 }): ServerProvider => ({
371 instanceId: input.instanceId,
372 driver: input.driver,
373 enabled: input.enabled ?? true,
374 installed: true,
375 version: "test",
376 status: "ready",
377 auth: { status: "authenticated" },
378 checkedAt: "2026-09-13T00:00:00.000Z",
379 models:
380 input.model === undefined
381 ? []
382 : [{ slug: input.model, name: input.model, isCustom: false, capabilities: null }],
383 slashCommands: [],
384 skills: [],
385 });
386​
387 const adapterRegistryLayer = (instanceIds: ReadonlyArray<ProviderInstanceId>) =>
388 Layer.succeed(
389 ProviderAdapterRegistryV2,
390 ProviderAdapterRegistryV2.of({
391 list: () => Effect.succeed(instanceIds),
392 get: (instanceId) =>
393 instanceIds.includes(instanceId)
394 ? Effect.succeed({ instanceId } as unknown as ProviderAdapterV2Shape)
395 : Effect.fail(new ProviderAdapterRegistryLookupError({ instanceId })),
396 }),
397 );
398​
399 const parentProjection = (subagents: ReadonlyArray<unknown>): OrchestrationV2ThreadProjection =>
400 ({
401 thread: {
402 id: parentThreadId,
403 projectId,
404 title: "MCP parent",
405 createdBy: "user",
406 creationSource: "web",
407 modelSelection: { instanceId: codexInstanceId, model: "gpt-5.4" },
408 runtimeMode: "full-access",
409 interactionMode: "default",
410 },
411 runs: [
412 {
413 id: parentRunId,
414 ordinal: 1,
415 status: "running",
416 rootNodeId: parentNodeId,
417 providerInstanceId: codexInstanceId,
418 modelSelection: { instanceId: codexInstanceId, model: "gpt-5.4" },
419 },
420 ],
421 contextTransfers: [],
422 subagents,
423 }) as unknown as OrchestrationV2ThreadProjection;
424​
425 const childProjection = {
426 thread: { id: childThreadId },
427 runs: [],
428 contextTransfers: [],
429 messages: [],
430 subagents: [],
431 providerThreads: [],
432 turnItems: [],
433 } as unknown as OrchestrationV2ThreadProjection;
434​
435 it.effect(
436 "advertises orchestration capability from registered adapters rather than a driver allowlist",
437 () =>
438 Effect.gen(function* () {
439 const disabledAntigravityInstanceId = ProviderInstanceId.make("antigravity-alt");
440 const forkOnlyInstanceId = ProviderInstanceId.make("forkOnly");
441 const forkShadow = yield* buildUnavailableProviderSnapshot({
442 driverKind: "forkOnly",
443 instanceId: forkOnlyInstanceId,
444 reason: "Driver 'forkOnly' is not registered in this build.",
445 checkedAt: "2026-09-13T00:00:00.000Z",
446 });
447 const providers: ReadonlyArray<ServerProvider> = [
448 providerSnapshot({
449 instanceId: codexInstanceId,
450 driver: ProviderDriverKind.make("codex"),
451 model: "gpt-5.4",
452 }),
453 providerSnapshot({
454 instanceId: ProviderInstanceId.make("claudeAgent"),
455 driver: ProviderDriverKind.make("claudeAgent"),
456 model: "claude-sonnet-4-6",
457 }),
458 providerSnapshot({
459 instanceId: ProviderInstanceId.make("pi"),
460 driver: ProviderDriverKind.make("pi"),
461 model: "pi-model",
462 }),
463 providerSnapshot({
464 instanceId: ProviderInstanceId.make("acpRegistry"),
465 driver: ProviderDriverKind.make("acpRegistry"),
466 model: "acp-model",
467 }),
468 // Antigravity has a live orchestration adapter through the provider
469 // instance registry even though no adapter driver entry exists in
470 // the static built-in list.
471 providerSnapshot({
472 instanceId: antigravityInstanceId,
473 driver: ProviderDriverKind.make("antigravity"),
474 model: "ant-model",
475 }),
476 // A second Antigravity instance whose adapter resolves but whose
477 // provider state still blocks delegation.
478 providerSnapshot({
479 instanceId: disabledAntigravityInstanceId,
480 driver: ProviderDriverKind.make("antigravity"),
481 model: "ant-model",
482 enabled: false,
483 }),
484 forkShadow,
485 ];
486 const dependencies = Layer.mergeAll(
487 NodeServices.layer,
488 Layer.mock(ThreadManagementService)({
489 getThreadProjection: () => Effect.succeed(parentProjection([])),
490 }),
491 Layer.mock(ProviderRegistry)({ getProviders: Effect.succeed(providers) }),
492 adapterRegistryLayer([
493 codexInstanceId,
494 ProviderInstanceId.make("claudeAgent"),
495 ProviderInstanceId.make("pi"),
496 ProviderInstanceId.make("acpRegistry"),
497 antigravityInstanceId,
498 disabledAntigravityInstanceId,
499 ]),
500 Layer.mock(ScheduledTaskService)({}),
501 );
502​
503 yield* Effect.gen(function* () {
504 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
505 const capabilities = yield* service.capabilities(scope);
506 const byId = new Map(
507 capabilities.providers.map((provider) => [provider.providerInstanceId, provider]),
508 );
509​
510 for (const instanceId of [
511 codexInstanceId,
512 ProviderInstanceId.make("claudeAgent"),
513 ProviderInstanceId.make("pi"),
514 ProviderInstanceId.make("acpRegistry"),
515 antigravityInstanceId,
516 ]) {
517 const entry = byId.get(instanceId);
518 assert.isDefined(entry);
519 assert.isTrue(
520 entry!.canRunChildTask,
521 `expected ${instanceId} to advertise canRunChildTask`,
522 );
523 assert.isTrue(entry!.canRunCrossProviderChildTask);
524 assert.deepEqual(entry!.constraints, []);
525 }
526​
527 const disabled = byId.get(disabledAntigravityInstanceId);
528 assert.isDefined(disabled);
529 assert.isFalse(disabled!.canRunChildTask);
530 assert.deepEqual(disabled!.constraints, ["Provider instance is disabled."]);
531​
532 const fork = byId.get(forkOnlyInstanceId);
533 assert.isDefined(fork);
534 assert.isFalse(fork!.canRunChildTask);
535 assert.isTrue(
536 fork!.constraints.includes("No V2 provider adapter is registered."),
537 `expected missing-adapter constraint, got ${fork!.constraints.join(" | ")}`,
538 );
539 assert.isTrue(
540 fork!.constraints.includes("Driver 'forkOnly' is not registered in this build."),
541 );
542 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
543 }),
544 );
545​
546 it.effect(
547 "delegates to an Antigravity instance whose adapter resolves through the registry",
548 () =>
549 Effect.gen(function* () {
550 let delegated = false;
551 const task = {
552 id: taskId,
553 threadId: parentThreadId,
554 runId: parentRunId,
555 parentNodeId,
556 origin: "app_owned",
557 createdBy: "agent",
558 driver: ProviderDriverKind.make("antigravity"),
559 providerInstanceId: antigravityInstanceId,
560 providerThreadId: null,
561 childThreadId,
562 nativeTaskRef: null,
563 prompt: "Summarize the diff.",
564 title: null,
565 model: "ant-model",
566 status: "running",
567 result: null,
568 startedAt: null,
569 completedAt: null,
570 };
571 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
572 const dependencies = Layer.mergeAll(
573 NodeServices.layer,
574 Layer.mock(ThreadManagementService)({
575 getThreadProjection: (threadId) =>
576 Effect.succeed(
577 threadId === parentThreadId
578 ? parentProjection(delegated ? [task] : [])
579 : childProjection,
580 ),
581 dispatch: (command) =>
582 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
583 Effect.andThen(
584 Effect.sync(() => {
585 delegated = true;
586 }),
587 ),
588 Effect.as({
589 sequence: 1,
590 storedEvents: [
591 {
592 sequence: 1,
593 commandId: null,
594 event: { type: "subagent.updated", payload: task },
595 },
596 ],
597 } as never),
598 ),
599 }),
600 Layer.mock(ProviderRegistry)({
601 getProviders: Effect.succeed([
602 providerSnapshot({
603 instanceId: codexInstanceId,
604 driver: ProviderDriverKind.make("codex"),
605 model: "gpt-5.4",
606 }),
607 providerSnapshot({
608 instanceId: antigravityInstanceId,
609 driver: ProviderDriverKind.make("antigravity"),
610 model: "ant-model",
611 }),
612 ]),
613 }),
614 adapterRegistryLayer([codexInstanceId, antigravityInstanceId]),
615 Layer.mock(ScheduledTaskService)({}),
616 );
617​
618 yield* Effect.gen(function* () {
619 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
620 const result = yield* service.delegateTask(scope, {
621 task: "Summarize the diff.",
622 target: { providerInstanceId: antigravityInstanceId, model: "ant-model" },
623 mode: "async",
624 clientRequestId: "delegate-antigravity-1",
625 });
626 assert.equal(result.status, "running");
627 assert.equal(result.providerInstanceId, antigravityInstanceId);
628 const commands = yield* Ref.get(dispatched);
629 assert.equal(commands.length, 1);
630 const request = commands[0] as {
631 type: string;
632 modelSelection: { instanceId: string; model: string };
633 };
634 assert.equal(request.type, "delegated_task.request");
635 assert.equal(request.modelSelection.instanceId, antigravityInstanceId);
636 assert.equal(request.modelSelection.model, "ant-model");
637 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
638 }),
639 );
640​
641 it.effect("resolves a driverKind target to a capable Antigravity instance", () =>
642 Effect.gen(function* () {
643 const dispatched = yield* Ref.make<ReadonlyArray<unknown>>([]);
644 const task = {
645 id: taskId,
646 threadId: parentThreadId,
647 runId: parentRunId,
648 parentNodeId,
649 origin: "app_owned",
650 createdBy: "agent",
651 driver: ProviderDriverKind.make("antigravity"),
652 providerInstanceId: antigravityInstanceId,
653 providerThreadId: null,
654 childThreadId,
655 nativeTaskRef: null,
656 prompt: "Summarize the diff.",
657 title: null,
658 model: "ant-model",
659 status: "running",
660 result: null,
661 startedAt: null,
662 completedAt: null,
663 };
664 let delegated = false;
665 const dependencies = Layer.mergeAll(
666 NodeServices.layer,
667 Layer.mock(ThreadManagementService)({
668 getThreadProjection: (threadId) =>
669 Effect.succeed(
670 threadId === parentThreadId
671 ? parentProjection(delegated ? [task] : [])
672 : childProjection,
673 ),
674 dispatch: (command) =>
675 Ref.update(dispatched, (commands) => [...commands, command]).pipe(
676 Effect.andThen(
677 Effect.sync(() => {
678 delegated = true;
679 }),
680 ),
681 Effect.as({
682 sequence: 1,
683 storedEvents: [
684 {
685 sequence: 1,
686 commandId: null,
687 event: { type: "subagent.updated", payload: task },
688 },
689 ],
690 } as never),
691 ),
692 }),
693 Layer.mock(ProviderRegistry)({
694 getProviders: Effect.succeed([
695 providerSnapshot({
696 instanceId: codexInstanceId,
697 driver: ProviderDriverKind.make("codex"),
698 model: "gpt-5.4",
699 }),
700 providerSnapshot({
701 instanceId: antigravityInstanceId,
702 driver: ProviderDriverKind.make("antigravity"),
703 model: "ant-model",
704 }),
705 ]),
706 }),
707 adapterRegistryLayer([codexInstanceId, antigravityInstanceId]),
708 Layer.mock(ScheduledTaskService)({}),
709 );
710​
711 yield* Effect.gen(function* () {
712 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
713 const result = yield* service.delegateTask(scope, {
714 task: "Summarize the diff.",
715 target: { driverKind: ProviderDriverKind.make("antigravity") },
716 mode: "async",
717 clientRequestId: "delegate-antigravity-driver-1",
718 });
719 assert.equal(result.status, "running");
720 const commands = yield* Ref.get(dispatched);
721 assert.equal(commands.length, 1);
722 const request = commands[0] as {
723 modelSelection: { instanceId: string; model: string };
724 };
725 assert.equal(request.modelSelection.instanceId, antigravityInstanceId);
726 assert.equal(request.modelSelection.model, "ant-model");
727 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
728 }),
729 );
730​
731 it.effect("rejects delegation to a provider without a registered adapter", () =>
732 Effect.gen(function* () {
733 const forkOnlyInstanceId = ProviderInstanceId.make("forkOnly");
734 const forkShadow = yield* buildUnavailableProviderSnapshot({
735 driverKind: "forkOnly",
736 instanceId: forkOnlyInstanceId,
737 reason: "Driver 'forkOnly' is not registered in this build.",
738 checkedAt: "2026-09-13T00:00:00.000Z",
739 });
740 const dependencies = Layer.mergeAll(
741 NodeServices.layer,
742 Layer.mock(ThreadManagementService)({
743 getThreadProjection: () => Effect.succeed(parentProjection([])),
744 }),
745 Layer.mock(ProviderRegistry)({
746 getProviders: Effect.succeed([
747 providerSnapshot({
748 instanceId: codexInstanceId,
749 driver: ProviderDriverKind.make("codex"),
750 model: "gpt-5.4",
751 }),
752 forkShadow,
753 ]),
754 }),
755 adapterRegistryLayer([codexInstanceId]),
756 Layer.mock(ScheduledTaskService)({}),
757 );
758​
759 yield* Effect.gen(function* () {
760 const service = yield* OrchestratorMcpService.OrchestratorMcpService;
761 const byInstance = yield* service
762 .delegateTask(scope, {
763 task: "Summarize the diff.",
764 target: { providerInstanceId: forkOnlyInstanceId },
765 mode: "async",
766 clientRequestId: "delegate-fork-1",
767 })
768 .pipe(Effect.flip);
769 assert.equal(byInstance.code, "provider_unavailable");
770 assert.isTrue(byInstance.message.includes("No V2 provider adapter is registered."));
771​
772 const byDriver = yield* service
773 .delegateTask(scope, {
774 task: "Summarize the diff.",
775 target: { driverKind: ProviderDriverKind.make("forkOnly") },
776 mode: "async",
777 clientRequestId: "delegate-fork-2",
778 })
779 .pipe(Effect.flip);
780 assert.equal(byDriver.code, "provider_unavailable");
781 assert.isTrue(
782 byDriver.message.includes("No V2 provider adapter is registered for driver forkOnly."),
783 );
784 }).pipe(Effect.provide(OrchestratorMcpService.layer.pipe(Layer.provide(dependencies))));
785 }),
786 );
787});