Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Install each seed skill once when several servers boot together (#1920)

Regan Bellb06e8c95fb19
diff --git a/src/skills/seed.ts b/src/skills/seed.ts
index 0e52de6a4..cde91dd76 100644
--- a/src/skills/seed.ts
+++ b/src/skills/seed.ts
@@ -89,7 +89,10 @@ export function upsertSeedSkill(
skills: SkillStore,
input: { scopeId: ScopeId; manifest: SkillManifest; createdBy: string; reviewer: string; pack?: Skill["pack"] },
): Promise<UpsertOutcome> {
- return upsertQueue(`${input.scopeId}\0${input.manifest.name}`, () => upsertSeedSkillUnsafe(skills, input));
+ const run = () => upsertSeedSkillUnsafe(skills, input);
+ return upsertQueue(`${input.scopeId}\0${input.manifest.name}`, () =>
+ skills.withNameLock ? skills.withNameLock(input.scopeId, input.manifest.name, run) : run(),
+ );
}
async function upsertSeedSkillUnsafe(
diff --git a/src/skills/skill-store.ts b/src/skills/skill-store.ts
index 9fe8692a4..5cba51ed9 100644
--- a/src/skills/skill-store.ts
+++ b/src/skills/skill-store.ts
@@ -4,6 +4,7 @@ import type { ScopeId } from "../types.ts";
import { parseScopeId } from "../types.ts";
import { createMemoryMap, type DurableMap } from "../persistence/durable-map.ts";
import { assertSafeSkillName, isSafeSkillName } from "./skill-name.ts";
+import type { AdvisoryLock } from "../persistence/advisory-lock.ts";
type SkillStatus = "draft" | "reviewed" | "published" | "archived";
@@ -77,11 +78,13 @@ export interface SkillResolution {
export interface SkillStoreOptions {
signingSecret?: string;
backing?: DurableMap<Skill>;
+ advisoryLock?: AdvisoryLock;
}
export interface SkillStore {
create(input: { scopeId: ScopeId; manifest: SkillManifest; createdBy: string; pack?: Skill["pack"] }): Promise<Skill>;
update(id: string, manifest: SkillManifest): Promise<Skill>;
+ withNameLock?<T>(scopeId: ScopeId, name: string, fn: () => Promise<T>): Promise<T>;
get(id: string): Promise<Skill | null>;
list(): Promise<Skill[]>;
verify(skill: Skill): boolean;
@@ -181,6 +184,8 @@ export function createSkillStore(opts: SkillStoreOptions = {}): SkillStore {
}
return {
+ withNameLock: (scopeId, name, fn) =>
+ opts.advisoryLock ? opts.advisoryLock.withLock(`skill-name:${scopeId}:${name}`, fn) : fn(),
async create(input) {
assertSafeSkillName(input.manifest.name);
const at = Date.now();
diff --git a/src/wiring.ts b/src/wiring.ts
index 8fb402aba..f9cc57dcc 100644
--- a/src/wiring.ts
+++ b/src/wiring.ts
@@ -698,6 +698,7 @@ export function buildApp(
void configStore.hydrate?.();
const skills: SkillStore = createSkillStore({
backing: artifactMap<Skill>("skills"),
+ advisoryLock,
...(config.skillSigningSecret ? { signingSecret: config.skillSigningSecret } : {}),
});
const skillPacks = createSkillPackStore({ backing: artifactMap<SkillPack>("skill_packs") });
diff --git a/test/skills-seed-cross-server.test.ts b/test/skills-seed-cross-server.test.ts
new file mode 100644
index 000000000..f9a846067
--- /dev/null
+++ b/test/skills-seed-cross-server.test.ts
@@ -0,0 +1,61 @@
+import { test, after } from "node:test";
+import assert from "node:assert/strict";
+import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { createSkillStore, type Skill } from "../src/skills/skill-store.ts";
+import { createMemoryMap, createPostgresMapFactory, type DurableMap } from "../src/persistence/durable-map.ts";
+import {
+ createMemoryAdvisoryLock,
+ createPostgresAdvisoryLock,
+ type AdvisoryLock,
+} from "../src/persistence/advisory-lock.ts";
+import { scopeId } from "../src/types.ts";
+
+const URL = process.env.DATABASE_URL;
+const factory = URL ? createPostgresMapFactory(URL) : null;
+after(async () => {
+ await factory?.pool.close();
+});
+
+const shared = async (): Promise<{ backing: DurableMap<Skill>; lock: () => AdvisoryLock }> => {
+ if (!factory) {
+ const lock = createMemoryAdvisoryLock();
+ return { backing: createMemoryMap<Skill>(), lock: () => lock };
+ }
+ const table = `xsrv_skills_${process.pid}`;
+ await factory.pool.query(`DROP TABLE IF EXISTS ${table}`);
+ return { backing: factory.map<Skill>(table), lock: () => createPostgresAdvisoryLock(factory.pool) };
+};
+
+const slowReads = (map: DurableMap<Skill>): DurableMap<Skill> => ({
+ ...map,
+ async all() {
+ const rows = await map.all();
+ await new Promise((resolve) => setTimeout(resolve, 50));
+ return rows;
+ },
+});
+
+test("two servers seeding the same catalog at boot install each skill once", async () => {
+ const dir = mkdtempSync(join(tmpdir(), "seed-xsrv-"));
+ mkdirSync(join(dir, "hello"));
+ writeFileSync(join(dir, "hello", "SKILL.md"), "---\nname: hello\ndescription: Say hello.\n---\nSay hello.\n");
+ const { backing, lock } = await shared();
+ const left = createSkillStore({ backing: slowReads(backing), signingSecret: "s", advisoryLock: lock() });
+ const right = createSkillStore({ backing: slowReads(backing), signingSecret: "s", advisoryLock: lock() });
+ const org = scopeId("org", "default-org");
+
+ const seedModule = "../src/skills/seed.ts";
+ type Seed = typeof import("../src/skills/seed.ts");
+ const leftServer = (await import(`${seedModule}?server=left`)) as Seed;
+ const rightServer = (await import(`${seedModule}?server=right`)) as Seed;
+ await Promise.all([
+ leftServer.installSeedSkills(left, { dir, scopeId: org }),
+ rightServer.installSeedSkills(right, { dir, scopeId: org }),
+ ]);
+
+ const rows = (await backing.all()).filter((s) => s.manifest.name === "hello");
+ assert.equal(rows.length, 1);
+ assert.equal(rows[0]!.status, "published");
+});