|
| 1 | +import { assert, describe, it } from "@effect/vitest"; |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as Layer from "effect/Layer"; |
| 5 | +import * as Stream from "effect/Stream"; |
| 6 | + |
| 7 | +import { EnvironmentId, ProviderInstanceId, ProviderSessionId, ThreadId } from "@t3tools/contracts"; |
| 8 | +import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 9 | +import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
| 10 | +import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 11 | +import * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 12 | +import * as ProviderHost from "@t3tools/provider-core/server/ProviderHost"; |
| 13 | +import * as orchestrationInstructions from "@t3tools/provider-core/server/orchestrationInstructions"; |
| 14 | +import { mcpRules, make, t3McpServerName } from "./adapter.ts"; |
| 15 | +import * as OpenCode2Server from "./OpenCode2Server.ts"; |
| 16 | + |
| 17 | +type RecordedCall = { readonly op: string; readonly args: Record<string, unknown> }; |
| 18 | + |
| 19 | +const recordingClient = (calls: Array<RecordedCall>) => |
| 20 | + ({ |
| 21 | + mcp: { |
| 22 | + add: (input: Record<string, unknown>) => |
| 23 | + Effect.sync(() => { |
| 24 | + calls.push({ op: "mcp.add", args: input }); |
| 25 | + return {}; |
| 26 | + }), |
| 27 | + remove: (input: Record<string, unknown>) => |
| 28 | + Effect.sync(() => { |
| 29 | + calls.push({ op: "mcp.remove", args: input }); |
| 30 | + return {}; |
| 31 | + }), |
| 32 | + }, |
| 33 | + session: { |
| 34 | + create: (input: Record<string, unknown>) => |
| 35 | + Effect.sync(() => { |
| 36 | + calls.push({ op: "session.create", args: input }); |
| 37 | + return { id: "ses_fake" }; |
| 38 | + }), |
| 39 | + form: { list: () => Effect.succeed([]) }, |
| 40 | + }, |
| 41 | + model: { list: () => Effect.succeed({ data: [] }) }, |
| 42 | + agent: { list: () => Effect.succeed({ data: [{ id: "build", permissions: [] }] }) }, |
| 43 | + permission: { list: () => Effect.succeed([]) }, |
| 44 | + message: { list: () => Effect.succeed({ data: [] }) }, |
| 45 | + }) as unknown as OpenCode2Server.OpenCode2Connection["client"]; |
| 46 | + |
| 47 | +const makeLayers = (calls: Array<RecordedCall>) => { |
| 48 | + const connection = { |
| 49 | + client: recordingClient(calls), |
| 50 | + events: Effect.succeed(Stream.never), |
| 51 | + url: "http://127.0.0.1:4096", |
| 52 | + version: "2.1.2", |
| 53 | + external: false, |
| 54 | + } as unknown as OpenCode2Server.OpenCode2Connection; |
| 55 | + return Layer.mergeAll( |
| 56 | + Layer.succeed(OpenCode2Server.OpenCode2Server, { |
| 57 | + withConnection: <A, E, R>( |
| 58 | + use: (connection: OpenCode2Server.OpenCode2Connection) => Effect.Effect<A, E, R>, |
| 59 | + ) => use(connection), |
| 60 | + } as unknown as OpenCode2Server.OpenCode2Server["Service"]), |
| 61 | + Layer.succeed(ProviderHost.ProviderHost, { |
| 62 | + paths: { cwd: "/workspace" }, |
| 63 | + resolveAttachmentPath: () => null, |
| 64 | + } as unknown as ProviderHost.ProviderHost["Service"]), |
| 65 | + IdAllocator.layer, |
| 66 | + McpProviderSessions.layer, |
| 67 | + ProviderContinuationRequests.layer, |
| 68 | + ).pipe(Layer.provideMerge(NodeServices.layer)); |
| 69 | +}; |
| 70 | + |
| 71 | +const mcpConfig = (threadId: ThreadId) => ({ |
| 72 | + environmentId: EnvironmentId.make("environment-test"), |
| 73 | + threadId, |
| 74 | + providerSessionId: ProviderSessionId.make("provider-session-test"), |
| 75 | + providerInstanceId: ProviderInstanceId.make("opencode-test"), |
| 76 | + endpoint: "http://127.0.0.1:4096/mcp", |
| 77 | + authorizationHeader: "Bearer test-token", |
| 78 | + browserToolsAvailable: false, |
| 79 | + capabilities: new Set(["orchestration", "worktree", "pull-requests"]), |
| 80 | +}); |
| 81 | + |
| 82 | +const runtimePolicy = () => |
| 83 | + ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 84 | + runtimeMode: "full-access", |
| 85 | + interactionMode: "default", |
| 86 | + cwd: "/workspace", |
| 87 | + }); |
| 88 | + |
| 89 | +const openFor = (adapter: ProviderAdapter.ProviderAdapterV2["Service"], threadId: ThreadId) => |
| 90 | + adapter.openSession({ |
| 91 | + threadId, |
| 92 | + providerSessionId: ProviderSessionId.make(`provider-session-${threadId}`), |
| 93 | + modelSelection: { |
| 94 | + instanceId: ProviderInstanceId.make("opencode-test"), |
| 95 | + model: "anthropic/claude-sonnet", |
| 96 | + options: [], |
| 97 | + }, |
| 98 | + runtimePolicy: runtimePolicy(), |
| 99 | + }); |
| 100 | + |
| 101 | +it.effect( |
| 102 | + "registers the thread's T3 MCP server when the session opens, before any OpenCode session exists", |
| 103 | + () => { |
| 104 | + const calls: Array<RecordedCall> = []; |
| 105 | + return Effect.gen(function* () { |
| 106 | + const threadId = ThreadId.make("thread-490f020d"); |
| 107 | + const adapter = yield* make(ProviderInstanceId.make("opencode-test")); |
| 108 | + const mcpSessions = yield* McpProviderSessions.McpProviderSessions; |
| 109 | + yield* mcpSessions.set(mcpConfig(threadId)); |
| 110 | + |
| 111 | + yield* openFor(adapter, threadId); |
| 112 | + |
| 113 | + const adds = calls.filter((call) => call.op === "mcp.add"); |
| 114 | + assert.strictEqual(adds.length, 1); |
| 115 | + assert.strictEqual(adds[0]!.args.server, `t3-code-${threadId}`); |
| 116 | + // OpenCode builds a session's tool catalog at session creation, so the |
| 117 | + // server must already be in place when the first `session.create` runs. |
| 118 | + assert.strictEqual(calls.filter((call) => call.op === "session.create").length, 0); |
| 119 | + }).pipe(Effect.provide(makeLayers(calls))); |
| 120 | + }, |
| 121 | +); |
| 122 | + |
| 123 | +it.effect( |
| 124 | + "two concurrent threads register distinct servers and keep only their own routing rules", |
| 125 | + () => { |
| 126 | + const calls: Array<RecordedCall> = []; |
| 127 | + return Effect.gen(function* () { |
| 128 | + const threadA = ThreadId.make("thread-aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"); |
| 129 | + const threadB = ThreadId.make("thread-bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"); |
| 130 | + const adapter = yield* make(ProviderInstanceId.make("opencode-test")); |
| 131 | + const mcpSessions = yield* McpProviderSessions.McpProviderSessions; |
| 132 | + yield* mcpSessions.set(mcpConfig(threadA)); |
| 133 | + yield* mcpSessions.set(mcpConfig(threadB)); |
| 134 | + |
| 135 | + yield* openFor(adapter, threadA); |
| 136 | + yield* openFor(adapter, threadB); |
| 137 | + |
| 138 | + const names = calls |
| 139 | + .filter((call) => call.op === "mcp.add") |
| 140 | + .map((call) => call.args.server as string); |
| 141 | + const nameA = `t3-code-${threadA}`; |
| 142 | + const nameB = `t3-code-${threadB}`; |
| 143 | + assert.deepStrictEqual([...names].sort(), [nameA, nameB].sort()); |
| 144 | + assert.notStrictEqual(nameA, nameB); |
| 145 | + |
| 146 | + // Each thread's rules deny every T3 server and re-allow only its own, |
| 147 | + // so one thread's agent can never route into another thread's tools. |
| 148 | + for (const [own, other] of [ |
| 149 | + [nameA, nameB], |
| 150 | + [nameB, nameA], |
| 151 | + ] as const) { |
| 152 | + const rules = mcpRules(own); |
| 153 | + assert.deepStrictEqual(rules, [ |
| 154 | + { action: "t3-code-*", resource: "*", effect: "deny" }, |
| 155 | + { action: `${own}_*`, resource: "*", effect: "allow" }, |
| 156 | + ]); |
| 157 | + const otherPrefix = other.slice(0, other.lastIndexOf("-") + 1); |
| 158 | + assert.isFalse( |
| 159 | + rules.some((entry) => entry.effect === "allow" && otherPrefix.startsWith(entry.action)), |
| 160 | + ); |
| 161 | + } |
| 162 | + }).pipe(Effect.provide(makeLayers(calls))); |
| 163 | + }, |
| 164 | +); |
| 165 | + |
| 166 | +it.layer(NodeServices.layer)("OpenCode2Adapter MCP server names", (it) => { |
| 167 | + it.effect("replaces a thread id that exceeds OpenCode's namespace limit with a digest", () => |
| 168 | + Effect.gen(function* () { |
| 169 | + const longA = `thread-${"a".repeat(80)}`; |
| 170 | + const longB = `thread-${"b".repeat(80)}`; |
| 171 | + const nameA = yield* t3McpServerName(longA); |
| 172 | + const nameB = yield* t3McpServerName(longB); |
| 173 | + assert.isTrue(nameA.length <= 64); |
| 174 | + assert.isTrue(nameB.length <= 64); |
| 175 | + assert.isTrue(nameA.startsWith("t3-code-")); |
| 176 | + assert.notStrictEqual(nameA, nameB); |
| 177 | + // A short id keeps its readable, sanitized form. |
| 178 | + const short = yield* t3McpServerName("thread_490f020d"); |
| 179 | + assert.strictEqual(short, "t3-code-thread_490f020d"); |
| 180 | + }), |
| 181 | + ); |
| 182 | +}); |
| 183 | + |
| 184 | +describe("T3 orchestration instructions", () => { |
| 185 | + it("names the session's registered namespace when the server is thread-suffixed", () => { |
| 186 | + const prompt = orchestrationInstructions.t3OrchestrationSystemPrompt(true, "t3-code-490f020d"); |
| 187 | + assert.isString(prompt); |
| 188 | + assert.include(prompt!, 'tools["t3-code-490f020d"].'); |
| 189 | + assert.include(prompt!, 'search({ query: "orchestrator_capabilities" })'); |
| 190 | + }); |
| 191 | + |
| 192 | + it("keeps the shared instructions unchanged when the server has its plain name", () => { |
| 193 | + const base = orchestrationInstructions.t3OrchestrationSystemPrompt(true); |
| 194 | + assert.strictEqual(base, orchestrationInstructions.T3_CODE_ORCHESTRATION_INSTRUCTIONS); |
| 195 | + assert.strictEqual( |
| 196 | + orchestrationInstructions.t3OrchestrationSystemPrompt(true, "t3-code"), |
| 197 | + base, |
| 198 | + ); |
| 199 | + assert.strictEqual(orchestrationInstructions.t3OrchestrationSystemPrompt(false), undefined); |
| 200 | + }); |
| 201 | + |
| 202 | + it("documents the thread-suffixed OpenCode pattern for discovery", () => { |
| 203 | + assert.include( |
| 204 | + orchestrationInstructions.T3_CODE_ORCHESTRATION_INSTRUCTIONS, |
| 205 | + "t3-code-<threadId>", |
| 206 | + ); |
| 207 | + assert.include( |
| 208 | + orchestrationInstructions.T3_CODE_ORCHESTRATION_INSTRUCTIONS, |
| 209 | + 'tools["t3-code-..."]', |
| 210 | + ); |
| 211 | + }); |
| 212 | +}); |
0 commit comments