import type { Close, Event, Goal, GoalId, ProofTree, Script } from "../ir.ts"; type WalkResult = { closed: Set; opened: Set; entered: GoalId[]; }; const dedupe = (items: readonly GoalId[]): GoalId[] => { const seen = new Set(); const out: GoalId[] = []; for (const id of items) { if (seen.has(id)) continue; seen.add(id); out.push(id); } return out; }; export function attribute(script: Script): ProofTree { const goalById = new Map(); for (const g of script.goals) goalById.set(g.id, g); const rootSet = new Set(script.roots.map((r) => r.goal)); const closes = new Map(); const notes: string[] = []; const isGone = (id: GoalId): boolean => goalById.get(id)?.final === "gone"; const walk = (ev: Event): WalkResult => { const belowClosed = new Set(); const belowOpened = new Set(); const childEnteredRaw: GoalId[] = []; for (const child of ev.children) { const sub = walk(child); for (const g of sub.closed) belowClosed.add(g); for (const g of sub.opened) belowOpened.add(g); for (const g of sub.entered) childEnteredRaw.push(g); } const afterSet = new Set(ev.after); for (const g of ev.after) { if (belowClosed.has(g)) { belowClosed.delete(g); closes.delete(g); } } const childRoots = dedupe(childEnteredRaw).filter( (g) => !belowOpened.has(g) && !isGone(g) && !afterSet.has(g), ); const assignedSet = new Set(ev.assigned); const beforeRank = new Map(); for (const [i, g] of ev.before.entries()) { if (!beforeRank.has(g)) beforeRank.set(g, i); } const declaredSet = new Set(ev.declared); const reachMap = new Map(); for (const [from, tos] of ev.reach) reachMap.set(from, tos); const hasGoneReach = (g: GoalId): boolean => (reachMap.get(g) ?? []).some(isGone); const mine = dedupe(ev.assigned) .filter( (g) => !declaredSet.has(g) && !belowClosed.has(g) && !afterSet.has(g) && !isGone(g) && !hasGoneReach(g), ) .sort((a, b) => { const ra = beforeRank.get(a) ?? Number.MAX_SAFE_INTEGER; const rb = beforeRank.get(b) ?? Number.MAX_SAFE_INTEGER; return ra - rb; }); const mineSet = new Set(mine); const openedNow = new Set(); if (mine.length > 0) { const poolRaw: GoalId[] = [...childRoots, ...ev.after]; for (const g of mine) { const rs = reachMap.get(g); if (rs) { for (const c of rs) poolRaw.push(c); } } const pool = dedupe(poolRaw).filter( (c) => declaredSet.has(c) && !mineSet.has(c) && !rootSet.has(c) && !belowOpened.has(c) && !isGone(c), ); const claimed = new Set(); const opensByGoal = new Map(); for (const g of mine) { const reach = reachMap.get(g); const opens: GoalId[] = []; if (reach !== undefined) { const reachSet = new Set(reach); for (const c of pool) { if (reachSet.has(c) && !claimed.has(c) && c !== g) { claimed.add(c); opens.push(c); } } } else if (mine.length === 1) { for (const c of pool) { if (!claimed.has(c) && c !== g) { claimed.add(c); opens.push(c); } } } opensByGoal.set(g, opens); } const unclaimed = pool.filter((c) => !claimed.has(c)); if (unclaimed.length > 0) { const owner = mine[0]; const list = opensByGoal.get(owner) ?? []; for (const c of unclaimed) { if (c === owner) continue; claimed.add(c); list.push(c); } opensByGoal.set(owner, list); if (reachMap.size > 0 && mine.length > 1) { notes.push( `Step ${ev.step} attached ${unclaimed.length} subgoal(s) via fallback to goal ${owner}.`, ); } } for (const g of mine) { const opens = opensByGoal.get(g) ?? []; closes.set(g, { step: ev.step, opens }); for (const c of opens) openedNow.add(c); } } const closed = new Set(belowClosed); for (const g of mine) closed.add(g); const opened = new Set(belowOpened); for (const g of openedNow) opened.add(g); const entered = dedupe([...mine, ...childRoots, ...ev.before]).filter( (g) => !opened.has(g) && (assignedSet.has(g) || closed.has(g) || ev.before.includes(g)), ); return { closed, opened, entered }; }; for (const ev of script.events) walk(ev); return { roots: script.roots, closes, notes }; }