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(xhs): fail when container restart fails

lukiod7e9ff38a2483
diff --git a/agent_reach/cli.py b/agent_reach/cli.py
index dae3b3fe..7d0cf771 100644
--- a/agent_reach/cli.py
+++ b/agent_reach/cli.py
@@ -1772,14 +1772,23 @@ def _configure_xhs_cookies(value) -> bool:
# Restart container so it reloads cookies from disk
print(" Restarting container to reload cookies...", end=" ", flush=True)
try:
- subprocess.run(
+ restart = subprocess.run(
[docker, "restart", container_name],
capture_output=True, encoding="utf-8", timeout=30,
)
+ if restart.returncode != 0:
+ detail = (
+ (restart.stderr or "").strip()[:200]
+ or f"exit {restart.returncode}"
+ )
+ print(f"\n [!] Could not restart container: {detail}")
+ print(f" Restart manually: docker restart {container_name}")
+ return False
print("done")
except Exception as e:
print(f"\n [!] Could not restart container: {e}")
print(f" Restart manually: docker restart {container_name}")
+ return False
except Exception as e:
print(f"[X] Failed to write cookies: {e}")
return False
diff --git a/tests/test_p0_cli.py b/tests/test_p0_cli.py
index 783ebd91..c64e1871 100644
--- a/tests/test_p0_cli.py
+++ b/tests/test_p0_cli.py
@@ -659,6 +659,38 @@ def test_xhs_docker_cookie_copy_always_removes_temporary_file(monkeypatch, capsy
assert "Failed to copy cookies: copy failed" in capsys.readouterr().out
+def test_xhs_docker_restart_failure_returns_failure(monkeypatch, capsys):
+ """Cookies are not active until the container successfully restarts."""
+
+ def fake_which(name):
+ return "/usr/bin/docker" if name == "docker" else None
+
+ def fake_run(args, **_kwargs):
+ if args[1] == "ps":
+ return _docker_result(args, stdout="xiaohongshu-mcp\n")
+ if args[1:3] == ["exec", "xiaohongshu-mcp"]:
+ return _docker_result(args, stdout="/app/data/cookies.json\n")
+ if args[1] == "restart":
+ return _docker_result(
+ args,
+ returncode=1,
+ stderr="no such container",
+ )
+ return _docker_result(args)
+
+ monkeypatch.setattr("shutil.which", fake_which)
+ monkeypatch.setattr(subprocess, "run", fake_run)
+
+ result = cli._configure_xhs_cookies("web_session=xhs_secret")
+
+ output = capsys.readouterr().out
+ assert result is False
+ assert "Could not restart container" in output
+ assert "no such container" in output
+ assert "Restart manually" in output
+ assert "done" not in output
+
+
def test_system_install_uses_ytdlp_first_user_config(
monkeypatch, tmp_path
):