Skip to content

refactor(sqlite): one shared operator write-lock poll for store and auth - #1074

Merged
jimhoyd merged 1 commit into
mainfrom
claude/shared-operator-lock-wait
Sep 30, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
claude/shared-operator-lock-wait

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

Part of #1041

What

#1071 (packages/auth, openAuthDatabase(path, {operator})) and #1073 (packages/store/src/database.ts, openStoreDatabase(path, {operator: true})) each added their own copy of the same logic: an operator connection that retries BEGIN IMMEDIATE every ~1 ms with busy_timeout=0 up to a 10 s deadline, then restores the timeout, plus a SQLITE_BUSY (extended codes included) check.

This moves it into one helper in core's @jimhoyd/urlcode/sqlite subpath (defined in packages/core/src/server-lock.ts, re-exported by packages/core/src/sqlite.ts):

beginImmediateWithin<T>(db: DatabaseSync, waitMs: number, begin: () => T): T
  • Store: beginImmediate now calls it with () => db.exec('BEGIN IMMEDIATE').
  • Auth: the begin statement proxy's run calls it with () => target.run() (Better Auth's dialect still gets the statement's own run() result).
  • Core imports neither package. The server lock's own busy check, which matches the primary code only, is left as it is.
  • Behaviour is identical: same deadline, poll and timeout restore. The thrown error is SQLite's own, rethrown unchanged, so each package surfaces the same messages and codes. Each package keeps its exported OPERATOR_LOCK_WAIT_MS.
  • The two byte-identical slow-commit-writer.ts fixtures become one test/slow-commit-writer.ts. The only change in the two tests is the path they spawn.
  • Docs: a new row in the docs/EXTENSIONS.md SQLite helpers table. The RIM-STORE-PERSIST-001 card lists beginImmediateWithin, describes the operator poll and cites the operator-lock tests. llms-full.txt is regenerated.

Measure (git diff --numstat origin/main)

File + -
packages/core/src/server-lock.ts 30 0
packages/core/src/sqlite.ts 2 2
packages/auth/src/auth.ts 4 29
packages/store/src/database.ts 4 30
packages/auth/test/slow-commit-writer.ts 0 24
{packages/store/test => test}/slow-commit-writer.ts 2 2
packages/auth/test/auth.test.ts 1 1
packages/store/test/operator-lock.test.ts 2 2
docs/EXTENSIONS.md, docs/RUNTIME-IMPLEMENTATION.md, llms-full.txt 3 1
Total 48 91

Net -43 lines (code and tests alone: +45 / -90).

Evidence

  • packages/store/test/operator-lock.test.ts and packages/auth/test/auth.test.ts (the create-user-beside-a-slow-committing-server test) pass. Apart from the fixture path, neither test changed.
  • npm run verify is green locally (macOS). The first run failed only on the stale llms-full.txt check, which npm run docs:llms regenerated.
  • Not verified here: the Windows slow-flush case these tests model. CI covers the platforms it runs.

🤖 Generated with Claude Code

#1071 (auth) and #1073 (store) each carried a copy of the same operator
connection logic: retry BEGIN IMMEDIATE every millisecond with busy_timeout 0
up to a 10 s deadline, then restore the timeout, plus a SQLITE_BUSY check.
Move it into core's @jimhoyd/urlcode/sqlite subpath as
beginImmediateWithin(db, waitMs, begin) and call it from both packages. The
error surfaced is still SQLite's own, rethrown unchanged. The identical
slow-commit writer fixtures become one test/slow-commit-writer.ts.

Part of #1041

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd enabled auto-merge (squash) September 30, 2026 18:09
@jimhoyd
jimhoyd merged commit 0d8ae1e into main Sep 30, 2026
23 checks passed
@jimhoyd
jimhoyd deleted the claude/shared-operator-lock-wait branch September 30, 2026 18:16
jimhoyd added a commit that referenced this pull request Sep 30, 2026
jimhoyd added a commit that referenced this pull request Sep 30, 2026
…ter recent merges (#1076)

- FRAMEWORK.md, STORE.md: urlcode benchmark was removed (#1048); test runs
  are urlcode test and audit.
- STORE.md: quote the server-lock refusal as core now words it (#1069).
- TYPESCRIPT.md: list beginImmediateWithin on @jimhoyd/urlcode/sqlite (#1074).
- store CHANGELOG (unreleased): several servers need a database server the
  bundled store does not use, not one URLCode does not provide (#1052).
- llms-full.txt regenerated.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

1 participant