mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 22:07:15 +01:00
Extracted and generalized from diet_guard's existing tombstone-merge scheme and GitHub Contents API client, extended with per-field HLC LWW so the same library can also cover todo's mutable notes and wake_alarm's single-value sync (Workstream D scoping decision, 2026-07-04). 100% branch coverage, clean ruff/mypy/pylint/bandit. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017McuA1nAi6jBT1EaS5BrVf
33 lines
1.0 KiB
Python
33 lines
1.0 KiB
Python
"""Log: a collection of Records keyed by id, with a union-merge."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from crdt_sync._record import Record, merge_record
|
|
|
|
Log = dict[str, Record]
|
|
|
|
|
|
def merge_logs(local: Log, remote: Log) -> Log:
|
|
"""Return the union of ``local`` and ``remote``, merging shared ids.
|
|
|
|
Commutative and idempotent: ``merge_logs(a, b) == merge_logs(b, a)`` and
|
|
``merge_logs(x, x) == x``, so pull order between devices never matters
|
|
and a repeated sync tick is a no-op.
|
|
|
|
Args:
|
|
local: This device's current full log (including tombstones).
|
|
remote: Another device's last-pushed full log.
|
|
|
|
Returns:
|
|
The merged log, containing every id present in either input.
|
|
"""
|
|
merged: Log = dict(local)
|
|
for record_id, remote_record in remote.items():
|
|
local_record = merged.get(record_id)
|
|
merged[record_id] = (
|
|
remote_record
|
|
if local_record is None
|
|
else merge_record(local_record, remote_record)
|
|
)
|
|
return merged
|