diff --git a/tests/runner-e2e/everyday-flow.ts b/tests/runner-e2e/everyday-flow.ts index 47cf65c97b5..a6edebe8545 100644 --- a/tests/runner-e2e/everyday-flow.ts +++ b/tests/runner-e2e/everyday-flow.ts @@ -22,7 +22,7 @@ import type { MatrixExecution } from "./types.js"; import { submitTaskReply } from "./user-actions.js"; import { createTaskFromPromptThroughUi } from "./plan-task-ui.js"; import { waitForTaskChatRendered, waitForTaskIdentity } from "./continuation-screenshot.js"; -import { hasPersistedSource, isSavedSourceCheckpoint } from "./everyday-interruption.js"; +import { hasPersistedSource, isSavedSourceCheckpoint, readWorkspaceSource } from "./everyday-interruption.js"; import { collectRunEvents } from "./run-observations.js"; import { gradeRemoteRestart, observeRemoteRestartBoundary, projectRemoteRestartEvents, projectRemoteRestartRun, waitForRemoteRestartBoundary, type RemoteRestartEvidence } from "./everyday-remote-restart.js"; import { controllerOutageForExecution } from "./controller-outage.js"; @@ -222,16 +222,22 @@ export async function runEverydayFlow(input: Input) { parentBlockedObserved: boolean; } | undefined; async function workspaceFiles() { - const workspaceRoot = input.workspacePath; + // Include project folders so a stop check still sees edits when the agent + // saved the project below the workspace root (see readWorkspaceSource). const files: Record = {}; - for (const entry of await readdir(workspaceRoot, { - withFileTypes: true, - })) { - if (entry.isFile() && /\.(py|md|zip)$/.test(entry.name)) - files[entry.name] = createHash("sha256") - .update(await readFile(path.join(workspaceRoot, entry.name))) - .digest("hex"); - } + const visit = async (relative: string, depth: number) => { + for (const entry of await readdir(path.join(input.workspacePath, relative), { withFileTypes: true })) { + const name = path.join(relative, entry.name); + if (entry.isFile() && /\.(py|md|zip)$/.test(entry.name)) + files[name] = createHash("sha256") + .update(await readFile(path.join(input.workspacePath, name))) + .digest("hex"); + else if (entry.isDirectory() && depth < 2 && !entry.name.startsWith(".") + && !["node_modules", "__pycache__", "venv"].includes(entry.name)) + await visit(name, depth + 1); + } + }; + await visit("", 0); return files; } async function runnerStopped(run: StoryRun) { @@ -529,14 +535,13 @@ export async function runEverydayFlow(input: Input) { requireActive = false, maxWaitMs = 180_000, ) { - const filePath = path.join(input.workspacePath, "slugify.py"); const bytes = await pollUntil({ label: requireActive ? "active run with saved source" : "saved source after interruption", deadlineAt: Math.min(input.deadlineAt, Date.now() + maxWaitMs), intervalMs: 500, load: async () => { await refresh(); - const source = await readFile(filePath).catch(() => undefined); + const source = await readWorkspaceSource(input.workspacePath); return { active: ev.runs.some(isActiveStoryRun), source }; }, accept: ({ active, source }) => @@ -1763,11 +1768,10 @@ export async function runEverydayFlow(input: Input) { .map((r) => ({ runId: r.id, code: r.errorCode })), }); if (execution.environment.id === "local") { + const saved = await readWorkspaceSource(input.workspacePath); try { - const source = await readFile( - path.join(input.workspacePath, "slugify.py"), - "utf8", - ); + if (!saved) throw new Error("No saved slugify.py in the workspace"); + const source = saved.toString("utf8"); await input.evidence("source-after-interruption.json", { body: source, sha256: createHash("sha256").update(source).digest("hex"), diff --git a/tests/runner-e2e/everyday-interruption.test.ts b/tests/runner-e2e/everyday-interruption.test.ts new file mode 100644 index 00000000000..7cafebbc9a6 --- /dev/null +++ b/tests/runner-e2e/everyday-interruption.test.ts @@ -0,0 +1,49 @@ +import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { afterEach, describe, expect, it } from "vitest"; +import { readWorkspaceSource } from "./everyday-interruption.js"; + +const roots: string[] = []; +async function workspace(files: Record) { + const root = await mkdtemp(path.join(tmpdir(), "everyday-interruption-")); + roots.push(root); + for (const [name, body] of Object.entries(files)) { + await mkdir(path.dirname(path.join(root, name)), { recursive: true }); + await writeFile(path.join(root, name), body); + } + return root; +} +afterEach(async () => { + await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))); +}); + +describe("readWorkspaceSource", () => { + it("reads slugify.py at the workspace root", async () => { + const root = await workspace({ "slugify.py": "root" }); + expect((await readWorkspaceSource(root))?.toString()).toBe("root"); + }); + + it("finds slugify.py the agent saved in a project folder inside the workspace", async () => { + // The brief says "in slugify.py ... Work in the project workspace"; it does not + // require the workspace root, and agents legitimately create a project folder. + const root = await workspace({ "slugify-tool/slugify.py": "nested", "slugify-tool/README.md": "readme" }); + expect((await readWorkspaceSource(root))?.toString()).toBe("nested"); + }); + + it("prefers the root copy and ignores caches, hidden folders, and deep paths", async () => { + const root = await workspace({ "slugify.py": "root", "a/slugify.py": "nested" }); + expect((await readWorkspaceSource(root))?.toString()).toBe("root"); + const ignored = await workspace({ + ".git/slugify.py": "hidden", + "node_modules/x/slugify.py": "deps", + "a/b/c/slugify.py": "too deep", + }); + expect(await readWorkspaceSource(ignored)).toBeUndefined(); + }); + + it("chooses the shallowest, then alphabetically first, nested copy", async () => { + const root = await workspace({ "b/slugify.py": "b", "a/slugify.py": "a", "a/x/slugify.py": "deeper" }); + expect((await readWorkspaceSource(root))?.toString()).toBe("a"); + }); +}); diff --git a/tests/runner-e2e/everyday-interruption.ts b/tests/runner-e2e/everyday-interruption.ts index 9d6611c5d41..e8c3c5d143b 100644 --- a/tests/runner-e2e/everyday-interruption.ts +++ b/tests/runner-e2e/everyday-interruption.ts @@ -1,3 +1,6 @@ +import { readFile, readdir } from "node:fs/promises"; +import path from "node:path"; + export function hasPersistedSource(value: Buffer | undefined): boolean { return Boolean(value && value.length > 0); } @@ -9,3 +12,34 @@ export function isSavedSourceCheckpoint( ): boolean { return active && hasPersistedSource(value); } + +const SOURCE_FILE = "slugify.py"; +const SOURCE_SEARCH_DEPTH = 2; +const SKIPPED_DIRECTORIES = new Set(["node_modules", "__pycache__", "venv"]); + +/** + * Reads the slugify source the agent saved in the task workspace. The brief + * asks for `slugify.py` "in the project workspace", not at its root, so agents + * may reasonably create a project folder. Prefer the root copy, then the + * shallowest (alphabetically first) copy in a bounded set of subfolders. + */ +export async function readWorkspaceSource( + workspaceRoot: string, +): Promise { + let level = [workspaceRoot]; + for (let depth = 0; depth <= SOURCE_SEARCH_DEPTH && level.length; depth += 1) { + const next: string[] = []; + for (const directory of level) { + const source = await readFile(path.join(directory, SOURCE_FILE)).catch(() => undefined); + if (source) return source; + const entries = await readdir(directory, { withFileTypes: true }).catch(() => []); + next.push(...entries + .filter((entry) => entry.isDirectory() && !entry.name.startsWith(".") && !SKIPPED_DIRECTORIES.has(entry.name)) + .map((entry) => entry.name) + .sort() + .map((name) => path.join(directory, name))); + } + level = next; + } + return undefined; +}