|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { assert, it } from "@effect/vitest"; |
| 3 | +import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"; |
| 4 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 5 | +import * as Effect from "effect/Effect"; |
| 6 | +import * as Exit from "effect/Exit"; |
| 7 | +import * as Layer from "effect/Layer"; |
| 8 | +import * as Stream from "effect/Stream"; |
| 9 | +import type { Tool } from "effect/ai"; |
| 10 | +import { McpServer } from "effect/ai"; |
| 11 | + |
| 12 | +import * as ThreadGroups from "../../../orchestration-v2/ThreadGroupsService.fork.ts"; |
| 13 | +import { ensureForkSchema } from "../../../persistence/ForkSchema.ts"; |
| 14 | +import * as McpHttpServer from "../../McpHttpServer.ts"; |
| 15 | +import * as McpInvocationContext from "../../McpInvocationContext.ts"; |
| 16 | +import * as McpToolAccess from "../../McpToolAccess.ts"; |
| 17 | +import * as McpToolAccessTestkit from "../../McpToolAccess.testkit.ts"; |
| 18 | +import { prepareLaunchGroupFork, ThreadGroupsToolkitHandlersLiveFork } from "./handlers.fork.ts"; |
| 19 | +import { ThreadGroupsToolkitFork } from "./tools.fork.ts"; |
| 20 | + |
| 21 | +const orchestrator = ThreadId.make("orchestrator"); |
| 22 | +const first = ThreadId.make("first"); |
| 23 | +const second = ThreadId.make("second"); |
| 24 | + |
| 25 | +const invocation: McpInvocationContext.McpInvocationScope = { |
| 26 | + environmentId: EnvironmentId.make("environment-1"), |
| 27 | + requestNamespace: "provider-session:provider-session-1", |
| 28 | + thread: { |
| 29 | + threadId: orchestrator, |
| 30 | + providerSessionId: "provider-session-1", |
| 31 | + providerInstanceId: ProviderInstanceId.make("codex"), |
| 32 | + }, |
| 33 | + client: undefined, |
| 34 | + capabilities: new Set(["orchestration"]), |
| 35 | + issuedAt: 1, |
| 36 | +}; |
| 37 | + |
| 38 | +// Every thread is live and titled by its id; each test gets a fresh database. |
| 39 | +const services = ThreadGroups.layer.pipe( |
| 40 | + Layer.provide(Layer.effectDiscard(ensureForkSchema())), |
| 41 | + Layer.provideMerge( |
| 42 | + Layer.mergeAll( |
| 43 | + NodeSqliteClient.layer({ filename: ":memory:" }), |
| 44 | + NodeCrypto.layer, |
| 45 | + McpToolAccessTestkit.liveThreadsLayer, |
| 46 | + ), |
| 47 | + ), |
| 48 | +); |
| 49 | + |
| 50 | +const call = <Name extends keyof typeof ThreadGroupsToolkitFork.tools>( |
| 51 | + name: Name, |
| 52 | + params: Tool.Parameters<(typeof ThreadGroupsToolkitFork.tools)[Name]>, |
| 53 | +) => |
| 54 | + ThreadGroupsToolkitFork.pipe( |
| 55 | + Effect.flatMap((toolkit) => toolkit.handle(name, params as never)), |
| 56 | + Stream.unwrap, |
| 57 | + Stream.runCollect, |
| 58 | + Effect.map( |
| 59 | + (chunk) => chunk.at(-1)!.result as Tool.Success<(typeof ThreadGroupsToolkitFork.tools)[Name]>, |
| 60 | + ), |
| 61 | + Effect.provide(McpToolAccess.HandlersLayer.layer(ThreadGroupsToolkitHandlersLiveFork)), |
| 62 | + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation), |
| 63 | + ); |
| 64 | + |
| 65 | +const startGroup = ThreadGroups.ThreadGroupsServiceFork.pipe( |
| 66 | + Effect.flatMap((groups) => |
| 67 | + groups.mutate({ |
| 68 | + type: "set-membership", |
| 69 | + threadIds: [orchestrator, first], |
| 70 | + target: { type: "new", groupId: "g1", title: "Work", asParent: true }, |
| 71 | + }), |
| 72 | + ), |
| 73 | +); |
| 74 | + |
| 75 | +it.effect("agents move threads into the caller's group and list it by name", () => |
| 76 | + Effect.gen(function* () { |
| 77 | + yield* startGroup; |
| 78 | + yield* call("t3_thread_group_update", { |
| 79 | + edit: { |
| 80 | + type: "set-membership", |
| 81 | + threadIds: [second], |
| 82 | + target: { type: "existing", groupId: "g1" }, |
| 83 | + }, |
| 84 | + }); |
| 85 | + assert.deepStrictEqual(yield* call("t3_thread_group_list", {}), { |
| 86 | + groups: [ |
| 87 | + { |
| 88 | + id: "g1", |
| 89 | + title: "Work", |
| 90 | + parentThreadId: orchestrator, |
| 91 | + threads: [orchestrator, first, second].map((threadId) => ({ threadId, title: threadId })), |
| 92 | + }, |
| 93 | + ], |
| 94 | + callerGroupId: "g1", |
| 95 | + }); |
| 96 | + }).pipe(Effect.provide(services)), |
| 97 | +); |
| 98 | + |
| 99 | +it.effect("launches join the caller's group by default and refuse an unknown group", () => |
| 100 | + Effect.gen(function* () { |
| 101 | + yield* startGroup; |
| 102 | + const groups = yield* ThreadGroups.ThreadGroupsServiceFork; |
| 103 | + const ungrouped = yield* prepareLaunchGroupFork(orchestrator, "none"); |
| 104 | + yield* ungrouped(second); |
| 105 | + assert.strictEqual(yield* groups.groupOf(second), undefined); |
| 106 | + |
| 107 | + const joinCaller = yield* prepareLaunchGroupFork(orchestrator, undefined); |
| 108 | + yield* joinCaller(second); |
| 109 | + assert.deepStrictEqual((yield* groups.groupOf(second))?.threadIds, [ |
| 110 | + orchestrator, |
| 111 | + first, |
| 112 | + second, |
| 113 | + ]); |
| 114 | + // The thread already exists, so a group removed mid-launch must not fail it. |
| 115 | + yield* groups.mutate({ type: "remove", groupId: "g1" }); |
| 116 | + yield* joinCaller(first); |
| 117 | + assert.strictEqual(yield* groups.groupOf(first), undefined); |
| 118 | + |
| 119 | + const unknown = yield* prepareLaunchGroupFork(orchestrator, { groupId: "gone" }).pipe( |
| 120 | + Effect.exit, |
| 121 | + ); |
| 122 | + assert.ok(Exit.isFailure(unknown)); |
| 123 | + }).pipe(Effect.provide(services)), |
| 124 | +); |
| 125 | + |
| 126 | +// MCP refuses a tool whose parameters lack an object root, crashing startup. |
| 127 | +it.effect("registers with an MCP server", () => |
| 128 | + Effect.void.pipe( |
| 129 | + Effect.provide( |
| 130 | + McpHttpServer.toolkitRegistration( |
| 131 | + ThreadGroupsToolkitFork, |
| 132 | + ThreadGroupsToolkitHandlersLiveFork, |
| 133 | + ).pipe(Layer.provideMerge(McpServer.McpServer.layer), Layer.provideMerge(services)), |
| 134 | + ), |
| 135 | + ), |
| 136 | +); |
0 commit comments