commit 456e0b8dbb93bc4f766da807a10c4e6aefd10af5
parent daa6f119a15b886ea90c671c76809a48df6d2fe0
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 19 Oct 2026 14:19:52 -0400
clips: Find clips follows lane B's final clips route — a busy umtool is waited for, href, segment and notes kept
A 409 that names umtool's running job is asked again every 30 s (up to an
hour) instead of failing the step; any other 409 (the slug exists) still
refuses. The answer's `href` is the record's URL, its `segment` (auto-scope's
choice) is recorded when none was asked, and its `notes` are logged.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
5 files changed, 103 insertions(+), 17 deletions(-)
diff --git a/common/controller/findClips.test.ts b/common/controller/findClips.test.ts
@@ -85,7 +85,7 @@ type Harness = {
log: string[];
};
-function harness(dir: string, o: { umtool?: "ok" | "down" | "refuses" | "fails"; paused?: boolean; pauseHeld?: string[]; chatFails?: boolean } = {}): Harness {
+function harness(dir: string, o: { umtool?: "ok" | "down" | "refuses" | "fails" | "busy" | "exists"; paused?: boolean; pauseHeld?: string[]; chatFails?: boolean } = {}): Harness {
const h: Harness = { actions: [], umtoolCalls: [], words: [], diarized: 0, log: [], deps: null as unknown as FindClipsDeps };
let n = 0;
const editor = {
@@ -110,7 +110,22 @@ function harness(dir: string, o: { umtool?: "ok" | "down" | "refuses" | "fails";
if (o.umtool === "down") throw new TypeError("fetch failed (ECONNREFUSED)");
if (method === "POST") {
if (o.umtool === "refuses") return Response.json({ error: "no diarization.json for synth-react/v" }, { status: 400 });
- return Response.json({ jobId: "u1", project: "v-clips", created: true, segments: SEGMENTS });
+ if (o.umtool === "exists") return Response.json({ error: "project v-clips already exists" }, { status: 409 });
+ if (o.umtool === "busy" && h.umtoolCalls.filter((c) => c.method === "POST").length < 3) {
+ return Response.json({ error: "a job is already running (clips-analyze)", running: { id: "u0" } }, { status: 409 });
+ }
+ return Response.json({
+ ok: true,
+ jobId: "u1",
+ job: { id: "u1" },
+ project: "v-clips",
+ created: true,
+ segments: SEGMENTS,
+ segment: "s1",
+ scope: [290, 6300],
+ href: "/projects/v-clips/clips",
+ notes: ["words.json copied in"],
+ });
}
polls++;
if (polls === 1) return Response.json({ state: "running", log: ["analyze: laughs 12"], next: 1 });
@@ -233,6 +248,9 @@ test("every step run: each in order, named in the log, outputs written, umtool f
assert.equal(r.project, "v-clips");
assert.equal(r.url, "http://127.0.0.1:9/projects/v-clips/clips");
assert.equal(r.created, true);
+ assert.equal(r.segment, "s1", "the segment auto-scope chose");
+ assert.match(text, /umtool: made project v-clips \(segment s1\) \(job u1\)/);
+ assert.match(text, /umtool: note: words\.json copied in/);
assert.deepEqual(r.segments.map((s) => s.id), ["s1", "s2"]);
assert.deepEqual(r.steps.map((s) => s.action), ["ran", "ran", "ran", "ran", "ran", "ran"]);
});
@@ -273,6 +291,7 @@ test("umtool down, refusing, or failing: a clear failure naming the step", async
["down", /^step umtool: umtool at http:\/\/127\.0\.0\.1:9 did not answer \(fetch failed \(ECONNREFUSED\)\) — is it running\?$/],
["refuses", /^step umtool: umtool refused the clips request \(HTTP 400\): no diarization\.json/],
["fails", /^step umtool: umtool job u1 failed: CED-mini exited 1$/],
+ ["exists", /^step umtool: umtool refused the clips request \(HTTP 409\): project v-clips already exists$/],
] as const) {
const id = `um${mode}`.padEnd(11, "0");
const dir = await video(id);
@@ -281,6 +300,17 @@ test("umtool down, refusing, or failing: a clear failure naming the step", async
}
});
+test("a busy umtool (409 naming its running job) is waited for, then asked again", async () => {
+ const id = "umbusy00001";
+ const dir = await video(id);
+ await complete(dir);
+ const h = harness(dir, { umtool: "busy" });
+ const r = await run(h, id);
+ assert.equal(r.project, "v-clips");
+ assert.equal(h.umtoolCalls.filter((c) => c.method === "POST").length, 3);
+ assert.equal(h.log.filter((l) => /umtool is busy \(a job is already running \(clips-analyze\)\); waiting for it/.test(l)).length, 1);
+});
+
test("a twitch video skips the chat with the sentence; a refused chat fetch is logged and the chain goes on", async () => {
const tw = await video("twitch00001", "https://www.twitch.tv/videos/123");
await complete(tw);
diff --git a/common/controller/findClips.ts b/common/controller/findClips.ts
@@ -264,6 +264,8 @@ export type FindClipsDeps = {
// whilePaused run (diarizeOne.ts diarizeOneVideo).
diarizeInline?: (o: { onLog: (line: string) => void; signal: AbortSignal }) => Promise<DiarizeOneOutcome>;
pollMs?: number;
+ // The pause before asking a busy umtool again (default 30 s).
+ busyWaitMs?: number;
sleep?: (ms: number) => Promise<void>;
now?: () => Date;
// Called inside the job after the record is written (the editor revalidates
@@ -272,6 +274,9 @@ export type FindClipsDeps = {
};
const TERMINAL = new Set(["done", "failed", "cancelled"]);
+// How many times a busy umtool (409 naming its running job) is asked again,
+// `busyWaitMs` (30 s) apart: an hour.
+const UMTOOL_BUSY_TRIES = 120;
class StepError extends Error {
constructor(step: FindClipsStepId, message: string) {
@@ -353,7 +358,16 @@ async function runAction(
// ─── umtool ───
-type UmtoolNew = { jobId: string; project: string; created: boolean; segments: FindClipsSegment[] };
+type UmtoolNew = {
+ jobId: string;
+ project: string;
+ created: boolean;
+ segments: FindClipsSegment[];
+ // The segment umtool chose (the one asked, else auto-scope's), and the
+ // clip editor's path ("/projects/<id>/clips").
+ segment?: string;
+ href?: string;
+};
function segmentsOf(v: unknown): FindClipsSegment[] {
if (!Array.isArray(v)) return [];
@@ -386,14 +400,25 @@ async function runUmtool(
const doFetch = deps.fetch ?? fetch;
const endpoint = `${o.umtool}/api/report/clips`;
const headers = { authorization: `Bearer ${o.token}`, "content-type": "application/json" };
- let res: Response;
- try {
- res = await doFetch(endpoint, { method: "POST", headers, body: JSON.stringify(o.body), signal: o.signal });
- } catch (e) {
- if (o.signal.aborted) throw new StepError("umtool", "cancelled");
- throw new StepError("umtool", `umtool at ${o.umtool} did not answer (${(e as Error).message}) — is it running?`);
+ const sleep = sleeper(deps);
+ let res!: Response;
+ let posted: Record<string, unknown> = {};
+ // umtool runs one job at a time: a 409 that names the running job is a
+ // wait, not a refusal (the run is retried until umtool is free); any other
+ // 409 (the project's slug exists) is a refusal.
+ for (let tries = 0; ; tries++) {
+ try {
+ res = await doFetch(endpoint, { method: "POST", headers, body: JSON.stringify(o.body), signal: o.signal });
+ } catch (e) {
+ if (o.signal.aborted) throw new StepError("umtool", "cancelled");
+ throw new StepError("umtool", `umtool at ${o.umtool} did not answer (${(e as Error).message}) — is it running?`);
+ }
+ posted = await umtoolJson(res);
+ if (res.status !== 409 || !posted.running || tries >= UMTOOL_BUSY_TRIES) break;
+ if (tries === 0) o.onLog(`umtool is busy (${typeof posted.error === "string" ? posted.error : "a job is running"}); waiting for it`);
+ await sleep(deps.busyWaitMs ?? 30_000);
+ if (o.signal.aborted) throw new StepError("umtool", "cancelled while waiting for umtool");
}
- const posted = await umtoolJson(res);
if (!res.ok) {
const why = typeof posted.error === "string" ? posted.error : `HTTP ${res.status}`;
throw new StepError("umtool", `umtool refused the clips request (HTTP ${res.status}): ${why}`);
@@ -404,10 +429,14 @@ async function runUmtool(
if (!jobId || !project) throw new StepError("umtool", `umtool's answer named no job or project: ${JSON.stringify(posted).slice(0, 300)}`);
const created = posted.created !== false;
let segments = segmentsOf(posted.segments);
- o.onLog(`umtool: ${created ? "made" : "re-analyzing"} project ${project} (job ${jobId})`);
+ const segment = typeof posted.segment === "string" && posted.segment ? posted.segment : undefined;
+ const href = typeof posted.href === "string" && posted.href.startsWith("/") ? posted.href : undefined;
+ o.onLog(`umtool: ${created ? "made" : "re-analyzing"} project ${project}${segment ? ` (segment ${segment})` : ""} (job ${jobId})`);
+ if (Array.isArray(posted.notes)) {
+ for (const n of posted.notes) if (typeof n === "string" && n.trim()) o.onLog(`umtool: note: ${n}`);
+ }
if (segments.length) o.onLog(`umtool: segments ${segments.map((s) => `${s.id} ${Math.round(s.from)}–${Math.round(s.to)} s`).join(", ")}`);
- const sleep = sleeper(deps);
const pollMs = deps.pollMs ?? 2000;
let since = 0;
let failures = 0;
@@ -439,7 +468,7 @@ async function runUmtool(
const why = typeof view.error === "string" && view.error ? view.error : `the job ended ${state}`;
throw new StepError("umtool", `umtool job ${jobId} failed: ${why}`);
}
- return { jobId, project, created, segments, state };
+ return { jobId, project, created, segments, state, ...(segment ? { segment } : {}), ...(href ? { href } : {}) };
}
}
await sleep(pollMs);
@@ -614,9 +643,9 @@ export async function runFindClips(
});
return {
project: u.project,
- url: umtoolProjectUrl(umtool, u.project),
+ url: u.href ? `${umtool}${u.href}` : umtoolProjectUrl(umtool, u.project),
created: u.created,
- ...(req.segment ? { segment: req.segment } : {}),
+ ...(req.segment ?? u.segment ? { segment: req.segment ?? u.segment } : {}),
segments: u.segments,
steps,
umtoolJob: u.jobId,
diff --git a/common/lib/findClipsRecord-server.ts b/common/lib/findClipsRecord-server.ts
@@ -35,7 +35,8 @@ export type FindClipsRecord = {
project: string;
url: string;
created: boolean;
- // The segment this run was scoped to, when one was asked for.
+ // The segment this run was scoped to: the one asked for, else the one
+ // umtool's auto-scope chose (its answer's `segment`).
segment?: string;
// Every narrated-video segment umtool found in the recording.
segments: FindClipsSegment[];
diff --git a/common/lib/umtoolUrl.test.ts b/common/lib/umtoolUrl.test.ts
@@ -0,0 +1,24 @@
+import { test } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtemp, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import path from "node:path";
+import { umtoolUrl } from "./umtoolUrl";
+
+test("umtoolUrl: UMTOOL_URL without its trailing slashes, null when unset; a test's loopback origin from the file", async () => {
+ assert.equal(umtoolUrl({}), null);
+ assert.equal(umtoolUrl({ UMTOOL_URL: " " }), null);
+ assert.equal(umtoolUrl({ UMTOOL_URL: "http://localhost:3050//" }), "http://localhost:3050");
+ const dir = await mkdtemp(path.join(tmpdir(), "umtool-url-"));
+ try {
+ const file = path.join(dir, ".fake-umtool-origin");
+ const env = { E2E_UMTOOL_ORIGIN_FILE: file, UMTOOL_URL: "http://localhost:3050" };
+ assert.equal(umtoolUrl(env), "http://localhost:3050", "no file yet");
+ await writeFile(file, "http://127.0.0.1:41234/\n");
+ assert.equal(umtoolUrl(env), "http://127.0.0.1:41234");
+ await writeFile(file, "https://umtool.example.com");
+ assert.equal(umtoolUrl(env), "http://localhost:3050", "not loopback: ignored");
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+});
diff --git a/plans/FACTS.md b/plans/FACTS.md
@@ -10875,7 +10875,9 @@ parsed to 0 cues, indexing the video textless) is CLOSED by `200a3105` (transcri
- **The umtool call** (lane B's route): `POST ${umtoolUrl()}/api/report/clips` with `authorization: Bearer
WORKER_TOKEN`, body `{mode: "new", slug: <project ?? projectSlugFor(id, segment)>, from: "<slug>/<id>", target?,
segment? | scope: "auto", wordsPath?}` → reads `jobId` (or `job.id`), `project`, `created` (≠ false), `segments`
- `[{id, from, to, subject?, host?, why?}]`; then `GET ?job=<id>&since=<n>` every 2 s, relaying `log[]` as
+ `[{id, from, to, subject?, host?, why?}]`, `segment` (the chosen one, recorded when none was asked), `href`
+ (`/projects/<id>/clips`, the record's url), `notes[]` (logged `umtool: note: …`); a 409 carrying `running` (umtool
+ busy) is asked again every 30 s up to an hour, any other 409 (slug exists) refuses; then `GET ?job=<id>&since=<n>` every 2 s, relaying `log[]` as
`umtool: <line>`, `next` the cursor, until `state !== "running"` (`done` ok, else `error`); 15 failed polls or a
404 fail the step; cancel posts `?cancel=<job>`. `projectSlugFor("fGSRhEWc9M4", "s2")` = `fgsrhewc9m4-clips-s2`.
`umtoolUrl()` (`common/lib/umtoolUrl.ts`) = `UMTOOL_URL` minus trailing slashes, or the loopback origin in