From 34789fe58137071c1808145391cb879370c36c3c Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Mon, 16 Nov 2026 16:25:52 +0100 Subject: [PATCH] Skip shadow billing on unlimited days The leisure watcher is uninstalled; an unlimited day must not read its now.json or bill failclosed:watcher into a budget nobody has. shadow_step returns an idle verdict when gaming is not enforced. Before the switch the fail-closed bill is unchanged (both sides tested). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WzxdWViGJJaqBKGr1Hbppr --- steam_backlog_enforcer/_playtime.py | 7 +- steam_backlog_enforcer/_shadow.py | 33 ++++++++- .../tests/test_gaming_unlimited_shadow.py | 69 +++++++++++++++++++ 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 steam_backlog_enforcer/tests/test_gaming_unlimited_shadow.py diff --git a/steam_backlog_enforcer/_playtime.py b/steam_backlog_enforcer/_playtime.py index f0718cd..624d5f7 100644 --- a/steam_backlog_enforcer/_playtime.py +++ b/steam_backlog_enforcer/_playtime.py @@ -57,7 +57,7 @@ from steam_backlog_enforcer._playtime_state import ( rules_for, save_state, ) -from steam_backlog_enforcer._shadow import observe_shadow, shadow_accumulate +from steam_backlog_enforcer._shadow import shadow_step from steam_backlog_enforcer._total_block import is_total_block_active if TYPE_CHECKING: @@ -123,10 +123,7 @@ def playtime_tick( owners = qualifying_pids(rules, config_mod.CONFIG_FILE) pids = set(owners) - shadow = observe_shadow(owners, stamp=now.timestamp()) - state = shadow_accumulate( - state, stamp=now.timestamp(), interval=interval, verdict=shadow - ) + state, shadow = shadow_step(state, rules, owners, now.timestamp(), interval) tick = Tick(now=now, interval=interval, owners=owners, verdict=shadow) state, rules = bill_tick(state, rules, tick) state = _policy(state, rules, now=now) diff --git a/steam_backlog_enforcer/_shadow.py b/steam_backlog_enforcer/_shadow.py index ec811a2..000b8a4 100644 --- a/steam_backlog_enforcer/_shadow.py +++ b/steam_backlog_enforcer/_shadow.py @@ -38,7 +38,7 @@ from steam_backlog_enforcer._playtime_state import _DELTA_CLAMP_FACTOR if TYPE_CHECKING: from leisure_watch.facts import Now - from steam_backlog_enforcer._playtime_state import PlaytimeState + from steam_backlog_enforcer._playtime_state import PlaytimeRules, PlaytimeState # Explicit, because the daemon is root: these are the desktop user's (uid 1000). NOW_PATH: Final = Path("/run/user/1000/leisure-watch/now.json") @@ -189,3 +189,34 @@ def shadow_accumulate( per_source=per_source, ), ) + + +def shadow_step( + state: PlaytimeState, + rules: PlaytimeRules, + owners: dict[int, str], + stamp: float, + interval: float, +) -> tuple[PlaytimeState, ShadowVerdict]: + """Observe and bill one tick's shadow time, unless there is no budget. + + From 2026-11-16 (:mod:`_gaming_gate`) there is no leisure budget, so the + watcher is neither read nor billed: with it uninstalled that would only + bill ``failclosed:watcher`` into a budget nobody has. The idle verdict is + free and unpaused. + + Args: + state: Accounting state before this tick's enforced accumulation. + rules: Policy for this tick (``gaming_enforced`` decides). + owners: This tick's qualifying game PIDs and their keys. + stamp: Epoch seconds of this tick. + interval: Nominal seconds between ticks. + + Returns: + The billed state and the tick's verdict. + """ + if not rules.gaming_enforced: + return state, ShadowVerdict(paused_by=None, keys=()) + verdict = observe_shadow(owners, stamp=stamp) + billed = shadow_accumulate(state, stamp=stamp, interval=interval, verdict=verdict) + return billed, verdict diff --git a/steam_backlog_enforcer/tests/test_gaming_unlimited_shadow.py b/steam_backlog_enforcer/tests/test_gaming_unlimited_shadow.py new file mode 100644 index 0000000..eca8454 --- /dev/null +++ b/steam_backlog_enforcer/tests/test_gaming_unlimited_shadow.py @@ -0,0 +1,69 @@ +"""With the watcher uninstalled, an unlimited day bills no shadow time at all. + +Before 2026-11-16 a missing ``now.json`` fails closed and bills the shadow +budget (``failclosed:watcher``). Unlimited days must not read the watcher. +""" + +from __future__ import annotations + +from contextlib import ExitStack +from datetime import datetime, timedelta, timezone +from unittest.mock import DEFAULT, patch + +import pytest + +from steam_backlog_enforcer._budget_resolve import BudgetResolution +from steam_backlog_enforcer._playtime import playtime_tick +from steam_backlog_enforcer._playtime_state import ( + PlaytimeState, + load_state, + save_state, +) +from steam_backlog_enforcer.config import Config +from steam_backlog_enforcer.tests._fake_session import fake_session + +PKG = "steam_backlog_enforcer._playtime" +NOW = datetime(2026, 11, 17, 20, 0, tzinfo=timezone(timedelta(hours=2))) + + +def _tick_with_no_watcher(*, enforced: bool) -> PlaytimeState: + """One idle tick (no game, no ``now.json``) on a day that is or is not capped.""" + save_state( + PlaytimeState(day_key="2026-11-17", last_tick_at=NOW.timestamp() - 3.0), + demo=False, + ) + day = BudgetResolution(0.0, 0.0, 0.0, 0.0, "stub", enforced=enforced) + with ExitStack() as stack: + stack.enter_context( + patch( + "steam_backlog_enforcer._playtime_state.resolve_budget", + return_value=day, + ) + ) + stack.enter_context( + patch.multiple( + f"{PKG}", + qualifying_pids=lambda *_: {}, + mounted_targets=set, + is_total_block_active=lambda: False, + ) + ) + stack.enter_context( + patch("steam_backlog_enforcer._playtime_cutoff.reconcile", DEFAULT) + ) + clock = stack.enter_context(patch(f"{PKG}.datetime")) + clock.now.return_value = NOW + playtime_tick(Config(), interval=3.0, session=fake_session()) + stored = load_state(demo=False) + assert stored is not None + return stored + + +@pytest.mark.parametrize(("enforced", "billed"), [(True, 3.0), (False, 0.0)]) +def test_shadow_bills_only_while_a_budget_exists( + *, enforced: bool, billed: float +) -> None: + """The fail-closed watcher bill is the old behaviour; unlimited bills nothing.""" + stored = _tick_with_no_watcher(enforced=enforced) + assert stored.shadow.seconds == billed + assert ("failclosed:watcher" in stored.shadow.per_source) is enforced