git clone https://garage44.eu/jeroen/midas.git.. /web/src/lib/status.ts
import {now} from '@/lib/format'
import type {Hosted} from '@/lib/stream'
/**
* Whether it waits on the person: asking, and not on autopilot (which answers for
* them) — or on autopilot that left something to them. The lists, Home's count
* and the push (push.rs) say the same.
*/
export function asksPerson(h: Hosted): boolean {
const p = h.autopilot
if (p?.on && h.status !== 'stopped' && h.status !== 'failed') return p.state === 'left'
return h.status === 'needs_permission'
}
/**
* Whether it waits on the person for something only they can do (`midas
* await`), not put off: Needs you, the push (push.rs) the same. Declared by its
* agent, never guessed from its being idle (see `State`).
*/
export function awaitsPerson(h: Hosted, t: number = now()): boolean {
return !!h.awaits && !h.archived && !h.finished && !((h.awaits.snooze ?? 0) > t)
}
/**
* A running session is in one of three states, said the same way on the whole page:
* `working` while a turn runs or anything it started still runs in the background
* (a build, a subagent, a workflow: the runner counts them as working); `asks`
* while it waits on the person's answer or permission, the only one that is
* theirs to act on; `idle` when it has done its turn and asked nothing. "Your
* turn" was a fourth, an idle session within the hour painted like one that
* asks, and read as one (2026-10-27). `over` is a session that no longer runs.
*/
export type State = 'working' | 'asks' | 'idle' | 'over'
export function stateOf(h: Hosted): State {
switch (h.status) {
case 'queued':
case 'starting':
case 'working':
return 'working'
case 'needs_permission':
return 'asks'
case 'waiting':
return 'idle'
default:
return 'over'
}
}
/**
* How long it has been idle (`idle_s`): since `active` (a turn's start or end,
* a person's reopen), which a runner starting again does not move; `updated`,
* which it does, only for a hub from before it. `yours`: it asks the person.
*/
export function turnOf(h: Hosted, t: number = now()): {yours: boolean; idle_s: number} {
const idle_s = Math.max(0, t - (h.active ?? h.updated))
return {yours: stateOf(h) === 'asks', idle_s}
}
/**
* What a session midas runs is doing, in one set of words and colours for the
* whole page: its badge on the board, on its own page, and its dot in the nav.
*/
export const STATUS: Record<Hosted['status'], {label: string; tone: 'success' | 'warning' | 'danger' | 'info'}> = {
queued: {label: 'starting', tone: 'info'},
starting: {label: 'starting', tone: 'info'},
working: {label: 'working', tone: 'success'},
waiting: {label: 'idle', tone: 'info'},
needs_permission: {label: 'asks you', tone: 'warning'},
stopped: {label: 'ended', tone: 'info'},
failed: {label: 'failed', tone: 'danger'},
}
/**
* Where a session runs, for a chip beside its status: an editor's thread on its machine
* (`Zed · mini`), or the hub's own runner (`midas`), handed over from an editor or started
* on the page. An imported conversation runs nowhere: none.
*/
export function place(h: Hosted): {label: string; title: string} | null {
if (h.imported) return null
if (h.local) {
const at = `${h.local.client} on ${h.local.machine}${h.local.cwd ? ` (${h.local.cwd})` : ''}`
return {label: `${h.local.client} · ${h.local.machine}`, title: `runs in ${at}: this page follows and steers it`}
}
const from = h.handed ? `, handed over from ${h.handed.from.client} on ${h.handed.from.machine}` : ''
return {label: 'midas', title: `runs on the hub's runner${from}`}
}