mirror of
https://github.com/kuhyx/steam-backlog-enforcer.git
synced 2026-11-22 01:52: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
189 lines
6.5 KiB
Python
189 lines
6.5 KiB
Python
"""Mutating MCP tools: manual picks, abandonment and gaming blocks.
|
|
|
|
Every tool here takes an explicit ``confirm`` flag, so a model cannot change
|
|
enforcement state on a single unconfirmed call. The read-only half lives in
|
|
:mod:`steam_backlog_enforcer._mcp_query`.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from steam_backlog_enforcer._actions import (
|
|
ABANDON_COOLDOWN_DAYS,
|
|
MANUAL_GRACE_DAYS,
|
|
abandon_manual_pick,
|
|
active_manual_picks,
|
|
apply_manual_pick,
|
|
can_abandon_manual_pick,
|
|
find_manual_pick,
|
|
manual_pick_grace_remaining,
|
|
)
|
|
from steam_backlog_enforcer._allowed_games import MANUAL_LOCK_DAYS
|
|
from steam_backlog_enforcer._mcp_server import (
|
|
_resolve_game_name,
|
|
logger,
|
|
mcp,
|
|
)
|
|
from steam_backlog_enforcer._total_block import start_total_block
|
|
from steam_backlog_enforcer.config import Config, State
|
|
|
|
|
|
@mcp.tool()
|
|
def pick_manual(app_id: int, *, confirm: bool = False) -> dict[str, Any]:
|
|
"""Lock the enforcer to a game by Steam app_id (gated write).
|
|
|
|
With ``confirm=False`` (the default) this performs **no** mutation and
|
|
returns a preview of what confirming would do. Call again with
|
|
``confirm=True`` to apply. Applying overrides the current assignment and
|
|
locks all other commands for ``MANUAL_LOCK_DAYS`` days (or until 100%
|
|
achievements). Unlike the CLI's ``pick-manual``, this mutates **state only**
|
|
— it never uninstalls, installs, or hides games.
|
|
|
|
Args:
|
|
app_id: The Steam app id to lock in (must exist in the last snapshot).
|
|
confirm: Set ``True`` to actually apply the pick; otherwise preview.
|
|
"""
|
|
game_name = _resolve_game_name(app_id)
|
|
if game_name is None:
|
|
return {
|
|
"ok": False,
|
|
"reason": f"AppID={app_id} not found in the snapshot. Run 'scan' first.",
|
|
}
|
|
if not confirm:
|
|
return {
|
|
"ok": True,
|
|
"preview": True,
|
|
"action": "pick_manual",
|
|
"app_id": app_id,
|
|
"game_name": game_name,
|
|
"effect": (
|
|
"Overrides the current assignment and locks all other commands "
|
|
f"for {MANUAL_LOCK_DAYS} days or until 100% achievements."
|
|
),
|
|
"confirm_required": True,
|
|
}
|
|
state = State.load()
|
|
refused = apply_manual_pick(
|
|
state,
|
|
app_id,
|
|
game_name,
|
|
max_picks=Config.load().max_manual_picks,
|
|
)
|
|
if refused is not None:
|
|
return {"ok": False, "reason": refused}
|
|
logger.info("pick_manual applied: %s (AppID=%s)", game_name, app_id)
|
|
return {
|
|
"ok": True,
|
|
"applied": True,
|
|
"action": "pick_manual",
|
|
"app_id": app_id,
|
|
"game_name": game_name,
|
|
}
|
|
|
|
|
|
@mcp.tool()
|
|
def abandon_pick(app_id: int, *, confirm: bool = False) -> dict[str, Any]:
|
|
"""Undo a manual pick inside its grace window (gated write).
|
|
|
|
With ``confirm=False`` (the default) this performs **no** mutation and
|
|
returns a preview. Only works within ``MANUAL_GRACE_DAYS`` days of the
|
|
pick. Like ``pick_manual``, this mutates **state only** — the CLI's
|
|
``abandon-pick`` additionally uninstalls the abandoned game.
|
|
|
|
Args:
|
|
app_id: The manually-picked app id to back out of.
|
|
confirm: Set ``True`` to actually abandon the pick; otherwise preview.
|
|
"""
|
|
state = State.load()
|
|
picks = active_manual_picks(state)
|
|
if not picks:
|
|
return {"ok": False, "reason": "No manual pick is active."}
|
|
|
|
pick = find_manual_pick(state, app_id)
|
|
if pick is None:
|
|
listed = ", ".join(f"AppID={p['app_id']}" for p in picks)
|
|
return {
|
|
"ok": False,
|
|
"reason": (
|
|
f"AppID={app_id} is not one of the active manual picks ({listed})."
|
|
),
|
|
}
|
|
|
|
remaining = manual_pick_grace_remaining(state, app_id)
|
|
if not can_abandon_manual_pick(state, app_id):
|
|
return {
|
|
"ok": False,
|
|
"reason": (
|
|
f"The {MANUAL_GRACE_DAYS}-day grace period has expired; "
|
|
"the pick can no longer be abandoned."
|
|
),
|
|
}
|
|
|
|
game_name = str(pick["game_name"])
|
|
if not confirm:
|
|
return {
|
|
"ok": True,
|
|
"preview": True,
|
|
"action": "abandon_pick",
|
|
"app_id": app_id,
|
|
"game_name": game_name,
|
|
"grace_days_left": remaining,
|
|
"effect": (
|
|
"Releases the manual pick lock, clears the assignment, and "
|
|
f"keeps the game out of auto-assignment for "
|
|
f"{ABANDON_COOLDOWN_DAYS} days."
|
|
),
|
|
"confirm_required": True,
|
|
}
|
|
|
|
abandon_manual_pick(state, app_id)
|
|
logger.info("abandon_pick applied: %s (AppID=%s)", game_name, app_id)
|
|
return {
|
|
"ok": True,
|
|
"applied": True,
|
|
"action": "abandon_pick",
|
|
"app_id": app_id,
|
|
"game_name": game_name,
|
|
}
|
|
|
|
|
|
@mcp.tool()
|
|
def block_gaming(days: int, *, confirm: bool = False) -> dict[str, Any]:
|
|
"""Start a total gaming block for ``days`` days (gated, privileged write).
|
|
|
|
With ``confirm=False`` (the default) returns a preview only. With
|
|
``confirm=True`` it attempts the block, which **requires root** (it edits
|
|
``/etc/hosts`` and uninstalls Steam). The MCP server runs unprivileged, so
|
|
this will normally return ``{"ok": false, "reason": "requires elevated
|
|
privileges"}`` rather than succeeding. There is no in-app undo.
|
|
|
|
Args:
|
|
days: Whole number of days to block all gaming (must be >= 1).
|
|
confirm: Set ``True`` to attempt the block; otherwise preview.
|
|
"""
|
|
if days < 1:
|
|
return {"ok": False, "reason": "days must be at least 1."}
|
|
if not confirm:
|
|
return {
|
|
"ok": True,
|
|
"preview": True,
|
|
"action": "block_gaming",
|
|
"days": days,
|
|
"effect": (
|
|
f"For {days} day(s): uninstalls Steam, kills game launchers, and "
|
|
"blocks Steam + game-website domains. There is NO in-app undo."
|
|
),
|
|
"requires_root": True,
|
|
"confirm_required": True,
|
|
}
|
|
try:
|
|
applied = start_total_block(days)
|
|
except (OSError, RuntimeError) as exc: # never crash the server on a write
|
|
logger.warning("block_gaming failed: %s", exc)
|
|
return {"ok": False, "reason": "requires elevated privileges"}
|
|
if not applied:
|
|
return {"ok": False, "reason": "requires elevated privileges"}
|
|
logger.info("block_gaming applied: %d day(s)", days)
|
|
return {"ok": True, "applied": True, "action": "block_gaming", "days": days}
|