Skip to content

fix(cognition): whole-word, marker-safe episodic keyword recall (recall quality) - #4265

Merged
rysweet merged 1 commit into
mainfrom
engineer/continuously-research-and-improve-your-own-cogn-70ab8541-1784289647-70b3cd
Jul 17, 2026
Merged

fix(cognition): whole-word, marker-safe episodic keyword recall (recall quality)#4265
rysweet merged 1 commit into
mainfrom
engineer/continuously-research-and-improve-your-own-cogn-70ab8541-1784289647-70b3cd

Conversation

@rysweet

@rysweet rysweet commented Jul 17, 2026

Copy link
Copy Markdown
Owner

Summary

Improves episodic recall quality on the flat keyword scan
LibraryCognitiveMemory::search_episodes_by_keywords. It matched every keyword by
raw case-insensitive substring (content.contains(kw)), so a short clean token
embedded in the interior/suffix of an unrelated episode word spuriously recalled
off-topic episodes for its natural-language callers — e.g. decision ⊂ "indecision",
test ⊂ "latest", own ⊂ "download". This dilutes keyword recall and,
downstream, reasoner reliability.

The sibling ranked path (recall_episodes_ranked) already uses a word-boundary
gate (shares_word_prefix/tokenize_words). The flat scan was deliberately left on
substring only because its bracketed-marker callers
(memory_consolidation::reflection_lessons) depend on exact-substring match. This PR
closes that gap safely: it partitions query keywords by shape so clean tokens get
the word-boundary gate while markers keep substring — resolving the exact constraint
the substring path was preserved for, and reusing main's existing helpers (no second
matching policy).

The fix (marker-safe, reuses the established word-boundary gate)

  • Clean keyword (non-empty, all alphanumeric — the shape creative_ideas
    ("meeting"/"conversation"/"decision") and tokenize_objective emit): matched at a
    word boundary via the existing shares_word_prefix (a query token must be a
    prefix of a whole content word). Inflection-tolerant, so meeting still recalls
    "meetings" and deploy still recalls "deployed" (no Fix the episodic-recall-returns-zero defect in Simard's cognitive memory. SYMPTOM: Memory preparation logs "preparation: 5 procedures, 0 episodes recalled (0 raw, 0 session-filtered)" and "prepared c #2299 "0 raw" regression). Kills
    the interior/suffix false positives.
  • Phrase / bracketed marker keyword (any non-alphanumeric char — [reflect-occ=…],
    [reflect-key=…|…]): keeps legacy case-insensitive substring semantics; the
    reflection_lessons dedup and recurring-failure counting are byte-for-byte unchanged.
  • Perf guard: a has_clean flag skips the word-boundary tokenization entirely on the
    marker-only path (count_recurring_failures scans with limit = u32::MAX).
  • Also fixes a latent degenerate case: an empty-string keyword no longer matches every
    episode.

Merge-ready evidence

1. qa-team scenario (written, validated, run)

  • New scenario: tests/qa-scenarios/whole-word-episode-recall.yaml
  • gadugi-test validate -f tests/qa-scenarios/whole-word-episode-recall.yaml --strict
    ✓ Scenario "whole-word-episode-recall" is valid (1 valid, 0 invalid)
  • gadugi-test run -d tests/qa-scenarios -s whole-word-episode-recall
    ✓ Passed: 1 ✗ Failed: 0 (drives the live-backend contract, the pure
    word_boundary_gate_tests, and the full memory_consolidation suite proving the
    exact-marker reflection_lessons dedup is unregressed).

2. Docs updated (user-facing surfaces)

  • docs/architecture/cognitive-memory-library-adapter.md: updated the
    search_episodes_by_keywords behavior rows and added a "Keyword matching:
    word-boundary vs substring"
    subsection documenting the clean-token vs marker split.
  • src/cognitive_memory/mod.rs: trait doc for search_episodes_by_keywords updated to
    describe the word-boundary + marker-safe semantics.

Changed surfaces: search_episodes_by_keywords recall behavior for clean keywords
(now word-boundary). Marker/phrase callers are behavior-preserving (substring path
unchanged).

3. quality-audit (≥3 SEEK→VALIDATE→FIX cycles, ended clean)

  • Cycle 1 (SEEK): raw-substring gate admits interior/suffix embeddings → FIX:
    partition keywords into clean (word-boundary) vs raw (substring).
  • Cycle 2 (SEEK): could this break the marker callers? → VALIDATE: markers carry
    non-alphanumeric chars → routed to the substring path; confirmed by the full
    memory_consolidation suite (148 pass, incl. reflection_lessons dedup and the PR-C
    recall/count e2e tests).
  • Cycle 3 (SEEK): perf regression on the marker-only u32::MAX scan (needless
    tokenization) → FIX: has_clean guard skips shares_word_prefix when no clean
    keyword is present.
  • Final cycle (VALIDATE, clean): reuse main's tokenize_words/shares_word_prefix
    (avoid a second, divergent matching policy); re-ran targeted suites, clippy --lib --tests -- -D warnings (clean), fmt --check (clean). Zero critical/high; zero
    medium correctness/security findings.

Local validation (all green): cognitive_memory:: (129), memory_consolidation (148),
creative_ideas (74), memory_ipc:: (68), the new
cognitive_memory::tests_whole_word_episode_recall (6), docs_integrity (4).
Pre-push gate: race-subset suite 443 passed, 0 failed and
clippy --all-targets --all-features --locked -- -D warnings (clean). Pre-commit gate:
release clippy --release --no-deps -- -D warnings (clean).

4. CI

  • All pre-commit/pre-push hooks green locally (fmt, race-subset 443/443, release clippy,
    full clippy).
  • Branch rebased on latest main (resolved a conflict from the concurrently-merged
    recall_episodes_ranked word-boundary work); full CI monitored on this PR and will be
    confirmed 100% green before requesting review/merge.

6. Focused diff

5 files, no unrelated edits:

  • src/cognitive_memory/library_adapter.rs — partition + reuse of shares_word_prefix
  • src/cognitive_memory/mod.rs — trait doc + test module registration
  • src/cognitive_memory/tests_whole_word_episode_recall.rs — new integration test module
  • tests/qa-scenarios/whole-word-episode-recall.yaml — new qa scenario
  • docs/architecture/cognitive-memory-library-adapter.md — behavior/divergence doc

Standing-goal alignment

A durable code improvement to recall quality (cognition), extending the just-merged
word-boundary gate to the last substring recall path in a marker-safe way — consistent
with the perpetual "research and improve your own cognition" goal (durable PRs, not
snapshot docs).

…all quality)

