fix(cli): preserve live task-isolation sandboxes on worktree clear
`omp worktree clear` (without `--all`) removed every task-isolation dir under the worktree base, including sandboxes owned by subagents running right now, and the "no live task owns it" reason was asserted from the mere presence of the `m` mount dir with no ownership check. `ensureIsolation` now stamps each sandbox base dir with a pid-bearing ownership marker before the backend materialises `m`, and the worktree scanner classifies a sandbox as live while its owning process is alive, so `clear` reclaims only crashed leftovers. Fixes #6761
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "bun:test";
|
||||
import * as fs from "node:fs/promises";
|
||||
import * as os from "node:os";
|
||||
import * as path from "node:path";
|
||||
import { clearWorktrees } from "@oh-my-pi/pi-coding-agent/cli/worktree-cli";
|
||||
import { ISOLATION_OWNER_FILE, writeIsolationOwner } from "@oh-my-pi/pi-coding-agent/task/isolation-ownership";
|
||||
import { setWorktreesDir } from "@oh-my-pi/pi-utils";
|
||||
|
||||
/**
|
||||
* Regression for #6761: `omp worktree clear` (no `--all`) must delete only
|
||||
* task-isolation sandboxes whose owner process is gone. A sandbox owned by a
|
||||
* live omp process holds a running subagent's uncaptured work and must survive.
|
||||
*/
|
||||
describe("worktree clear task-isolation ownership", () => {
|
||||
let base: string;
|
||||
let savedEnv: string | undefined;
|
||||
|
||||
beforeEach(async () => {
|
||||
base = await fs.mkdtemp(path.join(os.tmpdir(), "omp-wt-clear-"));
|
||||
savedEnv = process.env.OMP_WORKTREE_DIR;
|
||||
delete process.env.OMP_WORKTREE_DIR;
|
||||
setWorktreesDir(base);
|
||||
vi.spyOn(console, "log").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
setWorktreesDir(undefined);
|
||||
if (savedEnv === undefined) delete process.env.OMP_WORKTREE_DIR;
|
||||
else process.env.OMP_WORKTREE_DIR = savedEnv;
|
||||
vi.restoreAllMocks();
|
||||
await fs.rm(base, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
async function makeSandbox(name: string): Promise<string> {
|
||||
const dir = path.join(base, name);
|
||||
await fs.mkdir(path.join(dir, "m"), { recursive: true });
|
||||
await Bun.write(path.join(dir, "m", "work.txt"), "uncaptured\n");
|
||||
return dir;
|
||||
}
|
||||
|
||||
/** A pid that has been spawned and reaped, so `kill(pid, 0)` reports ESRCH. */
|
||||
async function deadPid(): Promise<number> {
|
||||
const proc = Bun.spawn(["true"], { stdout: "ignore", stderr: "ignore" });
|
||||
await proc.exited;
|
||||
return proc.pid;
|
||||
}
|
||||
|
||||
it("keeps live-owned sandboxes and reclaims dead/markerless/corrupt ones", async () => {
|
||||
const live = await makeSandbox("tlive0001");
|
||||
await writeIsolationOwner(live, "live0001"); // marker names this test process
|
||||
|
||||
const dead = await makeSandbox("tdead0002");
|
||||
await Bun.write(path.join(dead, ISOLATION_OWNER_FILE), JSON.stringify({ pid: await deadPid(), id: "dead0002" }));
|
||||
|
||||
const orphan = await makeSandbox("tnone0003"); // no marker at all (crashed pre-marker run)
|
||||
|
||||
const corrupt = await makeSandbox("tbad00004");
|
||||
await Bun.write(path.join(corrupt, ISOLATION_OWNER_FILE), "{ not json");
|
||||
|
||||
await clearWorktrees({ all: false, dryRun: false, json: true });
|
||||
|
||||
const exists = async (p: string): Promise<boolean> =>
|
||||
await fs.stat(p).then(
|
||||
() => true,
|
||||
() => false,
|
||||
);
|
||||
expect(await Bun.file(path.join(live, "m", "work.txt")).exists()).toBe(true);
|
||||
expect(await exists(dead)).toBe(false);
|
||||
expect(await exists(orphan)).toBe(false);
|
||||
expect(await exists(corrupt)).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user