mirror of
https://github.com/0xWheatyz/handler.git
synced 2026-08-30 03:31:36 +00:00
fix(mise-init): unblock the bootstrap agent + surface live agent output
The mise-init agent wedged on launch and the UI reported it green. Three distinct problems, fixed together: 1. Onboarding wedge (the proximate bug). A freshly-installed claude opens interactive setup — theme picker, then a folder-trust prompt — before the REPL. A detached tmux agent has no one to answer it, so it sat on the theme picker forever while agents.status said 'working'. New control.claude_config.ensure_onboarded() marks onboarding complete and trusts the working dir in ~/.claude.json (merge-only, so the login flow's oauthAccount survives); spawn() calls it before launching. 2. Config-name gate. control/mise.py only recognized `.mise.toml`, so a repo shipping `mise.toml` (no dot) — or config under `.config/mise/` — failed the [tasks.test] gate even when healthy. It now accepts the filenames mise itself reads and scans them all for the test task. 3. "Done" != done (the design gap). A spawned agent's real state lives in its tmux pane, but the socket is control-container-only, so the API couldn't see it. The worker now snapshots each working agent's pane tail (last ~40 lines) into two new agents columns (last_output, output_at, migration 0007) on its existing poll loop; the API serializes them and AgentsSection renders a live-output <pre> under each running agent. A wedged agent now shows the theme picker instead of a misleading green badge. Tests: home-dir writes are isolated to tmp in conftest; added coverage for claude_config seeding/merge, the mise filename set, the worker capture (including dead-session skip), and the API serialization. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BxKY28XKCM6o4ag3nmaVsZ
This commit is contained in:
@@ -33,6 +33,9 @@ def env(tmp_path, monkeypatch):
|
||||
monkeypatch.setenv("SHARED_CONTEXT_WRITE_TOKEN", "shared-token")
|
||||
monkeypatch.setenv("PROJECTS_ROOT", str(tmp_path / "projects"))
|
||||
monkeypatch.delenv("WEBHOOK_URL", raising=False)
|
||||
# Isolate any home-dir writes (e.g. control.claude_config seeding ~/.claude.json at
|
||||
# spawn) to the per-test tmp dir, so tests never touch the real user's config.
|
||||
monkeypatch.setenv("HOME", str(tmp_path))
|
||||
_reset_caches()
|
||||
|
||||
cfg = Config(str(REPO_ROOT / "alembic.ini"))
|
||||
|
||||
@@ -32,6 +32,24 @@ def test_create_and_list_agent(client, auth):
|
||||
assert [a["name"] for a in agents] == ["api"]
|
||||
|
||||
|
||||
def test_agent_serializes_live_output_snapshot(client, auth, engine):
|
||||
from handler.db import repository as repo
|
||||
|
||||
_mk_project(client, auth)
|
||||
client.post(
|
||||
"/projects/proj/agents",
|
||||
json={"name": "api", "working_dir": "/tmp/proj/api", "status": "working"},
|
||||
headers=auth,
|
||||
)
|
||||
with engine.begin() as conn:
|
||||
agent = repo.get_agent_by_name(conn, "proj", "api")
|
||||
repo.update_agent_output(conn, agent["id"], "boot\nTheme picker")
|
||||
|
||||
listed = client.get("/projects/proj/agents", headers=auth).json()[0]
|
||||
assert listed["last_output"] == "boot\nTheme picker"
|
||||
assert listed["output_at"] is not None
|
||||
|
||||
|
||||
def test_agent_under_missing_project_is_404(client, auth):
|
||||
r = client.get("/projects/ghost/agents", headers=auth)
|
||||
assert r.status_code == 404
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
"""Seeding Claude Code onboarding so a detached agent boots straight to the REPL."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from handler.control import claude_config
|
||||
|
||||
|
||||
def _read(home: Path) -> dict:
|
||||
return json.loads((home / ".claude.json").read_text())
|
||||
|
||||
|
||||
def test_ensure_onboarded_seeds_defaults_on_fresh_home(tmp_path):
|
||||
claude_config.ensure_onboarded(home=str(tmp_path))
|
||||
data = _read(tmp_path)
|
||||
assert data["hasCompletedOnboarding"] is True
|
||||
assert data["theme"] == "dark"
|
||||
|
||||
|
||||
def test_ensure_onboarded_merges_without_clobbering(tmp_path):
|
||||
# The login flow's oauthAccount (and a chosen theme) must survive the seeding.
|
||||
(tmp_path / ".claude.json").write_text(
|
||||
json.dumps({"oauthAccount": {"emailAddress": "a@b.c"}, "theme": "light"})
|
||||
)
|
||||
claude_config.ensure_onboarded(home=str(tmp_path))
|
||||
data = _read(tmp_path)
|
||||
assert data["oauthAccount"] == {"emailAddress": "a@b.c"} # preserved
|
||||
assert data["theme"] == "light" # operator's chosen theme untouched
|
||||
assert data["hasCompletedOnboarding"] is True
|
||||
|
||||
|
||||
def test_ensure_onboarded_forces_flag_and_trusts_working_dir(tmp_path):
|
||||
# A stale false must not leave onboarding armed; the working dir gets trusted.
|
||||
(tmp_path / ".claude.json").write_text(json.dumps({"hasCompletedOnboarding": False}))
|
||||
wd = "/var/lib/handler/projects/x"
|
||||
claude_config.ensure_onboarded(working_dir=wd, home=str(tmp_path))
|
||||
data = _read(tmp_path)
|
||||
assert data["hasCompletedOnboarding"] is True
|
||||
assert data["projects"][wd]["hasTrustDialogAccepted"] is True
|
||||
|
||||
|
||||
def test_ensure_onboarded_survives_corrupt_file(tmp_path):
|
||||
(tmp_path / ".claude.json").write_text("{ not valid json")
|
||||
claude_config.ensure_onboarded(home=str(tmp_path))
|
||||
assert _read(tmp_path)["hasCompletedOnboarding"] is True
|
||||
@@ -37,10 +37,22 @@ def test_spawn_refuses_without_mise_file(env, fake_tmux):
|
||||
root = env["tmp"] / "proj"
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
_register_project(root)
|
||||
with pytest.raises(spawn.SpawnError, match="no .mise.toml"):
|
||||
with pytest.raises(spawn.SpawnError, match="no mise config"):
|
||||
spawn.spawn("proj", "api")
|
||||
|
||||
|
||||
def test_spawn_accepts_dotless_mise_toml(env, fake_tmux):
|
||||
# mise also reads `mise.toml` (no leading dot); the gate must honor it too.
|
||||
root = env["tmp"] / "proj"
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
(root / "mise.toml").write_text("[tasks.test]\nrun = 'pytest'\n")
|
||||
_register_project(root)
|
||||
|
||||
agent = spawn.spawn("proj", "api")
|
||||
with get_engine().begin() as conn:
|
||||
assert repo.get_agent_by_name(conn, "proj", "api")["id"] == agent["id"]
|
||||
|
||||
|
||||
def test_spawn_creates_agent_settings_and_session(env, fake_tmux):
|
||||
root = env["tmp"] / "proj"
|
||||
_write_mise(root, with_test=True)
|
||||
@@ -89,7 +101,7 @@ def test_spawn_still_gates_without_mise_init_flag(env, fake_tmux):
|
||||
root.mkdir(parents=True, exist_ok=True)
|
||||
_register_project(root)
|
||||
# require_tests defaults on, so a normal spawn against a mise-less repo still refuses.
|
||||
with pytest.raises(spawn.SpawnError, match="no .mise.toml"):
|
||||
with pytest.raises(spawn.SpawnError, match="no mise config"):
|
||||
spawn.spawn("proj", "api")
|
||||
assert fake_tmux["calls"]["new_session"] == []
|
||||
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
"""The mise-config gate: which filenames count, and the [tasks.test] check."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from handler.control import mise
|
||||
|
||||
|
||||
def test_no_config_is_no_config(tmp_path):
|
||||
assert mise.has_config(str(tmp_path)) is False
|
||||
assert mise.has_test_task(str(tmp_path)) is False
|
||||
|
||||
|
||||
def test_dotless_mise_toml_is_accepted(tmp_path):
|
||||
(tmp_path / "mise.toml").write_text("[tasks.test]\nrun = 'pytest'\n")
|
||||
assert mise.has_config(str(tmp_path)) is True
|
||||
assert mise.has_test_task(str(tmp_path)) is True
|
||||
|
||||
|
||||
def test_dotted_mise_toml_is_accepted(tmp_path):
|
||||
(tmp_path / ".mise.toml").write_text("[tasks.test]\nrun = 'go test ./...'\n")
|
||||
assert mise.has_test_task(str(tmp_path)) is True
|
||||
|
||||
|
||||
def test_config_under_dot_config_dir(tmp_path):
|
||||
cfg = tmp_path / ".config" / "mise"
|
||||
cfg.mkdir(parents=True)
|
||||
(cfg / "config.toml").write_text("[tasks.test]\nrun = 'pytest'\n")
|
||||
assert mise.has_test_task(str(tmp_path)) is True
|
||||
|
||||
|
||||
def test_config_without_test_task_fails_the_gate(tmp_path):
|
||||
(tmp_path / "mise.toml").write_text("[tasks.lint]\nrun = 'ruff check .'\n")
|
||||
assert mise.has_config(str(tmp_path)) is True
|
||||
assert mise.has_test_task(str(tmp_path)) is False
|
||||
|
||||
|
||||
def test_corrupt_config_is_not_a_test_task(tmp_path):
|
||||
(tmp_path / "mise.toml").write_text("this = = not valid toml")
|
||||
assert mise.has_config(str(tmp_path)) is True
|
||||
assert mise.has_test_task(str(tmp_path)) is False
|
||||
@@ -211,6 +211,45 @@ def test_bad_command_is_recorded_failed_not_raised(env):
|
||||
assert "agent name" in failed["error"]
|
||||
|
||||
|
||||
def test_capture_agent_output_snapshots_working_agents(env, monkeypatch):
|
||||
with get_engine().begin() as conn:
|
||||
repo.create_project(conn, "p", "/tmp/p")
|
||||
agent = repo.create_agent(conn, "p", "api", "/tmp/p/api", status="working")
|
||||
|
||||
monkeypatch.setattr(worker.tmux, "has_session", lambda name: True)
|
||||
monkeypatch.setattr(
|
||||
worker.tmux, "capture_pane", lambda name, escapes=False: "boot\nTheme picker\n\n\n"
|
||||
)
|
||||
|
||||
assert worker.capture_agent_output() == 1
|
||||
with get_engine().begin() as conn:
|
||||
row = repo.get_agent_by_id(conn, agent["id"])
|
||||
# The tail is stored with trailing blank lines trimmed.
|
||||
assert row["last_output"] == "boot\nTheme picker"
|
||||
assert row["output_at"] is not None
|
||||
|
||||
|
||||
def test_capture_agent_output_skips_dead_sessions_and_nonworking(env, monkeypatch):
|
||||
with get_engine().begin() as conn:
|
||||
repo.create_project(conn, "p", "/tmp/p")
|
||||
repo.create_agent(conn, "p", "gone", "/tmp/p/gone", status="working")
|
||||
done = repo.create_agent(conn, "p", "done", "/tmp/p/done", status="done")
|
||||
|
||||
captured = []
|
||||
monkeypatch.setattr(worker.tmux, "has_session", lambda name: False)
|
||||
monkeypatch.setattr(
|
||||
worker.tmux,
|
||||
"capture_pane",
|
||||
lambda name, escapes=False: captured.append(name) or "x",
|
||||
)
|
||||
|
||||
# The working agent's session is dead (skipped); the done agent isn't queried at all.
|
||||
assert worker.capture_agent_output() == 0
|
||||
assert captured == []
|
||||
with get_engine().begin() as conn:
|
||||
assert repo.get_agent_by_id(conn, done["id"])["last_output"] is None
|
||||
|
||||
|
||||
def test_drain_processes_multiple_then_stops(env, monkeypatch):
|
||||
_seed_project()
|
||||
monkeypatch.setattr(poller, "sweep", lambda project_id=None: {"checked": 0})
|
||||
|
||||
Reference in New Issue
Block a user