`LibraryCognitiveMemory::search_episodes_by_keywords` matched every keyword by
raw case-insensitive substring, so a short clean token embedded in the
interior/suffix of an unrelated episode word ("test" in "latest", "decision"
in "indecision") spuriously recalled off-topic episodes for the
natural-language callers (creative_ideas: "meeting"/"conversation"/"decision").
The sibling ranked path already uses a word-boundary gate; the flat scan was
left on substring only because its bracketed-marker callers
(memory_consolidation::reflection_lessons) depend on exact-substring match.

Partition the query keywords by shape: a clean alphanumeric keyword is matched
at a word boundary via the existing `shares_word_prefix` gate (inflection-
tolerant, so plural/verb recall is preserved), while a phrase or bracketed
provenance marker keeps the exact substring semantics reflection_lessons dedup
and recurring-failure counting rely on. A `has_clean` guard skips the
word-boundary tokenization on the marker-only path (count_recurring_failures
scans with limit = u32::MAX).

Reuses main's tokenize_words/shares_word_prefix helpers (no second policy).
Adds a live-backend integration test module and a qa-team scenario; updates the
adapter architecture doc and the trait doc.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rysweet
rysweet force-pushed the engineer/continuously-research-and-improve-your-own-cogn-70ab8541-1784289647-70b3cd branch from a8fac1d to 30b2eee Compare July 17, 2026 13:32
@github-actions

Copy link
Copy Markdown

📊 Coverage Summary

Generated by cargo llvm-cov --workspace --summary-only (nightly, excluding test files)

Module Lines Covered Coverage
Total 183336 153183 83.6%

Coverage data from CI run. Test files matching tests?/ are excluded from line counts.

@rysweet
rysweet merged commit d877579 into main Jul 17, 2026
18 checks passed
@rysweet
rysweet deleted the engineer/continuously-research-and-improve-your-own-cogn-70ab8541-1784289647-70b3cd branch July 17, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant