// A JOB THAT HANDS ITS WORK ON TO ANOTHER, AS ITS CALLER SEES IT. // // A whisper-video downloads the audio on its platform's queue, then queues the // transcription as a transcribe-one job of its own and ends — so the platform // queue is free the moment the bytes are down, while the transcription waits // for a worker (videoActions.ts whisperVideoAction). The job records where // the work went (JobRunContext.handOff → `handedOffTo`); this is the other // half, for the caller holding the action's result: ONE stream, the first // job's log and then the second's, and a `done` that settles when the work // does — so a run panel reads "… Transcribe done" as it did when the two // were one job. // // const slot = handOffSlot(); // const r = await runManagedFunction({ …, fn: async (…, ctx) => { // const child = await queueTheRest(); // if (child.ok) { ctx.handOff(child.jobId); slot.set(child); } // } }); // return r.ok ? slot.wrap(r) : r; // // A consumer that goes away (the ops route cancels the stream at once) cancels // the second stream too, the moment it exists: an unread job stream would // otherwise buffer a long transcription's whole log. Cancelling a stream never // stops a job (streamCommand.ts). import { getRegistry } from "./registry"; import type { JobDoneResult, StreamActionResult } from "./streamCommand"; export type StartedJob = Extract; export type HandOffSlot = { // Inside the first job: the job it handed its work on to. set: (child: StartedJob) => void; // The first job's result, as its caller should hold it. wrap: (parent: StartedJob) => StartedJob; }; export function handOffSlot(): HandOffSlot { let child: StartedJob | null = null; let consumerGone = false; return { set(c) { child = c; if (consumerGone) void c.stream.cancel().catch(() => {}); }, wrap(parent) { let current = parent.stream.getReader(); let onChild = false; const stream = new ReadableStream({ async pull(controller) { for (;;) { const { value, done } = await current.read(); if (!done) { controller.enqueue(value); return; } if (!onChild && child) { onChild = true; current = child.stream.getReader(); continue; } controller.close(); return; } }, cancel() { consumerGone = true; void current.cancel().catch(() => {}); if (child && !onChild) void child.stream.cancel().catch(() => {}); }, }); const done: Promise = parent.done.then((r) => r.status === "done" && child ? child.done : r, ); return { ...parent, stream, done }; }, }; } // The job doing `id`'s work now: `id` itself, or — while it ended `done` // having handed its work on — the job it named, followed down the chain. What // a run panel's Cancel cancels (jobs/actions.ts cancelJobAction): the panel is // still showing the transcription the whisper-video handed off. export function jobDoingTheWork(id: string): string { const registry = getRegistry(); let target = id; for (let hops = 0; hops < 5; hops++) { const r = registry.get(target); if (r?.status !== "done" || !r.handedOffTo) break; target = r.handedOffTo; } return target; }