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("always grants pull-requests and gates browser and device access independently", () =>
59 Effect.gen(function* () {
60 const registry = yield* makeRegistry(() => 1_000);
61 const withPreview = yield* registry.issue({
62 threadId: ThreadId.make("thread-preview"),
63 providerInstanceId: ProviderInstanceId.make("codex"),
64 capabilities: new Set(["preview"]),
65 });
66 const withoutPreview = yield* registry.issue({
67 threadId: ThreadId.make("thread-no-preview"),
68 providerInstanceId: ProviderInstanceId.make("codex"),
69 capabilities: new Set(),
70 });
71 const withDevice = yield* registry.issue({
72 threadId: ThreadId.make("thread-device"),
73 providerInstanceId: ProviderInstanceId.make("codex"),
74 capabilities: new Set(["device"]),
75 });
76 const capabilitiesOf = (issued: typeof withPreview) =>
77 registry
78 .resolve(issued.config.authorizationHeader.replace(/^Bearer\s+/, ""))
79 .pipe(Effect.map((scope) => [...(scope?.capabilities ?? [])].sort()));
80
81 expect(yield* capabilitiesOf(withPreview)).toEqual(["preview", "pull-requests"]);
82 expect(yield* capabilitiesOf(withoutPreview)).toEqual(["pull-requests"]);
83 expect(yield* capabilitiesOf(withDevice)).toEqual(["device", "pull-requests"]);
84 }),
85);
86
87it.effect("builds MCP endpoints from the bound server host", () =>
88 Effect.gen(function* () {
89 const cases = [
90 ["100.64.0.40", "http://100.64.0.40:43123/mcp"],
91 ["0.0.0.0", "http://127.0.0.1:43123/mcp"],
92 ["localhost", "http://localhost:43123/mcp"],
93 ["127.0.0.1", "http://127.0.0.1:43123/mcp"],
94 ] as const;
95
96 for (const [hostname, expectedEndpoint] of cases) {
97 const registry = yield* makeRegistry(() => 1_000, makeFakeHttpServer(hostname));
98 const issued = yield* registry.issue({
99 threadId: ThreadId.make(`thread-${hostname}`),
100 providerInstanceId: ProviderInstanceId.make("codex"),
101 capabilities: new Set(["preview"]),
102 });
103 expect(issued.config.endpoint).toBe(expectedEndpoint);
104 }
105 }),
106);
107
108it.effect("expires credentials once their session stops showing signs of life", () =>
109 Effect.gen(function* () {
110 let timestamp = 1_000;
111 const registry = yield* makeRegistry(() => timestamp);
112 const issued = yield* registry.issue({
113 threadId: ThreadId.make("thread-2"),
114 providerInstanceId: ProviderInstanceId.make("claude"),
115 capabilities: new Set(["preview"]),
116 });
117 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
118 timestamp += 101;
119 expect(yield* registry.resolve(token)).toBeUndefined();
120 }),
121);
122
123it.effect("keeps a credential alive across turns that never touch an MCP tool", () =>
124 Effect.gen(function* () {
125 let timestamp = 1_000;
126 const registry = yield* makeRegistry(() => timestamp);
127 const threadId = ThreadId.make("thread-3");
128 const issued = yield* registry.issue({
129 threadId,
130 providerInstanceId: ProviderInstanceId.make("claude"),
131 capabilities: new Set(["preview"]),
132 });
133 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
134
135 // Well past the liveness window in total, but each turn reports in before
136 // it lapses — this is the long-session case that used to lose the toolkit.
137 for (let turn = 0; turn < 10; turn += 1) {
138 timestamp += 99;
139 yield* registry.touch(threadId);
140 }
141
142 expect((yield* registry.resolve(token))?.threadId).toBe(threadId);
143 }),
144);
145
146it.effect("does not keep credentials of other threads alive", () =>
147 Effect.gen(function* () {
148 let timestamp = 1_000;
149 const registry = yield* makeRegistry(() => timestamp);
150 const issued = yield* registry.issue({
151 threadId: ThreadId.make("thread-4"),
152 providerInstanceId: ProviderInstanceId.make("codex"),
153 capabilities: new Set(["preview"]),
154 });
155 const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
156
157 timestamp += 99;
158 yield* registry.touch(ThreadId.make("thread-unrelated"));
159 timestamp += 2;
160
161 expect(yield* registry.resolve(token)).toBeUndefined();
162 }),
163);