export type NeedKind = "approval" | "default" | "storage" | "recovery" | "delivery" | "review" | "message" | "budget" | "offline" | "stall" | "offer" | "question" | "result"; export interface NeedItem { kind: NeedKind; level: "act" | "watch"; peer?: string; id?: string | number; until?: number; since?: number; } export interface NeedsInput { approvals?: readonly { id: string; peer?: string; expiresAt: number; since?: number }[]; defaults?: readonly { peer: string }[]; peers?: Readonly>; storageBlocked?: boolean; recovery?: { id?: string; running?: boolean; since?: number }; tasks?: readonly { id: number; state: string; owner?: string | null; reviewer?: string | null }[]; facts?: readonly { kind: "review" | "offline" | "stall" | "offer"; id: number; peer?: string }[]; messages?: readonly { id?: string; peer?: string; important: boolean; since?: number }[]; budget?: Readonly>; /** #1053: the open questions a peer asked the person: the set id, the asking peer and when it was asked, never a prompt or an option. */ questions?: readonly { set: string; peer?: string; since?: number }[]; /** #1054: unread results, a run a person started that settled and has not been seen: the completion id, the peer and when it settled, never a prompt or a reply. */ results?: readonly { id: string; peer?: string; since?: number }[]; } export interface Needs { items: NeedItem[]; act: number; watch: number; state: "waiting" | "blocked" | "review" | "working" | "idle" | "sleeping"; } export interface NeedsSummary { act: number; watch: number; state: Needs["state"]; first?: NeedItem } /** Self-contained, read-only structural attention model; no source text is copied or inspected. */ export function needs(input: NeedsInput, now: number): Needs { const items: NeedItem[] = []; const id = (value: unknown): value is string => typeof value === "string" && /^[A-Za-z0-9_.:-]{1,128}$/.test(value); const agent = (value: unknown): value is string => id(value) && value !== "user" && value !== "hub"; const time = (value: unknown): value is number => typeof value === "number" && Number.isSafeInteger(value) && value >= 0; const add = (kind: NeedKind, level: "act" | "watch", peer?: unknown, identifier?: unknown, until?: unknown, since?: unknown) => items.push({ kind, level, ...(id(peer) ? { peer } : {}), ...(id(identifier) || typeof identifier === "number" && Number.isSafeInteger(identifier) && identifier > 0 ? { id: identifier } : {}), ...(time(until) ? { until } : {}), ...(time(since) ? { since } : {}) }); for (const request of input.approvals ?? []) if (id(request.id) && time(request.expiresAt) && request.expiresAt > now) add("approval", "act", request.peer, request.id, request.expiresAt, request.since); for (const pending of input.defaults ?? []) if (id(pending.peer)) add("default", "act", pending.peer); for (const question of input.questions ?? []) if (id(question.set)) add("question", "act", question.peer, question.set, undefined, question.since); if (input.storageBlocked === true) add("storage", "act"); if (input.recovery) add("recovery", input.recovery.running === false ? "act" : "watch", undefined, input.recovery.id, undefined, input.recovery.since); for (const [peer, status] of Object.entries(input.peers ?? {})) { const count = Number.isSafeInteger(status.needsReview) && status.needsReview! > 0 ? status.needsReview! : 0; for (let index = 0; index < count; index++) add("delivery", "act", peer, index === 0 ? status.heldBy : undefined); } for (const message of input.messages ?? []) add("message", message.important ? "act" : "watch", message.peer, message.id, undefined, message.since); const offline = new Set(); const tasks = [...(input.tasks ?? [])].filter(task => Number.isSafeInteger(task.id) && task.id > 0).sort((a,b) => a.id - b.id); for (const task of tasks) { if (task.state === "in_review" && task.reviewer === "user") add("review", "act", undefined, task.id); if (["proposed", "in_progress", "changes_requested"].includes(task.state) && agent(task.owner) && input.peers?.[task.owner]?.state === "offline" && !offline.has(task.owner)) { offline.add(task.owner); add("offline", "watch", task.owner, task.id); } } for (const fact of input.facts ?? []) if (Number.isSafeInteger(fact.id) && fact.id > 0) { if (fact.kind === "stall") add("stall", "act", undefined, fact.id); else if (fact.kind === "offer" && agent(fact.peer)) add("offer", "act", fact.peer, fact.id); else if (fact.kind === "review") add("review", "act", undefined, fact.id); else if (fact.kind === "offline" && agent(fact.peer) && !offline.has(fact.peer)) { offline.add(fact.peer); add("offline", "watch", fact.peer, fact.id); } } for (const [peer, budget] of Object.entries(input.budget ?? {})) if (budget.paused) add("budget", "watch", peer, undefined, budget.paused.resetsAt, budget.paused.since); for (const result of input.results ?? []) if (id(result.id)) add("result", "watch", result.peer, result.id, undefined, result.since); const order: Record = { approval: 0, default: 1, question: 2, storage: 3, recovery: 4, delivery: 5, review: 6, stall: 7, offer: 8, message: 9, result: 10, budget: 11, offline: 12 }; items.sort((a,b) => (a.level === "act" ? 0 : 1) - (b.level === "act" ? 0 : 1) || order[a.kind] - order[b.kind] || (a.kind === "approval" ? (a.until ?? Infinity) - (b.until ?? Infinity) : 0) || (a.since ?? 0) - (b.since ?? 0)); const act = items.filter(item => item.level === "act").length, watch = items.length - act; const peers = Object.values(input.peers ?? {}), attached = peers.some(peer => peer.attached !== false && ["idle", "busy", "paused"].includes(peer.state ?? "")); const state: Needs["state"] = items.some(item => item.kind === "approval" || item.kind === "default" || item.kind === "question") ? "waiting" : items.some(item => item.level === "act" && ["storage", "delivery", "recovery", "stall"].includes(item.kind)) ? "blocked" : act ? "review" : peers.some(peer => peer.attached !== false && peer.state === "busy") ? "working" : attached ? "idle" : "sleeping"; return { items, act, watch, state }; } /** Global status never reads console-local unread messages or source text. */ export function summarizeNeeds(input: Omit, now: number): NeedsSummary { const value = needs({ approvals: input.approvals, defaults: input.defaults, peers: input.peers, storageBlocked: input.storageBlocked, recovery: input.recovery, tasks: input.tasks, facts: input.facts, budget: input.budget, questions: input.questions, results: input.results }, now); return { act: value.act, watch: value.watch, state: value.state, ...(value.items[0] ? { first: value.items[0] } : {}) }; }