@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 140 lines 5 KB
Raw
1 import { expect, it } from "@effect/vitest";
2 import {
3 EnvironmentId,
4 type OrchestrationV2ThreadShell,
5 ProviderInstanceId,
6 ThreadId,
7 } from "@t3tools/contracts";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10
11 import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
12 import * as McpInvocationContext from "./McpInvocationContext.ts";
13 import { liveThreadShell } from "./McpToolAccess.testkit.ts";
14 import { assertTargetWithinLimits, readThread } from "./threadAccess.ts";
15
16 const bareId = ThreadId.make("5d0b4a3e-8c1f-4a52-9d37-2f6c7e1b9a04");
17 const claudeCode: McpInvocationContext.McpClientCaller = {
18 sessionId: "thread-access",
19 label: "Claude Code",
20 access: "full-access",
21 };
22 const 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
31 const 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
34 const 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
54 it.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
65 it.effect("tells a caller with linked environments that the thread may be on one of them", () =>
66 Effect.gen(function* () {
67 const error = yield* readBare([], { ...client, linkedEnvironments: true });
68
69 expect(error.code).toBe("thread_not_found");
70 expect(error.message).toContain("t3_environment_links");
71 expect(error.message).toContain("environmentId");
72
73 const linksOff = yield* readBare([], { ...client, linkedEnvironments: false });
74 expect(linksOff.message).toBe("The thread was not found.");
75 const peerLink = yield* readBare([], {
76 ...client,
77 linkedEnvironments: true,
78 client: { ...claudeCode, linked: true },
79 });
80 expect(peerLink.message).toBe("The thread was not found.");
81
82 // A thread a fenced link started may not use this environment's links.
83 const fencedThread = ThreadId.make("thread:started-by-fenced-link");
84 const fenced = yield* readBare(
85 [
86 {
87 ...liveThreadShell(fencedThread),
88 linkOrigin: { sessionId: "peer-session", label: "Laptop", access: "auto" },
89 },
90 ],
91 {
92 ...client,
93 linkedEnvironments: true,
94 client: undefined,
95 thread: {
96 threadId: fencedThread,
97 providerSessionId: "provider-session",
98 providerInstanceId: ProviderInstanceId.make("codex"),
99 },
100 },
101 );
102 expect(fenced.message).toBe("The thread was not found.");
103 }),
104 );
105
106 it.effect("points an unencoded delegated-task id at the encoded thread it belongs to", () =>
107 Effect.gen(function* () {
108 const commandId = `command:mcp:${bareId}:delegate-task:0e7d2c41-6b9a-4f3e-8d15-a2c4b6e8f019`;
109 const encoded = ThreadId.make(`thread:delegated-task:${encodeURIComponent(commandId)}`);
110 const error = yield* readBare(
111 [encoded],
112 client,
113 ThreadId.make(`thread:delegated-task:${commandId}`),
114 );
115
116 expect(error.code).toBe("thread_not_found");
117 expect(error.message).toContain(`\`${encoded}\``);
118 }),
119 );
120
121 it.effect("identifies the target and effective caller ceiling when a thread write is refused", () =>
122 Effect.gen(function* () {
123 for (const [runtimeMode, interactionMode, code] of [
124 ["approval-required", "default", "runtime_mode_escalation_denied"],
125 ["full-access", "plan", "interaction_mode_escalation_denied"],
126 ["approval-required", "plan", "runtime_mode_escalation_denied"],
127 ] as const) {
128 const target = liveThreadShell(bareId, { runtimeMode: "full-access" });
129 const error = yield* assertTargetWithinLimits({ runtimeMode, interactionMode }, target).pipe(
130 Effect.flip,
131 );
132
133 expect(error.code).toBe(code);
134 expect(error.message).toContain(bareId);
135 expect(error.message).toContain("full-access/default");
136 expect(error.message).toContain(`${runtimeMode}/${interactionMode}`);
137 expect(error.message).not.toMatch(/child|parent mode/i);
138 }
139 }),
140 );