mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-21 20:12:18 +01:00
Each of these was one file doing several jobs, so they are split along what they actually do rather than at arbitrary line offsets: - _total_block: the lock file and the status derived from it stay in _total_block; making Steam *not installed* moves to _total_block_purge, making it *unreachable* to _total_block_hosts / _total_block_iptables. The generic pacman verbs (owner, installed, remove) become _pacman, since nothing about them is total-block-specific. Domain and process name tables become data modules. - _playtime: budget state and persistence (_playtime_state), pure transitions (_playtime_budget), cmdline process detection (_playtime_procs), the warn/cutoff/kill ladder (_playtime_cutoff), desktop notifications (_playtime_notify), process-tree walking and killing (_playtime_kill), mountinfo parsing (_playtime_mountinfo) and the shared privileged-command helper (_playtime_run). - _mcp: read-only tools (_mcp_query) split from mutating ones (_mcp_actions, _mcp_gaming_time), so "what can this server change?" is answerable from one file. _mcp keeps the server object and entry point. No re-export shims: callers import from the module that defines the name. That keeps a stale patch target loud (AttributeError) instead of letting it pass while intercepting nothing. `ruff check --select F821,F401` is run after every extraction -- an earlier mechanical attempt moved functions without the module-level names they referenced, which imported fine and only failed when called (349 errors), so it was reverted and redone by hand. The autouse fixtures in conftest are now imported by name and held by __all__; ruff --unsafe-fixes had deleted them as unused, since pytest resolves fixtures by name. 1324 tests pass at 100.00% coverage in the clean venv; `main status` is byte-identical to a worktree at HEAD. 49 -> 39 files over the cap. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPpmMpTSkp8UziRc5u8B7P
110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
"""MCP tools for the daily gaming-time budget.
|
|
|
|
Reading today's usage and resetting it are separated from the pick/block
|
|
tools because they operate on the playtime state, not on which game is
|
|
assigned.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
import os
|
|
from typing import Any
|
|
|
|
from steam_backlog_enforcer._mcp_server import (
|
|
logger,
|
|
mcp,
|
|
)
|
|
from steam_backlog_enforcer._playtime import (
|
|
PlaytimeState,
|
|
gaming_day_key,
|
|
load_state,
|
|
rules_for,
|
|
save_state,
|
|
)
|
|
from steam_backlog_enforcer._playtime_block import mounted_targets, release_block
|
|
from steam_backlog_enforcer.config import Config
|
|
|
|
|
|
@mcp.tool()
|
|
def get_gaming_time() -> dict[str, Any]:
|
|
"""Return today's gaming-budget usage and whether the cutoff has engaged.
|
|
|
|
The gaming day starts at 06:00 local time, so a session played after
|
|
midnight still counts against the day it began in. Reads on-disk state and
|
|
the live mount table only — no network, no Steam API key, no config secrets.
|
|
"""
|
|
config = Config.load()
|
|
rules = rules_for(config, demo=False)
|
|
stored = load_state(demo=False)
|
|
masked = sorted(str(path) for path in mounted_targets())
|
|
|
|
if stored is None:
|
|
return {
|
|
"ok": True,
|
|
"recorded": False,
|
|
"budget_seconds": rules.budget_seconds,
|
|
"enforcement": rules.enforcement,
|
|
"masked_launchers": masked,
|
|
}
|
|
|
|
return {
|
|
"ok": True,
|
|
"recorded": True,
|
|
"gaming_day": stored.day_key,
|
|
"day_starts_at": "06:00 local",
|
|
"seconds_used": round(stored.seconds, 1),
|
|
"budget_seconds": rules.budget_seconds,
|
|
"seconds_remaining": round(max(0.0, rules.budget_seconds - stored.seconds), 1),
|
|
"blocked": stored.is_blocked(),
|
|
"enforcement": rules.enforcement,
|
|
"counts_launchers": rules.count_launchers,
|
|
"masked_launchers": masked,
|
|
}
|
|
|
|
|
|
@mcp.tool()
|
|
def reset_gaming_time(*, confirm: bool = False) -> dict[str, Any]:
|
|
"""Reset today's gaming counter and lift the block (gated, privileged write).
|
|
|
|
With ``confirm=False`` (the default) returns a preview and mutates nothing.
|
|
With ``confirm=True`` it attempts the reset, which **requires root** (it
|
|
releases bind mounts over ``/usr/bin/steam`` and friends). The MCP server
|
|
runs unprivileged, so this will normally return ``{"ok": false, "reason":
|
|
"requires elevated privileges"}`` rather than succeeding.
|
|
|
|
Args:
|
|
confirm: Set ``True`` to attempt the reset; otherwise preview.
|
|
"""
|
|
if not confirm:
|
|
return {
|
|
"ok": True,
|
|
"preview": True,
|
|
"action": "reset_gaming_time",
|
|
"effect": (
|
|
"Zeroes today's gaming counter and unmasks every launcher "
|
|
"binary, restoring a full daily budget immediately."
|
|
),
|
|
"requires_root": True,
|
|
"confirm_required": True,
|
|
}
|
|
if os.geteuid() != 0:
|
|
return {"ok": False, "reason": "requires elevated privileges"}
|
|
try:
|
|
released = release_block()
|
|
now = datetime.now(timezone.utc).astimezone()
|
|
save_state(
|
|
PlaytimeState(day_key=gaming_day_key(now), last_tick_at=now.timestamp()),
|
|
demo=False,
|
|
)
|
|
except (OSError, ValueError) as exc: # never crash the server on a write
|
|
logger.warning("reset_gaming_time failed: %s", exc)
|
|
return {"ok": False, "reason": "requires elevated privileges"}
|
|
logger.info("reset_gaming_time applied; released %d mount(s)", len(released))
|
|
return {
|
|
"ok": True,
|
|
"applied": True,
|
|
"action": "reset_gaming_time",
|
|
"released": [str(path) for path in released],
|
|
}
|