Compare commits

..
8 Commits
Author SHA1 Message Date
openbot 17be84f041 docs: CHANGELOG 0.2.0 (gateway/acpclient split) 2026-10-25 14:00:27 +08:00
openbot e5dad05c41 0.2.0: rename package agenthub-gateway; extract ACP client into acpclient (shims keep old imports) 2026-10-25 13:58:41 +08:00
openbot 80bb05510f docs: gateway/worker split design (deepagents orchestrator + HTTP/WS dispatch) 2026-10-24 15:47:27 +08:00
openbot 353db7ba10 fix(wecom): proactive fallback must not spam one message per delta
The 59c44e9 fallback replied every coalesced delta (~0.8s) as a separate
proactive message once the callback req_id went stale - a wall of
repeating snapshots. Now: intermediate deltas are dropped after a single
'long reply, result will follow' notice; only the final text goes out.
2026-10-24 10:12:19 +08:00
openbot 59c44e94ca fix(wecom): degrade stream replies to proactive msg when callback req_id stale
Turns that outlive the WeCom callback req_id window (e.g. approval
round-trips >5min) had their delta/finish frames silently rejected by
the gateway, so the user saw 'clicked approve, nothing happened' while
the agent kept working. Track req_id recv time; past a 240s TTL, stream
updates fall back to proactive aibot_send_msg. Also log non-zero
errcode ack frames so future gateway rejections are visible.
2026-10-24 09:18:19 +08:00
qbot e07f37fbb4 chore: gitignore runtime sqlite db and local config backups 2026-10-22 18:54:58 +08:00
qbot 7ba5d75f3a test: isolate $HOME per test so media fallback stops writing ~/agenthub-media
test_channels_e2e / test_relay_no_stack / test_session_manager drive prompt()
with agents that lack cwd_whitelist, so the ~/agenthub-media fallback created
directories in the real home directory on every run.
2026-10-22 18:54:58 +08:00
qbot 74d99f8705 fix(media): derive agent workspace root from glob-free prefix of cwd_whitelist
cwd_whitelist entries are fnmatch patterns (`~/workspace/*`), but
_agent_cwd() only ran expanduser() on the first entry, so media_in/ and the
outbound media sentinel used a literal `*` directory
(`~/workspace/*/media_in`). _glob_root() now strips the glob segment.

Also covers sending files prematurely: sentinel scan of the literal dir
never saw agent output.
2026-10-22 18:54:58 +08:00
13 changed files with 369 additions and 478 deletions

No files matched your search

