Skip to content

Nothing protects the memory bank from concurrent operations #3

Closed

Description

@krapcys1-maker

The semantic memory bank has no locking. An administrative rebuild deletes the bank and repopulates it; a run reads from it at start and writes at end. Nothing prevents these overlapping.

Observed twice:

  1. A rebuild of the experimental bank aborted mid-write with TimeoutError because a research run and a second rebuild were hitting the same instance. The bank was left deleted and only partially repopulated — neither the old state nor the new one.
  2. A test that touches live infrastructure failed once during a concurrent rebuild and passed on re-run.

Nothing was lost because a full export is taken before any destructive step, but that is a procedure, not a safeguard.

What is needed: at minimum an advisory lock that makes a rebuild refuse to start while a run is active, and makes a run refuse to start while a rebuild holds the lock. A read-only snapshot for recall during rebuilds would be better still.

Activity

  1. added
    infrastructure
    on Aug 25, 2026
  2. krapcys1-maker commented on Aug 31, 2026

    @krapcys1-maker
    OwnerAuthor

    Fixed.

    src/lib/db/advisory-lock.ts adds withAdvisoryLock(), used by scripts/memory-rebuild.ts. Postgres session-level advisory locks were chosen because they need no new table and are released automatically when the process dies — which is the failure that left the bank wedged in the first place.

    Two things surfaced while building it, both kept in the code with their reasoning:

    • Postgres advisory locks are re-entrant within a session, and Prisma pools connections, so the lock alone gives no intra-process guarantee. A test caught this: the nested acquisition in 'refuses a second holder' succeeded against an implementation whose own docstring promised exclusion. A process-local heldLocally set is what makes the promise true inside one process; the Postgres lock is what excludes others.
    • pg_advisory_unlock returns false rather than raising when the session does not hold the lock. The release discarded that boolean, so the one failure this design has looked exactly like success. Orphaned locks are now recorded, and the next acquisition says so instead of blaming a concurrent rebuild that does not exist.
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

    infrastructure

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions