opencode-self-improvement 0.24.1 → 0.25.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.
@@ -3,7 +3,7 @@
3
3
  import type { ToolRegister } from "./deps.ts";
4
4
  import { memoryRoot } from "../core/config/paths.ts";
5
5
  import { record } from "../evidence/record.ts";
6
- import { withStoreLock } from "../store/lock.ts";
6
+ import { withStoreBatch } from "../store/lock.ts";
7
7
  import { loadAll } from "../store/files/load.ts";
8
8
  import { clearSuperseded, markSuperseded } from "../store/files/supersede.ts";
9
9
  import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
@@ -56,8 +56,9 @@ export const register: ToolRegister = (editor, deps) => {
56
56
  // records that publish before the overall promise settles (#197).
57
57
  const mergeDurable: string[] = [];
58
58
  try {
59
- return await withStoreLock(
60
- join(memoryRoot(), "update-order"),
59
+ return await withStoreBatch(
60
+ memoryRoot(),
61
+ "update-order",
61
62
  async () => {
62
63
  const all = await loadAll();
63
64
  const survivorRow = all.find((m) => m.ts === args.survivor);
@@ -10,7 +10,7 @@ import { auditCaveat, runWriteGate, sameStrList } from "../gate/gate.ts";
10
10
  import { loadAll } from "../store/files/load.ts";
11
11
  import { markSuperseded } from "../store/files/supersede.ts";
12
12
  import { resolveMemoryTarget } from "../store/files/target.ts";
13
- import { withStoreLock } from "../store/lock.ts";
13
+ import { withStoreBatch } from "../store/lock.ts";
14
14
  import type { Memory } from "../store/model/memory.ts";
15
15
  import { isActive } from "../store/model/predicates.ts";
16
16
  import { isVisibleTo, readerFromSid } from "../store/model/visibility.ts";
@@ -45,13 +45,14 @@ export const register: ToolRegister = (editor, deps) => {
45
45
  // resolved the same still-active target both promoted and both
46
46
  // claimed success: markSuperseded is first-call-wins, so one
47
47
  // replacement row silently survived un-superseded (measured 5/5).
48
- // This outer lock is a DISTINCT file from anything runWriteGate or
49
- // markSuperseded take (sections run flat — never re-enter a file
50
- // you hold), and the acquisition order is always update-order
51
- // then store, never the reverse, so it cannot deadlock against a
52
- // plain writer. On timeout it FAILS closed: proceeding unlocked
53
- // is exactly the race this lock exists to close. The loser then
54
- // re-resolves under the lock, finds its target already retired,
48
+ // This outer lock is DISTINCT from anything runWriteGate or
49
+ // markSuperseded take — withStoreBatch gives it its own database, so
50
+ // the gate never holds the store-wide lock (sections run flat — never
51
+ // re-enter a lock you hold). The acquisition order is always
52
+ // update-order then store, never the reverse, so it cannot deadlock
53
+ // against a plain writer. On timeout it FAILS closed: proceeding
54
+ // unlocked is exactly the race this lock exists to close. The loser
55
+ // then re-resolves under the lock, finds its target already retired,
55
56
  // and writes nothing — the documented fail-closed contract.
56
57
  // Flips once a replacement row is durable, so the catch below can
57
58
  // never claim "Nothing was written" about a write that happened
@@ -62,8 +63,9 @@ export const register: ToolRegister = (editor, deps) => {
62
63
  // is no longer writable.
63
64
  const gateDurable: string[] = [];
64
65
  try {
65
- return await withStoreLock(
66
- join(memoryRoot(), "update-order"),
66
+ return await withStoreBatch(
67
+ memoryRoot(),
68
+ "update-order",
67
69
  async () => {
68
70
  const prior = (await loadAll()).filter(
69
71
  (m) =>
@@ -7,16 +7,7 @@
7
7
  // #223: a non-loopback bind or allowed host mints a startup token (0600, in
8
8
  // the store root) and every request must present it; loopback stays open.
9
9
  import { basename, join } from "node:path";
10
- import {
11
- closeSync,
12
- constants,
13
- linkSync,
14
- openSync,
15
- readFileSync,
16
- renameSync,
17
- rmSync,
18
- statSync,
19
- } from "node:fs";
10
+ import { linkSync, readFileSync, renameSync, rmSync } from "node:fs";
20
11
  import { isIP } from "node:net";
21
12
  import { randomBytes, timingSafeEqual } from "node:crypto";
22
13
  import { serve, type Server } from "bun";
@@ -36,6 +27,7 @@ import {
36
27
  snapshotDir,
37
28
  } from "../core/config/paths.ts";
38
29
  import { blockedEntries } from "../store/blocklist.ts";
30
+ import { LOCK_WRITE, withStoreLock } from "../store/lock.ts";
39
31
  import { listSnapshots, restoreSnapshot } from "../store/undo.ts";
40
32
  import { appendMemory } from "../store/files/append.ts";
41
33
  import { loadAll, memoryFiles } from "../store/files/load.ts";
@@ -190,56 +182,34 @@ async function mintTokenOnce(): Promise<string> {
190
182
  }
191
183
  }
192
184
 
193
- // Repair a corrupt token file under an O_EXCL lock (F1 residual, #223
194
- // review): exactly one starter replaces the file, the rest adopt its token.
195
- // The lock also keeps Windows from renaming onto a file a rival starter is
196
- // mid-read — the observed EPERM. A holder that crashes must not wedge every
197
- // later start, so a lock older than the wait window is stolen and retried.
198
- const RECOVER_WAIT_MS = 5_000;
199
- const RECOVER_STALE_MS = 15_000;
200
-
185
+ // Repair a corrupt token file: exactly one starter replaces the file and the
186
+ // rest adopt its token (F1 residual, #223 review). The repair also keeps Windows
187
+ // from renaming onto a file a rival starter is mid-read — the observed EPERM.
188
+ //
189
+ // It takes the STORE's lock to do it. It used to take its own O_EXCL
190
+ // `.recover` file, which is the #407 shape exactly: the state of the lock lived
191
+ // in a directory entry, and on Windows that entry outlives the holder whenever
192
+ // a third-party handle is open without FILE_SHARE_DELETE. The age-based steal
193
+ // here could not rescue that case either, because the steal is itself an unlink
194
+ // and the same refusal applies to it — a viewer that hit it could never start
195
+ // again.
201
196
  async function recoverCorruptToken(
202
197
  path: string,
203
198
  tmp: string,
204
199
  token: string,
205
200
  ): Promise<string> {
206
- const lock = path + ".recover";
207
- const deadline = Date.now() + RECOVER_WAIT_MS;
208
- for (;;) {
209
- let held = false;
210
- try {
211
- closeSync(
212
- openSync(
213
- lock,
214
- constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL,
215
- 0o600,
216
- ),
217
- );
218
- held = true;
201
+ return withStoreLock(
202
+ path,
203
+ async () => {
204
+ // Re-read inside the lock: a rival repair may have completed while this
205
+ // call waited, and its token is the one URLs in this store already carry.
219
206
  const settled = readToken(path);
220
207
  if (TOKEN_SHAPE.test(settled)) return settled;
221
208
  renameSync(tmp, path);
222
209
  return token;
223
- } catch (err) {
224
- if (held || (err as NodeJS.ErrnoException).code !== "EEXIST") throw err;
225
- const waiting = readToken(path);
226
- if (TOKEN_SHAPE.test(waiting)) return waiting;
227
- if (Date.now() < deadline) {
228
- await Bun.sleep(25);
229
- continue;
230
- }
231
- let stale = true;
232
- try {
233
- stale = Date.now() - statSync(lock).mtimeMs > RECOVER_STALE_MS;
234
- } catch {
235
- stale = true;
236
- }
237
- if (!stale) throw err;
238
- rmSync(lock, { force: true });
239
- } finally {
240
- if (held) rmSync(lock, { force: true });
241
- }
242
- }
210
+ },
211
+ LOCK_WRITE,
212
+ );
243
213
  }
244
214
 
245
215
  function tokenMatches(candidate: string, expected: string): boolean {