From 317fed5de0f5f7a8b10ae5f734a917886abefa73 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Thu, 12 Nov 2026 01:25:30 -0600 Subject: [PATCH] fix(claimant): check terminal markers before timing in poll cap count (#8853) A finished root with a malformed timestamp classified unknown and blocked all launches via cap_room=None. Terminal markers (time.idle / outcome) are decidable without a readable clock; roots with malformed timing and no marker still fail closed. --- pm-stack/opencode-claimant/poll.py | 9 +++++++++ pm-stack/opencode-claimant/test_poll.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/pm-stack/opencode-claimant/poll.py b/pm-stack/opencode-claimant/poll.py index 8a05d72a8..f72989c6e 100755 --- a/pm-stack/opencode-claimant/poll.py +++ b/pm-stack/opencode-claimant/poll.py @@ -255,6 +255,15 @@ def _active_root_state(s, now_ms, active_ids=None): # counts the root no matter what its stored markers say. if active_ids is not None and s.get("id") in active_ids: return True + # Terminal markers are decidable without a readable clock + # (#8853): a finished root stays finished even when its + # timestamps are malformed — checking timing first made one + # such root report the whole cap unknown and block every + # launch. A root with malformed timing and NO terminal marker + # still falls through to the fail-closed None below. + t = s.get("time") + if (isinstance(t, dict) and t.get("idle")) or s.get("outcome"): + return False timing = _session_timing(s) if timing is None: return None diff --git a/pm-stack/opencode-claimant/test_poll.py b/pm-stack/opencode-claimant/test_poll.py index 46bbe1dc2..929bb4f54 100755 --- a/pm-stack/opencode-claimant/test_poll.py +++ b/pm-stack/opencode-claimant/test_poll.py @@ -153,6 +153,27 @@ def test_summarize_cap_unknown_not_manufactured_room(self): self.assertIsNone(active) self.assertIsNone(room) + def test_terminal_marker_with_malformed_timing_is_inactive(self): + # #8853 (oracle: the acceptance example on issue #8853): a + # finished root's malformed timestamp must not make the cap + # unknown — the terminal marker is checked before timing. + s = {"id": "ses_done", "time": {"idle": 123, "updated": "bad"}} + self.assertIs(poll._active_root_state(s, NOW, set()), False) + self.assertEqual(poll.count_active_roots([s], NOW, set()), 0) + live, active, room = poll.summarize_cap([root(), s], set(), NOW) + self.assertEqual((live, active, room), (2, 1, poll.CAP - 1)) + + def test_outcome_with_malformed_timing_is_inactive(self): + s = {"id": "ses_done", "time": {"updated": "bad"}, + "outcome": "failed"} + self.assertIs(poll._active_root_state(s, NOW, set()), False) + + def test_active_set_still_overrides_terminal_marker(self): + # A resumed session in the server's active set counts active + # even with a stored terminal marker and malformed timing. + s = {"id": "ses_done", "time": {"idle": 123, "updated": "bad"}} + self.assertIs(poll._active_root_state(s, NOW, {"ses_done"}), True) + def test_is_stuck_bound_never_raises_and_never_stuck(self): row = TestIsStuckBound().row() for bad in self.BAD_TIMES: