diet-guard/diet_guard/tests/test_sync_banks_mirror.py
Krzysztof kuhy Rudnicki 6258b35f88
Some checks are pending
file-length / file length <= 250 lines (push) Waiting to run
pre-commit / pre-commit (push) Waiting to run
Tests / test (3.10) (push) Waiting to run
Tests / test (3.11) (push) Waiting to run
Tests / test (3.12) (push) Waiting to run
release-apk / release (APK) (push) Waiting to run
perf(sync): hoist the device listing and stop the curated-bank 404 storm
Two independent costs in the background tick, both pure waste.

`list_directory` was called four times per tick -- once by the log pull and
once by each of the three bank syncs. It is the single most expensive call
in a tick at ~445ms mirrored (the GitHub half alone is ~360ms), so that was
~1.3s spent asking the same question four times. It is now listed once in
`run_sync` and passed down.

The curated-bank loop was worse: 17s and 62 requests to fetch *zero* useful
bytes. Firebase returns None for every peer's `food_bank_manual.json`, so
`MirrorSyncClient.get_file_text` treated each miss as "this device has not
migrated yet" and fell through to GitHub, which answers `{}` after ~320ms.
`_primary_only_text` skips that per-peer fallthrough for this one artifact.

Checked before narrowing rather than assumed, because CLAUDE.md requires
both halves of the food bank to sync: reading every peer's curated bank
straight off the mirror found 23 absent, 2 empty, and zero non-empty. No
curated food exists on the mirror alone. The probe is written into the
docstring so it can be re-run before anyone widens this again.

Measured end to end: manual-bank loop 17.0s -> 3.4s, full tick ~27s -> 15.2s,
log/bank/peer state unchanged (441 entries, 117 foods, 22 peers).

`_pull_remote_logs` now returns the revisions it saw instead of mutating a
caller-supplied dict, which keeps it under the argument limit and makes the
data flow visible at the call site.

Deliberately NOT done: revision-skipping the derived food bank. `run_sync`
rebuilds that bank from the merged log immediately before syncing it, so
unlike the log the peer records are not durably held locally -- skipping a
peer would drop its counts from the pushed bank and oscillate under
max-count-wins. Verified empirically: the on-disk bank and a pure log-derived
rebuild are identical (117 foods, 0 differing counts), and the 2 foods peers
know that the local bank lacks are discarded by the existing `if name in
local` filter anyway. The loop is off every interactive path now, so the
remaining cost is background-only.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJ5YnA3CERitQ57HjMu2Qz
2026-08-21 13:16:08 +02:00

36 lines
1.2 KiB
Python

"""Tests for the curated bank's primary-only read.
Split out of ``test_sync_banks.py`` to keep both files under the repo's
250-line cap.
"""
from __future__ import annotations
from unittest.mock import MagicMock
from crdt_sync import MirrorSyncClient
from diet_guard import _sync_banks
class TestPrimaryOnlyText:
"""Reading the curated bank without the per-peer mirror fallthrough."""
def test_a_mirrored_client_reads_only_the_primary(self) -> None:
"""The 17s/tick the fallthrough cost was spent fetching ``{}``."""
primary = MagicMock()
primary.get_file_text.return_value = None
mirror = MagicMock()
client = MirrorSyncClient(primary, mirror)
assert _sync_banks._primary_only_text(client, "some/path") is None
mirror.get_file_text.assert_not_called()
def test_an_unmirrored_client_is_read_directly(self) -> None:
"""GitHub-only devices have no primary/mirror split to skip."""
client = MagicMock()
client.get_file_text.return_value = "{}"
assert _sync_banks._primary_only_text(client, "some/path") == "{}"
client.get_file_text.assert_called_once_with("some/path")