@devries-cameron20 / t3code-1 / commits / 22ad904382
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): t3-code tools work again after a thread sits idle for a day (#19491)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Will Sheldon committed Oct 19, 2026 at 21:11 UTC 22ad90438238680642be4ad0ead3ac895512b1f5
2 files changed +73 -20
apps/server/src/mcp/McpSessionRegistry.test.ts
+62
@@ -178,3 +178,65 @@ it.effect("does not keep credentials of other threads alive", () =>
178 expect(yield* registry.resolve(token)).toBeUndefined();
179 }),
180 );
181 +
182 +it.effect("only a trusted touch restores an expired token with its original scope", () =>
183 + Effect.gen(function* () {
184 + let timestamp = 1_000;
185 + const registry = yield* makeRegistry(() => timestamp);
186 + const threadId = ThreadId.make("thread-idle");
187 + const issued = yield* registry.issue({
188 + threadId,
189 + providerInstanceId: ProviderInstanceId.make("claude"),
190 + capabilities: new Set(["preview"]),
191 + });
192 + const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
193 + const scope = yield* registry.resolve(token);
194 + expect(scope).toBeDefined();
195 +
196 + timestamp += 101;
197 + expect(yield* registry.resolve(token)).toBeUndefined();
198 + const otherThreadId = ThreadId.make("thread-active");
199 + const other = yield* registry.issue({
200 + threadId: otherThreadId,
201 + providerInstanceId: ProviderInstanceId.make("codex"),
202 + capabilities: new Set(["device"]),
203 + });
204 + yield* registry.touch(otherThreadId);
205 + expect(
206 + (yield* registry.resolve(other.config.authorizationHeader.replace(/^Bearer\s+/, "")))?.thread
207 + .threadId,
208 + ).toBe(otherThreadId);
209 + expect(yield* registry.resolve("unknown-token")).toBeUndefined();
210 + expect(yield* registry.resolve(token)).toBeUndefined();
211 +
212 + yield* registry.touch(threadId);
213 + expect(yield* registry.resolve(token)).toEqual(scope);
214 + timestamp += 101;
215 + expect(yield* registry.resolve(token)).toBeUndefined();
216 + }),
217 +);
218 +
219 +it.effect.each(["provider-session", "thread", "all"] as const)(
220 + "never restores an expired token after %s revocation",
221 + (revocation) =>
222 + Effect.gen(function* () {
223 + let timestamp = 1_000;
224 + const registry = yield* makeRegistry(() => timestamp);
225 + const threadId = ThreadId.make("thread-revoked");
226 + const issued = yield* registry.issue({
227 + threadId,
228 + providerInstanceId: ProviderInstanceId.make("codex"),
229 + capabilities: new Set(["preview"]),
230 + });
231 + const token = issued.config.authorizationHeader.replace(/^Bearer\s+/, "");
232 +
233 + timestamp += 101;
234 + yield* {
235 + "provider-session": registry.revokeProviderSession(issued.config.providerSessionId),
236 + thread: registry.revokeThread(threadId),
237 + all: registry.revokeAll,
238 + }[revocation];
239 + yield* registry.touch(threadId);
240 + expect(yield* registry.resolve(token)).toBeUndefined();
241 + }),
242 +);
apps/server/src/mcp/McpSessionRegistry.ts
+11 -20
@@ -60,9 +60,9 @@ export interface McpSessionRegistryOptions {
60 }
61
62 /**
63 - * The bound matters because `/mcp` is mounted outside the environment auth
64 - * stack and is reachable on whatever host the server binds to, so this token is
65 - * the only thing guarding the `t3-code` toolkits on a remote-reachable server.
63 + * `/mcp` is outside environment auth, so idle tokens stop authenticating.
64 + * Keep expired records until explicit revocation: a trusted provider turn can
65 + * refresh the token its subprocess still holds; MCP requests cannot.
66 */
67 const DEFAULT_LIVENESS_WINDOW_MS = 24 * 60 * 60 * 1_000;
68
@@ -95,15 +95,6 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
95 .digest("SHA-256", new TextEncoder().encode(token))
96 .pipe(Effect.map(bytesToHex), Effect.orDie);
97
98 - const pruneDead = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
99 - const next = new Map(
100 - Array.from(records).filter(
101 - ([, record]) => timestamp - record.lastAliveAt <= livenessWindowMs,
102 - ),
103 - );
104 - return next.size === records.size ? records : next;
105 - };
106 -
98 const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")(
99 function* (request) {
100 const issuedAt = yield* currentTimeMillis;
@@ -129,7 +120,7 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
120 issuedAt,
121 };
122 yield* SynchronizedRef.update(state, ({ records }) => {
132 - const next = new Map(pruneDead(records, issuedAt));
123 + const next = new Map(records);
124 next.set(tokenHash, { tokenHash, scope, lastAliveAt: issuedAt });
125 return { records: next };
126 });
@@ -154,10 +145,11 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
145 const tokenHash = yield* hashToken(rawToken);
146 const timestamp = yield* currentTimeMillis;
147 return yield* SynchronizedRef.modify(state, ({ records }) => {
157 - const current = pruneDead(records, timestamp);
158 - const record = current.get(tokenHash);
159 - if (!record) return [undefined, { records: current }] as const;
160 - const next = new Map(current);
148 + const record = records.get(tokenHash);
149 + if (!record || timestamp - record.lastAliveAt > livenessWindowMs) {
150 + return [undefined, { records }] as const;
151 + }
152 + const next = new Map(records);
153 next.set(tokenHash, { ...record, lastAliveAt: timestamp });
154 return [record.scope, { records: next }] as const;
155 });
@@ -168,9 +160,8 @@ const makeWithOptions = Effect.fn("McpSessionRegistry.make")(function* (
160 function* (threadId) {
161 const timestamp = yield* currentTimeMillis;
162 yield* SynchronizedRef.update(state, ({ records }) => {
171 - const current = pruneDead(records, timestamp);
172 - const next = new Map(current);
173 - for (const [tokenHash, record] of current) {
163 + const next = new Map(records);
164 + for (const [tokenHash, record] of records) {
165 if (record.scope.thread.threadId === threadId) {
166 next.set(tokenHash, { ...record, lastAliveAt: timestamp });
167 }