From 605ca8e16c80fe268e36fc297928104153e68f11 Mon Sep 17 00:00:00 2001 From: Yaojia Wang Date: Sat, 21 Mar 2026 16:57:25 +0100 Subject: [PATCH] vault: update Trading Agents with WebSocket fix, sessions_spawn solution, and final verification --- .../Trading Agents 混合架构方案.md | 34 ++--- .../Trading Agents 调试与优化记录.md | 113 +++++++++++++++- .../Trading-Agents/Trading Agents 部署记录.md | 124 ++++++++++++++---- 3 files changed, 226 insertions(+), 45 deletions(-) diff --git a/2 - Projects/Trading-Agents/Trading Agents 混合架构方案.md b/2 - Projects/Trading-Agents/Trading Agents 混合架构方案.md index b34207d..43e1e41 100644 --- a/2 - Projects/Trading-Agents/Trading Agents 混合架构方案.md +++ b/2 - Projects/Trading-Agents/Trading Agents 混合架构方案.md @@ -46,9 +46,10 @@ tags: [trading, multi-agent, openclaw, openbb, architecture] > 复用 invest-analyst 作为编排者(已有 invest-api skill 和 Stock Guild 绑定)。 > 分析师做成 skill 而非独立 agent(省资源,数据采集不需要对话)。 > 只有需要"对话"的辩论角色才需要独立 agent。 -> **经实测确认**:辩论 agent 无 `groupChat.mentionPatterns`,只通过 `sessions_send` 调用(@ mention 会导致无限循环,详见 [[Trading Agents 调试与优化记录]])。 +> **经实测确认**:辩论 agent 通过 `sessions_spawn` 调用(@ mention 会导致无限循环,`sessions_send` 会 gateway 死锁,详见 [[Trading Agents 调试与优化记录]])。 +> 辩论 agent 的 Discord 已禁用(`enabled: false`),不在频道输出,只在后台运行。 -### 通信模式 +### 通信模式(最终确认) ``` 用户: /trade-analyze NVDA @@ -56,22 +57,25 @@ tags: [trading, multi-agent, openclaw, openbb, architecture] v invest-analyst (编排者) │ - ├── 执行 4 个分析 skill(数据采集 + LLM 生成报告) - │ ├── /market-analysis NVDA → 技术分析报告 - │ ├── /fundamental-analysis NVDA → 基本面报告 - │ ├── /sentiment-analysis NVDA → 情感分析报告 - │ └── /macro-analysis NVDA → 宏观环境报告 + ├── exec curl 收集 4 类数据(technical, metrics, sentiment, macro) │ - ├── sessions_send → invest-bull(发送 4 份报告,要求出 bull case) - │ invest-bull ⇄ invest-bear(ping-pong 3-5 轮辩论) - │ invest-analyst 收到完整辩论记录(announce) + ├── sessions_spawn → invest-bull(Bull Case) + │ 等待 announce → Bull 结果返回 │ - ├── sessions_send → invest-hawk(发送交易方案,要求评估风险) - │ invest-hawk ⇄ invest-dove(ping-pong 2-3 轮辩论) - │ invest-analyst 收到风控辩论记录(announce) + ├── sessions_spawn → invest-bear(Bear Case,附带 Bull 论点) + │ 等待 announce → Bear 结果返回 │ - └── 综合所有报告 + 辩论记录 → 最终 BUY/SELL/HOLD 决策 - 投递到 Stock Guild Discord 频道 + ├── sessions_spawn → invest-bull(Final Rebuttal) + │ 等待 announce → Bull 最终反驳返回 + │ + ├── sessions_spawn → invest-hawk(激进风控评估) + │ 等待 announce → Hawk 结果返回 + │ + ├── sessions_spawn → invest-dove(保守风控评估) + │ 等待 announce → Dove 结果返回 + │ + └── 综合所有结果 → 最终 BUY/SELL/HOLD 裁决 + 只有 invest-analyst 输出到 Discord ``` ### 为什么不用群聊辩论 diff --git a/2 - Projects/Trading-Agents/Trading Agents 调试与优化记录.md b/2 - Projects/Trading-Agents/Trading Agents 调试与优化记录.md index 168d861..71894b8 100644 --- a/2 - Projects/Trading-Agents/Trading Agents 调试与优化记录.md +++ b/2 - Projects/Trading-Agents/Trading Agents 调试与优化记录.md @@ -182,7 +182,118 @@ trade-analyze 流程: --- -## 六、Session 文件位置 +## 六、Gateway WebSocket 超时修复(根本问题) + +### 问题 + +`sessions_send` 和 `sessions_spawn` 全部报错: +``` +gateway timeout after 10000ms +Gateway target: ws://127.0.0.1:18789 +Source: local loopback +``` + +所有 session 工具、`openclaw status --deep`、`openclaw gateway call` 都超时。 + +### 根因分析 + +1. Gateway 配置 `bind: "lan"`,监听 `0.0.0.0:18789` +2. 但 **`127.0.0.1:18789` 实际连不通**(`curl http://127.0.0.1:18789/` 超时,但 `curl http://192.168.68.108:18789/` 成功) +3. OpenClaw 内部工具默认连 `ws://127.0.0.1:18789`,导致所有 RPC 超时 +4. 同时存在 [v2026.3.13 WebSocket handshake bug](https://github.com/openclaw/openclaw/issues/48167):handshake timeout 只有 3 秒 + +### 修复 + +**修复 1:Systemd 环境变量** + +在 `~/.config/systemd/user/openclaw-gateway.service` 中添加: +```ini +Environment=OPENCLAW_GATEWAY_URL=ws://192.168.68.108:18789 +Environment=OPENCLAW_ALLOW_INSECURE_PRIVATE_WS=1 +``` + +然后 `systemctl --user daemon-reload`。 + +这让 gateway 内部工具通过 LAN IP(而非 localhost)连接,绕过了 loopback 不通的问题。 + +**修复 2:Patch handshake timeout(来自 [PR #47388](https://github.com/openclaw/openclaw/pull/47388))** + +文件:`~/.nvm/versions/node/v24.13.1/lib/node_modules/openclaw/dist/gateway-cli-CuZs0RlJ.js`(和 `Ol-vpIk7.js`) + +```javascript +// 原始(第 7588 行) +const DEFAULT_HANDSHAKE_TIMEOUT_MS = 3e3; +// 修改为 +const DEFAULT_HANDSHAKE_TIMEOUT_MS = 10e3; +``` + +**修复 3:Patch scope grant(来自 [PR #47388](https://github.com/openclaw/openclaw/pull/47388))** + +同一文件,第 22605 行附近: + +```javascript +// 原始 +if (!device && (!isControlUi || decision.kind !== "allow")) clearUnboundScopes(); +// 修改为 +if (!device && (!isControlUi || decision.kind !== "allow")) { clearUnboundScopes(); } else if (!device && decision.kind === "allow") { scopes = ["operator.read"]; connectParams.scopes = scopes; } +``` + +### 验证 + +修复后 `openclaw gateway call status` 返回正常 JSON,`sessions_spawn` 成功: +``` +15:49:43 status: "accepted", childSessionKey: "agent:invest-bull:subagent:ad2d265d..." +15:50:06 [subagent task] bull-AMZN: completed successfully +``` + +### 注意事项 + +- 这些 patch 在 `npm update openclaw` 后会被覆盖,需要重新打 +- 关注 [PR #47388](https://github.com/openclaw/openclaw/pull/47388) 和 [PR #48950](https://github.com/openclaw/openclaw/pull/48950) 的合并状态 +- 合并后升级即可去掉手动 patch + +--- + +## 七、最终验证:AMZN 辩论流程 + +### 完整时间线 + +| 时间 (UTC) | 事件 | 状态 | +|------------|------|------| +| 15:48:20 | 读取 trade-analyze skill | ✅ | +| 15:48:36 | 收集 AMZN 数据(curl API) | ✅ | +| 15:49:13 | metrics + sentiment 数据返回 | ✅ | +| 15:49:27 | technical + macro 返回 503 | ⚠️ K8s API pod 暂时不可用 | +| 15:49:43 | `sessions_spawn` → invest-bull | ✅ accepted | +| 15:50:06 | Bull 完成,结果返回 | ✅ | +| 15:50:25 | `sessions_spawn` → invest-bear | ✅ accepted | +| 15:50:53 | Bear 完成,结果返回 | ✅ | +| 15:51:04 | `sessions_spawn` → invest-bull (final rebuttal) | ✅ accepted | +| 15:51:08 | 等待 Bull Final + Hawk + Dove... | ⏳ | + +### 关键确认 + +1. **`sessions_spawn` 成功调用了辩论 agent** ✅ +2. **辩论 agent 在后台执行,不在 Discord 输出** ✅(Discord 已禁用) +3. **结果通过 subagent announce 自动返回给 invest-analyst** ✅ +4. **流程按顺序执行**:Bull → Bear → Bull Final → (Hawk → Dove) ✅ +5. **没有循环** ✅(sessions_spawn 是一次性的,不会互相触发) + +--- + +## 八、sessions_send vs sessions_spawn 最终结论 + +| 工具 | 能否工作 | 原因 | +|------|---------|------| +| **@ mention** | ❌ | 导致无限循环 | +| **sessions_send** | ❌ | Gateway 内部 WebSocket 死锁(同进程内自连) | +| **sessions_spawn** | ✅ | 非阻塞,独立 lane 执行,announce 回传结果 | + +**最终方案:sessions_spawn + announce 回传。** + +--- + +## 九、Session 文件位置 | Agent | Session 路径 | |-------|-------------| diff --git a/2 - Projects/Trading-Agents/Trading Agents 部署记录.md b/2 - Projects/Trading-Agents/Trading Agents 部署记录.md index 9b8b808..7d81f66 100644 --- a/2 - Projects/Trading-Agents/Trading Agents 部署记录.md +++ b/2 - Projects/Trading-Agents/Trading Agents 部署记录.md @@ -409,56 +409,122 @@ with open("/home/yiukai/.openclaw/openclaw.json", "w") as f: --- -## 八、已知问题 +## 八、Gateway WebSocket 修复(关键补丁) -### 重启时的 Warning +### 问题 -| Warning | 影响 | 处理 | -|---------|------|------| -| `google-antigravity-auth` 插件已移除 | 无 | 可从 config 中删除该条目 | -| Telegram groupPolicy allowlist 但无 allowFrom | Telegram 群组消息被丢弃 | 非本次范围,已记录在 [[OpenClaw 部署配置分析]] | -| Security audit: 12 critical | groupPolicy=open 风险 | 已有 guild-level channel allowlist 限制,后续可收紧 | +所有内部 RPC(sessions_send/spawn、gateway call、status --deep)超时,原因: +1. `bind: "lan"` 后 `127.0.0.1:18789` 连不通 +2. v2026.3.13 的 WebSocket handshake timeout 只有 3 秒 -### 辩论 Bot 的 requireMention 设置 +### 修复 1:Systemd 环境变量 -所有 4 个新 bot 都设置了 `requireMention: true`,这意味着: -- 用户在 Discord 直接说话 **不会** 触发辩论 bot -- 只有 invest-analyst 通过 `sessions_send` A2A 协议才能触发 -- 这是正确的行为——辩论 bot 不应该自行响应用户消息 +在 `~/.config/systemd/user/openclaw-gateway.service` 的 `[Service]` 中添加: + +```ini +Environment=OPENCLAW_GATEWAY_URL=ws://192.168.68.108:18789 +Environment=OPENCLAW_ALLOW_INSECURE_PRIVATE_WS=1 +``` + +```bash +systemctl --user daemon-reload +``` + +### 修复 2:Patch handshake timeout + +文件:`~/.nvm/versions/node/v24.13.1/lib/node_modules/openclaw/dist/gateway-cli-CuZs0RlJ.js`(和 `Ol-vpIk7.js`) + +```javascript +// 第 7588 行:3e3 → 10e3 +const DEFAULT_HANDSHAKE_TIMEOUT_MS = 10e3; +``` + +### 修复 3:Patch scope grant + +同一文件,第 22605 行: + +```javascript +// 原始 +if (!device && (!isControlUi || decision.kind !== "allow")) clearUnboundScopes(); +// 改为 +if (!device && (!isControlUi || decision.kind !== "allow")) { clearUnboundScopes(); } else if (!device && decision.kind === "allow") { scopes = ["operator.read"]; connectParams.scopes = scopes; } +``` + +> 来源:[PR #47388](https://github.com/openclaw/openclaw/pull/47388)、[PR #48950](https://github.com/openclaw/openclaw/pull/48950) +> **注意**:`npm update openclaw` 后 patch 会被覆盖,需要重新打 + +### 修复 4:辩论 Agent Discord 配置 + +辩论 agent 的 Discord 已完全禁用(不在频道输出): + +```json +"invest-bull": { "enabled": false, "groupPolicy": "disabled" } +``` + +invest-analyst 通过 `sessions_spawn`(非 sessions_send)在后台调用辩论 agent。 + +### 修复 5:subagents allowAgents + +invest-analyst 需要允许 spawn 辩论 agent: + +```json +{ + "id": "invest-analyst", + "subagents": { + "allowAgents": ["invest-bull", "invest-bear", "invest-hawk", "invest-dove"] + } +} +``` --- -## 九、测试步骤 +## 九、其他已知问题 -### 1. 单个分析 Skill +| 问题 | 影响 | 处理 | +|------|------|------| +| Telegram groupPolicy allowlist 无 allowFrom | 群组消息被丢弃 | 非本次范围 | +| `google-antigravity-auth` 插件 | 已删除 | ✅ | +| openbb-invest-api 偶尔 503 | K8s pod 可能重启 | 检查 pod 状态 | + +--- + +## 十、测试步骤 + +### 1. 快速分析 -在 Stock Guild Discord 中对 invest-analyst 说: ``` -/market-analysis NVDA +帮我看看 AAPL 的行情 ``` -期望:返回结构化技术分析报告。 +期望:invest-analyst 直接用 invest-api skill 回答。 -### 2. 辩论 Ping-Pong +### 2. 深度辩论分析 + +``` +/trade-analyze AMZN +``` +期望: +1. 收集数据(curl API) +2. `sessions_spawn` → invest-bull(Bull Case) +3. `sessions_spawn` → invest-bear(Bear Case) +4. `sessions_spawn` → invest-bull(Final Rebuttal) +5. `sessions_spawn` → invest-hawk(激进风控) +6. `sessions_spawn` → invest-dove(保守风控) +7. invest-analyst 输出最终 BUY/SELL/HOLD 裁决 + +### 3. 验证辩论 agent 参与 -在远程服务器上手动测试 A2A: ```bash -openclaw agent --agent invest-analyst --message "请用 sessions_send 给 invest-bull 发送:'请基于 NVDA RSI 75, PE 45 构建看多论点'" +for a in invest-bull invest-bear invest-hawk invest-dove; do + echo "$a: $(ls ~/.openclaw/agents/$a/sessions/*.jsonl 2>/dev/null | wc -l) sessions" +done ``` -期望:invest-bull 回复 bull case → 自动 ping-pong 到 invest-bear。 - -### 3. 完整流程 - -在 Stock Guild Discord 中: -``` -/trade-analyze AAPL -``` -期望:4 报告 → Bull/Bear 辩论 → Hawk/Dove 风控 → 最终 BUY/SELL/HOLD。 --- ## Related - [[Trading Agents 混合架构方案]] +- [[Trading Agents 调试与优化记录]] - [[TradingAgents 原始架构分析]] - [[OpenClaw 部署配置分析]] - [[OpenClaw Stock Agent 配置详情]]