TypeScript
fix(server): terminals open for threads with long ids (#18669)
Batuhan1999fe949fe1bc8b
diff --git a/apps/server/src/terminal/Manager.test.ts b/apps/server/src/terminal/Manager.test.tsindex d7babfbeac8..94af3c5bfca 100644--- a/apps/server/src/terminal/Manager.test.ts+++ b/apps/server/src/terminal/Manager.test.ts@@ -2081,6 +2081,53 @@ it.layer(}),);+ it.effect.each([+ {+ label: "subagent",+ threadId:+ "thread:provider:claudeAgent:native-thread:provider-thread%3Aprovider%3AclaudeAgent%3Anative-thread%3Apending%253Arun%253Athread%253A9175fb53-52d7-4d4d-9be7-25a76cf84b91%253Aordinal%253A7%3Aad899ae84c7a131b0",+ },+ // Also too long for the legacy `<sanitized id>.log` lookup.+ { label: "300-character", threadId: "x".repeat(300) },+ ])("persists history for a $label thread id too long to encode into a filename", ({ threadId }) =>+ Effect.gen(function* () {+ const terminalId = "t".repeat(128);+ const { manager, ptyAdapter, logsDir } = yield* createManager();+ const fs = yield* FileSystem.FileSystem;+ yield* manager.open(openInput({ threadId }));+ yield* manager.open(openInput({ threadId, terminalId }));+ ptyAdapter.processes[0]!.emitData("default\n");+ ptyAdapter.processes[1]!.emitData("long\n");+ yield* manager.close({ threadId });++ expect((yield* manager.open(openInput({ threadId }))).history).toBe("default\n");+ expect((yield* manager.open(openInput({ threadId, terminalId }))).history).toBe("long\n");++ yield* manager.close({ threadId, deleteHistory: true });+ expect(yield* fs.readDirectory(logsDir)).toEqual([]);+ }),+ );++ it.effect("keeps reading and deleting history at an encoded path that already fit", () =>+ Effect.gen(function* () {+ const threadId = "x".repeat(120);+ const { manager, logsDir } = yield* createManager();+ const fs = yield* FileSystem.FileSystem;+ const logPath = yield* historyLogPath(logsDir, threadId);+ yield* fs.makeDirectory(logsDir, { recursive: true });+ yield* fs.writeFileString(logPath, "before upgrade\n");++ expect((yield* manager.open(openInput({ threadId }))).history).toBe("before upgrade\n");+ // A long terminal id pushes only this terminal's name past the limit, so the thread's+ // history is split across both forms and deleting the thread must remove both.+ yield* manager.open(openInput({ threadId, terminalId: "t".repeat(128) }));++ yield* manager.close({ threadId, deleteHistory: true });+ expect(yield* fs.exists(logPath)).toBe(false);+ expect(yield* fs.readDirectory(logsDir)).toEqual([]);+ }),+ );+it.effect("closes all terminals for a thread when close omits terminalId", () =>Effect.gen(function* () {const { manager, ptyAdapter, logsDir } = yield* createManager();diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.tsindex e10d263dc33..fc798714b58 100644--- a/apps/server/src/terminal/Manager.ts+++ b/apps/server/src/terminal/Manager.ts@@ -1,3 +1,6 @@+// @effect-diagnostics-next-line nodeBuiltinImport:off -- Effect's Crypto digest is effectful; a sync hash keeps historyPath a plain function.+import * as NodeCrypto from "node:crypto";+import { withWorkspaceLease } from "../workspace/workspaceLease.ts";import {DEFAULT_TERMINAL_ID,@@ -1253,12 +1256,23 @@ function legacySafeThreadId(threadId: string): string {return threadId.replace(/[^a-zA-Z0-9._-]/g, "_");}-function toSafeThreadId(threadId: string): string {+const MAX_FILE_NAME_BYTES = 255;++function encodedHistoryThreadPart(threadId: string): string {return `terminal_${Base64Url.encode(threadId)}`;}-function toSafeTerminalId(terminalId: string): string {- return Base64Url.encode(terminalId);+function hashId(id: string): string {+ return NodeCrypto.createHash("sha256").update(id).digest("base64url");+}++// "." is outside the base64url alphabet, so a hashed name can never collide with an encoded one.+function hashedHistoryThreadPart(threadId: string): string {+ return `terminal_sha256.${hashId(threadId)}`;+}++function joinHistoryFileName(threadPart: string, terminalPart: string | null): string {+ return terminalPart === null ? `${threadPart}.log` : `${threadPart}_${terminalPart}.log`;}function toSessionKey(threadId: string, terminalId: string): string {@@ -1599,16 +1613,28 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func}});+ // Nested subagent thread ids can encode past the filename limit. Only names that would not fit+ // switch to hashed ids, so every history file that already exists keeps its path.const historyPath = (threadId: string, terminalId: string) => {- const threadPart = toSafeThreadId(threadId);- if (terminalId === DEFAULT_TERMINAL_ID) {- return path.join(logsDir, `${threadPart}.log`);+ const isDefault = terminalId === DEFAULT_TERMINAL_ID;+ const encodedName = joinHistoryFileName(+ encodedHistoryThreadPart(threadId),+ isDefault ? null : Base64Url.encode(terminalId),+ );+ if (encodedName.length <= MAX_FILE_NAME_BYTES) {+ return path.join(logsDir, encodedName);}- return path.join(logsDir, `${threadPart}_${toSafeTerminalId(terminalId)}.log`);+ return path.join(+ logsDir,+ joinHistoryFileName(hashedHistoryThreadPart(threadId), isDefault ? null : hashId(terminalId)),+ );};- const legacyHistoryPath = (threadId: string) =>- path.join(logsDir, `${legacySafeThreadId(threadId)}.log`);+ const legacyHistoryName = (threadId: string) => `${legacySafeThreadId(threadId)}.log`;+ const legacyHistoryPath = (threadId: string) => path.join(logsDir, legacyHistoryName(threadId));+ // Ids this long could never have been written under the legacy name.+ const hasLegacyHistoryPath = (threadId: string) =>+ Buffer.byteLength(legacyHistoryName(threadId)) <= MAX_FILE_NAME_BYTES;const readManagerState = SynchronizedRef.get(managerStateRef);@@ -1871,7 +1897,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(funcreturn history;}- if (terminalId !== DEFAULT_TERMINAL_ID) {+ if (terminalId !== DEFAULT_TERMINAL_ID || !hasLegacyHistoryPath(threadId)) {return new BoundedTerminalHistory(historyLineLimit, "", historyByteLimit);}@@ -1929,7 +1955,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func}),),);- if (terminalId === DEFAULT_TERMINAL_ID) {+ if (terminalId === DEFAULT_TERMINAL_ID && hasLegacyHistoryPath(threadId)) {yield* fileSystem.remove(legacyHistoryPath(threadId), { force: true }).pipe(Effect.catch((error) =>Effect.logWarning("failed to delete terminal history", {@@ -1945,16 +1971,15 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(funcconst deleteAllHistoryForThread = Effect.fn("terminal.deleteAllHistoryForThread")(function* (threadId: string,) {- const threadPrefix = `${toSafeThreadId(threadId)}_`;+ const threadParts = [encodedHistoryThreadPart(threadId), hashedHistoryThreadPart(threadId)];const entries = yield* fileSystem.readDirectory(logsDir, { recursive: false }).pipe(Effect.orElseSucceed(() => [] as Array<string>));yield* Effect.forEach(entries.filter((name) =>- name === `${toSafeThreadId(threadId)}.log` ||name === `${legacySafeThreadId(threadId)}.log` ||- name.startsWith(threadPrefix),+ threadParts.some((part) => name === `${part}.log` || name.startsWith(`${part}_`)),),(name) =>fileSystem.remove(path.join(logsDir, name), { force: true }).pipe(