Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: put_page's summary guard refuses a summary a whole read never produces, as a cut window gives (/ship fix-check)
garrytan7b5fe87df026
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 65abea3e07..a5e041d337 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -22,7 +22,7 @@ Before, the quote fence (#8066) covered chat page types and conversation-derived- `src/core/search/transcript-quote.ts`: `quotesTier` / `quotesStored` (a missing tier reads as `unknown`, so it fails closed); `quoteFacts` reads each fact's stored tier from the database for a remote reader.- Search, query, recall, assemble_evidence, saved facts, context_pack, delta, `_meta.brain_hot_memory` (its cache key carries the quoting), `get_page` (body, timeline, windows), `fetch`, `get_timeline`, `get_chunks`, `search_exact`, `think`'s extractive fallback, `get_versions` and `known_as_of` reads (by the version's tier snapshot; a version without one reads as unverified) and `list_write_requests` / `get_write_request` outcome text quote by tier for remote readers.-- Tool-call blocks in conversation pages are summarized on `get_page`'s reading shape and `view: read` windows, `fetch`, `search_exact`, `get_chunks` and `think`; `include_content`, `view: markdown` and gbrain's thin client keep the stored bytes inside the fence. A remote `put_page` whose content keeps a tool call's summary but fewer of that call than the stored conversation page holds refuses with `invalid_params` and stores nothing.+- Tool-call blocks in conversation pages are summarized on `get_page`'s reading shape and `view: read` windows, `fetch`, `search_exact`, `get_chunks` and `think`; `include_content`, `view: markdown` and gbrain's thin client keep the stored bytes inside the fence. A remote `put_page` whose content holds a tool-call summary that a whole read of the stored conversation page never produces, or keeps a call's summary but fewer of that call than the page holds, refuses with `invalid_params` and stores nothing.- A remote `put_page` round trip of any quoted page stores the body, never the fence; a forged fence in stored text stays inside the quote.- Rendered context_pack/delta lines put a quoted fact's label on the marker line and close its fence on its own line.- `gbrain doctor`: `trust_tiers` and `trust_sources_unclaimed` say remote readers see unverified pages quoted and name the claim and backfill commands.diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.tsindex 9c811e4018..4e8d5047da 100644--- a/src/core/search/transcript-quote.ts+++ b/src/core/search/transcript-quote.ts@@ -429,9 +429,10 @@ function neutralizeWithMap(text: string, header: boolean): { text: string; map:const SUMMARY_RE = /\[tool calls?(?:: [^\]\n]*)?, arguments omitted\]/g;/**- * GST-687: `text` holds the summary of a tool call the stored page (`stored`: its body) holds as a call, and keeps fewer of- * those calls than the stored page does, so a reading shape (get_page, fetch, search) replaced at least one with its summary- * and writing it back would lose the call. Each string is scanned once, whatever the number of distinct summaries.+ * GST-687: `text` holds a tool-call summary that a whole read of the stored page (`stored`: its body) does not hold (a+ * partial read, a window or a chunk, can cut a call into another summary), or the summary of a call the stored page holds+ * and fewer of those calls, so a reading shape (get_page, fetch, search) replaced at least one call with its summary and+ * writing it back would lose the call. Each string is scanned once, whatever the number of distinct summaries.*/export function holdsSummarizedToolCalls(stored: string, text: string): boolean {const tally = (s: string) => {@@ -439,16 +440,17 @@ export function holdsSummarizedToolCalls(stored: string, text: string): booleanfor (const [summary] of s.matchAll(SUMMARY_RE)) counts.set(summary, (counts.get(summary) ?? 0) + 1);return counts;};- const calls = (s: string) => {- const counts = tally(neutralizeToolCalls(s));+ const calls = (s: string, read = tally(neutralizeToolCalls(s))) => {+ const counts = new Map(read);for (const [summary, n] of tally(s)) counts.set(summary, (counts.get(summary) ?? 0) - n);return counts;};const summaries = tally(text);if (summaries.size === 0) return false;- const storedCalls = calls(stored);+ const read = tally(neutralizeToolCalls(stored));+ const storedCalls = calls(stored, read);const kept = calls(text);- return [...summaries.keys()].some(s => (kept.get(s) ?? 0) < (storedCalls.get(s) ?? 0));+ return [...summaries.keys()].some(s => !read.has(s) || (kept.get(s) ?? 0) < (storedCalls.get(s) ?? 0));}/**diff --git a/test/search/transcript-quote.test.ts b/test/search/transcript-quote.test.tsindex d6e3605848..a80cabeac3 100644--- a/test/search/transcript-quote.test.ts+++ b/test/search/transcript-quote.test.ts@@ -512,6 +512,19 @@ describe('holdsSummarizedToolCalls (GST-687 put_page guard)', () => {expect(holdsSummarizedToolCalls(stored, '**alice-example**: noted.')).toBe(false);});+ test('a summary a whole read of the stored page never produces, as a cut window or chunk gives, is refused', () => {+ expect(holdsSummarizedToolCalls(stored, `${stored}\n\n**bob-example**: [tool call, arguments omitted]`)).toBe(true);+ const long = JSON.stringify({ type: 'tool_use', id: 'toolu_b', input: { query: 'ledger '.repeat(20) }, name: 'search' });+ const page = `**alice-example**: looking it up ${long}\n\n**bob-example**: thanks.`;+ const cut = page.indexOf('ledger') + 30;+ expect(holdsSummarizedToolCalls(page, neutralizeToolCalls(page.slice(0, cut)) + neutralizeToolCalls(page.slice(cut)))).toBe(true);+ const xml = '<function_calls>\n<invoke name="search">\n<parameter name="query">ledger</parameter>\n</invoke>\n<invoke name="fetch">\n<parameter name="url">x</parameter>\n</invoke>\n</function_calls>';+ const xmlPage = `**alice-example**: checking\n${xml}\n\n**bob-example**: thanks.`;+ const at = xmlPage.indexOf('<invoke name="fetch"');+ expect(holdsSummarizedToolCalls(xmlPage, neutralizeToolCalls(xmlPage.slice(0, at)) + neutralizeToolCalls(xmlPage.slice(at)))).toBe(true);+ expect(holdsSummarizedToolCalls(xmlPage, neutralizeToolCalls(xmlPage))).toBe(true);+ });+test('thousands of distinct summaries are counted in one scan', () => {const many = Array.from({ length: 20_000 }, (_, i) => `[tool call: tool_${i}, arguments omitted]`).join('\n');expect(holdsSummarizedToolCalls(many, many)).toBe(false);