From 5abd07d2bf905fe9e313ff71ae0b538c3b6049ed Mon Sep 17 00:00:00 2001 From: DCCONSTRUCTIONS Date: Fri, 24 Jul 2026 23:27:51 +0300 Subject: [PATCH] fix(simulation): cache idle worker state --- src/k1link/simulation/worker_agent.py | 5 ++--- tests/test_simulation_worker_agent_command.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/k1link/simulation/worker_agent.py b/src/k1link/simulation/worker_agent.py index 2e4d81a..08278c7 100644 --- a/src/k1link/simulation/worker_agent.py +++ b/src/k1link/simulation/worker_agent.py @@ -637,9 +637,8 @@ class SimulationWorkerAgent: ) def _active_run(self) -> QualificationRun | None: - cached: QualificationRun | None = getattr(self, "_active_run_cache", None) - if cached is not None: - return cached + if hasattr(self, "_active_run_cache"): + return self._active_run_cache active = tuple(run for run in self.store.list_runs() if run.state in ACTIVE_STATES) if len(active) > 1: raise WorkerAgentError("multiple active qualification runs violate worker authority") diff --git a/tests/test_simulation_worker_agent_command.py b/tests/test_simulation_worker_agent_command.py index 8be23ae..6c48036 100644 --- a/tests/test_simulation_worker_agent_command.py +++ b/tests/test_simulation_worker_agent_command.py @@ -171,6 +171,28 @@ def test_worker_agent_command_dispatch_fails_closed_on_unsafe_envelope() -> None ) +def test_worker_agent_caches_the_absence_of_an_active_run() -> None: + class EmptyStore: + def __init__(self) -> None: + self.list_calls = 0 + + def list_runs(self) -> tuple[QualificationRun, ...]: + self.list_calls += 1 + return () + + agent = object.__new__(SimulationWorkerAgent) + store = EmptyStore() + agent.__dict__["store"] = store + + first = agent.status() + second = agent.status() + + assert first["available"] is True + assert first["active_run_id"] is None + assert second["active_run_id"] is None + assert store.list_calls == 1 + + def test_px4_adapter_maps_speed_to_stock_rover_throttle_without_direct_actuators() -> None: publishers = [_Publisher() for _ in range(3)]