Open History to see the last commit
1import { expect, it } from "@effect/vitest";
2import {
3 EnvironmentId,
4 type OrchestrationV2ThreadShell,
5 ProviderInstanceId,
6 ThreadId,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10
11import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
12import * as McpInvocationContext from "./McpInvocationContext.ts";
13import { liveThreadShell } from "./McpToolAccess.testkit.ts";
14import { assertTargetWithinLimits, readThread } from "./threadAccess.ts";
15
16const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
17const claudeCode: McpInvocationContext.McpClientCaller = {
18 sessionId: "thread-access",
19 label: "Claude Code",
20 access: "full-access",
21};
22const client: McpInvocationContext.McpInvocationScope = {
23 environmentId: EnvironmentId.make("environment:thread-access"),
24 requestNamespace: "client:thread-access",
25 thread: undefined,
26 client: claudeCode,
27 capabilities: new Set(["orchestration"]),
28 issuedAt: 1,
29};
30
31const linksHint =
32 "Thread ids belong to one environment: for a thread on a linked one, find it with `t3_environment_links` and pass its `environmentId` to a tool that takes one.";
33
34const readBare = (
35 existing: ReadonlyArray<OrchestrationV2ThreadShell | ThreadId>,
36 scope: McpInvocationContext.McpInvocationScope = client,
37 threadId: ThreadId = bareId,
38) => {
39 const shells = existing.map((shell) =>
40 typeof shell === "string" ? liveThreadShell(shell) : shell,
41 );
42 return readThread(threadId).pipe(
43 Effect.flip,
44 Effect.provideService(McpInvocationContext.McpInvocationContext, scope),
45 Effect.provide(
46 Layer.mock(ThreadManagement.ThreadManagementService)({
47 getThreadShell: (threadId) =>
48 Effect.succeed(shells.find((shell) => shell.id === threadId) ?? null),
49 }),
50 ),
51 );
52};
53
54it.effect("points a bare uuid at the mcp: thread it belongs to", () =>
55 Effect.gen(function* () {
56 const error = yield* readBare([ThreadId.make(`mcp:${bareId}`)]);
57
58 expect(error.code).toBe("thread_not_found");
59 expect(error.message).toBe(
60 `The thread was not found. Did you mean \`mcp:${bareId}\`? ${linksHint}`,
61 );
62 }),
63);
64
65it.effect("points a bare launch hash at the mcp:launch: thread it belongs to", () =>
66 Effect.gen(function* () {
67 const hash = "3f9a1c0be47d25a86e1f0c9d4b7a2e5f8c3d6b9a0e1f4c7d2a5b8e0f3c6d9a1b";
68 const launched = ThreadId.make(`mcp:launch:${hash}`);
69 const error = yield* readBare([launched], client, ThreadId.make(hash));
70
71 expect(error.code).toBe("thread_not_found");
72 expect(error.message).toBe(
73 `The thread was not found. Did you mean \`${launched}\`? ${linksHint}`,
74 );
75 }),
76);
77
78it.effect("tells a caller with linked environments that the thread may be on one of them", () =>
79 Effect.gen(function* () {
80 const error = yield* readBare([], { ...client, linkedEnvironments: true });
81
82 expect(error.code).toBe("thread_not_found");
83 expect(error.message).toContain("t3_environment_links");
84 expect(error.message).toContain("environmentId");
85
86 const linksOff = yield* readBare([], { ...client, linkedEnvironments: false });
87 expect(linksOff.message).toBe("The thread was not found.");
88 const peerLink = yield* readBare([], {
89 ...client,
90 linkedEnvironments: true,
91 client: { ...claudeCode, linked: true },
92 });
93 expect(peerLink.message).toBe("The thread was not found.");
94
95 // A thread a fenced link started may not use this environment's links.
96 const fencedThread = ThreadId.make("thread:started-by-fenced-link");
97 const fenced = yield* readBare(
98 [
99 {
100 ...liveThreadShell(fencedThread),
101 linkOrigin: { sessionId: "peer-session", label: "Laptop", access: "auto" },
102 },
103 ],
104 {
105 ...client,
106 linkedEnvironments: true,
107 client: undefined,
108 thread: {
109 threadId: fencedThread,
110 providerSessionId: "provider-session",
111 providerInstanceId: ProviderInstanceId.make("codex"),
112 },
113 },
114 );
115 expect(fenced.message).toBe("The thread was not found.");
116 }),
117);
118
119it.effect("points an unencoded delegated-task id at the encoded thread it belongs to", () =>
120 Effect.gen(function* () {
121 const commandId = `command:mcp:${bareId}:delegate-task:0e7d2c41-6b9a-4f3e-8d15-a2c4b6e8f019`;
122 const encoded = ThreadId.make(`thread:delegated-task:${encodeURIComponent(commandId)}`);
123 const error = yield* readBare(
124 [encoded],
125 client,
126 ThreadId.make(`thread:delegated-task:${commandId}`),
127 );
128
129 expect(error.code).toBe("thread_not_found");
130 expect(error.message).toContain(`\`${encoded}\``);
131 }),
132);
133
134it.effect("identifies the target and effective caller ceiling when a thread write is refused", () =>
135 Effect.gen(function* () {
136 for (const [runtimeMode, interactionMode, code] of [
137 ["approval-required", "default", "runtime_mode_escalation_denied"],
138 ["full-access", "plan", "interaction_mode_escalation_denied"],
139 ["approval-required", "plan", "runtime_mode_escalation_denied"],
140 ] as const) {
141 const target = liveThreadShell(bareId, { runtimeMode: "full-access" });
142 const error = yield* assertTargetWithinLimits({ runtimeMode, interactionMode }, target).pipe(
143 Effect.flip,
144 );
145
146 expect(error.code).toBe(code);
147 expect(error.message).toContain(bareId);
148 expect(error.message).toContain("full-access/default");
149 expect(error.message).toContain(`${runtimeMode}/${interactionMode}`);
150 expect(error.message).not.toMatch(/child|parent mode/i);
151 }
152 }),
153);