opencode-self-improvement 0.14.3 → 0.14.5

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 +2 -1
  2. package/src/lock.ts +37 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.14.3",
3
+ "version": "0.14.5",
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": {
@@ -68,6 +68,7 @@
68
68
  "bench": "bun bench/inject-cost.ts",
69
69
  "bench:recall": "bun bench/recall-quality.ts",
70
70
  "bench:write": "bun bench/write-latency.ts",
71
+ "sweep:test": "bun scripts/test-sweep.ts",
71
72
  "memory-view": "bun run src/viewer/server.ts",
72
73
  "lint": "biome lint"
73
74
  },
package/src/lock.ts CHANGED
@@ -90,13 +90,17 @@ async function timed(prev: Promise<void>, ms: number): Promise<boolean> {
90
90
 
91
91
  // The one probe that can answer "there is no holder" without itself being a
92
92
  // creation attempt: stat reports a held file happily (Windows shares metadata
93
- // reads with the handle owner), and ENOENT — uniquely — proves absence.
94
- async function lockFileAbsent(lockPath: string): Promise<boolean> {
93
+ // reads with the handle owner), ENOENT — uniquely — proves absence, and any
94
+ // other answer (an ACL that lets us neither create nor see, say) is no answer
95
+ // at all: it must count as neither.
96
+ type LockProbe = "present" | "absent" | "unknown";
97
+
98
+ async function probeLock(lockPath: string): Promise<LockProbe> {
95
99
  try {
96
100
  await stat(lockPath);
97
- return false;
101
+ return "present";
98
102
  } catch (probe) {
99
- return (probe as NodeJS.ErrnoException).code === "ENOENT";
103
+ return (probe as NodeJS.ErrnoException).code === "ENOENT" ? "absent" : "unknown";
100
104
  }
101
105
  }
102
106
 
@@ -121,20 +125,32 @@ async function stealIfStale(lockPath: string, now: number): Promise<boolean> {
121
125
  return true;
122
126
  }
123
127
 
124
- /** Cross-process half: hold the lock file while `fn` runs. */
125
- async function holdFileLock<T>(
128
+ /**
129
+ * Cross-process half: hold the lock file while `fn` runs. `now` is a seam for
130
+ * tests that must land the budget deadline inside a specific probe iteration;
131
+ * production callers never pass it and the real clock is used.
132
+ */
133
+ export async function holdFileLock<T>(
126
134
  lockPath: string,
127
135
  file: string,
128
136
  fn: () => Promise<T>,
129
137
  opts: LockOptions,
130
138
  budgetMs: number,
139
+ now: () => number = Date.now,
131
140
  ): Promise<T> {
132
141
  await mkdir(dirname(lockPath), { recursive: true });
133
142
  const token = `${process.pid}-${Math.random().toString(36).slice(2)}`;
134
- const started = Date.now();
143
+ const started = now();
135
144
  let backoff = MIN_BACKOFF_MS;
136
145
  let firstErr: unknown;
137
146
  let absentStrikes = 0;
147
+ // #107: did ANY probe see a lock file during this wait? The rethrow at the
148
+ // deadline promises we never proceed as a stranger to the lock file — but
149
+ // `absentStrikes > 0` only remembers the LAST probe, so a holder that
150
+ // legitimately finished and unlinked just as the budget expired was
151
+ // throwing away ordinary writes instead of taking the contention
152
+ // concession it describes.
153
+ let everSawHolder = false;
138
154
  for (;;) {
139
155
  try {
140
156
  const handle = await open(lockPath, "wx");
@@ -157,27 +173,31 @@ async function holdFileLock<T>(
157
173
  // wait for — our own ability to create files is what is broken, and
158
174
  // the original error must surface instead of rotting into a timeout.
159
175
  firstErr ??= err;
160
- if (await lockFileAbsent(lockPath)) {
176
+ const seen = await probeLock(lockPath);
177
+ if (seen === "absent") {
161
178
  if (++absentStrikes >= ABSENT_THROW_STRIKES) throw firstErr;
162
179
  } else {
163
180
  absentStrikes = 0;
181
+ if (seen === "present") everSawHolder = true;
164
182
  }
165
- if (await stealIfStale(lockPath, Date.now())) {
166
- opts.onEvent?.("steal", Date.now() - started);
183
+ if (await stealIfStale(lockPath, now())) {
184
+ opts.onEvent?.("steal", now() - started);
167
185
  continue;
168
186
  }
169
- if (Date.now() - started >= budgetMs) {
170
- opts.onEvent?.("timeout", Date.now() - started);
187
+ if (now() - started >= budgetMs) {
188
+ opts.onEvent?.("timeout", now() - started);
171
189
  if (opts.onTimeout === "fail") {
172
190
  throw new Error(
173
- `memory store is busy: ${basename(file)} has been locked for ${Date.now() - started}ms — retry`,
191
+ `memory store is busy: ${basename(file)} has been locked for ${now() - started}ms — retry`,
174
192
  );
175
193
  }
176
194
  // Proceeding without the lock is the designed concession to
177
- // contention — but never while the lock file has been absent this
178
- // whole time: on a filesystem where we cannot create it, writing
179
- // "unlocked" would just mean writing unguarded.
180
- if (absentStrikes > 0) throw firstErr;
195
+ // contention — but never while we have seen the lock file not even
196
+ // once: on a filesystem where we cannot create it, writing "unlocked"
197
+ // would just mean writing unguarded. A holder we DID see and then lost
198
+ // is ordinary contention, and belongs to the concession (#107 —
199
+ // `absentStrikes > 0` remembered only the last probe, not the wait).
200
+ if (!everSawHolder) throw firstErr;
181
201
  return fn();
182
202
  }
183
203
  await sleep(backoff);