Skip to content

[Issue #125, #87] Per-secret operation lock; lock the encrypted vault - #132

Merged
Clarit-AI-Inc merged 2 commits into
mainfrom
fix/125-87-secret-op-lock
Oct 26, 2026
Merged

Clarit-AI-Inc merged 2 commits into
mainfrom
fix/125-87-secret-op-lock

Conversation

@Clarit-AI-Inc

Copy link
Copy Markdown
Collaborator

Closes #125. Closes #87.

#125: move cleanup could delete the current stored value

setSecret (set / rotate / import), deleteSecret and enigma move now hold a per-secret lock, keyed by scope and NAME, across their whole sequence: depository write → index commit → old-copy cleanup → render fan-out. Two changes to the same secret can no longer interleave in any depository, so a move's cleanup can't delete a copy another writer just stored at the same address.

  • The lock is async (acquireFileLockAsync) because it may be held across a depository prompt (Keychain, Touch ID, 1Password). A second change to the same name waits; after 30 s it fails with E_LOCK_TIMEOUT: "another change to NAME is in progress; try again when it finishes".
  • Not re-entrant (flock binds to the open file description), so move passes opLockHeld to its inner setSecret.
  • Lock order: operation lock first, then the index lock, render NAME/target locks and the vault lock, all taken and released inside it.

Departure from the issue's proposal. #125 suggested "fresh store refs". That can't protect the env depository, where a value's address is the variable name itself, and the issue's second reproduction deletes a dotenv line. A per-name lock covers every depository.

#87: encrypted vault read-modify-write had no lock

Encrypted set, delete and deleteIfUnchanged (and first-use key creation) run under <ENIGMA_HOME>/locks/vault.lock, so two processes writing at once can't drop an entry.

Tests

  • test/integration/secret-op-lock.test.ts: both enigma move: cleanup can delete the current stored value (data loss); use fresh store refs #125 reproductions plus a multi-process vault race, using real processes. All fail with the locks disabled (the vault test lost 83 of 160 entries) and pass with them on.
  • test/unit/storage/op-lock.test.ts: nested moves, lock timeout, scope isolation.
  • Existing render fan-out suites that deliberately drive cross-command interleavings now bypass the operation lock through a test-only setter (__setSecretOpLockBypassForTesting). It is never read from the environment in shipped code and is not present in dist. Rules A–C stay as defence in depth and keep their tests.

Docs

docs/api-contracts.md, docs/architecture.md and docs/feature-log.md are updated for the lock and its timeout.

Verification (Linux, Node 24, rebased on main at 2783b5b)

  • lint, typecheck, leak-fence, npm audit --audit-level=high, build, check:native: pass
  • npm test: 113 files passed, 1 skipped; 1758 tests passed, 6 skipped
  • npm run build leaves the committed dist byte-identical

🤖 Generated with Claude Code

#125: set/rotate/import (setSecret), remove (deleteSecret) and enigma move
hold a per-secret flock, keyed by scope and NAME, across their whole
write -> index commit -> old-copy cleanup -> render fan-out sequence. A
move's cleanup can no longer delete a copy another writer stored at the
same address. The lock is async (acquireFileLockAsync) because it can be
held across a depository prompt; a waiter fails after 30 s with
E_LOCK_TIMEOUT naming the secret. move passes opLockHeld to setSecret.

This departs from the issue's "fresh store refs" proposal: a dotenv
value's address is the variable name itself, so fresh refs cannot protect
it, and the issue's second reproduction deletes a dotenv line.

#87: encrypted set/delete/deleteIfUnchanged (and first-use key creation)
run under <ENIGMA_HOME>/locks/vault.lock.

Tests: both #125 reproductions and a multi-process vault test fail with
the locks disabled and pass with them. The render fan-out suites that
drive cross-command interleavings now bypass the operation lock (test-only
switch), since Rules A-C remain as defence in depth.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kilo-code-bot

kilo-code-bot Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (4 files)
  • docs/api-contracts.md
  • docs/feature-log.md
  • src/storage/op-lock.ts
  • test/integration/secret-op-lock.test.ts
Previous Review Summary (commit 3a80e07)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 3a80e07)

Status: No Issues Found | Recommendation: Merge

Files Reviewed (9 files)
  • src/cli/commands/move.ts
  • src/core/file-lock.ts
  • src/core/paths.ts
  • src/storage/depositories/encrypted.ts
  • src/storage/manager.ts
  • src/storage/op-lock.ts
  • test/integration/*
  • test/unit/*
  • docs/*

Reviewed by free · Input: 69.8K · Output: 4.8K · Cached: 663.7K

…st workers

Independent review of PR #132:
- import holds each entry's operation lock only for its write and index
  commit; the batch render fan-out runs after the whole batch commits,
  outside the lock. A rotate in that window can leave the name with no
  .env line (no stored value lost; enigma render recovers). The docs and
  the op-lock header no longer claim import holds the lock through
  fan-out. Holding the locks across the batch is tracked as #133.
- secret-op-lock.test.ts tracks its spawned workers and kills any still
  running in afterEach, so a failed assertion cannot leave a worker
  parked forever on a go file that teardown deleted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Clarit-AI-Inc

Copy link
Copy Markdown
Collaborator Author

Review fixes pushed (f116729), from an independent review:

  • Import claim narrowed. import holds each entry's operation lock for its write and index commit only; its batch render fan-out runs after the whole batch commits, outside the lock. The docs and the op-lock.ts header now say so, and list it under "Not guaranteed". No stored value can be lost this way; the failure is a missing rendered line that enigma render restores. Holding the locks across the batch: import: hold the per-secret operation lock across the batch render fan-out #133.
  • Race-test cleanup. secret-op-lock.test.ts now kills any worker still running in afterEach, so a failed assertion no longer leaves a process waiting forever. Verified by forcing the assertion to fail: no orphan worker remains.

Gate: lint, typecheck, leak-fence, audit, build, check:native pass; 1758 tests pass, 6 skipped; dist unchanged.

@Clarit-AI-Inc
Clarit-AI-Inc merged commit fc2773a into main Oct 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant