@devries-cameron20 / t3code Mirror · read-only / commits / fe949fe1bc
Fork 0

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 22, 23:15 UTC

fix(server): terminals open for threads with long ids (#18669)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Batuhan1999 committed Oct 20, 2026 at 06:17 UTC fe949fe1bc8bc0fe25fcd77059a44db72839b467
2 files changed +86 -14
apps/server/src/terminal/Manager.test.ts
+47
@@ -2081,6 +2081,53 @@ it.layer(
2081 }),
2082 );
2083
2084 + it.effect.each([
2085 + {
2086 + label: "subagent",
2087 + threadId:
2088 + "thread:provider:claudeAgent:native-thread:provider-thread%3Aprovider%3AclaudeAgent%3Anative-thread%3Apending%253Arun%253Athread%253A9175fb53-52d7-4d4d-9be7-25a76cf84b91%253Aordinal%253A7%3Aad899ae84c7a131b0",
2089 + },
2090 + // Also too long for the legacy `<sanitized id>.log` lookup.
2091 + { label: "300-character", threadId: "x".repeat(300) },
2092 + ])("persists history for a $label thread id too long to encode into a filename", ({ threadId }) =>
2093 + Effect.gen(function* () {
2094 + const terminalId = "t".repeat(128);
2095 + const { manager, ptyAdapter, logsDir } = yield* createManager();
2096 + const fs = yield* FileSystem.FileSystem;
2097 + yield* manager.open(openInput({ threadId }));
2098 + yield* manager.open(openInput({ threadId, terminalId }));
2099 + ptyAdapter.processes[0]!.emitData("default\n");
2100 + ptyAdapter.processes[1]!.emitData("long\n");
2101 + yield* manager.close({ threadId });
2102 +
2103 + expect((yield* manager.open(openInput({ threadId }))).history).toBe("default\n");
2104 + expect((yield* manager.open(openInput({ threadId, terminalId }))).history).toBe("long\n");
2105 +
2106 + yield* manager.close({ threadId, deleteHistory: true });
2107 + expect(yield* fs.readDirectory(logsDir)).toEqual([]);
2108 + }),
2109 + );
2110 +
2111 + it.effect("keeps reading and deleting history at an encoded path that already fit", () =>
2112 + Effect.gen(function* () {
2113 + const threadId = "x".repeat(120);
2114 + const { manager, logsDir } = yield* createManager();
2115 + const fs = yield* FileSystem.FileSystem;
2116 + const logPath = yield* historyLogPath(logsDir, threadId);
2117 + yield* fs.makeDirectory(logsDir, { recursive: true });
2118 + yield* fs.writeFileString(logPath, "before upgrade\n");
2119 +
2120 + expect((yield* manager.open(openInput({ threadId }))).history).toBe("before upgrade\n");
2121 + // A long terminal id pushes only this terminal's name past the limit, so the thread's
2122 + // history is split across both forms and deleting the thread must remove both.
2123 + yield* manager.open(openInput({ threadId, terminalId: "t".repeat(128) }));
2124 +
2125 + yield* manager.close({ threadId, deleteHistory: true });
2126 + expect(yield* fs.exists(logPath)).toBe(false);
2127 + expect(yield* fs.readDirectory(logsDir)).toEqual([]);
2128 + }),
2129 + );
2130 +
2131 it.effect("closes all terminals for a thread when close omits terminalId", () =>
2132 Effect.gen(function* () {
2133 const { manager, ptyAdapter, logsDir } = yield* createManager();
apps/server/src/terminal/Manager.ts
+39 -14
@@ -1,3 +1,6 @@
1 +// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto digest is effectful; a sync hash keeps historyPath a plain function.
2 +import * as NodeCrypto from "node:crypto";
3 +
4 import { withWorkspaceLease } from "../workspace/workspaceLease.ts";
5 import {
6 DEFAULT_TERMINAL_ID,
@@ -1253,12 +1256,23 @@ function legacySafeThreadId(threadId: string): string {
1256 return threadId.replace(/[^a-zA-Z0-9._-]/g, "_");
1257 }
1258
1256 -function toSafeThreadId(threadId: string): string {
1259 +const MAX_FILE_NAME_BYTES = 255;
1260 +
1261 +function encodedHistoryThreadPart(threadId: string): string {
1262 return `terminal_${Base64Url.encode(threadId)}`;
1263 }
1264
1260 -function toSafeTerminalId(terminalId: string): string {
1261 - return Base64Url.encode(terminalId);
1265 +function hashId(id: string): string {
1266 + return NodeCrypto.createHash("sha256").update(id).digest("base64url");
1267 +}
1268 +
1269 +// "." is outside the base64url alphabet, so a hashed name can never collide with an encoded one.
1270 +function hashedHistoryThreadPart(threadId: string): string {
1271 + return `terminal_sha256.${hashId(threadId)}`;
1272 +}
1273 +
1274 +function joinHistoryFileName(threadPart: string, terminalPart: string | null): string {
1275 + return terminalPart === null ? `${threadPart}.log` : `${threadPart}_${terminalPart}.log`;
1276 }
1277
1278 function toSessionKey(threadId: string, terminalId: string): string {
@@ -1599,16 +1613,28 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
1613 }
1614 });
1615
1616 + // Nested subagent thread ids can encode past the filename limit. Only names that would not fit
1617 + // switch to hashed ids, so every history file that already exists keeps its path.
1618 const historyPath = (threadId: string, terminalId: string) => {
1603 - const threadPart = toSafeThreadId(threadId);
1604 - if (terminalId === DEFAULT_TERMINAL_ID) {
1605 - return path.join(logsDir, `${threadPart}.log`);
1619 + const isDefault = terminalId === DEFAULT_TERMINAL_ID;
1620 + const encodedName = joinHistoryFileName(
1621 + encodedHistoryThreadPart(threadId),
1622 + isDefault ? null : Base64Url.encode(terminalId),
1623 + );
1624 + if (encodedName.length <= MAX_FILE_NAME_BYTES) {
1625 + return path.join(logsDir, encodedName);
1626 }
1607 - return path.join(logsDir, `${threadPart}_${toSafeTerminalId(terminalId)}.log`);
1627 + return path.join(
1628 + logsDir,
1629 + joinHistoryFileName(hashedHistoryThreadPart(threadId), isDefault ? null : hashId(terminalId)),
1630 + );
1631 };
1632
1610 - const legacyHistoryPath = (threadId: string) =>
1611 - path.join(logsDir, `${legacySafeThreadId(threadId)}.log`);
1633 + const legacyHistoryName = (threadId: string) => `${legacySafeThreadId(threadId)}.log`;
1634 + const legacyHistoryPath = (threadId: string) => path.join(logsDir, legacyHistoryName(threadId));
1635 + // Ids this long could never have been written under the legacy name.
1636 + const hasLegacyHistoryPath = (threadId: string) =>
1637 + Buffer.byteLength(legacyHistoryName(threadId)) <= MAX_FILE_NAME_BYTES;
1638
1639 const readManagerState = SynchronizedRef.get(managerStateRef);
1640
@@ -1871,7 +1897,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
1897 return history;
1898 }
1899
1874 - if (terminalId !== DEFAULT_TERMINAL_ID) {
1900 + if (terminalId !== DEFAULT_TERMINAL_ID || !hasLegacyHistoryPath(threadId)) {
1901 return new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit);
1902 }
1903
@@ -1929,7 +1955,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
1955 }),
1956 ),
1957 );
1932 - if (terminalId === DEFAULT_TERMINAL_ID) {
1958 + if (terminalId === DEFAULT_TERMINAL_ID && hasLegacyHistoryPath(threadId)) {
1959 yield* fileSystem.remove(legacyHistoryPath(threadId), { force: true }).pipe(
1960 Effect.catch((error) =>
1961 Effect.logWarning("failed to delete terminal history", {
@@ -1945,16 +1971,15 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
1971 const deleteAllHistoryForThread = Effect.fn("terminal.deleteAllHistoryForThread")(function* (
1972 threadId: string,
1973 ) {
1948 - const threadPrefix = `${toSafeThreadId(threadId)}_`;
1974 + const threadParts = [encodedHistoryThreadPart(threadId), hashedHistoryThreadPart(threadId)];
1975 const entries = yield* fileSystem
1976 .readDirectory(logsDir, { recursive: false })
1977 .pipe(Effect.orElseSucceed(() => [] as Array<string>));
1978 yield* Effect.forEach(
1979 entries.filter(
1980 (name) =>
1955 - name === `${toSafeThreadId(threadId)}.log` ||
1981 name === `${legacySafeThreadId(threadId)}.log` ||
1957 - name.startsWith(threadPrefix),
1982 + threadParts.some((part) => name === `${part}.log` || name.startsWith(`${part}_`)),
1983 ),
1984 (name) =>
1985 fileSystem.remove(path.join(logsDir, name), { force: true }).pipe(