// THE WALK'S VERDICT WRITER, outside the bench (umtool redesign L7b): one clip's verdict -- `confirmed`, or
// `incorrect` with its `correction` -- written exactly as the clip bench writes it: the clip's own token read
// (GET /api/report/clip), then the window writer (PUT /api/report/window), one write and one undo point
// (lib/report/manifest.mjs updateClip). The bench's Undo of a Confirm writes through this, and the workspace's
// review pass's Keep does; ClipBench's own Confirm keeps its in-component save, which carries a moved
// window in the same patch. Its terminal twin: `umtool window
--verdict confirmed|incorrect`.
export type ClipWrite =
| { ok: true; entry: Record; token: string; json: Record }
| { ok: false; error: string; stale: boolean };
/**
* One clip's patch through the window writer, on the clip's token as it is now. `refuse` sees the clip as that
* read gave it and may refuse the write (its words); the token is the same read's, so a write landing after it
* makes the PUT stale rather than overwritten.
*/
export async function writeClipPatch(
project: string,
clip: string,
patch: Record,
refuse?: (clip: Record) => string | null,
): Promise {
const r = await fetch(`/api/report/clip?project=${encodeURIComponent(project)}&clip=${encodeURIComponent(clip)}`, { cache: "no-store" });
if (!r.ok) return { ok: false, error: `HTTP ${r.status}`, stale: false };
const got = (await r.json().catch(() => ({}))) as { token?: string; clip?: Record };
const said = refuse?.(got.clip ?? {}) ?? null;
if (said) return { ok: false, error: said, stale: false };
const tok = got.token ?? null;
const w = await fetch("/api/report/window", {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ project, clip, token: tok, ...patch }),
});
const j = (await w.json().catch(() => ({}))) as Record;
if (!w.ok) return { ok: false, error: String(j.error ?? `HTTP ${w.status}`), stale: !!j.stale };
return { ok: true, entry: (j.entry ?? {}) as Record, token: String(j.token ?? ""), json: j };
}
/** The words a Keep is refused with over a clip the bench ruled. */
export const ruledInBench = (verdict: string) => `marked ${verdict} in the bench: the Full bench changes it`;
/**
* The walk's verdict a review pass writes: Keep (`confirmed`) -- never `incorrect`, the bench's fact-error ruling,
* and never OVER it: the clip's verdict is read at write time (the view's entry may be older than a ruling the
* bench made since), and `confirmed` is refused over `incorrect` or `rejected`.
*/
export function writeWalkVerdict(project: string, clip: string, v: { verdict: "confirmed" } | { verdict: "" }): Promise {
return writeClipPatch(project, clip, v, (c) => {
const now = typeof c.verdict === "string" ? c.verdict : null;
return v.verdict === "confirmed" && (now === "incorrect" || now === "rejected") ? ruledInBench(now) : null;
});
}
/**
* A review pass's Keep on a clip, in the order that loses nothing: the verdict first (when it is not `confirmed`
* already), and only once it is written is the Fix mark it carried taken back -- a refused or failed write leaves
* the mark, and its note, where they were. `wrote`: a verdict was written (the draft changed).
*/
export async function keepClip(o: {
confirmed: boolean;
hasMark: boolean;
writeVerdict: () => Promise;
clearMark: () => Promise;
}): Promise<{ error: string | null; wrote: boolean }> {
let wrote = false;
if (!o.confirmed) {
const w = await o.writeVerdict();
if (!w.ok) return { error: w.stale ? "the draft changed since — try again" : w.error, wrote: false };
wrote = true;
}
if (o.hasMark) {
const err = await o.clearMark();
if (err) return { error: err, wrote };
}
return { error: null, wrote };
}