mirror of
https://github.com/0xWheatyz/handler.git
synced 2026-08-30 07:56:24 +00:00
feat!: headless is the only runner - delete the tmux run path (phase 4)
Agent runs are now always worker-owned 'claude -p' subprocesses; tmux survives only for the interactive /login flow. - deleted: worker.capture_agent_output/_pane_tail + the capture loop arm (the empty-/log bug's home), spawn's tmux launch/_claude_command, the tmux resume/kill branches (the silent-send-keys bug's home), tmux.session_name/list_sessions, the CLI attach subcommand, the 'runner' setting - spawn: task is now a hard requirement (headless has no idle REPL) - enforced in spawn (SpawnError) and the API (400); onboarding seeding dropped (-p skips the trust dialog) - resume: single headless path; pre-headless agent rows (no session_id) degrade to the context-re-injection fresh run - settings_gen: permissions allowlist is always emitted - credsync: change-triggered uploads key on .claude/.credentials.json only (claude touches ~/.claude.json every run - keying on it would ping-pong uploads between workers); logins still publish explicitly - cli list: liveness from agent_runs in the DB, not tmux - tests: spawn/kill/resume re-pointed at the fake_launch seam (conftest); integration test now drives API -> worker -> real fake claude subprocess -> events endpoint; README documents the headless model + multi-worker deployment invariants Suite 295 green; frontend unchanged since phase 3.
This commit is contained in:
@@ -1,13 +1,32 @@
|
||||
"""End-to-end web management: the dashboard's HTTP calls -> command queue -> worker ->
|
||||
real ``spawn.spawn`` -> tmux seam. Proves the full container-split flow works with only the
|
||||
tmux/claude boundary faked, not the control layer itself."""
|
||||
real ``spawn.spawn`` -> a real headless subprocess (the fake claude binary). Proves the
|
||||
full container-split flow works with only the claude binary faked, not the control
|
||||
layer: events stream into the DB, the run reconciles, kill cancels."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from handler.control import worker
|
||||
from handler.db import repository as repo
|
||||
from handler.db.engine import get_engine
|
||||
|
||||
REPO_ROOT = Path(__file__).resolve().parents[1]
|
||||
FAKE_CLAUDE = str(REPO_ROOT / "tests" / "fixtures" / "fake_claude.py")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def headless_env(env, monkeypatch):
|
||||
from handler import config
|
||||
|
||||
monkeypatch.setenv("CLAUDE_BIN", FAKE_CLAUDE)
|
||||
config.get_settings.cache_clear()
|
||||
yield env
|
||||
config.get_settings.cache_clear()
|
||||
|
||||
|
||||
def _spawnable_project(root):
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
@@ -16,10 +35,21 @@ def _spawnable_project(root):
|
||||
repo.create_project(conn, "proj", str(root))
|
||||
|
||||
|
||||
def test_spawn_via_api_then_worker_creates_agent_and_session(client, auth, env, fake_tmux):
|
||||
_spawnable_project(env["tmp"] / "proj")
|
||||
def _wait(predicate, timeout=20.0):
|
||||
deadline = time.monotonic() + timeout
|
||||
while time.monotonic() < deadline:
|
||||
result = predicate()
|
||||
if result:
|
||||
return result
|
||||
time.sleep(0.1)
|
||||
return None
|
||||
|
||||
# 1. The dashboard enqueues a spawn (202 + a queued command).
|
||||
|
||||
def test_spawn_via_api_then_worker_runs_headless_claude(client, auth, headless_env):
|
||||
_spawnable_project(headless_env["tmp"] / "proj")
|
||||
|
||||
# 1. The dashboard enqueues a spawn (202 + a queued command). A task is mandatory —
|
||||
# headless claude has no idle-REPL mode.
|
||||
r = client.post(
|
||||
"/projects/proj/agents/spawn",
|
||||
json={"name": "api", "task": "build the thing"},
|
||||
@@ -32,29 +62,62 @@ def test_spawn_via_api_then_worker_creates_agent_and_session(client, auth, env,
|
||||
# No agent yet — the worker hasn't run.
|
||||
assert client.get("/projects/proj/agents", headers=auth).json() == []
|
||||
|
||||
# 2. The control worker drains the queue (runs the real spawn.spawn).
|
||||
# 2. The control worker drains the queue (real spawn.spawn -> real subprocess).
|
||||
assert worker.drain("test-worker") == 1
|
||||
|
||||
# 3. The command is done and the agent + tmux session now exist.
|
||||
# 3. The command finished at launch (fire-and-forget)...
|
||||
got = client.get(f"/commands/{command_id}", headers=auth).json()
|
||||
assert got["status"] == "done"
|
||||
assert got["result"]["name"] == "api"
|
||||
|
||||
agents = client.get("/projects/proj/agents", headers=auth).json()
|
||||
assert [a["name"] for a in agents] == ["api"]
|
||||
assert fake_tmux["calls"]["new_session"][0]["name"] == "proj__api"
|
||||
# ...and the run's whole life shows up via the API: events stream in, the agent
|
||||
# reconciles to done, last_output is the assistant's text.
|
||||
def finished():
|
||||
agents = client.get("/projects/proj/agents", headers=auth).json()
|
||||
return agents if agents and agents[0]["status"] == "done" else None
|
||||
|
||||
agents = _wait(finished)
|
||||
assert agents is not None, "run never reconciled to done"
|
||||
agent = agents[0]
|
||||
assert agent["name"] == "api"
|
||||
assert agent["session_id"]
|
||||
assert agent["worker_id"] == "test-worker"
|
||||
assert agent["last_output"] == "working on: build the thing"
|
||||
|
||||
events = client.get("/projects/proj/agents/api/events", headers=auth).json()
|
||||
assert [e["type"] for e in events] == ["system", "assistant", "result"]
|
||||
|
||||
|
||||
def test_kill_via_api_then_worker(client, auth, env, fake_tmux):
|
||||
_spawnable_project(env["tmp"] / "proj")
|
||||
client.post("/projects/proj/agents/spawn", json={"name": "api"}, headers=auth)
|
||||
def test_spawn_without_task_is_rejected(client, auth, headless_env):
|
||||
_spawnable_project(headless_env["tmp"] / "proj")
|
||||
r = client.post("/projects/proj/agents/spawn", json={"name": "api"}, headers=auth)
|
||||
assert r.status_code == 400
|
||||
assert "task is required" in r.json()["detail"]
|
||||
|
||||
|
||||
def test_kill_via_api_then_worker(client, auth, headless_env, monkeypatch):
|
||||
monkeypatch.setenv("FAKE_CLAUDE_MODE", "hang")
|
||||
_spawnable_project(headless_env["tmp"] / "proj")
|
||||
client.post(
|
||||
"/projects/proj/agents/spawn", json={"name": "api", "task": "hang"}, headers=auth
|
||||
)
|
||||
worker.drain("w")
|
||||
|
||||
# The hanging run is live; kill flags it and the supervisor SIGTERMs its child.
|
||||
r = client.post("/projects/proj/agents/api/kill", headers=auth)
|
||||
assert r.status_code == 202
|
||||
worker.drain("w")
|
||||
|
||||
assert client.get(f"/commands/{r.json()['id']}", headers=auth).json()["status"] == "done"
|
||||
assert "proj__api" in fake_tmux["calls"]["kill_session"]
|
||||
with get_engine().begin() as conn:
|
||||
assert repo.get_agent_by_name(conn, "proj", "api")["status"] == "done"
|
||||
agent = repo.get_agent_by_name(conn, "proj", "api")
|
||||
assert agent["status"] == "done"
|
||||
|
||||
def canceled():
|
||||
with get_engine().begin() as conn:
|
||||
run = repo.get_latest_run(conn, agent["id"])
|
||||
return run if run["status"] != "running" else None
|
||||
|
||||
run = _wait(canceled, timeout=30.0)
|
||||
assert run is not None, "kill never terminated the hanging run"
|
||||
assert run["status"] == "canceled"
|
||||
|
||||
Reference in New Issue
Block a user