Add bounded skill handoff graph
This commit is contained in:
1 parent
54bc295ead
commit
c14adac162
42 files changed
+2243
-31
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import type { SkillRegistry } from "./skill-loader.js";
|
||||
|
||||
export class TransitionMatrix {
|
||||
private constructor(private readonly transitions: Map<string, Set<string>>) {}
|
||||
|
||||
static fromRegistry(registry: SkillRegistry): TransitionMatrix {
|
||||
const transitions = new Map<string, Set<string>>();
|
||||
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<string>())];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user