steam-backlog-enforcer/steam_backlog_enforcer/_research_tick.py
Krzysztof kuhy Rudnicki 5d5aa3e773
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (mypy, mypy, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (pylint, pylint, ) (push) Waiting to run
pre-commit / pre-commit (${{ matrix.group }}) (rest, , mypy,pylint) (push) Waiting to run
Tests / test (push) Waiting to run
Tests / web-coverage (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
Add Librarian-only research window to total block
kuhy 2026-11-11: game-design research needs Librarian (app 4197610)
actually played during the 6-day total block. New root command
total-block-research --until T opens ONE window per lock: <= 48 h,
never past the lock end, a rerun never extends it.

The command only validates and records (/var/lib/steam-backlog-
enforcer, root 0700; lock id + deadline + two latches). The daemon
opens and closes everything: Steam hosts names, the store block, the
total-block chain (IP cache kept) and the steam package, reinstalled
with only guard-lib's package-block hook masked for that one pacman
call (--hookdir, proved in a guest). Lock file, hook and end date are
never touched. Browser-game sites stay null-routed.

Only Librarian may run: loop-local allowed set, strict mode (no
whitelist exceptions, no zero-achievement games), launchers and
counted games killed; state.json is never read or written.

Expiry latches the record, then the ordinary tick re-applies hosts
(now reconciled each tick), chain, uninstall and purge; the normal
store block is restored once, unconditionally.

Failure modes: missing/corrupt/unparseable record = no window and a
corrupt one also refuses a new window (fail closed); a clock set
back cannot reopen a latched window.

Checked: 3290 tests at 100% coverage; whole-tree pylint 10/10 run in
three parts (the hook exceeds the 60 s cap, so SKIP=pylint here); vmbox
guest with real guardctl package-block and the real unit, window
opened, Librarian-only enforced, clock moved past the deadline and the
full block re-applied. Not verified on the host.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWWzuCEg1m87jpKyRrw
2026-11-11 14:29:58 +01:00

110 lines
4.5 KiB
Python

"""One daemon pass under a total block: the research window or the full block.
The enforce loop hands its whole total-block branch here. With a research
window open (see :mod:`steam_backlog_enforcer._research_window`) Steam is left
installed and reachable, and the *only* thing kept is the one-allowed-game
rule, bent to a single game: the loop-local allowed set is ``{Librarian}``. The
real assignment in ``state.json`` is never read or written, so there is
nothing to restore afterwards. Everything else the block does to Steam itself
(kill, uninstall, purge) is skipped while the window is open.
Once the deadline passes the record is latched closed (a clock set back cannot
reopen it) and every pass is the ordinary :func:`enforce_total_block_tick`
again: it kills Steam, removes the package, purges its files, rebuilds the
firewall chain from the kept IP cache, and puts the full hosts section back.
The lock's end date is never involved.
"""
from __future__ import annotations
from dataclasses import replace
import logging
from typing import TYPE_CHECKING
from leisure_watch.procscan import LAUNCHER_PROCESS_NAMES, kill_target_names
from steam_backlog_enforcer import _research_net as net
from steam_backlog_enforcer import _research_window as window
from steam_backlog_enforcer import config as config_module
from steam_backlog_enforcer._enforce_guards import guard_installed_games
from steam_backlog_enforcer._research_net import reconcile_open_window
from steam_backlog_enforcer._steam_launch import steam_is_installed
from steam_backlog_enforcer._total_block import enforce_total_block_tick
from steam_backlog_enforcer._total_block_purge import kill_and_uninstall_launchers
from steam_backlog_enforcer.enforcer import (
enforce_allowed_game,
kill_processes_by_name,
send_notification,
)
from steam_backlog_enforcer.store_blocker import restore_store_block
if TYPE_CHECKING:
from steam_backlog_enforcer._research_record import ResearchRecord
from steam_backlog_enforcer.config import Config
logger = logging.getLogger(__name__)
def _latch_closed(record: ResearchRecord) -> ResearchRecord:
"""Persist that the window is over; blocking does not wait for the write."""
closed = replace(record, closed=True)
try:
window.write_record(closed)
except OSError:
logger.exception("Could not latch the research window closed")
return closed
def _reblock_store(config: Config, record: ResearchRecord) -> None:
"""Put the normal store block back once, after the window ended.
The total block's own hosts section covers the store until the lock ends,
but ``end_total_block_cleanup`` does not restore the normal block, so
without this the store would stay open after the lock. Unconditional
(:func:`restore_store_block`): the section being back makes the store look
blocked already.
"""
if not net.attempt_due("store"):
return
if config.block_store and not restore_store_block():
net.note_failure("store")
return
try:
window.write_record(replace(record, store_reblocked=True))
except OSError:
logger.exception("Could not record that the store block is back")
def _research_pass(record: ResearchRecord) -> None:
"""Keep Steam open and Librarian the only game while the window is open."""
reconcile_open_window(record)
# Non-Steam launchers and counted games are still forbidden; Steam itself
# is not killed, uninstalled or purged (that is the block's tick).
kill_and_uninstall_launchers(LAUNCHER_PROCESS_NAMES)
kill_processes_by_name(kill_target_names(config_module.CONFIG_FILE))
if not steam_is_installed():
return
allowed = {window.LIBRARIAN_APP_ID}
killed = enforce_allowed_game(allowed, kill_unauthorized=True, strict=True)
for _pid, app_id in killed:
send_notification("⛔🎮", f"{app_id} → 🎯 {window.LIBRARIAN_NAME}")
guard_installed_games(allowed, strict=True)
def total_block_pass(config: Config) -> None:
"""The total-block branch of one enforce pass.
Args:
config: Loaded configuration (``block_store`` gates the store re-block).
"""
at = window.now()
record = window.live_record(at)
if record is not None and record.is_open(at):
_research_pass(record)
return
if record is not None and not record.closed and at >= record.until:
record = _latch_closed(record)
enforce_total_block_tick()
if record is not None and record.closed and not record.store_reblocked:
_reblock_store(config, record)