Files
roboco/tests/unit/foundation/policy/content/test_markers.py
T
eb0dcb6ecb fix(orchestrator): task-scoped oscillation breaker for escalate/unblock ping-pong (#685)
* fix(orchestrator): task-scoped oscillation breaker for escalate/unblock ping-pong

An escalation ping-pong oscillates a task between two agents (cell PM
escalate_up -> BLOCKED -> main PM unblock -> restored -> respawn ->
escalate again). The per-(agent, task) respawn gate never trips on it:
the restored side is dispatched by _dispatch_claimed_without_agent,
which consults no respawn counter at all, so one side of the round trip
always has fuel regardless of the other's strikes — and even a tripped
main-PM counter only stalls the task silently at blocked instead of
surfacing the oscillation.

- Strikes are counted task-scoped at the unblock() chokepoint
  (agent-agnostic; legitimate needs_revision rework never calls
  unblock, so it structurally cannot trip this), durable in the
  existing orchestration_markers column — no migration.
- Progress between round-trips (commits / revision_count advancing)
  resets the count: real forward motion is not an oscillation.
- On trip: the task is blocked with a HUMAN resolver (the budget-breach
  posture), both dispatchers stop respawning onto it, further unblock()
  refuses until an admin override clears the marker, and the CEO
  notification names both agents and the cycle count.
- _notification_has_live_work now treats a HITL-blocked related task as
  no live work, closing the same loop for the admin-route escalation
  path.

* fix(orchestrator): wire the oscillation trip to the dispatchers and make recovery reachable

- TaskResponse serializes blocker_resolver_type: the dispatchers' HITL-blocked
  skip and the notification-path live-work check now actually fire over the
  wire instead of only against in-process rows.
- The oscillation marker clears on every human transition out of BLOCKED
  (snapshot or not), and the human unblock route treats a tripped task as
  the requested intervention: clears the marker and proceeds, while the
  agent gateway verb keeps refusing.
- The progress fingerprint includes the terminal-children count, so a
  coordination root whose children advanced between escalations resets
  instead of accruing toward a false trip.

---------

Co-authored-by: Renn F <rennf93@users.noreply.github.com>
2026-07-24 17:20:20 +02:00

203 lines
7.1 KiB
Python

"""Tests for the orchestration-marker accessors."""
from __future__ import annotations
from types import SimpleNamespace
from roboco.foundation.policy.content import markers as m
# Named constant — ruff PLR2004 forbids magic-value comparisons.
_TWO = 2
_THREE = 3
def _task(om: dict | None = None) -> SimpleNamespace:
return SimpleNamespace(orchestration_markers=om)
def test_original_developer_roundtrip() -> None:
t = _task()
assert m.get_original_developer(t) is None
m.set_original_developer(t, "00000000-0000-0000-0001-000000000002")
assert m.get_original_developer(t) == "00000000-0000-0000-0001-000000000002"
def test_required_cells_roundtrip() -> None:
t = _task()
assert m.get_required_cells(t) == []
m.set_required_cells(t, ["backend", "frontend"])
assert m.get_required_cells(t) == ["backend", "frontend"]
def test_dismissed_flag() -> None:
t = _task()
assert m.is_dismissed(t) is False
m.mark_dismissed(t)
assert m.is_dismissed(t) is True
def test_set_marker_reassigns_dict_for_orm_dirty_tracking() -> None:
t = _task({"a": 1})
before = t.orchestration_markers
m.set_marker(t, "b", 2)
# A new dict object — SQLAlchemy only flags JSON columns dirty on reassign.
assert t.orchestration_markers is not before
assert t.orchestration_markers == {"a": 1, "b": 2}
def test_clear_marker_nulls_when_empty() -> None:
t = _task({"x": 1})
m.clear_marker(t, "x")
assert t.orchestration_markers is None
# Clearing an absent key is a no-op.
m.clear_marker(t, "missing")
assert t.orchestration_markers is None
def test_escalation_roundtrip() -> None:
t = _task()
assert m.get_escalation(t) is None
m.set_escalation(t, from_slug="be-pm", to_slug="main-pm", reason="re-open please")
assert m.get_escalation(t) == {
"from": "be-pm",
"to": "main-pm",
"reason": "re-open please",
}
def test_approve_and_start_notes_roundtrip() -> None:
t = _task()
assert m.get_approve_and_start_notes(t) is None
m.set_approve_and_start_notes(t, "Board approved; build it.")
assert m.get_approve_and_start_notes(t) == "Board approved; build it."
def test_transition_note_roundtrip_keyed_by_event() -> None:
t = _task()
assert m.get_transition_note(t, "ceo_rejection") is None
m.set_transition_note(t, "completion", "Reviewed and merged.")
m.set_transition_note(t, "ceo_rejection", "Needs the migration first.")
# Each event keeps its own note; setting one doesn't clobber another.
assert m.get_transition_note(t, "completion") == "Reviewed and merged."
assert m.get_transition_note(t, "ceo_rejection") == "Needs the migration first."
assert m.get_transition_note(t, "never_set") is None
def test_video_draft_roundtrip() -> None:
t = _task()
assert m.get_video_draft(t) is None
m.set_video_draft(
t,
{
"occasion": "release v1.0.0",
"script": "Here's what shipped...",
"platforms": ["x", "tiktok"],
"brief": "Announce the release",
},
)
draft = m.get_video_draft(t)
assert draft is not None
assert draft["occasion"] == "release v1.0.0"
assert draft["platforms"] == ["x", "tiktok"]
def test_video_draft_extended_not_replaced() -> None:
"""The render pass extends the authoring marker rather than clobbering it —
the caller is responsible for spreading the existing dict (set_video_draft
itself just reassigns whatever payload it is given)."""
t = _task()
m.set_video_draft(t, {"occasion": "spotlight: org-memory", "script": "x"})
existing = m.get_video_draft(t) or {}
m.set_video_draft(
t, {**existing, "mp4_paths": {"vertical": "a.mp4", "square": "b.mp4"}}
)
draft = m.get_video_draft(t)
assert draft is not None
assert draft["occasion"] == "spotlight: org-memory"
assert draft["mp4_paths"] == {"vertical": "a.mp4", "square": "b.mp4"}
def test_documenter_self_heal_head_supersede() -> None:
t = _task()
m.set_documenter(t, "doc-uuid")
m.set_self_heal_fingerprint(t, "deadbeef")
m.set_external_pr_head(t, "sha123")
m.set_external_pr_supersede(t, "pr=1 review=2 closed=1")
assert m.get_documenter(t) == "doc-uuid"
assert m.get_self_heal_fingerprint(t) == "deadbeef"
assert m.get_external_pr_head(t) == "sha123"
assert m.get_external_pr_supersede(t) == "pr=1 review=2 closed=1"
def test_docs_sync_release_version_roundtrip() -> None:
t = _task()
assert m.get_docs_sync_release_version(t) is None
m.set_docs_sync_release_version(t, "0.23.0")
assert m.get_docs_sync_release_version(t) == "0.23.0"
def test_resubmit_unchanged_head_roundtrip() -> None:
t = _task()
assert m.get_resubmit_unchanged_head(t) is None
m.set_resubmit_unchanged_head(t, "aaaa1111bbbb2222")
assert m.get_resubmit_unchanged_head(t) == "aaaa1111bbbb2222"
def test_block_flip_count_bump_and_notify() -> None:
t = _task()
assert m.get_block_flip_count(t) == 0
assert m.is_block_flip_notified(t) is False
assert m.bump_block_flip_count(t) == 1
assert m.bump_block_flip_count(t) == _TWO
assert m.get_block_flip_count(t) == _TWO
# notified stays False across bumps until explicitly marked.
assert m.is_block_flip_notified(t) is False
m.mark_block_flip_notified(t)
assert m.is_block_flip_notified(t) is True
# Marking notified must not reset the counter.
assert m.get_block_flip_count(t) == _TWO
def test_oscillation_strikes_accrue_on_unchanged_fingerprint() -> None:
t = _task()
assert m.get_oscillation_strikes(t) == 0
assert m.is_oscillation_tripped(t) is False
assert m.bump_oscillation_strikes(t, [0, 0]) == 1
assert m.bump_oscillation_strikes(t, [0, 0]) == _TWO
assert m.bump_oscillation_strikes(t, [0, 0]) == _THREE
assert m.get_oscillation_strikes(t) == _THREE
assert m.is_oscillation_tripped(t) is False
def test_oscillation_strikes_reset_on_progress() -> None:
t = _task()
m.bump_oscillation_strikes(t, [0, 0])
assert m.bump_oscillation_strikes(t, [0, 0]) == _TWO
# A new commit landed between rounds — real progress resets to 1.
assert m.bump_oscillation_strikes(t, [1, 0]) == 1
# A revision round completing is progress too.
assert m.bump_oscillation_strikes(t, [1, 0]) == _TWO
assert m.bump_oscillation_strikes(t, [1, 1]) == 1
def test_mark_oscillation_tripped_preserves_strikes_and_fingerprint() -> None:
t = _task()
m.bump_oscillation_strikes(t, [2, 1])
m.bump_oscillation_strikes(t, [2, 1])
m.mark_oscillation_tripped(t)
assert m.is_oscillation_tripped(t) is True
assert m.get_oscillation_strikes(t) == _TWO
# tripped survives a subsequent bump (belt-and-suspenders — the guard is
# meant to refuse before another bump ever happens).
m.bump_oscillation_strikes(t, [2, 1])
assert m.is_oscillation_tripped(t) is True
def test_clear_marker_removes_oscillation_state() -> None:
t = _task()
m.bump_oscillation_strikes(t, [0, 0])
m.mark_oscillation_tripped(t)
m.clear_marker(t, m.OSCILLATION_STRIKES)
assert m.get_oscillation_strikes(t) == 0
assert m.is_oscillation_tripped(t) is False