mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-10 21:27:14 +01:00
Groundwork for moving diet-guard, screen-locker and wake-alarm off fixed
role ids ('pc'/'phone'/'desktop') onto per-install uuids, the scheme todo
and home_inventory already use. A role constant collides the moment a
second machine takes the same role, and a reinstall silently inherits the
previous install's CRDT identity.
The migration cannot rewrite history: the device id is baked into every HLC
stamp already written, is the record path segment and the revision key. So
a device that has just switched ids must still recognise its former id as
its own -- otherwise skip-own-writes stops matching the old path, and the
device pulls back and re-merges its own pre-migration log as though a peer
had written it. Idempotent under LWW, but pure wasted transfer on every
tick.
Both halves therefore take an optional legacy id and test set membership
(own_ids / ownIds) rather than equality against a single id:
- crdt-sync (Python) 0.5.1 -> 0.6.0: new crdt_sync._device with
DeviceIdentity + load_device_identity (generate-or-load, persisted); the
mkdir is deliberately unguarded, since handing back an unpersisted id
would mint a fresh uuid every restart and strand a device dir each time.
sync_log gains legacy_device_id.
- crdt_sync_dart 0.6.0 -> 0.7.0: DeviceIdentity value type; syncLog gains
legacyDeviceId. Persistence stays app-side, where the existing uuid apps
already keep it in SharedPreferences under 'crdt.nodeId'.
Both changes are backward compatible: the parameter defaults to null, which
reproduces the previous single-id behaviour exactly.
Each half has a positive test and a negative control proving the old path
*is* pulled when no legacy id is declared -- so the skip is verified to be
the parameter's doing, not an artifact of the fixture.
Python: 239 tests, 100% branch coverage. Dart: 248 tests, analyzer clean,
device.dart 11/11 and sync.dart 57/57 lines covered.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GZH89WEqAjuJT8WZdqc7AP
88 lines
3.5 KiB
Python
88 lines
3.5 KiB
Python
"""This device's stable sync identity.
|
|
|
|
Every device that pushes into a namespace needs an id that is *its own*: it
|
|
is the directory segment its log is pushed under
|
|
(``<path_prefix>/<device_id>/<filename>``), the revision key, and the node
|
|
component baked into every HLC stamp it writes (``<iso>-<counter>-<id>``).
|
|
|
|
Two devices sharing an id overwrite each other's pushed file on every tick,
|
|
so the id must be per-*install*, not per-role: a fixed ``"pc"``/``"phone"``
|
|
constant silently collides the moment a second machine takes the same role,
|
|
and a reinstall inherits the previous install's CRDT identity.
|
|
|
|
A migration from a role constant to a persisted uuid cannot rewrite history.
|
|
Stamps already written keep the old id, and the old path keeps the log that
|
|
was pushed under it -- so a device that has just switched ids must still
|
|
recognise its *former* id as itself. That is what [legacy_id] carries, and
|
|
why [DeviceIdentity.own_ids] exists: skip-own-writes has to test membership
|
|
of that set rather than equality with one id, or the device re-downloads and
|
|
re-merges its own pre-migration history as though a peer had written it.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import TYPE_CHECKING
|
|
import uuid
|
|
|
|
if TYPE_CHECKING:
|
|
from pathlib import Path
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class DeviceIdentity:
|
|
"""This device's current sync id, plus the id it used to push under.
|
|
|
|
Attributes:
|
|
device_id: The id to push, stamp and key revisions under from now on.
|
|
legacy_id: The id this device pushed under before the migration to
|
|
persisted uuids, or ``None`` once its old path has been reclaimed.
|
|
"""
|
|
|
|
device_id: str
|
|
legacy_id: str | None = None
|
|
|
|
@property
|
|
def own_ids(self) -> frozenset[str]:
|
|
"""Every id that means "this device", for skip-own-writes checks."""
|
|
if self.legacy_id is None:
|
|
return frozenset({self.device_id})
|
|
return frozenset({self.device_id, self.legacy_id})
|
|
|
|
def is_own(self, other_device_id: str) -> bool:
|
|
"""Return whether [other_device_id] is one of this device's own ids."""
|
|
return other_device_id in self.own_ids
|
|
|
|
|
|
def load_device_identity(path: Path, *, legacy_id: str | None = None) -> DeviceIdentity:
|
|
"""Return the id persisted at [path], creating it on first call.
|
|
|
|
The file holds exactly one uuid4 and is written once; every later call
|
|
returns the same value, which is what makes the id stable across restarts
|
|
(and is why it must live in durable state, not a temp dir).
|
|
|
|
Args:
|
|
path: File holding this device's uuid. Parent dirs are created.
|
|
legacy_id: The role constant this device pushed under before
|
|
migrating, recorded on the returned identity so skip-own-writes
|
|
still recognises the old path as this device's own. Pass ``None``
|
|
once that path has been reclaimed.
|
|
|
|
Returns:
|
|
This device's [DeviceIdentity].
|
|
"""
|
|
try:
|
|
existing = path.read_text(encoding="utf-8").strip()
|
|
except FileNotFoundError:
|
|
existing = ""
|
|
if existing:
|
|
return DeviceIdentity(device_id=existing, legacy_id=legacy_id)
|
|
|
|
# Deliberately not guarded: if the state directory cannot be created the
|
|
# id cannot be persisted, and returning an unpersisted one would mint a
|
|
# fresh uuid on every restart, stranding a device directory each time.
|
|
minted = str(uuid.uuid4())
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_text(f"{minted}\n", encoding="utf-8")
|
|
return DeviceIdentity(device_id=minted, legacy_id=legacy_id)
|