mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-28 22:39:04 +01:00
Compare commits
2 Commits
893eea4901
...
5add5fa55f
| Author | SHA1 | Date | |
|---|---|---|---|
| 5add5fa55f | |||
| ffd30b0476 |
@ -111,12 +111,12 @@ repos:
|
|||||||
# direct URLs for one distribution, so any pin that drifts from
|
# direct URLs for one distribution, so any pin that drifts from
|
||||||
# the manifest becomes an unresolvable hook environment rather
|
# the manifest becomes an unresolvable hook environment rather
|
||||||
# than merely an old one.
|
# than merely an old one.
|
||||||
- gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock
|
- 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
|
- 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
|
- 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
|
- sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays
|
||||||
- rtdb-relay @ git+https://github.com/kuhyx/utils@rtdb-relay-v0.2.0#subdirectory=rtdb_relay
|
- rtdb-relay @ git+https://github.com/kuhyx/utils@rtdb-relay-v0.2.0#subdirectory=rtdb_relay
|
||||||
- claude-agent-sdk==0.2.183
|
- claude-agent-sdk==0.2.184
|
||||||
- httpx==0.28.1
|
- httpx==0.28.1
|
||||||
- mcp>=2,<3
|
- mcp>=2,<3
|
||||||
- pydantic
|
- pydantic
|
||||||
|
|||||||
@ -22,6 +22,7 @@ show adherence, streaks, a YTD tally and weekly/monthly averages, all derived fr
|
|||||||
- test-changed: `scripts/test_changed.sh` (maps py and app/ dart changes to their tests, no coverage)
|
- test-changed: `scripts/test_changed.sh` (maps py and app/ dart changes to their tests, no coverage)
|
||||||
- lint: `pre-commit run --all-files`
|
- lint: `pre-commit run --all-files`
|
||||||
- coverage: `python -m pytest -n auto -p no:sugar -q` (writes coverage.lcov)
|
- coverage: `python -m pytest -n auto -p no:sugar -q` (writes coverage.lcov)
|
||||||
|
- deploy: `bash scripts/deploy.sh` (pull ~/src/diet-guard, system-Python install, desktop app; health-checked)
|
||||||
- coverage-gaps: `coverage-gaps coverage.lcov`
|
- coverage-gaps: `coverage-gaps coverage.lcov`
|
||||||
|
|
||||||
## Scheduling
|
## Scheduling
|
||||||
@ -238,10 +239,8 @@ only); the CLI, gate and phone show both.
|
|||||||
|
|
||||||
## Flutter/Dart AI rules
|
## Flutter/Dart AI rules
|
||||||
|
|
||||||
_Vendored from [flutter/flutter
|
_Vendored from [flutter/flutter docs/rules/rules.md](https://github.com/flutter/flutter/blob/main/docs/rules/rules.md)
|
||||||
docs/rules/rules.md](https://github.com/flutter/flutter/blob/main/docs/rules/rules.md) via
|
via `~/.claude/CLAUDE.md`. Re-fetch periodically. Split across five files for the 250-line cap._
|
||||||
`~/.claude/CLAUDE.md` Flutter AI tooling setup. Re-fetch periodically. Split across five files to
|
|
||||||
stay under the 250-line cap._
|
|
||||||
|
|
||||||
- [Language, style and architecture](docs/DOCS-flutter-rules-language.md)
|
- [Language, style and architecture](docs/DOCS-flutter-rules-language.md)
|
||||||
- [Architecture, lint rules, state and data](docs/DOCS-flutter-rules-state-and-data.md)
|
- [Architecture, lint rules, state and data](docs/DOCS-flutter-rules-state-and-data.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
|
Then restart Claude Code in this repo and approve the project MCP server
|
||||||
(registered in `.mcp.json`).
|
(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
|
## Development
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import 'package:diet_guard_app/services/foodbank_service.dart';
|
|||||||
import 'package:diet_guard_app/services/github_client_factory.dart';
|
import 'package:diet_guard_app/services/github_client_factory.dart';
|
||||||
import 'package:diet_guard_app/services/health_steps.dart';
|
import 'package:diet_guard_app/services/health_steps.dart';
|
||||||
import 'package:diet_guard_app/services/log_storage_service.dart';
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
||||||
|
import 'package:diet_guard_app/services/sync_attempt.dart';
|
||||||
import 'package:diet_guard_app/services/sync_health.dart';
|
import 'package:diet_guard_app/services/sync_health.dart';
|
||||||
import 'package:diet_guard_app/services/sync_service.dart';
|
import 'package:diet_guard_app/services/sync_service.dart';
|
||||||
import 'package:diet_guard_app/services/sync_settings.dart';
|
import 'package:diet_guard_app/services/sync_settings.dart';
|
||||||
@ -68,6 +69,7 @@ mixin LogMealSyncMixin<T extends StatefulWidget> on State<T>
|
|||||||
Future<void> autoSync() async {
|
Future<void> autoSync() async {
|
||||||
if (autoSyncing) return;
|
if (autoSyncing) return;
|
||||||
autoSyncing = true;
|
autoSyncing = true;
|
||||||
|
bool? landed; // null until the sync itself ran
|
||||||
try {
|
try {
|
||||||
final settings = await SyncSettings.load();
|
final settings = await SyncSettings.load();
|
||||||
// Either backend counts. `isConfigured` means "has a GitHub token", so
|
// Either backend counts. `isConfigured` means "has a GitHub token", so
|
||||||
@ -87,17 +89,19 @@ mixin LogMealSyncMixin<T extends StatefulWidget> on State<T>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final client = createGitHubClient(settings, httpClient: syncHttpClient);
|
final client = createGitHubClient(settings, httpClient: syncHttpClient);
|
||||||
try {
|
landed = await attemptSync(() async {
|
||||||
// Foreground only (this mixin is UI), and never prompts: fresh
|
try {
|
||||||
// Health Connect steps ride along in this tick's body.json.
|
// Foreground only (this mixin is UI), and never prompts: fresh
|
||||||
await refreshSteps();
|
// Health Connect steps ride along in this tick's body.json.
|
||||||
await runSync(await syncBackend(client));
|
await refreshSteps();
|
||||||
await SyncHealth.recordSuccess();
|
await runSync(await syncBackend(client));
|
||||||
} finally {
|
} finally {
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
await refreshSlots();
|
// Outside attemptSync: a refresh error is no sync failure.
|
||||||
|
if (landed) await refreshSlots();
|
||||||
await refreshSyncHealth();
|
await refreshSyncHealth();
|
||||||
} on Object catch (error, stackTrace) {
|
} on Object catch (error, stackTrace) {
|
||||||
// Best-effort, but never silent: this swallowed the reason a desktop
|
// Best-effort, but never silent: this swallowed the reason a desktop
|
||||||
@ -110,7 +114,9 @@ mixin LogMealSyncMixin<T extends StatefulWidget> on State<T>
|
|||||||
error: error,
|
error: error,
|
||||||
stackTrace: stackTrace,
|
stackTrace: stackTrace,
|
||||||
);
|
);
|
||||||
await SyncHealth.recordFailure();
|
// attemptSync already recorded the sync's own outcome; only a failure
|
||||||
|
// before it (loading settings) is one here.
|
||||||
|
if (landed == null) await SyncHealth.recordFailure();
|
||||||
if (mounted) await refreshSyncHealth();
|
if (mounted) await refreshSyncHealth();
|
||||||
} finally {
|
} finally {
|
||||||
autoSyncing = false;
|
autoSyncing = false;
|
||||||
|
|||||||
@ -15,6 +15,7 @@ import 'package:diet_guard_app/services/github_client_factory.dart';
|
|||||||
import 'package:diet_guard_app/services/kuchnia_credential_service.dart';
|
import 'package:diet_guard_app/services/kuchnia_credential_service.dart';
|
||||||
import 'package:diet_guard_app/services/log_storage_service.dart';
|
import 'package:diet_guard_app/services/log_storage_service.dart';
|
||||||
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
||||||
|
import 'package:diet_guard_app/services/sync_attempt.dart';
|
||||||
import 'package:diet_guard_app/services/sync_device_id.dart';
|
import 'package:diet_guard_app/services/sync_device_id.dart';
|
||||||
import 'package:diet_guard_app/services/sync_health.dart';
|
import 'package:diet_guard_app/services/sync_health.dart';
|
||||||
import 'package:diet_guard_app/services/sync_service.dart';
|
import 'package:diet_guard_app/services/sync_service.dart';
|
||||||
@ -90,21 +91,14 @@ Future<bool> backgroundSyncPush({http.Client? httpClient}) async {
|
|||||||
return true; // nothing to push; don't retry
|
return true; // nothing to push; don't retry
|
||||||
}
|
}
|
||||||
final client = createGitHubClient(settings, httpClient: httpClient);
|
final client = createGitHubClient(settings, httpClient: httpClient);
|
||||||
try {
|
// Never silent: attemptSync records the outcome, the only signal that a
|
||||||
await runSync(await syncBackend(client));
|
// background push failed. False -> WorkManager retries with backoff.
|
||||||
await SyncHealth.recordSuccess();
|
final landed = await attemptSync(() async {
|
||||||
return true;
|
try {
|
||||||
} on Exception catch (error, stackTrace) {
|
await runSync(await syncBackend(client));
|
||||||
// Never silent: this is the only signal that a background push failed.
|
} finally {
|
||||||
log(
|
client.close();
|
||||||
'diet_guard background sync failed',
|
}
|
||||||
level: 1000,
|
});
|
||||||
error: error,
|
return landed;
|
||||||
stackTrace: stackTrace,
|
|
||||||
);
|
|
||||||
await SyncHealth.recordFailure();
|
|
||||||
return false; // offline / transient error -> retry with backoff
|
|
||||||
} finally {
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
42
app/lib/services/sync_attempt.dart
Normal file
42
app/lib/services/sync_attempt.dart
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/// Runs one sync and records its outcome for the "not syncing" banner.
|
||||||
|
///
|
||||||
|
/// The banner said "Not syncing" on a phone whose sync looked fine, and the
|
||||||
|
/// release build keeps no log that could say why (see `sync_health.dart`).
|
||||||
|
/// So a failure here also goes to the board through the app-wide reporter:
|
||||||
|
/// the next one files its own ticket with the exception that caused it.
|
||||||
|
/// Inside a WorkManager isolate no reporter is installed, so a background
|
||||||
|
/// failure is only recorded, not filed.
|
||||||
|
///
|
||||||
|
/// Only the sync itself is timed in: screen refreshes after a landed sync
|
||||||
|
/// run outside, so their errors never mark a working sync as failed.
|
||||||
|
library;
|
||||||
|
|
||||||
|
import 'dart:developer';
|
||||||
|
|
||||||
|
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart' show AppErrors;
|
||||||
|
import 'package:diet_guard_app/services/sync_health.dart';
|
||||||
|
|
||||||
|
/// Runs [sync]; records success, or failure plus a `sync.failed` report.
|
||||||
|
///
|
||||||
|
/// Returns whether the sync landed. Never throws.
|
||||||
|
Future<bool> attemptSync(Future<void> Function() sync) async {
|
||||||
|
try {
|
||||||
|
await sync();
|
||||||
|
} on Object catch (error, stackTrace) {
|
||||||
|
log(
|
||||||
|
'diet_guard sync failed',
|
||||||
|
level: 1000,
|
||||||
|
error: error,
|
||||||
|
stackTrace: stackTrace,
|
||||||
|
);
|
||||||
|
AppErrors.instance?.reporter.report(
|
||||||
|
'sync.failed',
|
||||||
|
error,
|
||||||
|
stack: stackTrace,
|
||||||
|
);
|
||||||
|
await SyncHealth.recordFailure();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
await SyncHealth.recordSuccess();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@ -5,10 +5,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: android_file_picker
|
name: android_file_picker
|
||||||
sha256: "04c1306eda42c23ecbf449c909f4fa05c5e66fc48cf5f994d8d08b9d45e5396b"
|
sha256: ec3cf2a2c7a9b2bd56747e85f80efdd994e563049dfb0981a23903acfdd58cb5
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.5"
|
version: "2.2.0"
|
||||||
archive:
|
archive:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -207,11 +207,11 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: data_transfer_flutter
|
path: data_transfer_flutter
|
||||||
ref: "data_transfer_flutter-v0.2.2"
|
ref: "data_transfer_flutter-v0.2.3"
|
||||||
resolved-ref: eef7af4c86837ee82891493587ff3456297e5dea
|
resolved-ref: bb2f92f459283a29ad4864cf37704cc5f749cc6d
|
||||||
url: "https://github.com/kuhyx/utils"
|
url: "https://github.com/kuhyx/utils"
|
||||||
source: git
|
source: git
|
||||||
version: "0.2.2"
|
version: "0.2.3"
|
||||||
dbus:
|
dbus:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -280,10 +280,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: file_picker
|
name: file_picker
|
||||||
sha256: "079dc46b753abbddb7f77cd87e6ed9ecc62902580707514568321bdf8e050a86"
|
sha256: c3780977700b8962c3b9b6a9aa4e2c988956893d70d4a4fa2d7adf4ec9b9eaee
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "13.3.3"
|
version: "13.4.0"
|
||||||
file_picker_darwin:
|
file_picker_darwin:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -304,10 +304,10 @@ packages:
|
|||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
name: file_picker_platform_interface
|
name: file_picker_platform_interface
|
||||||
sha256: "336e7168306834a2b68e6168ed768e00d3fabeac23154d5793e277ca9f97cf20"
|
sha256: a2ef71c4628f92a4471d424576e3bf7042b40a2683c9f9a0e3033a7fc7f7763c
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.2.0"
|
version: "4.3.0"
|
||||||
file_picker_web:
|
file_picker_web:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -413,10 +413,10 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
name: flutter_local_notifications
|
name: flutter_local_notifications
|
||||||
sha256: fac349e7361f80591a0cedff03548f807282c9faee9fc6192763166e5f81def2
|
sha256: "7d37d69ded3028af4f22531a0097a3baa478a384dff2e23e873707c93df3b704"
|
||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "23.0.2"
|
version: "23.0.3"
|
||||||
flutter_local_notifications_linux:
|
flutter_local_notifications_linux:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@ -1067,11 +1067,11 @@ packages:
|
|||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
path: sync_settings_ui
|
path: sync_settings_ui
|
||||||
ref: "sync_settings_ui-v0.8.3"
|
ref: "sync_settings_ui-v0.8.4"
|
||||||
resolved-ref: dde5566b2c6eb38a8e1e58a4252925f3baca8ad1
|
resolved-ref: "15ef5b44f186cea0ab163cc88cb8743c51d58247"
|
||||||
url: "https://github.com/kuhyx/utils"
|
url: "https://github.com/kuhyx/utils"
|
||||||
source: git
|
source: git
|
||||||
version: "0.8.3"
|
version: "0.8.4"
|
||||||
synchronized:
|
synchronized:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: diet_guard_app
|
|||||||
description: "Companion phone app for diet_guard: log meals on the go."
|
description: "Companion phone app for diet_guard: log meals on the go."
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
version: 1.0.179+1
|
version: 1.0.181+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.12.2
|
sdk: ^3.12.2
|
||||||
@ -27,12 +27,12 @@ dependencies:
|
|||||||
data_transfer_flutter:
|
data_transfer_flutter:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: data_transfer_flutter-v0.2.2
|
ref: data_transfer_flutter-v0.2.3
|
||||||
path: data_transfer_flutter
|
path: data_transfer_flutter
|
||||||
fl_chart: 1.3.1
|
fl_chart: 1.3.1
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
flutter_local_notifications: 23.0.2
|
flutter_local_notifications: 23.0.3
|
||||||
flutter_secure_storage: 11.2.0
|
flutter_secure_storage: 11.2.0
|
||||||
|
|
||||||
# One-tap sign-in for the sync account. App-side only: crdt_sync stays pure
|
# One-tap sign-in for the sync account. App-side only: crdt_sync stays pure
|
||||||
@ -66,7 +66,7 @@ dependencies:
|
|||||||
sync_settings_ui:
|
sync_settings_ui:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: sync_settings_ui-v0.8.3
|
ref: sync_settings_ui-v0.8.4
|
||||||
path: sync_settings_ui
|
path: sync_settings_ui
|
||||||
url_launcher: 6.3.3
|
url_launcher: 6.3.3
|
||||||
uuid: 4.6.0
|
uuid: 4.6.0
|
||||||
|
|||||||
58
app/test/services/sync_attempt_test.dart
Normal file
58
app/test/services/sync_attempt_test.dart
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
|
||||||
|
show AppErrors, AppErrorsConfig, ErrorQueue;
|
||||||
|
import 'package:diet_guard_app/services/sync_attempt.dart';
|
||||||
|
import 'package:diet_guard_app/services/sync_health.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
const _config = AppErrorsConfig(
|
||||||
|
app: 'diet-guard',
|
||||||
|
repo: 'kuhyx/diet-guard',
|
||||||
|
package: 'diet_guard_app',
|
||||||
|
);
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
TestWidgetsFlutterBinding.ensureInitialized();
|
||||||
|
|
||||||
|
late ErrorQueue queue;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
SharedPreferences.setMockInitialValues({});
|
||||||
|
queue = await ErrorQueue.open(await SharedPreferences.getInstance());
|
||||||
|
AppErrors.instance = AppErrors.forTest(_config, queue);
|
||||||
|
});
|
||||||
|
|
||||||
|
tearDown(() => AppErrors.instance = null);
|
||||||
|
|
||||||
|
test('a sync that lands records success and reports nothing', () async {
|
||||||
|
expect(await attemptSync(() async {}), isTrue);
|
||||||
|
final status = await SyncHealth.read();
|
||||||
|
expect(status.lastSuccess, isNotNull);
|
||||||
|
expect(status.failureKind, isNull);
|
||||||
|
expect(queue.entries, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a failed sync records the failure and files its cause', () async {
|
||||||
|
final ok = await attemptSync(() async => throw StateError('locked'));
|
||||||
|
expect(ok, isFalse);
|
||||||
|
final status = await SyncHealth.read();
|
||||||
|
expect(status.failureKind, SyncFailureKind.failed);
|
||||||
|
final entry = queue.entries.single;
|
||||||
|
expect(entry.kind, 'sync.failed');
|
||||||
|
expect(entry.error, contains('locked'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without installed reporting a failure is still recorded', () async {
|
||||||
|
AppErrors.instance = null;
|
||||||
|
expect(await attemptSync(() async => throw StateError('x')), isFalse);
|
||||||
|
expect((await SyncHealth.read()).failureKind, SyncFailureKind.failed);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a later success clears the failure (the banner goes away)', () async {
|
||||||
|
await attemptSync(() async => throw StateError('offline'));
|
||||||
|
await attemptSync(() async {});
|
||||||
|
final status = await SyncHealth.read();
|
||||||
|
expect(status.failureKind, isNull);
|
||||||
|
expect(status.message, isNull);
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -6,12 +6,10 @@ Examples:
|
|||||||
python -m diet_guard ate "two slices of pizza" --grams 240
|
python -m diet_guard ate "two slices of pizza" --grams 240
|
||||||
python -m diet_guard ate "protein shake" --kcal 180
|
python -m diet_guard ate "protein shake" --kcal 180
|
||||||
python -m diet_guard status
|
python -m diet_guard status
|
||||||
python -m diet_guard averages
|
|
||||||
python -m diet_guard undo
|
python -m diet_guard undo
|
||||||
|
|
||||||
The daily budget lives outside the repo (so it is never exposed online) but is
|
``status`` and each log print the calories left of the day's budget (kept
|
||||||
shown freely on this machine: ``status`` and each log print how many calories
|
outside the repo) and which meal slots still need logging.
|
||||||
are left of the day's budget, plus which meal slots still need logging.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@ -19,6 +17,7 @@ from __future__ import annotations
|
|||||||
import sys
|
import sys
|
||||||
from typing import TYPE_CHECKING
|
from typing import TYPE_CHECKING
|
||||||
|
|
||||||
|
from diet_guard import _errors
|
||||||
from diet_guard._budget import (
|
from diet_guard._budget import (
|
||||||
BudgetFileCorruptError,
|
BudgetFileCorruptError,
|
||||||
BudgetNotInitializedError,
|
BudgetNotInitializedError,
|
||||||
@ -247,4 +246,5 @@ def main(argv: list[str] | None = None) -> int:
|
|||||||
A process exit code (0 on success).
|
A process exit code (0 on success).
|
||||||
"""
|
"""
|
||||||
args = parse_args(sys.argv[1:] if argv is None else argv)
|
args = parse_args(sys.argv[1:] if argv is None else argv)
|
||||||
|
_errors.install()
|
||||||
return _COMMANDS[args.command](args)
|
return _COMMANDS[args.command](args)
|
||||||
|
|||||||
45
diet_guard/_errors.py
Normal file
45
diet_guard/_errors.py
Normal 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)
|
||||||
@ -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
|
:mod:`._gatelock_mealflow` the submit/log flow, dashboard, callback errors and
|
||||||
pull-it-in buttons (``_GateMealFlow`` + ``_PullFlows``); :mod:`._gatelock_calendar`
|
pull-it-in buttons (``_GateMealFlow`` + ``_PullFlows``); :mod:`._gatelock_calendar`
|
||||||
the tab -- calendar, streaks, YTD tally (:mod:`._daystatus`), budget editing.
|
the tab -- calendar, streaks, YTD tally (:mod:`._daystatus`), budget editing.
|
||||||
``MealGate`` wires these together and owns the ``gatelock.LockWindow``.
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
@ -52,6 +51,7 @@ from gatelock import (
|
|||||||
wait_for_turn,
|
wait_for_turn,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
from diet_guard import _errors
|
||||||
from diet_guard._gate import due_slots
|
from diet_guard._gate import due_slots
|
||||||
from diet_guard._gatelock_calendar import _GateCalendar, make_calendar_vars
|
from diet_guard._gatelock_calendar import _GateCalendar, make_calendar_vars
|
||||||
from diet_guard._gatelock_core import _GateState
|
from diet_guard._gatelock_core import _GateState
|
||||||
@ -215,6 +215,7 @@ class MealGate(_GateCalendar):
|
|||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
"""Run the Tk loop (a hold or a meal logged elsewhere closes it), VT-safe."""
|
"""Run the Tk loop (a hold or a meal logged elsewhere closes it), VT-safe."""
|
||||||
watch_hold(self.root, self.close)
|
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
|
if not self.demo_mode: # a demo owes a synthetic slot: it would self-close
|
||||||
arm_credit_watch(
|
arm_credit_watch(
|
||||||
self.root, lambda: self._pending, self._reconcile_after_fetch
|
self.root, lambda: self._pending, self._reconcile_after_fetch
|
||||||
|
|||||||
@ -27,6 +27,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from diet_guard import _errors
|
||||||
from diet_guard._estimator import Nutrition
|
from diet_guard._estimator import Nutrition
|
||||||
|
|
||||||
# Importing these fixtures IS their registration: pytest discovers fixtures
|
# Importing these fixtures IS their registration: pytest discovers fixtures
|
||||||
@ -113,6 +114,15 @@ def _isolate_state(tmp_path: Path) -> Iterator[None]:
|
|||||||
stack.enter_context(
|
stack.enter_context(
|
||||||
patch.dict(os.environ, {"HOLDS_DATA": str(tmp_path / "holds.json")})
|
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
|
yield
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
65
diet_guard/tests/test_errors.py
Normal file
65
diet_guard/tests/test_errors.py
Normal 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()
|
||||||
@ -4,12 +4,12 @@ version = "1.0.0"
|
|||||||
description = "Tkinter/systemd log-to-unlock meal gate with calorie/macro budget tracking"
|
description = "Tkinter/systemd log-to-unlock meal gate with calorie/macro budget tracking"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock",
|
"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",
|
"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",
|
"freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays",
|
||||||
"holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds",
|
"holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds",
|
||||||
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays",
|
"sickdays @ git+https://github.com/kuhyx/utils@sickdays-v0.5.5#subdirectory=sickdays",
|
||||||
"claude-agent-sdk==0.2.183",
|
"claude-agent-sdk==0.2.184",
|
||||||
"defusedxml==0.7.1",
|
"defusedxml==0.7.1",
|
||||||
"httpx==0.28.1",
|
"httpx==0.28.1",
|
||||||
"requests==2.34.2",
|
"requests==2.34.2",
|
||||||
@ -23,6 +23,9 @@ dependencies = [
|
|||||||
# to import the MCP SDK.
|
# to import the MCP SDK.
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
mcp = ["mcp==2.3.0"]
|
mcp = ["mcp==2.3.0"]
|
||||||
|
# Test-only: the property tests (test_claude_log.py) need it; same pin as
|
||||||
|
# requirements.txt.
|
||||||
|
dev = ["hypothesis==6.170.15"]
|
||||||
|
|
||||||
# Without this, setuptools' flat-layout auto-discovery treats the Flutter
|
# Without this, setuptools' flat-layout auto-discovery treats the Flutter
|
||||||
# app/ directory as a second top-level package and refuses to build.
|
# app/ directory as a second top-level package and refuses to build.
|
||||||
|
|||||||
@ -1,13 +1,13 @@
|
|||||||
# Diet Guard — runtime + development dependencies
|
# Diet Guard — runtime + development dependencies
|
||||||
# Runtime: tkinter/json/hmac/fcntl (stdlib) plus gatelock and requests below.
|
# Runtime: tkinter/json/hmac/fcntl (stdlib) plus gatelock and requests below.
|
||||||
bandit==1.9.4
|
bandit==1.9.4
|
||||||
claude-agent-sdk==0.2.183
|
claude-agent-sdk==0.2.184
|
||||||
codespell==2.4.3
|
codespell==2.4.3
|
||||||
coverage==7.17.3
|
coverage==7.17.3
|
||||||
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync
|
crdt-sync @ git+https://github.com/kuhyx/utils@crdt-sync-v0.11.1#subdirectory=crdt-sync
|
||||||
defusedxml==0.7.1
|
defusedxml==0.7.1
|
||||||
freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays
|
freedays @ git+https://github.com/kuhyx/utils@freedays-v0.4.3#subdirectory=freedays
|
||||||
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.15.1#subdirectory=gatelock
|
gatelock @ git+https://github.com/kuhyx/utils@gatelock-v0.16.0#subdirectory=gatelock
|
||||||
holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds
|
holds @ git+https://github.com/kuhyx/utils@holds-v0.1.0#subdirectory=holds
|
||||||
httpx==0.28.1
|
httpx==0.28.1
|
||||||
hypothesis==6.170.15
|
hypothesis==6.170.15
|
||||||
|
|||||||
37
scripts/deploy.sh
Executable file
37
scripts/deploy.sh
Executable file
@ -0,0 +1,37 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# ============================================================================
|
||||||
|
# Deploy main everywhere it runs on this PC, then prove it.
|
||||||
|
#
|
||||||
|
# 1. Pull the durable clone the gate timer runs from (editable install).
|
||||||
|
# 2. Reinstall it into system Python's user site: diet-guard-gate.service
|
||||||
|
# runs /usr/bin/python, not a venv (CLAUDE.md, the 2026-06-19 outage).
|
||||||
|
# 3. Rebuild and reinstall the desktop app package (app/install_arch.sh).
|
||||||
|
#
|
||||||
|
# The phone APK ships through CI (release-apk.yml) and phone_deploy.sh.
|
||||||
|
# Idempotent; exits non-zero unless every health check passes.
|
||||||
|
# ============================================================================
|
||||||
|
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
readonly REPO="$HOME/src/diet-guard"
|
||||||
|
readonly PY=/usr/bin/python3
|
||||||
|
|
||||||
|
fail() {
|
||||||
|
echo "deploy: $*" >&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
git -C "$REPO" pull --ff-only
|
||||||
|
"$PY" -m pip install --user --break-system-packages -q -e "$REPO"
|
||||||
|
(cd "$REPO/app" && bash install_arch.sh)
|
||||||
|
|
||||||
|
# Health: the gate's own interpreter imports the code the timer will run...
|
||||||
|
"$PY" -c 'import diet_guard._cli, diet_guard._errors, diet_guard._gatelock' \
|
||||||
|
|| fail "system python cannot import the gate"
|
||||||
|
systemctl --user is-enabled --quiet diet-guard-gate.timer \
|
||||||
|
|| fail "diet-guard-gate.timer is not enabled"
|
||||||
|
# ...and the desktop package is the version this checkout builds.
|
||||||
|
want="$(sed -n 's/^version:[[:space:]]*\([^+]*\).*/\1/p' "$REPO/app/pubspec.yaml")"
|
||||||
|
have="$(pacman -Q diet-guard-app | awk '{print $2}')"
|
||||||
|
[[ "$have" == "$want-1" ]] || fail "desktop app is $have, want $want-1"
|
||||||
|
echo "deploy: ok (gate imports, timer enabled, desktop app $have)"
|
||||||
Loading…
Reference in New Issue
Block a user