opencode-self-improvement 0.33.8 → 0.33.9
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/package.json +1 -1
- package/src/recall/ambiguous.ts +133 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-self-improvement",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.9",
|
|
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/ambiguous.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
import type { Memory } from "../store/model/memory.ts";
|
|
12
12
|
import { flattenMemoryText } from "../store/sanitize.ts";
|
|
13
13
|
import { contentTokens } from "./dedupe.ts";
|
|
14
|
+
import { cmpTs } from "../core/cmp-ts.ts";
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Cap on each token list. A pair that reaches the threshold while differing
|
|
@@ -25,6 +26,89 @@ function list(tokens: string[]): string {
|
|
|
25
26
|
return `${shown.join(", ")}${held > 0 ? ` (+${held} more)` : ""}`;
|
|
26
27
|
}
|
|
27
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Dropped tokens that share the SAME set of older source rows, so a token several
|
|
31
|
+
* rows carried prints once beside all of them and a token unique to one row never
|
|
32
|
+
* rides along claiming a row that never held it. The sources are per group, not
|
|
33
|
+
* pooled across the family: in a three-member group the newest row can drop one
|
|
34
|
+
* token from two older rows and a different token from only one of them, and a
|
|
35
|
+
* single shared source list would assert the second token came from a row that
|
|
36
|
+
* never had it.
|
|
37
|
+
*/
|
|
38
|
+
interface DropGroup {
|
|
39
|
+
/** Member indexes (into `members`) that held these tokens and newest lacks. */
|
|
40
|
+
sources: number[];
|
|
41
|
+
/** Tokens those rows carried and the newest row dropped. */
|
|
42
|
+
tokens: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Whether the newest member of a family drops tokens an older member carries.
|
|
47
|
+
*
|
|
48
|
+
* Containment alone cannot tell the two situations apart: "the longer row adds a
|
|
49
|
+
* second case" and "the newer row corrected the older one" produce the same pair
|
|
50
|
+
* of token sets, and the marker used to resolve that in one direction for both.
|
|
51
|
+
* Recency does discriminate. A newer row that is wholly contained in an older
|
|
52
|
+
* one removed something, and a removal is exactly what a correction looks like.
|
|
53
|
+
*
|
|
54
|
+
* `sets` are the CONTENT-only token sets, not `contentTokens`: this sentence
|
|
55
|
+
* claims a token was dropped from text the reader is looking at, and the recall
|
|
56
|
+
* renderer prints `content` but never `kw`, so a keyword-only difference would
|
|
57
|
+
* assert a removal of a token that appears in neither rendered row.
|
|
58
|
+
*
|
|
59
|
+
* Returns null when the newest row cannot be identified — a family sharing one
|
|
60
|
+
* timestamp carries no direction, and guessing would be worse than the neutral
|
|
61
|
+
* instruction — or when no older row holds a content token the newest row lacks.
|
|
62
|
+
*/
|
|
63
|
+
function droppedByNewest(
|
|
64
|
+
members: Memory[],
|
|
65
|
+
sets: Set<string>[],
|
|
66
|
+
): { newest: number; groups: DropGroup[] } | null {
|
|
67
|
+
let newest = 0;
|
|
68
|
+
for (let i = 1; i < members.length; i++)
|
|
69
|
+
if (cmpTs(members[i]!.ts, members[newest]!.ts) > 0) newest = i;
|
|
70
|
+
if (
|
|
71
|
+
members.some(
|
|
72
|
+
(m, i) => i !== newest && cmpTs(m.ts, members[newest]!.ts) === 0,
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
return null;
|
|
76
|
+
const now = sets[newest]!;
|
|
77
|
+
// First pass: which older rows held each token the newest row dropped.
|
|
78
|
+
const order: string[] = [];
|
|
79
|
+
const byToken = new Map<string, number[]>();
|
|
80
|
+
sets.forEach((o, i) => {
|
|
81
|
+
if (i === newest || o.size <= now.size) return;
|
|
82
|
+
if (![...now].every((t) => o.has(t))) return;
|
|
83
|
+
for (const t of o) {
|
|
84
|
+
if (now.has(t)) continue;
|
|
85
|
+
let srcs = byToken.get(t);
|
|
86
|
+
if (!srcs) {
|
|
87
|
+
srcs = [];
|
|
88
|
+
byToken.set(t, srcs);
|
|
89
|
+
order.push(t);
|
|
90
|
+
}
|
|
91
|
+
if (!srcs.includes(i)) srcs.push(i);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
// Second pass: collapse tokens that share a source set into one group, keeping
|
|
95
|
+
// first-seen order so the sentence and the cap stay stable.
|
|
96
|
+
const groups: DropGroup[] = [];
|
|
97
|
+
const at = new Map<string, number>();
|
|
98
|
+
for (const t of order) {
|
|
99
|
+
const srcs = byToken.get(t)!;
|
|
100
|
+
const key = srcs.join(",");
|
|
101
|
+
let gi = at.get(key);
|
|
102
|
+
if (gi === undefined) {
|
|
103
|
+
gi = groups.length;
|
|
104
|
+
at.set(key, gi);
|
|
105
|
+
groups.push({ sources: srcs, tokens: [] });
|
|
106
|
+
}
|
|
107
|
+
groups[gi]!.tokens.push(t);
|
|
108
|
+
}
|
|
109
|
+
return groups.length ? { newest, groups } : null;
|
|
110
|
+
}
|
|
111
|
+
|
|
28
112
|
/**
|
|
29
113
|
* One line for one additive family, naming each member by its 1-based position
|
|
30
114
|
* in the returned window. A family is however many rows the pass kept together,
|
|
@@ -36,6 +120,13 @@ function list(tokens: string[]): string {
|
|
|
36
120
|
*/
|
|
37
121
|
export function ambiguousLine(members: Memory[], positions: number[]): string {
|
|
38
122
|
const sets = members.map((m) => contentTokens(m));
|
|
123
|
+
// The drop claim is drawn from CONTENT only. The clash test above may use
|
|
124
|
+
// keywords, but this sentence describes text the reader sees, and the recall
|
|
125
|
+
// renderer prints `content` and never `kw` — a keyword-only difference would
|
|
126
|
+
// assert the removal of a token that appears in neither rendered row.
|
|
127
|
+
const contentSets = members.map((m) =>
|
|
128
|
+
contentTokens({ ...m, kw: undefined }),
|
|
129
|
+
);
|
|
39
130
|
// Each side lists what THAT member has that none of the others have, so a
|
|
40
131
|
// nested chain prints one side rather than one per member: every row but the
|
|
41
132
|
// largest is wholly contained in the union of the rest. The line therefore
|
|
@@ -49,18 +140,54 @@ export function ambiguousLine(members: Memory[], positions: number[]): string {
|
|
|
49
140
|
const only = [...t].filter((tok) => !others.has(tok));
|
|
50
141
|
if (only.length) sides.push(`only in ${positions[i]}: ${list(only)}`);
|
|
51
142
|
});
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
? `
|
|
55
|
-
: `rows ${
|
|
143
|
+
const rowsLabel = (idx: number[]): string =>
|
|
144
|
+
idx.length === 1
|
|
145
|
+
? `row ${positions[idx[0]!]}`
|
|
146
|
+
: `rows ${idx
|
|
147
|
+
.slice(0, -1)
|
|
148
|
+
.map((i) => positions[i])
|
|
149
|
+
.join(", ")} and ${positions[idx[idx.length - 1]!]}`;
|
|
150
|
+
const where = rowsLabel(positions.map((_, i) => i));
|
|
56
151
|
const rest =
|
|
57
152
|
positions.length === 2
|
|
58
153
|
? "neither restates the other"
|
|
59
154
|
: "none restates the others";
|
|
155
|
+
// Two readings fit the same token sets, and they need opposite instructions.
|
|
156
|
+
// When the newest row is the one that left something out, saying "neither
|
|
157
|
+
// supersedes the other" states as fact the one thing the text cannot
|
|
158
|
+
// establish, so the line names the removal and leaves the verdict open.
|
|
159
|
+
const drop = droppedByNewest(members, contentSets);
|
|
160
|
+
let action: string;
|
|
161
|
+
if (drop) {
|
|
162
|
+
// One clause per source group, capped across the whole family. With a single
|
|
163
|
+
// source this collapses to the original "row 2 drops python from row 1" (and
|
|
164
|
+
// the same list(...) form for several tokens), which the reproducer pins.
|
|
165
|
+
let budget = MAX_MARKER_TOKENS;
|
|
166
|
+
let held = 0;
|
|
167
|
+
const clauses: string[] = [];
|
|
168
|
+
for (const g of drop.groups) {
|
|
169
|
+
if (budget <= 0) {
|
|
170
|
+
held += g.tokens.length;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const shown = g.tokens.slice(0, budget);
|
|
174
|
+
budget -= shown.length;
|
|
175
|
+
held += g.tokens.length - shown.length;
|
|
176
|
+
clauses.push(`${list(shown)} from ${rowsLabel(g.sources)}`);
|
|
177
|
+
}
|
|
178
|
+
action =
|
|
179
|
+
`Action: ${rowsLabel([drop.newest])} drops ${clauses.join(" and ")}` +
|
|
180
|
+
(held > 0 ? ` (+${held} more)` : "") +
|
|
181
|
+
` — that may be a correction rather than a narrower case. Verify which ` +
|
|
182
|
+
`before relying on either; both stay available.`;
|
|
183
|
+
} else {
|
|
184
|
+
action =
|
|
185
|
+
`Action: neither supersedes the other. Pick by what you are actually doing; ` +
|
|
186
|
+
`if both apply, use both.`;
|
|
187
|
+
}
|
|
60
188
|
return (
|
|
61
189
|
`AMBIGUOUS (${flattenMemoryText(members[0]!.scope)}): ${where} share ` +
|
|
62
190
|
`shape but ${rest}${sides.length ? ` — ${sides.join("; ")}` : ""}. ` +
|
|
63
|
-
|
|
64
|
-
`if both apply, use both.`
|
|
191
|
+
action
|
|
65
192
|
);
|
|
66
193
|
}
|