Files
mempalace/tests/test_wal.py
T
Igor Lins e Silva 7fb981c538 fix(daemon): address post-merge review feedback on #1826
Five fixes from the Copilot review of the merged daemon PR:

1. Privacy: the queue DB's SQLite WAL/SHM sidecars hold un-checkpointed
   verbatim payloads but were created with the caller's umask. Set the
   owner-only umask in run_server BEFORE DaemonRuntime builds the QueueStore
   (not only once the HTTP server starts), and harden any existing sidecars in
   QueueStore._init_db as defense-in-depth.

2. DoS guard: reject a negative Content-Length in the request reader.
   rfile.read(-1) would block until the client disconnects and bypass the
   MAX_BODY_BYTES cap.

3. Side effects: extract _wal_log (+ _ensure_wal, _WAL_FILE, _WAL_REDACT_KEYS)
   into a new side-effect-free mempalace/wal.py. The CLI sync path and the
   daemon service layer obtained _wal_log via `from .mcp_server import _wal_log`,
   which runs mcp_server's import-time stdio protection (os.dup2(2, 1);
   sys.stdout = sys.stderr) in a non-MCP process and misroutes operator output.
   mcp_server/cli/service now import from mempalace.wal.

4. Correctness: run_mcp_tool treated any dict as success. Write tools that
   return a bare {"error": ...} (e.g. tool_create_tunnel/tool_delete_tunnel
   validation) were recorded as succeeded; now the "error" key infers failure.

5. Hook budget: get_client_if_running()/health() take an explicit timeout, and
   the hook "is the daemon up?" precheck uses a short HOOK_PROBE_TIMEOUT (0.5s)
   so a wedged daemon can't stall the hook for the default 5s.

Adds tests/test_wal.py (import isolation + redaction) and daemon tests for the
umask ordering, negative Content-Length, run_mcp_tool error inference, and the
short probe timeout.
2026-06-19 11:56:09 -03:00

43 lines
1.5 KiB
Python

import subprocess
import sys
def test_wal_import_has_no_mcp_server_side_effect():
"""Importing mempalace.wal must NOT import mempalace.mcp_server.
mcp_server installs MCP stdio protection at import time (os.dup2(2, 1) and
sys.stdout = sys.stderr). The CLI sync path and the daemon service layer
obtain _wal_log from mempalace.wal precisely so they can audit writes
without triggering that process-global redirect. Run in a fresh subprocess
so the already-imported mcp_server in this test session can't mask a
regression.
"""
code = (
"import sys\n"
"import mempalace.wal\n"
"assert 'mempalace.mcp_server' not in sys.modules, "
"'importing mempalace.wal pulled in mempalace.mcp_server'\n"
"print('ok')\n"
)
result = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True)
assert result.returncode == 0, result.stderr
assert "ok" in result.stdout
def test_wal_log_redacts_and_writes(tmp_path, monkeypatch):
"""_wal_log lives in mempalace.wal now; smoke-test redaction + write there."""
import json
from mempalace import wal
wal_file = tmp_path / "wal" / "write_log.jsonl"
monkeypatch.setattr(wal, "_WAL_FILE", wal_file)
monkeypatch.setattr(wal, "_WAL_INITIALIZED_DIR", None)
wal._wal_log("op", {"entry": "secret diary text", "safe": "ok"})
entry = json.loads(wal_file.read_text().strip())
assert entry["operation"] == "op"
assert entry["params"]["entry"].startswith("[REDACTED")
assert entry["params"]["safe"] == "ok"