Skip to content

The agent that grows with you

fix(delegation): preserve state database journal mode

fangliquanflq4a8b4d43a317
diff --git a/tests/tools/test_async_delegation.py b/tests/tools/test_async_delegation.py
index d91ff879903..4bc5af595e5 100644
--- a/tests/tools/test_async_delegation.py
+++ b/tests/tools/test_async_delegation.py
@@ -8,6 +8,7 @@ formatting, capacity rejection, and crash handling.
import json
import os
import queue
+import sqlite3
import subprocess
import sys
import threading
@@ -65,6 +66,26 @@ def _drain_for(delegation_id, timeout=5.0):
return None
+def test_schema_init_preserves_shared_state_db_journal_mode(tmp_path, monkeypatch):
+ """The delegation ledger is a guest in state.db, not its mode owner."""
+ import hermes_state
+
+ monkeypatch.setattr(hermes_state, "is_sqlite_wal_reset_vulnerable", lambda: False)
+ conn = sqlite3.connect(tmp_path / "state.db")
+ try:
+ assert conn.execute("PRAGMA journal_mode=DELETE").fetchone()[0] == "delete"
+
+ ad._initialize_schema(conn)
+
+ assert conn.execute("PRAGMA journal_mode").fetchone()[0] == "delete"
+ assert conn.execute(
+ "SELECT name FROM sqlite_master WHERE type='table' "
+ "AND name='async_delegations'"
+ ).fetchone() == ("async_delegations",)
+ finally:
+ conn.close()
+
+
def test_active_for_session_counts_every_live_delegation_state():
with ad._records_lock:
ad._records.update(
diff --git a/tools/async_delegation.py b/tools/async_delegation.py
index 4965363a07e..5025a0a2715 100644
--- a/tools/async_delegation.py
+++ b/tools/async_delegation.py
@@ -140,9 +140,11 @@ def _connect() -> sqlite3.Connection:
def _initialize_schema(conn: sqlite3.Connection) -> None:
- from hermes_state import apply_wal_with_fallback
-
- apply_wal_with_fallback(conn, db_label="state.db (async_delegation)")
+ # state.db's owning SessionDB connection establishes the configured journal
+ # mode. This secondary durability ledger must preserve that mode: applying
+ # WAL here on every short-lived connection requires an exclusive lock when
+ # the file is not already WAL and can collide with live transcript/FTS
+ # writers. The ledger works in either WAL or DELETE mode.
conn.execute(
"""CREATE TABLE IF NOT EXISTS async_delegations (
delegation_id TEXT PRIMARY KEY,