Multiplayer agent harness for work.
Resolve skill shadowing from one catalog snapshot in visibleFor (#160)
Joshua Franceeabe3d76692a
diff --git a/src/skills/skill-store.ts b/src/skills/skill-store.tsindex 85a8d6331..8d17ccdae 100644--- a/src/skills/skill-store.ts+++ b/src/skills/skill-store.ts@@ -94,6 +94,23 @@ function scopeKind(scopeId: ScopeId): string {return parseScopeId(scopeId).kind ?? scopeId;}+function publishedByScopeAndName(all: Skill[]): Map<string, Skill> {+ const index = new Map<string, Skill>();+ for (const s of all) {+ if (s.status !== "published") continue;+ const key = `${s.scopeId}\u0000${s.manifest.name}`;+ if (!index.has(key)) index.set(key, s);+ }+ return index;+}++function resolveFromIndex(index: Map<string, Skill>, name: string, orderedScopes: ScopeId[]): SkillResolution {+ if (!isSafeSkillName(name)) return { skill: null, shadowed: [] };+ const published = orderedScopes.map((sc) => index.get(`${sc}\u0000${name}`)).filter((s): s is Skill => Boolean(s));+ const [skill, ...shadowed] = published;+ return { skill: skill ?? null, shadowed };+}+export function createSkillStore(opts: SkillStoreOptions = {}): SkillStore {const skills = opts.backing ?? createMemoryMap<Skill>();const secret = opts.signingSecret ?? randomUUID();@@ -202,26 +219,23 @@ export function createSkillStore(opts: SkillStoreOptions = {}): SkillStore {},async resolve(name, orderedScopes) {- if (!isSafeSkillName(name)) return { skill: null, shadowed: [] };- const all = await skills.all();- const published = orderedScopes- .map((sc) => all.find((s) => s.status === "published" && s.manifest.name === name && s.scopeId === sc))- .filter((s): s is Skill => Boolean(s));- const [skill, ...shadowed] = published;- return { skill: skill ?? null, shadowed };+ return resolveFromIndex(publishedByScopeAndName(await skills.all()), name, orderedScopes);},async visibleFor(orderedScopes) {+ const all = await skills.all();+ const index = publishedByScopeAndName(all);const inScope = new Set(orderedScopes);const names = [...new Set(- (await skills.all())+ all.filter((s) => s.status === "published" && inScope.has(s.scopeId) && isSafeSkillName(s.manifest.name)).map((s) => s.manifest.name),),];- const resolved = await Promise.all(names.map((n) => this.resolve(n, orderedScopes)));- return resolved.filter((r): r is SkillResolution & { skill: Skill } => r.skill !== null);+ return names+ .map((n) => resolveFromIndex(index, n, orderedScopes))+ .filter((r): r is SkillResolution & { skill: Skill } => r.skill !== null);},async promote(id, targetScopeId) {diff --git a/test/skills-visible.test.ts b/test/skills-visible.test.tsindex 3b59ec3d3..6c70075fe 100644--- a/test/skills-visible.test.ts+++ b/test/skills-visible.test.ts@@ -9,6 +9,8 @@ import { buildApp } from "../src/wiring.ts";import type { Config } from "../src/config.ts";import { scopeId, type ScopeId } from "../src/types.ts";import { testConfig } from "./support/test-config.ts";+import { createMemoryMap } from "../src/persistence/durable-map.ts";+import { createSkillStore, type Skill } from "../src/skills/skill-store.ts";function freshApp() {const config: Config = testConfig({@@ -78,3 +80,44 @@ test("an unpublished (draft/reviewed) skill is not visible", async () => {const visible = await app.listVisibleSkills("U1");assert.ok(!visible.some((r) => r.skill!.manifest.name === "wip"));});++test("visibleFor reads the full skills table once, not once per skill name", async () => {+ const backing = createMemoryMap<Skill>();+ let allReads = 0;+ const originalAll = backing.all.bind(backing);+ backing.all = () => {+ allReads += 1;+ return originalAll();+ };+ const skills = createSkillStore({ backing });++ const org = scopeId("org", "default-org");+ const personal = scopeId("personal", "U1");+ for (let i = 0; i < 200; i++) {+ const scope = i % 2 === 0 ? org : personal;+ const sk = await skills.create({+ scopeId: scope,+ manifest: { name: `skill-${i}`, description: `skill ${i}`, requiredCapabilities: [], body: `# skill-${i}` },+ createdBy: "author",+ });+ await skills.review(sk.id, "reviewer-1", []);+ await skills.publish(sk.id);+ }+ const shadowing = await skills.create({+ scopeId: personal,+ manifest: { name: "skill-0", description: "personal override", requiredCapabilities: [], body: "# skill-0" },+ createdBy: "U1",+ });+ await skills.review(shadowing.id, "reviewer-1", []);+ await skills.publish(shadowing.id);++ allReads = 0;+ const visible = await skills.visibleFor([personal, org]);++ assert.equal(allReads, 1, "one full skills read per visibleFor call");+ assert.equal(visible.length, 200);+ const shadowed = visible.find((r) => r.skill!.manifest.name === "skill-0")!;+ assert.equal(shadowed.skill!.scopeId, personal);+ assert.equal(shadowed.shadowed.length, 1);+ assert.equal(shadowed.shadowed[0]!.scopeId, org);+});