steam-backlog-enforcer/steam_backlog_enforcer/_shadow_log.py
Krzysztof kuhy Rudnicki e82ddd9a4b playtime: shadow leisure billing (C4); /proc scan from leisure_watch
The enforcer now imports the Steam/launcher/counted-process scan from
leisure_watch.procscan (v0.1.0, shared with the leisure watcher) and drops
its own copies (_playtime_procs, _counted_procs, _total_block_launchers and
enforcer's two scanners). Every counted-process read passes
config.CONFIG_FILE explicitly, as before.

Each tick also bills a shadow leisure budget beside the enforced one, from
the same incoming stamp and delta clamp, before accumulate: paused (held
gatelock grab or i3lock) -> shadow_paused_seconds; else a qualifying game,
a "leisure" now.json, or an untrusted/missing now.json (fail closed) bills
shadow_seconds once, split over shadow_per_source. shadow_budget_seconds is
earned_time.resolve(leisure=True) from the same answers, held as a same-day
high-water mark. Not billed under a total block. Shadow verdict changes go
to budget.jsonl as shadow_change (the live game view skips them); /api/budget
and MCP get_gaming_time carry a "shadow" block. Enforced fields, cutoff,
warnings and existing log records are unchanged.

Also fixes two tests that assumed earned_time < 0.14 internals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Dsh4Znwh2KnC7q6vbGpX1N
2026-10-19 12:37:01 +02:00

59 lines
2.1 KiB
Python

"""The shadow verdict in the budget audit log: one record per change.
The game journal (:class:`~steam_backlog_enforcer._playtime_log.TickJournal`)
only writes when the *game* verdict changes, so browsing with no game running
would never show up there. Shadow changes get their own event in the same
``budget.jsonl``, written only when the verdict (paused/leisure/free plus the
billed sources) changes -- no heartbeat; the state file has the running total.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Final
if TYPE_CHECKING:
from steam_backlog_enforcer._playtime_log import BudgetLog
from steam_backlog_enforcer._playtime_state import PlaytimeState
from steam_backlog_enforcer._shadow import ShadowVerdict
EVENT_SHADOW_CHANGE: Final = "shadow_change"
class ShadowJournal:
"""Records the shadow verdict whenever it changes."""
def __init__(self, log: BudgetLog) -> None:
"""Start with nothing observed.
Args:
log: Sink to write through (the game journal's own trail).
"""
self._log = log
self._last: ShadowVerdict | None = None
def observe(self, verdict: ShadowVerdict, state: PlaytimeState) -> None:
"""Record *verdict* with the shadow totals if it differs from the last.
Args:
verdict: This tick's shadow verdict.
state: Accounting state after this tick.
"""
if verdict == self._last:
return
self._last = verdict
remaining = state.shadow_budget_seconds - state.shadow_seconds
self._log.record(
EVENT_SHADOW_CHANGE,
shadow_state=verdict.state,
paused_by=verdict.paused_by,
sources=list(verdict.keys),
day_key=state.day_key,
shadow_seconds=round(state.shadow_seconds, 3),
shadow_budget_seconds=round(state.shadow_budget_seconds, 3),
shadow_remaining_seconds=round(remaining, 3),
shadow_paused_seconds=round(state.shadow_paused_seconds, 3),
shadow_per_source={
k: round(v, 3) for k, v in state.shadow_per_source.items()
},
)