Skip to content

The agent that grows with you

chore(tests): remove the never-executed kanban stress/chaos suite

Tekniumcce2d9418b89
diff --git a/tests/stress/README.md b/tests/stress/README.md
deleted file mode 100644
index 8f56f24f35c..00000000000
--- a/tests/stress/README.md
+++ /dev/null
@@ -1,41 +0,0 @@
-# Stress / battle-test suite
-
-Long-running tests that exercise the Kanban kernel under adversarial
-conditions. **Not run by `scripts/run_tests.sh`** because they can
-take 30+ seconds each and spawn real subprocesses.
-
-Run manually:
-
-```bash
-./venv/bin/python -m pytest tests/stress/ -v -s
-# or individual files:
-./venv/bin/python tests/stress/test_concurrency.py
-./venv/bin/python tests/stress/test_subprocess_e2e.py
-./venv/bin/python tests/stress/test_property_fuzzing.py
-./venv/bin/python tests/stress/test_benchmarks.py
-```
-
-## What's covered
-
-- **test_concurrency.py** — 5 workers, 100 tasks, race-for-claim. Asserts
- no double-claims, no orphan runs, no SQLite errors escape retry.
-- **test_concurrency_mixed.py** — 10 workers + 1 reclaimer, 500 tasks,
- random ops (claim/complete/block/unblock/archive). Same invariants
- under adversarial scheduling.
-- **test_concurrency_reclaim_race.py** — TTL < work duration so the
- reclaimer intentionally yanks tasks mid-work; verifies the worker's
- late-complete is refused cleanly (CAS guard works).
-- **test_subprocess_e2e.py** — dispatcher spawns real Python subprocess
- workers that heartbeat + complete via the CLI; crash detection
- against a real dead PID.
-- **test_property_fuzzing.py** — 500 random operation sequences,
- ~40k operations total, 9 invariant checks after each step.
-- **test_atypical_scenarios.py** — 28 scenarios covering atypical
- user inputs: unicode/emoji/RTL, 1 MB strings, SQL injection
- attempts, cycles, self-parents, wide fan-in/out, clock skew,
- HERMES_HOME with spaces/unicode/symlinks, 1000 runs on one
- task, idempotency-key race across processes, terminal-state
- resurrection attempts, dashboard REST with weird JSON.
-- **test_benchmarks.py** — latency at 100/1k/10k tasks for dispatch,
- recompute_ready, list_tasks, build_worker_context, etc. Results saved
- to JSON for regression diffing.
diff --git a/tests/stress/_fake_worker.py b/tests/stress/_fake_worker.py
deleted file mode 100644
index 9a4d17bbbbb..00000000000
--- a/tests/stress/_fake_worker.py
+++ /dev/null
@@ -1,49 +0,0 @@
-#!/usr/bin/env python3
-"""Fake worker process that exercises the real subprocess contract.
-
-Reads HERMES_KANBAN_TASK from env, heartbeats periodically, does short
-work, completes via the CLI. Designed to be spawned by the dispatcher
-exactly the way `hermes chat -q` would be, minus the LLM cost.
-"""
-
-import json
-import os
-import subprocess
-import time
-
-
-def main():
- tid = os.environ["HERMES_KANBAN_TASK"]
- workspace = os.environ.get("HERMES_KANBAN_WORKSPACE", "")
-
- # Announce via CLI (goes through real argparse + init_db + etc)
- subprocess.run(
- ["hermes", "kanban", "heartbeat", tid, "--note", "started"],
- check=True, capture_output=True,
- )
-
- # Simulate work with periodic heartbeats
- for i in range(3):
- time.sleep(0.3)
- subprocess.run(
- ["hermes", "kanban", "heartbeat", tid, "--note", f"progress {i+1}/3"],
- check=True, capture_output=True,
- )
-
- # Complete with structured handoff
- subprocess.run(
- [
- "hermes", "kanban", "complete", tid,
- "--summary", f"real-subprocess worker finished {tid}",
- "--metadata", json.dumps({
- "workspace": workspace,
- "worker_pid": os.getpid(),
- "iterations": 3,
- }),
- ],
- check=True, capture_output=True,
- )
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/conftest.py b/tests/stress/conftest.py
deleted file mode 100644
index 4c72a0462d0..00000000000
--- a/tests/stress/conftest.py
+++ /dev/null
@@ -1,37 +0,0 @@
-"""pytest config for the stress/ subdirectory.
-
-These tests are slow (30s+), spawn subprocesses, and are not run by
-default. Enable via `pytest --run-stress` or by running the scripts
-directly.
-
-The scripts are primarily __main__-executable entry points; pytest
-isn't expected to collect individual test functions from them.
-"""
-import pytest
-
-
-def pytest_collection_modifyitems(config, items):
- if config.getoption("--run-stress", default=False):
- return
- skip_stress = pytest.mark.skip(
- reason="stress test (opt-in via --run-stress or run script directly)"
- )
- for item in items:
- if "tests/stress" in str(item.fspath):
- item.add_marker(skip_stress)
-
-
-def pytest_addoption(parser):
- parser.addoption(
- "--run-stress",
- action="store_true",
- default=False,
- help="Run the stress/battle-test suite (slow, spawns subprocesses).",
- )
-
-
-collect_ignore_glob = [
- # The stress scripts have top-level code and hard-coded paths; they're
- # meant to run as `python tests/stress/<name>.py`, not as pytest modules.
- "*.py",
-]
diff --git a/tests/stress/test_atypical_scenarios.py b/tests/stress/test_atypical_scenarios.py
deleted file mode 100644
index ed537956ce8..00000000000
--- a/tests/stress/test_atypical_scenarios.py
+++ /dev/null
@@ -1,1063 +0,0 @@
-"""Atypical user scenarios and configurations.
-
-Exercises the kernel against user inputs and environments that the
-normal tests assume away:
-
- - Data: unicode, emoji, RTL, huge strings, control chars, SQL
- injection attempts, malformed JSON, newlines in summaries.
- - Graph: cycles, self-parenting, diamonds, wide fan-out/fan-in.
- - Workspace: non-existent, spaces, symlinks, path traversal.
- - Clock: skew, pre-1970 timestamps, zero-duration runs.
- - Filesystem: HERMES_HOME with spaces / unicode / symlinks.
- - Scale extremes: 100k tasks, 10k runs per task, huge bodies.
- - Concurrency: idempotency-key race across processes.
- - Hostile: path traversal attempts, injection attempts.
-
-Each scenario is self-contained. Failures are collected and printed
-together at the end. Script exits 0 iff every scenario passed or was
-cleanly SKIPPED (with reason).
-"""
-
-import multiprocessing as mp
-import os
-import shutil
-import sqlite3
-import subprocess
-import sys
-import tempfile
-import time
-from pathlib import Path
-
-# Resolve the worktree path robustly.
-_THIS = Path(__file__).resolve()
-WT = _THIS.parents[2] if _THIS.parent.name == "stress" else Path.cwd()
-
-FAILURES: list[str] = []
-SKIPS: list[str] = []
-_REGISTERED: list = []
-
-
-def scenario(name):
- """Decorator: run `fn` in its own HERMES_HOME, collect failures.
-
- The returned function is named `_scenario_<name>` so discovery can
- find it in globals() reliably.
- """
- def wrap(fn):
- def run():
- home = tempfile.mkdtemp(prefix=f"hermes_atyp_{name}_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- for m in list(sys.modules.keys()):
- if m.startswith(("hermes_cli", "plugins", "gateway")):
- del sys.modules[m]
- sys.path.insert(0, str(WT))
- from hermes_cli import kanban_db as kb # noqa: F401
- print(f"\n═══ {name} ═══")
- try:
- fn(home, kb)
- print(f" ✔ {name}")
- except AssertionError as e:
- msg = f"{name}: {e}"
- FAILURES.append(msg)
- print(f" ✗ FAIL: {e}")
- except Exception as e:
- msg = f"{name}: unexpected {type(e).__name__}: {e}"
- FAILURES.append(msg)
- import traceback
- traceback.print_exc()
- print(f" ✗ ERROR: {msg}")
- finally:
- try:
- shutil.rmtree(home)
- except Exception:
- pass
- run.__name__ = f"_scenario_{name}"
- # Register in a module-level list so discovery is trivial.
- _REGISTERED.append(run)
- return run
- return wrap
-
-
-# =============================================================================
-# DATA WEIRDNESS
-# =============================================================================
-
-@scenario("unicode_and_emoji")
-def _(home, kb):
- kb.init_db()
- conn = kb.connect()
- try:
- # Emoji, CJK, RTL, zero-width joiner
- cases = [
- ("📋 buy groceries 🍎", "shopping"),
- ("设计认证模式", "implement"),
- ("אימות משתמש חדש", "auth-rtl"), # Hebrew RTL
- ("مهمة تصحيح الأخطاء", "bug-arabic"),
- ("👨‍👩‍👧‍👦 family emoji ZWJ sequences 🏳️‍🌈", "emoji-stress"),
- ("control\x01chars\x02in\x03body", "ctrl"),
- ("null\x00bytes", "nullbyte"),
- ]
- for title, kind in cases:
- tid = kb.create_task(conn, title=title, assignee="w")
- back = kb.get_task(conn, tid)
- assert back.title == title, (
- f"[{kind}] round-trip mismatch: {title!r} → {back.title!r}"
- )
- print(f" {len(cases)} unicode titles round-tripped")
-
- # Metadata with non-ASCII + emoji
- tid = kb.create_task(conn, title="with meta", assignee="w")
- kb.claim_task(conn, tid)
- meta = {
- "作者": "张三",
- "summary_fr": "résumé avec des caractères accentués",
- "emoji": "🎉🔥💯",
- "mixed_list": ["normal", "日本語", "🇺🇸"],
- }
- kb.complete_task(
- conn, tid,
- summary="完成了 📝 résumé",
- metadata=meta,
- )
- run = kb.latest_run(conn, tid)
- assert run.summary == "完成了 📝 résumé", "summary round-trip failed"
- assert run.metadata == meta, (
- f"metadata round-trip failed: {run.metadata} != {meta}"
- )
- print(" metadata with CJK + emoji round-tripped")
- finally:
- conn.close()
-
-
-@scenario("huge_strings")
-def _(home, kb):
- """1MB body + 1MB summary + deeply nested metadata."""
- kb.init_db()
- conn = kb.connect()
- try:
- huge_body = "x" * (1024 * 1024) # 1 MB
- huge_summary = "y" * (1024 * 1024)
- # Nested metadata: 50 levels deep
- meta = "leaf"
- for _ in range(50):
- meta = {"nested": meta}
- tid = kb.create_task(
- conn, title="huge task", body=huge_body, assignee="w",
- )
- kb.claim_task(conn, tid)
- kb.complete_task(conn, tid, summary=huge_summary, metadata=meta)
-
- back = kb.get_task(conn, tid)
- assert back.body == huge_body, f"body truncated: {len(back.body)} vs {len(huge_body)}"
- run = kb.latest_run(conn, tid)
- assert run.summary == huge_summary
- assert run.metadata == meta
- print(" 1 MB body + 1 MB summary + 50-deep metadata OK")
- finally:
- conn.close()
-
-
-@scenario("sql_injection_attempts")
-def _(home, kb):
- """SQLite parameterized queries should neutralize all of these, but
- verify empirically across every string field."""
- kb.init_db()
- conn = kb.connect()
- try:
- payloads = [
- "'; DROP TABLE tasks; --",
- "\" OR 1=1 --",
- "'; DELETE FROM task_runs; --",
- "Robert'); DROP TABLE students;--", # Little Bobby Tables
- "\\x00\\x01\\x02",
- "' UNION SELECT * FROM kanban_notify_subs --",
- ]
- for p in payloads:
- tid = kb.create_task(
- conn, title=p, body=p, assignee=p, tenant=p,
- )
- back = kb.get_task(conn, tid)
- assert back.title == p
- assert back.body == p
- # Kernel should have stored, not executed
- # Verify tasks table still has rows
- count = conn.execute("SELECT COUNT(*) FROM tasks").fetchone()[0]
- assert count == len(payloads), f"lost rows: {count} vs {len(payloads)}"
- # tasks table wasn't dropped (we're still here)
- print(f" {len(payloads)} injection payloads neutralized")
- finally:
- conn.close()
-
-
-@scenario("newlines_in_summary")
-def _(home, kb):
- """Summaries with newlines, tabs, and shell metachars.
-
- The notifier truncates to first line — verify that's right, not
- that the kernel loses data."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="multiline", assignee="w")
- kb.claim_task(conn, tid)
- multi = "line 1\nline 2\tindented\n\nline 4"
- kb.complete_task(conn, tid, summary=multi)
- run = kb.latest_run(conn, tid)
- assert run.summary == multi, "full summary should survive in kernel"
- # Event payload takes first line (for notifier brevity)
- events = [e for e in kb.list_events(conn, tid) if e.kind == "completed"]
- assert events[0].payload["summary"] == "line 1", (
- f"event payload should be first line, got {events[0].payload['summary']!r}"
- )
- print(" multiline summary preserved on run; first line in event")
- finally:
- conn.close()
-
-
-@scenario("malformed_metadata_via_cli")
-def _(home, kb):
- """CLI rejects malformed JSON and non-dict JSON cleanly."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="meta test", assignee="w")
- kb.claim_task(conn, tid)
- finally:
- conn.close()
-
- env = {**os.environ, "PYTHONPATH": str(WT), "HERMES_HOME": home, "HOME": home}
- bad_metas = [
- "not-json",
- "[1, 2, 3]", # array not dict
- "42", # scalar
- '{"unclosed', # truncated
- ]
- for bad in bad_metas:
- r = subprocess.run(
- [sys.executable, "-m", "hermes_cli.main", "kanban",
- "complete", tid, "--metadata", bad],
- capture_output=True, text=True, env=env,
- )
- # Should print an error to stderr, exit non-zero, not touch the task
- assert "metadata" in r.stderr.lower() or "json" in r.stderr.lower(), (
- f"bad metadata {bad!r} didn't produce a metadata error: "
- f"stderr={r.stderr!r}"
- )
- # Verify task is still running (no partial apply)
- conn = kb.connect()
- try:
- assert kb.get_task(conn, tid).status == "running"
- finally:
- conn.close()
- print(f" {len(bad_metas)} malformed --metadata values cleanly rejected")
-
-
-# =============================================================================
-# DEPENDENCY GRAPH PATHOLOGIES
-# =============================================================================
-
-@scenario("dependency_cycle")
-def _(home, kb):
- """A → B → A should be refused. If it's allowed, recompute_ready
- could infinite-loop or never promote."""
- kb.init_db()
- conn = kb.connect()
- try:
- a = kb.create_task(conn, title="A", assignee="w")
- b = kb.create_task(conn, title="B", assignee="w", parents=[a])
- # Try to link A back to B — creating the cycle
- try:
- kb.link_tasks(conn, parent_id=b, child_id=a)
- # If that didn't raise, the kernel allowed a cycle.
- # Verify recompute_ready at least doesn't hang.
- import threading
- done = threading.Event()
- result = []
- def run():
- try:
- result.append(kb.recompute_ready(conn))
- except Exception as e:
- result.append(e)
- done.set()
- t = threading.Thread(target=run, daemon=True)
- t.start()
- done.wait(timeout=5)
- if not done.is_set():
- assert False, "recompute_ready HUNG on cyclic graph"
- raise AssertionError(
- "cycle creation was allowed; kernel should reject"
- )
- except (ValueError, RuntimeError, sqlite3.IntegrityError) as e:
- # Expected: kernel refuses the cycle
- print(f" cycle correctly rejected: {e}")
- finally:
- conn.close()
-
-
-@scenario("self_parent")
-def _(home, kb):
- """A task cannot be its own parent."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="self", assignee="w")
- try:
- kb.link_tasks(conn, parent_id=tid, child_id=tid)
- raise AssertionError("self-parenting should be rejected")
- except (ValueError, RuntimeError, sqlite3.IntegrityError) as e:
- print(f" self-parent rejected: {e}")
- finally:
- conn.close()
-
-
-@scenario("diamond_dependency")
-def _(home, kb):
- """Root → (A, B) → leaf. Leaf should promote to ready only when
- BOTH A and B are done."""
- kb.init_db()
- conn = kb.connect()
- try:
- root = kb.create_task(conn, title="root", assignee="w")
- kb.claim_task(conn, root)
- kb.complete_task(conn, root, result="ready")
- a = kb.create_task(conn, title="A", assignee="w", parents=[root])
- b = kb.create_task(conn, title="B", assignee="w", parents=[root])
- leaf = kb.create_task(conn, title="leaf", assignee="w", parents=[a, b])
-
- # A done but B not → leaf stays todo
- kb.claim_task(conn, a)
- kb.complete_task(conn, a, result="a done")
- kb.recompute_ready(conn)
- assert kb.get_task(conn, leaf).status == "todo", (
- f"leaf should still be todo with B unfinished, got "
- f"{kb.get_task(conn, leaf).status}"
- )
- # Both done → leaf promotes
- kb.claim_task(conn, b)
- kb.complete_task(conn, b, result="b done")
- kb.recompute_ready(conn)
- assert kb.get_task(conn, leaf).status == "ready", (
- f"leaf should promote with both parents done, got "
- f"{kb.get_task(conn, leaf).status}"
- )
- print(" diamond dependency resolved correctly")
- finally:
- conn.close()
-
-
-@scenario("wide_fan_out")
-def _(home, kb):
- """One parent, 500 children. Completing the parent should promote
- all 500 in its own recompute_ready pass (triggered by complete_task).
- """
- kb.init_db()
- conn = kb.connect()
- try:
- parent = kb.create_task(conn, title="root", assignee="w")
- children = [
- kb.create_task(conn, title=f"c{i}", assignee="w", parents=[parent])
- for i in range(500)
- ]
- kb.claim_task(conn, parent)
- t0 = time.monotonic()
- kb.complete_task(conn, parent, result="done")
- elapsed = (time.monotonic() - t0) * 1000
- # complete_task calls recompute_ready internally; check result.
- ready_count = conn.execute(
- "SELECT COUNT(*) FROM tasks WHERE status='ready' AND id != ?",
- (parent,),
- ).fetchone()[0]
- assert ready_count == 500, f"expected 500 promoted, got {ready_count}"
- for cid in children[:5]:
- assert kb.get_task(conn, cid).status == "ready"
- print(f" 500 children promoted in {elapsed:.0f}ms (via complete_task)")
- finally:
- conn.close()
-
-
-@scenario("wide_fan_in")
-def _(home, kb):
- """500 parents, 1 child. Child should not promote until all 500 done."""
- kb.init_db()
- conn = kb.connect()
- try:
- parents = [
- kb.create_task(conn, title=f"p{i}", assignee="w") for i in range(500)
- ]
- child = kb.create_task(
- conn, title="leaf", assignee="w", parents=parents,
- )
- # Complete 499 parents
- for p in parents[:-1]:
- kb.claim_task(conn, p)
- kb.complete_task(conn, p)
- kb.recompute_ready(conn)
- assert kb.get_task(conn, child).status == "todo", (
- "child should still be todo with 1/500 parents incomplete"
- )
- # Finish the last one
- kb.claim_task(conn, parents[-1])
- kb.complete_task(conn, parents[-1])
- kb.recompute_ready(conn)
- assert kb.get_task(conn, child).status == "ready"
- print(" 500 parents → 1 child promotion works")
- finally:
- conn.close()
-
-
-# =============================================================================
-# WORKSPACE EDGE CASES
-# =============================================================================
-
-@scenario("workspace_path_traversal")
-def _(home, kb):
- """`workspace_path='../../../etc/passwd'` or absolute-outside-home
- should not be silently accepted and then executed in the wrong place."""
- kb.init_db()
- conn = kb.connect()
- try:
- # Direct kernel API — create with an attacker-ish path
- tid = kb.create_task(
- conn, title="path-traversal",
- assignee="w",
- workspace_kind="dir",
- workspace_path="../../../tmp/attacker",
- )
- task = kb.get_task(conn, tid)
- # Document what actually happens — is the path stored verbatim?
- # Is it resolved? Is it rejected?
- print(f" stored workspace_path: {task.workspace_path!r}")
- print(f" workspace_kind: {task.workspace_kind!r}")
- # Verify resolve_workspace (which the dispatcher calls) doesn't
- # allow escape.
- try:
- from hermes_cli.kanban_db import resolve_workspace
- resolved = resolve_workspace(task)
- # If resolve succeeded, check it's actually escape-safe.
- resolved_abs = str(Path(resolved).resolve())
- home_abs = str(Path(os.environ["HERMES_HOME"]).resolve())
- if not resolved_abs.startswith(home_abs) and resolved_abs.startswith("/tmp"):
- # This is escaping the home dir. Whether that's actually
- # a problem depends on the threat model. Flag for attention.
- print(f" ⚠ workspace resolved OUTSIDE hermes_home: {resolved}")
- print(" (not necessarily a bug — dir: workspaces are intentionally arbitrary, but worth documenting)")
- except Exception as e:
- print(f" resolve_workspace rejected: {e}")
- finally:
- conn.close()
-
-
-@scenario("workspace_nonexistent_path")
-def _(home, kb):
- """Dispatching a task whose workspace can't be resolved should go
- through the spawn-failure circuit breaker, not crash."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(
- conn, title="bad-workspace", assignee="w",
- workspace_kind="dir",
- workspace_path="/nonexistent/path/that/does/not/exist",
- )
- # Run dispatch_once with a dummy spawn_fn
- result = kb.dispatch_once(conn, spawn_fn=lambda *_: 99999)
- # If the path was rejected, the task went through _record_spawn_failure
- task = kb.get_task(conn, tid)
- # Possible outcomes:
- # - Task back in ready (workspace issue = spawn_failed, retries)
- # - Task in running (kernel accepted the bogus path and spawned)
- # - Task auto-blocked (after N retries, but we only ran 1 tick)
- print(f" after 1 tick with nonexistent workspace: status={task.status}")
- if task.status == "ready":
- # Expected path: workspace failure led to release
- spawn_failures = task.spawn_failures
- print(f" spawn_failures counter: {spawn_failures}")
- assert spawn_failures >= 1, "spawn_failures counter didn't increment"
- elif task.status == "running":
- # Workspace not checked before spawn — the worker would hit
- # the bad path itself. Defensible for `dir:` workspaces that
- # the user might create later.
- print(" kernel accepted bogus path (deferred check to worker)")
- finally:
- conn.close()
-
-
-# =============================================================================
-# CLOCK SKEW
-# =============================================================================
-
-@scenario("clock_skew_start_greater_than_end")
-def _(home, kb):
- """NTP jumps backward. Run.started_at gets written as 1234 but by
- the time complete_task runs, time.time() returned 1230. A human
- reading run history sees negative elapsed."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="time-travel", assignee="w")
- kb.claim_task(conn, tid)
- # Force a future started_at via raw SQL
- future = int(time.time()) + 3600
- conn.execute(
- "UPDATE task_runs SET started_at = ? WHERE task_id = ?",
- (future, tid),
- )
- conn.commit()
- # Complete normally — ended_at will be now, < started_at
- kb.complete_task(conn, tid, summary="time-skewed")
- run = kb.latest_run(conn, tid)
- # Invariant I5 (from property fuzzer): started_at <= ended_at
- # when ended_at is set. Verify this is enforced OR gracefully
- # handled in display.
- if run.ended_at < run.started_at:
- # Kernel didn't reject the write; check that CLI display
- # doesn't produce "-1800s" elapsed.
- elapsed = run.ended_at - run.started_at
- print(f" clock-skewed run: elapsed = {elapsed}s (negative)")
- print(" ⚠ kernel stores this; UI should clamp to 0 or handle")
- # Don't fail — document the behavior.
- else:
- print(" kernel normalized ended_at >= started_at")
- finally:
- conn.close()
-
-
-# =============================================================================
-# FILESYSTEM WEIRDNESS
-# =============================================================================
-
-@scenario("hermes_home_with_spaces")
-def _(home, kb):
- """HERMES_HOME at a path with spaces — should work but catches
- anyone doing string interpolation without quoting."""
- # Note: home was already created with a safe prefix. We need to
- # reset to a weird one for this test.
- weird = tempfile.mkdtemp(prefix="hermes with spaces ")
- os.environ["HERMES_HOME"] = weird
- os.environ["HOME"] = weird
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="spaced", assignee="w")
- kb.claim_task(conn, tid)
- kb.complete_task(conn, tid, summary="path has spaces")
- runs = kb.list_runs(conn, tid)
- assert len(runs) == 1 and runs[0].outcome == "completed"
- # Verify the DB file is actually in the weird path
- db_path = Path(weird) / "kanban.db"
- assert db_path.exists(), f"DB not at {db_path}"
- print(f" HERMES_HOME with spaces: OK at {weird}")
- finally:
- conn.close()
- shutil.rmtree(weird, ignore_errors=True)
-
-
-@scenario("hermes_home_with_unicode")
-def _(home, kb):
- """HERMES_HOME with non-ASCII chars."""
- # Pre-create directly since tempfile doesn't love unicode prefixes
- weird = f"/tmp/hermes_héllo_émöji_{os.getpid()}"
- os.makedirs(weird, exist_ok=True)
- os.environ["HERMES_HOME"] = weird
- os.environ["HOME"] = weird
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="unicode home", assignee="w")
- kb.claim_task(conn, tid)
- kb.complete_task(conn, tid, summary="ok")
- assert (Path(weird) / "kanban.db").exists()
- print(f" HERMES_HOME with unicode path: OK at {weird}")
- finally:
- conn.close()
- shutil.rmtree(weird, ignore_errors=True)
-
-
-@scenario("hermes_home_via_symlink")
-def _(home, kb):
- """HERMES_HOME is a symlink to the real dir. _INITIALIZED_PATHS
- uses Path.resolve() — two different symlink names pointing at the
- same dir should NOT double-init."""
- real = tempfile.mkdtemp(prefix="hermes_real_")
- link1 = real + "_link1"
- link2 = real + "_link2"
- os.symlink(real, link1)
- os.symlink(real, link2)
- try:
- os.environ["HERMES_HOME"] = link1
- os.environ["HOME"] = link1
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn1 = kb.connect()
- kb.create_task(conn1, title="t1", assignee="w")
- conn1.close()
-
- # Switch to link2 pointing at the same dir
- os.environ["HERMES_HOME"] = link2
- os.environ["HOME"] = link2
- conn2 = kb.connect()
- # Should see the task we created via link1
- all_tasks = kb.list_tasks(conn2)
- assert len(all_tasks) == 1, (
- f"symlinks to same dir should share DB, got {len(all_tasks)} tasks"
- )
- conn2.close()
- print(" symlinks to same HERMES_HOME share DB correctly")
- finally:
- for p in (link1, link2):
- try:
- os.remove(p)
- except OSError:
- pass
- shutil.rmtree(real, ignore_errors=True)
-
-
-# =============================================================================
-# SCALE EXTREMES
-# =============================================================================
-
-@scenario("huge_run_count_on_one_task")
-def _(home, kb):
- """1000 reclaim cycles on a single task → 1000 run rows. Verify
- list_runs still performs, and build_worker_context isn't quadratic."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="retry-heavy", assignee="w")
- # Force reclaims by manually closing runs
- for i in range(1000):
- kb.claim_task(conn, tid)
- # Force close the run directly so we can make another claim
- rid = kb.latest_run(conn, tid).id
- kb._end_run(conn, tid, outcome="reclaimed", summary=f"attempt {i}")
- conn.execute(
- "UPDATE tasks SET status='ready', claim_lock=NULL, "
- "claim_expires=NULL WHERE id=?", (tid,),
- )
- conn.commit()
- runs = kb.list_runs(conn, tid)
- assert len(runs) == 1000, f"expected 1000 runs, got {len(runs)}"
- # build_worker_context should NOT take forever
- t0 = time.monotonic()
- ctx = kb.build_worker_context(conn, tid)
- elapsed = (time.monotonic() - t0) * 1000
- # The "Prior attempts" section renders ALL closed runs.
- # For 1000 runs this could produce a massive string.
- # Fair question: is this bounded? Let's measure.
- print(f" 1000 runs → list_runs OK; build_worker_context = {elapsed:.0f}ms, {len(ctx)} chars")
- if len(ctx) > 200_000:
- print(f" ⚠ build_worker_context unbounded on retry-heavy tasks "
- f"({len(ctx)} chars) — worker context will be huge")
- finally:
- conn.close()
-
-
-@scenario("hundred_tenants")
-def _(home, kb):
- """100 distinct tenants with 50 tasks each. board_stats + list_tasks
- should still return quickly."""
- kb.init_db()
- conn = kb.connect()
- try:
- for t in range(100):
- for i in range(50):
- kb.create_task(
- conn, title=f"tenant-{t}-task-{i}",
- tenant=f"tenant_{t:03d}",
- assignee="w",
- )
- t0 = time.monotonic()
- stats = kb.board_stats(conn)
- el_stats = (time.monotonic() - t0) * 1000
- t0 = time.monotonic()
- tasks = kb.list_tasks(conn)
- el_list = (time.monotonic() - t0) * 1000
- print(f" 5000 tasks / 100 tenants: stats={el_stats:.0f}ms, list={el_list:.0f}ms")
- assert len(tasks) == 5000
- finally:
- conn.close()
-
-
-# =============================================================================
-# CONCURRENCY CORNERS
-# =============================================================================
-
-def _idempotency_race_worker(hermes_home: str, key: str, result_file: str,
- barrier_path: str) -> None:
- """Subprocess body for the idempotency race test."""
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, str(WT))
- from hermes_cli import kanban_db as kb
-
- # Spin until the barrier file exists (crude sync across processes)
- while not os.path.exists(barrier_path):
- time.sleep(0.001)
-
- conn = kb.connect()
- try:
- tid = kb.create_task(
- conn, title=f"race pid={os.getpid()}",
- assignee="w", idempotency_key=key,
- )
- finally:
- conn.close()
- with open(result_file, "w", encoding="utf-8") as f:
- f.write(tid)
-
-
-@scenario("idempotency_key_race")
-def _(home, kb):
- """Two processes concurrently call create_task with the same
- idempotency_key — should both get back the SAME task id, not two
- different ones."""
- kb.init_db()
- # Spawn workers, then drop the barrier so they fire ~simultaneously.
- key = "race-key-12345"
- barrier = os.path.join(home, "barrier")
- results = [os.path.join(home, f"res_{i}") for i in range(2)]
- ctx = mp.get_context("spawn")
- procs = [
- ctx.Process(
- target=_idempotency_race_worker,
- args=(home, key, results[i], barrier),
- )
- for i in range(2)
- ]
- for p in procs:
- p.start()
- time.sleep(0.1) # let them hit the spin
- # Fire the gun
- with open(barrier, "w", encoding="utf-8") as f:
- f.write("go")
- for p in procs:
- p.join(timeout=10)
-
- tids = [
- Path(r).read_text(encoding="utf-8").strip()
- for r in results
- if os.path.exists(r)
- ]
- assert len(tids) == 2, f"only {len(tids)} workers finished"
- assert tids[0] == tids[1], (
- f"idempotency key race produced two different tasks: {tids}"
- )
- # Also verify there's only ONE row in the DB
- conn = kb.connect()
- try:
- count = conn.execute(
- "SELECT COUNT(*) FROM tasks WHERE idempotency_key = ?",
- (key,),
- ).fetchone()[0]
- assert count == 1, f"expected 1 task with key, got {count}"
- finally:
- conn.close()
- print(f" idempotency race: both workers got {tids[0]}")
-
-
-
-# =============================================================================
-# MORE EDGE CASES
-# =============================================================================
-
-@scenario("assignee_with_special_chars")
-def _(home, kb):
- """Profile names can contain @-signs, dots, hyphens. Some users
- might try nonsense. Kernel shouldn't break on any of them."""
- kb.init_db()
- conn = kb.connect()
- try:
- assignees = [
- "normal-dev",
- "dev.with.dots",
- "backend@v2",
- "日本語-dev",
- "🤖-bot",
- "x" * 200, # very long
- "", # empty string
- ]
- for a in assignees:
- tid = kb.create_task(conn, title=f"for {a!r}", assignee=a or None)
- back = kb.get_task(conn, tid)
- # Empty string is coerced to None by kernel, or stored verbatim?
- if a:
- assert back.assignee == a, f"assignee round-trip: {a!r} → {back.assignee!r}"
- print(f" {len(assignees)} weird assignee names round-tripped")
- finally:
- conn.close()
-
-
-@scenario("completed_task_reclaim_attempt")
-def _(home, kb):
- """A task in 'done' should NOT be reclaimable — reclaim/claim paths
- must refuse."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="terminal", assignee="w")
- kb.claim_task(conn, tid)
- kb.complete_task(conn, tid, summary="all done")
- # Try to re-claim a done task
- claimed = kb.claim_task(conn, tid)
- assert claimed is None, "done task should not be claimable"
- # Try to complete it again
- ok = kb.complete_task(conn, tid, summary="oops twice")
- assert ok is False, "completing an already-done task should refuse"
- # Try to block it
- ok = kb.block_task(conn, tid, reason="trying")
- assert ok is False, "blocking a done task should refuse"
- print(" done task correctly resists re-claim/complete/block")
- finally:
- conn.close()
-
-
-@scenario("archived_task_resurrection_attempt")
-def _(home, kb):
- """An archived task should be invisible to normal ops."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="archive-me", assignee="w")
- kb.archive_task(conn, tid)
- # Archived task shouldn't appear in default list
- tasks = kb.list_tasks(conn)
- assert all(t.id != tid for t in tasks), "archived task leaked into default list"
- # But it should still exist in the DB
- row = conn.execute("SELECT status FROM tasks WHERE id = ?", (tid,)).fetchone()
- assert row is not None
- assert row["status"] == "archived"
- # Trying to claim an archived task: should refuse
- claimed = kb.claim_task(conn, tid)
- assert claimed is None, "archived task should not be claimable"
- # Archived can be un-archived via direct status? No API for that intentionally
- # (archive is meant to be terminal). Verify this.
- # complete/block/unblock on archived should all refuse.
- assert kb.complete_task(conn, tid) is False
- assert kb.block_task(conn, tid, reason="no") is False
- assert kb.unblock_task(conn, tid) is False
- print(" archived task cannot be resurrected via normal APIs")
- finally:
- conn.close()
-
-
-@scenario("unassigned_task_never_claims")
-def _(home, kb):
- """Task without an assignee should never be claimed by dispatch_once,
- even though its status might be 'ready' if it has no parents."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="orphan", assignee=None)
- assert kb.get_task(conn, tid).status == "ready"
- result = kb.dispatch_once(conn, spawn_fn=lambda *_: 42)
- assert tid in result.skipped_unassigned
- assert len(result.spawned) == 0
- # Task should still be ready, untouched
- assert kb.get_task(conn, tid).status == "ready"
- print(" unassigned ready task correctly skipped by dispatcher")
- finally:
- conn.close()
-
-
-@scenario("comment_storm")
-def _(home, kb):
- """1000 comments on a single task — build_worker_context should still
- be reasonable."""
- kb.init_db()
- conn = kb.connect()
- try:
- tid = kb.create_task(conn, title="chatty", assignee="w")
- for i in range(1000):
- kb.add_comment(conn, tid, author=f"user{i % 5}", body=f"comment number {i}")
- comments = kb.list_comments(conn, tid)
- assert len(comments) == 1000
- t0 = time.monotonic()
- ctx = kb.build_worker_context(conn, tid)
- elapsed = (time.monotonic() - t0) * 1000
- print(f" 1000 comments: list in {elapsed:.0f}ms, context size = {len(ctx)} chars")
- if len(ctx) > 200_000:
- print(" ⚠ comment thread unbounded in worker context")
- finally:
- conn.close()
-
-
-@scenario("empty_string_fields")
-def _(home, kb):
- """Empty title should be rejected (we already do this). Empty body,
- empty summary, etc. should be accepted."""
- kb.init_db()
- conn = kb.connect()
- try:
- # Empty title → reject
- try:
- kb.create_task(conn, title="", assignee="w")
- raise AssertionError("empty title should have been rejected")
- except ValueError:
- pass
- # Whitespace-only title → reject
- try:
- kb.create_task(conn, title=" \t\n ", assignee="w")
- raise AssertionError("whitespace-only title should have been rejected")
- except ValueError:
- pass
- # Empty body → accept (legitimate: just title says it all)
- tid = kb.create_task(conn, title="empty body ok", body="", assignee="w")
- assert kb.get_task(conn, tid).body in {"", None}
- # Empty summary on complete → accept
- kb.claim_task(conn, tid)
- kb.complete_task(conn, tid, summary="")
- run = kb.latest_run(conn, tid)
- # Empty summary falls back to result; both empty → None on run
- print(" empty body accepted, empty-title rejected")
- finally:
- conn.close()
-
-
-@scenario("tenant_with_newlines")
-def _(home, kb):
- """Someone pastes a multi-line string into --tenant. Kernel should
- store what it gets — but queries filtering by tenant should still
- work against the raw value."""
- kb.init_db()
- conn = kb.connect()
- try:
- weird_tenant = "line1\nline2\tindented"
- tid = kb.create_task(conn, title="weird tenant", assignee="w", tenant=weird_tenant)
- back = kb.get_task(conn, tid)
- assert back.tenant == weird_tenant
- # board_stats groups by tenant — verify it doesn't fall over
- stats = kb.board_stats(conn)
- print(" multiline tenant stored and stats still work")
- finally:
- conn.close()
-
-
-@scenario("parent_in_different_status_states")
-def _(home, kb):
- """recompute_ready promotes a todo child only if ALL parents are
- in 'done'. Verify against parents in every non-done state."""
- kb.init_db()
- conn = kb.connect()
- try:
- # Create one parent in each possible non-done state
- p_ready = kb.create_task(conn, title="p-ready", assignee="w")
- p_running = kb.create_task(conn, title="p-running", assignee="w")
- kb.claim_task(conn, p_running)
- p_blocked = kb.create_task(conn, title="p-blocked", assignee="w")
- kb.block_task(conn, p_blocked, reason="stuck")
- p_triage = kb.create_task(conn, title="p-triage", assignee="w", triage=True)
- p_archived = kb.create_task(conn, title="p-archived", assignee="w")
- kb.archive_task(conn, p_archived)
- p_done = kb.create_task(conn, title="p-done", assignee="w")
- kb.claim_task(conn, p_done)
- kb.complete_task(conn, p_done)
-
- # Child with just one parent, cycle it through each state
- for parent, expected in [
- (p_ready, "todo"), # parent not done → child stays todo
- (p_running, "todo"),
- (p_blocked, "todo"),
- (p_triage, "todo"),
- (p_archived, "todo"), # archived != done!
- (p_done, "ready"), # only done parent unblocks child
- ]:
- child = kb.create_task(
- conn, title=f"child-of-{parent}", assignee="w", parents=[parent],
- )
- kb.recompute_ready(conn)
- actual = kb.get_task(conn, child).status
- assert actual == expected, (
- f"child of {parent} ({kb.get_task(conn, parent).status}): "
- f"expected {expected}, got {actual}"
- )
- print(" child promotion correctly gated on parent.status == 'done'")
- finally:
- conn.close()
-
-
-@scenario("dashboard_rest_with_weird_inputs")
-def _(home, kb):
- """FastAPI TestClient POST /tasks with atypical JSON bodies."""
- kb.init_db()
- # Set a session token so the ws check doesnt bomb on import
- try:
- from hermes_cli import web_server as ws # noqa
- except Exception:
- pass
-
- from fastapi import FastAPI
- from fastapi.testclient import TestClient
- from plugins.kanban.dashboard.plugin_api import router as kanban_router
- app = FastAPI()
- app.include_router(kanban_router, prefix="/api/plugins/kanban")
- client = TestClient(app)
-
- # Empty title
- r = client.post("/api/plugins/kanban/tasks", json={"title": ""})
- assert r.status_code in {400, 422}, f"empty title should 4xx, got {r.status_code}"
-
- # Title only
- r = client.post("/api/plugins/kanban/tasks", json={"title": "x"})
- assert r.status_code == 200, r.text
-
- # Huge title
- r = client.post("/api/plugins/kanban/tasks", json={"title": "x" * 10000})
- # Should succeed — kernel doesn't cap title length
- assert r.status_code == 200
-
- # Unicode + emoji
- r = client.post("/api/plugins/kanban/tasks", json={
- "title": "📋 deploy 🚀 to 生产",
- "body": "日本語 body",
- "assignee": "deploy-bot",
- })
- assert r.status_code == 200
- tid = r.json()["task"]["id"]
- assert r.json()["task"]["title"] == "📋 deploy 🚀 to 生产"
-
- # Invalid JSON schema — unknown field, pydantic should either ignore or 422
- r = client.post("/api/plugins/kanban/tasks", json={
- "title": "fine", "nonexistent_field": "whatever",
- })
- assert r.status_code in {200, 422}
-
- # Priority as non-int
- r = client.post("/api/plugins/kanban/tasks", json={"title": "prio", "priority": "high"})
- assert r.status_code == 422, f"string priority should 422, got {r.status_code}"
-
- # PATCH with empty body (no changes requested)
- r = client.patch(f"/api/plugins/kanban/tasks/{tid}", json={})
- # Accept either success-no-op or 400
- assert r.status_code in {200, 400}
- print(" dashboard REST handles weird inputs correctly")
-
-# =============================================================================
-# RUN ALL
-# =============================================================================
-
-def main():
- print(f"Running {len(_REGISTERED)} atypical-scenario tests...")
- for fn in _REGISTERED:
- fn()
-
- print()
- print("=" * 60)
- print("SUMMARY")
- print("=" * 60)
- print(f" Ran: {len(_REGISTERED)}")
- print(f" Failures: {len(FAILURES)}")
- print(f" Skips: {len(SKIPS)}")
- if FAILURES:
- print()
- for f in FAILURES:
- print(f" ✗ {f}")
- sys.exit(1)
- else:
- print("\n✔ ALL ATYPICAL SCENARIOS HANDLED CORRECTLY")
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_benchmarks.py b/tests/stress/test_benchmarks.py
deleted file mode 100644
index e092ed0fcc7..00000000000
--- a/tests/stress/test_benchmarks.py
+++ /dev/null
@@ -1,221 +0,0 @@
-"""Scale benchmarks for the Kanban kernel.
-
-Measures:
- - dispatch_once latency at 100, 1000, 10000 tasks
- - recompute_ready latency at 100, 1000, 10000 todo tasks with wide parent graphs
- - build_worker_context latency with 1, 10, 50 parent dependencies
- - board list/stats query latency
- - task_runs query latency at scale
-
-Results printed as a table. Saved to JSON for regression-diffing in CI
-or future reviews. Not a pass/fail test — records numbers so we know
-when a change regresses latency by 10x and can decide whether to care.
-"""
-
-import json
-import os
-import random
-import sys
-import tempfile
-import time
-from pathlib import Path
-
-WT = str(Path(__file__).resolve().parents[2])
-
-
-def bench(label, fn, iterations=5):
- """Time fn over `iterations` runs, return (min, median, max) in ms."""
- times = []
- for _ in range(iterations):
- t0 = time.perf_counter()
- fn()
- times.append((time.perf_counter() - t0) * 1000)
- times.sort()
- mn = times[0]
- md = times[len(times) // 2]
- mx = times[-1]
- return {"label": label, "iter": iterations, "min_ms": mn, "median_ms": md, "max_ms": mx}
-
-
-def seed_tasks(conn, kb, n, assignee="bench-worker", with_parents=False):
- """Seed n tasks. Optionally give each task 5 parents."""
- ids = []
- for i in range(n):
- if with_parents and i >= 5:
- parents = random.sample(ids[:i], 5)
- else:
- parents = ()
- tid = kb.create_task(
- conn, title=f"bench {i}", assignee=assignee,
- tenant="bench", parents=parents,
- )
- ids.append(tid)
- return ids
-
-
-def main():
- home = tempfile.mkdtemp(prefix="hermes_bench_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
-
- results = []
-
- # ============ dispatch_once latency ============
- for n in [100, 1000, 10000]:
- print(f"\n== dispatch_once @ {n} tasks ==")
- # Fresh DB each time so we're not measuring cumulative effects
- import shutil
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- seed_tasks(conn, kb, n, assignee=None) # no assignee → won't spawn
- r = bench(
- f"dispatch_once (n={n}, no spawn)",
- lambda: kb.dispatch_once(conn, spawn_fn=lambda *_: None),
- iterations=5,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["n"] = n
- results.append(r)
- conn.close()
-
- # ============ recompute_ready at scale with parent graphs ============
- for n in [100, 1000, 10000]:
- print(f"\n== recompute_ready @ {n} tasks (5 parents each) ==")
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- ids = seed_tasks(conn, kb, n, assignee=None, with_parents=True)
- # Complete the first 100 so some todo tasks might get promoted
- for tid in ids[:min(100, n // 10)]:
- kb.complete_task(conn, tid, result="bench")
- r = bench(
- f"recompute_ready (n={n}, with parents)",
- lambda: kb.recompute_ready(conn),
- iterations=5,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["n"] = n
- results.append(r)
- conn.close()
-
- # ============ build_worker_context with N parents ============
- for parent_count in [1, 10, 50]:
- print(f"\n== build_worker_context with {parent_count} parents ==")
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- # Create parents, complete them with summaries+metadata
- parent_ids = []
- for i in range(parent_count):
- pid = kb.create_task(conn, title=f"parent {i}", assignee="p")
- kb.claim_task(conn, pid)
- kb.complete_task(
- conn, pid,
- summary=f"parent {i} result that is longer than a single token "
- f"so we actually measure the IO",
- metadata={"files": [f"file_{j}.py" for j in range(5)], "i": i},
- )
- parent_ids.append(pid)
- child_id = kb.create_task(
- conn, title="child", assignee="c", parents=parent_ids,
- )
- r = bench(
- f"build_worker_context (parents={parent_count})",
- lambda: kb.build_worker_context(conn, child_id),
- iterations=10,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["parent_count"] = parent_count
- results.append(r)
- conn.close()
-
- # ============ list_tasks at scale ============
- for n in [100, 1000, 10000]:
- print(f"\n== list_tasks @ {n} ==")
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- seed_tasks(conn, kb, n)
- r = bench(
- f"list_tasks (n={n})",
- lambda: kb.list_tasks(conn),
- iterations=5,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["n"] = n
- results.append(r)
- conn.close()
-
- # ============ board_stats at scale ============
- for n in [100, 1000, 10000]:
- print(f"\n== board_stats @ {n} ==")
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- seed_tasks(conn, kb, n)
- r = bench(
- f"board_stats (n={n})",
- lambda: kb.board_stats(conn),
- iterations=5,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["n"] = n
- results.append(r)
- conn.close()
-
- # ============ list_runs at scale ============
- for n in [100, 1000]:
- print(f"\n== list_runs for task with {n} attempts ==")
- shutil.rmtree(home, ignore_errors=True)
- os.makedirs(home)
- kb._INITIALIZED_PATHS.clear()
- kb.init_db()
- conn = kb.connect()
- tid = kb.create_task(conn, title="x", assignee="w")
- # Create N attempts via claim/release
- for i in range(n):
- kb.claim_task(conn, tid, ttl_seconds=0)
- kb.release_stale_claims(conn)
- r = bench(
- f"list_runs (runs={n})",
- lambda: kb.list_runs(conn, tid),
- iterations=10,
- )
- print(f" min={r['min_ms']:.1f} median={r['median_ms']:.1f} max={r['max_ms']:.1f} ms")
- r["run_count"] = n
- results.append(r)
- conn.close()
-
- # ============ SUMMARY TABLE ============
- print()
- print("=" * 60)
- print("SUMMARY")
- print("=" * 60)
- print(f"{'Benchmark':<50} {'min':>8} {'median':>8} {'max':>8}")
- for r in results:
- print(f"{r['label']:<50} {r['min_ms']:>7.1f}ms {r['median_ms']:>7.1f}ms {r['max_ms']:>7.1f}ms")
-
- # Save for future diffing.
- out_path = "/tmp/kanban_bench_results.json"
- with open(out_path, "w") as f:
- json.dump(results, f, indent=2)
- print(f"\nResults saved to {out_path}")
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_concurrency.py b/tests/stress/test_concurrency.py
deleted file mode 100644
index 80d9183003a..00000000000
--- a/tests/stress/test_concurrency.py
+++ /dev/null
@@ -1,301 +0,0 @@
-"""Multi-process concurrency stress test for the Kanban kernel.
-
-5 worker processes race for claims on a shared DB with 100 tasks. Each
-worker loops: claim -> simulate work -> complete. Asserts the invariants
-that make the system worth building:
-
- - No task claimed by two workers simultaneously
- - No task completed twice
- - Every claim produces exactly one run row
- - Every completion closes exactly one run row
- - Zero SQLite locking errors that escape the retry layer
- - Total run count == total claim events == total completed events
-
-This test is the primary justification for WAL + CAS-based claim. If it
-passes, the architecture holds. If it fails, we have a real bug to fix
-before anyone runs this in anger.
-"""
-
-import json
-import multiprocessing as mp
-import os
-import random
-import sqlite3
-import sys
-import tempfile
-import time
-from pathlib import Path
-
-
-NUM_WORKERS = 5
-NUM_TASKS = 100
-WORKER_TIMEOUT_S = 60
-WT = str(Path(__file__).resolve().parents[2])
-
-
-def worker_loop(worker_id: int, hermes_home: str, result_file: str) -> None:
- """One worker's inner loop. Runs in a fresh Python process.
-
- Tries to claim a ready task, marks it done with a per-worker summary,
- repeats until the ready pool is empty. Records every claim + complete
- into its own JSON result file for later aggregation.
- """
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, WT)
-
- from hermes_cli import kanban_db as kb
-
- events = []
- empty_polls = 0
- start = time.monotonic()
-
- while time.monotonic() - start < WORKER_TIMEOUT_S:
- conn = kb.connect()
- try:
- # Find any ready task (non-deterministic order intentional — we
- # want workers to race on popular assignees).
- row = conn.execute(
- "SELECT id FROM tasks WHERE status = 'ready' "
- "AND claim_lock IS NULL LIMIT 1"
- ).fetchone()
- if row is None:
- empty_polls += 1
- if empty_polls > 20:
- break # queue empty long enough, stop
- time.sleep(0.01)
- continue
- empty_polls = 0
-
- tid = row["id"]
- try:
- claimed = kb.claim_task(
- conn, tid, claimer=f"worker-{worker_id}",
- )
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err_on_claim", "task": tid, "err": str(e)})
- continue
- if claimed is None:
- # Someone else beat us — expected contention, not an error.
- events.append({"kind": "lost_claim_race", "task": tid})
- continue
-
- run = kb.latest_run(conn, tid)
- events.append({
- "kind": "claimed",
- "task": tid,
- "worker": worker_id,
- "run_id": run.id,
- "t": time.monotonic() - start,
- })
-
- # Simulate short, variable work
- time.sleep(random.uniform(0.001, 0.05))
-
- try:
- kb.complete_task(
- conn, tid,
- result=f"done by worker-{worker_id}",
- summary=f"worker-{worker_id} finished task {tid}",
- metadata={"worker_id": worker_id, "run_id": run.id},
- )
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err_on_complete", "task": tid, "err": str(e)})
- continue
- events.append({
- "kind": "completed",
- "task": tid,
- "worker": worker_id,
- "run_id": run.id,
- "t": time.monotonic() - start,
- })
- finally:
- conn.close()
-
- with open(result_file, "w") as f:
- json.dump(events, f)
-
-
-def main():
- home = tempfile.mkdtemp(prefix="hermes_concurrency_")
- print(f"HERMES_HOME = {home}")
-
- # Seed.
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
- conn = kb.connect()
- tids = []
- for i in range(NUM_TASKS):
- tid = kb.create_task(
- conn, title=f"task #{i}", assignee="shared",
- tenant="concurrency-test",
- )
- tids.append(tid)
- conn.close()
- print(f"Seeded {NUM_TASKS} tasks.")
-
- # Spawn workers.
- ctx = mp.get_context("spawn")
- result_files = [f"/tmp/concurrency_worker_{i}.json" for i in range(NUM_WORKERS)]
- procs = []
- start = time.monotonic()
- for i in range(NUM_WORKERS):
- p = ctx.Process(target=worker_loop, args=(i, home, result_files[i]))
- p.start()
- procs.append(p)
-
- for p in procs:
- p.join(timeout=WORKER_TIMEOUT_S + 30)
- if p.is_alive():
- p.terminate()
- p.join()
-
- elapsed = time.monotonic() - start
- print(f"All workers done in {elapsed:.1f}s")
-
- # Aggregate worker events.
- all_events = []
- for i, f in enumerate(result_files):
- if not os.path.isfile(f):
- print(f" WORKER {i} produced no result file — died?")
- continue
- with open(f) as fh:
- events = json.load(fh)
- all_events.extend(events)
-
- # ============ INVARIANT CHECKS ============
- print()
- print("=" * 60)
- print("INVARIANT CHECKS")
- print("=" * 60)
-
- failures = []
-
- # Check 1: no task claimed by two different workers
- claims_by_task = {}
- for e in all_events:
- if e["kind"] == "claimed":
- if e["task"] in claims_by_task:
- prev = claims_by_task[e["task"]]
- if prev["worker"] != e["worker"]:
- failures.append(
- f"DOUBLE CLAIM: task {e['task']} claimed by "
- f"worker {prev['worker']} AND worker {e['worker']}"
- )
- claims_by_task[e["task"]] = e
-
- # Check 2: every completion has a matching claim from the same worker
- for e in all_events:
- if e["kind"] == "completed":
- prev_claim = claims_by_task.get(e["task"])
- if prev_claim is None:
- failures.append(f"COMPLETION WITHOUT CLAIM: task {e['task']}")
- elif prev_claim["worker"] != e["worker"]:
- failures.append(
- f"WORKER MISMATCH: task {e['task']} claimed by "
- f"{prev_claim['worker']} but completed by {e['worker']}"
- )
-
- # Check 3: DB state — every task should be in 'done', no dangling claims
- conn = kb.connect()
- try:
- bad_status = conn.execute(
- "SELECT id, status, claim_lock, current_run_id FROM tasks "
- "WHERE status != 'done' OR claim_lock IS NOT NULL "
- "OR current_run_id IS NOT NULL"
- ).fetchall()
- if bad_status:
- for row in bad_status:
- failures.append(
- f"BAD FINAL STATE: task {row['id']} status={row['status']} "
- f"claim_lock={row['claim_lock']} current_run_id={row['current_run_id']}"
- )
-
- # Check 4: exactly one run per task, all closed as completed
- bad_runs = conn.execute(
- "SELECT task_id, COUNT(*) as n FROM task_runs "
- "GROUP BY task_id HAVING n != 1"
- ).fetchall()
- if bad_runs:
- for row in bad_runs:
- failures.append(
- f"WRONG RUN COUNT: task {row['task_id']} has {row['n']} runs (expected 1)"
- )
-
- open_runs = conn.execute(
- "SELECT id, task_id FROM task_runs WHERE ended_at IS NULL"
- ).fetchall()
- for row in open_runs:
- failures.append(f"OPEN RUN: run {row['id']} on task {row['task_id']}")
-
- wrong_outcomes = conn.execute(
- "SELECT task_id, outcome FROM task_runs "
- "WHERE outcome IS NULL OR outcome != 'completed'"
- ).fetchall()
- for row in wrong_outcomes:
- failures.append(
- f"WRONG OUTCOME: task {row['task_id']} run outcome={row['outcome']}"
- )
-
- # Check 5: event counts — exactly NUM_TASKS completed events
- completed_events = conn.execute(
- "SELECT COUNT(*) as n FROM task_events WHERE kind='completed'"
- ).fetchone()["n"]
- if completed_events != NUM_TASKS:
- failures.append(
- f"EVENT COUNT MISMATCH: {completed_events} completed events "
- f"expected {NUM_TASKS}"
- )
-
- # Check 6: count SQLite errors that escaped retry
- sqlite_errs = sum(
- 1 for e in all_events if e["kind"].startswith("sqlite_err")
- )
- if sqlite_errs > 0:
- failures.append(f"UNRETRIED SQLITE ERRORS: {sqlite_errs}")
-
- finally:
- conn.close()
-
- # ============ STATS ============
- print()
- total_claims = sum(1 for e in all_events if e["kind"] == "claimed")
- total_completes = sum(1 for e in all_events if e["kind"] == "completed")
- total_lost_races = sum(1 for e in all_events if e["kind"] == "lost_claim_race")
-
- per_worker = {}
- for e in all_events:
- if e["kind"] == "completed":
- per_worker.setdefault(e["worker"], 0)
- per_worker[e["worker"]] += 1
-
- print(f"Total claims: {total_claims}")
- print(f"Total completes: {total_completes}")
- print(f"Lost claim races: {total_lost_races} (expected contention; not a bug)")
- print(f"Elapsed: {elapsed:.2f}s")
- print(f"Throughput: {NUM_TASKS/elapsed:.1f} tasks/sec")
- print("Per-worker completions:")
- for w in sorted(per_worker.keys()):
- print(f" worker-{w}: {per_worker[w]}")
-
- if failures:
- print()
- print("=" * 60)
- print(f"FAILURES ({len(failures)}):")
- print("=" * 60)
- for f in failures[:20]:
- print(f" {f}")
- if len(failures) > 20:
- print(f" ... and {len(failures) - 20} more")
- sys.exit(1)
- else:
- print()
- print("✔ ALL INVARIANTS HELD")
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_concurrency_mixed.py b/tests/stress/test_concurrency_mixed.py
deleted file mode 100644
index 8b6ef718667..00000000000
--- a/tests/stress/test_concurrency_mixed.py
+++ /dev/null
@@ -1,350 +0,0 @@
-"""Harder concurrency stress: mixed operations + larger scale.
-
-Scales to 500 tasks, 10 workers, 60s runtime. Each worker randomly:
- - claims + completes (70%)
- - claims + blocks with a reason (15%)
- - unblocks a random blocked task (10%)
- - archives a random done task (5%)
-
-Adds a background "dispatcher" process that calls release_stale_claims
-and detect_crashed_workers every 200ms, racing against the workers to
-surface TTL + crash detection races.
-
-Pass criteria: runs invariant holds, no double-completions, no orphan
-runs, no SQLite errors escape the retry layer.
-"""
-
-import json
-import multiprocessing as mp
-import os
-import random
-import sqlite3
-import sys
-import tempfile
-import time
-from pathlib import Path
-
-NUM_WORKERS = 10
-NUM_TASKS = 500
-RUN_DURATION_S = 30
-WT = str(Path(__file__).resolve().parents[2])
-
-
-def worker_loop(worker_id: int, hermes_home: str, result_file: str) -> None:
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- events = []
- start = time.monotonic()
- idle_rounds = 0
-
- while time.monotonic() - start < RUN_DURATION_S:
- conn = kb.connect()
- try:
- op = random.random()
-
- if op < 0.10:
- # Try to unblock a blocked task.
- row = conn.execute(
- "SELECT id FROM tasks WHERE status='blocked' "
- "ORDER BY RANDOM() LIMIT 1"
- ).fetchone()
- if row:
- try:
- ok = kb.unblock_task(conn, row["id"])
- events.append({"kind": "unblocked" if ok else "unblock_noop",
- "task": row["id"], "worker": worker_id})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "unblock",
- "task": row["id"], "err": str(e)[:100]})
- continue
-
- if op < 0.15:
- # Try to archive a done task.
- row = conn.execute(
- "SELECT id FROM tasks WHERE status='done' "
- "ORDER BY RANDOM() LIMIT 1"
- ).fetchone()
- if row:
- try:
- kb.archive_task(conn, row["id"])
- events.append({"kind": "archived", "task": row["id"],
- "worker": worker_id})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "archive",
- "task": row["id"], "err": str(e)[:100]})
- continue
-
- # Default: claim + complete-or-block.
- row = conn.execute(
- "SELECT id FROM tasks WHERE status='ready' "
- "AND claim_lock IS NULL LIMIT 1"
- ).fetchone()
- if row is None:
- idle_rounds += 1
- if idle_rounds > 50:
- break
- time.sleep(0.02)
- continue
- idle_rounds = 0
-
- tid = row["id"]
- try:
- claimed = kb.claim_task(
- conn, tid, claimer=f"worker-{worker_id}",
- ttl_seconds=5, # short TTL so reclaim races in
- )
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "claim",
- "task": tid, "err": str(e)[:100]})
- continue
- if claimed is None:
- events.append({"kind": "lost_claim_race", "task": tid})
- continue
-
- run = kb.latest_run(conn, tid)
- events.append({"kind": "claimed", "task": tid, "worker": worker_id,
- "run_id": run.id, "t": time.monotonic() - start})
-
- time.sleep(random.uniform(0.005, 0.05))
-
- # 20% of the time, block instead of complete
- if random.random() < 0.20:
- try:
- kb.block_task(conn, tid,
- reason=f"blocked by worker-{worker_id}")
- events.append({"kind": "blocked", "task": tid,
- "worker": worker_id, "run_id": run.id})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "block",
- "task": tid, "err": str(e)[:100]})
- else:
- try:
- kb.complete_task(
- conn, tid,
- result=f"done by worker-{worker_id}",
- summary=f"worker-{worker_id} ok",
- metadata={"worker_id": worker_id},
- )
- events.append({"kind": "completed", "task": tid,
- "worker": worker_id, "run_id": run.id,
- "t": time.monotonic() - start})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "complete",
- "task": tid, "err": str(e)[:100]})
- finally:
- conn.close()
-
- with open(result_file, "w") as f:
- json.dump(events, f)
-
-
-def reclaimer_loop(hermes_home: str, result_file: str) -> None:
- """Background dispatcher-like loop that reclaims stale tasks."""
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- events = []
- start = time.monotonic()
- while time.monotonic() - start < RUN_DURATION_S + 2:
- conn = kb.connect()
- try:
- try:
- reclaimed = kb.release_stale_claims(conn)
- if reclaimed:
- events.append({"kind": "reclaimed", "count": reclaimed,
- "t": time.monotonic() - start})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "reclaim",
- "err": str(e)[:100]})
- finally:
- conn.close()
- time.sleep(0.2)
-
- with open(result_file, "w") as f:
- json.dump(events, f)
-
-
-def main():
- home = tempfile.mkdtemp(prefix="hermes_mixed_stress_")
- print(f"HERMES_HOME = {home}")
-
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
- conn = kb.connect()
- for i in range(NUM_TASKS):
- kb.create_task(
- conn, title=f"t#{i}", assignee="shared", tenant="mixed-stress",
- )
- conn.close()
- print(f"Seeded {NUM_TASKS} tasks, launching {NUM_WORKERS} workers + 1 reclaimer")
-
- ctx = mp.get_context("spawn")
- worker_results = [f"/tmp/mixed_worker_{i}.json" for i in range(NUM_WORKERS)]
- reclaim_result = "/tmp/mixed_reclaim.json"
-
- procs = []
- start = time.monotonic()
- for i in range(NUM_WORKERS):
- p = ctx.Process(target=worker_loop, args=(i, home, worker_results[i]))
- p.start()
- procs.append(p)
- r = ctx.Process(target=reclaimer_loop, args=(home, reclaim_result))
- r.start()
- procs.append(r)
-
- for p in procs:
- p.join(timeout=RUN_DURATION_S + 30)
- if p.is_alive():
- p.terminate()
- p.join()
-
- elapsed = time.monotonic() - start
- print(f"Done in {elapsed:.1f}s")
-
- # Aggregate.
- all_events = []
- for i, f in enumerate(worker_results):
- if os.path.isfile(f):
- with open(f) as fh:
- all_events.extend(json.load(fh))
- else:
- print(f" WORKER {i} died with no result file!")
- reclaim_events = []
- if os.path.isfile(reclaim_result):
- with open(reclaim_result) as fh:
- reclaim_events = json.load(fh)
-
- # ============ INVARIANT CHECKS ============
- print()
- print("=" * 60)
- print("INVARIANT CHECKS")
- print("=" * 60)
-
- failures = []
-
- # Per-run attribution tracking
- claims = [e for e in all_events if e["kind"] == "claimed"]
- completions = [e for e in all_events if e["kind"] == "completed"]
- blocks = [e for e in all_events if e["kind"] == "blocked"]
-
- # Every completion must have a matching claim on the same run_id AND
- # the same worker (workers don't steal each other's runs).
- claims_by_run = {c["run_id"]: c for c in claims}
- for comp in completions:
- claim = claims_by_run.get(comp["run_id"])
- if claim is None:
- # It's possible this worker saw a reclaimed run from another worker
- # — that's still a bug: the worker shouldn't be able to complete
- # a run it didn't claim. But let me check if reclaim happened first.
- failures.append(
- f"COMPLETION WITHOUT CLAIM: task {comp['task']} run {comp['run_id']} "
- f"by worker {comp['worker']}"
- )
- elif claim["worker"] != comp["worker"]:
- failures.append(
- f"CROSS-WORKER COMPLETION: run {comp['run_id']} claimed by "
- f"worker {claim['worker']} but completed by worker {comp['worker']}"
- )
-
- # SQLite errors that escaped the retry layer
- sqlite_errs = [e for e in all_events if e["kind"] == "sqlite_err"]
- if sqlite_errs:
- for e in sqlite_errs[:5]:
- failures.append(f"SQLITE ERROR: op={e.get('op')} err={e.get('err')}")
- if len(sqlite_errs) > 5:
- failures.append(f" ... and {len(sqlite_errs) - 5} more sqlite errs")
-
- # DB final state — every task should be in a clean terminal state.
- conn = kb.connect()
- try:
- # Invariant: current_run_id NULL iff latest run is terminal
- inconsistent = conn.execute("""
- SELECT t.id, t.status, t.current_run_id
- FROM tasks t
- WHERE t.current_run_id IS NOT NULL
- AND EXISTS (SELECT 1 FROM task_runs r
- WHERE r.id = t.current_run_id AND r.ended_at IS NOT NULL)
- """).fetchall()
- for row in inconsistent:
- failures.append(
- f"INVARIANT VIOLATION: task {row['id']} status={row['status']} "
- f"has current_run_id={row['current_run_id']} but run is ended"
- )
-
- # Invariant: no orphan open runs
- orphans = conn.execute("""
- SELECT r.id, r.task_id, r.status
- FROM task_runs r
- LEFT JOIN tasks t ON t.current_run_id = r.id
- WHERE r.ended_at IS NULL AND t.id IS NULL
- """).fetchall()
- for row in orphans:
- failures.append(
- f"ORPHAN OPEN RUN: run {row['id']} on task {row['task_id']}"
- )
-
- # Counts — should roughly balance.
- status_counts = dict(
- conn.execute("SELECT status, COUNT(*) FROM tasks GROUP BY status").fetchall()
- )
- run_outcome_counts = dict(
- conn.execute(
- "SELECT outcome, COUNT(*) FROM task_runs "
- "WHERE ended_at IS NOT NULL GROUP BY outcome"
- ).fetchall()
- )
- active_runs = conn.execute(
- "SELECT COUNT(*) FROM task_runs WHERE ended_at IS NULL"
- ).fetchone()[0]
-
- finally:
- conn.close()
-
- # ============ STATS ============
- print()
- print(f"Workers: {NUM_WORKERS}, Tasks: {NUM_TASKS}")
- print(f"Elapsed: {elapsed:.1f}s")
- print(f"Events collected: {len(all_events)} (+{len(reclaim_events)} reclaim)")
- print()
- print("Operations:")
- op_counts = {}
- for e in all_events:
- op_counts[e["kind"]] = op_counts.get(e["kind"], 0) + 1
- for k in sorted(op_counts.keys()):
- print(f" {k:<25} {op_counts[k]}")
-
- print()
- print("Final task status:")
- for s, n in sorted(status_counts.items()):
- print(f" {s:<10} {n}")
- print("Final run outcomes:")
- for o, n in sorted(run_outcome_counts.items(), key=lambda x: (x[0] or '',)):
- print(f" {o:<12} {n}")
- print(f" active {active_runs}")
-
- if failures:
- print()
- print("=" * 60)
- print(f"FAILURES ({len(failures)}):")
- print("=" * 60)
- for f in failures[:30]:
- print(f" {f}")
- if len(failures) > 30:
- print(f" ... and {len(failures) - 30} more")
- sys.exit(1)
- else:
- print()
- print("✔ ALL INVARIANTS HELD UNDER MIXED STRESS")
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_concurrency_parent_gate.py b/tests/stress/test_concurrency_parent_gate.py
deleted file mode 100644
index 406774bad5b..00000000000
--- a/tests/stress/test_concurrency_parent_gate.py
+++ /dev/null
@@ -1,183 +0,0 @@
-"""Stress test for parent-completion invariant at the claim gate.
-
-Simulates the create-then-link race described in RCA t_a6acd07d:
-
- Thread A: repeatedly inserts a child row with status='ready' (racy
- writer) and a split-second-later inserts the parent link,
- emulating the pre-fix _kanban_create path.
- Thread B: repeatedly runs claim_task against every ready task.
-
-Pass criteria: no task is ever 'claimed' while any of its parents is
-not 'done'. The claim_task gate added in hermes_cli/kanban_db.py must
-demote such tasks back to 'todo' and emit a 'claim_rejected' event
-instead of spawning.
-
-Run as a script (`python tests/stress/test_concurrency_parent_gate.py`)
-or via `pytest --run-stress`. The default pytest collection in
-tests/stress/conftest.py ignores *.py globs, so this is a script.
-"""
-from __future__ import annotations
-
-import os
-import random
-import sys
-import tempfile
-import threading
-import time
-from pathlib import Path
-
-WT = str(Path(__file__).resolve().parents[2])
-sys.path.insert(0, WT)
-
-NUM_CREATE_ROUNDS = 200
-WORKERS_RUN_DURATION_S = 8
-
-
-def run() -> int:
- home = tempfile.mkdtemp(prefix="hermes_parent_gate_stress_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
-
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
-
- # Seed N parents in 'ready' state. They stay ready for the whole run
- # (never 'done'), so every child linked to one of them must remain
- # unclaimable.
- parent_ids: list[str] = []
- conn = kb.connect()
- try:
- for i in range(10):
- parent_ids.append(
- kb.create_task(conn, title=f"parent-{i}", assignee="a")
- )
- finally:
- conn.close()
-
- created_children: list[str] = []
- created_lock = threading.Lock()
- stop = threading.Event()
- violations: list[str] = []
-
- def racy_creator() -> None:
- """Inserts child rows with status='ready' and links them after.
-
- This is the pre-fix _kanban_create behavior — the very race
- the gate in claim_task must catch.
- """
- conn = kb.connect()
- try:
- for _ in range(NUM_CREATE_ROUNDS):
- if stop.is_set():
- return
- parents = random.sample(parent_ids, k=2)
- # Step 1: insert child WITHOUT parents (ends up ready).
- child = kb.create_task(
- conn, title="child", assignee="a", parents=[],
- )
- # Tiny delay so worker threads get a chance to see the
- # ready row before the links are inserted.
- time.sleep(random.uniform(0.0001, 0.002))
- # Step 2: add the parent links after the fact.
- for p in parents:
- try:
- kb.link_tasks(conn, parent_id=p, child_id=child)
- except Exception:
- pass
- with created_lock:
- created_children.append(child)
- finally:
- conn.close()
-
- def worker_loop() -> None:
- conn = kb.connect()
- try:
- end = time.monotonic() + WORKERS_RUN_DURATION_S
- while time.monotonic() < end and not stop.is_set():
- row = conn.execute(
- "SELECT id FROM tasks WHERE status='ready' "
- "AND claim_lock IS NULL ORDER BY RANDOM() LIMIT 1"
- ).fetchone()
- if row is None:
- time.sleep(0.002)
- continue
- tid = row["id"]
- try:
- claimed = kb.claim_task(conn, tid, claimer="w")
- except Exception:
- continue
- if claimed is None:
- continue
- # Invariant: a successful claim on `tid` must mean all
- # parents are 'done'. Check in the same connection txn
- # so we see the post-claim state.
- undone = conn.execute(
- "SELECT l.parent_id, p.status FROM task_links l "
- "JOIN tasks p ON p.id = l.parent_id "
- "WHERE l.child_id = ? AND p.status != 'done'",
- (tid,),
- ).fetchall()
- if undone:
- violations.append(
- f"claimed {tid} while parents not done: "
- + ",".join(f"{r['parent_id']}={r['status']}" for r in undone)
- )
- # Release so the run doesn't leak and the next round sees ready.
- kb.complete_task(conn, tid, result="stress-ok")
- finally:
- conn.close()
-
- creator = threading.Thread(target=racy_creator, daemon=True)
- workers = [threading.Thread(target=worker_loop, daemon=True)
- for _ in range(4)]
- creator.start()
- for w in workers:
- w.start()
- creator.join()
- # Give the workers a chance to fully drain ready rows before we stop.
- time.sleep(0.5)
- stop.set()
- for w in workers:
- w.join(timeout=WORKERS_RUN_DURATION_S + 2)
-
- # Post-run audit: the DB event log must show no 'claimed' event on any
- # task whose parents were not 'done' at the time of the claim.
- conn = kb.connect()
- try:
- bad = conn.execute(
- """
- WITH claims AS (
- SELECT task_id, created_at AS t
- FROM task_events WHERE kind='claimed'
- )
- SELECT c.task_id, l.parent_id, p.status, p.completed_at
- FROM claims c
- JOIN task_links l ON l.child_id = c.task_id
- JOIN tasks p ON p.id = l.parent_id
- WHERE p.completed_at IS NULL OR p.completed_at > c.t
- """
- ).fetchall()
- rejections = conn.execute(
- "SELECT COUNT(*) FROM task_events WHERE kind='claim_rejected'"
- ).fetchone()[0]
- finally:
- conn.close()
-
- print(f"children created: {len(created_children)}")
- print(f"violations: {len(violations)}")
- print(f"event-log bad: {len(bad)}")
- print(f"claim_rejected: {rejections}")
-
- if violations or bad:
- for v in violations[:10]:
- print(" VIOLATION:", v)
- for row in list(bad)[:10]:
- print(" EVENT-LOG BAD:", dict(row))
- return 1
- print("PARENT-GATE INVARIANT HELD UNDER RACE")
- return 0
-
-
-if __name__ == "__main__":
- sys.exit(run())
diff --git a/tests/stress/test_concurrency_reclaim_race.py b/tests/stress/test_concurrency_reclaim_race.py
deleted file mode 100644
index 6a636de72ef..00000000000
--- a/tests/stress/test_concurrency_reclaim_race.py
+++ /dev/null
@@ -1,241 +0,0 @@
-"""Target the reclaim race specifically.
-
-Workers claim tasks with a 1s TTL but sleep 2s before completing. The
-reclaimer runs every 200ms. Scenario: worker claims, reclaimer expires
-the claim mid-work, worker tries to complete AFTER its run has been
-reclaimed.
-
-Expected behavior (per design): the worker's complete_task should
-either succeed on the reclaimed-and-re-claimed-by-another-worker case
-(no, it should refuse — the claim was invalidated), OR succeed by
-grace (we "forgive" a late complete from the original worker if no
-one else picked it up).
-
-Actually looking at complete_task: it doesn't check claim_lock. It just
-transitions from 'running' -> 'done'. So if the reclaimer moved it back
-to 'ready', the late worker's complete_task will fail (CAS on
-status='running' fails). This is the CORRECT behavior.
-
-Invariant being tested: race between worker.complete and
-dispatcher.reclaim must not produce a double-run-close or other
-inconsistency.
-"""
-
-import json
-import multiprocessing as mp
-import os
-import random
-import sqlite3
-import sys
-import tempfile
-import time
-from pathlib import Path
-
-NUM_WORKERS = 5
-NUM_TASKS = 50
-TTL = 1
-WORK_DURATION_S = 2.0 # longer than TTL => reclaimer wins
-WT = str(Path(__file__).resolve().parents[2])
-
-
-def worker_loop(worker_id: int, hermes_home: str, result_file: str) -> None:
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- events = []
- start = time.monotonic()
- idle = 0
-
- while time.monotonic() - start < 40:
- conn = kb.connect()
- try:
- row = conn.execute(
- "SELECT id FROM tasks WHERE status='ready' AND claim_lock IS NULL LIMIT 1"
- ).fetchone()
- if row is None:
- idle += 1
- if idle > 30:
- break
- time.sleep(0.05)
- continue
- idle = 0
- tid = row["id"]
- try:
- claimed = kb.claim_task(conn, tid, claimer=f"worker-{worker_id}",
- ttl_seconds=TTL)
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "claim", "err": str(e)[:100]})
- continue
- if claimed is None:
- events.append({"kind": "lost_claim", "task": tid})
- continue
- run = kb.latest_run(conn, tid)
- events.append({"kind": "claimed", "task": tid, "worker": worker_id,
- "run_id": run.id})
-
- # Sleep longer than TTL so reclaimer has a chance to intervene
- time.sleep(WORK_DURATION_S + random.uniform(-0.3, 0.3))
-
- try:
- ok = kb.complete_task(
- conn, tid,
- result=f"by worker-{worker_id}",
- summary=f"worker-{worker_id} finished",
- )
- events.append({"kind": "complete_ok" if ok else "complete_refused",
- "task": tid, "worker": worker_id, "run_id": run.id})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "op": "complete", "err": str(e)[:100]})
- finally:
- conn.close()
-
- with open(result_file, "w") as f:
- json.dump(events, f)
-
-
-def reclaimer_loop(hermes_home: str, result_file: str) -> None:
- os.environ["HERMES_HOME"] = hermes_home
- os.environ["HOME"] = hermes_home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- events = []
- start = time.monotonic()
- while time.monotonic() - start < 42:
- conn = kb.connect()
- try:
- try:
- n = kb.release_stale_claims(conn)
- if n:
- events.append({"kind": "reclaimed", "count": n,
- "t": time.monotonic() - start})
- except sqlite3.OperationalError as e:
- events.append({"kind": "sqlite_err", "err": str(e)[:100]})
- finally:
- conn.close()
- time.sleep(0.2)
- with open(result_file, "w") as f:
- json.dump(events, f)
-
-
-def main():
- home = tempfile.mkdtemp(prefix="hermes_reclaim_race_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
- conn = kb.connect()
- for i in range(NUM_TASKS):
- kb.create_task(conn, title=f"t{i}", assignee="shared",
- tenant="reclaim-race")
- conn.close()
- print(f"Seeded {NUM_TASKS} tasks. TTL={TTL}s, work_duration={WORK_DURATION_S}s")
- print("(worker work > TTL guarantees reclaims)")
-
- ctx = mp.get_context("spawn")
- worker_results = [f"/tmp/rc_worker_{i}.json" for i in range(NUM_WORKERS)]
- reclaim_result = "/tmp/rc_reclaim.json"
- procs = []
- for i in range(NUM_WORKERS):
- p = ctx.Process(target=worker_loop, args=(i, home, worker_results[i]))
- p.start()
- procs.append(p)
- r = ctx.Process(target=reclaimer_loop, args=(home, reclaim_result))
- r.start()
- procs.append(r)
-
- for p in procs:
- p.join(timeout=60)
- if p.is_alive():
- p.terminate()
- p.join()
-
- # Aggregate.
- all_events = []
- for f in worker_results:
- if os.path.isfile(f):
- with open(f) as fh:
- all_events.extend(json.load(fh))
- reclaim_events = []
- if os.path.isfile(reclaim_result):
- with open(reclaim_result) as fh:
- reclaim_events = json.load(fh)
-
- op_counts = {}
- for e in all_events:
- op_counts[e["kind"]] = op_counts.get(e["kind"], 0) + 1
- total_reclaims = sum(e.get("count", 0) for e in reclaim_events)
- print(f"\nReclaimer fired {len(reclaim_events)} times, total tasks reclaimed: {total_reclaims}")
- print("Worker events:")
- for k in sorted(op_counts):
- print(f" {k:<25} {op_counts[k]}")
-
- # Invariant checks
- failures = []
- conn = kb.connect()
- try:
- # Any task stuck with current_run_id pointing at a closed run?
- bad = conn.execute("""
- SELECT t.id, t.status, t.current_run_id, r.ended_at, r.outcome
- FROM tasks t
- JOIN task_runs r ON r.id = t.current_run_id
- WHERE r.ended_at IS NOT NULL
- """).fetchall()
- for row in bad:
- failures.append(
- f"INVARIANT VIOLATION: task {row['id']} status={row['status']} "
- f"current_run_id={row['current_run_id']} but run ended "
- f"outcome={row['outcome']}"
- )
- # Every run with NULL ended_at should still have the task pointing at it
- orphans = conn.execute("""
- SELECT r.id, r.task_id
- FROM task_runs r
- LEFT JOIN tasks t ON t.current_run_id = r.id
- WHERE r.ended_at IS NULL AND t.id IS NULL
- """).fetchall()
- for row in orphans:
- failures.append(f"ORPHAN OPEN RUN: run {row['id']} on task {row['task_id']}")
- # Event counts
- claim_evts = conn.execute(
- "SELECT COUNT(*) FROM task_events WHERE kind='claimed'").fetchone()[0]
- reclaim_evts = conn.execute(
- "SELECT COUNT(*) FROM task_events WHERE kind='reclaimed'").fetchone()[0]
- comp_evts = conn.execute(
- "SELECT COUNT(*) FROM task_events WHERE kind='completed'").fetchone()[0]
- print(f"\nDB event counts: claimed={claim_evts} reclaimed={reclaim_evts} completed={comp_evts}")
- # Every reclaimed run must have ended_at set
- unended_reclaims = conn.execute(
- "SELECT COUNT(*) FROM task_runs WHERE outcome='reclaimed' AND ended_at IS NULL"
- ).fetchone()[0]
- if unended_reclaims:
- failures.append(f"UNENDED RECLAIMED RUNS: {unended_reclaims}")
- # Count of completed runs
- comp_runs = conn.execute(
- "SELECT COUNT(*) FROM task_runs WHERE outcome='completed'"
- ).fetchone()[0]
- reclaim_runs = conn.execute(
- "SELECT COUNT(*) FROM task_runs WHERE outcome='reclaimed'"
- ).fetchone()[0]
- print(f"DB run outcomes: completed={comp_runs} reclaimed={reclaim_runs}")
- finally:
- conn.close()
-
- if reclaim_runs == 0:
- failures.append("NO RECLAIMS HAPPENED — test didn't stress what it was supposed to")
-
- if failures:
- print(f"\nFAILURES ({len(failures)}):")
- for f in failures[:20]:
- print(f" {f}")
- sys.exit(1)
- else:
- print("\n✔ RECLAIM RACE INVARIANTS HELD")
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_property_fuzzing.py b/tests/stress/test_property_fuzzing.py
deleted file mode 100644
index b8270b2b352..00000000000
--- a/tests/stress/test_property_fuzzing.py
+++ /dev/null
@@ -1,282 +0,0 @@
-"""Randomized property testing for the Kanban kernel.
-
-Generates 1000 random operation sequences, each 20-50 ops, on small
-task graphs. After each step, checks the full invariant set:
-
- I1. If tasks.current_run_id IS NOT NULL, the run MUST exist AND
- ended_at MUST be NULL (we never point at a closed run).
- I2. If a run has ended_at NULL, SOME task MUST have current_run_id
- pointing at it (no orphan open runs).
- I3. task.status in the valid set {triage, todo, ready, running,
- blocked, done, archived}.
- I4. task.claim_lock NULL iff status not in (running,).
- I5. Every run has started_at <= ended_at (or ended_at is NULL).
- I6. If outcome is set, ended_at must also be set.
- I7. Events are strictly monotonic in (created_at, id).
- I8. task_events.run_id references a task_runs.id that exists
- (or is NULL).
- I9. Parent completion invariant: if all parents are 'done', the
- child cannot be in 'todo' status (recompute_ready should have
- promoted it). This is called out in the comment on
- recompute_ready; verify it holds after every random seq.
-
-Not using hypothesis the lib; just Python random for simplicity.
-"""
-
-import os
-import random
-import sys
-import tempfile
-from pathlib import Path
-
-WT = str(Path(__file__).resolve().parents[2])
-NUM_SEQUENCES = 500
-OPS_PER_SEQUENCE = 100
-TASK_POOL = 10
-
-OPS = [
- "create", "create_child", "claim", "complete", "block", "unblock",
- "archive", "heartbeat", "release_stale", "detect_crashed",
- "recompute_ready", "reassign",
-]
-
-
-def assert_invariants(conn, kb, ops_log):
- """Run all invariant checks; raise AssertionError with context on any."""
- failures = []
-
- # I1: current_run_id → run exists and not ended
- bad_ptr = conn.execute("""
- SELECT t.id, t.current_run_id, r.ended_at, r.outcome
- FROM tasks t
- LEFT JOIN task_runs r ON r.id = t.current_run_id
- WHERE t.current_run_id IS NOT NULL
- AND (r.id IS NULL OR r.ended_at IS NOT NULL)
- """).fetchall()
- for row in bad_ptr:
- if row["ended_at"] is None and row["outcome"] is None:
- detail = "missing"
- else:
- detail = f"closed ({row['outcome']})"
- failures.append(
- f"I1: task {row['id']} points at run {row['current_run_id']} "
- f"which is {detail}"
- )
-
- # I2: open run → some task points at it
- orphans = conn.execute("""
- SELECT r.id, r.task_id
- FROM task_runs r
- WHERE r.ended_at IS NULL
- AND NOT EXISTS (SELECT 1 FROM tasks t WHERE t.current_run_id = r.id)
- """).fetchall()
- for row in orphans:
- failures.append(f"I2: open run {row['id']} on task {row['task_id']} has no pointer")
-
- # I3: valid statuses
- valid = {"triage", "todo", "ready", "running", "blocked", "done", "archived"}
- bad_status = conn.execute("SELECT id, status FROM tasks").fetchall()
- for row in bad_status:
- if row["status"] not in valid:
- failures.append(f"I3: task {row['id']} has invalid status {row['status']!r}")
-
- # I4: claim_lock set only when running
- bad_lock = conn.execute("""
- SELECT id, status, claim_lock FROM tasks
- WHERE (status != 'running' AND claim_lock IS NOT NULL)
- """).fetchall()
- for row in bad_lock:
- failures.append(
- f"I4: task {row['id']} status={row['status']} but claim_lock={row['claim_lock']!r}"
- )
-
- # I5: run started_at <= ended_at
- bad_times = conn.execute("""
- SELECT id, started_at, ended_at FROM task_runs
- WHERE ended_at IS NOT NULL AND started_at > ended_at
- """).fetchall()
- for row in bad_times:
- failures.append(
- f"I5: run {row['id']} started_at={row['started_at']} > ended_at={row['ended_at']}"
- )
-
- # I6: outcome set → ended_at set
- bad_outcome = conn.execute("""
- SELECT id, outcome, ended_at FROM task_runs
- WHERE outcome IS NOT NULL AND ended_at IS NULL
- """).fetchall()
- for row in bad_outcome:
- failures.append(f"I6: run {row['id']} outcome={row['outcome']} but ended_at NULL")
-
- # I7: events monotonic in id (always true for autoincrement)
- # Skip — autoincrement guarantees it.
-
- # I8: event.run_id references existing run
- bad_ev_fk = conn.execute("""
- SELECT e.id, e.run_id FROM task_events e
- LEFT JOIN task_runs r ON r.id = e.run_id
- WHERE e.run_id IS NOT NULL AND r.id IS NULL
- """).fetchall()
- for row in bad_ev_fk:
- failures.append(f"I8: event {row['id']} references missing run {row['run_id']}")
-
- # I9: if all parents done → child not in todo
- # (Only applies to children with at least one parent)
- orphaned_todo = conn.execute("""
- SELECT c.id AS child_id,
- COUNT(*) AS n_parents,
- SUM(CASE WHEN p.status = 'done' THEN 1 ELSE 0 END) AS done_parents
- FROM tasks c
- JOIN task_links l ON l.child_id = c.id
- JOIN tasks p ON p.id = l.parent_id
- WHERE c.status = 'todo'
- GROUP BY c.id
- HAVING n_parents > 0 AND n_parents = done_parents
- """).fetchall()
- for row in orphaned_todo:
- failures.append(
- f"I9: task {row['child_id']} is todo but all {row['n_parents']} parents are done"
- )
-
- if failures:
- print(f"\n!!! INVARIANT VIOLATION after {len(ops_log)} ops:")
- for f in failures[:10]:
- print(f" {f}")
- if len(failures) > 10:
- print(f" ... and {len(failures) - 10} more")
- print("\nLast 10 ops:")
- for op in ops_log[-10:]:
- print(f" {op}")
- return False
- return True
-
-
-def random_op(rng, conn, kb, task_pool):
- op = rng.choice(OPS)
-
- if op == "create":
- tid = kb.create_task(
- conn,
- title=f"rand {rng.randint(0, 1000)}",
- assignee=rng.choice(["w1", "w2", "w3", None]),
- )
- task_pool.append(tid)
- return {"op": "create", "tid": tid}
-
- if op == "create_child" and task_pool:
- parent = rng.choice(task_pool)
- tid = kb.create_task(
- conn, title=f"child of {parent}",
- assignee=rng.choice(["w1", "w2", "w3", None]),
- parents=[parent],
- )
- task_pool.append(tid)
- return {"op": "create_child", "tid": tid, "parent": parent}
-
- if not task_pool:
- return None
-
- tid = rng.choice(task_pool)
- task = kb.get_task(conn, tid)
- if task is None:
- task_pool.remove(tid)
- return None
-
- if op == "claim":
- claimed = kb.claim_task(conn, tid, ttl_seconds=rng.choice([1, 3, 10]))
- return {"op": "claim", "tid": tid, "ok": claimed is not None}
- if op == "complete":
- summary = rng.choice([None, f"done via op {rng.randint(0, 1000)}"])
- ok = kb.complete_task(conn, tid, summary=summary)
- return {"op": "complete", "tid": tid, "ok": ok}
- if op == "block":
- reason = rng.choice([None, "rand block"])
- ok = kb.block_task(conn, tid, reason=reason)
- return {"op": "block", "tid": tid, "ok": ok}
- if op == "unblock":
- ok = kb.unblock_task(conn, tid)
- return {"op": "unblock", "tid": tid, "ok": ok}
- if op == "archive":
- ok = kb.archive_task(conn, tid)
- if ok:
- task_pool.remove(tid)
- return {"op": "archive", "tid": tid, "ok": ok}
- if op == "heartbeat":
- ok = kb.heartbeat_worker(conn, tid)
- return {"op": "heartbeat", "tid": tid, "ok": ok}
- if op == "release_stale":
- n = kb.release_stale_claims(conn)
- return {"op": "release_stale", "n": n}
- if op == "detect_crashed":
- # Force-kill a fake PID first so there's something to detect
- crashed = kb.detect_crashed_workers(conn)
- return {"op": "detect_crashed", "n": len(crashed)}
- if op == "recompute_ready":
- n = kb.recompute_ready(conn)
- return {"op": "recompute_ready", "promoted": n}
- if op == "reassign":
- # Reassignment isn't a direct API; simulate via assign_task
- new_a = rng.choice(["w1", "w2", "w3", None])
- try:
- kb.assign_task(conn, tid, new_a)
- return {"op": "reassign", "tid": tid, "to": new_a}
- except Exception as e:
- return {"op": "reassign", "tid": tid, "err": str(e)[:50]}
-
- return None
-
-
-def main():
- total_ops = 0
- total_violations = 0
-
- for seq_idx in range(NUM_SEQUENCES):
- seed = random.randint(0, 10**9)
- rng = random.Random(seed)
- home = tempfile.mkdtemp(prefix=f"hermes_fuzz_{seq_idx}_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
-
- # Fresh module state per sequence to avoid cached init paths.
- for m in list(sys.modules.keys()):
- if m.startswith("hermes_cli"):
- del sys.modules[m]
- from hermes_cli import kanban_db as kb
-
- kb.init_db()
- conn = kb.connect()
- task_pool = []
- ops_log = []
-
- try:
- for i in range(OPS_PER_SEQUENCE):
- result = random_op(rng, conn, kb, task_pool)
- if result is None:
- continue
- ops_log.append(result)
- total_ops += 1
- if not assert_invariants(conn, kb, ops_log):
- total_violations += 1
- print(f" sequence {seq_idx} (seed={seed}) failed at op {i}")
- break
- finally:
- conn.close()
-
- if seq_idx % 10 == 0:
- print(f" seq {seq_idx:3d}: {total_ops} ops so far, {total_violations} violations")
-
- print()
- print("=" * 60)
- print(f"Total sequences: {NUM_SEQUENCES}")
- print(f"Total operations: {total_ops}")
- print(f"Invariant violations: {total_violations}")
- if total_violations == 0:
- print("\n✔ ALL INVARIANTS HELD ACROSS RANDOMIZED SEQUENCES")
- else:
- print("\n✗ INVARIANT VIOLATIONS FOUND")
- sys.exit(1)
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/stress/test_subprocess_e2e.py b/tests/stress/test_subprocess_e2e.py
deleted file mode 100644
index ebdab39fd23..00000000000
--- a/tests/stress/test_subprocess_e2e.py
+++ /dev/null
@@ -1,228 +0,0 @@
-"""E2E: dispatcher spawns real Python subprocess workers.
-
-This validates the IPC + lifecycle story that mocks can't:
- - spawn_fn returns a real PID
- - the child process resolves hermes_cli.kanban_db on its own
- - the child writes heartbeats via the CLI (real argparse, real init_db)
- - the child completes via the CLI with --summary + --metadata
- - the dispatcher observes all of this through the DB only
- - worker logs are captured to HERMES_HOME/kanban/logs/<task>.log
- - crash detection works against a real dead PID
-"""
-
-import os
-from pathlib import Path
-import subprocess
-import sys
-import tempfile
-import time
-
-WT = str(Path(__file__).resolve().parents[2])
-FAKE_WORKER = str(Path(__file__).parent / "_fake_worker.py")
-PY = sys.executable
-
-
-def make_spawn_fn(home: str):
- """Return a spawn_fn the dispatcher can call. Launches the fake
- worker as a detached subprocess."""
-
- def _spawn(task, workspace):
- log_path = os.path.join(home, f"worker_{task.id}.log")
- env = {
- **os.environ,
- "HERMES_HOME": home,
- "HOME": home,
- "PYTHONPATH": WT,
- "HERMES_KANBAN_TASK": task.id,
- "HERMES_KANBAN_WORKSPACE": workspace,
- "PATH": f"{os.path.dirname(PY)}:{os.environ.get('PATH','')}",
- }
- log_f = open(log_path, "ab")
- proc = subprocess.Popen(
- [PY, FAKE_WORKER],
- stdin=subprocess.DEVNULL,
- stdout=log_f,
- stderr=subprocess.STDOUT,
- env=env,
- start_new_session=True,
- )
- return proc.pid
-
- return _spawn
-
-
-def main():
- home = tempfile.mkdtemp(prefix="hermes_e2e_")
- os.environ["HERMES_HOME"] = home
- os.environ["HOME"] = home
- sys.path.insert(0, WT)
- from hermes_cli import kanban_db as kb
-
- # Point the `hermes` CLI child processes will run at the worktree
- # hermes_cli.main. We do this by putting a shim on PATH.
- shim_dir = os.path.join(home, "bin")
- os.makedirs(shim_dir, exist_ok=True)
- shim_path = os.path.join(shim_dir, "hermes")
- with open(shim_path, "w") as f:
- f.write(f"""#!/bin/sh
-exec {PY} -m hermes_cli.main "$@"
-""")
- os.chmod(shim_path, 0o755)
- os.environ["PATH"] = f"{shim_dir}:{os.environ.get('PATH','')}"
-
- kb.init_db()
- conn = kb.connect()
-
- # ============ SCENARIO A: happy path, 3 tasks ============
- print("=" * 60)
- print("A. Real-subprocess happy path (3 tasks)")
- print("=" * 60)
-
- tids = []
- for i in range(3):
- tid = kb.create_task(
- conn, title=f"real-e2e-{i}", assignee="default",
- )
- tids.append(tid)
-
- spawn_fn = make_spawn_fn(home)
- result = kb.dispatch_once(conn, spawn_fn=spawn_fn)
- print(f" dispatched: {len(result.spawned)} spawned")
- spawned_pids = []
- # The dispatcher sets worker_pid on each claimed task via _set_worker_pid.
- for tid in tids:
- task = kb.get_task(conn, tid)
- spawned_pids.append(task.worker_pid)
- print(f" task {tid}: pid={task.worker_pid} status={task.status}")
-
- # Wait for all workers to complete (up to 10s).
- deadline = time.monotonic() + 10
- while time.monotonic() < deadline:
- statuses = [kb.get_task(conn, tid).status for tid in tids]
- if all(s == "done" for s in statuses):
- break
- time.sleep(0.2)
-
- print()
- failures = []
- for tid in tids:
- task = kb.get_task(conn, tid)
- runs = kb.list_runs(conn, tid)
- print(f" task {tid}: status={task.status}, current_run_id={task.current_run_id}, "
- f"runs={[(r.id, r.outcome) for r in runs]}")
- if task.status != "done":
- failures.append(f"task {tid} not done: status={task.status}")
- if task.current_run_id is not None:
- failures.append(f"task {tid} has dangling current_run_id={task.current_run_id}")
- if len(runs) != 1:
- failures.append(f"task {tid} has {len(runs)} runs, expected 1")
- else:
- r = runs[0]
- if r.outcome != "completed":
- failures.append(f"task {tid} run outcome={r.outcome}, expected completed")
- if not r.summary or "real-subprocess worker finished" not in r.summary:
- failures.append(f"task {tid} summary missing: {r.summary!r}")
- if not r.metadata or r.metadata.get("iterations") != 3:
- failures.append(f"task {tid} metadata missing iterations: {r.metadata}")
- # Heartbeat events should be present
- events = kb.list_events(conn, tid)
- heartbeats = [e for e in events if e.kind == "heartbeat"]
- if len(heartbeats) < 3: # start + 3 progress
- failures.append(f"task {tid} heartbeats={len(heartbeats)} expected >=3")
-
- if failures:
- print("\nFAILURES:")
- for f in failures:
- print(f" {f}")
- sys.exit(1)
-
- print("\n ✔ Scenario A: all 3 real-subprocess workers completed cleanly")
-
- # ============ SCENARIO B: crashed worker ============
- print()
- print("=" * 60)
- print("B. Crashed worker (kill -9 mid-heartbeat)")
- print("=" * 60)
-
- crash_tid = kb.create_task(
- conn, title="crash-e2e", assignee="default",
- )
-
- # Spawn a worker that sleeps long enough for us to kill it.
- # CRITICAL: spawn through a double-fork so when we kill the child it
- # doesn't zombify under our pid (which would fool kill -0 liveness
- # checks into thinking it's still alive). In production the
- # dispatcher daemon is long-lived but its workers are reaped by init
- # after exit; the test needs to match that orphaning behavior.
- def spawn_sleeper(task, workspace):
- r, w = os.pipe()
- middleman = subprocess.Popen(
- [
- PY, "-c",
- "import os,sys,subprocess;"
- "p=subprocess.Popen(['sleep','30'],"
- "stdin=subprocess.DEVNULL,"
- "stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,"
- "start_new_session=True);"
- "os.write(int(sys.argv[1]), str(p.pid).encode());"
- "sys.exit(0)",
- str(w),
- ],
- pass_fds=(w,),
- stdin=subprocess.DEVNULL,
- stdout=subprocess.DEVNULL,
- stderr=subprocess.DEVNULL,
- )
- os.close(w)
- middleman.wait() # middleman exits immediately, orphaning the sleep
- grandchild_pid = int(os.read(r, 16))
- os.close(r)
- return grandchild_pid
-
- result = kb.dispatch_once(conn, spawn_fn=spawn_sleeper)
- task = kb.get_task(conn, crash_tid)
- print(f" spawned sleeper pid={task.worker_pid} for {crash_tid}")
- # Kill the sleeper forcibly
- os.kill(task.worker_pid, 9)
- # Give the OS a moment to reap
- time.sleep(0.5)
-
- # Simulate next dispatcher tick — should detect the crashed PID
- crashed = kb.detect_crashed_workers(conn)
- print(f" detect_crashed_workers returned {len(crashed)} crashed (expected 1)")
-
- task = kb.get_task(conn, crash_tid)
- runs = kb.list_runs(conn, crash_tid)
- print(f" task status={task.status}, runs={[(r.id, r.outcome) for r in runs]}")
-
- if len(crashed) < 1:
- print(" ✗ crash NOT detected")
- sys.exit(1)
- if task.status != "ready":
- print(f" ✗ task should be back to ready, got {task.status}")
- sys.exit(1)
- if runs[0].outcome != "crashed":
- print(f" ✗ run outcome should be 'crashed', got {runs[0].outcome!r}")
- sys.exit(1)
- print("\n ✔ Scenario B: crash detected, task re-queued, run outcome=crashed")
-
- # ============ SCENARIO C: worker log was captured ============
- print()
- print("=" * 60)
- print("C. Worker log captured to disk")
- print("=" * 60)
- # Scenario A workers wrote to /tmp/hermes_e2e_*/worker_*.log
- import glob
- logs = glob.glob(os.path.join(home, "worker_*.log"))
- print(f" {len(logs)} worker log files")
- for lp in logs[:3]:
- size = os.path.getsize(lp)
- print(f" {os.path.basename(lp)}: {size} bytes")
- # Our fake worker is quiet (no prints); size=0 is fine
-
- conn.close()
- print("\n✔ ALL E2E SCENARIOS PASS")
-
-
-if __name__ == "__main__":
- main()