fix(cognition): word-boundary relevance gate on fact recall (recall precision + fact-yield) - #4309
Merged
rysweet merged 1 commit intoJul 18, 2026
Conversation
…recision + fact-yield)
`LibraryCognitiveMemory::search_facts` — the fact-recall seam the OODA
turn/context path (`base_type_turn::prepare_turn_context`) and every
natural-language fact caller reach — delegated matching to the upstream library,
which matches each query token as a RAW case-insensitive SUBSTRING of a fact's
concept OR content. A clean natural-language token therefore floated facts in on
the INTERIOR/SUFFIX of an unrelated word.
Empirically (live in-memory backend), for content "the reactor overheated":
* search_facts("act") -> matched "re(act)or" and "artif(act)" [interior]
* search_facts("own") -> matched "d(own)load" [interior]
* search_facts("test") -> matched "la(test)" [suffix]
while whole-word ("reactor"), prefix ("react"), and inflectional ("deploy" ->
"deployed") matches were legitimate. Those off-topic facts crowd the CAPPED
working-context recall the OODA cycle feeds to reasoning, dragging fact recall
precision — and effective distillation fact-yield — down. This is the same
defect the EPISODIC recall gate already removed (`recall_episodes_ranked` /
`search_episodes_by_keywords`, PR #4241 lineage); fact recall was never gated.
Fix: apply the analogous marker-safe word-boundary gate to the FACT path,
reusing the existing `shares_word_prefix` helper and mirroring
`search_episodes_by_keywords`'s clean/raw partition:
* a CLEAN query token (all-alphanumeric) is kept only when it is a prefix of a
whole word in the concept OR content (word-boundary), dropping interior/suffix
noise while preserving inflectional recall;
* a RAW token (any non-alphanumeric char — a hyphenated concept like
"bug-pattern", or a "journal:"/"goal-edge:"/"sub:" marker) keeps the library's
exact contiguous-substring semantics its callers store and re-filter on; a
query with NO clean token bypasses the gate entirely, so the many
concept/marker callers are provably unaffected.
Both concept AND content are checked (the library matches both). Truncation to
`limit` is deferred until AFTER the gate so a relevant fact ranked behind an
interior-substring false positive is not dropped before the gate runs (mirroring
`recall_episodes_ranked`). Wildcard/empty queries keep the return-all path.
The gate only ever REMOVES interior/suffix false positives — it never adds a
fact the backend did not return and never reorders. Verified empirically that
the library treats a hyphenated token as a contiguous substring (matching the
raw-token check exactly), so mixed clean+marker queries do not regress.
Tests:
* library_adapter::fact_query_gate_tests — pure helpers (partition_fact_query,
fact_shares_query_relevance): clean/raw partition, interior-vs-word-boundary,
inflection, concept-field match, marker substring, mixed query.
* cognitive_memory::tests_fact_recall_word_boundary — end-to-end over the live
in-memory backend: interior/suffix dropped, word-boundary + inflectional
preserved, concept-field match, marker/concept substring preserved, deferred
truncation honours limit, wildcard/empty bypass.
qa-team: tests/gadugi/fact-recall-word-boundary-precision.yaml drives the
end-to-end + unit tests plus an episodic-gate regression guard.
Docs: docs/reference/cognitive-memory-fact-recall.md (new "Word-boundary
relevance gate" section) and docs/architecture/cognitive-memory-library-adapter.md
(fact-path gate subsection + in-adapter table row).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📊 Coverage Summary
Coverage data from CI run. Test files matching |
rysweet
deleted the
engineer/continuously-research-and-improve-your-own-cogn-70ab8541-1784337090-73f124
branch
July 18, 2026 04:13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
LibraryCognitiveMemory::search_facts— the fact-recall seam the OODA turn/context path (base_type_turn::prepare_turn_context) and every natural-language fact caller reach — delegated matching to the upstream library, which matches each query token as a raw case-insensitive SUBSTRING of a fact'sconceptORcontent. A clean natural-language token therefore floated facts in on the interior/suffix of an unrelated word, polluting the capped working-context recall the OODA cycle feeds to reasoning and dragging fact recall precision — and effective distillation fact-yield — down.This is the same defect the episodic recall gate already removed (
recall_episodes_ranked/search_episodes_by_keywords, PR #4241 lineage). Fact recall was never gated. This PR applies the analogous marker-safe word-boundary gate to the FACT path, reusing the existingshares_word_prefixhelper.Empirical bug evidence (live in-memory backend)
For a fact whose content is
"the reactor overheated":reactorreactdeploydeployedactowntestFix
search_factspartitions the query into two token shapes (mirroringsearch_episodes_by_keywords) and post-filters the backend's results:conceptORcontent(word-boundary), dropping interior/suffix noise while preserving inflectional recall (deploy→ "deployed").bug-pattern, or ajournal:/goal-edge:/sub:marker): keeps the library's exact contiguous-substring semantics its callers store and re-filter on. A query with no clean token bypasses the gate, so the many concept/marker callers are provably unaffected.Both fields are checked (the library matches both). Truncation to
limitis deferred until after the gate (backend queried unbounded on the clean path) so a relevant fact ranked behind a false positive is not dropped before the gate runs — mirroringrecall_episodes_ranked. Wildcard/empty queries keep the return-all path. The gate only ever removes false positives — it never adds a fact the backend did not return and never reorders.Verified empirically that the library treats a hyphenated token as a contiguous substring (
goal-nodematched only "a goal-node payload", not "goal … node"), matching the raw-token check exactly — so mixed clean+marker queries do not regress. Caller safety: concept/marker callers pass colon/hyphen queries (raw → gate bypassed) or recall under the identical concept a fact was stored under (self word-boundary match → always kept);journalenumeration keeps working (cleanjournalword-boundary-matches thejournal:YYYY-MM-DDconcept).Merge-ready evidence
(1) qa-team scenario —
tests/gadugi/fact-recall-word-boundary-precision.yaml(mirrorsdistill-concept-canonicalization.yaml; YAML-validated). Drives:cargo test --locked --lib tests_fact_recall_word_boundary(end-to-end, live in-memory backend)cargo test --locked --lib fact_query_gate_tests(pure gate helpers)cargo test --locked --lib tests_whole_word_episode_recall(episodic-gate regression guard)New tests (12): 6 unit (
library_adapter::fact_query_gate_tests) + 6 integration (cognitive_memory::tests_fact_recall_word_boundary) covering interior/suffix drop, word-boundary + inflectional preservation, concept-field match, marker/concept substring preservation, deferred-truncationlimithonouring, and wildcard/empty bypass.(2) Docs —
docs/reference/cognitive-memory-fact-recall.md(new "Word-boundary relevance gate" section) anddocs/architecture/cognitive-memory-library-adapter.md(fact-path gate subsection + in-adapter table row); bothlast_updatedbumped.(3) Quality-audit — ≥3 SEEK→VALIDATE→FIX cycles, ending clean:
usize::MAXto the library is safe (established adapter pattern for episodic recall + empirically verified no capacity pre-alloc);limit=0degenerate-but-correct;" * "behavior unchanged vs. prior code.search_factscallers; marker/concept/self-identical recalls provably preserved; full suite green confirms none regressed.(4) CI-equivalent gates (local):
cargo fmt --all --check— cleancargo clippy --all-targets --all-features --locked -- -D warnings— cleancargo clippy --release --no-deps -- -D warnings(pre-commit) — clean(6) Focused diff — 6 files (1 source, 1 test module + wiring, 2 docs, 1 qa scenario); +648/−3; no unrelated edits.
Scope
One durable, self-contained improvement toward the standing perpetual cognition goal (recall quality + distillation fact-yield). Supersedes/replaces the earlier redundant #4307 (whose concept-canonicalization change had already landed independently in #4246); this branch was reset onto latest
mainand targets a distinct, still-open gap on the fact read path.Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com