Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

GBRA-156 (#7600): quote CRLF speaker lines as message boundaries; summarize <tool_call> blocks and Gemini functionCall values

garrytanc8a473d13b5e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dd4b4c529b..d2733c5e3e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -16,7 +16,7 @@ identifiers and attribution are available in the pre-removal Git revision
Imported chat transcripts can quote tool-call or function-call JSON, or XML-style call blocks, from the original conversation: a pasted agent log, a model that wrote its call as text. Search used to return that JSON verbatim, and one agent harness's prompt-injection classifier flagged such a result as malicious (#7600).
-A row whose page has type `conversation` (what `gbrain transcripts ingest` and the chat connectors write) now comes back with `quoted: "transcript"` and its `chunk_text` wrapped: a line naming the page as source material, not instructions, then the text inside a code fence it cannot close. Each tool-call block of these shapes becomes `[tool call: <name>, arguments omitted]`: a JSON object whose `type` names a tool or function call (`tool_use`, `function_call`, `tool_call` and their variants), a `tool_calls` or `function_call` value, or an XML-style `function_calls`, `invoke` or `tool_use` element. Other encodings (Gemini `functionCall`, `<tool_call>` wrappers, escaped or untyped JSON) are not summarized and stay inside the fence. An XML block cut off at either end of a chunk is replaced, and so is a JSON block cut at the end or whose type key is still in the chunk; tool results, prose that names a tag, and the conversation around a block stay. This happens at read time where `search` and `query` shape their rows (MCP and the CLI both pass through it) and where `assemble_evidence` assembles its evidence. An open-ended or cut block stops at the next message's speaker line. Under `return_unit: auto` an explicit `token_budget` counts the fence, so the delivered evidence still fits it, and a conversation-heavy result can come back with fewer or shorter rows (a rank-one conversation row is still returned when the fence alone is larger than a very small budget); budgets under other units and `query`'s legacy chunk budget do not count the fence, so those rows can run over by the marker and fence lines; a row's `delivered.tokens` and `match_spans` and the delivery's `tokens_delivered` and `budget_used` describe the fenced text. Stored pages, ranking and scores are unchanged, and every other page type comes back byte-for-byte as before. Only type `conversation` is quoted, and only by these three operations: `recall`, `think`, `search_exact`, `fetch` and `get_chunks` still return the text as stored.
+A row whose page has type `conversation` (what `gbrain transcripts ingest` and the chat connectors write) now comes back with `quoted: "transcript"` and its `chunk_text` wrapped: a line naming the page as source material, not instructions, then the text inside a code fence it cannot close. Each tool-call block of these shapes becomes `[tool call: <name>, arguments omitted]`: a JSON object whose `type` names a tool or function call (`tool_use`, `function_call`, `tool_call` and their variants), a `tool_calls`, `function_call` or Gemini `functionCall` value, or an XML-style `function_calls`, `invoke`, `tool_use` or `tool_call` element. Escaped or untyped JSON is not summarized and stays inside the fence. An XML block cut off at either end of a chunk is replaced, and so is a JSON block cut at the end or whose type key is still in the chunk; tool results, prose that names a tag, and the conversation around a block stay. This happens at read time where `search` and `query` shape their rows (MCP and the CLI both pass through it) and where `assemble_evidence` assembles its evidence. An open-ended or cut block stops at the next message's speaker line, with LF or CRLF line endings. Under `return_unit: auto` an explicit `token_budget` counts the fence, so the delivered evidence still fits it, and a conversation-heavy result can come back with fewer or shorter rows (a rank-one conversation row is still returned when the fence alone is larger than a very small budget); budgets under other units and `query`'s legacy chunk budget do not count the fence, so those rows can run over by the marker and fence lines; a row's `delivered.tokens` and `match_spans` and the delivery's `tokens_delivered` and `budget_used` describe the fenced text. Stored pages, ranking and scores are unchanged, and every other page type comes back byte-for-byte as before. Only type `conversation` is quoted, and only by these three operations: `recall`, `think`, `search_exact`, `fetch` and `get_chunks` still return the text as stored.
### What to watch for after you upgrade
diff --git a/docs/architecture/key-files/core-search-evidence.md b/docs/architecture/key-files/core-search-evidence.md
index 349870839e..4dddb562ce 100644
--- a/docs/architecture/key-files/core-search-evidence.md
+++ b/docs/architecture/key-files/core-search-evidence.md
@@ -5,4 +5,4 @@ Current behavior and load-bearing invariants; history belongs in Git and CHANGEL
- `src/core/search/evidence-date.ts` — C1 dated evidence blocks: the fixed header grammar (`[observed YYYY-MM-DD|unknown]` for page blocks; `[observed unknown; valid FROM to UNTIL]` for `recall` facts), `pageObservationDate` (effective-date precedence with `event_date` removed and no row-timestamp fallback; mirrors #6020's `resolveObservationDate`, the seam to swap once it lands), `formatBrainDay` from `effective-date.ts` (midnight UTC renders as written, other instants in `brain.timezone`; the one function think's `<page date>` also renders through, while the header's `observed` stays when the text was said, never the content date), `loadPageDateHeaders` (one `pages` read per delivery; only the derived day leaves it) and `evidenceDateHeaderEnabled` (`search.evidence_date_header`, default off). Tests: `test/evidence-date-header.test.ts`.
- `src/core/search/evidence-tail-trim.ts` — the protected tail trim (`search.evidence_tail_trim`, default off, GBRA-156). `resolveEvidencePlan` sets `EvidencePlan.tailTrim` only for `unit: auto` with the key on; `deliverEvidence` calls `trimEvidenceTail(results, opts.query)` on the rank-order delivered rows after redaction and before the token totals, and only when `DeliverOptions.query` is set (`search`/`query` pass `p.query`; the library `assembleEvidenceForHits` passes `input.query` and frozen hits may carry `score`; the `assemble_evidence` op passes neither, so the trim never runs there). Rows past `TAIL_TRIM_PROTECTED` = 3 that are whole-conversation blocks (`delivered.reason` `conversation_type`/`conversation_slug`) and score under `TAIL_TRIM_SCORE_FRACTION` = 0.5 of rank one are dropped, unless their `chunk_text` holds at least `TAIL_TRIM_TERM_FRACTION` = 0.8 of rank one's share of the query's terms (`queryTerms`: letter/digit runs, stopwords and single characters dropped, cut to six characters). Rank one's score must be a positive finite number and every row's finite, or nothing changes. `delivery.tail_trim` is `{ dropped, kept_by_query_terms }`; `dropped_reasons.tail_trim` counts drops. Pinned by `test/evidence-tail-trim.test.ts` (the knowledge-update regression case).
-- `src/core/search/transcript-quote.ts` — quotes conversation rows in `search`, `query` and `assemble_evidence` results (#7600). The signal is the stored page type: a row whose `type` is `conversation` (what `transcripts/render.ts` writes) and that is not a fact row; nothing reads the text to decide, so other pages come back byte-for-byte unchanged. `searchOutput` (`ops/search.ts`) runs `neutralizeTranscriptRows` before the snippet and evidence caps and `fenceTranscriptRows` after them, on copies, after ranking and evidence delivery; `assembleEvidenceForHits` (`evidence-delivery.ts`, so the `assemble_evidence` op and gbrain-evals) does the same around `capEvidenceToBudget`. That cap counts a conversation row as delivered, inside its fence (`deliveredQuoteText`, its `quote` flag; deliverEvidence's own cap, which recall uses, counts the text as returned), so an explicit `auto` token budget still holds, `snippet_chars` included (a rank-one row is kept even when its fence alone exceeds a very small budget); a row it cuts is shortened until the fenced text fits, and other rows are cut exactly as before. The neutralizer replaces each tool-call block with `[tool call: <name>, arguments omitted]` (`[tool calls: a, b, …]` for several): an object typed `tool_use`, `server_tool_use`, `mcp_tool_use`, `function_call`, `custom_tool_call`, `tool_call` or `toolCall` (the enclosing object, found by walking back over candidate `{` with a string-aware scan), the value of a `tool_calls` array or `function_call` object (the message object and its `content` stay), and an XML-style `function_calls`, `invoke` or `tool_use` element with its `parameter` children, namespace prefix optional (the name from `invoke name="…"` or a `<name>` child; result elements are never matched, and a tag counts as markup only when it ends its line or the text, runs into the next tag, or is an `invoke` with a name; blocks separated only by whitespace merge). The XML pass runs before the JSON pass. Text between evidence omission lines is read separately; a block cut by the end of the text is replaced to the end or the next message anchor line (an open-ended block never swallows a later message), one whose `{` lies before the text from the text start (or the last message anchor) when the type is followed by `,` or `}`, and prose that only names the shape is left alone. An XML block whose close lies past the text runs to the end or the next message anchor line; a closing tag with no opener (or a `parameter` opener outside a call) starts at the text start or the last message anchor. A trailing evidence cut marker and a leading `[observed …]` date header stay outside any block. Scanning is linear: anchors are indexed once, and past a per-text work budget the rest of a message is summarized (fails closed). Names outside `[A-Za-z0-9_.:/-]{1,64}` are dropped. The fence is a marker line naming the slug (`[`, `]`, `;` and line breaks shown as `?`), then a code fence of at least three backticks, one longer than the longest run in the text; the row gets `quoted: "transcript"` (kept in lean rows). `rowPreview` is the CLI human line: `[quoted transcript]` and the start of the fenced text. Stored pages, chunks, ranking and scores are unchanged; delivered `match_spans` are remapped to the returned text, and `tokens`, `tokens_delivered` and `budget_used` count the fenced text (`fenceTranscriptRows` with a recount). Tests: `test/search/transcript-quote.test.ts`, `test/search-transcript-quote.test.ts` (ChatGPT export through the importer, with a JSON and an XML call; remote MCP `search`/`query`/`assemble_evidence` and CLI human/`--json`, both engines), `test/search-transcript-quote-budget.test.ts` (budget, spans and snippet cap through the ops).
+- `src/core/search/transcript-quote.ts` — quotes conversation rows in `search`, `query` and `assemble_evidence` results (#7600). The signal is the stored page type: a row whose `type` is `conversation` (what `transcripts/render.ts` writes) and that is not a fact row; nothing reads the text to decide, so other pages come back byte-for-byte unchanged. `searchOutput` (`ops/search.ts`) runs `neutralizeTranscriptRows` before the snippet and evidence caps and `fenceTranscriptRows` after them, on copies, after ranking and evidence delivery; `assembleEvidenceForHits` (`evidence-delivery.ts`, so the `assemble_evidence` op and gbrain-evals) does the same around `capEvidenceToBudget`. That cap counts a conversation row as delivered, inside its fence (`deliveredQuoteText`, its `quote` flag; deliverEvidence's own cap, which recall uses, counts the text as returned), so an explicit `auto` token budget still holds, `snippet_chars` included (a rank-one row is kept even when its fence alone exceeds a very small budget); a row it cuts is shortened until the fenced text fits, and other rows are cut exactly as before. The neutralizer replaces each tool-call block with `[tool call: <name>, arguments omitted]` (`[tool calls: a, b, …]` for several): an object typed `tool_use`, `server_tool_use`, `mcp_tool_use`, `function_call`, `custom_tool_call`, `tool_call` or `toolCall` (the enclosing object, found by walking back over candidate `{` with a string-aware scan), the value of a `tool_calls` array or a `function_call` or Gemini `functionCall` object (the message object and its `content` or `parts` stay), and an XML-style `function_calls`, `invoke`, `tool_use` or `tool_call` element with its `parameter` children, namespace prefix optional (the name from `invoke name="…"`, a `<name>` child, or the root `name` of the JSON object a `tool_call` opens with; result elements are never matched, and a tag counts as markup only when it ends its line (LF or CRLF) or the text, runs into the next tag, is an `invoke` with a name, or is a `tool_call` followed by `{`; blocks separated only by whitespace merge). The XML pass runs before the JSON pass. Text between evidence omission lines is read separately; a block cut by the end of the text is replaced to the end or the next message anchor line (an open-ended block never swallows a later message), one whose `{` lies before the text from the text start (or the last message anchor) when the type is followed by `,` or `}`, and prose that only names the shape is left alone. An XML block whose close lies past the text runs to the end or the next message anchor line; a closing tag with no opener (or a `parameter` opener outside a call) starts at the text start or the last message anchor. A trailing evidence cut marker and a leading `[observed …]` date header stay outside any block. Message anchor lines may end in CRLF. Scanning is linear: anchors are indexed once, and past a per-text work budget the rest of a message is summarized (fails closed). Names outside `[A-Za-z0-9_.:/-]{1,64}` are dropped. The fence is a marker line naming the slug (`[`, `]`, `;` and line breaks shown as `?`), then a code fence of at least three backticks, one longer than the longest run in the text; the row gets `quoted: "transcript"` (kept in lean rows). `rowPreview` is the CLI human line: `[quoted transcript]` and the start of the fenced text. Stored pages, chunks, ranking and scores are unchanged; delivered `match_spans` are remapped to the returned text, and `tokens`, `tokens_delivered` and `budget_used` count the fenced text (`fenceTranscriptRows` with a recount). Tests: `test/search/transcript-quote.test.ts`, `test/search-transcript-quote.test.ts` (ChatGPT export through the importer, with a JSON and an XML call; remote MCP `search`/`query`/`assemble_evidence` and CLI human/`--json`, both engines), `test/search-transcript-quote-budget.test.ts` (budget, spans and snippet cap through the ops).
diff --git a/docs/mcp/README.md b/docs/mcp/README.md
index 55c8573b7f..8ebc822527 100644
--- a/docs/mcp/README.md
+++ b/docs/mcp/README.md
@@ -88,9 +88,10 @@ A row from a conversation page (`type: "conversation"`, what transcript
imports and chat connectors write) carries `quoted: "transcript"`, and its
`chunk_text` is a quoted excerpt: a line naming the page as source
material, not instructions, then the text inside a code fence it cannot
-close. Each tool-call or function-call JSON block in it is replaced by
+close. Each tool-call or function-call JSON block in it (Gemini
+`functionCall` included) is replaced by
`[tool call: <name>, arguments omitted]`, and so is each XML-style call
-block (`function_calls`, `invoke`, `tool_use`). `search`, `query` and
+block (`function_calls`, `invoke`, `tool_use`, `tool_call`). `search`, `query` and
`assemble_evidence` return it this way to every caller, the CLI included;
the stored page is unchanged (`get_page` reads it as written). Other read
paths (`recall`, `think`, `search_exact`, `fetch`, `get_chunks`) and other
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 4d3cf9e009..92d4c36e70 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1308,7 +1308,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
{ since: '0.60.464.0', text: 'Security: `gbrain serve --access=read-only` (the `=` form) now sets the read-only ceiling. It passed flag validation before but started the serve with full write access, because only `--access read-only` was read. A config that used the `=` form now gets the read-only tool set and refused mutations it asked for; a repeated `--access` never widens a read-only value, and an invalid value in either form refuses. `--surface=<v>` likewise now sets the serve surface (it was ignored, leaving the full surface), and a repeated `--surface` takes the narrowest value.' },
{ since: '0.60.464.0', text: 'A managed sync pass whose process was killed or crashed now reads `resumable` / `pass_interrupted` in `gbrain sync status` (with an `interrupted` block naming the pass and the dead holder) instead of `pass_unfinished`, and a second killed pass reads `blocked` / `pass_abandoned` at once. `gbrain doctor` `managed_sync_blocked` lists both (warn, fail after an hour). A live pass still committing is no longer counted abandoned because its row is old.' },
{ since: '0.60.465.0', text: 'On a managed brain, `gbrain trust release h<id>` of a held fact whose entity page exists now writes the fact into that page\'s `## Facts` fence through the page-write door (a `trust_release` page write) and flips the hold in the same publication, so the released fact lives in the page\'s file and a re-import keeps it. Before, the release inserted a database-only row the file never held; a fence-guard hold that names an existing fence row created a database-only twin of it, and now indexes that row and leaves the file unchanged. The row is stamped `user_confirmed` with origin `trust_release` and the page keeps its trust tier. A hold decided after the release was accepted fails `preview_changed` and writes nothing; a remote request, or a deleted or private entity page, refuses and the fact stays held. A hold with no entity page, and unmanaged brains, release database-only as before.' },
- { since: '0.60.466.0', id: 'search_quotes_transcript_rows', text: '`search`, `query` and `assemble_evidence` now return a row from a conversation page (type `conversation`, what transcript imports write) with `quoted: "transcript"` and its text wrapped as a quoted excerpt: a line naming the page as source material, then a code fence. Tool-call and function-call JSON inside, and XML-style `function_calls`, `invoke` and `tool_use` blocks, become `[tool call: <name>, arguments omitted]`. Under `return_unit: auto` an explicit `token_budget` pays for the fence, so a conversation-heavy result can return fewer or shorter rows. Stored pages, ranking, scores and other page types are unchanged; read `quoted` to tell these rows apart, and `get_page` for the text as stored.' },
+ { since: '0.60.466.0', id: 'search_quotes_transcript_rows', text: '`search`, `query` and `assemble_evidence` now return a row from a conversation page (type `conversation`, what transcript imports write) with `quoted: "transcript"` and its text wrapped as a quoted excerpt: a line naming the page as source material, then a code fence. Tool-call and function-call JSON inside (Gemini `functionCall` included), and XML-style `function_calls`, `invoke`, `tool_use` and `tool_call` blocks, become `[tool call: <name>, arguments omitted]`. Under `return_unit: auto` an explicit `token_budget` pays for the fence, so a conversation-heavy result can return fewer or shorter rows. Stored pages, ranking, scores and other page types are unchanged; read `quoted` to tell these rows apart, and `get_page` for the text as stored.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.ts
index 5de105e208..26e93f78ed 100644
--- a/src/core/search/transcript-quote.ts
+++ b/src/core/search/transcript-quote.ts
@@ -10,13 +10,13 @@
* `[tool call: <name>, arguments omitted]`: an object typed tool_use,
* server_tool_use, mcp_tool_use, function_call, custom_tool_call,
* tool_call or toolCall, the value of a `tool_calls` array or a
- * `function_call` object, and an XML-style function_calls, invoke or
- * tool_use element (namespace prefix optional) with its parameter
- * children. Results (function_results, tool_result) and prose that names
+ * `function_call` or Gemini `functionCall` object, and an XML-style
+ * function_calls, invoke, tool_use or tool_call element (namespace
+ * prefix optional) with its parameter children. Results (function_results, tool_result) and prose that names
* a tag stay. The rest of the text is kept. A block cut by
* the start or end of the text (a chunk boundary) or by an evidence
* omission line is replaced up to that cut; an open-ended block never
- * runs past the next message's anchor line, and a leading evidence date
+ * runs past the next message's anchor line (LF or CRLF), and a leading evidence date
* header is kept. Work is linear in the text: past a scan budget the
* rest of a message is summarized (fails closed).
* - `fenceTranscriptText` wraps the text as quoted source material: a
@@ -42,14 +42,15 @@ const ANCHOR_RE = ANCHOR.regex;
const ANCHOR_TEXT = ANCHOR.captures.text_group;
/** The page date header evidence delivery may put first (evidence-date.ts pageDateHeader). */
const DATE_HEADER_RE = /^\[observed [^\]\n]*\]\n/;
-const TRIGGER_RE = /"type"\s*:\s*"(?:tool_use|server_tool_use|mcp_tool_use|function_call|custom_tool_call|tool_call|toolCall)"|"(?:tool_calls|function_call)"\s*:\s*([[{])/g;
+const TRIGGER_RE = /"type"\s*:\s*"(?:tool_use|server_tool_use|mcp_tool_use|function_call|custom_tool_call|tool_call|toolCall)"|"(?:tool_calls|function_call|functionCall)"\s*:\s*([[{])/g;
const NAME_RE = /^[A-Za-z0-9_.:/-]{1,64}$/;
/** The optional namespace prefix of XML-style call tags. */
const XML_PREFIX = '(?:' + ['ant', 'ml:'].join('') + ')?';
-const XML_TAG_RE = new RegExp(`<(/?)${XML_PREFIX}(function_calls|invoke|tool_use|parameter)\\b([^<>]*)>`, 'g');
+const XML_TAG_RE = new RegExp(`<(/?)${XML_PREFIX}(function_calls|invoke|tool_use|tool_call|parameter)\\b([^<>]*)>`, 'g');
+const XML_TOOL_CALL_JSON_RE = new RegExp(`<${XML_PREFIX}tool_call\\b[^<>]*>\\s*\\{`, 'g');
const XML_INVOKE_NAME_RE = new RegExp(`<${XML_PREFIX}invoke\\b[^<>]*?\\bname\\s*=\\s*"([^"]*)"`, 'g');
/** A call tag that is markup, not a mention in a sentence: it ends its line, the text, or runs into the next tag. */
-const ENDS_LINE_RE = /^[ \t]*(?:\n|$|<)/;
+const ENDS_LINE_RE = /^[ \t\r]*(?:\n|$|<)/;
const CUT_MARKER = OMISSION.trimEnd();
const MAX_CANDIDATES = 64;
/** A segment's scan budget, in characters walked: this many per character plus a floor. Once spent, the rest of the message at each later trigger is summarized (fails closed). */
@@ -72,12 +73,13 @@ function anchorsOf(segment: string): Anchors {
const a: Anchors = { lines: [], texts: [], ends: [] };
let lineStart = 0;
for (const line of segment.split('\n')) {
- const m = ANCHOR_RE.exec(line);
+ const text = line.endsWith('\r') ? line.slice(0, -1) : line;
+ const m = ANCHOR_RE.exec(text);
if (m) {
let end = lineStart;
while (end > (a.lines.at(-1) ?? 0) && /\s/.test(segment[end - 1]!)) end--;
a.lines.push(lineStart);
- a.texts.push(lineStart + line.length - (m[ANCHOR_TEXT] ?? '').length);
+ a.texts.push(lineStart + text.length - (m[ANCHOR_TEXT] ?? '').length);
a.ends.push(end);
}
lineStart += line.length + 1;
@@ -210,7 +212,12 @@ function jsonEdits(segment: string): Edit[] {
function xmlNames(block: string): string[] {
const invoked = [...block.matchAll(XML_INVOKE_NAME_RE)].map(m => m[1]!);
- const names = invoked.length > 0 ? invoked : [...block.matchAll(/<name>([^<]*)<\/name>/g)].map(m => m[1]!);
+ const children = invoked.length > 0 ? invoked : [...block.matchAll(/<name>([^<]*)<\/name>/g)].map(m => m[1]!);
+ const openers = children.length > 0 ? [] : [...block.matchAll(XML_TOOL_CALL_JSON_RE)];
+ const names = children.length > 0 ? children : openers.flatMap((m, i) => {
+ const brace = m.index + m[0].length - 1;
+ return scanJson(block, brace, { limit: openers[i + 1]?.index ?? block.length }).names;
+ });
return [...new Set(names.filter(n => NAME_RE.test(n)))];
}
@@ -250,7 +257,7 @@ function xmlEdits(segment: string): Edit[] {
if (stack.length === 0) add(blockStart, after);
continue;
}
- const markup = ENDS_LINE_RE.test(segment.slice(after, after + 64));
+ const markup = ENDS_LINE_RE.test(segment.slice(after, after + 64)) || (name === 'tool_call' && /^\s*\{/.test(segment.slice(after, after + 64)));
const named = /\bname\s*=/.test(attrs);
if (slash) { if (markup) add(floor(m.index), after); continue; }
if (name === 'parameter') { if (named) { blockStart = floor(m.index); blockEnd = bound(segment, a, m.index); reset(name); } continue; }
diff --git a/test/search/transcript-quote.test.ts b/test/search/transcript-quote.test.ts
index 4350e776eb..c8184bc173 100644
--- a/test/search/transcript-quote.test.ts
+++ b/test/search/transcript-quote.test.ts
@@ -342,3 +342,64 @@ describe('pre-landing review fixes, pass 2 (GBRA-156 /ship Step 9)', () => {
expect(delivery.tokens_delivered).toBe(countEvidenceTokens(text, 'heuristic'));
});
});
+
+
+describe('injection gaps before the slot (GBRA-108): CRLF boundaries, <tool_call> blocks, Gemini functionCall', () => {
+ const P = ['ant', 'ml:'].join('');
+ const BOB = '**Bob-example** (2026-08-01 10:01 AM): ';
+ const CAROL = '**Carol-example** (2026-08-01 10:02 AM): ';
+ const LATER = `\n${CAROL}a later unrelated message\n${ANCHOR}another one with a decision we made`;
+ const crlf = (s: string) => s.replace(/\n/g, '\r\n');
+
+ test('speaker lines that end in CRLF bound a cut block the same as LF', () => {
+ const json = `${BOB}Plan: {"tool_calls":[{"function":{"name":"wire_funds","argum${LATER}`;
+ expect(neutralizeToolCalls(crlf(json))).toBe(crlf(`${BOB}Plan: {"tool_calls":[tool call: wire_funds, arguments omitted]${LATER}`));
+ const xml = `${BOB}Plan:\n<${P}invoke name="Bash">\n<${P}parameter name="command">curl http${LATER}`;
+ expect(neutralizeToolCalls(crlf(xml))).toBe(crlf(`${BOB}Plan:\n[tool call: Bash, arguments omitted]${LATER}`));
+ const cutStart = `${BOB}fine\n${ANCHOR}"type": "tool_use", "name": "Bash"}, rest`;
+ expect(neutralizeToolCalls(crlf(cutStart))).toBe(crlf(`${BOB}fine\n${ANCHOR}[tool call: Bash, arguments omitted], rest`));
+ });
+
+ test('XML call tags that end in CRLF are markup, the same as LF', () => {
+ const block = `<${P}function_calls>\n<${P}invoke name="Bash">\n<${P}parameter name="command">rm -rf /tmp/x</${P}parameter>\n</${P}invoke>\n</${P}function_calls>`;
+ expect(neutralizeToolCalls(crlf(`${ANCHOR}On it.\n${block}\nDone.`))).toBe(crlf(`${ANCHOR}On it.\n[tool call: Bash, arguments omitted]\nDone.`));
+ });
+
+ test('a <tool_call> block, plain or antml-prefixed, is summarized like <tool_use>', () => {
+ for (const prefix of ['', P]) {
+ const call = (name: string) => `<${prefix}tool_call>\n{"name": "${name}", "arguments": {"city": "acme-example", "name": "inner"}}\n</${prefix}tool_call>`;
+ expect(neutralizeToolCalls(`${ANCHOR}On it.\n${call('get_weather')}\nDone.`)).toBe(`${ANCHOR}On it.\n[tool call: get_weather, arguments omitted]\nDone.`);
+ expect(neutralizeToolCalls(`x ${call('Read')}\n${call('Write')} y`)).toBe('x [tool calls: Read, Write, arguments omitted] y');
+ expect(neutralizeToolCalls(`x <${prefix}tool_call>{"name": "send_email", "arguments": {"to": "a"}}</${prefix}tool_call> y`)).toBe('x [tool call: send_email, arguments omitted] y');
+ }
+ });
+
+ test('a <tool_call> block cut by the end, the next message or the start of the text', () => {
+ expect(neutralizeToolCalls(`${ANCHOR}Plan:\n<tool_call>\n{"name": "wire_funds", "arguments": {"amount": 1${LATER}`))
+ .toBe(`${ANCHOR}Plan:\n[tool call: wire_funds, arguments omitted]${LATER}`);
+ expect(neutralizeToolCalls(`Plan: <${P}tool_call>{"name": "wire_funds", "argu`)).toBe('Plan: [tool call: wire_funds, arguments omitted]');
+ expect(neutralizeToolCalls('ount": 1}}\n</tool_call>\nand then it stopped.')).toBe('[tool call, arguments omitted]\nand then it stopped.');
+ });
+
+ test('<tool_call> results and prose are left alone', () => {
+ for (const text of [
+ '<tool_response>\n{"name": "get_weather", "content": "sunny"}\n</tool_response>',
+ 'wrap the call in <tool_call> tags when you call a function',
+ '<tool_call_id>c1</tool_call_id>',
+ ]) expect(neutralizeToolCalls(text)).toBe(text);
+ });
+
+ test('a Gemini functionCall part, and a function_call part of the same shape, become a summary', () => {
+ expect(neutralizeToolCalls('{"role":"model","parts":[{"text":"Checking."},{"functionCall":{"name":"get_weather","args":{"city":"acme-example","name":"inner"}}}]}'))
+ .toBe('{"role":"model","parts":[{"text":"Checking."},{"functionCall":[tool call: get_weather, arguments omitted]}]}');
+ expect(neutralizeToolCalls('{"parts":[{"function_call":{"name":"lookup","args":{"q":"x"}}}]}')).toBe('{"parts":[{"function_call":[tool call: lookup, arguments omitted]}]}');
+ });
+
+ test('a Gemini functionCall cut by the end or the next message is replaced to the cut; functionResponse stays', () => {
+ expect(neutralizeToolCalls(`${BOB}Plan: {"parts":[{"functionCall":{"name":"wire_funds","args":{"amount":${LATER}`))
+ .toBe(`${BOB}Plan: {"parts":[{"functionCall":[tool call: wire_funds, arguments omitted]${LATER}`);
+ expect(neutralizeToolCalls('x {"functionCall": {"name": "delete_file", "args": {"path": "/tmp/a')).toBe('x {"functionCall": [tool call: delete_file, arguments omitted]');
+ const response = '{"parts":[{"functionResponse":{"name":"get_weather","response":{"temp":20}}}]}';
+ expect(neutralizeToolCalls(response)).toBe(response);
+ });
+});