Skip to content

feat(recall): two-concept admission for memory_recall behind a flag - #418

Merged
abogartz merged 2 commits into
mainfrom
feat/409-query-token-gate
Nov 17, 2026
Merged

abogartz merged 2 commits into
mainfrom
feat/409-query-token-gate

Conversation

@abogartz

@abogartz abogartz commented Nov 16, 2026 •

Copy link
Copy Markdown
Owner

What

Adds two-concept admission to the memory_recall tool's matcher behind OPENCODE_MEMORY_QUERY_GATE (unset = shipped behaviour): once a query carries three or more content words, a returned row must share two whole words with it. This is the rule the injection path already applies (LONG_QUERY_TOKENS in src/recall/score.ts); the tool did not, so one shared word — head, run, on — was enough to fill the window.

Reworked after adversarial review

The first version counted the scorer's substring hits. Review showed the gate was then satisfiable by two accidents, reproduced against the shipped code:

  • on matched inside the type field (context, decision), pr inside preference;
  • 283 matched inside build2837 — the example query in the original body still admitted the row it was meant to refuse;
  • OPENCODE_MEMORY_QUERY_GATE=no (and n, 00, 0.0) enabled the gate.

Admission now:

  • compares whole words — db_id and OPENCODE_MEMORY_DIR stay single concepts (splitting them is the regression that blocked perf(recall): split identifiers in the matcher tokenizer (#409) #417), dashes separate, so 283 no longer matches build2837 or #2830;
  • drops the injection scorer's own stopword list and fragments (< 3 chars) from the query, so how do I set the pr on reduces to the one concept set;
  • gateEnabled() allowlists 1|true|yes|on; everything else is off;
  • a query that reduces to zero concepts abstains rather than returning grammar-matched noise.

Measurements — re-run; the first version's numbers are void

The original numbers were measured with substring admission and relevance labels taken from a judge that saw row prose clipped to 500 chars. Rebuilding the labels with the store's own full text (4,000-char cap) moved 158 of 1,863 labels across the answer boundary, so every decisive pair — any row a gate moved into or out of a window — was re-adjudicated in isolation on two independent seats (27B judge + a second model). The isolated seats overturned 29 of the 36 disputed ANSWER labels: batch context inflates answer labels on long rows. Numbers below use the adjudicated labels, measured through the real handler (poc-409/casebank/gate-e2e.ts, PLUGIN_SRC at this branch).

Held-out half, n=227, window k=3:

flag off flag on
answers at rank 1 56 57
queries whose window holds an answer 96 95
paired discordance (off-only / on-only) — 1 / 0 (p = 1.0)
windows shown 217 206
rows returned 571 479
rows not answering 438 346
no-gold queries shown a window 121/131 110/131

All 404 queries: answers at rank 1 106 → 106, paired discordance 3/2 (p = 1.0), rows 1,043 → 893.

Flag off, the handler's windows are byte-identical to origin/main on 404/404 queries (diffed as ts lists).

What this does not fix

Tests

17 tests pin the counterexamples above, the env allowlist, the distinct-concept boundary (three tokens with a duplicate stay permissive), and the flag-off path; beforeEach clears the flag so an ambient CI value cannot flip the off-tests. bun run gate (tsc + biome + unit + e2e) green.

Case bank: 404 real memory_recall queries from 30 days of session transcripts, relevance labelled by independent readers against a pinned store snapshot (not committed — it carries query text and memory prose). See #409 for the instrument and its known limits.

@abogartz

Copy link
Copy Markdown
Owner Author

Composition note. Measured alone, admission only — ranking, scoring and window size untouched. Off by default. Every row this gate promotes into the window from below rank 3 is labelled: 9 queries gain a row, one an answer, one something useful, eight junk. That is the counterweight to the 145 rows removed across all 404 queries. With the flag unset the windows are identical to origin/main on 404 of 404. Interacts with #415 and #416, which change the same window by other means.

@abogartz

Copy link
Copy Markdown
Owner Author

Rework pushed (d3cc122); the body now carries the current numbers — everything quoted here before this comment is void.

Two independent problems, both found in adversarial review:

  1. The gate counted substrings, so two accidents satisfied it: on inside the type field, pr inside preference, and 283 inside build2837 — the example in the original body still admitted the row it was meant to refuse. Also =no enabled the gate. Fixed: whole-word concept counting (identifiers with underscores stay whole), shared stopword list, env allowlist. Counterexamples from the review are now pinned by tests.

  2. The relevance labels were clipped. The judge saw row prose cut to 500 chars; 83% of labelled pairs were judged on a clipped row. All 1,863 pairs were relabelled with full text, and every pair a gate moved in or out of a window was re-adjudicated in isolation on two seats. The isolated seats overturned 29 of the 36 disputed ANSWER labels — batch context inflates answer labels on long rows. The substring gate's "zero recall cost" was partly a label artifact; the concept gate's apparent 21-query recall loss was mostly the other label artifact.

Adjudicated held-out (n=227, k=3): answers at rank 1 56→57, paired discordance 1/0 (p=1.0), rows 571→479, non-answering rows 438→346. Flag off byte-identical to origin/main, 404/404.

memory_recall scores a row by counting how many of the query's words appear in
it as substrings, and returns anything that scores above zero. One shared word
is enough, so `watermark 283 287 head` answers with every row that mentions a
head, and `fixer ready tickets queue blocked` answers with rows about the GitHub
CLI and about npm publishing. On 404 real recall queries from 30 days of session
transcripts, labelled by two independent readers against a pinned store snapshot
(#409 case bank), 42% of the rows the tool returned were judged irrelevant and a
further 30% related-but-not-an-answer.

The injected relevance block already refuses single-concept matches once a query
is long enough to be a deliberate search (LONG_QUERY_TOKENS). This applies that
rule to the tool, behind OPENCODE_MEMORY_QUERY_GATE, so the two paths agree on
when nothing is a good enough answer. Admission only: ranking, scoring and the
window size are untouched.

Measured by running the handler itself with the flag flipped once per query:
held-out half (227 queries, k=3), answers in the window 148 -> 149, answers at
rank 1 125 -> 125 (zero discordant pairs against recall), returned rows
571 -> 480, non-answering rows 363 -> 272, and no-gold queries still shown a
window 0.847 -> 0.678, paired 10-to-0 (McNemar p=0.002). Every row the gate
promotes from below rank 3 is labelled too: nine queries gain a row, one an
answer, one something useful, eight junk.

With the flag unset the tool is unchanged: the same 404 queries run through
origin/main and through this branch produce identical windows on 404 of 404.

Refs #409
The first version counted the scorer's substring hits, which made the
gate satisfiable by accidents: 'on' occurs inside 'context' and
'decision', 'pr' inside 'preference', and '283' inside 'build2837' — so
the query that motivated this change, 'watermark 283 287 head', still
admitted the row it was meant to refuse. Admission now compares whole
words (underscore kept inside identifiers, so 'db_id' and
'OPENCODE_MEMORY_DIR' survive), drops the query's stopwords and
fragments, and shares the injection scorer's stopword list.

gateEnabled() now allowlists '1', 'true', 'yes' and 'on'; the old
deny-list treated 'no', 'n', '00' and '0.0' as on.

Tests pin the counterexamples, the distinct-concept boundary, and clear
OPENCODE_MEMORY_QUERY_GATE in beforeEach so an ambient flag cannot flip
the flag-off tests. The substring gate's case-bank numbers are void;
they are re-measured against full-text labels before the PR body is
updated.
@abogartz
abogartz force-pushed the feat/409-query-token-gate branch from d3cc122 to 79c2d8e Compare November 17, 2026 05:43
@abogartz
abogartz merged commit 9c5c867 into main Nov 17, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.27.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@abogartz
abogartz deleted the feat/409-query-token-gate branch November 19, 2026 05:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant