Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

doctor stale_mentions: shell-quote each suggested unlink command

garrytanaa9cdaff7d5c
diff --git a/src/commands/doctor/checks/stale-mentions.ts b/src/commands/doctor/checks/stale-mentions.ts
index c2a321b696..0dbfdc35b3 100644
--- a/src/commands/doctor/checks/stale-mentions.ts
+++ b/src/commands/doctor/checks/stale-mentions.ts
@@ -24,6 +24,7 @@
import type { BrainEngine } from '../../../core/engine.ts';
import type { Check } from '../../doctor.ts';
import { scanStaleMentions } from '../../../core/by-mention.ts';
+import { shellQuote } from '../../../core/agent-output.ts';
export async function staleMentionsCheck(engine: BrainEngine): Promise<Check> {
try {
@@ -51,7 +52,11 @@ export async function staleMentionsCheck(engine: BrainEngine): Promise<Check> {
.sort(([a], [b]) => a.localeCompare(b))
.map(([k, n]) => `${n} ${k}`)
.join(', ');
- const eg = res.examples.map(e => `${e.from} -> ${e.to}: gbrain unlink ${e.from} ${e.to} --link-type ${e.link_type} --link-source mentions`).join('; ');
+ const unlink = res.examples.map(e => {
+ const argv = ['gbrain', 'unlink', e.from, e.to, '--link-type', e.link_type, '--link-source', 'mentions'];
+ return { argv, command: shellQuote(argv) };
+ });
+ const eg = unlink.map((u, i) => `${res.examples[i]!.from} -> ${res.examples[i]!.to}: ${u.command}`).join('; ');
// An empty gazetteer makes EVERY mentions row unreproducible, which is
// a different diagnosis from ordinary drift — name it, or the operator
// reads "everything is stale" and starts deleting real links.
@@ -65,6 +70,7 @@ export async function staleMentionsCheck(engine: BrainEngine): Promise<Check> {
`${res.staleLinks} of ${res.linksScanned} by-mention link(s) no longer follow from the current gazetteer${coverage} (${kinds}). ` +
`${why}Re-running the scan will NOT remove them — the write path is additive. ` +
`Remove each one with its own command, which names the link type it is stored under (typed links use a verb): ${eg}`,
+ details: { unlink },
};
} catch (e) {
return {
diff --git a/test/doctor-stale-mentions-link-type-8814.test.ts b/test/doctor-stale-mentions-link-type-8814.test.ts
index acfb2bd65e..837a1745e5 100644
--- a/test/doctor-stale-mentions-link-type-8814.test.ts
+++ b/test/doctor-stale-mentions-link-type-8814.test.ts
@@ -9,6 +9,7 @@ import { afterAll, beforeAll, expect, test } from 'bun:test';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { staleMentionsCheck } from '../src/commands/doctor/checks/stale-mentions.ts';
import { operationsByName } from '../src/core/operations.ts';
+import { shellQuote } from '../src/core/agent-output.ts';
import type { OperationContext } from '../src/core/operations.ts';
let engine: PGLiteEngine;
@@ -36,3 +37,21 @@ test('the suggested unlink removes a stale typed_ner link', async () => {
expect(check.message).toContain('gbrain unlink notes/meeting companies/acme-example --link-type founded --link-source mentions');
expect(await engine.executeRaw("SELECT 1 FROM links WHERE link_source = 'mentions'")).toHaveLength(0);
});
+
+test('the suggested unlink is shell-quoted: metacharacters reach gbrain as plain words', async () => {
+ await engine.executeRaw("DELETE FROM links");
+ const from = "notes/o'brien-$(printf x)";
+ const linkType = "founded$(printf x)'s";
+ await engine.putPage(from, { type: 'note', title: 'Quoted', compiled_truth: 'Quiet day.', timeline: '', frontmatter: {} });
+ await engine.addLinksBatch([{ from_slug: from, to_slug: 'companies/acme-example', link_type: linkType, link_source: 'mentions', link_kind: 'typed_ner' }]);
+
+ const check = await staleMentionsCheck(engine);
+ expect(check.status).toBe('warn');
+ const argv = ['gbrain', 'unlink', from, 'companies/acme-example', '--link-type', linkType, '--link-source', 'mentions'];
+ const command = shellQuote(argv);
+ const run = Bun.spawnSync(['bash', '-c', `gbrain() { printf '%s\\0' "$@"; }; ${command}`]);
+ expect(run.exitCode).toBe(0);
+ expect(run.stdout.toString().split('\0').slice(0, -1)).toEqual(argv.slice(1));
+ expect(check.message).toContain(command);
+ expect(check.details?.unlink).toEqual([{ argv, command }]);
+});