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/unstable/http";
6
7import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
8import * as McpSessionRegistry from "./McpSessionRegistry.ts";
9
10const environmentId = EnvironmentId.make("environment-1");
11const makeFakeHttpServer = (hostname: string, port = 43123) =>
12 HttpServer.HttpServer.of({
13 address: { _tag: "TcpAddress", hostname, port },
14 serve: (() => Effect.void) as HttpServer.HttpServer["Service"]["serve"],
15 });
16const fakeHttpServer = makeFakeHttpServer("127.0.0.1");
17const fakeEnvironment = ServerEnvironment.ServerEnvironment.of({
18 getEnvironmentId: Effect.succeed(environmentId),
19 getDescriptor: Effect.die("unused"),
20});
21
22const makeRegistry = (now: () => number, httpServer = fakeHttpServer) =>
23 McpSessionRegistry.__testing
24 .make({
25 now,
26 livenessWindowMs: 100,
27 })
28 .pipe(
29 Effect.provideService(HttpServer.HttpServer, httpServer),
30 Effect.provideService(ServerEnvironment.ServerEnvironment, fakeEnvironment),
31 Effect.provide(NodeServices.layer),
32 );
33
34it.effect("stores only a token hash, resolves the bearer token, and revokes by thread", () =>
35 Effect.gen(function* () {
36 let timestamp = 1_000;
37 const registry = yield* makeRegistry(() => timestamp);
38 const threadId = ThreadId.make("thread-1");
39 const issued = yield* registry.issue({
40 threadId,
41 providerInstanceId: ProviderInstanceId.make("codex"),
42 capabilities: new Set(["preview"]),
43 });
44 expect(issued.config.endpoint).toBe("http://127.0.0.1:43123/mcp");
45 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
46 expect(token.length).toBeGreaterThan(20);
47
48 const resolved = yield* registry.resolve(token);
49 expect(resolved?.threadId).toBe(threadId);
50
51 yield* registry.revokeThread(threadId);
52 expect(yield* registry.resolve(token)).toBeUndefined();
53
54 timestamp += 2_000;
55 }),
56);
57
58it.effect(
59 "always grants pull-requests and threads and gates browser and device access independently",
60 () =>
61 Effect.gen(function* () {
62 const registry = yield* makeRegistry(() => 1_000);
63 const withPreview = yield* registry.issue({
64 threadId: ThreadId.make("thread-preview"),
65 providerInstanceId: ProviderInstanceId.make("codex"),
66 capabilities: new Set(["preview"]),
67 });
68 const withoutPreview = yield* registry.issue({
69 threadId: ThreadId.make("thread-no-preview"),
70 providerInstanceId: ProviderInstanceId.make("codex"),
71 capabilities: new Set(),
72 });
73 const withDevice = yield* registry.issue({
74 threadId: ThreadId.make("thread-device"),
75 providerInstanceId: ProviderInstanceId.make("codex"),
76 capabilities: new Set(["device"]),
77 });
78 const capabilitiesOf = (issued: typeof withPreview) =>
79 registry
80 .resolve(issued.config.authorizationHeader.replace(/^Bearer\s+/, ""))
81 .pipe(Effect.map((scope) => [...(scope?.capabilities ?? [])].sort()));
82
83 expect(yield* capabilitiesOf(withPreview)).toEqual(["preview", "pull-requests", "threads"]);
84 expect(yield* capabilitiesOf(withoutPreview)).toEqual(["pull-requests", "threads"]);
85 expect(yield* capabilitiesOf(withDevice)).toEqual(["device", "pull-requests", "threads"]);
86 }),
87);
88
89it.effect("builds MCP endpoints from the bound server host", () =>
90 Effect.gen(function* () {
91 const cases = [
92 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
93 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
94 ["localhost", "http://localhost:43123/mcp"],
95 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
96 ] as const;
97
98 for (const [hostname, expectedEndpoint] of cases) {
99 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
100 const issued = yield* registry.issue({
101 threadId: ThreadId.make(`thread-${hostname}`),
102 providerInstanceId: ProviderInstanceId.make("codex"),
103 capabilities: new Set(["preview"]),
104 });
105 expect(issued.config.endpoint).toBe(expectedEndpoint);
106 }
107 }),
108);
109
110it.effect("expires credentials once their session stops showing signs of life", () =>
111 Effect.gen(function* () {
112 let timestamp = 1_000;
113 const registry = yield* makeRegistry(() => timestamp);
114 const issued = yield* registry.issue({
115 threadId: ThreadId.make("thread-2"),
116 providerInstanceId: ProviderInstanceId.make("claude"),
117 capabilities: new Set(["preview"]),
118 });
119 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
120 timestamp += 101;
121 expect(yield* registry.resolve(token)).toBeUndefined();
122 }),
123);
124
125it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
126 Effect.gen(function* () {
127 let timestamp = 1_000;
128 const registry = yield* makeRegistry(() => timestamp);
129 const threadId = ThreadId.make("thread-3");
130 const issued = yield* registry.issue({
131 threadId,
132 providerInstanceId: ProviderInstanceId.make("claude"),
133 capabilities: new Set(["preview"]),
134 });
135 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
136
137 // Well past the liveness window in total, but each turn reports in before
138 // it lapses — this is the long-session case that used to lose the toolkit.
139 for (let turn = 0; turn < 10; turn += 1) {
140 timestamp += 99;
141 yield* registry.touch(threadId);
142 }
143
144 expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
145 }),
146);
147
148it.effect("does not keep credentials of other threads alive", () =>
149 Effect.gen(function* () {
150 let timestamp = 1_000;
151 const registry = yield* makeRegistry(() => timestamp);
152 const issued = yield* registry.issue({
153 threadId: ThreadId.make("thread-4"),
154 providerInstanceId: ProviderInstanceId.make("codex"),
155 capabilities: new Set(["preview"]),
156 });
157 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
158
159 timestamp += 99;
160 yield* registry.touch(ThreadId.make("thread-unrelated"));
161 timestamp += 2;
162
163 expect(yield* registry.resolve(token)).toBeUndefined();
164 }),
165);