feat(mempalace): retention enforcement + tunnel sync client (#1083, #1078)
Some checks failed
CI / test (pull_request) Failing after 7s
CI / validate (pull_request) Failing after 3s
Review Approval Gate / verify-review (pull_request) Failing after 4s

**retain_closets.py** — 90-day closet aging enforcement for #1083.
Removes *.closet.json files older than --days (default 90) from the
fleet palace. Supports --dry-run for safe preview. Wired into the
weekly-audit workflow as a dry-run CI step; production cron guidance
added to workflow comments.

**tunnel_sync.py** — remote wizard wing pull client for #1078.
Connects to a peer's fleet_api.py HTTP endpoint, discovers wings via
/wings, and pulls core rooms via /search into local *.closet.json
files. Zero new dependencies (stdlib urllib only). Supports --dry-run.
This is the code side of the inter-wizard tunnel; infrastructure
(second wizard VPS + fleet_api.py running) still required.

**Tests:** 29 new tests, all passing. Total suite: 294 passing.

Refs #1075, #1078, #1083
This commit is contained in:
Alexander Whitestone
2026-04-07 11:05:00 -04:00
parent b445c04037
commit e644b00dff
5 changed files with 822 additions and 1 deletions

View File

@@ -0,0 +1,139 @@
"""
Tests for mempalace/retain_closets.py — 90-day closet retention enforcement.
Refs: #1083, #1075
"""
from __future__ import annotations
import json
import time
from pathlib import Path
import pytest
from mempalace.retain_closets import (
RetentionResult,
_file_age_days,
enforce_retention,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _write_closet(directory: Path, name: str, age_days: float) -> Path:
"""Create a *.closet.json file with a mtime set to *age_days* ago."""
p = directory / name
p.write_text(json.dumps({"drawers": [{"text": "summary", "closet": True}]}))
# Set mtime to simulate age
mtime = time.time() - age_days * 86400.0
import os
os.utime(p, (mtime, mtime))
return p
# ---------------------------------------------------------------------------
# _file_age_days
# ---------------------------------------------------------------------------
def test_file_age_days_recent(tmp_path):
p = tmp_path / "recent.closet.json"
p.write_text("{}")
age = _file_age_days(p)
assert 0 <= age < 1 # just created
def test_file_age_days_old(tmp_path):
p = _write_closet(tmp_path, "old.closet.json", age_days=100)
age = _file_age_days(p)
assert 99 < age < 101
# ---------------------------------------------------------------------------
# enforce_retention — dry_run
# ---------------------------------------------------------------------------
def test_dry_run_does_not_delete(tmp_path):
old = _write_closet(tmp_path, "old.closet.json", age_days=100)
_write_closet(tmp_path, "new.closet.json", age_days=10)
result = enforce_retention(tmp_path, retention_days=90, dry_run=True)
# File still exists after dry-run
assert old.exists()
assert result.removed == 1 # counted but not actually removed
assert result.kept == 1
assert result.ok
def test_dry_run_keeps_recent_files(tmp_path):
_write_closet(tmp_path, "recent.closet.json", age_days=5)
result = enforce_retention(tmp_path, retention_days=90, dry_run=True)
assert result.removed == 0
assert result.kept == 1
# ---------------------------------------------------------------------------
# enforce_retention — live mode
# ---------------------------------------------------------------------------
def test_live_removes_old_closets(tmp_path):
old = _write_closet(tmp_path, "old.closet.json", age_days=100)
new = _write_closet(tmp_path, "new.closet.json", age_days=10)
result = enforce_retention(tmp_path, retention_days=90, dry_run=False)
assert not old.exists()
assert new.exists()
assert result.removed == 1
assert result.kept == 1
assert result.ok
def test_live_keeps_files_within_window(tmp_path):
f = _write_closet(tmp_path, "edge.closet.json", age_days=89)
result = enforce_retention(tmp_path, retention_days=90, dry_run=False)
assert f.exists()
assert result.removed == 0
assert result.kept == 1
def test_empty_directory_is_ok(tmp_path):
result = enforce_retention(tmp_path, retention_days=90)
assert result.scanned == 0
assert result.removed == 0
assert result.ok
def test_subdirectory_closets_are_pruned(tmp_path):
"""enforce_retention should recurse into subdirs (wing directories)."""
sub = tmp_path / "bezalel"
sub.mkdir()
old = _write_closet(sub, "hermes.closet.json", age_days=120)
result = enforce_retention(tmp_path, retention_days=90, dry_run=False)
assert not old.exists()
assert result.removed == 1
def test_non_closet_files_ignored(tmp_path):
"""Non-closet files should not be counted or touched."""
(tmp_path / "readme.txt").write_text("hello")
(tmp_path / "data.drawer.json").write_text("{}")
result = enforce_retention(tmp_path, retention_days=90)
assert result.scanned == 0
# ---------------------------------------------------------------------------
# RetentionResult.ok
# ---------------------------------------------------------------------------
def test_retention_result_ok_with_no_errors():
r = RetentionResult(scanned=5, removed=2, kept=3)
assert r.ok is True
def test_retention_result_not_ok_with_errors():
r = RetentionResult(errors=["could not stat file"])
assert r.ok is False

View File

@@ -0,0 +1,205 @@
"""
Tests for mempalace/tunnel_sync.py — remote wizard wing sync client.
Refs: #1078, #1075
"""
from __future__ import annotations
import json
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from mempalace.tunnel_sync import (
SyncResult,
_peer_url,
_write_closet,
get_remote_wings,
search_remote_room,
sync_peer,
)
# ---------------------------------------------------------------------------
# _peer_url
# ---------------------------------------------------------------------------
def test_peer_url_strips_trailing_slash():
assert _peer_url("http://host:7771/", "/wings") == "http://host:7771/wings"
def test_peer_url_with_path():
assert _peer_url("http://host:7771", "/search") == "http://host:7771/search"
# ---------------------------------------------------------------------------
# get_remote_wings
# ---------------------------------------------------------------------------
def test_get_remote_wings_returns_list():
with patch("mempalace.tunnel_sync._get", return_value={"wings": ["bezalel", "timmy"]}):
wings = get_remote_wings("http://peer:7771")
assert wings == ["bezalel", "timmy"]
def test_get_remote_wings_empty():
with patch("mempalace.tunnel_sync._get", return_value={"wings": []}):
wings = get_remote_wings("http://peer:7771")
assert wings == []
# ---------------------------------------------------------------------------
# search_remote_room
# ---------------------------------------------------------------------------
def _make_entry(text: str, room: str = "forge", wing: str = "bezalel", score: float = 0.9) -> dict:
return {"text": text, "room": room, "wing": wing, "score": score}
def test_search_remote_room_deduplicates():
entry = _make_entry("CI passed")
# Same entry returned from multiple queries — should only appear once
with patch("mempalace.tunnel_sync._get", return_value={"results": [entry]}):
results = search_remote_room("http://peer:7771", "forge", n=50)
assert len(results) == 1
assert results[0]["text"] == "CI passed"
def test_search_remote_room_respects_n_limit():
entries = [_make_entry(f"item {i}") for i in range(100)]
with patch("mempalace.tunnel_sync._get", return_value={"results": entries}):
results = search_remote_room("http://peer:7771", "forge", n=5)
assert len(results) <= 5
def test_search_remote_room_handles_request_error():
import urllib.error
with patch("mempalace.tunnel_sync._get", side_effect=urllib.error.URLError("refused")):
results = search_remote_room("http://peer:7771", "forge")
assert results == []
# ---------------------------------------------------------------------------
# _write_closet
# ---------------------------------------------------------------------------
def test_write_closet_creates_file(tmp_path):
entries = [_make_entry("a memory")]
ok = _write_closet(tmp_path, "bezalel", "forge", entries, dry_run=False)
assert ok is True
closet = tmp_path / "bezalel" / "forge.closet.json"
assert closet.exists()
data = json.loads(closet.read_text())
assert data["wing"] == "bezalel"
assert data["room"] == "forge"
assert len(data["drawers"]) == 1
assert data["drawers"][0]["closet"] is True
assert data["drawers"][0]["text"] == "a memory"
def test_write_closet_dry_run_does_not_create(tmp_path):
entries = [_make_entry("a memory")]
ok = _write_closet(tmp_path, "bezalel", "forge", entries, dry_run=True)
assert ok is True
closet = tmp_path / "bezalel" / "forge.closet.json"
assert not closet.exists()
def test_write_closet_creates_wing_subdirectory(tmp_path):
entries = [_make_entry("memory")]
_write_closet(tmp_path, "timmy", "hermes", entries, dry_run=False)
assert (tmp_path / "timmy").is_dir()
def test_write_closet_source_file_is_tunnel_tagged(tmp_path):
entries = [_make_entry("memory")]
_write_closet(tmp_path, "bezalel", "hermes", entries, dry_run=False)
closet = tmp_path / "bezalel" / "hermes.closet.json"
data = json.loads(closet.read_text())
assert data["drawers"][0]["source_file"].startswith("tunnel:")
# ---------------------------------------------------------------------------
# sync_peer
# ---------------------------------------------------------------------------
def _mock_get_responses(peer_url: str) -> dict:
"""Minimal mock _get returning health, wings, and search results."""
def _get(url: str) -> dict:
if url.endswith("/health"):
return {"status": "ok", "palace": "/var/lib/mempalace/fleet"}
if url.endswith("/wings"):
return {"wings": ["bezalel"]}
if "/search" in url:
return {"results": [_make_entry("test memory")]}
return {}
return _get
def test_sync_peer_writes_closets(tmp_path):
(tmp_path / ".gitkeep").touch() # ensure palace dir exists
with patch("mempalace.tunnel_sync._get", side_effect=_mock_get_responses("http://peer:7771")):
result = sync_peer("http://peer:7771", tmp_path, n_results=10)
assert result.ok
assert "bezalel" in result.wings_found
assert result.closets_written > 0
def test_sync_peer_dry_run_no_files(tmp_path):
(tmp_path / ".gitkeep").touch()
with patch("mempalace.tunnel_sync._get", side_effect=_mock_get_responses("http://peer:7771")):
result = sync_peer("http://peer:7771", tmp_path, n_results=10, dry_run=True)
assert result.ok
# No closet files should be written
closets = list(tmp_path.rglob("*.closet.json"))
assert closets == []
def test_sync_peer_unreachable_returns_error(tmp_path):
import urllib.error
with patch("mempalace.tunnel_sync._get", side_effect=urllib.error.URLError("refused")):
result = sync_peer("http://unreachable:7771", tmp_path)
assert not result.ok
assert any("unreachable" in e or "refused" in e for e in result.errors)
def test_sync_peer_unhealthy_returns_error(tmp_path):
with patch("mempalace.tunnel_sync._get", return_value={"status": "degraded"}):
result = sync_peer("http://peer:7771", tmp_path)
assert not result.ok
assert any("unhealthy" in e for e in result.errors)
def test_sync_peer_no_wings_is_ok(tmp_path):
def _get(url: str) -> dict:
if "/health" in url:
return {"status": "ok"}
return {"wings": []}
with patch("mempalace.tunnel_sync._get", side_effect=_get):
result = sync_peer("http://peer:7771", tmp_path)
assert result.ok
assert result.closets_written == 0
# ---------------------------------------------------------------------------
# SyncResult.ok
# ---------------------------------------------------------------------------
def test_sync_result_ok_no_errors():
r = SyncResult(wings_found=["bezalel"], rooms_pulled=5, closets_written=5)
assert r.ok is True
def test_sync_result_not_ok_with_errors():
r = SyncResult(errors=["connection refused"])
assert r.ok is False