feat(realism): operator-tunable planner weights via realism_config

New realism_config table (uuid PK + unique key) + two repo methods
(get/set) backs an admin-only GET/PUT /api/v1/realism/config surface.

The planner now exposes apply_payload(payload) / current_payload() /
reset_to_defaults() and reads its weights through mutable module
globals; pick() resolves the live values each call. Validation
catches negative weights, zero totals, out-of-range canary_probability,
unknown content_class names, and silently drops cross-list entries
(canary class on the user list, etc).

The orchestrator worker calls _refresh_realism_config(repo) on
startup and every 5 ticks (~5min at 60s interval). Operator changes
land within one refresh window with no bus signal — the simpler path
for a knob whose latency tolerance is minutes.
This commit is contained in:
2026-04-27 18:00:08 -04:00
parent da3c35c6a4
commit 2cc60bd677
12 changed files with 711 additions and 9 deletions

View File

@@ -0,0 +1,74 @@
"""The orchestrator pulls operator-tuned weights from realism_config.
§3c contract: the planner reads in-memory module globals, but the
operator's tuning lives in the DB (admin PUT /api/v1/realism/config).
The orchestrator worker bridges the two by calling
``_refresh_realism_config(repo)`` at startup and every Nth tick.
"""
from __future__ import annotations
import json
from unittest.mock import AsyncMock
import pytest
from decnet.orchestrator.worker import _refresh_realism_config
from decnet.realism import planner
@pytest.fixture(autouse=True)
def _reset_planner():
yield
planner.reset_to_defaults()
@pytest.mark.asyncio
async def test_refresh_no_row_keeps_defaults():
repo = AsyncMock()
repo.get_realism_config = AsyncMock(return_value=None)
await _refresh_realism_config(repo)
assert planner.current_payload()["canary_probability"] == pytest.approx(0.03)
@pytest.mark.asyncio
async def test_refresh_applies_stored_payload():
repo = AsyncMock()
repo.get_realism_config = AsyncMock(return_value={
"key": "weights",
"value": json.dumps({"canary_probability": 0.12}),
})
await _refresh_realism_config(repo)
assert planner.current_payload()["canary_probability"] == pytest.approx(0.12)
@pytest.mark.asyncio
async def test_refresh_swallows_db_error():
"""A wedged DB must not bring down the orchestrator's tick loop."""
repo = AsyncMock()
repo.get_realism_config = AsyncMock(side_effect=RuntimeError("boom"))
await _refresh_realism_config(repo) # does not raise
# planner unchanged
assert planner.current_payload()["canary_probability"] == pytest.approx(0.03)
@pytest.mark.asyncio
async def test_refresh_swallows_malformed_json():
repo = AsyncMock()
repo.get_realism_config = AsyncMock(return_value={
"key": "weights",
"value": "not-json",
})
await _refresh_realism_config(repo)
assert planner.current_payload()["canary_probability"] == pytest.approx(0.03)
@pytest.mark.asyncio
async def test_refresh_swallows_invalid_payload():
repo = AsyncMock()
repo.get_realism_config = AsyncMock(return_value={
"key": "weights",
"value": json.dumps({"canary_probability": 9.0}),
})
await _refresh_realism_config(repo)
# Planner config not corrupted by a bad refresh.
assert planner.current_payload()["canary_probability"] == pytest.approx(0.03)