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