Skip to content
167 changes: 167 additions & 0 deletions apps/server/src/project/AgentSessionScanner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -1087,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", () => {
Expand Down Expand Up @@ -1345,6 +1378,140 @@ 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");
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 = [
{
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",
},
{
id: "019a5f00-0000-7000-8000-00000000000c",
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<string> = [];
for (const [index, chat] of chats.entries()) {
const filePath = path.join(
codexHomePath,
"sessions",
"2026",
"11",
"08",
`rollout-2026-11-08T08-0${index}-00-${"rolloutId" in chat ? `${chat.id}_${chat.rolloutId}` : 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([
"Rewound with no project",
"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 }))
.toSorted((left, right) => left.path.localeCompare(right.path)),
).toEqual([
{ path: projectDir, threadCount: 3 },
{ path: otherDir, 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
83 changes: 76 additions & 7 deletions apps/server/src/project/AgentSessionScanner.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as NodeOS from "node:os";
import * as NodeSqlite from "node:sqlite";

import {
AgentSessionScanError,
Expand Down Expand Up @@ -55,9 +56,19 @@ class TranscriptParseError extends Schema.TaggedError<TranscriptParseError>()(
return `Could not parse the transcript at ${this.filePath}.`;
}
}

class CodexStateReadError extends Schema.TaggedError<CodexStateReadError>()("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;

Expand Down Expand Up @@ -159,7 +170,9 @@ const decodeCodexHistoryBase = Schema.decodeUnknownOption(
);
// Original rollouts end in their thread ID, rewound ones in `<thread ID>_<rollout ID>`.
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),
);
Expand Down Expand Up @@ -224,6 +237,7 @@ interface RawCandidate {
readonly transcripts: ReadonlyArray<{
readonly filePath: string;
readonly mtimeMs: number | null;
readonly projectRoots?: ReadonlyArray<string> | undefined;
}>;
}

Expand All @@ -233,6 +247,7 @@ interface TranscriptCandidate {
readonly providerInstanceId: ProviderInstanceId;
readonly homePath: string;
readonly size: number;
readonly projectRoots?: ReadonlyArray<string> | undefined;
}

interface MetadataReadBudget {
Expand Down Expand Up @@ -1234,6 +1249,47 @@ export const make = Effect.gen(function* () {
},
);

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({
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<string, ReadonlyArray<string>> => new Map()));
});

// Only chats left in an excluded folder, such as Codex's scratch folder, take their project's root.
const projectCwd = (cwd: string, projectRoots: ReadonlyArray<string> | 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) {
const sessionsDir = path.join(homePath, "sessions");
Expand Down Expand Up @@ -1295,7 +1351,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,
};
},
);

Expand Down Expand Up @@ -1365,13 +1429,14 @@ export const make = Effect.gen(function* () {
providerInstanceId: ProviderInstanceId;
homePath: string;
lastActiveAtMs: number;
transcripts: Array<{ filePath: string; mtimeMs: number }>;
transcripts: Array<TranscriptCandidate>;
}
>();

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) {
Expand Down Expand Up @@ -1718,7 +1783,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];
})
: [],
Expand Down Expand Up @@ -1804,7 +1870,10 @@ export const make = Effect.gen(function* () {
if (Option.isNone(snapshot.cwd)) {
return Option.some<AgentSessionRecentThread>({ _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
Expand Down
Loading