Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: a withdrawal companion page passes the shared-skillpack publication guard

garrytan3a001231f42a
diff --git a/src/core/persistence/coordinator.ts b/src/core/persistence/coordinator.ts
index 63d6cb87d7..6634334f78 100644
--- a/src/core/persistence/coordinator.ts
+++ b/src/core/persistence/coordinator.ts
@@ -483,6 +483,9 @@ export async function publishMutation(engine: BrainEngine, row: WriteRequest, pr
const companions = prepared.target === 'skill_bundle' ? [] : prepared.companions ?? [];
// GBRA-190 G4 (D2): the request's own page file first, then each companion's, in one recovery record and one commit.
const files = skill ? prepared.files ?? [] : [...(prepared.file ? [prepared.file] : []), ...companions.flatMap(companion => companion.file ? [companion.file] : [])];
+ const guardKnowledgePages = async (db: BrainEngine) => {
+ for (const page of [{ slug: row.slug, file: prepared.file }, ...companions]) await assertKnowledgePublicationAllowed(db, { ...row, slug: page.slug }, page.file);
+ };
const beliefReport = { appended: 0 };
try {
assertMutationProtocol(row);
@@ -527,7 +530,7 @@ export async function publishMutation(engine: BrainEngine, row: WriteRequest, pr
await releaseUnpublishedClaim(engine, row, 'writer_pool_capacity');
return (await getWriteRequestById(engine, row.id))!;
}
- if (!skill) await assertKnowledgePublicationAllowed(engine, row, prepared.file);
+ if (!skill) await guardKnowledgePages(engine);
if (skill && !prepared.noop) {
if (!binding || !lock) throw opError('storage_error', 'Skill publication requires a canonical owner.',
`Shared-skill request ${row.request_id} reached publication without this host holding source ${row.source_id}'s canonical worktree, so no skill file was written. Inspect the owner; the skill publishes only on the host that owns the source.`,
@@ -605,7 +608,7 @@ export async function publishMutation(engine: BrainEngine, row: WriteRequest, pr
const companionSnapshots = new Map<string, PageSnapshot | null>();
if (skill) await assertSharedSkillPersistence(tx, row.source_id);
else {
- await assertKnowledgePublicationAllowed(tx, row, prepared.file);
+ await guardKnowledgePages(tx);
await tx.lockPageKeys([{ sourceId: row.source_id, slug: row.slug },...(prepared.additionalPageKeys??[]),
...companions.map(companion => ({ sourceId: row.source_id, slug: companion.slug }))]);
snapshot = await tx.readPageSnapshot(row.slug, { sourceId: row.source_id, includeDeleted: true });
diff --git a/test/forget-companion-skill-guard.test.ts b/test/forget-companion-skill-guard.test.ts
new file mode 100644
index 0000000000..3b917b8b09
--- /dev/null
+++ b/test/forget-companion-skill-guard.test.ts
@@ -0,0 +1,64 @@
+/**
+ * A withdrawal's companion pages pass the knowledge publication guard the request's own page passes. A forget strikes
+ * the claim in the file of every page carrying it (forget-prepare.ts companions); before the fix only the request's own
+ * page was screened, so a forget rewrote a file of an adopted shared skillpack that a direct page write is refused
+ * (skill_bundle_required) and left the catalog describing bytes no longer on disk. Drives the real put_page, remember
+ * and forget handlers on a PGLite managed brain with a really adopted pack.
+ */
+import { describe, expect, test } from 'bun:test';
+import { randomUUID } from 'node:crypto';
+import { readFileSync, writeFileSync } from 'node:fs';
+import { join } from 'node:path';
+import type { OperationContext } from '../src/core/ops/contract.ts';
+import { operationsByName } from '../src/core/operations.ts';
+import { adoptSharedSkillpack } from '../src/core/shared-skills/publication.ts';
+import { setSharedSkillPolicy } from '../src/core/shared-skills/policy.ts';
+import { durableGitRepo } from './helpers/git-publication.ts';
+import { managedBrain } from './helpers/managed-brain.ts';
+
+const CLAIM = 'Keeps a rooftop beehive skillguard';
+const OTHER = 'Moved the hives indoors skillguard';
+const SKILL_PAGE = 'skills/alpha/notes';
+const fence = (rows: string[]) => ['<!--- gbrain:facts:begin -->',
+ '| # | claim | kind | confidence | visibility | notability | valid_from | valid_until | source | context |',
+ '|---|-------|------|------------|------------|------------|------------|-------------|--------|---------|', ...rows, '<!--- gbrain:facts:end -->'].join('\n');
+/** CLAIM superseded by OTHER: no active entity-linked copy, so forgetting the unlinked copy reaches this page. */
+const SUPERSEDED = [`| 1 | ~~${CLAIM}~~ | fact | 1.0 | world | medium | 2026-01-01 | 2026-02-01 | test | superseded by #2 |`,
+ `| 2 | ${OTHER} | fact | 1.0 | world | medium | 2026-02-01 | | test | |`];
+const skillNotes = `---\ntype: note\ntitle: Alpha notes\n---\n\nNotes.\n\n## Facts\n\n${fence(SUPERSEDED)}\n`;
+const call = (ctx: OperationContext, name: string, params: Record<string, unknown>) =>
+ operationsByName[name]!.handler(ctx, { request_id: randomUUID(), wait_ms: 30_000, ...params }) as Promise<Record<string, any>>;
+const setup = ({ root }: { root: string }) => {
+ writeFileSync(join(root, '.gitignore'), '\n');
+ writeFileSync(join(root, 'skillpack.json'), JSON.stringify({ api_version: 'gbrain-skillpack-v1', name: 'example-pack', version: '1.0.0',
+ description: 'Synthetic fixture', author: 'Example Maintainer', license: 'MIT', homepage: 'https://example.com/skills',
+ gbrain_min_version: '0.51.0', brain_resident: true, skills: ['skills/alpha'] }));
+ durableGitRepo(root, ['.gitignore', 'skillpack.json']);
+};
+
+describe('forget companions and the shared skillpack', () => {
+ test('a forget whose companion page is an adopted skillpack file is refused skill_bundle_required; the file and catalog stay', () => managedBrain(async ({ engine, ctx, root }) => {
+ expect(await call(ctx, 'put_page', { slug: SKILL_PAGE, content: skillNotes })).toMatchObject({ state: 'committed' });
+ await setSharedSkillPolicy(ctx, 'default', { version: 1, enabled: true, classes: ['prose'], audiences: ['readers'], requirements: [], allow_follow: true }, null);
+ const adopted = await adoptSharedSkillpack(ctx, 'default', { request_id: randomUUID(), pack_id: 'example-pack', skills: [
+ { pack_id: 'example-pack', name: 'alpha', expected_revision: null, files: [
+ { path: 'skills/alpha/SKILL.md', content: '---\nname: alpha\ndescription: A synthetic shared skill\n---\n\nApproved instructions.\n', file_class: 'prose' },
+ ] },
+ ] });
+ expect(adopted.receipts[0].state).toBe('committed');
+ const skillFile = join(root, `${SKILL_PAGE}.md`);
+ const before = readFileSync(skillFile, 'utf8');
+ const catalog = () => engine.executeRaw("SELECT name,revision FROM shared_skill_heads WHERE source_id='default' ORDER BY name");
+ const heads = await catalog();
+ // A direct write to the page is refused by the knowledge guard.
+ await expect(call(ctx, 'put_page', { slug: SKILL_PAGE, content: skillNotes.replace('Notes.', 'Edited.') }))
+ .rejects.toMatchObject({ code: 'skill_bundle_required' });
+ expect(await call(ctx, 'remember', { fact: CLAIM, provenance: 'test', visibility: 'world', infer_entity: false })).toMatchObject({ state: 'committed', entity_slug: null });
+ const [fact] = await engine.executeRaw<{ id: number }>(
+ "SELECT id FROM facts WHERE source_id='default' AND entity_slug IS NULL AND fact=$1 AND expired_at IS NULL", [CLAIM]);
+ await expect(call(ctx, 'forget', { id: String(fact!.id) })).rejects.toMatchObject({ detail: 'skill_bundle_required' });
+ expect(readFileSync(skillFile, 'utf8')).toBe(before);
+ expect(await catalog()).toEqual(heads);
+ expect(await engine.executeRaw("SELECT 1 FROM fact_withdrawals WHERE source_id='default' AND fact_hash=gbrain_fact_fingerprint($1)", [CLAIM])).toHaveLength(0);
+ }, { setup }), 180_000);
+});