+5
View File
@@ -5,3 +5,8 @@ dist/
.pytest_cache/ .pytest_cache/
config.local.yaml config.local.yaml
node_modules/ node_modules/
# runtime state + local config backups (never committed)
agenthub.db
agenthub.db-shm
agenthub.db-wal
config.json.bak.*
+20
View File
@@ -0,0 +1,20 @@
# Changelog
## [0.2.0] — 2026-10-25
### 拆分(gateway / acpclient)
- 发行名 `agenthub` → **`agenthub-gateway`**(import 名仍为 `agenthub`,兼容旧部署与测试)。
- ACP 客户端独立成包 **`acpclient`**(新仓库 `vxquant/acpclient`):
- `acpclient.ACPClient` — stdio + ndjson/lsp-frame 客户端(自 agenthub 平移)。
- `acpclient.RemoteACPClient` — 同 API,transport 为 HTTP POST(指令)+ WebSocket(事件回流),
帧仍是 ACP JSON-RPC 原样透传;面向远程 worker-agentd(M-w1 目标)。
- `acpclient.framing` — ndjson / LSP Content-Length 编解码。
- `agenthub/framing.py`、`agenthub/acp_client.py` 变 3 行 re-export shim,老 import 零破坏。
- `[project.scripts]` 增加 `agenthub-gateway`(保留 `agenthub` 别名)。
### 文档
- 新增 `docs/DESIGN_GATEWAY_WORKER.md`(gateway + worker 架构:deepagents 总管 + HTTP/WS 派活)。
- 新增 `docs/UPGRADE_SPLIT.md`(拆分实施清单)。
### 依赖
- 新增 `acpclient>=0.1.0` 运行时依赖。
+81
View File
@@ -0,0 +1,81 @@
# agenthub → gateway / worker 拆分设计(v1 草案)
## 目标形态
```
IM(企微) ──► gateway(api 机 hub) worker(quant-host)
│ 总管 agent = deepagents opencode acp (stdio, worker 本地)
│ (进程内, LLM tool-calling) ▲
│ │ dispatch_task tool │
│ ▼ │
│ dispatcher ──HTTP/WS──► worker-agentd(ACP client + 请求透传)
│ │
└── 进度/结果/审批事件 ◄──HTTP/WS────┘
```
- **gateway**:现有 hub 原样保留(IM 通道、session 绑定、审批卡片、follow-up 队列、admin UI)。
新增 `worker:<name>` agent 类型:对 SessionManager 来说它是一个普通 agent,但 ACP client
不在本地拉起子进程,而是把 ACP 帧经 HTTP/WS 转发给远端 worker。
- **总管 agent**:deepagents(进程内),见关键设计 1。
- **worker**:新服务 `worker-agentd`,跑在 worker 机器上:本地 stdio 拉起 `opencode acp`,
HTTP 收指令、WS/SSE 回事件(详见关键设计 2/3)。
## 关键设计决定
### 1. 总管 agent = deepagents(gateway 进程内,不再用 opencode)
- `pip install deepagents`(LangChain 官方 harness,LangGraph 之上),
`create_deep_agent(model=..., system_prompt=..., tools=[dispatch_task, ...])`。
- 好处:派活就是普通 tool call(结构化、schema 校验、LangSmith 可观测),
不用解析自由文本标记;sub-agents / planning / context 管理 / 权限中间件
(human-in-the-loop)开箱即用,与 hub 审批语义天然对齐。
- 模型可配(tool-calling 模型均可),gateway 配置里加 `orchestrator` 段。
- 总管**不执行任务**,只有 dispatch_task 工具;task 描述由 LLM 生成。
### 2. 派活传输 = HTTP(POST)+ WS(事件回流),不用 NATS
- **指令面**:gateway dispatcher POST `http://<worker>:<port>/acp/dispatch`
body {conn_id, frames:[...]}(ACP JSON-RPC 帧数组)→ 202。
- **事件面**:worker-agentd 主动连 gateway 的 WS `/acp/worker-events`
(worker→gateway 单向更符合防火墙现实:worker 出站连 gateway),
推送 {conn_id, frame}——update 流、request_permission、response 全走这里。
- 选型理由(vs ACP-over-HTTP RFD):RFD 未定稿且 opencode 不支持 server 模式;
这里的 HTTP/WS 是 **agenthub 私有信封**(帧仍 ACP JSON-RPC),将来
opencode 支持官方传输时,worker-agentd 的信封可整体替换。
- 依赖最小:gateway 已有 stdlib ThreadingHTTPServer 先例;WS 用 websockets 库
(或先 SSE 单向,v1 够用)。
### 3. worker-agentd(worker 机器新服务)
1. 本地 stdio 拉起 `opencode acp`(不变);
2. HTTP POST 接收 dispatch:把帧写入本地 opencode stdin;
3. WS/SSE 把 opencode stdout 帧回传 gateway;
4. 心跳 + conn_id 复用(session/load 恢复)。
### 4. 审批链路零新代码
worker opencode 的 `session/request_permission` 帧原样经 WS 回 gateway →
现有审批卡片逻辑直接工作;点击→reply 帧沿 dispatch POST 回 worker。
depth+1 护栏在 dispatcher 注入。
### 5. 生命周期
- conn_id = gateway 侧 ACP session id;绑定表/idle reaper//new/switch 语义不变;
reaper 关「NATS/WS 会话」→ worker-agentd 杀本地 opencode。
- worker 掉线:gateway pending ACP 请求以 ACPProcessDead 失败(现成机制)。
## 分期
- **M-w1(worker-agentd MVP)**:单 worker 单 agent。HTTP dispatch + SSE/WS 事件回流,
stdio 透传。gateway 侧加 `worker:<name>` agent 类型(ACP transport=remote)。
验收:curl 直接 dispatch → worker opencode 回帧全环(不经过总管)。
- **M-w2(deepagents 总管)**:gateway 进程内跑 deepagents 总管,
tools=[dispatch_task(worker 名单+能力描述)];system prompt 定义派活策略;
LangSmith 可选。验收:IM 消息 → 总管派活 → worker 执行 → 结果/审批回 IM。
- **M-w3(可观测与运维)**:worker 心跳、gateway 状态页 worker 列表、dispatch 审计、
总管派活失败重试策略。
## 风险
| 风险 | 缓解 |
|---|---|
| HTTP/WS 上大 payload(agent 输出大文本/文件) | 沿用 hub 900KB 分片先例;文件走 workspace 不走帧 |
| 双跳审批延迟 | 帧透传无额外 RTT 协商;卡片点击→gateway→HTTP→worker 单向链 |
| deepagents 依赖较重(langchain 栈) | 仅 gateway 安装;总管失败可降级直连默认 worker |
| 总管误派 | 结构化解析失败即不派;depth 护栏;worker 能力清单限定 |
| worker-agentd 崩溃丢会话 | gateway 端 ACPProcessDead 兜底 + 用户重试(与进程死亡同语义) |
+61
View File
@@ -0,0 +1,61 @@
# 升级方案:拆分为 agenthub-gateway / acpclient 两个包
## 目标
把现有单体 `agenthub` 升级为两个独立发布的 Python 包:
1. **acpclient**(新独立包,零 gateway 依赖)
- 迁入:`framing.py`(ndjson / lsp-frame 分帧)+ `acp_client.py`(ACPClient、
ACPError、ACPProcessDead、handler 协议)
- 新增(M-w1 需要的远程传输):`RemoteACPClient`——与 ACPClient 同一接口
(request/notify/register_*/initialize/session_*),transport 不是子进程
stdin/stdout,而是 HTTP POST(指令)+ WS(事件回流),帧仍是 ACP JSON-RPC 原样。
- 传输层抽象:`start()/close()/is_alive()/_send()/读循环` 收敛为 Transport 协议,
StdioTransport(现有逻辑原样)与 RemoteTransport(新)两个实现。上层
(session.py 的 pending/permission/relay 逻辑)完全不感知差别。
- 发布名:`acpclient`(PyPI 空闲已确认过 agenthub 被占但 acpclient 待查),
单独 repo `vxquant/acpclient`,CI 同 agenthub(Gitea 先行→GitHub)。
2. **agenthub-gateway**(原 agenthub 改名重发布)
- 目录 `src/agenthub/` 不动(import 名保留 `agenthub`,避免大改 140 个测试与
三台部署脚本),只改发行元数据:
- `name = "agenthub-gateway"`,`[project.scripts] agenthub-gateway = "agenthub.__main__:main"`
- 依赖加 `acpclient`(本地 path/内网 PyPI),删除内部 framing/acp_client 模块,
`from acpclient import ...` 兼容 shim:`agenthub/acp_client.py` 留 3 行 re-export,
老测试/老 import 不破坏。
- gateway 侧后续新增(本升级不实现,仅留口):`worker:<name>` agent 类型
(cmd 字段换 url 字段)、deepagents orchestrator 段。
## 为什么保留 import 名 `agenthub`
- 140 tests + 三台部署(zd git pull、api rsync、proxy git)+ supervisor unit 文件
都引用 `agenthub` 包与 `agenthub` 命令;改名 import 是纯机械风险零收益。
- 发行名与 import 名不一致是 Python 常态(`pip install agent-client-protocol`
→ `import acp`)。
## 实施清单(一次升级,向后兼容)
| # | 事项 | 验证 |
|---|---|---|
| 1 | 新 repo `vxquant/acpclient`:src/acpclient/{framing,client,transports}.py(代码从 agenthub 平移) | uv run pytest(迁过去的老测试 + RemoteTransport 单测 mock HTTP/WS) |
| 2 | acpclient pyproject:独立版本 0.1.0,零依赖(stdlib only,WS 用 websockets optional extra) | uv build + 安装冒烟 |
| 3 | agenthub pyproject:rename `agenthub-gateway`,deps + acpclient,scripts 改名,旧脚本 `agenthub` 保留别名 | 全量 140 tests 过(import 不变) |
| 4 | agenthub/acp_client.py 与 agenthub/framing.py 变 re-export shim | 老测试不动全绿 |
| 5 | Gitea 建 vxquant/acpclient、推送;agenthub-gateway 版本 bump 0.2.0 | push 成功 |
| 6 | 三台部署:zd/proxy git pull(requirements 解析 acpclient 需内网可装——Gitea 直装 `agenthub-gateway @ gitea+https` 或 vendor);api rsync | 目标机 pytest + 重启 + status |
| 7 | CHANGELOG + DESIGN_GATEWAY_WORKER.md 更新实施状态 | — |
## RemoteACPClient 接口草案
```python
class RemoteACPClient:
"""ACPClient 兼容接口,帧走 HTTP/WS 到 worker-agentd。"""
def __init__(self, base_url: str, worker_token: str | None = None): ...
# 与 ACPClient 相同:request/notify/register_notification_handler/
# register_request_handler/initialize/session_new/session_prompt/session_cancel
# start() = 打开 WS 事件流 + POST initialize;close() = POST /close
# is_alive() = WS 连接健康 + 心跳未超时
```
worker-agentd(后续 M-w1 实现,不在本升级)作为 acpclient 的姊妹服务,
复用 acpclient 的 framing/handler 协议。
+5 -3
View File
@@ -1,7 +1,7 @@
[project] [project]
name = "agenthub" name = "agenthub-gateway"
version = "0.1.0" version = "0.2.0"
description = "IM/NATS to local ACP (Agent Client Protocol) bridge - drive Claude Code, Codex, Gemini CLI, OpenCode from messaging front-ends" description = "IM/NATS to ACP (Agent Client Protocol) gateway - drive Claude Code, Codex, Gemini CLI, OpenCode from messaging front-ends; orchestrates remote workers via acpclient"
readme = "README.md" readme = "README.md"
license = { text = "MIT" } license = { text = "MIT" }
authors = [{ name = "OpenBot", email = "openbot@vxquant.com" }] authors = [{ name = "OpenBot", email = "openbot@vxquant.com" }]
@@ -20,6 +20,7 @@ classifiers = [
dependencies = [ dependencies = [
"cryptography>=42", "cryptography>=42",
"websockets>=12", "websockets>=12",
"acpclient>=0.1.0",
] ]
[project.optional-dependencies] [project.optional-dependencies]
@@ -32,6 +33,7 @@ Documentation = "https://gitea.vxquant.com/vxquant/agenthub/src/branch/main/docs
[project.scripts] [project.scripts]
agenthub = "agenthub.__main__:main" agenthub = "agenthub.__main__:main"
agenthub-gateway = "agenthub.__main__:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
+3 -353
View File
@@ -1,355 +1,5 @@
"""ACP client: JSON-RPC 2.0 over stdio to an agent subprocess. """Backward-compat shim: ACP client moved to the acpclient package."""
M0 scope: from acpclient.client import ACPClient, ACPError, ACPProcessDead # noqa: F401
- launch agent subprocess, talk ACP frames over stdin/stdout
- initialize / session/new / session/prompt
- dispatch server->client notifications (session/update) and requests
(fs/*, terminal/*, session/request_permission) to registered handlers
- thread-safe request/response correlation by id
"""
from __future__ import annotations __all__ = ["ACPClient", "ACPError", "ACPProcessDead"]
import json
import shlex
import subprocess
import threading
from dataclasses import dataclass, field
from typing import Any, Callable
from .framing import FrameError, decode_frames, decode_ndjson_lines, encode_frame, encode_ndjson
# ---------------------------------------------------------------------------
# JSON-RPC error codes
# ---------------------------------------------------------------------------
PARSE_ERROR = -32700
INVALID_REQUEST = -32600
METHOD_NOT_FOUND = -32601
INVALID_PARAMS = -32602
INTERNAL_ERROR = -32603
class ACPError(Exception):
"""An error returned by the agent for a client request."""
def __init__(self, code: int, message: str, data: Any = None):
super().__init__(f"ACP error {code}: {message}")
self.code = code
self.message = message
self.data = data
class ACPProcessDead(ACPError):
def __init__(self, detail: str = ""):
super().__init__(INTERNAL_ERROR, f"agent process is not running: {detail}")
# ---------------------------------------------------------------------------
# handler signatures
# ---------------------------------------------------------------------------
NotificationHandler = Callable[[str, dict[str, Any]], None]
RequestHandler = Callable[[str, dict[str, Any]], Any] # returns result, raises ACPError
@dataclass
class ACPClient:
"""A single ACP connection to one agent subprocess."""
cmd: list[str]
cwd: str | None = None
env: dict[str, str] | None = None
transport: str = "ndjson" # "ndjson" (current ACP SDK agents) | "lsp-frame"
_proc: subprocess.Popen | None = field(default=None, init=False, repr=False)
_lock: threading.Lock = field(default_factory=threading.Lock, init=False, repr=False)
_write_lock: threading.Lock = field(default_factory=threading.Lock, init=False, repr=False)
_next_id: int = field(default=0, init=False, repr=False)
_pending: dict[int, tuple[threading.Event, list]] = field(
default_factory=dict, init=False, repr=False
)
_notif_handlers: list[NotificationHandler] = field(default_factory=list, init=False, repr=False)
_req_handlers: list[RequestHandler] = field(default_factory=list, init=False, repr=False)
_buf: bytearray = field(default_factory=bytearray, init=False, repr=False)
_closed: bool = field(default=False, init=False, repr=False)
_stderr_lines: list[str] = field(default_factory=list, init=False, repr=False)
_last_error: str = field(default="", init=False)
# -- lifecycle ---------------------------------------------------------
def start(self) -> None:
flat: list[str] = []
for c in self.cmd:
flat.extend(shlex.split(c) if isinstance(c, str) else list(c))
self._proc = subprocess.Popen(
flat,
cwd=self.cwd,
env=self.env,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
bufsize=0,
)
threading.Thread(target=self._read_loop, name="acp-reader", daemon=True).start()
threading.Thread(target=self._stderr_loop, name="acp-stderr", daemon=True).start()
def close(self, timeout: float = 5.0) -> None:
self._closed = True
proc = self._proc
if proc is None:
return
try:
if proc.stdin:
proc.stdin.close()
except OSError:
pass
try:
proc.wait(timeout=timeout)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait(timeout=timeout)
def is_alive(self) -> bool:
return self._proc is not None and self._proc.poll() is None
@property
def stderr_tail(self) -> list[str]:
return list(self._stderr_lines[-50:])
# -- low level io ------------------------------------------------------
def _send(self, obj: dict[str, Any]) -> None:
if not self.is_alive():
raise ACPProcessDead(self._last_error or "process exited")
assert self._proc and self._proc.stdin
data = encode_ndjson(obj) if self.transport == "ndjson" else encode_frame(obj)
with self._write_lock:
try:
self._proc.stdin.write(data)
self._proc.stdin.flush()
except (BrokenPipeError, OSError) as e:
raise ACPProcessDead(str(e)) from e
def _stderr_loop(self) -> None:
assert self._proc and self._proc.stderr
for raw in iter(self._proc.stderr.readline, b""):
line = raw.decode("utf-8", "replace").rstrip()
if line:
self._stderr_lines.append(line)
def _read_loop(self) -> None:
assert self._proc and self._proc.stdout
try:
while not self._closed:
chunk = self._proc.stdout.read(65536)
if not chunk:
break
self._buf.extend(chunk)
if self.transport == "ndjson":
self._drain_ndjson()
else:
self._drain()
except (OSError, ValueError):
pass
finally:
detail = self._last_error
if not detail and self._proc:
detail = f"exit={self._proc.poll()}"
self._fail_all(f"reader stopped ({detail})")
def _drain(self) -> None:
"""Parse complete frames out of the buffer, keeping partial tails.
decode_frames raises FrameError for both 'need more bytes' and
'corrupt stream'; we distinguish by scanning incrementally:
try to parse with a shrinking window until only an incomplete
tail remains.
"""
while True:
if not self._buf:
return
try:
msgs = list(decode_frames(self._buf))
except FrameError as e:
msg = str(e)
if "incomplete frame" in msg or "no header terminator" in msg:
return # partial tail: wait for more bytes
# corrupt stream: record and drop
self._last_error = f"framing error: {e}"
self._buf.clear()
return
if not msgs:
return
# decode_frames consumed the whole buffer by definition
# (it only yields complete frames and the buffer holds only
# whole frames here, else it would have raised 'incomplete')
self._buf.clear()
for m in msgs:
self._dispatch(m)
return
def _drain_ndjson(self) -> None:
try:
msgs, consumed = decode_ndjson_lines(self._buf)
except (UnicodeDecodeError, json.JSONDecodeError) as e:
self._last_error = f"ndjson parse error: {e}"
self._buf.clear()
return
if consumed:
del self._buf[:consumed]
for m in msgs:
self._dispatch(m)
def _fail_all(self, detail: str) -> None:
self._last_error = detail
with self._lock:
pending = list(self._pending.items())
self._pending.clear()
for _rid, (ev, slot) in pending:
slot.append(ACPProcessDead(detail))
ev.set()
# -- dispatch ----------------------------------------------------------
def _dispatch(self, msg: dict[str, Any]) -> None:
if not isinstance(msg, dict):
return
has_id = "id" in msg
method = msg.get("method")
if method is not None and not has_id:
for h in self._notif_handlers:
try:
h(method, msg.get("params") or {})
except Exception: # noqa: BLE001 - handler errors must not kill the reader
pass
return
if method is not None and has_id:
self._handle_server_request(msg)
return
if has_id:
self._resolve_response(msg)
def _handle_server_request(self, msg: dict[str, Any]) -> None:
# Handlers may BLOCK (e.g. session/request_permission waits minutes for
# the user's IM reply). Run them on a worker thread: blocking here would
# stall the reader loop, and every pending request would time out.
threading.Thread(
target=self._run_server_request, args=(msg,), daemon=True,
name=f"acp-srvreq-{msg.get('method')}-{msg.get('id')}",
).start()
def _run_server_request(self, msg: dict[str, Any]) -> None:
rid = msg["id"]
method = msg["method"]
params = msg.get("params") or {}
result: Any = None
error: dict[str, Any] | None = None
handled = False
for h in self._req_handlers:
try:
result = h(method, params)
handled = True
break
except ACPError as e:
error = {"code": e.code, "message": e.message, "data": e.data}
handled = True
break
except NotImplementedError:
continue
except Exception as e: # noqa: BLE001
error = {"code": INTERNAL_ERROR, "message": str(e)}
handled = True
break
if not handled:
error = {"code": METHOD_NOT_FOUND, "message": f"unsupported: {method}"}
reply: dict[str, Any] = {"jsonrpc": "2.0", "id": rid}
if error:
reply["error"] = error
else:
reply["result"] = result
try:
self._send(reply)
except ACPProcessDead:
pass
def _resolve_response(self, msg: dict[str, Any]) -> None:
rid = msg["id"]
with self._lock:
slot = self._pending.pop(rid, None)
if slot is None:
return
ev, out = slot
if "error" in msg:
err = msg["error"] or {}
out.append(
ACPError(err.get("code", INTERNAL_ERROR), err.get("message", "unknown"), err.get("data"))
)
else:
out.append(msg.get("result"))
ev.set()
# -- public API --------------------------------------------------------
def register_notification_handler(self, h: NotificationHandler) -> None:
self._notif_handlers.append(h)
def register_request_handler(self, h: RequestHandler) -> None:
self._req_handlers.append(h)
def request(
self, method: str, params: dict[str, Any] | None = None, timeout: float | None = 60.0
) -> Any:
if not self.is_alive():
raise ACPProcessDead(self._last_error or "not started")
with self._lock:
self._next_id += 1
rid = self._next_id
ev = threading.Event()
slot: list = []
self._pending[rid] = (ev, slot)
try:
self._send({"jsonrpc": "2.0", "id": rid, "method": method, "params": params or {}})
except ACPProcessDead:
with self._lock:
self._pending.pop(rid, None)
raise
if not ev.wait(timeout):
with self._lock:
self._pending.pop(rid, None)
raise ACPError(INTERNAL_ERROR, f"timeout waiting for {method} ({timeout}s)")
out = slot[0]
if isinstance(out, Exception):
raise out
return out
def notify(self, method: str, params: dict[str, Any] | None = None) -> None:
self._send({"jsonrpc": "2.0", "method": method, "params": params or {}})
# -- ACP convenience ---------------------------------------------------
def initialize(
self, client_capabilities: dict[str, Any] | None = None, protocol_version: int = 1
) -> dict[str, Any]:
return self.request(
"initialize",
{
"protocolVersion": protocol_version,
"clientCapabilities": client_capabilities
or {
"fs": {"readTextFile": True, "writeTextFile": True},
"terminal": True,
},
},
)
def session_new(self, cwd: str, mcp_servers: list[dict[str, Any]] | None = None) -> str:
res = self.request("session/new", {"cwd": cwd, "mcpServers": mcp_servers or []})
sid = (res or {}).get("sessionId")
if not sid:
raise ACPError(INTERNAL_ERROR, f"session/new returned no sessionId: {res!r}")
return sid
def session_prompt(
self, session_id: str, blocks: list[dict[str, Any]], timeout: float | None = None
) -> dict[str, Any]:
return self.request(
"session/prompt", {"sessionId": session_id, "prompt": blocks}, timeout=timeout
)
def session_cancel(self, session_id: str) -> None:
self.notify("session/cancel", {"sessionId": session_id})
+49 -18
View File
@@ -57,9 +57,9 @@ class WecomAibotChannel:
self._ws = None # current socket (set by _connect_and_serve) self._ws = None # current socket (set by _connect_and_serve)
self.connection_state = "idle" self.connection_state = "idle"
self.connection_error: str = "" self.connection_error: str = ""
# chat -> streaming state; chat -> last callback req_id (respond must reuse it) # chat -> streaming state; chat -> (last callback req_id, recv time)
self._streams: dict[str, dict] = {} self._streams: dict[str, dict] = {}
self._req_ids: dict[str, str] = {} self._req_ids: dict[str, tuple[str, float]] = {}
self._stream_lock = threading.Lock() self._stream_lock = threading.Lock()
# media upload acks: req_id -> {event, msg} # media upload acks: req_id -> {event, msg}
self._pending_acks: dict[str, dict] = {} self._pending_acks: dict[str, dict] = {}
@@ -165,6 +165,8 @@ class WecomAibotChannel:
cmd = msg.get("cmd", "") cmd = msg.get("cmd", "")
if msg.get("errcode") is not None and not cmd: if msg.get("errcode") is not None and not cmd:
# ack/pong frame — route to a waiting media-upload _request() if any # ack/pong frame — route to a waiting media-upload _request() if any
if msg.get("errcode") != 0:
self._logger.warning(f"[wecom] gateway rejected frame: errcode={msg.get('errcode')} errmsg={msg.get('errmsg')} req_id={(msg.get('headers') or {}).get('req_id', '')}")
self._deliver_ack(msg) self._deliver_ack(msg)
return return
req_id = (msg.get("headers") or {}).get("req_id", "") req_id = (msg.get("headers") or {}).get("req_id", "")
@@ -216,7 +218,7 @@ class WecomAibotChannel:
# remember req_id for streaming replies keyed by chat # remember req_id for streaming replies keyed by chat
if req_id: if req_id:
with self._stream_lock: with self._stream_lock:
self._req_ids[chat_id] = req_id self._req_ids[chat_id] = (req_id, time.time())
text = clean_control_chars(text, max_len=100000).strip() if text else "" text = clean_control_chars(text, max_len=100000).strip() if text else ""
# group chat: only respond when @mentioned is handled by WeCom itself # group chat: only respond when @mentioned is handled by WeCom itself
if not text and not images and not files: if not text and not images and not files:
@@ -469,23 +471,52 @@ class WecomAibotChannel:
for chunk in overflow: for chunk in overflow:
self.reply(chat_id, chunk) self.reply(chat_id, chunk)
# The inbound callback's req_id is only accepted by the WeCom gateway for a
# limited window (~minutes). After an approval round-trip the turn can
# outlive that window; frames reusing the stale req_id are silently
# rejected, so the user sees "clicked approve, nothing happened". Past the
# TTL, stream updates degrade to proactive aibot_send_msg messages.
_CALLBACK_REQ_TTL_S = 240.0
def _reply_stream(self, chat_id: str, st: dict, text: str, finished: bool) -> None: def _reply_stream(self, chat_id: str, st: dict, text: str, finished: bool) -> None:
# official: reuse the CALLBACK's req_id in headers; stream.id identifies the bubble entry = self._req_ids.get(chat_id)
req_id = self._req_ids.get(chat_id, "") req_id = ""
self._send( stale = True
{ if entry:
"cmd": "aibot_respond_msg", req_id, ts = entry
"headers": {"req_id": req_id}, stale = (time.time() - ts) > self._CALLBACK_REQ_TTL_S
"body": { if req_id and not stale:
"msgtype": "stream", self._send(
"stream": {"id": st["req_id"], "finish": finished, "content": text}, {
}, "cmd": "aibot_respond_msg",
} "headers": {"req_id": req_id},
) "body": {
if finished: "msgtype": "stream",
st["sent"] = True "stream": {"id": st["req_id"], "finish": finished, "content": text},
# proactive follow-ups after the stream need aibot_send_msg },
}
)
if finished:
st["sent"] = True
with self._stream_lock:
self._req_ids.pop(chat_id, None)
return
# No live callback req_id (stale or absent): proactive aibot_send_msg
# territory. Intermediate deltas are DROPPED — replying each would spam
# one full-snapshot message every 0.8s. Instead: a single notice the
# first time we notice staleness, then silence until the final text,
# which always goes out (possibly in overflow chunks).
if not finished:
if not st.get("proactive_noticed"):
st["proactive_noticed"] = True
self.reply(chat_id, "⏳ 回复时间较长,处理完成后将直接发送结果…")
return
st["sent"] = True
with self._stream_lock:
self._req_ids.pop(chat_id, None) self._req_ids.pop(chat_id, None)
if text:
self.reply(chat_id, text)
self._logger.info(f"[wecom] stream finished via proactive message (callback req_id stale or absent) chat={chat_id}")
def _send(self, obj: dict) -> None: def _send(self, obj: dict) -> None:
raw = json.dumps(obj, ensure_ascii=False) raw = json.dumps(obj, ensure_ascii=False)
+15 -98
View File
@@ -1,100 +1,17 @@
"""ACP JSON-RPC over stdio: Content-Length framing (LSP style). """Backward-compat shim: framing codecs moved to the acpclient package."""
Frame format used by ACP (same as LSP): from acpclient.framing import ( # noqa: F401
Content-Length: <N>\r\n FrameError,
\r\n decode_frames,
<N bytes of UTF-8 JSON> decode_ndjson_lines,
""" encode_frame,
encode_ndjson,
)
from __future__ import annotations __all__ = [
"FrameError",
import json "decode_frames",
from typing import Any, Iterator "decode_ndjson_lines",
"encode_frame",
_HEADER_SEP = b"\r\n\r\n" "encode_ndjson",
_HEADER_END = b"\r\n" ]
class FrameError(Exception):
"""Raised when the byte stream cannot be parsed as a valid frame."""
def encode_frame(payload: dict[str, Any]) -> bytes:
"""Serialize a JSON-RPC message to one ACP frame."""
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
header = b"Content-Length: " + str(len(body)).encode() + _HEADER_END * 2
return header + body
def _read_exact(buf: memoryview, pos: int, n: int) -> tuple[memoryview, int]:
if pos + n > len(buf):
raise FrameError(f"incomplete frame: need {n} bytes at pos {pos}, have {len(buf) - pos}")
return buf[pos : pos + n], pos + n
def decode_frames(buf: bytes | bytearray | memoryview) -> Iterator[dict[str, Any]]:
"""Parse all complete frames from a buffer.
Raises FrameError on malformed headers or truncated frames;
returns an iterator of decoded JSON objects.
"""
view = memoryview(bytes(buf))
pos = 0
while pos < len(view):
# find header terminator
idx = bytes(view[pos:]).find(_HEADER_SEP)
if idx == -1:
raise FrameError(f"no header terminator after pos {pos}")
header_blob = bytes(view[pos : pos + idx])
pos += idx + len(_HEADER_SEP)
length: int | None = None
content_type: str | None = None
for line in header_blob.split(_HEADER_END):
if not line:
continue
name, _, value = line.partition(b":")
name = name.strip().lower()
if name == b"content-length":
try:
length = int(value.strip())
except ValueError as e:
raise FrameError(f"bad Content-Length: {value!r}") from e
elif name == b"content-type":
content_type = value.strip().decode("ascii", "replace")
if length is None:
raise FrameError(f"missing Content-Length header: {header_blob!r}")
if length < 0:
raise FrameError(f"negative Content-Length: {length}")
body_view, pos = _read_exact(view, pos, length)
try:
yield json.loads(bytes(body_view).decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError) as e:
raise FrameError(f"bad JSON body ({length} bytes): {e}") from e
# ---------------------------------------------------------------------------
# newline-delimited JSON (ndjson) transport — used by current ACP SDK agents
# (e.g. @zed-industries/claude-code-acp via @agentclientprotocol/sdk).
# LSP-style Content-Length frames are kept in encode_frame/decode_frames for
# agents that use them; transport is auto-detected by ACPClient.
# ---------------------------------------------------------------------------
def encode_ndjson(payload: dict) -> bytes:
return json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8") + b"\n"
def decode_ndjson_lines(buf: bytes | bytearray):
"""Yield (messages, consumed_bytes) for complete newline-terminated lines."""
view = bytes(buf)
idx = view.rfind(b"\n")
if idx == -1:
return [], 0
out = []
for line in view[:idx].split(b"\n"):
line = line.strip()
if not line:
continue
out.append(json.loads(line.decode("utf-8")))
return out, idx + 1
+23 -2
View File
@@ -519,10 +519,10 @@ class SessionManager:
# -- media --------------------------------------------------------------- # -- media ---------------------------------------------------------------
def _agent_cwd(self, agent_name: str) -> str | None: def _agent_cwd(self, agent_name: str) -> str | None:
"""Agent workspace root: first whitelist entry (expanded) or None.""" """Agent workspace root: base dir of the first whitelist entry, or None."""
spec = self.agents.get(agent_name) spec = self.agents.get(agent_name)
if spec and spec.cwd_whitelist: if spec and spec.cwd_whitelist:
return os.path.expanduser(spec.cwd_whitelist[0]) return _glob_root(spec.cwd_whitelist[0])
return None return None
def _media_dir(self, agent: str) -> str: def _media_dir(self, agent: str) -> str:
@@ -960,6 +960,27 @@ class SessionManager:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def _glob_root(pattern: str) -> str:
"""Base directory of a cwd whitelist pattern (``~`` expanded).
Whitelist entries are fnmatch patterns such as ``~/workspace/*``; the agent
workspace root must be the longest glob-free prefix, otherwise media files
land in a literal ``*`` directory and the media sentinel scans nothing.
"""
pattern = os.path.expanduser(pattern)
if not any(ch in pattern for ch in "*?["):
return pattern
parts: list[str] = []
for part in pattern.split(os.sep):
if any(ch in part for ch in "*?["):
break
parts.append(part)
root = os.sep.join(parts)
if not root:
return os.sep if os.path.isabs(pattern) else "."
return root
def _check_cwd(cwd: str, whitelist: list[str]) -> None: def _check_cwd(cwd: str, whitelist: list[str]) -> None:
"""Enforce the agent's cwd whitelist (fnmatch patterns, ~ expanded).""" """Enforce the agent's cwd whitelist (fnmatch patterns, ~ expanded)."""
if not whitelist: if not whitelist:
+19
View File
@@ -0,0 +1,19 @@
"""Shared test isolation.
Several tests drive ``SessionManager.prompt(files=...)`` / ``_media_dir()`` with
agents that have no ``cwd_whitelist``, which makes the fallback path write to
``~/agenthub-media``. Pointing ``HOME`` at a per-test temp dir keeps the suite
from littering the real home directory.
"""
from __future__ import annotations
import pytest
@pytest.fixture(autouse=True)
def isolated_home(tmp_path, monkeypatch):
"""Per-test ``$HOME`` so nothing is written to the real home directory."""
home = tmp_path / "home"
home.mkdir()
monkeypatch.setenv("HOME", str(home))
return home
+20 -2
View File
@@ -77,7 +77,7 @@ def test_wecom_dispatch_text_and_permission():
"from": {"userid": "u1"}, "text": {"content": "hello\u200b"}}} "from": {"userid": "u1"}, "text": {"content": "hello\u200b"}}}
ch._dispatch(frame, ws) ch._dispatch(frame, ws)
assert len(got) == 1 and got[0].text == "hello" and got[0].chat_id == "c1" assert len(got) == 1 and got[0].text == "hello" and got[0].chat_id == "c1"
assert ch._req_ids["c1"] == "r1" # captured for the streaming reply assert ch._req_ids["c1"][0] == "r1" # captured for the streaming reply
def test_wecom_allowed_users_drops_others(): def test_wecom_allowed_users_drops_others():
@@ -91,7 +91,8 @@ def test_wecom_allowed_users_drops_others():
def test_wecom_streaming_delta_coalesce_and_final(): def test_wecom_streaming_delta_coalesce_and_final():
ch, ws = _make_wecom() ch, ws = _make_wecom()
ch._ws = ws # pretend connected (stream sends go through the socket) ch._ws = ws # pretend connected (stream sends go through the socket)
ch._req_ids["c1"] = "req-9" # as set by an inbound callback ch._req_ids["c1"] = ("req-9", __import__("time").time()) # fresh inbound callback
ch._req_ids["stale"] = ("req-old", __import__("time").time() - 600) # expired callback
ch.push_event("c1", "delta", {"text": "hello "}) ch.push_event("c1", "delta", {"text": "hello "})
ch.push_event("c1", "delta", {"text": "world"}) ch.push_event("c1", "delta", {"text": "world"})
ch.push_event("c1", "final", {"text": "hello world"}) ch.push_event("c1", "final", {"text": "hello world"})
@@ -171,3 +172,20 @@ def test_weixin_no_edit_deltas_dropped_final_sent():
assert not http.posts assert not http.posts
ch.push_event("u", "final", {"text": "full text"}) ch.push_event("u", "final", {"text": "full text"})
assert http.posts[-1][1]["text"]["content"] == "full text" assert http.posts[-1][1]["text"]["content"] == "full text"
def test_wecom_stream_falls_back_when_callback_req_stale():
ch, ws = _make_wecom()
ch._ws = ws
ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL
# intermediate deltas: only ONE proactive notice, no full-snapshot spam
for _ in range(5):
ch.push_event("c1", "delta", {"text": "more words "})
send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"]
assert len(send) == 1 and "处理完成后" in json.dumps(send[0], ensure_ascii=False)
respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"]
assert respond == [] # stale req_id must NOT be reused
# final: exactly one proactive message with the full text
ch.push_event("c1", "final", {"text": "late answer"})
send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"]
assert len(send) == 2 and "late answer" in json.dumps(send[-1], ensure_ascii=False)
+2 -2
View File
@@ -37,7 +37,7 @@ def _channel() -> tuple[WecomAibotChannel, MockWS]:
def test_small_final_single_stream_frame(): def test_small_final_single_stream_frame():
ch, ws = _channel() ch, ws = _channel()
ch._req_ids["c1"] = "r1" ch._req_ids["c1"] = ("r1", __import__("time").time())
ch._stream_close("c1", "hello") ch._stream_close("c1", "hello")
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"] streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]
assert len(streams) == 1 assert len(streams) == 1
@@ -47,7 +47,7 @@ def test_small_final_single_stream_frame():
def test_large_final_splits_into_stream_plus_markdown(): def test_large_final_splits_into_stream_plus_markdown():
ch, ws = _channel() ch, ws = _channel()
ch._req_ids["c1"] = "r1" ch._req_ids["c1"] = ("r1", __import__("time").time())
big = "字" * 12000 # 3 bytes/char -> 36000 bytes > 19000 limit big = "字" * 12000 # 3 bytes/char -> 36000 bytes > 19000 limit
ch._stream_close("c1", big) ch._stream_close("c1", big)
streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"] streams = [m for m in ws.sent if m.get("cmd") == "aibot_respond_msg"]
+66
View File
@@ -0,0 +1,66 @@
"""Agent workspace root derivation from ``cwd_whitelist`` patterns.
Regression: whitelist entries are fnmatch patterns (``~/workspace/*``), so a
naive ``expanduser`` of the first entry produced a literal ``*`` directory and
every inbound attachment landed in ``~/workspace/*/media_in``.
"""
from __future__ import annotations
import os
import pytest
from agenthub.session import SessionManager, _glob_root
def test_glob_root_strips_glob_segment():
assert _glob_root("/home/qbot/workspace/*") == "/home/qbot/workspace"
assert _glob_root("/home/qbot/projects/*") == "/home/qbot/projects"
def test_glob_root_keeps_plain_path():
assert _glob_root("/home/qbot/workspace") == "/home/qbot/workspace"
def test_glob_root_expands_home(monkeypatch, tmp_path):
monkeypatch.setenv("HOME", str(tmp_path))
assert _glob_root("~/workspace/*") == str(tmp_path / "workspace")
def test_glob_root_mid_path_glob():
assert _glob_root("/home/*/workspace") == "/home"
def test_glob_root_edge_cases():
assert _glob_root("/*") == os.sep
assert _glob_root("*.py") == "."
assert _glob_root("/home/qbot/workspace/??") == "/home/qbot/workspace"
@pytest.fixture()
def mgr(tmp_path, monkeypatch):
from agenthub.session import AgentSpec
monkeypatch.setenv("HOME", str(tmp_path))
(tmp_path / "workspace").mkdir()
return SessionManager(
{
"deepseek": AgentSpec(
name="deepseek",
cmd=["opencode acp"],
cwd_whitelist=[str(tmp_path / "workspace") + "/*", str(tmp_path / "projects") + "/*"],
)
},
db_path=str(tmp_path / "s.db"),
)
def test_media_dir_not_literal_star(mgr, tmp_path):
d = mgr._media_dir("deepseek")
assert d == str(tmp_path / "workspace" / "media_in")
assert os.path.isdir(d)
assert not (tmp_path / "workspace" / "*").exists()
def test_agent_cwd_is_glob_base(mgr, tmp_path):
assert mgr._agent_cwd("deepseek") == str(tmp_path / "workspace")