Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(trust): the release preview prints the whole claim and every field with control characters neutralized; doctor lists each fact once

garrytan23c371ae2a42
diff --git a/src/commands/doctor/checks/trust-release-unpreviewed.ts b/src/commands/doctor/checks/trust-release-unpreviewed.ts
index 9a905f0411..d0d70b62eb 100644
--- a/src/commands/doctor/checks/trust-release-unpreviewed.ts
+++ b/src/commands/doctor/checks/trust-release-unpreviewed.ts
@@ -18,8 +18,8 @@ async function runTrustReleaseUnpreviewed(ctx: DoctorContext): Promise<Check[]>
try {
const rows = await connectedEngine(ctx).executeRaw<{ id: number; source_id: string; slug: string; row_num: number; hold_id: number; total: number }>(
`SELECT f.id, f.source_id, f.source_markdown_slug AS slug, f.row_num, h.id AS hold_id, COUNT(*) OVER () AS total
- FROM facts f JOIN write_gate_holds h ON h.kind = 'fact' AND h.status = 'released' AND h.source_id = f.source_id
- AND h.slug = f.source_markdown_slug AND h.payload->>'row_num' = f.row_num::text
+ FROM facts f JOIN LATERAL (SELECT id FROM write_gate_holds WHERE kind = 'fact' AND status = 'released' AND source_id = f.source_id
+ AND slug = f.source_markdown_slug AND payload->>'row_num' = f.row_num::text ORDER BY id DESC LIMIT 1) h ON true
WHERE f.trust_tier = 'user_confirmed' AND f.expired_at IS NULL AND f.write_origin->>'channel' = 'trust_release'
AND f.write_origin->>'fields_previewed' IS NULL
ORDER BY f.id LIMIT ${LIMIT}`);
diff --git a/src/core/trust/owner-actions.ts b/src/core/trust/owner-actions.ts
index 1e7cc47c42..e103c0a97e 100644
--- a/src/core/trust/owner-actions.ts
+++ b/src/core/trust/owner-actions.ts
@@ -107,8 +107,10 @@ const bindingOf = (value: unknown): string => createHash('sha256')
/** A hold's approved state; #7656: plus the fence cell fields an index-existing release raises (`releaseCellOf`). */
const holdBinding = (hold: Pick<HoldRow, 'id' | 'status' | 'last_seen_at'>, cellFields: Record<string, unknown> | null = null): string =>
bindingOf({ id: hold.id, status: hold.status, last_seen_at: hold.last_seen_at, ...(cellFields ? { cell: cellFields } : {}) });
+/** One printable line: control characters (terminal escapes included) and runs of whitespace become one space. */
+const printable = (text: unknown): string => String(text ?? '').replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ').replace(/\s+/g, ' ').trim();
const snippet = (text: unknown, max = 80): string => {
- const flat = String(text ?? '').replace(/\s+/g, ' ').trim();
+ const flat = printable(text);
return flat.length > max ? `${flat.slice(0, max - 3)}...` : flat;
};
const trustCommand = (...args: string[]): string[] => ['gbrain', 'trust', ...args];
@@ -216,7 +218,7 @@ async function releaseCellOf(engine: BrainEngine, hold: HoldRow): Promise<{ cell
/** #7656: what a fact release raises besides its text, for the preview: the fence cell an index-existing release indexes, else the held payload. */
function releasedFields(hold: HoldRow, cell: ParsedFact | null): string {
const p = hold.payload;
- const quoted = (v: unknown) => typeof v === 'string' && v ? `"${snippet(v)}"` : null;
+ const quoted = (v: unknown) => typeof v === 'string' && v ? `"${printable(v)}"` : null;
const date = (v: unknown) => typeof v === 'string' && v ? v.slice(0, 10) : null;
const entries: Array<[string, unknown]> = cell
? [['kind', cell.kind], ['visibility', cell.visibility], ['confidence', cell.confidence], ['notability', cell.notability], ['valid from', cell.validFrom],
@@ -224,7 +226,7 @@ function releasedFields(hold: HoldRow, cell: ParsedFact | null): string {
['attributed to', cell.attributedTo], ['source', cell.source], ['context', quoted(cell.context)]]
: [['kind', typeof p.kind === 'string' && p.kind ? p.kind : 'fact'], ['visibility', p.visibility === 'world' ? 'world' : 'private'], ['valid from', date(p.valid_from)],
['valid until', date(p.valid_until)], ['source', p.source], ['context', quoted(p.context)]];
- const shown = entries.filter(([, v]) => v !== undefined && v !== null && v !== '').map(([k, v]) => `${k} ${v}`).join(', ');
+ const shown = entries.filter(([, v]) => v !== undefined && v !== null && v !== '').map(([k, v]) => `${k} ${printable(v)}`).join(', ');
return cell ? ` with fence row ${cell.rowNum} as it stands: ${shown}` : ` with ${shown}`;
}
/**
@@ -365,7 +367,7 @@ async function previewHold(engine: BrainEngine, id: number, action: 'release' |
binding: holdBinding(hold, cell?.fields ?? null), tier: hold.tier,
...(action === 'release' ? { target_tier: 'user_confirmed' as TrustTier } : {}),
summary: action === 'release'
- ? `Release held ${hold.kind} ${label} ("${snippet(text)}", ${hold.reason_families.join(', ') || 'detector error'})${entity} into memory as "confirmed by you"`
+ ? `Release held ${hold.kind} ${label} ("${hold.kind === 'fact' ? printable(text) : snippet(text)}", ${hold.reason_families.join(', ') || 'detector error'})${printable(entity)} into memory as "confirmed by you"`
+ (hold.kind === 'fact' ? releasedFields(hold, cell?.cell ?? null) : '')
: `Drop held ${hold.kind} ${label} ("${snippet(text)}"); it stays out of memory` };
}
diff --git a/test/trust-release-preview-binding.test.ts b/test/trust-release-preview-binding.test.ts
index 1397b6fabf..78af8563c3 100644
--- a/test/trust-release-preview-binding.test.ts
+++ b/test/trust-release-preview-binding.test.ts
@@ -153,6 +153,19 @@ for (const backend of testBackends()) {
expect(receipt).toMatchObject({ state: 'failed', error_code: 'preview_changed' });
}, { setup, databaseUrl }), 180_000);
+ test('the preview prints the whole claim and its source with control characters neutralized', () => repoLessBrain(databaseUrl, async (engine, ctx) => {
+ await enableTrustProtections(engine);
+ let ref = '';
+ try { await operationsByName.remember.handler(ctx, { fact: `${CLAIM} ${'and more words '.repeat(6)}tail\u001b[2K`, provenance: 'email\u001b[1A\u001b[2Kend of source',
+ content_origin: 'tool_output', request_id: randomUUID() }); }
+ catch (e) { ref = JSON.parse((e as { detail: string }).detail).hold_ref; }
+ expect(ref).toMatch(/^h\d+$/);
+ const { summary } = await previewOwnerAction(engine, { action: 'release', ref });
+ expect(summary).not.toMatch(/[\u0000-\u001f\u007f-\u009f]/);
+ expect(summary).toContain('more words tail ');
+ expect(summary).toContain('source email [1A [2Kend of source');
+ }), 180_000);
+
test('on a repo-less brain the preview shows the held payload fields the database-only release raises', () => repoLessBrain(databaseUrl, async (engine, ctx) => {
await operationsByName.put_page.handler(ctx, { slug, request_id: randomUUID(), content: PAGE });
const ref = await holdRemember(engine, ctx, slug);