feat(k1): add USB networking, device management and acquisition recovery

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 00:42:57 +03:00
parent 14c3c7c2b6
commit 038337783f
78 changed files with 4612 additions and 384 deletions
+110
View File
@@ -11543,6 +11543,7 @@ class _ActualWorkerStopRaceExecutor:
observed: Callable[[], bool],
*,
reconciled_active_observed: Callable[[], bool] | None = None,
standby_work: Callable[[], None] | None = None,
) -> None:
deadline = time.monotonic() + 3.0
while not observed():
@@ -11550,6 +11551,8 @@ class _ActualWorkerStopRaceExecutor:
return None
if time.monotonic() >= deadline:
raise TimeoutError("actual worker checkpoint was not released")
if standby_work is not None:
standby_work()
threading.Event().wait(0.005)
return None
@@ -15472,6 +15475,62 @@ def test_post_start_refresh_timeout_wakes_existing_read_only_recovery(
assert service._physical_command_coordinator.snapshot() is physical # noqa: SLF001
@pytest.mark.parametrize("confirmed_start", [True, False])
@pytest.mark.parametrize("brief_gap", [True, False])
def test_returned_host_epoch_wakes_active_recovery_with_surviving_data_socket(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
brief_gap: bool,
confirmed_start: bool,
) -> None:
service, runtime = service_with_fake_runtime(tmp_path)
control, physical = _install_composite_active_recovery_fixture(
service, runtime, monkeypatch, runtime_phase="live", camera_phase="streaming",
)
if not confirmed_start:
physical["requires_reconciliation"] = True
supervisor = service._connection_supervisor # noqa: SLF001
before = supervisor.snapshot()
if brief_gap:
supervisor.observe_host_path(HostPathProbeResult(
available=False, fingerprint=None, interface=None, source_ipv4=None,
route_class="unavailable", reason_code="host-route-unavailable",
))
supervisor.observe_host_path(HostPathProbeResult(
available=True,
fingerprint=before.host_path.fingerprint if brief_gap else "same-lan-new-association",
interface=before.host_path.interface, source_ipv4=before.host_path.source_ipv4,
route_class=before.host_path.route_class,
kernel_route_fingerprint=before.host_path.kernel_route_fingerprint,
))
assert supervisor.snapshot().host_path_negative_streak == 0
assert supervisor.snapshot().control_plane.reason_code == "host-path-epoch-changed"
starts = list(control.start_projects)
stops = control.stop_calls
state = service.state()
service.state()
if not confirmed_start:
assert runtime.recovery_requests == []
assert control.start_projects == starts
assert control.stop_calls == stops
assert runtime.stop_calls == 0
return
assert runtime.recovery_requests == [
("host-path-epoch-changed", runtime.producer_generation),
]
assert state["phase"] == "reconnecting"
assert state["connection_recovery"]["automatic_read_only_rebind"] is True
assert state["acquisition"]["state"] == "acquiring"
assert state["selected_device_id"] == "test-ble-transport"
assert control.state == "scanning"
assert control.start_projects == starts
assert control.stop_calls == stops
assert runtime.stop_calls == 0
assert service._physical_command_coordinator.snapshot() is physical # noqa: SLF001
def test_incident_host_route_streak_wakes_recovery_before_binding_retirement(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
@@ -34813,3 +34872,54 @@ def test_completed_acquisition_does_not_imply_control_loss(tmp_path, socket_open
{**control, "state": "failed"}, {"source_mode": "idle"},
)
assert not service._connection_supervisor.snapshot().authority.control_allowed # noqa: SLF001
@pytest.mark.parametrize("writer_alive", [False, True])
def test_fresh_start_recovers_only_terminated_evidence_writer_marker(
tmp_path: Path, writer_alive: bool,
) -> None:
from k1link.sessions.active import ActiveSessionLease, ActiveSessionLeaseError
service, runtime = service_with_fake_runtime(tmp_path)
control = FakeInteractiveControlSession()
service._application_control_session = control # type: ignore[assignment] # noqa: SLF001
_seed_supervised_connection(service)
prepared = service.prepare_acquisition(
_prepare_request(
project_name="LEASE_RECOVERY",
host="192.168.1.20",
compatibility_attestation=ATTESTATION,
)
)
root = service.evidence_root
root.mkdir(parents=True, exist_ok=True)
interrupted = root / "20260813T120000Z_viewer_live"
interrupted.mkdir()
original_evidence = interrupted / "preserved.raw"
original_evidence.write_bytes(b"synthetic interrupted evidence")
owner = ActiveSessionLease.acquire(root, interrupted) if writer_alive else None
if owner is None:
(root / ".current_session").write_text(interrupted.name + "\n")
try:
request = _start_request(
acquisition_id=prepared["acquisition"]["acquisition_id"],
physical_acceptance=PHYSICAL_ACCEPTANCE,
)
if writer_alive:
with pytest.raises(ActiveSessionLeaseError):
service.start_acquisition(request)
assert runtime.start_calls == []
assert control.start_projects == []
assert (root / ".current_session").read_text().strip() == interrupted.name
else:
service.start_acquisition(request)
assert len(runtime.start_calls) == 1
assert control.start_projects == ["LEASE_RECOVERY"]
assert (root / ".current_session").read_text().strip() != interrupted.name
assert original_evidence.read_bytes() == b"synthetic interrupted evidence"
finally:
if owner is not None:
owner.release()
lease = service._acquisition_session_lease # noqa: SLF001
if lease is not None:
lease.release()