/** * The board's lane vocabulary, and the one table that decides what each lane means. * * Five lanes, and a card is in exactly one of them: `backlog` is the pool nobody holds, * `plan` is where a contract is written and approved, `execute` is where the work happens, * `blocked` is a card waiting on a person, and `done` is closed. A lane is not a status * among others — an approval and a review are facts about a card, not places it can be — * so every surface reads this table rather than spelling a label, a colour, or a rule of * its own. The page receives it in the snapshot, the tool's schema is generated from * {@link LANES}, and `card.ts` reads the ownership invariant off `ownerless`. * * {@link TRANSITIONS} is the other half: the lanes a move may join, and what that move * requires first. `rules.ts` enforces the requirements and the tool stops carrying a * second copy of the list, which is how one surface came to offer a move another refused. * * Value-only: no I/O, no clock. A caller that has a card reads `card.lane`, and a route is a * sentence about that lane rather than something the caller invents. */ import type { Card } from "./card.ts"; import { overLimit, sanitizeLine } from "../format.ts"; /** The lanes, in the order the page, the rollup, and the transcript read them. */ export const LANES = ["backlog", "plan", "execute", "blocked", "done"] as const; /** One lane a card can sit in. */ export type Lane = (typeof LANES)[number]; /** * How a card records its plan again, which depends on the lane it is in. * * `contract` is written in `plan` and nowhere else, so a refusal that names it from `execute` would * send a reader into another refusal — and from the pool the missing step is the claim. A finished * card cannot be re-planned at all; that branch is unreachable through a gate, and says the fact * rather than a route so the table is complete for its type. * * Here rather than beside the plan gates, because it is a lane rule and every refusal that names a * door has to agree: `noVerifyCommand` in `lead.ts` used to spell the route itself, and named * `contract` to a card in `execute`, where `contract` is refused (#602). */ export function contractRoute(card: Pick): string { switch (card.lane) { case "plan": return "record it with `contract`."; case "backlog": return "claim it, then record it with `contract`."; case "blocked": case "execute": return "move it back to plan, then record it with `contract`."; case "done": return "a finished card's plan cannot change."; } } /** * When the card's work began: its first entry into `execute`, or its creation. * * A landing is a merge that landed *after* the work started, and the board reads that window off * the card. The lane's own entry is the wrong end of it: a card put back through the pool and * re-claimed after its branch merged is the same work continued, and `laneSince` — which answers * how long the card has been where it is — would put the merge before the window and make the * close impossible (#670). * * The first entry can be gone: the log is capped, and its oldest lines are the first to drop. The * fallback is creation, which is earlier rather than later, so the window is never *narrower* than * the work — and a merge is only ever named when it carries the reviewed commit, which is a commit * of this card's own branch and cannot predate the card. */ export function workStartedAt(card: Pick): number { const first = card.log?.find((entry) => entry.lane === "execute"); return first?.at ?? card.createdAt; } /** * The colours a lane may wear: the page's kit names them as properties, and * `extensions/board/tone.ts` bridges them to the terminal's own roles — the two * vocabularies are not shared, which is what that bridge exists for. */ export type LaneRole = "muted" | "warn" | "accent" | "danger" | "success"; /** What one lane means, in the words every surface uses. */ export interface LaneRule { /** The word a head, a row, and a sentence print: one name for one lane. */ label: string; /** What an empty tray says, so a lane with no work reads as a fact rather than a blank. */ empty: string; /** The colour the lane's dot, its reason line, and its dialog state take. */ role: LaneRole; /** * Whether a card in this lane has no owner. * * `backlog` is the pool, so a card nobody has taken belongs there; every other lane is * somebody's, which is what a claim, a release, and `isCard`'s invariant turn on. */ ownerless: boolean; /** Whether the lane is the end of the line: nothing moves out of it. */ terminal: boolean; /** Whether the lane admits cards by pull, and so is held to a WIP limit. */ pulled: boolean; } /** One rule per lane, keyed by the lane itself so a lane cannot arrive without one. */ export const LANE_RULES: Record = { backlog: { label: "Backlog", empty: "the pool is empty", role: "muted", ownerless: true, terminal: false, pulled: false, }, plan: { label: "Plan", empty: "nothing being planned", role: "warn", ownerless: false, terminal: false, pulled: true, }, execute: { label: "Execute", empty: "nothing in flight", role: "accent", ownerless: false, terminal: false, pulled: true, }, blocked: { label: "Blocked", empty: "nothing blocked", role: "danger", ownerless: false, terminal: false, pulled: false, }, done: { label: "Done", empty: "nothing finished yet", role: "success", ownerless: false, terminal: true, pulled: false, }, }; /** * What a move must satisfy before it is allowed. * * Each name is one gate `rules.ts` can answer from the card, the board, and the fact the * caller handed in: `contract` is the four lines being present, `approval` is somebody who * does not hold the card having read them, `review` is the same for the finished work, * `artifact` is the plan file the card is executed against — recorded, and proved to be in * this repository's plan store — `blocker` is the recorded reason and kind, and `waiting` is * no unfinished dependency. The blocker's own kind is a label: where a blocked card may go is * decided by what it has, not by what it was blocked on. */ export const REQUIREMENTS = ["contract", "approval", "review", "landing", "artifact", "blocker", "waiting"] as const; /** One requirement a transition may carry. */ export type Requirement = (typeof REQUIREMENTS)[number]; /** One lane change a `move` may make, and the gates in front of it. */ export interface Transition { from: Lane; to: Lane; /** Checked in this order, so a card missing two things hears about the first. */ needs: readonly Requirement[]; } /** * The lane changes a `move` may make. * * Absent pairs are refused, and the two absences that matter are deliberate: nothing moves * *into* `backlog` (a card nobody holds is `release`'s or `reclaim`'s decision, not a * move's), and nothing moves *out of* `done` (finished work is history). Re-planning is * `execute → plan`, and a blocked card goes back to `plan` or resumes in `execute` — either * one into a lane that has room for it, since a block frees a slot rather than reserving one. * * The dependency gate is not one of these: it belongs to the pull out of the pool, which * `claim` asks itself, and to `execute` and `done`. Planning is where a wait is read against * the contract, so a card may be planned before the work it waits on is finishable. * * `execute` has two doors and asks the same gates at both: no unfinished dependency, the * contract's four lines, the plan file the card is executed against, and the approval a * session that does not hold the card gave. The plan joins the contract rather than `done` * because a plan recorded after the work began is a note about what happened, not the * agreement the work was done against. So the way out of a block follows the card's state * rather than the block's kind — a card blocked from `plan` cannot enter `execute` until it * has been planned, proved, and approved, and one blocked from `execute` already carries all * three and resumes. */ export const TRANSITIONS: readonly Transition[] = [ { from: "backlog", to: "plan", needs: [] }, { from: "plan", to: "execute", needs: ["waiting", "contract", "artifact", "approval"] }, { from: "plan", to: "blocked", needs: ["blocker"] }, { from: "execute", to: "plan", needs: [] }, { from: "execute", to: "blocked", needs: ["blocker"] }, { from: "execute", to: "done", needs: ["waiting", "review", "landing", "artifact"] }, { from: "blocked", to: "plan", needs: [] }, { from: "blocked", to: "execute", needs: ["waiting", "contract", "artifact", "approval"] }, ]; /** The one transition from `from` to `to`, or `undefined` when a move may not make it. */ export function transitionOf(from: Lane, to: Lane): Transition | undefined { return TRANSITIONS.find((transition) => transition.from === from && transition.to === to); } /** Where a lane sits in {@link LANES}: the order a listing, a tray, and a rollup share. */ export function laneIndex(lane: Lane): number { return LANES.indexOf(lane); } /** Whether `value` is one of the lanes, so an untrusted lane is a refusal and not a crash. */ export function isLane(value: unknown): value is Lane { return typeof value === "string" && LANES.includes(value as Lane); } /** The lanes a `move` may name, in lane order: everything but `backlog`, which `release` reaches. */ export function movableLanes(): Lane[] { return LANES.filter((lane) => lane !== "backlog"); } /** * The lanes a `move` from `lane` may reach, in lane order. * * `steps.ts` reads this for one question, which is also the sentence its refusal prints. The * tool's `lane` description names `movableLanes()` instead: it has no card, so it cannot ask * which targets exist from a lane it does not know. */ export function targetsFrom(lane: Lane): Lane[] { return LANES.filter((to) => transitionOf(lane, to) !== undefined); } /** * What the `blocked` lane records: one kind, and the sentence a person reads. * * Here rather than in `card.ts` because a block is the blocked lane's own fact — {@link TRANSITIONS} * requires a `blocker` of every move into it — and because that record had to leave a module at the * module bar without costing an entry a new module evaluation: this table is one every entry that * holds a card already pays. The shelf's record is the opposite case and stayed in `card.ts`: it is * a fact about the pool, and no lane owns it. */ /** Longest sentence saying what a blocked card is waiting on: one line, so it fits a row. */ export const MAX_BLOCKED = 200; /** * Why a card is blocked: what a person has to read before it moves again. * * A block is not one thing: a contract that turned out wrong, a dependency, and a decision a * person owes the card are different errands. The kind is the card's own claim — the board records * it and draws it, and nothing here can verify that a "contract" block really is one — so it * explains the card rather than routing it: where a blocked card may go is decided by what it has. */ export const BLOCKERS = ["contract", "dependency", "decision"] as const; /** One kind of block. */ export type BlockerKind = (typeof BLOCKERS)[number]; /** * Why a card is blocked: the kind, and the sentence a person reads. * * One record rather than two sibling keys, because one fact deserves one key. The kind is absent * only on a card written before it existed — a v1 block may hold the sentence alone — and every * block this build writes carries one, because the move into `blocked` requires it. It says what * kind of errand blocked the card, and it is not a route: the way out is the lane the card's * contract lets it resume in. */ export interface CardBlock { /** Why it is blocked, when it was recorded. */ kind?: BlockerKind; /** What it waits on, in one line; absent means the block was recorded without a sentence. */ note?: string; } /** * The block a move records: one kind, and the sentence when one is given. * * The refusals name the tool's parameters (`blocker`, `blockedBy`) rather than the record's fields, * because this is the boundary the model's arguments pass, and the model wrote a parameter. The kind * is required here — a block this build writes always says what kind it is — and the sentence is * not: a card may be blocked with nothing written down. */ export function normalizeBlock(rawKind: unknown, rawNote: unknown): { blocked: CardBlock } | { error: string } { const allowed = BLOCKERS.join(", "); const kind = typeof rawKind === "string" ? sanitizeLine(rawKind).trim().toLowerCase() : ""; const known = BLOCKERS.find((entry) => entry === kind); if (known === undefined) return { error: `blocker must be one of ${allowed}.` }; if (rawNote === undefined) return { blocked: { kind: known } }; if (typeof rawNote !== "string") return { error: "blockedBy must be text." }; const note = sanitizeLine(rawNote).trim(); if (note === "") return { error: "blockedBy must say what the card waits on." }; if (note.length > MAX_BLOCKED) return { error: overLimit("blockedBy", note.length, MAX_BLOCKED) }; return { blocked: { kind: known, note } }; } /** What a lane change to `blocked` records: one block, holding the kind and the sentence. */ export function blockFields(rawBlocker: unknown, rawSentence: unknown): { error: string } | { fields: Partial } { if (rawBlocker === undefined) { return { error: `a block needs a blocker: ${BLOCKERS.join(", ")}.` }; } const block = normalizeBlock(rawBlocker, rawSentence); if ("error" in block) return { error: block.error }; return { fields: { blocked: block.blocked } }; } /** * A recorded block: a kind this build knows when one is there, and a sentence a row can hold when * one is there — at least one of the two, because an empty block says nothing. */ export function isBlock(value: unknown): value is CardBlock { if (typeof value !== "object" || value === null) return false; const block = value as Record; if (block.kind !== undefined && !BLOCKERS.includes(block.kind as BlockerKind)) return false; if (block.note !== undefined) { if (typeof block.note !== "string" || block.note === "" || block.note.length > MAX_BLOCKED) return false; } return block.kind !== undefined || block.note !== undefined; }