// THE GOAL BOARD (6 Nov 2026, the operator: "in a sequence of multiple goals // such as at the 7m clip, show each goal on the dial, and prioritize the // closest one rather than the most ambitious one since failing the easiest // goal would be the most interesting"): every goal in force at the playhead, // the CLOSEST of them as the headline, and each one whose deadline passed // unmet, held a moment as MISSED. // // active every goal (`goal`, `goal_loss`) of his the cut has shown (an // `unresolved` row -- a joke -- is no goal), not // yet met, replaced or past its deadline -- in the order set. // A newer goal with the same HORIZON replaces the older one: the // same deadline day, else the same deadline words, else both // undated ("eventual" goals stay until one replaces them). // headline the active goal with the least loss still needed from his // latest stated weight (to a range's top); a goal with no weight // (an unresolved "lose 75") by its loss; ties to the soonest // deadline, then the latest set. Not the most ambitious, not // simply the latest. // missed the goals whose deadline the record's date (the latest date of // the events shown so far) has passed with no stated weight of his // at or under it by then, while his latest stated weight is over // it -- each `{ ...goal, at, wasHeadline }`, held MISS_HOLD s from // `at`, then gone. A goal whose deadline passes with his latest // weight under it (a reading after the day: we cannot say) or that // a reading meets in time leaves quietly. // // A goal: `{ row, kind, t, said, target, range, loss, display, deadline, // deadlineDate, key }` -- `target` the weight it comes to (a `goal_loss` // resolved against its base by the slice: the state's `goal.value`), null // when unresolved; `deadlineDate` the ledger's `due`, else the date his // deadline words name (measure-extras.mjs deadlineDate), else the row's // `asOf` (the day a goal is for: "320 by his birthday", asOf 2026-04-01). // // PURE and SEEK-SAFE: goalBoard is a function of the schedule's measure alone; // goalsAt(board, t) is the board at a cut second. The dial draws it // (chrome-instruments.mjs); any page that shows a goal reads the same // `headline` from here. import { deadlineDate } from "./measure-extras.mjs"; /** How long a missed goal stays on the dial, struck, before it goes (s). */ export const MISS_HOLD = 6; const ISO_RE = /^\d{4}-\d{2}-\d{2}$/; const isIso = (v) => typeof v === "string" && ISO_RE.test(v); const own = (e) => !(typeof e?.speaker === "string" && e.speaker.trim()); const r4 = (v) => Math.round(v * 10000) / 10000; /** The weight a goal must be at or under to be met: a range's top, else its target. */ export const goalEdge = (g) => (Array.isArray(g?.range) && g.range.length === 2 ? Math.max(g.range[0], g.range[1]) : g?.target ?? null); /** Pounds still to lose from `cur` to goal `g` (its loss when it has no weight), or Infinity when unknown. */ export function goalNeed(g, cur) { const edge = goalEdge(g); if (Number.isFinite(edge) && Number.isFinite(cur)) return r4(cur - edge); if (Number.isFinite(g?.loss)) return g.loss; return Infinity; } /** * The closest of `goals` from weight `cur`: the least still needed (a goal * already under `cur`'s reach -- need 0 or less -- after every one still * ahead), ties to the soonest deadline (undated last), then the latest set. */ export function headlineOf(goals, cur) { if (!goals.length) return null; const rank = (g) => { const n = goalNeed(g, cur); return n > 0 ? n : 1e9 - n; }; return [...goals].sort((a, b) => rank(a) - rank(b) || String(a.deadlineDate ?? "9999").localeCompare(String(b.deadlineDate ?? "9999")) || b.t - a.t)[0]; } /** A goal event as the board carries it, with the state after it (`s`, for a goal_loss's resolution). */ function goalOf(e, s) { const kind = e.kind; const resolved = s?.goal && s.goal.row === e.row ? s.goal : null; const target = kind === "goal" && Number.isFinite(Number(e.value)) && e.value !== null ? Number(e.value) : Number.isFinite(resolved?.value) ? resolved.value : null; const loss = kind === "goalLoss" && Number.isFinite(Number(e.value)) ? Math.abs(Number(e.value)) : Number.isFinite(resolved?.loss) ? resolved.loss : null; const words = typeof e.deadline === "string" && e.deadline.trim() ? e.deadline.trim() : null; const date = isIso(e.due) ? e.due : (words ? deadlineDate(words, e.date) : null) ?? (isIso(e.asOf) ? e.asOf : null); const range = Array.isArray(e.range) && e.range.length === 2 ? [...e.range] : Array.isArray(resolved?.range) ? [...resolved.range] : null; return { row: e.row ?? null, kind, t: e.t, said: isIso(e.date) ? e.date : null, target, ...(range ? { range } : {}), loss, display: e.display ?? null, deadline: words, deadlineDate: date, key: date ? `d:${date}` : words ? `w:${words.toLowerCase()}` : "eventual", }; } const sameBoard = (a, b) => a && b && a.headline?.row === b.headline?.row && a.active.map((g) => g.row).join("|") === b.active.map((g) => g.row).join("|") && a.missed.map((g) => g.row).join("|") === b.missed.map((g) => g.row).join("|"); /** * The goal board over the cut: `[{ t, active, headline, missed, cur }]`, one * per change, the first at t 0 (nothing yet), each the board from its `t` to * the next one's. `cur` is his latest stated weight then (null before one). * * @param {{ events?: object[], state?: object[] }} measure the schedule's measure slice * @param {{ hold?: number }} [opts] how long a missed goal is held (MISS_HOLD) */ export function goalBoard(measure, { hold = MISS_HOLD } = {}) { const events = Array.isArray(measure?.events) ? measure.events : []; const states = Array.isArray(measure?.state) ? measure.state : []; const steps = events .map((e, i) => ({ e, s: states[i + 1] ?? states[states.length - 1] ?? null, i })) .filter(({ e }) => Number.isFinite(e?.t) && own(e) && e.unresolved !== true) .sort((a, b) => a.e.t - b.e.t || a.i - b.i); let active = []; let cur = null; let recordDate = null; const misses = []; const points = [{ t: 0, active: [], headline: null, cur: null }]; for (const { e, s } of steps) { if (isIso(e.date) && (recordDate === null || e.date > recordDate)) recordDate = e.date; const before = headlineOf(active, cur); if (e.kind === "goal" || e.kind === "goalLoss") { const g = goalOf(e, s); active = [...active.filter((x) => x.key !== g.key), g]; } else if (e.kind === "stated" && Number.isFinite(Number(e.value)) && e.value !== null) { cur = Number(e.value); // Met: a reading of his at or under it, said once it was set and by its day. A ">300" says nothing under. if (e.qualifier !== ">") { active = active.filter((g) => { const edge = goalEdge(g); const inTime = !g.deadlineDate || !isIso(e.date) || e.date <= g.deadlineDate; const after = !g.said || !isIso(e.date) || e.date >= g.said; return !(Number.isFinite(edge) && cur <= edge + 1e-9 && inTime && after); }); } } // Past its day: missed when his latest weight is over it, else gone without a word. if (recordDate) { const kept = []; for (const g of active) { if (g.deadlineDate && recordDate > g.deadlineDate) { const edge = goalEdge(g); if (Number.isFinite(edge) && Number.isFinite(cur) && cur > edge) { misses.push({ ...g, at: e.t, wasHeadline: before?.row === g.row }); } } else kept.push(g); } active = kept; } points.push({ t: e.t, active: [...active], headline: headlineOf(active, cur), cur }); } // Every second the board changes: each step, and each missed goal's end. const times = [...new Set([...points.map((p) => p.t), ...misses.map((m) => r4(m.at + hold))])].sort((a, b) => a - b); const at = (t) => { let p = points[0]; for (const q of points) if (q.t <= t + 1e-9) p = q; else break; return p; }; const out = []; for (const t of times) { const p = at(t); const b = { t, active: p.active, headline: p.headline, missed: misses.filter((m) => m.at <= t + 1e-9 && t < m.at + hold - 1e-9), cur: p.cur }; if (!out.length || !sameBoard(out[out.length - 1], b)) out.push(b); } return out; } /** The board at cut second `t` (goalBoard's state in force then). */ export function goalsAt(board, t) { let b = board[0] ?? { t: 0, active: [], headline: null, missed: [], cur: null }; for (const x of board) if (x.t <= t + 1e-9) b = x; else break; return b; }