Skip to content

Give your AI agent eyes to see the entire internet. Read & search Twitter, Reddit, YouTube, GitHub, Bilibili, XiaoHongShu — one CLI, zero API fees.

fix: report NEED_LOGIN instead of UPSTREAM_BROKEN when not logged in

Pnant556112bbb325
diff --git a/CHANGELOG.md b/CHANGELOG.md
index db93f99c..888ecd6d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- 查询默认无头,优先只读复用本机已有的站点登录;显窗登录与无头查询共用配置。B站字幕、雪球社区自动同步登录,BOSS JD 使用上游浏览器接口。缺失接口和限流不再误报为需要重登。
- 专用浏览器按扩展保存在它自己配置里的编号识别,不会再把用户日常的 Chrome 认成专用浏览器;登录窗口开着时查询不再把它收掉。
- 浏览器渠道可以并行查询:同一平台最多 2 条、合计 4 条同时跑,多出来的排队而不是立刻报"正忙";登录窗口、退出登录等操作等正在跑的查询结束再做。
+- 没登录时报"需要登录"而不是"上游工具坏了":雪球社区(错误码 400016)、B站字幕(没有 B站登录)、LinkedIn(专用浏览器里没有 LinkedIn 登录)。
English:
- Browser channels share one profile, close after ten idle minutes and reopen on demand.
@@ -28,6 +29,7 @@ English:
- Keep Xiaoyuzhou transcription; remove the extra planner and duplicate site extraction.
- The dedicated browser is identified by the id its extension saves inside its own profile, so the user's own Chrome is never mistaken for it; a query no longer closes an open login window.
- Browser channels run queries in parallel: up to 2 per platform and 4 in total, with the rest queued instead of failing BUSY at once; login windows, logout and browser setup wait for running queries.
+- Report NEED_LOGIN instead of UPSTREAM_BROKEN when not logged in: Xueqiu community (error 400016), Bilibili subtitles (no Bilibili login), LinkedIn (no LinkedIn login in the dedicated browser).
中文:
diff --git a/agent_reach/channels/_opencli_site.py b/agent_reach/channels/_opencli_site.py
index c1a272e9..c9d394d8 100644
--- a/agent_reach/channels/_opencli_site.py
+++ b/agent_reach/channels/_opencli_site.py
@@ -2,7 +2,7 @@
"""Shared channel helper for OpenCLI browser-session-only platforms."""
from agent_reach.backends.opencli import opencli_call, opencli_items
-from agent_reach.runtime.result import NOT_FOUND, UPSTREAM_BROKEN, ReachError
+from agent_reach.runtime.result import NEED_LOGIN, NOT_FOUND, UPSTREAM_BROKEN, ReachError
from agent_reach.utils.url import host_matches
from .base import Channel
@@ -15,6 +15,8 @@ class OpenCLISiteChannel(Channel):
domains: tuple[str, ...] = ()
usage: str = ""
login_hint: str = ""
+ #: Cookie the site sets only for a logged-in user; its absence turns an unclear failure into NEED_LOGIN.
+ login_cookie: str = ""
tools = ("opencli",)
prereqs = ("node", "chrome")
@@ -35,9 +37,23 @@ class OpenCLISiteChannel(Channel):
f"Run `agent-reach login {self.name}` and ask the user to log in to {self.login_hint} "
"in the Agent Reach browser window it opens. Never log in for them."
)
- items = opencli_items(opencli_call(self.site, list(args), timeout=timeout, login_hint=hint))
- if not items:
- if empty_is_error:
+ try:
+ items = opencli_items(opencli_call(self.site, list(args), timeout=timeout, login_hint=hint))
+ if not items and empty_is_error:
raise ReachError(UPSTREAM_BROKEN, "OpenCLI returned nothing; the page most likely did not load")
+ except ReachError as err:
+ if err.code == UPSTREAM_BROKEN and self._logged_out():
+ raise ReachError(NEED_LOGIN, f"Not logged in to {self.login_hint}", hint=hint) from None
+ raise
+ if not items:
raise ReachError(NOT_FOUND, "nothing found")
return items
+
+ def _logged_out(self) -> bool:
+ """True only when the dedicated browser is running and lacks this site's login cookie."""
+ if not self.login_cookie:
+ return False
+ from agent_reach.runtime import browser, cdp
+ if not browser.is_running():
+ return False
+ return not any(c.get("name") == self.login_cookie for d in self.domains for c in cdp.cookies(d))
diff --git a/agent_reach/channels/bilibili.py b/agent_reach/channels/bilibili.py
index 1112d8cc..26d7d0b4 100644
--- a/agent_reach/channels/bilibili.py
+++ b/agent_reach/channels/bilibili.py
@@ -227,10 +227,11 @@ class BilibiliChannel(Channel):
data = _bili_json(["video", _bvid(target, timeout), "--subtitle"], timeout)
sub = data.get("subtitle") or {}
if not sub.get("available"):
- if any(w.get("code") == "subtitle_unavailable" for w in data.get("warnings", []) if isinstance(w, dict)):
- raise ReachError(UPSTREAM_BROKEN, "Bilibili could not retrieve subtitles; this does not prove the video has none")
+ # Without a Bilibili login, upstream only says subtitles were unavailable.
if not (sessions_root() / "bili-cli" / ".bilibili-cli" / "credential.json").exists():
raise ReachError(NEED_LOGIN, "Bilibili is not logged in", hint=_BILI_LOGIN_HINT)
+ if any(w.get("code") == "subtitle_unavailable" for w in data.get("warnings", []) if isinstance(w, dict)):
+ raise ReachError(UPSTREAM_BROKEN, "Bilibili could not retrieve subtitles; this does not prove the video has none")
raise ReachError(NOT_FOUND, "this video has no subtitles")
text = sub.get("text") or "\n".join(i.get("content", "") for i in sub.get("items") or [])
return [_cli_video_item(data.get("video") or {}, text=text)]
diff --git a/agent_reach/channels/linkedin.py b/agent_reach/channels/linkedin.py
index 6e438ca1..48a4009c 100644
--- a/agent_reach/channels/linkedin.py
+++ b/agent_reach/channels/linkedin.py
@@ -15,6 +15,7 @@ class LinkedInChannel(OpenCLISiteChannel):
domains = ("linkedin.com",)
login_hint = "linkedin.com"
login_url = "https://www.linkedin.com/login"
+ login_cookie = "li_at"
level = "optional"
actions = (
Action("jobs", "Search LinkedIn jobs", (
diff --git a/agent_reach/channels/xueqiu.py b/agent_reach/channels/xueqiu.py
index af26a6e0..9c18ccf7 100644
--- a/agent_reach/channels/xueqiu.py
+++ b/agent_reach/channels/xueqiu.py
@@ -188,9 +188,18 @@ def _xq_json(url: str) -> Any:
except urllib.error.HTTPError as exc:
if exc.code == 401:
raise ReachError(NEED_LOGIN, "Xueqiu rejected the session", hint=_XQ_LOGIN_HINT) from None
+ if exc.code == 400 and _error_code(exc) == "400016": # Xueqiu: login token missing or expired
+ raise ReachError(NEED_LOGIN, "Xueqiu requires a logged-in session", hint=_XQ_LOGIN_HINT) from None
raise
+def _error_code(exc: urllib.error.HTTPError) -> str:
+ try:
+ return str(json.loads(exc.read().decode("utf-8")).get("error_code", ""))
+ except Exception: # noqa: BLE001 - an unreadable body is not login evidence
+ return ""
+
+
_HOT_POSTS_URL = (
"https://xueqiu.com/v4/statuses/public_timeline_by_category.json"
"?since_id=-1&max_id=-1&count={limit}&category=-1"
diff --git a/tests/test_native_access_errors.py b/tests/test_native_access_errors.py
index 0c6a700a..2ef1712a 100644
--- a/tests/test_native_access_errors.py
+++ b/tests/test_native_access_errors.py
@@ -32,10 +32,18 @@ def test_github_403_requires_rate_limit_evidence(monkeypatch, headers, code):
assert exc.value.code == code
+def _http_error(code, body):
+ import io
+ return urllib.error.HTTPError("https://xueqiu.com/test", code, "x", {}, io.BytesIO(body))
+
+
@pytest.mark.parametrize("failure,code", [
(json.JSONDecodeError("invalid", "", 0), "UPSTREAM_BROKEN"),
(urllib.error.HTTPError("https://xueqiu.com/test", 403, "Forbidden", {}, None), "UPSTREAM_BROKEN"),
(urllib.error.HTTPError("https://xueqiu.com/test", 401, "Unauthorized", {}, None), "NEED_LOGIN"),
+ (_http_error(400, b'{"error_code":"400016"}'), "NEED_LOGIN"),
+ (_http_error(400, b'{"error_code":"10001"}'), "UPSTREAM_BROKEN"),
+ (_http_error(400, b"<html>"), "UPSTREAM_BROKEN"),
])
def test_xueqiu_only_confirmed_auth_failure_requests_login(monkeypatch, failure, code):
def fail(*a, **k):
@@ -56,3 +64,36 @@ def test_youtube_bot_challenge_does_not_instruct_a_login(monkeypatch):
with pytest.raises(ReachError) as exc:
youtube._run_ytdlp([], 5)
assert exc.value.code == "PLATFORM_BLOCKED"
+
+
+def test_bilibili_subtitles_without_login_ask_for_login(monkeypatch, tmp_path):
+ from agent_reach.channels import bilibili
+ monkeypatch.setattr(bilibili, "sessions_root", lambda: tmp_path)
+ monkeypatch.setattr(bilibili, "_reuse_subtitle_login", lambda timeout: None)
+ monkeypatch.setattr(bilibili, "_bvid", lambda target, timeout: "BV1")
+ monkeypatch.setattr(bilibili, "_bili_json", lambda argv, timeout: {
+ "subtitle": {"available": False}, "warnings": [{"code": "subtitle_unavailable"}]})
+ ch = bilibili.BilibiliChannel()
+ with pytest.raises(ReachError) as exc:
+ ch._cli_subtitle(target="https://www.bilibili.com/video/BV1", timeout=5)
+ assert exc.value.code == "NEED_LOGIN"
+ cred = tmp_path / "bili-cli" / ".bilibili-cli" / "credential.json"
+ cred.parent.mkdir(parents=True)
+ cred.write_text("{}")
+ with pytest.raises(ReachError) as exc:
+ ch._cli_subtitle(target="https://www.bilibili.com/video/BV1", timeout=5)
+ assert exc.value.code == "UPSTREAM_BROKEN" # logged in: a real upstream failure stays one
+
+
[email protected]("cookies,code", [([], "NEED_LOGIN"), ([{"name": "li_at"}], "UPSTREAM_BROKEN")])
+def test_opencli_failure_without_the_site_login_cookie_asks_for_login(monkeypatch, cookies, code):
+ from agent_reach.channels import _opencli_site, linkedin
+ from agent_reach.runtime import browser, cdp
+ def broken(*a, **k):
+ raise ReachError("UPSTREAM_BROKEN", "OpenCLI: Error: Text not found: Jobs")
+ monkeypatch.setattr(_opencli_site, "opencli_call", broken)
+ monkeypatch.setattr(browser, "is_running", lambda: True)
+ monkeypatch.setattr(cdp, "cookies", lambda domain: cookies)
+ with pytest.raises(ReachError) as exc:
+ linkedin.LinkedInChannel()._jobs(query="python", location=None, limit=2, timeout=5)
+ assert exc.value.code == code