feat(routing): ordered backend candidates + real-probing doctor
- backends is now an ordered candidate list (first = preferred); channels report the backend actually serving via active_backend, surfaced in the doctor text report and --json - new agent_reach/probe.py really executes upstream commands and tells apart missing / broken (stale venv shebang after a system Python upgrade) / timeout, with a reinstall prescription for broken installs - all 13 channels migrated off which()-only checks: fixes bilibili false-positive "bili-cli 可用" on broken shims, misleading xiaohongshu "连接失败", rdt OSError crashing doctor, mcporter breakage masquerading as "未配置" - twitter: 15s probe + 1 retry (flaky 10s timeout), broken twitter-cli now falls back to bird instead of aborting the check - doctor survives per-channel exceptions; config supports per-channel backend override (<channel>_backend / <CHANNEL>_BACKEND env) - fix skill install/uninstall crash on symlinked skill dirs (the "[Errno None] None" warning from shutil.rmtree on a symlink) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -10,12 +10,24 @@ import json
|
||||
import shutil
|
||||
import subprocess
|
||||
|
||||
from agent_reach.utils.process import utf8_subprocess_env
|
||||
|
||||
from .base import Channel
|
||||
|
||||
_CREDENTIAL_FILE = "~/.config/rdt-cli/credential.json"
|
||||
# Pinned to the 0.4.2 state — PyPI still only has 0.4.1 (upstream issue #10).
|
||||
_RDT_GIT_SOURCE = "git+https://github.com/public-clis/rdt-cli.git@5e4fb3720d5c174e976cd425ccc3b879d52cac66"
|
||||
|
||||
#: shell 对"找到但不可执行/找不到"使用的退出码(对齐 agent_reach.probe)
|
||||
_BROKEN_EXIT_CODES = (126, 127)
|
||||
|
||||
#: rdt 应从固定 git 源安装(PyPI 落后),断链处方与 probe 默认的 pipx/uv 不同
|
||||
_RDT_BROKEN_HINT = (
|
||||
"rdt 命令存在但无法执行——通常是系统 Python 升级后 venv 解释器丢失。\n"
|
||||
"PyPI 版本落后,推荐用固定 git 源强制重装:\n"
|
||||
f" pipx install --force '{_RDT_GIT_SOURCE}'"
|
||||
)
|
||||
|
||||
|
||||
class RedditChannel(Channel):
|
||||
name = "reddit"
|
||||
@@ -30,6 +42,8 @@ class RedditChannel(Channel):
|
||||
return "reddit.com" in d or "redd.it" in d
|
||||
|
||||
def check(self, config=None):
|
||||
self.active_backend = None
|
||||
|
||||
rdt = shutil.which("rdt")
|
||||
if not rdt:
|
||||
return "off", (
|
||||
@@ -42,6 +56,10 @@ class RedditChannel(Channel):
|
||||
"安装后运行 `rdt login` 登录(需先在浏览器登录 reddit.com)"
|
||||
)
|
||||
|
||||
# 不走 probe_command:实测 `rdt status --json` 成功时(rc=0)也会向 stderr
|
||||
# 打网络重试日志,probe 把 stdout+stderr 合并后 JSON 解析必炸。
|
||||
# 故保留手写 subprocess(stdout 单独捕获),但异常分类对齐 probe 语义:
|
||||
# exec 失败/126/127 → broken(venv 断链处方),TimeoutExpired → 超时。
|
||||
try:
|
||||
r = subprocess.run(
|
||||
[rdt, "status", "--json"],
|
||||
@@ -49,31 +67,55 @@ class RedditChannel(Channel):
|
||||
encoding="utf-8",
|
||||
errors="replace",
|
||||
timeout=10,
|
||||
env=utf8_subprocess_env(),
|
||||
)
|
||||
data = json.loads(r.stdout or "{}")
|
||||
authenticated = data.get("data", {}).get("authenticated", False)
|
||||
username = data.get("data", {}).get("username") or ""
|
||||
except subprocess.TimeoutExpired:
|
||||
return "error", "rdt 响应超时(>10s),Reddit 状态未知。稍后重试或运行 `rdt status` 查看详情"
|
||||
except OSError:
|
||||
# 含 FileNotFoundError:which 命中但 exec 失败 = venv 断链(probe 的 broken)
|
||||
return "error", _RDT_BROKEN_HINT
|
||||
|
||||
if authenticated:
|
||||
suffix = f"(已登录:{username})" if username else ""
|
||||
return "ok", (f"rdt-cli 可用{suffix}(搜索帖子、阅读全文、查看评论)")
|
||||
if r.returncode in _BROKEN_EXIT_CODES:
|
||||
return "error", _RDT_BROKEN_HINT
|
||||
|
||||
return "warn", (
|
||||
"rdt-cli 已安装但未登录。Reddit 自 2024 年起要求认证,"
|
||||
"未登录时所有请求均返回 403。\n\n"
|
||||
"方法一(自动):运行 `rdt login`\n"
|
||||
" 先在浏览器登录 reddit.com,再运行此命令自动提取 Cookie。\n\n"
|
||||
"方法二(手动,适用于 Chrome/Edge 127+ 无法自动提取时):\n"
|
||||
" 1. Chrome 应用商店安装 Cookie-Editor 扩展:\n"
|
||||
" https://chromewebstore.google.com/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm\n"
|
||||
" 2. 在浏览器打开 reddit.com(确保已登录)\n"
|
||||
" 3. 点击 Cookie-Editor 图标,找到 `reddit_session`,复制其 Value\n"
|
||||
f" 4. 将以下内容写入 {_CREDENTIAL_FILE}:\n"
|
||||
' {"cookies": {"reddit_session": "<粘贴 Value>"}, '
|
||||
'"source": "manual", "username": "<你的用户名>", '
|
||||
'"modhash": null, "saved_at": 0, "last_verified_at": null}\n\n'
|
||||
"验证:`rdt status --json` 确认 authenticated: true"
|
||||
)
|
||||
if r.returncode != 0:
|
||||
detail = (r.stderr or r.stdout or "").strip().splitlines()
|
||||
tail = detail[-1] if detail else "无输出"
|
||||
return "error", f"rdt 异常退出(exit {r.returncode}):{tail}。运行 `rdt status` 查看详情"
|
||||
|
||||
except (json.JSONDecodeError, FileNotFoundError, subprocess.TimeoutExpired):
|
||||
return "warn", "rdt-cli 已安装但状态检查失败,运行 `rdt status` 查看详情"
|
||||
# 进程正常退出 → rdt 本身是活的(无论登录与否),后端即为可用
|
||||
self.active_backend = "rdt-cli"
|
||||
|
||||
try:
|
||||
data = json.loads(r.stdout or "")
|
||||
except json.JSONDecodeError:
|
||||
data = None
|
||||
if not isinstance(data, dict):
|
||||
return "warn", "rdt-cli 可用但状态输出无法解析,运行 `rdt status` 查看登录状态"
|
||||
|
||||
info = data.get("data")
|
||||
if not isinstance(info, dict):
|
||||
info = {}
|
||||
authenticated = info.get("authenticated", False)
|
||||
username = info.get("username") or ""
|
||||
|
||||
if authenticated:
|
||||
suffix = f"(已登录:{username})" if username else ""
|
||||
return "ok", (f"rdt-cli 可用{suffix}(搜索帖子、阅读全文、查看评论)")
|
||||
|
||||
return "warn", (
|
||||
"rdt-cli 已安装但未登录。Reddit 自 2024 年起要求认证,"
|
||||
"未登录时所有请求均返回 403。\n\n"
|
||||
"方法一(自动):运行 `rdt login`\n"
|
||||
" 先在浏览器登录 reddit.com,再运行此命令自动提取 Cookie。\n\n"
|
||||
"方法二(手动,适用于 Chrome/Edge 127+ 无法自动提取时):\n"
|
||||
" 1. Chrome 应用商店安装 Cookie-Editor 扩展:\n"
|
||||
" https://chromewebstore.google.com/detail/cookie-editor/hlkenndednhfkekhgcdicdfddnkalmdm\n"
|
||||
" 2. 在浏览器打开 reddit.com(确保已登录)\n"
|
||||
" 3. 点击 Cookie-Editor 图标,找到 `reddit_session`,复制其 Value\n"
|
||||
f" 4. 将以下内容写入 {_CREDENTIAL_FILE}:\n"
|
||||
' {"cookies": {"reddit_session": "<粘贴 Value>"}, '
|
||||
'"source": "manual", "username": "<你的用户名>", '
|
||||
'"modhash": null, "saved_at": 0, "last_verified_at": null}\n\n'
|
||||
"验证:`rdt status --json` 确认 authenticated: true"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user