From a41f3c6847d0673eefa36b922d0ade8763837ae6 Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Fri, 27 Nov 2026 16:50:23 +0100 Subject: [PATCH] Offline-first rule, crdt contract test, audit Every app that syncs or shares data is offline-first, whatever the transport (~/.claude/rules/offline-first.instructions.md). crdt_sync_dart documents the contract and test/offline_first_test.dart checks it: a write is readable at once and after restart, an offline tick keeps it, it is pushed once the remote is back, and pulled peer data stays readable. DOCS-offline-first.md audits every syncing app with evidence and names the fix per failing row. Bug-class-check: crdt_sync_dart/test/offline_first_test.dart Co-Authored-By: Claude Opus 5.5 --- CLAUDE.md | 4 + DOCS-offline-first.md | 40 ++++ crdt_sync_dart/README.md | 10 + crdt_sync_dart/test/offline_first_test.dart | 199 ++++++++++++++++++++ 4 files changed, 253 insertions(+) create mode 100644 DOCS-offline-first.md create mode 100644 crdt_sync_dart/test/offline_first_test.dart diff --git a/CLAUDE.md b/CLAUDE.md index 02edd75..ef2c31d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -21,3 +21,7 @@ overwrite it with `repo_contract/templates/check_repo_contract.shim.sh`. - coverage: `PYTHONPATH=. python -m pytest -q agent_ready/tests devstats/tests flutter_web_drive/tests repo_contract/tests unified-design-system/tests --cov=agent_ready --cov=devstats --cov=flutter_web_drive --cov=repo_contract --cov=conformance --cov=conformance_diff --cov-branch --cov-report=xml --cov-fail-under=100` - coverage-gaps: `coverage-gaps coverage.xml` - deploy: `git -C ~/src/utils pull --ff-only && ~/src/utils/scripts/deploy.sh` (installs the sickdays CLI + launcher from the live checkout, then exits 1 unless every released library's version tag is on origin and matches HEAD and the live `sickdays` runs the text-free checkout; `--verify-only` for the health check alone) + +## Rules + +- Offline-first: every app that syncs or shares data, whatever the transport, saves each write locally first (readable at once) and keeps all pulled data, details included (`~/.claude/rules/offline-first.instructions.md`; audit `DOCS-offline-first.md`). The `crdt_sync_dart` contract is in its README ("Offline-first contract"), check `cd crdt_sync_dart && dart test test/offline_first_test.dart`. diff --git a/DOCS-offline-first.md b/DOCS-offline-first.md new file mode 100644 index 0000000..feb952d --- /dev/null +++ b/DOCS-offline-first.md @@ -0,0 +1,40 @@ +# Offline-first audit of kuhy's syncing apps + +Rule: `~/.claude/rules/offline-first.instructions.md` (kuhy, 2026-11-27). Every app that syncs or shares data +is offline-first: (a) a create/save lands locally first and is readable at once; (b) sync runs by itself and a +network failure keeps the write and retries; (c) everything pulled, details included, is kept locally and shown +offline and after a restart. Apps on `crdt_sync_dart` follow its README "Offline-first contract" +(`crdt_sync_dart/test/offline_first_test.dart`). + +Audit run 2026-11-27 by todoImplementer card c5ee9e59-9 (read-only sweep of `~/src/*` manifests, evidence as +`file:line` under each app root, `lib/` left out for Dart apps). A FAIL row is fixed by its own commit; the +"Fix" column names it. A new syncing app adds its row here when it ships. + +| App | Sync | (a) | (b) | (c) | Verdict | Evidence | Fix | +|---|---|---|---|---|---|---|---| +| ~/src/todo | crdt_sync LogStore + Firebase/GitHub mirror | PASS | PASS | PASS | PASS | `data/note_repository.dart:59-60`, launch/pause/focus sync `ui/capture_screen.dart:118,180,187`, importLog before push `sync/sync_service.dart:171` | model app | +| ~/src/todoImplementer/app | rtdb_relay board relay | FAIL | FAIL | FAIL | FAIL | new task existed only after a relay send `ui/board/new_task_sheet.dart:121-133`; board and task details never kept `sync/rtdb_board_relay.dart:106-118`, `ui/board/detail_page.dart:88-99` | T2603 board + details kept, prefetched per sync; T2604 outbox (kuhyx/todoImplementer) | +| ~/src/home_inventory | crdt_sync LogStore + mirror | PASS | FAIL | PASS | FAIL | only the "Sync now" button syncs `screens/github_mirror_screen.dart:168` | pending | +| ~/src/lyricanki | crdt_sync LogStore + openSync | PASS | FAIL | PASS | FAIL | sync only from `onSync` `screens/song_search_screen.dart:219` | pending | +| ~/src/restaurant-rater | crdt_sync LogStore + openSync | PASS | FAIL | PASS | FAIL | no sync on launch/resume, a failed push waits for the next edit `sync/sync_bootstrap.dart:105`, `app.dart:17-26` | pending | +| ~/src/leetcode-guard/app | crdt_sync snapshot + coach relay | FAIL | FAIL | FAIL | FAIL | coach requests memory-only `coach/controller_send.dart:25-37`; chat memory-only `coach/controller.dart:216-222` | pending | +| ~/src/tutor/app | rtdb_relay | FAIL | FAIL | FAIL | FAIL | archive is a direct PUT `history/history.dart:122-125`; history and transcripts uncached `:80-117`, `ui/transcript_screen.dart:52` | pending | +| ~/src/screen-locker/stronglift_replacement/workout_app | crdt_sync syncLog + run queue | PASS | FAIL | FAIL | FAIL | failed workout push only logged `screens/workout_screen_finish.dart:125-130`; merge thrown away `services/workout_sync_session.dart:111` | pending | +| ~/src/dufs-cloud/app | WebDAV | unverified | unverified | FAIL | FAIL | listings uncached `services/dufs_client.dart:65` | pending | +| ~/src/signal-bot/app | HTTP management API | FAIL | FAIL | FAIL | FAIL | live reads, direct edits `api.dart:63-71,95` | pending | +| ~/src/logo-review/app | HTTP review server | unverified | unverified | FAIL | FAIL | pictures session-only `ui/service_badge.dart:12`; feedback POST `api.dart:74` | pending | +| ~/src/kuhytrack | own HTTP server + watchers | PASS | PASS | FAIL | N/A: archived | readers fetch live `web/dashboard.html:119`, `cli/ktq.py:58`; repo archived on GitHub 2026-11-02 | fix kept unpushed on local branch `todo/c5ee9e59-9-kuhytrack` (30ae4a8) | +| ~/src/punchme | crdt_sync LogStore + openSync | PASS | PASS | PASS | PASS | store loads `sync/sync_bootstrap.dart:66` before openSync `:73`, push per write `:90` | none | +| ~/src/diet-guard/app | crdt_sync + RTDB relay | PASS | PASS | PASS | PASS | `services/log_storage_service.dart:108`, WorkManager backstop `services/background_tasks_io.dart:21,36` | none | +| ~/src/wake-alarm/phone_app | crdt_sync LogStore + native sync | PASS | PASS | PASS | PASS | `services/alarm_policy.dart:117-118`, `services/alarm_sync_service.dart:102-103` | none | +| ~/src/daily-limits/app | crdt_sync RTDB + request queue | PASS | PASS | PASS | PASS | `services/limits_controller.dart:102-130`, `services/sync_once.dart:68-97` | none | +| ~/src/home-guard/app | crdt_sync RTDB + upload queue | PASS | PASS | PASS | PASS | `screens/home_screen.dart:168-169`, zones cached `services/zone_sync.dart:73-87` (remote read before cache, no timeout `:105-108`) | none | +| ~/src/phone-focus-mode/focus-owner | crdt_sync push + native queue | PASS | PASS | PASS | PASS | `android/.../UninstallEvents.kt:25,112-115` | none | +| ~/src/kartka | own RTDB engine | PASS | PASS | PASS | PASS | `sync/decorators/synced_library.dart:35-42`, `sync/sync_engine.dart:127-129` | none | +| ~/src/sufler | crdt_sync + RTDB relay | PASS | PASS | PASS | PASS | `assist/ask.dart:19-20,85,99` | none | +| ~/src/book-guard/app | WebDAV outbox | PASS | PASS | PASS | PASS | `src/guard_api.dart:95,101-105,128-133` | none | +| PC sides (Python): diet-guard, wake-alarm, screen-locker, leetcode-guard, daily-limits, build-your-x, home-guard | crdt-sync / RTDB | PASS | PASS | PASS | PASS | e.g. `diet_guard/_sync.py:181,242`, `wake_alarm/_sync_transport.py:5-18`; build-your-x trigger and home-guard zone merge unverified | none | +| Relay workers: todoImplementer, tutor, sufler | rtdb_relay (PC) | ? | ? | ? | pending | not read yet | pending | + +Not syncing (dropped): kuhylog, tunedeck, habit_stack (no sync wiring yet: it must meet this rule when it gets +one), umbrella, game-research, pacman-yay-gui, WUT_Computer_Science, dufs-cloud/firebase_backup. diff --git a/crdt_sync_dart/README.md b/crdt_sync_dart/README.md index c356fe3..4273051 100644 --- a/crdt_sync_dart/README.md +++ b/crdt_sync_dart/README.md @@ -173,6 +173,16 @@ the core stays web-safe. Querying is deliberately **not** in the library — a The Python side mirrors the on-disk format via `crdt_sync.dump_log`/`read_log` (load/save only; no reactive stream — that's a Dart/Flutter convenience). +## Offline-first contract + +Apps on this library are offline-first: write user data to `LogStore` first +and show it at once, never block the UI on the network. Run `syncLog` ticks +in the background and treat a `RemoteSyncError` as "try later", never as a +lost write. After every tick, `replaceAll` the merged result so pulled peer +data is cached locally and stays readable offline. Load the store before +`openSync` (crdt_sync_flutter): it may return null or hit the network, and +never gates local data. `test/offline_first_test.dart` checks this contract. + ## Development ```bash diff --git a/crdt_sync_dart/test/offline_first_test.dart b/crdt_sync_dart/test/offline_first_test.dart new file mode 100644 index 0000000..6276ec8 --- /dev/null +++ b/crdt_sync_dart/test/offline_first_test.dart @@ -0,0 +1,199 @@ +// Offline-first contract: writes are stored locally first and readable at +// once; an unreachable remote makes syncLog throw without losing the write; +// pulled peer data is cached locally for offline reads. + +import 'dart:convert'; + +import 'package:crdt_sync/crdt_sync.dart'; +import 'package:test/test.dart'; + +class _MemPersistence implements LogPersistence { + String? text; + + @override + Future read() async => text; + + @override + Future write(String t) async => text = t; +} + +/// A remote that is unreachable: every call throws RemoteSyncError. +class _OfflineRemote implements RemoteStore { + @override + Future> listDirectory(String path) async => + throw RemoteSyncError('offline'); + + @override + Future getFileText(String path) async => + throw RemoteSyncError('offline'); + + @override + Future putFileText( + String path, + String text, { + required String message, + }) async => throw RemoteSyncError('offline'); + + @override + Future deleteFile(String path, {String? message}) async => + throw RemoteSyncError('offline'); + + @override + Future canAccessRemote() async => throw RemoteSyncError('offline'); + + @override + void close() {} +} + +/// An in-memory reachable remote: a `Map` of files; +/// listDirectory returns the distinct next path segment of every key under +/// [pathPrefix]. +class _MemRemote implements RemoteStore { + final Map files = {}; + + @override + Future> listDirectory(String path) async { + final prefix = '$path/'; + return files.keys + .where((key) => key.startsWith(prefix)) + .map((key) => key.substring(prefix.length).split('/').first) + .toSet() + .toList(); + } + + @override + Future getFileText(String path) async => files[path]; + + @override + Future putFileText( + String path, + String text, { + required String message, + }) async => files[path] = text; + + @override + Future deleteFile(String path, {String? message}) async { + files.remove(path); + } + + @override + Future canAccessRemote() async => true; + + @override + void close() {} +} + +String _encode(Log log) => + jsonEncode(log.map((id, r) => MapEntry(id, r.toJson()))); + +Log _decode(String text) => (jsonDecode(text) as Map).map( + (id, d) => MapEntry(id, Record.fromJson(d as Map)), +); + +void main() { + group('offline-first contract', () { + test( + 'a write is readable at once and survives a restart with no network', + () async { + final persistence = _MemPersistence(); + final store = LogStore(persistence: persistence, nodeId: 'phone'); + await store.load(); + await store.upsert( + Record(id: 'a', fields: {'title': ('milk', store.nextHlc())}), + ); + + // Readable at once, no remote involved; a fresh store on the same + // persistence sees it after load. + expect(store.get('a'), isNotNull); + final restarted = LogStore(persistence: persistence, nodeId: 'phone'); + await restarted.load(); + expect(restarted.get('a'), isNotNull); + }, + ); + + test('a failed tick leaves the local log intact', () async { + final persistence = _MemPersistence(); + final store = LogStore(persistence: persistence, nodeId: 'phone'); + await store.load(); + await store.upsert( + Record(id: 'a', fields: {'title': ('milk', store.nextHlc())}), + ); + + await expectLater( + syncLog( + client: _OfflineRemote(), + deviceId: 'phone', + pathPrefix: 'app-sync/devices', + localLog: store.snapshot(), + encode: _encode, + decode: _decode, + ), + throwsA(isA()), + ); + + expect(store.get('a'), isNotNull); + expect(persistence.text, contains('"a"')); + }); + + test('the offline write is pushed once the remote is back', () async { + final persistence = _MemPersistence(); + final store = LogStore(persistence: persistence, nodeId: 'phone'); + await store.load(); + await store.upsert( + Record(id: 'a', fields: {'title': ('milk', store.nextHlc())}), + ); + // Offline tick fails; once the remote is back it pushes the write. + await expectLater( + syncLog( + client: _OfflineRemote(), + deviceId: 'phone', + pathPrefix: 'app-sync/devices', + localLog: store.snapshot(), + encode: _encode, + decode: _decode, + ), + throwsA(isA()), + ); + final remote = _MemRemote(); + await syncLog( + client: remote, + deviceId: 'phone', + pathPrefix: 'app-sync/devices', + localLog: store.snapshot(), + encode: _encode, + decode: _decode, + ); + + final pushed = remote.files['app-sync/devices/phone/log.json']; + expect(pushed, isNotNull); + expect(_decode(pushed!).containsKey('a'), isTrue); + }); + + test('pulled records are stored locally, readable offline', () async { + final persistence = _MemPersistence(); + final store = LogStore(persistence: persistence, nodeId: 'phone'); + await store.load(); + + final peerStore = LogStore(persistence: _MemPersistence(), nodeId: 'pc'); + await peerStore.load(); + await peerStore.upsert( + Record(id: 'b', fields: {'title': ('bread', peerStore.nextHlc())}), + ); + + final remote = _MemRemote() + ..files['app-sync/devices/pc/log.json'] = _encode(peerStore.snapshot()); + final merged = await syncLog( + client: remote, + deviceId: 'phone', + pathPrefix: 'app-sync/devices', + localLog: store.snapshot(), + encode: _encode, + decode: _decode, + ); + await store.replaceAll(merged); + final restarted = LogStore(persistence: persistence, nodeId: 'phone'); + await restarted.load(); + expect(restarted.get('b'), isNotNull); + }); + }); +}