import type {BoardCard, Hosted, NodeWait} from '@/lib/stream' import type {Ship} from '@/lib/types' /** * A session's way from "Land it" to live, from what the hub already says: its * card's lane (board.rs: the landing queue, the gate), the landed commit and * whether it is live (ship.rs), and the session's own finish. The page showed * only "on main · live with the next deploy" until it was live; this says * where it is in line, what checks it, and what the deploy waits for. */ /** Where it is: in line to land, its gate running, on main (not live yet), live, finished and filed. */ export type Phase = 'queued' | 'gate' | 'on_main' | 'live' | 'finished' /** What the deploy after landings does with it while it is on main. */ export type Deploy = | {kind: 'none'} | {kind: 'waits'; for: BoardCard[]; by: number | null} | {kind: 'running'} | {kind: 'deploying'; targets: Ship['targets']} | {kind: 'held'; by: string; why: string | null; at: number} | {kind: 'failed'; targets: Ship['targets']} | {kind: 'ungated'} /** Announced to the sessions at work before it restarts what they use, or put off by one: the hub's words, and when it goes. */ | {kind: 'notice'; words: string; deferred: boolean; at: number | null} /** Landed a moment ago: the hub has not looked at the new main yet. */ | {kind: 'planning'} export interface Journey { phase: Phase /** Its place in the landing queue, from 1; null once it is not in it. */ position: number | null /** Since when it is in this step (the queue, the gate, on main), or when it finished. */ since: number | null /** The gate's own word for what it does (`rebasing onto main`, `gating 1a2b3c4d`). */ step: string | null node: string | null /** Its gate waits for a node: its place in that line, and who holds the nodes it may take. */ nodeWait: NodeWait | null sha: string | null /** Its project is deployed after landings: live is a step. Elsewhere on main is the end. */ shipped: boolean /** Its project's deploys after landings, where `shipped`. */ ship: Ship | null deploy: Deploy liveAt: number | null /** It finishes by itself once live (`land --finish`). */ finishes: boolean } /** * The deploys after landings that take a landing in these projects live: the * first of theirs the hub deploys (ship.rs, one per project). A session that * never said its project is the first project's, as the hub reads it. */ export function shipOf(ships: readonly Ship[] | null | undefined, repos: string[]): Ship | null { const going = (ships ?? []).filter((s) => s.targets.length) if (!repos.length) return going[0] ?? null return going.find((s) => !!s.repo && repos.includes(s.repo)) ?? null } /** The landing queue as the page shows it: the one being checked first, then the line in order. */ export function queue(cards: readonly BoardCard[]): BoardCard[] { const rank = (c: BoardCard) => (c.stage === 'landing' ? 0 : (c.position ?? 99)) return cards.filter((c) => c.stage === 'landing' || c.stage === 'waiting_to_land').sort((a, b) => rank(a) - rank(b)) } /** What the deploys after landings do with a commit on main, said once for all of it. */ export function deployOf(ship: Ship | null | undefined, cards: readonly BoardCard[], self?: string): Deploy { if (!ship?.targets.length) return {kind: 'none'} const ts = ship.targets if (ship.held) return {kind: 'held', by: ship.held.by, why: ship.held.why, at: ship.held.at} if (ts.some((t) => t.state === 'deploying' || t.state === 'go')) return {kind: 'deploying', targets: ts} if (ts.some((t) => t.state === 'failed')) return {kind: 'failed', targets: ts} if (ts.some((t) => t.state === 'ungated')) return {kind: 'ungated'} const told = ts.find((t) => t.state === 'deferred') ?? ts.find((t) => t.state === 'announced' || t.state === 'announce') if (told) return {kind: 'notice', words: told.words, deferred: told.state === 'deferred', at: told.goes_at ?? null} const line = queue(cards).filter((c) => c.session !== self) if (line.length) return {kind: 'waits', for: line, by: ship.landed_at ? ship.landed_at + (ship.max_wait_s ?? 1800) : null} // Waiting with nobody in line: an older tip's deploy still runs. if (ts.some((t) => t.state === 'waiting')) return {kind: 'running'} return {kind: 'none'} } /** * Where a session is on its way to live, or null when it is not on it: at * work, or landed and at work again (a card back in the working lane). */ export function journey(h: Hosted | undefined, c: BoardCard | undefined, cards: readonly BoardCard[], ships: readonly Ship[] | null | undefined, repos: string[]): Journey | null { const ship = shipOf(ships, repos) const shipped = !!ship const base = {position: null, step: null, node: null, nodeWait: null, sha: null, shipped, ship, deploy: {kind: 'none'} as Deploy, liveAt: null, finishes: !!h?.finish} if (h?.finished?.outcome === 'landed') { return {...base, phase: 'finished', since: h.finished.at, sha: h.finished.sha ?? null, liveAt: h.finished.live_at ?? null} } if (c?.stage === 'waiting_to_land') return {...base, phase: 'queued', position: c.position, since: c.since} if (c?.stage === 'landing') { return {...base, phase: 'gate', since: c.since, step: c.step ?? null, node: c.node_gate ? c.node : null, nodeWait: c.node_wait?.gate ? c.node_wait : null} } const landed = c?.stage === 'landed' if (!landed && !h?.finish) return null const sha = (landed ? c!.sha : null) ?? h?.finish?.sha ?? null const since = (landed ? c!.since : null) ?? h?.finish?.at ?? null // Live: every target deployed after landings serves it; where nothing is, on main is the end. if (landed && (c!.live || !shipped)) return {...base, phase: 'live', since, sha, liveAt: c!.live ? null : since} // What the hub plans is for its main's tip: one from before this landing says nothing of it. // One after it — another session landed on top — carries it, and its plan is this one's: // read as "before", the page said the hub was planning until the next deploy (2026-10-29). // Main only moves forward, so a tip made after this landing reached main contains it. const other = !!ship?.tip && !!sha && !ship.tip.startsWith(sha) && !sha.startsWith(ship.tip) const behind = other && !(since != null && ship!.landed_at != null && ship!.landed_at >= since) // Every target serves a tip that contains it: live, whatever the card's lane says (a // session still marked at work has no "landed" card, and read "on main" after it went out). // A target no landing picked (`unpicked`) is left to later, and is not this landing's to wait for. if (shipped && !behind && ship!.targets.every((t) => t.state === 'live' || t.state === 'skip' || t.state === 'unpicked')) return {...base, phase: 'live', since, sha} const deploy: Deploy = !shipped ? {kind: 'none'} : behind ? {kind: 'planning'} : deployOf(ship, cards, c?.session) return {...base, phase: 'on_main', since, sha, deploy} } /** When the deploy that takes it live starts, in unix seconds, where that is known: by when at the latest, for one waiting on landings. */ export function deployAt(d: Deploy): number | null { return d.kind === 'notice' ? d.at : d.kind === 'waits' ? d.by : null } /** On its way, not there yet: in line, being checked, or on main waiting to go live. */ export const underway = (j: Journey | null): j is Journey => !!j && (j.phase === 'queued' || j.phase === 'gate' || j.phase === 'on_main') /** Its step in words, short: for a row in a list. */ export function shortWords(j: Journey): string { switch (j.phase) { case 'queued': return j.position && j.position > 1 ? `#${j.position} in line` : 'next to land' case 'gate': return j.nodeWait ? 'gate waits for a node' : 'checks' case 'on_main': return j.deploy.kind === 'deploying' ? 'deploying' : j.deploy.kind === 'held' ? 'held' : j.deploy.kind === 'failed' ? 'deploy failed' : 'on main' case 'live': return 'live' case 'finished': return 'finished' } }