diff --git a/pulse-memory/scripts/pulse_common.py b/pulse-memory/scripts/pulse_common.py index 23de646..dbdddce 100755 --- a/pulse-memory/scripts/pulse_common.py +++ b/pulse-memory/scripts/pulse_common.py @@ -90,10 +90,30 @@ def db_path() -> Path: return Path.home() / ".local/share/pulse-memory/pulse.sqlite" +STORE_MODE = 0o600 +STORE_SIDECARS = ("", "-wal", "-shm", "-journal") + + +def secure_store(path) -> list: + """Narrow the store and its SQLite sidecars to 0600 when wider. Data untouched.""" + narrowed = [] + for suffix in STORE_SIDECARS: + file = Path(f"{path}{suffix}") + try: + mode = file.stat().st_mode & 0o777 + except FileNotFoundError: + continue + if mode & ~STORE_MODE: + os.chmod(file, mode & STORE_MODE) + narrowed.append(file.name) + return narrowed + + def connect(): path = db_path() if not path.exists(): raise SystemExit(f"missing db: {path} (run pulse_init_db.py)") + secure_store(path) conn = sqlite3.connect(path) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys = ON") diff --git a/pulse-memory/scripts/pulse_init_db.py b/pulse-memory/scripts/pulse_init_db.py index 34c506b..650bfb5 100755 --- a/pulse-memory/scripts/pulse_init_db.py +++ b/pulse-memory/scripts/pulse_init_db.py @@ -8,7 +8,7 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) -from pulse_common import db_path, init_schema +from pulse_common import db_path, init_schema, secure_store def main(): @@ -19,12 +19,14 @@ def main(): path = db_path() path.parent.mkdir(parents=True, exist_ok=True) created = not path.exists() + secure_store(path) conn = sqlite3.connect(path) try: init_schema(conn) finally: conn.close() os.chmod(path, 0o600) + secure_store(path) print(json.dumps({"db": "created" if created else "ready", "mode": "0600"})) diff --git a/pulse-memory/skills/pulse-memory/references/store.md b/pulse-memory/skills/pulse-memory/references/store.md index 181fe75..92d26b2 100644 --- a/pulse-memory/skills/pulse-memory/references/store.md +++ b/pulse-memory/skills/pulse-memory/references/store.md @@ -2,7 +2,7 @@ Do not dump transcripts into the context window. Keep **archive** (append-only) and **memory** (admitted, tagged) as two tables. Pattern from `agent-local-db`: WAL, best-effort writes, never block the agent loop. -Default file: $XDG_DATA_HOME/tstack/state.sqlite, or ~/.local/share/tstack/state.sqlite when XDG_DATA_HOME is unset. TSTACK_STATE_DB overrides, else PULSE_MEMORY_DB. Writes, FTS rebuild, and migrations go through tstack-state (see references/state.md in the tstack plugin). Do not move or delete ~/.local/share/pulse-memory/pulse.sqlite. Point PULSE_MEMORY_DB at that file to keep using it. Mode 0600. +Default file: $XDG_DATA_HOME/tstack/state.sqlite, or ~/.local/share/tstack/state.sqlite when XDG_DATA_HOME is unset. TSTACK_STATE_DB overrides, else PULSE_MEMORY_DB. Writes, FTS rebuild, and migrations go through tstack-state (see references/state.md in the tstack plugin). Do not move or delete ~/.local/share/pulse-memory/pulse.sqlite. Point PULSE_MEMORY_DB at that file to keep using it. Mode 0600: init and every open narrow a wider store and its -wal, -shm, -journal to 0600; rows untouched. ## Tools diff --git a/pulse-memory/test/test_retrieve.py b/pulse-memory/test/test_retrieve.py index 820bfa6..c706a8f 100755 --- a/pulse-memory/test/test_retrieve.py +++ b/pulse-memory/test/test_retrieve.py @@ -366,6 +366,56 @@ def test_init_db_creates_private_store(self): self.assertEqual(admitted.stdout.strip(), "inserted") tmp.cleanup() + def test_wide_store_narrowed_to_0600_data_kept(self): + tmp = tempfile.TemporaryDirectory() + db = Path(tmp.name) / "pulse.sqlite" + env = os.environ.copy() + env["PULSE_MEMORY_DB"] = str(db) + self.assertEqual(self.run_py("pulse_init_db.py", env=env).returncode, 0) + self.run_py("pulse_admit.py", "a/b", "kept", "test", env=env) + conn = sqlite3.connect(db) + before = conn.execute( + "SELECT nat_key, snippet, verify_hash FROM memory_traces").fetchall() + conn.close() + sidecars = [Path(f"{db}-wal"), Path(f"{db}-shm")] + for file in [db, *sidecars]: + file.touch() + os.chmod(file, 0o644) + admitted = self.run_py("pulse_admit.py", "a/c", "next", "test", env=env) + self.assertEqual(admitted.stdout.strip(), "inserted", admitted.stderr) + for file in [db, *sidecars]: + if file.exists(): + self.assertEqual(file.stat().st_mode & 0o777, 0o600, file.name) + os.chmod(db, 0o664) + ready = self.run_py("pulse_init_db.py", env=env) + self.assertEqual(json.loads(ready.stdout)["db"], "ready") + self.assertEqual(db.stat().st_mode & 0o777, 0o600) + conn = sqlite3.connect(db) + after = conn.execute( + "SELECT nat_key, snippet, verify_hash FROM memory_traces WHERE nat_key='a/b'" + ).fetchall() + self.assertEqual(before, after) + self.assertEqual(conn.execute("SELECT COUNT(*) FROM memory_traces").fetchone()[0], 2) + conn.close() + tmp.cleanup() + + def test_secure_store_never_widens(self): + from pulse_common import secure_store + tmp = tempfile.TemporaryDirectory() + db = Path(tmp.name) / "pulse.sqlite" + db.touch() + os.chmod(db, 0o400) + self.assertEqual(secure_store(db), []) + self.assertEqual(db.stat().st_mode & 0o777, 0o400) + os.chmod(db, 0o755) + wal = Path(f"{db}-wal") + wal.touch() + os.chmod(wal, 0o644) + self.assertEqual(secure_store(db), ["pulse.sqlite", "pulse.sqlite-wal"]) + self.assertEqual(db.stat().st_mode & 0o777, 0o600) + self.assertEqual(wal.stat().st_mode & 0o777, 0o600) + tmp.cleanup() + def test_locked_disagreement_forks_without_averaging(self): tmp, conn = fresh() admit(conn, "trace", "alpha", source="test", lock="locked") diff --git a/tstack/scripts/tstack-state b/tstack/scripts/tstack-state index d3ecd7c..705cf8e 100755 --- a/tstack/scripts/tstack-state +++ b/tstack/scripts/tstack-state @@ -105,6 +105,17 @@ def resolve_path(): return Path.home() / ".local" / "share" / "tstack" / "state.sqlite" +def narrow_store(path): + for suffix in ("", "-wal", "-shm", "-journal"): + file = Path(f"{path}{suffix}") + try: + mode = file.stat().st_mode & 0o777 + except FileNotFoundError: + continue + if mode & ~0o600: + os.chmod(file, mode & 0o600) + + def connect(): os.umask(0o077) path = resolve_path() @@ -116,6 +127,7 @@ def connect(): if created_parent: os.chmod(parent, 0o700) created_file = not path.exists() + narrow_store(path) conn = sqlite3.connect(path) conn.isolation_level = None conn.row_factory = sqlite3.Row diff --git a/tstack/test/test-state.sh b/tstack/test/test-state.sh index fdf78f0..20ec56c 100755 --- a/tstack/test/test-state.sh +++ b/tstack/test/test-state.sh @@ -264,6 +264,20 @@ else bad "path existing dir" fi +wide="$(mktemp -d)/pulse.sqlite" +env -u TSTACK_STATE_DB PULSE_MEMORY_DB="$wide" "$STATE" init >/dev/null +chmod 644 "$wide" +touch "$wide-wal" "$wide-shm" +chmod 644 "$wide-wal" "$wide-shm" +if env -u TSTACK_STATE_DB PULSE_MEMORY_DB="$wide" "$STATE" init >/dev/null; then + [[ "$(mode "$wide")" == "600" ]] && ok "existing store narrowed 600" || bad "existing store mode $(mode "$wide")" + for f in "$wide-wal" "$wide-shm"; do + [[ ! -e "$f" || "$(mode "$f")" == "600" ]] && ok "sidecar 600 ${f##*-}" || bad "sidecar mode ${f##*-}" + done +else + bad "existing store init" +fi + echo "---" [[ "$fail" -eq 0 ]] || { echo "$fail failure(s)"; exit 1; } echo "ALL CHECKS PASSED"