commit a8d2357c34b82498b866e64dfa909d07457ee103
parent 72933ac42fbf28f2ce0ac101da7845d906786b76
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Mon, 9 Nov 2026 17:18:06 -0500
umtool: open a version in the draft, save the draft -- POST /api/report/takes/open|save (L3 S2)
The API half of the working draft: POST /api/report/takes/open runs
versions.mjs openVersion under the manifest lock -- the draft saved as the
next v<N> when it has edits since its parent, the version written over it
with one undo point (auto-before-open-<id>, every time), the pick and its pin
untouched. It takes JSON or a plain form (the Open in draft page's button:
303 to `next`, or back with ?error=). A stale token is a 409.
POST /api/report/takes/save freezes the draft as the next version.
e2e draft-versions.spec.ts (its own fixture): edit, save v1, edit, open v1 --
the draft saved as v2 first, v1 back byte for byte -- then Undo restores the
pre-open draft AND its parent (v2); a stale token is refused.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
4 files changed, 173 insertions(+), 0 deletions(-)
diff --git a/umtool/app/api/report/takes/open/route.ts b/umtool/app/api/report/takes/open/route.ts
@@ -0,0 +1,56 @@
+import { invalidateProjects, projectRef } from "@/lib/projects";
+import { refuseCaller } from "@/lib/report/caller";
+import { StaleToken, StructureRefused } from "@/lib/report/manifest.mjs";
+import { isTakeId } from "@/lib/report/takes.mjs";
+import { VersionRefused, openVersion } from "@/lib/report/versions.mjs";
+
+export const dynamic = "force-dynamic";
+
+// OPEN A VERSION IN THE DRAFT (lib/report/versions.mjs openVersion; the CLI
+// twin is `umtool takes open <p> <take>`). The draft is saved first as the
+// next version when it has edits since its parent; the version is then
+// written over the draft with one undo point ("Undo open <take>": the Cut
+// tab's Undo last edit, `umtool timeline <p> undo`). The pick is untouched.
+//
+// POST JSON { project, take, token? } -> { ok, opened, how, saved, snapshot, token }
+// POST a FORM (the Open in draft page's button, no script needed) with
+// project, take, token, next (where to land: a /projects/… address), back
+// -> 303 to `next`; refused -> 303 to `back` with `?error=<why>`.
+//
+// A stale token is a 409 (the draft changed since the page was read).
+const isForm = (request: Request) => /application\/x-www-form-urlencoded|multipart\/form-data/.test(request.headers.get("content-type") ?? "");
+const local = (v: unknown) => (typeof v === "string" && v.startsWith("/projects/") && !v.startsWith("//") ? v : null);
+
+export async function POST(request: Request) {
+ const refused = refuseCaller(request, "/api/report/takes/open");
+ if (refused) return refused;
+ const form = isForm(request);
+ let body: Record<string, unknown> | null = null;
+ if (form) {
+ const f = await request.formData().catch(() => null);
+ body = f ? Object.fromEntries([...f.entries()].map(([k, v]) => [k, typeof v === "string" ? v : ""])) : null;
+ } else body = (await request.json().catch(() => null)) as Record<string, unknown> | null;
+ if (!body || typeof body !== "object") return Response.json({ error: "bad body" }, { status: 400 });
+ const back = local(body.back);
+ const fail = (status: number, error: string, extra: Record<string, unknown> = {}) => {
+ if (form && back) return new Response(null, { status: 303, headers: { location: `${back}${back.includes("?") ? "&" : "?"}error=${encodeURIComponent(error)}` } });
+ return Response.json({ error, ...extra }, { status });
+ };
+ if (!isTakeId(body.take)) return fail(400, "take must be a take id");
+ const project = await projectRef(String(body.project ?? ""));
+ if (!project) return fail(404, "no such project");
+ const token = body.token === undefined || body.token === null || body.token === "" ? null : String(body.token);
+ try {
+ const r = await openVersion(project, body.take, { token });
+ invalidateProjects();
+ const next = local(body.next);
+ if (form) return new Response(null, { status: 303, headers: { location: next ?? `/projects/${project.id}/play` } });
+ return Response.json({ ok: true, ...r }, { headers: { "cache-control": "no-store" } });
+ } catch (e) {
+ if (e instanceof StaleToken) return fail(409, "the draft changed since this page was read — look again", { stale: true });
+ if (e instanceof StructureRefused) return fail(400, e.message, { errors: (e as unknown as { errors?: string[] }).errors ?? [] });
+ const message = e instanceof Error ? e.message : String(e);
+ if (e instanceof VersionRefused) return fail(/^no take /.test(message) ? 404 : 400, message);
+ return fail(500, message);
+ }
+}
diff --git a/umtool/app/api/report/takes/save/route.ts b/umtool/app/api/report/takes/save/route.ts
@@ -0,0 +1,29 @@
+import { invalidateProjects, projectRef } from "@/lib/projects";
+import { refuseCaller } from "@/lib/report/caller";
+import { VersionRefused, saveVersion } from "@/lib/report/versions.mjs";
+
+export const dynamic = "force-dynamic";
+
+// SAVE THE DRAFT AS A VERSION (lib/report/versions.mjs saveVersion; the CLI
+// twin is `umtool takes save <p> [--label L] [--force]`): video.manifest.json
+// frozen byte for byte as the next v<N>, named the draft's parent. Nothing is
+// saved when the draft has no edits since its parent (`saved: false`) unless
+// `force`. The pick is never touched.
+//
+// POST { project, label?, force? } -> { ok, saved, id, label?, why? }
+export async function POST(request: Request) {
+ const refused = refuseCaller(request, "/api/report/takes/save");
+ if (refused) return refused;
+ const body = (await request.json().catch(() => null)) as { project?: unknown; label?: unknown; force?: unknown } | null;
+ if (!body || typeof body !== "object") return Response.json({ error: "bad json" }, { status: 400 });
+ const project = await projectRef(String(body.project ?? ""));
+ if (!project) return Response.json({ error: "no such project" }, { status: 404 });
+ try {
+ const r = await saveVersion(project, { label: typeof body.label === "string" ? body.label : null, force: body.force === true });
+ invalidateProjects();
+ return Response.json({ ok: true, ...r }, { headers: { "cache-control": "no-store" } });
+ } catch (e) {
+ const message = e instanceof Error ? e.message : String(e);
+ return Response.json({ error: message }, { status: e instanceof VersionRefused ? 400 : 500 });
+ }
+}
diff --git a/umtool/e2e/draft-versions.spec.ts b/umtool/e2e/draft-versions.spec.ts
@@ -0,0 +1,78 @@
+import { test, expect } from "@playwright/test";
+import { readFileSync } from "node:fs";
+import path from "node:path";
+import { fileURLToPath } from "node:url";
+
+// ---------------------------------------------------------------------------
+// THE WORKING DRAFT AND ITS SAVED VERSIONS (umtool/docs/knobs.md, "The working draft and saved versions";
+// lib/report/versions.mjs): every edit lands in video.manifest.json (the draft); a saved version is frozen;
+// opening a version saves the draft first when it has edits, and one Undo puts the draft -- and its parent --
+// back.
+//
+// draft-versions-fixture (WRITES; make-fixture.mjs): play-cut-fixture's cut on vid1 (c01 c02 k01 c03 c04),
+// no takes. The tests run in order, each leaving what the next reads.
+// ---------------------------------------------------------------------------
+
+test.describe.configure({ mode: "serial" });
+
+const P = "reports/draft-versions-fixture";
+const DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "..", ".e2e-song", "reports", "draft-versions-fixture");
+const manifestText = () => readFileSync(path.join(DIR, "video.manifest.json"), "utf8");
+const order = () => (JSON.parse(manifestText()) as { timeline: { id: string }[] }).timeline.map((e) => e.id).join(" ");
+const draftFrom = () => (JSON.parse(readFileSync(path.join(DIR, "takes", "draft.json"), "utf8")) as { from: string }).from;
+const base = (id: string) => readFileSync(path.join(DIR, "takes", id, "base.manifest.json"), "utf8");
+const takeDoc = (id: string) => JSON.parse(readFileSync(path.join(DIR, "takes", id, "take.json"), "utf8")) as Record<string, unknown>;
+
+test("edit → save v1 → edit → open v1: the draft saved as v2 first, v1 back byte for byte; Undo restores the pre-open draft and its parent", async ({ request }) => {
+ expect(order()).toBe("c01 c02 k01 c03 c04");
+ const original = manifestText();
+ // An edit, then saved as v1: frozen byte for byte.
+ let r = await request.post("/api/report/timeline", { data: { project: P, op: "move", id: "c04", toIndex: 0 } });
+ expect(r.ok(), await r.text()).toBe(true);
+ const edited = manifestText();
+ expect(edited).not.toBe(original);
+ r = await request.post("/api/report/takes/save", { data: { project: P, label: "First pass" } });
+ expect(r.ok(), await r.text()).toBe(true);
+ expect(await r.json()).toMatchObject({ saved: true, id: "v1", label: "First pass" });
+ expect(base("v1")).toBe(edited);
+ expect(takeDoc("v1")).toMatchObject({ version: true, draft: true, patch: null, window: "full" });
+ expect(draftFrom()).toBe("v1");
+ // Saving again with no edits saves nothing.
+ r = await request.post("/api/report/takes/save", { data: { project: P } });
+ expect(await r.json()).toMatchObject({ saved: false, id: "v1" });
+
+ // Another edit: v1 does not follow it.
+ r = await request.post("/api/report/timeline", { data: { project: P, op: "move", id: "c01", toIndex: 4 } });
+ expect(r.ok(), await r.text()).toBe(true);
+ const preOpen = manifestText();
+ expect(order()).toBe("c04 c02 k01 c03 c01");
+ expect(base("v1")).toBe(edited);
+
+ // Open v1: the draft (1 edit) is saved as v2 first, then v1 is the draft.
+ r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1" } });
+ expect(r.ok(), await r.text()).toBe(true);
+ const o = await r.json();
+ expect(o).toMatchObject({ opened: "v1", saved: "v2", how: "frozen" });
+ expect(base("v2")).toBe(preOpen);
+ expect(takeDoc("v2")).toMatchObject({ version: true, draftedFrom: "v1" });
+ expect(manifestText()).toBe(edited);
+ expect(draftFrom()).toBe("v1");
+ // Opening it again writes nothing.
+ r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1" } });
+ expect(await r.json()).toMatchObject({ unchanged: true });
+
+ // Undo: exactly the open -- the pre-open draft, and its parent (v2, what it was saved as).
+ r = await request.post("/api/report/timeline", { data: { project: P, op: "undo" } });
+ expect(r.ok(), await r.text()).toBe(true);
+ expect((await r.json()).op).toBe("open-v1");
+ expect(manifestText()).toBe(preOpen);
+ expect(draftFrom()).toBe("v2");
+});
+
+test("the CLI twin's contract: a stale token is refused, the draft untouched", async ({ request }) => {
+ const before = manifestText();
+ const r = await request.post("/api/report/takes/open", { data: { project: P, take: "v1", token: "1" } });
+ expect(r.status()).toBe(409);
+ expect((await r.json()).stale).toBe(true);
+ expect(manifestText()).toBe(before);
+});
diff --git a/umtool/e2e/fixtures/make-fixture.mjs b/umtool/e2e/fixtures/make-fixture.mjs
@@ -2134,6 +2134,16 @@ makePlayCutFixture({ reports, deckManifest, cachedWindow: path.join(REPORT, "out
writeFileSync(path.join(pwDir, "takes", "verdicts.json"), JSON.stringify({ "draft-0": { verdict: null, note: "", at: "2026-10-30T11:00:00.000Z", picked: true } }, null, 2) + "\n");
}
+// The working draft and its saved versions (draft-versions.spec.ts, WRITES): play-cut-fixture's cut on vid1,
+// no takes -- the spec saves v1, edits, opens v1 (the draft saved as v2 first) and undoes it.
+{
+ const { dir: dvDir } = makePlayCutFixture({
+ reports, deckManifest, cachedWindow: path.join(REPORT, "out", "clips-raw", "vid1_0.00-9.00.mp4"),
+ name: "draft-versions-fixture", title: "The Draft Versions Fixture", video: "vid1",
+ });
+ rmSync(path.join(dvDir, "takes"), { recursive: true, force: true });
+}
+
// TWO MORE cached windows for vid1, and they are the regression rather than
// decoration.
//