Skip to content

fix: serialize credential writers and sync atomic state writes - #84

Merged
OBenner merged 1 commit into
mainfrom
feat/15-credential-durability
Nov 16, 2026
Merged

OBenner merged 1 commit into
mainfrom
feat/15-credential-durability

Conversation

@OBenner

@OBenner OBenner commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Concurrent credential writers could overwrite each other's hashes or issue multiple tokens during parallel init. All CredentialStore mutations now hold a per-project exclusive lock through read/modify/write, token-file updates and audit. Atomic state writes sync and close the temporary file before rename; POSIX also syncs the parent directory.

Closes #15.

Validation: pnpm verify passed (93 files / 808 tests, format/import checks/typecheck/build). Regression tests reproduced failures before the fix. Coverage includes independent stores, four real writer processes exercising issue/ensure/rotate/revoke, stale-lock refusal and exception cleanup, injected write/sync/rename failures, and a real child killed before rename retaining the previous valid JSON. Existing audit and permission diagnostics pass.

Limits: the lock is cooperative, waits at most 10 seconds and deliberately never steals stale-looking locks. A killed writer requires the documented operator recovery after stopping all project writers. Windows has no directory fsync via Node; no guarantee of rename durability after power loss. Credential JSON and token files remain separate transactions. No dependencies, protocol changes or live credential mutations. Historical security review is preserved; L2/L3 closure evidence is in the new follow-up document.

@OBenner OBenner left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev review — ACCEPT.

Проверено по диффу (fefbfbd, base e25fa04) и прогону на ветке (worktree review-84): file-lock 2/2, credential-concurrency 3/3 (включая реальную 4-процессную гонку), atomic-durability 3/3, atomic-crash 1/1, atomic-file 1/1 — 13/13 PASS. CI 3/3 зелёный (run 43850126018). git merge-tree с текущим main — чисто.

Проверки по существу:

  1. Серилизация: единый lock на credentials.json покрывает read-modify-write + token-file update + audit record для issue/ensure/rotate/revoke. Рекурсивного лока нет — ensureCredential зовёт issueLocked внутри своего lock, а не issue (иначе self-EEXIST до timeout).
  2. Долговечность: writeTextAtomic теперь fsync файла до rename + directory sync на POSIX; пропуск dir-sync на Windows задокументирован в durability-заметке. Crash-тест kill'ит реальный child до rename — хвостов .tmp не остаётся, состояние консистентно.
  3. Fail-closed: stale lock → STORAGE_FAILURE через 10 с с явной инструкцией оператору; лок никогда не крадётся по PID/возрасту — осознанный cooperative-контракт, зафиксирован в limitations.
  4. authenticate остаётся lock-free read-only — верно, читатели не должны ждать writers.
  5. Без новых зависимостей; wire и спека не затронуты.

Неблокирующие замечания:

  • В finally withFileLock unlink lock'а после throw из action может замаскировать исходное исключение при ошибке unlink — приемлемо для cooperative lock, но можно обернуть позже.
  • docs/security/credential-durability-2026-11-16.md добавлен staff1 в зоне владения security — прошу security подтвердить документ при re-check (ниже).

Прошу security сделать re-check как владельца исходных находок L2/L3. После него передаю CTO на финал.

@OBenner
OBenner merged commit 8266b42 into main Nov 16, 2026
3 checks passed
@OBenner
OBenner deleted the feat/15-credential-durability branch November 16, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P2] L-findings: credentials.json lock, fsync discipline

1 participant