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
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ describe("claudeSessionStore.hasSession", () => {
return configDir;
}

const ask = (configDir: string) =>
claudeSessionStore.hasSession?.({
sessionId,
worktreePath: "/work/tree",
env: { CLAUDE_CONFIG_DIR: configDir },
});
const ask = (configDir: string, maxDirs?: number) =>
claudeSessionStore.hasSession?.(
{
sessionId,
worktreePath: "/work/tree",
env: { CLAUDE_CONFIG_DIR: configDir },
},
maxDirs,
);

test("answers false only after searching every project directory", () => {
expect(ask(configWithProjects(10))).toBe(false);
});

test("answers unknown when the search stopped at its cap", () => {
expect(ask(configWithProjects(5_000))).toBeNull();
expect(ask(configWithProjects(2), 2)).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function layoutPath(root: string, query: HarnessSessionQuery): string | null {
function searchProjects(
root: string,
fileName: string,
maxDirs = MAX_PROJECT_DIRS_SCANNED,
): { path: string | null; complete: boolean } {
const projectsDir = join(root, "projects");
let entries: string[];
Expand All @@ -72,13 +73,13 @@ function searchProjects(
} catch {
return { path: null, complete: false };
}
for (const entry of entries.slice(0, MAX_PROJECT_DIRS_SCANNED)) {
for (const entry of entries.slice(0, maxDirs)) {
const candidate = join(projectsDir, entry, fileName);
if (isFile(candidate)) return { path: candidate, complete: true };
}
return {
path: null,
complete: entries.length <= MAX_PROJECT_DIRS_SCANNED,
complete: entries.length <= maxDirs,
};
}

Expand Down Expand Up @@ -157,17 +158,21 @@ export const claudeSessionFiles: HarnessSessionFiles = {
parseTurns,
};

export const claudeSessionStore: HarnessSessionStore = {
export const claudeSessionStore = {
/** Only the launch env's store: that is where a relaunch will look. */
hasSession(query) {
hasSession(query, maxDirs = MAX_PROJECT_DIRS_SCANNED) {
const root = configDir(query.env);
if (!existsSync(root)) return null;
if (layoutPath(root, query)) return true;
const search = searchProjects(root, sessionFileName(query.sessionId));
const search = searchProjects(
root,
sessionFileName(query.sessionId),
maxDirs,
);
if (search.path) return true;
// Absence is evidence only when the whole store was searched and the
// worktree's own project directory is there to have held the file.
if (!search.complete || !query.worktreePath) return null;
return existsSync(projectDir(root, query.worktreePath)) ? false : null;
},
};
} satisfies HarnessSessionStore;
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as os from "node:os";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { hasHarnessSession } from ".";
import { codexSessionStore } from "./codex";
import { readHarnessTranscript } from "./transcript";

const sessionId = "019f5cac-9077-73d2-83a3-f17e383fc705";
Expand Down Expand Up @@ -160,10 +161,12 @@ describe("codex session files", () => {
).toBeNull();

const sessions = join(home, ".codex-work", "sessions");
for (let i = 0; i < 2_000; i++)
mkdirSync(join(sessions, `d${i}`), { recursive: true });
mkdirSync(join(sessions, "day"), { recursive: true });
expect(
hasHarnessSession({ agentId: "codex", sessionId, env: workEnv() }),
codexSessionStore.hasSession(
{ sessionId, worktreePath: null, env: workEnv() },
1,
),
).toBeNull();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ function codexHome(env: HarnessEnv): string {
function findRollout(
home: string,
sessionId: string,
{ compressed }: { compressed: boolean },
{
compressed,
maxDirs = MAX_DIRS_VISITED,
}: { compressed: boolean; maxDirs?: number },
): { path: string | null; complete: boolean } {
const root = join(home, "sessions");
if (!existsSync(root)) return { path: null, complete: false };
Expand All @@ -52,7 +55,7 @@ function findRollout(
const stack = [root];
let visited = 0;
while (stack.length > 0) {
if (visited++ >= MAX_DIRS_VISITED) return { path: null, complete: false };
if (visited++ >= maxDirs) return { path: null, complete: false };
const dir = stack.pop() as string;
const entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) =>
b.name.localeCompare(a.name),
Expand Down Expand Up @@ -132,13 +135,13 @@ export const codexSessionFiles: HarnessSessionFiles = {
parseTurns,
};

export const codexSessionStore: HarnessSessionStore = {
export const codexSessionStore = {
/** Only the launch env's home: that is where a relaunch will look. */
hasSession({ sessionId, env }) {
hasSession({ sessionId, env }, maxDirs = MAX_DIRS_VISITED) {
const home = codexHome(env);
if (!existsSync(join(home, "sessions"))) return null;
const search = findRollout(home, sessionId, { compressed: true });
const search = findRollout(home, sessionId, { compressed: true, maxDirs });
if (search.path) return true;
return search.complete ? false : null;
},
};
} satisfies HarnessSessionStore;
Loading