From f6a1cfe3e79912471785d229c11dcbb45d38677c Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 08:06:08 +0000 Subject: [PATCH 1/4] test(server): reproduce #32873 Codex chats moved into a project are not imported Codex Desktop starts "No project" chats in ~/Documents/Codex// and records a later move into a project only in state_5.sqlite. The rollout keeps the scratch cwd, so the session scanner never offers the moved chat under the project. This test reproduces #32873 and fails on main. Co-Authored-By: Claude Opus 5.5 --- .../src/project/AgentSessionScanner.test.ts | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index f9c7aaf3fd3d..a06f62545547 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -4,6 +4,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import * as NodeChildProcess from "node:child_process"; import * as NodeFS from "node:fs"; import * as NodeOS from "node:os"; +import * as NodeSqlite from "node:sqlite"; import { describe, expect, it } from "@effect/vitest"; import { AgentSessionImportSource, @@ -1345,6 +1346,121 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("imports Codex chats moved from a scratch folder into a Codex project", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const nowMs = Date.parse("2026-11-08T12:00:00.000Z"); + yield* TestClock.setTime(nowMs); + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const homeDir = yield* makeTempDir("t3code-home-"); + const projectDir = path.join(homeDir, "code", "my-project"); + const scratchDir = path.join(homeDir, "Documents", "Codex", "2026-11-08", "new-chat"); + yield* fileSystem.makeDirectory(projectDir, { recursive: true }); + yield* fileSystem.makeDirectory(scratchDir, { recursive: true }); + + const projectId = "019a5f00-0000-7000-8000-000000000001"; + const chats = [ + { + id: "019a5f00-0000-7000-8000-00000000000a", + cwd: scratchDir, + message: "Started with no project", + }, + { + id: "019a5f00-0000-7000-8000-00000000000b", + cwd: projectDir, + message: "Started in the project", + }, + ]; + const rolloutPaths: Array = []; + for (const [index, chat] of chats.entries()) { + const filePath = path.join( + codexHomePath, + "sessions", + "2026", + "11", + "08", + `rollout-2026-11-08T08-0${index}-00-${chat.id}.jsonl`, + ); + rolloutPaths.push(filePath); + yield* writeTranscript({ + filePath, + contents: [ + encodeTranscriptRecord({ + type: "session_meta", + payload: { id: chat.id, cwd: chat.cwd }, + }), + encodeTranscriptRecord({ type: "turn_context", payload: { cwd: chat.cwd } }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: chat.message }, + }), + ].join("\n"), + mtimeMs: nowMs - 60_000 + index, + }); + } + // Codex records a chat's project only in its state DB; the rollout keeps the original cwd. + yield* Effect.sync(() => { + const db = new NodeSqlite.DatabaseSync(path.join(codexHomePath, "state_5.sqlite")); + try { + db.exec(` + CREATE TABLE projects ( + id TEXT PRIMARY KEY, + name TEXT NOT NULL, + metadata TEXT NOT NULL DEFAULT '{}', + position INTEGER NOT NULL, + created_at_ms INTEGER NOT NULL, + updated_at_ms INTEGER NOT NULL + ); + CREATE TABLE project_roots ( + project_id TEXT NOT NULL REFERENCES projects(id) ON DELETE CASCADE, + position INTEGER NOT NULL, + path TEXT NOT NULL, + PRIMARY KEY (project_id, position) + ); + CREATE TABLE threads ( + id TEXT PRIMARY KEY, + rollout_path TEXT NOT NULL, + cwd TEXT NOT NULL, + project_id TEXT REFERENCES projects(id) ON DELETE SET NULL + ); + `); + db.prepare("INSERT INTO projects VALUES (?, ?, '{}', 0, ?, ?)").run( + projectId, + "my-project", + nowMs, + nowMs, + ); + db.prepare("INSERT INTO project_roots VALUES (?, 0, ?)").run(projectId, projectDir); + const insertThread = db.prepare("INSERT INTO threads VALUES (?, ?, ?, ?)"); + for (const [index, chat] of chats.entries()) { + insertThread.run(chat.id, rolloutPaths[index]!, chat.cwd, projectId); + } + } finally { + db.close(); + } + }); + + const threads = yield* runRecentThreads({ + claudeHomePath, + codexHomePath, + workspaceRoot: projectDir, + }).pipe(Effect.provideService(HostProcess.HomeDirectory, homeDir)); + expect(threads.map((thread) => thread.title).toSorted()).toEqual([ + "Started in the project", + "Started with no project", + ]); + + const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe( + Effect.provideService(HostProcess.HomeDirectory, homeDir), + ); + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ + { path: projectDir, threadCount: 2 }, + ]); + }), + ); + it.effect("imports a rewound Codex thread with the history its new rollout inherits", () => Effect.gen(function* () { const path = yield* Path.Path; From f68f7cfca2681b9e372f330bc251f0839d892d46 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 08:15:48 +0000 Subject: [PATCH 2/4] fix(server): import Codex chats moved into a project from a scratch folder Codex Desktop starts "No project" chats in ~/Documents/Codex// and records a later move into a project only in its state DB (state_5.sqlite). The rollout keeps the scratch cwd, which T3 excludes, so the session scanner never offered the moved chat under its project. Codex discovery now reads the thread-to-project roots from that DB read-only, once per Codex home. A chat whose rollout cwd lies outside its Codex project's roots is grouped and imported under the project's first root; chats already inside the project keep their own cwd. A missing, locked or unreadable DB leaves the old behaviour unchanged. Fixes pingdotgg/t3code#32873 Co-Authored-By: Claude Opus 5.5 --- .../src/project/AgentSessionScanner.test.ts | 32 +++++++ .../server/src/project/AgentSessionScanner.ts | 90 +++++++++++++++++-- 2 files changed, 115 insertions(+), 7 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index a06f62545547..326d6a9129a2 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1088,6 +1088,38 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { ]); }), ); + + it.effect("keeps Codex rollout cwds when the Codex state database is unreadable", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const claudeHomePath = yield* makeTempDir("t3code-claude-home-"); + const codexHomePath = yield* makeTempDir("t3code-codex-home-"); + const workspace = yield* makeTempDir("t3code-workspace-"); + yield* writeTranscript({ + filePath: path.join( + codexHomePath, + "sessions", + "2026", + "05", + "03", + "rollout-2026-05-03T00-00-00-019a5f00-0000-7000-8000-00000000000a.jsonl", + ), + contents: codexRolloutLine(workspace), + mtimeMs: Date.parse("2026-05-03T00:00:00.000Z"), + }); + yield* fileSystem.writeFileString( + path.join(codexHomePath, "state_5.sqlite"), + "not a database", + ); + + const result = yield* runScan({ claudeHomePath, codexHomePath }); + + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ + { path: workspace, threadCount: 1 }, + ]); + }), + ); }); describe("recentThreads", () => { diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index dc67bd9387e0..db70bf325283 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -1,4 +1,5 @@ import * as NodeOS from "node:os"; +import * as NodeSqlite from "node:sqlite"; import { AgentSessionScanError, @@ -55,9 +56,19 @@ class TranscriptParseError extends Schema.TaggedError()( return `Could not parse the transcript at ${this.filePath}.`; } } + +class CodexStateReadError extends Schema.TaggedError()("CodexStateReadError", { + databasePath: Schema.String, + cause: Schema.Defect(), +}) { + override get message(): string { + return `Could not read Codex projects from ${this.databasePath}.`; + } +} const METADATA_READ_BYTES = 8 * 1024; const MAX_TRANSCRIPT_SCAN_BYTES = 1024 * 1024; const MAX_CODEX_SESSION_INDEX_BYTES = 1024 * 1024; +const MAX_CODEX_PROJECT_ROOT_ROWS = 100_000; const MAX_TRANSCRIPTS_PER_SOURCE = 5000; @@ -159,7 +170,9 @@ const decodeCodexHistoryBase = Schema.decodeUnknownOption( ); // Original rollouts end in their thread ID, rewound ones in `_`. const CODEX_ROLLOUT_ID_PATTERN = - /[-_]([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.jsonl$/i; + /-([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; +// Codex records a chat's project only here; moving a chat leaves its rollout cwd unchanged. +const CODEX_STATE_DATABASE = "state_5.sqlite"; const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption( Schema.fromJsonString(CodexSessionIndexEntry), ); @@ -224,6 +237,7 @@ interface RawCandidate { readonly transcripts: ReadonlyArray<{ readonly filePath: string; readonly mtimeMs: number | null; + readonly projectRoots?: ReadonlyArray | undefined; }>; } @@ -233,6 +247,7 @@ interface TranscriptCandidate { readonly providerInstanceId: ProviderInstanceId; readonly homePath: string; readonly size: number; + readonly projectRoots?: ReadonlyArray | undefined; } interface MetadataReadBudget { @@ -1234,6 +1249,54 @@ export const make = Effect.gen(function* () { }, ); + const readCodexProjectRoots = Effect.fn("AgentSessionScanner.readCodexProjectRoots")(function* ( + homePath: string, + ) { + const rootsByThread = new Map>(); + const databasePath = path.join(homePath, CODEX_STATE_DATABASE); + return yield* Effect.try({ + try: () => { + const database = new NodeSqlite.DatabaseSync(databasePath, { readOnly: true }); + try { + database.exec("PRAGMA busy_timeout = 100"); + const rows = database + .prepare( + `SELECT threads.id AS id, project_roots.path AS path + FROM threads JOIN project_roots ON project_roots.project_id = threads.project_id + ORDER BY threads.rowid DESC, project_roots.position LIMIT ?`, + ) + .all(MAX_CODEX_PROJECT_ROOT_ROWS); + for (const { id, path: root } of rows) { + if (typeof id !== "string" || typeof root !== "string" || root.trim().length === 0) { + continue; + } + const roots = rootsByThread.get(id); + if (roots === undefined) rootsByThread.set(id, [root]); + else roots.push(root); + } + return rootsByThread; + } finally { + database.close(); + } + }, + catch: (cause) => new CodexStateReadError({ databasePath, cause }), + }).pipe(Effect.orElseSucceed((): ReadonlyMap> => new Map())); + }); + + // A chat inside its project, such as in a subdirectory, keeps its own cwd. + const projectCwd = (cwd: string, projectRoots: ReadonlyArray | undefined) => { + if (projectRoots === undefined) return cwd; + const resolved = normalizeForWorktreeMatch( + path.resolve(expandHomePath(cwd.trim(), homeDir)), + foldWorktreeCase, + ); + return projectRoots.some((root) => + resolved.startsWith(normalizeForWorktreeMatch(path.resolve(root), foldWorktreeCase)), + ) + ? cwd + : projectRoots[0]!; + }; + const discoverCodexTranscripts = Effect.fn("AgentSessionScanner.discoverCodexTranscripts")( function* (homePath: string, providerInstanceId: ProviderInstanceId, operationBudget: number) { const sessionsDir = path.join(homePath, "sessions"); @@ -1295,7 +1358,15 @@ export const make = Effect.gen(function* () { } } } - return { transcripts, truncated }; + const projectRoots = yield* readCodexProjectRoots(homePath); + return { + transcripts: transcripts.map((transcript) => { + const threadId = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1]; + const roots = threadId === undefined ? undefined : projectRoots.get(threadId); + return roots === undefined ? transcript : { ...transcript, projectRoots: roots }; + }), + truncated, + }; }, ); @@ -1365,13 +1436,14 @@ export const make = Effect.gen(function* () { providerInstanceId: ProviderInstanceId; homePath: string; lastActiveAtMs: number; - transcripts: Array<{ filePath: string; mtimeMs: number }>; + transcripts: Array; } >(); for (const transcript of transcripts) { - const cwd = yield* readCwd(source, transcript, budget); - if (cwd === null || cwd === false) continue; + const rolloutCwd = yield* readCwd(source, transcript, budget); + if (rolloutCwd === null || rolloutCwd === false) continue; + const cwd = projectCwd(rolloutCwd, transcript.projectRoots); const key = `${transcript.providerInstanceId}\0${cwd}`; const existing = byOwnerAndCwd.get(key); if (existing) { @@ -1718,7 +1790,8 @@ export const make = Effect.gen(function* () { candidates.flatMap((candidate) => candidate.source === "codex" && candidate.providerInstanceId === providerInstanceId ? candidate.transcripts.flatMap((transcript) => { - const id = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath)?.[1]; + const match = CODEX_ROLLOUT_ID_PATTERN.exec(transcript.filePath); + const id = match?.[2] ?? match?.[1]; return id === undefined ? [] : [[id, transcript.filePath] as const]; }) : [], @@ -1804,7 +1877,10 @@ export const make = Effect.gen(function* () { if (Option.isNone(snapshot.cwd)) { return Option.some({ _tag: "Skipped" }); } - const expandedCwd = expandHomePath(snapshot.cwd.value.trim(), homeDir); + const expandedCwd = expandHomePath( + projectCwd(snapshot.cwd.value, transcript.projectRoots).trim(), + homeDir, + ); if ( !path.isAbsolute(expandedCwd) || (yield* directoryIdentity(path.resolve(expandedCwd))) !== rootIdentity From 60005e5afdc6a76e4d9a047ca1a76c846586841e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 08:22:15 +0000 Subject: [PATCH 3/4] fix(server): only re-home Codex project chats left in an excluded folder Chats assigned to a Codex project but running in another real directory, such as a linked worktree, keep their own cwd. Co-Authored-By: Claude Opus 5.5 --- .../src/project/AgentSessionScanner.test.ts | 14 +++++++++++++- .../server/src/project/AgentSessionScanner.ts | 19 ++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 326d6a9129a2..613e9ab65841 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1389,8 +1389,10 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const homeDir = yield* makeTempDir("t3code-home-"); const projectDir = path.join(homeDir, "code", "my-project"); const scratchDir = path.join(homeDir, "Documents", "Codex", "2026-11-08", "new-chat"); + const otherDir = path.join(homeDir, "code", "other-project"); yield* fileSystem.makeDirectory(projectDir, { recursive: true }); yield* fileSystem.makeDirectory(scratchDir, { recursive: true }); + yield* fileSystem.makeDirectory(otherDir, { recursive: true }); const projectId = "019a5f00-0000-7000-8000-000000000001"; const chats = [ @@ -1404,6 +1406,11 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { cwd: projectDir, message: "Started in the project", }, + { + id: "019a5f00-0000-7000-8000-00000000000c", + cwd: otherDir, + message: "Started in another folder", + }, ]; const rolloutPaths: Array = []; for (const [index, chat] of chats.entries()) { @@ -1487,8 +1494,13 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe( Effect.provideService(HostProcess.HomeDirectory, homeDir), ); - expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ + expect( + result.candidates + .map(({ path, threadCount }) => ({ path, threadCount })) + .toSorted((left, right) => left.path.localeCompare(right.path)), + ).toEqual([ { path: projectDir, threadCount: 2 }, + { path: otherDir, threadCount: 1 }, ]); }), ); diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index db70bf325283..1b32de22a887 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -1283,19 +1283,12 @@ export const make = Effect.gen(function* () { }).pipe(Effect.orElseSucceed((): ReadonlyMap> => new Map())); }); - // A chat inside its project, such as in a subdirectory, keeps its own cwd. - const projectCwd = (cwd: string, projectRoots: ReadonlyArray | undefined) => { - if (projectRoots === undefined) return cwd; - const resolved = normalizeForWorktreeMatch( - path.resolve(expandHomePath(cwd.trim(), homeDir)), - foldWorktreeCase, - ); - return projectRoots.some((root) => - resolved.startsWith(normalizeForWorktreeMatch(path.resolve(root), foldWorktreeCase)), - ) - ? cwd - : projectRoots[0]!; - }; + // Only chats left in an excluded folder, such as Codex's scratch folder, take their project's root. + const projectCwd = (cwd: string, projectRoots: ReadonlyArray | undefined) => + projectRoots !== undefined && + isExcludedProjectPath(path.resolve(expandHomePath(cwd.trim(), homeDir))) + ? projectRoots[0]! + : cwd; const discoverCodexTranscripts = Effect.fn("AgentSessionScanner.discoverCodexTranscripts")( function* (homePath: string, providerInstanceId: ProviderInstanceId, operationBudget: number) { From 81b2b669792620f9f327093e6eb89f8bf5151e18 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 8 Nov 2026 08:32:25 +0000 Subject: [PATCH 4/4] test(server): cover a rewound Codex chat moved into a project Co-Authored-By: Claude Opus 5.5 --- apps/server/src/project/AgentSessionScanner.test.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 613e9ab65841..6ab04f7e302a 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1411,6 +1411,12 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { cwd: otherDir, message: "Started in another folder", }, + { + id: "019a5f00-0000-7000-8000-00000000000d", + rolloutId: "019a5f00-0000-7000-8000-0000000000d1", + cwd: scratchDir, + message: "Rewound with no project", + }, ]; const rolloutPaths: Array = []; for (const [index, chat] of chats.entries()) { @@ -1420,7 +1426,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { "2026", "11", "08", - `rollout-2026-11-08T08-0${index}-00-${chat.id}.jsonl`, + `rollout-2026-11-08T08-0${index}-00-${"rolloutId" in chat ? `${chat.id}_${chat.rolloutId}` : chat.id}.jsonl`, ); rolloutPaths.push(filePath); yield* writeTranscript({ @@ -1487,6 +1493,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { workspaceRoot: projectDir, }).pipe(Effect.provideService(HostProcess.HomeDirectory, homeDir)); expect(threads.map((thread) => thread.title).toSorted()).toEqual([ + "Rewound with no project", "Started in the project", "Started with no project", ]); @@ -1499,7 +1506,7 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { .map(({ path, threadCount }) => ({ path, threadCount })) .toSorted((left, right) => left.path.localeCompare(right.path)), ).toEqual([ - { path: projectDir, threadCount: 2 }, + { path: projectDir, threadCount: 3 }, { path: otherDir, threadCount: 1 }, ]); }),