|
|
@@ -0,0 +1,708 @@
|
|
|
+# 容器取证问答:agent-node-lj-test
|
|
|
+
|
|
|
+- 时间:2026-11-17T09:10:35.209Z
|
|
|
+- 房间:`!qLkZ1OWJriVOOYXZToPhpgVxwLZZnEAI9RcgCR-fiQI`(临时房间,可保留查阅)
|
|
|
+- 消息统计:正文 26 段 / 工具过程 38 段 / 文件 0 个
|
|
|
+- 结束原因:收到 ✅ reaction
|
|
|
+
|
|
|
+## 我发出的问题
|
|
|
+
|
|
|
+```text
|
|
|
+# 第二轮取证(只读):补齐两处未取证 + 确认改法边界
|
|
|
+
|
|
|
+第一轮结论我已认可方向。现在只补你标「未取证」的两处,并回答改法边界的三个问题。**仍然只读,不要改任何文件、不要重启。**
|
|
|
+
|
|
|
+## Q6. hook 是通过哪个 hook 点观测产物的?
|
|
|
+
|
|
|
+```bash
|
|
|
+grep -n "def handle_\|hookmanager\|register\|pre_tool\|post_tool\|on_tool" /opt/data/scripts/fmode-deliverable-hook.py | head -40
|
|
|
+```
|
|
|
+
|
|
|
+要回答:
|
|
|
+1. 挂在哪个 hook 事件上(pre_tool_call / post_tool_call / 其它)?
|
|
|
+2. **产物路径是从哪里来的**:工具参数(如 `write_file` 的 `path`)里解析,还是扫描命令执行前后的文件系统差异?
|
|
|
+3. 如果是「解析工具参数」——那么 `terminal` 里跑 `python gen.py` 生成的文件、`execute_code` 里写出的文件,**天然不会被观测到**,对吗?请贴代码证明。
|
|
|
+
|
|
|
+## Q7. `local_files` 是怎么从正文识别出 a.xlsx 的?
|
|
|
+
|
|
|
+第 4 轮队列只有 1 条 `a.xlsx`,来源是 `local_files`。请说清:
|
|
|
+
|
|
|
+```bash
|
|
|
+grep -rn "local_files" /opt/hermes/gateway/platforms/base.py | head -20
|
|
|
+grep -rn "local_files\s*=" -A15 /opt/hermes/gateway/platforms/base.py | head -40
|
|
|
+```
|
|
|
+
|
|
|
+1. 它的识别规则是什么(正则?路径存在性?)
|
|
|
+2. 为什么 4 轮里只有第 4 轮的 `a.xlsx` 被识别到(第 1 轮 1.csv 是走 MEDIA 行还是 local_files?)
|
|
|
+
|
|
|
+## Q8. 改法边界(请评估可行性,先别动手)
|
|
|
+
|
|
|
+我想按下面三条改,请逐条回答「可行 / 不可行 / 需要你换个做法」,并说明原因:
|
|
|
+
|
|
|
+**改法 A:hook 增加「轮次结束时扫描目录」兜底**
|
|
|
+在一轮结束前(`handle_post_tool_call` 或轮次收尾钩子),用 `os.stat` 记录**本轮开始时的目录快照**,
|
|
|
+轮次结束时对比,找出新增/修改的、扩展名在白名单内的文件,补进 `pushed[]` 并上传。
|
|
|
+
|
|
|
+要回答:
|
|
|
+- 有哪些可靠的位置能拿到「本轮开始时刻」与「本轮工作目录」?
|
|
|
+- 会不会把用户上传的输入文件、临时文件误收?现有 `EXT_ALLOW` / `NAME_DENY_GLOB` 够不够挡?
|
|
|
+
|
|
|
+**改法 B:交付段落(`已上云` 链接块)改为「只列本轮新增」**
|
|
|
+现在靠 30 分钟时间窗,轮次一多就会把上一轮的链接贴回来。
|
|
|
+改成:每轮开始时清空/快照一个 `round_id`,注入时只列本轮 `pushed[]`。
|
|
|
+
|
|
|
+要回答:
|
|
|
+- `_on_transform_llm_output` 里能拿到「本轮」的准确边界吗?拿不到的话,能不能在每轮开始时由 hook 写一个 `round_start_ts`?
|
|
|
+- 改完之后,同一轮里多次 transform(多次工具调用后才出正文)会不会重复/漏?
|
|
|
+
|
|
|
+**改法 C:失败可见 —— 不要把 `no-preuploaded-url` 变成一句英文提示**
|
|
|
+现在 Hermes 原生会发 `⚠️ Couldn't deliver the file attachment (x)`,App 侧会把这句话**静默过滤**掉。
|
|
|
+所以即使失败,用户也看不到。能不能让 outbound 插件在上游就把失败吃掉,**改由你们自己发一条中文说明**
|
|
|
+(例如「⚠️ 文件 a.xlsx 未能在对话内直接发送,请用上方链接下载」),并带 `fmode.kind` 标记?
|
|
|
+
|
|
|
+要回答:
|
|
|
+- 这个拦截点在哪最合适(`_upload_and_send` 里?还是 `base.py` 的 `_notify_media_delivery_failure` 前)?
|
|
|
+- 会不会影响 Hermes 原生其它平台的行为(Discord 等)——我们要的只是本容器 Matrix 侧。
|
|
|
+
|
|
|
+## 回答格式
|
|
|
+
|
|
|
+每问:`结论一句话` + `原始代码(贴全文)` + `文件:行号`。不要省略代码。
|
|
|
+```
|
|
|
+
|
|
|
+## 容器回答(原文,按时间顺序,未删改)
|
|
|
+
|
|
|
+### 1【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+我先读取你要求的文件与代码,再逐问取证。
|
|
|
+```
|
|
|
+
|
|
|
+### 2【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+execute_code · from hermes_tools import terminal, read_file, search_files #…
|
|
|
+```
|
|
|
+
|
|
|
+### 3【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看 transform 端(第 900~1078 行)与 main 的事件分发。
|
|
|
+```
|
|
|
+
|
|
|
+### 4【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在查 Hermes 侧:shell hook 的注册配置、payload 里给了哪些字段、以及 local_files 的识别规则。
|
|
|
+```
|
|
|
+
|
|
|
+### 5【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在查 local_files 在 Hermes 侧的实现。
|
|
|
+```
|
|
|
+
|
|
|
+### 6【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看 filter_local_delivery_paths 与 delivery 链路的 4200-4260 段,以及 MEDIA_DELIVERY_EXTS。
|
|
|
+```
|
|
|
+
|
|
|
+### 7【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看 4150-4260 的发送主链与失败通知点。
|
|
|
+```
|
|
|
+
|
|
|
+### 8【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在找 `_upload_and_send` 与 `_notify_media_delivery_failure`。
|
|
|
+```
|
|
|
+
|
|
|
+### 9【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看 Matrix adapter 的 `_upload_and_send` 和相关失败提示文案。
|
|
|
+```
|
|
|
+
|
|
|
+### 10【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看现有的 outbound 插件(fmode_outbound_media.py),因为 Q8C 与它相关。
|
|
|
+```
|
|
|
+
|
|
|
+### 11【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看这个 outbound 被谁调用:是插件 patch,还是 adapter 里的补丁。
|
|
|
+```
|
|
|
+
|
|
|
+### 12【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+execute_code · from hermes_tools import read_file print(read_file(path="/op…
|
|
|
+```
|
|
|
+
|
|
|
+### 13【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在核查 hook payload 里有没有「本轮」边界字段,以及 shell_hooks 的调用时机。
|
|
|
+```
|
|
|
+
|
|
|
+### 14【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+Q6/Q8B 的关键点:`turn_id` 会进入 payload 的 `extra`。核查 pre_llm_call / post_llm_call 是否也有 turn_id,以及 hook 是否只有这两个事件可注册。
|
|
|
+```
|
|
|
+
|
|
|
+### 15【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在核实 ext 白名单常量的取值(Q8A 依赖它),以及 `paths_from_tool` 全文(Q6 的核心证据)。
|
|
|
+```
|
|
|
+
|
|
|
+### 16【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在看 local_files 的正文识别核心实现(第 3129 行起的 `extract_local_files` 全文)。
|
|
|
+```
|
|
|
+
|
|
|
+### 17【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在核实 delivery 链路上、extract_local_files 之前的守卫条件(4200–4250 段),以及 MEDIA 提取与 local_files 的先后关系。
|
|
|
+```
|
|
|
+
|
|
|
+### 18【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+现在确认 `_notify_media_delivery_failure` 之前是否有平台钩子可拦,以及 Matrix adapter 是否覆写了 `send_document` / `emit_warning`。
|
|
|
+```
|
|
|
+
|
|
|
+### 19【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+最后一处:Q7 需确认「第 4 轮只有 a.xlsx」是从哪条路径来的,因此要查 hook 日志与 state 文件的实际记录。
|
|
|
+```
|
|
|
+
|
|
|
+### 20【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+Q7 需要第 4 轮的 a.xlsx 证据,我要在日志里找到它。
|
|
|
+```
|
|
|
+
|
|
|
+### 21【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+第 4 轮只出现 1 个文件,是走了 MEDIA 的路径,还是走了 bare-path 的路径?我要看第 4 轮的会话状态与响应正文,确认究竟哪条路径。
|
|
|
+```
|
|
|
+
|
|
|
+### 22【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+关键发现:16:11:57 那轮只记录了 “Delivering 1 non-image MEDIA attachment(s)”,没有 extract_local_files 日志。我再查第 4 轮 assistant 正文原文,核实是 MEDIA 还是裸路径。
|
|
|
+```
|
|
|
+
|
|
|
+### 23【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+事实已推翻上一轮对 Q7 的判断:第 4 轮的 a.xlsx 走的是 `MEDIA:` 标签,并未经过 local_files。我要再确认第一轮 1.csv 的路径,然后写报告。
|
|
|
+```
|
|
|
+
|
|
|
+### 24【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+execute_code · import sqlite3 con = sqlite3.connect("file:/opt/data/state.d…
|
|
|
+```
|
|
|
+
|
|
|
+### 25【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+两处「未取证」查清后,我发现第一轮的一个前提需要修正。下面逐问给出结论、原始代码和行号。
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## Q6. hook 挂在哪个 hook 点,产物路径从哪里来
|
|
|
+
|
|
|
+**结论:挂在 `post_tool_call`(只排队)和 `transform_llm_output`(真正上传)两个事件上;产物路径来自「解析工具参数」(`tool_input` 的路径键 + 正则扫描命令/代码里出现的绝对路径),没有做文件系统前后差异扫描。因此,`execute_code`/`terminal` 里的文件只能被正则「猜」到,猜不到就永远不排队;这一点已由你自己的日志验证。**
|
|
|
+
|
|
|
+1. **事件**:`hooks.json` 审批表与 `config.yaml` 都只有这两个事件。
|
|
|
+
|
|
|
+```
|
|
|
+{
|
|
|
+ "approvals": [
|
|
|
+ ...,
|
|
|
+ { "command": "python3 /opt/data/scripts/fmode-deliverable-hook.py", "event": "post_tool_call" },
|
|
|
+ { "command": "python3 /opt/data/scripts/fmode-deliverable-hook.py", "event": "transform_llm_output" }
|
|
|
+ ]
|
|
|
+}
|
|
|
+```
|
|
|
+`/opt/data/shell-hooks-allowlist.json:2-20`
|
|
|
+
|
|
|
+```yaml
|
|
|
+hooks:
|
|
|
+ post_tool_call:
|
|
|
+ - matcher: write_file|patch|execute_code|terminal
|
|
|
+ command: python3 /opt/data/scripts/fmode-deliverable-hook.py
|
|
|
+ timeout: 20
|
|
|
+ transform_llm_output:
|
|
|
+ - command: python3 /opt/data/scripts/fmode-deliverable-hook.py
|
|
|
+ timeout: 180
|
|
|
+```
|
|
|
+`/opt/data/config.yaml:74-80`
|
|
|
+
|
|
|
+分发点:
|
|
|
+```python
|
|
|
+ event = payload.get("hook_event_name") or payload.get("event") or ""
|
|
|
+ try:
|
|
|
+ if event == "post_tool_call":
|
|
|
+ return handle_post_tool_call(payload)
|
|
|
+ if event == "transform_llm_output":
|
|
|
+ return handle_transform_llm_output(payload)
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:1055-1060`
|
|
|
+
|
|
|
+2. **路径来源 = 解析工具参数**,见 `paths_from_tool` 全文:
|
|
|
+
|
|
|
+```python
|
|
|
+WRITE_TOOLS = {"write_file", "patch", "execute_code", "terminal", "create_file", "edit_file"}
|
|
|
+
|
|
|
+PATH_ARG_KEYS = ("path", "file_path", "filepath", "filename", "file", "target", "dest", "output")
|
|
|
+
|
|
|
+def paths_from_tool(tool: str, ti: dict) -> list[str]:
|
|
|
+ out: list[str] = []
|
|
|
+ if not isinstance(ti, dict):
|
|
|
+ return out
|
|
|
+ for k in PATH_ARG_KEYS:
|
|
|
+ v = ti.get(k)
|
|
|
+ if isinstance(v, str) and v.strip():
|
|
|
+ out.append(v.strip())
|
|
|
+ elif isinstance(v, list):
|
|
|
+ out.extend([x for x in v if isinstance(x, str)])
|
|
|
+ # 显式标记键
|
|
|
+ for k in MARK_KEYS:
|
|
|
+ v = ti.get(k)
|
|
|
+ if isinstance(v, bool):
|
|
|
+ continue
|
|
|
+ if isinstance(v, str):
|
|
|
+ out.append(v)
|
|
|
+ elif isinstance(v, list):
|
|
|
+ out.extend([x for x in v if isinstance(x, str)])
|
|
|
+ # execute_code / terminal:从代码/命令里捞绝对路径(只认带白名单扩展名的)
|
|
|
+ blob = ""
|
|
|
+ for k in ("code", "command", "script"):
|
|
|
+ v = ti.get(k)
|
|
|
+ if isinstance(v, str):
|
|
|
+ blob += v + "\n"
|
|
|
+ if blob:
|
|
|
+ for mt in re.finditer(r"[\"']?(/(?:opt/data|tmp|home|var)[\w./\-]+\.[A-Za-z0-9]{1,5})[\"']?", blob):
|
|
|
+ out.append(mt.group(1))
|
|
|
+ for mt in re.finditer(r"(?:>|\btee\b)\s*[\"']?(/[\w./\-]+\.[A-Za-z0-9]{1,5})", blob):
|
|
|
+ out.append(mt.group(1))
|
|
|
+ # 去重保序
|
|
|
+ seen, uniq = set(), []
|
|
|
+ for x in out:
|
|
|
+ if x not in seen:
|
|
|
+ seen.add(x)
|
|
|
+ uniq.append(x)
|
|
|
+ return uniq
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:224-265`
|
|
|
+
|
|
|
+调用它的位置:
|
|
|
+```python
|
|
|
+ cands = paths_from_tool(tool, ti)
|
|
|
+ if not cands:
|
|
|
+ return 0
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:344-346`
|
|
|
+
|
|
|
+3. **是的,「自然不会被观测到」在你的实测日志里已是事实**。证据:
|
|
|
+
|
|
|
+- 第 4 轮由 `gen.py` 生成 5 个文件,hook 日志只有一条 SKIP,没有任何 QUEUE:
|
|
|
+```
|
|
|
+2026-11-17T16:11:51 SKIP /opt/data/diag-487734/gen.py tool=write_file reason=ext-not-allowlisted(.py)
|
|
|
+```
|
|
|
+`/opt/data/cache/deliverable-hook/hook.log`
|
|
|
+
|
|
|
+- hook 端的正则只在**工具参数文本**里捞路径。用 `python gen.py` 生成时,`command` 里只有 `gen.py`,没有 `a.xlsx`;`os.stat` 差异从未被采集。
|
|
|
+
|
|
|
+唯一例外是「命令里明文写了输出路径」(`>`、`tee` 或 `-o /x/foo.pdf` 命中了 `/.xlsx` 这类正则),这属于误打误撞,不是目录扫描。
|
|
|
+
|
|
|
+**修正一处**:第一轮我曾说这部分「未取证」。现在有新证据:`execute_code` 经由 `code` 键确实能被正则捞到(`code` 键在 `blob` 采集范围内,第 250-253 行)。真正捞不到的是「代码里用变量拼路径」的情况(如 `Path(d)/'a.xlsx'`),因为那时参数里没有 `.xlsx` 字面量。
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## Q7. `local_files` 是怎么识别出 a.xlsx 的
|
|
|
+
|
|
|
+**结论:上一轮的前提不成立。第 4 轮那条 `a.xlsx` 不是 `local_files` 识别的,而是 `` 标签走的 `media_files` 通道。`local_files` 在整条第 4 轮没有命中。你问的「识别规则」是裸绝对路径 + 扩展名白名单 + `os.path.isfile` 三重条件;而第 4 轮根本没有进这个函数。**
|
|
|
+
|
|
|
+原始证据(第 4 轮 assistant 正文原文,取自 `/opt/data/state.db`):
|
|
|
+
|
|
|
+```
|
|
|
+五个文件已生成,下面逐个发送。
|
|
|
+
|
|
|
+**1/5 — a.xlsx**
|
|
|
+
|
|
|
+MEDIA:/opt/data/diag-487734/d/a.xlsx
|
|
|
+```
|
|
|
+
|
|
|
+日志侧交叉验证(`/opt/data/logs/gateway.log:27255-27257`):
|
|
|
+```
|
|
|
+2026-11-17 16:11:57,816 INFO gateway.platforms.base: [Matrix] Delivering 1 non-image MEDIA attachment(s)
|
|
|
+2026-11-17 16:11:57,832 ERROR hermes_plugins.plugin_matrix_outbound_s3: matrix-outbound-s3: FAIL outbound: 没有可用的云盘直链(filename=a.xlsx size=4942)。
|
|
|
+2026-11-17 16:11:57,833 WARNING gateway.platforms.base: [Matrix] Failed to send media (.xlsx): no-preuploaded-url
|
|
|
+```
|
|
|
+注意日志措辞是 `Failed to send media`,不是 `local file`,因为 `media_tag=True`。若是 `local_files`,应是 `Failed to send local file (.xlsx)`。
|
|
|
+
|
|
|
+**第 1 轮 1.csv 也是 MEDIA 行**,数据库原文:
|
|
|
+```
|
|
|
+1.csv 已生成(表头 + 三行两列数字)。
|
|
|
+
|
|
|
+MEDIA:/opt/data/diag-487734/a/1.csv
|
|
|
+```
|
|
|
+
|
|
|
+**`local_files` 真正的识别规则全文**:
|
|
|
+
|
|
|
+```python
|
|
|
+ def extract_local_files(content: str) -> Tuple[List[str], str]:
|
|
|
+ """Bare local file paths (absolute, ``~/`` or drive-letter) with deliverable extensions ->
|
|
|
+ ``(expanded_paths, cleaned_text)``. Candidates must exist on disk (URLs / hallucinated paths
|
|
|
+ ignored); paths inside fenced or inline code are skipped so code samples are never
|
|
|
+ mutilated. Dispatch by type lives in ``gateway/run.py``."""
|
|
|
+ ext_part = '|'.join(e.lstrip('.') for e in MEDIA_DELIVERY_EXTS)
|
|
|
+ # Lookbehind rejects URL/relative matches ( ./foo.png).
|
|
|
+ # (?<![/:\w.]) prevents matching inside URLs (e.g. ) and relative paths (./foo.png)
|
|
|
+ # (?:~/|/) anchors to absolute or home-relative Unix paths (?:[A-Za-z]:[/\\]) anchors to Windows
|
|
|
+ # drive-letter paths (#34632)
|
|
|
+ path_re = re.compile(
|
|
|
+ r'(?<![/:\w.])(?:~/|/|[A-Za-z]:[/\\])(?:[\w.\-]+[/\\])*[\w.\-]+\.(?:' + ext_part + r')\b',
|
|
|
+ re.IGNORECASE)
|
|
|
+ code_spans = _code_spans(content)
|
|
|
+ unique: dict = {} # expanded_path -> raw_match_text, deduped in discovery order
|
|
|
+ for match in path_re.finditer(content):
|
|
|
+ if any(s <= match.start() < e for s, e in code_spans):
|
|
|
+ continue
|
|
|
+ raw = match.group(0)
|
|
|
+ expanded = os.path.expanduser(raw)
|
|
|
+ if os.path.isfile(expanded):
|
|
|
+ unique.setdefault(expanded, raw)
|
|
|
+ else:
|
|
|
+ # Most common reason a promised file never arrives — log the gap.
|
|
|
+ logger.info("Skipping bare file path in reply (no file on disk): %s", _log_safe_path(raw))
|
|
|
+ if not unique:
|
|
|
+ return [], content
|
|
|
+ cleaned = content
|
|
|
+ for raw in unique.values():
|
|
|
+ cleaned = cleaned.replace(raw, '')
|
|
|
+ return list(unique), re.sub(r'\n{3,}', '\n\n', cleaned).strip()
|
|
|
+```
|
|
|
+`/opt/hermes/gateway/platforms/base.py:3129-3159`
|
|
|
+
|
|
|
+调用顺序(MEDIA 先、local_files 后,且 local_files 在 `text_content` 已剥离 MEDIA 之后):
|
|
|
+
|
|
|
+```python
|
|
|
+ with self._media_delivery_scope(event.source):
|
|
|
+ media_files, response = self.extract_media(response)
|
|
|
+ media_files = self.filter_media_delivery_paths(media_files, session_key=***
|
|
|
+ images, text_content = self.extract_images(response)
|
|
|
+ # Strip any remaining internal directives from message body (fixes #1561). _strip_media_directives
|
|
|
+ # shares MEDIA_TAG_CLEANUP_RE, so a tag with an unknown extension is intentionally left in the
|
|
|
+ # body for extract_local_files below to pick up rather than silently dropped (#34517).
|
|
|
+ text_content = _strip_media_directives(text_content).strip()
|
|
|
+ if images:
|
|
|
+ logger.info("[%s] extract_images found %d image(s) in response (%d chars)", self.name, len(images), len(response))
|
|
|
+ local_files = []
|
|
|
+ if not is_ephemeral_response:
|
|
|
+ local_files, text_content = self.extract_local_files(text_content)
|
|
|
+ local_files = self.filter_local_delivery_paths(local_files, session_key=***
|
|
|
+ history = (await self._bounded_history_media_paths_for_session(session_key)
|
|
|
+ if local_files else None)
|
|
|
+ if history:
|
|
|
+ suppressed = [p for p in local_files if p in history]
|
|
|
+ if suppressed:
|
|
|
+ logger.info("[%s] Suppressing %d bare local file path(s) already delivered in "
|
|
|
+ "this session: %s", self.name, len(suppressed), suppressed)
|
|
|
+ local_files = [p for p in local_files if p not in history]
|
|
|
+ if local_files:
|
|
|
+ logger.info("[%s] extract_local_files found %d file(s) in response", self.name, len(local_files))
|
|
|
+```
|
|
|
+`/opt/hermes/gateway/platforms/base.py:4201-4224`
|
|
|
+
|
|
|
+白名单扩展名集合:
|
|
|
+```python
|
|
|
+MEDIA_DELIVERY_EXTS: Tuple[str, ...] = (
|
|
|
+ ".png", ".jpg", ".jpeg", ".gif", ".webp", ".bmp", ".tiff", ".svg", # images (embed inline)
|
|
|
+ ".mp4", ".mov", ".avi", ".mkv", ".webm", ".3gp", # video (embed inline where supported)
|
|
|
+ ".mp3", ".m2a", ".wav", ".ogg", ".opus", ".m4a", ".flac", # audio (voice/audio where supported)
|
|
|
+ ".pdf", ".docx", ".doc", ".odt", ".rtf", ".txt", ".md", ".epub", # documents (file attachments)
|
|
|
+ ".xlsx", ".xls", ".ods", ".csv", ".tsv", ".json", ".xml", ".yaml", ".yml", # spreadsheets/data
|
|
|
+ ".kmz", ".kml", ".geojson", ".gpx", # geospatial / GIS
|
|
|
+ ".pptx", ".ppt", ".odp", ".key", # presentations
|
|
|
+ ".zip", ".tar", ".gz", ".tgz", ".bz2", ".xz", ".7z", ".rar", ".apk", ".ipa", # archives
|
|
|
+ ".html", ".htm") # web / rendered output
|
|
|
+```
|
|
|
+`/opt/hermes/gateway/platforms/base.py:1188-1197`
|
|
|
+
|
|
|
+`local_files` 在本机真实命中的唯一一次(今天 17:07,另一个会话,被删 URL 后剩裸路径):
|
|
|
+```
|
|
|
+2026-11-17 17:07:26,997 INFO gateway.platforms.base: Skipping bare file path in reply (no file on disk): /opt/data/diag-487734/a/1.csv
|
|
|
+2026-11-17 17:07:27,014 INFO gateway.platforms.base: [Matrix] extract_local_files found 1 file(s) in response
|
|
|
+2026-11-17 17:07:27,441 WARNING gateway.platforms.base: [Matrix] Failed to send local file (.html): no-preuploaded-url
|
|
|
+```
|
|
|
+`/opt/data/logs/gateway.log:27452-27457`
|
|
|
+
|
|
|
+**回答你的第 2 小问**:4 轮里没有「只有第 4 轮被识别到」这回事。第 1 轮和第 4 轮都是 MEDIA 行;`local_files` 两轮都没参与。差异只在 MEDIA 数量:第 1 轮 1 个文件,第 4 轮 5 个文件,但正文只写了 1 个 MEDIA 标签,所以只发 1 个,失败 1 个。
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## Q8. 改法边界
|
|
|
+
|
|
|
+### 改法 A:轮次结束扫目录兜底
|
|
|
+
|
|
|
+**结论:可行,而且这是唯一能覆盖「`python gen.py` 生成的文件」的做法;但「本轮开始快照」这个点 Hermes 没有现成字段,必须自己在 `pre_llm_call` 里落 `round_start_ts`。误收风险真实存在,`EXT_ALLOW` 足够挡扩展名,`NAME_DENY_GLOB` 挡不住「用户上传的输入文件」和「同目录旧产物」。**
|
|
|
+
|
|
|
+可靠拿到「本轮开始」的位置(按可靠性):
|
|
|
+
|
|
|
+```python
|
|
|
+ try:
|
|
|
+ from hermes_cli.lifecycle import invoke_hook as _invoke_hook
|
|
|
+ _pre_results = _invoke_hook(
|
|
|
+ "pre_llm_call",
|
|
|
+ session_id=agent.session_id,
|
|
|
+ task_id=effective_task_id,
|
|
|
+ turn_id=turn_id,
|
|
|
+ user_message=original_user_message,
|
|
|
+ conversation_history=list(messages),
|
|
|
+ is_first_turn=(not bool(conversation_history)),
|
|
|
+ model=agent.model,
|
|
|
+ platform=getattr(agent, "platform", None) or "",
|
|
|
+ parent_session_id=getattr(agent, "_parent_session_id", None) or "",
|
|
|
+ sender_id=getattr(agent, "_user_id", None) or "",
|
|
|
+ )
|
|
|
+```
|
|
|
+`/opt/hermes/agent/turn_context.py:691-705`
|
|
|
+
|
|
|
+- **可靠**:`pre_llm_call` 带 `turn_id`,每轮开头触发一次,这是最干净的「本轮开始」时间点。
|
|
|
+- **可靠**:`transform_llm_output` 带 `turn_id`(见改法 B 的代码),轮末触发。
|
|
|
+- **不可靠**:`post_tool_call` 的 payload 没有 `turn_id`,只有 `_DEFAULT_PAYLOADS` 里的 `task_id`/`tool_call_id`:
|
|
|
+```python
|
|
|
+ "post_tool_call": {
|
|
|
+ "tool_name": "terminal", "args": {"command": "echo hello"},
|
|
|
+ "session_id": "test-session", "task_id": "test-task", "tool_call_id": "test-call",
|
|
|
+ "result": '{"output": "hello"}', "duration_ms": 42,
|
|
|
+ },
|
|
|
+```
|
|
|
+`/opt/hermes/hermes_cli/hooks.py:111-115`
|
|
|
+
|
|
|
+- **「本轮工作目录」拿不到准确值**:payload 里的 `cwd` 是 hook 子进程的 cwd,不是工具的工作目录:
|
|
|
+```python
|
|
|
+def _payload_fields(kwargs: Dict[str, Any]) -> Dict[str, Any]:
|
|
|
+ """Common stdin/POST payload fields (shared with outbound webhooks); key order is wire order."""
|
|
|
+ try:
|
|
|
+ cwd = str(Path.cwd())
|
|
|
+ except OSError:
|
|
|
+ cwd = ""
|
|
|
+```
|
|
|
+`/opt/hermes/agent/shell_hooks.py:79-84`
|
|
|
+
|
|
|
+ 所以扫描根目录只能从 `tool_input` 里推(`terminal` 的 `cwd` 参数 / 命令里的绝对路径 / `write_file` 的父目录),或者固定扫 `/opt/data/work/` 和 `/opt/data/diag-*/`。这是改法 A 最脏的部分。
|
|
|
+
|
|
|
+误收风险与现有闸门:
|
|
|
+- `EXT_ALLOW` 挡扩展名够用(27 个扩展名,`/opt/data/scripts/fmode-deliverable-hook.py:79-84`)。
|
|
|
+- `NAME_DENY_GLOB` 只挡 `*.bak* *.tmp *.temp *.log *.pyc *.pyo *~ .DS_Store *.part *.swp`(第 117 行),**挡不住**:用户上传的 `input.xlsx`(放同一目录)、同目录上一轮的旧产物、`.venv` 里被复制出来的 `.csv`。不过 `DIR_DENY_SUBSTR` 里有 `/.venv`、`/node_modules/`、`/__pycache__/`,那部分能挡。
|
|
|
+- 结论:**只靠「目录 diff + 现有闸门」会误收**。必须加两条:① 只认 `st_mtime >= round_start_ts`(新写的);② 只认本轮 `tool_input` 里出现过的目录,不全局扫 `/opt/data`。
|
|
|
+
|
|
|
+### 改法 B:交付段落只列本轮新增
|
|
|
+
|
|
|
+**结论:可行,但要放弃「transform 里靠 30 分钟窗推断本轮」的思路。`transform_llm_output` 本身拿得到 `turn_id`,可是**同一个 turn 里它可能被调用多次**(每次有工具调用后出正文),所以不能把它当「轮次收尾」用;`round_id` 必须由 `pre_llm_call` 写,`transform` 只读。**
|
|
|
+
|
|
|
+`turn_id` 确实进了 transform 的 payload:
|
|
|
+```python
|
|
|
+ for _hook_result in _invoke_hook_safely(
|
|
|
+ "transform_llm_output", logger,
|
|
|
+ response_text=final_response,
|
|
|
+ session_id=agent.session_id or "",
|
|
|
+ model=agent.model,
|
|
|
+ platform=platform,
|
|
|
+ turn_id=turn_id, # per-turn identity for the hook callback gate
|
|
|
+ ):
|
|
|
+```
|
|
|
+`/opt/hermes/agent/turn_finalizer.py:419-426`
|
|
|
+
|
|
|
+但「轮次边界」在 Hermes 里是 `turn_id`,不是时间。你现在的判据是时间窗:
|
|
|
+```python
|
|
|
+ # 白名单:历史上真实 push 成功的地址(30 分钟内有效)——只有这些 URL 允许出现在正文
|
|
|
+ allowed_urls = {p.get("url") for p in st.get("pushed", []) if isinstance(p, dict) and p.get("url")}
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:951-952`
|
|
|
+
|
|
|
+以及:
|
|
|
+```python
|
|
|
+ pushed_paths = {p["path"] for p in st.get("pushed", [])}
|
|
|
+ todo = [q for q in queued if q["path"] not in pushed_paths]
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:959-960`
|
|
|
+
|
|
|
+注意这里**没有**按 `turn_id` 过滤 `pushed[]`,所以上一轮的 URL 会一直复活。改成 `round_id` 是正确方向。
|
|
|
+
|
|
|
+- 「`transform` 里能拿到本轮准确边界吗」→ 能拿到 `turn_id`,但拿不到「这是不是本 turn 最后一次 transform」。所以:**在 `pre_llm_call` 里写 `state["round_id"] = turn_id` + `state["round_start_ts"] = time.time()`,`transform` 里按 `pushed[].round_id == 当前 turn_id` 过滤**,同时保留 `transform` 末尾把 `round_id` 标记为本轮,保证同一轮多次 transform 复用同一批。
|
|
|
+- 「同一轮多次 transform 会不会重复/漏」→ 现有代码已有 `todo`/`pushed_paths` 去重(第 959-960 行),同一轮第二次 transform 时 `todo` 为空,走 `_emit(text, allowed_urls)` 直出,不会重复上传也不会重复贴链接。**真正的漏**出现在 `queued` 被第 962 行清空:
|
|
|
+```python
|
|
|
+ if not todo:
|
|
|
+ st["queued"] = []
|
|
|
+ save_state(sid, st)
|
|
|
+ return _emit(text, allowed_urls)
|
|
|
+```
|
|
|
+`/opt/data/scripts/fmode-deliverable-hook.py:961-964`
|
|
|
+
|
|
|
+ 如果在同一 turn 里「先 transform 一次(清空 queued)→ 再跑工具生成新文件 → 再 transform」,新文件会在 `post_tool_call` 重新入队,不会漏。但如果工具在第一次 transform 之后才落盘、且 `post_tool_call` 没抓到(就是 Q6 的场景),第二次 transform 也补不上。所以 B 的完成度依赖 A。
|
|
|
+
|
|
|
+### 改法 C:失败可见
|
|
|
+
|
|
|
+**结论:可行,但拦截点不在 `_upload_and_send` 内部(那里连不上「怎么把话发给用户」的语义),最合适的是插件里给 `MatrixAdapter` 再包一层 `_notify_media_delivery_failure`,或者包 `send_document`。不会影响 Discord,因为这个 patch 只打在 Matrix 真类上。**
|
|
|
+
|
|
|
+`_notify_media_delivery_failure` 是 base 类的通用出口:
|
|
|
+```python
|
|
|
+ async def _notify_media_delivery_failure(
|
|
|
+ self, chat_id: str, media_path: str, *, is_voice: bool = False,
|
|
|
+ metadata: Optional[Dict[str, Any]] = None) -> None:
|
|
|
+ """User-visible notice when a MEDIA attachment upload failed: the tag was
|
|
|
+ already stripped from the text, so silence would be a silent drop.
|
|
|
+
|
|
|
+ The non-streaming dispatch loop strips ```` tags before sending attachments. When the
|
|
|
+ subsequent upload returns ``success=False`` (for example Discord accepted the message but attached
|
|
|
+ nothing), the user must see a failure notice instead of a silent drop (#66797).
|
|
|
+ """
|
|
|
+ ext = Path(media_path).suffix.lower()
|
|
|
+ if is_voice or should_send_media_as_audio(self.platform, ext, is_voice=is_voice):
|
|
|
+ text = _media_failure_text("audio")
|
|
|
+ elif ext in _VIDEO_EXTS:
|
|
|
+ text = _media_failure_text("video")
|
|
|
+ else:
|
|
|
+ text = _media_failure_text("file", os.path.basename(media_path))
|
|
|
+ try:
|
|
|
+ notice = await self.emit_warning(chat_id, text, metadata=metadata)
|
|
|
+ problem = None if notice is None or notice.success else notice.error
|
|
|
+ except Exception as notify_err:
|
|
|
+ problem = notify_err
|
|
|
+ if problem is not None:
|
|
|
+ logger.debug("[%s] Could not send media-delivery-failure notice: %s", self.name, problem)
|
|
|
+``` (1/2)
|
|
|
+```
|
|
|
+
|
|
|
+### 26【prose】
|
|
|
+
|
|
|
+```text
|
|
|
+`/opt/hermes/gateway/platforms/base.py:2985-3008`
|
|
|
+
|
|
|
+文案来源:
|
|
|
+```python
|
|
|
+def _media_failure_text(kind: str, file_name: "str | None" = None) -> str:
|
|
|
+ """User-facing "couldn't deliver" notice; ``file_name`` is the only name ever shown."""
|
|
|
+ suffix = f" ({file_name})" if file_name else ""
|
|
|
+ return f"⚠️ Couldn't deliver the {kind} attachment{suffix}."
|
|
|
+```
|
|
|
+`/opt/hermes/gateway/platforms/base.py:177-180`
|
|
|
+
|
|
|
+调用点(失败后立即调用):
|
|
|
+```python
|
|
|
+ if not result.success:
|
|
|
+ logger.warning("[%s] Failed to send %s (%s): %s", self.name,
|
|
|
+ "media" if media_tag else "local file", ext, result.error)
|
|
|
+ await self._notify_media_delivery_failure(chat_id, path, is_voice=is_voice, metadata=metadata)
|
|
|
+```
|
|
|
+`/opt/hermes/gateway/platforms/base.py:4065-4068`
|
|
|
+
|
|
|
+失效链路(Matrix 侧 `_upload_and_send` 返回 `no-preuploaded-url`):
|
|
|
+```python
|
|
|
+ media_url = _preuploaded_lookup(filename, len(data))
|
|
|
+ if not media_url:
|
|
|
+ logger.error(
|
|
|
+ "matrix-outbound-s3: FAIL outbound: 没有可用的云盘直链(filename=%s size=%d)。"
|
|
|
+ "交付物必须先由 fmode-deliverable-hook.py 上传并写入 pushed[],"
|
|
|
+ "本插件不再自行上传,也不回退 mxc(2026-11-14 用户裁决)。",
|
|
|
+ filename, len(data))
|
|
|
+ return _fail("no-preuploaded-url")
|
|
|
+```
|
|
|
+`/opt/data/plugins/plugin-matrix-outbound-s3/__init__.py:338-345`
|
|
|
+
|
|
|
+这个插件里已经有「自己发一条中文提示」的现成写法,可直接复用:
|
|
|
+```python
|
|
|
+async def _notify_user_blocked(self, room_id: str, text: str) -> None:
|
|
|
+ """给用户回一条失败提示。绝不抛异常(提示失败不能影响主流程)。"""
|
|
|
+ try:
|
|
|
+ await self._send_content_event(room_id, {"msgtype": "m.text", "body": text})
|
|
|
+ logger.info("matrix-outbound-s3: 已回复用户提示: %s", text)
|
|
|
+ except Exception as exc:
|
|
|
+ logger.error("matrix-outbound-s3: 回复用户提示失败: %s", exc)
|
|
|
+```
|
|
|
+`/opt/data/plugins/plugin-matrix-outbound-s3/__init__.py:233-239`
|
|
|
+
|
|
|
+**拦截点建议(按优先级)**:
|
|
|
+1. **在插件 `_install_patch` 里同时包 `MatrixAdapter._notify_media_delivery_failure`**:这是失败提示的唯一出口;包住它就能把英文换成中文、并给 `_send_content_event` 的 content 加 `fmode.kind` 标记。注意 `_send_content_event` 只发 `msgtype`/`body`,要带 `fmode.kind` 得自己拼 `msg_content`(第 236 行现写法不带)。
|
|
|
+2. **不要在 `_upload_and_send` 里发提示**:那里是 `send_*` 的底层,一个失败会同时被 `_deliver_media_attachments` 和 `emit_media_warning` 两条路走到,容易一条失败发两条提示。
|
|
|
+3. **影响面**:`_install_patch` 只遍历 `_candidate_classes()` 拿到的 `MatrixAdapter`(第 289-303 行),不碰 `BasePlatformAdapter`,Discord/Slack 完全不受影响。反过来说,如果你的 patch 打在 `BasePlatformAdapter` 上,才会波及所有平台。
|
|
|
+
|
|
|
+**另有一处必须提醒**:`send_document` 里还有一条**路径不存在**的独立提示,文案也不同(`Couldn't deliver the attachment.`),它不走 `_notify_media_delivery_failure`:
|
|
|
+```python
|
|
|
+ p = Path(file_path).expanduser()
|
|
|
+ if not p.exists():
|
|
|
+ # file_path is host-local; never echo it into chat.
|
|
|
+ logger.warning("[%s] upload fallback: media file not found for %s", self.name, file_path)
|
|
|
+ text = "⚠️ Couldn't deliver the attachment."
|
|
|
+ return await self.emit_media_warning(room_id, text, caption=caption, reply_to=reply_to, metadata=metadata)
|
|
|
+```
|
|
|
+`/opt/hermes/plugins/platforms/matrix/adapter.py:1828-1833`
|
|
|
+
|
|
|
+要彻底「失败必可见」,这条也要一起改。 (2/2)
|
|
|
+```
|
|
|
+
|
|
|
+### 27【ack】
|
|
|
+
|
|
|
+```text
|
|
|
+✅ 容器已标记完成
|
|
|
+```
|
|
|
+
|
|
|
+---
|
|
|
+
|
|
|
+## 工具过程(仅供核对,非回答内容)
|
|
|
+
|
|
|
+- `🐍 Running code from hermes_tools import terminal, re...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal r =... 📖 Reading fmode-deliverable-hook.py L1-300`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal r =... 📖 Reading fmode-deliverable-hook.py L1-300 📖 Reading fmode-deliverable-hook.py L300-479`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal r =... 📖 Reading fmode-deliverable-hook.py L1-300 📖 Reading fmode-deliverable-hook.py L300-479 📖 Reading fmode-deliverable-hook.py L900-1079`
|
|
|
+- `🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `📖 Reading base.py L3100-3229`
|
|
|
+- `* 📖 Reading base.py L3100-3229 🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `📖 Reading base.py L4100-4249`
|
|
|
+- `🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `🐍 Running code from hermes_tools import read_file, t...`
|
|
|
+- `* 🐍 Running code from hermes_tools import read_file, t... (×2)`
|
|
|
+- `🐍 Running code from hermes_tools import read_file pr...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal r =...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal, re...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import read_file pr...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import read_file pr... 🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal, re...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import terminal pri... 🐍 Running code from hermes_tools import read_file pr...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import terminal pri... 🐍 Running code from hermes_tools import read_file pr... 🐍 Running code from hermes`
|
|
|
+- `🐍 Running code from hermes_tools import terminal, re...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal, re... 🐍 Running code from hermes_tools import read_file c ...`
|
|
|
+- `🐍 Running code from hermes_tools import read_file pr...`
|
|
|
+- `🐍 Running code from hermes_tools import read_file c ...`
|
|
|
+- `* 🐍 Running code from hermes_tools import read_file c ... 🐍 Running code from hermes_tools import read_file pr...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal pri... (×2)`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal pri... (×3)`
|
|
|
+- `🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `* 🐍 Running code from hermes_tools import terminal pri... (×2)`
|
|
|
+- `🐍 Running code from hermes_tools import terminal pri...`
|
|
|
+- `🐍 Running code import sqlite3, json con = sqlite3.co...`
|
|
|
+- `* 🐍 Running code import sqlite3, json con = sqlite3.co... 🐍 Running code import sqlite3 con = sqlite3.connect(...`
|
|
|
+- `* 🐍 Running code import sqlite3, json con = sqlite3.co... 🐍 Running code import sqlite3 con = sqlite3.connect(... (×2)`
|