Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion kits/agent-kiro-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ strategy the step failure `BEDDEL-EXEC-002` wraps it as its cause.

Operator install/verify: `PYTHONPATH=<kit>/python python3 -m beddel_agent_kiro.agent_config install|verify`; supports `--home`, `--pointers`, `--replace`.
Pointer `<pointers>/<sha256(realpath(cwd))>.json`; policy `beddel.kiro-hook-policy/1`, first-match-wins, no rules deny all; Python 3.11+; errors `KIRO_AGENT_INVALID`/`KIRO_CLI_MISSING`.
Pointer or log path security failures deny without writing through the pointer. Logs use
Pointer or log path security failures deny without writing through the pointer. Each agent-stage policy must declare a positive `max_tool_calls`; the call past the cap is denied with `cap.max_tool_calls` and counted as `denied`. 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.
Expand Down
31 changes: 26 additions & 5 deletions kits/agent-kiro-kit/python/beddel_agent_kiro/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
SCHEMA = "beddel.kiro-hook/1"
_POLICY_SCHEMA = "beddel.kiro-hook-policy/1"
_POINTER_KEYS = frozenset({"policy", "log_path", "stage"})
_POLICY_KEYS = frozenset({"schema", "rules"})
_POLICY_KEYS = frozenset({"schema", "rules", "max_tool_calls"})
_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}")
Expand Down Expand Up @@ -139,6 +139,9 @@ def _validate_policy(policy: Any) -> None:
policy.get("rules"), list
):
raise ValueError("invalid policy schema")
max_tool_calls = policy.get("max_tool_calls")
if type(max_tool_calls) is not int or max_tool_calls < 1:
raise ValueError("invalid max tool calls")
seen: set[str] = set()
for rule in policy["rules"]:
if not isinstance(rule, dict) or not set(rule) <= _RULE_KEYS:
Expand Down Expand Up @@ -282,7 +285,9 @@ def _record(
return {key: values[key] for key in _LOG_KEYS}


def _append_log(path: Path, record: dict[str, Any]) -> None:
def _append_log(
path: Path, record: dict[str, Any], max_tool_calls: int | None
) -> tuple[str, str]:
import fcntl

if path.is_symlink() or not path.parent.is_dir():
Expand All @@ -306,22 +311,35 @@ def _append_log(path: Path, record: dict[str, Any]) -> None:
locked = True
read_fd = os.open(path, os.O_RDONLY | getattr(os, "O_NOFOLLOW", 0))
maximum = 0
pre_calls = 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"])
if isinstance(value, dict):
if isinstance(value.get("seq"), int):
maximum = max(maximum, value["seq"])
if value.get("event") == "pre":
pre_calls += 1
except (json.JSONDecodeError, TypeError):
pass
if (
record.get("event") == "pre"
and record.get("decision") != "error"
and max_tool_calls is not None
and pre_calls >= max_tool_calls
):
record["decision"] = "deny"
record["rule_id"] = "cap.max_tool_calls"
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)
return str(record["decision"]), str(record["rule_id"])
finally:
if locked:
fcntl.flock(fd, fcntl.LOCK_UN)
Expand Down Expand Up @@ -350,6 +368,7 @@ def _run(payload: dict[str, Any], pointers: Path) -> int:
event = _event_name(payload.get("hook_event_name"))
log_path: Path | None = None
policy_sha: str | None = None
policy: dict[str, Any] | None = None
loaded = False
try:
if not _secure_file(pointer):
Expand All @@ -371,7 +390,7 @@ def _run(payload: dict[str, Any], pointers: Path) -> int:
if event == "pre"
else ("allow", f"hook.{event}")
)
_append_log(
decision, rule = _append_log(
log_path,
_record(
event,
Expand All @@ -383,6 +402,7 @@ def _run(payload: dict[str, Any], pointers: Path) -> int:
command,
tool_class,
),
policy["max_tool_calls"],
)
_emit(decision, rule)
return 0 if decision == "allow" else 2
Expand All @@ -403,6 +423,7 @@ def _run(payload: dict[str, Any], pointers: Path) -> int:
None,
None,
),
None,
)
except BaseException: # noqa: BLE001
log_path = None
Expand Down
1 change: 1 addition & 0 deletions kits/agent-kiro-kit/tests/test_delivery_stage.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ async def test_trust_all_hook_policy_counts(
json.dumps(
{
"schema": "beddel.kiro-hook-policy/1",
"max_tool_calls": 100,
"rules": [
{
"id": "shell.git-push",
Expand Down
63 changes: 62 additions & 1 deletion kits/agent-kiro-kit/tests/test_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
import pytest
from beddel_agent_kiro import hook

_OMIT_CAP = object()


def _setup(
tmp_path: Path, rules: list[dict[str, Any]] | None = None
tmp_path: Path,
rules: list[dict[str, Any]] | None = None,
cap: Any = 100,
) -> tuple[Path, Path, Path]:
pointers = tmp_path / "pointers"
pointers.mkdir(parents=True, mode=0o700)
Expand All @@ -28,6 +32,8 @@ def _setup(
{"id": "read.any", "effect": "allow", "tool_class": "read"},
],
}
if cap is not _OMIT_CAP:
policy["max_tool_calls"] = cap
policy_path.write_text(json.dumps(policy), encoding="utf-8")
os.chmod(policy_path, 0o600)
pointer = hook.pointer_path(pointers, str(tmp_path))
Expand Down Expand Up @@ -220,3 +226,58 @@ def test_fifo_paths_do_not_hang(tmp_path: Path, target: str) -> None:
timeout=5,
)
assert completed.returncode == 2


def test_cap_max_tool_calls_denies_past_the_cap(tmp_path: Path) -> None:
rules = [
{
"id": "shell.git-push",
"effect": "deny",
"tool_class": "shell",
"argv_prefix": ["git", "push"],
},
{"id": "shell.any", "effect": "allow", "tool_class": "shell"},
]
pointers, _policy, log = _setup(tmp_path, rules, cap=2)
assert _call(pointers, _event(tmp_path, command="true"))[0] == 0
assert _call(pointers, _event(tmp_path, command="true"))[0] == 0
code, _out, err = _call(pointers, _event(tmp_path, command="git push origin x"))
assert code == 2 and "cap.max_tool_calls" in err
assert _call(pointers, _event(tmp_path, command="true", name="postToolUse"))[0] == 0
assert _call(pointers, _event(tmp_path, name="stop"))[0] == 0
records = [json.loads(line) for line in log.read_text().splitlines()]
assert records[2]["decision"] == "deny"
assert records[2]["rule_id"] == "cap.max_tool_calls"


@pytest.mark.parametrize("cap", [_OMIT_CAP, 0, -1, True, 1.5, "2", None])
def test_cap_missing_or_invalid_policy_denies(tmp_path: Path, cap: Any) -> None:
pointers, _policy, log = _setup(tmp_path, cap=cap)
code, _out, err = _call(pointers, _event(tmp_path, command="true"))
assert code == 2 and "policy.invalid" in err
code, _out, err = _call(pointers, _event(tmp_path, command="true"))
assert code == 2 and "policy.invalid" in err
records = [json.loads(line) for line in log.read_text().splitlines()]
assert [record["rule_id"] for record in records] == [
"policy.invalid",
"policy.invalid",
]


def test_internal_error_past_the_cap_stays_an_error(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
pointers, _policy, log = _setup(tmp_path, cap=1)
assert _call(pointers, _event(tmp_path, command="true"))[0] == 0

def fail(*_args: Any) -> tuple[str, str]:
raise RuntimeError("test failure")

monkeypatch.setattr(hook, "_evaluate", fail)
code, _out, err = _call(pointers, _event(tmp_path, command="true"))
assert code == 2 and "hook.internal" in err
records = [json.loads(line) for line in log.read_text().splitlines()]
assert records[-1]["decision"] == "error"
assert records[-1]["rule_id"] == "hook.internal"
summary = hook.summarize(pointers, tmp_path)
assert summary["errors"] == 1 and summary["denied"] == 0
18 changes: 17 additions & 1 deletion kits/profile-dev-kit/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,10 @@ beddel run -i ... workflows/plan-story.yaml

The plan, implement and verify-and-fix stages use tracked policies at
`policies/plan.json`, `policies/implement.json` and
`policies/verify-and-fix.json`. Each policy denies shell `git push` and `gh pr
`policies/verify-and-fix.json`. Each policy declares a positive
`max_tool_calls` cap. The hook denies the call past the cap with
`cap.max_tool_calls`, counted in `hook_denied`, and overrides any rule
decision. Each policy denies shell `git push` and `gh pr
merge` before allowing the other declared shell, read and write classes. The
policy copy and `hook.jsonl` live in one fresh 0700 run directory under
`~/.local/state/beddel/kiro-hook/runs/`; the policy digest is recorded in the
Expand Down Expand Up @@ -574,3 +577,16 @@ once. Each run ends with a `deliver` record. One Change per run.
beddel run -i ... workflows/deliver-story.yaml
```

## Delivery: summary (Story 8.7)

`read-delivery-summary` is a read-only entry workflow. Given `repo_path` and
`story_key`, it reads the existing Stage outcomes, Reports and Gate outcomes
and prints one row per stage plus totals. It reports wall seconds, agent calls,
denied calls, Review rounds and Gate exit codes. Missing records or fields are
reported as `missing`; Kiro credits and tokens are always `not observable`.
The workflow creates no records and does not change the records tree.

```text
beddel run -i repo_path=<repo> -i story_key=<story> workflows/read-delivery-summary.yaml
```

9 changes: 9 additions & 0 deletions kits/profile-dev-kit/kit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ tools:
target: "beddel_profile_dev.tools.deliver:profile_deliver_approval"
description: "Check and settle the Delivery Approval stage"
category: profile
- name: profile_read_delivery_summary
target: "beddel_profile_dev.tools.summary:profile_read_delivery_summary"
description: "Read a delivery cost summary from existing profile records"
category: profile

workflows:
- name: verify-change
Expand Down Expand Up @@ -202,6 +206,9 @@ workflows:
- name: deliver-story
path: workflows/deliver-story.yaml
description: Chains the Delivery stages or runs one selected stage, waits for Approval and records the complete Delivery outcome.
- name: read-delivery-summary
path: workflows/read-delivery-summary.yaml
description: Reads a delivery cost summary from existing profile records.

targets:
python:
Expand Down Expand Up @@ -273,6 +280,8 @@ targets:
target: "beddel_profile_dev.tools.deliver:profile_deliver_gate"
- name: profile_deliver_approval
target: "beddel_profile_dev.tools.deliver:profile_deliver_approval"
- name: profile_read_delivery_summary
target: "beddel_profile_dev.tools.summary:profile_read_delivery_summary"
typescript:
status: unavailable
unavailable_reason: >
Expand Down
2 changes: 1 addition & 1 deletion kits/profile-dev-kit/policies/implement.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]}
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}],"max_tool_calls":400}
2 changes: 1 addition & 1 deletion kits/profile-dev-kit/policies/plan.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]}
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}],"max_tool_calls":200}
2 changes: 1 addition & 1 deletion kits/profile-dev-kit/policies/verify-and-fix.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}]}
{"schema":"beddel.kiro-hook-policy/1","rules":[{"id":"shell.git-push","effect":"deny","tool_class":"shell","argv_prefix":["git","push"]},{"id":"shell.gh-pr-merge","effect":"deny","tool_class":"shell","argv_prefix":["gh","pr","merge"]},{"id":"shell.any","effect":"allow","tool_class":"shell"},{"id":"read.any","effect":"allow","tool_class":"read"},{"id":"write.any","effect":"allow","tool_class":"write"}],"max_tool_calls":400}
Loading
Loading