mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-19 21:34:30 +01:00
Some checks are pending
dependency freshness / dependencies on newest stable (push) Waiting to run
file length / file-length (push) Waiting to run
md-naming / markdown naming convention (push) Waiting to run
palette drift / palette (push) Waiting to run
repo-contract / repo contract (push) Waiting to run
shared gate tests / gate unit tests (push) Waiting to run
sickdays pre-commit / pre-commit (push) Waiting to run
sickdays tests / test (push) Waiting to run
A sick day may keep some lockers active: a locker stands down iff the day is sick and the locker is not in that day's keep set. New record field `keep` = [sorted, de-duplicated locker ids, hlc]. - add/remove write state, actor and keep on one tick (keep=[] on a clear), so a stale keep cannot survive a clear + re-add. - set_kept_lockers re-stamps only keep and actor; state keeps its clock, so a keep edit never re-asserts sick over a concurrent clear. Ticks are issued after every clock already on the record (clock-skew safe). - Reading: no keep (0.1.0) or keep older than state -> keep nothing; malformed keep -> every locker kept (fail closed, logged); unknown ids ignored. - API: stands_down, kept_lockers, set_kept_lockers, add_sick_day(keep=), SickDay.keep, LOCKER_* / LOCKERS. is_sick_day unchanged. - CLI: add --keep, keep DAY [LOCKER ...], list shows keep, check --locker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PAhbNszs5JKdtnD4uBi3xp
72 lines
2.4 KiB
Python
72 lines
2.4 KiB
Python
# Copyright (c) 2026 Krzysztof Rudnicki
|
|
"""The two ways a write reaches the pool: a whole record, or one field update.
|
|
|
|
A whole-record write (add, remove, import) stamps every field with one tick.
|
|
A keep edit must not: it re-stamps only ``keep`` and ``actor``, leaving
|
|
``state`` on its own clock (:func:`sickdays._keep.with_keep`).
|
|
|
|
Every tick is issued *after* both this node's last tick and every clock
|
|
already on the day's record, so a write always beats what it was looking at,
|
|
even when another device's wall clock runs ahead of this one's. Without that,
|
|
a keep edit made under clock skew could be born older than ``state`` -- and a
|
|
keep older than ``state`` reads as stale.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from crdt_sync import Hlc
|
|
|
|
from sickdays._day import to_iso
|
|
from sickdays._errors import PastDateError
|
|
from sickdays._keep import with_keep
|
|
from sickdays._model import build_record, latest_hlc, record_hlc
|
|
from sickdays._store import write_pool
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import date
|
|
|
|
from crdt_sync import Log, Record
|
|
|
|
from sickdays._model import SickDay
|
|
from sickdays._paths import Paths
|
|
|
|
|
|
def refuse_past(day: date, reference: date, verb: str) -> None:
|
|
"""Raise :class:`PastDateError` when ``day`` is before ``reference``."""
|
|
if day < reference:
|
|
msg = (
|
|
f"{to_iso(day)} has already passed -- sick days can only be "
|
|
f"{verb} for today or later"
|
|
)
|
|
raise PastDateError(msg)
|
|
|
|
|
|
def next_tick(log: Log, actor: str, existing: Record | None) -> Hlc:
|
|
"""This node's next clock, later than its own and ``existing``'s ticks."""
|
|
seen = [t for t in (latest_hlc(log, actor), record_hlc(existing)) if t is not None]
|
|
return Hlc.new_tick(actor, max(seen) if seen else None)
|
|
|
|
|
|
def write_entry(log: Log, entry: SickDay, resolved: Paths) -> None:
|
|
"""Replace the day's whole record with ``entry`` and persist the pool."""
|
|
key = to_iso(entry.day)
|
|
tick = next_tick(log, entry.actor, log.get(key))
|
|
log[key] = build_record(entry, tick)
|
|
write_pool(resolved.log, log)
|
|
|
|
|
|
def write_keep(
|
|
log: Log,
|
|
existing: Record,
|
|
keep: frozenset[str],
|
|
*,
|
|
actor: str,
|
|
resolved: Paths,
|
|
) -> None:
|
|
"""Re-stamp only ``keep`` and ``actor`` on ``existing`` and persist."""
|
|
tick = next_tick(log, actor, existing)
|
|
log[existing.id] = with_keep(existing, keep, actor=actor, tick=tick)
|
|
write_pool(resolved.log, log)
|