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