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: replace reused site snapshots and keep native login independent

LohseZSai77c26168b18c
diff --git a/agent_reach/runtime/session_reuse.py b/agent_reach/runtime/session_reuse.py
index e64cda53..dd098b19 100644
--- a/agent_reach/runtime/session_reuse.py
+++ b/agent_reach/runtime/session_reuse.py
@@ -44,13 +44,25 @@ def profiles() -> list:
def use(profile: str) -> dict:
if profile == browser.bound_context() or profile not in browser.connected_contexts():
raise ReachError(NEED_SETUP, "Choose a connected local browser profile from browser profiles")
- state.mutate(lambda data: data.update(local_browser_source=profile, session_reuse={}))
+ def select(data):
+ data["local_browser_source"] = profile
+ data["session_reuse"] = {name: record for name, record in data.get("session_reuse", {}).items()
+ if record.get("blocked") or record.get("source") == "manual"}
+ state.mutate(select)
return {"source_profile": profile, "scope": "requested site only", "source_changed": False}
def _source_cookies(channel: str, domains) -> tuple[list, str]:
from agent_reach.backends.opencli import export_site_cookies
- contexts = [p["id"] for p in profiles() if not p["dedicated"]]
+ try:
+ contexts = [p["id"] for p in profiles() if not p["dedicated"]]
+ except ReachError as exc:
+ if exc.code != NEED_SETUP:
+ raise
+ # Native CLI sessions remain usable when optional OpenCLI/source
+ # discovery is absent or incompatible. Account ambiguity below still
+ # requires a choice rather than silently using another account.
+ contexts = []
preferred = state.load().get("local_browser_source")
if preferred:
contexts = [preferred] if preferred in contexts else []
@@ -89,14 +101,29 @@ def prepare(channel: str, *, force: bool = False, action_locked: bool = False) -
browser.ensure_running()
record = state.load().get("session_reuse", {}).get(channel, {})
if not force and (record.get("blocked") or record.get("source") == "manual"
- or (record.get("copied_cookies", 0) > 0 and state.age_seconds(record.get("at")) < 3600)):
+ or (record.get("cookie_snapshot") == 1 and record.get("copied_cookies", 0) > 0
+ and state.age_seconds(record.get("at")) < 3600)):
return record
cookies, source = _source_cookies(channel, site_domains(ch))
if cookies:
- if cdp.call("Storage.setCookies", {"cookies": cookies}) is None:
- raise ReachError(UPSTREAM_BROKEN, "Could not apply the local site session; the original browser is unchanged")
+ current = cdp.call("Storage.getCookies")
+ if current is None:
+ raise ReachError(UPSTREAM_BROKEN, "Could not inspect the saved site session; the original browser is unchanged")
+ previous = _cookies(current.get("cookies", []), site_domains(ch))
+ def key(cookie):
+ return cookie.get("name"), cookie.get("domain"), cookie.get("path", "/")
+ incoming = {key(c) for c in cookies}
+ # The source is a site snapshot, not an overlay onto another account.
+ # Expire destination-only cookies (including stale UI preferences),
+ # preserving all other sites and every cookie supplied by the source.
+ stale = [{**c, "value": "", "expires": 1} for c in previous if key(c) not in incoming]
+ if cdp.call("Storage.setCookies", {"cookies": stale + cookies}) is None:
+ old = {key(c) for c in previous}
+ rollback = [{**c, "value": "", "expires": 1} for c in cookies if key(c) not in old] + previous
+ cdp.call("Storage.setCookies", {"cookies": rollback})
+ raise ReachError(UPSTREAM_BROKEN, "Could not apply the local site session; saved state retained and the original browser is unchanged")
browser.save_session()
result = {"source": source, "copied_cookies": len(cookies), "at": state.now(),
- "verified": False, "source_changed": False}
+ "verified": False, "source_changed": False, "cookie_snapshot": 1}
state.mutate(lambda data: data.setdefault("session_reuse", {}).__setitem__(channel, result))
return result
diff --git a/agent_reach/skill/references/sessions.md b/agent_reach/skill/references/sessions.md
index d269dc87..b007a554 100644
--- a/agent_reach/skill/references/sessions.md
+++ b/agent_reach/skill/references/sessions.md
@@ -21,7 +21,9 @@
多个本地浏览器都可能有该站账号时:`browser profiles` 查看连接,`browser use ID` 选择来源。`login CHANNEL --import-existing` 可主动刷新该站登录,不打开登录窗口。普通调用不会反复复制;退出和删除渠道后停止自动导入,避免登录复活。
-BOSS 搜索和搜索结果的 JD 读取都走上游 CLI 的浏览器通道,共用该配置;短期职位引用只保存在本地,不返回给模型。B站只在读字幕时尝试把已有站点登录交给官方 B站 CLI,普通搜索不准备浏览器。雪球社区动作会自动同步已有登录,无需额外执行同步命令。
+复用以该站 Cookie 快照为准,清除专用浏览器中来源没有的旧 Cookie,保留其他网站。导入失败会尝试恢复原站点 Cookie,并保留已保存的会话。切换来源不会撤销已退出的渠道,也不覆盖本人明确选择的专用登录。
+
+BOSS 搜索和搜索结果的 JD 读取都走上游 CLI 的浏览器通道,共用该配置;短期职位引用只保存在本地,不返回给模型。B站只在读字幕时尝试把已有站点登录交给官方 B站 CLI,普通搜索不准备浏览器;没装 OpenCLI 时,仍可沿用保存的 B站 CLI 登录。雪球社区动作会自动同步已有登录,无需额外执行同步命令。
登录期间定期保存,无需关窗才能保存。真实页面操作和 Reach 调用重置十分钟计时。OpenCLI Bridge 随专用浏览器由计时程序管理;Reach 启动的桥随浏览器退出,用户已有桥保留。
diff --git a/docs/cloakbrowser.md b/docs/cloakbrowser.md
index 646bbe63..317b9d81 100644
--- a/docs/cloakbrowser.md
+++ b/docs/cloakbrowser.md
@@ -8,6 +8,8 @@ Queries default to headless; human login opens a window using the same profile.
Previous Reach-owned per-site snapshots are merged once without replacing newer shared cookies. Logout/removal clears the retired per-site state too. Chrome fallback is remembered; use `agent-reach browser setup --retry-cloak` after fixing the download connection.
+An imported local site's Cookie snapshot replaces that site's cookies, expiring destination-only leftovers while preserving every other site. Failed imports attempt a rollback and leave the saved snapshot intact. Selecting another source preserves explicit logout blocks and manually selected dedicated logins.
+
This candidate installs official OpenCLI 1.8.7 for a new installation; 1.8.8 is excluded because its unchanged Xiaohongshu default-filter path failed local acceptance. OpenCLI 2.x has not completed business acceptance here. Existing user installations require explicit `--allow-upgrade` before an incompatible copy is replaced. Reach does not edit OpenCLI's source files.
CloakBrowser's wrapper and browser binary have separate licenses; see [upstream binary license](https://github.com/CloakHQ/CloakBrowser/blob/main/BINARY-LICENSE.md). Do not describe the binary as MIT/open-source or promise unrestricted redistribution.
diff --git a/docs/validation/headless-session-reuse-20261021.md b/docs/validation/headless-session-reuse-20261021.md
index 38325d80..30a4f558 100644
--- a/docs/validation/headless-session-reuse-20261021.md
+++ b/docs/validation/headless-session-reuse-20261021.md
@@ -24,7 +24,7 @@
| 雪球 | 通过 | 热帖、热股;公开行情和股票搜索不准备浏览器 |
| B站 | 通过 | 首P已有字幕,所测样本返回 979 字;不代表整套多P视频 |
| Instagram | 未通过 | 搜索接口返回 404;主页接口明确返回 429,已停止并进入限流保护 |
-| LinkedIn | 正式路由未通过 | 现有 1.8.7 卡在等待 Jobs;隔离的官方 2.0.1 + CDP 搜到职位,尚未接入正式路由 |
+| LinkedIn | 部分通过 | 现有 1.8.7 正式路由已读取 4,783 字 JD;搜索卡在中文账号界面的 Jobs 等待;2.0.1 CDP 搜索仅为隔离试验 |
Instagram 的 404 不应引导反复重登,错误映射已修正。限流解除后才能继续验收。领英的新后端是否接入,需要确认后再改依赖;独立试验结果不计正式通过。
@@ -40,6 +40,16 @@ Instagram 的 404 不应引导反复重登,错误映射已修正。限流解
本轮尚未完成所有登录渠道的目标,不合并、不发布。发布门禁仍要求全部已声明动作的当前源码实测,单元测试不能代替业务结果。候选包不含账号、Cookie、Key、浏览器配置或浏览器二进制。
-本轮最终源码:`4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685`。17 个动作通过,对应 7 类登录渠道;全项目 40 个声明动作中仍有 23 个缺少本次源码的实测,所以发布门禁保持阻断。411 项单测、Ruff 和 Mypy 通过。官方 OpenCLI 1.8.7 的 2,340 个发布文件再次逐字节核对,无修改。
+本轮最终源码:`3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f`。18 个动作通过,覆盖 7 类完整渠道和领英 JD;全项目 40 个声明动作中仍有 22 个缺少本次源码的实测,所以发布门禁保持阻断。415 项单测、Ruff 和 Mypy 通过。官方 OpenCLI 1.8.7 的 2,340 个发布文件再次逐字节核对,无修改。
Instagram 在等待期结束后又做了一次低频主页验收,仍返回 HTTP 429,已重新暂停,未尝试其他账号或后端。安装后的命令已成功查询小红书;重新启动的实际进程带 headless 参数,官方扩展已连接,Cursor 配置前后校验一致。
+
+## 本轮继续核验
+
+- 领英 JD 在现有官方 1.8.7 正式路由上通过。搜索故障定位到中文账号界面与英文 Jobs 等待不匹配;试过浏览器语言设置后仍失败,已撤回该尝试,没有强制更改所有网站的界面语言,也未新增第二套后端。
+- Cookie 复用改成该站快照,清掉专用浏览器中来源没有的旧 Cookie,保留其他网站;导入失败尝试回滚并保留保存的快照。真实 CDP 测试验证旧 Cookie 消失、其他网站 Cookie 不变,测试数据已清除。
+- 更换来源保留明确退出和本人手动登录的选择,不会让已退出渠道自动重新登录。
+- 原生 B站字幕不再被可选 OpenCLI 来源发现拦住。模拟未安装 OpenCLI 的运行环境后,真实 B站 CLI 仍取到 979 字字幕;这项额外诊断不重复计入 18 个正式动作。
+- 修正两个仅在 POSIX 成立的测试断言,保留 Windows 真机业务验收待办。415 项单测、Ruff、Mypy 通过。
+
+本目标共 22 个登录相关动作,当前 18 个通过,剩余为领英搜索及 Instagram 搜索、主页、帖子。Instagram 仍受保护期约束,未因代码调整换后端或继续请求。目标保持未完成,PR 保持草稿。
diff --git a/docs/validation/release-acceptance.json b/docs/validation/release-acceptance.json
index 735cfbbb..66816213 100644
--- a/docs/validation/release-acceptance.json
+++ b/docs/validation/release-acceptance.json
@@ -1,262 +1,262 @@
{
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"results": [
{
"channel": "xiaohongshu",
"action": "search",
- "at": "2026-10-20T16:54:27Z",
+ "at": "2026-10-20T19:58:01Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 2,
"text_chars": 0,
- "elapsed_ms": 3103,
+ "elapsed_ms": 2498,
"error": null
},
{
"channel": "xiaohongshu",
"action": "read",
- "at": "2026-10-20T16:54:40Z",
+ "at": "2026-10-20T19:58:16Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 1,
"text_chars": 22,
- "elapsed_ms": 5269,
+ "elapsed_ms": 6823,
"error": null
},
{
"channel": "xiaohongshu",
"action": "comments",
- "at": "2026-10-20T16:54:58Z",
+ "at": "2026-10-20T19:58:34Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 25,
- "text_chars": 519,
- "elapsed_ms": 9435,
+ "text_chars": 523,
+ "elapsed_ms": 8982,
"error": null
},
{
"channel": "reddit",
"action": "search",
- "at": "2026-10-20T16:55:17Z",
+ "at": "2026-10-20T19:58:48Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 2,
"text_chars": 0,
- "elapsed_ms": 10631,
+ "elapsed_ms": 6538,
"error": null
},
{
"channel": "reddit",
"action": "read",
- "at": "2026-10-20T16:55:34Z",
+ "at": "2026-10-20T19:59:02Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 1,
"text_chars": 17821,
- "elapsed_ms": 9088,
+ "elapsed_ms": 5368,
"error": null
},
{
"channel": "reddit",
"action": "subreddit",
- "at": "2026-10-20T16:55:49Z",
+ "at": "2026-10-20T19:59:15Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 2,
"text_chars": 0,
- "elapsed_ms": 6758,
+ "elapsed_ms": 5134,
"error": null
},
{
"channel": "twitter",
"action": "search",
- "at": "2026-10-20T16:56:05Z",
+ "at": "2026-10-20T19:59:28Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 2,
- "text_chars": 1058,
- "elapsed_ms": 8377,
+ "text_chars": 1060,
+ "elapsed_ms": 5009,
"error": null
},
{
"channel": "twitter",
"action": "profile",
- "at": "2026-10-20T16:56:20Z",
+ "at": "2026-10-20T19:59:44Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 1,
"text_chars": 0,
- "elapsed_ms": 6619,
+ "elapsed_ms": 7478,
"error": null
},
{
"channel": "twitter",
"action": "tweets",
- "at": "2026-10-20T16:56:42Z",
+ "at": "2026-10-20T19:59:56Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 2,
- "text_chars": 759,
- "elapsed_ms": 13774,
+ "text_chars": 960,
+ "elapsed_ms": 4345,
"error": null
},
{
"channel": "twitter",
"action": "thread",
- "at": "2026-10-20T16:57:02Z",
+ "at": "2026-10-20T20:00:12Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 50,
- "text_chars": 10375,
- "elapsed_ms": 11982,
+ "text_chars": 7992,
+ "elapsed_ms": 7816,
"error": null
},
{
"channel": "facebook",
"action": "search",
- "at": "2026-10-20T16:57:21Z",
+ "at": "2026-10-20T20:00:28Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 1,
"text_chars": 22,
- "elapsed_ms": 10282,
+ "elapsed_ms": 7678,
"error": null
},
{
"channel": "facebook",
"action": "profile",
- "at": "2026-10-20T16:57:37Z",
+ "at": "2026-10-20T20:00:43Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "opencli",
"passed": true,
"item_count": 1,
"text_chars": 0,
- "elapsed_ms": 8321,
+ "elapsed_ms": 6983,
"error": null
},
{
"channel": "boss",
"action": "search",
- "at": "2026-10-20T16:57:46Z",
+ "at": "2026-10-20T20:00:53Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "boss-agent-cli",
"passed": true,
"item_count": 2,
"text_chars": 58,
- "elapsed_ms": 1220,
+ "elapsed_ms": 1128,
"error": null
},
{
"channel": "boss",
"action": "read",
- "at": "2026-10-20T16:57:55Z",
+ "at": "2026-10-20T20:01:01Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "boss-agent-cli",
"passed": true,
"item_count": 1,
"text_chars": 525,
- "elapsed_ms": 626,
+ "elapsed_ms": 565,
"error": null
},
{
"channel": "xueqiu",
"action": "hot_posts",
- "at": "2026-10-20T16:58:04Z",
+ "at": "2026-10-20T20:01:11Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "xueqiu-api",
"passed": true,
"item_count": 2,
"text_chars": 274,
- "elapsed_ms": 614,
+ "elapsed_ms": 1563,
"error": null
},
{
"channel": "xueqiu",
"action": "hot_stocks",
- "at": "2026-10-20T16:58:13Z",
+ "at": "2026-10-20T20:01:20Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "xueqiu-api",
"passed": true,
"item_count": 2,
- "text_chars": 43,
- "elapsed_ms": 547,
+ "text_chars": 41,
+ "elapsed_ms": 477,
"error": null
},
{
"channel": "bilibili",
"action": "subtitle",
- "at": "2026-10-20T16:58:22Z",
+ "at": "2026-10-20T20:01:29Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
"backend": "bili-cli",
"passed": true,
"item_count": 1,
"text_chars": 979,
- "elapsed_ms": 1654,
+ "elapsed_ms": 1261,
"error": null
},
{
- "channel": "instagram",
- "action": "profile",
- "at": "2026-10-20T19:38:06Z",
+ "channel": "linkedin",
+ "action": "read",
+ "at": "2026-10-20T20:01:56Z",
"os": "macOS",
"method": "real-cli",
- "source_sha256": "4b83ad3557945be1a47ab37790696fc2850737bf60398659fac15420b9919685",
- "backend": null,
- "passed": false,
- "item_count": 0,
- "text_chars": 0,
- "elapsed_ms": 10404,
- "error": "RATE_LIMITED"
+ "source_sha256": "3ac98c106151237cb8e4deff46d315b23411411e3779d70728c3c0bc756e338f",
+ "backend": "opencli",
+ "passed": true,
+ "item_count": 1,
+ "text_chars": 4783,
+ "elapsed_ms": 18682,
+ "error": null
}
],
"candidate": true,
"version": "1.6.0",
- "recorded_at": "2026-10-20T19:39:02Z",
+ "recorded_at": "2026-10-20T20:03:15Z",
"release_ready": false,
"pending": [
{
@@ -287,27 +287,22 @@
{
"channel": "instagram",
"action": "posts",
- "reason": "Platform returned HTTP 429; one-hour cooldown, no retries"
+ "reason": "Earlier HTTP 429; cooldown remains in effect, no new request"
},
{
"channel": "instagram",
"action": "profile",
- "reason": "Platform returned HTTP 429; one-hour cooldown, no retries"
+ "reason": "Earlier HTTP 429; cooldown remains in effect, no new request"
},
{
"channel": "instagram",
"action": "search",
- "reason": "Platform returned HTTP 429; one-hour cooldown, no retries"
+ "reason": "Earlier HTTP 429; cooldown remains in effect, no new request"
},
{
"channel": "linkedin",
"action": "jobs",
- "reason": "Official CDP trial succeeded; current route still fails, integration decision pending"
- },
- {
- "channel": "linkedin",
- "action": "read",
- "reason": "Official CDP trial succeeded; current route still fails, integration decision pending"
+ "reason": "Job search waits for English Jobs on Chinese account UI; official CDP integration decision pending"
},
{
"channel": "v2ex",
diff --git a/tests/test_boss_channel.py b/tests/test_boss_channel.py
index 3cf47a71..7d151d8e 100644
--- a/tests/test_boss_channel.py
+++ b/tests/test_boss_channel.py
@@ -22,6 +22,7 @@ def test_can_handle_matches_zhipin_hosts():
def test_native_detail_uses_browser_only_and_keeps_references_private(monkeypatch, tmp_path):
import json
+ import os
import sys
from types import SimpleNamespace
@@ -51,7 +52,8 @@ def test_native_detail_uses_browser_only_and_keeps_references_private(monkeypatc
result=boss_native.main()
assert 'private-reference' not in json.dumps(result)
path=tmp_path/'reach-job-refs.json'
- assert path.stat().st_mode & 0o077 == 0
+ if os.name != 'nt':
+ assert path.stat().st_mode & 0o077 == 0
spec.update(action='read',job_id='job')
monkeypatch.setattr(sys,'argv',['boss_native',json.dumps(spec)])
result=boss_native.main()
diff --git a/tests/test_session_onboarding.py b/tests/test_session_onboarding.py
index cd464c1b..f981b161 100644
--- a/tests/test_session_onboarding.py
+++ b/tests/test_session_onboarding.py
@@ -65,7 +65,7 @@ def test_automatic_reuse_copies_only_the_requested_site_and_reuses_cache(monkeyp
monkeypatch.setattr(cdp, 'call', lambda method, params=None: calls.append((method,params)) or {})
result=session_reuse.prepare('xiaohongshu')
assert result['copied_cookies']==1 and not result['verified'] and not result['source_changed']
- cookie=calls[0][1]['cookies'][0]
+ cookie=next(params['cookies'][0]for method,params in calls if method=='Storage.setCookies')
assert cookie['name']=='sid' and cookie['sameSite']=='Lax' and cookie['expires']==9999999999
assert session_reuse.prepare('xiaohongshu')==result
export.assert_called_once_with('daily','xiaohongshu.com')
@@ -90,6 +90,20 @@ def test_logout_blocks_automatic_session_revival(monkeypatch):
assert session_reuse.prepare('xiaohongshu')['blocked']
+def test_selecting_another_source_keeps_explicit_logout_and_manual_login(monkeypatch):
+ from agent_reach.runtime import session_reuse
+ monkeypatch.setattr(browser,'bound_context',lambda:'owned')
+ monkeypatch.setattr(browser,'connected_contexts',lambda:['daily','owned'])
+ state.mutate(lambda data:data.update(session_reuse={
+ 'xiaohongshu':{'blocked':True},'bilibili':{'source':'manual'},'reddit':{'source':'previous','copied_cookies':10}}))
+ session_reuse.use('daily')
+ saved=state.load()
+ assert saved['local_browser_source']=='daily'
+ assert saved['session_reuse']['xiaohongshu']['blocked']
+ assert saved['session_reuse']['bilibili']['source']=='manual'
+ assert 'reddit'not in saved['session_reuse']
+
+
def test_unavailable_source_keeps_saved_destination_and_does_not_open_login(monkeypatch):
from agent_reach.runtime import session_reuse
monkeypatch.setattr(browser,'ensure_running',lambda:None)
@@ -100,6 +114,48 @@ def test_unavailable_source_keeps_saved_destination_and_does_not_open_login(monk
assert result['source']=='saved-profile' and result['copied_cookies']==0
+def test_site_snapshot_expires_destination_only_cookies_and_preserves_other_sites(monkeypatch):
+ from agent_reach.runtime import session_reuse
+ monkeypatch.setattr(browser,'ensure_running',lambda:None)
+ monkeypatch.setattr(browser,'save_session',lambda:None)
+ incoming=[{'name':'sid','value':'chosen','domain':'.xiaohongshu.com','path':'/'}]
+ monkeypatch.setattr(session_reuse,'_source_cookies',lambda *a:(incoming,'daily'))
+ jar=[{'name':'old_account','value':'stale','domain':'.xiaohongshu.com','path':'/'},
+ {'name':'sid','value':'other-account','domain':'.xiaohongshu.com','path':'/'},
+ {'name':'other_site','value':'keep','domain':'.other.test','path':'/'}]
+ applied=[]
+ def call(method,params=None):
+ if method=='Storage.getCookies':
+ return {'cookies':jar}
+ if method=='Storage.setCookies':
+ applied.extend(params['cookies'])
+ return {}
+ monkeypatch.setattr(cdp,'call',call)
+ out=session_reuse.prepare('xiaohongshu')
+ assert out['cookie_snapshot']==1
+ assert any(c['name']=='old_account'and c['expires']==1 for c in applied)
+ assert not any(c['name']=='other_site'for c in applied)
+ assert not any(c['name']=='sid'and c.get('expires')==1 for c in applied)
+
+
+def test_site_import_failure_restores_previous_site_and_retains_saved_snapshot(monkeypatch):
+ from agent_reach.runtime import session_reuse
+ monkeypatch.setattr(browser,'ensure_running',lambda:None)
+ monkeypatch.setattr(browser,'save_session',lambda:pytest.fail('saved a failed import'))
+ monkeypatch.setattr(session_reuse,'_source_cookies',lambda *a:([{'name':'new','value':'incoming','domain':'.xiaohongshu.com'}],'daily'))
+ calls=[]
+ def call(method,params=None):
+ if method=='Storage.getCookies':
+ return {'cookies':[{'name':'old','value':'saved','domain':'.xiaohongshu.com'}]}
+ calls.append(params['cookies'])
+ return None if len(calls)==1 else {}
+ monkeypatch.setattr(cdp,'call',call)
+ with pytest.raises(ReachError,match='saved state retained'):
+ session_reuse.prepare('xiaohongshu')
+ assert any(c['name']=='old'and c['value']=='saved'for c in calls[1])
+ assert any(c['name']=='new'and c['expires']==1 for c in calls[1])
+
+
def test_dispatch_worker_reuses_session_without_relocking_the_calling_thread(monkeypatch):
from agent_reach.channels.xiaohongshu import XiaoHongShuChannel
from agent_reach.runtime import dispatch, run, session_reuse
@@ -113,6 +169,7 @@ def test_dispatch_worker_reuses_session_without_relocking_the_calling_thread(mon
def test_bili_reuses_only_site_cookies_through_upstream_writer_and_private_stdin(monkeypatch):
+ from pathlib import Path
from unittest.mock import Mock
from agent_reach.channels import bilibili
@@ -126,12 +183,21 @@ def test_bili_reuses_only_site_cookies_through_upstream_writer_and_private_stdin
assert 'test-secret' not in repr(args.args)
assert json.loads(args.kwargs['input_data']) == {'SESSDATA':'test-secret'}
assert 'save_credential' in args.args[0][-1]
- assert args.kwargs['env']['HOME'].endswith('/.agent-reach/sessions/bili-cli')
+ assert Path(args.kwargs['env']['HOME']) == state.root() / 'sessions' / 'bili-cli'
state.mutate(lambda data: data['session_reuse'].__setitem__('bilibili', {'blocked':True}))
bilibili._reuse_subtitle_login(10)
assert call.call_count == 1
+def test_native_bili_subtitles_do_not_require_an_optional_browser_bridge(monkeypatch):
+ from agent_reach.channels import bilibili
+ monkeypatch.setattr(browser,'connected_contexts',lambda:(_ for _ in()).throw(ReachError('NEED_SETUP','OpenCLI is not installed')))
+ monkeypatch.setattr(bilibili,'run_cmd',lambda *a,**k:pytest.fail('started optional browser integration'))
+ monkeypatch.setattr(bilibili,'_bili_json',lambda *a:{'subtitle':{'available':True,'text':'Saved native login works'},'video':{'bvid':'BV1Jgf6YvE8e'}})
+ out=bilibili.BilibiliChannel()._cli_subtitle(target='BV1Jgf6YvE8e',timeout=2)
+ assert out[0]['text']=='Saved native login works'
+
+
def test_node_warning_does_not_hide_upstream_error_and_404_is_not_login():
from agent_reach.backends.opencli import _opencli_error
warning='(node:123) Warning: experimental proxy\n'