From af6e5a83fe9efbbeed577fff6d8f3792297cfabc Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 20:28:24 +0000 Subject: [PATCH 1/3] test(server): scratch Codex chats assigned to a local project in Codex Desktop Fails on main: the scanner only reads project roots from the state DB's threads.project_id, which Codex Desktop leaves NULL for these chats. AI-assisted; verified with the tests and tools listed above. --- .../src/project/AgentSessionScanner.test.ts | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/apps/server/src/project/AgentSessionScanner.test.ts b/apps/server/src/project/AgentSessionScanner.test.ts index 956a65976369f..315a6cbadc216 100644 --- a/apps/server/src/project/AgentSessionScanner.test.ts +++ b/apps/server/src/project/AgentSessionScanner.test.ts @@ -1512,6 +1512,99 @@ it.layer(NodeServices.layer)("AgentSessionScanner", (it) => { }), ); + it.effect("imports scratch Codex chats that Codex Desktop assigned to a local 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 = "local-project-1"; + const assignedId = "019a5f00-0000-7000-8000-00000000001a"; + const chats = [ + { id: assignedId, message: "Moved into the project" }, + { id: "019a5f00-0000-7000-8000-00000000001b", message: "Still has no 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: scratchDir }, + }), + encodeTranscriptRecord({ type: "turn_context", payload: { cwd: scratchDir } }), + encodeTranscriptRecord({ + type: "event_msg", + payload: { type: "user_message", message: chat.message }, + }), + ].join("\n"), + mtimeMs: nowMs - 60_000 + index, + }); + } + // Codex Desktop keeps this assignment only in its global state; the state DB's project_id stays NULL. + yield* fileSystem.writeFileString( + path.join(codexHomePath, ".codex-global-state.json"), + JSON.stringify({ + "thread-project-assignments": { + [assignedId]: { projectKind: "local", projectId }, + }, + "local-projects": { [projectId]: { rootPaths: [projectDir] } }, + }), + ); + 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); + CREATE TABLE project_roots ( + project_id TEXT NOT NULL, + 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 + ); + `); + const insertThread = db.prepare("INSERT INTO threads VALUES (?, ?, ?, NULL)"); + for (const [index, chat] of chats.entries()) { + insertThread.run(chat.id, rolloutPaths[index]!, scratchDir); + } + } finally { + db.close(); + } + }); + + const result = yield* runScan({ claudeHomePath, codexHomePath }).pipe( + Effect.provideService(HostProcess.HomeDirectory, homeDir), + ); + expect(result.candidates.map(({ path, threadCount }) => ({ path, threadCount }))).toEqual([ + { path: projectDir, threadCount: 1 }, + ]); + }), + ); + it.effect("imports a rewound Codex thread with the history its new rollout inherits", () => Effect.gen(function* () { const path = yield* Path.Path; From fa566bb03c4e127c7d03ef9a6570f5ba52267a83 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 11 Nov 2026 20:33:05 +0000 Subject: [PATCH 2/3] fix(server): import Codex Desktop chats moved into a local project Codex Desktop leaves threads.project_id NULL for chats moved into a local project and records the assignment only in .codex-global-state.json. Read thread-project-assignments and local-projects rootPaths from it as a fallback when the state database has no project for the thread. Fixes pingdotgg/t3code#34520 AI-assisted; verified with the tests and tools listed above. --- .../server/src/project/AgentSessionScanner.ts | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index 6c8410241a3f5..ab770edc5ebd7 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -69,6 +69,7 @@ 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_CODEX_GLOBAL_STATE_BYTES = 32 * 1024 * 1024; const MAX_TRANSCRIPTS_PER_SOURCE = 5000; @@ -173,6 +174,22 @@ 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})(?:_([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"; +// Codex Desktop keeps chats moved into a local project here and leaves threads.project_id NULL. +const CODEX_GLOBAL_STATE = ".codex-global-state.json"; +const decodeCodexGlobalState = Schema.decodeUnknownOption( + Schema.fromJsonString( + Schema.Struct({ + "thread-project-assignments": Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), + "local-projects": Schema.optional(Schema.Record(Schema.String, Schema.Unknown)), + }), + ), +); +const decodeCodexProjectAssignment = Schema.decodeUnknownOption( + Schema.Struct({ projectKind: Schema.Literal("local"), projectId: Schema.String }), +); +const decodeCodexLocalProject = Schema.decodeUnknownOption( + Schema.Struct({ rootPaths: Schema.Array(Schema.String) }), +); const decodeCodexSessionIndexEntry = Schema.decodeUnknownOption( Schema.fromJsonString(CodexSessionIndexEntry), ); @@ -1285,12 +1302,49 @@ export const make = Effect.gen(function* () { }, ); + const readCodexGlobalStateRoots = Effect.fn("AgentSessionScanner.readCodexGlobalStateRoots")( + function* (homePath: string) { + const rootsByThread = new Map>(); + const filePath = path.join(homePath, CODEX_GLOBAL_STATE); + const stats = yield* statOption(filePath); + if ( + Option.isNone(stats) || + stats.value.type !== "File" || + Number(stats.value.size) > MAX_CODEX_GLOBAL_STATE_BYTES + ) { + return rootsByThread; + } + const contents = yield* fileSystem + .readFileString(filePath) + .pipe(Effect.orElseSucceed(() => "")); + const state = decodeCodexGlobalState(contents); + if (Option.isNone(state)) return rootsByThread; + const localProjects = state.value["local-projects"] ?? {}; + for (const [threadId, value] of Object.entries( + state.value["thread-project-assignments"] ?? {}, + )) { + const assignment = decodeCodexProjectAssignment(value); + if ( + Option.isNone(assignment) || + !Object.hasOwn(localProjects, assignment.value.projectId) + ) { + continue; + } + const project = decodeCodexLocalProject(localProjects[assignment.value.projectId]); + if (Option.isNone(project)) continue; + const roots = project.value.rootPaths.filter((root) => root.trim().length > 0); + if (roots.length > 0) rootsByThread.set(threadId, roots); + } + return rootsByThread; + }, + ); + 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({ + const databaseRoots: Map> = yield* Effect.try({ try: () => { const database = new NodeSqlite.DatabaseSync(databasePath, { readOnly: true }); try { @@ -1316,7 +1370,11 @@ export const make = Effect.gen(function* () { } }, catch: (cause) => new CodexStateReadError({ databasePath, cause }), - }).pipe(Effect.orElseSucceed((): ReadonlyMap> => new Map())); + }).pipe(Effect.orElseSucceed((): Map> => new Map())); + for (const [threadId, roots] of yield* readCodexGlobalStateRoots(homePath)) { + if (!databaseRoots.has(threadId)) databaseRoots.set(threadId, roots); + } + return databaseRoots; }); // Only chats left in an excluded folder, such as Codex's scratch folder, take their project's root. From cfb733ec2423e02a9673d7c6720a39846d6a16de Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 13 Nov 2026 16:21:52 +0000 Subject: [PATCH 3/3] chore(server): correct Codex project-source comment Co-Authored-By: Claude Opus 5.5 --- apps/server/src/project/AgentSessionScanner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/project/AgentSessionScanner.ts b/apps/server/src/project/AgentSessionScanner.ts index a975ae1c00bba..3dedb6cf67aab 100644 --- a/apps/server/src/project/AgentSessionScanner.ts +++ b/apps/server/src/project/AgentSessionScanner.ts @@ -179,7 +179,7 @@ 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})(?:_([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. +// Codex records a chat's project here, not in the rollout; moving a chat leaves its cwd unchanged. const CODEX_STATE_DATABASE = "state_5.sqlite"; // Codex Desktop keeps chats moved into a local project here and leaves threads.project_id NULL. const CODEX_GLOBAL_STATE = ".codex-global-state.json";