From 5e739272c1d2bd6e4fbf2f90a29016651b11b229 Mon Sep 17 00:00:00 2001 From: Haewon Kam Date: Mon, 2 Nov 2026 15:46:44 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20watchdog=20=EB=A9=94=EC=9D=BC=20?= =?UTF-8?q?=EA=B2=8C=EC=9D=B4=ED=8A=B8=20=ED=9B=85=EC=9D=B4=20=EC=B5=9C?= =?UTF-8?q?=EC=8B=A0=20Claude=20Code=20=EA=B8=B0=EB=A1=9D=20=ED=98=95?= =?UTF-8?q?=EC=8B=9D=EC=9D=98=20=EB=B0=B1=EA=B7=B8=EB=9D=BC=EC=9A=B4?= =?UTF-8?q?=EB=93=9C=20=EA=B2=B0=EA=B3=BC=EB=A5=BC=20=EC=9D=BD=EA=B2=8C=20?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - task-notification 이 attachment.queued_command 로 기록되는 형식을 읽어 view-mou-watchdog PASS 를 인식한다 - 앞선 도구 호출을 다시 담는 api-request-blob 행은 건너뛴다 - "PASS 없이는 초안 불가, 검토 한 번에 초안 한 번" 규칙은 그대로다 Co-Authored-By: Claude Opus 5.5 --- .claude/hooks/watchdog_gate.py | 113 +++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 .claude/hooks/watchdog_gate.py diff --git a/.claude/hooks/watchdog_gate.py b/.claude/hooks/watchdog_gate.py new file mode 100644 index 0000000..0fc3782 --- /dev/null +++ b/.claude/hooks/watchdog_gate.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python3 +"""Outlook 초안 생성·수정·발송 전에 view-mou-watchdog 검토 통과를 요구하는 PreToolUse 훅. + +통과 조건: 가장 최근 view-mou-watchdog 결과(동기 결과 또는 백그라운드 task-notification)가 'WATCHDOG: PASS' 이고, +그 PASS 이후에 아직 다른 Outlook 초안 도구 호출이 없다 (검토 한 번에 초안 한 번). +""" +import json +import re +import sys + +AGENT = "view-mou-watchdog" +OUTLOOK_PREFIX = "mcp__claude_ai_Microsoft_365__outlook_" +GATED = ("create_draft", "create_reply_draft", "create_reply_all_draft", "update_draft", "send_draft", "send_mail", "forward_mail") + + +def deny(reason): + print(json.dumps({ + "hookSpecificOutput": { + "hookEventName": "PreToolUse", + "permissionDecision": "deny", + "permissionDecisionReason": reason, + } + }, ensure_ascii=False)) + sys.exit(0) + + +def is_gated(name): + return name.startswith(OUTLOOK_PREFIX) and name[len(OUTLOOK_PREFIX):] in GATED + + +def result_text(item): + c = item.get("content") + if isinstance(c, str): + return c + if isinstance(c, list): + return "\n".join(x.get("text", "") for x in c if isinstance(x, dict)) + return "" + + +def verdict(text): + return "WATCHDOG: PASS" in text and "WATCHDOG: FIX" not in text + + +def main(): + payload = json.load(sys.stdin) + if not is_gated(payload.get("tool_name", "")): + sys.exit(0) + + rows = [] + with open(payload["transcript_path"], encoding="utf-8") as f: + for line in f: + line = line.strip() + if line: + try: + rows.append(json.loads(line)) + except json.JSONDecodeError: + continue + + # 검토 1회(PASS) = 초안 도구 1회. 백그라운드 검토 결과는 task-notification 으로 도착한다 + watchdog_ids = set() + passed = False + for r in rows: + if r.get("isSidechain"): + continue + # 요청 사본 행은 앞선 도구 호출을 다시 담고 있어 순서를 흐트러뜨린다. 건너뛴다 + if r.get("type") == "api-request-blob": + continue + # 최신 Claude Code 는 백그라운드 결과(task-notification)를 attachment.queued_command 로 기록한다 + att = r.get("attachment") or {} + if att.get("type") == "queued_command": + prompt = att.get("prompt") or "" + if "" in prompt: + m = re.search(r"([^<]+)", prompt) + if m and m.group(1) in watchdog_ids: + passed = verdict(prompt) + continue + c = r.get("message", {}).get("content") + if isinstance(c, str): + if "" in c: + m = re.search(r"([^<]+)", c) + if m and m.group(1) in watchdog_ids: + passed = verdict(c) + continue + if not isinstance(c, list): + continue + for x in c: + if not isinstance(x, dict): + continue + if x.get("type") == "tool_use": + inp = x.get("input") or {} + # 에이전트 정의가 아직 로드되지 않은 세션에서는 general-purpose 에 정의 파일을 넘기고 description 에 이름을 단다 + if x.get("name") in ("Agent", "Task") and ( + inp.get("subagent_type") == AGENT or AGENT in (inp.get("description") or "") + ): + watchdog_ids.add(x.get("id")) + elif is_gated(x.get("name", "")): + passed = False + elif x.get("type") == "tool_result" and x.get("tool_use_id") in watchdog_ids: + text = result_text(x) + if "WATCHDOG:" in text: + passed = verdict(text) + + if not passed: + deny( + "view-mou-watchdog 검토가 통과되지 않았습니다. 메일 초안을 만들거나 고치기 전에 " + "Agent(subagent_type='view-mou-watchdog')에 초안 전문과 근거 위치를 넘겨 검토받고, " + "'WATCHDOG: PASS'가 나온 뒤 다시 시도하세요. FIX면 정정한 초안으로 다시 검토받으세요." + ) + sys.exit(0) + + +if __name__ == "__main__": + main()