diff --git a/pm-stack/opencode-claimant/claimant.py b/pm-stack/opencode-claimant/claimant.py index 7ed0c50281..f53722c99f 100755 --- a/pm-stack/opencode-claimant/claimant.py +++ b/pm-stack/opencode-claimant/claimant.py @@ -273,9 +273,10 @@ def get_active_root_session_count() -> int: active_ids = poll.fetch_active_ids() if active_ids is None: return -1 # Unknown - return poll.count_active_roots( + count = poll.count_active_roots( sessions, int(time.time() * 1000), active_ids ) + return -1 if count is None else count except Exception as e: print(f"Warning: Could not get active session count: {e}") return -1 # Unknown diff --git a/pm-stack/opencode-claimant/poll.py b/pm-stack/opencode-claimant/poll.py index 0398f27b3f..8a05d72a8e 100755 --- a/pm-stack/opencode-claimant/poll.py +++ b/pm-stack/opencode-claimant/poll.py @@ -165,6 +165,40 @@ def parse_lease_ms(value): return int(dt.timestamp() * 1000) + +def _session_timing(session): + """(idle, updated_ms) for a session entry, or None when its timing + cannot be read (#6868). + + Readable: `time` absent/None (timeless: updated_ms 0, matching the + pinned timeless-entry behaviour), or a dict whose present + updated/created values are numbers (bool is not a number here). + The updated/created choice keeps the historical `or` chain: the + first present non-zero numeric value wins, absent stamps read 0. + + Unreadable (None): `time` present but not an object, or a present + updated/created value of a non-numeric shape (string, list, dict, + bool). Callers fail closed on None: the entry is never counted as + known-inactive (cap counts go unknown instead) and never + classified stuck-bound on timing evidence it does not have.""" + t = session.get("time") + if t is None: + t = {} + if not isinstance(t, dict): + return None + updated = 0 + for key in ("updated", "created"): + v = t.get(key) + if v is None: + continue + if isinstance(v, bool) or not isinstance(v, (int, float)): + return None + if v: + updated = v + break + return t.get("idle"), updated + + def is_stuck_bound(row, session, active_ids, now_ms): """True iff a claimed opencode-lane task is bound to a session that looks created-but-never-prompted and dead (#4758): the task's lease @@ -195,10 +229,12 @@ def is_stuck_bound(row, session, active_ids, now_ms): return False if active_ids is None or session_id in active_ids: return False - t = session.get("time") or {} - if t.get("idle") or session.get("outcome"): + timing = _session_timing(session) + if timing is None: + return False + idle, updated = timing + if idle or session.get("outcome"): return False - updated = t.get("updated") or t.get("created") or 0 return now_ms - updated >= ACTIVE_WINDOW_MS @@ -206,7 +242,10 @@ def is_root_session(s): return isinstance(s, dict) and not s.get("parentID") and not s.get("parent_id") -def is_active_root(s, now_ms, active_ids=None): +def _active_root_state(s, now_ms, active_ids=None): + """True (proven active), False (proven inactive), or None (the + entry's timing cannot be read, so its activity is UNKNOWN — see + _session_timing; #6868).""" if not is_root_session(s): return False # The server's active set is authoritative: a RESUMED session keeps @@ -216,15 +255,27 @@ def is_active_root(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 - t = s.get("time") or {} - if t.get("idle") or s.get("outcome"): + timing = _session_timing(s) + if timing is None: + return None + idle, updated = timing + if idle or s.get("outcome"): return False - updated = t.get("updated") or t.get("created") or 0 return now_ms - updated < ACTIVE_WINDOW_MS +def is_active_root(s, now_ms, active_ids=None): + return _active_root_state(s, now_ms, active_ids) is True + + def count_active_roots(sessions, now_ms, active_ids=None): - return sum(1 for s in sessions if is_active_root(s, now_ms, active_ids)) + """Count of proven-active roots, or None when any root's activity + is unknown (#6868): an unreadable entry must never be silently + counted as inactive, which would manufacture cap room.""" + states = [_active_root_state(s, now_ms, active_ids) for s in sessions] + if any(state is None for state in states): + return None + return sum(1 for state in states if state) def summarize_cap(sessions, active_ids, now_ms): @@ -243,6 +294,8 @@ def summarize_cap(sessions, active_ids, now_ms): if active_ids is None: return len(roots), None, None active = count_active_roots(roots, now_ms, active_ids) + if active is None: + return len(roots), None, None return len(roots), active, max(0, CAP - active) @@ -474,7 +527,8 @@ def _candidate_entry(row, queue): def _stuck_entry(row, queue, session): prog = row.get("progress") or {} - sess_t = (session or {}).get("time") or {} + timing = _session_timing(session) if isinstance(session, dict) else None + sess_updated = timing[1] if timing and timing[1] else None issue = prog.get("issue_number") or prog.get("issue") or "" return { "id": row.get("id"), @@ -486,7 +540,7 @@ def _stuck_entry(row, queue, session): "lane": prog.get("lane"), "stage": prog.get("stage"), "session_id": prog.get("session_id"), - "session_updated": sess_t.get("updated") or sess_t.get("created"), + "session_updated": sess_updated, "issue": issue, "issue_number": prog.get("issue_number"), "attempts": row.get("attempts"), @@ -527,7 +581,8 @@ def get_active_root_session_count(): active_ids = fetch_active_ids() if active_ids is None: return -1 - return count_active_roots(sessions, int(time.time() * 1000), active_ids) + count = count_active_roots(sessions, int(time.time() * 1000), active_ids) + return -1 if count is None else count def main(): diff --git a/pm-stack/opencode-claimant/test_poll.py b/pm-stack/opencode-claimant/test_poll.py index 44e875e7d9..46bbe1dc22 100755 --- a/pm-stack/opencode-claimant/test_poll.py +++ b/pm-stack/opencode-claimant/test_poll.py @@ -121,6 +121,67 @@ def test_known_counts_give_room(self): self.assertEqual((live, active, room), (2, 1, poll.CAP - 1)) + +class TestWrongShapeTime(unittest.TestCase): + """#6868: a wrong-shaped `time` / timestamp must never raise out of + the classifiers, and must fail closed: never counted as a + known-inactive root (cap goes unknown), never stuck-bound.""" + + BAD_TIMES = ["soon", 123, True, ["x"], {"updated": "soon"}, + {"created": "soon"}, {"updated": ["x"]}, + {"created": {"at": 1}}, {"updated": True}] + + def test_is_active_root_never_raises_and_never_active(self): + for bad in self.BAD_TIMES: + s = {"id": "ses_x", "time": bad} + self.assertFalse(poll.is_active_root(s, NOW), repr(bad)) + self.assertFalse(poll.is_active_root(s, NOW, set()), repr(bad)) + + def test_active_set_membership_still_authoritative(self): + s = {"id": "ses_x", "time": "soon"} + self.assertTrue(poll.is_active_root(s, NOW, {"ses_x"})) + + def test_count_active_roots_unknown_on_malformed_entry(self): + sessions = [root(), {"id": "ses_bad", "time": "soon"}] + self.assertIsNone(poll.count_active_roots(sessions, NOW, set())) + + def test_summarize_cap_unknown_not_manufactured_room(self): + live, active, room = poll.summarize_cap( + [root(), {"id": "ses_bad", "time": {"updated": "soon"}}], + set(), NOW) + self.assertEqual(live, 2) + self.assertIsNone(active) + self.assertIsNone(room) + + def test_is_stuck_bound_never_raises_and_never_stuck(self): + row = TestIsStuckBound().row() + for bad in self.BAD_TIMES: + s = {"id": "ses_x", "time": bad} + self.assertFalse( + poll.is_stuck_bound(row, s, set(), NOW), repr(bad)) + + def test_stuck_entry_session_updated_none_on_malformed(self): + row = TestIsStuckBound().row() + for bad in self.BAD_TIMES: + entry = poll._stuck_entry(row, "q", {"id": "ses_x", "time": bad}) + self.assertIsNone(entry["session_updated"], repr(bad)) + + def test_stuck_entry_keeps_numeric_updated(self): + row = TestIsStuckBound().row() + s = {"id": "ses_x", + "time": {"created": NOW - 90 * MIN, "updated": NOW - 31 * MIN}} + entry = poll._stuck_entry(row, "q", s) + self.assertEqual(entry["session_updated"], NOW - 31 * MIN) + + def test_get_stuck_bound_over_mixed_list(self): + row = TestIsStuckBound().row() + good_dead = TestIsStuckBound().dead_session() + bad = {"id": "ses_bad", "time": "soon"} + by_id = {"ses_x": good_dead, "ses_bad": bad} + stuck = poll.get_stuck_bound([row], by_id, set(), NOW) + self.assertEqual(stuck, [row]) + + class TestIsCandidate(unittest.TestCase): def row(self, **over): r = {