steam-backlog-enforcer/steam_backlog_enforcer/tests/test_playtime_session.py
Krzysztof kuhy Rudnicki b3e629e3f0 Bill gaming time only while the user is actually playing
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
2026-08-28 19:24:14 +02:00

34 lines
1.2 KiB
Python

"""Tests for building the per-daemon budget session."""
from __future__ import annotations
from unittest.mock import patch
from steam_backlog_enforcer._playtime_engagement import EngagementTracker
from steam_backlog_enforcer._playtime_log import TickJournal
from steam_backlog_enforcer._playtime_session import new_session
PS = "steam_backlog_enforcer._playtime_session"
class TestNewSession:
def test_builds_a_tracker_and_a_journal(self) -> None:
with (
patch(f"{PS}.resolve_desktop_user", return_value="kuhy"),
patch(f"{PS}.desktop_uid", return_value=1000) as uid,
):
session = new_session()
assert isinstance(session.tracker, EngagementTracker)
assert isinstance(session.journal, TickJournal)
uid.assert_called_once_with("kuhy")
def test_binds_the_tracker_to_the_desktop_users_runtime_dir(self) -> None:
# Root's own /run/user/0 would be the wrong place to look for the
# gatelock holder file.
with (
patch(f"{PS}.resolve_desktop_user", return_value="kuhy"),
patch(f"{PS}.desktop_uid", return_value=1000),
):
session = new_session()
assert "/run/user/1000/" in str(session.tracker._holder_path)