opencode-self-improvement 0.15.7 → 0.15.8

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/hits/ledger.ts +25 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.15.7",
3
+ "version": "0.15.8",
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,6 +39,8 @@ export { HITS_FILENAME };
39
39
  // own first line rather than inflating "never" (status.ledgerSince).
40
40
  const MAX_HITS_BYTES = 2_000_000;
41
41
  export const THROTTLE_MS = 3_600_000;
42
+ // Per-key failure cooldown, measured from the attempt's observation time.
43
+ export const RETRY_BACKOFF_MS = 60_000;
42
44
  const MAX_LOGGED_TERMS = 8;
43
45
 
44
46
  export function hitsFile(): string {
@@ -55,14 +57,18 @@ export interface HitEntry {
55
57
 
56
58
  // --- writing --------------------------------------------------------------------
57
59
 
58
- // key -> ms of the last line actually written. The epoch block rides EVERY
59
- // request of an epoch, so without this one "offer" would be counted dozens of
60
- // times and the dead-weight number would be fiction.
61
- const lastWrite = new Map<string, number>();
60
+ // key -> next eligible observation time. Successful writes wait an hour;
61
+ // failures wait only a minute so repeated reads do not each pay a lock timeout.
62
+ const nextAttempt = new Map<string, number>();
63
+ // Reserve identical calls only while I/O is pending. A failed attempt must
64
+ // release this reservation without starting the hour-long success throttle.
65
+ const inFlight = new Set<string>();
62
66
 
63
- /** Clear the throttle window (tests; also what a process restart does). */
67
+ /** Clear completed-attempt cooldowns. Tests must await all writes first:
68
+ * clearing reservations cannot cancel I/O or isolate a still-running writer. */
64
69
  export function resetHitThrottle(): void {
65
- lastWrite.clear();
70
+ if (inFlight.size) throw new Error("await pending recordHits calls before resetHitThrottle");
71
+ nextAttempt.clear();
66
72
  }
67
73
 
68
74
  async function rotateIfNeeded(): Promise<void> {
@@ -102,14 +108,9 @@ export async function recordHits(
102
108
  if (!rows.length) return false;
103
109
  const ids = rows.map((m) => m.ts);
104
110
  const key = `${source}|${sid}|${ids.join(",")}`;
105
- const prev = lastWrite.get(key);
106
- if (prev !== undefined && now - prev < THROTTLE_MS) return false;
107
- lastWrite.set(key, now);
108
- // Bound the throttle map: sessions churn, and a long-lived server must not
109
- // grow one entry per row-set forever. Oldest-inserted entries go first.
110
- if (lastWrite.size > 2000) {
111
- for (const k of [...lastWrite.keys()].slice(0, 1000)) lastWrite.delete(k);
112
- }
111
+ if (inFlight.has(key)) return false;
112
+ const eligibleAt = nextAttempt.get(key);
113
+ if (eligibleAt !== undefined && now < eligibleAt) return false;
113
114
  const line = formatLine({
114
115
  ev: "memory_used",
115
116
  ts: new Date(now).toISOString(),
@@ -118,6 +119,8 @@ export async function recordHits(
118
119
  mem: bracketList(ids),
119
120
  ...(terms.length ? { terms: bracketList(terms.slice(0, MAX_LOGGED_TERMS)) } : {}),
120
121
  });
122
+ inFlight.add(key);
123
+ let cooldown = RETRY_BACKOFF_MS;
121
124
  try {
122
125
  await withStoreLock(
123
126
  hitsFile(),
@@ -127,12 +130,20 @@ export async function recordHits(
127
130
  },
128
131
  LOCK_WRITE,
129
132
  );
133
+ cooldown = THROTTLE_MS;
130
134
  return true;
131
135
  } catch {
132
136
  // Fail-closed lock contention can drop this use observation. Decay and
133
137
  // consolidation therefore see a best-effort ledger, not proof of non-use;
134
138
  // retrieval remains available even when its telemetry cannot be persisted.
135
139
  return false; // telemetry never breaks retrieval
140
+ } finally {
141
+ nextAttempt.set(key, now + cooldown);
142
+ // Bound both successful and failed keys with the existing eviction cap.
143
+ if (nextAttempt.size > 2000) {
144
+ for (const k of [...nextAttempt.keys()].slice(0, 1000)) nextAttempt.delete(k);
145
+ }
146
+ inFlight.delete(key);
136
147
  }
137
148
  }
138
149