opencode-self-improvement 0.15.27 → 0.15.28

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.15.27",
3
+ "version": "0.15.28",
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": {
@@ -42,6 +42,18 @@ const ENGAGEMENT_TOOLS = new Set([
42
42
  ]);
43
43
 
44
44
  const RECENT_EVIDENCE_CALLS = 10;
45
+ // #224: the retained call tail. A resident server must not grow one session's
46
+ // ledger without bound; past the cap the OLDEST entries drop. Everything the
47
+ // pipeline needs past the cap is recorded on SessionState as it happens
48
+ // (callsTotal, reDerives, firstCallTs, sawEngagement, sawGateCheck), so
49
+ // telemetry counts, grounding signals and ms_to_tap keep their full-session
50
+ // meaning; attribution (callsFor) sees the retained tail.
51
+ // The cap sits ABOVE the 2,000-call scale pinned by write-evidence.test.ts
52
+ // (#163: the full ledger stays available at that scale), so that contract
53
+ // keeps holding; the bound is the point — a pathological resident session
54
+ // grows this to hundreds of thousands of entries, and 4,096 is where the
55
+ // tail is cut.
56
+ export const CALLS_CAP = 4096;
45
57
  // Pending recovery stores IDs only; the full attribution ledger stays in calls.
46
58
  function trackPending(s: SessionState, rec: ToolResult): void {
47
59
  const pending = s.pendingGate;
@@ -58,6 +70,12 @@ export const MAX_WRITE_EVIDENCE_REFS = RECENT_EVIDENCE_CALLS + 1 + 3 + 2;
58
70
 
59
71
  function trackCall(s: SessionState, rec: ToolResult): void {
60
72
  s.calls.push(rec);
73
+ s.callsTotal++;
74
+ if (["grep", "glob", "read"].includes(rec.tool)) s.reDerives++;
75
+ if (!s.firstCallTs) s.firstCallTs = rec.ts;
76
+ if (ENGAGEMENT_TOOLS.has(rec.tool)) s.sawEngagement = true;
77
+ if (rec.gateCheck) s.sawGateCheck = true;
78
+ if (s.calls.length > CALLS_CAP) s.calls.splice(0, s.calls.length - CALLS_CAP);
61
79
  s.writeEvidence ??= { recent: [] };
62
80
  const evidence = s.writeEvidence;
63
81
  if (ENGAGEMENT_TOOLS.has(rec.tool)) {
@@ -69,9 +87,10 @@ function trackCall(s: SessionState, rec: ToolResult): void {
69
87
 
70
88
  // True once the session has exercised the codebase (the FAIL->PASS signals use
71
89
  // the same whitelist, so engagement == "a tool_result was recorded for a code
72
- // tool").
90
+ // tool"). Sticky (#224): the CALLS_CAP tail dropping the original engagement
91
+ // call must not un-ground a session that demonstrably engaged.
73
92
  export function hasEngagement(sid: string): boolean {
74
- return session(sid).calls.some((c) => ENGAGEMENT_TOOLS.has(c.tool));
93
+ return session(sid).sawEngagement;
75
94
  }
76
95
 
77
96
  const GATE_CMDS: [RegExp, string][] = [
@@ -247,7 +266,7 @@ export async function onCompacting(input: AfterInput): Promise<void> {
247
266
  "t2_compaction",
248
267
  {
249
268
  session: sid,
250
- captured_calls: s.calls.length,
269
+ captured_calls: s.callsTotal,
251
270
  pending_gate: s.pendingGate ? s.pendingGate.call : "-",
252
271
  },
253
272
  false,
@@ -255,8 +274,9 @@ export async function onCompacting(input: AfterInput): Promise<void> {
255
274
  await finalizeSession(sid);
256
275
  }
257
276
 
258
- // Full IDs for callers that explicitly need the ledger. Production write
259
- // contexts use writeEvidence so they do not allocate this growing list.
277
+ // IDs for callers that explicitly need the ledger: the retained tail (capped
278
+ // at CALLS_CAP, oldest dropped first — #224). Production write contexts use
279
+ // writeEvidence so they do not allocate this growing list.
260
280
  export function evidenceCalls(sid: string): string[] {
261
281
  return session(sid).calls.map((c) => c.call);
262
282
  }
@@ -277,16 +297,18 @@ export function writeEvidence(sid: string): {
277
297
  ...(evidence?.gateCheck !== undefined ? [evidence.gateCheck] : []),
278
298
  ...(evidence?.recent ?? []),
279
299
  ]);
280
- return { evidenceCalls: [...refs], evidenceCallsCount: s.calls.length };
300
+ return { evidenceCalls: [...refs], evidenceCallsCount: s.callsTotal };
281
301
  }
282
302
 
283
- // The full per-session tool ledger, for memory attribution (src/attribution.ts).
303
+ // The per-session tool ledger for memory attribution (src/attribution.ts):
304
+ // the retained tail, capped at CALLS_CAP with the oldest calls dropped first
305
+ // (#224), so a resident server bounds memory instead of holding every call.
284
306
  export function callsFor(sid: string): ToolResult[] {
285
307
  return session(sid).calls;
286
308
  }
287
309
 
288
310
  export function hasGateCheck(sid: string): boolean {
289
- return session(sid).calls.some((c) => c.gateCheck === true);
311
+ return session(sid).sawGateCheck;
290
312
  }
291
313
 
292
314
  // ---- Session-convergence tracking (time-to-frontier) -----------------------
@@ -321,9 +343,9 @@ export async function finalizeSession(sid: string): Promise<void> {
321
343
  const s = session(sid);
322
344
  if (s.finalized) return;
323
345
  s.finalized = true;
324
- if (s.calls.length === 0 && s.turns === 0) return;
346
+ if (s.callsTotal === 0 && s.turns === 0) return;
325
347
  const tap = s.firstTap;
326
- const start = s.firstActivityTs ?? s.calls[0]?.ts;
348
+ const start = s.firstActivityTs ?? s.firstCallTs;
327
349
  const ms =
328
350
  tap && start
329
351
  ? Math.max(0, Date.parse(tap.ts) - Date.parse(start))
@@ -335,11 +357,9 @@ export async function finalizeSession(sid: string): Promise<void> {
335
357
  turns: s.turns,
336
358
  tap_at: tap ? tap.call : "-",
337
359
  turns_to_tap: tap ? tap.turn : undefined,
338
- re_derives: s.calls.filter((c) =>
339
- ["grep", "glob", "read"].includes(c.tool),
340
- ).length,
360
+ re_derives: s.reDerives,
341
361
  ms_to_tap: ms,
342
- calls: s.calls.length,
362
+ calls: s.callsTotal,
343
363
  },
344
364
  false,
345
365
  );
package/src/core/lru.ts CHANGED
@@ -4,7 +4,8 @@
4
4
  // recency list owns eviction so the maps are always dropped together: an
5
5
  // evicted session simply starts over (no block until its next prompt, then a
6
6
  // "first" epoch), which costs one recompute and never serves stale state.
7
- // Same cap as repokey.ts's session-scope cache.
7
+ // Single shared cap: repokey.ts's session-scope cache imports this constant,
8
+ // so the ledgers cannot drift (#224).
8
9
  export const SESSION_CAP = 64;
9
10
 
10
11
  export class SessionLru {
package/src/core/state.ts CHANGED
@@ -27,6 +27,14 @@ export interface FirstTap {
27
27
  export interface SessionState {
28
28
  sid: string;
29
29
  calls: ToolResult[];
30
+ // Lifetime facts that outlive the CALLS_CAP tail (#224): calls keeps only
31
+ // the most recent CALLS_CAP entries, so counts and ever-seen signals are
32
+ // recorded as they happen instead of being derived from the array.
33
+ callsTotal: number;
34
+ reDerives: number;
35
+ firstCallTs?: string;
36
+ sawEngagement: boolean;
37
+ sawGateCheck: boolean;
30
38
  // Last observed status per tool, so a FAIL->PASS transition is visible.
31
39
  lastByTool: Map<
32
40
  string,
@@ -104,12 +112,25 @@ export function setLastSeenSid(sid: string): void {
104
112
 
105
113
  const sessions = new Map<string, SessionState>();
106
114
 
115
+ // Bounded like every other session-keyed ledger (#224): a resident server must
116
+ // not retain a SessionState for every session it ever saw. An evicted session
117
+ // starts over — its retained call tail and lifetime counters are dropped, the
118
+ // same "starts over" cost lru.ts already accepts for the epoch caches.
119
+ const sessionOrder = new SessionLru(SESSION_CAP, (sid) => {
120
+ sessions.delete(sid);
121
+ });
122
+
107
123
  export function session(sid: string): SessionState {
124
+ sessionOrder.touch(sid);
108
125
  let s = sessions.get(sid);
109
126
  if (!s) {
110
127
  s = {
111
128
  sid,
112
129
  calls: [],
130
+ callsTotal: 0,
131
+ reDerives: 0,
132
+ sawEngagement: false,
133
+ sawGateCheck: false,
113
134
  lastByTool: new Map(),
114
135
  pendingGate: null,
115
136
  turns: 0,
@@ -17,6 +17,7 @@
17
17
  // everywhere — which keeps every upgrade non-breaking.
18
18
  import { createHash } from "node:crypto";
19
19
  import { lstatSync, realpathSync, statSync } from "node:fs";
20
+ import { SESSION_CAP } from "../core/lru.ts";
20
21
 
21
22
  export interface RepoScope {
22
23
  key: string;
@@ -183,9 +184,10 @@ export function resolveRepo(startDir: string): RepoScope {
183
184
  }
184
185
 
185
186
  // --- per-session identity -----------------------------------------------------
186
- // Learned from session events (created / execution.started / moved). Bounded:
187
- // a long-lived server must not grow this with every session ever seen.
188
- const SESSION_CAP = 64;
187
+ // Learned from session events (created / execution.started / moved). Bounded
188
+ // by the shared session cap imported from core/lru.ts, so the two caps cannot
189
+ // drift (#224): a long-lived server must not grow this with every session
190
+ // ever seen.
189
191
  const byDir = new Map<string, RepoScope>();
190
192
  const bySession = new Map<string, { dir: string; scope: RepoScope }>();
191
193
 
@@ -5,6 +5,7 @@ import { isInjectable } from "../store/model/predicates.ts";
5
5
  import { readerFromSid, visibleTo } from "../store/model/visibility.ts";
6
6
  import { record } from "../evidence/record.ts";
7
7
  import { frontierBudget } from "../core/config/budgets.ts";
8
+ import { SESSION_CAP, SessionLru } from "../core/lru.ts";
8
9
  import { dateStr } from "../core/time.ts";
9
10
  import { bracketList } from "../core/logfmt/format.ts";
10
11
  import { recentConvergenceSummary } from "../capture/triggers.ts";
@@ -19,6 +20,12 @@ import { type TransformOutput } from "./pull.ts";
19
20
  const OPEN_THREAD_LIMIT = 3;
20
21
 
21
22
  const frontierInjectedSessions = new Set<string>();
23
+ // #224: the "once per process" set is bounded by the shared session cap. An
24
+ // evicted session is shown the frontier block once more on a later prompt —
25
+ // one extra evidence row, never unbounded growth.
26
+ const frontierOrder = new SessionLru(SESSION_CAP, (sid) => {
27
+ frontierInjectedSessions.delete(sid);
28
+ });
22
29
 
23
30
  export interface DigestOptions {
24
31
  // Rows the caller already loaded for this request (shared with the epoch).
@@ -78,6 +85,7 @@ export async function systemDigest(
78
85
 
79
86
  if (frontierInjectedSessions.has(sid)) return;
80
87
  frontierInjectedSessions.add(sid);
88
+ frontierOrder.touch(sid);
81
89
  const openMems = mems.filter((m) => m.status === "open");
82
90
  await record(
83
91
  "frontier_injected",