Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(claude): key the transcript folder the way the installed CLI does
刘伟3abbd7141c59
diff --git a/server/drivers/claude-transcript-images.test.ts b/server/drivers/claude-transcript-images.test.tsindex f5434787bb..43676ea4cf 100644--- a/server/drivers/claude-transcript-images.test.ts+++ b/server/drivers/claude-transcript-images.test.ts@@ -1,7 +1,7 @@import assert from "node:assert/strict";-import { mkdirSync, mkdtempSync, readFileSync, statSync, symlinkSync, writeFileSync } from "node:fs";+import { mkdirSync, mkdtempSync, readFileSync, realpathSync, statSync, symlinkSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";-import { join } from "node:path";+import { basename, dirname, join } from "node:path";import { test } from "vitest";import { CLAUDE_IMAGE_KEEP, CLAUDE_IMAGE_LIMIT, foldTranscriptImages, imagesToKeep, transcriptImagesOver, transcriptPath } from "./claude-transcript-images.ts";@@ -100,19 +100,39 @@ test("the transcript is found where Claude Code writes it: a linked working direconst base = mkdtempSync(join(tmpdir(), "claude-images-"));const configDir = join(base, ".claude-api");const rows = Array.from({ length: 21 }, (_, i) => userRow([image(i)], i)).join("\n");- // the folder named after the resolved directory, built here by the CLI's own rule rather than through transcriptPath+ // the folder named after the resolved directory (the CLI resolves links, on macOS even /tmp to /private/tmp), built+ // here by the CLI's own rule rather than through transcriptPathconst real = join(base, "real");mkdirSync(real);const link = join(base, "link");symlinkSync(real, link);- const linked = join(configDir, "projects", real.replace(/[^A-Za-z0-9]/g, "-"));+ const linked = join(configDir, "projects", realpathSync.native(real).replace(/[^A-Za-z0-9]/g, "-"));mkdirSync(linked, { recursive: true });writeFileSync(join(linked, "s1.jsonl"), rows);assert.equal(transcriptImagesOver({ configDir, cwd: link, sessionId: "s1" }), true);assert.equal(foldTranscriptImages({ configDir, cwd: link, sessionId: "s1" }).folded, 21 - CLAUDE_IMAGE_KEEP);- // a long working directory: cut at 200 characters, then '-' and the CLI's hash of the full path- const long = "/" + "deep/".repeat(50) + "thread";- assert.equal(transcriptPath(configDir, long, "s1"), join(configDir, "projects", `${long.replace(/[^A-Za-z0-9]/g, "-").slice(0, 200)}-h0oumx`, "s1.jsonl"));+ // a long working directory: the CLI cuts the folder name at 200 characters and appends a hash of the resolved path+ const suffix = (path: string) => {+ let hash = 0;+ for (let i = 0; i < path.length; i++) hash = ((hash << 5) - hash + path.charCodeAt(i)) | 0;+ return Math.abs(hash).toString(36);+ };+ const long = mkdtempSync(join(tmpdir(), "claude-images-")) + "/" + "deep/".repeat(50) + "thread";+ mkdirSync(long, { recursive: true });+ const resolvedLong = realpathSync(long);+ assert.ok(resolvedLong.length > 200);+ const longName = transcriptPath(configDir, long, "s1");+ assert.equal(longName, join(configDir, "projects", `${resolvedLong.replace(/[^A-Za-z0-9]/g, "-").slice(0, 200)}-${suffix(resolvedLong)}`, "s1.jsonl"));+ // the CLI's own shape: the cut name, then '-' and a short base-36 hash+ assert.equal(longName.split("/").at(-2)!.length, 200 + 1 + suffix(resolvedLong).length);+ assert.match(longName.split("/").at(-2)!, /-[0-9a-z]+$/);+ // reached through a link, the same folder: the resolution happens inside transcriptPath+ const longLink = join(dirname(long), "link-" + basename(long).slice(0, 40));+ symlinkSync(long, longLink);+ assert.equal(transcriptPath(configDir, longLink, "s1"), longName);+ // a working directory that is gone: named after the path as given, hashed the same way+ const gone = "/" + "deep/".repeat(50) + "thread";+ assert.equal(transcriptPath(configDir, gone, "s1"), join(configDir, "projects", `${gone.replace(/[^A-Za-z0-9]/g, "-").slice(0, 200)}-${suffix(gone)}`, "s1.jsonl"));});test("a transcript that cannot be read keeps the warm process and costs the turn nothing", () => {diff --git a/server/drivers/claude-transcript-images.ts b/server/drivers/claude-transcript-images.tsindex 1c4dda762f..bf65c57f99 100644--- a/server/drivers/claude-transcript-images.ts+++ b/server/drivers/claude-transcript-images.ts@@ -33,31 +33,33 @@ interface TranscriptOptions {sessionId: string;}-/** Where Claude Code keeps one session's transcript, as its own code names it: the working directory with everything but- * letters and digits turned into '-', cut at 200 characters plus a hash of the full path when longer. */+/** Where Claude Code keeps one session's transcript, as its own code names it: the resolved working directory with+ * everything but letters and digits turned into '-', cut at 200 characters plus a base-36 hash of the resolved path+ * when longer (checked against the installed CLI: the hash runs over the path as resolved, not as given). */export function transcriptPath(configDir: string, cwd: string, sessionId: string): string {- let name = cwd.replace(/[^A-Za-z0-9]/g, "-");- if (name.length > PROJECT_DIR_MAX) {- let hash = 0;- for (let i = 0; i < cwd.length; i++) hash = ((hash << 5) - hash + cwd.charCodeAt(i)) | 0;- name = `${name.slice(0, PROJECT_DIR_MAX)}-${Math.abs(hash).toString(36)}`;- }+ let dir = cwd, resolved = false;+ try {+ dir = realpathSync.native(cwd).normalize("NFC");+ resolved = true;+ } catch { /* a working directory that is gone: only the name given */ }+ let name = dir.replace(/[^A-Za-z0-9]/g, "-");+ if (name.length > PROJECT_DIR_MAX) name = `${name.slice(0, PROJECT_DIR_MAX)}-${folderHash(resolved ? dir : cwd)}`;return join(configDir, "projects", name, `${sessionId}.jsonl`);}-/** The transcript under the working directory as given, or as resolved when it is reached through a link. */+/** The suffix the CLI appends to a project folder over 200 characters: a rolling hash of the path, in base-36. */+const folderHash = (path: string) => {+ let hash = 0;+ for (let i = 0; i < path.length; i++) hash = ((hash << 5) - hash + path.charCodeAt(i)) | 0;+ return Math.abs(hash).toString(36);+};++/** The transcript for one session: the folder the CLI names after the resolved working directory. */function findTranscript(options: TranscriptOptions): { file: string; stat: Stats } | undefined {- const dirs = [options.cwd];+ const file = transcriptPath(options.configDir, options.cwd, options.sessionId);try {- const real = realpathSync(options.cwd);- if (real !== options.cwd) dirs.push(real);- } catch { /* a working directory that is gone: only the name given */ }- for (const dir of dirs) {- const file = transcriptPath(options.configDir, dir, options.sessionId);- try {- return { file, stat: statSync(file) };- } catch { /* not under this name */ }- }+ return { file, stat: statSync(file) };+ } catch { /* no transcript under the resolved name, or the name as given when the directory is gone */ }return undefined;}@@ -83,7 +85,7 @@ export const imagesToKeep = (incoming = 0) => Math.max(0, Math.min(CLAUDE_IMAGE_/** Whether a warm process on this session would send more than the limit with `incoming` new images: its transcript* already holds everything it will resend. False when there is no transcript, and when it cannot be read: the warm- * process then keeps the turn, as it did before this check. */+ * process then keeps the turn. */export function transcriptImagesOver(options: TranscriptOptions, incoming = 0): boolean {try {const found = findTranscript(options);