From d9b3d57900f5535a9a7e999c6b54205a69f50e53 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Sun, 23 Aug 2026 09:17:30 +0200 Subject: [PATCH] docs: record the phone-side catering import, verified on device MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Documents the Dart mirror, the phone's two triggers (guarded log screen, unguarded settings button — the same split as the gate vs the CLI), and the device-local fetch marker on both sides. The three parity traps are written where the next reader will hit them, because each survived review once: `_ENERGY_TOLERANCE` is 0.35 and the "~1%" elsewhere describes the captured day, not the threshold; `jsonEncode(435)` emits `435` where Python emits `435.0`; and Dart's `List.sort` is unstable where Python's `sorted` is stable. Also states plainly what the shared fixture does *not* prove: that the two implementations agree on a fixed payload, not that they agree against the live panel. **That second claim is now observed rather than inferred.** On 2026-08-23, with the credential synced to the phone and nothing typed on it, the phone logged into the live panel and banked 5 dishes. Pulling that back to the PC left all five curated-bank records carrying their original 08:00:03 PC timestamps, unchanged — the phone's records were byte-identical, so its idempotency guard wrote nothing. Any disagreement would instead have re-added them with fresh stamps and started the republish flood. Split into a second file for the 250-line cap. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01QQpCiPaSbyRpwsLkH3dgsX --- AGENTS.md | 7 ++- NEXT_SESSION.md | 108 ---------------------------------- app/pubspec.yaml | 2 +- docs/kuchnia-wikinga-phone.md | 50 ++++++++++++++++ docs/kuchnia-wikinga.md | 24 ++++++-- 5 files changed, 74 insertions(+), 117 deletions(-) delete mode 100644 NEXT_SESSION.md create mode 100644 docs/kuchnia-wikinga-phone.md diff --git a/AGENTS.md b/AGENTS.md index 5de3d19..c1aa72b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -225,9 +225,10 @@ phone show both. - Don't strip the `components` field: nothing writes it, but historical composite entries and the food bank read it, and it is part of the sync wire format. -- Don't let the catering import write a log entry unattended: a delivered - meal is not an eaten meal, and the gate would satisfy its own checkpoint. - See [docs/kuchnia-wikinga.md](docs/kuchnia-wikinga.md). +- Don't let the catering import log unattended (delivered ≠ eaten), don't let + its Python and Dart halves drift (one shared fixture gates them), and don't + make the synced password machine-local — the phone could not then fetch. + [docs](docs/kuchnia-wikinga.md), [phone](docs/kuchnia-wikinga-phone.md). - Don't add a dependency without the production install-path check above. - Don't reintroduce a seal/`chattr +i` on the budget file. - Don't re-add a Linux embedder target, and don't change the wrapper's port or diff --git a/NEXT_SESSION.md b/NEXT_SESSION.md deleted file mode 100644 index 13f7589..0000000 --- a/NEXT_SESSION.md +++ /dev/null @@ -1,108 +0,0 @@ -# Next session: put the Kuchnia importer on the phone - -Paste the prompt below into a fresh session. Delete this file once it's done. - ---- - -## Prompt - -Port the Kuchnia Wikinga catering import to the Flutter app so the phone can -fetch the day's dishes **with the PC completely off**. Today the importer is -Python-only and the phone just receives the banked results through sync. - -Read `docs/kuchnia-wikinga.md` first — it has the recovered API map, the traps, -and why each decision went the way it did. `CLAUDE.md`'s "Do NOT" section and -`docs/meal-schedule.md` are the other two load-bearing reads. - -State: `main` is clean at `93669776b6d`. Phone (Pixel 6a, `23181JEGR08034`) -has 1.0.55/vc130 installed and verified working; repo is at 1.0.56. - -### Decisions already made (do not re-litigate) - -The user answered these explicitly on 2026-08-23: - -1. **The phone fetches directly.** Not "PC banks on a timer and the phone - receives it" — it must work when the PC is unavailable. So the HTTP client, - the parser and the slot spread all get Dart implementations. -2. **The catering password syncs**, so a complete reinstall restores it. The - user chose this knowing the trade-off. It will be **plaintext in Firebase - RTDB** — the rest of the synced state is not encrypted either, so do not - describe it as "encrypted like everything else". -3. That contradicts the current doc rule ("a credential must never leave the - machine it was entered on"). **Rewrite that paragraph**, don't bolt an - "Exception:" onto it (`memories/mistakes.md`). -4. `kuchnia_session.json` (the live cookie) stays **device-local**. It is - regenerable, so syncing it buys nothing and widens exposure. - -### Sequence (the first item is a prerequisite, not a follow-up) - -1. **Settings field + credential sync.** The phone cannot fetch until the - password is on it. Sync it as its own record; check `sync_merge/` relays a - new record type rather than dropping it — it enumerates handlers - (`_banks`, `_budget`, `_daylog`, `_schedule`), the same fixed-key-set shape - that would silently drop an unknown field. -2. **Fetch + parse + bank**, read-only. No logging. Verify against the live - panel on-device. -3. **Wire to the log flow**, keeping the never-log-unattended rule: the phone - *offers* dishes, the user still taps to log. - -### The parity risk stops being theoretical - -`_kuchnia_spread.py`'s docstring says the integer-only rule is theoretical -"because the phone has no importer and so no mirror of this code". **This port -creates that mirror — update that docstring in the same commit.** Two things -now have to agree exactly across languages: - -- **`i * S // N` → `i * S ~/ N`.** Python's `round` is banker's, Dart's is - half-away-from-zero. A float path silently desyncs which slot a dish lands - in, and a slot one device offers while the other does not is a checkpoint - that can never be satisfied — a permanent lock. -- **`_energy_is_consistent`'s ~1% tolerance**, which *drops* dishes. If the - two sides disagree about which dishes pass, each re-adds what the other - dropped, `add_manual_entry` restamps `t` unconditionally, and the curated - bank republishes to every peer on every refresh. - -Gate both with **one shared JSON fixture** committed to the repo, asserted by -`pytest` *and* `flutter test` — same input, same expected dish list, dropped -dishes, slot assignment and bank keys. Two independently-written suites from -the same prose is not a gate. - -Also verify a Dart-banked and a Python-banked record for the same dish are -**byte-identical JSON**; float formatting (`435.0` vs `435`) is the likely -divergence and would cause the same rewrite/restamp flood. - -### Platform seam - -`app/` builds Android + web only; the desktop app *is* the web build in Chrome -(`app/test/repo_invariants_test.dart` enforces this). The catering panel sends -no CORS headers, so a browser cannot call it — **make the feature Android-only -and disabled on web**. `dart:io` does not fail a web compile, it throws at -runtime as a blank white window, so branch on `kIsWeb` *before* any -`Platform.is…`, and keep `dart:io` to `*_io.dart` seams. `package:http` and -`flutter_secure_storage` are already dependencies. - -### Constraints - -- 250 lines per file (860 Python lines → expect 6+ Dart files). -- 100% branch coverage on the Python side; `flutter test` green. -- `pre-commit run --files ` before finishing. -- Any new Python state path must patch `conftest._isolate_state` in the *same* - edit, or the suite writes to live user data. -- Never log a delivery unattended — a delivered meal is not an eaten meal. - -### Verify on the phone, not just in tests - -Mobile is the primary platform (`phone-deploy` skill). Deploy with -`~/.claude/scripts/phone_deploy.sh ~/diet-guard/app --release`, which now runs -a pre-install sync gate (`scripts/sync_freshness.py`, added 2026-08-23) and -picks the build number itself. Never uninstall or `pm clear`. - -**The phone is the user's daily driver — do not leave the app mid-flow, and -do not grab focus on their desktop.** (Two complaints on 2026-08-22 about a -test gate window and an emulator stealing the cursor.) - -### Done - -`python -m diet_guard kuchnia` and the phone's own fetch produce the **same -banked dishes for the same day**, proven by the shared fixture in both suites -and observed once on-device with the PC's importer not run that day. diff --git a/app/pubspec.yaml b/app/pubspec.yaml index 89fd722..3d02d50 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.65+1 +version: 1.0.66+1 environment: sdk: ^3.12.2 diff --git a/docs/kuchnia-wikinga-phone.md b/docs/kuchnia-wikinga-phone.md new file mode 100644 index 0000000..d86c6f5 --- /dev/null +++ b/docs/kuchnia-wikinga-phone.md @@ -0,0 +1,50 @@ +# Kuchnia Wikinga on the phone + +The Dart half of the catering import. Read +[kuchnia-wikinga.md](kuchnia-wikinga.md) first — the API map, the traps and the +delivered-is-not-eaten rule all live there and are not repeated here. + +`app/` carries a Dart mirror of the whole walk, so the feature works with the +PC switched off. The files map one-to-one: + +| Python | Dart | +|---|---| +| `_kuchnia_client.py` | `services/kuchnia_client.dart` | +| `_kuchnia_orders.py` | `services/kuchnia_orders.dart` | +| `_kuchnia_parse.py` | `services/kuchnia_parse.dart` | +| `_kuchnia_spread.py` | `services/kuchnia_spread.dart` | +| `_kuchnia_import.py` | `services/kuchnia_import.dart` | +| `_gatelock_kuchnia.py` | `screens/log_meal_kuchnia_mixin.dart` + `services/kuchnia_queue.dart` | + +**Android only.** The panel sends no CORS headers, so a browser cannot call it; +`refreshDelivery` checks `kIsWeb` first and returns a reason. The settings +*fields* still show on web — the desktop app is where a password is most likely +to be typed, and the credential syncs from there to the phone. + +`package:http` has no cookie jar, so the `SESSION` cookie is read off the login +response and replayed by hand. `Response.headers` folds duplicate `set-cookie` +values into one comma-joined string, so the parser splits on both `,` and `;`. + +### What the parity gate covers, and what it does not + +`tests/fixtures/kuchnia_day.json` is one payload with one expected result, read +by **both** `diet_guard/tests/test_kuchnia_parity.py` and +`app/test/kuchnia_parity_test.dart`. It pins the dish list, the dropped dishes, +the slot assignment, the bank keys and the encoded records. A second fixture, +`kuchnia_credential.json`, does the same for the credential adapter. + +Three traps it exists for, each of which passed review once: + +- **`_ENERGY_TOLERANCE` is 0.35**, not the "~1%" quoted above for the captured + day. Porting 0.01 would drop dishes the PC keeps, and then each device + re-adds what the other dropped. +- **`jsonEncode(435)` emits `435` where Python emits `435.0`.** Every Dart + macro is declared `double`, and the fixture compares canonicalised encodings, + not just values — `435 == 435.0` is true, so a value-only test misses this. +- **Dart's `List.sort` is unstable; Python's `sorted` is stable.** The Dart + comparator carries the payload index as a final tiebreak, and the fixture + contains two dishes sharing both a priority and a name. + +The gate proves the two implementations agree **on a fixed payload**. It does +not prove they agree against the live panel on a given day; that is a separate, +on-device observation. diff --git a/docs/kuchnia-wikinga.md b/docs/kuchnia-wikinga.md index bdfa618..b97b372 100644 --- a/docs/kuchnia-wikinga.md +++ b/docs/kuchnia-wikinga.md @@ -135,7 +135,7 @@ returns a reason string and the caller carries on. ## Triggers -Three, all event-driven. **No new systemd timer** — `diet-guard-sync.timer` was +All event-driven. **No new systemd timer** — `diet-guard-sync.timer` was deliberately deleted and `install.sh` actively uninstalls it. | | Where | Guarded? | @@ -143,11 +143,19 @@ deliberately deleted and `install.sh` actively uninstalls it. | CLI | `diet-guard kuchnia` | no — an explicit ask always goes and looks | | Gate | `MealGate.on_focus_ready` → `_autoload_delivery` | yes | | After a meal | `_cli_log.cmd_ate`, on the existing background thread | yes | +| Phone: log screen | `LogMealKuchniaMixin.loadTodaysDelivery` on `initState` | yes | +| Phone: settings | the "Fetch today's delivery" button | no — same rule as the CLI | -The guarded pair go through `refresh_delivery_once`, which skips the walk when -today has already been fetched (`kuchnia_last_import`, one ISO date). Without -it the after-log hook would pay a login plus three requests after *every* meal. -Only a clean fetch records the date, so an outage is retried. +The guarded ones go through `refresh_delivery_once` (Python) or +`KuchniaQueueService.refreshOnce` (Dart), which skip the walk when today has +already been fetched. Without that the after-log hook would pay a login plus +three requests after *every* meal. Only a clean fetch records the date, so an +outage is retried. + +The marker is **device-local on both sides** — `kuchnia_last_import` under +`~/.config` on the PC, `kuchnia_last_import.json` in the app's document store +on the phone. It is a rate limit, not shared state: syncing it would let one +device's fetch suppress the other's. **Why the gate hook is in `on_focus_ready` and not `_cli_gate._should_lock`:** `gate_is_due()` reads only the food log and the schedule, never the food bank, @@ -211,6 +219,12 @@ news: it must not talk over the prompt telling the user which slots to fill. unify them would strand every existing entry. The Dart test above pins the agreement, so a future non-Polish dish name cannot break it unnoticed. +## The phone runs the same import + +`app/` carries a Dart mirror of the whole walk, so the feature works with the +PC switched off — including the cross-language parity gate that keeps the two +from drifting. See [kuchnia-wikinga-phone.md](kuchnia-wikinga-phone.md). + ## Re-probing the API `scripts/probe_kuchnia.py` (login, order walk) and