diff --git a/loopx/capabilities/native_chat/steward_monitor.py b/loopx/capabilities/native_chat/steward_monitor.py index 17d41b3e4a..d172dd3371 100644 --- a/loopx/capabilities/native_chat/steward_monitor.py +++ b/loopx/capabilities/native_chat/steward_monitor.py @@ -11,6 +11,7 @@ from collections.abc import Mapping, Sequence import subprocess as subprocess +from ...control_plane.runtime.time import chronology_key from ...control_plane.scheduler.monitor_todo import monitor_todo_is_due from ...control_plane.todos.list_readback import list_goal_todos from ...file_lock import exclusive_file_lock @@ -157,7 +158,7 @@ def runnable_advancement_todos( rows, key=lambda row: ( prio.get(str(row.get("priority") or "P1"), 9), - str(row.get("created_at") or ""), + chronology_key(row.get("created_at")), str(row["todo_id"]), ), ) diff --git a/regression/StewardFollowupOrder.lean b/regression/StewardFollowupOrder.lean new file mode 100644 index 0000000000..c5e7d35ab8 --- /dev/null +++ b/regression/StewardFollowupOrder.lean @@ -0,0 +1,77 @@ +import Std + +/- +Run with Lean 4.22.0: `lean -DwarningAsError=true regression/StewardFollowupOrder.lean`. + +This models `runnable_advancement_todos` after eligibility and timestamp +parsing. Smaller priority numbers rank first. Timestamps are optional UTC +microseconds, with ranks for the original timestamp and Todo ID strings. +The Python regression covers the actual filters, parser, and sorted output. +-/ + +namespace StewardFollowupOrder + +structure Todo where + eligible : Bool + priority : Nat + time : Option Int + rawRank : Nat + idRank : Nat + +abbrev tieBeforeOrAt (a b : Todo) : Prop := + a.rawRank < b.rawRank ∨ (a.rawRank = b.rawRank ∧ a.idRank ≤ b.idRank) + +def timeBeforeOrAt (a b : Todo) : Bool := + match a.time, b.time with + | some left, some right => decide (left < right ∨ (left = right ∧ tieBeforeOrAt a b)) + | none, some _ => true + | some _, none => false + | none, none => decide (tieBeforeOrAt a b) + +def beforeOrAt (a b : Todo) : Bool := + decide (a.priority < b.priority ∨ + (a.priority = b.priority ∧ timeBeforeOrAt a b = true)) + +def runnable (todos : List Todo) : List Todo := + (todos.filter Todo.eligible).mergeSort beforeOrAt + +theorem runnable_keeps_exactly_the_eligible_todos (todos : List Todo) : + (runnable todos).Perm (todos.filter Todo.eligible) := + List.mergeSort_perm _ _ + +theorem runnable_is_ordered (todos : List Todo) : + (runnable todos).Pairwise (fun a b => beforeOrAt a b = true) := by + unfold runnable + apply List.sorted_mergeSort + · rintro ⟨_, ap, atime, ar, ai⟩ ⟨_, bp, btime, br, bi⟩ ⟨_, cp, ctime, cr, ci⟩ hab hbc + cases atime <;> cases btime <;> cases ctime <;> + simp [beforeOrAt, timeBeforeOrAt, tieBeforeOrAt] at * <;> omega + · rintro ⟨_, ap, atime, ar, ai⟩ ⟨_, bp, btime, br, bi⟩ + cases atime <;> cases btime <;> + simp [beforeOrAt, timeBeforeOrAt, tieBeforeOrAt, Bool.or_eq_true] <;> omega + +theorem priority_precedes_time (todos : List Todo) : + (runnable todos).Pairwise (fun a b => a.priority ≤ b.priority) := by + apply (runnable_is_ordered todos).imp + intro a b ordered + simp only [beforeOrAt, decide_eq_true_eq] at ordered + omega + +theorem equal_priority_uses_chronological_order (todos : List Todo) : + (runnable todos).Pairwise (fun a b => a.priority = b.priority → + ∀ first second, a.time = some first → b.time = some second → first ≤ second) := by + apply (runnable_is_ordered todos).imp + intro a b ordered samePriority first second firstTime secondTime + simp [beforeOrAt, timeBeforeOrAt, firstTime, secondTime, samePriority] at ordered + omega + +theorem text_sort_can_put_a_newer_equal_priority_todo_first : + "2026-02-03T10:00:00.000001Z" < "2026-02-03T10:00:00Z" ∧ + beforeOrAt ⟨true, 1, some 0, 1, 0⟩ ⟨true, 1, some 1, 0, 1⟩ = true := by decide + +#print axioms runnable_keeps_exactly_the_eligible_todos +#print axioms priority_precedes_time +#print axioms equal_priority_uses_chronological_order +#print axioms text_sort_can_put_a_newer_equal_priority_todo_first + +end StewardFollowupOrder diff --git a/tests/capabilities/test_steward_monitor_contract.py b/tests/capabilities/test_steward_monitor_contract.py index 8553ba3f38..949577f751 100644 --- a/tests/capabilities/test_steward_monitor_contract.py +++ b/tests/capabilities/test_steward_monitor_contract.py @@ -429,6 +429,36 @@ def fake_list_goal_todos(**_kwargs: object) -> dict[str, object]: assert read_calls == 1 +def test_followups_keep_priority_and_order_equal_priority_by_utc(tmp_path: Path) -> None: + monitor = StewardMonitor(_as_controller(_StubController(tmp_path))) + todos: list[StewardTodoRecord] = [ + { + "todo_id": todo_id, + "status": "open", + "task_class": "advancement_task", + "claimed_by": "steward", + "target_key": FOLLOWUP_TARGET_PREFIX + todo_id, + "priority": priority, + "created_at": timestamp, + "resume_ready": resume_ready, + } + for todo_id, priority, timestamp, resume_ready in [ + ("earlier-z", "P1", "2026-02-03T10:00:00Z", True), + ("later", "P1", "2026-02-03T10:00:00.000001Z", True), + ("urgent", "P0", "2026-02-03T10:00:01Z", True), + ("equal-offset", "P1", "2026-02-03T18:00:00+08:00", True), + ("earlier-a", "P1", "2026-02-03T10:00:00Z", True), + ("waiting", "P0", "2026-02-03T09:00:00Z", False), + ] + ] + + selected = monitor.runnable_advancement_todos({"agent_id": "steward"}, todos=todos) + + assert [row["todo_id"] for row in selected] == [ + "urgent", "earlier-a", "earlier-z", "equal-offset", "later", + ] + + def test_collect_signal_monitor_key_presence_and_quiet_zero_todo_reads( tmp_path: Path, monkeypatch: pytest.MonkeyPatch ) -> None: