commit 3ad7e707c1ceff37e084b146ab025ad5d257beab
parent 8d12e1a8ea5f6b71b10694df27f89bbe953ede23
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 12 Oct 2026 16:55:55 -0400
umtool /drafts: cancel a running step — its whole process tree, SIGTERM then SIGKILL after 5 s
An editor job the step had handed over (a clip fetch, a voice) finishes on the editor,
cached for a re-run; the log says so.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
3 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/umtool/app/api/drafts/route.ts b/umtool/app/api/drafts/route.ts
@@ -1,5 +1,5 @@
import {
- apiKeyReady, createRun, createVersion, listChannels, listRuns, PRESETS, readPolicy, runFile, startStep, STEPS, writePolicy,
+ apiKeyReady, cancelStep, createRun, createVersion, listChannels, listRuns, PRESETS, readPolicy, runFile, startStep, STEPS, writePolicy,
type GenPolicy, type Step,
} from "@/lib/drafts";
@@ -46,6 +46,10 @@ export async function POST(request: Request) {
startStep(m.name, "sweep");
return Response.json({ ok: true, name: m.name });
}
+ if (b.action === "cancel" && (STEPS as readonly string[]).includes(b.step)) {
+ cancelStep(String(b.name), b.step as Step);
+ return Response.json({ ok: true });
+ }
if (b.action === "policy") {
return Response.json({ ok: true, policy: writePolicy(b.policy as GenPolicy) });
}
diff --git a/umtool/components/drafts/DraftsPanel.tsx b/umtool/components/drafts/DraftsPanel.tsx
@@ -27,6 +27,7 @@ const stateColor: Record<string, string> = {
running: "text-amber-500",
done: "text-emerald-500",
failed: "text-red-500",
+ cancelled: "text-[var(--color-dim)] line-through",
};
const label = (m: string) => (m.startsWith("claude-") ? `${m} (API key)` : `${m} (local)`);
@@ -322,6 +323,17 @@ export default function DraftsPanel() {
{s === "sweep" && r.kind && r.kind !== "sweep" ? "revise" : s}
</button>
<span className={`text-[11px] ${stateColor[r.steps[s].state]}`}>{r.steps[s].state}</span>
+ {r.steps[s].state === "running" && (
+ <button
+ className={`${btn} border-red-700 text-red-500`}
+ title="stop this step (an editor job it started finishes on the editor)"
+ onClick={() => {
+ if (confirm(`Cancel ${s} on ${r.name}?`)) post({ action: "cancel", name: r.name, step: s });
+ }}
+ >
+ cancel
+ </button>
+ )}
</span>
))}
{r.files.map((f) => (
diff --git a/umtool/lib/drafts.ts b/umtool/lib/drafts.ts
@@ -1,4 +1,4 @@
-import { spawn } from "node:child_process";
+import { execFileSync, spawn } from "node:child_process";
import { existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -36,7 +36,7 @@ export type RunMeta = {
project?: string;
};
-export type StepState = { state: "idle" | "running" | "done" | "failed"; exit?: number; tail?: string };
+export type StepState = { state: "idle" | "running" | "done" | "failed" | "cancelled"; exit?: number; tail?: string };
export type RunView = RunMeta & {
steps: Record<Step, StepState>;
files: string[];
@@ -71,7 +71,9 @@ function stepState(dir: string, step: Step): StepState {
const tail = readFileSync(log, "utf8").split("\n").slice(-14).join("\n");
const ex = path.join(dir, `ui-${step}.exit`);
if (!existsSync(ex)) return { state: "running", tail };
- const exit = Number(readFileSync(ex, "utf8").trim());
+ const raw = readFileSync(ex, "utf8").trim();
+ if (raw === "cancelled") return { state: "cancelled", tail };
+ const exit = Number(raw);
return { state: exit === 0 ? "done" : "failed", exit, tail };
}
@@ -295,9 +297,50 @@ export function startStep(name: string, step: Step, narrateModel?: string): void
stdio: "ignore",
env: { ...process.env, ...envFor(meta), UI_LOG: log, UI_EXIT: ex, TMPDIR: tmp, PYTHONDONTWRITEBYTECODE: "1" },
});
+ writeFileSync(path.join(dir, `ui-${step}.pid`), String(child.pid));
child.unref();
}
+/** Every descendant of a pid (ps), children first. */
+function descendants(pid: number): number[] {
+ const rows = execFileSync("ps", ["-eo", "pid=,ppid="], { encoding: "utf8" })
+ .trim().split("\n").map((l) => l.trim().split(/\s+/).map(Number));
+ const out: number[] = [];
+ const walk = (p: number) => {
+ for (const [c, pp] of rows) if (pp === p) { walk(c); out.push(c); }
+ };
+ walk(pid);
+ return out;
+}
+
+/**
+ * Cancel a running step: SIGTERM its whole process tree (the wrapper, the heavy-slot queue, the script and what it
+ * runs), then SIGKILL what is left after 5 s. Its exit file says "cancelled". A job the step had already handed to
+ * the EDITOR (a clip fetch, a voice) is the editor's: it finishes there, and its result is cached for a re-run.
+ */
+export function cancelStep(name: string, step: Step): void {
+ const dir = runDir(name);
+ if (stepState(dir, step).state !== "running") throw new Error(`${step} is not running`);
+ const pid = Number(readFileSync(path.join(dir, `ui-${step}.pid`), "utf8").trim());
+ const ex = path.join(dir, `ui-${step}.exit`);
+ const tree = pid > 0 ? [...descendants(pid), pid] : [];
+ writeFileSync(ex, "cancelled");
+ const kill = (sig: NodeJS.Signals) => {
+ for (const p of tree) {
+ try {
+ process.kill(p, sig);
+ } catch {}
+ }
+ };
+ kill("SIGTERM");
+ writeFileSync(path.join(dir, `ui-${step}.log`),
+ `${readFileSync(path.join(dir, `ui-${step}.log`), "utf8")}\n[cancelled ${new Date().toISOString()} — editor jobs it started finish on the editor (cached for a re-run)]\n`);
+ setTimeout(() => {
+ kill("SIGKILL");
+ writeFileSync(ex, "cancelled"); // the wrapper may have written its own exit code on the way down
+ }, 5000).unref?.();
+}
+
/** Whether Claude can be asked: a key in the env or the key file (claude_api.py reads the same two). */
export function apiKeyReady(): boolean {
return !!process.env.ANTHROPIC_API_KEY || existsSync(path.join(os.homedir(), ".config/archilyzer/anthropic.env"));