Stage 3 of the realism migration. Replaces orchestrator/scheduler.py's
hardcoded _FILE_TEMPLATES/_USERS (3 templates emitting epoch-suffixed
filenames like notes-1777315854.txt with identical bodies per
template) with a persona-driven realism engine.
New surface:
- SyntheticFile SQLModel (synthetic_files table, UNIQUE on
decky_uuid+path) — per-(decky, path) state for the future
edit-in-place flow. Pre-v1, no _migrate_* helper.
- BaseRepository methods: record_synthetic_file,
update_synthetic_file, list_synthetic_files,
pick_random_synthetic_file_for_edit (used by stage 3b).
- realism/naming.py: per-content-class filename templates,
persona-conditioned. /var/log/cron.log + logrotate skeleton for
system-class; /home/<persona>/TODO.md, scratch.md, etc. for
user-class. Anti-regression test pins "no 8+ digit decimals in
basenames" (the realism failure today).
- realism/bodies.py: deterministic body templates per content_class.
TODO body uses checkbox markdown, script body has a shebang, cron
body matches syslog cron shape ("CRON[PID]: (user) CMD (...)").
- realism/planner.py: pick(deckies, now, rng) returns a Plan.
Diurnal-gated, weighted user/system content split (70/30 user
bias). Create-only in stage 3; edit branch lands in stage 3b.
Scheduler split:
- scheduler.pick is now traffic-only (sync).
- scheduler.pick_file is async, takes a repo, resolves personas
(Topology.email_personas for topology-source deckies; global
realism.personas_pool otherwise), and maps Plan -> FileAction.
- FileAction gains persona/content_class/mtime fields.
Worker:
- _one_tick rolls 50/50 between traffic and file each tick. After a
successful FileAction plant, _record_synthetic_file persists or
patches the synthetic_files row (catching the unique-constraint
collision on re-plant of the same path).
- SSHDriver._run_file passes action.mtime through to plant_file so
files don't all stamp at wall-clock-now.
117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
"""record / update / list / pick-for-edit on the synthetic_files table.
|
|
|
|
Stage 3 of the realism migration introduces the synthetic_files
|
|
table for per-(decky, path) state. Tests pin the contract on a
|
|
real :class:`SQLiteRepository` so SQLModel schema bugs surface here
|
|
rather than in production.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
from datetime import datetime, timedelta, timezone
|
|
|
|
import pytest
|
|
import pytest_asyncio
|
|
|
|
from decnet.web.db.sqlite.repository import SQLiteRepository
|
|
|
|
|
|
@pytest_asyncio.fixture
|
|
async def repo(tmp_path):
|
|
r = SQLiteRepository(db_path=str(tmp_path / "decnet.db"))
|
|
await r.initialize()
|
|
yield r
|
|
await r.engine.dispose()
|
|
|
|
|
|
def _row(
|
|
decky: str = "d1",
|
|
path: str = "/home/admin/TODO.md",
|
|
persona: str = "admin",
|
|
cls: str = "todo",
|
|
body: str = "- [ ] rotate keys\n",
|
|
ts: datetime | None = None,
|
|
) -> dict:
|
|
now = ts or datetime.now(timezone.utc)
|
|
return {
|
|
"decky_uuid": decky,
|
|
"path": path,
|
|
"persona": persona,
|
|
"content_class": cls,
|
|
"created_at": now,
|
|
"last_modified": now,
|
|
"edit_count": 0,
|
|
"content_hash": hashlib.sha256(body.encode()).hexdigest(),
|
|
"last_body": body,
|
|
}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_record_returns_uuid(repo):
|
|
uuid = await repo.record_synthetic_file(_row())
|
|
assert isinstance(uuid, str) and uuid
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_unique_constraint_on_decky_path(repo):
|
|
await repo.record_synthetic_file(_row())
|
|
with pytest.raises(Exception):
|
|
await repo.record_synthetic_file(_row())
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_update_synthetic_file_patches_fields(repo):
|
|
uuid = await repo.record_synthetic_file(_row())
|
|
await repo.update_synthetic_file(
|
|
uuid,
|
|
{"edit_count": 1, "last_body": "- [x] rotate keys\n"},
|
|
)
|
|
listing = await repo.list_synthetic_files(decky_uuid="d1")
|
|
assert len(listing) == 1
|
|
assert listing[0]["edit_count"] == 1
|
|
assert listing[0]["last_body"].startswith("- [x]")
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_list_filters_by_decky_and_persona(repo):
|
|
await repo.record_synthetic_file(_row(decky="d1", path="/a"))
|
|
await repo.record_synthetic_file(_row(decky="d1", path="/b", persona="ubuntu"))
|
|
await repo.record_synthetic_file(_row(decky="d2", path="/c"))
|
|
|
|
by_decky = await repo.list_synthetic_files(decky_uuid="d1")
|
|
assert {r["path"] for r in by_decky} == {"/a", "/b"}
|
|
|
|
by_persona = await repo.list_synthetic_files(decky_uuid="d1", persona="ubuntu")
|
|
assert {r["path"] for r in by_persona} == {"/b"}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pick_random_returns_none_when_empty(repo):
|
|
assert await repo.pick_random_synthetic_file_for_edit("d-empty") is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pick_random_excludes_canary_classes(repo):
|
|
# Canary-class files are stored on the same table (stage 7) but
|
|
# the editor must skip them — their bodies are binary blobs.
|
|
await repo.record_synthetic_file(_row(cls="canary_aws_creds"))
|
|
picked = await repo.pick_random_synthetic_file_for_edit("d1")
|
|
assert picked is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pick_random_excludes_too_old_rows(repo):
|
|
old = datetime.now(timezone.utc) - timedelta(days=120)
|
|
await repo.record_synthetic_file(_row(ts=old))
|
|
picked = await repo.pick_random_synthetic_file_for_edit("d1", max_age_days=30)
|
|
assert picked is None
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_pick_random_returns_eligible_row(repo):
|
|
await repo.record_synthetic_file(_row(cls="todo"))
|
|
picked = await repo.pick_random_synthetic_file_for_edit("d1")
|
|
assert picked is not None
|
|
assert picked["content_class"] == "todo"
|
|
assert picked["path"] == "/home/admin/TODO.md"
|