// CLAIM-GROUP PROPOSALS (lane L4, 2026-11-02): `/group-proposals.json`, // beside a umtool video project's video.manifest.json. // // { "version": 1, "promptVersion": "propose-groups-1", "at": ISO, "model": "qwen3:8b", // "request"?: "…", the operator's words for the ask, when given // "groups": [ { "id": "p1", "section": 1, "claim": "…", "ids": ["ow1", "a01"], "why"?: "…", // "status": "proposed" | "accepted" | "rejected" | "failed", "at"?: ISO, "error"?: "…" } ], // "refused": [ { "group": {…}, "why": "…" } ] } // // Written whole by the editor's `propose-groups` task (a new ask replaces the // file); each group's `status` marked by umtool's Accept and Reject. PROPOSALS // ONLY: nothing here is applied by being here. Accept writes the group through // umtool's structural writer (lib/report/manifest.mjs setGroup), which checks // it again against the manifest as it is then (claimGroups.mjs validateGroups). // // checkGroupProposals is the task's check of a model's answer, PURE and // self-contained (common never imports umtool): the cut's sections are read // from `render.chrome.deck.markers` by umtool's own rule (sectionEdit.mjs // sectionModel) -- a marker `{after}` opens the next section right after that // clip, markers placed by where their clip stands; none, one section 0. // // Tmp + rename, so a reader never sees half a file. Plain ESM with a .d.mts. import { readFile, rename, writeFile } from "node:fs/promises"; import path from "node:path"; import { tmpPathFor } from "yt-dlp-transcript-common/lib/atomicFile.mjs"; export const GROUP_PROPOSALS_NAME = "group-proposals.json"; export const GROUPS_PROMPT_VERSION = "propose-groups-1"; export const GROUP_PROPOSAL_STATUSES = Object.freeze(["proposed", "accepted", "rejected", "failed"]); /** A claim's characters, at most (umtool claimGroups.mjs GROUP_CLAIM_MAX). */ export const PROPOSED_CLAIM_MAX = 160; /** Groups one answer may propose, at most. */ export const PROPOSED_GROUPS_MAX = 200; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const isClip = (e) => !!e && (e.kind ?? e.type) === "clip"; const fileOf = (dir) => path.join(/* turbopackIgnore: true */ dir, GROUP_PROPOSALS_NAME); /** * The cut as a proposal is checked against: its clips in order (each id * once), each clip's section, the sections' labels, and the clips already in * a group. * * @param {unknown} manifest */ export function cutSections(manifest) { const m = isObj(manifest) ? manifest : {}; const tl = Array.isArray(m.timeline) ? m.timeline : []; const order = []; const pos = new Map(); tl.forEach((e, i) => { if (isClip(e) && typeof e.id === "string" && !pos.has(e.id)) { pos.set(e.id, i); order.push(e.id); } }); const chrome = isObj(m.render) && isObj(m.render.chrome) ? m.render.chrome : null; const markers = chrome && isObj(chrome.deck) && Array.isArray(chrome.deck.markers) ? chrome.deck.markers : []; const placed = []; markers.forEach((mk, k) => { const p = isObj(mk) ? pos.get(String(mk.after ?? "").trim()) : undefined; if (p !== undefined) placed.push({ k, mk, pos: p }); }); placed.sort((a, b) => a.pos - b.pos || a.k - b.k); const labels = [null, ...placed.map((p) => (typeof p.mk.label === "string" && p.mk.label.trim() ? p.mk.label.trim() : null))]; const sectionOf = new Map(); let s = 0; let next = 0; tl.forEach((e, i) => { if (isClip(e) && !sectionOf.has(e.id)) sectionOf.set(e.id, s); while (next < placed.length && placed[next].pos === i) { next += 1; s = next; } }); const grouped = new Map(); for (const g of Array.isArray(m.groups) ? m.groups : []) { if (!isObj(g) || !Array.isArray(g.ids)) continue; for (const id of g.ids) if (typeof id === "string") grouped.set(id, String(g.id ?? "a group")); } return { order, sectionOf, labels, grouped }; } /** * A model's (or a session's) answer, checked against the cut as it is now: * a proposed group is kept only when its ids are clips of the cut, together * (no other clip between them; a card does not count), in one section, in no * group already and in no other kept proposal, and its claim is a sentence of * at most 160 characters. Each other one is refused, in words. * * @param {unknown} manifest * @param {unknown} answer `{ groups: [{ section?, claim, ids, why? }] }` * @returns {{ groups: Array<{ section: number, claim: string, ids: string[], why?: string }>, refused: Array<{ group: unknown, why: string }> }} */ export function checkGroupProposals(manifest, answer) { const cut = cutSections(manifest); const at = new Map(cut.order.map((id, k) => [id, k])); const list = isObj(answer) && Array.isArray(answer.groups) ? answer.groups : []; const groups = []; const refused = []; const taken = new Map(); const refuse = (group, why) => refused.push({ group, why }); for (const g of list.slice(0, PROPOSED_GROUPS_MAX)) { if (!isObj(g)) { refuse(g, "a group is {section, claim, ids, why}"); continue; } const claim = typeof g.claim === "string" ? g.claim.replace(/\s+/g, " ").trim() : ""; if (!claim) { refuse(g, "it has no claim"); continue; } if ([...claim].length > PROPOSED_CLAIM_MAX) { refuse(g, `its claim is ${[...claim].length} characters (at most ${PROPOSED_CLAIM_MAX})`); continue; } const ids = Array.isArray(g.ids) ? [...new Set(g.ids.map((x) => String(x ?? "").trim()).filter(Boolean))] : []; if (!ids.length) { refuse(g, "it names no clip"); continue; } const unknown = ids.filter((id) => !at.has(id)); if (unknown.length) { refuse(g, `${unknown.join(", ")} ${unknown.length === 1 ? "is not a clip" : "are not clips"} of the cut`); continue; } const inGroup = ids.filter((id) => cut.grouped.has(id)); if (inGroup.length) { refuse(g, `${inGroup.map((id) => `${id} (${cut.grouped.get(id)})`).join(", ")} ${inGroup.length === 1 ? "is" : "are"} in a group already`); continue; } const sorted = ids.slice().sort((a, b) => at.get(a) - at.get(b)); const lo = at.get(sorted[0]); const hi = at.get(sorted[sorted.length - 1]); if (hi - lo + 1 !== sorted.length) { const between = cut.order.slice(lo, hi + 1).filter((id) => !ids.includes(id)); refuse(g, `its clips are not together: ${between.join(", ")} ${between.length === 1 ? "stands" : "stand"} between ${sorted[0]} and ${sorted[sorted.length - 1]}`); continue; } const secs = [...new Set(sorted.map((id) => cut.sectionOf.get(id)))].sort((a, b) => a - b); if (secs.length > 1) { refuse(g, `it spans §${secs.join(" and §")}: a group never crosses a section boundary`); continue; } const twice = ids.filter((id) => taken.has(id)); if (twice.length) { refuse(g, `${twice.join(", ")} ${twice.length === 1 ? "is" : "are"} in another proposed group (${[...new Set(twice.map((id) => taken.get(id)))].join(", ")})`); continue; } const why = typeof g.why === "string" ? g.why.replace(/\s+/g, " ").trim().slice(0, 300) : ""; const kept = { section: secs[0], claim, ids: sorted, ...(why ? { why } : {}) }; const label = `the proposal "${claim.slice(0, 40)}"`; for (const id of sorted) taken.set(id, label); groups.push(kept); } if (list.length > PROPOSED_GROUPS_MAX) refuse({ more: list.length - PROPOSED_GROUPS_MAX }, `only the first ${PROPOSED_GROUPS_MAX} groups are read`); return { groups, refused }; } /** The file's document from one ask: the kept groups, each given an id and the status "proposed". */ export function groupProposalsDocOf({ at, model, request = null, checked }) { return { version: 1, promptVersion: GROUPS_PROMPT_VERSION, at, model, ...(request ? { request: String(request).replace(/\s+/g, " ").trim().slice(0, 2000) } : {}), groups: (checked?.groups ?? []).map((g, i) => ({ id: `p${i + 1}`, ...g, status: "proposed" })), refused: (checked?.refused ?? []).map((r) => ({ group: r.group, why: r.why })), }; } /** The file's problems (empty: it can be read as proposals). */ export function groupProposalsErrors(doc) { if (!isObj(doc)) return ["not an object"]; const errors = []; if (doc.version !== 1) errors.push("version is not 1"); if (typeof doc.model !== "string") errors.push("no model"); if (!Array.isArray(doc.groups)) errors.push("no groups list"); else doc.groups.forEach((g, i) => { if (!isObj(g) || typeof g.id !== "string" || typeof g.claim !== "string" || !Array.isArray(g.ids) || !Number.isInteger(g.section) || !GROUP_PROPOSAL_STATUSES.includes(g.status)) { errors.push(`groups[${i}] is not { id, section, claim, ids, status }`); } }); if (!Array.isArray(doc.refused)) errors.push("no refused list"); return errors; } /** `{ doc, error }`: the proposals, or null (no file, or one that is not proposals: `error` says why). */ export async function readGroupProposals(dir) { let raw; try { raw = await readFile(fileOf(dir), "utf8"); } catch { return { doc: null, error: null }; } try { const doc = JSON.parse(raw); const errors = groupProposalsErrors(doc); return errors.length ? { doc: null, error: `${GROUP_PROPOSALS_NAME}: ${errors.join("; ")}` } : { doc, error: null }; } catch (e) { return { doc: null, error: `${GROUP_PROPOSALS_NAME}: ${e instanceof Error ? e.message : String(e)}` }; } } /** Write the whole file (tmp + rename). Returns its path. */ export async function writeGroupProposals(dir, doc) { const errors = groupProposalsErrors(doc); if (errors.length) throw new Error(`not group proposals: ${errors.join("; ")}`); const file = fileOf(dir); const tmp = tmpPathFor(file); await writeFile(tmp, `${JSON.stringify(doc, null, 2)}\n`, "utf8"); await rename(tmp, file); return file; } /** The doc with each group of `marks` (`{ [id]: { status, error?, group? } }`) marked at `at`; ids not there are ignored. */ export function markGroupProposals(doc, marks, at) { return { ...doc, groups: doc.groups.map((g) => { const m = marks[g.id]; if (!m) return g; const { error: _e, ...rest } = g; return { ...rest, status: m.status, at, ...(m.error ? { error: String(m.error).slice(0, 400) } : {}), ...(m.group ? { group: String(m.group) } : {}) }; }), }; }