Skip to content

Commit 1d2f907

Browse files
committed
[None][fix] agent-flow hooks: a compaction summary does not start a turn
After Claude Code compacts a session, the continuation summary is a user record (isCompactSummary) whose paths are elided as /.../. The stop hook took it as the turn's prompt, so a planner that had written its decision file was sent back once to write the elided path. Skip compaction summaries and meta records (a stop hook's own feedback) when finding the turn-starting user message. Signed-off-by: Wanli Jiang <35160485+Wanli-Jiang@users.noreply.github.com>
1 parent 70a9861 commit 1d2f907

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎agent-flow/agent_flow/hooks.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,12 @@ def _is_human_user_entry(entry: dict) -> bool:
8585
8686
Tool results are also delivered as ``type=="user"`` records whose content
8787
is a list of ``tool_result`` blocks; those are not real turn boundaries.
88+
Neither are the user records the CLI writes itself inside a turn: the
89+
summary that continues a compacted session (``isCompactSummary``), which
90+
names paths only in elided ``/…/`` form, and meta records such as a Stop
91+
hook's feedback (``isMeta``).
8892
"""
89-
if entry.get("type") != "user":
93+
if entry.get("type") != "user" or entry.get("isCompactSummary") or entry.get("isMeta"):
9094
return False
9195
message = entry.get("message") or {}
9296
content = message.get("content")

‎agent-flow/tests/workflows/trtllm_modeling_v2/test_roles.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,39 @@ async def test_a_turn_that_ends_before_writing_its_decision_is_sent_back_to_writ
489489
assert await hook.hooks[0](stop, None, {}) == {}
490490

491491

492+
async def test_a_compacted_turn_is_held_to_its_prompts_decision_file(tmp_path):
493+
"""After a compaction the CLI's summary names the path elided; the hook keeps the prompt's path."""
494+
target = mark_decision_dispatch(tmp_path, "planner", 1)
495+
prompt = f"## Plan\n\n{decision_file_instruction(target)}"
496+
elided = Path("/…") / target.relative_to(tmp_path.parent)
497+
summary = (
498+
"This session is being continued from a previous conversation that ran out of context."
499+
)
500+
entries = [
501+
{"type": "user", "message": {"role": "user", "content": prompt}},
502+
{"type": "assistant", "message": {"role": "assistant", "content": [
503+
{"type": "tool_use", "id": "t1", "name": "Write", "input": {"file_path": "plan.md"}},
504+
]}},
505+
{"type": "user", "isCompactSummary": True, "isVisibleInTranscriptOnly": True,
506+
"message": {"role": "user", "content": f"{summary}\n{decision_file_instruction(elided)}"}},
507+
{"type": "assistant", "message": {"role": "assistant", "content": [
508+
{"type": "text", "text": "Continuing the plan."},
509+
]}},
510+
] # fmt: skip
511+
transcript = tmp_path / "turn.jsonl"
512+
transcript.write_text("\n".join(json.dumps(e) for e in entries) + "\n", encoding="utf-8")
513+
hook = require_tool_call_stop_hook(["Write"], required_file=decision_file_named)["Stop"][0]
514+
stop = {
515+
"hook_event_name": "Stop",
516+
"transcript_path": str(transcript),
517+
"stop_hook_active": False,
518+
}
519+
held = await hook.hooks[0](stop, None, {})
520+
assert held["decision"] == "block" and f"`{target}`" in held["reason"]
521+
target.write_text("summary: s\ndecision: GRAPH_UPDATED\n", encoding="utf-8")
522+
assert await hook.hooks[0](stop, None, {}) == {}
523+
524+
492525
def test_missing_context_raises() -> None:
493526
assert _render._ENVIRONMENT.undefined is StrictUndefined
494527
with pytest.raises(UndefinedError):

0 commit comments

Comments
 (0)