Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix: pre-landing review fixes (linear unquote scan budget, ontology_conflicts quoted, agent tilde block kept)

garrytan80eba81db146
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 781328a934..90c7018227 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -14,13 +14,13 @@ identifiers and attribution are available in the pre-removal Git revision
**A remote `put_page` of a page an agent read whole now keeps its frontmatter and the code blocks it added, and timeline sources and ontology values are quoted below "your notes" like the rest of their rows.**
-Before, an agent that read a page with `include_content`, added a fenced code block, or had a newline prepended and the closing fence replaced, could store the page with the read's fence still in the body (its frontmatter inside it) or with its own code block's opener stripped. A timeline entry's free-text `source` and `volunteer_chronicle`'s and `ontology_get`'s ontology values reached remote readers raw. Now the write keeps the page as the agent meant it, those fields are quoted by tier, and text copied back from them is stored without the fence. A remote read quoted only because the page's origin is unverified names the command that clears it.
+Before, an agent that read a page with `include_content`, added a fenced code block, or had a newline prepended and the closing fence replaced, could store the page with the read's fence still in the body (its frontmatter inside it) or with its own code block's opener stripped. A timeline entry's free-text `source` and the ontology values `volunteer_chronicle`, `ontology_get` and `ontology_conflicts` return reached remote readers raw. Now the write keeps the page as the agent meant it, those fields are quoted by tier, and text copied back from them is stored without the fence. A remote read quoted only because the page's origin is unverified names the command that clears it.
### Itemized changes
-- Remote `put_page` finds a whole-markdown read's closing fence as the first line that is exactly that fence outside a code block the content opens itself, so a code block added inside, in place of or after the read is stored whole; a read with blank lines before its marker and its closing line replaced keeps its frontmatter. A fence naming another page, or one after other text, is stored as written.
+- Remote `put_page` finds a whole-markdown read's closing fence as the first line that is exactly that fence outside a code block the content opens itself, so a code block added inside, in place of or after the read is stored whole; a read with blank lines before its marker and its closing line replaced keeps its frontmatter. A fence naming another page, or one after other text, is stored as written. Content holding many markers that never close is kept as written in linear time (the unquote shares one scan budget per text).
- `get_timeline`, the chronicle reads and `volunteer_chronicle` quote a timeline row's `source` with its summary and detail; remote `add_timeline_entry` stores a copied source without the fence.
-- `volunteer_chronicle`'s `ontologies` and `ontology_get` quote a value by its fact's stored tier for remote readers (`quoted: "transcript"`); remote `ontology_propose` stores a copied value without the fence.
+- `volunteer_chronicle`'s `ontologies`, `ontology_get` and `ontology_conflicts` quote a value by its fact's stored tier for remote readers (`quoted: "transcript"`); remote `ontology_propose` stores a copied value without the fence.
- A remote `get_page` or `fetch` of a page quoted because its trust tier is `unknown` carries a `quoted_unverified_origin` info notice, once per session, naming `gbrain trust claim-sources` (preview with `--dry-run --json`) and `gbrain trust backfill`. Quoting is unchanged.
- Tests: `test/search-transcript-quote-8384.test.ts` (PGLite and Postgres), red on master for each change.
diff --git a/docs/architecture/key-files/core-search-evidence.md b/docs/architecture/key-files/core-search-evidence.md
index 9204787fc0..0e69beeb38 100644
--- a/docs/architecture/key-files/core-search-evidence.md
+++ b/docs/architecture/key-files/core-search-evidence.md
@@ -5,6 +5,6 @@ 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) `evidenceDateHeaderEnabled` (`search.evidence_date_header`, default off) and `evidenceTitlesEnabled` (`search.evidence_titles`, GBRA-205, default off: with the header on, `blockDateHeaders` ends a page block's line with its title via `titledPageHeader`, fact headers unchanged; think's `<page>` tag gets a `title` attribute). Tests: `test/evidence-date-header.test.ts`, `test/search-8k-conversion.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 third-party text on every read that hands it to a model (#7600, #8042). The signal is stored metadata, in one place: `isQuotedPageType` (every concrete type in `facts/conversation-types.ts` `ALLOWED_TYPE_ALIASES` plus `transcript` and `chat`) for page rows, and for fact rows `isQuotedFactSource` (`facts.source` starting `cli:extract-conversation-facts`, the prefix in `facts/audit-sources.ts`) or `factsFromQuotedPages` (one bounded read, same source: the fact's `source_markdown_slug` page, a `derivation_inputs` page edge the backstop and extract_facts already write, or for a `sync:import` / `mcp:put_page` fact the page its `source_session` names (`sync:<slug>`, `put_page:<slug>`, covering facts saved before edges or rebuilt from git) has a quoted type; it writes nothing, and a lookup failure withholds `saved_facts` (stage `saved_facts_withheld`) and remote recall facts (`facts_degraded.reason`) and fails the delta facts arm; the facts arm carries both on `fact_row.source`/`from_quoted_page`, stripped by `factRowOutput`; marker `fact <id>`); nothing reads the text to decide, so other rows come back byte-for-byte unchanged. `quoteLabel` is the row decision. GST-687: for an untrusted (remote, `ctx.remote !== false`) reader the signal also includes the stored trust tier: `quotesTier` (below `OWNER_TIER_FLOOR`, `operator_curated`; a missing tier reads as `unknown`, so it fails closed) and `quotesStored` (page type or tier) quote agent-written, tool-data, external and unverified-origin text. `quoteLabel(row, untrusted)`, `deliveredQuoteText`, `fenceTranscriptRows` and `quoteFacts(engine, facts, untrusted)` take that flag (`quoteFacts` reads each fact's stored tier from `facts`, never from the caller's row); only third-party types get tool calls summarized. `quotePageText` and `pageTypesById` serve reads whose rows name only a page (`get_timeline`, `get_chunks`). Local reads are unchanged. Beyond search: `recall` (remote callers: neutralize + fence its results before packing, `quoteFacts` its facts), think's pages block (`think/gather.ts renderPagesBlockDetailed`: neutralize before the excerpt, fence what it renders, every run), `saved_facts` in search/query meta, remote `delta` facts and `_meta.brain_hot_memory` (`quoteFacts`, which also quotes a representative when a collapsed member qualifies), remote `get_page known_as_of` and `get_versions` bodies and diffs (`ops/known-as-of.ts intervalView`, `ops/admin.ts`), and remote `get_page`/`fetch` (`ops/get-page-projection.ts projectGetPage` `quote`, which `projectFetchText` reuses: compiled_truth, timeline and `content` fenced without the tool-call summary, so text inside stays byte-exact; windows fenced in `get-page-body.ts envelopeText`; `quoted: "transcript"`; `quotesBody(ctx, page, trust)` also quotes a page below the owner's tier, and `neutralizesBody` summarizes tool calls only on a third-party page's reading shapes, never for the thin client, with `include_content` and `view: markdown` keeping the stored bytes inside the fence). Remote `get_timeline`, `get_chunks`, `search_exact`, think's extractive fallback, `context_pack` and `list_write_requests`/`get_write_request` outcome text quote by tier too, and so (#8377) do the `chronicle_*` reads and `volunteer_chronicle`'s timeline (`ops/chronicle.ts remoteTimeline`, each row by its own page; `quoteTimelineRows` fences a row's free-text `source` with summary and detail, #8384), its ontology values and `ontology_get`'s (`remoteOntology`, `quoteFacts` by `fact_id`; remote `ontology_propose` drops a fact fence), `takes_list`/`takes_search` (`ops/takes.ts quoteTakes`), a quoted page's title and frontmatter strings (`quotedPageMeta`; `include_content` and fetch `text` fence the whole serialized markdown), the per-turn push hook's hot facts and the pushed context pack (`untrusted: true`); `context_pack` counts entity facts a failed quote lookup withholds in `suppressed`. Remote `remember` drops a fact fence (`unquoteFactText`) and `add_timeline_entry` a fence naming its page, so a copied-back entry keeps its replay key. Remote `put_page` (`ctx.remote !== false`) runs `unquoteTranscriptText` over `content` (`persistence/page-mutations.ts unquotedPutContent`, after the replay check so the request digest covers what the caller sent; slug lowercased as admission does) when the stored page or the incoming frontmatter has a quoted type or the stored page is below the owner's tier (an owner-tier page keeps a fence naming it only when its stored body already holds one), removing only a marker naming that slug and its own fence, so get → edit → put writes the body and a note's fence-like text is kept (`unclosed`: a fence opening the content, after blank lines at most, whose closing line the edit replaced is removed too, #8377, #8384; `closingLine` takes the first line that is exactly the read's fence outside a code block the text opens itself, so an agent's added block stays whole). Remote `put_page` content holding a tool-call summary that a whole read of the stored conversation page never produces, or fewer of a call's summaries than the page holds (`holdsSummarizedToolCalls`), refuses `invalid_params` and stores nothing; only summaries outside the text's own tool-call blocks count, and a named summary the page never wrote is admitted while every stored call stays (#8377). A thin client paging a quoted page (`cli/page-render.ts spoolWholePage`) unquotes each window before checking size and sha256, then prints the page (and `--json` content) inside one quote fence (`transcriptFence`). Trusted local get_page/fetch/put_page, recall and delta are 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), `test/search-transcript-quote-coverage.test.ts` (#8042 matrix: slack, imessage, email, meeting and conversation pages plus a conversation-extracted fact against a note and a remembered fact, plus backstop facts linked to a synced slack page by a derivation edge or only by their sync session, and one derived from a note, a failed lookup withholding facts, a retried retype put, over remote search, query, saved_facts, assemble_evidence, recall, get_page body/content/window, local get_page, the get→put round trip (a note's fence-like text, a mixed-case slug), thin-client paging of a 12,000-line slack page, fetch, think's prompt, delta, hot memory, and get_page known_as_of / get_versions bodies and diff, both engines), `test/search-transcript-quote-tiers.test.ts` (GST-687: every placement and read surface by tier, both engines) and `test/search-transcript-quote-legacy-claim.test.ts` (a legacy note is quoted, raw again after claim and backfill, both engines), `test/search-transcript-quote-followups.test.ts` (#8377: the chronicle, takes and page title/frontmatter reads, hook-injected facts, and the copied-back writes, red on master, both engines) and `test/trust-keep-maintenance-reimport-8352.test.ts` (#8352: maintenance re-imports keep an owner note's tier, both engines) and `test/search-transcript-quote-8384.test.ts` (#8384: whole-markdown round-trip edges, timeline source, ontology values, the `quoted_unverified_origin` notice from `get-page-projection.ts unverifiedOriginNotice`, both engines).
+- `src/core/search/transcript-quote.ts` — quotes third-party text on every read that hands it to a model (#7600, #8042). The signal is stored metadata, in one place: `isQuotedPageType` (every concrete type in `facts/conversation-types.ts` `ALLOWED_TYPE_ALIASES` plus `transcript` and `chat`) for page rows, and for fact rows `isQuotedFactSource` (`facts.source` starting `cli:extract-conversation-facts`, the prefix in `facts/audit-sources.ts`) or `factsFromQuotedPages` (one bounded read, same source: the fact's `source_markdown_slug` page, a `derivation_inputs` page edge the backstop and extract_facts already write, or for a `sync:import` / `mcp:put_page` fact the page its `source_session` names (`sync:<slug>`, `put_page:<slug>`, covering facts saved before edges or rebuilt from git) has a quoted type; it writes nothing, and a lookup failure withholds `saved_facts` (stage `saved_facts_withheld`) and remote recall facts (`facts_degraded.reason`) and fails the delta facts arm; the facts arm carries both on `fact_row.source`/`from_quoted_page`, stripped by `factRowOutput`; marker `fact <id>`); nothing reads the text to decide, so other rows come back byte-for-byte unchanged. `quoteLabel` is the row decision. GST-687: for an untrusted (remote, `ctx.remote !== false`) reader the signal also includes the stored trust tier: `quotesTier` (below `OWNER_TIER_FLOOR`, `operator_curated`; a missing tier reads as `unknown`, so it fails closed) and `quotesStored` (page type or tier) quote agent-written, tool-data, external and unverified-origin text. `quoteLabel(row, untrusted)`, `deliveredQuoteText`, `fenceTranscriptRows` and `quoteFacts(engine, facts, untrusted)` take that flag (`quoteFacts` reads each fact's stored tier from `facts`, never from the caller's row); only third-party types get tool calls summarized. `quotePageText` and `pageTypesById` serve reads whose rows name only a page (`get_timeline`, `get_chunks`). Local reads are unchanged. Beyond search: `recall` (remote callers: neutralize + fence its results before packing, `quoteFacts` its facts), think's pages block (`think/gather.ts renderPagesBlockDetailed`: neutralize before the excerpt, fence what it renders, every run), `saved_facts` in search/query meta, remote `delta` facts and `_meta.brain_hot_memory` (`quoteFacts`, which also quotes a representative when a collapsed member qualifies), remote `get_page known_as_of` and `get_versions` bodies and diffs (`ops/known-as-of.ts intervalView`, `ops/admin.ts`), and remote `get_page`/`fetch` (`ops/get-page-projection.ts projectGetPage` `quote`, which `projectFetchText` reuses: compiled_truth, timeline and `content` fenced without the tool-call summary, so text inside stays byte-exact; windows fenced in `get-page-body.ts envelopeText`; `quoted: "transcript"`; `quotesBody(ctx, page, trust)` also quotes a page below the owner's tier, and `neutralizesBody` summarizes tool calls only on a third-party page's reading shapes, never for the thin client, with `include_content` and `view: markdown` keeping the stored bytes inside the fence). Remote `get_timeline`, `get_chunks`, `search_exact`, think's extractive fallback, `context_pack` and `list_write_requests`/`get_write_request` outcome text quote by tier too, and so (#8377) do the `chronicle_*` reads and `volunteer_chronicle`'s timeline (`ops/chronicle.ts remoteTimeline`, each row by its own page; `quoteTimelineRows` fences a row's free-text `source` with summary and detail, #8384), its ontology values, `ontology_get`'s and `ontology_conflicts`' (`remoteOntology`, `quoteFacts` by `fact_id`; remote `ontology_propose` drops a fact fence), `takes_list`/`takes_search` (`ops/takes.ts quoteTakes`), a quoted page's title and frontmatter strings (`quotedPageMeta`; `include_content` and fetch `text` fence the whole serialized markdown), the per-turn push hook's hot facts and the pushed context pack (`untrusted: true`); `context_pack` counts entity facts a failed quote lookup withholds in `suppressed`. Remote `remember` drops a fact fence (`unquoteFactText`) and `add_timeline_entry` a fence naming its page, so a copied-back entry keeps its replay key. Remote `put_page` (`ctx.remote !== false`) runs `unquoteTranscriptText` over `content` (`persistence/page-mutations.ts unquotedPutContent`, after the replay check so the request digest covers what the caller sent; slug lowercased as admission does) when the stored page or the incoming frontmatter has a quoted type or the stored page is below the owner's tier (an owner-tier page keeps a fence naming it only when its stored body already holds one), removing only a marker naming that slug and its own fence, so get → edit → put writes the body and a note's fence-like text is kept (`unclosed`: a fence opening the content, after blank lines at most, whose closing line the edit replaced is removed too, #8377, #8384; `closingLine` takes the first line that is exactly the read's fence outside a code block the text opens itself, so an agent's added block stays whole; every search of one text shares a budget of twice its length, past which the rest is kept as written, so many unclosed markers stay linear). Remote `put_page` content holding a tool-call summary that a whole read of the stored conversation page never produces, or fewer of a call's summaries than the page holds (`holdsSummarizedToolCalls`), refuses `invalid_params` and stores nothing; only summaries outside the text's own tool-call blocks count, and a named summary the page never wrote is admitted while every stored call stays (#8377). A thin client paging a quoted page (`cli/page-render.ts spoolWholePage`) unquotes each window before checking size and sha256, then prints the page (and `--json` content) inside one quote fence (`transcriptFence`). Trusted local get_page/fetch/put_page, recall and delta are 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), `test/search-transcript-quote-coverage.test.ts` (#8042 matrix: slack, imessage, email, meeting and conversation pages plus a conversation-extracted fact against a note and a remembered fact, plus backstop facts linked to a synced slack page by a derivation edge or only by their sync session, and one derived from a note, a failed lookup withholding facts, a retried retype put, over remote search, query, saved_facts, assemble_evidence, recall, get_page body/content/window, local get_page, the get→put round trip (a note's fence-like text, a mixed-case slug), thin-client paging of a 12,000-line slack page, fetch, think's prompt, delta, hot memory, and get_page known_as_of / get_versions bodies and diff, both engines), `test/search-transcript-quote-tiers.test.ts` (GST-687: every placement and read surface by tier, both engines) and `test/search-transcript-quote-legacy-claim.test.ts` (a legacy note is quoted, raw again after claim and backfill, both engines), `test/search-transcript-quote-followups.test.ts` (#8377: the chronicle, takes and page title/frontmatter reads, hook-injected facts, and the copied-back writes, red on master, both engines) and `test/trust-keep-maintenance-reimport-8352.test.ts` (#8352: maintenance re-imports keep an owner note's tier, both engines) and `test/search-transcript-quote-8384.test.ts` (#8384: whole-markdown round-trip edges, timeline source, ontology values, the `quoted_unverified_origin` notice from `get-page-projection.ts unverifiedOriginNotice`, both engines).
- `src/core/search/facts-arm.ts` — the `query` facts arm behind `search.query_facts_arm` (default on since gates 1b and 2 passed; `false` turns it off). `matchQueryFacts` reads the candidate pool from `fact-relevance.ts` (active facts under recall's read policy) and matches them by query-embedding cosine (`FACT_COSINE_MIN` 0.6, the embedding `hybridSearch` already computed, via `HybridSearchOpts.onQueryEmbedding`) or by query-term share (half the terms, substring match) or a named entity (in that page's source), newest `valid_from` first, at most `MAX_FACT_ROWS` (3). `applyFactsArm` adds them as `fact_row` rows in spare capacity only: free slots under the caller's row count, and whole rows that fit in what the page rows leave of the token budget; a page row is never displaced `factRowOutput` strips page fields (slug, id, type, chunk_id) from fact rows on output, leaving `result_type: 'fact'`, `fact_id`, `follow_up` (`recall` by entity, or `grep` without one) and `page_slug` when the caller can read the entity page (`readableEntityPages`); and stamps `superseded_claim` on page rows whose typed `claim_metric` (plus `claim_period` when both carry it) a newer active fact covers. No model call. Tests: `test/search-facts-arm.test.ts`. Gates and verdict: `docs/eval/decisions/query-facts-arm/`. Fact reserves: `applyFactReserve` serves two policies. The temporal fact reserve (`applyTemporalFactReserve`, `search.temporal_fact_reserve`, default off): `hasTemporalCue` (the preregistered word, phrase, ISO date and month-name regex), candidates from `collectFactCandidates` (50 cosine, 50 keyword, 20 entity facts, cosine for every candidate), scored by the shared `scoreFactCandidates` and kept by its `reserve` admission rule (fail-open on any error), packed into 15% of the token budget and 30% of the row count (1-20), rendered as fact rows whose text starts with `factDateHeader` (evidence delivery adds no second header), oldest first after the pages. Tests: `test/search-temporal-fact-reserve.test.ts`. Gates: `docs/eval/decisions/temporal-fact-reserve/`. The facts block (`applyFactsBlock`, `search.query_facts_block`, GBRA-205, default on since the held-out gate passed; `false` or a config read error turns it off): the same facts for every budgeted query, up to 22% of the budget and 40 rows, oldest first ahead of the pages, adding rows. Fact source pages (`applyFactSourcePages`, `search.fact_source_pages`, GBRA-205, default off): the conversations (`facts.source_markdown_slug`, never the entity page) of the best-matching admitted facts, at most 2 not already present, read through `getChunkWindows` under the caller's read scope and placed after the top 3 rows; the row count never grows. Tests: `test/search-8k-conversion.test.ts`.
- `src/core/search/remote-page-cap.ts` — GBRA-156: the default distinct-page cap of a remote `search`/`query` that names no `limit` (and no `offset`). `remotePageCap(ctx, p)` reads `search.remote_page_cap` (default `DEFAULT_REMOTE_PAGE_CAP`, `0` = off; null for trusted local callers and `viaSubagent` host tool calls); `capVectorOnlyPages(rows, cap)` keeps every page with lexical evidence (`keyword_hit`, strict or relaxed, `alias_hit`, `exact_lookup`, `relational_pinned`) and admits vector-only pages, in rank order, only while the result holds fewer than `cap` distinct `(source_id, slug)` pages; it reports `{ cap, kept_pages, dropped_pages }` as `_meta.retrieval.page_cap` when it dropped any. `pageCapLine(retrieval, how)` renders that as the model-visible line dispatch's `retrievalNoticeBlocks` appends after the keyword count line (hosts that show only `content[]` never see `_meta`), and the CLI's `formatResult` writes on stderr with `--limit`. Applied to the hybrid output before alias fan-out (search) and to the first pass and an adopted CRAG re-run (query); keyword-only paths carry no vector-only rows and the image branch is uncapped (`test/search-remote-page-cap.test.ts`). Every vector path (the hybrid vector arm, the embed-failure fallback, the `query` image branch) reads the keyword path's source scope (`test/search-vector-scope-parity.test.ts`).
diff --git a/docs/mcp/README.md b/docs/mcp/README.md
index 6eeba1935c..36b3fe7c7e 100644
--- a/docs/mcp/README.md
+++ b/docs/mcp/README.md
@@ -136,7 +136,7 @@ notes" (written by an agent, tool data, external, or unverified origin) comes
back quoted on search, `query`, `recall`, `assemble_evidence`, `get_page`
(title and frontmatter included; `include_content` fences the whole markdown),
`fetch`, `get_timeline`, `chronicle_day`, `chronicle_since`,
-`chronicle_on_this_day`, `volunteer_chronicle` (its ontology values too), `ontology_get`, `takes_list`, `takes_search` (claim and source),
+`chronicle_on_this_day`, `volunteer_chronicle` (its ontology values too), `ontology_get`, `ontology_conflicts`, `takes_list`, `takes_search` (claim and source),
where a timeline row's free-text `source` is quoted with its summary and detail,
`get_chunks`, `search_exact`, `get_versions`, `known_as_of`, `think`'s extractive
answer, `context_pack`, hot memory and write receipts. The context gbrain's
diff --git a/src/core/behavior-change-notice.ts b/src/core/behavior-change-notice.ts
index 221a47c64b..302c87da27 100644
--- a/src/core/behavior-change-notice.ts
+++ b/src/core/behavior-change-notice.ts
@@ -1395,7 +1395,7 @@ export const BEHAVIOR_CHANGES: ReadonlyArray<{ since: string; text: ChangeText;
// #8377 (GBRA-269) quote fence follow-ups. since stamped at the merge slot.
{ since: '0.60.538.0', id: 'remote_quotes_followups_8377', text: 'Agents connected over MCP now also get text below "your notes" quoted on the chronicle reads (chronicle_day, chronicle_since, chronicle_on_this_day, volunteer_chronicle), takes_list and takes_search (claim and source), and a quoted page\'s title and frontmatter (put_page of that read stores them unfenced); get_page include_content and fetch text fence the whole markdown, frontmatter included. The context gbrain\'s hooks inject (the per-turn hook and the pushed context pack) quotes facts below the owner\'s tier too, so hook output can hold quote fences. A thin client\'s paged `gbrain get` of a quoted page prints it inside the fence. remember, add_timeline_entry and the takes writes store text copied out of a quoted read without the fence, and put_page admits a conversation round trip whose stored tool call holds a summary in its arguments. Local reads are unchanged.' },
// #8384 (GBRA-269) quote fence follow-ups 2. since stamped at the merge slot.
- { since: '0.60.538.0', id: 'remote_quotes_followups_8384', text: 'Agents connected over MCP now get a timeline entry\'s free-text source quoted with its summary and detail (get_timeline, the chronicle reads, volunteer_chronicle), and ontology values (volunteer_chronicle, ontology_get) quoted by their fact\'s trust tier; add_timeline_entry and ontology_propose store text copied out of those reads without the fence. A remote put_page of a whole-markdown read keeps a code block the agent added inside, in place of or after the read, and keeps the frontmatter when blank lines were added before the marker. A remote get_page or fetch of a page quoted only because its origin is unverified carries a quoted_unverified_origin notice naming gbrain trust claim-sources and gbrain trust backfill, once per session. Local reads are unchanged.' },
+ { since: '0.60.538.0', id: 'remote_quotes_followups_8384', text: 'Agents connected over MCP now get a timeline entry\'s free-text source quoted with its summary and detail (get_timeline, the chronicle reads, volunteer_chronicle), and ontology values (volunteer_chronicle, ontology_get, ontology_conflicts) quoted by their fact\'s trust tier; add_timeline_entry and ontology_propose store text copied out of those reads without the fence. A remote put_page of a whole-markdown read keeps a code block the agent added inside, in place of or after the read, and keeps the frontmatter when blank lines were added before the marker. A remote get_page or fetch of a page quoted only because its origin is unverified carries a quoted_unverified_origin notice naming gbrain trust claim-sources and gbrain trust backfill, once per session. Local reads are unchanged.' },
];
/** The newest disclosed change's release: the notice id moves only when a release adds rows. */
diff --git a/src/core/ops/chronicle.ts b/src/core/ops/chronicle.ts
index c23cadfb92..d185d4b62b 100644
--- a/src/core/ops/chronicle.ts
+++ b/src/core/ops/chronicle.ts
@@ -35,7 +35,7 @@ function remoteTimeline(ctx: OperationContext, rows: ChronicleTimelineRow[]) {
return quoteTimelineRows(ctx, r => r.page_slug, redactDiaryTimeline(ctx, rows));
}
/** #8384: ontology values a remote reader gets, each quoted by its fact's stored tier as recall quotes the fact (quoteFacts). */
-async function remoteOntology(ctx: OperationContext, values: OntologyValue[]): Promise<Array<OntologyValue & { quoted?: 'transcript' }>> {
+async function remoteOntology<V extends Pick<OntologyValue, 'value' | 'source' | 'fact_id'>>(ctx: OperationContext, values: V[]): Promise<Array<V & { quoted?: 'transcript' }>> {
if (ctx.remote === false || values.length === 0) return values;
const facts = await quoteFacts(ctx.engine, values.map(v => ({ id: v.fact_id, fact: v.value, source: v.source })), true);
return values.map((v, i) => facts[i]!.quoted ? { ...v, value: facts[i]!.fact, quoted: 'transcript' as const } : v);
@@ -279,9 +279,10 @@ const ontology_conflicts: Operation = {
if (ctx.remote === false) return conflicts;
// Remote: redact diary-sourced values; drop conflicts that no longer have
// ≥2 distinct values once diary provenance is removed (no leak via conflicts).
- return conflicts
+ return Promise.all(conflicts
.map((c) => ({ ...c, values: c.values.filter((v) => !(v.source ?? '').startsWith('life/diary/')) }))
- .filter((c) => new Set(c.values.map((v) => v.value)).size >= 2);
+ .filter((c) => new Set(c.values.map((v) => v.value)).size >= 2)
+ .map(async (c) => ({ ...c, values: await remoteOntology(ctx, c.values) })));
},
cliHints: { name: 'ontology-contradictions' },
};
diff --git a/src/core/search/transcript-quote.ts b/src/core/search/transcript-quote.ts
index d05943dfd3..567e1930a9 100644
--- a/src/core/search/transcript-quote.ts
+++ b/src/core/search/transcript-quote.ts
@@ -551,11 +551,13 @@ export function unquoteFactText(text: string): string {
function unquoteMarked(text: string, find: (from: number) => { i: number; length: number } | null, unclosed = false): string {
let out = '';
let at = 0;
+ const work = { left: 2 * text.length + 1 };
for (let found = find(0); found; found = find(at)) {
const { i, length } = found;
const open = /^(`{3,})text\n/.exec(text.slice(i + length));
const start = i + length + (open?.[0].length ?? 0);
- const close = open ? closingLine(text, start, open[1]) : null;
+ const close = open ? closingLine(text, start, open[1], work, true) ?? closingLine(text, start, open[1], work, false) : null;
+ if (close === 'spent') break;
if (!close && unclosed && open && at === 0 && !text.slice(0, i).trim()) return text.slice(start);
if (!close) { out += text.slice(at, i + length); at = i + length; continue; }
out += (unclosed && at === 0 && !text.slice(0, i).trim() ? '' : text.slice(at, i)) + text.slice(start, close.end);
@@ -568,17 +570,21 @@ function unquoteMarked(text: string, find: (from: number) => { i: number; length
* #8384: where the quote fence opened at `start` closes: the first line that is exactly `fence` (trailing blanks allowed,
* or the next quote's marker right after it, as concatenated windows join) outside a code block the text opens itself,
* so an agent's added block (```js … ```) is kept whole. A host's quoted text never holds a backtick run as long as its
- * fence, so a line of exactly `fence` inside a shorter or tilde block (an unclosed one in a cut window too) still closes it.
- * `end` is the newline before that line and `after` the end of the fence and its blanks.
+ * fence, so a line of exactly `fence` inside a shorter backtick block still closes it; a tilde block holds it only while
+ * `tilde` is set, and the caller searches again without it when the text leaves one unclosed (a host's cut window).
+ * `end` is the newline before that line and `after` the end of the fence and its blanks. Every search of one text shares
+ * `work` (twice the text's length): closes found are disjoint, so only fences that never close spend it twice, and past
+ * it ('spent') the rest of the text is kept as written, so hostile content of many unclosed markers stays linear.
*/
-function closingLine(text: string, start: number, fence: string): { end: number; after: number } | null {
+function closingLine(text: string, start: number, fence: string, work: { left: number }, tilde: boolean): { end: number; after: number } | null | 'spent' {
let inner: { char: string; length: number } | null = null;
for (let pos = start; pos <= text.length;) {
const nl = text.indexOf('\n', pos);
const line = text.slice(pos, nl < 0 ? text.length : nl);
+ if ((work.left -= line.length + 1) < 0) return 'spent';
const bare = /^(`{3,}|~{3,})[ \t]*\r?$/.exec(line);
const closes = bare?.[1] === fence || line.startsWith(`${fence}${MARKER_START}`);
- if (pos > start && closes && (!inner || inner.char !== '`' || inner.length < fence.length)) return { end: pos - 1, after: pos + (bare ? line.replace(/\r$/, '').length : fence.length) };
+ if (pos > start && closes && (!inner || (inner.char === '`' ? inner.length < fence.length : !tilde))) return { end: pos - 1, after: pos + (bare ? line.replace(/\r$/, '').length : fence.length) };
const run = /^(`{3,}|~{3,})/.exec(line)?.[1];
if (inner) { if (bare && bare[1][0] === inner.char && bare[1].length >= inner.length) inner = null; }
else if (run && !(run[0] === '`' && line.slice(run.length).includes('`'))) inner = { char: run[0], length: run.length };
diff --git a/test/search-transcript-quote-8384.test.ts b/test/search-transcript-quote-8384.test.ts
index a7bda66b77..1cb7de158d 100644
--- a/test/search-transcript-quote-8384.test.ts
+++ b/test/search-transcript-quote-8384.test.ts
@@ -9,7 +9,7 @@ import { afterAll, beforeAll, describe, expect, test } from 'bun:test';
import type { BrainEngine } from '../src/core/engine.ts';
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
import { __resetBackupNoticeForTests, dispatchToolCall } from '../src/mcp/dispatch.ts';
-import { fenceTranscriptText, unquoteTranscriptText } from '../src/core/search/transcript-quote.ts';
+import { fenceTranscriptText, unquoteFactText, unquoteTranscriptText } from '../src/core/search/transcript-quote.ts';
import { isolatedPersistencePostgres } from './helpers/persistence-postgres.ts';
import { asOwner } from './helpers/owner-tier.ts';
import { requirePostgresTestDatabase, testBackends } from './helpers/test-backends.ts';
@@ -54,6 +54,8 @@ for (const backend of testBackends()) {
await asOwner(engine, tx => tx.putPage(ENTITY_SLUG, { type: 'person', title: 'Alice Example', compiled_truth: 'A person.', timeline: '', frontmatter: {} }));
await ok('ontology_propose', { entity: ENTITY_SLUG, dimension: 'role', value: `advisor ${CODE.value}`, visibility: 'world' });
await asOwner(engine, tx => tx.mergeOntologyFact({ entitySlug: ENTITY_SLUG, dimension: 'location', value: `Lisbon ${CODE.owner}`, source: 'manual', visibility: 'world' } as never));
+ await ok('ontology_propose', { entity: ENTITY_SLUG, dimension: 'expertise', value: `Lead ${CODE.value}`, source: 'notes/agent-8384', valid_from: '2026-05-01', visibility: 'world' });
+ await asOwner(engine, tx => tx.mergeOntologyFact({ entitySlug: ENTITY_SLUG, dimension: 'expertise', value: `Chief ${CODE.owner}`, source: 'notes/owner-8384', validFrom: '2026-01-01', visibility: 'world' } as never));
}, 180_000);
afterAll(async () => { await close?.(); });
@@ -77,7 +79,9 @@ for (const backend of testBackends()) {
test('a code block an agent adds inside, in place of the closing fence, or after the read is stored intact', async () => {
const block = '```js\nconst x = 1;\n```';
+ const tilde = '~~~md\n```\n~~~';
const cases: Array<[string, (c: string) => string, string]> = [
+ ['notes/tilde-inside', c => c.replace('Second line.', `Second line.\n\n${tilde}`), `Second line.\n\n${tilde}`],
['notes/block-inside', c => c.replace('Second line.', `Second line.\n\n${block}`), `Second line.\n\n${block}`],
['notes/block-replaces-closer', c => `${dropCloser(c)}${block}\n`, `Second line.\n\n${block}`],
['notes/block-after', c => `${c}\n\n${block}\n`, `Second line.\n\n${block}`],
@@ -123,6 +127,11 @@ for (const backend of testBackends()) {
expect(value(vals, 'location')).toBe(`Lisbon ${CODE.owner}`);
}
expect(value(await ok('ontology_get', { entity: ENTITY_SLUG }, local), 'role')).toBe(`advisor ${CODE.value}`);
+ const titles = async (opts: Opts) => (await ok('ontology_conflicts', {}, opts) as Array<{ entity_slug: string; dimension: string; values: Array<{ value: string }> }>)
+ .find(c => c.entity_slug === ENTITY_SLUG && c.dimension === 'expertise')?.values.map(v => v.value).sort();
+ const conflict = await titles(remote);
+ expect([conflict?.length, conflict?.[0], conflict?.[1]?.includes(MARKER), conflict?.[1]?.includes(CODE.value)]).toEqual([2, `Chief ${CODE.owner}`, true, true]);
+ expect(await titles(local)).toEqual([`Chief ${CODE.owner}`, `Lead ${CODE.value}`]);
await ok('ontology_propose', { entity: ENTITY_SLUG, dimension: 'employer', value: value(got, 'role'), visibility: 'world' });
expect(value(await ok('ontology_get', { entity: ENTITY_SLUG }, local), 'employer')).toBe(`advisor ${CODE.value}`);
}, 120_000);
@@ -153,4 +162,13 @@ describe('#8384: a host\'s own quoted text unquotes exactly, whatever code fence
.toEqual([text, text, text + text, text]);
}
});
+
+ test('a megabyte of markers that never close is kept as written in linear time', () => {
+ const fence = fenceTranscriptText('x', 'slack/a').split('\n').slice(0, 2).join('\n');
+ const hostile = `intro\n${`${fence}\n`.repeat(Math.ceil(1_048_576 / (fence.length + 1)))}`;
+ const facts = `intro\n${`${fenceTranscriptText('y', 'fact 7').split('\n').slice(0, 2).join('\n')}\n`.repeat(12_000)}`;
+ const t0 = performance.now();
+ expect([unquoteTranscriptText(hostile, 'slack/a', true) === hostile, unquoteFactText(facts) === facts]).toEqual([true, true]);
+ expect(performance.now() - t0).toBeLessThan(1_000);
+ });
});