From ffd30b04763f6b88b9acd6988f9ab382779ef092 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Wed, 18 Nov 2026 22:02:08 +0100 Subject: [PATCH] Report gate errors and stalls to the board gatelock 0.15.1 error_report, installed by the CLI as app diet-guard-gate: uncaught, thread and Tk callback errors plus a gate loop frozen over 1 s go to the app-errors spool. The stall watch arms in run(), after the turn wait, so building the gate is no stall. Co-Authored-By: Claude Opus 5.5 --- README.md | 9 +++++ app/pubspec.yaml | 2 +- diet_guard/_cli.py | 8 ++-- diet_guard/_errors.py | 45 +++++++++++++++++++++++ diet_guard/_gatelock.py | 3 +- diet_guard/tests/conftest.py | 10 +++++ diet_guard/tests/test_errors.py | 65 +++++++++++++++++++++++++++++++++ 7 files changed, 136 insertions(+), 6 deletions(-) create mode 100644 diet_guard/_errors.py create mode 100644 diet_guard/tests/test_errors.py diff --git a/README.md b/README.md index 406ab7b..93387c7 100644 --- a/README.md +++ b/README.md @@ -87,6 +87,15 @@ bash scripts/setup_mcp.sh # create the venv and install diet_guard[mcp] Then restart Claude Code in this repo and approve the project MCP server (registered in `.mcp.json`). +## Error reports + +Every uncaught, thread or Tk callback error, and any freeze of the gate window +over 1 s, goes to the shared app-errors spool as app `diet-guard-gate` +through `gatelock.error_report` (`diet_guard/_errors.py`, installed by the +CLI). The PC's `app-errors` timer turns each one into a ticket on the +todoImplementer board (kuhyx/utils `DOCS-app-errors.md`). Nothing shows on +screen. The Flutter app reports on its own, as `diet-guard`. + ## Development ```bash diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 156a336..0930102 100644 --- a/app/pubspec.yaml +++ b/app/pubspec.yaml @@ -2,7 +2,7 @@ name: diet_guard_app description: "Companion phone app for diet_guard: log meals on the go." publish_to: 'none' -version: 1.0.179+1 +version: 1.0.180+1 environment: sdk: ^3.12.2 diff --git a/diet_guard/_cli.py b/diet_guard/_cli.py index 12ce175..e2b0d40 100644 --- a/diet_guard/_cli.py +++ b/diet_guard/_cli.py @@ -6,12 +6,10 @@ Examples: python -m diet_guard ate "two slices of pizza" --grams 240 python -m diet_guard ate "protein shake" --kcal 180 python -m diet_guard status - python -m diet_guard averages python -m diet_guard undo -The daily budget lives outside the repo (so it is never exposed online) but is -shown freely on this machine: ``status`` and each log print how many calories -are left of the day's budget, plus which meal slots still need logging. +``status`` and each log print the calories left of the day's budget (kept +outside the repo) and which meal slots still need logging. """ from __future__ import annotations @@ -19,6 +17,7 @@ from __future__ import annotations import sys from typing import TYPE_CHECKING +from diet_guard import _errors from diet_guard._budget import ( BudgetFileCorruptError, BudgetNotInitializedError, @@ -247,4 +246,5 @@ def main(argv: list[str] | None = None) -> int: A process exit code (0 on success). """ args = parse_args(sys.argv[1:] if argv is None else argv) + _errors.install() return _COMMANDS[args.command](args) diff --git a/diet_guard/_errors.py b/diet_guard/_errors.py new file mode 100644 index 0000000..e2163d4 --- /dev/null +++ b/diet_guard/_errors.py @@ -0,0 +1,45 @@ +"""Error and slow-wait reporting for the diet gate (utils DOCS-app-errors.md). + +``install`` runs once from the CLI entry point: uncaught, thread and Tk +callback errors, and a Tk loop frozen over 1 s, go to the app-errors spool, +from which the PC's ingest files a board ticket. Only the CLI installs it, so +tests that build a gate window never touch the real spool. The Flutter app +reports separately, as ``diet-guard``. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from gatelock import error_report +from gatelock.error_report import watch_stalls + +if TYPE_CHECKING: + import tkinter as tk + +APP = "diet-guard-gate" +REPO = "kuhyx/diet-guard" + + +@dataclass +class _Slot: + """Holds the process's reporter once installed.""" + + reporter: error_report.Reporter | None = None + + +#: The installed reporter (``SLOT.reporter``); None until ``install``. +SLOT = _Slot() + + +def install() -> error_report.Reporter: + """Hook this process's errors once; return the reporter.""" + SLOT.reporter = error_report.install(APP, REPO) + return SLOT.reporter + + +def watch(root: tk.Misc) -> None: + """Report ``root``'s freezes over 1 s, once ``install`` has run.""" + if SLOT.reporter is not None: + watch_stalls(root, SLOT.reporter) diff --git a/diet_guard/_gatelock.py b/diet_guard/_gatelock.py index 3213875..8d7898e 100644 --- a/diet_guard/_gatelock.py +++ b/diet_guard/_gatelock.py @@ -34,7 +34,6 @@ Building ``MealGate`` spans several sibling modules (the 250-line limit): :mod:`._gatelock_mealflow` the submit/log flow, dashboard, callback errors and pull-it-in buttons (``_GateMealFlow`` + ``_PullFlows``); :mod:`._gatelock_calendar` the tab -- calendar, streaks, YTD tally (:mod:`._daystatus`), budget editing. -``MealGate`` wires these together and owns the ``gatelock.LockWindow``. """ from __future__ import annotations @@ -52,6 +51,7 @@ from gatelock import ( wait_for_turn, ) +from diet_guard import _errors from diet_guard._gate import due_slots from diet_guard._gatelock_calendar import _GateCalendar, make_calendar_vars from diet_guard._gatelock_core import _GateState @@ -215,6 +215,7 @@ class MealGate(_GateCalendar): def run(self) -> None: """Run the Tk loop (a hold or a meal logged elsewhere closes it), VT-safe.""" watch_hold(self.root, self.close) + _errors.watch(self.root) # here, not at build: the turn wait is no stall if not self.demo_mode: # a demo owes a synthetic slot: it would self-close arm_credit_watch( self.root, lambda: self._pending, self._reconcile_after_fetch diff --git a/diet_guard/tests/conftest.py b/diet_guard/tests/conftest.py index 5f45f95..3f38009 100644 --- a/diet_guard/tests/conftest.py +++ b/diet_guard/tests/conftest.py @@ -27,6 +27,7 @@ from unittest.mock import patch import pytest +from diet_guard import _errors from diet_guard._estimator import Nutrition # Importing these fixtures IS their registration: pytest discovers fixtures @@ -113,6 +114,15 @@ def _isolate_state(tmp_path: Path) -> Iterator[None]: stack.enter_context( patch.dict(os.environ, {"HOLDS_DATA": str(tmp_path / "holds.json")}) ) + # gatelock hooks a process once; a reporter already in INSTALLED makes + # install() return it untouched, so no hook is set and every report + # lands in this test's folder, never the real app-errors spool. + spool = tmp_path / "app-errors-spool" + rep = _errors.error_report.Reporter(_errors.APP, _errors.REPO, spool=spool) + stack.enter_context( + patch.object(_errors.error_report.INSTALLED, "reporter", rep) + ) + stack.enter_context(patch.object(_errors.SLOT, "reporter", None)) yield diff --git a/diet_guard/tests/test_errors.py b/diet_guard/tests/test_errors.py new file mode 100644 index 0000000..1e81039 --- /dev/null +++ b/diet_guard/tests/test_errors.py @@ -0,0 +1,65 @@ +"""The gate's error reporting: installed once by the CLI, into the shared spool.""" + +from __future__ import annotations + +import json +from typing import TYPE_CHECKING +from unittest.mock import MagicMock, patch + +from diet_guard import _cli, _errors, _gatelock +from diet_guard._gatelock import MealGate +from diet_guard.tests._gate_fixtures import fake_tk + +if TYPE_CHECKING: + from pathlib import Path + + import pytest + + +def test_install_keeps_the_reporter_and_writes_the_spool(tmp_path: Path) -> None: + """The CLI's install names the gate app and its repo in every report.""" + rep = _errors.install() + assert _errors.SLOT.reporter is rep + assert rep.app == "diet-guard-gate" + rep.report("uncaught.python", ValueError("deliberate")) + line = (tmp_path / "app-errors-spool" / "diet-guard-gate.jsonl").read_text() + entry = json.loads(line) + assert entry["repo"] == "kuhyx/diet-guard" + assert entry["error"] == "ValueError: deliberate" + + +def test_cli_main_installs_before_dispatch(monkeypatch: pytest.MonkeyPatch) -> None: + """Every CLI run hooks errors, the gate's included.""" + install = MagicMock() + monkeypatch.setattr(_errors, "install", install) + monkeypatch.setitem(_cli._COMMANDS, "status", lambda _args: 0) + assert _cli.main(["status"]) == 0 + install.assert_called_once_with() + + +def _run(gate: MealGate) -> None: + """Run the gate's start-up with its Tk loop stubbed out.""" + with ( + patch.object(gate, "_lock", MagicMock()), + patch.object(_gatelock, "watch_hold"), + ): + gate.run() + + +def test_gate_watches_stalls_once_installed(monkeypatch: pytest.MonkeyPatch) -> None: + """A gate built after install reports its own freezes.""" + reporter = MagicMock() + monkeypatch.setattr(_errors.SLOT, "reporter", reporter) + with patch.object(_errors, "watch_stalls") as watch, fake_tk(): + gate = MealGate(demo_mode=True) + watch.assert_not_called() # the turn wait while building is no stall + _run(gate) + watch.assert_called_once_with(gate.root, reporter) + assert _gatelock._errors is _errors + + +def test_gate_skips_the_watch_without_a_reporter() -> None: + """A gate built outside the CLI (tests, imports) never starts a watch.""" + with patch.object(_errors, "watch_stalls") as watch, fake_tk(): + _run(MealGate(demo_mode=True)) + watch.assert_not_called()