fix: centre search excerpts on rare query terms, match simple inflections #29

Merged
jknapp merged 1 commits from fix/excerpt-centering into main 2026-11-14 19:14:27 +00:00
Owner

Problem

Reported from claude.ai after #28: searching WHP founding partner pricing returned this excerpt from the OLS memory:

…AI agents are treated as business partners: honest, direct, push back when something won't work. Q1 2027 priorities: (1) WHP commercial launch…

It should have shown the Founding Partner and pricing passage further down.

Root cause

Every query term counted the same:

  • "WHP" appears four times in the memory, and "partners" turns up coincidentally in an earlier sentence.
  • That window matched 2 terms, the same as the "Founding Partner Program" window, and won the tie by coming first.
  • "pricing" matched nothing, because the memory says "price".

Fix

All changes are in lib/memory-excerpt.ts:

  • Rarity weighting: each term counts 1 ÷ (its occurrences in this memory). "founding" appears once and counts fully; "WHP" appears four times and counts a quarter.
  • Light suffix stripping: strip one of -ing, -ed, -es, -s or -e (keeping 4+ characters) and match as a prefix, so "pricing" and "price" both become "pric".

Why not ts_headline

I tested it on the same text in Postgres 16:

  • OR query (whp | founding | partner | pricing): returns the same wrong "business partners … WHP" passage, because it also weighs words equally. Same result with 1 or 2 fragments.
  • plainto_tsquery (AND): no passage contains all four terms, so it falls back to the "WHP decisions … $20 or $25 per server" sentence, which still misses Founding Partner.

Result

…"cPanel" in feature names, trademark). Founding Partner Program: first 10 hosts get 90 days free on 1 server, one-on-one onboarding (setup, test migrations, WHMCS integration) and a locked price;…

Tests

  • Regression test using the real memory text: the excerpt must contain "Founding Partner Program" and "locked price".
  • Inflection test: "pricing" must find "flat price per server".
  • Both failed before the fix. Full suite: 90 passing. Typecheck and lint are clean.

🤖 Generated with Claude Code

## Problem Reported from claude.ai after #28: searching `WHP founding partner pricing` returned this excerpt from the OLS memory: > …AI agents are treated as business partners: honest, direct, push back when something won't work. Q1 2027 priorities: (1) WHP commercial launch… It should have shown the Founding Partner and pricing passage further down. ## Root cause Every query term counted the same: - "WHP" appears four times in the memory, and "partners" turns up coincidentally in an earlier sentence. - That window matched 2 terms, the same as the "Founding Partner Program" window, and won the tie by coming first. - "pricing" matched nothing, because the memory says "price". ## Fix All changes are in `lib/memory-excerpt.ts`: - **Rarity weighting:** each term counts 1 ÷ (its occurrences in this memory). "founding" appears once and counts fully; "WHP" appears four times and counts a quarter. - **Light suffix stripping:** strip one of -ing, -ed, -es, -s or -e (keeping 4+ characters) and match as a prefix, so "pricing" and "price" both become "pric". ## Why not `ts_headline` I tested it on the same text in Postgres 16: - **OR query** (`whp | founding | partner | pricing`): returns the same wrong "business partners … WHP" passage, because it also weighs words equally. Same result with 1 or 2 fragments. - **`plainto_tsquery` (AND):** no passage contains all four terms, so it falls back to the "WHP decisions … $20 or $25 per server" sentence, which still misses Founding Partner. ## Result > …"cPanel" in feature names, trademark). Founding Partner Program: first 10 hosts get 90 days free on 1 server, one-on-one onboarding (setup, test migrations, WHMCS integration) and a locked price;… ## Tests - Regression test using the real memory text: the excerpt must contain "Founding Partner Program" and "locked price". - Inflection test: "pricing" must find "flat price per server". - Both failed before the fix. Full suite: 90 passing. Typecheck and lint are clean. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jknapp added 1 commit 2026-11-14 19:12:42 +00:00
Searching "WHP founding partner pricing" excerpted the "business
partners ... (1) WHP commercial launch" sentence instead of the Founding
Partner passage. Every query term counted the same, so a common term
("WHP", four times) plus a coincidental hit ("partners") tied the real
passage on two terms and won by coming first. "pricing" matched nothing:
the memory says "price".

- Weight each term by rarity within the memory (1 / occurrences), so a
  word that appears once outweighs one sprinkled throughout.
- Strip one common suffix (-ing/-ed/-es/-s/-e, keeping 4+ chars) and
  match as a prefix, so "pricing" and "price" both become "pric".

Postgres ts_headline, suggested as the alternative, was tested on the
same text and picked the same wrong passage with an OR query (it also
weighs words equally); with plainto_tsquery's AND it found no cover and
fell back to a different, also wrong, passage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jknapp merged commit 3da0c77849 into main 2026-11-14 19:14:27 +00:00
jknapp deleted branch fix/excerpt-centering 2026-11-14 19:14:28 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: cybercove-labs/shared-memory#29