Skip to content
Merged
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
53 changes: 33 additions & 20 deletions packages/agent-eval/src/run/clone.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import {
access,
chmod,
mkdtemp,
mkdir,
readdir,
rm,
writeFile
} from "node:fs/promises";
import { access, cp, chmod, mkdtemp, mkdir, readdir, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { simpleGit } from "simple-git";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest";

import { cloneTarget } from "./clone.js";

Expand All @@ -25,6 +17,8 @@ interface FixtureRepo {
}

let roots: string[] = [];
let fixtureRoot: string;
let fixtureTemplate: FixtureRepo;

async function tempRoot(): Promise<string> {
const root = await mkdtemp(path.join(tmpdir(), "agent-eval-clone-"));
Expand Down Expand Up @@ -57,6 +51,16 @@ async function createFixtureRepo(root: string): Promise<FixtureRepo> {
return { sourceRepo, bareRepo, headSha };
}

async function copyFixtureRepo(root: string): Promise<FixtureRepo> {
const sourceRepo = path.join(root, "source");
const bareRepo = path.join(root, "fixture.git");
await Promise.all([
cp(fixtureTemplate.sourceRepo, sourceRepo, { recursive: true }),
cp(fixtureTemplate.bareRepo, bareRepo, { recursive: true })
]);
return { sourceRepo, bareRepo, headSha: fixtureTemplate.headSha };
}

async function commitFixtureChange(
sourceRepo: string,
fileName: string,
Expand All @@ -76,6 +80,15 @@ async function expectMissing(target: string): Promise<void> {
}

describe("cloneTarget", () => {
beforeAll(async () => {
fixtureRoot = await mkdtemp(path.join(tmpdir(), "agent-eval-clone-template-"));
fixtureTemplate = await createFixtureRepo(fixtureRoot);
});

afterAll(async () => {
await rm(fixtureRoot, { recursive: true, force: true });
});

beforeEach(() => {
roots = [];
});
Expand All @@ -86,7 +99,7 @@ describe("cloneTarget", () => {

it("clones a ref and returns the resolved HEAD sha", async () => {
const root = await tempRoot();
const fixture = await createFixtureRepo(root);
const fixture = await copyFixtureRepo(root);
const dest = path.join(root, "clone");

const result = await cloneTarget({
Expand All @@ -101,7 +114,7 @@ describe("cloneTarget", () => {

it("reuses a cached bare repo for repeated worktrees", async () => {
const root = await tempRoot();
const fixture = await createFixtureRepo(root);
const fixture = await copyFixtureRepo(root);
const cacheDir = path.join(root, "cache");

await expect(
Expand Down Expand Up @@ -129,7 +142,7 @@ describe("cloneTarget", () => {

it("fetches cached bare repos before creating later worktrees", async () => {
const root = await tempRoot();
const fixture = await createFixtureRepo(root);
const fixture = await copyFixtureRepo(root);
const cacheDir = path.join(root, "cache");

await expect(
Expand Down Expand Up @@ -161,7 +174,7 @@ describe("cloneTarget", () => {

it("reuses a cached destination after its previous worktree was deleted", async () => {
const root = await tempRoot();
const fixture = await createFixtureRepo(root);
const fixture = await copyFixtureRepo(root);
const cacheDir = path.join(root, "cache");
const dest = path.join(root, "same-dest");

Expand All @@ -188,7 +201,7 @@ describe("cloneTarget", () => {

it("cleans up the destination when an in-flight clone is aborted", async () => {
const root = await tempRoot();
const fixture = await createFixtureRepo(root);
const fixture = await copyFixtureRepo(root);
const wrapperDir = path.join(root, "bin");
const dest = path.join(root, "aborted");
const originalPath = process.env.PATH;
Expand All @@ -200,13 +213,13 @@ describe("cloneTarget", () => {
path.join(wrapperDir, "git"),
[
"#!/bin/sh",
"if [ \"$1\" = \"clone\" ]; then",
" dest=\"\"",
" for arg in \"$@\"; do dest=\"$arg\"; done",
" mkdir -p \"$dest/.git\"",
'if [ "$1" = "clone" ]; then',
' dest=""',
' for arg in "$@"; do dest="$arg"; done',
' mkdir -p "$dest/.git"',
" sleep 2",
"fi",
"exec \"$REAL_GIT\" \"$@\"",
'exec "$REAL_GIT" "$@"',
""
].join("\n")
);
Expand Down
56 changes: 48 additions & 8 deletions packages/agent-harness-tools/src/run-poe-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@poe-code/poe-code-config";
import type { RunHandle, RunResult, RunSpec } from "@poe-code/process-runner";
import { createFsFromVolume, Volume } from "memfs";
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import type {
DownloadResult,
ExecutionEnvFactory,
Expand Down Expand Up @@ -112,15 +112,16 @@ function createMockEnv(
},
exec(spec): RunHandle {
env.execSpecs.push(spec);
if (spec.command === "sh" && spec.args?.[0] === "-c") {
void writeExitFile(rawFs.promises, spec, commandExitCode);
}
const executionResult =
spec.command === "sh" && spec.args?.[0] === "-c"
? writeExitFile(rawFs.promises, spec, commandExitCode).then(() => result)
: result;
return {
pid: 123,
stdout: null,
stderr: null,
stdin: null,
result,
result: executionResult,
kill() {}
};
},
Expand Down Expand Up @@ -255,6 +256,46 @@ describe("runPoeCommand", () => {
});
});

it("does not poll for a wrapped command exit file after its process completes", async () => {
vi.useFakeTimers();
const { state } = createRecordingState();
const env = createMockEnv();

try {
await expect(
runPoeCommand({
factory: createFactory(env),
openSpec: createOpenSpec(),
detach: false,
state
})
).resolves.toMatchObject({ kind: "sync", exitCode: 0 });
} finally {
vi.useRealTimers();
}
});

it("does not poll for a wrapped command exit file when cancellation remains inactive", async () => {
vi.useFakeTimers();
const { state } = createRecordingState();
const env = createMockEnv();
const controller = new AbortController();

try {
await expect(
runPoeCommand({
factory: createFactory(env),
openSpec: createOpenSpec(),
detach: false,
state,
signal: controller.signal
})
).resolves.toMatchObject({ kind: "sync", exitCode: 0 });
} finally {
vi.useRealTimers();
}
});

it("leaves the environment open in detach mode", async () => {
const { state, statuses } = createRecordingState();
const runResult = deferred<RunResult>();
Expand Down Expand Up @@ -373,9 +414,8 @@ describe("runPoeCommand", () => {
};
env.exec = (spec): RunHandle => {
env.execSpecs.push(spec);
const nextIteration = remoteIterations.trim().length === 0
? 1
: remoteIterations.trim().split("\n").length + 1;
const nextIteration =
remoteIterations.trim().length === 0 ? 1 : remoteIterations.trim().split("\n").length + 1;
remoteIterations += `${nextIteration}\n`;
return {
pid: 123,
Expand Down
33 changes: 16 additions & 17 deletions packages/agent-harness-tools/src/run-poe-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function runPoeCommand(opts: {
stdout: execution?.stdout ?? "pipe",
stderr: execution?.stderr ?? "pipe",
signal: opts.signal
});
});

if (execution?.input !== undefined) {
writeExecutionInput(handle, execution.input);
Expand Down Expand Up @@ -479,7 +479,13 @@ function createAbortSync(

if (signal === undefined) {
return {
waitForExit: (env, jobId) => waitForExit(toLogStreamEnv(env), jobId),
waitForExit: async (env, jobId) => {
await handle.result;
if (timedOut) {
throw createActivityTimeoutError(activityTimeoutMs!);
}
return waitForExit(toLogStreamEnv(env), jobId);
},
waitForHandle: async () => {
const result = await handle.result;
if (timedOut) {
Expand All @@ -494,7 +500,6 @@ function createAbortSync(
};
}

const exitWaitController = new AbortController();
let aborted = signal.aborted;
const abortedPromise = new Promise<void>((resolve) => {
notifyAbort = resolve;
Expand All @@ -517,26 +522,21 @@ function createAbortSync(
return handle.result;
}

const exit = waitForExit(toLogStreamEnv(env), jobId, {
signal: exitWaitController.signal
}).then(
(value) => ({ kind: "exit" as const, value }),
(error: unknown) => ({ kind: "error" as const, error })
);
const result = await Promise.race([
exit,
handle.result.then((value) => ({ kind: "exit" as const, value })),
abortedPromise.then(() => ({ kind: "abort" as const }))
]);

if (result.kind === "exit") {
return result.value;
}

if (result.kind === "error") {
throw result.error;
if (timedOut) {
throw createActivityTimeoutError(activityTimeoutMs!);
}
if (aborted) {
return result.value;
}
return waitForExit(toLogStreamEnv(env), jobId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Keep aborts active while polling for wrapped exits

When a caller supplies an AbortSignal, this branch waits for the wrapper process to finish and then calls waitForExit without any abort signal. If the exit artifact is missing or delayed after handle.result resolves, a later cancellation can no longer interrupt the polling loop, so runPoeCommand can hang indefinitely instead of honoring the caller's abort; the previous implementation kept the exit wait abortable while polling.

Useful? React with 👍 / 👎.

}

exitWaitController.abort();
return handle.result;
},
async waitForHandle() {
Expand All @@ -563,7 +563,6 @@ function createAbortSync(
resetActivityTimer,
dispose() {
if (activityTimer) clearTimeout(activityTimer);
exitWaitController.abort();
signal.removeEventListener("abort", kill);
}
};
Expand Down
Loading