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
3 changes: 2 additions & 1 deletion packages/app/src/exe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export function stopAgentCommand(connection: ExeConnection, agentName: string):
/** Revalidate terminal, run directory, and lease immediately before closing a pane. */
export function garbageCollectPaneCommand(connection: ExeConnection, paneId: string, terminalId: string, runPath: string, lease: string): string {
const herdr = herdrBinary(connection), pane = shellAtom(paneId);
return [herdrPrefix(connection), `pane_json=$(${herdr} pane get ${pane})`, `printf '%s' "$pane_json" | jq -e --arg pane ${pane} --arg terminal ${shellAtom(terminalId)} '(.result.pane // .result) | .pane_id == $pane and .terminal_id == $terminal' >/dev/null`, `test -d ${shellAtom(runPath)}`, `test "$(cat ${shellAtom(`${runPath}/.factorize-lease`)})" = ${shellAtom(lease)}`, `agent=$(${herdr} agent get ${pane} 2>/dev/null || true)`, `status=$(printf '%s' "$agent" | jq -r '.result.agent.agent_status // .result.agent.state // empty' | tr '[:upper:]' '[:lower:]')`, `case "$status" in ''|idle|done) ;; *) exit 1 ;; esac`, `${herdr} pane close ${pane}`].join(" && ");
const runsDirectory = `${connection.cwd.replace(/\/$/, "")}/.factorize-runs`;
return [herdrPrefix(connection), `test ${shellAtom(runPath)} != ${shellAtom(runsDirectory)}`, `case ${shellAtom(runPath)} in ${shellAtom(`${runsDirectory}/`)}*) ;; *) exit 1 ;; esac`, `pane_json=$(${herdr} pane get ${pane})`, `printf '%s' "$pane_json" | jq -e --arg pane ${pane} --arg terminal ${shellAtom(terminalId)} '(.result.pane // .result) | .pane_id == $pane and .terminal_id == $terminal' >/dev/null`, `test -d ${shellAtom(runPath)}`, `test "$(cat ${shellAtom(`${runPath}/.factorize-lease`)})" = ${shellAtom(lease)}`, `agent=$(${herdr} agent get ${pane} 2>/dev/null || true)`, `status=$(printf '%s' "$agent" | jq -r '.result.agent.agent_status // .result.agent.state // empty' | tr '[:upper:]' '[:lower:]')`, `case "$status" in ''|idle|done) ;; *) exit 1 ;; esac`, `${herdr} pane close ${pane}`, `rm -rf -- ${shellAtom(runPath)}`, `test ! -e ${shellAtom(runPath)}`].join(" && ");
}

export function herdrCheckCommand(connection: ExeConnection): string {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ export class TenantV2 extends DurableObject<Env> {
}
this.commandActivity(run.id, "pane garbage collection", collected);
if (collected.ok && (collected.exitCode === null || collected.exitCode === 0)) {
this.ctx.storage.sql.exec("UPDATE runs SET pane_collected=1,pane_collection_attempt=?,pane_collection_next_at=NULL,worktree_disposition='retained',updated_at=? WHERE id=?", attempt, now(), run.id);
this.ctx.storage.sql.exec("UPDATE runs SET pane_collected=1,pane_collection_attempt=?,pane_collection_next_at=NULL,worktree_disposition='deleted',updated_at=? WHERE id=?", attempt, now(), run.id);
return;
}
this.schedulePaneCollection(run.id, attempt);
Expand Down
11 changes: 10 additions & 1 deletion packages/app/test/workspace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,21 @@ describe("flow workspaces", () => {
expect(command).toContain("-- '--model' 'gpt 5'");
});

it("garbage collects only after terminal, run directory, and lease revalidation", () => {
it("garbage collects the pane and run directory only after ownership revalidation", () => {
const command = garbageCollectPaneCommand({ vmName: "vm", apiToken: "token", agentKind: "codex", cwd: "/repo" }, "w1:p2", "term-7", "/repo/.factorize-runs/run", "lease-7");
expect(command).toContain(".terminal_id == $terminal");
expect(command).toContain("factorize-lease");
expect(command).not.toContain("agent stop");
expect(command).toContain("pane close 'w1:p2'");
expect(command).toContain("rm -rf -- '/repo/.factorize-runs/run'");
expect(command).toContain("test ! -e '/repo/.factorize-runs/run'");
expect(command.indexOf("pane close 'w1:p2'")).toBeLessThan(command.indexOf("rm -rf -- '/repo/.factorize-runs/run'"));
});

it("refuses to garbage collect outside the per-run directory", () => {
const command = garbageCollectPaneCommand({ vmName: "vm", apiToken: "token", agentKind: "codex", cwd: "/repo" }, "w1:p2", "term-7", "/repo", "lease-7");
expect(command).toContain("case '/repo' in '/repo/.factorize-runs/'*");
expect(command.indexOf("case '/repo' in")).toBeLessThan(command.indexOf("pane close 'w1:p2'"));
});

it("quotes the complete exe.dev SSH request body", () => {
Expand Down