Skip to content

P0: Lock read-modify-write paths (canonical registry, snapshot restore) — two-writer stress #36

Closed

Description

@abogartz

Problem

Append is atomic (O_APPEND, proven by logfmt.test.ts "concurrent appends lose nothing"), and memory_undo restore is conflict-aware (check/merge/force, src/undo.ts). But the remaining read-modify-write surfaces — the canonical registry and snapshot/restore itself — have no cross-process lock. Two plugin processes (two opencode instances on the same machine) writing canonical entries or restoring concurrently can interleave.

Atlas stress checklist item: "Concurrency: two writers, one correction, interleaved. Non-atomic JSON rewrites lose data under this."

Proposal

  • Directory-lock (or Bun advisory lock) around the canonical-registry RMW and around restoreSnapshot, modeled on Prime Agent's cross-language harness_state.json.lock (owner file, retrying acquire, stale timeout) — we only need one language, so it can be much simpler.
  • Test: two interleaved writers exercising canonical writes + a restore, asserting no row loss.

Note

merge restore deliberately keeps the snapshotter's own post-snapshot rows; undoing your own newer writes still requires FORCE (documented, accepted for now). Revisit only if real usage bites.

Activity

  1. abogartz commented on Oct 23, 2026

    @abogartz
    OwnerAuthor

    Scope correction found while implementing: canonical.logfmt is not a live write path. The plugin only reads it (src/gates.ts); nothing at runtime writes it — only tests and hand-seeded fixtures do. So the canonical-registry RMW this issue proposed locking had no exposure.

    The real unguarded read-modify-write surfaces are worse, because they run against the daily file every session appends to:

    • markSuperseded / clearSuperseded (src/memory.ts) — full-file Bun.write. Reached by memory_update, drift stamps, canonical neutralization, merge review and decay.
    • prune (src/memory.ts) — filters rows out of a whole file.
    • deleteMemory (src/viewer/mutate.ts) — same shape.
    • restoreSnapshot (src/undo.ts) — plus its divergence check read the file before comparing, so a row that landed after the read was waved through as "no divergence".

    Measured on main before the fix: 40 concurrent appends against 40 supersedes left 40 of 80 rows on disk; four processes writing one store kept 20 of 120. Two extras on the same paths: appendMemory seeded ts from a store scan (two processes, one millisecond, one row identity), and snapshotFiles claimed snapshot names with exists()-then-write, so a cross-process same-millisecond collision could clobber an undo point.

    Implemented in #60 as a per-file lock (in-process FIFO + O_EXCL lock file, shared timeout budget, dead holders recovered by age), with rewrites published by atomic rename so lock-free readers never see a truncated store. Lock events go to evidence as store_lock. Restore fails closed on contention; ordinary writes proceed rather than lose the write.

  2. abogartz-opencode-self-improvement commented on Oct 23, 2026

    @abogartz-opencode-self-improvement

    🎉 This issue has been resolved in version 0.8.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions