refactor(utils): compact atomic-write helpers, hermes_time, registration_lifecycle, setup.py guard (-27% LOC, zero behavior change)

This commit is contained in:
Teknium
2026-09-02 18:09:02 -07:00
parent 113f04616b
commit 1dcf189041
4 changed files with 205 additions and 494 deletions

View File

@@ -1,8 +1,7 @@
"""Ownership leases for replaceable runtime registrations.
The coordinator models registration *generations*, not just value identity.
That distinction matters when the same provider singleton is registered again
after an older ownership generation was unloaded.
The coordinator models registration *generations*, not just value identity: the same provider
singleton may be registered again after an older ownership generation was unloaded.
"""
from __future__ import annotations
@@ -15,11 +14,9 @@ from typing import Any
def same_registration(left: Any, right: Any) -> bool:
"""Compare opaque registry snapshots using identity only."""
"""Compare opaque registry snapshots using identity only (element-wise for tuples)."""
if isinstance(left, tuple) and isinstance(right, tuple):
return len(left) == len(right) and all(
same_registration(a, b) for a, b in zip(left, right)
)
return len(left) == len(right) and all(same_registration(a, b) for a, b in zip(left, right))
return left is right
@@ -54,34 +51,14 @@ class ReplacementCoordinator:
yield
def acquire(
self,
slot: Hashable,
*,
current: Any,
previous: Any,
restore: Callable[[Any], bool],
self, slot: Hashable, *, current: Any, previous: Any, restore: Callable[[Any], bool],
finalize: Callable[[], None] | None = None,
) -> ReplacementLease:
"""Attach a new live generation to the matching active predecessor."""
with self._lock:
leases = self._active.setdefault(slot, [])
predecessor = next(
(
lease
for lease in reversed(leases)
if lease.active and same_registration(lease.current, previous)
),
None,
)
lease = ReplacementLease(
coordinator=self,
slot=slot,
current=current,
previous=previous,
restore=restore,
finalize=finalize,
predecessor=predecessor,
)
predecessor = next((c for c in reversed(leases) if c.active and same_registration(c.current, previous)), None)
lease = ReplacementLease(self, slot, current, previous, restore, finalize, predecessor)
leases.append(lease)
return lease
@@ -91,15 +68,10 @@ class ReplacementCoordinator:
if not lease.active:
return
leases = self._active.get(lease.slot, [])
latest = next(
(candidate for candidate in reversed(leases) if candidate.active),
None,
)
latest = next((c for c in reversed(leases) if c.active), None)
lease.active = False
# An older generation can share the exact same object identity as
# a newer one. Registry-level CAS cannot distinguish those leases,
# so only the latest live generation is allowed to mutate the slot.
# An older generation can share the exact object identity of a newer one; registry-level
# CAS cannot tell them apart, so only the latest live generation may mutate the slot.
try:
try:
if latest is lease:
@@ -111,17 +83,16 @@ class ReplacementCoordinator:
break
replacement = predecessor.previous
predecessor = predecessor.predecessor
lease.restore(replacement)
finally:
if lease.finalize is not None:
lease.finalize()
finally:
if leases:
self._active[lease.slot] = [
item for item in leases if item.active
]
if not self._active[lease.slot]:
live = [item for item in leases if item.active]
if live:
self._active[lease.slot] = live
else:
self._active.pop(lease.slot, None)