@devries-cameron20 / t3code Mirror · read-only / commits / bbd51f74b4
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, 00:45 UTC

fix(server): import Codex chats moved into a project from the scratch folder (#32880)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Nov 10, 2026 at 19:11 UTC bbd51f74b425a1c7d47bc0be72bcfc6d15c44676
2 files changed +243 -7
apps/server/src/project/AgentSessionScanner.test.ts
+167
@@ -4,6 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
4 import * as NodeChildProcess from "node:child_process";
5 import * as NodeFS from "node:fs";
6 import * as NodeOS from "node:os";
7 +import * as NodeSqlite from "node:sqlite";
8 import { describe, expect, it } from "@effect/vitest";
9 import {
10 AgentSessionImportSource,
@@ -1087,6 +1088,38 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
1088 ]);
1089 }),
1090 );
1091 +
1092 + it.effect("keeps Codex rollout cwds when the Codex state database is unreadable", () =>
1093 + Effect.gen(function* () {
1094 + const fileSystem = yield* FileSystem.FileSystem;
1095 + const path = yield* Path.Path;
1096 + const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1097 + const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1098 + const workspace = yield* makeTempDir("t3code-workspace-");
1099 + yield* writeTranscript({
1100 + filePath: path.join(
1101 + codexHomePath,
1102 + "sessions",
1103 + "2026",
1104 + "05",
1105 + "03",
1106 + "rollout-2026-05-03T00-00-00-019a5f00-0000-7000-8000-00000000000a.jsonl",
1107 + ),
1108 + contents: codexRolloutLine(workspace),
1109 + mtimeMs: Date.parse("2026-05-03T00:00:00.000Z"),
1110 + });
1111 + yield* fileSystem.writeFileString(
1112 + path.join(codexHomePath, "state_5.sqlite"),
1113 + "not a database",
1114 + );
1115 +
1116 + const result = yield* runScan({ claudeHomePath, codexHomePath });
1117 +
1118 + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([
1119 + { path: workspace, threadCount: 1 },
1120 + ]);
1121 + }),
1122 + );
1123 });
1124
1125 describe("recentThreads", () => {
@@ -1345,6 +1378,140 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => {
1378 }),
1379 );
1380
1381 + it.effect("imports Codex chats moved from a scratch folder into a Codex project", () =>
1382 + Effect.gen(function* () {
1383 + const fileSystem = yield* FileSystem.FileSystem;
1384 + const path = yield* Path.Path;
1385 + const nowMs = Date.parse("2026-11-08T12:00:00.000Z");
1386 + yield* TestClock.setTime(nowMs);
1387 + const claudeHomePath = yield* makeTempDir("t3code-claude-home-");
1388 + const codexHomePath = yield* makeTempDir("t3code-codex-home-");
1389 + const homeDir = yield* makeTempDir("t3code-home-");
1390 + const projectDir = path.join(homeDir, "code", "my-project");
1391 + const scratchDir = path.join(homeDir, "Documents", "Codex", "2026-11-08", "new-chat");
1392 + const otherDir = path.join(homeDir, "code", "other-project");
1393 + yield* fileSystem.makeDirectory(projectDir, { recursive: true });
1394 + yield* fileSystem.makeDirectory(scratchDir, { recursive: true });
1395 + yield* fileSystem.makeDirectory(otherDir, { recursive: true });
1396 +
1397 + const projectId = "019a5f00-0000-7000-8000-000000000001";
1398 + const chats = [
1399 + {
1400 + id: "019a5f00-0000-7000-8000-00000000000a",
1401 + cwd: scratchDir,
1402 + message: "Started with no project",
1403 + },
1404 + {
1405 + id: "019a5f00-0000-7000-8000-00000000000b",
1406 + cwd: projectDir,
1407 + message: "Started in the project",
1408 + },
1409 + {
1410 + id: "019a5f00-0000-7000-8000-00000000000c",
1411 + cwd: otherDir,
1412 + message: "Started in another folder",
1413 + },
1414 + {
1415 + id: "019a5f00-0000-7000-8000-00000000000d",
1416 + rolloutId: "019a5f00-0000-7000-8000-0000000000d1",
1417 + cwd: scratchDir,
1418 + message: "Rewound with no project",
1419 + },
1420 + ];
1421 + const rolloutPaths: Array<string> = [];
1422 + for (const [index, chat] of chats.entries()) {
1423 + const filePath = path.join(
1424 + codexHomePath,
1425 + "sessions",
1426 + "2026",
1427 + "11",
1428 + "08",
1429 + `rollout-2026-11-08T08-0${index}-00-${"rolloutId" in chat ? `${chat.id}_${chat.rolloutId}` : chat.id}.jsonl`,
1430 + );
1431 + rolloutPaths.push(filePath);
1432 + yield* writeTranscript({
1433 + filePath,
1434 + contents: [
1435 + encodeTranscriptRecord({
1436 + type: "session_meta",
1437 + payload: { id: chat.id, cwd: chat.cwd },
1438 + }),
1439 + encodeTranscriptRecord({ type: "turn_context", payload: { cwd: chat.cwd } }),
1440 + encodeTranscriptRecord({
1441 + type: "event_msg",
1442 + payload: { type: "user_message", message: chat.message },
1443 + }),
1444 + ].join("\n"),
1445 + mtimeMs: nowMs - 60_000 + index,
1446 + });
1447 + }
1448 + // Codex records a chat's project only in its state DB; the rollout keeps the original cwd.
1449 + yield* Effect.sync(() => {
1450 + const db = new NodeSqlite.DatabaseSync(path.join(codexHomePath, "state_5.sqlite"));
1451 + try {
1452 + db.exec(`
1453 + CREATE TABLE projects (
1454 + id TEXT PRIMARY KEY,
1455 + name TEXT NOT NULL,
1456 + metadata TEXT NOT NULL DEFAULT '{}',
1457 + position INTEGER NOT NULL,
1458 + created_at_ms INTEGER NOT NULL,
1459 + updated_at_ms INTEGER NOT NULL
1460 + );
1461 + CREATE TABLE project_roots (
1462 + project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
1463 + position INTEGER NOT NULL,
1464 + path TEXT NOT NULL,
1465 + PRIMARY KEY (project_id, position)
1466 + );
1467 + CREATE TABLE threads (
1468 + id TEXT PRIMARY KEY,
1469 + rollout_path TEXT NOT NULL,
1470 + cwd TEXT NOT NULL,
1471 + project_id TEXT REFERENCES projects(id) ON DELETE SET NULL
1472 + );
1473 + `);
1474 + db.prepare("INSERT INTO projects VALUES (?, ?, '{}', 0, ?, ?)").run(
1475 + projectId,
1476 + "my-project",
1477 + nowMs,
1478 + nowMs,
1479 + );
1480 + db.prepare("INSERT INTO project_roots VALUES (?, 0, ?)").run(projectId, projectDir);
1481 + const insertThread = db.prepare("INSERT INTO threads VALUES (?, ?, ?, ?)");
1482 + for (const [index, chat] of chats.entries()) {
1483 + insertThread.run(chat.id, rolloutPaths[index]!, chat.cwd, projectId);
1484 + }
1485 + } finally {
1486 + db.close();
1487 + }
1488 + });
1489 +
1490 + const threads = yield* runRecentThreads({
1491 + claudeHomePath,
1492 + codexHomePath,
1493 + workspaceRoot: projectDir,
1494 + }).pipe(Effect.provideService(HostProcess.HomeDirectory, homeDir));
1495 + expect(threads.map((thread) => thread.title).toSorted()).toEqual([
1496 + "Rewound with no project",
1497 + "Started in the project",
1498 + "Started with no project",
1499 + ]);
1500 +
1501 + const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe(
1502 + Effect.provideService(HostProcess.HomeDirectory, homeDir),
1503 + );
1504 + expect(
1505 + result.candidates
1506 + .map(({ path, threadCount }) => ({ path, threadCount }))
1507 + .toSorted((left, right) => left.path.localeCompare(right.path)),
1508 + ).toEqual([
1509 + { path: projectDir, threadCount: 3 },
1510 + { path: otherDir, threadCount: 1 },
1511 + ]);
1512 + }),
1513 + );
1514 +
1515 it.effect("imports a rewound Codex thread with the history its new rollout inherits", () =>
1516 Effect.gen(function* () {
1517 const path = yield* Path.Path;
apps/server/src/project/AgentSessionScanner.ts
+76 -7
@@ -1,4 +1,5 @@
1 import * as NodeOS from "node:os";
2 +import * as NodeSqlite from "node:sqlite";
3
4 import {
5 AgentSessionScanError,
@@ -55,9 +56,19 @@ class TranscriptParseError extends Schema.TaggedError<TranscriptParseError>()(
56 return `Could not parse the transcript at ${this.filePath}.`;
57 }
58 }
59 +
60 +class CodexStateReadError extends Schema.TaggedError<CodexStateReadError>()("CodexStateReadError", {
61 + databasePath: Schema.String,
62 + cause: Schema.Defect(),
63 +}) {
64 + override get message(): string {
65 + return `Could not read Codex projects from ${this.databasePath}.`;
66 + }
67 +}
68 const METADATA_READ_BYTES = 8 * 1024;
69 const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024;
70 const MAX_CODEX_SESSION_INDEX_BYTES = 1024 * 1024;
71 +const MAX_CODEX_PROJECT_ROOT_ROWS = 100_000;
72
73 const MAX_TRANSCRIPTS_PER_SOURCE = 5000;
74
@@ -159,7 +170,9 @@ const decodeCodexHistoryBase = Schema.decodeUnknownOption(
170 );
171 // Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
172 const CODEX_ROLLOUT_ID_PATTERN =
162 - /[-_]([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i;
173 + /-([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:_([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}))?\.jsonl$/i;
174 +// Codex records a chat's project only here; moving a chat leaves its rollout cwd unchanged.
175 +const CODEX_STATE_DATABASE = "state_5.sqlite";
176 const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption(
177 Schema.fromJsonString(CodexSessionIndexEntry),
178 );
@@ -224,6 +237,7 @@ interface RawCandidate {
237 readonly transcripts: ReadonlyArray<{
238 readonly filePath: string;
239 readonly mtimeMs: number | null;
240 + readonly projectRoots?: ReadonlyArray<string> | undefined;
241 }>;
242 }
243
@@ -233,6 +247,7 @@ interface TranscriptCandidate {
247 readonly providerInstanceId: ProviderInstanceId;
248 readonly homePath: string;
249 readonly size: number;
250 + readonly projectRoots?: ReadonlyArray<string> | undefined;
251 }
252
253 interface MetadataReadBudget {
@@ -1234,6 +1249,47 @@ export const make = Effect.gen(function* () {
1249 },
1250 );
1251
1252 + const readCodexProjectRoots = Effect.fn("AgentSessionScanner.readCodexProjectRoots")(function* (
1253 + homePath: string,
1254 + ) {
1255 + const rootsByThread = new Map<string, Array<string>>();
1256 + const databasePath = path.join(homePath, CODEX_STATE_DATABASE);
1257 + return yield* Effect.try({
1258 + try: () => {
1259 + const database = new NodeSqlite.DatabaseSync(databasePath, { readOnly: true });
1260 + try {
1261 + database.exec("PRAGMA busy_timeout = 100");
1262 + const rows = database
1263 + .prepare(
1264 + `SELECT threads.id AS id, project_roots.path AS path
1265 + FROM threads JOIN project_roots ON project_roots.project_id = threads.project_id
1266 + ORDER BY threads.rowid DESC, project_roots.position LIMIT ?`,
1267 + )
1268 + .all(MAX_CODEX_PROJECT_ROOT_ROWS);
1269 + for (const { id, path: root } of rows) {
1270 + if (typeof id !== "string" || typeof root !== "string" || root.trim().length === 0) {
1271 + continue;
1272 + }
1273 + const roots = rootsByThread.get(id);
1274 + if (roots === undefined) rootsByThread.set(id, [root]);
1275 + else roots.push(root);
1276 + }
1277 + return rootsByThread;
1278 + } finally {
1279 + database.close();
1280 + }
1281 + },
1282 + catch: (cause) => new CodexStateReadError({ databasePath, cause }),
1283 + }).pipe(Effect.orElseSucceed((): ReadonlyMap<string, ReadonlyArray<string>> => new Map()));
1284 + });
1285 +
1286 + // Only chats left in an excluded folder, such as Codex's scratch folder, take their project's root.
1287 + const projectCwd = (cwd: string, projectRoots: ReadonlyArray<string> | undefined) =>
1288 + projectRoots !== undefined &&
1289 + isExcludedProjectPath(path.resolve(expandHomePath(cwd.trim(), homeDir)))
1290 + ? projectRoots[0]!
1291 + : cwd;
1292 +
1293 const discoverCodexTranscripts = Effect.fn("AgentSessionScanner.discoverCodexTranscripts")(
1294 function* (homePath: string, providerInstanceId: ProviderInstanceId, operationBudget: number) {
1295 const sessionsDir = path.join(homePath, "sessions");
@@ -1295,7 +1351,15 @@ export const make = Effect.gen(function* () {
1351 }
1352 }
1353 }
1298 - return { transcripts, truncated };
1354 + const projectRoots = yield* readCodexProjectRoots(homePath);
1355 + return {
1356 + transcripts: transcripts.map((transcript) => {
1357 + const threadId = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1];
1358 + const roots = threadId === undefined ? undefined : projectRoots.get(threadId);
1359 + return roots === undefined ? transcript : { ...transcript, projectRoots: roots };
1360 + }),
1361 + truncated,
1362 + };
1363 },
1364 );
1365
@@ -1365,13 +1429,14 @@ export const make = Effect.gen(function* () {
1429 providerInstanceId: ProviderInstanceId;
1430 homePath: string;
1431 lastActiveAtMs: number;
1368 - transcripts: Array<{ filePath: string; mtimeMs: number }>;
1432 + transcripts: Array<TranscriptCandidate>;
1433 }
1434 >();
1435
1436 for (const transcript of transcripts) {
1373 - const cwd = yield* readCwd(source, transcript, budget);
1374 - if (cwd === null || cwd === false) continue;
1437 + const rolloutCwd = yield* readCwd(source, transcript, budget);
1438 + if (rolloutCwd === null || rolloutCwd === false) continue;
1439 + const cwd = projectCwd(rolloutCwd, transcript.projectRoots);
1440 const key = `${transcript.providerInstanceId}\0${cwd}`;
1441 const existing = byOwnerAndCwd.get(key);
1442 if (existing) {
@@ -1718,7 +1783,8 @@ export const make = Effect.gen(function* () {
1783 candidates.flatMap((candidate) =>
1784 candidate.source === "codex" && candidate.providerInstanceId === providerInstanceId
1785 ? candidate.transcripts.flatMap((transcript) => {
1721 - const id = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1];
1786 + const match = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath);
1787 + const id = match?.[2] ?? match?.[1];
1788 return id === undefined ? [] : [[id, transcript.filePath] as const];
1789 })
1790 : [],
@@ -1804,7 +1870,10 @@ export const make = Effect.gen(function* () {
1870 if (Option.isNone(snapshot.cwd)) {
1871 return Option.some<AgentSessionRecentThread>({ _tag: "Skipped" });
1872 }
1807 - const expandedCwd = expandHomePath(snapshot.cwd.value.trim(), homeDir);
1873 + const expandedCwd = expandHomePath(
1874 + projectCwd(snapshot.cwd.value, transcript.projectRoots).trim(),
1875 + homeDir,
1876 + );
1877 if (
1878 !path.isAbsolute(expandedCwd) ||
1879 (yield* directoryIdentity(path.resolve(expandedCwd))) !== rootIdentity