Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import { HttpServer } from "effect/http";
6import * as NetAddress from "effect/net/NetAddress";
7
8import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
9import * as McpSessionRegistry from "./McpSessionRegistry.ts";
10
11const environmentId = EnvironmentId.make("environment-1");
12const makeFakeHttpServer = (hostname: string, port = 43123) =>
13 HttpServer.HttpServer.of({
14 address: NetAddress.inetAddressFromIpStringUnsafe(hostname, port),
15 serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
16 });
17const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
18const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
19 getEnvironmentId: Effect.succeed(environmentId),
20 getDescriptor: Effect.die("unused"),
21});
22
23const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
24 McpSessionRegistry.__testing
25 .make({
26 now,
27 livenessWindowMs: 100,
28 })
29 .pipe(
30 Effect.provideService(HttpServer.HttpServer, httpServer),
31 Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment),
32 Effect.provide(NodeServices.layer),
33 );
34
35it.effect("stores only a token hash, resolves the bearer token, and revokes by thread", () =>
36 Effect.gen(function* () {
37 let timestamp = 1_000;
38 const registry = yield* makeRegistry(() => timestamp);
39 const threadId = ThreadId.make("thread-1");
40 const issued = yield* registry.issue({
41 threadId,
42 providerInstanceId: ProviderInstanceId.make("codex"),
43 capabilities: new Set(["preview"]),
44 });
45 expect(issued.config.endpoint).toBe("http://127.0.0.1:43123/mcp");
46 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
47 expect(token.length).toBeGreaterThan(20);
48
49 const resolved = yield* registry.resolve(token);
50 expect(resolved?.thread.threadId).toBe(threadId);
51 expect(resolved?.capabilities).toEqual(
52 new Set(["preview", "orchestration", "worktree", "pull-requests"]),
53 );
54
55 yield* registry.revokeThread(threadId);
56 expect(yield* registry.resolve(token)).toBeUndefined();
57
58 timestamp += 2_000;
59 }),
60);
61
62it.effect("always grants pull-requests and gates browser and device access independently", () =>
63 Effect.gen(function* () {
64 const registry = yield* makeRegistry(() => 1_000);
65 const withPreview = yield* registry.issue({
66 threadId: ThreadId.make("thread-preview"),
67 providerInstanceId: ProviderInstanceId.make("codex"),
68 capabilities: new Set(["preview"]),
69 });
70 const withoutPreview = yield* registry.issue({
71 threadId: ThreadId.make("thread-no-preview"),
72 providerInstanceId: ProviderInstanceId.make("codex"),
73 capabilities: new Set(),
74 });
75 const withDevice = yield* registry.issue({
76 threadId: ThreadId.make("thread-device"),
77 providerInstanceId: ProviderInstanceId.make("codex"),
78 capabilities: new Set(["device"]),
79 });
80 const capabilitiesOf = (issued: typeof withPreview) =>
81 registry
82 .resolve(issued.config.authorizationHeader.replace(/^Bearer\s+/, ""))
83 .pipe(Effect.map((scope) => [...(scope?.capabilities ?? [])].sort()));
84
85 expect(yield* capabilitiesOf(withPreview)).toEqual([
86 "orchestration",
87 "preview",
88 "pull-requests",
89 "worktree",
90 ]);
91 expect(yield* capabilitiesOf(withoutPreview)).toEqual([
92 "orchestration",
93 "pull-requests",
94 "worktree",
95 ]);
96 expect(yield* capabilitiesOf(withDevice)).toEqual([
97 "device",
98 "orchestration",
99 "pull-requests",
100 "worktree",
101 ]);
102 }),
103);
104
105// regression: #3114 use bound host for MCP endpoint
106it.effect("builds MCP endpoints from the bound server host", () =>
107 Effect.gen(function* () {
108 const cases = [
109 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
110 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
111 ["::", "http://127.0.0.1:43123/mcp"],
112 ["::1", "http://[::1]:43123/mcp"],
113 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
114 ] as const;
115
116 for (const [hostname, expectedEndpoint] of cases) {
117 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
118 const issued = yield* registry.issue({
119 threadId: ThreadId.make(`thread-${hostname}`),
120 providerInstanceId: ProviderInstanceId.make("codex"),
121 capabilities: new Set(["preview"]),
122 });
123 expect(issued.config.endpoint).toBe(expectedEndpoint);
124 }
125 }),
126);
127
128it.effect("expires credentials once their session stops showing signs of life", () =>
129 Effect.gen(function* () {
130 let timestamp = 1_000;
131 const registry = yield* makeRegistry(() => timestamp);
132 const issued = yield* registry.issue({
133 threadId: ThreadId.make("thread-2"),
134 providerInstanceId: ProviderInstanceId.make("claude"),
135 capabilities: new Set(["preview"]),
136 });
137 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
138 timestamp += 101;
139 expect(yield* registry.resolve(token)).toBeUndefined();
140 }),
141);
142
143it.effect("does not keep credentials of other threads alive", () =>
144 Effect.gen(function* () {
145 let timestamp = 1_000;
146 const registry = yield* makeRegistry(() => timestamp);
147 const issued = yield* registry.issue({
148 threadId: ThreadId.make("thread-4"),
149 providerInstanceId: ProviderInstanceId.make("codex"),
150 capabilities: new Set(["preview"]),
151 });
152 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
153
154 timestamp += 99;
155 yield* registry.touch(ThreadId.make("thread-unrelated"));
156 timestamp += 2;
157
158 expect(yield* registry.resolve(token)).toBeUndefined();
159 }),
160);
161
162it.effect("only a trusted touch restores an expired token with its original scope", () =>
163 Effect.gen(function* () {
164 let timestamp = 1_000;
165 const registry = yield* makeRegistry(() => timestamp);
166 const threadId = ThreadId.make("thread-idle");
167 const issued = yield* registry.issue({
168 threadId,
169 providerInstanceId: ProviderInstanceId.make("claude"),
170 capabilities: new Set(["preview"]),
171 });
172 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
173 const scope = yield* registry.resolve(token);
174 expect(scope).toBeDefined();
175
176 timestamp += 101;
177 expect(yield* registry.resolve(token)).toBeUndefined();
178 const otherThreadId = ThreadId.make("thread-active");
179 const other = yield* registry.issue({
180 threadId: otherThreadId,
181 providerInstanceId: ProviderInstanceId.make("codex"),
182 capabilities: new Set(["device"]),
183 });
184 yield* registry.touch(otherThreadId);
185 expect(
186 (yield* registry.resolve(other.config.authorizationHeader.replace(/^Bearer\s+/, "")))?.thread
187 .threadId,
188 ).toBe(otherThreadId);
189 expect(yield* registry.resolve("unknown-token")).toBeUndefined();
190 expect(yield* registry.resolve(token)).toBeUndefined();
191
192 yield* registry.touch(threadId);
193 expect(yield* registry.resolve(token)).toEqual(scope);
194 timestamp += 101;
195 expect(yield* registry.resolve(token)).toBeUndefined();
196 }),
197);
198
199it.effect.each(["provider-session", "thread", "all"] as const)(
200 "never restores an expired token after %s revocation",
201 (revocation) =>
202 Effect.gen(function* () {
203 let timestamp = 1_000;
204 const registry = yield* makeRegistry(() => timestamp);
205 const threadId = ThreadId.make("thread-revoked");
206 const issued = yield* registry.issue({
207 threadId,
208 providerInstanceId: ProviderInstanceId.make("codex"),
209 capabilities: new Set(["preview"]),
210 });
211 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
212
213 timestamp += 101;
214 yield* {
215 "provider-session": registry.revokeProviderSession(issued.config.providerSessionId),
216 thread: registry.revokeThread(threadId),
217 all: registry.revokeAll,
218 }[revocation];
219 yield* registry.touch(threadId);
220 expect(yield* registry.resolve(token)).toBeUndefined();
221 }),
222);