TypeScript
fix(server): t3-code tools work again after a thread sits idle for a day (#19491)
Will Sheldon22ad90438238
diff --git a/apps/server/src/mcp/McpSessionRegistry.test.ts b/apps/server/src/mcp/McpSessionRegistry.test.tsindex a3f74c3e28c..94f6aa67779 100644--- a/apps/server/src/mcp/McpSessionRegistry.test.ts+++ b/apps/server/src/mcp/McpSessionRegistry.test.ts@@ -178,3 +178,65 @@ it.effect("does not keep credentials of other threads alive", () =>expect(yield* registry.resolve(token)).toBeUndefined();}),);++it.effect("only a trusted touch restores an expired token with its original scope", () =>+ Effect.gen(function* () {+ let timestamp = 1_000;+ const registry = yield* makeRegistry(() => timestamp);+ const threadId = ThreadId.make("thread-idle");+ const issued = yield* registry.issue({+ threadId,+ providerInstanceId: ProviderInstanceId.make("claude"),+ capabilities: new Set(["preview"]),+ });+ const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");+ const scope = yield* registry.resolve(token);+ expect(scope).toBeDefined();++ timestamp += 101;+ expect(yield* registry.resolve(token)).toBeUndefined();+ const otherThreadId = ThreadId.make("thread-active");+ const other = yield* registry.issue({+ threadId: otherThreadId,+ providerInstanceId: ProviderInstanceId.make("codex"),+ capabilities: new Set(["device"]),+ });+ yield* registry.touch(otherThreadId);+ expect(+ (yield* registry.resolve(other.config.authorizationHeader.replace(/^Bearer\s+/, "")))?.thread+ .threadId,+ ).toBe(otherThreadId);+ expect(yield* registry.resolve("unknown-token")).toBeUndefined();+ expect(yield* registry.resolve(token)).toBeUndefined();++ yield* registry.touch(threadId);+ expect(yield* registry.resolve(token)).toEqual(scope);+ timestamp += 101;+ expect(yield* registry.resolve(token)).toBeUndefined();+ }),+);++it.effect.each(["provider-session", "thread", "all"] as const)(+ "never restores an expired token after %s revocation",+ (revocation) =>+ Effect.gen(function* () {+ let timestamp = 1_000;+ const registry = yield* makeRegistry(() => timestamp);+ const threadId = ThreadId.make("thread-revoked");+ const issued = yield* registry.issue({+ threadId,+ providerInstanceId: ProviderInstanceId.make("codex"),+ capabilities: new Set(["preview"]),+ });+ const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");++ timestamp += 101;+ yield* {+ "provider-session": registry.revokeProviderSession(issued.config.providerSessionId),+ thread: registry.revokeThread(threadId),+ all: registry.revokeAll,+ }[revocation];+ yield* registry.touch(threadId);+ expect(yield* registry.resolve(token)).toBeUndefined();+ }),+);diff --git a/apps/server/src/mcp/McpSessionRegistry.ts b/apps/server/src/mcp/McpSessionRegistry.tsindex b16529924a4..ae05529cd25 100644--- a/apps/server/src/mcp/McpSessionRegistry.ts+++ b/apps/server/src/mcp/McpSessionRegistry.ts@@ -60,9 +60,9 @@ export interface McpSessionRegistryOptions {}/**- * The bound matters because `/mcp` is mounted outside the environment auth- * stack and is reachable on whatever host the server binds to, so this token is- * the only thing guarding the `t3-code` toolkits on a remote-reachable server.+ * `/mcp` is outside environment auth, so idle tokens stop authenticating.+ * Keep expired records until explicit revocation: a trusted provider turn can+ * refresh the token its subprocess still holds; MCP requests cannot.*/const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000;@@ -95,15 +95,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (.digest("SHA-256", new TextEncoder().encode(token)).pipe(Effect.map(bytesToHex), Effect.orDie);- const pruneDead = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {- const next = new Map(- Array.from(records).filter(- ([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs,- ),- );- return next.size === records.size ? records : next;- };-const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")(function* (request) {const issuedAt = yield* currentTimeMillis;@@ -129,7 +120,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (issuedAt,};yield* SynchronizedRef.update(state, ({ records }) => {- const next = new Map(pruneDead(records, issuedAt));+ const next = new Map(records);next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt });return { records: next };});@@ -154,10 +145,11 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (const tokenHash = yield* hashToken(rawToken);const timestamp = yield* currentTimeMillis;return yield* SynchronizedRef.modify(state, ({ records }) => {- const current = pruneDead(records, timestamp);- const record = current.get(tokenHash);- if (!record) return [undefined, { records: current }] as const;- const next = new Map(current);+ const record = records.get(tokenHash);+ if (!record || timestamp - record.lastAliveAt > livenessWindowMs) {+ return [undefined, { records }] as const;+ }+ const next = new Map(records);next.set(tokenHash, { ...record, lastAliveAt: timestamp });return [record.scope, { records: next }] as const;});@@ -168,9 +160,8 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (function* (threadId) {const timestamp = yield* currentTimeMillis;yield* SynchronizedRef.update(state, ({ records }) => {- const current = pruneDead(records, timestamp);- const next = new Map(current);- for (const [tokenHash, record] of current) {+ const next = new Map(records);+ for (const [tokenHash, record] of records) {if (record.scope.thread.threadId === threadId) {next.set(tokenHash, { ...record, lastAliveAt: timestamp });}