Skip to content

fix(audit): SQLite WAL mode for concurrent agents (#101) - #119

Merged
jaylfc merged 2 commits into
masterfrom
fix/audit-wal-1
Jun 7, 2026
Merged

fix(audit): SQLite WAL mode for concurrent agents (#101)#119
jaylfc merged 2 commits into
masterfrom
fix/audit-wal-1

Conversation

@jaylfc

@jaylfc jaylfc commented Jun 7, 2026

Copy link
Copy Markdown
Owner

What

Routes every sqlite3.connect(...) in the package through a new shared helper, taosmd/_db.py:connect(), which enables WAL journal mode and a 5s busy timeout on each connection.

Why

By default sqlite3.connect opens databases in rollback-journal mode. That mode takes an exclusive lock for the duration of a write and permits only one writer at a time, so when multiple agents (or multiple processes on the same machine) share a memory store, contention surfaces as SQLITE_BUSY / "database is locked" errors.

  • WAL (PRAGMA journal_mode=WAL) lets readers proceed concurrently with a writer instead of blocking, which is exactly the multi-machine / concurrent-agent access pattern taOSmd targets.
  • busy_timeout (PRAGMA busy_timeout=5000) makes a contended writer block-and-retry for up to 5s rather than failing immediately.

Both are plain PRAGMAs: no new dependencies, no benchmark impact. Standalone single-process behaviour is unchanged apart from the journal mode (and WAL silently stays memory on :memory: databases, so it is harmless there).

Call sites routed

archive.py, vector_memory.py, knowledge_graph.py, access_tracker.py, browsing_history.py, crystallize.py, pending_decisions.py, reflect.py, session_catalog.py, taosmd_backend.py (x2). Existing row_factory / other per-connection attributes are preserved (set on the returned connection as before).

Tests

Full suite green: 209 passed. Verified WAL + busy_timeout are actually applied on a real file DB and that :memory: stays in memory mode.

@coderabbitai

coderabbitai Bot commented Jun 7, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@jaylfc, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 58 minutes and 29 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: e861137b-efb5-4c18-946f-28b45ceb57f1

📥 Commits

Reviewing files that changed from the base of the PR and between 7f798aa and 81139f6.

📒 Files selected for processing (11)
  • taosmd/_db.py
  • taosmd/access_tracker.py
  • taosmd/archive.py
  • taosmd/browsing_history.py
  • taosmd/crystallize.py
  • taosmd/knowledge_graph.py
  • taosmd/pending_decisions.py
  • taosmd/reflect.py
  • taosmd/session_catalog.py
  • taosmd/taosmd_backend.py
  • taosmd/vector_memory.py
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/audit-wal-1

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread taosmd/_db.py
"""Open a SQLite connection in WAL mode with a busy timeout.

Drop-in replacement for ``sqlite3.connect(db_path)``. Callers that need a
``row_factory`` or other connection attributes should set them on the

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Hardcoded busy timeout (5000ms) should be configurable

Consider making this a parameter with a default value so callers can adjust based on their contention expectations:

Suggested change
``row_factory`` or other connection attributes should set them on the
BUSY_TIMEOUT_MS = 5000
def connect(db_path: Union[str, Path], busy_timeout_ms: int = BUSY_TIMEOUT_MS) -> sqlite3.Connection:

Then update the PRAGMA line to use the parameter.

Comment thread taosmd/_db.py Outdated
conn = sqlite3.connect(db_path)
# WAL is harmless (silently stays "memory") on ``:memory:`` databases.
conn.execute("PRAGMA journal_mode=WAL")
conn.execute(f"PRAGMA busy_timeout={BUSY_TIMEOUT_MS}")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: WAL mode PRAGMA result not checked

PRAGMA journal_mode=WAL returns the actual journal mode. On some filesystems (network drives, some FUSE mounts) WAL cannot be enabled and it silently falls back to DELETE mode. Check the result to ensure WAL was actually enabled:

result = conn.execute("PRAGMA journal_mode=WAL").fetchone()
if result[0] != "wal":
    logger.warning(f"WAL mode not enabled, got: {result[0]}")

Comment thread taosmd/_db.py
# WAL is harmless (silently stays "memory") on ``:memory:`` databases.
conn.execute("PRAGMA journal_mode=WAL")
conn.execute(f"PRAGMA busy_timeout={BUSY_TIMEOUT_MS}")
return conn

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

SUGGESTION: Use parameter binding for PRAGMA busy_timeout

While the value is a constant here, using parameter binding is safer practice and avoids any injection concerns if this becomes configurable:

Suggested change
return conn
conn.execute("PRAGMA busy_timeout=?", (busy_timeout_ms,))

Comment thread taosmd/taosmd_backend.py
"""Initialise settings database."""
Path(self._settings_db_path).parent.mkdir(parents=True, exist_ok=True)
self._conn = sqlite3.connect(self._settings_db_path)
self._conn = _db.connect(self._settings_db_path)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

WARNING: _ensure_conn creates a new connection without closing the old one

If _ensure_conn is called multiple times (e.g. after a connection error), the previous connection is leaked. Consider closing the existing connection first:

if self._conn is not None:
    self._conn.close()

This is a pre-existing issue but worth fixing since you're touching this code.

@kilo-code-bot

kilo-code-bot Bot commented Jun 7, 2026

Copy link
Copy Markdown

Code Review Summary

Status: 1 Issue Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 0
SUGGESTION 1
Issue Details (click to expand)

SUGGESTION

File Line Issue
taosmd/_db.py 26 Hardcoded busy timeout (5000ms) should be configurable
Other Observations (not in diff)

Issues found in unchanged code that cannot receive inline comments:

File Line Issue
taosmd/_db.py 26 Hardcoded busy timeout (5000ms) should be configurable
Files Reviewed (2 files changed, 9 files unchanged)
  • taosmd/_db.py - 1 issue (previously 4, 3 fixed)
  • taosmd/taosmd_backend.py - 0 issues (previously 1, fixed)
  • taosmd/access_tracker.py - No issues
  • taosmd/archive.py - No issues
  • taosmd/browsing_history.py - No issues
  • taosmd/crystallize.py - No issues
  • taosmd/knowledge_graph.py - No issues
  • taosmd/pending_decisions.py - No issues
  • taosmd/reflect.py - No issues
  • taosmd/session_catalog.py - No issues
  • taosmd/vector_memory.py - No issues

Reviewed by nemotron-3-ultra-550b-a55b-20260604:free · 144,216 tokens

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