mirror of
https://github.com/0xWheatyz/handler.git
synced 2026-08-30 08:06:25 +00:00
072f63bf2d
Some repos an operator wants to manage don't yet define the `.mise.toml` `[tasks.test]` task the spawn gate hard-requires — a chicken-and-egg, since you can't run an agent to author that file without it. This adds a one-click bootstrap. Ticking "Initialize mise" on the add step enqueues a `mise_init` command after the clone. The worker launches a dedicated agent that detects the repo's stack, writes a `.mise.toml` with a canonical `[tasks.test]` task, and commits + pushes it. That agent runs with the test-task gate off (creating the task is the point) and a `HANDLER_MISE_INIT` marker on, so its hooks enforce a bootstrap contract instead of the normal test gate: - Stop hook blocks the turn until `.mise.toml` defines `[tasks.test]` and the change is committed (clean tree) and pushed (no commits ahead of an upstream) — so claude cannot end before the work has actually landed. - git-push hook lets the bootstrap push through, skipping the test/build gate (there may be no working suite yet) so the file reaches the remote. Backend: `mise_init` command type (+ migration 0006), a shared `control.mise` helper for the test-task check, `spawn(require_tests=, mise_init=)`, gitops `is_clean`/`ahead_count`, and `init_mise` on the project-create API (only acts when a git remote exists to push to). Frontend: the checkbox, plumbed through the store, following the launch. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BxKY28XKCM6o4ag3nmaVsZ
129 lines
5.2 KiB
Python
129 lines
5.2 KiB
Python
"""Stop / SessionEnd checkpoint gate."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from handler.control import gitops, mise
|
|
from handler.db import repository as repo
|
|
from handler.hooks import checkpoint, verify
|
|
from handler.hooks.context import HookInput, Identity
|
|
|
|
|
|
def _seed(conn, mise_init=False):
|
|
repo.create_project(conn, "p", "/tmp/p")
|
|
a = repo.create_agent(conn, "p", "a", "/tmp/p/a")
|
|
return Identity(a["id"], "p", "a", "/tmp/p/a", mise_init=mise_init)
|
|
|
|
|
|
def _fake_mise_state(monkeypatch, *, has_test, clean, ahead):
|
|
monkeypatch.setattr(mise, "has_test_task", lambda cwd: has_test)
|
|
monkeypatch.setattr(gitops, "is_clean", lambda cwd: clean)
|
|
monkeypatch.setattr(gitops, "ahead_count", lambda cwd: ahead)
|
|
|
|
|
|
def test_stop_blocks_on_failing_tests(conn, monkeypatch):
|
|
ident = _seed(conn)
|
|
monkeypatch.setattr(verify, "run_test", lambda cwd: (False, "1 failed"))
|
|
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result["decision"] == "block"
|
|
assert "test gate failed" in result["reason"]
|
|
|
|
cm = repo.get_checkmark(conn, ident.agent_id)
|
|
assert cm["tests_status"] == "fail"
|
|
assert cm["status"] == "blocked"
|
|
# A blocked turn never records "done".
|
|
assert repo.get_agent_by_name(conn, "p", "a")["status"] == "blocked"
|
|
|
|
|
|
def test_stop_allows_done_on_passing_tests(conn, monkeypatch):
|
|
ident = _seed(conn)
|
|
monkeypatch.setattr(verify, "run_test", lambda cwd: (True, "ok"))
|
|
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result == {} # no block
|
|
|
|
cm = repo.get_checkmark(conn, ident.agent_id)
|
|
assert cm["tests_status"] == "pass"
|
|
assert cm["status"] == "done"
|
|
assert cm["log_entry_id"] is not None
|
|
|
|
|
|
def test_stop_does_not_reblock_when_already_active(conn, monkeypatch):
|
|
ident = _seed(conn)
|
|
monkeypatch.setattr(verify, "run_test", lambda cwd: (False, "still failing"))
|
|
hi = HookInput({"session_id": "s1", "stop_hook_active": True}, "stop")
|
|
result = checkpoint.handle_stop(conn, ident, hi)
|
|
assert result == {} # recorded, but not an infinite block
|
|
assert repo.get_checkmark(conn, ident.agent_id)["tests_status"] == "fail"
|
|
|
|
|
|
def test_mise_init_stop_blocks_when_no_test_task(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
# The normal test gate must NOT run for a mise-init agent.
|
|
monkeypatch.setattr(
|
|
verify, "run_test", lambda cwd: (_ for _ in ()).throw(AssertionError("test gate ran"))
|
|
)
|
|
_fake_mise_state(monkeypatch, has_test=False, clean=True, ahead=0)
|
|
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result["decision"] == "block"
|
|
assert "[tasks.test]" in result["reason"]
|
|
assert repo.get_agent_by_name(conn, "p", "a")["status"] == "blocked"
|
|
|
|
|
|
def test_mise_init_stop_blocks_on_uncommitted_changes(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
_fake_mise_state(monkeypatch, has_test=True, clean=False, ahead=0)
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result["decision"] == "block"
|
|
assert "uncommitted" in result["reason"]
|
|
|
|
|
|
def test_mise_init_stop_blocks_when_no_upstream(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
_fake_mise_state(monkeypatch, has_test=True, clean=True, ahead=None)
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result["decision"] == "block"
|
|
assert "upstream" in result["reason"]
|
|
|
|
|
|
def test_mise_init_stop_blocks_on_unpushed_commits(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
_fake_mise_state(monkeypatch, has_test=True, clean=True, ahead=2)
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result["decision"] == "block"
|
|
assert "not been pushed" in result["reason"]
|
|
|
|
|
|
def test_mise_init_stop_allows_when_committed_and_pushed(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
_fake_mise_state(monkeypatch, has_test=True, clean=True, ahead=0)
|
|
result = checkpoint.handle_stop(conn, ident, HookInput({"session_id": "s1"}, "stop"))
|
|
assert result == {} # contract met — the turn may end
|
|
cm = repo.get_checkmark(conn, ident.agent_id)
|
|
assert cm["status"] == "done"
|
|
assert repo.get_agent_by_name(conn, "p", "a")["status"] == "done"
|
|
|
|
|
|
def test_mise_init_stop_does_not_reblock_when_already_active(conn, monkeypatch):
|
|
ident = _seed(conn, mise_init=True)
|
|
_fake_mise_state(monkeypatch, has_test=False, clean=True, ahead=0)
|
|
hi = HookInput({"session_id": "s1", "stop_hook_active": True}, "stop")
|
|
result = checkpoint.handle_stop(conn, ident, hi)
|
|
assert result == {} # recorded, but not an infinite block
|
|
|
|
|
|
def test_session_end_records_without_gate(conn, monkeypatch):
|
|
ident = _seed(conn)
|
|
# Even if tests would fail, SessionEnd must not run the gate or block.
|
|
monkeypatch.setattr(
|
|
verify, "run_test", lambda cwd: (_ for _ in ()).throw(AssertionError("gate ran"))
|
|
)
|
|
result = checkpoint.handle_session_end(
|
|
conn, ident, HookInput({"reason": "clear"}, "session_end")
|
|
)
|
|
assert result == {}
|
|
assert repo.get_checkmark(conn, ident.agent_id)["where_it_stopped"].startswith(
|
|
"session ended"
|
|
)
|