mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-11 13:27:33 +01:00
A running game process was treated as proof of play. On 2026-08-28 the budget
billed ~2h23m while a gatelock screen locker covered the display and the
machine could not be touched at all -- the game was simply still resident.
A tick now bills only when all three hold, evaluated together rather than
short-circuited so the audit log can answer "was the screen ALSO locked?":
* no gatelock guard owns the screen (_screen_hold, via /proc/locks)
* the focused window belongs to a qualifying process or its ancestry
* some input source has been used within idle_grace_seconds (default 300)
Any probe that cannot answer BILLS the tick and is recorded in `degraded`.
A broken detector must never become an unlimited-gaming exploit.
Three findings from measuring this machine rather than assuming:
* /dev/input/js0 here is an *ASRock LED Controller* -- the kernel exposes
the motherboard RGB device as a joystick. A presence check would have
disabled the idle criterion permanently, so _controller_input watches for
real events instead (skipping joydev's synthetic open burst).
* Aseprite's window carries no _NET_WM_PID at all, so focus attribution
falls back to XRes, which asks the server who owns the window rather than
trusting the client to have announced itself.
* /proc/locks reports gatelock's hold without taking it; gatelock's own
read_claim_if_held detects held-ness by acquiring, which would race a
guard every 3 seconds.
Idle pauses also refund the grace period once per walk-away, on the
engaged->paused edge only, never across a day rollover, and never on the first
tick after a restart -- the only place the counter moves backwards.
Everything is recorded to /var/log/steam-backlog-enforcer/budget.jsonl, on
change plus a heartbeat rather than one line per 3s tick. Records name the
qualifying PIDs: reconstructing this incident from journald alone was only
possible by inferring accrual from the spacing of two warning notifications.
Also: refuse to bounce Steam while a game is running. ensure_steam_debug_port
relaunches Steam when the CDP port is closed, which kills the running game --
that is how restarting the daemon to deploy this change ended a live session.
It now defers exactly as it already did for in-flight game updates.
Talks to X through ctypes over one cached connection: at a 3s tick, forking
xprintidle and xprop would cost ~40 forks a minute.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019CEsG6BzHtYR9cHcdxPZqG
84 lines
2.6 KiB
Python
84 lines
2.6 KiB
Python
"""The cross-tick state the daily budget carries, and how it is built.
|
|
|
|
Two things cannot be recomputed from scratch each tick. The engagement backdate
|
|
must recognise an engaged-to-paused *edge*, so it needs the previous verdict;
|
|
and the audit journal only writes on change, so it needs the previous record.
|
|
Both live on a session that the enforce loop creates once and threads through,
|
|
rather than in module globals.
|
|
|
|
The session's fields are typed by :class:`Protocol` so a test can supply an
|
|
honest stub. A ``MagicMock`` would satisfy every attribute lookup — including
|
|
the ones a refactor forgot to update — which is exactly the failure a type
|
|
would otherwise have caught.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING, Protocol
|
|
|
|
from steam_backlog_enforcer._desktop_env import desktop_uid, resolve_desktop_user
|
|
from steam_backlog_enforcer._playtime_engagement import EngagementTracker
|
|
from steam_backlog_enforcer._playtime_log import BudgetLog, TickJournal
|
|
|
|
if TYPE_CHECKING:
|
|
from steam_backlog_enforcer._engagement_types import EngagementVerdict
|
|
from steam_backlog_enforcer._playtime_state import PlaytimeRules, PlaytimeState
|
|
|
|
|
|
class EngagementSource(Protocol):
|
|
"""What the budget tick needs from an engagement tracker."""
|
|
|
|
def assess(
|
|
self,
|
|
rules: PlaytimeRules,
|
|
*,
|
|
qualifying: set[int],
|
|
now_monotonic: float,
|
|
) -> EngagementVerdict:
|
|
"""Judge whether this tick counts against the budget."""
|
|
|
|
def backdate(
|
|
self,
|
|
state: PlaytimeState,
|
|
verdict: EngagementVerdict,
|
|
*,
|
|
rules: PlaytimeRules,
|
|
) -> PlaytimeState:
|
|
"""Refund the idle grace period on an engaged-to-idle edge."""
|
|
|
|
|
|
class TickRecorder(Protocol):
|
|
"""What the budget tick needs from the audit journal."""
|
|
|
|
def observe(
|
|
self,
|
|
verdict: EngagementVerdict,
|
|
state: PlaytimeState,
|
|
*,
|
|
rules: PlaytimeRules,
|
|
now_monotonic: float,
|
|
) -> None:
|
|
"""Record this tick if it is worth recording."""
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class PlaytimeSession:
|
|
"""Per-daemon engagement and logging state."""
|
|
|
|
tracker: EngagementSource
|
|
journal: TickRecorder
|
|
|
|
|
|
def new_session() -> PlaytimeSession:
|
|
"""Build the session the enforce loop threads through every tick.
|
|
|
|
Returns:
|
|
A session bound to the desktop user's X display and runtime dir.
|
|
"""
|
|
uid = desktop_uid(resolve_desktop_user())
|
|
return PlaytimeSession(
|
|
tracker=EngagementTracker(uid=uid),
|
|
journal=TickJournal(BudgetLog()),
|
|
)
|