mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-11 14:27:33 +01:00
The budget tab answered "how much did I play" but not "what did I play":
playtime_state.json and playtime_history.json each stored one aggregate
seconds figure per gaming day, so the 14-day chart was a row of featureless
bars. Separately, qualifying_pids had exactly two sources — the SteamAppId
env var and a hardcoded 16-launcher frozenset — so osu!lazer, which sets no
SteamAppId and matches no launcher name, was never billed at all.
qualifying_pids now returns pid -> attribution key instead of a bare pid set.
The app id was already in hand and thrown away on its first line; it now
survives into PlaytimeState.per_game, alongside a user-editable
counted_processes list in config.json seeded with osu!lazer.
Two things the parts/whole relationship forces:
- per_game only ever sums to AT MOST seconds, never equals it. backdate
refunds the idle grace on every engaged-to-paused edge, and
_state_or_recover synthesises a full budget with no breakdown. The
refund is debited from last_credited_key, and the remainder is rendered
as "Unattributed" rather than stored — one path then covers legacy days,
focus-probe failures and drift alike.
- counted_processes is kept apart from LAUNCHER_PROCESS_NAMES. That
frozenset is also the total-block kill list, and it routes through
kill_and_uninstall_launchers, which pacman -R's the owning package.
Counted entries are killed on total block, never uninstalled.
Both storage formats stay at schema 1: load_state already filters to
dataclass fields, so a record without per_game migrates for free, and the
history reader has to accept both a bare float and an object regardless, so
a version bump would distinguish nothing.
Chart segments use the shared design system's categorical ramp (--cat-1..6),
capped at six games plus "Other" because a seventh distinguishable hue does
not exist under simulated CVD. The red over-budget fill is gone: colour now
encodes which game, and the dashed budget line alone says over or under.
Snapshot persistence moves to _snapshot.py to keep config.py under the
250-line cap. Three modules bound config paths at import time, which
bypasses the conftest redirect — they now reach them through the module.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MKFzwzwRU1JD7SFLHtQBsz
126 lines
3.7 KiB
Python
126 lines
3.7 KiB
Python
"""Warning, cutoff and kill steps of the playtime policy.
|
|
|
|
The escalation ladder once a budget is spent: warn, ask Steam to shut down
|
|
cleanly, then mask its binaries and SIGTERM/SIGKILL what is still running.
|
|
The grace between shutdown and masking exists because masking makes
|
|
``steam -shutdown`` itself a no-op.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import replace
|
|
import logging
|
|
from typing import TYPE_CHECKING
|
|
|
|
from steam_backlog_enforcer._playtime_block import (
|
|
reconcile,
|
|
)
|
|
from steam_backlog_enforcer._playtime_budget import pending_warning
|
|
from steam_backlog_enforcer._playtime_kill import (
|
|
kill_gaming_processes,
|
|
request_steam_shutdown,
|
|
steam_and_launcher_pids,
|
|
)
|
|
from steam_backlog_enforcer._playtime_notify import _humanise, notify_desktop_user
|
|
from steam_backlog_enforcer._playtime_procs import qualifying_pids
|
|
from steam_backlog_enforcer._playtime_state import (
|
|
_SHUTDOWN_GRACE_SECONDS,
|
|
PlaytimeRules,
|
|
PlaytimeState,
|
|
)
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import datetime
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _warn(state: PlaytimeState, rules: PlaytimeRules) -> PlaytimeState:
|
|
"""Fire the due warning, if any, and record that it fired.
|
|
|
|
Args:
|
|
state: Current accounting state.
|
|
rules: Policy for this tick.
|
|
|
|
Returns:
|
|
The updated state.
|
|
"""
|
|
threshold = pending_warning(state, rules)
|
|
if threshold is None:
|
|
return state
|
|
|
|
notify_desktop_user(
|
|
"Gaming time running out",
|
|
f"{_humanise(threshold)} of today's gaming budget left.",
|
|
)
|
|
return replace(state, warned_seconds=[*state.warned_seconds, threshold])
|
|
|
|
|
|
def _begin_cutoff(
|
|
state: PlaytimeState,
|
|
rules: PlaytimeRules,
|
|
*,
|
|
now: datetime,
|
|
) -> PlaytimeState:
|
|
"""Start the cutoff: ask Steam to close cleanly, then SIGTERM the games.
|
|
|
|
No mount happens on this tick. Masking the Steam binaries turns
|
|
``steam -shutdown`` into a no-op, which would cost a clean close — and
|
|
therefore a cloud-save flush — on every single cutoff.
|
|
|
|
Args:
|
|
state: Current accounting state.
|
|
rules: Policy for this tick.
|
|
now: Timezone-aware local timestamp.
|
|
|
|
Returns:
|
|
The updated state.
|
|
"""
|
|
logger.warning("Daily gaming budget exhausted; shutting Steam down.")
|
|
notify_desktop_user(
|
|
"Gaming time is up",
|
|
"Daily budget used up. Steam and games are shutting down. Unblocks at 06:00.",
|
|
)
|
|
request_steam_shutdown()
|
|
kill_gaming_processes(_kill_set(rules), force=False)
|
|
return replace(state, blocked_at=now.timestamp())
|
|
|
|
|
|
def _sustain_block(
|
|
state: PlaytimeState,
|
|
rules: PlaytimeRules,
|
|
*,
|
|
now: datetime,
|
|
) -> PlaytimeState:
|
|
"""Hold the block: mask the binaries once the grace has passed, keep killing.
|
|
|
|
Args:
|
|
state: Current accounting state.
|
|
rules: Policy for this tick.
|
|
now: Timezone-aware local timestamp.
|
|
|
|
Returns:
|
|
*state*, unchanged — upkeep mutates the system, not the record.
|
|
"""
|
|
elapsed = now.timestamp() - state.blocked_at
|
|
if elapsed >= _SHUTDOWN_GRACE_SECONDS:
|
|
reconcile(should_block=True)
|
|
kill_gaming_processes(_kill_set(rules), force=elapsed >= rules.sigkill_after)
|
|
return state
|
|
|
|
|
|
def _kill_set(rules: PlaytimeRules) -> set[int]:
|
|
"""Return every PID the cutoff should terminate.
|
|
|
|
Wider than :func:`qualifying_pids` on purpose. A Lutris-launched Wine game
|
|
has no ``SteamAppId`` and its ``comm`` is the game's own binary, so neither
|
|
matcher sees it — only walking down from the launcher does.
|
|
|
|
Args:
|
|
rules: Policy for this tick.
|
|
|
|
Returns:
|
|
PIDs to signal.
|
|
"""
|
|
return set(qualifying_pids(rules)) | steam_and_launcher_pids()
|