opencode-self-improvement 0.15.30 → 0.15.31

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/store/lock.ts +40 -15
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-self-improvement",
3
- "version": "0.15.30",
3
+ "version": "0.15.31",
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/store/lock.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  // - a lock FILE created with O_EXCL, which is atomic across processes (and
17
17
  // exclusive against our own tasks too, since the check is in the kernel).
18
18
  // A killed holder is recovered by age. A writer that waits past the budget
19
- // gives up waiting — see onTimeout.
19
+ // gives up waiting — see onTimeout (default: fail closed, #216).
20
20
  //
21
21
  // Readers take NO lock: publishers hand over the new file with a rename
22
22
  // (writeAtomic in logfmt.ts), so a reader always sees a complete store, never
@@ -30,6 +30,11 @@ const LOCK_SUFFIX = ".lock";
30
30
  // A lock older than this belongs to a process that died holding it.
31
31
  const STALE_MS = 10_000;
32
32
  const DEFAULT_TIMEOUT_MS = 5_000;
33
+ // #216: the default policy is fail-closed, the policy #196 moved every
34
+ // production writer to. A writer that omits opts must refuse at the deadline,
35
+ // not silently reintroduce the unlocked-proceed row loss the LOCK_WRITE
36
+ // contract retired. "proceed" survives only as an explicit opt-in.
37
+ const DEFAULT_ON_TIMEOUT = "fail";
33
38
  const MIN_BACKOFF_MS = 1;
34
39
  const MAX_BACKOFF_MS = 25;
35
40
  // How many consecutive create-failures with a proven-absent lock file mean
@@ -37,6 +42,13 @@ const MAX_BACKOFF_MS = 25;
37
42
  // race with a live holder". The wait is a fast, bounded spin, not a budget.
38
43
  const ABSENT_THROW_STRIKES = 8;
39
44
 
45
+ // The single source of the timeout policy (#216): both give-up paths — the
46
+ // direct budget and the queue hand-off — resolve through here, so the
47
+ // fail-closed default cannot drift between them.
48
+ function timeoutPolicy(opts: LockOptions): "proceed" | "fail" {
49
+ return opts.onTimeout === "proceed" ? "proceed" : DEFAULT_ON_TIMEOUT;
50
+ }
51
+
40
52
  export function lockFileFor(file: string): string {
41
53
  return file + LOCK_SUFFIX;
42
54
  }
@@ -47,11 +59,11 @@ export type LockEvent = "steal" | "timeout";
47
59
  export interface LockOptions {
48
60
  timeoutMs?: number;
49
61
  /**
50
- * What waiting past `timeoutMs` means. "fail" refuses the mutation (for
51
- * callers where proceeding unlocked could destroy more than it saves, e.g.
52
- * snapshot restore). The legacy default, "proceed", runs it unlocked and
53
- * can lose acknowledged writes when another caller rewrites the same file.
54
- * Store writers must use LOCK_WRITE (or an explicit fail-closed policy).
62
+ * What waiting past `timeoutMs` means. The default, "fail", refuses the
63
+ * mutation (#216: fail-closed is the contract every store writer keeps;
64
+ * LOCK_WRITE and LOCK_RESTORE spell it out). "proceed" runs the write
65
+ * unlocked and can lose acknowledged writes when another caller rewrites
66
+ * the same file — pass it only where that concession is intended.
55
67
  */
56
68
  onTimeout?: "proceed" | "fail";
57
69
  onEvent?: (event: LockEvent, waitedMs: number) => void;
@@ -154,7 +166,8 @@ export async function holdFileLock<T>(
154
166
  // `absentStrikes > 0` only remembers the LAST probe, so a holder that
155
167
  // legitimately finished and unlinked just as the budget expired was
156
168
  // throwing away ordinary writes instead of taking the contention
157
- // concession it describes.
169
+ // concession it describes. An EEXIST from a failed create counts as such a
170
+ // sighting too (a create that returns EEXIST saw the file).
158
171
  let everSawHolder = false;
159
172
  for (;;) {
160
173
  try {
@@ -178,6 +191,16 @@ export async function holdFileLock<T>(
178
191
  // wait for — our own ability to create files is what is broken, and
179
192
  // the original error must surface instead of rotting into a timeout.
180
193
  firstErr ??= err;
194
+ // An EEXIST from the create is a sighting in its own right: on POSIX it
195
+ // proves the lock file WAS there at the attempt, even if the probe
196
+ // below then proves the holder unlinked in between (the vanish race).
197
+ // Without this, that race reached the deadline with everSawHolder false
198
+ // and surfaced the stale creation error instead of the busy refusal.
199
+ // Ambiguous labels (the Windows EPERM/EACCES family) stay non-sightings
200
+ // — blindness is blindness (#107) — and a create-yes/stat-no filesystem
201
+ // still dies on the strike rule long before the budget.
202
+ if ((err as NodeJS.ErrnoException).code === "EEXIST")
203
+ everSawHolder = true;
181
204
  const seen = await probeLock(lockPath);
182
205
  if (seen === "absent") {
183
206
  if (++absentStrikes >= ABSENT_THROW_STRIKES) throw firstErr;
@@ -191,18 +214,20 @@ export async function holdFileLock<T>(
191
214
  }
192
215
  if (now() - started >= budgetMs) {
193
216
  opts.onEvent?.("timeout", now() - started);
194
- if (opts.onTimeout === "fail") {
217
+ // Blindness outranks policy (#107): a wait that never once saw a lock
218
+ // file has no holder to wait for — our own ability to create files is
219
+ // what is broken, and the original error must surface, never a busy
220
+ // refusal and never an unguarded write.
221
+ if (!everSawHolder) throw firstErr;
222
+ if (timeoutPolicy(opts) === "fail") {
195
223
  throw new Error(
196
224
  `memory store is busy: ${basename(file)} has been locked for ${now() - started}ms — retry`,
197
225
  );
198
226
  }
199
- // Proceeding without the lock is the designed concession to
200
- // contention — but never while we have seen the lock file not even
201
- // once: on a filesystem where we cannot create it, writing "unlocked"
202
- // would just mean writing unguarded. A holder we DID see and then lost
203
- // is ordinary contention, and belongs to the concession (#107 —
227
+ // Proceeding without the lock is the explicit "proceed" opt-in
228
+ // (#216: no longer the default). A holder we DID see and then lost is
229
+ // ordinary contention, and belongs to the opt-in (#107 —
204
230
  // `absentStrikes > 0` remembered only the last probe, not the wait).
205
- if (!everSawHolder) throw firstErr;
206
231
  return fn();
207
232
  }
208
233
  await sleep(backoff);
@@ -253,7 +278,7 @@ export async function withStoreLock<T>(
253
278
  // writer that is not going to finish.
254
279
  finish();
255
280
  opts.onEvent?.("timeout", Date.now() - started);
256
- if (opts.onTimeout === "fail") {
281
+ if (timeoutPolicy(opts) === "fail") {
257
282
  throw new Error(
258
283
  `memory store is busy: ${basename(file)} has been locked for ${Date.now() - started}ms — retry`,
259
284
  );