From cc9ac2893724b948397167fad54bfaa96f4435e5 Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Tue, 17 Nov 2026 19:07:18 -0300 Subject: [PATCH 1/3] feat(agent-kiro-kit): run a pinned agent with policy hooks (Story 8.1) --- kits/agent-kiro-kit/README.md | 35 +- .../python/beddel_agent_kiro/__init__.py | 13 +- .../python/beddel_agent_kiro/adapter.py | 99 ++-- .../python/beddel_agent_kiro/agent_config.py | 148 ++++++ .../beddel-delivery.template.json | 56 +++ .../python/beddel_agent_kiro/errors.py | 11 + .../python/beddel_agent_kiro/hook.py | 434 ++++++++++++++++++ .../agent-kiro-kit/tests/test_agent_config.py | 73 +++ .../tests/test_delivery_stage.py | 141 +++++- kits/agent-kiro-kit/tests/test_headless.py | 79 +--- kits/agent-kiro-kit/tests/test_hook.py | 253 ++++++++++ kits/agent-kiro-kit/tests/test_kiro_cli.py | 37 +- 12 files changed, 1248 insertions(+), 131 deletions(-) create mode 100644 kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py create mode 100644 kits/agent-kiro-kit/python/beddel_agent_kiro/beddel-delivery.template.json create mode 100644 kits/agent-kiro-kit/python/beddel_agent_kiro/errors.py create mode 100644 kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py create mode 100644 kits/agent-kiro-kit/tests/test_agent_config.py create mode 100644 kits/agent-kiro-kit/tests/test_hook.py diff --git a/kits/agent-kiro-kit/README.md b/kits/agent-kiro-kit/README.md index 764289fc..8973d8e8 100644 --- a/kits/agent-kiro-kit/README.md +++ b/kits/agent-kiro-kit/README.md @@ -2,6 +2,8 @@ Kiro CLI agent adapter for the Beddel SDK. Implements the `IAgentAdapter` port to execute prompts via the Kiro CLI subprocess. +Migration: install agent + hook. + ## Dependencies None — uses subprocess only (no pip dependencies). @@ -42,6 +44,9 @@ A Delivery stage declares trust through the step's `config.sandbox` value: - Headless mode ignores `sandbox` and uses the adapter's `trust_tools`; the default is `--trust-all-tools`. +Every invocation pins `--agent-engine v3` and passes `--agent`: `tools[0]` or +constructor `agent` (`beddel-delivery`). + The step's `timeout` is the stage time bound. When `beddel run` builds this adapter with no constructor arguments, the adapter timeout is 600 seconds, so the step timeout must be at most 600 seconds. A step timeout kills and reaps @@ -56,6 +61,20 @@ Delivery stage reads repository changes separately. Adapter errors use timeouts. A step timeout is raised as `BEDDEL-EXEC-005`; under the default FAIL strategy the step failure `BEDDEL-EXEC-002` wraps it as its cause. +`/.json` has absolute paths. +Schema `beddel.kiro-hook-policy/1`; first-match-wins, no rules deny all. +Hook exit 2 means pointer/policy/denial/error or missing log dir. Python +3.11+ is required for `python3 -P`. + +Install and verify the operator-owned agent outside a workflow: + +```bash +python3 -m beddel_agent_kiro.agent_config install --home DIR --pointers DIR [--replace] +python3 -m beddel_agent_kiro.agent_config verify --home DIR --pointers DIR +``` + +POSIX; writes `~/.kiro/agents/beddel-delivery.json`. + ## Headless Mode (CI/CD) For containers, CI/CD pipelines, and headless environments where browser-based SSO is unavailable, set the `KIRO_API_KEY` environment variable. The adapter automatically detects it and activates headless mode. @@ -93,6 +112,8 @@ adapter = KiroCLIAgentAdapter( | `timeout` | `int` | `600` | Max execution time in seconds | | `api_key` | `str \| None` | `None` | API key for headless mode (falls back to `KIRO_API_KEY` env var) | | `trust_tools` | `list[str] \| None` | `None` | Tool categories to trust in headless mode (`None` = trust all) | +| `agent` | `str` | `"beddel-delivery"` | Agent when `tools` is absent | +| `pointers_dir` | `Path \| None` | `None` | Hook pointer directory | ### Docker Example @@ -138,17 +159,23 @@ jobs: ### Binary Auto-Discovery -When `cli_path` is not provided, the adapter discovers `kiro-cli` via: +If `cli_path` is not provided, the adapter discovers an already-installed +`kiro-cli` via: 1. Default path: `~/.local/bin/kiro-cli` 2. PATH lookup: `shutil.which("kiro-cli")` -3. Auto-install: `curl -fsSL https://cli.kiro.dev/install | bash` -If all steps fail, an `AgentError(BEDDEL-AGENT-701)` is raised. +If both fail, `KIRO_CLI_MISSING`; manual text. No auto-install/network call. + +Schema `beddel.kiro-hook/1`; fields: +`v`, `ts`, `seq`, `session_id`, `event`, `tool_name`, `tool_class`, `cmd_head`, +`input_sha256`, `input_len`, `response_sha256`, `response_len`, `decision`, +`rule_id`, `policy_sha256`, and `hook_ms`. Raw values never appear; results +contain hook counts and the log digest. ## Running Tests -Run the kit tests from the kit directory with the Beddel SDK installed: +Run tests from its directory: ```bash pytest -q diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/__init__.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/__init__.py index f4d9d5ba..d6ed9d4a 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/__init__.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/__init__.py @@ -1,5 +1,16 @@ """Beddel Kiro CLI agent adapter kit.""" -from beddel_agent_kiro.adapter import KiroCLIAgentAdapter +from __future__ import annotations + +from typing import Any __all__ = ["KiroCLIAgentAdapter"] + + +def __getattr__(name: str) -> Any: + """Load the SDK-dependent adapter without burdening the stdlib hook.""" + if name == "KiroCLIAgentAdapter": + from beddel_agent_kiro.adapter import KiroCLIAgentAdapter + + return KiroCLIAgentAdapter + raise AttributeError(name) diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py index 604be07a..0110ddf9 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py @@ -12,9 +12,8 @@ The CLI reports no changed-file list, so successful results always use ``files_changed=[]``. -The CLI is auto-discovered from ``~/.local/bin/kiro-cli`` or via -``shutil.which``. If neither resolves, the adapter attempts a one-time -auto-install from ``https://cli.kiro.dev/install``. +The CLI is discovered from ``~/.local/bin/kiro-cli`` or via +``shutil.which``. The adapter never installs the CLI during a workflow run. Headless mode is activated when an ``api_key`` is provided or the ``KIRO_API_KEY`` environment variable is set. In headless mode, the @@ -28,7 +27,6 @@ import contextlib import os import shutil -import subprocess from collections.abc import AsyncGenerator from pathlib import Path from typing import Any @@ -41,6 +39,9 @@ AGENT_TIMEOUT, ) +from beddel_agent_kiro import hook +from beddel_agent_kiro.errors import KIRO_AGENT_INVALID, KIRO_CLI_MISSING + __all__ = ["KiroCLIAgentAdapter"] _DEFAULT_CLI_PATH = Path.home() / ".local" / "bin" / "kiro-cli" @@ -77,8 +78,7 @@ class KiroCLIAgentAdapter: model: Default model identifier for CLI invocations. cli_path: Explicit path to the ``kiro-cli`` binary. When ``None``, auto-discovered from ``~/.local/bin/kiro-cli``, then - ``shutil.which``, then auto-installed from the official - installer script. + ``shutil.which``. Missing binaries raise ``KIRO_CLI_MISSING``. timeout: Maximum execution time in seconds. The subprocess is terminated and reaped when this bound expires or the caller cancels the execution. @@ -90,6 +90,8 @@ class KiroCLIAgentAdapter: When ``None`` and headless is active, ``--trust-all-tools`` is appended. When a list is provided, ``--trust-tools=`` is appended instead. + agent: Installed Kiro agent name used when ``tools`` is absent. + pointers_dir: Directory containing cwd-keyed stage policy pointers. """ def __init__( @@ -99,12 +101,16 @@ def __init__( timeout: int = 600, api_key: str | None = None, trust_tools: list[str] | None = None, + agent: str = "beddel-delivery", + pointers_dir: Path | None = None, ) -> None: self._model = model self._cli_path = cli_path or self._discover_cli() self._timeout = timeout self._headless: bool = bool(api_key or os.environ.get("KIRO_API_KEY")) self._trust_tools = trust_tools + self._agent = agent + self._pointers_dir = pointers_dir or hook.DEFAULT_POINTERS_DIR # ------------------------------------------------------------------ # Binary discovery @@ -112,55 +118,31 @@ def __init__( @staticmethod def _discover_cli() -> Path: - """Discover or auto-install the ``kiro-cli`` binary. + """Discover the already-installed ``kiro-cli`` binary. Discovery order: 1. ``~/.local/bin/kiro-cli`` (default install location) 2. ``shutil.which("kiro-cli")`` (PATH lookup) - 3. Auto-install via official installer script, then retry step 1 - - Returns: - Path to the resolved ``kiro-cli`` binary. Raises: - AgentError: ``BEDDEL-AGENT-701`` if discovery and auto-install - both fail. + AgentError: ``KIRO_CLI_MISSING`` when neither location resolves. """ - # Step 1: default location if _DEFAULT_CLI_PATH.exists(): return _DEFAULT_CLI_PATH - # Step 2: PATH lookup which_result = shutil.which("kiro-cli") if which_result is not None: return Path(which_result) - # Step 3: auto-install - try: - subprocess.run( - ["bash", "-c", "curl -fsSL https://cli.kiro.dev/install | bash"], - capture_output=True, - timeout=60, - ) - except (subprocess.TimeoutExpired, OSError): - pass - - # Retry default location after install attempt - if _DEFAULT_CLI_PATH.exists(): - return _DEFAULT_CLI_PATH - - # Step 4: give up raise AgentError( - code=AGENT_EXECUTION_FAILED, - message="kiro-cli not found and auto-install failed", + code=KIRO_CLI_MISSING, + message="kiro-cli is not installed or discoverable", details={ - "searched": [ - str(_DEFAULT_CLI_PATH), - "shutil.which('kiro-cli')", - "curl -fsSL https://cli.kiro.dev/install | bash", - ], + "searched": [str(_DEFAULT_CLI_PATH), "shutil.which('kiro-cli')"], "hint": ( - "Install manually: curl -fsSL https://cli.kiro.dev/install | bash" + "Install kiro-cli manually: https://kiro.dev/docs/cli/ " + "(for example curl -fsSL https://cli.kiro.dev/install | bash), " + "then re-run." ), }, ) @@ -191,9 +173,9 @@ async def execute( ``--trust-tools=`` (empty value), ``"workspace-write"`` and ``"danger-full-access"`` append ``-a``. Ignored in headless mode. - tools: Optional list of tool/agent config names. When a - single-element list is provided, appends - ``--agent ``. + tools: Optional list of tool/agent config names. A single + element overrides the configured agent; an empty or absent + list uses the configured default agent. output_schema: Optional JSON Schema dict (currently unused by the CLI backend). @@ -208,6 +190,7 @@ async def execute( ``BEDDEL-AGENT-701`` on non-zero exit code. """ model_used = model or self._model + agent_used = tools[0] if tools else self._agent cmd = self._build_command(prompt, model=model_used, sandbox=sandbox, tools=tools) proc = await asyncio.create_subprocess_exec( @@ -237,6 +220,16 @@ async def execute( stderr_text = stderr_bytes.decode("utf-8", errors="replace") if stderr_bytes else "" if proc.returncode != 0: + if proc.returncode == 4: + raise AgentError( + code=KIRO_AGENT_INVALID, + message=f"Kiro CLI agent is not installed: {agent_used}", + details={ + "exit_code": proc.returncode, + "agent": agent_used, + "hint": "Install or verify the agent with beddel_agent_kiro.agent_config.", + }, + ) raise AgentError( code=AGENT_EXECUTION_FAILED, message=f"Kiro CLI exited with code {proc.returncode}", @@ -250,7 +243,7 @@ async def execute( return AgentResult( exit_code=proc.returncode, output=stdout_text, - events=[], + events=[hook.summarize(self._pointers_dir, Path.cwd())], files_changed=[], usage={"model": model_used, "timeout": self._timeout}, agent_id="kiro-cli", @@ -375,20 +368,20 @@ def _build_command( }, ) - # Model - cmd.extend(["--model", model]) + # Model and pinned engine + cmd.extend(["--agent-engine", "v3", "--model", model]) # Tools → --agent (CLI supports at most one --agent flag) - if tools: - if len(tools) > 1: - raise AgentError( - code=AGENT_EXECUTION_FAILED, - message=f"Kiro CLI supports at most one agent, got {len(tools)}", - details={"tools": tools}, - ) - cmd.extend(["--agent", tools[0]]) + if tools is not None and len(tools) > 1: + raise AgentError( + code=AGENT_EXECUTION_FAILED, + message=f"Kiro CLI supports at most one agent, got {len(tools)}", + details={"tools": tools}, + ) + agent_name = tools[0] if tools else self._agent + cmd.extend(["--agent", agent_name]) - # Prompt as final positional argument + # Prompt as final positional arg cmd.append(prompt) return cmd diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py new file mode 100644 index 00000000..35a91490 --- /dev/null +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py @@ -0,0 +1,148 @@ +"""Operator install and verification for the global Beddel Delivery agent.""" + +from __future__ import annotations + +import argparse +import hashlib +import json +import os +import shlex +import sys +from pathlib import Path +from typing import Any + +from . import hook + +__all__ = ["install", "main", "render", "verify"] + +_AGENT_NAME = "beddel-delivery" +_TEMPLATE = Path(__file__).with_name("beddel-delivery.template.json") + + +def _canonical_bytes(value: Any) -> bytes: + return json.dumps(value, indent=2, ensure_ascii=False).encode("utf-8") + b"\n" + + +def _digest(raw: bytes) -> str: + return hashlib.sha256(raw).hexdigest() + + +def render(pointers_dir: Path) -> bytes: + """Render the stable agent configuration for *pointers_dir*.""" + template = json.loads(_TEMPLATE.read_text(encoding="utf-8")) + python_dir = Path(__file__).resolve().parent.parent + command = ( + f"env PYTHONPATH={shlex.quote(str(python_dir))} python3 -P -m " + f"beddel_agent_kiro.hook --pointers {shlex.quote(str(pointers_dir.resolve()))}" + ) + for entry in template["hooks"]: + entry["action"]["command"] = command + return _canonical_bytes(template) + + +def _agent_path(home: Path) -> Path: + return home / ".kiro" / "agents" / f"{_AGENT_NAME}.json" + + +def _private_dir(path: Path) -> None: + created = False + if path.exists(): + if not path.is_dir() or path.is_symlink(): + raise OSError(f"not a directory: {path}") + else: + path.mkdir(mode=0o700) + created = True + if created: + os.chmod(path, 0o700) + + +def install(home: Path, pointers_dir: Path, replace: bool = False) -> tuple[Path, str]: + """Install the agent config and secure pointer directory under *home*.""" + target = _agent_path(home) + rendered = render(pointers_dir) + expected = _digest(rendered) + home.mkdir(parents=True, exist_ok=True) + _private_dir(home / ".kiro") + _private_dir(target.parent) + _private_dir(pointers_dir) + if target.exists() or target.is_symlink(): + if target.is_symlink() or target.read_bytes() != rendered: + if not replace: + raise FileExistsError(f"{target} differs; use --replace") + else: + os.chmod(target, 0o600) + return target, expected + temporary = target.with_name(f".{target.name}.{os.getpid()}.tmp") + flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL + if hasattr(os, "O_NOFOLLOW"): + flags |= os.O_NOFOLLOW + fd = os.open(temporary, flags, 0o600) + try: + view = memoryview(rendered) + while view: + view = view[os.write(fd, view) :] + os.fchmod(fd, 0o600) + os.fsync(fd) + finally: + os.close(fd) + os.replace(temporary, target) + os.chmod(target, 0o600) + return target, expected + + +def verify(home: Path, pointers_dir: Path) -> tuple[bool, str, str]: + """Compare the installed agent against the rendered template.""" + expected_raw = render(pointers_dir) + expected = _digest(expected_raw) + target = _agent_path(home) + if not target.is_file() or target.is_symlink(): + return False, expected, "missing" + try: + found = _digest(target.read_bytes()) + except OSError: + return False, expected, "missing" + return found == expected, expected, found + + +def _parser() -> argparse.ArgumentParser: + parser = argparse.ArgumentParser( + description="Install or verify the Beddel Delivery agent" + ) + subparsers = parser.add_subparsers(dest="command", required=True) + for name in ("install", "verify"): + command = subparsers.add_parser(name) + command.add_argument("--home", type=Path, default=Path.home()) + command.add_argument("--pointers", type=Path, default=hook.DEFAULT_POINTERS_DIR) + if name == "install": + command.add_argument("--replace", action="store_true") + return parser + + +def main(argv: list[str] | None = None) -> int: + """Run the operator command and return its process exit code.""" + args = _parser().parse_args(argv) + pointers = args.pointers.expanduser().resolve() + home = args.home.expanduser().resolve() + if args.command == "install": + try: + target, digest = install(home, pointers, args.replace) + except (OSError, ValueError, KeyError) as error: + print(f"install failed: {error}", file=sys.stderr) + return 1 + print(f"installed {target} sha256={digest}") + print(f"pointers {pointers}") + return 0 + try: + ok, expected, found = verify(home, pointers) + except (OSError, ValueError, KeyError) as error: + print(f"verify failed: {error}", file=sys.stderr) + return 1 + if ok: + print(f"ok {expected}") + return 0 + print(f"drift expected={expected} found={found}") + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/beddel-delivery.template.json b/kits/agent-kiro-kit/python/beddel_agent_kiro/beddel-delivery.template.json new file mode 100644 index 00000000..92bbb3ab --- /dev/null +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/beddel-delivery.template.json @@ -0,0 +1,56 @@ +{ + "name": "beddel-delivery", + "description": "Beddel Delivery agent (agent-kiro-kit)", + "prompt": null, + "tools": [ + "glob", + "grep", + "read", + "shell", + "write" + ], + "allowedTools": [], + "resources": [], + "mcpServers": {}, + "includeMcpJson": false, + "hooks": [ + { + "name": "preToolUse-0", + "trigger": "preToolUse", + "action": { + "type": "command", + "command": "{HOOK}" + }, + "timeout": 10 + }, + { + "name": "postToolUse-0", + "trigger": "postToolUse", + "action": { + "type": "command", + "command": "{HOOK}" + }, + "timeout": 10 + }, + { + "name": "stop-0", + "trigger": "stop", + "action": { + "type": "command", + "command": "{HOOK}" + }, + "timeout": 10 + } + ], + "permissions": { + "rules": [ + { + "capability": "fs_read", + "match": [ + "./**" + ], + "effect": "allow" + } + ] + } +} diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/errors.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/errors.py new file mode 100644 index 00000000..89570b96 --- /dev/null +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/errors.py @@ -0,0 +1,11 @@ +"""Error codes owned by the agent-kiro kit.""" + +from __future__ import annotations + +KIRO_AGENT_INVALID = "KIRO_AGENT_INVALID" +"""The configured Delivery agent is not installed or cannot be loaded.""" + +KIRO_CLI_MISSING = "KIRO_CLI_MISSING" +"""The kiro-cli executable is not installed or discoverable.""" + +__all__ = ["KIRO_AGENT_INVALID", "KIRO_CLI_MISSING"] diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py new file mode 100644 index 00000000..96a06c09 --- /dev/null +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py @@ -0,0 +1,434 @@ +"""Stdlib-only Kiro hook policy evaluator and redacted event logger.""" + +from __future__ import annotations + +import hashlib +import json +import os +import re +import shlex +import stat +import sys +import time +from datetime import datetime, timezone +from pathlib import Path +from typing import Any, TextIO + +DEFAULT_POINTERS_DIR = Path.home() / ".local/state/beddel/kiro-hook/pointers" +SCHEMA = "beddel.kiro-hook/1" +_POLICY_SCHEMA = "beddel.kiro-hook-policy/1" +_POINTER_KEYS = frozenset({"policy", "log_path", "stage"}) +_POLICY_KEYS = frozenset({"schema", "rules"}) +_RULE_KEYS = frozenset({"id", "effect", "tool_class", "argv_prefix"}) +_ID_RE = re.compile(r"[a-z0-9._-]{1,64}") +_CMD_RE = re.compile(r"[a-z0-9._-]{1,32}") +_SPLIT_RE = re.compile(r"&&|\|\||[;|&\n]") +_CLASSES = {"read", "write", "shell", "other"} +_LOG_KEYS = ( + "v", + "ts", + "seq", + "session_id", + "event", + "tool_name", + "tool_class", + "cmd_head", + "input_sha256", + "input_len", + "response_sha256", + "response_len", + "decision", + "rule_id", + "policy_sha256", + "hook_ms", +) + + +def _canonical(value: Any) -> bytes: + return json.dumps( + value, sort_keys=True, separators=(",", ":"), ensure_ascii=False + ).encode("utf-8") + + +def _digest(value: Any) -> tuple[str, int]: + raw = _canonical(value) + return hashlib.sha256(raw).hexdigest(), len(raw) + + +def pointer_path(pointers: Path, cwd: str | os.PathLike[str]) -> Path: + """Return the cwd-realpath keyed pointer path.""" + digest = hashlib.sha256(os.path.realpath(os.fspath(cwd)).encode()).hexdigest() + return Path(pointers) / f"{digest}.json" + + +def classify(tool_name: Any) -> str: + name = str(tool_name or "").lower() + if name in {"read", "fs_read", "glob", "grep", "code"}: + return "read" + if name in {"write", "fs_write"}: + return "write" + if name in {"shell", "execute_bash", "execute_cmd"}: + return "shell" + return "other" + + +def _command(tool_input: Any) -> str | None: + if isinstance(tool_input, str): + return tool_input + if isinstance(tool_input, dict): + value = tool_input.get("command", tool_input.get("cmd")) + if isinstance(value, str): + return value + return None + + +def _segments(command: str) -> list[str]: + return [part.strip() for part in _SPLIT_RE.split(command) if part.strip()] + + +def cmd_head(command: Any) -> str | None: + try: + parts = _segments(command) if isinstance(command, str) else [] + tokens = shlex.split(parts[0]) if parts else [] + if not tokens: + return None + head = os.path.basename(tokens[0]).lower() + return head if _CMD_RE.fullmatch(head) else None + except (TypeError, ValueError): + return None + + +def _secure_file(path: Path) -> bool: + try: + return ( + path.is_file() + and not path.is_symlink() + and not (path.stat().st_mode & 0o022) + ) + except OSError: + return False + + +def _load_json(path: Path) -> Any: + if not _secure_file(path): + raise ValueError("insecure or missing file") + return json.loads(path.read_text(encoding="utf-8")) + + +def _validate_policy(policy: Any) -> None: + if not isinstance(policy, dict) or set(policy) != _POLICY_KEYS: + raise ValueError("invalid policy keys") + if policy.get("schema") != _POLICY_SCHEMA or not isinstance( + policy.get("rules"), list + ): + raise ValueError("invalid policy schema") + seen: set[str] = set() + for rule in policy["rules"]: + if not isinstance(rule, dict) or not set(rule) <= _RULE_KEYS: + raise ValueError("invalid policy rule") + rule_id = rule.get("id") + if ( + not isinstance(rule_id, str) + or not _ID_RE.fullmatch(rule_id) + or rule_id in seen + ): + raise ValueError("invalid or duplicate rule id") + seen.add(rule_id) + if ( + rule.get("effect") not in {"allow", "deny"} + or rule.get("tool_class") not in _CLASSES + ): + raise ValueError("invalid policy rule") + prefix = rule.get("argv_prefix") + if prefix is not None and ( + rule["tool_class"] != "shell" + or not isinstance(prefix, list) + or not prefix + or not all(isinstance(item, str) for item in prefix) + ): + raise ValueError("invalid argv prefix") + + +def _pointer_paths(data: dict[str, Any]) -> tuple[Path, Path]: + if set(data) != _POINTER_KEYS: + raise ValueError("invalid pointer") + policy, log_path = data.get("policy"), data.get("log_path") + if not isinstance(policy, str) or not isinstance(log_path, str): + raise TypeError("invalid pointer paths") + policy_path, log = Path(policy), Path(log_path) + if ( + not policy_path.is_absolute() + or not log.is_absolute() + or not isinstance(data.get("stage"), str) + ): + raise ValueError("invalid pointer paths") + return policy_path, log + + +def _load_context( + data: dict[str, Any], +) -> tuple[dict[str, Any], dict[str, Any], str, Path]: + policy_path, log_path = _pointer_paths(data) + policy = _load_json(policy_path) + _validate_policy(policy) + return data, policy, hashlib.sha256(_canonical(policy)).hexdigest(), log_path + + +def _evaluate( + policy: dict[str, Any], tool_class: str, command: str | None +) -> tuple[str, str]: + if tool_class == "shell": + if command is None: + return "deny", "shell.no_command" + if any(marker in command for marker in ("$(", "`", "<(", ">(")): + return "deny", "shell.substitution" + parts = _segments(command) or [""] + else: + parts = [""] + selected = None + for part in parts: + try: + tokens = shlex.split(part) + except ValueError: + return "deny", "shell.parse" + rule = next( + ( + r + for r in policy["rules"] + if r["tool_class"] == tool_class + and ( + r.get("argv_prefix") is None + or tokens[: len(r["argv_prefix"])] == r["argv_prefix"] + ) + ), + None, + ) + if rule is None: + return "deny", "policy.default_deny" + if rule["effect"] != "allow": + return "deny", rule["id"] + selected = rule["id"] + return "allow", selected or "policy.allow" + + +def _record( + event: str, + decision: str, + rule_id: str, + policy_sha: str | None, + elapsed: float, + payload: dict[str, Any], + command: str | None, + tool_class: str | None, +) -> dict[str, Any]: + inp = ( + _digest(payload.get("tool_input")) if event in {"pre", "post"} else (None, None) + ) + response = ( + _digest(payload.get("tool_response")) if event == "post" else (None, None) + ) + values = { + "v": SCHEMA, + "ts": datetime.now(timezone.utc).isoformat(), + "seq": 0, + "session_id": payload.get("session_id"), + "event": event, + "tool_name": payload.get("tool_name") if event != "stop" else None, + "tool_class": tool_class if event != "stop" else None, + "cmd_head": cmd_head(command) + if event != "stop" and tool_class == "shell" + else None, + "input_sha256": inp[0], + "input_len": inp[1], + "response_sha256": response[0], + "response_len": response[1], + "decision": decision, + "rule_id": rule_id, + "policy_sha256": policy_sha, + "hook_ms": elapsed, + } + return {key: values[key] for key in _LOG_KEYS} + + +def _append_log(path: Path, record: dict[str, Any]) -> None: + import fcntl + + if path.is_symlink() or not path.parent.is_dir(): + raise OSError("log path or parent directory is unavailable") + flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND | getattr(os, "O_NOFOLLOW", 0) + fd = os.open(path, flags, 0o600) + read_fd: int | None = None + locked = False + try: + os.fchmod(fd, 0o600) + mode = os.fstat(fd).st_mode + if not stat.S_ISREG(mode) or mode & 0o077: + raise OSError("log file is not a private regular file") + fcntl.flock(fd, fcntl.LOCK_EX) + locked = True + read_fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) + maximum = 0 + with os.fdopen(read_fd, "r", encoding="utf-8", errors="replace") as stream: + read_fd = None + for line in stream: + try: + value = json.loads(line) + if isinstance(value, dict) and isinstance(value.get("seq"), int): + maximum = max(maximum, value["seq"]) + except (json.JSONDecodeError, TypeError): + pass + record["seq"] = maximum + 1 + raw = ( + json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n" + ).encode() + while raw: + raw = raw[os.write(fd, raw) :] + os.fsync(fd) + finally: + if locked: + fcntl.flock(fd, fcntl.LOCK_UN) + if read_fd is not None: + os.close(read_fd) + os.close(fd) + + +def _emit(decision: str, rule_id: str) -> None: + print( + f"decision=allow rule_id={rule_id}" + if decision == "allow" + else f"beddel-kiro-hook: {rule_id}", + file=None if decision == "allow" else sys.stderr, + ) + + +def _run(payload: dict[str, Any], pointers: Path) -> int: + cwd = payload.get("cwd") + if not isinstance(cwd, str): + raise TypeError("missing cwd") + pointer = pointer_path(pointers, cwd) + if not _secure_file(pointer): + _emit("deny", "pointer.missing") + return 2 + log_path: Path | None = None + policy_sha: str | None = None + loaded = False + try: + data = _load_json(pointer) + if not isinstance(data, dict): + raise TypeError("invalid pointer") + _, log_path = _pointer_paths(data) + data, policy, policy_sha, log_path = _load_context(data) + loaded = True + name = str(payload.get("hook_event_name", "")).lower() + event = { + "pretooluse": "pre", + "pre": "pre", + "posttooluse": "post", + "post": "post", + "stop": "stop", + "sessionend": "stop", + }.get(name) + if event is None: + raise ValueError("unknown event") + started = time.perf_counter() + tool_class = classify(payload.get("tool_name")) + command = _command(payload.get("tool_input")) + decision, rule = ( + _evaluate(policy, tool_class, command) + if event == "pre" + else ("allow", f"hook.{event}") + ) + _append_log( + log_path, + _record( + event, + decision, + rule, + policy_sha, + (time.perf_counter() - started) * 1000, + payload, + command, + tool_class, + ), + ) + _emit(decision, rule) + return 0 if decision == "allow" else 2 + except BaseException: # noqa: BLE001 + if log_path is not None: + try: + _append_log( + log_path, + _record( + "error", + "error", + "hook.internal" if loaded else "policy.invalid", + policy_sha, + 0.0, + payload, + None, + None, + ), + ) + except BaseException: # noqa: BLE001 + log_path = None + _emit("error", "hook.internal" if loaded else "policy.invalid") + return 2 + + +def main(argv: list[str] | None = None, stdin: TextIO | None = None) -> int: + args = list(sys.argv[1:] if argv is None else argv) + if len(args) != 2 or args[0] != "--pointers": + _emit("error", "hook.internal") + return 2 + try: + payload = json.load(stdin or sys.stdin) + if not isinstance(payload, dict): + raise TypeError("event is not an object") + return _run(payload, Path(args[1])) + except BaseException: # noqa: BLE001 + _emit("error", "hook.internal") + return 2 + + +def summarize(pointers: Path, cwd: Path) -> dict[str, Any]: + result: dict[str, Any] = { + "type": "kiro-hook", + "events": 0, + "pre": 0, + "post": 0, + "denied": 0, + "stop": 0, + "errors": 0, + "log_sha256": None, + } + try: + data = _load_json(pointer_path(pointers, str(cwd))) + if not isinstance(data, dict): + return result + _, log_path = _pointer_paths(data) + if not _secure_file(log_path): + return result + raw = log_path.read_bytes() + result["log_sha256"] = hashlib.sha256(raw).hexdigest() + for line in raw.decode("utf-8", errors="replace").splitlines(): + try: + record = json.loads(line) + except (json.JSONDecodeError, UnicodeError): + continue + event = record.get("event") if isinstance(record, dict) else None + if event in {"pre", "post", "stop"}: + result["events"] += 1 + result[event] += 1 + if isinstance(record, dict) and record.get("decision") == "deny": + result["denied"] += 1 + if isinstance(record, dict) and ( + record.get("decision") == "error" or event == "error" + ): + result["errors"] += 1 + except BaseException: # noqa: BLE001 + return result + return result + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/kits/agent-kiro-kit/tests/test_agent_config.py b/kits/agent-kiro-kit/tests/test_agent_config.py new file mode 100644 index 00000000..837e44d3 --- /dev/null +++ b/kits/agent-kiro-kit/tests/test_agent_config.py @@ -0,0 +1,73 @@ +"""Tests for operator-only Delivery agent installation.""" + +from __future__ import annotations + +import json +from pathlib import Path + +from beddel_agent_kiro.agent_config import install, main, render, verify + + +def test_install_verify_and_refuse_drift(tmp_path: Path, capsys) -> None: + home = tmp_path / "home" + pointers = tmp_path / "pointers" + target, digest = install(home, pointers) + assert target == home / ".kiro/agents/beddel-delivery.json" + assert target.stat().st_mode & 0o777 == 0o600 + assert pointers.stat().st_mode & 0o777 == 0o700 + assert verify(home, pointers) == (True, digest, digest) + + target.write_text("{}", encoding="utf-8") + assert verify(home, pointers)[0] is False + assert install(home, pointers, replace=True)[1] == digest + assert main(["verify", "--home", str(home), "--pointers", str(pointers)]) == 0 + assert "ok " in capsys.readouterr().out + + +def test_install_refuses_different_existing_config(tmp_path: Path, capsys) -> None: + home = tmp_path / "home" + pointers = tmp_path / "pointers" + install(home, pointers) + target = home / ".kiro/agents/beddel-delivery.json" + target.write_text("drift", encoding="utf-8") + assert main(["install", "--home", str(home), "--pointers", str(pointers)]) == 1 + stderr = capsys.readouterr().err + assert "--replace" in stderr + + +def test_template_invariants(tmp_path: Path) -> None: + config = json.loads(render(tmp_path / "pointers")) + assert config["name"] == "beddel-delivery" + assert config["tools"] == ["glob", "grep", "read", "shell", "write"] + assert config["allowedTools"] == [] + assert config["resources"] == [] + assert config["mcpServers"] == {} + assert config["includeMcpJson"] is False + assert "*" not in json.dumps(config["tools"]) + assert "@mcp" not in json.dumps(config) + assert [entry["trigger"] for entry in config["hooks"]] == [ + "preToolUse", + "postToolUse", + "stop", + ] + for entry in config["hooks"]: + command = entry["action"]["command"] + assert "-m beddel_agent_kiro.hook --pointers" in command + assert "PYTHONPATH=" in command + + +def test_missing_and_drift_verify_output(tmp_path: Path, capsys) -> None: + home = tmp_path / "home" + pointers = tmp_path / "pointers" + assert main(["verify", "--home", str(home), "--pointers", str(pointers)]) == 1 + output = capsys.readouterr().out + assert "found=missing" in output + + +def test_install_preserves_preexisting_kiro_mode(tmp_path: Path) -> None: + home = tmp_path / "home" + kiro = home / ".kiro" + kiro.mkdir(parents=True) + kiro.chmod(0o755) + install(home, tmp_path / "pointers") + assert kiro.stat().st_mode & 0o777 == 0o755 diff --git a/kits/agent-kiro-kit/tests/test_delivery_stage.py b/kits/agent-kiro-kit/tests/test_delivery_stage.py index 560bf77f..bad3ad67 100644 --- a/kits/agent-kiro-kit/tests/test_delivery_stage.py +++ b/kits/agent-kiro-kit/tests/test_delivery_stage.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +import hashlib import json import os import signal @@ -25,7 +26,10 @@ ) from beddel.primitives import register_builtins from beddel.tools.kits import discover_kits, load_kit_adapters +from beddel_agent_kiro import hook from beddel_agent_kiro.adapter import KiroCLIAgentAdapter +from beddel_agent_kiro.agent_config import install +from beddel_agent_kiro.errors import KIRO_AGENT_INVALID @pytest.fixture @@ -37,6 +41,7 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat f"""#!{sys.executable} import json import os +import subprocess import sys import time @@ -52,6 +57,46 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat mode = os.environ.get("FAKE_KIRO_MODE", "ok") if mode == "ok": sys.stdout.write("ok") +elif mode == "agent_missing": + sys.exit(4) +elif mode == "hooks": + agent_name = sys.argv[sys.argv.index("--agent") + 1] + with open(os.path.join(os.environ["HOME"], ".kiro", "agents", agent_name + ".json")) as agent_file: + agent = json.load(agent_file) + hook_results = [] + tool_allowed = True + for hook in agent["hooks"]: + event_name = hook["trigger"] + if event_name == "postToolUse" and not tool_allowed: + continue + payload = {{ + "cwd": os.getcwd(), + "hook_event_name": event_name, + "session_id": "fake-session", + }} + if event_name != "stop": + payload["tool_name"] = "shell" + payload["tool_input"] = {{"command": os.environ.get("FAKE_KIRO_COMMAND", "true")}} + if event_name == "postToolUse": + payload["tool_response"] = {{"ok": True}} + completed = subprocess.run( + hook["action"]["command"], + input=json.dumps(payload), + text=True, + shell=True, + check=False, + ) + hook_results.append(completed.returncode) + if event_name == "preToolUse" and completed.returncode == 2: + tool_allowed = False + if event_name == "preToolUse" and completed.returncode == 0: + marker = os.environ.get("FAKE_KIRO_MARKER") + if marker: + open(marker, "w").close() + record["hook_results"] = hook_results + with open(os.environ["FAKE_KIRO_LOG"], "w") as log_file: + json.dump(record, log_file) + sys.stdout.write("ok") elif mode == "fail": sys.exit(3) elif mode == "sleep": @@ -67,6 +112,7 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat ) cli_path.chmod(0o755) monkeypatch.setenv("FAKE_KIRO_LOG", str(log_path)) + monkeypatch.setattr(hook, "DEFAULT_POINTERS_DIR", tmp_path / "pointers") monkeypatch.setenv("FAKE_KIRO_MODE", "ok") monkeypatch.delenv("KIRO_API_KEY", raising=False) return cli_path, log_path @@ -103,12 +149,103 @@ async def test_success_returns_structured_result(fake_cli: tuple[Path, Path]) -> assert result.exit_code == 0 assert result.output == "ok" - assert result.events == [] + assert result.events[0]["type"] == "kiro-hook" + assert result.events[0]["events"] == 0 assert result.files_changed == [] assert result.usage == {"model": "model-default", "timeout": 17} assert result.agent_id == "kiro-cli" +@pytest.mark.asyncio +async def test_agent_missing_exit_maps_to_kit_error( + fake_cli: tuple[Path, Path], monkeypatch: pytest.MonkeyPatch +) -> None: + cli_path, _log_path = fake_cli + monkeypatch.setenv("FAKE_KIRO_MODE", "agent_missing") + adapter = KiroCLIAgentAdapter(cli_path=cli_path) + + with pytest.raises(AgentError) as exc_info: + await adapter.execute("prompt", tools=["custom-agent"]) + + assert exc_info.value.code == KIRO_AGENT_INVALID + assert exc_info.value.details["agent"] == "custom-agent" + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + ("command", "expected_events", "expected_marker", "expected_first"), + [("true", 3, True, 0), ("git push", 2, False, 2)], +) +async def test_trust_all_hook_policy_counts( + fake_cli: tuple[Path, Path], + monkeypatch: pytest.MonkeyPatch, + tmp_path: Path, + command: str, + expected_events: int, + expected_marker: bool, + expected_first: int, +) -> None: + cli_path, _log_path = fake_cli + home = tmp_path / "home" + pointers = tmp_path / "pointers" + install(home, pointers) + policy_path = tmp_path / "policy.json" + log_path = tmp_path / "hook.jsonl" + policy_path.write_text( + json.dumps( + { + "schema": "beddel.kiro-hook-policy/1", + "rules": [ + { + "id": "shell.git-push", + "effect": "deny", + "tool_class": "shell", + "argv_prefix": ["git", "push"], + }, + {"id": "shell.any", "effect": "allow", "tool_class": "shell"}, + ], + } + ), + encoding="utf-8", + ) + os.chmod(policy_path, 0o600) + monkeypatch.chdir(tmp_path) + pointer = hook.pointer_path(pointers, str(tmp_path)) + pointer.write_text( + json.dumps( + {"policy": str(policy_path), "log_path": str(log_path), "stage": "test"} + ), + encoding="utf-8", + ) + os.chmod(pointer, 0o600) + monkeypatch.setenv("HOME", str(home)) + monkeypatch.setenv("FAKE_KIRO_MODE", "hooks") + monkeypatch.setenv("FAKE_KIRO_COMMAND", command) + marker = tmp_path / "allowed.marker" + monkeypatch.setenv("FAKE_KIRO_MARKER", str(marker)) + adapter = KiroCLIAgentAdapter(cli_path=cli_path, pointers_dir=pointers) + + result = await adapter.execute("prompt") + fake_record = _read_record(fake_cli[1]) + + assert fake_record["hook_results"][0] == expected_first + if expected_first == 0: + assert ( + result.events[0]["pre"] + == result.events[0]["post"] + == result.events[0]["stop"] + == 1 + ) + else: + assert result.events[0]["pre"] == result.events[0]["stop"] == 1 + assert result.events[0]["post"] == 0 + assert ( + result.events[0]["log_sha256"] + == hashlib.sha256(log_path.read_bytes()).hexdigest() + ) + assert marker.exists() is expected_marker + + @pytest.mark.asyncio async def test_argv_uses_model_agent_and_read_only_trust( fake_cli: tuple[Path, Path], @@ -124,6 +261,8 @@ async def test_argv_uses_model_agent_and_read_only_trust( "chat", "--no-interactive", "--trust-tools=", + "--agent-engine", + "v3", "--model", "m-x", "--agent", diff --git a/kits/agent-kiro-kit/tests/test_headless.py b/kits/agent-kiro-kit/tests/test_headless.py index 9a13db2c..66acdfd1 100644 --- a/kits/agent-kiro-kit/tests/test_headless.py +++ b/kits/agent-kiro-kit/tests/test_headless.py @@ -8,23 +8,24 @@ import os from pathlib import Path -from typing import Any from unittest.mock import AsyncMock, MagicMock, patch import pytest +from beddel_agent_kiro import hook from beddel_agent_kiro.adapter import _DEFAULT_CLI_PATH, KiroCLIAgentAdapter - from beddel.domain.errors import AgentError from beddel.error_codes import AGENT_EXECUTION_FAILED +from beddel_agent_kiro.errors import KIRO_CLI_MISSING + + +@pytest.fixture(autouse=True) +def _patch_pointer_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + monkeypatch.setattr(hook, "DEFAULT_POINTERS_DIR", tmp_path / "pointers") -# --------------------------------------------------------------------------- -# Helpers -# --------------------------------------------------------------------------- _PROMPT = "Deploy the application" _PATCH_EXEC = "beddel_agent_kiro.adapter.asyncio.create_subprocess_exec" _PATCH_WHICH = "beddel_agent_kiro.adapter.shutil.which" -_PATCH_SUBPROCESS_RUN = "beddel_agent_kiro.adapter.subprocess.run" _PATCH_PATH_EXISTS = "beddel_agent_kiro.adapter._DEFAULT_CLI_PATH" @@ -263,72 +264,18 @@ def test_discovery_which_fallback(self, tmp_path: Path) -> None: result = KiroCLIAgentAdapter._discover_cli() assert result == Path(found_path) - def test_discovery_auto_install_success(self, tmp_path: Path) -> None: - """Step 3: auto-install succeeds and binary appears at default path.""" - fake_default = tmp_path / "kiro-cli" - # Initially does not exist - call_count = 0 - - def _exists_side_effect() -> bool: - nonlocal call_count - call_count += 1 - if call_count == 1: - return False # First check: not found - fake_default.touch() - return True # After install: found - - mock_path = MagicMock(spec=Path) - mock_path.exists = _exists_side_effect - mock_path.__str__ = lambda _: str(fake_default) - mock_path.__fspath__ = lambda _: str(fake_default) - mock_path.__eq__ = lambda self, other: str(self) == str(other) - mock_path.__hash__ = lambda _: hash(str(fake_default)) - - with ( - patch("beddel_agent_kiro.adapter._DEFAULT_CLI_PATH", fake_default), - patch(_PATCH_WHICH, return_value=None), - patch(_PATCH_SUBPROCESS_RUN) as mock_run, - ): - # Make the path exist after subprocess.run is called - def side_effect(*args: Any, **kwargs: Any) -> MagicMock: - fake_default.touch() - return MagicMock(returncode=0) - - mock_run.side_effect = side_effect - result = KiroCLIAgentAdapter._discover_cli() - - assert result == fake_default - mock_run.assert_called_once() - - def test_discovery_all_fail_raises(self, tmp_path: Path) -> None: - """Step 4: all discovery steps fail, raises AgentError.""" + def test_discovery_missing_does_not_install(self, tmp_path: Path) -> None: + """Missing binaries raise without invoking an installer.""" fake_default = tmp_path / "nonexistent" / "kiro-cli" with ( patch("beddel_agent_kiro.adapter._DEFAULT_CLI_PATH", fake_default), patch(_PATCH_WHICH, return_value=None), - patch(_PATCH_SUBPROCESS_RUN) as mock_run, + pytest.raises(AgentError) as exc_info, ): - mock_run.return_value = MagicMock(returncode=0) - with pytest.raises(AgentError) as exc_info: - KiroCLIAgentAdapter._discover_cli() - - assert exc_info.value.code == AGENT_EXECUTION_FAILED - assert "kiro-cli not found" in exc_info.value.message + KiroCLIAgentAdapter._discover_cli() - def test_discovery_auto_install_timeout(self, tmp_path: Path) -> None: - """Auto-install times out gracefully.""" - import subprocess as sp - - fake_default = tmp_path / "nonexistent" / "kiro-cli" - with ( - patch("beddel_agent_kiro.adapter._DEFAULT_CLI_PATH", fake_default), - patch(_PATCH_WHICH, return_value=None), - patch(_PATCH_SUBPROCESS_RUN, side_effect=sp.TimeoutExpired(cmd="bash", timeout=60)), - ): - with pytest.raises(AgentError) as exc_info: - KiroCLIAgentAdapter._discover_cli() - - assert exc_info.value.code == AGENT_EXECUTION_FAILED + assert exc_info.value.code == KIRO_CLI_MISSING + assert "kiro.dev/docs/cli" in exc_info.value.details["hint"] # =================================================================== diff --git a/kits/agent-kiro-kit/tests/test_hook.py b/kits/agent-kiro-kit/tests/test_hook.py new file mode 100644 index 00000000..7c55c925 --- /dev/null +++ b/kits/agent-kiro-kit/tests/test_hook.py @@ -0,0 +1,253 @@ +"""Tests for the policy hook, redacted log, and result summary.""" + +from __future__ import annotations + +import io +import json +import multiprocessing +import os +import subprocess +from pathlib import Path +from typing import Any + +import pytest +from beddel_agent_kiro import hook + + +def _setup( + tmp_path: Path, rules: list[dict[str, Any]] | None = None +) -> tuple[Path, Path, Path]: + pointers = tmp_path / "pointers" + pointers.mkdir(parents=True, mode=0o700) + policy_path, log_path = tmp_path / "policy.json", tmp_path / "events.jsonl" + policy = { + "schema": "beddel.kiro-hook-policy/1", + "rules": rules + or [ + {"id": "shell.any", "effect": "allow", "tool_class": "shell"}, + {"id": "read.any", "effect": "allow", "tool_class": "read"}, + ], + } + policy_path.write_text(json.dumps(policy), encoding="utf-8") + os.chmod(policy_path, 0o600) + pointer = hook.pointer_path(pointers, str(tmp_path)) + pointer.write_text( + json.dumps( + {"policy": str(policy_path), "log_path": str(log_path), "stage": "test"} + ), + encoding="utf-8", + ) + os.chmod(pointer, 0o600) + return pointers, policy_path, log_path + + +def _event( + root: Path, + tool: str = "shell", + command: str | None = None, + name: str = "preToolUse", +) -> dict[str, Any]: + value: dict[str, Any] = { + "cwd": str(root), + "hook_event_name": name, + "tool_name": tool, + } + if command is not None: + value["tool_input"] = {"command": command} + return value + + +def _call(pointers: Path, payload: dict[str, Any]) -> tuple[int, str, str]: + out, err = io.StringIO(), io.StringIO() + old_out, old_err = __import__("sys").stdout, __import__("sys").stderr + try: + __import__("sys").stdout, __import__("sys").stderr = out, err + code = hook.main( + ["--pointers", str(pointers)], io.StringIO(json.dumps(payload)) + ) + finally: + __import__("sys").stdout, __import__("sys").stderr = old_out, old_err + return code, out.getvalue(), err.getvalue() + + +@pytest.mark.parametrize( + "command", + [ + "true\nblocked", + "true|blocked", + "true&blocked", + "true||blocked", + "true;blocked", + "echo `blocked`", + "cat <(blocked)", + "cat >(blocked)", + "'unbalanced", + ], +) +def test_shell_segments_and_substitutions_deny(tmp_path: Path, command: str) -> None: + rules = [ + { + "id": "shell.blocked", + "effect": "deny", + "tool_class": "shell", + "argv_prefix": ["blocked"], + }, + {"id": "shell.any", "effect": "allow", "tool_class": "shell"}, + ] + pointers, _policy, log = _setup(tmp_path, rules) + code, _out, err = _call(pointers, _event(tmp_path, command=command)) + assert code == 2 and "beddel-kiro-hook:" in err + assert command not in log.read_text(encoding="utf-8") + + +def test_allow_pointer_errors_and_no_command(tmp_path: Path) -> None: + pointers, policy, log = _setup(tmp_path) + assert _call(pointers, _event(tmp_path, command="true"))[0] == 0 + payload = _event(tmp_path) + payload["tool_input"] = {"args": ["true"]} + assert "shell.no_command" in _call(pointers, payload)[2] + pointer = hook.pointer_path(pointers, str(tmp_path)) + before = log.read_bytes() + pointer.unlink() + assert _call(pointers, _event(tmp_path))[0] == 2 and log.read_bytes() == before + pointers, policy, log = _setup(tmp_path / "invalid") + policy.write_text("{}", encoding="utf-8") + os.chmod(policy, 0o600) + assert "policy.invalid" in _call(pointers, _event(policy.parent))[2] + assert '"decision":"error"' in log.read_text(encoding="utf-8") + + +def test_post_stop_redaction_schema_and_digest(tmp_path: Path) -> None: + pointers, _policy, log = _setup(tmp_path) + marker = "TOP-SECRET-PROMPT-MARKER" + post = _event(tmp_path, command="printf x", name="postToolUse") + post["tool_input"]["marker"] = marker + post["tool_response"] = {"marker": marker} + assert _call(pointers, post)[0] == 0 + assert _call(pointers, _event(tmp_path, name="stop"))[0] == 0 + raw = log.read_text(encoding="utf-8") + records = [json.loads(line) for line in raw.splitlines()] + assert all(set(record) == set(hook._LOG_KEYS) for record in records) + assert marker not in raw and records[0]["response_sha256"] + assert records[1]["tool_name"] is None + + +def _child(pointers: str, cwd: str) -> None: + payload = _event(Path(cwd), tool="read") + payload["tool_input"] = {} + raise SystemExit( + hook.main(["--pointers", pointers], io.StringIO(json.dumps(payload))) + ) + + +def test_concurrent_sequences_are_unique(tmp_path: Path) -> None: + pointers, _policy, log = _setup(tmp_path) + processes = [ + multiprocessing.Process(target=_child, args=(str(pointers), str(tmp_path))) + for _ in range(2) + ] + for process in processes: + process.start() + for process in processes: + process.join(5) + assert process.exitcode == 0 + records = [ + json.loads(line) for line in log.read_text(encoding="utf-8").splitlines() + ] + assert sorted(record["seq"] for record in records) == [1, 2] + + +def test_policy_schema_log_permissions_and_relative_path(tmp_path: Path) -> None: + cases = ( + [ + { + "id": "shell.empty", + "effect": "allow", + "tool_class": "shell", + "argv_prefix": [], + } + ], + [ + {"id": "shell.same", "effect": "allow", "tool_class": "shell"}, + {"id": "shell.same", "effect": "deny", "tool_class": "shell"}, + ], + ) + for index, rules in enumerate(cases): + root = tmp_path / str(index) + pointers, _policy, log = _setup(root, rules) + assert "policy.invalid" in _call(pointers, _event(root, command="true"))[2] + assert log.exists() + pointers, policy, _log = _setup(tmp_path / "relative") + root = tmp_path / "relative" + hook.pointer_path(pointers, str(root)).write_text( + json.dumps( + {"policy": str(policy), "log_path": "relative.json", "stage": "test"} + ), + encoding="utf-8", + ) + assert ( + _call(pointers, _event(root, name="stop"))[0] == 2 + and not (root / "relative.json").exists() + ) + + +def test_fresh_log_umask_and_invalid_utf8(tmp_path: Path) -> None: + pointers, _policy, log = _setup(tmp_path) + payload = _event(tmp_path, tool="read") + payload["tool_input"] = {} + old_umask = os.umask(0o002) + try: + assert _call(pointers, payload)[0] == _call(pointers, payload)[0] == 0 + finally: + os.umask(old_umask) + assert log.stat().st_mode & 0o077 == 0 + log.write_bytes(log.read_bytes() + b"\xff\n") + assert _call(pointers, payload)[0] == 0 + + +def test_getattr_and_rendered_commands(tmp_path: Path) -> None: + import beddel_agent_kiro + from beddel_agent_kiro.adapter import KiroCLIAgentAdapter + from beddel_agent_kiro.agent_config import render + + assert getattr(beddel_agent_kiro, "KiroCLIAgentAdapter") is KiroCLIAgentAdapter # noqa: B009 + pointers, _policy, log = _setup( + tmp_path, [{"id": "shell.deny", "effect": "deny", "tool_class": "shell"}] + ) + command = json.loads(render(pointers))["hooks"][0]["action"]["command"] + env = {"PATH": os.environ["PATH"]} + denied = subprocess.run( + command, + input=json.dumps(_event(tmp_path, command="true")), + text=True, + shell=True, + check=False, + env=env, + ) + invalid = subprocess.run( + command, + input=json.dumps(_event(tmp_path / "missing")), + text=True, + shell=True, + check=False, + env=env, + ) + assert denied.returncode == invalid.returncode == 2 + assert log.exists() + stub = tmp_path / "stub" / "beddel" + stub.mkdir(parents=True) + (stub / "__init__.py").write_text( + "raise RuntimeError('must not import')", encoding="utf-8" + ) + stub_command = command.replace("PYTHONPATH=", f"PYTHONPATH={stub}:", 1) + assert ( + subprocess.run( + stub_command, + input=json.dumps(_event(tmp_path, command="true")), + text=True, + shell=True, + check=False, + env=env, + ).returncode + == 2 + ) diff --git a/kits/agent-kiro-kit/tests/test_kiro_cli.py b/kits/agent-kiro-kit/tests/test_kiro_cli.py index 55c50493..2e06859e 100644 --- a/kits/agent-kiro-kit/tests/test_kiro_cli.py +++ b/kits/agent-kiro-kit/tests/test_kiro_cli.py @@ -8,6 +8,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest +from beddel_agent_kiro import hook from beddel_agent_kiro.adapter import _DEFAULT_CLI_PATH, KiroCLIAgentAdapter from beddel.domain.errors import AgentError @@ -29,6 +30,16 @@ _PATCH_WAIT = "beddel_agent_kiro.adapter.asyncio.wait_for" +@pytest.fixture(autouse=True) +def _patch_cli_discovery(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: + monkeypatch.setattr(hook, "DEFAULT_POINTERS_DIR", tmp_path / "pointers") + monkeypatch.setattr( + KiroCLIAgentAdapter, + "_discover_cli", + staticmethod(lambda: _DEFAULT_CLI_PATH), + ) + + def _make_context(workflow_id: str) -> ExecutionContext: """Build the execution context required by the registry primitive.""" return ExecutionContext(workflow_id=workflow_id, current_step_id="step-1") @@ -73,6 +84,7 @@ def test_default_values(self) -> None: assert adapter._model == "claude-sonnet-4.6" assert adapter._cli_path == _DEFAULT_CLI_PATH assert adapter._timeout == 600 + assert adapter._agent == "beddel-delivery" def test_custom_values(self) -> None: custom_path = Path("/usr/local/bin/kiro-cli") @@ -166,7 +178,7 @@ def test_tools_single_agent(self) -> None: agent_idx = cmd.index("--agent") assert cmd[agent_idx + 1] == "my-agent" - def test_tools_none_ignored(self) -> None: + def test_tools_none_uses_default_agent(self) -> None: adapter = KiroCLIAgentAdapter() cmd = adapter._build_command( @@ -176,7 +188,8 @@ def test_tools_none_ignored(self) -> None: tools=None, ) - assert "--agent" not in cmd + assert "--agent" in cmd + assert cmd[cmd.index("--agent") + 1] == "beddel-delivery" def test_tools_multiple_raises(self) -> None: """Multi-element tools list raises AgentError (CLI supports one --agent).""" @@ -194,8 +207,8 @@ def test_tools_multiple_raises(self) -> None: assert "at most one" in exc_info.value.message.lower() assert exc_info.value.details["tools"] == ["agent-a", "agent-b"] - def test_tools_empty_list_ignored(self) -> None: - """Empty tools list produces no --agent flag.""" + def test_tools_empty_list_uses_default_agent(self) -> None: + """Empty tools list uses the configured default agent.""" adapter = KiroCLIAgentAdapter() cmd = adapter._build_command( @@ -205,7 +218,8 @@ def test_tools_empty_list_ignored(self) -> None: tools=[], ) - assert "--agent" not in cmd + assert "--agent" in cmd + assert cmd[cmd.index("--agent") + 1] == "beddel-delivery" def test_sandbox_unknown_raises(self) -> None: """Unknown sandbox value raises AgentError.""" @@ -279,7 +293,18 @@ async def test_happy_path(self) -> None: assert result.exit_code == 0 assert result.output == "Task completed successfully" assert result.agent_id == "kiro-cli" - assert result.events == [] + assert result.events == [ + { + "type": "kiro-hook", + "events": 0, + "pre": 0, + "post": 0, + "denied": 0, + "stop": 0, + "errors": 0, + "log_sha256": None, + } + ] assert result.files_changed == [] assert result.usage == { "model": "claude-sonnet-4.6", From 05ae8d641be6037e088ade41625d9bafa6b99ddd Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Tue, 17 Nov 2026 19:34:58 -0300 Subject: [PATCH 2/3] fix(agent-kiro-kit): address code review round 1 for the pinned agent hooks (Story 8.1) --- kits/agent-kiro-kit/README.md | 26 +- .../python/beddel_agent_kiro/adapter.py | 2 +- .../python/beddel_agent_kiro/agent_config.py | 15 +- .../python/beddel_agent_kiro/hook.py | 228 +++++------------- .../agent-kiro-kit/tests/test_agent_config.py | 7 + .../tests/test_delivery_stage.py | 16 +- kits/agent-kiro-kit/tests/test_headless.py | 13 +- kits/agent-kiro-kit/tests/test_hook.py | 52 +++- 8 files changed, 147 insertions(+), 212 deletions(-) diff --git a/kits/agent-kiro-kit/README.md b/kits/agent-kiro-kit/README.md index 8973d8e8..e44d4d5b 100644 --- a/kits/agent-kiro-kit/README.md +++ b/kits/agent-kiro-kit/README.md @@ -2,8 +2,6 @@ Kiro CLI agent adapter for the Beddel SDK. Implements the `IAgentAdapter` port to execute prompts via the Kiro CLI subprocess. -Migration: install agent + hook. - ## Dependencies None — uses subprocess only (no pip dependencies). @@ -61,19 +59,11 @@ Delivery stage reads repository changes separately. Adapter errors use timeouts. A step timeout is raised as `BEDDEL-EXEC-005`; under the default FAIL strategy the step failure `BEDDEL-EXEC-002` wraps it as its cause. -`/.json` has absolute paths. -Schema `beddel.kiro-hook-policy/1`; first-match-wins, no rules deny all. -Hook exit 2 means pointer/policy/denial/error or missing log dir. Python -3.11+ is required for `python3 -P`. - -Install and verify the operator-owned agent outside a workflow: - -```bash -python3 -m beddel_agent_kiro.agent_config install --home DIR --pointers DIR [--replace] -python3 -m beddel_agent_kiro.agent_config verify --home DIR --pointers DIR -``` +### Delivery agent and hooks -POSIX; writes `~/.kiro/agents/beddel-delivery.json`. +Operator install/verify: `PYTHONPATH=/python python3 -m beddel_agent_kiro.agent_config install|verify`; supports `--home`, `--pointers`, `--replace`. +Pointer `/.json`; policy `beddel.kiro-hook-policy/1`, first-match-wins, no rules deny all; Python 3.11+; errors `KIRO_AGENT_INVALID`/`KIRO_CLI_MISSING`. +Log schema `beddel.kiro-hook/1`: `v`, `ts`, `seq`, `session_id`, `event`, `tool_name`, `tool_class`, `cmd_head`, `input_sha256`, `input_len`, `response_sha256`, `response_len`, `decision`, `rule_id`, `policy_sha256`, `hook_ms`. ## Headless Mode (CI/CD) @@ -167,15 +157,9 @@ If `cli_path` is not provided, the adapter discovers an already-installed If both fail, `KIRO_CLI_MISSING`; manual text. No auto-install/network call. -Schema `beddel.kiro-hook/1`; fields: -`v`, `ts`, `seq`, `session_id`, `event`, `tool_name`, `tool_class`, `cmd_head`, -`input_sha256`, `input_len`, `response_sha256`, `response_len`, `decision`, -`rule_id`, `policy_sha256`, and `hook_ms`. Raw values never appear; results -contain hook counts and the log digest. - ## Running Tests -Run tests from its directory: +Run the kit tests from the kit directory with the Beddel SDK installed: ```bash pytest -q diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py index 0110ddf9..ea744fcf 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/adapter.py @@ -381,7 +381,7 @@ def _build_command( agent_name = tools[0] if tools else self._agent cmd.extend(["--agent", agent_name]) - # Prompt as final positional arg + # Prompt as final positional argument cmd.append(prompt) return cmd diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py index 35a91490..325deafe 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/agent_config.py @@ -11,8 +11,6 @@ from pathlib import Path from typing import Any -from . import hook - __all__ = ["install", "main", "render", "verify"] _AGENT_NAME = "beddel-delivery" @@ -50,7 +48,7 @@ def _private_dir(path: Path) -> None: if not path.is_dir() or path.is_symlink(): raise OSError(f"not a directory: {path}") else: - path.mkdir(mode=0o700) + path.mkdir(parents=True, mode=0o700) created = True if created: os.chmod(path, 0o700) @@ -112,7 +110,7 @@ def _parser() -> argparse.ArgumentParser: for name in ("install", "verify"): command = subparsers.add_parser(name) command.add_argument("--home", type=Path, default=Path.home()) - command.add_argument("--pointers", type=Path, default=hook.DEFAULT_POINTERS_DIR) + command.add_argument("--pointers", type=Path, default=None) if name == "install": command.add_argument("--replace", action="store_true") return parser @@ -121,8 +119,15 @@ def _parser() -> argparse.ArgumentParser: def main(argv: list[str] | None = None) -> int: """Run the operator command and return its process exit code.""" args = _parser().parse_args(argv) - pointers = args.pointers.expanduser().resolve() home = args.home.expanduser().resolve() + pointers = ( + ( + args.pointers + or home / ".local" / "state" / "beddel" / "kiro-hook" / "pointers" + ) + .expanduser() + .resolve() + ) if args.command == "install": try: target, digest = install(home, pointers, args.replace) diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py index 96a06c09..0533e989 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py @@ -14,6 +14,8 @@ from pathlib import Path from typing import Any, TextIO +# fmt: off + DEFAULT_POINTERS_DIR = Path.home() / ".local/state/beddel/kiro-hook/pointers" SCHEMA = "beddel.kiro-hook/1" _POLICY_SCHEMA = "beddel.kiro-hook-policy/1" @@ -25,29 +27,14 @@ _SPLIT_RE = re.compile(r"&&|\|\||[;|&\n]") _CLASSES = {"read", "write", "shell", "other"} _LOG_KEYS = ( - "v", - "ts", - "seq", - "session_id", - "event", - "tool_name", - "tool_class", - "cmd_head", - "input_sha256", - "input_len", - "response_sha256", - "response_len", - "decision", - "rule_id", - "policy_sha256", - "hook_ms", + "v", "ts", "seq", "session_id", "event", "tool_name", "tool_class", + "cmd_head", "input_sha256", "input_len", "response_sha256", "response_len", + "decision", "rule_id", "policy_sha256", "hook_ms", ) def _canonical(value: Any) -> bytes: - return json.dumps( - value, sort_keys=True, separators=(",", ":"), ensure_ascii=False - ).encode("utf-8") + return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8") def _digest(value: Any) -> tuple[str, int]: @@ -56,7 +43,6 @@ def _digest(value: Any) -> tuple[str, int]: def pointer_path(pointers: Path, cwd: str | os.PathLike[str]) -> Path: - """Return the cwd-realpath keyed pointer path.""" digest = hashlib.sha256(os.path.realpath(os.fspath(cwd)).encode()).hexdigest() return Path(pointers) / f"{digest}.json" @@ -82,6 +68,14 @@ def _command(tool_input: Any) -> str | None: return None +def _safe_text(value: Any) -> str | None: + return value if isinstance(value, str) and len(value) <= 128 else None + + +def _event_name(value: Any) -> str | None: + return {"pretooluse": "pre", "posttooluse": "post", "stop": "stop"}.get(str(value or "").lower()) + + def _segments(command: str) -> list[str]: return [part.strip() for part in _SPLIT_RE.split(command) if part.strip()] @@ -100,11 +94,8 @@ def cmd_head(command: Any) -> str | None: def _secure_file(path: Path) -> bool: try: - return ( - path.is_file() - and not path.is_symlink() - and not (path.stat().st_mode & 0o022) - ) + mode = path.stat().st_mode + return path.is_file() and not path.is_symlink() and not (mode & 0o022) and path.stat().st_uid == os.getuid() except OSError: return False @@ -118,34 +109,20 @@ def _load_json(path: Path) -> Any: def _validate_policy(policy: Any) -> None: if not isinstance(policy, dict) or set(policy) != _POLICY_KEYS: raise ValueError("invalid policy keys") - if policy.get("schema") != _POLICY_SCHEMA or not isinstance( - policy.get("rules"), list - ): + if policy.get("schema") != _POLICY_SCHEMA or not isinstance(policy.get("rules"), list): raise ValueError("invalid policy schema") seen: set[str] = set() for rule in policy["rules"]: if not isinstance(rule, dict) or not set(rule) <= _RULE_KEYS: raise ValueError("invalid policy rule") rule_id = rule.get("id") - if ( - not isinstance(rule_id, str) - or not _ID_RE.fullmatch(rule_id) - or rule_id in seen - ): + if not isinstance(rule_id, str) or not _ID_RE.fullmatch(rule_id) or rule_id in seen: raise ValueError("invalid or duplicate rule id") seen.add(rule_id) - if ( - rule.get("effect") not in {"allow", "deny"} - or rule.get("tool_class") not in _CLASSES - ): + if rule.get("effect") not in {"allow", "deny"} or rule.get("tool_class") not in _CLASSES: raise ValueError("invalid policy rule") prefix = rule.get("argv_prefix") - if prefix is not None and ( - rule["tool_class"] != "shell" - or not isinstance(prefix, list) - or not prefix - or not all(isinstance(item, str) for item in prefix) - ): + if prefix is not None and (rule["tool_class"] != "shell" or not isinstance(prefix, list) or not prefix or not all(isinstance(item, str) for item in prefix)): raise ValueError("invalid argv prefix") @@ -156,27 +133,25 @@ def _pointer_paths(data: dict[str, Any]) -> tuple[Path, Path]: if not isinstance(policy, str) or not isinstance(log_path, str): raise TypeError("invalid pointer paths") policy_path, log = Path(policy), Path(log_path) - if ( - not policy_path.is_absolute() - or not log.is_absolute() - or not isinstance(data.get("stage"), str) - ): + if not policy_path.is_absolute() or not log.is_absolute() or not isinstance(data.get("stage"), str): raise ValueError("invalid pointer paths") return policy_path, log -def _load_context( - data: dict[str, Any], -) -> tuple[dict[str, Any], dict[str, Any], str, Path]: +def _load_context(data: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any], str, Path]: policy_path, log_path = _pointer_paths(data) - policy = _load_json(policy_path) + if policy_path.is_symlink() or not policy_path.is_file(): + raise ValueError("insecure or missing policy") + raw = policy_path.read_bytes() + policy_sha = hashlib.sha256(raw).hexdigest() + if not _secure_file(policy_path): + raise ValueError("insecure policy") + policy = json.loads(raw) _validate_policy(policy) - return data, policy, hashlib.sha256(_canonical(policy)).hexdigest(), log_path + return data, policy, policy_sha, log_path -def _evaluate( - policy: dict[str, Any], tool_class: str, command: str | None -) -> tuple[str, str]: +def _evaluate(policy: dict[str, Any], tool_class: str, command: str | None) -> tuple[str, str]: if tool_class == "shell": if command is None: return "deny", "shell.no_command" @@ -191,18 +166,7 @@ def _evaluate( tokens = shlex.split(part) except ValueError: return "deny", "shell.parse" - rule = next( - ( - r - for r in policy["rules"] - if r["tool_class"] == tool_class - and ( - r.get("argv_prefix") is None - or tokens[: len(r["argv_prefix"])] == r["argv_prefix"] - ) - ), - None, - ) + rule = next((r for r in policy["rules"] if r["tool_class"] == tool_class and (r.get("argv_prefix") is None or tokens[:len(r["argv_prefix"])] == r["argv_prefix"])), None) if rule is None: return "deny", "policy.default_deny" if rule["effect"] != "allow": @@ -211,41 +175,18 @@ def _evaluate( return "allow", selected or "policy.allow" -def _record( - event: str, - decision: str, - rule_id: str, - policy_sha: str | None, - elapsed: float, - payload: dict[str, Any], - command: str | None, - tool_class: str | None, -) -> dict[str, Any]: - inp = ( - _digest(payload.get("tool_input")) if event in {"pre", "post"} else (None, None) - ) - response = ( - _digest(payload.get("tool_response")) if event == "post" else (None, None) - ) +def _record(event: str, decision: str, rule_id: str, policy_sha: str | None, elapsed: float, payload: dict[str, Any], command: str | None, tool_class: str | None) -> dict[str, Any]: + inp = _digest(payload.get("tool_input")) if event in {"pre", "post"} else (None, None) + response = _digest(payload.get("tool_response")) if event == "post" else (None, None) values = { - "v": SCHEMA, - "ts": datetime.now(timezone.utc).isoformat(), - "seq": 0, - "session_id": payload.get("session_id"), - "event": event, - "tool_name": payload.get("tool_name") if event != "stop" else None, + "v": SCHEMA, "ts": datetime.now(timezone.utc).isoformat(), "seq": 0, + "session_id": _safe_text(payload.get("session_id")), "event": event, + "tool_name": _safe_text(payload.get("tool_name")) if event != "stop" else None, "tool_class": tool_class if event != "stop" else None, - "cmd_head": cmd_head(command) - if event != "stop" and tool_class == "shell" - else None, - "input_sha256": inp[0], - "input_len": inp[1], - "response_sha256": response[0], - "response_len": response[1], - "decision": decision, - "rule_id": rule_id, - "policy_sha256": policy_sha, - "hook_ms": elapsed, + "cmd_head": cmd_head(command) if event != "stop" and tool_class == "shell" else None, + "input_sha256": inp[0], "input_len": inp[1], "response_sha256": response[0], + "response_len": response[1], "decision": decision, "rule_id": rule_id, + "policy_sha256": policy_sha, "hook_ms": elapsed, } return {key: values[key] for key in _LOG_KEYS} @@ -278,11 +219,9 @@ def _append_log(path: Path, record: dict[str, Any]) -> None: except (json.JSONDecodeError, TypeError): pass record["seq"] = maximum + 1 - raw = ( - json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n" - ).encode() + raw = (json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n").encode() while raw: - raw = raw[os.write(fd, raw) :] + raw = raw[os.write(fd, raw):] os.fsync(fd) finally: if locked: @@ -293,12 +232,7 @@ def _append_log(path: Path, record: dict[str, Any]) -> None: def _emit(decision: str, rule_id: str) -> None: - print( - f"decision=allow rule_id={rule_id}" - if decision == "allow" - else f"beddel-kiro-hook: {rule_id}", - file=None if decision == "allow" else sys.stderr, - ) + print(f"decision=allow rule_id={rule_id}" if decision == "allow" else f"beddel-kiro-hook: {rule_id}", file=None if decision == "allow" else sys.stderr) def _run(payload: dict[str, Any], pointers: Path) -> int: @@ -306,69 +240,40 @@ def _run(payload: dict[str, Any], pointers: Path) -> int: if not isinstance(cwd, str): raise TypeError("missing cwd") pointer = pointer_path(pointers, cwd) - if not _secure_file(pointer): + if pointer.is_symlink() or not pointer.is_file(): _emit("deny", "pointer.missing") return 2 + event = _event_name(payload.get("hook_event_name")) log_path: Path | None = None policy_sha: str | None = None loaded = False try: - data = _load_json(pointer) + data = json.loads(pointer.read_text(encoding="utf-8")) if not isinstance(data, dict): raise TypeError("invalid pointer") _, log_path = _pointer_paths(data) + policy_path, _ = _pointer_paths(data) + try: + policy_sha = hashlib.sha256(policy_path.read_bytes()).hexdigest() + except OSError: + pass data, policy, policy_sha, log_path = _load_context(data) + if not _secure_file(pointer): + raise ValueError("insecure pointer") loaded = True - name = str(payload.get("hook_event_name", "")).lower() - event = { - "pretooluse": "pre", - "pre": "pre", - "posttooluse": "post", - "post": "post", - "stop": "stop", - "sessionend": "stop", - }.get(name) if event is None: raise ValueError("unknown event") started = time.perf_counter() tool_class = classify(payload.get("tool_name")) command = _command(payload.get("tool_input")) - decision, rule = ( - _evaluate(policy, tool_class, command) - if event == "pre" - else ("allow", f"hook.{event}") - ) - _append_log( - log_path, - _record( - event, - decision, - rule, - policy_sha, - (time.perf_counter() - started) * 1000, - payload, - command, - tool_class, - ), - ) + decision, rule = _evaluate(policy, tool_class, command) if event == "pre" else ("allow", f"hook.{event}") + _append_log(log_path, _record(event, decision, rule, policy_sha, (time.perf_counter() - started) * 1000, payload, command, tool_class)) _emit(decision, rule) return 0 if decision == "allow" else 2 except BaseException: # noqa: BLE001 if log_path is not None: try: - _append_log( - log_path, - _record( - "error", - "error", - "hook.internal" if loaded else "policy.invalid", - policy_sha, - 0.0, - payload, - None, - None, - ), - ) + _append_log(log_path, _record(event or "unknown", "error", "hook.internal" if loaded else "policy.invalid", policy_sha, 0.0, payload, None, None)) except BaseException: # noqa: BLE001 log_path = None _emit("error", "hook.internal" if loaded else "policy.invalid") @@ -391,16 +296,7 @@ def main(argv: list[str] | None = None, stdin: TextIO | None = None) -> int: def summarize(pointers: Path, cwd: Path) -> dict[str, Any]: - result: dict[str, Any] = { - "type": "kiro-hook", - "events": 0, - "pre": 0, - "post": 0, - "denied": 0, - "stop": 0, - "errors": 0, - "log_sha256": None, - } + result: dict[str, Any] = {"type": "kiro-hook", "events": 0, "pre": 0, "post": 0, "denied": 0, "stop": 0, "errors": 0, "log_sha256": None} try: data = _load_json(pointer_path(pointers, str(cwd))) if not isinstance(data, dict): @@ -411,19 +307,17 @@ def summarize(pointers: Path, cwd: Path) -> dict[str, Any]: raw = log_path.read_bytes() result["log_sha256"] = hashlib.sha256(raw).hexdigest() for line in raw.decode("utf-8", errors="replace").splitlines(): + result["events"] += 1 try: record = json.loads(line) except (json.JSONDecodeError, UnicodeError): continue event = record.get("event") if isinstance(record, dict) else None if event in {"pre", "post", "stop"}: - result["events"] += 1 result[event] += 1 if isinstance(record, dict) and record.get("decision") == "deny": result["denied"] += 1 - if isinstance(record, dict) and ( - record.get("decision") == "error" or event == "error" - ): + if isinstance(record, dict) and (record.get("decision") == "error" or event == "error"): result["errors"] += 1 except BaseException: # noqa: BLE001 return result diff --git a/kits/agent-kiro-kit/tests/test_agent_config.py b/kits/agent-kiro-kit/tests/test_agent_config.py index 837e44d3..0ffc02b9 100644 --- a/kits/agent-kiro-kit/tests/test_agent_config.py +++ b/kits/agent-kiro-kit/tests/test_agent_config.py @@ -71,3 +71,10 @@ def test_install_preserves_preexisting_kiro_mode(tmp_path: Path) -> None: kiro.chmod(0o755) install(home, tmp_path / "pointers") assert kiro.stat().st_mode & 0o777 == 0o755 + + +def test_default_pointers_follow_home(tmp_path: Path, monkeypatch) -> None: + monkeypatch.setattr(Path, "home", lambda: tmp_path / "sentinel-home") + assert main(["install", "--home", str(tmp_path)]) == 0 + assert (tmp_path / ".local/state/beddel/kiro-hook/pointers").is_dir() + assert not (tmp_path / "sentinel-home/.local/state").exists() diff --git a/kits/agent-kiro-kit/tests/test_delivery_stage.py b/kits/agent-kiro-kit/tests/test_delivery_stage.py index bad3ad67..2993f4e8 100644 --- a/kits/agent-kiro-kit/tests/test_delivery_stage.py +++ b/kits/agent-kiro-kit/tests/test_delivery_stage.py @@ -64,6 +64,7 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat with open(os.path.join(os.environ["HOME"], ".kiro", "agents", agent_name + ".json")) as agent_file: agent = json.load(agent_file) hook_results = [] + hook_output = [] tool_allowed = True for hook in agent["hooks"]: event_name = hook["trigger"] @@ -76,7 +77,7 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat }} if event_name != "stop": payload["tool_name"] = "shell" - payload["tool_input"] = {{"command": os.environ.get("FAKE_KIRO_COMMAND", "true")}} + payload["tool_input"] = {{"command": os.environ.get("FAKE_KIRO_COMMAND", "true"), "marker": os.environ.get("FAKE_KIRO_MARKER_TEXT", "")}} if event_name == "postToolUse": payload["tool_response"] = {{"ok": True}} completed = subprocess.run( @@ -85,7 +86,9 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat text=True, shell=True, check=False, + capture_output=True, ) + hook_output.append(completed.stdout + completed.stderr) hook_results.append(completed.returncode) if event_name == "preToolUse" and completed.returncode == 2: tool_allowed = False @@ -94,6 +97,7 @@ def fake_cli(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Pat if marker: open(marker, "w").close() record["hook_results"] = hook_results + record["hook_output"] = hook_output with open(os.environ["FAKE_KIRO_LOG"], "w") as log_file: json.dump(record, log_file) sys.stdout.write("ok") @@ -169,6 +173,8 @@ async def test_agent_missing_exit_maps_to_kit_error( assert exc_info.value.code == KIRO_AGENT_INVALID assert exc_info.value.details["agent"] == "custom-agent" + assert exc_info.value.details["exit_code"] == 4 + assert exc_info.value.details["hint"] @pytest.mark.asyncio @@ -222,13 +228,17 @@ async def test_trust_all_hook_policy_counts( monkeypatch.setenv("FAKE_KIRO_MODE", "hooks") monkeypatch.setenv("FAKE_KIRO_COMMAND", command) marker = tmp_path / "allowed.marker" + marker_text = "TOP-SECRET-HOOK-MARKER" monkeypatch.setenv("FAKE_KIRO_MARKER", str(marker)) + monkeypatch.setenv("FAKE_KIRO_MARKER_TEXT", marker_text) adapter = KiroCLIAgentAdapter(cli_path=cli_path, pointers_dir=pointers) - result = await adapter.execute("prompt") + result = await adapter.execute("prompt", sandbox="workspace-write") fake_record = _read_record(fake_cli[1]) + assert "-a" in fake_record["argv"] assert fake_record["hook_results"][0] == expected_first + assert result.events[0]["denied"] == int(expected_first == 2) if expected_first == 0: assert ( result.events[0]["pre"] @@ -244,6 +254,8 @@ async def test_trust_all_hook_policy_counts( == hashlib.sha256(log_path.read_bytes()).hexdigest() ) assert marker.exists() is expected_marker + assert marker_text not in json.dumps(result.events) + assert marker_text not in "".join(fake_record["hook_output"]) @pytest.mark.asyncio diff --git a/kits/agent-kiro-kit/tests/test_headless.py b/kits/agent-kiro-kit/tests/test_headless.py index 66acdfd1..fc8efb7b 100644 --- a/kits/agent-kiro-kit/tests/test_headless.py +++ b/kits/agent-kiro-kit/tests/test_headless.py @@ -11,8 +11,9 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest -from beddel_agent_kiro import hook from beddel_agent_kiro.adapter import _DEFAULT_CLI_PATH, KiroCLIAgentAdapter +from beddel_agent_kiro import hook + from beddel.domain.errors import AgentError from beddel.error_codes import AGENT_EXECUTION_FAILED from beddel_agent_kiro.errors import KIRO_CLI_MISSING @@ -23,6 +24,11 @@ def _patch_pointer_dir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: monkeypatch.setattr(hook, "DEFAULT_POINTERS_DIR", tmp_path / "pointers") +# --------------------------------------------------------------------------- +# Helpers +# --------------------------------------------------------------------------- + + _PROMPT = "Deploy the application" _PATCH_EXEC = "beddel_agent_kiro.adapter.asyncio.create_subprocess_exec" _PATCH_WHICH = "beddel_agent_kiro.adapter.shutil.which" @@ -265,14 +271,15 @@ def test_discovery_which_fallback(self, tmp_path: Path) -> None: assert result == Path(found_path) def test_discovery_missing_does_not_install(self, tmp_path: Path) -> None: - """Missing binaries raise without invoking an installer.""" + """Missing binaries fail at construction without an installer.""" fake_default = tmp_path / "nonexistent" / "kiro-cli" with ( patch("beddel_agent_kiro.adapter._DEFAULT_CLI_PATH", fake_default), patch(_PATCH_WHICH, return_value=None), + patch("subprocess.run", side_effect=AssertionError("installer called")), pytest.raises(AgentError) as exc_info, ): - KiroCLIAgentAdapter._discover_cli() + KiroCLIAgentAdapter() assert exc_info.value.code == KIRO_CLI_MISSING assert "kiro.dev/docs/cli" in exc_info.value.details["hint"] diff --git a/kits/agent-kiro-kit/tests/test_hook.py b/kits/agent-kiro-kit/tests/test_hook.py index 7c55c925..3ed29a3b 100644 --- a/kits/agent-kiro-kit/tests/test_hook.py +++ b/kits/agent-kiro-kit/tests/test_hook.py @@ -71,20 +71,22 @@ def _call(pointers: Path, payload: dict[str, Any]) -> tuple[int, str, str]: @pytest.mark.parametrize( - "command", + ("command", "rule"), [ - "true\nblocked", - "true|blocked", - "true&blocked", - "true||blocked", - "true;blocked", - "echo `blocked`", - "cat <(blocked)", - "cat >(blocked)", - "'unbalanced", + ("true\nblocked", "shell.blocked"), + ("true|blocked", "shell.blocked"), + ("true&blocked", "shell.blocked"), + ("true||blocked", "shell.blocked"), + ("true;blocked", "shell.blocked"), + ("echo `blocked`", "shell.substitution"), + ("cat <(blocked)", "shell.substitution"), + ("cat >(blocked)", "shell.substitution"), + ("'unbalanced", "shell.parse"), ], ) -def test_shell_segments_and_substitutions_deny(tmp_path: Path, command: str) -> None: +def test_shell_segments_and_substitutions_deny( + tmp_path: Path, command: str, rule: str +) -> None: rules = [ { "id": "shell.blocked", @@ -96,7 +98,7 @@ def test_shell_segments_and_substitutions_deny(tmp_path: Path, command: str) -> ] pointers, _policy, log = _setup(tmp_path, rules) code, _out, err = _call(pointers, _event(tmp_path, command=command)) - assert code == 2 and "beddel-kiro-hook:" in err + assert code == 2 and f"beddel-kiro-hook: {rule}" in err assert command not in log.read_text(encoding="utf-8") @@ -114,7 +116,13 @@ def test_allow_pointer_errors_and_no_command(tmp_path: Path) -> None: policy.write_text("{}", encoding="utf-8") os.chmod(policy, 0o600) assert "policy.invalid" in _call(pointers, _event(policy.parent))[2] - assert '"decision":"error"' in log.read_text(encoding="utf-8") + pointers, _policy, _log = _setup(tmp_path / "default") + root = tmp_path / "default" + code, _out, err = _call(pointers, _event(root, tool="other", command="x")) + assert code == 2 and "policy.default_deny" in err + assert hook.cmd_head("K=v x") is None + assert hook.cmd_head("/usr/bin/Git") == "git" + assert hook.cmd_head("écho") is None def test_post_stop_redaction_schema_and_digest(tmp_path: Path) -> None: @@ -251,3 +259,21 @@ def test_getattr_and_rendered_commands(tmp_path: Path) -> None: ).returncode == 2 ) + + +def test_internal_and_pointer_error_rows(tmp_path: Path) -> None: + pointers, policy, log = _setup(tmp_path) + assert hook.main(["--pointers", str(pointers)], io.StringIO("{")) == 2 + assert _call(pointers, {"hook_event_name": "preToolUse"})[0] == 2 + assert _call(pointers, _event(tmp_path, name="other"))[0] == 2 + policy.chmod(0o622) + code, _out, err = _call(pointers, _event(tmp_path, command="true")) + assert code == 2 and "policy.invalid" in err + record = json.loads(log.read_text(encoding="utf-8").splitlines()[-1]) + assert record["policy_sha256"] + pointer = hook.pointer_path(pointers, str(tmp_path)) + pointer.chmod(0o622) + assert "policy.invalid" in _call(pointers, _event(tmp_path, command="true"))[2] + assert ( + json.loads(log.read_text(encoding="utf-8").splitlines()[-1])["event"] == "pre" + ) From 26d3f55cf8d901d0bd3123ca1b6ae4431271427b Mon Sep 17 00:00:00 2001 From: Carlos Bacelar Date: Tue, 17 Nov 2026 19:53:19 -0300 Subject: [PATCH 3/3] fix(agent-kiro-kit): harden policy and log opens against FIFOs and untrusted pointers (Story 8.1) --- kits/agent-kiro-kit/README.md | 7 +- .../python/beddel_agent_kiro/hook.py | 243 ++++++++++++++---- kits/agent-kiro-kit/tests/test_hook.py | 155 ++++------- 3 files changed, 246 insertions(+), 159 deletions(-) diff --git a/kits/agent-kiro-kit/README.md b/kits/agent-kiro-kit/README.md index e44d4d5b..b31311ad 100644 --- a/kits/agent-kiro-kit/README.md +++ b/kits/agent-kiro-kit/README.md @@ -63,7 +63,10 @@ strategy the step failure `BEDDEL-EXEC-002` wraps it as its cause. Operator install/verify: `PYTHONPATH=/python python3 -m beddel_agent_kiro.agent_config install|verify`; supports `--home`, `--pointers`, `--replace`. Pointer `/.json`; policy `beddel.kiro-hook-policy/1`, first-match-wins, no rules deny all; Python 3.11+; errors `KIRO_AGENT_INVALID`/`KIRO_CLI_MISSING`. -Log schema `beddel.kiro-hook/1`: `v`, `ts`, `seq`, `session_id`, `event`, `tool_name`, `tool_class`, `cmd_head`, `input_sha256`, `input_len`, `response_sha256`, `response_len`, `decision`, `rule_id`, `policy_sha256`, `hook_ms`. +Pointer or log path security failures deny without writing through the pointer. Logs use +`beddel.kiro-hook/1` and 16 fields; results include `type=kiro-hook`, counts, +and `log_sha256`. Exit 2 means deny/error; raw values are never logged. +`policy_sha256` is the SHA-256 of raw policy bytes. ## Headless Mode (CI/CD) @@ -155,7 +158,7 @@ If `cli_path` is not provided, the adapter discovers an already-installed 1. Default path: `~/.local/bin/kiro-cli` 2. PATH lookup: `shutil.which("kiro-cli")` -If both fail, `KIRO_CLI_MISSING`; manual text. No auto-install/network call. +If both fail, `KIRO_CLI_MISSING` is raised. The adapter never auto-installs. ## Running Tests diff --git a/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py index 0533e989..aafa2691 100644 --- a/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py +++ b/kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py @@ -14,8 +14,6 @@ from pathlib import Path from typing import Any, TextIO -# fmt: off - DEFAULT_POINTERS_DIR = Path.home() / ".local/state/beddel/kiro-hook/pointers" SCHEMA = "beddel.kiro-hook/1" _POLICY_SCHEMA = "beddel.kiro-hook-policy/1" @@ -27,14 +25,35 @@ _SPLIT_RE = re.compile(r"&&|\|\||[;|&\n]") _CLASSES = {"read", "write", "shell", "other"} _LOG_KEYS = ( - "v", "ts", "seq", "session_id", "event", "tool_name", "tool_class", - "cmd_head", "input_sha256", "input_len", "response_sha256", "response_len", - "decision", "rule_id", "policy_sha256", "hook_ms", + "v", + "ts", + "seq", + "session_id", + "event", + "tool_name", + "tool_class", + "cmd_head", + "input_sha256", + "input_len", + "response_sha256", + "response_len", + "decision", + "rule_id", + "policy_sha256", + "hook_ms", ) +class _PolicyError(ValueError): + def __init__(self, message: str, digest: str | None = None) -> None: + super().__init__(message) + self.digest = digest + + def _canonical(value: Any) -> bytes: - return json.dumps(value, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8") + return json.dumps( + value, sort_keys=True, separators=(",", ":"), ensure_ascii=False + ).encode("utf-8") def _digest(value: Any) -> tuple[str, int]: @@ -73,7 +92,9 @@ def _safe_text(value: Any) -> str | None: def _event_name(value: Any) -> str | None: - return {"pretooluse": "pre", "posttooluse": "post", "stop": "stop"}.get(str(value or "").lower()) + return {"pretooluse": "pre", "posttooluse": "post", "stop": "stop"}.get( + str(value or "").lower() + ) def _segments(command: str) -> list[str]: @@ -95,7 +116,12 @@ def cmd_head(command: Any) -> str | None: def _secure_file(path: Path) -> bool: try: mode = path.stat().st_mode - return path.is_file() and not path.is_symlink() and not (mode & 0o022) and path.stat().st_uid == os.getuid() + return ( + path.is_file() + and not path.is_symlink() + and not (mode & 0o022) + and path.stat().st_uid == os.getuid() + ) except OSError: return False @@ -109,20 +135,34 @@ def _load_json(path: Path) -> Any: def _validate_policy(policy: Any) -> None: if not isinstance(policy, dict) or set(policy) != _POLICY_KEYS: raise ValueError("invalid policy keys") - if policy.get("schema") != _POLICY_SCHEMA or not isinstance(policy.get("rules"), list): + if policy.get("schema") != _POLICY_SCHEMA or not isinstance( + policy.get("rules"), list + ): raise ValueError("invalid policy schema") seen: set[str] = set() for rule in policy["rules"]: if not isinstance(rule, dict) or not set(rule) <= _RULE_KEYS: raise ValueError("invalid policy rule") rule_id = rule.get("id") - if not isinstance(rule_id, str) or not _ID_RE.fullmatch(rule_id) or rule_id in seen: + if ( + not isinstance(rule_id, str) + or not _ID_RE.fullmatch(rule_id) + or rule_id in seen + ): raise ValueError("invalid or duplicate rule id") seen.add(rule_id) - if rule.get("effect") not in {"allow", "deny"} or rule.get("tool_class") not in _CLASSES: + if ( + rule.get("effect") not in {"allow", "deny"} + or rule.get("tool_class") not in _CLASSES + ): raise ValueError("invalid policy rule") prefix = rule.get("argv_prefix") - if prefix is not None and (rule["tool_class"] != "shell" or not isinstance(prefix, list) or not prefix or not all(isinstance(item, str) for item in prefix)): + if prefix is not None and ( + rule["tool_class"] != "shell" + or not isinstance(prefix, list) + or not prefix + or not all(isinstance(item, str) for item in prefix) + ): raise ValueError("invalid argv prefix") @@ -133,25 +173,42 @@ def _pointer_paths(data: dict[str, Any]) -> tuple[Path, Path]: if not isinstance(policy, str) or not isinstance(log_path, str): raise TypeError("invalid pointer paths") policy_path, log = Path(policy), Path(log_path) - if not policy_path.is_absolute() or not log.is_absolute() or not isinstance(data.get("stage"), str): + if ( + not policy_path.is_absolute() + or not log.is_absolute() + or not isinstance(data.get("stage"), str) + ): raise ValueError("invalid pointer paths") return policy_path, log -def _load_context(data: dict[str, Any]) -> tuple[dict[str, Any], dict[str, Any], str, Path]: +def _load_context( + data: dict[str, Any], +) -> tuple[dict[str, Any], dict[str, Any], str, Path]: policy_path, log_path = _pointer_paths(data) - if policy_path.is_symlink() or not policy_path.is_file(): - raise ValueError("insecure or missing policy") - raw = policy_path.read_bytes() + flags = os.O_RDONLY | os.O_NONBLOCK | getattr(os, "O_NOFOLLOW", 0) + fd = os.open(policy_path, flags) + try: + st = os.fstat(fd) + if not stat.S_ISREG(st.st_mode): + raise OSError("policy is not regular") + raw = os.read(fd, 1 << 20) + if len(raw) >= 1 << 20: + raise OSError("policy is too large") + policy_sha = hashlib.sha256(raw).hexdigest() + if st.st_uid != os.getuid() or st.st_mode & 0o022: + raise _PolicyError("insecure policy", policy_sha) + finally: + os.close(fd) policy_sha = hashlib.sha256(raw).hexdigest() - if not _secure_file(policy_path): - raise ValueError("insecure policy") policy = json.loads(raw) _validate_policy(policy) return data, policy, policy_sha, log_path -def _evaluate(policy: dict[str, Any], tool_class: str, command: str | None) -> tuple[str, str]: +def _evaluate( + policy: dict[str, Any], tool_class: str, command: str | None +) -> tuple[str, str]: if tool_class == "shell": if command is None: return "deny", "shell.no_command" @@ -166,7 +223,18 @@ def _evaluate(policy: dict[str, Any], tool_class: str, command: str | None) -> t tokens = shlex.split(part) except ValueError: return "deny", "shell.parse" - rule = next((r for r in policy["rules"] if r["tool_class"] == tool_class and (r.get("argv_prefix") is None or tokens[:len(r["argv_prefix"])] == r["argv_prefix"])), None) + rule = next( + ( + r + for r in policy["rules"] + if r["tool_class"] == tool_class + and ( + r.get("argv_prefix") is None + or tokens[: len(r["argv_prefix"])] == r["argv_prefix"] + ) + ), + None, + ) if rule is None: return "deny", "policy.default_deny" if rule["effect"] != "allow": @@ -175,18 +243,41 @@ def _evaluate(policy: dict[str, Any], tool_class: str, command: str | None) -> t return "allow", selected or "policy.allow" -def _record(event: str, decision: str, rule_id: str, policy_sha: str | None, elapsed: float, payload: dict[str, Any], command: str | None, tool_class: str | None) -> dict[str, Any]: - inp = _digest(payload.get("tool_input")) if event in {"pre", "post"} else (None, None) - response = _digest(payload.get("tool_response")) if event == "post" else (None, None) +def _record( + event: str, + decision: str, + rule_id: str, + policy_sha: str | None, + elapsed: float, + payload: dict[str, Any], + command: str | None, + tool_class: str | None, +) -> dict[str, Any]: + inp = ( + _digest(payload.get("tool_input")) if event in {"pre", "post"} else (None, None) + ) + response = ( + _digest(payload.get("tool_response")) if event == "post" else (None, None) + ) values = { - "v": SCHEMA, "ts": datetime.now(timezone.utc).isoformat(), "seq": 0, - "session_id": _safe_text(payload.get("session_id")), "event": event, + "v": SCHEMA, + "ts": datetime.now(timezone.utc).isoformat(), + "seq": 0, + "session_id": _safe_text(payload.get("session_id")), + "event": event, "tool_name": _safe_text(payload.get("tool_name")) if event != "stop" else None, "tool_class": tool_class if event != "stop" else None, - "cmd_head": cmd_head(command) if event != "stop" and tool_class == "shell" else None, - "input_sha256": inp[0], "input_len": inp[1], "response_sha256": response[0], - "response_len": response[1], "decision": decision, "rule_id": rule_id, - "policy_sha256": policy_sha, "hook_ms": elapsed, + "cmd_head": cmd_head(command) + if event != "stop" and tool_class == "shell" + else None, + "input_sha256": inp[0], + "input_len": inp[1], + "response_sha256": response[0], + "response_len": response[1], + "decision": decision, + "rule_id": rule_id, + "policy_sha256": policy_sha, + "hook_ms": elapsed, } return {key: values[key] for key in _LOG_KEYS} @@ -196,15 +287,21 @@ def _append_log(path: Path, record: dict[str, Any]) -> None: if path.is_symlink() or not path.parent.is_dir(): raise OSError("log path or parent directory is unavailable") - flags = os.O_WRONLY | os.O_CREAT | os.O_APPEND | getattr(os, "O_NOFOLLOW", 0) + flags = ( + os.O_WRONLY + | os.O_CREAT + | os.O_APPEND + | os.O_NONBLOCK + | getattr(os, "O_NOFOLLOW", 0) + ) fd = os.open(path, flags, 0o600) + st = os.fstat(fd) + if not stat.S_ISREG(st.st_mode) or st.st_uid != os.getuid() or st.st_mode & 0o077: + os.close(fd) + raise OSError("log file is not a private regular file") read_fd: int | None = None locked = False try: - os.fchmod(fd, 0o600) - mode = os.fstat(fd).st_mode - if not stat.S_ISREG(mode) or mode & 0o077: - raise OSError("log file is not a private regular file") fcntl.flock(fd, fcntl.LOCK_EX) locked = True read_fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0)) @@ -219,9 +316,11 @@ def _append_log(path: Path, record: dict[str, Any]) -> None: except (json.JSONDecodeError, TypeError): pass record["seq"] = maximum + 1 - raw = (json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n").encode() + raw = ( + json.dumps(record, sort_keys=True, separators=(",", ":")) + "\n" + ).encode() while raw: - raw = raw[os.write(fd, raw):] + raw = raw[os.write(fd, raw) :] os.fsync(fd) finally: if locked: @@ -232,7 +331,12 @@ def _append_log(path: Path, record: dict[str, Any]) -> None: def _emit(decision: str, rule_id: str) -> None: - print(f"decision=allow rule_id={rule_id}" if decision == "allow" else f"beddel-kiro-hook: {rule_id}", file=None if decision == "allow" else sys.stderr) + print( + f"decision=allow rule_id={rule_id}" + if decision == "allow" + else f"beddel-kiro-hook: {rule_id}", + file=None if decision == "allow" else sys.stderr, + ) def _run(payload: dict[str, Any], pointers: Path) -> int: @@ -248,32 +352,58 @@ def _run(payload: dict[str, Any], pointers: Path) -> int: policy_sha: str | None = None loaded = False try: + if not _secure_file(pointer): + _emit("error", "policy.invalid") + return 2 data = json.loads(pointer.read_text(encoding="utf-8")) if not isinstance(data, dict): raise TypeError("invalid pointer") _, log_path = _pointer_paths(data) - policy_path, _ = _pointer_paths(data) - try: - policy_sha = hashlib.sha256(policy_path.read_bytes()).hexdigest() - except OSError: - pass data, policy, policy_sha, log_path = _load_context(data) - if not _secure_file(pointer): - raise ValueError("insecure pointer") loaded = True if event is None: raise ValueError("unknown event") started = time.perf_counter() tool_class = classify(payload.get("tool_name")) command = _command(payload.get("tool_input")) - decision, rule = _evaluate(policy, tool_class, command) if event == "pre" else ("allow", f"hook.{event}") - _append_log(log_path, _record(event, decision, rule, policy_sha, (time.perf_counter() - started) * 1000, payload, command, tool_class)) + decision, rule = ( + _evaluate(policy, tool_class, command) + if event == "pre" + else ("allow", f"hook.{event}") + ) + _append_log( + log_path, + _record( + event, + decision, + rule, + policy_sha, + (time.perf_counter() - started) * 1000, + payload, + command, + tool_class, + ), + ) _emit(decision, rule) return 0 if decision == "allow" else 2 - except BaseException: # noqa: BLE001 + except BaseException as error: # noqa: BLE001 + if isinstance(error, _PolicyError): + policy_sha = error.digest if log_path is not None: try: - _append_log(log_path, _record(event or "unknown", "error", "hook.internal" if loaded else "policy.invalid", policy_sha, 0.0, payload, None, None)) + _append_log( + log_path, + _record( + event or "unknown", + "error", + "hook.internal" if loaded else "policy.invalid", + policy_sha, + 0.0, + payload, + None, + None, + ), + ) except BaseException: # noqa: BLE001 log_path = None _emit("error", "hook.internal" if loaded else "policy.invalid") @@ -296,7 +426,16 @@ def main(argv: list[str] | None = None, stdin: TextIO | None = None) -> int: def summarize(pointers: Path, cwd: Path) -> dict[str, Any]: - result: dict[str, Any] = {"type": "kiro-hook", "events": 0, "pre": 0, "post": 0, "denied": 0, "stop": 0, "errors": 0, "log_sha256": None} + result: dict[str, Any] = { + "type": "kiro-hook", + "events": 0, + "pre": 0, + "post": 0, + "denied": 0, + "stop": 0, + "errors": 0, + "log_sha256": None, + } try: data = _load_json(pointer_path(pointers, str(cwd))) if not isinstance(data, dict): @@ -317,7 +456,9 @@ def summarize(pointers: Path, cwd: Path) -> dict[str, Any]: result[event] += 1 if isinstance(record, dict) and record.get("decision") == "deny": result["denied"] += 1 - if isinstance(record, dict) and (record.get("decision") == "error" or event == "error"): + if isinstance(record, dict) and ( + record.get("decision") == "error" or event == "error" + ): result["errors"] += 1 except BaseException: # noqa: BLE001 return result diff --git a/kits/agent-kiro-kit/tests/test_hook.py b/kits/agent-kiro-kit/tests/test_hook.py index 3ed29a3b..91255dfe 100644 --- a/kits/agent-kiro-kit/tests/test_hook.py +++ b/kits/agent-kiro-kit/tests/test_hook.py @@ -4,9 +4,9 @@ import io import json -import multiprocessing import os import subprocess +import sys from pathlib import Path from typing import Any @@ -78,7 +78,8 @@ def _call(pointers: Path, payload: dict[str, Any]) -> tuple[int, str, str]: ("true&blocked", "shell.blocked"), ("true||blocked", "shell.blocked"), ("true;blocked", "shell.blocked"), - ("echo `blocked`", "shell.substitution"), + ("echo $(blocked)", "shell.substitution"), + ("a&&blocked", "shell.blocked"), ("cat <(blocked)", "shell.substitution"), ("cat >(blocked)", "shell.substitution"), ("'unbalanced", "shell.parse"), @@ -122,7 +123,7 @@ def test_allow_pointer_errors_and_no_command(tmp_path: Path) -> None: assert code == 2 and "policy.default_deny" in err assert hook.cmd_head("K=v x") is None assert hook.cmd_head("/usr/bin/Git") == "git" - assert hook.cmd_head("écho") is None + assert hook.cmd_head("cmd=git push") is None def test_post_stop_redaction_schema_and_digest(tmp_path: Path) -> None: @@ -131,7 +132,8 @@ def test_post_stop_redaction_schema_and_digest(tmp_path: Path) -> None: post = _event(tmp_path, command="printf x", name="postToolUse") post["tool_input"]["marker"] = marker post["tool_response"] = {"marker": marker} - assert _call(pointers, post)[0] == 0 + code, out, err = _call(pointers, post) + assert code == 0 and marker not in out and marker not in err assert _call(pointers, _event(tmp_path, name="stop"))[0] == 0 raw = log.read_text(encoding="utf-8") records = [json.loads(line) for line in raw.splitlines()] @@ -140,31 +142,6 @@ def test_post_stop_redaction_schema_and_digest(tmp_path: Path) -> None: assert records[1]["tool_name"] is None -def _child(pointers: str, cwd: str) -> None: - payload = _event(Path(cwd), tool="read") - payload["tool_input"] = {} - raise SystemExit( - hook.main(["--pointers", pointers], io.StringIO(json.dumps(payload))) - ) - - -def test_concurrent_sequences_are_unique(tmp_path: Path) -> None: - pointers, _policy, log = _setup(tmp_path) - processes = [ - multiprocessing.Process(target=_child, args=(str(pointers), str(tmp_path))) - for _ in range(2) - ] - for process in processes: - process.start() - for process in processes: - process.join(5) - assert process.exitcode == 0 - records = [ - json.loads(line) for line in log.read_text(encoding="utf-8").splitlines() - ] - assert sorted(record["seq"] for record in records) == [1, 2] - - def test_policy_schema_log_permissions_and_relative_path(tmp_path: Path) -> None: cases = ( [ @@ -185,95 +162,61 @@ def test_policy_schema_log_permissions_and_relative_path(tmp_path: Path) -> None pointers, _policy, log = _setup(root, rules) assert "policy.invalid" in _call(pointers, _event(root, command="true"))[2] assert log.exists() - pointers, policy, _log = _setup(tmp_path / "relative") - root = tmp_path / "relative" - hook.pointer_path(pointers, str(root)).write_text( - json.dumps( - {"policy": str(policy), "log_path": "relative.json", "stage": "test"} - ), - encoding="utf-8", - ) - assert ( - _call(pointers, _event(root, name="stop"))[0] == 2 - and not (root / "relative.json").exists() - ) - - -def test_fresh_log_umask_and_invalid_utf8(tmp_path: Path) -> None: - pointers, _policy, log = _setup(tmp_path) - payload = _event(tmp_path, tool="read") - payload["tool_input"] = {} - old_umask = os.umask(0o002) - try: - assert _call(pointers, payload)[0] == _call(pointers, payload)[0] == 0 - finally: - os.umask(old_umask) - assert log.stat().st_mode & 0o077 == 0 - log.write_bytes(log.read_bytes() + b"\xff\n") - assert _call(pointers, payload)[0] == 0 - - -def test_getattr_and_rendered_commands(tmp_path: Path) -> None: - import beddel_agent_kiro - from beddel_agent_kiro.adapter import KiroCLIAgentAdapter - from beddel_agent_kiro.agent_config import render - - assert getattr(beddel_agent_kiro, "KiroCLIAgentAdapter") is KiroCLIAgentAdapter # noqa: B009 - pointers, _policy, log = _setup( - tmp_path, [{"id": "shell.deny", "effect": "deny", "tool_class": "shell"}] - ) - command = json.loads(render(pointers))["hooks"][0]["action"]["command"] - env = {"PATH": os.environ["PATH"]} - denied = subprocess.run( - command, - input=json.dumps(_event(tmp_path, command="true")), - text=True, - shell=True, - check=False, - env=env, - ) - invalid = subprocess.run( - command, - input=json.dumps(_event(tmp_path / "missing")), - text=True, - shell=True, - check=False, - env=env, - ) - assert denied.returncode == invalid.returncode == 2 - assert log.exists() - stub = tmp_path / "stub" / "beddel" - stub.mkdir(parents=True) - (stub / "__init__.py").write_text( - "raise RuntimeError('must not import')", encoding="utf-8" - ) - stub_command = command.replace("PYTHONPATH=", f"PYTHONPATH={stub}:", 1) - assert ( - subprocess.run( - stub_command, - input=json.dumps(_event(tmp_path, command="true")), - text=True, - shell=True, - check=False, - env=env, - ).returncode - == 2 - ) def test_internal_and_pointer_error_rows(tmp_path: Path) -> None: pointers, policy, log = _setup(tmp_path) assert hook.main(["--pointers", str(pointers)], io.StringIO("{")) == 2 assert _call(pointers, {"hook_event_name": "preToolUse"})[0] == 2 - assert _call(pointers, _event(tmp_path, name="other"))[0] == 2 + assert _call(pointers, _event(tmp_path, command="true", name="other"))[0] == 2 + assert any( + json.loads(line)["event"] == "unknown" for line in log.read_text().splitlines() + ) policy.chmod(0o622) code, _out, err = _call(pointers, _event(tmp_path, command="true")) assert code == 2 and "policy.invalid" in err record = json.loads(log.read_text(encoding="utf-8").splitlines()[-1]) assert record["policy_sha256"] + assert record["event"] == "pre" + policy.chmod(0o600) + before = (log.stat().st_mode, log.read_bytes()) pointer = hook.pointer_path(pointers, str(tmp_path)) pointer.chmod(0o622) assert "policy.invalid" in _call(pointers, _event(tmp_path, command="true"))[2] - assert ( - json.loads(log.read_text(encoding="utf-8").splitlines()[-1])["event"] == "pre" + assert (log.stat().st_mode, log.read_bytes()) == before + + +@pytest.mark.parametrize("target", ["policy", "log"]) +def test_fifo_paths_do_not_hang(tmp_path: Path, target: str) -> None: + pointers, policy, log = _setup(tmp_path) + fifo = tmp_path / f"{target}.fifo" + os.mkfifo(fifo) + if target == "policy": + pointer_data = {"policy": str(fifo), "log_path": str(log), "stage": "test"} + else: + pointer_data = {"policy": str(policy), "log_path": str(fifo), "stage": "test"} + hook.pointer_path(pointers, str(tmp_path)).write_text( + json.dumps(pointer_data), encoding="utf-8" + ) + command = [ + sys.executable, + "-P", + "-m", + "beddel_agent_kiro.hook", + "--pointers", + str(pointers), + ] + env = { + "PYTHONPATH": str(Path(__file__).resolve().parent.parent / "python"), + "PATH": os.environ["PATH"], + } + completed = subprocess.run( + command, + input=json.dumps(_event(tmp_path, command="true")), + text=True, + capture_output=True, + check=False, + env=env, + timeout=5, ) + assert completed.returncode == 2