import type { SkillRegistry } from "./skill-loader.js"; export class TransitionMatrix { private constructor(private readonly transitions: Map>) {} static fromRegistry(registry: SkillRegistry): TransitionMatrix { const transitions = new Map>(); for (const manifest of registry.list()) { const targets = manifest.name === "supervisor" ? manifest.handoffTargets : [...manifest.handoffTargets, "supervisor"]; transitions.set( manifest.name, new Set(targets), ); } return new TransitionMatrix(transitions); } isAllowed(from: string, to: string): boolean { if (from === to) { return false; } return this.transitions.get(from)?.has(to) ?? true; } targetsOf(from: string): string[] { return [...(this.transitions.get(from) ?? new Set())]; } validateGraph(knownSkills?: readonly string[]): string[] { const known = new Set([...(knownSkills ?? this.transitions.keys()), "supervisor"]); const errors: string[] = []; for (const [from, targets] of this.transitions.entries()) { if (targets.has(from)) { errors.push(`${from}: handoffTargets must not include itself`); } for (const target of targets) { if (!known.has(target)) { errors.push(`${from}: unknown handoff target ${target}`); } } } return errors; } }