Skip to content

Commit 9f04e51

Browse files
nik13claude
andcommitted
fix(coding-agents): make Kiro IDE hooks observe-only and fail-open (TH-8286)
Remove gating hooks, preserve state across SessionEnd, guard shell commands, and add missing-emitter/resume coverage. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a39bc34 commit 9f04e51

6 files changed

Lines changed: 91 additions & 99 deletions

File tree

‎examples/coding-agents/hook-to-otlp/adapters/kiro_ide.py‎

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
88
SessionStart -> session start
99
UserPromptSubmit -> turn start prompt = content
10-
PreToolUse -> tool start tool_input = content
11-
PostToolUse -> tool end tool_response = content,
12-
ERROR when tool_response.success is false
13-
PreTaskExec -> spec task start spec/task names = content
14-
PostTaskExec -> spec task end ERROR when task_success is false
10+
PostToolUse -> tool span tool_response = content,
11+
ERROR when tool_response.success is false
12+
PostTaskExec -> spec task span ERROR when task_success is false
13+
14+
Permission and pre-task hooks are intentionally not registered: this recipe is observe-only.
1515
PostFileCreate/Save/Delete -> "File ..." event file path = content
1616
Stop -> turn end
1717
SessionEnd -> session end reason kept when it is an enum value
@@ -25,12 +25,11 @@
2525
"""
2626
from __future__ import annotations
2727

28-
import hashlib
2928
import re
3029
from typing import Any, Dict, List
3130

3231
from adapters.base import (
33-
END, ERROR, EVENT, INSTANT, OK, SESSION, START, TOOL, TURN, SpanEvent, compact, first,
32+
END, ERROR, EVENT, INSTANT, OK, SESSION, START, TOOL, TURN, UPDATE, SpanEvent, compact, first,
3433
)
3534

3635
APP = "kiro_ide"
@@ -43,7 +42,7 @@
4342

4443
def respond(event: str, payload: Dict[str, Any]) -> str:
4544
# Kiro adds a successful hook's stdout to the agent context on
46-
# SessionStart, UserPromptSubmit and PreTaskExec: answer with nothing.
45+
# SessionStart, UserPromptSubmit and PostTaskExec: answer with nothing.
4746
return ""
4847

4948

@@ -56,11 +55,6 @@ def _tool_failed(response: Any) -> bool:
5655
return isinstance(response, dict) and response.get("success") is False
5756

5857

59-
def _task_key(payload: Dict[str, Any]) -> str:
60-
raw = f"{payload.get('spec_name')}\x1f{payload.get('task_name')}"
61-
return "task-" + hashlib.sha256(raw.encode("utf-8", "replace")).hexdigest()[:16]
62-
63-
6458
def parse(event: str, payload: Dict[str, Any]) -> List[SpanEvent]:
6559
session = first(payload, "session_id")
6660
if not session:
@@ -71,7 +65,9 @@ def parse(event: str, payload: Dict[str, Any]) -> List[SpanEvent]:
7165
return [SpanEvent(START, SESSION, sid)]
7266

7367
if event == "SessionEnd":
74-
return [SpanEvent(END, SESSION, sid, status=OK,
68+
# Kiro fires this when it unloads/reloads a persisted session. Keep the
69+
# state and turn sequence: SessionEnd is not a terminal chat end.
70+
return [SpanEvent(UPDATE, SESSION, sid,
7571
attributes=compact({"kiro.session.end_reason": _enum(payload.get("reason"))}))]
7672

7773
if event == "UserPromptSubmit":
@@ -81,15 +77,11 @@ def parse(event: str, payload: Dict[str, Any]) -> List[SpanEvent]:
8177
}))]
8278

8379
if event == "Stop":
84-
return [SpanEvent(END, TURN, sid, status=OK, attributes=compact({
85-
"kiro.stop.user_decision": _enum(payload.get("user_decision")),
86-
}))]
80+
return [SpanEvent(END, TURN, sid, status=OK)]
8781

88-
if event in ("PreToolUse", "PostToolUse"):
82+
if event == "PostToolUse":
8983
tool = str(first(payload, "tool_name") or "tool")
9084
attrs = {"kiro.tool.name": tool}
91-
if event == "PreToolUse":
92-
return [SpanEvent(START, TOOL, sid, name=tool, input=payload.get("tool_input"), attributes=attrs)]
9385
response = payload.get("tool_response")
9486
failed = _tool_failed(response)
9587
return [SpanEvent(
@@ -102,14 +94,12 @@ def parse(event: str, payload: Dict[str, Any]) -> List[SpanEvent]:
10294
attributes=attrs,
10395
)]
10496

105-
if event in ("PreTaskExec", "PostTaskExec"):
97+
if event == "PostTaskExec":
98+
# Observe the completed task only. PreTaskExec is a gating hook.
10699
names = {"spec": payload.get("spec_name"), "task": payload.get("task_name")}
107-
if event == "PreTaskExec":
108-
return [SpanEvent(START, EVENT, sid, key=_task_key(payload), name="Spec task", input=names,
109-
attributes={"kiro.event": "spec_task"})]
110100
success = payload.get("task_success")
111101
failed = success is False
112-
return [SpanEvent(END, EVENT, sid, key=_task_key(payload), name="Spec task", input=names,
102+
return [SpanEvent(INSTANT, EVENT, sid, name="Spec task", input=names,
113103
status=ERROR if failed else OK,
114104
status_message="spec task failed" if failed else None,
115105
attributes=compact({"kiro.event": "spec_task",

‎examples/coding-agents/hook-to-otlp/tests/test_kiro_ide_adapter.py‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from __future__ import annotations
77

88
import json
9+
import os
10+
import pathlib
11+
import subprocess
912
import tempfile
1013
import unittest
1114

@@ -41,7 +44,7 @@ def test_one_trace_session_turn_tool_tree(self):
4144
root_id = emit.span_id("kiro_ide", SID, SESSION, "")
4245
root = latest(spans, root_id)
4346
self.assertEqual(root["name"], "Kiro IDE session")
44-
self.assertEqual(attrs(root)["kiro.session.end_reason"], "other")
47+
self.assertNotIn("kiro.session.end_reason", attrs(root))
4548
turns = [s for s in spans if s["name"] == "Kiro IDE turn"]
4649
self.assertEqual(len({t["spanId"] for t in turns}), 1)
4750
turn = turns[-1]
@@ -55,15 +58,16 @@ def test_tools_pair_pre_and_post_and_mark_failures(self):
5558
read, = by_name(spans, "read_file")
5659
bash, = by_name(spans, "execute_bash")
5760
self.assertEqual(attrs(read)["gen_ai.tool.name"], "read_file")
58-
self.assertEqual(int(read["endTimeUnixNano"]) - int(read["startTimeUnixNano"]), 1_000_000_000)
61+
# Observe-only PostToolUse has no vendor duration; start and end are the hook time.
62+
self.assertEqual(int(read["endTimeUnixNano"]) - int(read["startTimeUnixNano"]), 0)
5963
self.assertEqual(read["status"]["code"], 1)
6064
self.assertEqual(bash["status"], {"code": 2, "message": "tool reported failure"})
6165
self.assertNotIn("coding_agent.incomplete", attrs(read))
6266

6367
def test_spec_task_pairs_on_names(self):
6468
task, = by_name(self.spans(), "Spec task")
6569
self.assertEqual(attrs(task)["kiro.task.success"], True)
66-
self.assertEqual(int(task["endTimeUnixNano"]) - int(task["startTimeUnixNano"]), 1_000_000_000)
70+
self.assertEqual(int(task["endTimeUnixNano"]) - int(task["startTimeUnixNano"]), 0)
6771

6872
def test_default_export_has_no_content_or_paths(self):
6973
encoded = json.dumps(self.spans())
@@ -92,8 +96,37 @@ def test_free_text_reason_is_not_exported(self):
9296
ev, = kiro_ide.parse("SessionEnd", {"session_id": "s", "reason": "the user closed it because..."})
9397
self.assertNotIn("kiro.session.end_reason", ev.attributes)
9498

99+
def test_pre_gating_hooks_are_not_mapped(self):
100+
payload = {"session_id": "s", "tool_name": "read_file", "tool_input": {"path": "/tmp/a"}}
101+
self.assertEqual(kiro_ide.parse("PreToolUse", payload), [])
102+
self.assertEqual(kiro_ide.parse("PreTaskExec", payload), [])
103+
104+
def test_session_end_keeps_state_for_resume(self):
105+
first = self.spans()
106+
later = list(self.records)
107+
# A runtime unload/reload uses the same session id and starts a new turn.
108+
later.extend([
109+
{"event": "SessionEnd", "payload": {"session_id": SID, "reason": "other"}},
110+
{"event": "UserPromptSubmit", "payload": {"session_id": SID, "prompt": "second"}},
111+
{"event": "Stop", "payload": {"session_id": SID}},
112+
])
113+
resumed = replay(kiro_ide, config("kiro_ide", self.tmp.name), later)
114+
turns = [s for s in resumed if s["name"] == "Kiro IDE turn"]
115+
self.assertGreaterEqual(len({s["spanId"] for s in turns}), 2)
116+
117+
def test_shipped_commands_fail_open_when_emitter_is_missing(self):
118+
root = pathlib.Path(__file__).resolve().parents[2] / "kiro-ide"
119+
for path in (root / "fi-hook-to-otlp.json", root / "fi-hook-to-otlp.user.json"):
120+
doc = json.loads(path.read_text())
121+
for hook in doc["hooks"]:
122+
command = hook["action"]["command"]
123+
result = subprocess.run(["sh", "-c", command], input="{}", text=True,
124+
capture_output=True, cwd=tempfile.mkdtemp())
125+
self.assertEqual(result.returncode, 0, (path, command, result.stderr))
126+
self.assertEqual(result.stdout, "", (path, command, result.stdout))
127+
95128
def test_stdout_adds_nothing_to_agent_context(self):
96-
for event in ("SessionStart", "UserPromptSubmit", "PreTaskExec", "Stop"):
129+
for event in ("SessionStart", "UserPromptSubmit", "PostTaskExec", "Stop"):
97130
self.assertEqual(kiro_ide.respond(event, {}), "")
98131

99132

‎examples/coding-agents/kiro-ide/fi-hook-to-otlp.json‎

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"trigger": "SessionStart",
88
"action": {
99
"type": "command",
10-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
10+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
1111
},
1212
"timeout": 10
1313
},
@@ -17,37 +17,18 @@
1717
"trigger": "UserPromptSubmit",
1818
"action": {
1919
"type": "command",
20-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
21-
},
22-
"timeout": 10
23-
},
24-
{
25-
"name": "Future AGI tracing: tool start",
26-
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
27-
"trigger": "PreToolUse",
28-
"action": {
29-
"type": "command",
30-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
20+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
3121
},
3222
"timeout": 10
3323
},
3424
{
3525
"name": "Future AGI tracing: tool end",
3626
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
3727
"trigger": "PostToolUse",
28+
"matcher": ".*",
3829
"action": {
3930
"type": "command",
40-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
41-
},
42-
"timeout": 10
43-
},
44-
{
45-
"name": "Future AGI tracing: spec task start",
46-
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
47-
"trigger": "PreTaskExec",
48-
"action": {
49-
"type": "command",
50-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
31+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
5132
},
5233
"timeout": 10
5334
},
@@ -57,7 +38,7 @@
5738
"trigger": "PostTaskExec",
5839
"action": {
5940
"type": "command",
60-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
41+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
6142
},
6243
"timeout": 10
6344
},
@@ -67,7 +48,7 @@
6748
"trigger": "PostFileCreate",
6849
"action": {
6950
"type": "command",
70-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
51+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
7152
},
7253
"timeout": 10
7354
},
@@ -77,7 +58,7 @@
7758
"trigger": "PostFileSave",
7859
"action": {
7960
"type": "command",
80-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
61+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
8162
},
8263
"timeout": 10
8364
},
@@ -87,7 +68,7 @@
8768
"trigger": "PostFileDelete",
8869
"action": {
8970
"type": "command",
90-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
71+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
9172
},
9273
"timeout": 10
9374
},
@@ -97,7 +78,7 @@
9778
"trigger": "Stop",
9879
"action": {
9980
"type": "command",
100-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
81+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
10182
},
10283
"timeout": 10
10384
},
@@ -107,7 +88,7 @@
10788
"trigger": "SessionEnd",
10889
"action": {
10990
"type": "command",
110-
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide"
91+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true || true"
11192
},
11293
"timeout": 10
11394
}

‎examples/coding-agents/kiro-ide/fi-hook-to-otlp.user.json‎

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"trigger": "SessionStart",
88
"action": {
99
"type": "command",
10-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
10+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
1111
},
1212
"timeout": 10
1313
},
@@ -17,37 +17,18 @@
1717
"trigger": "UserPromptSubmit",
1818
"action": {
1919
"type": "command",
20-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
21-
},
22-
"timeout": 10
23-
},
24-
{
25-
"name": "Future AGI tracing: tool start",
26-
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
27-
"trigger": "PreToolUse",
28-
"action": {
29-
"type": "command",
30-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
20+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
3121
},
3222
"timeout": 10
3323
},
3424
{
3525
"name": "Future AGI tracing: tool end",
3626
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
3727
"trigger": "PostToolUse",
28+
"matcher": ".*",
3829
"action": {
3930
"type": "command",
40-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
41-
},
42-
"timeout": 10
43-
},
44-
{
45-
"name": "Future AGI tracing: spec task start",
46-
"description": "Send this event to Future AGI (metadata only unless FI_HOOK_CAPTURE_CONTENT=1)",
47-
"trigger": "PreTaskExec",
48-
"action": {
49-
"type": "command",
50-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
31+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
5132
},
5233
"timeout": 10
5334
},
@@ -57,7 +38,7 @@
5738
"trigger": "PostTaskExec",
5839
"action": {
5940
"type": "command",
60-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
41+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
6142
},
6243
"timeout": 10
6344
},
@@ -67,7 +48,7 @@
6748
"trigger": "PostFileCreate",
6849
"action": {
6950
"type": "command",
70-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
51+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
7152
},
7253
"timeout": 10
7354
},
@@ -77,7 +58,7 @@
7758
"trigger": "PostFileSave",
7859
"action": {
7960
"type": "command",
80-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
61+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
8162
},
8263
"timeout": 10
8364
},
@@ -87,7 +68,7 @@
8768
"trigger": "PostFileDelete",
8869
"action": {
8970
"type": "command",
90-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
71+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
9172
},
9273
"timeout": 10
9374
},
@@ -97,7 +78,7 @@
9778
"trigger": "Stop",
9879
"action": {
9980
"type": "command",
100-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
81+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
10182
},
10283
"timeout": 10
10384
},
@@ -107,7 +88,7 @@
10788
"trigger": "SessionEnd",
10889
"action": {
10990
"type": "command",
110-
"command": "python3 \"$HOME/.kiro/fi-hook-to-otlp/emit.py\" kiro_ide"
91+
"command": "python3 .kiro/fi-hook-to-otlp/emit.py kiro_ide || true"
11192
},
11293
"timeout": 10
11394
}

‎examples/coding-agents/kiro-ide/install.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ else
2222
exit 2
2323
fi
2424

25+
HOOK_FILE="$KIRO_DIR/hooks/fi-hook-to-otlp.json"
26+
if [[ -e "$HOOK_FILE" ]] && grep -q 'kiro_cli' "$HOOK_FILE"; then
27+
echo "refusing to install Kiro IDE hooks: $HOOK_FILE is configured for Kiro CLI" >&2
28+
echo "Use one Kiro adapter per hook scope, or add the IDE hooks manually under a separate project scope." >&2
29+
exit 1
30+
fi
2531
DEST="$KIRO_DIR/fi-hook-to-otlp"
2632
mkdir -p "$DEST/adapters" "$KIRO_DIR/hooks"
2733
cp "$EMITTER/emit.py" "$DEST/emit.py"

0 commit comments

Comments
 (0)