Every store writes only the fields its records know
The admin and remote stores round-tripped the raw JSON they read, so any field a past version wrote survived forever. Both now read into typed records (AdminCredential; _RemoteNode holding CachedValue settings) and write out of them, keeping the file's existing spelling. The settings store keeps only keys device_config still registers: it offers remembered values back to the radio, so an unregistered key must not reach a restore. Every persisted record now declares a RETIRED set (all empty: the history holds no removed fields), device_config gains its own RETIRED registry, and test_retired pins each store's records so the sweep cannot go vacuous. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
6a8efa2566
commit
6b13755c2b
11 files changed
+328
-84
No files matched your search
@@ -86,8 +86,9 @@ not in `config.toml`: a code constant a reader can't reach is a preference nobod
|
||||
|
||||
What MeshTerm writes, it writes from what it knows: a store reads its file into typed
|
||||
records and writes back out of *them*, never the raw document it read, so a field no
|
||||
record knows (retired, or invented by a hand edit) is gone at the next write —
|
||||
`preferences.toml` already works this way, `adverts.json` is the store-side model. And
|
||||
record knows (retired, or invented by a hand edit) is gone at the next write — every
|
||||
`*_store.py` and `preferences.toml` work this way, and the settings store keeps only
|
||||
keys `device_config` still registers, since it offers them back to the radio. And
|
||||
**a name that leaves is retired for good**, never reused with another meaning, because a
|
||||
value can outlive its key in a file nobody saved since, and an old `30` that passes a new
|
||||
spec means something else undetectably. A preference key goes in `preferences.RETIRED`
|
||||
|
||||
@@ -7,6 +7,10 @@ password means the user isn't prompted on every run. Like the remembered device
|
||||
directory (``<config_dir>/admin.json``) rather than the per-invocation SQLite database,
|
||||
because admin credentials are global machine state.
|
||||
|
||||
The file is read into :class:`AdminCredential` records and written back out of them,
|
||||
never round-tripped as the raw JSON it was read as, so a field no record knows is gone
|
||||
after the next write (see :data:`AdminCredential.RETIRED`).
|
||||
|
||||
Passwords are stored in plaintext, so the file is written with owner-only permissions
|
||||
where the platform supports it. This is a local operator tool tuning their own mesh, not
|
||||
a multi-user secret store; treat the file accordingly.
|
||||
@@ -17,6 +21,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .models import Contact, LoginResult, utcnow
|
||||
@@ -43,7 +48,8 @@ class AdminCredential:
|
||||
"""A remembered admin password for one remote node.
|
||||
|
||||
Attributes:
|
||||
key: The :func:`admin_key` the password is stored under.
|
||||
key: The :func:`admin_key` the password is stored under (the record's key in the
|
||||
file, not one of its fields).
|
||||
password: The node's admin password (plaintext).
|
||||
label: A friendly node name for display.
|
||||
last_used: ISO-8601 timestamp of the last successful login.
|
||||
@@ -54,6 +60,26 @@ class AdminCredential:
|
||||
label: str
|
||||
last_used: str
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@classmethod
|
||||
def read(cls, key: str, raw: object) -> AdminCredential | None:
|
||||
"""Build a record from one file entry, or ``None`` when it holds no password."""
|
||||
if not isinstance(raw, dict) or raw.get("password") is None:
|
||||
return None
|
||||
return cls(
|
||||
key=key,
|
||||
password=str(raw["password"]),
|
||||
label=str(raw.get("label") or ""),
|
||||
last_used=str(raw.get("last_used") or ""),
|
||||
)
|
||||
|
||||
def to_json(self) -> dict:
|
||||
"""The record as written: its own fields and nothing it happened to be read with."""
|
||||
return {"password": self.password, "label": self.label, "last_used": self.last_used}
|
||||
|
||||
|
||||
class AdminStore:
|
||||
"""Reads and writes remembered remote-node admin passwords."""
|
||||
@@ -66,13 +92,20 @@ class AdminStore:
|
||||
"""
|
||||
self._path = path
|
||||
|
||||
def _load_all(self) -> dict[str, dict]:
|
||||
"""Return the raw key -> record mapping, or empty on missing/corrupt file."""
|
||||
def _load(self) -> dict[str, AdminCredential]:
|
||||
"""Read every credential, keyed by :func:`admin_key`; empty on a missing/corrupt file."""
|
||||
try:
|
||||
data = json.loads(self._path.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
if not isinstance(data, dict):
|
||||
return {}
|
||||
records = {}
|
||||
for key, raw in data.items():
|
||||
record = AdminCredential.read(str(key), raw)
|
||||
if record is not None:
|
||||
records[record.key] = record
|
||||
return records
|
||||
|
||||
def get(self, node: Contact) -> str | None:
|
||||
"""Return the remembered password for ``node``, or ``None`` if not stored.
|
||||
@@ -83,11 +116,8 @@ class AdminStore:
|
||||
Returns:
|
||||
The stored password, or ``None``.
|
||||
"""
|
||||
record = self._load_all().get(admin_key(node))
|
||||
if isinstance(record, dict):
|
||||
password = record.get("password")
|
||||
return str(password) if password is not None else None
|
||||
return None
|
||||
record = self._load().get(admin_key(node))
|
||||
return record.password if record is not None else None
|
||||
|
||||
def remember(self, node: Contact, password: str) -> None:
|
||||
"""Store (or update) the admin password for ``node``.
|
||||
@@ -96,12 +126,11 @@ class AdminStore:
|
||||
node: The contact the password belongs to.
|
||||
password: The admin password to remember.
|
||||
"""
|
||||
records = self._load_all()
|
||||
records[admin_key(node)] = {
|
||||
"password": password,
|
||||
"label": node.name,
|
||||
"last_used": utcnow().isoformat(),
|
||||
}
|
||||
records = self._load()
|
||||
key = admin_key(node)
|
||||
records[key] = AdminCredential(
|
||||
key=key, password=password, label=node.name, last_used=utcnow().isoformat()
|
||||
)
|
||||
self._write(records)
|
||||
|
||||
def record(self, node: Contact, password: str, outcome: LoginResult) -> None:
|
||||
@@ -133,10 +162,11 @@ class AdminStore:
|
||||
Args:
|
||||
node: The contact to forget.
|
||||
"""
|
||||
records = self._load_all()
|
||||
records = self._load()
|
||||
if records.pop(admin_key(node), None) is not None:
|
||||
self._write(records)
|
||||
|
||||
def _write(self, records: dict[str, dict]) -> None:
|
||||
def _write(self, records: dict[str, AdminCredential]) -> None:
|
||||
"""Persist ``records`` atomically with owner-only permissions where supported."""
|
||||
write_atomically(self._path, json.dumps(records, indent=2), owner_only=True)
|
||||
data = {key: record.to_json() for key, record in records.items()}
|
||||
write_atomically(self._path, json.dumps(data, indent=2), owner_only=True)
|
||||
@@ -29,7 +29,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING
|
||||
from typing import TYPE_CHECKING, ClassVar
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .channels import (
|
||||
@@ -52,6 +52,10 @@ class RememberedChannel:
|
||||
name: str
|
||||
secret: bytes
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@property
|
||||
def identity(self) -> str:
|
||||
"""The channel's slot-independent intrinsic identity (see :func:`channel_identity`)."""
|
||||
|
||||
@@ -56,6 +56,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from dataclasses import dataclass, replace
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .models import Contact, advert_time
|
||||
@@ -118,6 +119,10 @@ class RememberedContact:
|
||||
archived_at: int | None = None
|
||||
locked: bool = False
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@property
|
||||
def archived(self) -> bool:
|
||||
"""Whether this contact has been swept off the device but kept here."""
|
||||
|
||||
@@ -19,6 +19,7 @@ import json
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .models import utcnow
|
||||
@@ -66,6 +67,10 @@ class QueuedMessage:
|
||||
status: str = QUEUED
|
||||
finished: datetime | None = None
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
|
||||
class CourierStore:
|
||||
"""Reads and writes the outbox, memory-first (loaded once, persisted on change)."""
|
||||
|
||||
@@ -795,6 +795,18 @@ DEVICE_SETTINGS: list[SettingSpec] = [
|
||||
|
||||
_BY_KEY: dict[str, SettingSpec] = {s.key: s for s in DEVICE_SETTINGS}
|
||||
|
||||
#: Every key that was once a device setting and no longer is — none yet. A retired key is
|
||||
#: never reused, for the reason :data:`meshterm.core.preferences.RETIRED` gives, and with
|
||||
#: more at stake here: the settings store remembers values to *restore onto the radio*, so a
|
||||
#: key that came back meaning something else would offer to write the old value's number
|
||||
#: into the new setting. ``tests/test_retired.py`` fails a registry that takes one back.
|
||||
RETIRED: frozenset[str] = frozenset()
|
||||
|
||||
|
||||
def is_setting_key(key: str) -> bool:
|
||||
"""Whether ``key`` names a device setting in the registry (and not a retired one)."""
|
||||
return key in _BY_KEY
|
||||
|
||||
|
||||
def get_spec(key: str) -> SettingSpec:
|
||||
"""Return the spec for ``key``.
|
||||
|
||||
@@ -28,6 +28,7 @@ from __future__ import annotations
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .discovery import (
|
||||
@@ -75,6 +76,10 @@ class RememberedDevice:
|
||||
host: str = ""
|
||||
tcp_port: int = 0
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@property
|
||||
def is_ble(self) -> bool:
|
||||
"""Whether this remembered device was reached over Bluetooth LE."""
|
||||
|
||||
+125
-54
@@ -8,14 +8,19 @@ here, per node (keyed like the admin passwords, by public key), in a small JSON
|
||||
the config directory (``<config_dir>/remote.json``) alongside each node's remote
|
||||
command-line history — global machine state, like the other JSON stores, deliberately
|
||||
outside the per-invocation SQLite database.
|
||||
|
||||
The file is read into typed records (:class:`_RemoteNode`, each holding its
|
||||
:class:`CachedValue` settings) and written back out of them, never round-tripped as the
|
||||
raw JSON it was read as, so a field no record knows is gone after the next write.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, field, replace
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .admin_store import admin_key
|
||||
from .atomicwrite import write_atomically
|
||||
@@ -46,6 +51,83 @@ class CachedValue:
|
||||
supported: bool = True
|
||||
discovered: bool = False
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@classmethod
|
||||
def read(cls, raw: object) -> CachedValue | None:
|
||||
"""Build a value from one file entry, or ``None`` when it is malformed.
|
||||
|
||||
A supported entry must carry its value; an unsupported one carries none.
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
return None
|
||||
supported = not raw.get("unsupported", False)
|
||||
if supported and "value" not in raw:
|
||||
return None
|
||||
return cls(
|
||||
value=str(raw.get("value", "")),
|
||||
read_at=_as_time(raw.get("read_at")),
|
||||
supported=supported,
|
||||
discovered=bool(raw.get("discovered", False)),
|
||||
)
|
||||
|
||||
def to_json(self) -> dict:
|
||||
"""The entry as written, in the file's long-standing spelling.
|
||||
|
||||
A supported value carries ``value``; an unsupported one says ``unsupported`` and
|
||||
carries no value; ``discovered`` appears only when true.
|
||||
"""
|
||||
entry: dict = {"value": self.value} if self.supported else {"unsupported": True}
|
||||
entry["read_at"] = self.read_at.isoformat() if self.read_at is not None else None
|
||||
if self.discovered:
|
||||
entry["discovered"] = True
|
||||
return entry
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _RemoteNode:
|
||||
"""Everything remembered about one remote node.
|
||||
|
||||
Attributes:
|
||||
settings: Its cached settings, keyed by CLI parameter name.
|
||||
history: Its remote command-line history, oldest first.
|
||||
"""
|
||||
|
||||
settings: dict[str, CachedValue] = field(default_factory=dict)
|
||||
history: list[str] = field(default_factory=list)
|
||||
|
||||
#: Fields this record once held; none yet (see :attr:`CachedValue.RETIRED`).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
@classmethod
|
||||
def read(cls, raw: object) -> _RemoteNode:
|
||||
"""Build a node's record from its file entry, taking only the fields it knows."""
|
||||
record = raw if isinstance(raw, dict) else {}
|
||||
settings: dict[str, CachedValue] = {}
|
||||
raw_settings = record.get("settings")
|
||||
if isinstance(raw_settings, dict):
|
||||
for key, entry in raw_settings.items():
|
||||
value = CachedValue.read(entry)
|
||||
if value is not None:
|
||||
settings[str(key)] = value
|
||||
raw_history = record.get("history")
|
||||
history = [str(c) for c in raw_history] if isinstance(raw_history, list) else []
|
||||
return cls(settings=settings, history=history)
|
||||
|
||||
@property
|
||||
def empty(self) -> bool:
|
||||
"""Whether there is nothing left to remember about this node."""
|
||||
return not self.settings and not self.history
|
||||
|
||||
def to_json(self) -> dict:
|
||||
"""The record as written: its own fields and nothing it happened to be read with."""
|
||||
return {
|
||||
"settings": {key: value.to_json() for key, value in self.settings.items()},
|
||||
"history": list(self.history),
|
||||
}
|
||||
|
||||
|
||||
class RemoteStore:
|
||||
"""Reads and writes per-node remote-admin state (setting cache + CLI history)."""
|
||||
@@ -58,101 +140,90 @@ class RemoteStore:
|
||||
"""
|
||||
self._path = path
|
||||
|
||||
def _load_all(self) -> dict[str, dict]:
|
||||
"""Return the raw key -> record mapping, or empty on missing/corrupt file."""
|
||||
def _load(self) -> dict[str, _RemoteNode]:
|
||||
"""Read every node's record, keyed by :func:`admin_key`; empty on trouble."""
|
||||
try:
|
||||
data = json.loads(self._path.read_text(encoding="utf-8"))
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
return data if isinstance(data, dict) else {}
|
||||
if not isinstance(data, dict):
|
||||
return {}
|
||||
return {str(key): _RemoteNode.read(raw) for key, raw in data.items()}
|
||||
|
||||
def _node(self, node: Contact) -> _RemoteNode:
|
||||
"""One node's record, or an empty one when nothing is remembered."""
|
||||
return self._load().get(admin_key(node)) or _RemoteNode()
|
||||
|
||||
# -- the settings cache -------------------------------------------------------
|
||||
|
||||
def settings(self, node: Contact) -> dict[str, CachedValue]:
|
||||
"""Every remembered setting for ``node``, keyed by CLI parameter name."""
|
||||
record = self._load_all().get(admin_key(node))
|
||||
raw = record.get("settings") if isinstance(record, dict) else None
|
||||
out: dict[str, CachedValue] = {}
|
||||
if not isinstance(raw, dict):
|
||||
return out
|
||||
for key, entry in raw.items():
|
||||
if not isinstance(entry, dict):
|
||||
continue
|
||||
supported = not entry.get("unsupported", False)
|
||||
if supported and "value" not in entry:
|
||||
continue
|
||||
read_at: datetime | None = None
|
||||
stamp = entry.get("read_at")
|
||||
if isinstance(stamp, str):
|
||||
try:
|
||||
read_at = datetime.fromisoformat(stamp)
|
||||
except ValueError:
|
||||
read_at = None
|
||||
out[key] = CachedValue(
|
||||
value=str(entry.get("value", "")),
|
||||
read_at=read_at,
|
||||
supported=supported,
|
||||
discovered=bool(entry.get("discovered", False)),
|
||||
)
|
||||
return out
|
||||
return dict(self._node(node).settings)
|
||||
|
||||
def remember_setting(self, node: Contact, key: str, value: str) -> None:
|
||||
"""Cache one setting's value for ``node``, stamped now."""
|
||||
self._put(node, key, {"value": value, "read_at": utcnow().isoformat()})
|
||||
self._put(node, key, CachedValue(value=value, read_at=utcnow()))
|
||||
|
||||
def remember_discovered(self, node: Contact, key: str, value: str) -> None:
|
||||
"""Cache a setting the catalog hasn't got, which ``node`` has just proved it has."""
|
||||
self._put(node, key, {"value": value, "read_at": utcnow().isoformat(), "discovered": True})
|
||||
self._put(node, key, CachedValue(value=value, read_at=utcnow(), discovered=True))
|
||||
|
||||
def remember_unsupported(self, node: Contact, key: str) -> None:
|
||||
"""Record that ``node`` answered a read of ``key`` with an error, stamped now."""
|
||||
self._put(node, key, {"unsupported": True, "read_at": utcnow().isoformat()})
|
||||
self._put(node, key, CachedValue(value="", read_at=utcnow(), supported=False))
|
||||
|
||||
def forget_setting(self, node: Contact, key: str) -> None:
|
||||
"""Drop one cached setting, so the row reads as never read."""
|
||||
records = self._load_all()
|
||||
settings = records.get(admin_key(node), {}).get("settings")
|
||||
if isinstance(settings, dict) and settings.pop(key, None) is not None:
|
||||
records = self._load()
|
||||
record = records.get(admin_key(node))
|
||||
if record is not None and record.settings.pop(key, None) is not None:
|
||||
self._write(records)
|
||||
|
||||
def _put(self, node: Contact, key: str, entry: dict) -> None:
|
||||
def _put(self, node: Contact, key: str, value: CachedValue) -> None:
|
||||
"""Store one setting's cache entry for ``node``, keeping it discovered if it was.
|
||||
|
||||
A discovered row is refreshed by the same reads and writes as any other — through
|
||||
:meth:`remember_setting` — and that must not quietly demote it to a catalog row it
|
||||
has no entry for, which would leave a row nothing draws.
|
||||
"""
|
||||
records = self._load_all()
|
||||
record = records.setdefault(admin_key(node), {})
|
||||
settings = record.setdefault("settings", {})
|
||||
previous = settings.get(key)
|
||||
if isinstance(previous, dict) and previous.get("discovered"):
|
||||
entry = {**entry, "discovered": True}
|
||||
settings[key] = entry
|
||||
records = self._load()
|
||||
record = records.setdefault(admin_key(node), _RemoteNode())
|
||||
previous = record.settings.get(key)
|
||||
if previous is not None and previous.discovered:
|
||||
value = replace(value, discovered=True)
|
||||
record.settings[key] = value
|
||||
self._write(records)
|
||||
|
||||
# -- the command-line history ---------------------------------------------------
|
||||
|
||||
def history(self, node: Contact) -> list[str]:
|
||||
"""The node's remote CLI history, oldest first."""
|
||||
record = self._load_all().get(admin_key(node))
|
||||
raw = record.get("history") if isinstance(record, dict) else None
|
||||
return [str(c) for c in raw] if isinstance(raw, list) else []
|
||||
return list(self._node(node).history)
|
||||
|
||||
def append_history(self, node: Contact, command: str) -> None:
|
||||
"""Append one sent command to the node's history (dropping an adjacent dupe)."""
|
||||
command = command.strip()
|
||||
if not command:
|
||||
return
|
||||
records = self._load_all()
|
||||
record = records.setdefault(admin_key(node), {})
|
||||
history = record.setdefault("history", [])
|
||||
if history and history[-1] == command:
|
||||
records = self._load()
|
||||
record = records.setdefault(admin_key(node), _RemoteNode())
|
||||
if record.history and record.history[-1] == command:
|
||||
return # re-running the last command shouldn't stutter the recall
|
||||
history.append(command)
|
||||
del history[:-HISTORY_CAP]
|
||||
record.history.append(command)
|
||||
del record.history[:-HISTORY_CAP]
|
||||
self._write(records)
|
||||
|
||||
def _write(self, records: dict[str, dict]) -> None:
|
||||
def _write(self, records: dict[str, _RemoteNode]) -> None:
|
||||
"""Persist ``records`` atomically (crash mid-write keeps the previous file)."""
|
||||
write_atomically(self._path, json.dumps(records, indent=2))
|
||||
data = {key: record.to_json() for key, record in records.items() if not record.empty}
|
||||
write_atomically(self._path, json.dumps(data, indent=2))
|
||||
|
||||
|
||||
def _as_time(value: object) -> datetime | None:
|
||||
"""Parse a stored ISO-8601 timestamp, or ``None`` if absent or corrupt."""
|
||||
if not isinstance(value, str):
|
||||
return None
|
||||
try:
|
||||
return datetime.fromisoformat(value)
|
||||
except ValueError:
|
||||
return None
|
||||
@@ -34,7 +34,13 @@ from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from .atomicwrite import write_atomically
|
||||
from .device_config import DeviceConfigError, format_value, get_spec, parse_value
|
||||
from .device_config import (
|
||||
DeviceConfigError,
|
||||
format_value,
|
||||
get_spec,
|
||||
is_setting_key,
|
||||
parse_value,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .connection import Device
|
||||
@@ -86,9 +92,14 @@ class SettingsStore:
|
||||
for pubkey, entries in (data.get("devices") or {}).items():
|
||||
if not isinstance(entries, dict):
|
||||
continue
|
||||
# Keep only plain scalar values; a container or null is a malformed entry.
|
||||
# Keep only plain scalar values of settings the registry knows: a container or
|
||||
# null is a malformed entry, and a key that is no longer a setting (or a typo)
|
||||
# is not something to offer to write back onto the radio. Whatever is left out
|
||||
# here is gone from the file at its next save.
|
||||
values = {
|
||||
str(key): value for key, value in entries.items() if isinstance(value, _SCALARS)
|
||||
str(key): value
|
||||
for key, value in entries.items()
|
||||
if isinstance(value, _SCALARS) and is_setting_key(str(key))
|
||||
}
|
||||
if values:
|
||||
devices[_norm(str(pubkey))] = values
|
||||
|
||||
@@ -23,6 +23,7 @@ import time
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import ClassVar
|
||||
|
||||
from .models import utcnow
|
||||
|
||||
@@ -73,6 +74,10 @@ class WatchedNode:
|
||||
silent_since: datetime | None = None
|
||||
node_type: int | None = None
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class Alert:
|
||||
@@ -94,6 +99,10 @@ class Alert:
|
||||
message: str
|
||||
acked: bool = False
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class _State:
|
||||
@@ -105,6 +114,10 @@ class _State:
|
||||
new_node_alerts: bool = True
|
||||
next_id: int = 1
|
||||
|
||||
#: Fields this record once held and must never hold again under another meaning (see
|
||||
#: :data:`meshterm.core.preferences.RETIRED` for why a name is never reused).
|
||||
RETIRED: ClassVar[frozenset[str]] = frozenset()
|
||||
|
||||
|
||||
class WatchStore:
|
||||
"""Reads and writes the Watchtower's state, memory-first."""
|
||||
|
||||
+93
-6
@@ -7,7 +7,8 @@ value that happens to pass the new spec would load cleanly and mean something el
|
||||
no validation can catch that. So every name that leaves is recorded as retired, and these
|
||||
tests fail the moment one comes back:
|
||||
|
||||
* the preference registry against :data:`meshterm.core.preferences.RETIRED`;
|
||||
* the preference registry against :data:`meshterm.core.preferences.RETIRED`, and the
|
||||
device-setting registry against :data:`meshterm.core.device_config.RETIRED`;
|
||||
* every persisted record type — a dataclass in :mod:`meshterm.core` declaring its own
|
||||
``RETIRED`` set — against its own fields. The record is the path: a name retired from
|
||||
one record says nothing about another, where it may be exactly the right name.
|
||||
@@ -17,6 +18,7 @@ from __future__ import annotations
|
||||
|
||||
import dataclasses
|
||||
import importlib
|
||||
import json
|
||||
import logging
|
||||
import pkgutil
|
||||
from pathlib import Path
|
||||
@@ -24,7 +26,9 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
import meshterm.core
|
||||
from meshterm.core import preferences
|
||||
from meshterm.core import device_config, preferences
|
||||
from meshterm.core.admin_store import AdminStore
|
||||
from meshterm.core.models import Contact
|
||||
from meshterm.core.preferences import (
|
||||
PREFERENCES,
|
||||
RETIRED,
|
||||
@@ -32,6 +36,8 @@ from meshterm.core.preferences import (
|
||||
Preferences,
|
||||
report_dropped,
|
||||
)
|
||||
from meshterm.core.remote_store import RemoteStore
|
||||
from meshterm.core.settings_store import SettingsStore
|
||||
|
||||
|
||||
def _record_types() -> list[type]:
|
||||
@@ -55,10 +61,30 @@ def test_no_live_preference_takes_a_retired_key() -> None:
|
||||
assert not {spec.key for spec in PREFERENCES} & RETIRED
|
||||
|
||||
|
||||
def test_the_record_walk_finds_the_stores() -> None:
|
||||
"""The sweep below is not vacuous: it sees at least the advert store's records."""
|
||||
names = {cls.__qualname__ for cls in _record_types()}
|
||||
assert {"_Device", "_Document"} <= names
|
||||
def test_no_live_device_setting_takes_a_retired_key() -> None:
|
||||
"""The settings store restores values onto the radio; a reused key would misplace one."""
|
||||
assert not {spec.key for spec in device_config.DEVICE_SETTINGS} & device_config.RETIRED
|
||||
|
||||
|
||||
#: Every persisted record type, by the module that writes it. A store that grows a record
|
||||
#: adds it here, and the sweep below checks it without being told how.
|
||||
_EXPECTED_RECORDS = {
|
||||
"admin_store": {"AdminCredential"},
|
||||
"advert_store": {"_Device", "_Document"},
|
||||
"channel_store": {"RememberedChannel"},
|
||||
"contact_store": {"RememberedContact"},
|
||||
"courier_store": {"QueuedMessage"},
|
||||
"device_store": {"RememberedDevice"},
|
||||
"remote_store": {"CachedValue", "_RemoteNode"},
|
||||
"watch_store": {"WatchedNode", "Alert", "_State"},
|
||||
}
|
||||
|
||||
|
||||
def test_the_record_walk_finds_every_store() -> None:
|
||||
"""The sweep below is not vacuous: it sees each store's records."""
|
||||
found = {(cls.__module__.rsplit(".", 1)[1], cls.__qualname__) for cls in _record_types()}
|
||||
expected = {(module, name) for module, names in _EXPECTED_RECORDS.items() for name in names}
|
||||
assert expected <= found
|
||||
|
||||
|
||||
@pytest.mark.parametrize("record", _record_types(), ids=lambda cls: cls.__qualname__)
|
||||
@@ -113,3 +139,64 @@ def test_drops_are_logged_by_kind(tmp_path: Path, caplog: pytest.LogCaptureFixtu
|
||||
assert any(level == logging.INFO and retired in msg for level, msg in by_level)
|
||||
assert any(level == logging.WARNING and "trace_cooldwn_s" in msg for level, msg in by_level)
|
||||
assert any(level == logging.WARNING and "history_days" in msg for level, msg in by_level)
|
||||
|
||||
|
||||
# -- the stores write only what they know ----------------------------------------------
|
||||
|
||||
_NODE = Contact(name="Hilltop-Repeater", public_key="cd" * 32, key_prefix="cd" * 6)
|
||||
|
||||
|
||||
def test_the_admin_store_writes_only_known_fields(tmp_path: Path) -> None:
|
||||
"""A stray field on a credential, or a stray record, is gone after the next write."""
|
||||
path = tmp_path / "admin.json"
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"cd" * 32: {"password": "pw", "label": "H", "last_used": "", "role": "x"},
|
||||
"junk": {"no": "password"},
|
||||
}
|
||||
)
|
||||
)
|
||||
store = AdminStore(path)
|
||||
assert store.get(_NODE) == "pw"
|
||||
store.remember(Contact(name="Other", public_key="ef" * 32, key_prefix="ef" * 6), "pw2")
|
||||
written = json.loads(path.read_text())
|
||||
assert set(written) == {"cd" * 32, "ef" * 32}
|
||||
assert written["cd" * 32].keys() == {"password", "label", "last_used"}
|
||||
|
||||
|
||||
def test_the_remote_store_writes_only_known_fields(tmp_path: Path) -> None:
|
||||
"""Unknown fields on a node or a cached value do not survive, and the spelling holds."""
|
||||
path = tmp_path / "remote.json"
|
||||
stamp = "2026-09-01T12:00:00+00:00"
|
||||
path.write_text(
|
||||
json.dumps(
|
||||
{
|
||||
"cd" * 32: {
|
||||
"settings": {
|
||||
"txdelay": {"value": "0.5", "read_at": stamp, "unit": "s"},
|
||||
"bridge.delay": {"unsupported": True, "read_at": stamp},
|
||||
},
|
||||
"history": ["ver"],
|
||||
"notes": "stray",
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
store = RemoteStore(path)
|
||||
store.append_history(_NODE, "clock")
|
||||
record = json.loads(path.read_text())["cd" * 32]
|
||||
assert record.keys() == {"settings", "history"}
|
||||
assert record["settings"]["txdelay"] == {"value": "0.5", "read_at": stamp}
|
||||
assert record["settings"]["bridge.delay"] == {"unsupported": True, "read_at": stamp}
|
||||
assert record["history"] == ["ver", "clock"]
|
||||
|
||||
|
||||
def test_the_settings_store_keeps_only_registered_settings(tmp_path: Path) -> None:
|
||||
"""A key that is not a device setting is never offered for restore, and leaves the file."""
|
||||
path = tmp_path / "settings.json"
|
||||
path.write_text(json.dumps({"devices": {"ab" * 32: {"name": "Node", "no_such_key": 3}}}))
|
||||
store = SettingsStore(path)
|
||||
assert store.settings("ab" * 32) == {"name": "Node"}
|
||||
store.remember("ab" * 32, "tx_power", 14)
|
||||
assert json.loads(path.read_text())["devices"]["ab" * 32] == {"name": "Node", "tx_power": 14}
|
||||
Reference in new issue
Block a user