工人有名字+回覆也是派工+未經調查不寫診斷(inkstone/ISEP#86/#87/#88)
三張票同一族(誰在派、派給誰、派的內容住哪裡),做在同一條分支: #86 工人名單:agents/ 七位有名字的工人+scripts/roster+hooks/roster-guard.sh 派工用 Task 的 subagent_type 指名,派工單格式一個字都沒改; 指對名字就把那位的檔案原文注入(你是誰/先讀什麼/你的紅線)。 【身份】欄同時吃得下工人名字(原本三個角色照舊)。 #87 未經調查不寫診斷:hooks/diagnosis-evidence-guard.sh + investigate-first-stamp.sh 三個結構訊號(派過人查沒/有沒有走得過去的出處/有沒有份量), 一個關鍵字比對都沒有;轉述有出處不會被誤擋。 #88 回覆也是派工:不另造閘,把攔截點加掛上去。 hooks/lib/dispatch_parse.py 的 tool_channel() 一次列全所有通往 subagent 的路 (SendMessage/雲端 session・trigger/claude -p);擋下來時把那段內容原文印出來。 subagent 往上回報(to: "main")=交件不是派工,刻意不管。 順手修掉一個真的會咬人的 flake:dispatch-format-guard 原本開四支 python 各讀一個欄位, 機器忙的時候某個欄位會靜靜變空字串(實測連跑 10 次有 1 次「豁免了卻還是被擋」)。 四個欄位改成一次讀完。 版本號待總管定(plugin.json 只更新了描述裡的數字,版本沒動)。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016ZBu4Sa1cGntKFRBYNZ6xs
This commit is contained in:
+169
-10
@@ -59,6 +59,37 @@
|
||||
第一行就要認得出是誰寫的。角色是三選一的允許清單:`總管`/`subagent`/`leo`。
|
||||
2026-08-27 實害:總管寫的診斷被當成 subagent 的結論,而其中一則是錯的。
|
||||
|
||||
━━ 通往 subagent 的路不只一條(inkstone/ISEP#88)━━━━━━━━━━━━━━━━
|
||||
|
||||
第一版的閘只掛在 `PreToolUse(Task|Agent)`——那只涵蓋「**新開**一個 subagent」。
|
||||
**回覆一個正在跑的 subagent 走的是別的工具,根本不經過那個攔截點。**
|
||||
實況(2026-08-27 本 session):第一次派工乾乾淨淨只有票號,
|
||||
中途回覆時又把一長串修改要求直接丟過去,**那些話票上一個字都沒有**。
|
||||
那條線被停掉或換人接手,它們就消失——正是壓縮派工單原本要解決的問題。
|
||||
|
||||
⇒ 本檔不再只認一種 payload 形狀。`tool_channel()` 把所有通往 subagent 的路
|
||||
收成一張表(見該函式),一次列全:
|
||||
|
||||
Task / Agent 新開一個 subagent → prompt
|
||||
SendMessage 回覆正在跑的那個 → message
|
||||
*Claude_Code_Remote__create_session 開一個新的雲端 session → prompt
|
||||
*Claude_Code_Remote__send_message 送訊息進某個 session → text / message
|
||||
*Claude_Code_Remote__create_trigger 排程派工(未來會醒) → prompt
|
||||
*Claude_Code_Remote__update_trigger 改排程派工的內容 → prompt
|
||||
*Claude_Code_Remote__fire_trigger 當場點燃排程派工 → text
|
||||
*Claude_Code_Remote__send_later 排一則訊息給自己 → message
|
||||
Bash `claude -p <prompt>` 用 CLI 開一個新 session → 指令裡那段 prompt
|
||||
|
||||
🔴 這張表就是 `ticket-api-bypass-guard.sh` 檔頭記過的那一課的同款:
|
||||
「認動作的方式漏了一條路」是這一族的通病(那支第一版只認大寫裸字 `POST`,
|
||||
於是 `requests.post()` 與 urllib 的隱式 POST 全部漏掉)。
|
||||
⇒ 新增一條路要加在 `tool_channel()`,**不要另造一支平行的閘**。
|
||||
|
||||
兩條路的分界只有一個(其餘判準完全共用,這是刻意的):
|
||||
· `dispatch`(Task/Agent)沒有【工單】 ⇒ **skip**,那是 no-ticket-no-dispatch 的地盤
|
||||
· `reply`(其餘全部)沒有【工單】 ⇒ **違規**,因為那支閘沒有掛在這些路上,
|
||||
不擋就等於這條路整條裸奔
|
||||
|
||||
用法:
|
||||
import dispatch_parse
|
||||
r = dispatch_parse.parse_dispatch(prompt_text)
|
||||
@@ -69,6 +100,7 @@
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
import shlex
|
||||
import sys
|
||||
|
||||
# ── 派工單的字彙表:**只有一個欄位**。這是允許清單,不是黑名單 ──────────────
|
||||
@@ -206,6 +238,22 @@ def dispatch_violations(parsed):
|
||||
return v
|
||||
|
||||
|
||||
def identity_roles():
|
||||
"""合法的身份角色 = 三個固定角色 + **名單上的工人名字**(inkstone/ISEP#86)。
|
||||
|
||||
leo:「票上的紀錄要看得出是哪個工人做的。」`subagent` 這個字回答不了「是誰」——
|
||||
多條線並行時,三個 subagent 的留言長得一模一樣。
|
||||
名單讀不到就退回三個固定角色(fail-open:名單壞掉不該讓人貼不了留言)。
|
||||
"""
|
||||
try:
|
||||
import os
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import roster
|
||||
return tuple(IDENTITY_ROLES) + tuple(roster.names())
|
||||
except Exception:
|
||||
return tuple(IDENTITY_ROLES)
|
||||
|
||||
|
||||
def parse_identity(body):
|
||||
"""交件回覆的【身份】欄。回 (ok, detail)。"""
|
||||
for raw in (body or "").split("\n"):
|
||||
@@ -218,12 +266,105 @@ def parse_identity(body):
|
||||
if not val:
|
||||
return False, "【身份】後面是空的"
|
||||
role = re.split(r"[//]", val)[0].strip()
|
||||
if role not in IDENTITY_ROLES:
|
||||
return False, "角色「%s」不在 %s 之內" % (role or "(空)", "/".join(IDENTITY_ROLES))
|
||||
allowed = identity_roles()
|
||||
if role not in allowed:
|
||||
return False, "角色「%s」不在 %s 之內" % (role or "(空)", "/".join(allowed))
|
||||
return True, val
|
||||
return False, "內文是空的"
|
||||
|
||||
|
||||
# ── 通往 subagent 的路:一張表,一次列全(inkstone/ISEP#88)────────────────
|
||||
# 新增一條路加在這裡。**不要另造一支平行的閘**——票上寫死了:
|
||||
# 「要嘛把攔截點加掛上去,要嘛讓兩條路共用同一個判斷函式。」
|
||||
#
|
||||
# 值是「這個工具的哪幾個欄位裝著要送給 subagent 的話」,依序取第一個有內容的。
|
||||
_DISPATCH_TOOLS = {
|
||||
"Task": ("prompt",),
|
||||
"Agent": ("prompt",),
|
||||
}
|
||||
_REPLY_TOOLS = {
|
||||
"SendMessage": ("message",),
|
||||
"create_session": ("prompt",),
|
||||
"send_message": ("text", "message"),
|
||||
"create_trigger": ("prompt",),
|
||||
"update_trigger": ("prompt",),
|
||||
"fire_trigger": ("text",),
|
||||
"send_later": ("message",),
|
||||
}
|
||||
# `claude -p <prompt>` = 用 CLI 開一個新 session。判準是**指令名**(結構),
|
||||
# 不是指令內容裡有沒有某個詞——所以一段剛好含有「【工單】」字樣的 echo 不會被誤認。
|
||||
_CLAUDE_CLI_RE = re.compile(r"(?:^|[;&|]\s*|\s)claude\s")
|
||||
_CLI_PRINT_FLAGS = ("-p", "--print")
|
||||
# 這些旗標後面跟的是它自己的值,不是 prompt
|
||||
_CLI_VALUE_FLAGS = {"--model", "-m", "--append-system-prompt", "--system-prompt",
|
||||
"--allowedTools", "--permission-mode", "--output-format",
|
||||
"--input-format", "--session-id", "--resume", "--add-dir",
|
||||
"--mcp-config", "--settings", "--agents"}
|
||||
|
||||
|
||||
def _claude_cli_prompt(command):
|
||||
"""從一段 shell 指令裡把 `claude -p <prompt>` 的 prompt 取出來。取不到回 ""。"""
|
||||
if not command or not _CLAUDE_CLI_RE.search(command):
|
||||
return ""
|
||||
try:
|
||||
toks = shlex.split(command)
|
||||
except Exception:
|
||||
return ""
|
||||
for i, t in enumerate(toks):
|
||||
if t.rsplit("/", 1)[-1] != "claude":
|
||||
continue
|
||||
rest = toks[i + 1:]
|
||||
if not any(f in rest for f in _CLI_PRINT_FLAGS):
|
||||
return "" # 沒有 -p ⇒ 不是一次性的 prompt 執行
|
||||
skip = False
|
||||
for j, tok in enumerate(rest):
|
||||
if skip:
|
||||
skip = False
|
||||
continue
|
||||
if tok in _CLI_VALUE_FLAGS:
|
||||
skip = True
|
||||
continue
|
||||
if tok.startswith("-"):
|
||||
continue
|
||||
return tok # 第一個位置參數 = prompt
|
||||
return ""
|
||||
return ""
|
||||
|
||||
|
||||
def tool_channel(payload):
|
||||
"""回 (channel, text)。channel ∈ {"dispatch", "reply", ""}。
|
||||
|
||||
"" = 這個工具呼叫不是在驅動任何 subagent,本閘管不到。
|
||||
"""
|
||||
name = payload.get("tool_name") or ""
|
||||
ti = payload.get("tool_input") or {}
|
||||
if not isinstance(ti, dict):
|
||||
return "", ""
|
||||
short = name.rsplit("__", 1)[-1]
|
||||
|
||||
if name == "Bash" or short == "Bash":
|
||||
return ("reply", _claude_cli_prompt(ti.get("command") or ""))
|
||||
|
||||
# 🔴 方向很重要:本規則管的是「**派工的人**送出去的話」。
|
||||
# subagent 往上回報(`to: "main"`)是**交件**,不是派工——
|
||||
# 擋它等於擋掉交件本身。交件的規矩由 baton-handback-guard/
|
||||
# reply-identity-guard 管,不在這裡。
|
||||
if (name == "SendMessage" or short == "SendMessage") and \
|
||||
str(ti.get("to") or "").strip().lower() == "main":
|
||||
return "", ""
|
||||
|
||||
for table, channel in ((_DISPATCH_TOOLS, "dispatch"), (_REPLY_TOOLS, "reply")):
|
||||
keys = table.get(name) or table.get(short)
|
||||
if not keys:
|
||||
continue
|
||||
for k in keys:
|
||||
v = ti.get(k)
|
||||
if isinstance(v, str) and v.strip():
|
||||
return channel, v
|
||||
return channel, ""
|
||||
return "", ""
|
||||
|
||||
|
||||
# ── CLI ──────────────────────────────────────────────────────────────────
|
||||
def _main():
|
||||
mode = sys.argv[1] if len(sys.argv) > 1 else "dispatch"
|
||||
@@ -237,21 +378,39 @@ def _main():
|
||||
print(json.dumps({"status": "skip", "why": "未知模式 %s" % mode}))
|
||||
return
|
||||
|
||||
prompt = (payload.get("tool_input") or {}).get("prompt") or ""
|
||||
parsed = parse_dispatch(prompt)
|
||||
|
||||
# 連【工單】都沒有 => 那是 no-ticket-no-dispatch.sh 的地盤,本閘閉嘴。
|
||||
# 兩支閘同時對同一件事開口,收工方會拿到兩份互相打架的教學。
|
||||
if not parsed["has_ticket_marker"]:
|
||||
print(json.dumps({"status": "skip", "why": "沒有【工單】,交給 no-ticket-no-dispatch"}))
|
||||
channel, text = tool_channel(payload)
|
||||
if not channel:
|
||||
print(json.dumps({"status": "skip", "why": "不是通往 subagent 的路"}))
|
||||
return
|
||||
if channel == "reply" and not (text or "").strip():
|
||||
print(json.dumps({"status": "skip", "why": "這條路這次沒有帶任何話"}))
|
||||
return
|
||||
|
||||
parsed = parse_dispatch(text)
|
||||
|
||||
if not parsed["has_ticket_marker"]:
|
||||
if channel == "dispatch":
|
||||
# 連【工單】都沒有 => 那是 no-ticket-no-dispatch.sh 的地盤,本閘閉嘴。
|
||||
# 兩支閘同時對同一件事開口,收工方會拿到兩份互相打架的教學。
|
||||
print(json.dumps({"status": "skip",
|
||||
"why": "沒有【工單】,交給 no-ticket-no-dispatch"}))
|
||||
return
|
||||
# reply 這條路上**沒有**那支閘(它只掛 Task|Agent)⇒ 不擋就整條裸奔。
|
||||
violations = [{"code": "回覆沒有票號",
|
||||
"detail": "回覆也是派工:內容寫進票,訊息只給【工單】owner/repo#N"}]
|
||||
extra_text = (text or "").strip()
|
||||
else:
|
||||
violations = [{"code": c, "detail": d} for c, d in dispatch_violations(parsed)]
|
||||
extra_text = "\n".join(r.rstrip() for _, r in parsed["extra"]).strip()
|
||||
|
||||
print(json.dumps({
|
||||
"status": "ok",
|
||||
"violations": [{"code": c, "detail": d} for c, d in dispatch_violations(parsed)],
|
||||
"channel": channel,
|
||||
"violations": violations,
|
||||
"refs": parsed["refs"],
|
||||
"retired_seen": parsed["retired_seen"],
|
||||
"extra_lines": parsed["extra_lines"],
|
||||
"extra_text": extra_text,
|
||||
"session_id": payload.get("session_id") or "",
|
||||
}, ensure_ascii=False))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user