Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(cli): escape record-boundary chars in list_pages TSV output (#5784)

garrytan8a99a38c9152
diff --git a/scripts/module-size-limits.tsv b/scripts/module-size-limits.tsv
index 6d544473bb..3eb706b937 100644
--- a/scripts/module-size-limits.tsv
+++ b/scripts/module-size-limits.tsv
@@ -3,7 +3,7 @@
# Raising a ceiling is a conscious, reviewer-visible act. Lower ceilings in
# the same commit as any peel (the guard fails on >50 lines of stale slack).
# Columns: path max_lines policy note
-src/cli.ts 3198 ratchet +2 fix wave 5 (#5401): projections drain help line and thin-client hint; +2 (#5137) key-shadow warning seam, logic in ai/key-warnings.ts; +2 System One v1: decide help line + thin-client refusal hint; refactor wave 1 split target (W4 cli); ceiling rationale: docs/architecture/key-files/entrypoints-and-docs.md entry for src/cli.ts; evidence delivery: thin-client return_unit version-skew warning (+3; logic in search/evidence-delivery.ts); put stdin recovery (#5700): stdin hint in the --content requires-a-value error and stdin usage/required text in printOpHelp (+7); logic in persistence/deactivation.ts); +36 #5827: link-command help examples, local --source vs --source-id/--all-sources conflict check, ambient-binding return from applyThinClientSourceScope (logic in cli/source-scope.ts)
+src/cli.ts 3202 ratchet +2 fix wave 5 (#5401): projections drain help line and thin-client hint; +2 (#5137) key-shadow warning seam, logic in ai/key-warnings.ts; +2 System One v1: decide help line + thin-client refusal hint; refactor wave 1 split target (W4 cli); ceiling rationale: docs/architecture/key-files/entrypoints-and-docs.md entry for src/cli.ts; evidence delivery: thin-client return_unit version-skew warning (+3; logic in search/evidence-delivery.ts); put stdin recovery (#5700): stdin hint in the --content requires-a-value error and stdin usage/required text in printOpHelp (+7); logic in persistence/deactivation.ts); +36 #5827: link-command help examples, local --source vs --source-id/--all-sources conflict check, ambient-binding return from applyThinClientSourceScope (logic in cli/source-scope.ts); +4 #5433: list_pages TSV cell escaping in formatResult
src/commands/autopilot.ts 1635 ratchet refactor wave 1 split target (W4 autopilot, cut line); ceiling rationale: docs/architecture/key-files/commands-1.md entry for src/commands/autopilot.ts; +2 fix wave 5: nosemgrep justifications on the per-brain job launchctl/systemctl calls (#5195)
src/commands/doctor.ts 653 ratchet refactor wave 1 split target (W4 doctor); ceiling rationale: docs/architecture/key-files/commands-2.md entry for src/commands/doctor.ts
src/commands/embed.ts 2190 ratchet lowered v0.46.26.0: embedBatchWithBackoff + retry cluster peeled to core/embed-retry.ts (core/commands layering fix); grown wave-g (#4530): per-model input-limit wiring; wave-k #3622 reimpl: --stale zero-progress failure quarantine; +29 #4668 (#4530): oversize-heal call sites in both --stale drains — kept low by peeling the healed-row remap to core/embed-oversize-heal.ts (shared with embed-stale.ts); +35 #4647 (refs #4599): per-tick timeout+abort on the single-flight lock heartbeat refresh (interval deliberately NOT unref'd); +122 refs #4599: stall-watchdog race around the drain + shared idempotent flushAndRelease + CLI stall exit mapping (watchdog machinery itself lives in core/embed-stall.ts); +12: adversarial-review hardening (healed_splits progress key + heal-tick comment); #5105: guarded derived projection preparation and read seals; #5105: conditional embedding installer wiring; archived eligibility: active-only dispatch with honest stranded-work reporting; explicit archived admission: live-source snapshots and honest refused-installation failures; +2 fw3: embed --stale --images dispatch (sweep lives in core/embed-stale-images.ts); +5 #5289: dry run reports restamp-only chunks apart from chunks to embed (count lives in core/embedding-invalidation.ts); +6 #4616: keep a partially refused batch in embedPageTexts and name the recovery command in failure samples (guard lives in core/ai/embedding-guard.ts); +5 #5226: embed summary counts distinct pages; +3 #5804: null projection snapshot counted as a failure with one batch summary in embed --stale
diff --git a/src/cli.ts b/src/cli.ts
index 034e9d26e6..eb4667548c 100755
--- a/src/cli.ts
+++ b/src/cli.ts
@@ -1649,8 +1649,12 @@ export function formatResult(
case 'list_pages': {
const pages = result as any[];
if (pages.length === 0) return 'No pages found.\n';
+ const cell = (v: unknown): string => {
+ const s = v === null || v === undefined ? '' : String(v);
+ return s.replace(/\\/g, '\\\\').replace(/\t/g, '\\t').replace(/\r/g, '\\r').replace(/\n/g, '\\n');
+ };
return pages.map(p =>
- `${p.slug}\t${p.type}\t${p.updated_at?.toString().slice(0, 10) || '?'}\t${p.title}`,
+ `${cell(p.slug)}\t${cell(p.type)}\t${cell(p.updated_at?.toString().slice(0, 10) || '?')}\t${cell(p.title)}`,
).join('\n') + '\n';
}
case 'search':
diff --git a/test/cli.test.ts b/test/cli.test.ts
index e24009009a..7dd5d8818c 100644
--- a/test/cli.test.ts
+++ b/test/cli.test.ts
@@ -128,6 +128,65 @@ describe('BigInt-safe output normalization (#2450)', () => {
});
});
+// #5433: `gbrain list` (formatResult('list_pages', ...) at src/cli.ts) joined
+// raw fields with \t and rows with \n. Titles are free text; a title with a
+// newline turns one page into two output lines, a tab adds a column. Anything
+// that pipes `gbrain list` into `cut`/`awk`/`while read` miscounts or
+// misattributes pages. The fix escapes \n/\r/\t/\\ in every cell so the row
+// boundary is always the single \n the caller asked for. --json output is
+// untouched (the JSON path carries the raw characters).
+describe("list_pages TSV escaping (#5433)", () => {
+ async function render(pages: unknown[]): Promise<string> {
+ const { formatResult } = await import('../src/cli.ts');
+ return formatResult('list_pages', pages, {});
+ }
+
+ test('a title with a newline stays on one output line', async () => {
+ const out = await render([
+ { slug: 'notes/example-one', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'first line\nsecond\tpart' },
+ ]);
+ // Exactly one row (plus the trailing newline) — the embedded \n /\t are
+ // escaped as two-character sequences, not emitted as record separators.
+ expect(out.split('\n').length).toBe(2);
+ expect(out).toContain('first line\\nsecond\\tpart');
+ });
+
+ test('a title with a tab does not add a column', async () => {
+ const out = await render([
+ { slug: 'notes/example-one', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'a\tb' },
+ ]);
+ // The escaped title sits in column 4; column counts stay at 4 per row.
+ const rows = out.trim().split('\n');
+ for (const r of rows) expect(r.split('\t').length).toBe(4);
+ });
+
+ test('a title with a literal backslash is escaped first so later escapes do not double-escape', async () => {
+ const out = await render([
+ { slug: 'notes/example-one', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'a\\b\nc' },
+ ]);
+ // \\ escapes to \\\\, then \n escapes to \\n. Final: a\\\\b\\nc.
+ expect(out).toContain('a\\\\b\\nc');
+ });
+
+ test('multiple rows with adversarial titles all stay on their own lines', async () => {
+ const out = await render([
+ { slug: 'a', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'plain' },
+ { slug: 'b', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'has\nnewline' },
+ { slug: 'c', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'has\ttab' },
+ { slug: 'd', type: 'note', updated_at: '2026-01-05T10:00:00Z', title: 'has\r\nwindows-end' },
+ ]);
+ const rows = out.trim().split('\n');
+ expect(rows.length).toBe(4);
+ // 4 columns per row.
+ for (const r of rows) expect(r.split('\t').length).toBe(4);
+ });
+
+ test('empty list still prints the "No pages found." sentinel', async () => {
+ const out = await render([]);
+ expect(out).toBe('No pages found.\n');
+ });
+});
+
describe('CLI version', () => {
test('VERSION matches package.json', async () => {
const { VERSION } = await import('../src/version.ts');