import {asksPerson, awaitsPerson} from '@/lib/status' import type {Hosted} from '@/lib/stream' import type {AdminRequest, Decision} from '@/lib/types' /** A session still starting after this long is stuck: something on its machine went wrong. */ export const STUCK_AFTER_S = 5 * 60 /** One thing that waits for the person: a question to answer, a permission to give, a session that will not start. */ export type Need = | {kind: 'admin'; request: AdminRequest} | {kind: 'decision'; decision: Decision} | {kind: 'permission'; session: Hosted} | {kind: 'awaits'; session: Hosted} | {kind: 'stuck'; session: Hosted; for_s: number} /** * What waits for the person: a session asking for root first (its work stops on it), then the * oldest question, then permissions, then what only they can do (the oldest first), then stuck sessions. */ export function needs(decisions: Decision[], sessions: readonly Hosted[], now: number, admin: readonly AdminRequest[] = []): Need[] { const out: Need[] = admin.filter((r) => r.state === 'asked').map((request) => ({kind: 'admin', request})) for (const decision of decisions.filter((d) => d.answer == null).sort((a, b) => a.at - b.at)) out.push({kind: 'decision', decision}) const live = sessions.filter((s) => !s.archived) for (const s of live) if (asksPerson(s)) out.push({kind: 'permission', session: s}) const busy = (s: Hosted) => s.status === 'working' || s.status === 'starting' || s.status === 'queued' for (const s of [...live].sort((a, b) => (a.awaits?.at ?? 0) - (b.awaits?.at ?? 0))) if (awaitsPerson(s, now) && !asksPerson(s) && !busy(s)) out.push({kind: 'awaits', session: s}) for (const s of live) { const for_s = now - s.updated if ((s.status === 'starting' || s.status === 'queued') && for_s > STUCK_AFTER_S) out.push({kind: 'stuck', session: s, for_s}) } return out }