opencode-self-improvement 0.25.0 → 0.26.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 +1 -0
- package/package.json +1 -1
- package/src/recall/dedupe.ts +182 -0
- package/src/tools/recall.ts +6 -1
package/README.md
CHANGED
|
@@ -87,6 +87,7 @@ Zero configuration is the default. To tune, use the object form of the plugin en
|
|
|
87
87
|
| `OPENCODE_MEMORY_ALLOW_OUTSIDE_PATHS` | Operator-only. When truthy, `memory_export`/`memory_import` accept paths anywhere on disk instead of confining themselves to the store. Off by default. See [Export and move between machines](#export-and-move-between-machines). |
|
|
88
88
|
| `OPENCODE_MEMORY_INJECT_BUDGET` | Fallback for `injectBudget`. |
|
|
89
89
|
| `OPENCODE_MEMORY_FRONTIER_BUDGET` | Fallback for `frontierBudget`. |
|
|
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). |
|
|
90
91
|
| `OPENCODE_COMMANDS_DIR` | Where the plugin mirrors its slash commands (default `~/.config/opencode/commands`). |
|
|
91
92
|
| `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). |
|
|
92
93
|
| `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.
|
|
3
|
+
"version": "0.26.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,182 @@
|
|
|
1
|
+
// Near-duplicate suppression inside a returned window (#409, RESULTS §17).
|
|
2
|
+
//
|
|
3
|
+
// The live store carries repeated rows — superseded snapshots, cursor rows that
|
|
4
|
+
// differ only by a timestamp, the same decision restated after a compaction —
|
|
5
|
+
// and the matcher scores them all the same. With a fixed window they crowd out
|
|
6
|
+
// distinct facts, which is what breaks composition: two memories a later task
|
|
7
|
+
// needs together rarely fit side by side because the first one's copies take
|
|
8
|
+
// the slots.
|
|
9
|
+
//
|
|
10
|
+
// The fix is one deterministic pass AFTER ranking: skip any candidate whose
|
|
11
|
+
// content-token Jaccard against an already-kept row of the same scope reaches
|
|
12
|
+
// the threshold, keeping the NEWEST copy of a clash (§17's adoption rule: the
|
|
13
|
+
// live duplicates are stale cursors and superseded snapshots, so the row that
|
|
14
|
+
// survives is the one written last, not the one that ranked highest). No model,
|
|
15
|
+
// no new index, and nothing changes unless the flag is set:
|
|
16
|
+
//
|
|
17
|
+
// OPENCODE_MEMORY_DEDUP=0.8 (unset or 0 = shipped behavior, byte-identical)
|
|
18
|
+
//
|
|
19
|
+
// Threshold guidance from the live replay (§17): 0.5-0.7 suppresses rows that
|
|
20
|
+
// are merely related (92% of them at 0.5) and forces the window deep into the
|
|
21
|
+
// ranking; 0.8 suppresses only near-identical copies. Values below 0.5 are a
|
|
22
|
+
// misconfiguration, not an aggressive setting: at Jaccard 1e-9 a single shared
|
|
23
|
+
// fragment suppressed rows, so anything under 0.5 turns the pass off.
|
|
24
|
+
//
|
|
25
|
+
// WHAT COUNTS AS A NEAR-COPY — the first version's three mistakes, each pinned
|
|
26
|
+
// by a test:
|
|
27
|
+
//
|
|
28
|
+
// 1. It scored `type` and `scope` as tokens. Same-scope rows then shared two
|
|
29
|
+
// free tokens, so short rows looked like copies of each other. Comparison is
|
|
30
|
+
// content-only.
|
|
31
|
+
// 2. It compared whole token sets, so a prohibition read as a copy of the
|
|
32
|
+
// permission it denies: "force push main" inside "never force push main"
|
|
33
|
+
// reaches Jaccard 0.833, and at threshold 0.8 the WRONG copy survived. If
|
|
34
|
+
// exactly one row of a pair carries a negation word, it is not a copy.
|
|
35
|
+
// 3. It had no floor. Non-Latin prose tokenises to nothing under `words()`
|
|
36
|
+
// (which splits on [^a-z0-9_]+), so two unrelated Chinese rows in one scope
|
|
37
|
+
// both reduced to the empty content set — Jaccard over empty sets is
|
|
38
|
+
// defined here as 0, but the pre-floor version scored them on type and
|
|
39
|
+
// scope alone at 1.0 and deleted the second row. A row with fewer than
|
|
40
|
+
// MIN_CONTENT content tokens is never suppressed: there is nothing to
|
|
41
|
+
// compare.
|
|
42
|
+
// 4. It treated differing numeric facts as duplicates. `words()` drops tokens
|
|
43
|
+
// of length <= 2, so "Rate limiter reads Redis DB 3." and "...DB 4." have
|
|
44
|
+
// identical token sets (Jaccard 1) and the older row was suppressed even at
|
|
45
|
+
// threshold 1 (review #415, reproduced through the handler). Numbers are
|
|
46
|
+
// compared as a sorted multiset of literals and a pair that disagrees on
|
|
47
|
+
// them is never a copy.
|
|
48
|
+
import type { Memory } from "../store/model/memory.ts";
|
|
49
|
+
import { haystack, words } from "./score.ts";
|
|
50
|
+
|
|
51
|
+
export const DEDUPE_ENV = "OPENCODE_MEMORY_DEDUP";
|
|
52
|
+
|
|
53
|
+
/** Below this, the value is a misconfiguration and the pass stays off. */
|
|
54
|
+
export const MIN_THRESHOLD = 0.5;
|
|
55
|
+
|
|
56
|
+
/** A row with fewer content tokens than this is never compared (§17 floor). */
|
|
57
|
+
export const MIN_CONTENT = 4;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Tokens that reverse a row's meaning. If exactly one row of a candidate pair
|
|
61
|
+
* carries any of them, the rows disagree, and disagreement is not duplication.
|
|
62
|
+
* Only words that survive `words()` (lower-case, length > 2) are listed;
|
|
63
|
+
* contractions split ("don't" yields "don"), which is why "don" appears.
|
|
64
|
+
*/
|
|
65
|
+
export const POLARITY_WORDS = new Set([
|
|
66
|
+
"never",
|
|
67
|
+
"not",
|
|
68
|
+
"cannot",
|
|
69
|
+
"cant",
|
|
70
|
+
"dont",
|
|
71
|
+
"don",
|
|
72
|
+
"avoid",
|
|
73
|
+
"forbidden",
|
|
74
|
+
"prohibited",
|
|
75
|
+
"banned",
|
|
76
|
+
]);
|
|
77
|
+
|
|
78
|
+
/** Threshold from the environment. Unset, empty, unparsable, < MIN_THRESHOLD or > 1 means off. */
|
|
79
|
+
export function dedupeThreshold(
|
|
80
|
+
env: Record<string, string | undefined> = process.env,
|
|
81
|
+
): number {
|
|
82
|
+
const raw = env[DEDUPE_ENV];
|
|
83
|
+
if (!raw) return 0;
|
|
84
|
+
const n = Number(raw);
|
|
85
|
+
return Number.isFinite(n) && n >= MIN_THRESHOLD && n <= 1 ? n : 0;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function jaccard(a: Set<string>, b: Set<string>): number {
|
|
89
|
+
let inter = 0;
|
|
90
|
+
const small = a.size <= b.size ? a : b;
|
|
91
|
+
const large = small === a ? b : a;
|
|
92
|
+
for (const t of small) if (large.has(t)) inter++;
|
|
93
|
+
const union = a.size + b.size - inter;
|
|
94
|
+
return union ? inter / union : 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The short tokens `words()` drops (chunks of length <= 2). They are where
|
|
99
|
+
* single-digit facts hide: "Rate limiter reads Redis DB 3." and "...DB 4."
|
|
100
|
+
* have identical token sets and reach Jaccard 1 (review #415). Timestamped
|
|
101
|
+
* cursor rows — the rows this pass exists to suppress — keep their digit
|
|
102
|
+
* groups inside longer tokens ("15z"), so comparing the dropped chunks only
|
|
103
|
+
* guards real fact changes without touching the cursor case. A pair whose
|
|
104
|
+
* dropped chunks differ is never a copy.
|
|
105
|
+
*/
|
|
106
|
+
export function droppedChunks(content: string): string[] {
|
|
107
|
+
return content
|
|
108
|
+
.toLowerCase()
|
|
109
|
+
.split(/[^a-z0-9_]+/)
|
|
110
|
+
.filter((t) => t.length > 0 && t.length <= 2)
|
|
111
|
+
.sort();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** True when two rows carry the same short chunks (same multiset). */
|
|
115
|
+
function sameDroppedChunks(a: Memory, b: Memory): boolean {
|
|
116
|
+
const na = droppedChunks(a.content);
|
|
117
|
+
const nb = droppedChunks(b.content);
|
|
118
|
+
if (na.length !== nb.length) return false;
|
|
119
|
+
for (let i = 0; i < na.length; i++) if (na[i] !== nb[i]) return false;
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A row's own tokens: everything except the `type` and `scope` values, which
|
|
125
|
+
* the same-scope requirement already guarantees are shared and which made short
|
|
126
|
+
* unrelated rows look like copies.
|
|
127
|
+
*/
|
|
128
|
+
export function contentTokens(m: Memory): Set<string> {
|
|
129
|
+
const out = new Set(words(haystack(m)));
|
|
130
|
+
out.delete(m.type.toLowerCase());
|
|
131
|
+
out.delete(m.scope.toLowerCase());
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function hasPolarity(tokens: Set<string>): boolean {
|
|
136
|
+
for (const w of tokens) if (POLARITY_WORDS.has(w)) return true;
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Keep at most `limit` rows from `ranked`, in ranking order, skipping a row
|
|
142
|
+
* when it is a near-copy (same scope, content-token Jaccard >= `threshold`) of
|
|
143
|
+
* a row already kept. When a clash happens the NEWER row takes the slot —
|
|
144
|
+
* §17: the duplicates are stale snapshots, so recency decides, not rank. Rows
|
|
145
|
+
* under `MIN_CONTENT` content tokens, pairs that disagree on a negation word,
|
|
146
|
+
* and pairs whose numeric literals differ are never suppressed. `threshold <= 0`
|
|
147
|
+
* returns the plain top-`limit` slice, so the flag-off path costs nothing and
|
|
148
|
+
* cannot reorder.
|
|
149
|
+
*/
|
|
150
|
+
export function dedupeWindow(
|
|
151
|
+
ranked: Memory[],
|
|
152
|
+
limit: number,
|
|
153
|
+
threshold: number,
|
|
154
|
+
): Memory[] {
|
|
155
|
+
if (threshold <= 0 || ranked.length <= 1) return ranked.slice(0, limit);
|
|
156
|
+
const tokenSets = ranked.map(contentTokens);
|
|
157
|
+
const polarity = tokenSets.map(hasPolarity);
|
|
158
|
+
const kept: number[] = [];
|
|
159
|
+
for (let i = 0; i < ranked.length && kept.length < limit; i++) {
|
|
160
|
+
if (tokenSets[i]!.size < MIN_CONTENT) {
|
|
161
|
+
kept.push(i);
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
let clash = -1; // position within `kept`, not the row index
|
|
165
|
+
for (const pos of kept.keys()) {
|
|
166
|
+
const k = kept[pos]!;
|
|
167
|
+
if (
|
|
168
|
+
ranked[k]!.scope === ranked[i]!.scope &&
|
|
169
|
+
tokenSets[k]!.size >= MIN_CONTENT &&
|
|
170
|
+
polarity[k] === polarity[i] &&
|
|
171
|
+
sameDroppedChunks(ranked[k]!, ranked[i]!) &&
|
|
172
|
+
jaccard(tokenSets[i]!, tokenSets[k]!) >= threshold
|
|
173
|
+
) {
|
|
174
|
+
clash = pos;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
if (clash === -1) kept.push(i);
|
|
179
|
+
else if (ranked[i]!.ts > ranked[kept[clash]!]!.ts) kept[clash] = i;
|
|
180
|
+
}
|
|
181
|
+
return kept.map((i) => ranked[i]!);
|
|
182
|
+
}
|
package/src/tools/recall.ts
CHANGED
|
@@ -10,6 +10,7 @@ import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
|
|
|
10
10
|
import type { Memory } from "../store/model/memory.ts";
|
|
11
11
|
import { RecallArgs, validated } from "./schema.ts";
|
|
12
12
|
import { firstFrontierTap, markFrontierTap } from "../capture/triggers.ts";
|
|
13
|
+
import { dedupeThreshold, dedupeWindow } from "../recall/dedupe.ts";
|
|
13
14
|
|
|
14
15
|
function scoreMemory(m: Memory, q: string): number {
|
|
15
16
|
// Same indexed fields as the injection scorer (#40): a row recalled by one of
|
|
@@ -52,8 +53,12 @@ export const register: ToolRegister = (editor, deps) => {
|
|
|
52
53
|
.map((x) => x.m);
|
|
53
54
|
}
|
|
54
55
|
const limit = args.limit || 20;
|
|
56
|
+
// #409 dedup lane (RESULTS §17): flag off, this is the same slice as
|
|
57
|
+
// before. Flag on, near-duplicate copies are skipped so the window
|
|
58
|
+
// fills with distinct rows instead of repeats of the top hit.
|
|
59
|
+
const dedupeAt = dedupeThreshold();
|
|
55
60
|
const limited = args.query
|
|
56
|
-
? results
|
|
61
|
+
? dedupeWindow(results, limit, dedupeAt)
|
|
57
62
|
: results.slice(-limit);
|
|
58
63
|
if (!limited.length) return { content: "No matching memories" };
|
|
59
64
|
const openIn = limited.filter((m) => m.status === "open").length;
|