Files
hermes-agent/tests/hermes_cli/test_kanban_blocked_sticky.py
T
Teknium 39975613b1 test: prune wave 2 + speed fixes — 28,106 → 19,757 test functions, suite wall 315s → 294s
Second, deeper pass over tools/gateway/hermes_cli plus first pass over
the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker,
dashboard, conformance, monitoring, secret_sources, hermes_state,
providers). Same rubric as wave 1 (AGENTS.md test policy); security,
alternation/caching invariants, issue-number regressions, and E2E kept.

Real test-quality fixes found and rooted out along the way:
- tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls
  (DEFAULT_CONFIG smart-approval leaked in) — pinned approval
  mode=manual via autouse fixture: 17.4s → 0.4s.
- test_model_switch_custom_providers.py / test_user_providers_model_switch.py
  silently probed live provider catalogs (~2s/test) — stubbed
  cached_provider_model_ids/provider_model_ids/fetch_api_models.
- test_telegram_noise_filter.py: 15-platform copy-paste matrix over
  shared gateway.run logic → 3 representative platforms (55s → 3.9s).
- test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on
  MagicMock agents — interrupt.side_effect now clears _running_agents
  (22s → 1.0s).
- test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x
  (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps
  patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait
  5s → 0.5s.
- test_telegram_init_deadline.py: loop-block margin restored to 1.0s
  with rationale comment — the watchdog-dump assertion needs the loop
  blocked well past deadline+grace under parallel load (flaked once in
  the 40-worker verification run at a 0.2s margin).

Verification: full hermetic suite via scripts/run_tests.sh —
2,438 files, 21,718 tests passed, 0 failed, 293.9s wall.
Suite totals vs original baseline: 46,820 → 19,757 test functions
(−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
2026-07-29 13:39:40 -07:00

164 lines
6.5 KiB
Python

"""Regression tests for #28712 — kanban dispatcher must not auto-promote
worker-initiated ``kanban_block`` (sticky blocks), but must keep
auto-recovering circuit-breaker blocks.
The bug: when a worker called ``kanban_block(reason="review-required:
...")`` to hand off to a human, the dispatcher's ``recompute_ready``
would promote the task back to ``ready`` on the next tick. The fresh
worker found nothing to do (work already applied), exited cleanly, and
got recorded as a ``protocol_violation`` → ``gave_up`` → promote → loop
until manual intervention.
These tests pin down:
* Worker / operator-initiated blocks are sticky and survive
``recompute_ready``.
* Circuit-breaker blocks (``gave_up`` event, status flipped via
``_record_task_failure``) still auto-recover — the original intent
of #40c1decb3 is preserved.
* An explicit ``kanban_unblock`` clears the sticky state.
* The full block → promote → crash → ``gave_up`` loop is broken after
this fix: subsequent ticks leave the task blocked.
The tangentially related schema-init ordering bug originally reported
in #28712 (``init_db`` crashing on legacy DBs that pre-dated the
``session_id`` migration) is covered separately by
``test_kanban_db.py::test_connect_migrates_legacy_db_before_optional_column_indexes``,
landed via #28754 / #28781 ahead of this fix.
"""
from __future__ import annotations
import time
from pathlib import Path
import pytest
from hermes_cli import kanban_db as kb
@pytest.fixture
def kanban_home(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
"""Isolated HERMES_HOME with an empty kanban DB."""
home = tmp_path / ".hermes"
home.mkdir()
monkeypatch.setenv("HERMES_HOME", str(home))
monkeypatch.setattr(Path, "home", lambda: tmp_path)
kb.init_db()
return home
# ---------------------------------------------------------------------------
# Worker-initiated kanban_block must be sticky
# ---------------------------------------------------------------------------
def test_worker_block_is_not_auto_promoted_by_recompute_ready(kanban_home: Path) -> None:
"""A standalone task that a worker explicitly blocks for review
must stay blocked across an arbitrary number of dispatcher ticks.
Before #28712's fix, ``recompute_ready`` would silently flip it
back to ``ready`` on the very next tick."""
with kb.connect() as conn:
tid = kb.create_task(conn, title="needs human review")
kb.claim_task(conn, tid)
assert kb.block_task(
conn, tid,
reason="review-required: please verify ACL change",
expected_run_id=kb.get_task(conn, tid).current_run_id,
)
assert kb.get_task(conn, tid).status == "blocked"
# Hammer the promotion code — exactly the dispatcher loop's
# behaviour, just compressed in time.
for _ in range(5):
promoted = kb.recompute_ready(conn)
assert promoted == 0, "worker-blocked task must not auto-promote"
assert kb.get_task(conn, tid).status == "blocked"
# ---------------------------------------------------------------------------
# Circuit-breaker blocks still auto-recover (preserve #40c1decb3 intent)
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# unblock_task clears the sticky state
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Full bug-shaped loop: block → promote → crash → gave_up → next tick
# ---------------------------------------------------------------------------
def test_protocol_violation_loop_is_broken(kanban_home: Path) -> None:
"""Reproduces the exact #28712 loop and asserts the dispatcher
leaves the task blocked instead of cycling.
Loop shape from the issue:
1. Worker calls ``kanban_block`` → status='blocked',
``task_runs.outcome='blocked'``, ``blocked`` event.
2. (Bug) Dispatcher promotes back to ``ready``.
3. Fresh worker exits cleanly without terminal tool call →
``protocol_violation`` event.
4. ``_record_task_failure(failure_limit=1)`` → ``gave_up`` event,
status='blocked' again.
5. (Bug) Dispatcher promotes again → infinite loop.
With the fix in place, step 2 never happens — the test simulates
one would-be loop cycle by faking the crash-then-gave_up entries
that *would* have been written and asserts the *next* tick still
leaves the task blocked.
"""
with kb.connect() as conn:
tid = kb.create_task(conn, title="loop reproducer")
kb.claim_task(conn, tid)
kb.block_task(
conn, tid,
reason="review-required: human eyes please",
expected_run_id=kb.get_task(conn, tid).current_run_id,
)
assert kb.get_task(conn, tid).status == "blocked"
# First dispatcher tick — must NOT promote.
assert kb.recompute_ready(conn) == 0
assert kb.get_task(conn, tid).status == "blocked"
# Simulate the (hypothetical) protocol_violation + gave_up
# entries that the dispatcher would have written if the bug
# were still present. Even with those event rows in place,
# the worker-initiated ``blocked`` event is the most recent
# of the ``{blocked, unblocked}`` pair, so the sticky guard
# still fires.
now = int(time.time())
conn.execute(
"INSERT INTO task_events (task_id, kind, payload, created_at) "
"VALUES (?, 'protocol_violation', NULL, ?)",
(tid, now),
)
conn.execute(
"INSERT INTO task_events (task_id, kind, payload, created_at) "
"VALUES (?, 'gave_up', NULL, ?)",
(tid, now + 1),
)
conn.commit()
# Subsequent ticks must still leave it blocked.
for _ in range(3):
promoted = kb.recompute_ready(conn)
assert promoted == 0
assert kb.get_task(conn, tid).status == "blocked"
# ---------------------------------------------------------------------------
# Schema-init recovery on legacy DBs is covered by
# tests/hermes_cli/test_kanban_db.py::test_connect_migrates_legacy_db_before_optional_column_indexes
# (landed via #28754 / #28781). The original PR shipped a duplicate test
# here; dropped during salvage to avoid two assertions of the same contract.
# ---------------------------------------------------------------------------