Skip to content
Python

fix(graph): remove the database sidecars when clearing checkpoints

Yijia-Xiaobbcd6661afa0
diff --git a/tests/test_checkpoint_lifecycle.py b/tests/test_checkpoint_lifecycle.py
index 1e5e2adf5..4933b5b67 100644
--- a/tests/test_checkpoint_lifecycle.py
+++ b/tests/test_checkpoint_lifecycle.py
@@ -153,3 +153,20 @@ def test_cli_style_usage_saves_then_resumes():
# Cleared on success -> a later run starts fresh.
assert checkpoint_step(tmp, "AAPL", "2026-05-08", sig) is None
+
+
+def test_clearing_removes_the_database_sidecars(tmp_path):
+ """SQLite writes -wal and -shm next to the database; leaving them behind
+ means a cleared checkpoint still has committed state on disk."""
+ from tradingagents.graph.checkpointer import clear_all_checkpoints
+
+ cp = tmp_path / "checkpoints"
+ cp.mkdir(parents=True)
+ for suffix in (".db", ".db-wal", ".db-shm"):
+ (cp / f"NVDA{suffix}").write_text("x")
+
+ cleared = clear_all_checkpoints(str(tmp_path))
+
+ assert cleared == 1
+ assert list(cp.iterdir()) == []
diff --git a/tradingagents/graph/checkpointer.py b/tradingagents/graph/checkpointer.py
index 750d8d12a..d40255fa4 100644
--- a/tradingagents/graph/checkpointer.py
+++ b/tradingagents/graph/checkpointer.py
@@ -71,13 +71,19 @@ def checkpoint_step(data_dir: str | Path, ticker: str, date: str, signature: str
def clear_all_checkpoints(data_dir: str | Path) -> int:
- """Remove all checkpoint DBs. Returns number of files deleted."""
+ """Remove all checkpoint databases. Returns the number of databases deleted.
+
+ SQLite keeps committed state in ``-wal`` and ``-shm`` files beside the
+ database, so deleting only the ``.db`` leaves a cleared checkpoint with data
+ still on disk.
+ """
cp_dir = Path(data_dir) / "checkpoints"
if not cp_dir.exists():
return 0
dbs = list(cp_dir.glob("*.db"))
for db in dbs:
- db.unlink()
+ for path in (db, *cp_dir.glob(f"{db.name}-*")):
+ path.unlink(missing_ok=True)
return len(dbs)