diff --git a/kits/profile-dev-kit/README.md b/kits/profile-dev-kit/README.md index 23f7918..ffac19c 100644 --- a/kits/profile-dev-kit/README.md +++ b/kits/profile-dev-kit/README.md @@ -456,6 +456,39 @@ Run the workflow from the repository root, for example: beddel run -i ... workflows/plan-story.yaml ``` +## Delivery: hook policy and proof (Story 8.2) + +The plan, implement and verify-and-fix stages use tracked policies at +`policies/plan.json`, `policies/implement.json` and +`policies/verify-and-fix.json`. Each policy denies shell `git push` and `gh pr +merge` before allowing the other declared shell, read and write classes. The +policy copy and `hook.jsonl` live in one fresh 0700 run directory under +`~/.local/state/beddel/kiro-hook/runs/`; the policy digest is recorded in the +Stage outcome. + +Immediately before each Coding-agent call, the stage writes +`~/.local/state/beddel/kiro-hook/pointers/.json` with +absolute `policy`, `log_path` and `stage` values. The pointer is created with +`O_CREAT|O_EXCL|O_NOFOLLOW` and mode 0600, and nothing is passed through the +agent environment. An existing pointer refuses the call. The pointer is +removed only when its bytes still match what the stage wrote. A pointer left by +a killed run is stale and the operator must remove it before retrying. + +Stage refs contain these flat hook keys: `hook_log_sha256`, `hook_events`, +`hook_pre`, `hook_post`, `hook_denied`, `hook_stop`, `hook_errors`, +`hook_policy_sha256` and `kiro_session`. The Stage outcome accepts up to 32 refs, +so the existing stage refs and these nine flat keys remain in the unchanged +`profile-dev/stage-outcome/1` record schema. With N agent calls, the stage succeeds +only when `hook_stop == N` and `hook_errors == 0`; missing or unreadable proof +uses `hooks.missing`, and malformed or error events use `hooks.errors`. With no +agent calls all nine keys are `null`. Hook policies, pointers and logs stay +outside the records root and are not Evidence, Findings, Verdicts or Approval +inputs. + +The hook wrapper does not normalize shell wrappers or grammar variants beyond +the agent-kit hook's existing rules. The installed agent must use the default +pointer directory. Review gets no pointer or hook proof (D-37). + ## Delivery: implement stage `implement-story` takes `repo_path`, `story_key`, `spec_path`, `branch` and diff --git a/kits/profile-dev-kit/kit.yaml b/kits/profile-dev-kit/kit.yaml index f738e00..c0c4039 100644 --- a/kits/profile-dev-kit/kit.yaml +++ b/kits/profile-dev-kit/kit.yaml @@ -109,6 +109,22 @@ tools: target: "beddel_profile_dev.tools.stage_outcome:profile_record_stage_outcome" description: "Append one create-only Stage outcome record outside the repository" category: profile + - name: profile_hook_open + target: "beddel_profile_dev.tools.hook:profile_hook_open" + description: "Open a private per-stage hook run and copy its tracked policy" + category: profile + - name: profile_hook_arm + target: "beddel_profile_dev.tools.hook:profile_hook_arm" + description: "Arm the exclusive Kiro hook pointer for the repository cwd" + category: profile + - name: profile_hook_disarm + target: "beddel_profile_dev.tools.hook:profile_hook_disarm" + description: "Remove an unchanged Kiro hook pointer after an agent call" + category: profile + - name: profile_hook_proof + target: "beddel_profile_dev.tools.hook:profile_hook_proof" + description: "Merge hook counts and proof gates into a Stage outcome" + category: profile - name: profile_implement_prepare target: "beddel_profile_dev.tools.implement:profile_implement_prepare" description: "Prepare one implementation unit on a feature branch" @@ -231,6 +247,14 @@ targets: target: "beddel_profile_dev.tools.plan:profile_plan_settle" - name: profile_record_stage_outcome target: "beddel_profile_dev.tools.stage_outcome:profile_record_stage_outcome" + - name: profile_hook_open + target: "beddel_profile_dev.tools.hook:profile_hook_open" + - name: profile_hook_arm + target: "beddel_profile_dev.tools.hook:profile_hook_arm" + - name: profile_hook_disarm + target: "beddel_profile_dev.tools.hook:profile_hook_disarm" + - name: profile_hook_proof + target: "beddel_profile_dev.tools.hook:profile_hook_proof" - name: profile_implement_prepare target: "beddel_profile_dev.tools.implement:profile_implement_prepare" - name: profile_implement_settle diff --git a/kits/profile-dev-kit/policies/implement.json b/kits/profile-dev-kit/policies/implement.json new file mode 100644 index 0000000..c6d27de --- /dev/null +++ b/kits/profile-dev-kit/policies/implement.json @@ -0,0 +1 @@ +{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]} diff --git a/kits/profile-dev-kit/policies/plan.json b/kits/profile-dev-kit/policies/plan.json new file mode 100644 index 0000000..c6d27de --- /dev/null +++ b/kits/profile-dev-kit/policies/plan.json @@ -0,0 +1 @@ +{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]} diff --git a/kits/profile-dev-kit/policies/verify-and-fix.json b/kits/profile-dev-kit/policies/verify-and-fix.json new file mode 100644 index 0000000..c6d27de --- /dev/null +++ b/kits/profile-dev-kit/policies/verify-and-fix.json @@ -0,0 +1 @@ +{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]} diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/hook_proof.py b/kits/profile-dev-kit/python/beddel_profile_dev/hook_proof.py new file mode 100644 index 0000000..3ea5f34 --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/hook_proof.py @@ -0,0 +1,163 @@ +# fmt: off +from __future__ import annotations + +import hashlib +import json +import os +import re +import secrets +import stat +from collections.abc import Mapping +from datetime import UTC, datetime +from pathlib import Path +from typing import Any + +from beddel_profile_dev.config import get_kit_root + +HOOK_KEYS = ("hook_log_sha256", "hook_events", "hook_pre", "hook_post", "hook_denied", "hook_stop", "hook_errors", "hook_policy_sha256", "kiro_session") +_STAGE_RE = re.compile(r"[a-z][a-z0-9-]{0,31}") +_SESSION_RE = re.compile(r"[A-Za-z0-9._-]{1,128}") +_MAX_LOG_BYTES = 8 * 1024 * 1024 +_HOOK_SCHEMA = "beddel.kiro-hook/1" + + +def pointers_dir() -> Path: + return Path.home() / ".local/state/beddel/kiro-hook/pointers" + + +def runs_dir() -> Path: + return Path.home() / ".local/state/beddel/kiro-hook/runs" + + +def pointer_path(cwd: str | os.PathLike[str]) -> Path: + digest = hashlib.sha256(os.path.realpath(os.fspath(cwd)).encode()).hexdigest() + return pointers_dir() / f"{digest}.json" + + +def policy_path(stage: str) -> Path: + return get_kit_root() / "policies" / f"{stage}.json" + + +def _private_directory(path: Path) -> bool: + try: + if path.is_symlink(): return False + if not path.exists(): path.mkdir(parents=True, mode=0o700) + info = path.stat() + return stat.S_ISDIR(info.st_mode) and info.st_uid == os.getuid() and not info.st_mode & 0o077 + except OSError: return False + + +def _secure_regular(path: Path, *, maximum: int | None = None) -> bool: + try: + info = path.lstat() + return stat.S_ISREG(info.st_mode) and info.st_uid == os.getuid() and not info.st_mode & 0o077 and (maximum is None or info.st_size <= maximum) + except OSError: return False + + +def _write_exclusive(path: Path, data: bytes, mode: int) -> None: + flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0) + fd = os.open(path, flags, mode) + try: + remaining = data + while remaining: remaining = remaining[os.write(fd, remaining):] + os.fchmod(fd, mode) + os.fsync(fd) + except BaseException: + try: path.unlink() + except OSError: pass + raise + finally: os.close(fd) + + +def open_run(stage: str) -> dict[str, Any]: + if not isinstance(stage, str) or _STAGE_RE.fullmatch(stage) is None: return {"ok": False, "reason": "hooks.unavailable"} + source = policy_path(stage) + try: + if source.is_symlink() or not source.is_file(): return {"ok": False, "reason": "hooks.unavailable"} + policy = source.read_bytes() + root = runs_dir() + if not _private_directory(root): return {"ok": False, "reason": "hooks.unavailable"} + run = root / f"{stage}-{datetime.now(UTC).strftime('%Y%m%dT%H%M%S%fZ')}-{secrets.token_hex(8)}" + try: os.mkdir(run, 0o700) + except FileExistsError: return {"ok": False, "reason": "hooks.unavailable"} + policy_copy, log_path = run / "policy.json", run / "hook.jsonl" + try: _write_exclusive(policy_copy, policy, 0o600) + except OSError: + try: policy_copy.unlink(missing_ok=True); run.rmdir() + except OSError: pass + return {"ok": False, "reason": "hooks.unavailable"} + return {"ok": True, "stage": stage, "run": str(run), "log_path": str(log_path), "policy_path": str(policy_copy), "policy_sha256": hashlib.sha256(policy).hexdigest()} + except (OSError, ValueError): return {"ok": False, "reason": "hooks.unavailable"} + + +def _repo_cwd(repo_path: str) -> Path | None: + try: + repo, cwd = Path(os.path.realpath(repo_path)), Path(os.path.realpath(os.getcwd())) + return repo if repo.is_dir() and cwd == repo else None + except (OSError, TypeError, ValueError): return None + + +def write_pointer(repo_path: str, stage: str, hook: Mapping[str, Any]) -> dict[str, Any]: + repo = _repo_cwd(repo_path) + result = {"armed": False, "reason": "hooks.unavailable", "stage": stage, "log_path": hook.get("log_path") if isinstance(hook, Mapping) else None, "pointer_sha256": None} + if repo is None or not isinstance(hook, Mapping) or hook.get("ok") is not True: return result + log_path, policy = hook.get("log_path"), hook.get("policy_path") + if not isinstance(log_path, str) or not isinstance(policy, str) or not Path(log_path).is_absolute() or not Path(policy).is_absolute() or hook.get("stage") != stage or not _secure_regular(Path(policy)): return result + if not _private_directory(pointers_dir()): return result + pointer = pointer_path(repo) + raw = json.dumps({"log_path": log_path, "policy": policy, "stage": stage}, sort_keys=True, separators=(",", ":")).encode() + try: _write_exclusive(pointer, raw, 0o600) + except FileExistsError: + result["reason"] = "hooks.pointer_exists" + return result + except OSError: return result + result.update(armed=True, reason=None, pointer_sha256=hashlib.sha256(raw).hexdigest()) + return result + + +def remove_pointer(repo_path: str, armed: Mapping[str, Any] | None) -> dict[str, bool]: + if not isinstance(armed, Mapping) or armed.get("armed") is not True: return {"removed": False} + expected, repo = armed.get("pointer_sha256"), _repo_cwd(repo_path) + if repo is None or not isinstance(expected, str): return {"removed": False} + pointer = pointer_path(repo) + try: + if not _secure_regular(pointer) or hashlib.sha256(pointer.read_bytes()).hexdigest() != expected: return {"removed": False} + pointer.unlink() + return {"removed": True} + except OSError: return {"removed": False} + + +def null_keys() -> dict[str, None]: return {key: None for key in HOOK_KEYS} + + +def count_log(log_path: str | os.PathLike[str], policy_sha256: str | None = None) -> dict[str, Any] | None: + path = Path(log_path) + if not _secure_regular(path, maximum=_MAX_LOG_BYTES): return None + try: raw = path.read_bytes() + except OSError: return None + result: dict[str, Any] = null_keys() + result.update({"hook_log_sha256": hashlib.sha256(raw).hexdigest(), "hook_events": 0, "hook_pre": 0, "hook_post": 0, "hook_denied": 0, "hook_stop": 0, "hook_errors": 0}) + sessions: list[str] = [] + for line in raw.splitlines(): + if not line.strip(): continue + result["hook_events"] += 1 + try: record = json.loads(line) + except (json.JSONDecodeError, UnicodeDecodeError, TypeError, RecursionError, ValueError): + result["hook_errors"] += 1 + continue + if not isinstance(record, dict) or record.get("v") != _HOOK_SCHEMA: + result["hook_errors"] += 1 + continue + event, digest = record.get("event"), record.get("policy_sha256") + if event in {"pre", "post", "stop"}: result[f"hook_{event}"] += 1 + if record.get("decision") == "deny": result["hook_denied"] += 1 + if record.get("decision") == "error": result["hook_errors"] += 1 + if policy_sha256 is not None and digest != policy_sha256: result["hook_errors"] += 1 + if result["hook_policy_sha256"] is None and isinstance(digest, str): result["hook_policy_sha256"] = digest + session = record.get("session_id") + if isinstance(session, str) and _SESSION_RE.fullmatch(session): + joined = ",".join([*sessions, session]) + if session not in sessions and len(joined) <= 512: sessions.append(session) + result["kiro_session"] = ",".join(sessions) if sessions else None + return result +# fmt: on diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/hook.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/hook.py new file mode 100644 index 0000000..30d77d9 --- /dev/null +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/hook.py @@ -0,0 +1,150 @@ +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any + +from beddel.tools import beddel_tool + +from beddel_profile_dev._errors import PROFILE_STAGE_OUTCOME_REFUSED, ProfileError +from beddel_profile_dev.hook_proof import ( + count_log, + null_keys, + open_run, + remove_pointer, + write_pointer, +) + + +def _runtime_failure(stage: str | None = None) -> dict[str, Any]: + result: dict[str, Any] = {"ok": False, "reason": "hooks.unavailable"} + if stage is not None: + result["stage"] = stage + return result + + +@beddel_tool( + name="profile_hook_open", + description="Open one private per-stage hook run and copy its tracked policy", + category="profile", +) +def profile_hook_open(stage: str) -> dict[str, Any]: + try: + return open_run(stage) + except Exception: # noqa: BLE001 + return _runtime_failure(stage) + + +@beddel_tool( + name="profile_hook_arm", + description="Arm the exclusive Kiro hook pointer for the repository cwd", + category="profile", +) +def profile_hook_arm( + repo_path: str, stage: str, hook: Mapping[str, Any] | None +) -> dict[str, Any]: + try: + if not isinstance(hook, Mapping): + return { + "armed": False, + "reason": "hooks.unavailable", + "stage": stage, + "log_path": None, + "pointer_sha256": None, + } + return write_pointer(repo_path, stage, hook) + except Exception: # noqa: BLE001 + return { + "armed": False, + "reason": "hooks.unavailable", + "stage": stage, + "log_path": hook.get("log_path") if isinstance(hook, Mapping) else None, + "pointer_sha256": None, + } + + +@beddel_tool( + name="profile_hook_disarm", + description="Remove a Kiro hook pointer only when its bytes are unchanged", + category="profile", +) +def profile_hook_disarm( + repo_path: str, armed: Mapping[str, Any] | None +) -> dict[str, bool]: + try: + return remove_pointer(repo_path, armed) + except Exception: # noqa: BLE001 + return {"removed": False} + + +@beddel_tool( + name="profile_hook_proof", + description="Merge hook counts and proof gates into a Stage outcome", + category="profile", +) +def profile_hook_proof( + stage: str, + hook: Mapping[str, Any] | None, + calls: list[Mapping[str, Any] | None] | None, + settled: Mapping[str, Any] | None, +) -> dict[str, Any]: + if not isinstance(settled, Mapping): + raise ProfileError( + PROFILE_STAGE_OUTCOME_REFUSED, + "stage outcome refused", + {"reason": "settled.invalid"}, + ) + refs = settled.get("refs") + base_refs = dict(refs) if isinstance(refs, Mapping) else {} + entries = calls if isinstance(calls, list) else ([] if calls is None else [calls]) + armed_entries = [ + item + for item in entries + if isinstance(item, Mapping) and item.get("armed") is True + ] + n_calls = len(armed_entries) + hook_keys = null_keys() + + refusal = next( + ( + item.get("reason") or "hooks.unavailable" + for item in entries + if isinstance(item, Mapping) and item.get("armed") is not True + ), + None, + ) + if refusal is not None: + return { + "stage": stage, + "story_key": settled.get("story_key"), + "outcome": "failed", + "reason": refusal if isinstance(refusal, str) else "hooks.unavailable", + "refs": {**base_refs, **hook_keys}, + } + if n_calls == 0: + return { + "stage": stage, + "story_key": settled.get("story_key"), + "outcome": settled.get("outcome"), + "reason": settled.get("reason"), + "refs": {**base_refs, **hook_keys}, + } + + log_path = hook.get("log_path") if isinstance(hook, Mapping) else None + policy_digest = hook.get("policy_sha256") if isinstance(hook, Mapping) else None + counts = count_log(log_path, policy_digest) if isinstance(log_path, str) else None + if counts is None: + reason: str | None = "hooks.missing" + else: + hook_keys = counts + reason = ( + "hooks.missing" + if counts["hook_stop"] != n_calls + else ("hooks.errors" if counts["hook_errors"] > 0 else None) + ) + return { + "stage": stage, + "story_key": settled.get("story_key"), + "outcome": "failed" if reason is not None else settled.get("outcome"), + "reason": reason if reason is not None else settled.get("reason"), + "refs": {**base_refs, **hook_keys}, + } diff --git a/kits/profile-dev-kit/python/beddel_profile_dev/tools/stage_outcome.py b/kits/profile-dev-kit/python/beddel_profile_dev/tools/stage_outcome.py index a83bec3..7fa893b 100644 --- a/kits/profile-dev-kit/python/beddel_profile_dev/tools/stage_outcome.py +++ b/kits/profile-dev-kit/python/beddel_profile_dev/tools/stage_outcome.py @@ -48,7 +48,7 @@ def _has_control(value: str) -> bool: def _valid_refs(refs: object) -> bool: - if not isinstance(refs, dict) or len(refs) > 16: + if not isinstance(refs, dict) or len(refs) > 32: return False for key, value in refs.items(): if not isinstance(key, str) or _REF_KEY.fullmatch(key) is None: diff --git a/kits/profile-dev-kit/tests/_helpers.py b/kits/profile-dev-kit/tests/_helpers.py index 4e7ce9e..09011e9 100644 --- a/kits/profile-dev-kit/tests/_helpers.py +++ b/kits/profile-dev-kit/tests/_helpers.py @@ -3,6 +3,9 @@ from __future__ import annotations import copy +import importlib.util +import io +import json import os import subprocess import sys @@ -295,3 +298,45 @@ def fake_review_env( "FAKE_REVIEW_MODE": mode, "FAKE_REVIEW_LOG": str(tmp_path / "review-calls.jsonl"), } + + +def emit_hook_events(cwd: Path, events: list[Any]) -> None: + """Run the agent-kit hook module for fake CLI events without changing sys.path.""" + from beddel_profile_dev.hook_proof import pointers_dir + + hook_path = ( + Path(__file__).resolve().parents[2] + / "agent-kiro-kit" + / "python" + / "beddel_agent_kiro" + / "hook.py" + ) + spec = importlib.util.spec_from_file_location( + "beddel_agent_kiro_hook_test", hook_path + ) + if spec is None or spec.loader is None: + raise RuntimeError("hook module could not be loaded") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + names = {"pre": "preToolUse", "post": "postToolUse", "stop": "stop"} + for event in events: + event_name = names.get(str(event), str(event)) + payload: dict[str, Any] = { + "cwd": str(cwd), + "hook_event_name": event_name, + "session_id": "fake-session", + } + if isinstance(event, dict): + payload.update(event) + payload["hook_event_name"] = names.get( + str(payload.get("hook_event_name")), str(payload.get("hook_event_name")) + ) + if payload["hook_event_name"] != "stop": + payload.setdefault("tool_name", "read") + payload.setdefault("tool_input", {}) + if payload["hook_event_name"] == "postToolUse": + payload.setdefault("tool_response", {}) + module.main( + ["--pointers", str(pointers_dir())], + io.StringIO(json.dumps(payload)), + ) diff --git a/kits/profile-dev-kit/tests/test_deliver_workflow.py b/kits/profile-dev-kit/tests/test_deliver_workflow.py index fb091cc..cc8aeaf 100644 --- a/kits/profile-dev-kit/tests/test_deliver_workflow.py +++ b/kits/profile-dev-kit/tests/test_deliver_workflow.py @@ -53,7 +53,7 @@ def test_stage_workflows_state_their_coding_agent() -> None: from beddel.primitives import register_builtins from beddel.tools.kits import load_kit -from ._helpers import commit_all, git, stub_gate_tools +from ._helpers import commit_all, emit_hook_events, git, stub_gate_tools from .test_integrate_workflow import _fixture, _Host STORY = "7-7-deliver-story-workflow-chains-stages-and-resumes" @@ -73,6 +73,7 @@ def __init__(self, repo: Path) -> None: async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: self.calls += 1 + emit_hook_events(self.repo, ["pre", "post", "stop"]) if "one plan stage" in prompt: (self.repo / "specs" / "story.md").write_bytes(_spec()) elif "one implementation unit" in prompt: @@ -264,6 +265,7 @@ def test_stage_failure_unrecorded_stage_and_fix_bound_do_not_advance(tmp_path: P class FailingAgent(_CodingAgent): async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: self.calls += 1 + emit_hook_events(self.repo, ["pre", "post", "stop"]) if "one plan stage" in prompt: (self.repo / "specs" / "story.md").write_bytes(_spec()) return AgentResult(0, "", [], [], {}, "fake") diff --git a/kits/profile-dev-kit/tests/test_hook_proof.py b/kits/profile-dev-kit/tests/test_hook_proof.py new file mode 100644 index 0000000..e731914 --- /dev/null +++ b/kits/profile-dev-kit/tests/test_hook_proof.py @@ -0,0 +1,186 @@ +from __future__ import annotations + +import hashlib +import json +import os +import stat +from pathlib import Path +from typing import Any + +import pytest +from beddel_profile_dev.hook_proof import ( + count_log, + null_keys, + pointer_path, + policy_path, +) +from beddel_profile_dev.tools.hook import ( + profile_hook_arm, + profile_hook_disarm, + profile_hook_open, + profile_hook_proof, +) +from beddel_profile_dev.tools.stage_outcome import profile_record_stage_outcome + +from ._helpers import commit_all, emit_hook_events, init_repo + +STORY = "8-2-hook-proof-fixture" + + +def _repo(tmp_path: Path) -> Path: + repo = init_repo(tmp_path / "repo") + (repo / "story.md").write_text("story\n") + commit_all(repo) + return repo + + +def _settled(stage: str = "implement") -> dict[str, Any]: + return { + "stage": stage, + "story_key": STORY, + "outcome": "succeeded", + "reason": "unit.committed", + "refs": {"base": "a" * 40}, + } + + +def _open_arm( + repo: Path, stage: str = "implement" +) -> tuple[dict[str, Any], dict[str, Any]]: + opened = profile_hook_open(stage) + assert opened["ok"] is True + armed = profile_hook_arm(str(repo), stage, opened) + assert armed["armed"] is True + return opened, armed + + +def test_run_pointer_lifecycle_and_foreign_pointer( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + monkeypatch.chdir(repo) + first, armed = _open_arm(repo) + second = profile_hook_open("implement") + assert first["run"] != second["run"] + run, policy, pointer = ( + Path(first["run"]), + Path(first["policy_path"]), + pointer_path(repo), + ) + assert stat.S_IMODE(run.stat().st_mode) == 0o700 + assert stat.S_IMODE(policy.stat().st_mode) == 0o600 + assert policy.read_bytes() == policy_path("implement").read_bytes() + assert first["policy_sha256"] == hashlib.sha256(policy.read_bytes()).hexdigest() + assert ( + profile_hook_arm(str(repo), "implement", first)["reason"] + == "hooks.pointer_exists" + ) + emit_hook_events( + repo, + [ + { + "hook_event_name": "pre", + "tool_name": "shell", + "tool_input": {"command": c}, + } + for c in ("git push origin x", "gh pr merge 1", "ls") + ] + + ["stop"], + ) + proof = profile_hook_proof("implement", first, [armed], _settled()) + assert (proof["outcome"], proof["refs"]["hook_denied"]) == ("succeeded", 2) + assert ( + policy_path("plan").read_bytes() + == policy_path("implement").read_bytes() + == policy_path("verify-and-fix").read_bytes() + ) + assert all( + x in Path(first["log_path"]).read_text() + for x in ("shell.git-push", "shell.gh-pr-merge", "shell.any") + ) + pointer.write_bytes(b"foreign\n") + assert profile_hook_disarm(str(repo), armed) == {"removed": False} + assert pointer.read_bytes() == b"foreign\n" + + +def test_missing_stop_log_and_hook_errors( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + monkeypatch.chdir(repo) + opened, armed = _open_arm(repo) + emit_hook_events(repo, ["pre", "post"]) + profile_hook_disarm(str(repo), armed) + result = profile_hook_proof("implement", opened, [armed], _settled()) + assert result["reason"] == "hooks.missing" and result["refs"]["hook_stop"] == 0 + + opened, armed = _open_arm(repo, "plan") + profile_hook_disarm(str(repo), armed) + result = profile_hook_proof("plan", opened, [armed], _settled("plan")) + assert result["reason"] == "hooks.missing" and result["refs"] == { + **_settled("plan")["refs"], + **null_keys(), + } + + opened, armed = _open_arm(repo) + emit_hook_events(repo, [{"hook_event_name": "unknown"}, "stop"]) + profile_hook_disarm(str(repo), armed) + result = profile_hook_proof("implement", opened, [armed], _settled()) + assert result["reason"] == "hooks.errors" and result["refs"]["hook_errors"] == 1 + log = Path(str(opened["log_path"])) + log.write_bytes( + log.read_bytes() + b"not-json\n" + b"[" * 5000 + b"]" * 5000 + b"\n" + ) + os.chmod(log, 0o600) + counts = count_log(log) + assert counts is not None and counts["hook_errors"] == 3 + insecure = log.parent / "insecure" + insecure.write_text("{}\n") + os.chmod(insecure, 0o644) + assert count_log(insecure) is None + oversized = log.parent / "oversized" + oversized.write_bytes(b"x" * (8 * 1024 * 1024 + 1)) + os.chmod(oversized, 0o600) + assert count_log(oversized) is None + link = log.parent / "link" + link.symlink_to(oversized) + assert count_log(link) is None + + +def test_digest_sessions_cleanup_and_record_boundary( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = _repo(tmp_path) + monkeypatch.setenv("HOME", str(tmp_path / "home")) + monkeypatch.chdir(repo) + opened, armed = _open_arm(repo) + emit_hook_events( + repo, + [ + {"hook_event_name": "stop", "session_id": "s" * 60 + str(i)} + for i in range(10) + ], + ) + log = Path(str(opened["log_path"])) + counts = count_log(log, opened["policy_sha256"]) + assert counts is not None and len(counts["kiro_session"]) <= 512 + raw = json.loads(log.read_text().splitlines()[0]) + raw["policy_sha256"] = "0" * 64 + log.write_text(json.dumps(raw) + "\n") + checked = count_log(log, opened["policy_sha256"]) + assert checked is not None and checked["hook_errors"] == 1 + assert profile_hook_disarm(str(repo), armed)["removed"] is True + refs = {**_settled()["refs"], **counts} + assert profile_record_stage_outcome( + str(repo), STORY, "implement", "succeeded", "unit.committed", refs + )["ok"] + assert sorted(path.name for path in repo.iterdir()) == [".git", "story.md"] + + opened = profile_hook_open("implement") + original = os.fsync + monkeypatch.setattr(os, "fsync", lambda fd: (_ for _ in ()).throw(OSError("x"))) + refused = profile_hook_arm(str(repo), "implement", opened) + monkeypatch.setattr(os, "fsync", original) + assert not refused["armed"] and not pointer_path(repo).exists() diff --git a/kits/profile-dev-kit/tests/test_implement_workflow.py b/kits/profile-dev-kit/tests/test_implement_workflow.py index 6e78685..633bf30 100644 --- a/kits/profile-dev-kit/tests/test_implement_workflow.py +++ b/kits/profile-dev-kit/tests/test_implement_workflow.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import hashlib import json import subprocess from pathlib import Path @@ -20,10 +21,11 @@ from beddel.primitives import register_builtins from beddel.tools.kits import load_kit from beddel_profile_dev._errors import ProfileError +from beddel_profile_dev.hook_proof import HOOK_KEYS, policy_path from beddel_profile_dev.tools import implement as implement_tools from beddel_profile_dev.tools.stage_outcome import profile_record_stage_outcome -from ._helpers import commit_all, git, init_repo, stub_gate_tools +from ._helpers import commit_all, emit_hook_events, git, init_repo, stub_gate_tools from .test_implement import BRANCH, SPEC_PATH, STORY, spec_bytes KIT_ROOT = Path(__file__).resolve().parents[1] @@ -52,6 +54,12 @@ async def execute( output_schema: dict[str, Any] | None = None, ) -> AgentResult: self.calls += 1 + events: list[Any] = ["pre", "post", "stop"] + if self.mode == "missing-stop": + events = ["pre", "post"] + elif self.mode == "hook-error": + events = [{"hook_event_name": "unknown"}, "stop"] + emit_hook_events(self.repo, events) if self.mode == "error": raise AgentError(code=AGENT_EXECUTION_FAILED, message="fake agent failed") if self.mode == "nothing": @@ -156,13 +164,22 @@ def test_workflow_shape_and_safety() -> None: "profile_implement_prepare", "profile_implement_settle", "profile_record_stage_outcome", + "profile_hook_open", + "profile_hook_arm", + "profile_hook_disarm", + "profile_hook_proof", ] assert "Coding agent" in document["description"] assert {step["id"] for step in document["steps"]} == { + "hook_open", "prepare", + "arm", "implement", + "disarm", "settle", "settle_stop", + "proof", + "proof_stop", "record", "record_stop", } @@ -174,9 +191,10 @@ def test_workflow_shape_and_safety() -> None: assert agent["timeout"] <= 600 workflow = WorkflowParser.parse(WORKFLOW_PATH.read_text()) steps = {step.id: step for step in workflow.steps} + assert steps["arm"].if_condition == "$stepResult.prepare.result.implement == true" + assert steps["implement"].if_condition == "$stepResult.arm.result.armed == true" assert ( - steps["implement"].if_condition - == "$stepResult.prepare.result.implement == true" + steps["disarm"].if_condition == "$stepResult.prepare.result.implement == true" ) assert ( steps["settle"].if_condition == "$stepResult.prepare.result.implement == true" @@ -227,13 +245,19 @@ def test_two_unit_sequence_records_and_resumes( assert stop["outcome"] == "succeeded" assert fake.calls == 2 assert git(repo, "rev-parse", "HEAD") == third_head - stored = list((tmp_path / "home").rglob("*.json")) + stored = list((tmp_path / "home").rglob("stage-outcomes/*.json")) assert len(stored) == 3 - assert sorted(json.loads(path.read_text())["record"] for path in stored) == [ - 1, - 2, - 3, - ] + records = [json.loads(path.read_text()) for path in sorted(stored)] + for record in records[:2]: + refs = record["refs"] + assert set(HOOK_KEYS) <= set(refs) + assert refs["hook_stop"] == 1 + assert refs["hook_errors"] == 0 + assert ( + refs["hook_policy_sha256"] + == hashlib.sha256(policy_path("implement").read_bytes()).hexdigest() + ) + assert all(records[2]["refs"][key] is None for key in HOOK_KEYS) @pytest.mark.parametrize( @@ -245,6 +269,8 @@ def test_two_unit_sequence_records_and_resumes( ("error", "agent.failed"), ("switch", "branch.moved"), ("regression", "gate.regressed"), + ("missing-stop", "hooks.missing"), + ("hook-error", "hooks.errors"), ], ) def test_agent_modes_are_recorded_without_losing_base( @@ -260,13 +286,21 @@ def test_agent_modes_are_recorded_without_losing_base( settle = result["step_results"].get( "settle", result["step_results"].get("settle_stop") ) - assert settle["result"]["reason"] == reason - assert settle["result"]["outcome"] == "failed" - records = list((tmp_path / mode / "home").rglob("*.json")) + assert settle["result"]["reason"] == ( + "unit.committed" if reason.startswith("hooks.") else reason + ) + assert settle["result"]["outcome"] == ( + "succeeded" if reason.startswith("hooks.") else "failed" + ) + records = list((tmp_path / mode / "home").rglob("stage-outcomes/*.json")) assert len(records) == 1 stored = json.loads(records[0].read_text()) assert (stored["outcome"], stored["reason"]) == ("failed", reason) - assert git(repo, "rev-parse", "HEAD") == before or reason == "head.moved" + assert git(repo, "rev-parse", "HEAD") == before or reason in { + "head.moved", + "hooks.missing", + "hooks.errors", + } def test_bad_repository_fails_at_record_boundary( @@ -303,7 +337,7 @@ def boom(_repo: Path) -> set[str]: result = _run(repo, base, FakeAgent(repo)) stop = result["step_results"]["settle_stop"]["result"] assert (stop["outcome"], stop["reason"]) == ("failed", "prepare.failed") - records = list((tmp_path / "home").rglob("*.json")) + records = list((tmp_path / "home").rglob("stage-outcomes/*.json")) assert len(records) == 1 stored = json.loads(records[0].read_text()) assert (stored["outcome"], stored["reason"]) == ("failed", "prepare.failed") diff --git a/kits/profile-dev-kit/tests/test_manifest.py b/kits/profile-dev-kit/tests/test_manifest.py index ba03678..f34ec20 100644 --- a/kits/profile-dev-kit/tests/test_manifest.py +++ b/kits/profile-dev-kit/tests/test_manifest.py @@ -33,6 +33,10 @@ "profile_plan_reconcile", "profile_plan_settle", "profile_record_stage_outcome", + "profile_hook_open", + "profile_hook_arm", + "profile_hook_disarm", + "profile_hook_proof", "profile_implement_prepare", "profile_implement_settle", "profile_fix_reconcile", diff --git a/kits/profile-dev-kit/tests/test_plan_workflow.py b/kits/profile-dev-kit/tests/test_plan_workflow.py index 4f44551..b727748 100644 --- a/kits/profile-dev-kit/tests/test_plan_workflow.py +++ b/kits/profile-dev-kit/tests/test_plan_workflow.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import hashlib import json import subprocess from pathlib import Path @@ -20,10 +21,11 @@ from beddel.primitives import register_builtins from beddel.tools.kits import load_kit from beddel_profile_dev._io import records_root +from beddel_profile_dev.hook_proof import HOOK_KEYS, policy_path from beddel_profile_dev.tools.plan import profile_plan_settle from beddel_profile_dev.tools.stage_outcome import profile_record_stage_outcome -from ._helpers import commit_all, init_repo +from ._helpers import commit_all, emit_hook_events, init_repo KIT_ROOT = Path(__file__).resolve().parents[1] WORKFLOW_PATH = KIT_ROOT / "workflows" / "plan-story.yaml" @@ -54,6 +56,12 @@ async def execute( output_schema: dict[str, Any] | None = None, ) -> AgentResult: self.calls += 1 + events: list[Any] = ["pre", "post", "stop"] + if self.mode == "missing-stop": + events = ["pre", "post"] + elif self.mode == "hook-error": + events = [{"hook_event_name": "unknown"}, "stop"] + emit_hook_events(self.repo, events) spec = self.repo / "specs" / "story.md" spec.parent.mkdir(exist_ok=True) if self.mode == "error": @@ -152,12 +160,20 @@ def test_workflow_shape() -> None: "profile_plan_reconcile", "profile_plan_settle", "profile_record_stage_outcome", + "profile_hook_open", + "profile_hook_arm", + "profile_hook_disarm", + "profile_hook_proof", ] assert "Coding agent" in document["description"] assert {step["id"] for step in document["steps"]} == { + "hook_open", "reconcile", + "arm", "author", + "disarm", "settle", + "proof", "record", } author = next(step for step in document["steps"] if step["id"] == "author") @@ -169,8 +185,11 @@ def test_workflow_shape() -> None: workflow = WorkflowParser.parse(WORKFLOW_PATH.read_text()) steps = {step.id: step for step in workflow.steps} assert steps["reconcile"].execution_strategy.type.value == "skip" - assert steps["author"].if_condition == "$stepResult.reconcile.result.author == true" + assert steps["arm"].if_condition == "$stepResult.reconcile.result.author == true" + assert steps["author"].if_condition == "$stepResult.arm.result.armed == true" + assert steps["disarm"].if_condition == "$stepResult.reconcile.result.author == true" assert steps["settle"].execution_strategy.type.value == "fail" + assert steps["proof"].execution_strategy.type.value == "fail" assert steps["record"].execution_strategy.type.value == "fail" @@ -182,6 +201,8 @@ def test_workflow_shape() -> None: ("extra", "failed", "scope.exceeded", 1), ("commit", "failed", "head.moved", 1), ("error", "failed", "spec.incomplete", 1), + ("missing-stop", "succeeded", "hooks.missing", 1), + ("hook-error", "succeeded", "hooks.errors", 1), ], ) def test_workflow_settles_agent_modes( @@ -198,11 +219,23 @@ def test_workflow_settles_agent_modes( fake = FakeAgent(repo, mode) result = _run(repo, fake, monkeypatch) assert result["step_results"]["settle"]["result"]["outcome"] == expected - assert result["step_results"]["settle"]["result"]["reason"] == reason + assert result["step_results"]["settle"]["result"]["reason"] == ( + "spec.created" if reason.startswith("hooks.") else reason + ) assert fake.calls == calls records = records_root(repo, "records/profile-dev") / "stage-outcomes" stored = json.loads((records / "1.json").read_text()) assert stored["reason"] == reason + assert stored["outcome"] == ("failed" if reason.startswith("hooks.") else expected) + if mode == "complete": + refs = stored["refs"] + assert set(HOOK_KEYS) <= set(refs) + assert refs["hook_stop"] == calls + assert refs["hook_errors"] == 0 + assert ( + refs["hook_policy_sha256"] + == hashlib.sha256(policy_path("plan").read_bytes()).hexdigest() + ) def test_reuse_and_refusal_skip_agent_and_still_record( @@ -224,6 +257,7 @@ def test_reuse_and_refusal_skip_agent_and_still_record( second_record = json.loads((records / "2.json").read_text()) assert second_record["record"] == 2 assert second_record["reason"] == "spec.reused" + assert all(second_record["refs"][key] is None for key in HOOK_KEYS) repo2 = _repo(tmp_path / "refusal") (repo2 / "specs").rmdir() diff --git a/kits/profile-dev-kit/tests/test_stage_outcome.py b/kits/profile-dev-kit/tests/test_stage_outcome.py index 3b2acfd..754edad 100644 --- a/kits/profile-dev-kit/tests/test_stage_outcome.py +++ b/kits/profile-dev-kit/tests/test_stage_outcome.py @@ -98,7 +98,7 @@ def racing(root_path: Path, relative: str, data: bytes) -> bool: ({"story_key": None, "outcome": "failed"}, "story_key.required"), ({"refs": {"Bad": 1}}, "refs.invalid"), ({"refs": {"count": -1}}, "refs.invalid"), - ({"refs": {f"key-{index}": index for index in range(17)}}, "refs.invalid"), + ({"refs": {f"key_{index}": index for index in range(33)}}, "refs.invalid"), ({"refs": {"value": "x" * 513}}, "refs.invalid"), ], ) @@ -192,3 +192,15 @@ def test_stage_outcomes_term_is_isolated_to_its_tool() -> None: if "stage-outcomes" in path.read_text() } assert matches == {"stage_outcome.py", "deliver.py"} + + +def test_thirty_two_valid_refs_are_accepted( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + repo = repo_fixture(tmp_path) + record_root(tmp_path, repo, monkeypatch) + refs = {f"key_{index}": index for index in range(32)} + result = profile_record_stage_outcome( + str(repo), "story-1", "plan", "succeeded", "spec.created", refs + ) + assert result["ok"] is True diff --git a/kits/profile-dev-kit/tests/test_verify_fix_workflow.py b/kits/profile-dev-kit/tests/test_verify_fix_workflow.py index dde46ac..4e4b955 100644 --- a/kits/profile-dev-kit/tests/test_verify_fix_workflow.py +++ b/kits/profile-dev-kit/tests/test_verify_fix_workflow.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import hashlib import json from pathlib import Path from typing import Any @@ -21,10 +22,12 @@ ASSESSMENT_END, REVIEW_ASSESSMENT_SCHEMA, ) +from beddel_profile_dev.hook_proof import HOOK_KEYS, policy_path from ._helpers import ( FakeReviewAdapter, commit_all, + emit_hook_events, git, good, init_repo, @@ -54,6 +57,10 @@ def test_workflow_shape_and_bound() -> None: "profile_fix_reconcile", "profile_fix_settle", "profile_record_stage_outcome", + "profile_hook_open", + "profile_hook_arm", + "profile_hook_disarm", + "profile_hook_proof", ] assert "mcp_servers" not in document assert not any( @@ -62,19 +69,25 @@ def test_workflow_shape_and_bound() -> None: ) steps = WorkflowParser.parse(WORKFLOW_PATH.read_text()).steps assert [step.id for step in steps] == [ + "hook_open", "reconcile", "verify", "round_1", + "arm_1", "fix_1", + "disarm_1", "settle_1", "settle_1_stop", "verify_1", "round_2", + "arm_2", "fix_2", + "disarm_2", "settle_2", "settle_2_stop", "verify_2", "close", + "proof", "record", ] assert all( @@ -105,17 +118,21 @@ def test_workflow_shape_and_bound() -> None: for step in nested ) assert all(step.execution_strategy.type is StrategyType.SKIP for step in nested) - assert steps[4].output_key == steps[5].output_key == "fixed_1" - assert steps[9].output_key == steps[10].output_key == "fixed_2" + assert steps[7].output_key == steps[8].output_key == "fixed_1" + assert steps[14].output_key == steps[15].output_key == "fixed_2" assert { step.id: step.if_condition for step in steps if step.if_condition is not None } == { "verify": "$stepResult.reconcile.result.verify == true", - "fix_1": "$stepResult.round_1.result.fix == true", + "arm_1": "$stepResult.round_1.result.fix == true", + "fix_1": "$stepResult.arm_1.result.armed == true", + "disarm_1": "$stepResult.round_1.result.fix == true", "settle_1": "$stepResult.round_1.result.fix == true", "settle_1_stop": "$stepResult.round_1.result.fix != true", "verify_1": "$stepResult.fixed_1.result.verify == true", - "fix_2": "$stepResult.round_2.result.fix == true", + "arm_2": "$stepResult.round_2.result.fix == true", + "fix_2": "$stepResult.arm_2.result.armed == true", + "disarm_2": "$stepResult.round_2.result.fix == true", "settle_2": "$stepResult.round_2.result.fix == true", "settle_2_stop": "$stepResult.round_2.result.fix != true", "verify_2": "$stepResult.fixed_2.result.verify == true", @@ -123,7 +140,7 @@ def test_workflow_shape_and_bound() -> None: assert all( step.execution_strategy.type is StrategyType.FAIL for step in steps - if step.primitive == "tool" and step.id != "reconcile" + if step.primitive == "tool" and step.id not in {"reconcile", "hook_open"} ) @@ -172,9 +189,15 @@ def _repo(tmp_path: Path) -> tuple[Path, str]: class FixAgent: - def __init__(self, repo: Path, monkeypatch: pytest.MonkeyPatch) -> None: + def __init__( + self, + repo: Path, + monkeypatch: pytest.MonkeyPatch, + hook_mode: str = "complete", + ) -> None: self.repo = repo self.monkeypatch = monkeypatch + self.hook_mode = hook_mode self.calls = 0 async def execute( @@ -187,6 +210,12 @@ async def execute( output_schema: dict[str, Any] | None = None, ) -> AgentResult: self.calls += 1 + events: list[Any] = ["pre", "post", "stop"] + if self.hook_mode == "missing-stop": + events = ["pre", "post"] + elif self.hook_mode == "hook-error": + events = [{"hook_event_name": "unknown"}, "stop"] + emit_hook_events(self.repo, events) (self.repo / "src/api.py").write_text("x = 3\n") self.monkeypatch.setenv("STUB_EXIT_PYTEST", "0") return AgentResult(0, "", [], [], {}, "fake") @@ -264,6 +293,10 @@ def test_in_process_consistent_run_has_one_report_and_no_fix_agent( 0, ) assert len(list((tmp_path / "home").rglob("changes/c1/report/*.json"))) == 1 + stored = json.loads( + next((tmp_path / "home").rglob("stage-outcomes/*.json")).read_text() + ) + assert all(stored["refs"][key] is None for key in HOOK_KEYS) # fmt: off @@ -299,18 +332,30 @@ def test_in_process_fix_round_commits_and_reverifies( links = [link for link in second["reverification"]["links"] if link["class"] == "gate-unit"] assert links and all(link["result"] == "resolved" for link in links) assert all(not item["ref"].startswith(f"revisions/{first['revision']}/") for item in second["reads"] if "ref" in item) + record = json.loads( + next((tmp_path / "home").rglob("stage-outcomes/*.json")).read_text() + ) + refs = record["refs"] + assert set(HOOK_KEYS) <= set(refs) + assert refs["hook_stop"] == 1 + assert refs["hook_errors"] == 0 + assert refs["hook_policy_sha256"] == hashlib.sha256( + policy_path("verify-and-fix").read_bytes() + ).hexdigest() class ErrorAgent(FixAgent): async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: from beddel.domain.errors import AgentError from beddel.error_codes import AGENT_EXECUTION_FAILED self.calls += 1 + emit_hook_events(self.repo, ["pre", "post", "stop"]) raise AgentError(code=AGENT_EXECUTION_FAILED, message="x") class PersistentAgent(FixAgent): async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: self.calls += 1 + emit_hook_events(self.repo, ["pre", "post", "stop"]) (self.repo / "src/api.py").write_text(f"x = {self.calls + 2}\n") return AgentResult(0, "", [], [], {}, "fake") @@ -318,6 +363,7 @@ async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: class OutOfScopeAgent(FixAgent): async def execute(self, prompt: str, **kwargs: Any) -> AgentResult: self.calls += 1 + emit_hook_events(self.repo, ["pre", "post", "stop"]) (self.repo / "README.md").write_text("outside\n") return AgentResult(0, "", [], [], {}, "fake") @@ -350,7 +396,11 @@ def test_workflow_persistent_finding_hits_bound_and_rerun_reuses_report( assert not list(home.rglob("changes/c1/approval.json")) rerun = _run_stage(repo, fake, base) assert len(list(home.rglob("changes/c1/report/*.json"))) == 3 - assert [path.name for path in sorted((tmp_path / "home").rglob("stage-outcomes/*.json"))] == ["1.json", "2.json"] + records = sorted(home.rglob("stage-outcomes/*.json")) + assert [path.name for path in records] == ["1.json", "2.json"] + first_stage = json.loads(records[0].read_text()) + assert first_stage["refs"]["hook_stop"] == 2 + assert first_stage["refs"]["hook_errors"] == 0 assert rerun["step_results"]["record"]["result"]["record"] == 2 @@ -393,3 +443,27 @@ def test_workflow_maps_agent_error_refusal_timeout_inventory_and_scope( # fmt: on + + +@pytest.mark.parametrize( + ("hook_mode", "reason"), + [("missing-stop", "hooks.missing"), ("hook-error", "hooks.errors")], +) +def test_workflow_records_hook_proof_failure( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + hook_mode: str, + reason: str, +) -> None: + repo, base = _repo(tmp_path / hook_mode) + monkeypatch.chdir(repo) + monkeypatch.setenv("PATH", stub_gate_tools(tmp_path / hook_mode)) + monkeypatch.setenv("HOME", str(tmp_path / hook_mode / "home")) + monkeypatch.setenv("STUB_EXIT_PYTEST", "1") + _install_review(monkeypatch) + result = _run_stage(repo, FixAgent(repo, monkeypatch, hook_mode), base) + record = json.loads( + next((tmp_path / hook_mode / "home").rglob("stage-outcomes/*.json")).read_text() + ) + assert record["reason"] == reason + assert result["step_results"]["record"]["result"]["record"] == 1 diff --git a/kits/profile-dev-kit/workflows/implement-story.yaml b/kits/profile-dev-kit/workflows/implement-story.yaml index 9e6ed3f..09abc82 100644 --- a/kits/profile-dev-kit/workflows/implement-story.yaml +++ b/kits/profile-dev-kit/workflows/implement-story.yaml @@ -9,6 +9,10 @@ allowed_tools: - profile_implement_prepare - profile_implement_settle - profile_record_stage_outcome + - profile_hook_open + - profile_hook_arm + - profile_hook_disarm + - profile_hook_proof inputs: - repo_path - story_key @@ -16,6 +20,14 @@ inputs: - branch - base_ref steps: + - id: hook_open + primitive: tool + execution_strategy: + type: skip + config: + tool: profile_hook_open + arguments: + stage: implement - id: prepare primitive: tool execution_strategy: @@ -28,9 +40,20 @@ steps: spec_path: "$input.spec_path" branch: "$input.branch" base_ref: "$input.base_ref" + - id: arm + primitive: tool + if: "$stepResult.prepare.result.implement == true" + execution_strategy: + type: fail + config: + tool: profile_hook_arm + arguments: + repo_path: "$input.repo_path" + stage: implement + hook: "$stepResult.hook_open.result" - id: implement primitive: agent-exec - if: "$stepResult.prepare.result.implement == true" + if: "$stepResult.arm.result.armed == true" execution_strategy: type: skip timeout: 600 @@ -44,6 +67,16 @@ steps: $stepResult.prepare.result.unit_id Change only those paths and files under them. Leave the spec unchanged. Do not stage, commit, push, create or switch branches, approve, verify or integrate. + - id: disarm + primitive: tool + if: "$stepResult.prepare.result.implement == true" + execution_strategy: + type: fail + config: + tool: profile_hook_disarm + arguments: + repo_path: "$input.repo_path" + armed: "$stepResult.arm.result" - id: settle primitive: tool execution_strategy: @@ -73,6 +106,31 @@ steps: branch: "$input.branch" base_ref: "$input.base_ref" prepare: "$stepResult.prepare.result" + - id: proof + primitive: tool + if: "$stepResult.prepare.result.implement == true" + execution_strategy: + type: fail + config: + tool: profile_hook_proof + arguments: + stage: implement + hook: "$stepResult.hook_open.result" + calls: + - "$stepResult.arm.result" + settled: "$stepResult.settle.result" + - id: proof_stop + primitive: tool + if: "$stepResult.prepare.result.implement != true" + execution_strategy: + type: fail + config: + tool: profile_hook_proof + arguments: + stage: implement + hook: "$stepResult.hook_open.result" + calls: [] + settled: "$stepResult.settle_stop.result" - id: record primitive: tool execution_strategy: @@ -82,11 +140,11 @@ steps: tool: profile_record_stage_outcome arguments: repo_path: "$input.repo_path" - story_key: "$stepResult.settle.result.story_key" - stage: implement - outcome: "$stepResult.settle.result.outcome" - reason: "$stepResult.settle.result.reason" - refs: "$stepResult.settle.result.refs" + story_key: "$stepResult.proof.result.story_key" + stage: "$stepResult.proof.result.stage" + outcome: "$stepResult.proof.result.outcome" + reason: "$stepResult.proof.result.reason" + refs: "$stepResult.proof.result.refs" - id: record_stop primitive: tool execution_strategy: @@ -96,8 +154,8 @@ steps: tool: profile_record_stage_outcome arguments: repo_path: "$input.repo_path" - story_key: "$stepResult.settle_stop.result.story_key" - stage: implement - outcome: "$stepResult.settle_stop.result.outcome" - reason: "$stepResult.settle_stop.result.reason" - refs: "$stepResult.settle_stop.result.refs" + story_key: "$stepResult.proof_stop.result.story_key" + stage: "$stepResult.proof_stop.result.stage" + outcome: "$stepResult.proof_stop.result.outcome" + reason: "$stepResult.proof_stop.result.reason" + refs: "$stepResult.proof_stop.result.refs" diff --git a/kits/profile-dev-kit/workflows/plan-story.yaml b/kits/profile-dev-kit/workflows/plan-story.yaml index 6dda160..d159215 100644 --- a/kits/profile-dev-kit/workflows/plan-story.yaml +++ b/kits/profile-dev-kit/workflows/plan-story.yaml @@ -9,6 +9,10 @@ allowed_tools: - profile_plan_reconcile - profile_plan_settle - profile_record_stage_outcome + - profile_hook_open + - profile_hook_arm + - profile_hook_disarm + - profile_hook_proof inputs: - repo_path - story_key @@ -16,6 +20,14 @@ inputs: - guide_path - spec_path steps: + - id: hook_open + primitive: tool + execution_strategy: + type: skip + config: + tool: profile_hook_open + arguments: + stage: plan - id: reconcile primitive: tool execution_strategy: @@ -28,9 +40,20 @@ steps: story_path: "$input.story_path" guide_path: "$input.guide_path" spec_path: "$input.spec_path" + - id: arm + primitive: tool + if: "$stepResult.reconcile.result.author == true" + execution_strategy: + type: fail + config: + tool: profile_hook_arm + arguments: + repo_path: "$input.repo_path" + stage: plan + hook: "$stepResult.hook_open.result" - id: author primitive: agent-exec - if: "$stepResult.reconcile.result.author == true" + if: "$stepResult.arm.result.armed == true" execution_strategy: type: skip timeout: 600 @@ -47,6 +70,16 @@ steps: set story_key to $input.story_key and size_cap_bytes to 64000. Keep the spec within the 64000-byte cap. Change no other file. Do not implement, test, verify, commit or push. + - id: disarm + primitive: tool + if: "$stepResult.reconcile.result.author == true" + execution_strategy: + type: fail + config: + tool: profile_hook_disarm + arguments: + repo_path: "$input.repo_path" + armed: "$stepResult.arm.result" - id: settle primitive: tool execution_strategy: @@ -60,6 +93,18 @@ steps: guide_path: "$input.guide_path" spec_path: "$input.spec_path" reconcile: "$stepResult.reconcile.result" + - id: proof + primitive: tool + execution_strategy: + type: fail + config: + tool: profile_hook_proof + arguments: + stage: plan + hook: "$stepResult.hook_open.result" + calls: + - "$stepResult.arm.result" + settled: "$stepResult.settle.result" - id: record primitive: tool execution_strategy: @@ -68,8 +113,8 @@ steps: tool: profile_record_stage_outcome arguments: repo_path: "$input.repo_path" - story_key: "$stepResult.settle.result.story_key" - stage: plan - outcome: "$stepResult.settle.result.outcome" - reason: "$stepResult.settle.result.reason" - refs: "$stepResult.settle.result.refs" + story_key: "$stepResult.proof.result.story_key" + stage: "$stepResult.proof.result.stage" + outcome: "$stepResult.proof.result.outcome" + reason: "$stepResult.proof.result.reason" + refs: "$stepResult.proof.result.refs" diff --git a/kits/profile-dev-kit/workflows/verify-and-fix-story.yaml b/kits/profile-dev-kit/workflows/verify-and-fix-story.yaml index e215ad6..75fcd38 100644 --- a/kits/profile-dev-kit/workflows/verify-and-fix-story.yaml +++ b/kits/profile-dev-kit/workflows/verify-and-fix-story.yaml @@ -9,6 +9,10 @@ allowed_tools: - profile_fix_reconcile - profile_fix_settle - profile_record_stage_outcome + - profile_hook_open + - profile_hook_arm + - profile_hook_disarm + - profile_hook_proof inputs: - repo_path - story_key @@ -18,6 +22,14 @@ inputs: - change_id - inventory_version steps: + - id: hook_open + primitive: tool + execution_strategy: + type: skip + config: + tool: profile_hook_open + arguments: + stage: verify-and-fix - id: reconcile primitive: tool execution_strategy: @@ -61,9 +73,20 @@ steps: change_id: "$input.change_id" max_fix_rounds: 2 prior: "$stepResult.reconcile.result" + - id: arm_1 + primitive: tool + if: "$stepResult.round_1.result.fix == true" + execution_strategy: + type: fail + config: + tool: profile_hook_arm + arguments: + repo_path: "$input.repo_path" + stage: verify-and-fix + hook: "$stepResult.hook_open.result" - id: fix_1 primitive: agent-exec - if: "$stepResult.round_1.result.fix == true" + if: "$stepResult.arm_1.result.armed == true" execution_strategy: type: skip timeout: 600 @@ -77,6 +100,16 @@ steps: $stepResult.round_1.result.scope_paths; change only those paths and leave the spec unchanged. Do not stage, commit, push, create or switch branches, approve, verify, ratify or integrate. + - id: disarm_1 + primitive: tool + if: "$stepResult.round_1.result.fix == true" + execution_strategy: + type: fail + config: + tool: profile_hook_disarm + arguments: + repo_path: "$input.repo_path" + armed: "$stepResult.arm_1.result" - id: settle_1 primitive: tool if: "$stepResult.round_1.result.fix == true" @@ -138,9 +171,20 @@ steps: change_id: "$input.change_id" max_fix_rounds: 2 prior: "$stepResult.fixed_1.result" + - id: arm_2 + primitive: tool + if: "$stepResult.round_2.result.fix == true" + execution_strategy: + type: fail + config: + tool: profile_hook_arm + arguments: + repo_path: "$input.repo_path" + stage: verify-and-fix + hook: "$stepResult.hook_open.result" - id: fix_2 primitive: agent-exec - if: "$stepResult.round_2.result.fix == true" + if: "$stepResult.arm_2.result.armed == true" execution_strategy: type: skip timeout: 600 @@ -154,6 +198,16 @@ steps: $stepResult.round_2.result.scope_paths; change only those paths and leave the spec unchanged. Do not stage, commit, push, create or switch branches, approve, verify, ratify or integrate. + - id: disarm_2 + primitive: tool + if: "$stepResult.round_2.result.fix == true" + execution_strategy: + type: fail + config: + tool: profile_hook_disarm + arguments: + repo_path: "$input.repo_path" + armed: "$stepResult.arm_2.result" - id: settle_2 primitive: tool if: "$stepResult.round_2.result.fix == true" @@ -215,6 +269,19 @@ steps: change_id: "$input.change_id" max_fix_rounds: 2 prior: "$stepResult.fixed_2.result" + - id: proof + primitive: tool + execution_strategy: + type: fail + config: + tool: profile_hook_proof + arguments: + stage: verify-and-fix + hook: "$stepResult.hook_open.result" + calls: + - "$stepResult.arm_1.result" + - "$stepResult.arm_2.result" + settled: "$stepResult.close.result" - id: record primitive: tool execution_strategy: @@ -223,8 +290,8 @@ steps: tool: profile_record_stage_outcome arguments: repo_path: "$input.repo_path" - story_key: "$stepResult.close.result.story_key" - stage: verify-and-fix - outcome: "$stepResult.close.result.outcome" - reason: "$stepResult.close.result.reason" - refs: "$stepResult.close.result.refs" + story_key: "$stepResult.proof.result.story_key" + stage: "$stepResult.proof.result.stage" + outcome: "$stepResult.proof.result.outcome" + reason: "$stepResult.proof.result.reason" + refs: "$stepResult.proof.result.refs"