wip(k1): checkpoint connection recovery rewrite

Capture the current unreleased K1 connection, recovery, lifecycle, viewer, and test work as a single known-bad baseline for subsequent fixes.
This commit is contained in:
DCCONSTRUCTIONS
2026-08-14 14:57:50 +03:00
parent aff331082f
commit 0ca7316a24
157 changed files with 152962 additions and 4036 deletions
+66
View File
@@ -1,9 +1,13 @@
from __future__ import annotations
import os
from pathlib import Path
import pytest
from k1link.device_plugins.xgrids_k1.archive import discover_legacy_viewer_sessions
from k1link.sessions import ActiveSessionLease, recover_stale_active_session_marker
from k1link.sessions import active as active_session_module
def test_active_session_lease_hides_live_evidence_until_release(tmp_path: Path) -> None:
@@ -38,3 +42,65 @@ def test_startup_recovery_removes_only_an_unlocked_stale_marker(tmp_path: Path)
assert recover_stale_active_session_marker(sessions) is True
assert not marker.exists()
assert recover_stale_active_session_marker(sessions) is False
def test_active_session_release_retries_unlink_without_dropping_lock(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
sessions = tmp_path / "sessions"
session = sessions / "20260812T231000Z_viewer_live"
lease = ActiveSessionLease.acquire(sessions, session)
marker = sessions / ".current_session"
original_unlink = Path.unlink
attempts = 0
def fail_once(path: Path, *args: object, **kwargs: object) -> None:
nonlocal attempts
if path == marker and attempts == 0:
attempts += 1
raise OSError("injected marker unlink failure")
original_unlink(path, *args, **kwargs)
monkeypatch.setattr(Path, "unlink", fail_once)
with pytest.raises(OSError, match="injected marker unlink failure"):
lease.release()
assert marker.exists()
assert lease._released is False # noqa: SLF001
os.fstat(lease._descriptor) # noqa: SLF001
assert recover_stale_active_session_marker(sessions) is False
lease.release()
assert lease._released is True # noqa: SLF001
assert not marker.exists()
def test_active_session_release_retries_directory_fsync_after_unlink(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
sessions = tmp_path / "sessions"
session = sessions / "20260812T231100Z_viewer_live"
lease = ActiveSessionLease.acquire(sessions, session)
original_fsync = active_session_module._fsync_directory_strict
attempts = 0
def fail_once(path: Path) -> None:
nonlocal attempts
if attempts == 0:
attempts += 1
raise OSError("injected directory fsync failure")
original_fsync(path)
monkeypatch.setattr(active_session_module, "_fsync_directory_strict", fail_once)
with pytest.raises(OSError, match="injected directory fsync failure"):
lease.release()
assert lease._marker_removed is True # noqa: SLF001
assert lease._released is False # noqa: SLF001
os.fstat(lease._descriptor) # noqa: SLF001
lease.release()
lease.release()
assert lease._released is True # noqa: SLF001