Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: put_page's summary guard counts each summary against the stored page's literal copies (/ship red-team)

garrytan4792e8c0328b
diff --git a/src/core/persistence/page-mutations.ts b/src/core/persistence/page-mutations.ts
index 0bfffaf40e..229ac84c18 100644
--- a/src/core/persistence/page-mutations.ts
+++ b/src/core/persistence/page-mutations.ts
@@ -267,7 +267,8 @@ async function unquotedPutContent(engine: BrainEngine, sourceId: string, slug: s
if (!summarized && (unquoted === content || isQuotedPageType(String(parseMarkdownHead(content).frontmatter.type ?? '')))) return unquoted;
const [stored] = await engine.executeRaw<{ id: number; type: string | null; trust_tier: string | null }>(
'SELECT id, type, trust_tier FROM pages WHERE source_id = $1 AND slug = $2', [sourceId, slug]);
- // The stored page's own literal summaries, read from its chunks; a summary split across two chunks is missed, so that write is refused.
+ // The stored page's own literal summaries, read from its chunks; a summary split across two chunks is missed, so that write is refused,
+ // and one in the overlap of two chunks counts twice.
const storedSummaries = async (pageId: number) => (await engine.executeRaw<{ chunk_text: string }>(
`SELECT cc.chunk_text FROM content_chunks cc JOIN pages p ON p.id = cc.page_id
WHERE cc.page_id = $1 AND cc.chunk_text LIKE '%, arguments omitted]%' AND ${chunkVisibleFilter('p', 'cc')}`, [pageId])).map(r => r.chunk_text).join('\n');
diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.ts
index ba0b99fa97..c95d6cdb9b 100644
--- a/src/core/search/transcript-quote.ts
+++ b/src/core/search/transcript-quote.ts
@@ -429,12 +429,13 @@ function neutralizeWithMap(text: string, header: boolean): { text: string; map:
const SUMMARY_RE = /\[tool calls?(?:: [^\]\n]*)?, arguments omitted\]/g;
/**
- * GST-687: `text` holds a tool-call summary the stored page does not hold as text (`stored`: the stored text that carries
- * summaries), so it came from a reading shape (get_page, fetch, search) and writing it back would replace a stored call
- * with its summary.
+ * GST-687: `text` holds a tool-call summary more times than the stored page holds it as text (`stored`: the stored text
+ * that carries summaries), so at least one came from a reading shape (get_page, fetch, search) and writing it back would
+ * replace a stored call with its summary.
*/
export function holdsSummarizedToolCalls(stored: string, text: string): boolean {
- return [...new Set(text.match(SUMMARY_RE) ?? [])].some(s => !stored.includes(s));
+ const count = (haystack: string, s: string) => haystack.split(s).length - 1;
+ return [...new Set(text.match(SUMMARY_RE) ?? [])].some(s => count(text, s) > count(stored, s));
}
/**
diff --git a/test/search-transcript-quote-tiers.test.ts b/test/search-transcript-quote-tiers.test.ts
index c613954763..187da3a61f 100644
--- a/test/search-transcript-quote-tiers.test.ts
+++ b/test/search-transcript-quote-tiers.test.ts
@@ -250,6 +250,28 @@ for (const backend of testBackends()) {
expect((await engine.getPage(slug))!.compiled_truth).toBe(stored.compiled_truth);
}, 120_000);
+ test('remote put_page counts summaries: a page holding one as text still refuses the read that summarized its call', async () => {
+ const slug = 'conversations/probe-literal-summary';
+ const literal = neutralizeToolCalls(TOOL_CALL);
+ expect(literal).toMatch(/^\[tool call: search, arguments omitted\]$/);
+ await ok('put_page', { slug, content: `${pageContent('conversation')}\n\n**bob-example** (2026-10-01 9:06 AM): earlier result ${literal}` });
+ const stored = (await engine.getPage(slug))!;
+ const chunks = await engine.executeRaw<{ n: number }>(
+ `SELECT count(*)::int AS n FROM content_chunks cc JOIN pages p ON p.id = cc.page_id WHERE p.slug = $1 AND cc.chunk_text LIKE '%, arguments omitted]%'`, [slug]);
+ expect(chunks[0]!.n).toBe(1);
+ const summarized = await ok('get_page', { slug });
+ const read = String(summarized.compiled_truth).split('\n').slice(2, -1).join('\n');
+ expect(read.split(literal).length - 1).toBe(2);
+ const res = await dispatchToolCall(engine, 'put_page',
+ { slug, content: `---\ntype: conversation\ntitle: Probe session\n---\n\n${read}`, expected_revision: summarized.revision }, remote);
+ expect(res.isError).toBe(true);
+ expect(res.content[0]!.text).toContain('summarized');
+ expect((await engine.getPage(slug))!.compiled_truth).toBe(stored.compiled_truth);
+ const page = await ok('get_page', { slug, include_content: true });
+ await ok('put_page', { slug, content: page.content.replace('noted.', 'noted (checked).'), expected_revision: page.revision });
+ expect((await engine.getPage(slug))!.compiled_truth).toBe(stored.compiled_truth.replace('noted.', 'noted (checked).'));
+ }, 120_000);
+
test('a remote round trip of an agent-written note stores the body, never the fence', async () => {
const slug = SLUG['note-first'];
const stored = (await engine.getPage(slug))!.compiled_truth;