opencode-self-improvement 0.18.0 → 0.19.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.18.0",
3
+ "version": "0.19.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": {
@@ -39,8 +39,10 @@ export function buildCapturePrompt(
39
39
  transcript: string,
40
40
  max = 3,
41
41
  userProse = "",
42
+ known = "",
42
43
  ): string {
43
44
  const prose = redactSecrets(userProse).trim();
45
+ const knownList = redactSecrets(known).trim();
44
46
  return [
45
47
  "You mine one coding-agent session transcript for durable memories worth keeping across future sessions.",
46
48
  "",
@@ -54,8 +56,15 @@ export function buildCapturePrompt(
54
56
  "- content: one self-contained fact, 20-600 characters, useful in a NEW session.",
55
57
  "- 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.",
56
58
  "- guided: true ONLY when the user STATED this fact in their own words — a value, rate, number, rule or correction that appears in a user: line or the REQUESTS block. Omit guided for anything the agent discovered by itself.",
59
+ "- A guided row states the user's value or rule and NOTHING more: never claim what hidden tests, graders or suites 'encode' — you cannot see them, and the claim makes the row impossible to check.",
57
60
  "- Keep: decisions made, working commands, user preferences, blockers hit, durable context, conventions.",
58
61
  "- Skip: anything derivable from the code or git history, small talk, and details that only mattered inside this conversation.",
62
+ ...(knownList
63
+ ? [
64
+ "- Already stored for this repo — do NOT output these or restatements of them:",
65
+ knownList,
66
+ ]
67
+ : []),
59
68
  ...(prose
60
69
  ? [
61
70
  // The brief is its own block, so the rule about it is its own too.
@@ -13,6 +13,15 @@ import { lagSince } from "./lag.ts";
13
13
  import { buildCapturePrompt } from "./prompt.ts";
14
14
  import { type SessionModelRef, sessionModel } from "./session-model.ts";
15
15
  import { flattenTranscript, splitTranscript } from "./transcript.ts";
16
+ import { loadAll } from "../store/files/load.ts";
17
+ import { isInjectable } from "../store/model/predicates.ts";
18
+ import {
19
+ readerFromSid,
20
+ visibleTo,
21
+ type Reader,
22
+ } from "../store/model/visibility.ts";
23
+ import { flattenMemoryText } from "../store/sanitize.ts";
24
+ import { cmpTs } from "../core/cmp-ts.ts";
16
25
  import { AUTO_CAPTURE_TAG, USER_GUIDED_TAG } from "../core/tags.ts";
17
26
 
18
27
  interface CaptureHost {
@@ -43,6 +52,34 @@ interface CaptureDependencies {
43
52
  // distinction the auto_capture_compaction_* evidence lines carry.
44
53
  const CAPTURE_COMPACT_TAG = "auto_capture_compaction";
45
54
 
55
+ async function knownRowsFor(reader: Reader): Promise<string> {
56
+ try {
57
+ const rows = visibleTo(await loadAll(), reader)
58
+ .filter(isInjectable)
59
+ .sort((a, b) => cmpTs(b.ts, a.ts))
60
+ .slice(0, 15);
61
+ const lines: string[] = [];
62
+ let length = 0;
63
+ for (const row of rows) {
64
+ // Scrub complete legacy values before cropping, or a token prefix can
65
+ // fall outside the window and leave an unrecognizable secret fragment.
66
+ const scope = flattenMemoryText(redactSecrets(row.scope));
67
+ const content = flattenMemoryText(redactSecrets(row.content)).slice(
68
+ 0,
69
+ 90,
70
+ );
71
+ const line = `- ${scope}: ${content}`;
72
+ if (length + line.length > 1400) break;
73
+ lines.push(line);
74
+ length += line.length + 1;
75
+ }
76
+ return lines.join("\n");
77
+ } catch {
78
+ // The write gate still deduplicates when this optional context is absent.
79
+ return "";
80
+ }
81
+ }
82
+
46
83
  // Auto-capture cooldown, shared by EVERY setup() registration (#108): setup
47
84
  // runs for more than one location, and per-closure cooldown maps made those
48
85
  // registrations strangers - one compaction could pay for two model calls on
@@ -215,6 +252,7 @@ export function configureAutoCapture(
215
252
  gate?: GateContext,
216
253
  ): Promise<void> {
217
254
  const captureGate = gate ?? bindCapture(sid);
255
+ const captureReader = captureGate.reader ?? readerFromSid(sid);
218
256
  // #139: resolve the model BEFORE any skip so every capture evidence
219
257
  // line below can carry model= (cost attributable to a named model —
220
258
  // the real answer to the cost-surprise concern that justified the
@@ -304,6 +342,7 @@ export function configureAutoCapture(
304
342
  redactSecrets(work).slice(-7000),
305
343
  captureMax,
306
344
  redactSecrets(userProse).slice(-1500),
345
+ await knownRowsFor(captureReader),
307
346
  ),
308
347
  });
309
348
  const text = typeof out === "string" ? out : (out?.text ?? "");
@@ -45,7 +45,8 @@ export function createInjectionEpochs({
45
45
  // Per-session frozen injection epochs: the whole memory section (digest +
46
46
  // frontier + relevance) plus the store fingerprint it was rendered from.
47
47
  // Recomputed on the first prompt of a tracked session, when injectable rows
48
- // change, or after a compaction (see the prompt/context/compaction hooks).
48
+ // change, when a new prompt re-scores the block (#335), or after a
49
+ // compaction (see the prompt/context/compaction hooks).
49
50
  // The RENDERED BYTES, not the fingerprint, decide whether the epoch moved
50
51
  // (issue #162): a write that changes nothing the model is shown must not
51
52
  // cost the next request a full re-prefill. And a pure addition the session
@@ -77,7 +78,7 @@ export function createInjectionEpochs({
77
78
  */
78
79
  prompt: string;
79
80
  at: string;
80
- reason: "first" | "store_changed";
81
+ reason: "first" | "store_changed" | "prompt_reveal";
81
82
  refs: MemoryRefs;
82
83
  }
83
84
  >();
@@ -201,17 +202,26 @@ export function createInjectionEpochs({
201
202
  const mems = rows ?? (await injectableRows(sid));
202
203
  const version = await storeVersion(mems);
203
204
  const cached = epoch.get(sid);
204
- // Cheap pre-check: no injectable row moved, so the frozen bytes still
205
- // stand. Skips re-scoring and the use-ledger write on ordinary turns.
206
- if (cached?.version === version) return;
205
+ const prompt = latestPrompt.get(sid) ?? "";
206
+ // Cheap pre-check: no injectable row moved AND the frozen bytes were
207
+ // already scored against THIS prompt. Skips re-scoring and the use-ledger
208
+ // write on ordinary turns and on every tool-loop request within a turn.
209
+ // A new prompt does re-score (#335, the epoch-freeze flaw): a block
210
+ // frozen at the first request carried the first topic's rows for the
211
+ // whole session and never picked up the rows a later prompt genuinely
212
+ // matches. Cache is still safe — the epoch only MOVES when the rendered
213
+ // bytes actually change, and a prompt change that keeps the same rows
214
+ // adopts silently below.
215
+ if (cached?.version === version && cached.prompt === prompt) return;
207
216
  const built = await buildMemorySection(sid, mems);
208
- // The store moved. A write that changes nothing RENDERED — a row that is
209
- // not relevant to this prompt, supersession elsewhere, a scope the reader
210
- // never sees rendered identically — must not cost this session a prompt
211
- // cache. Adopt the new fingerprint, keep riding the same bytes, emit
212
- // nothing: an invalidation has to mean bytes actually moved.
217
+ // The store and/or the prompt moved. A change that alters nothing
218
+ // RENDERED — a row that is not relevant to this prompt, supersession
219
+ // elsewhere, a scope the reader never sees rendered identically — must
220
+ // not cost this session a prompt cache. Adopt the new fingerprint and
221
+ // prompt, keep riding the same bytes, emit nothing: an invalidation has
222
+ // to mean bytes actually moved.
213
223
  if (cached && cached.prefix === built.prefix) {
214
- epoch.set(sid, { ...cached, version });
224
+ epoch.set(sid, { ...cached, version, prompt });
215
225
  return;
216
226
  }
217
227
  // Riding bytes (#165): a pure addition this session wrote itself is
@@ -240,15 +250,18 @@ export function createInjectionEpochs({
240
250
  }
241
251
  }
242
252
  }
243
- const prompt = latestPrompt.get(sid) ?? "";
244
253
  // Use ledger (#42): the rows that actually rode the block, credited with
245
254
  // the terms the prompt and those rows shared (never the prompt itself).
246
255
  // Only once the build is ADOPTED — a discarded rebuild must not credit
247
256
  // rows that were already offered under the frozen epoch.
248
257
  await noteHits(sid, built.rode, prompt);
249
258
  const at = new Date().toISOString();
250
- const reason: "first" | "store_changed" = cached
251
- ? "store_changed"
259
+ // Which input moved decides the reason: the store outranks the prompt, so
260
+ // a turn that did both is accounted as the (cache-critical) store change.
261
+ const reason: "first" | "store_changed" | "prompt_reveal" = cached
262
+ ? cached.version !== version
263
+ ? "store_changed"
264
+ : "prompt_reveal"
252
265
  : "first";
253
266
  epoch.set(sid, {
254
267
  version,
@@ -275,12 +288,13 @@ export function createInjectionEpochs({
275
288
  evidenceContext,
276
289
  );
277
290
  epochRecomputes++;
278
- // Ledger: a store_changed recompute of a live epoch is an
279
- // invalidation (the first epoch of a session busts nothing). The
280
- // epoch request count restarts with the new block.
291
+ // Ledger: any recompute of a live epoch that MOVED the bytes is an
292
+ // invalidation (the first epoch of a session busts nothing; a re-score
293
+ // that kept the bytes never reaches this line). The epoch request count
294
+ // restarts with the new block.
281
295
  const st = statsFor(sid);
282
296
  st.epochRequests = 0;
283
- if (reason === "store_changed") {
297
+ if (reason !== "first") {
284
298
  st.invalidations++;
285
299
  st.lastInvalidation = { at, reason };
286
300
  }
@@ -90,7 +90,7 @@ export interface StatusPayload {
90
90
  /** ISO instant of the epoch recompute that produced this snapshot. */
91
91
  at: string;
92
92
  sessionID: string;
93
- reason: "first" | "store_changed";
93
+ reason: "first" | "store_changed" | "prompt_reveal";
94
94
  /** epoch recomputes since the server process started, ALL sessions. */
95
95
  recomputes: number;
96
96
  /** exact injected block ("" = abstained: no vocabulary overlap). */