Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: put_page's summary guard compares tool calls kept, in one scan; local think fallback unchanged (/ship Step 11)

garrytand4ffed655813
diff --git a/src/core/persistence/page-mutations.ts b/src/core/persistence/page-mutations.ts
index e8b48976b4..6859a873d0 100644
--- a/src/core/persistence/page-mutations.ts
+++ b/src/core/persistence/page-mutations.ts
@@ -267,9 +267,12 @@ 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<{ type: string | null; trust_tier: string | null }>(
'SELECT type, trust_tier FROM pages WHERE source_id = $1 AND slug = $2', [sourceId, slug]);
- // The stored body's own literal summaries; a segmented page reads NULL inline columns, so its summarized round trip is refused.
- const storedText = async () => { const body = await readStoredBody(engine, sourceId, slug); return `${body?.compiled_truth ?? ''}\n${body?.timeline ?? ''}`; };
- if (summarized && stored && isQuotedPageType(stored.type) && holdsSummarizedToolCalls(await storedText(), unquoted)) {
+ // Compared with the stored body's tool calls; a segmented page reads NULL inline columns, so its summarized round trip is refused.
+ const losesCalls = async () => {
+ const body = await readStoredBody(engine, sourceId, slug);
+ return body?.compiled_truth == null || holdsSummarizedToolCalls(`${body.compiled_truth}\n${body.timeline ?? ''}`, unquoted);
+ };
+ if (summarized && stored && isQuotedPageType(stored.type) && await losesCalls()) {
throw new OperationError('invalid_params', `put_page: this content has tool calls that a read of ${slug} summarized; writing it would replace the stored calls with their summaries.`,
'Nothing was written. Read the page with get_page include_content: true (its content keeps the stored text) and write that back, or change the page with edit_page.');
}
diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.ts
index c95d6cdb9b..9c811e4018 100644
--- a/src/core/search/transcript-quote.ts
+++ b/src/core/search/transcript-quote.ts
@@ -429,13 +429,26 @@ 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 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.
+ * 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.
*/
export function holdsSummarizedToolCalls(stored: string, text: string): boolean {
- 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));
+ const tally = (s: string) => {
+ const counts = new Map<string, number>();
+ for (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));
+ 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 kept = calls(text);
+ return [...summaries.keys()].some(s => (kept.get(s) ?? 0) < (storedCalls.get(s) ?? 0));
}
/**
diff --git a/src/core/think/index.ts b/src/core/think/index.ts
index 60b26430ff..f6f46cc9dd 100644
--- a/src/core/think/index.ts
+++ b/src/core/think/index.ts
@@ -458,8 +458,8 @@ export interface ExtractiveFallback {
* title + the excerpt window with the strongest query-term coverage, cited
* `[slug]`. NEVER fabricates: every line quotes ONE gathered page and an
* empty gather returns null (ENG-19 — no pages, no answer, ever).
- * GST-687: a chat page's excerpt is chosen with its tool-call blocks summarized and is fenced as quoted source
- * material, as think's prompt does (#8042); with `untrusted` (a remote caller) so is a page below the owner's tier.
+ * GST-687: with `untrusted` (a remote caller), a chat page's excerpt is chosen with its tool-call blocks summarized, and it
+ * and a page below the owner's tier are fenced as quoted source material, as think's prompt does (#8042); local is unchanged.
*/
export function composeExtractiveFallback(
pages: SearchResult[],
@@ -477,8 +477,8 @@ export function composeExtractiveFallback(
const title = String(page.title ?? '') || slug;
const slugIdentity = slug.split('/').pop()?.replace(/[-_]/g, ' ') ?? '';
const raw = String(page.chunk_text ?? page.compiled_truth ?? page.snippet ?? '');
- const quote = quoteLabel({ ...p, chunk_text: raw }, untrusted);
- const content = quoteLabel({ ...p, chunk_text: raw }) !== null ? neutralizeToolCalls(raw) : raw;
+ const quote = untrusted ? quoteLabel({ ...p, chunk_text: raw }, true) : null;
+ const content = quote !== null && quoteLabel({ ...p, chunk_text: raw }) !== null ? neutralizeToolCalls(raw) : raw;
const excerpt = selectRelevantExcerpt(
content, question, EXTRACTIVE_EXCERPT_LEN, `${title} ${slugIdentity}`,
).trim();
diff --git a/test/search-transcript-quote-tiers.test.ts b/test/search-transcript-quote-tiers.test.ts
index c06a041174..e793d2ad00 100644
--- a/test/search-transcript-quote-tiers.test.ts
+++ b/test/search-transcript-quote-tiers.test.ts
@@ -273,6 +273,20 @@ for (const backend of testBackends()) {
expect((await engine.getPage(slug))!.compiled_truth).toBe(stored.compiled_truth.replace('noted.', 'noted (checked).'));
}, 120_000);
+ test('remote put_page refuses a summarized read even after the page\'s literal summary was cut from it', async () => {
+ const slug = 'conversations/probe-literal-summary';
+ const stored = (await engine.getPage(slug))!;
+ const summarized = await ok('get_page', { slug });
+ const read = String(summarized.compiled_truth).split('\n').slice(2, -1).join('\n');
+ const cut = read.split('\n\n').filter(paragraph => !paragraph.includes('earlier result')).join('\n\n');
+ expect(cut.split(neutralizeToolCalls(TOOL_CALL)).length - 1).toBe(1);
+ const res = await dispatchToolCall(engine, 'put_page',
+ { slug, content: `---\ntype: conversation\ntitle: Probe session\n---\n\n${cut}`, 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);
+ }, 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;
diff --git a/test/search/transcript-quote.test.ts b/test/search/transcript-quote.test.ts
index 1f6ec33468..d6e3605848 100644
--- a/test/search/transcript-quote.test.ts
+++ b/test/search/transcript-quote.test.ts
@@ -6,7 +6,7 @@
* put_page inverse.
*/
import { describe, expect, test } from 'bun:test';
-import { fenceTranscriptRows, fenceTranscriptText, isQuotedFactSource, isQuotedPageType, neutralizeToolCalls, neutralizeTranscriptRows, quoteFacts, rowPreview, unquoteTranscriptText } from '../../src/core/search/transcript-quote.ts';
+import { fenceTranscriptRows, fenceTranscriptText, holdsSummarizedToolCalls, isQuotedFactSource, isQuotedPageType, neutralizeToolCalls, neutralizeTranscriptRows, quoteFacts, rowPreview, unquoteTranscriptText } from '../../src/core/search/transcript-quote.ts';
import { capEvidenceToBudget, countEvidenceTokens, EVIDENCE_CUT_MARKER, EVIDENCE_OMISSION, type DeliveryMeta } from '../../src/core/search/evidence-delivery.ts';
import type { SearchResult } from '../../src/core/types.ts';
@@ -498,3 +498,22 @@ describe('#8042: what is quoted, and the page body fence round trip', () => {
expect(unquoteTranscriptText('plain body', 'slack/a')).toBe('plain body');
});
});
+
+describe('holdsSummarizedToolCalls (GST-687 put_page guard)', () => {
+ const call = JSON.stringify({ type: 'tool_use', id: 'toolu_a', name: 'search', input: { query: 'ledger' } });
+ const summary = neutralizeToolCalls(call);
+ const stored = `**alice-example**: earlier result ${summary}\n\n**bob-example**: ${call}`;
+
+ test('content that keeps a summary of a stored call but fewer of the calls is refused; the stored text, or text with no summary, is not', () => {
+ expect(holdsSummarizedToolCalls(stored, neutralizeToolCalls(stored))).toBe(true);
+ expect(holdsSummarizedToolCalls(stored, `**bob-example**: ${summary}`)).toBe(true);
+ expect(holdsSummarizedToolCalls(stored, `${stored}\n\nnoted.`)).toBe(false);
+ expect(holdsSummarizedToolCalls(stored, `**alice-example**: earlier result ${summary}`)).toBe(true);
+ expect(holdsSummarizedToolCalls(stored, '**alice-example**: noted.')).toBe(false);
+ });
+
+ 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);
+ }, 5_000);
+});
diff --git a/test/think-extractive.serial.test.ts b/test/think-extractive.serial.test.ts
index 325bf47d20..e389e4a195 100644
--- a/test/think-extractive.serial.test.ts
+++ b/test/think-extractive.serial.test.ts
@@ -138,6 +138,16 @@ describe('composeExtractiveFallback — ENG-19 never-fabricate pins', () => {
expect(r!.answer).toContain('Alpha Note');
});
+ it('a local fallback prints a conversation page as stored; a remote one summarizes its tool call inside the fence', () => {
+ const call = JSON.stringify({ type: 'tool_use', id: 'toolu_a', name: 'search', input: { query: 'zebras' } });
+ const chat = [{ slug: 'conversations/zebra-session', title: 'Zebra session', type: 'conversation', chunk_text: `**alice-example**: zebras migrating. ${call}` }] as unknown as SearchResult[];
+ expect(composeExtractiveFallback(chat, 'zebras migrating')!.answer).toContain(call);
+ const remote = composeExtractiveFallback(chat, 'zebras migrating', true)!.answer;
+ expect(remote).not.toContain(call);
+ expect(remote).toContain('[tool call: search, arguments omitted]');
+ expect(remote).toContain('[quoted conversation excerpt from conversations/zebra-session;');
+ });
+
it('caps the digest at the top 5 pages', () => {
const many = Array.from({ length: 8 }, (_, i) => ({
slug: `notes/p-${i}`, title: `Page ${i}`, chunk_text: `Content number ${i}.`,