Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions apps/server/src/project/AgentSessionScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,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<string> = [];
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;
Expand Down
64 changes: 61 additions & 3 deletions apps/server/src/project/AgentSessionScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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;

Expand Down Expand Up @@ -185,8 +186,24 @@ const CODEX_ROLLOUT_ID_PATTERN =
const COMPRESSED_TRANSCRIPT_SUFFIX = ".jsonl.zst";
const isCompressedTranscript = (filePath: string) =>
filePath.endsWith(COMPRESSED_TRANSCRIPT_SUFFIX);
// 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";
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),
);
Expand Down Expand Up @@ -1366,12 +1383,49 @@ export const make = Effect.gen(function* () {
},
);

const readCodexGlobalStateRoots = Effect.fn("AgentSessionScanner.readCodexGlobalStateRoots")(
function* (homePath: string) {
const rootsByThread = new Map<string, ReadonlyArray<string>>();
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<string, Array<string>>();
const databasePath = path.join(homePath, CODEX_STATE_DATABASE);
return yield* Effect.try({
const databaseRoots: Map<string, ReadonlyArray<string>> = yield* Effect.try({
try: () => {
const database = new NodeSqlite.DatabaseSync(databasePath, { readOnly: true });
try {
Expand All @@ -1397,7 +1451,11 @@ export const make = Effect.gen(function* () {
}
},
catch: (cause) => new CodexStateReadError({ databasePath, cause }),
}).pipe(Effect.orElseSucceed((): ReadonlyMap<string, ReadonlyArray<string>> => new Map()));
}).pipe(Effect.orElseSucceed((): Map<string, ReadonlyArray<string>> => 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.
Expand Down
Loading