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 <noreply@anthropic.com>
This commit is contained in:
Krzysztof Rudnicki 2026-11-18 22:02:08 +01:00
parent 893eea4901
commit ffd30b0476
7 changed files with 136 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

45
diet_guard/_errors.py Normal file
View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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()