git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/nodeline.ts
import type {Lease} from '@/lib/types'
import type {BoardCard, BoardNode, LogItem, NodeHeld} from '@/lib/stream'
/**
* How a session gets a node, as its conversation shows it: in line for one, or
* on one and whether it builds there now. Its landing gate's node is not here:
* that is a step of its way to live, which To live says.
*/
export type NodeLine =
| {kind: 'waits'; wants: string; position: number; since: number; held: NodeHeld[]}
| {kind: 'on'; node: string; busy: boolean; idle_s: number | null; since: number | null; quiet: {command: string; quiet_s: number} | null}
/** A session's node line from its card and the leases; null while it neither holds nor waits for one. */
export function nodeLine(c: BoardCard | undefined, leases: Record<string, Lease> | undefined, t: number): NodeLine | null {
if (!c) return null
if (c.node && !c.node_gate) {
const l = leases?.[c.node]
// A command running there is use, however long since the lease was renewed (home.tsx `blocker`).
const idle_s = c.busy ? 0 : l ? Math.max(0, t - l.renewed_at) : null
// Running but doing nothing is not building (s857's showed "building now" for 19 hours).
const quiet = c.busy && c.quiet ? c.quiet : null
return {kind: 'on', node: c.node, busy: !!c.busy, idle_s, since: c.since, quiet}
}
const w = c.node_wait
if (w && !w.gate) return {kind: 'waits', wants: w.wants, position: w.position, since: w.since, held: w.held ?? []}
return null
}
/**
* What a session waits for, as a person says it: "a node", "a GPU node", "mac-2",
* and for a machine whose nodes are all `<kind>-<n>`, "a mac" rather than "a node
* on mini".
*/
export function wantsWords(wants: string, nodes: readonly Pick<BoardNode, 'id' | 'machine'>[]): string {
if (!wants || wants === 'any') return 'a node'
if (wants === 'gpu') return 'a GPU node'
if (nodes.some((n) => n.id === wants)) return wants
const kinds = new Set(nodes.filter((n) => n.machine === wants).map((n) => n.id.match(/^(.+)-\d+$/)?.[1] ?? ''))
const [kind] = [...kinds]
return kinds.size === 1 && kind ? `a ${kind}` : `a node on ${wants}`
}
/** Its place in line, short: "next in line", "#2 in line". */
export const placeWords = (position: number) => (position > 1 ? `#${position} in line` : 'next in line')
/** A `midas take`, `wait`, `release` or `yield` the agent ran, and what came of it. */
export interface NodeStep {
verb: 'take' | 'wait' | 'release' | 'yield'
state: 'running' | 'background' | 'took' | 'next' | 'queued' | 'released' | 'yielding' | 'keeps' | 'failed'
node: string | null
/** Its place in line, as midas said it ("2nd in line"), when it had to wait. */
place: string | null
}
/**
* The command a node step is, alone: `midas take …` with at most a `cd` before
* it and a pipe into `tail`/`head` after. A command that does more (commit and
* release, fetch and take) stays a tool call, so nothing it did is folded away.
*/
const ALONE = /^\s*(?:cd\s+\S+\s*&&\s*)?(?:env(?:\s+-u\s+\S+|\s+\w+=\S+)*\s+)?midas\s+(take|wait|release|yield)\b[^|;&]*(?:\s*2>&1)?(?:\s*\|\s*(?:tail|head)(?:\s+-n?\s*\d+|\s+-\d+)?)?\s*$/
/** A tool call as a node step, or null for any other call. */
export function nodeStep(t: LogItem): NodeStep | null {
const command = typeof t.input?.command === 'string' ? t.input.command : null
const viaMcp = !command && t.title?.match(/(?:^|__)node_(take|release)$/)?.[1]
const verb = (command ? command.match(ALONE)?.[1] : viaMcp) as NodeStep['verb'] | undefined
if (!verb) return null
const out = t.output ?? ''
const step = (state: NodeStep['state'], node: string | null = null, place: string | null = null): NodeStep => ({verb, state, node, place})
if (t.status === 'pending' || t.status === 'in_progress') return step('running')
if (/^Command running in background/.test(out)) return step('background')
if (verb === 'release' || verb === 'yield') {
const gone = out.match(/\breleased (\S+)/) ?? out.match(/^(\S+) released: /m)
if (gone) return {verb: 'release', state: 'released', node: gone[1], place: null}
const later = out.match(/^(\S+) goes back when/m)
if (later) return step('yielding', later[1])
const keeps = out.match(/^keeping (\S+)/m)
if (keeps) return step('keeps', keeps[1])
return step('failed')
}
// `take` tells where on stderr and prints the node alone last; a `tail` keeps only some of it.
const took = out.match(/\btook (\S+)/) ?? out.match(/^(\S+) follows this worktree/m) ?? out.match(/ssh (\S+) 'cargo/) ?? out.match(/^midas: (\S+) is at /m)
if (took) return step('took', took[1])
const next = out.match(/^(\S+) is yours next/m)
if (next) return step('next', next[1])
if (/still in line|^waiting: /m.test(out)) return step('queued', null, out.match(/\b(next in line|\d+(?:st|nd|rd|th) in line)\b/)?.[1] ?? null)
return step('failed')
}
/** A node step in a few words, as its line in the conversation says it. */
export function nodeStepWords(s: NodeStep): string {
switch (s.state) {
case 'running':
return s.verb === 'release' || s.verb === 'yield' ? 'Giving its node back…' : s.verb === 'wait' ? 'Waiting for a node…' : 'Asking for a node…'
case 'background':
return s.verb === 'wait' ? 'Waits in line for a node, in the background' : 'Asks for a node, in the background'
case 'took':
return `Took ${s.node}`
case 'next':
return `${s.node} is its next: its holder has been told`
case 'queued':
return `No node free${s.place ? `: ${s.place}` : ''}, keeps its place`
case 'released':
return `Gave ${s.node ?? 'its node'} back`
case 'yielding':
return `Gives ${s.node} back when its build there ends`
case 'keeps':
return `Keeps ${s.node}`
case 'failed':
return s.verb === 'release' || s.verb === 'yield' ? 'Had no node to give back' : s.verb === 'wait' ? 'Not in line for a node' : 'Got no node'
}
}