opencode-self-improvement 0.26.1 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -88,6 +88,7 @@ Zero configuration is the default. To tune, use the object form of the plugin en
88
88
  | `OPENCODE_MEMORY_INJECT_BUDGET` | Fallback for `injectBudget`. |
89
89
  | `OPENCODE_MEMORY_FRONTIER_BUDGET` | Fallback for `frontierBudget`. |
90
90
  | `OPENCODE_MEMORY_DEDUP` | Near-duplicate suppression inside a `memory_recall` window. Set to a content-token Jaccard threshold, for example `0.8`: a row that is a near-copy (same scope, Jaccard at or above the threshold) of a row already returned is skipped — the newest copy of a clash survives — so the window fills with distinct facts instead of repeats of the top hit. Rows that disagree on a negation word, or carry too little comparable content, are never suppressed. Unset or `0` returns the plain top-`limit` slice, byte-identical to previous behavior; below `0.5` the value is treated as a misconfiguration and the pass stays off; see [#409](https://github.com/abogartz/opencode-self-improvement/issues/409). |
91
+ | `OPENCODE_MEMORY_QUERY_GATE` | When `1`, `true`, `yes` or `on`, a `memory_recall` match must share **two** whole words of the query once the query has three content words — the rule the injected relevance block already applies. Every other value, including `no`, keeps the matcher's permissive one-word admission. |
91
92
  | `OPENCODE_COMMANDS_DIR` | Where the plugin mirrors its slash commands (default `~/.config/opencode/commands`). |
92
93
  | `OPENCODE_VIEWER_HOST` | Interface the editor binds to (default `127.0.0.1`). A non-loopback bind serves the store over plain HTTP with no authentication — only do it on a network you control. See [Surfaces](#surfaces). |
93
94
  | `OPENCODE_VIEWER_PORT` | Admin editor port (default `8787`). |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.26.1",
3
+ "version": "0.27.0",
4
4
  "description": "Durable memory for opencode: remember facts, decisions, and commands across sessions, plus a one-command per-repo setup (/repo-init).",
5
5
  "main": "index.ts",
6
6
  "exports": {
@@ -0,0 +1,131 @@
1
+ // Two-concept admission for the recall tool's matcher (#409, case bank).
2
+ //
3
+ // The tool scores a row by counting how many of the query's words appear in it
4
+ // as substrings, and returns anything that scores above zero. One shared word is
5
+ // enough, so `watermark 283 287 head` returns every row that mentions a head,
6
+ // and `fixer ready tickets queue blocked` returns rows about the GitHub CLI and
7
+ // about npm publishing. Measured on 404 real recall queries taken from 30 days of
8
+ // session transcripts and labelled by two independent readers (poc-409/casebank),
9
+ // a large share of what the tool returned was judged irrelevant rather than
10
+ // answering.
11
+ //
12
+ // The injection path already refuses single-concept matches once a query is long
13
+ // enough to be a deliberate search (LONG_QUERY_TOKENS in score.ts). This applies
14
+ // that rule to the tool, so the two paths agree on when nothing is a good enough
15
+ // answer. It changes admission only: ranking, scoring and the window size are
16
+ // untouched, and the flag-off path is the shipped behaviour.
17
+ //
18
+ // OPENCODE_MEMORY_QUERY_GATE=1 (unset or anything else = shipped behaviour)
19
+ //
20
+ // WHY THE GATE COUNTS WORDS AND THE SCORER COUNTS SUBSTRINGS
21
+ //
22
+ // These are different questions, and using the tool's own substring test here was
23
+ // the first version's mistake. Scoring asks how strongly a row matches, and
24
+ // substring matching is deliberate there: it is how `getUserAccount` finds a row
25
+ // written `get_user_account`. Admission asks how many DISTINCT CONCEPTS the query
26
+ // and the row share, and a substring is not a concept. Counting substrings made
27
+ // the gate satisfiable by two accidents: `on` occurs inside `context` and inside
28
+ // `decision`, so every row of those types shared it; `pr` occurs inside
29
+ // `preference`; and `283` occurs inside `build2837`, so the query that motivated
30
+ // this change, `watermark 283 287 head`, still admitted the row it was supposed to
31
+ // refuse. The gate therefore compares whole words, and drops the query's stopwords
32
+ // and fragments, which is exactly the vocabulary the injection scorer counts.
33
+ //
34
+ // Whole words are also what makes the number queries work. `#277` tokenises to
35
+ // `277`, so a PR-number search still matches the row that cites it, while `283` no
36
+ // longer matches `#2830`.
37
+ //
38
+ // STATUS: measured on the case bank; see the PR for the current numbers. The
39
+ // first version's numbers were measured with substring counting and are void.
40
+ // See https://github.com/abogartz/opencode-self-improvement/issues/409.
41
+ import type { Memory } from "../store/model/memory.ts";
42
+ import { LONG_QUERY_TOKENS, QUERY_STOPWORDS } from "./score.ts";
43
+
44
+ export const GATE_ENV = "OPENCODE_MEMORY_QUERY_GATE";
45
+
46
+ /**
47
+ * On only for an explicit affirmative. Everything else — unset, empty, `0`,
48
+ * `false`, and in particular `no`, which a shell user writes meaning off — is off.
49
+ * A deny-list was the first version's mistake: `=no`, `=n`, `=00` and `=0.0` all
50
+ * enabled the gate.
51
+ */
52
+ export function gateEnabled(
53
+ env: Record<string, string | undefined> = process.env,
54
+ ): boolean {
55
+ const raw = env[GATE_ENV]?.trim().toLowerCase();
56
+ return raw === "1" || raw === "true" || raw === "yes" || raw === "on";
57
+ }
58
+
59
+ /**
60
+ * The text a recall query is matched against: type, scope, prose and the
61
+ * author-supplied keyword aliases, lower-cased. The tool's scorer shares this one
62
+ * definition so a row can never be scored against one string and admitted against
63
+ * another. Distinct from score.ts's `haystack`, which orders the same fields for
64
+ * the injection scorer.
65
+ */
66
+ export function recallHaystack(m: Memory): string {
67
+ const kw = m.kw?.length ? ` ${m.kw.join(" ")}` : "";
68
+ return `${m.type} ${m.scope} ${m.content}${kw}`.toLowerCase();
69
+ }
70
+
71
+ // Split into whole words at whitespace and punctuation. Underscore stays inside a
72
+ // word so the identifier a user searches for — `db_id`, `OPENCODE_MEMORY_DIR` —
73
+ // survives as the single concept it is (splitting it is the regression that
74
+ // blocked PR #417); a dash separates, because prose writes `get-user-account` as
75
+ // the three words it means. `build2837` stays one word, which is why `283` can no
76
+ // longer match it.
77
+ const WORD = /[^a-z0-9_]+/;
78
+
79
+ /**
80
+ * The query's distinct concepts: lower-cased whole words, with fragments and the
81
+ * scorer's own stopword list removed. A gate that counted `on` or `i` was counting
82
+ * punctuation and grammar, not concepts.
83
+ */
84
+ export function queryConcepts(query: string): string[] {
85
+ const out: string[] = [];
86
+ for (const w of query.toLowerCase().split(WORD))
87
+ if (w.length > 2 && !QUERY_STOPWORDS.has(w) && !out.includes(w))
88
+ out.push(w);
89
+ return out;
90
+ }
91
+
92
+ /** The row's concepts, as a set for whole-word membership tests. */
93
+ export function rowConcepts(m: Memory): Set<string> {
94
+ return new Set(
95
+ recallHaystack(m)
96
+ .split(WORD)
97
+ .filter((w) => w.length > 2),
98
+ );
99
+ }
100
+
101
+ /** How many of `concepts` the row contains as whole words. */
102
+ export function sharedConcepts(
103
+ m: Memory,
104
+ concepts: readonly string[],
105
+ row?: Set<string>,
106
+ ): number {
107
+ const words = row ?? rowConcepts(m);
108
+ let shared = 0;
109
+ for (const w of concepts) if (words.has(w)) shared++;
110
+ return shared;
111
+ }
112
+
113
+ /**
114
+ * True when the row may be returned. A query of LONG_QUERY_TOKENS or more
115
+ * concepts is a deliberate search, so a row must share two of them; a shorter
116
+ * query is too vague to hold a row to two. `concepts` is passed in so the split
117
+ * happens once per call rather than once per candidate.
118
+ */
119
+ export function admitsRow(
120
+ m: Memory,
121
+ concepts: readonly string[],
122
+ row?: Set<string>,
123
+ ): boolean {
124
+ // A query that reduces to zero concepts ("how do I") abstains on every row.
125
+ // That is intended: the scorer matched it on grammar alone — `i` occurs inside
126
+ // most English prose — and grammar-only matches are the noise this gate exists
127
+ // to refuse. "No matching memories" is the honest answer to a contentless
128
+ // query, and the tool already returns it when nothing scores.
129
+ const need = concepts.length >= LONG_QUERY_TOKENS ? 2 : 1;
130
+ return sharedConcepts(m, concepts, row) >= need;
131
+ }
@@ -95,7 +95,7 @@ export const QUERY_STOPWORDS = new Set(
95
95
  // is a deliberate search — one shared concept suffices. memory_recall and the
96
96
  // contextual pull keep single-overlap matches reachable; injection just stays
97
97
  // quiet unless the vocabulary genuinely agrees twice.
98
- const LONG_QUERY_TOKENS = 3;
98
+ export const LONG_QUERY_TOKENS = 3;
99
99
 
100
100
  // Production relevance ranking. Candidates must clear the concept-overlap
101
101
  // gate, then rank by Robertson-IF of the shared tokens (rare concepts beat
@@ -4,6 +4,12 @@
4
4
  import type { ToolRegister } from "./deps.ts";
5
5
  import { record } from "../evidence/record.ts";
6
6
  import { formatMemory } from "../inject/format.ts";
7
+ import {
8
+ admitsRow,
9
+ gateEnabled,
10
+ queryConcepts,
11
+ recallHaystack,
12
+ } from "../recall/gate.ts";
7
13
  import { loadAll } from "../store/files/load.ts";
8
14
  import { isInjectable } from "../store/model/predicates.ts";
9
15
  import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
@@ -15,8 +21,7 @@ import { dedupeThreshold, dedupeWindow } from "../recall/dedupe.ts";
15
21
  function scoreMemory(m: Memory, q: string): number {
16
22
  // Same indexed fields as the injection scorer (#40): a row recalled by one of
17
23
  // its keywords is the same row recalled by the prose that keyword stands for.
18
- const kw = m.kw?.length ? ` ${m.kw.join(" ")}` : "";
19
- const hay = `${m.type} ${m.scope} ${m.content}${kw}`.toLowerCase();
24
+ const hay = recallHaystack(m);
20
25
  let score = 0;
21
26
  for (const w of q.toLowerCase().split(/\s+/).filter(Boolean)) {
22
27
  if (hay.includes(w)) score++;
@@ -46,9 +51,14 @@ export const register: ToolRegister = (editor, deps) => {
46
51
  results = results.filter((m) => m.scope.includes(args.scope!));
47
52
  if (args.type) results = results.filter((m) => m.type === args.type);
48
53
  if (args.query) {
54
+ // Flag off, this is the shipped filter. Flag on, a row also has to
55
+ // share two whole words of the query — the concepts rule, not the
56
+ // scorer's substring test (src/recall/gate.ts, #409).
57
+ const gate = gateEnabled();
58
+ const concepts = gate ? queryConcepts(args.query) : [];
49
59
  results = results
50
60
  .map((m) => ({ m, score: scoreMemory(m, args.query!) }))
51
- .filter((x) => x.score > 0)
61
+ .filter((x) => x.score > 0 && (!gate || admitsRow(x.m, concepts)))
52
62
  .sort((a, b) => b.score - a.score)
53
63
  .map((x) => x.m);
54
64
  }