Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Fix review findings on page rules and the run notice

garrytan3ac631914c90
diff --git a/src/commands/access-rules.ts b/src/commands/access-rules.ts
index e93e038bc7..50975f883c 100644
--- a/src/commands/access-rules.ts
+++ b/src/commands/access-rules.ts
@@ -247,6 +247,8 @@ async function ruleCmd(engine: BrainEngine, sub: string | undefined, args: strin
if ('problem' in r) {
throw opError('access_rule_invalid', `The rule is invalid: ${r.problem}.`, 'Nothing was written. Example: gbrain access rule add --prefix partners/alice-example/ --readers alice-example,group:team-a');
}
+ const taken = flag(args, '--id') === undefined ? base.find(b => b.id === r.rule.id && b.prefix !== r.rule.prefix) : undefined;
+ if (taken) throw opError('access_rule_invalid', `Rule id "${taken.id}" already names ${taken.prefix}, and ${r.rule.prefix} derives the same id.`, `Nothing was written. Name the new rule yourself: gbrain access rule add --id <rule> ${r.rule.prefix.endsWith('/') ? '--prefix' : '--slug'} ${r.rule.prefix} --readers <list>.`);
const clash = base.find(b => b.prefix === r.rule.prefix && b.id !== r.rule.id);
if (clash) throw opError('access_rule_invalid', `Rule "${clash.id}" already covers ${r.rule.prefix}.`, `Nothing was written. Change that rule's readers instead: gbrain access rule add --id ${clash.id} ${r.rule.prefix.endsWith('/') ? '--prefix' : '--slug'} ${r.rule.prefix} --readers <list>.`);
desired = sortRules([...base.filter(b => b.id !== r.rule.id), r.rule]);
diff --git a/src/core/access/folder-rules.ts b/src/core/access/folder-rules.ts
index 0cdb28f753..ef6317169e 100644
--- a/src/core/access/folder-rules.ts
+++ b/src/core/access/folder-rules.ts
@@ -213,13 +213,14 @@ export function sortRules(rules: readonly FolderRule[]): FolderRule[] {
/** The file text for a rulebook: the same grammar parsePolicyYaml reads, members written bare. */
export function serializePolicyYaml(rules: readonly FolderRule[], comment?: string): string {
- const reader = (p: string) => p.startsWith('member:') ? p.slice(7) : p;
+ const plain = (v: string) => (/^[a-z][a-z0-9._/:-]*$/.test(v) && !['true', 'false', 'null'].includes(v) ? v : JSON.stringify(v));
+ const reader = (p: string) => plain(p.startsWith('member:') ? p.slice(7) : p);
const lines = [...(comment ? comment.split('\n').map(l => `# ${l}`) : []), `version: ${POLICY_VERSION}`];
const sorted = sortRules(rules);
if (!sorted.length) return [...lines, 'rules: []', ''].join('\n');
lines.push('rules:');
for (const r of sorted) {
- lines.push(` - id: ${r.id}`, r.prefix.endsWith('/') ? ` prefix: ${r.prefix}` : ` slug: ${r.prefix}`, ` readable_by: [${r.readable_by.map(reader).join(', ')}]`);
+ lines.push(` - id: ${plain(r.id)}`, r.prefix.endsWith('/') ? ` prefix: ${plain(r.prefix)}` : ` slug: ${plain(r.prefix)}`, ` readable_by: [${r.readable_by.map(reader).join(', ')}]`);
}
return [...lines, ''].join('\n');
}
diff --git a/src/core/ops/pages.ts b/src/core/ops/pages.ts
index 989d932a96..c77697734b 100644
--- a/src/core/ops/pages.ts
+++ b/src/core/ops/pages.ts
@@ -379,7 +379,7 @@ const put_page: Operation = {
}
if (ctx.remote !== false && typeof p.slug === 'string') await assertLargePageAdmission(ctx.engine, p.slug, sourceId, p);
(await import('../persistence/page-visibility.ts')).assertSubmittedReaders(ctx.remote, p.slug, p.content);
- return submitPageMutation(ctx, { operation: 'put_page', params: p }).then(funnelPassThrough('save')).catch(error => rethrowNamingRevisionSource(ctx, sourceId, p, error)).then(async result => (await import('../search/ignored-privacy-labels.ts')).withIgnoredPrivacyLabelNotice(ctx, sourceId, p, result));
+ return submitPageMutation(ctx, { operation: 'put_page', params: p }).then(funnelPassThrough('save')).catch(error => rethrowNamingRevisionSource(ctx, sourceId, p, error)).then(result => import('../search/ignored-privacy-labels.ts').then(m => m.withIgnoredPrivacyLabelNotice(ctx, sourceId, p, result), () => result));
},
cliHints: { name: 'put', positional: ['slug'], stdin: 'content' },
};
diff --git a/src/core/search/ignored-privacy-labels.ts b/src/core/search/ignored-privacy-labels.ts
index 6ad2118a52..83a7d750b0 100644
--- a/src/core/search/ignored-privacy-labels.ts
+++ b/src/core/search/ignored-privacy-labels.ts
@@ -155,6 +155,7 @@ export async function ignoredPrivacyLabelRunNotice(engine: Pick<BrainEngine, 'ex
const rows = await engine.executeRaw<{ type: string | null; frontmatter: Record<string, unknown> | null }>(
`SELECT p.type, p.frontmatter FROM pages p
WHERE p.source_id = $1 AND ${'slugs' in scope ? 'p.slug = ANY($2::text[])' : 'p.updated_at >= $2::timestamptz'} AND p.deleted_at IS NULL
+ AND NOT (p.frontmatter ? 'readable_by') AND p.folder_rule_ids IS NULL
AND ${privacyLabelKeySql('p')} AND ${ignoredPrivacyLabelSql('p')}
LIMIT ${SCAN_CAP}`, [sourceId, 'slugs' in scope ? [...new Set(scope.slugs)] : scope.since]);
const tally = tallyIgnoredLabels(rows.map(row => ignoredPrivacyLabels(row)));
diff --git a/test/access-page-rule-fileless.test.ts b/test/access-page-rule-fileless.test.ts
index cfe3ab4cb5..83a9ca5640 100644
--- a/test/access-page-rule-fileless.test.ts
+++ b/test/access-page-rule-fileless.test.ts
@@ -80,6 +80,12 @@ describe('page rules (slug:) in the folder-rule grammar', () => {
expect(folderRulesAllow('projects/plan', rules, ['member:alice-example'])).toBe(true);
});
+ test('numeric and keyword-like slugs, prefixes and readers survive a write and read of access.yml', () => {
+ const rules = ['2024', 'true', 'null', '1.5', '2024/'].map(p => { const r = normalizeRule(p.endsWith('/') ? { prefix: p, readable_by: ['2024'] } : { slug: p, readable_by: ['2024'] }); if (!('rule' in r)) throw new Error(r.problem); return r.rule; });
+ const back = parsePolicyYaml(serializePolicyYaml(rules));
+ expect(back.ok && back.rules.map(r => [r.id, r.prefix, r.readable_by])).toEqual([...rules].sort((a, b) => (a.id < b.id ? -1 : 1)).map(r => [r.id, r.prefix, r.readable_by]));
+ });
+
test('a prefix without a trailing slash is still refused, and names the slug form; slug and prefix together are refused', () => {
const r = normalizeRule({ prefix: 'projects', readable_by: ['owner-example'] });
expect('problem' in r && r.problem).toContain('slug: projects');
@@ -156,6 +162,9 @@ for (const backend of testBackends()) {
const read = await readers(engine);
const added = await cli(engine, ['rule', 'add', '--slug', 'projects', '--readers', 'owner-example', '--yes']);
expect({ state: added.state, store: added.store }).toEqual({ state: 'applied', store: 'database' });
+ const clash = await cli(engine, ['rule', 'add', '--prefix', 'projects.page/', '--readers', 'owner-example', '--yes']).catch(e => e);
+ expect({ code: clash.code, message: clash.message }).toEqual({ code: 'access_rule_invalid', message: 'Rule id "projects.page" already names projects, and projects.page/ derives the same id.' });
+ expect((await cli(engine, ['rule', 'list'])).rules.map((r: { id: string; prefix: string }) => [r.id, r.prefix])).toEqual([['projects.page', 'projects']]);
expect({ unbound: await read('unbound', 'projects'), owner: await read('owner', 'projects'), folder: await read('unbound', 'projects/plan') })
.toEqual({ unbound: false, owner: true, folder: true });
}, 120_000);