opencode-self-improvement 0.9.1 → 0.10.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 +21 -13
- package/package.json +1 -1
- package/src/capture.ts +30 -2
- package/src/gates.ts +12 -0
- package/src/index.ts +7 -3
- package/src/inject.ts +42 -8
- package/src/memory.ts +51 -0
- package/src/portable.ts +10 -0
- package/src/schema.ts +14 -0
package/README.md
CHANGED
|
@@ -161,11 +161,14 @@ A deterministic, auditable eval of the **shipped** matcher against classic
|
|
|
161
161
|
BM25, on query classes assigned by *measured* vocabulary overlap with the
|
|
162
162
|
right memory (store of 200; k=3 = the production injection limit):
|
|
163
163
|
|
|
164
|
-
| Query, vs. the stored memory | share | R@1 | R@3 |
|
|
165
|
-
| --- | ---: | ---: | ---: |
|
|
166
|
-
| lexical — reuses your project's words | 46% | 98% | 98% |
|
|
167
|
-
| near — reworded, one overlap | 17% | 28% | 28% |
|
|
168
|
-
| strict — zero shared words | 37% | 0% | 0% |
|
|
164
|
+
| Query, vs. the stored memory | share | R@1 | R@3 | with `kw` R@1 | with `kw` R@3 |
|
|
165
|
+
| --- | ---: | ---: | ---: | ---: | ---: |
|
|
166
|
+
| lexical — reuses your project's words | 46% | 98% | 98% | 98% | 98% |
|
|
167
|
+
| near — reworded, one overlap | 17% | 28% | 28% | 67% | 72% |
|
|
168
|
+
| strict — zero shared words | 37% | 0% | 0% | 26% | 26% |
|
|
169
|
+
|
|
170
|
+
"with `kw`" is the same matcher over the same store, with each row's write-time
|
|
171
|
+
keywords indexed — the only changed variable is the index.
|
|
169
172
|
|
|
170
173
|
What the benchmark bought us:
|
|
171
174
|
|
|
@@ -183,14 +186,19 @@ What the benchmark bought us:
|
|
|
183
186
|
exactly: near-class R@3 83% → 28% (the remainder is keyword-length queries,
|
|
184
187
|
which still inject on one concept) for the 34% → 6% wrong-memory number
|
|
185
188
|
above. A quiet turn beats a confident wrong one.
|
|
186
|
-
- **Strict paraphrase
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
189
|
+
- **Strict paraphrase was the real lexical wall, and write-time keywords
|
|
190
|
+
cracked part of it.** Nothing that reads-only ever crossed it: the matcher
|
|
191
|
+
went *silent* when the vocabulary shared nothing (35 of 39 strict queries
|
|
192
|
+
injected nothing at all), leaving `memory_recall` as the pull path. So the
|
|
193
|
+
fix moved to write time — `memory_remember` and auto-capture ask each row for
|
|
194
|
+
3-5 `kw` aliases (other names, other verb forms, the literal command), which
|
|
195
|
+
are indexed and never rendered. Result: strict R@1 0% → 26% and near R@1 28% →
|
|
196
|
+
67%, top-1 wrong-memory noise unchanged at 6%, abstentions 47 → 30. A
|
|
197
|
+
permutation control (every row given someone else's aliases) scores 29%/35%,
|
|
198
|
+
so the gain is meaning, not extra tokens. The wall is not gone — 26% is a
|
|
199
|
+
third of it, and embeddings remain the only general crossing (the bench
|
|
200
|
+
exposes a scorer seam if you want to measure them; requiring an inference
|
|
201
|
+
engine just to remember things is a non-starter for a zero-dependency plugin).
|
|
194
202
|
- Caveats, honestly: the dataset is synthetic (52 memories / 105 queries, a
|
|
195
203
|
fictional project), its class mix is deliberately adversarial (far richer in
|
|
196
204
|
zero-overlap paraphrases than real prompts), and scoring is single-gold.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-self-improvement",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.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": {
|
package/src/capture.ts
CHANGED
|
@@ -26,6 +26,28 @@ export interface CaptureCandidate {
|
|
|
26
26
|
scope: string;
|
|
27
27
|
content: string;
|
|
28
28
|
status?: "open" | "settled";
|
|
29
|
+
/** Retrieval keywords (#40): indexed by the store, never rendered. */
|
|
30
|
+
kw?: string[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Keywords are the vocabulary bridge for the strict-paraphrase wall, mined from
|
|
34
|
+
// the transcript the model just saw. Cap and shape mirror the store's own
|
|
35
|
+
// limits; the store normalises again at write, so this only has to stay
|
|
36
|
+
// junk-proof (capture.ts never imports the store — it stays dependency-free).
|
|
37
|
+
const CAPTURE_KEYWORD_CAP = 8;
|
|
38
|
+
const CAPTURE_KEYWORD_CHARS = 48;
|
|
39
|
+
|
|
40
|
+
function cleanKeywords(value: unknown): string[] | undefined {
|
|
41
|
+
if (!Array.isArray(value)) return undefined;
|
|
42
|
+
const out: string[] = [];
|
|
43
|
+
for (const item of value) {
|
|
44
|
+
if (typeof item !== "string") continue;
|
|
45
|
+
const k = item.trim().toLowerCase();
|
|
46
|
+
if (!k || k.length > CAPTURE_KEYWORD_CHARS || flattenMemoryText(k) !== k) continue;
|
|
47
|
+
if (!out.includes(k)) out.push(k);
|
|
48
|
+
if (out.length === CAPTURE_KEYWORD_CAP) break;
|
|
49
|
+
}
|
|
50
|
+
return out.length ? out : undefined;
|
|
29
51
|
}
|
|
30
52
|
|
|
31
53
|
// Prompt for the cheap capture model. The output contract is a bare JSON
|
|
@@ -35,13 +57,14 @@ export function buildCapturePrompt(transcript: string, max = 3): string {
|
|
|
35
57
|
"You mine one coding-agent session transcript for durable memories worth keeping across future sessions.",
|
|
36
58
|
"",
|
|
37
59
|
`Output ONLY a JSON array of at most ${max} objects (a fenced json block is fine), each:`,
|
|
38
|
-
'{"type": "...", "scope": "...", "content": "...", "status": "..."}',
|
|
60
|
+
'{"type": "...", "scope": "...", "content": "...", "status": "...", "kw": ["...", "..."]}',
|
|
39
61
|
"",
|
|
40
62
|
"Rules:",
|
|
41
63
|
`- type: one of ${TYPES.join(", ")}.`,
|
|
42
64
|
"- scope: short area name (e.g. auth, api, build).",
|
|
43
65
|
'- status: "open" for a live planning thread; otherwise "settled".',
|
|
44
66
|
"- content: one self-contained fact, 20-600 characters, useful in a NEW session.",
|
|
67
|
+
'- kw: 3-5 search keywords for how a DIFFERENT session would ask for this — synonyms and alternate names, other verb forms, the literal command or file name. They are indexed for retrieval and never shown to a model. Omit (or []) when the content already uses those words.',
|
|
45
68
|
"- Keep: decisions made, working commands, user preferences, blockers hit, durable context, conventions.",
|
|
46
69
|
"- Skip: anything derivable from the code or git history, small talk, and details that only mattered inside this conversation.",
|
|
47
70
|
"- If nothing is worth remembering, output [ ].",
|
|
@@ -112,7 +135,12 @@ export function parseCandidates(raw: string, max: number): CaptureCandidate[] {
|
|
|
112
135
|
if (!VALID_TYPES.has(type)) continue;
|
|
113
136
|
if (content.length < 20 || content.length > 600) continue;
|
|
114
137
|
const status = o.status === "open" || o.status === "settled" ? o.status : undefined;
|
|
115
|
-
|
|
138
|
+
// A bad kw list costs the row its keywords, never the row itself.
|
|
139
|
+
const kw = cleanKeywords(o.kw);
|
|
140
|
+
out.push({
|
|
141
|
+
...(status ? { type, scope, content, status } : { type, scope, content }),
|
|
142
|
+
...(kw ? { kw } : {}),
|
|
143
|
+
});
|
|
116
144
|
}
|
|
117
145
|
return out;
|
|
118
146
|
} catch {
|
package/src/gates.ts
CHANGED
|
@@ -33,6 +33,10 @@ export interface WriteCandidate {
|
|
|
33
33
|
content: string;
|
|
34
34
|
issue?: string;
|
|
35
35
|
tags?: string[];
|
|
36
|
+
// Retrieval keywords (#40) — indexed for recall, never rendered. Normalised
|
|
37
|
+
// (trim/lowercase/cap) at promotion, so a caller may hand junk without
|
|
38
|
+
// changing the verdict the prose deserves.
|
|
39
|
+
kw?: string[];
|
|
36
40
|
status?: "open" | "settled";
|
|
37
41
|
// "global" = cross-project (memory_remember global:true; imports carry a
|
|
38
42
|
// row's origin stamp). Any other value (including undefined) is resolved by
|
|
@@ -242,6 +246,13 @@ export async function runWriteGate(
|
|
|
242
246
|
) {
|
|
243
247
|
badMetadata.push("tags");
|
|
244
248
|
}
|
|
249
|
+
if (
|
|
250
|
+
candidate.kw !== undefined &&
|
|
251
|
+
(!Array.isArray(candidate.kw) ||
|
|
252
|
+
candidate.kw.some((k) => typeof k !== "string" || flattenMemoryText(k) !== k))
|
|
253
|
+
) {
|
|
254
|
+
badMetadata.push("kw");
|
|
255
|
+
}
|
|
245
256
|
if (badMetadata.length) {
|
|
246
257
|
await recordWriteBlocked(4, "invalid_metadata", [`fields=${badMetadata.join(",")}`], chain);
|
|
247
258
|
return {
|
|
@@ -255,6 +266,7 @@ export async function runWriteGate(
|
|
|
255
266
|
const credentialMetadata = [
|
|
256
267
|
containsSecret(candidate.scope) ? "scope" : undefined,
|
|
257
268
|
candidate.tags?.some(containsSecret) ? "tags" : undefined,
|
|
269
|
+
candidate.kw?.some(containsSecret) ? "kw" : undefined,
|
|
258
270
|
containsSecret(candidate.issue) ? "issue" : undefined,
|
|
259
271
|
].filter((field): field is string => field !== undefined);
|
|
260
272
|
if (credentialMetadata.length) {
|
package/src/index.ts
CHANGED
|
@@ -92,7 +92,10 @@ import {
|
|
|
92
92
|
} from "./schema.ts";
|
|
93
93
|
|
|
94
94
|
function scoreMemory(m: Memory, q: string): number {
|
|
95
|
-
|
|
95
|
+
// Same indexed fields as the injection scorer (#40): a row recalled by one of
|
|
96
|
+
// its keywords is the same row recalled by the prose that keyword stands for.
|
|
97
|
+
const kw = m.kw?.length ? ` ${m.kw.join(" ")}` : "";
|
|
98
|
+
const hay = `${m.type} ${m.scope} ${m.content}${kw}`.toLowerCase();
|
|
96
99
|
let score = 0;
|
|
97
100
|
for (const w of q.toLowerCase().split(/\s+/).filter(Boolean)) {
|
|
98
101
|
if (hay.includes(w)) score++;
|
|
@@ -297,7 +300,7 @@ const impl = {
|
|
|
297
300
|
editor.add(validated(RememberArgs, {
|
|
298
301
|
name: "memory_remember",
|
|
299
302
|
description:
|
|
300
|
-
"Store a memory after the 4-gate write pipeline validates it (canonical graph, stale refs, dupes). Blocked writes record evidence instead of persisting.",
|
|
303
|
+
"Store a memory after the 4-gate write pipeline validates it (canonical graph, stale refs, dupes). Blocked writes record evidence instead of persisting. Add `kw` — 3-5 aliases, synonyms, verb forms or the literal command a FUTURE session would search with — to make a row findable when the wording differs; keywords are indexed, never injected.",
|
|
301
304
|
options: direct,
|
|
302
305
|
async execute(args): Promise<{ content: string }> {
|
|
303
306
|
const candidate: WriteCandidate = {
|
|
@@ -306,6 +309,7 @@ const impl = {
|
|
|
306
309
|
content: args.content,
|
|
307
310
|
issue: args.issue,
|
|
308
311
|
tags: args.tags,
|
|
312
|
+
kw: args.kw,
|
|
309
313
|
status: args.status,
|
|
310
314
|
repo: args.global ? "global" : undefined,
|
|
311
315
|
};
|
|
@@ -398,7 +402,7 @@ const impl = {
|
|
|
398
402
|
}
|
|
399
403
|
const target = resolved.target;
|
|
400
404
|
const outcome = await runWriteGate(
|
|
401
|
-
{ type: args.type, scope: args.scope, content: args.content, status: args.status },
|
|
405
|
+
{ type: args.type, scope: args.scope, content: args.content, kw: args.kw, status: args.status },
|
|
402
406
|
gateContext(),
|
|
403
407
|
);
|
|
404
408
|
if (outcome.action === "promoted" && outcome.ts && target) {
|
package/src/inject.ts
CHANGED
|
@@ -99,20 +99,36 @@ const QUERY_STOPWORDS = new Set(
|
|
|
99
99
|
// quiet unless the vocabulary genuinely agrees twice.
|
|
100
100
|
const LONG_QUERY_TOKENS = 3;
|
|
101
101
|
|
|
102
|
-
// Production relevance ranking
|
|
102
|
+
// Production relevance ranking. Candidates must clear the concept-overlap
|
|
103
103
|
// gate, then rank by Robertson-IF of the shared tokens (rare concepts beat
|
|
104
104
|
// piles of common ones — the same idf form the bench's BM25 row uses).
|
|
105
|
+
// Ties break deliberately, in this order: open threads first (a pinned frontier
|
|
106
|
+
// row beats an equal-scoring settled one — that is what the frontier is for),
|
|
107
|
+
// then the newer row (#40 item 2: between two equally relevant facts, the one
|
|
108
|
+
// stating what is true NOW is the better guess; epoch-freeze-safe because ts
|
|
109
|
+
// never changes for a given row).
|
|
110
|
+
// The haystack is `scope + type + content + kw`, where `kw` are the
|
|
111
|
+
// author-supplied aliases indexed at write time (#40 item 3, the vocabulary
|
|
112
|
+
// bridge for the bench's strict-paraphrase wall). Keywords are indexed, never
|
|
113
|
+
// rendered, so they cannot change what a block costs.
|
|
105
114
|
// Exported for bench/recall-quality.ts — the benchmark measures the shipped
|
|
106
115
|
// matcher.
|
|
116
|
+
//
|
|
117
|
+
// Superseded rows are dropped HERE, not only by the callers. Every model-facing
|
|
118
|
+
// sink already passes isActive() rows, but a scorer that can be handed a raw
|
|
119
|
+
// loadAll() result must not be the one place a retired memory leaks back into
|
|
120
|
+
// an injected block (#43 item 1: forbidden hits). The drop also keeps a
|
|
121
|
+
// retired row from diluting the document frequencies the ranking depends on.
|
|
107
122
|
export function scoreMemories(term: string, mems: Memory[], limit: number): Memory[] {
|
|
123
|
+
const live = mems.filter((m) => !m.superseded_by);
|
|
108
124
|
const query = new Set(words(term).filter((t) => !QUERY_STOPWORDS.has(t)));
|
|
109
125
|
if (!query.size) return [];
|
|
110
126
|
const needShared = query.size >= LONG_QUERY_TOKENS ? 2 : 1;
|
|
111
|
-
const hays =
|
|
127
|
+
const hays = live.map((m) => new Set(words(haystack(m))));
|
|
112
128
|
const df = new Map<string, number>();
|
|
113
129
|
for (const h of hays) for (const t of h) df.set(t, (df.get(t) ?? 0) + 1);
|
|
114
|
-
const n =
|
|
115
|
-
return
|
|
130
|
+
const n = live.length || 1;
|
|
131
|
+
return live
|
|
116
132
|
.map((m, i) => {
|
|
117
133
|
let shared = 0;
|
|
118
134
|
let score = 0;
|
|
@@ -125,11 +141,29 @@ export function scoreMemories(term: string, mems: Memory[], limit: number): Memo
|
|
|
125
141
|
return { m, shared, score };
|
|
126
142
|
})
|
|
127
143
|
.filter((s) => s.shared >= needShared)
|
|
128
|
-
.sort((a, b) => b.score - a.score || openRank(b.m) - openRank(a.m))
|
|
144
|
+
.sort((a, b) => b.score - a.score || openRank(b.m) - openRank(a.m) || cmpTs(b.m.ts, a.m.ts))
|
|
129
145
|
.slice(0, limit)
|
|
130
146
|
.map((s) => s.m);
|
|
131
147
|
}
|
|
132
148
|
|
|
149
|
+
// What a row is matched against. Kept in one place so the bench's arms and
|
|
150
|
+
// production can never disagree about the indexed fields.
|
|
151
|
+
export function haystack(m: Memory): string {
|
|
152
|
+
return `${m.scope} ${m.type} ${m.content}${m.kw?.length ? ` ${m.kw.join(" ")}` : ""}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Numeric compare, with the string compare kept as fallback for non-dates
|
|
156
|
+
// (the ledger hands "" when a row was never used). ISO strings agree with
|
|
157
|
+
// Date.parse only while the FORMAT agrees: a legacy second-precision stamp
|
|
158
|
+
// ("…:01Z") and a millisecond stamp ("…:01.000Z") for the same instant order
|
|
159
|
+
// wrong as strings, which would silently mis-rank the recency tie-break.
|
|
160
|
+
function cmpTs(a: string, b: string): number {
|
|
161
|
+
const na = Date.parse(a);
|
|
162
|
+
const nb = Date.parse(b);
|
|
163
|
+
if (!Number.isNaN(na) && !Number.isNaN(nb)) return na < nb ? -1 : na > nb ? 1 : 0;
|
|
164
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
165
|
+
}
|
|
166
|
+
|
|
133
167
|
function openRank(m: Memory): number {
|
|
134
168
|
return m.status === "open" ? 1 : 0;
|
|
135
169
|
}
|
|
@@ -202,7 +236,7 @@ export async function systemDigest(
|
|
|
202
236
|
|
|
203
237
|
const open = mems
|
|
204
238
|
.filter((m) => m.status === "open")
|
|
205
|
-
.sort((a, b) => (
|
|
239
|
+
.sort((a, b) => cmpTs(b.ts, a.ts))
|
|
206
240
|
.slice(0, OPEN_THREAD_LIMIT);
|
|
207
241
|
if (!open.length) return;
|
|
208
242
|
|
|
@@ -286,7 +320,7 @@ export async function storeVersion(rows?: Memory[]): Promise<string> {
|
|
|
286
320
|
let max = "";
|
|
287
321
|
const hash = createHash("sha256");
|
|
288
322
|
for (const m of mems) {
|
|
289
|
-
if (m.ts >
|
|
323
|
+
if (cmpTs(m.ts, max) > 0) max = m.ts;
|
|
290
324
|
hash.update(JSON.stringify([m.ts, formatMemory(m)]));
|
|
291
325
|
hash.update("\n");
|
|
292
326
|
}
|
|
@@ -328,7 +362,7 @@ export async function compactionAnchor(limit = 12): Promise<string> {
|
|
|
328
362
|
// Repo-scoped: model-facing reads see this repo + global rows.
|
|
329
363
|
const mems = visibleHere((await loadAll()).filter(isActive));
|
|
330
364
|
if (!mems.length) return "";
|
|
331
|
-
const byNewest = (a: Memory, b: Memory) => (
|
|
365
|
+
const byNewest = (a: Memory, b: Memory) => cmpTs(b.ts, a.ts);
|
|
332
366
|
const open = mems
|
|
333
367
|
.filter((m) => m.status === "open")
|
|
334
368
|
.sort(byNewest)
|
package/src/memory.ts
CHANGED
|
@@ -24,6 +24,37 @@ export type MemoryStatus = "open" | "settled";
|
|
|
24
24
|
// absolutely in appendMemory (throws, covering gate-bypassing callers).
|
|
25
25
|
export const MAX_CONTENT_CHARS = 4000;
|
|
26
26
|
|
|
27
|
+
/** How many retrieval keywords a row may carry, and how long each one may be. */
|
|
28
|
+
export const MAX_KEYWORDS = 8;
|
|
29
|
+
export const MAX_KEYWORD_CHARS = 48;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Normalise author-supplied retrieval keywords: trim, lowercase, split on the
|
|
33
|
+
* storage delimiter, de-duplicate, drop empties and over-long entries, cap the
|
|
34
|
+
* count. TOTAL on purpose — junk yields a shorter list rather than a throw,
|
|
35
|
+
* because keywords only ever widen recall and must never be able to fail a
|
|
36
|
+
* write the prose alone deserved.
|
|
37
|
+
*/
|
|
38
|
+
export function normalizeKeywords(kw: unknown): string[] | undefined {
|
|
39
|
+
if (!Array.isArray(kw)) return undefined;
|
|
40
|
+
const out: string[] = [];
|
|
41
|
+
for (const item of kw) {
|
|
42
|
+
if (typeof item !== "string") continue;
|
|
43
|
+
// Commas are the storage delimiter (kw serialises as one comma-joined
|
|
44
|
+
// scalar), so an item carrying one would read back fragmented. Treat them
|
|
45
|
+
// as separators: "build, test" becomes two aliases, never a " test".
|
|
46
|
+
for (const part of item.split(",")) {
|
|
47
|
+
const k = part.trim().toLowerCase();
|
|
48
|
+
if (!k || k.length > MAX_KEYWORD_CHARS) continue;
|
|
49
|
+
if (!out.includes(k)) out.push(k);
|
|
50
|
+
if (out.length === MAX_KEYWORDS) break;
|
|
51
|
+
}
|
|
52
|
+
if (out.length === MAX_KEYWORDS) break;
|
|
53
|
+
}
|
|
54
|
+
return out.length ? out : undefined;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
27
58
|
export interface Memory {
|
|
28
59
|
ts: string;
|
|
29
60
|
type: string;
|
|
@@ -31,6 +62,13 @@ export interface Memory {
|
|
|
31
62
|
content: string;
|
|
32
63
|
issue?: string;
|
|
33
64
|
tags?: string[];
|
|
65
|
+
// Retrieval keywords (#40): author-supplied aliases, verb forms and synonyms
|
|
66
|
+
// for this row, written at capture time. They widen the scoring haystack
|
|
67
|
+
// ONLY — a row is never rendered through them, so injection bytes and the
|
|
68
|
+
// content cap stay exactly what the prose says. This is the zero-embedding
|
|
69
|
+
// answer to the bench's strict-paraphrase wall (0% R@1: the user's words are
|
|
70
|
+
// not the memory's words).
|
|
71
|
+
kw?: string[];
|
|
34
72
|
superseded_by?: string;
|
|
35
73
|
reason?: string;
|
|
36
74
|
status?: MemoryStatus;
|
|
@@ -88,6 +126,9 @@ function toMemory(fields: Record<string, unknown>, file: string, index: number):
|
|
|
88
126
|
content: typeof fields.content === "string" ? fields.content : "",
|
|
89
127
|
issue: typeof fields.issue === "string" ? fields.issue : undefined,
|
|
90
128
|
tags: typeof fields.tags === "string" ? fields.tags.split(",") : undefined,
|
|
129
|
+
kw: typeof fields.kw === "string" && fields.kw
|
|
130
|
+
? fields.kw.split(",").map((k) => k.trim()).filter((k) => k.length > 0)
|
|
131
|
+
: undefined,
|
|
91
132
|
superseded_by: typeof fields.superseded_by === "string" ? fields.superseded_by : undefined,
|
|
92
133
|
reason: typeof fields.reason === "string" ? fields.reason : undefined,
|
|
93
134
|
status:
|
|
@@ -139,6 +180,8 @@ export interface AppendMemoryInput {
|
|
|
139
180
|
content: string;
|
|
140
181
|
issue?: string;
|
|
141
182
|
tags?: string[];
|
|
183
|
+
/** Retrieval keywords (#40); normalised and capped by appendMemory. */
|
|
184
|
+
kw?: string[];
|
|
142
185
|
status?: MemoryStatus;
|
|
143
186
|
// "global" for explicit cross-project writes; imports may carry a validated
|
|
144
187
|
// same-repo origin stamp (see portable.ts). Any missing value is resolved by
|
|
@@ -182,9 +225,15 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
182
225
|
if (input.tags?.some((tag) => flattenMemoryText(tag) !== tag)) {
|
|
183
226
|
throw new Error("memory tags must not contain control characters");
|
|
184
227
|
}
|
|
228
|
+
// Keywords are indexed, never rendered, but they still live on the row's
|
|
229
|
+
// single line, so the same control-character floor applies.
|
|
230
|
+
if (input.kw?.some((k) => typeof k !== "string" || flattenMemoryText(k) !== k)) {
|
|
231
|
+
throw new Error("memory keywords must not contain control characters");
|
|
232
|
+
}
|
|
185
233
|
const credentialMetadata = [
|
|
186
234
|
containsSecret(input.scope) ? "scope" : undefined,
|
|
187
235
|
input.tags?.some(containsSecret) ? "tags" : undefined,
|
|
236
|
+
input.kw?.some(containsSecret) ? "kw" : undefined,
|
|
188
237
|
containsSecret(input.issue) ? "issue" : undefined,
|
|
189
238
|
].filter((field): field is string => field !== undefined);
|
|
190
239
|
if (credentialMetadata.length) {
|
|
@@ -221,6 +270,7 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
221
270
|
while (taken.has(ts) && guard++ < 1000) {
|
|
222
271
|
ts = new Date(new Date(ts).getTime() + 1).toISOString();
|
|
223
272
|
}
|
|
273
|
+
const kw = normalizeKeywords(input.kw);
|
|
224
274
|
await appendLog(
|
|
225
275
|
file,
|
|
226
276
|
formatLine({
|
|
@@ -230,6 +280,7 @@ export async function appendMemory(input: AppendMemoryInput): Promise<string> {
|
|
|
230
280
|
content,
|
|
231
281
|
issue: input.issue,
|
|
232
282
|
tags: input.tags?.length ? input.tags.join(",") : undefined,
|
|
283
|
+
kw: kw?.join(","),
|
|
233
284
|
status: input.status,
|
|
234
285
|
repo,
|
|
235
286
|
repoPath,
|
package/src/portable.ts
CHANGED
|
@@ -64,6 +64,7 @@ export async function exportMemories(opts: ExportOptions = {}): Promise<ExportRe
|
|
|
64
64
|
content: m.content,
|
|
65
65
|
status: m.status,
|
|
66
66
|
tags: m.tags,
|
|
67
|
+
kw: m.kw,
|
|
67
68
|
issue: m.issue,
|
|
68
69
|
// Origin stamps ride along so a same-repo machine hop can retain them.
|
|
69
70
|
// Import validates them against its current repo before promotion.
|
|
@@ -124,9 +125,18 @@ function parseRow(line: string): WriteCandidate | null {
|
|
|
124
125
|
r.tags.some((t) => typeof t === "string" && flattenMemoryText(t) !== t)
|
|
125
126
|
) return null;
|
|
126
127
|
if (typeof r.issue === "string" && flattenMemoryText(r.issue) !== r.issue) return null;
|
|
128
|
+
// Keywords widen recall only, so a malformed entry costs them, never the row
|
|
129
|
+
// (contrast scope/content, where a bad value makes the line malformed).
|
|
130
|
+
const kw = Array.isArray(r.kw)
|
|
131
|
+
? r.kw.filter(
|
|
132
|
+
(k): k is string =>
|
|
133
|
+
typeof k === "string" && !!k.trim() && flattenMemoryText(k.trim()) === k.trim(),
|
|
134
|
+
)
|
|
135
|
+
: undefined;
|
|
127
136
|
const c: WriteCandidate = { type: r.type, scope: r.scope, content: r.content };
|
|
128
137
|
if (r.status === "open" || r.status === "settled") c.status = r.status;
|
|
129
138
|
if (Array.isArray(r.tags) && r.tags.every((t) => typeof t === "string")) c.tags = r.tags;
|
|
139
|
+
if (kw?.length) c.kw = kw;
|
|
130
140
|
if (typeof r.issue === "string" && r.issue) c.issue = r.issue;
|
|
131
141
|
// Origin stamps survive an export/import hop (repokey.ts): a row written
|
|
132
142
|
// for repo X stays visible only where repo X is opened, even on a new
|
package/src/schema.ts
CHANGED
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
import { z, type ZodTypeAny } from "zod";
|
|
18
18
|
import { STATUSES, TYPES } from "./types.ts";
|
|
19
19
|
import { flattenMemoryText } from "./sanitize.ts";
|
|
20
|
+
import { MAX_KEYWORDS } from "./memory.ts";
|
|
20
21
|
|
|
21
22
|
type JSONSchema = Record<string, unknown>;
|
|
22
23
|
|
|
@@ -32,6 +33,17 @@ const memoryField = z.string().refine((value) => flattenMemoryText(value) === va
|
|
|
32
33
|
message: "must not contain control characters",
|
|
33
34
|
});
|
|
34
35
|
|
|
36
|
+
// Retrieval keywords (#40). The shape cap mirrors MAX_KEYWORDS in memory.ts,
|
|
37
|
+
// which is the authority at write time (it also lowercases, de-dupes and drops
|
|
38
|
+
// over-long entries rather than rejecting the write).
|
|
39
|
+
const keywordList = z
|
|
40
|
+
.array(memoryField)
|
|
41
|
+
.max(MAX_KEYWORDS)
|
|
42
|
+
.describe(
|
|
43
|
+
"3-5 keywords for how a FUTURE session would search for this row: aliases and alternate names, verb forms, synonyms of the words you used, the literal command or file name. Indexed for matching only — never shown in an injected block. Skip it when the prose already says it.",
|
|
44
|
+
)
|
|
45
|
+
.optional();
|
|
46
|
+
|
|
35
47
|
// ---- tool argument schemas (JSON Schema generated from these) ------------
|
|
36
48
|
|
|
37
49
|
export const RememberArgs = z.object({
|
|
@@ -40,6 +52,7 @@ export const RememberArgs = z.object({
|
|
|
40
52
|
content: z.string().describe("The memory content"),
|
|
41
53
|
issue: memoryField.describe("Related issue").optional(),
|
|
42
54
|
tags: z.array(memoryField).optional(),
|
|
55
|
+
kw: keywordList,
|
|
43
56
|
status: z
|
|
44
57
|
.enum(STATUSES)
|
|
45
58
|
.describe(
|
|
@@ -73,6 +86,7 @@ export const UpdateArgs = z.object({
|
|
|
73
86
|
scope: memoryField,
|
|
74
87
|
type: z.enum(TYPES),
|
|
75
88
|
content: z.string(),
|
|
89
|
+
kw: keywordList,
|
|
76
90
|
query: z
|
|
77
91
|
.string()
|
|
78
92
|
.describe(
|