opencode-self-improvement 0.26.0 → 0.26.1
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 +2 -0
- package/package.json +1 -1
- package/src/recall/score.ts +110 -12
- package/src/tools/recall.ts +10 -2
package/README.md
CHANGED
|
@@ -180,6 +180,7 @@ Two results. Only two are quoted, each with its `n` and the seat it ran on, beca
|
|
|
180
180
|
- **Section-frozen injection.** The whole memory section — standing digest, open-thread frontier, relevance block — is computed once per epoch and delivered byte-identically until something the model actually needs changes. The epoch's authority is the **rendered bytes**, not the store fingerprint: a promoted row that changes nothing visible costs no cache bust.
|
|
181
181
|
- **A session never pays for bytes it is not riding.** A memory *you* saved in this session is a fact you already produced — it is in your own transcript — so it never busts your cache. The test is not "did the store change" but "would the frozen bytes still have been produced from the same inputs, minus the rows this session authored since": a correction, blocklisting, redaction or supersession of a row you are riding, and anything another session, the viewer or an import wrote, all still land on your next request.
|
|
182
182
|
- **Two-concept threshold.** A free-text prompt must share at least two distinct concepts with a memory for it to be injected; keyword-length queries (one or two words) need only one. Silence on single-word coincidences is deliberate; `memory_recall` still reaches them on the pull path.
|
|
183
|
+
- **Identifiers split.** `getUserAccount`, `get_user_account`, `get-user-account` and `get.user.account` are one concept to the matcher rather than four, and a memory citing `src/recall/score.ts` answers a session that typed the path ([#409](https://github.com/abogartz/opencode-self-improvement/issues/409)). Measured on the published eval: aggregate recall unchanged, wrong-memory-first at the top falls 6% → 5%. Replayed over 1,091 logged recall queries through this scorer against the live store: 92.3% of top-5 sets are identical and 97.5% keep their top row.
|
|
183
184
|
- **User-stated guidance rides ahead of relevance.** A row auto-capture tagged `user-guided` — something you said outright, like "the correct sales tax rate is 8.25%" — rides the block ahead of the lexical picks: newest-first, at most three, and `injectBudget` still bounds the whole block (`test/hint-channel.test.ts`). The channel exists because ranking cannot bridge vocabulary: a prompt about "the injected bug" shares no word with a row stating a tax rate, and the shipped matcher scored it zero ([#334](https://github.com/abogartz/opencode-self-improvement/issues/334)). `memory_recall` and the contextual pull stay query-driven — the channel changes only what rides the prompt. Because always-riding priority is a poisoning amplifier, the tag is minted by auto-capture alone: `memory_remember`, `memory_update` replacements and JSONL imports strip it, along with the contradiction fields ([#342](https://github.com/abogartz/opencode-self-improvement/issues/342), `test/guidance-provenance.test.ts`).
|
|
184
185
|
- **The block re-scores at every user turn.** An epoch is not frozen at the first request: a later prompt re-scores, and the epoch MOVES only when the rendered bytes move — a prompt change that selects the same rows in the same order costs no cache bust ([#340](https://github.com/abogartz/opencode-self-improvement/issues/340), `test/epoch.test.ts`). Order is part of those bytes on this build: the lexical picks render in score order, so a prompt that keeps the same rows and re-ranks them moves the bytes and pays the bust. Canonical render order, which removes that churn, is still open ([#388](https://github.com/abogartz/opencode-self-improvement/issues/388), [#396](https://github.com/abogartz/opencode-self-improvement/pull/396)).
|
|
185
186
|
- **Hard budgets.** Relevance block and each pull are capped at `injectBudget` characters, the frontier at `frontierBudget`, trimmed at whole-memory boundaries.
|
|
@@ -220,6 +221,7 @@ No vector database, no embedding model, no API key. **Bench** — what the keywo
|
|
|
220
221
|
- **In-project prompts are overwhelmingly lexical** (`bun test`, "the rate limiter", "staging deploys"): 98–100% R@3, flat from store size 50 to 500.
|
|
221
222
|
- **Single-overlap guessing was traded away.** Free-text prompts that share one concept abstain rather than force a guess; wrong top-memory fell to 6%. `memory_recall` still covers them.
|
|
222
223
|
- **Zero-overlap paraphrase is the real limit, and write time is where it is answered.** `memory_remember` asks each row for 3–5 `kw` aliases (other names, other verb forms, the literal command); auto-capture asks for the same list, so these numbers are measured through the tool path rather than through capture. Keywords are indexed and never rendered. Strict R@1 0% → 26%, near R@1 28% → 67%. A permutation control (every row given someone else's aliases) scores 29%/35%, so the gain is meaning rather than extra tokens. The limit is narrowed, not removed.
|
|
224
|
+
- **`memory_recall` returns 5 rows by default, not 20.** A live audit joined 1,090 recall events to their session transcripts: a returned row at rank 1 shows up in a later tool call at 3.4x the rate of a random store row, ranks 2–3 at 2.4x, and ranks 11–20 at 1.16x — noise against a 0.129 null. The old default returned 17.7 rows per call, so most of the window was context nobody read. At limit 5, recall@1 and recall@3 are unchanged, precision rises 4.1% → 13.1%, and 74% of the returned characters disappear; every gold row the cut dropped sat at ranks 6–20. Pass `limit` for a wider sweep ([#409](https://github.com/abogartz/opencode-self-improvement/issues/409)).
|
|
223
225
|
- **Caveats.** The dataset is synthetic (52 memories / 105 queries, a fictional project), its class mix is deliberately adversarial, and scoring is single-gold.
|
|
224
226
|
|
|
225
227
|
Full methodology and all store sizes: `bench/recall-results.md`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-self-improvement",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.1",
|
|
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": {
|
package/src/recall/score.ts
CHANGED
|
@@ -5,11 +5,74 @@ import { isInjectable } from "../store/model/predicates.ts";
|
|
|
5
5
|
import { cmpTs } from "../core/cmp-ts.ts";
|
|
6
6
|
import { type UseSignals, useBoost } from "./relevant.ts";
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* The matcher's tokenizer. Identifier-aware (#409, RESULTS §15): the shipped
|
|
10
|
+
* version kept `_` inside a token and never touched case, so `getUserAccount`,
|
|
11
|
+
* `get_user_account` and `get-user-account` were three unrelated tokens.
|
|
12
|
+
*
|
|
13
|
+
* Each identifier (a maximal run of letters, digits, `_`, `.`, `-`, `/`) emits
|
|
14
|
+
* TWO granularities: the whole identifier lower-cased, and its segments split
|
|
15
|
+
* at camelCase boundaries and separators. The whole form is what preserves the
|
|
16
|
+
* exact match the shipped tokenizer had — `db_id` and `OPENCODE_MEMORY_DIR`
|
|
17
|
+
* stay single tokens, and an authored keyword stored fused (`memoryrecallthreshold`)
|
|
18
|
+
* still matches the query that cites it. Dropping the whole form is the
|
|
19
|
+
* regression that made the first version of this change abstain on `db_id`
|
|
20
|
+
* queries and break the #40 vocabulary bridge for camelCase aliases. The
|
|
21
|
+
* segments are what make the styles match each other: a query typed
|
|
22
|
+
* `getUserAccount` shares `get`, `user`, `account` with a row written
|
|
23
|
+
* `get_user_account`.
|
|
24
|
+
*
|
|
25
|
+
* The scan is one linear pass with no backtracking: a 4,000-character row of
|
|
26
|
+
* adversarial text costs microseconds, not milliseconds.
|
|
27
|
+
*
|
|
28
|
+
* Measured on the bench fixture at the SHIPPED configuration (`kw` indexed,
|
|
29
|
+
* store 200 and 48): lexical R@1 stays 98% and top-1 noise stays 6% — the
|
|
30
|
+
* fixture's queries never spell the same identifier two ways, so this is a
|
|
31
|
+
* correctness fix the fixture cannot price, not a measured win. The no-keyword
|
|
32
|
+
* arm moves (lexical R@1 98% -> 100%), but production indexes keywords.
|
|
33
|
+
*/
|
|
8
34
|
export function words(text: string): string[] {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
35
|
+
const out: string[] = [];
|
|
36
|
+
const ids = text.match(/[A-Za-z0-9_.\-/]+/g);
|
|
37
|
+
if (!ids) return out;
|
|
38
|
+
for (const id of ids) {
|
|
39
|
+
// Sentence punctuation is not part of the identifier: a row ending
|
|
40
|
+
// `Join on db_id.` must still carry the token `db_id`. Underscore stays
|
|
41
|
+
// (the shipped tokenizer kept it), and interior separators stay — only
|
|
42
|
+
// the edges are trimmed.
|
|
43
|
+
const whole = id.replace(/^[./-]+|[./-]+$/g, "").toLowerCase();
|
|
44
|
+
if (whole.length > 2) out.push(whole);
|
|
45
|
+
let cur = "";
|
|
46
|
+
const flush = () => {
|
|
47
|
+
if (cur.length > 2) {
|
|
48
|
+
const seg = cur.toLowerCase();
|
|
49
|
+
if (seg !== whole) out.push(seg); // single words emit once, as before
|
|
50
|
+
}
|
|
51
|
+
cur = "";
|
|
52
|
+
};
|
|
53
|
+
for (let i = 0; i < id.length; i++) {
|
|
54
|
+
const ch = id[i]!;
|
|
55
|
+
const c = id.charCodeAt(i);
|
|
56
|
+
const isUpper = c >= 65 && c <= 90;
|
|
57
|
+
const isLower = c >= 97 && c <= 122;
|
|
58
|
+
const isDigit = c >= 48 && c <= 57;
|
|
59
|
+
if (!isUpper && !isLower && !isDigit) {
|
|
60
|
+
flush(); // `_`, `.`, `-`, `/` end a segment
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if (isUpper && cur) {
|
|
64
|
+
const prev = cur.charCodeAt(cur.length - 1)!;
|
|
65
|
+
const next = i + 1 < id.length ? id.charCodeAt(i + 1)! : 0;
|
|
66
|
+
const boundary =
|
|
67
|
+
(prev >= 97 && prev <= 122) || // getUser -> get | User
|
|
68
|
+
(prev >= 65 && prev <= 90 && next >= 97 && next <= 122); // HTTPServer -> HTTP | Server
|
|
69
|
+
if (boundary) flush();
|
|
70
|
+
}
|
|
71
|
+
cur += ch;
|
|
72
|
+
}
|
|
73
|
+
flush();
|
|
74
|
+
}
|
|
75
|
+
return out;
|
|
13
76
|
}
|
|
14
77
|
|
|
15
78
|
// Query-side stopwords: bag-of-words scoring has no IDF, so "the/what/how"
|
|
@@ -24,7 +87,8 @@ export const QUERY_STOPWORDS = new Set(
|
|
|
24
87
|
),
|
|
25
88
|
);
|
|
26
89
|
|
|
27
|
-
// A free-text prompt long enough to carry >= this many
|
|
90
|
+
// A free-text prompt long enough to carry >= this many distinct concepts (see
|
|
91
|
+
// queryConcepts: an identifier and its segments count once) must
|
|
28
92
|
// share >= 2 distinct concepts with a memory to inject: with a long query
|
|
29
93
|
// vocabulary, one shared token is near-certain coincidence (a live store hit
|
|
30
94
|
// injected a git memory on the word "should", df=1). A keyword-length query
|
|
@@ -54,6 +118,30 @@ const LONG_QUERY_TOKENS = 3;
|
|
|
54
118
|
// loadAll() result must not be the one place a retired memory leaks back into
|
|
55
119
|
// an injected block (#43 item 1: forbidden hits). The drop also keeps a
|
|
56
120
|
// retired row from diluting the document frequencies the ranking depends on.
|
|
121
|
+
/**
|
|
122
|
+
* The query's distinct concepts. `words()` emits each identifier at two
|
|
123
|
+
* granularities — whole form plus camel/separator segments — and the admission
|
|
124
|
+
* gate must count ONE concept per identifier, not one per token: a query
|
|
125
|
+
* citing `memoryRecallThreshold` is a single concept even though it emits
|
|
126
|
+
* four tokens (review #417: the flat token count raised needShared from 1 to
|
|
127
|
+
* 2 and a row carrying that keyword fused in `kw` stopped matching its own
|
|
128
|
+
* keyword). Identifiers sharing any token — the same concept spelled two ways
|
|
129
|
+
* in one query — merge into one group.
|
|
130
|
+
*/
|
|
131
|
+
function queryConcepts(term: string): string[][] {
|
|
132
|
+
const groups: string[][] = [];
|
|
133
|
+
for (const id of term.match(/[A-Za-z0-9_.\-/]+/g) ?? []) {
|
|
134
|
+
const toks = words(id).filter((t) => !QUERY_STOPWORDS.has(t));
|
|
135
|
+
if (!toks.length) continue;
|
|
136
|
+
let hit = -1;
|
|
137
|
+
for (let g = 0; g < groups.length && hit === -1; g++)
|
|
138
|
+
if (toks.some((t) => groups[g]!.includes(t))) hit = g;
|
|
139
|
+
if (hit === -1) groups.push([...new Set(toks)]);
|
|
140
|
+
else groups[hit] = [...new Set([...groups[hit]!, ...toks])];
|
|
141
|
+
}
|
|
142
|
+
return groups;
|
|
143
|
+
}
|
|
144
|
+
|
|
57
145
|
export function scoreMemories(
|
|
58
146
|
term: string,
|
|
59
147
|
mems: Memory[],
|
|
@@ -61,9 +149,9 @@ export function scoreMemories(
|
|
|
61
149
|
use?: UseSignals,
|
|
62
150
|
): Memory[] {
|
|
63
151
|
const live = mems.filter(isInjectable);
|
|
64
|
-
const
|
|
65
|
-
if (!
|
|
66
|
-
const needShared =
|
|
152
|
+
const concepts = queryConcepts(term);
|
|
153
|
+
if (!concepts.length) return [];
|
|
154
|
+
const needShared = concepts.length >= LONG_QUERY_TOKENS ? 2 : 1;
|
|
67
155
|
const hays = live.map((m) => new Set(words(haystack(m))));
|
|
68
156
|
const df = new Map<string, number>();
|
|
69
157
|
for (const h of hays) for (const t of h) df.set(t, (df.get(t) ?? 0) + 1);
|
|
@@ -72,12 +160,22 @@ export function scoreMemories(
|
|
|
72
160
|
.map((m, i) => {
|
|
73
161
|
let shared = 0;
|
|
74
162
|
let score = 0;
|
|
75
|
-
for (const
|
|
76
|
-
|
|
163
|
+
for (const c of concepts) {
|
|
164
|
+
// A concept contributes at most its best-matching token's idf: the
|
|
165
|
+
// whole form and its segments are one concept, not four independent
|
|
166
|
+
// agreements, so a row cannot out-rank another by spelling the same
|
|
167
|
+
// identifier in more granularities.
|
|
168
|
+
let best = 0;
|
|
169
|
+
for (const w of c)
|
|
170
|
+
if (hays[i]!.has(w)) {
|
|
171
|
+
const d = df.get(w) ?? 0;
|
|
172
|
+
best = Math.max(best, Math.log(1 + (n - d + 0.5) / (d + 0.5)));
|
|
173
|
+
}
|
|
174
|
+
if (best > 0) {
|
|
77
175
|
shared++;
|
|
78
|
-
|
|
79
|
-
score += Math.log(1 + (n - d + 0.5) / (d + 0.5));
|
|
176
|
+
score += best;
|
|
80
177
|
}
|
|
178
|
+
}
|
|
81
179
|
return { m, shared, score };
|
|
82
180
|
})
|
|
83
181
|
.filter((s) => s.shared >= needShared)
|
package/src/tools/recall.ts
CHANGED
|
@@ -32,7 +32,7 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
32
32
|
validated(RecallArgs, {
|
|
33
33
|
name: "memory_recall",
|
|
34
34
|
description:
|
|
35
|
-
"Look up durable project knowledge — build/test/lint commands, decisions, patterns, preferences, blockers — BEFORE searching the repo or reading files. Use for 'how do I run X', conventions, and past decisions. By scope, type, or free-text query; with no filters it returns the most recent memories.",
|
|
35
|
+
"Look up durable project knowledge — build/test/lint commands, decisions, patterns, preferences, blockers — BEFORE searching the repo or reading files. Use for 'how do I run X', conventions, and past decisions. By scope, type, or free-text query; with no filters it returns the most recent memories. Returns at most 5 rows by default, best match first — pass `limit` when you genuinely need a wider sweep.",
|
|
36
36
|
options: direct,
|
|
37
37
|
async execute(args, context): Promise<{ content: string }> {
|
|
38
38
|
const { sid, call } = toolIds(context);
|
|
@@ -52,7 +52,15 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
52
52
|
.sort((a, b) => b.score - a.score)
|
|
53
53
|
.map((x) => x.m);
|
|
54
54
|
}
|
|
55
|
-
|
|
55
|
+
// #409: the default window was 20. A live audit joined 1,090 recall
|
|
56
|
+
// events to their session transcripts: rank-1 rows go on to be
|
|
57
|
+
// referenced at 3.4x the rate of a random store row, ranks 11-20 at
|
|
58
|
+
// 1.16x — noise. Returning 17.7 rows per call meant most of the window
|
|
59
|
+
// was paid for in context and never read. 5 is the last rank where a
|
|
60
|
+
// useful row survives the cut; a caller that wants more still asks.
|
|
61
|
+
// Non-positive or fractional limits are coerced, never trusted: a
|
|
62
|
+
// negative slice(0, n) / slice(-n) silently returns MORE rows, not fewer.
|
|
63
|
+
const limit = args.limit && args.limit > 0 ? Math.floor(args.limit) : 5;
|
|
56
64
|
// #409 dedup lane (RESULTS §17): flag off, this is the same slice as
|
|
57
65
|
// before. Flag on, near-duplicate copies are skipped so the window
|
|
58
66
|
// fills with distinct rows instead of repeats of the top hit.
|