Local-first 🧠 memory for Hermes Agent that lives in your Obsidian vault and remembers project context. Nightly 😴 dream passes turn repeat corrections into confirmed preferences with measurable confidence. Adapters ship for Claude Code, Codex, and OpenClaw, with an MCP server for anything else.
Distilled provenance - quotes that are checked, sources that say how much was captured
Status: draft (phase 1 spec review applied)
Author: release orchestrator (via feature-release-playbook)
Audience: implementation
Base: origin/main 3ec2334a (v1.66.0), branch feat/distilled-provenance
Problem statement
Open Second Brain presents a distillation page as a faithful record of its source, but distillSource validates claims structurally only (src/core/brain/distill/distill-source.ts:10-15, validate() at :141-155): a paraphrase wrapped in quotation marks is written exactly like a verbatim quote, and nothing compares it with the block the claim cites. Separately, no page records how much of its source was actually captured. A page whose only evidence is a URL looks the same as a page backed by a file the vault holds, and once the untrusted marker is removed (entity restore, entities/registry.ts:669-721) or was never written (research reports, research/research.ts:158-210), nothing reports that active knowledge rests on a locator that may rot.
Premise verification (live source, 3ec2334a)
Full evidence: the two reconnaissance reports of this wave. The orchestrator re-opened the anchors this design builds on: distill-source.ts:1-284, intake/source-trust.ts:94-135, 260-385, ingest/content-manifest.ts:81-87, ingest/ingest.ts:90-112, 156-200, research/research.ts:60-215, entities/canonical.ts:75-135, hygiene/types.ts:12-77, hygiene/scan.ts:29-36, hygiene/detectors/freshness.ts, mcp/tool-error-codes.ts:115-366, entities/status-scope.ts:34-76, trust/retrieval-gate.ts:38-104.
Card A (verify quotes on distilled pages verbatim) - confirmed, two framing claims corrected
- Confirmed: no quote-fidelity check exists anywhere in
src/. The kernel runs no model (distill-source.ts:10-11), so the check is deterministic and runs at write time insidedistillSource, the one place both surfaces (MCP and CLI) pass through (:172-174). - Corrected: "the substrate already exists (block ids)". Only the block-id grammar exists (
BLOCK_ID_RE,:61;parseWikilinkRichextracts^id). Nothing insrc/resolves a block id to its text. The block resolver is new code. - Corrected (scope-scan claim):
entities/canonical.tsdoes not find quotes.QUOTE_VARIANT_RE(:79) is a fold over identity keys built from\p{Pi}\p{Pf}plus U+02BC. It misses U+0022 and U+0027 (Po), U+201E and U+201A (Ps), the CJK corner brackets U+300C-U+300F and U+301D/U+301E (Ps/Pe), and»…«runs Pf to Pi. Its footprint is pinned over the whole code-point space (tests/core/brain/entities/quote-variant-fold.test.ts), so it is NOT widened. Span detection gets its own constant over the Unicode binary property\p{Quotation_Mark};foldQuoteVariantsByClass(:127-133) is reused only in the comparison step. - Corrected: the quarantine lane is not a usable answer for a failed span. The retrieval trust gate is off by default (
search_trust_gate_enabled, pinned bytests/cli/distill-trust-lane.test.ts:100-135), so a quarantined fabricated quote is still served by a default search. - Found: the source bytes are read inside
hashFileand discarded (source-trust.ts:369-372,content-manifest.ts:81-87). A second read for the check could verify bytes that differ from the hashed ones, contradicting the module's own claim (distill-source.ts:12-13). One read must feed both.
Card B (capture_scope plus a url-only health warning) - partly false, narrowed
- Confirmed:
capture_scope,full-local,bounded-localandurl-onlyhave zero hits insrc/,tests/anddocs/. - False, as the validator said: "nothing records how much of the source was captured". The trust lane (
untrusted_source,trust/untrusted-provenance.ts:88-90) is already a two-way local versus not-local verdict for a single source,source_content_hashrecords the hashed bytes, and the freshnessmissingsentinel (freshness.ts:31) records absence for handoff pages. The work is the third member (a bounded excerpt) and the warning. - Corrected: the warning condition is not on the ingest happy path. Untrusted ingest and entity intake already land in the quarantine lane. The condition lives in three places that escape it: research reports (
provenance: stated, URL wikilinks, no trust field), released entities (restoredeletes the marker on purpose,registry.ts:712-716) and trusted pages whose local source later vanished (only handoff pages carry the freshness contract). - Corrected anchors:
brain_sources(mcp/brain/query-tools.ts:1006) is a signal dashboard, not a list of ingested sources;vault_health(mcp/tools.ts:519) runs the vault/config doctor, not the Brain doctor, and is the wrong home. The OKF walk iscollectOkfPagesatportability/okf.ts:261, not:244. - Not usable as the base: the validator pointed at
MISSING_SOURCE_HASH. Ingest, distill, research and entity pages do not carry thesource_paths/source_hashescontract (onlyhandoff.ts:165writes it), so the scope is derived from the source identity through the trust classifier, not from that sentinel.
Scope
- A shared capture-scope vocabulary
CAPTURE_SCOPE(full-local,bounded-local,url-only) with a classifier over a source identity, a frontmatter helper that writes nothing forfull-local, and the bounded-excerpt block format (render, read, digest). - A quote-span finder over
\p{Quotation_Mark}with position-based pairing and the between-letters apostrophe exclusion, and a comparison-only normaliser. - A block resolver: block id to block text inside source bytes.
- The quote check inside
distillSource: each quoted span in a claim is compared with the cited block (or, for a claim with no block, the whole source); a failed span is unquoted by default and named in the result;strict_quotes/--strict-quotesrefuses the write with the registered codequote_unverified. - One read of the source feeds both the content digest and the check.
- A
distillSourceexcerptargument that makesbounded-localreachable: for a source with no local bytes, the caller may supply the verbatim excerpt it read; the page stores it in a marked block with its digest, and quotes are checked against it. capture_scopestamped at write time on distillation, ingest summary and research report pages (only when notfull-local), surfaced on each write result.- A default-on hygiene detector
capture-scope: a retrievable page of active knowledge whose every cited source is currentlyurl-onlygets awarningfinding withproposed_action: review. - Docs: new
brain_distill_sourceando2b brain distillreference sections (neither exists today), the detector in the hygiene lists, a capture-scope section.
Out of scope
- Checking pages that
brain_write_sessioncommits intoBrain/distillations(write-binding/index.ts:294). Those bytes never passdistillSource; a doctor or hygiene re-check over existing distillation pages is a later slice. - Re-checking quotes on pages already on disk (drift against changed source bytes). Same later slice.
- Wiring
research/extract.tsextractPage(network egress on a write path).bounded-localis reachable through the explicitexcerptargument only. - A
capture_scopefield onlistIngestedSources/IngestedSource, an OKFproducer_metafield, a "list ingested sources" surface. - The unchecked
rawquote onbrain_session_checkpointsignals (same class of gap, different surface). - Widening
QUOTE_VARIANT_RE, or any change tocanonical.ts. - CHANGELOG, version bump, OpenClaw bundle rebuild (handled by PR preparation and the integrator).
Chosen approach
Consultant variant 2 ("shared provenance module, write-time gate plus lint detector"), narrowed. Kept: one provenance vocabulary shared by both cards, one evidence read, a \p{Quotation_Mark} span scanner separate from QUOTE_VARIANT_RE, a comparison-only normaliser, the block resolver, unquote plus a named finding at write time, capture_scope on new pages, the url-only warning as a hygiene detector. Dropped: the second quote detector over existing and write-session pages (out of scope, item 1), the doctor code (a hygiene finding already reaches brain_status through the hygiene-findings count, operator-snapshot.ts:166), deriving scope from the missing sentinel (premise B.5), and the whole-source fallback when a cited block does not resolve (decision 6). Added: the excerpt argument, so bounded-local has a real producer.
Design decisions
Quote spans
- Delimiters. A quotation mark is any code point with the Unicode binary property
Quotation_Mark(/\p{Quotation_Mark}/u). No language list. A new constant in the span module;canonical.tsis untouched. - Apostrophe exclusion. A mark with a letter (
\p{L}) immediately on both sides is never a delimiter (don’t,l'eau). U+02BC is notQuotation_Markand needs no rule. Ps/Pe quotation marks are directional and never apostrophes. - Pairing by position, not by glyph. An opener is a mark at the start of the text or after whitespace or opening punctuation (
\p{Ps},\p{Pi}, another opener), and followed by a non-space. A closer is a mark followed by the end, whitespace or punctuation, and preceded by a non-space. This pairs"…",“…”,„…“,»…«,«…»,「…」and『…』without knowing which language uses which. Pairing is a stack; only outermost spans are checked, an inner quote is part of its outer span's text. - Unpaired marks are not spans. They are counted (
unpaired) in the result and left on the page untouched. An empty or whitespace-only span is not a quote.
Matching tolerance
- Comparison-only normalisation, applied identically to both sides: NFC; inline Markdown reduced to display text (emphasis and code markers
*,_,`,~~;[text](url)totext;[[target|alias]]toalias,[[target]]totarget); line-leading block markers removed (>, list bullets, ordered-list numbers); the trailing^idremoved;foldQuoteVariantsByClassfor marks inside the span; whitespace runs collapsed to one space; trimmed. Then the span must be an exact substring of the normalised block. Never folded: case, punctuation other than quote width, wording. Evidence:page-lint.ts:39-43refuses normalisation of WRITTEN bytes only; the page bytes are never normalised by this check. - Ellipsis. A span containing
…(U+2026) or a run of three or more.is split there; every non-empty fragment must appear in the block, in order, without overlap. A bracketed insertion ([sic],[the protocol]) is not interpreted and fails as a mismatch. - Comparison scope. A claim with
blockis compared with the resolved block only. If the block does not resolve, the outcome isblock-not-found(duplicate id:block-ambiguous), never a silent fall-back to the whole source: the citation is part of what the page asserts. A claim with noblockis compared with the whole source and the outcome isverified-in-source, distinct fromverified-in-block, so the weaker guarantee never reads as the stronger. Existing fixtures cite^abcagainst sources with no block ids but contain no quotation marks, so no claim in them is checked. - Block resolution. Fenced code is skipped. A line ending in whitespace plus
^idis a paragraph or list-item block (its paragraph, marker removed); a line holding only^idattaches to the block directly above it (table, blockquote, list). Two definitions of one id areblock-ambiguous. - Source text. The bytes from the single read are decoded as strict UTF-8 (
TextDecoderwithfatal: true); a decode failure gives every span of the page the outcomesource-not-text.
Failed-span policy
- Default: unquote and name it. The delimiter pair of a failed span is removed, the words stay, the write lands, and the result names each failure. The page never shows an unverified quote. Dropping the claim was rejected (it loses caller-supplied knowledge unasked); quarantine was rejected (premise A.4). Unquoting is a named, deliberate edit of authored bytes stated in the result and the docs, unlike the silent normalisation
page-lint.tsrefuses. - Opt-in strict refusal.
strict_quotes: true(MCP) /--strict-quotes(CLI) refuses the whole write when any span would be unquoted, withQuoteCheckErrorcarrying the registered wire codequote_unverified. Nothing is written. The message names claim indices and outcomes, never claim text or paths. - Unpaired marks never trigger strict refusal; they are counted only.
Capture scope
- One vocabulary for "no local bytes".
CAPTURE_SCOPE.urlOnly = "url-only"is the single name. The quote check's outcome for a span with no local bytes to check against IS that token (QUOTE_CHECK_OUTCOME.urlOnly = CAPTURE_SCOPE.urlOnly), not a second spelling. - Boundaries.
full-local: the identity is vault-shaped and resolves to a readable file (the trusted verdict ofclassifySourceTrust/classifySourceOrigin).url-only: the identity carries a URI scheme or authority shape, or is vault-shaped with no file behind it (the untrusted verdict), so the lane and the scope cannot disagree.bounded-local: aurl-onlyidentity whose page holds a verbatim excerpt the same writer stored, in the marked excerpt block, with its digest. Never inferred from prose: an ingestsummaryis a paraphrase, not a capture. - Oversized sources stay a refusal. A file above
SOURCE_HASH_MAX_BYTESkeeps throwingSourceTrustError; it never becomesbounded-localby truncation (consultant variant 1's idea, rejected: a refusal turned into a degraded success). - Where
bounded-localcomes from. OnlydistillSource, through the newexcerptargument, accepted only when the source isurl-only(a local source with an excerpt is refused by name: the file is the evidence) and bounded byCAPTURE_EXCERPT_MAX_BYTES. Ingest and research keep two-valued scopes in this release;extractPagestays unwired (out of scope, item 3). - Stamp at write, re-derive on read. Writers stamp
capture_scope(single source) orcapture_scopes(research reports, parallel to the report'ssourcesorder) only when a value is notfull-local, so every full-local page stays byte-identical (pinned bydistill-source-trust.test.ts:278-309). Absence means "full-local or written before this release" and is resolved by re-deriving, never by assuming. The detector re-derives the CURRENT scope from the identity (classifySourceTrust, no byte read), so a full-local page whose file vanished reports asurl-only; a stampedbounded-localcounts as backing only while the page's excerpt block is present and matches its digest. - The warning. A default-on hygiene detector
capture-scope(severitywarning,proposed_action: review) over retrievable pages (classifyRetrievalTrust(meta).quarantined === false) of kindbrain-distillation,brain-source,brain-report, andbrain-entityin the canonical status scope (entityStatusInScope(status, "canonical")). It fires when every source the page cites is currentlyurl-only. Quarantined pages are skipped: the gate already excludes them. A doctor check was rejected (it would need aDOCTOR_EXIT_EXCLUSIONSrow and adds nothing the hygiene count does not already show inbrain_status);brain_healthwas rejected (preference-centric fold). - Source extraction is not re-implemented.
stringArrayFieldandwikilinkTargetmove fromsource-cleanup.tsinto a small shared module with a## Sourceslink reader;SourceCleanupEntryand thedeleteBySourcepayload (pinned bygates/recoverability.test.ts) are untouched.
Result and page shapes
- Distill result. Additive keys, absent when there is nothing to report:
quotes(present only when at least one claim contains a span) andcaptureScope(always present, mirroringtrust). The MCP payload usesquotesandcapture_scope; the CLI--jsonthe same.quotes={checked, verified_in_block, verified_in_source, unquoted, unpaired, findings, total, returned, truncated}, findings only for unquoted spans, each{claim, outcome, span}withclaimthe 0-based index,spancapped atQUOTE_SPAN_PREVIEW_MAX_CHARS, findings capped at the page-lint cap (25). Built from the claim's own text only, never a path. - Distill page.
quotes_verifiedandquotes_unquotedfrontmatter integers only when spans exist;capture_scopeand, forbounded-local,excerpt_hashonly when not full-local; the excerpt in a## Excerptsection as a fenced block with the info stringexcerpt, so its bytes are verbatim and wikilink parsing skips it. A clean page stays byte-identical; the idempotent re-run stays inert. - CLI human line. The existing suffix idiom (
untrustedNote):[quotes verified:V unquoted:U]when spans were checked,[bounded-local]when an excerpt was stored. Exit 1 on a strict refusal, as for any validation failure. - Ingest and research results gain
captureScope/captureScopes, serialised ascapture_scope/capture_scopeson MCP. None of these tools has anoutputSchema, so no contract change. - Wire code.
QuoteCheckErroris registered inCLASSIFICATION(tool-error-codes.ts:340-366) asfixed(QuoteCheckError, TOOL_ERROR_CODE.quoteUnverified).wrapToolErrorsdrops data (mcp/brain/shared.ts:95-110), sodistill-tools.tsmaps this one class itself with{code}in theMCPErrordata;shared.tsis not changed.
File changes
New: src/core/brain/provenance/capture-scope.ts, src/core/brain/distill/quote-spans.ts, src/core/brain/distill/quote-verdict.ts, src/core/brain/distill/block-resolve.ts, src/core/brain/distill/quote-check.ts, src/core/brain/source-links.ts, src/core/brain/hygiene/detectors/capture-scope.ts, plus their tests.
Modified: src/core/brain/intake/source-trust.ts, src/core/brain/ingest/content-manifest.ts, src/mcp/tool-error-codes.ts, src/core/brain/distill/distill-source.ts, src/core/brain/ingest/ingest.ts, src/core/brain/research/research.ts, src/core/brain/source-cleanup.ts, src/core/brain/hygiene/types.ts, src/core/brain/hygiene/scan.ts, src/mcp/brain/distill-tools.ts, src/mcp/brain/ingest-tools.ts, src/mcp/brain/research-tools.ts, src/mcp/brain/hygiene-tools.ts, src/mcp/registry-guard.ts, src/cli/brain/verbs/distill.ts, src/cli/command-manifest.ts, docs/mcp.md, docs/cli-reference.md, docs/how-it-works.md, and the tests named in plan.md. Exact ownership is in plan.md.
Not touched: src/core/brain/entities/canonical.ts, src/mcp/brain/shared.ts, openclaw/index.js (nothing distill- or hygiene-related is bundled; the integrator verifies with bun run build:openclaw), plugins/codex/ (no skill text changes).
Risks and open questions
- MCP description budget.
brain_distill_sourceis at 280 of 300 characters. The newstrict_quotesandexcerptproperties carry their own descriptions (cap 160); the tool description gains at most one short clause. If the clause does not fit, the property descriptions carry the behaviour and the docs section carries the rest. - Unquoting changes caller bytes. A caller re-running the same claims gets the same unquoted page (idempotent), but a caller comparing its input with the page sees a difference. The result names every change; the docs state it.
- Position pairing ambiguity. Text like
5" screenorrock 'n' rollcan produce an unpaired mark or a short span. An unpaired mark is only counted; a short span that fails is unquoted, which is the honest outcome for an unverifiable quotation. - Detector cost. One
statper cited source per page on every default hygiene sweep. Bounded by the page kinds above; no byte reads. - Census coupling. The new four-piece vocabularies (
CAPTURE_SCOPE,QUOTE_CHECK_OUTCOME) must be registered inverdict-vocabulary-census.test.tsin the same task that adds them, or the census is red for the other lanes.