Fix diet-guard-gate: perf.slow slow wait: tk.main…
Some checks are pending
file-length / file length <= 250 lines (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,dependency-freshness) (push) Waiting to run
Tests / test (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
repo-contract / repo contract (push) Waiting to run

Todo: 08acb940
This commit is contained in:
Krzysztof Rudnicki 2026-11-22 00:51:32 +01:00
parent 88da2564a1
commit 95288a7e83
10 changed files with 322 additions and 70 deletions

View File

@ -114,8 +114,8 @@ repos:
- gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock
- crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync
- freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays
- sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays
- kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.0#subdirectory=kuhy_credit
- sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.8#subdirectory=sickdays
- kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.1#subdirectory=kuhy_credit
- rtdb-relay @ git+https://github.com/kuhyx/utils@rtdb-relay-v0.2.1#subdirectory=rtdb_relay
- claude-agent-sdk==0.2.184
- httpx==0.28.1

View File

@ -207,11 +207,11 @@ packages:
dependency: "direct main"
description:
path: data_transfer_flutter
ref: "data_transfer_flutter-v0.2.3"
resolved-ref: bb2f92f459283a29ad4864cf37704cc5f749cc6d
ref: "data_transfer_flutter-v0.2.4"
resolved-ref: "5c2effe7d1148830c9e273e47a272791dc17509e"
url: "https://github.com/kuhyx/utils"
source: git
version: "0.2.3"
version: "0.2.4"
dbus:
dependency: transitive
description:
@ -280,10 +280,10 @@ packages:
dependency: transitive
description:
name: file_picker
sha256: c3780977700b8962c3b9b6a9aa4e2c988956893d70d4a4fa2d7adf4ec9b9eaee
sha256: "5b99250437a4d34ee2cdc0686924452103540eaa1c248094b63f07f9d7216f6c"
url: "https://pub.dev"
source: hosted
version: "13.4.0"
version: "13.4.1"
file_picker_darwin:
dependency: transitive
description:
@ -296,10 +296,10 @@ packages:
dependency: transitive
description:
name: file_picker_linux
sha256: "9215ee8c672508cdd9a325202669f849e1baac883658626b3abed5d7777e6a4b"
sha256: cf3889644e8e54551f5c3a4f98b6b6ef5f0ef85c07d2a0c1a969538983be2f9b
url: "https://pub.dev"
source: hosted
version: "2.0.4"
version: "2.0.5"
file_picker_platform_interface:
dependency: transitive
description:
@ -686,11 +686,11 @@ packages:
dependency: "direct main"
description:
path: "kuhy_credit/flutter"
ref: "kuhy-credit-v0.3.0"
resolved-ref: c3946412729991bf1e9c3b6a5cc55dbe80419fc0
ref: "kuhy-credit-v0.3.2"
resolved-ref: adeb40f68e1d6c701da43e396fc46bffa3ef10c7
url: "https://github.com/kuhyx/utils"
source: git
version: "0.3.0"
version: "0.3.2"
leak_tracker:
dependency: transitive
description:
@ -1076,11 +1076,11 @@ packages:
dependency: "direct main"
description:
path: sync_settings_ui
ref: "sync_settings_ui-v0.8.4"
resolved-ref: "15ef5b44f186cea0ab163cc88cb8743c51d58247"
ref: "sync_settings_ui-v0.8.5"
resolved-ref: abfb596d7ce2f28acfefbd55e7b3227257325261
url: "https://github.com/kuhyx/utils"
source: git
version: "0.8.4"
version: "0.8.5"
synchronized:
dependency: transitive
description:

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.191+1
version: 1.0.192+1
environment:
sdk: ^3.12.2
@ -27,7 +27,7 @@ dependencies:
data_transfer_flutter:
git:
url: https://github.com/kuhyx/utils
ref: data_transfer_flutter-v0.2.3
ref: data_transfer_flutter-v0.2.4
path: data_transfer_flutter
fl_chart: 1.3.1
flutter:
@ -56,7 +56,7 @@ dependencies:
kuhy_credit:
git:
url: https://github.com/kuhyx/utils
ref: kuhy-credit-v0.3.0
ref: kuhy-credit-v0.3.2
path: kuhy_credit/flutter
material_ui: 1.11.1
path: 1.9.1
@ -71,7 +71,7 @@ dependencies:
sync_settings_ui:
git:
url: https://github.com/kuhyx/utils
ref: sync_settings_ui-v0.8.4
ref: sync_settings_ui-v0.8.5
path: sync_settings_ui
url_launcher: 6.3.3
uuid: 4.6.0

View File

@ -2,4 +2,12 @@
# entries are predicates: they clear themselves when the blocker moves, so
# they carry no `expires`; everything else is discretionary and expires.
exceptions: []
exceptions:
- ecosystem: gittag
package: kuhy-credit
pinned: 0.3.1
reason: >-
sickdays 0.5.8 (its newest release) pins kuhy-credit 0.3.1 exactly, so
pip refuses 0.3.2 next to it. Taking 0.3.2 means holding sickdays back
instead; sickdays is the dependency the gate actually runs.
blocked_by: transitive:sickdays@0.5.8

View File

@ -0,0 +1,112 @@
"""Off-thread plumbing for the lock screen's food lookup.
The lookup used to run on the Tk thread. A food missing from the bank falls
through to an Open Food Facts request (8 s timeout), so the fullscreen lock
froze while it ran: a 1.5 s ``tk.mainloop`` stall was reported (c890f31a).
Same rules as :mod:`._gatelock_fetch`: the worker touches no widget and never
calls ``after`` (Tcl is not thread-safe); it only ``put``s its result on a
queue, and a poll scheduled on the Tk thread picks it up.
"""
from __future__ import annotations
import logging
import queue
import threading
from typing import TYPE_CHECKING
from diet_guard._gatelock_fetch import FETCH_POLL_MS
from diet_guard._resolve import lookup_candidates
if TYPE_CHECKING: # pragma: no cover - typing only
from collections.abc import Callable
from tkinter import Misc
from diet_guard._estimator import Nutrition
Candidates = list[tuple[str, Nutrition]]
__all__ = ["LookupRunner", "start_lookup"]
_logger = logging.getLogger(__name__)
def start_lookup(
lookup: Callable[[str], Candidates],
description: str,
) -> queue.Queue[Candidates]:
"""Run ``lookup(description)`` on a daemon thread; return the result queue.
Daemon so a hung request can never keep the process alive after the window
is gone -- the gate is a ``Type=oneshot`` unit and must exit.
"""
result: queue.Queue[Candidates] = queue.Queue(maxsize=1)
def _worker() -> None:
"""Runs OFF the Tk thread. Touches no widget, by construction."""
candidates: Candidates = []
try:
candidates = lookup(description)
# An unreadable bank or a malformed OFF payload; network errors are
# already caught inside ``off_candidates``.
except OSError, ValueError, KeyError, TypeError:
_logger.exception("food lookup failed")
finally:
# Always feed the queue, so the poll never waits forever and the
# form never stays busy. An empty result asks for a manual kcal.
result.put(candidates)
threading.Thread(target=_worker, daemon=True).start()
return result
class LookupRunner:
"""One food lookup at a time, collected on the Tk thread.
The window stays live while a lookup runs, so the user can log the meal by
hand and move on. A result for a food no longer in the description box is
dropped: applied late, it would fill the next slot with the wrong macros.
"""
def __init__(
self,
root: Misc,
on_result: Callable[[Candidates], None],
current: Callable[[], str],
) -> None:
"""Bind the runner to the Tk root, the result handler and the live text.
Args:
root: The gate's Tk root (schedules the poll).
on_result: Fills the form from the candidates, on the Tk thread.
current: Returns the description box's text right now.
"""
self._root = root
self._on_result = on_result
self._current = current
self._result: queue.Queue[Candidates] | None = None
self._description = ""
def start(self, description: str) -> bool:
"""Start a lookup; return False, starting nothing, if one is running."""
if self._result is not None:
return False
self._result = start_lookup(lookup_candidates, description)
self._description = description
self._root.after(FETCH_POLL_MS, self.poll)
return True
def poll(self) -> None:
"""On the Tk thread: hand a finished result over, else poll again."""
result = self._result
if result is None:
return
try:
candidates = result.get_nowait()
except queue.Empty:
self._root.after(FETCH_POLL_MS, self.poll)
return
self._result = None
if self._current() == self._description:
self._on_result(candidates)

View File

@ -18,8 +18,8 @@ from diet_guard._budget_derived import protein_target_g
from diet_guard._foodbank import remember_food
from diet_guard._gatelock_delivery import _PullFlows
from diet_guard._gatelock_fillall import RECONCILE_LABELS, FillAllFlow, FillHooks
from diet_guard._gatelock_lookup import LookupRunner
from diet_guard._gatelock_ui import ERR, FG, UNIT_GRAMS
from diet_guard._resolve import lookup_candidates
from diet_guard._slots import slot_label
from diet_guard._state import entry_kcal, log_meal
from diet_guard._state_today import (
@ -45,6 +45,9 @@ _DASH_DESC_WIDTH = 22
class _GateMealFlow(_PullFlows):
"""Submit/lookup/log flow for a logged food."""
#: The off-thread food lookup, made on first use (it needs ``root``).
_lookup: LookupRunner | None = None
# -- slot walk ------------------------------------------------------------
def _clear_inputs(self) -> None:
@ -111,16 +114,19 @@ class _GateMealFlow(_PullFlows):
self._record(description, nutrition)
def _begin_lookup(self, description: str) -> None:
"""Step 1: look the food up, fill the label fields, offer alternatives.
"""Step 1: look the food up off the Tk thread; fill the form on return.
Nothing is logged here -- the user must see and confirm the filled
values (a second submit) before they are recorded. The food is looked
up at its natural basis (per 100 g / serving); the amount eaten scales
it, so the lookup never bakes in a portion.
values (a second submit) before they are recorded. A second submit
while a lookup runs is ignored.
"""
self._set_status(g.BUSY)
self.root.update_idletasks()
candidates = lookup_candidates(description)
if self._lookup is None:
self._lookup = LookupRunner(self.root, self._finish_lookup, self._get_desc)
if self._lookup.start(description):
self._set_status(g.BUSY)
def _finish_lookup(self, candidates: list[tuple[str, Nutrition]]) -> None:
"""Fill the label fields and offer alternatives, or ask for kcal."""
if not candidates:
# Not found: the kcal field takes focus, the cue names it.
self._set_status(f"{g.ERROR} {g.source_glyph('')} kcal", error=True)

View File

@ -0,0 +1,160 @@
"""Tests for the off-thread food lookup of MealGate.
Covers :mod:`._gatelock_lookup` (``start_lookup``, ``LookupRunner``) and the
two ``_GateMealFlow`` methods that use it. The functional fake ``tk`` widgets
and the ``gate`` fixture live in ``conftest.py``.
"""
from __future__ import annotations
import queue
import threading
import time
from typing import TYPE_CHECKING
from unittest.mock import MagicMock, patch
from diet_guard import _gatelock_glyphs as g
from diet_guard import _gatelock_lookup
from diet_guard._gatelock_fetch import FETCH_POLL_MS
from diet_guard._gatelock_lookup import LookupRunner, start_lookup
from diet_guard.tests.conftest import _nutrition
if TYPE_CHECKING:
import pytest
from diet_guard._estimator import Nutrition
from diet_guard._gatelock import MealGate
Candidates = list[tuple[str, Nutrition]]
def _failing_lookup(_desc: str) -> Candidates:
msg = "bad payload"
raise ValueError(msg)
class TestStartLookup:
"""The worker thread always feeds its queue."""
def test_puts_result(self) -> None:
"""A finished lookup's candidates land on the queue."""
found = [("apple", _nutrition(95, 100))]
assert start_lookup(lambda _d: found, "apple").get(timeout=5) == found
def test_failure_puts_empty_and_logs(
self, caplog: pytest.LogCaptureFixture
) -> None:
"""A failing lookup is logged and still answers, with no candidates."""
with caplog.at_level("ERROR"):
assert start_lookup(_failing_lookup, "x").get(timeout=5) == []
assert any("food lookup failed" in r.message for r in caplog.records)
class TestLookupRunner:
"""One lookup at a time, collected by a Tk-side poll."""
def test_start_schedules_poll(self) -> None:
"""Starting a lookup schedules the poll and reports it started."""
root = MagicMock()
runner = LookupRunner(root, MagicMock(), lambda: "apple")
with patch.object(_gatelock_lookup, "start_lookup", return_value=queue.Queue()):
assert runner.start("apple") is True
root.after.assert_called_once_with(FETCH_POLL_MS, runner.poll)
def test_second_start_while_running_is_ignored(self) -> None:
"""A second submit during a lookup starts nothing."""
runner = LookupRunner(MagicMock(), MagicMock(), lambda: "slow")
with patch.object(
_gatelock_lookup, "start_lookup", return_value=queue.Queue()
) as start:
runner.start("first")
assert runner.start("second") is False
start.assert_called_once()
def test_poll_reschedules_until_done(self) -> None:
"""An empty queue polls again; a result goes to the handler once."""
root, on_result = MagicMock(), MagicMock()
runner = LookupRunner(root, on_result, lambda: "apple")
pending: queue.Queue[Candidates] = queue.Queue()
with patch.object(_gatelock_lookup, "start_lookup", return_value=pending):
runner.start("apple")
runner.poll()
assert root.after.call_count == 2
on_result.assert_not_called()
pending.put([])
runner.poll()
on_result.assert_called_once_with([])
runner.poll() # nothing in flight any more: a stray poll is a no-op
assert root.after.call_count == 2
on_result.assert_called_once()
def test_start_does_not_wait_for_the_lookup(self) -> None:
"""The Tk thread returns at once while the lookup is still running."""
release = threading.Event()
def slow_lookup(desc: str) -> Candidates:
release.wait(5)
return [(desc, _nutrition())]
runner = LookupRunner(MagicMock(), MagicMock(), lambda: "slow")
began = time.perf_counter()
with patch.object(_gatelock_lookup, "lookup_candidates", slow_lookup):
runner.start("slow")
assert time.perf_counter() - began < 0.1
result = runner._result
assert result is not None
assert result.empty()
release.set()
assert result.get(timeout=5)[0][0] == "slow"
class TestGateLookup:
"""Step one of a submit: the gate fills the form from a lookup."""
def _lookup(self, gate: MealGate, found: Candidates) -> None:
"""Run ``_begin_lookup`` with a ready result, then collect it."""
ready: queue.Queue[Candidates] = queue.Queue()
ready.put(found)
gate._set_desc("apple")
with patch.object(_gatelock_lookup, "start_lookup", return_value=ready):
gate._begin_lookup("apple")
assert gate._vars.status.get() == g.BUSY
assert gate._lookup is not None
gate._lookup.poll()
def test_no_candidates(self, gate: MealGate) -> None:
"""No match asks for a manual value."""
self._lookup(gate, [])
assert gate._vars.status.get().startswith(g.ERROR)
def test_single_candidate(self, gate: MealGate) -> None:
"""A single match fills the fields and invites review."""
self._lookup(gate, [("apple", _nutrition(95, 100))])
assert gate._vars.status.get().startswith(g.OK)
assert g.TIMES not in gate._vars.status.get()
def test_multiple_candidates(self, gate: MealGate) -> None:
"""Several matches invite picking another."""
self._lookup(gate, [("a", _nutrition(95, 100)), ("b", _nutrition(120, 100))])
assert gate._vars.status.get().endswith(f" {g.TIMES}2")
def test_second_submit_keeps_busy_status(self, gate: MealGate) -> None:
"""A submit during a lookup neither restarts it nor touches the status."""
with patch.object(_gatelock_lookup, "start_lookup", return_value=queue.Queue()):
gate._begin_lookup("apple")
gate._set_status("other")
gate._begin_lookup("apple")
assert gate._vars.status.get() == "other"
def test_result_for_a_food_no_longer_typed_is_dropped(self, gate: MealGate) -> None:
"""A late result never fills the form for whatever the user moved on to."""
pending: queue.Queue[Candidates] = queue.Queue()
gate._set_desc("apple")
with patch.object(_gatelock_lookup, "start_lookup", return_value=pending):
gate._begin_lookup("apple")
gate._clear_inputs() # the meal was logged by hand; the form moved on
pending.put([("apple", _nutrition(95, 100))])
assert gate._lookup is not None
gate._lookup.poll()
assert gate._widgets.macros.kcal.get() == ""
assert gate._get_desc() == ""

View File

@ -1,8 +1,9 @@
"""Tests for the nutrition model, lookup, and meal-building flow of MealGate.
"""Tests for the nutrition model and meal-building flow of MealGate.
Covers :mod:`._gatelock_nutrition` (reference -> total maths, suggestions,
unit toggling) and :mod:`._gatelock_mealflow` (submit/lookup/record, the
dashboard, and multi-item meals). The functional fake ``tk`` widgets and the
unit toggling) and :mod:`._gatelock_mealflow` (submit/record, the
dashboard, and multi-item meals). Lookup tests moved to
:mod:`test_gatelock_lookup`. The functional fake ``tk`` widgets and the
``gate`` fixture live in ``conftest.py`` and are shared with
:mod:`test_gatelock`.
"""
@ -156,41 +157,6 @@ class TestSubmit:
submit.assert_called_once()
class TestLookup:
"""Step one: filling the form from a lookup."""
def test_no_candidates(self, gate: MealGate) -> None:
"""No match asks for a manual value."""
gate._set_desc("nonsense")
with patch.object(_gatelock_mealflow, "lookup_candidates", return_value=[]):
gate._begin_lookup("nonsense")
assert gate._vars.status.get().startswith(g.ERROR)
def test_single_candidate(self, gate: MealGate) -> None:
"""A single match fills the fields and invites review."""
with patch.object(
_gatelock_mealflow,
"lookup_candidates",
return_value=[("apple", _nutrition(95, 100))],
):
gate._begin_lookup("apple")
assert gate._vars.status.get().startswith(g.OK)
assert g.TIMES not in gate._vars.status.get()
def test_multiple_candidates(self, gate: MealGate) -> None:
"""Several matches invite picking another."""
with patch.object(
_gatelock_mealflow,
"lookup_candidates",
return_value=[
("a", _nutrition(95, 100)),
("b", _nutrition(120, 100)),
],
):
gate._begin_lookup("apple")
assert gate._vars.status.get().endswith(f"{g.TIMES}2")
class TestRecord:
"""Logging a meal and advancing the slot walk."""

View File

@ -7,9 +7,9 @@ dependencies = [
"gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock",
"crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync",
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays",
"kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.0#subdirectory=kuhy_credit",
"kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.1#subdirectory=kuhy_credit",
"holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds",
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.6#subdirectory=sickdays",
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.8#subdirectory=sickdays",
"claude-agent-sdk==0.2.187",
"defusedxml==0.7.1",
"httpx==0.28.1",

View File

@ -11,7 +11,7 @@ gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gate
holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds
httpx==0.28.1
hypothesis==6.170.17
kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.0#subdirectory=kuhy_credit
kuhy-credit @ git+https://github.com/kuhyx/utils@kuhy-credit-v0.3.1#subdirectory=kuhy_credit
mcp==2.3.0
mypy==2.4.1
pre-commit==4.7.0
@ -26,4 +26,4 @@ pytest-xvfb==3.1.1
requests==2.34.2
rtdb-relay @ git+https://github.com/kuhyx/utils@rtdb-relay-v0.2.1#subdirectory=rtdb_relay
ruff==0.17.6
sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.6#subdirectory=sickdays
sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.8#subdirectory=sickdays