Review fold on the #97462 salvage: the same ISO-text-in-INTEGER rows that crashed `kanban show` also abort the dispatcher's own raw-SQL reads, and those run every tick for the whole board: - enforce_max_runtime / detect_stale_running: int(active_started_at) and int(last_heartbeat_at) raised ValueError and stopped the sweep, so no stale run on the board was reclaimed. An unreadable start now counts as "just started" (never kill or reclaim on a clock we cannot read). - the dead-worker backstop (_reclaim_dead_workers) did time.time() - started_at on the raw column (TypeError), which also aborted every sweep after it; it is what still reclaims a skipped dead worker. - release_stale_claims heartbeat backstop and the reclaim / claim-extend event payloads; the active-PR respawn guard's comment timestamp. - `hermes kanban watch` formats its raw event rows through _to_epoch. - Task.last_heartbeat_at joins _TASK_EPOCH_COLUMNS (now read via the tolerant row getter); the active-PR guard fails closed on an unreadable comment time. Test: a run with text start/heartbeat clocks is skipped (not killed) by the max-runtime and stale sweeps and still reclaimed by the dead-worker backstop (ValueError on main, TypeError on the previous commit). The show test now pins exact epochs, not just int.
296 lines
11 KiB
Python
296 lines
11 KiB
Python
"""Tests for the kanban CLI surface (hermes_cli.kanban)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from hermes_cli import kanban as kc
|
|
from hermes_cli import kanban_db as kb
|
|
from hermes_cli import kanban_db_connect as kbc
|
|
|
|
|
|
@pytest.fixture
|
|
def kanban_home(tmp_path, monkeypatch):
|
|
home = tmp_path / ".hermes"
|
|
home.mkdir()
|
|
monkeypatch.setenv("HERMES_HOME", str(home))
|
|
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
|
kb.init_db()
|
|
return home
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Workspace flag parsing
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# run_slash smoke tests (end-to-end via the same entry both CLI and gateway use)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def test_kanban_list_json_includes_session_id(kanban_home):
|
|
"""JSON output exposes `session_id` so external clients (Scarf, web
|
|
dashboards) don't need a side query to filter by chat session."""
|
|
from hermes_cli import kanban_db as kb
|
|
from hermes_cli import kanban_db_connect as kbc
|
|
with kbc.connect() as conn:
|
|
kb.create_task(
|
|
conn, title="acp task", assignee="alice", session_id="acp-x"
|
|
)
|
|
raw = kc.run_slash("list --json")
|
|
payload = json.loads(raw)
|
|
assert any(
|
|
row.get("title") == "acp task"
|
|
and row.get("session_id") == "acp-x"
|
|
for row in payload
|
|
)
|
|
|
|
|
|
def test_run_slash_stats_text_includes_review_lane(kanban_home):
|
|
"""``stats`` text output must surface the ``review`` lane: board_stats() counts
|
|
every non-archived status, so the ``By status:`` block must list ``review`` too
|
|
or its count silently disappears while ``--json`` still shows it."""
|
|
import re
|
|
|
|
with kbc.connect_closing() as conn:
|
|
tid = kb.create_task(conn, title="needs review", assignee="alice")
|
|
assert kb.request_review(conn, tid, summary="s", force=True)
|
|
|
|
assert re.search(r"review\s+1", kc.run_slash("stats"))
|
|
assert json.loads(kc.run_slash("stats --json"))["by_status"].get("review") == 1
|
|
|
|
|
|
def test_kanban_show_json_includes_runtime_limit(kanban_home):
|
|
with kbc.connect() as conn:
|
|
bounded_id = kb.create_task(
|
|
conn, title="bounded task", max_runtime_seconds=2700
|
|
)
|
|
uncapped_id = kb.create_task(conn, title="uncapped task")
|
|
|
|
bounded = json.loads(kc.run_slash(f"show {bounded_id} --json"))
|
|
uncapped = json.loads(kc.run_slash(f"show {uncapped_id} --json"))
|
|
|
|
assert bounded["task"]["max_runtime_seconds"] == 2700
|
|
assert uncapped["task"]["max_runtime_seconds"] is None
|
|
|
|
|
|
def test_kanban_show_text_renders_graph_with_open_connection(kanban_home):
|
|
with kbc.connect_closing() as conn:
|
|
parent_id = kb.create_task(conn, title="parent task")
|
|
child_id = kb.create_task(conn, title="child task")
|
|
kb.link_tasks(conn, parent_id=parent_id, child_id=child_id)
|
|
|
|
output = kc.run_slash(f"show {child_id}")
|
|
|
|
assert "child task" in output
|
|
assert parent_id in output
|
|
assert "Cannot operate on a closed database" not in output
|
|
|
|
|
|
def test_kanban_show_text_renders_legacy_iso_completed_at(kanban_home):
|
|
with kbc.connect_closing() as conn:
|
|
task_id = kb.create_task(conn, title="legacy stamp")
|
|
kb.complete_task(conn, task_id, result="ok", force=True)
|
|
conn.execute("UPDATE tasks SET completed_at = '2026-09-30T14:22:11' WHERE id = ?", (task_id,))
|
|
conn.commit()
|
|
|
|
output = kc.run_slash(f"show {task_id}")
|
|
|
|
assert "completed: 2026-09-30 14:22" in " ".join(output.split())
|
|
|
|
|
|
def test_show_survives_iso_strings_in_integer_timestamp_columns(kanban_home):
|
|
"""`kanban show` must not crash on ISO-8601 strings written into the
|
|
INTEGER timestamp columns of tasks / task_runs / task_events by raw SQL
|
|
outside the mutators (#97455): both output modes stay readable and serve
|
|
epoch ints instead of raising ``invalid literal for int()``."""
|
|
iso_utc = "2026-08-28T14:35:22+00:00"
|
|
iso_z = "2026-08-28T16:58:56.613Z"
|
|
with kbc.connect_closing() as conn:
|
|
task_id = kb.create_task(conn, title="corrupted card", assignee="alice")
|
|
assert kb.claim_task(conn, task_id)
|
|
assert kb.complete_task(conn, task_id, summary="done")
|
|
conn.execute("UPDATE task_runs SET ended_at = ? WHERE task_id = ?", (iso_utc, task_id))
|
|
conn.execute("UPDATE tasks SET completed_at = ? WHERE id = ?", (iso_z, task_id))
|
|
conn.execute("UPDATE task_events SET created_at = ? WHERE task_id = ?", (iso_z, task_id))
|
|
conn.commit()
|
|
|
|
output = kc.run_slash(f"show {task_id}")
|
|
assert f"Task {task_id}: corrupted card" in output
|
|
assert "invalid literal" not in output
|
|
|
|
payload = json.loads(kc.run_slash(f"show {task_id} --json"))
|
|
assert payload["task"]["id"] == task_id
|
|
assert [r["ended_at"] for r in payload["runs"]] == [1787927722] # iso_utc, not a 0 fallback
|
|
assert payload["events"] and {e["created_at"] for e in payload["events"]} == {1787936336} # iso_z
|
|
|
|
|
|
def test_kanban_edit_updates_documented_task_fields(kanban_home):
|
|
with kbc.connect_closing() as conn:
|
|
task_id = kb.create_task(conn, title="old title", body="old body", priority=2)
|
|
|
|
kc.run_slash(
|
|
f"edit {task_id} --title 'new title' --body 'new body' --priority 70"
|
|
)
|
|
|
|
with kbc.connect_closing() as conn:
|
|
task = kb.get_task(conn, task_id)
|
|
events = kb.list_events(conn, task_id)
|
|
assert (task.title, task.body, task.priority) == ("new title", "new body", 70)
|
|
assert any(event.kind == "reprioritized" for event in events)
|
|
|
|
|
|
def test_worker_link_preserves_foreign_child_rules(kanban_home, monkeypatch):
|
|
with kbc.connect_closing() as conn:
|
|
worker = kb.create_task(conn, title="worker")
|
|
assert kb.claim_task(conn, worker, claimer="worker") is not None
|
|
worker_run_id = kb.get_task(conn, worker).current_run_id
|
|
parent = kb.create_task(conn, title="unfinished parent")
|
|
ready_child = kb.create_task(conn, title="foreign ready child")
|
|
running_child = kb.create_task(conn, title="foreign running child")
|
|
assert kb.claim_task(conn, running_child, claimer="other") is not None
|
|
|
|
monkeypatch.setenv("HERMES_KANBAN_TASK", worker)
|
|
monkeypatch.setenv("HERMES_KANBAN_RUN_ID", str(worker_run_id))
|
|
|
|
assert kc._cmd_link(argparse.Namespace(
|
|
parent_id=parent, child_id=ready_child,
|
|
)) == 0
|
|
with pytest.raises(ValueError, match="child is already running"):
|
|
kc._cmd_link(argparse.Namespace(
|
|
parent_id=parent, child_id=running_child,
|
|
))
|
|
# Owner handoff: the worker links its own running card, proving ownership
|
|
# with HERMES_KANBAN_RUN_ID — the one path _cmd_link forwards a run id for.
|
|
assert kc._cmd_link(argparse.Namespace(
|
|
parent_id=parent, child_id=worker,
|
|
)) == 0
|
|
|
|
with kbc.connect_closing() as conn:
|
|
assert kb.parent_ids(conn, ready_child) == [parent]
|
|
assert kb.parent_ids(conn, running_child) == []
|
|
assert kb.parent_ids(conn, worker) == [parent]
|
|
|
|
|
|
def test_board_override_is_isolated_per_concurrent_call(kanban_home, monkeypatch):
|
|
kb.create_board("alpha")
|
|
kb.create_board("beta")
|
|
|
|
parser = argparse.ArgumentParser(prog="hermes", add_help=False)
|
|
sub = parser.add_subparsers(dest="command")
|
|
kc.build_parser(sub)
|
|
|
|
barrier = threading.Barrier(2)
|
|
original_init_db = kb.init_db
|
|
|
|
def slow_init_db(*args, **kwargs):
|
|
try:
|
|
barrier.wait(timeout=5)
|
|
except threading.BrokenBarrierError:
|
|
pass
|
|
return original_init_db(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(kb, "init_db", slow_init_db)
|
|
|
|
failures: list[str] = []
|
|
|
|
def worker(board: str, title: str) -> None:
|
|
args = parser.parse_args(["kanban", "--board", board, "create", title])
|
|
rc = kc.kanban_command(args)
|
|
if rc != 0:
|
|
failures.append(f"{board}:{rc}")
|
|
|
|
t1 = threading.Thread(target=worker, args=("alpha", "alpha-task"))
|
|
t2 = threading.Thread(target=worker, args=("beta", "beta-task"))
|
|
t1.start()
|
|
t2.start()
|
|
t1.join()
|
|
t2.join()
|
|
|
|
assert failures == []
|
|
|
|
with kbc.connect_closing(board="alpha") as conn:
|
|
alpha_titles = [row.title for row in kb.list_tasks(conn, limit=100)]
|
|
with kbc.connect_closing(board="beta") as conn:
|
|
beta_titles = [row.title for row in kb.list_tasks(conn, limit=100)]
|
|
|
|
assert alpha_titles == ["alpha-task"]
|
|
assert beta_titles == ["beta-task"]
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Integration with the COMMAND_REGISTRY
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# reclaim + reassign CLI smoke tests
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def test_run_slash_reclaim_running_task(kanban_home):
|
|
import re
|
|
import time
|
|
import secrets
|
|
from hermes_cli import kanban_db_connect as kbc
|
|
|
|
out1 = kc.run_slash("create 'stuck worker task' --assignee broken-model")
|
|
m = re.search(r"(t_[a-f0-9]+)", out1)
|
|
assert m
|
|
tid = m.group(1)
|
|
|
|
# Simulate a running claim outside TTL.
|
|
conn = kbc.connect()
|
|
try:
|
|
lock = secrets.token_hex(4)
|
|
conn.execute(
|
|
"UPDATE tasks SET status='running', claim_lock=?, claim_expires=?, "
|
|
"worker_pid=? WHERE id=?",
|
|
(lock, int(time.time()) + 3600, 4242, tid),
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO task_runs (task_id, status, claim_lock, claim_expires, "
|
|
"worker_pid, started_at) VALUES (?, 'running', ?, ?, ?, ?)",
|
|
(tid, lock, int(time.time()) + 3600, 4242, int(time.time())),
|
|
)
|
|
rid = conn.execute("SELECT last_insert_rowid()").fetchone()[0]
|
|
conn.execute("UPDATE tasks SET current_run_id=? WHERE id=?", (rid, tid))
|
|
conn.commit()
|
|
finally:
|
|
conn.close()
|
|
|
|
out = kc.run_slash(f"reclaim {tid} --reason 'test'")
|
|
assert "Reclaimed" in out, out
|
|
# Status back to ready.
|
|
out2 = kc.run_slash(f"show {tid}")
|
|
assert "ready" in out2.lower()
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# /kanban specify — slash surface (same entry point CLI + gateway use)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# /kanban help / no-args / unknown-action UX (issue #21794)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|