From bdf8e1224623f3ee51ae817890e9717a5479f9b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=A8=E7=82=8E?= <635735027@qq.com> Date: Mon, 6 Apr 2026 21:44:53 +0800 Subject: [PATCH] feat: align browser callback runtime and export flows Consolidate the browser task runtime around the callback path, add safer artifact opening for Zhihu exports, and cover the new service/browser flows with focused tests and supporting docs. Co-Authored-By: Claude Sonnet 4.6 --- docs/_tmp_sgbrowser_ws_api_doc.txt | 1572 +++++++++++++++++ docs/_tmp_sgbrowser_ws_probe_transcript.md | 145 ++ .../2026-04-01-claw-ws-execution-cards.md | 425 +++++ .../2026-04-01-claw-ws-parallel-transport.md | 687 +++++++ ...2026-04-02-ws-browser-backend-auth-plan.md | 607 +++++++ .../2026-04-03-ws-browser-bridge-path-plan.md | 482 +++++ ...ser-integration-surface-correction-plan.md | 566 ++++++ ...026-04-03-ws-browser-welcome-frame-plan.md | 362 ++++ ...zhihu-release-ws-function-callback-plan.md | 564 ++++++ .../2026-04-04-zhihu-ws-submit-realignment.md | 322 ++++ ...026-04-06-service-chat-web-console-plan.md | 406 +++++ ...hihu-hotlist-post-export-auto-open-plan.md | 637 +++++++ ...26-04-02-ws-browser-backend-auth-design.md | 506 ++++++ ...026-04-03-ws-browser-bridge-path-design.md | 276 +++ ...r-integration-surface-correction-design.md | 288 +++ ...6-04-03-ws-browser-welcome-frame-design.md | 105 ++ ...4-04-zhihu-ws-submit-realignment-design.md | 182 ++ ...6-04-06-service-chat-web-console-design.md | 219 +++ ...hu-hotlist-post-export-auto-open-design.md | 373 ++++ skills-lock.json | 10 + src/agent/mod.rs | 62 +- src/agent/planner.rs | 216 --- src/agent/runtime.rs | 155 -- src/agent/task_runner.rs | 35 +- src/browser/callback_backend.rs | 120 +- src/browser/callback_host.rs | 47 +- src/compat/artifact_open.rs | 267 +++ src/compat/mod.rs | 1 + src/compat/runtime.rs | 35 +- src/compat/screen_html_export_tool.rs | 85 +- src/compat/workflow_executor.rs | 52 +- src/pipe/browser_tool.rs | 49 +- src/runtime/engine.rs | 11 - src/security/mac_policy.rs | 76 + src/security/mod.rs | 2 + tests/agent_runtime_test.rs | 352 +++- tests/browser_backend_capability_test.rs | 145 ++ tests/browser_bridge_backend_test.rs | 151 ++ tests/browser_bridge_contract_test.rs | 80 + tests/browser_script_skill_tool_test.rs | 4 +- tests/browser_tool_test.rs | 66 + tests/browser_ws_backend_test.rs | 356 ++++ tests/browser_ws_probe_test.rs | 422 +++++ tests/browser_ws_protocol_test.rs | 195 ++ tests/common/mod.rs | 1 + tests/compat_browser_tool_test.rs | 6 +- tests/compat_config_test.rs | 54 + tests/compat_openxml_office_tool_test.rs | 3 +- tests/compat_runtime_test.rs | 769 ++++---- tests/compat_screen_html_export_tool_test.rs | 9 + tests/pipe_protocol_test.rs | 28 + tests/planner_test.rs | 132 -- tests/service_task_flow_test.rs | 922 ++++++++++ tests/service_ws_session_test.rs | 1380 +++++++++++++++ tests/task_runner_test.rs | 471 +++++ 55 files changed, 14440 insertions(+), 1053 deletions(-) create mode 100644 docs/_tmp_sgbrowser_ws_api_doc.txt create mode 100644 docs/_tmp_sgbrowser_ws_probe_transcript.md create mode 100644 docs/superpowers/plans/2026-04-01-claw-ws-execution-cards.md create mode 100644 docs/superpowers/plans/2026-04-01-claw-ws-parallel-transport.md create mode 100644 docs/superpowers/plans/2026-04-02-ws-browser-backend-auth-plan.md create mode 100644 docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md create mode 100644 docs/superpowers/plans/2026-04-03-ws-browser-integration-surface-correction-plan.md create mode 100644 docs/superpowers/plans/2026-04-03-ws-browser-welcome-frame-plan.md create mode 100644 docs/superpowers/plans/2026-04-03-zhihu-release-ws-function-callback-plan.md create mode 100644 docs/superpowers/plans/2026-04-04-zhihu-ws-submit-realignment.md create mode 100644 docs/superpowers/plans/2026-04-06-service-chat-web-console-plan.md create mode 100644 docs/superpowers/plans/2026-04-06-zhihu-hotlist-post-export-auto-open-plan.md create mode 100644 docs/superpowers/specs/2026-04-02-ws-browser-backend-auth-design.md create mode 100644 docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md create mode 100644 docs/superpowers/specs/2026-04-03-ws-browser-integration-surface-correction-design.md create mode 100644 docs/superpowers/specs/2026-04-03-ws-browser-welcome-frame-design.md create mode 100644 docs/superpowers/specs/2026-04-04-zhihu-ws-submit-realignment-design.md create mode 100644 docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md create mode 100644 docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md create mode 100644 skills-lock.json delete mode 100644 src/agent/planner.rs delete mode 100644 src/agent/runtime.rs create mode 100644 src/compat/artifact_open.rs create mode 100644 tests/browser_backend_capability_test.rs create mode 100644 tests/browser_bridge_backend_test.rs create mode 100644 tests/browser_bridge_contract_test.rs create mode 100644 tests/browser_ws_backend_test.rs create mode 100644 tests/browser_ws_probe_test.rs create mode 100644 tests/browser_ws_protocol_test.rs delete mode 100644 tests/planner_test.rs create mode 100644 tests/service_task_flow_test.rs create mode 100644 tests/service_ws_session_test.rs create mode 100644 tests/task_runner_test.rs diff --git a/docs/_tmp_sgbrowser_ws_api_doc.txt b/docs/_tmp_sgbrowser_ws_api_doc.txt new file mode 100644 index 0000000..859d0a6 --- /dev/null +++ b/docs/_tmp_sgbrowser_ws_api_doc.txt @@ -0,0 +1,1572 @@ +多核浏览器管道API接口文档 +一体化平台项目组 +2026年3月 +目录 +一、API接口概述4 +二、API接口定义4 +1. 页面加载完成后执行回调接口4 +2. 隐藏域页面加载完成后执行回调接口4 +3. 执行ajax请求接口(第3种方法)5 +4. 指定域执行JS代码接口6 +5. 显示文本消息接口6 +6. 在隐藏域打开网页接口6 +7. 打开新页面接口7 +8. 在新页签打开第一个url并等待加载完成后打开第二个url接口(修改)7 +9. 使用指定URL打开弹出窗口接口8 +10. 设置标题栏样式接口9 +11. 设置左侧菜单栏样式接口9 +12. 切换隐藏域页面内核接口9 +13. 切换页面全屏状态接口10 +14. 获取浏览器版本号接口10 +15. 关闭文本消息窗接口11 +16. 关闭网页弹窗接口11 +17. 刷新主界面的指定页面接口11 +18. 刷新隐藏域的指定页面接口12 +19. 刷新指定的网页弹窗接口12 +20. 获取本地服务路径接口12 +21. 获取激活辅助服务窗口的页面URL接口13 +22. 模拟鼠标事件接口13 +23. 模拟键盘字符输入接口14 +24. 模拟键盘功能键输入接口14 +25. 指定domain网页执行JS代码接口15 +26. 获取主界面指定页面的内核类型接口15 +27. 获取隐藏域指定页面的内核类型接口16 +28. 设置浏览器主题接口16 +29. 获取当前浏览器的主题编号接口17 +30. 获取用户闲置状态接口17 +31. 打开智能体界面接口17 +32. 关闭智能体界面接口18 +33. 设置智能体界面宽度接口18 +34. 获取当前激活标签页的URL接口18 +35. 设置用户鉴权信息接口19 +36. 打开左侧菜单栏接口19 +37. 隐藏左侧菜单栏接口20 +38. 执行ajax 请求20 +39. 执行ajax 请求 (方式二)20 +40. JS 回调函数21 +41. 执行JS 代码22 +42. 注入JS 方法22 +43. 执行JS 方法22 +44. 执行sql 语句23 +45. 登录初始化23 +46. 退出登录24 +47. 设置缓存数据24 +48. 获取缓存数据25 +49. 在新页签打开URL,等第一个url 加载完成后,加载第二个url25 +50. 获取所有已经打开的url 地址25 +51. 打开本地应用26 +52. 激活页签26 +53. 打开消息框26 +54. 关闭消息框27 +55. 消息框隐藏27 +56. 消息框显示27 +57. 打开新页面28 +58. 打开用户信息页面28 +59. 隐藏用户信息页面28 +60. 显示用户信息页面29 +61. 设置登录后用户显示图标和名称29 +62. 设置浏览器背景色29 +63. 设置登录后浏览器 Logo 图标30 +64. 关闭显示的页面30 +65. 浏览器最大化30 +66. 在隐藏域打开网页31 +67. 获取隐藏域所有已经打开的 url31 +68. 关闭隐藏域网址31 +69. 改变消息框大小32 +70. 设置浏览器模式32 +71. 设置顶部标题颜色32 +72. 设置左侧菜单栏颜色33 +73. 修改已设置菜单属性33 +74. 新增菜单34 +75. 删除菜单34 +三、API接口返回值34 +一、API接口概述 +本文档包含的内容是最新管道通信模式下的API接口说明。 +API函数是多核浏览器在内核上实现的内置上下文接口,本文档是针对通过websocket通道方式调用的说明文档,用于替代Web页面在JavaScript中通过函数名称和参数调用执行,部分内置函数可以实现跨核心类型的页面注入和执行JS等操作,请查阅函数的详细说明。 +目前浏览器的通信服务地址是:ws://localhost:12345,客户端调用接口必须把作为请求的页面url(requesturl)和调用接口名称及参数以json对象方式组合成json字符串作为消息发送的通道服务端作为传递方式。[requesturl,"sgBrowerserGetUrls", callback] +举例说明,比如浏览器上当前打开的页面url是http://www.baidu.com/test.html, test.html页面需要获取浏览器上所有页面的url,并以函数GetAllUrls(urls)来获取所有的页面url,查看接口列表可知第50个接口sgBrowerserGetUrls能满足这个需求,通过管道访问时,先连接到服务端“ws://localhost:12345”,根据接口【管道消息】所描述格式,要发送的消息是: ["http://www.baidu.com/test.html","sgBrowerserGetUrls", "GetAllUrls"] +二、API接口定义 +1. 页面加载完成后执行回调接口 +接口名称 +页面加载完成后执行回调接口 +接口描述 +监测指定URL页面加载完成后,执行当前页面中的JS函数,自4.1.0/4.0.42开始可用 +接口地址 +sgBrowserCallAfterLoaded(targetUrl, callback) +管道消息 +[requesturl,"sgBrowserCallAfterLoaded", targetUrl, callback] +传输参数 +targetUrl: (不能为空)需要打开并监测的URL地址;callback: (不能为空)指定URL加载完成后需要执行的JS函数,包含括号和参数 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +2. 隐藏域页面加载完成后执行回调接口 +接口名称 +隐藏域页面加载完成后执行回调接口 +接口描述 +监测隐藏域中指定URL页面加载完成后,执行当前页面中的JS函数,自4.1.0/4.0.42开始可用 +接口地址 +sgHideBrowserCallAfterLoaded(targetUrl, callback) +管道消息 +[requesturl,"sgHideBrowserCallAfterLoaded", targetUrl, callback] +传输参数 +targetUrl: (不能为空)需要打开并监测的隐藏域URL地址;callback: (不能为空)指定URL加载完成后需要执行的JS函数,包含括号和参数 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +3. 执行ajax请求接口(第3种方法) +接口名称 +执行ajax请求接口(第3种方法) +接口描述 +根据传入的参数组合成ajax请求并执行,自4.1.7/4.0.51开始可用 +接口地址 +sgBrowerserJsAjax3(callback,targeturl,actionurl,requesttype,requestHeader,requestparams) +管道消息 +[requesturl,"sgBrowerserJsAjax3", callback,targeturl,actionurl,requesttype,requestHeader,requestparams] +传输参数 +callback:(不能为空)请求完成后的回调函数名称,请求结果以参数返回,callback(targeturl,actionurl,responseTxt) +targeturl:(不能为空)被操作网页地址 +actionurl:(不能为空)调用接口url +requesttype:(不能为空)POST、GET +requestHeader:(不能为空)请求header,一个json对象,包含所有的属性名称和值 +requestparams:(可为空)调用接口传入参数 +约束条件 +1.参数数量必须相符; +2.指定参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +4. 指定域执行JS代码接口 +接口名称 +在指定域中执行JS代码接口,自4.1.0/4.0.45开始可用,`agent`参数自4.1.25/4.0.71开始可用 +接口描述 +根据参数指示在主界面还是隐藏域中匹配制定的URL来执行JS代码 +接口地址 +sgBrowserExcuteJsCodeByArea(targetUrl, jsCode, area) +管道消息 +[requesturl,"sgBrowserExcuteJsCodeByArea", targetUrl, jsCode, area] +传输参数 +targeturl:(不能为空)注入并执行JS代码的目标页面地址 +jsCode:(不能为空)要注入并执行的JS代码 +area:(不能为空)指示执行的域,`show`表示在主界面中匹配,`hide`表示在隐藏域中匹配,`agent`表示指定智能体页面 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +5. 显示文本消息接口 +接口名称 +显示文本消息接口 +接口描述 +根据参数显示指定的文本消息弹窗,支持多个弹窗显示,自4.1.4/4.0.48开始可用,自4.1.10/4.0.55开始可以使用后2个参数 +接口地址 +sgBrowserShowTextMessage (message, msgId, seconds) +管道消息 +[requesturl,"sgBrowserShowTextMessage", message, msgId, seconds] +传输参数 +message: (不能为空)文本消息内容 +msgId: (非必传)此消息的唯一标识 +seconds: (非必传)此消息显示的时长 +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +6. 在隐藏域打开网页接口 +接口名称 +在隐藏域打开网页接口 +接口描述 +在隐藏域打开指定的URL地址,并通过参数指定使用的内核,自4.1.5/4.0.49开始可用 +接口地址 +sgHideBrowerserOpenPage(url, core) +管道消息 +[requesturl,"sgHideBrowerserOpenPage", url, core] +传输参数 +url:(不能为空)要打开的页面地址 +core:(非必传)要使用的内核类型,取值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +7. 打开新页面接口 +接口名称 +打开新网页接口 +接口描述 +在隐藏域打开指定的URL地址,并通过参数指定使用的内核,自4.1.5/4.0.49开始可用 +接口地址 +sgBrowerserOpenPage(url, core) +管道消息 +[requesturl,"sgBrowerserOpenPage", url, core] +传输参数 +url:(不能为空)要打开的页面地址 +core:(非必传)要使用的内核类型,取值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +8. 在新页签打开第一个url并等待加载完成后打开第二个url接口(修改) +接口名称 +在新页签打开第一个url并等待加载完成后打开第二个url接口 +接口描述 +在新页签打开第一个url并等待加载完成后打开第二个url,自4.1.5/4.0.49开始可用 +接口地址 +sgBrowerserOpen(url1, url2, core) +管道消息 +[requesturl,"sgBrowerserOpen", url1, url2, core] +传输参数 +url1:(不能为空)要打开的第一个页面地址 +url2:(不能为空)url1加载完成后在当前页签打开的地址 +core:(非必传)要使用的内核类型,取值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +9. 使用指定URL打开弹出窗口接口 +接口名称 +使用指定URL打开弹出窗口接口 +接口描述 +使用指定URL打开弹出窗口,并通过参数指定位置、大小、回调函数,4.1.5/4.0.49扩展了了参数core,4.1.29/4.0.75扩展了参数title +接口地址 +sgBrowerserDialog(x,y,width,height,url,callbackFun, core, title) +管道消息 +[requesturl,"sgBrowerserDialog", x,y,width,height,url,callbackFun, core, title] +传输参数 +x: (不能为空)窗口x轴坐标 +y: (不能为空)窗口y轴坐标 +width: (不能为空)窗口宽度 +height: (不能为空)窗口高度 +url:(不能为空)要打开的页面地址 +callbackFun: (不能为空)回调函数名称 +core:(非必传)要使用的内核类型,取值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +title: (非必传)指定弹窗的标题内容 +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +10. 设置标题栏样式接口 +接口名称 +设置标题栏样式接口 +接口描述 +设置浏览器顶部标题栏的背景色和文字颜色,自4.1.5/4.0.49开始可用 +接口地址 +sgBroewserSetTopcolor(bgColor, fontColor) +管道消息 +[requesturl,"sgBroewserSetTopcolor", bgColor, fontColor] +传输参数 +bgColor:(不能为空)背景色值,16进制格式:"#ffffff" +fontColor:(非必传)文字颜色值,16进制格式:"#ffffff" +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +11. 设置左侧菜单栏样式接口 +接口名称 +设置左侧菜单栏样式接口 +接口描述 +设置浏览器左侧菜单栏的背景色和文字颜色,自4.1.5/4.0.49开始可用 +接口地址 +sgBroewserSetLeftcolor(bgColor, fontColor) +管道消息 +[requesturl,"sgBroewserSetLeftcolor", bgColor, fontColor] +传输参数 +bgColor:(不能为空)背景色值,16进制格式:"#ffffff" +fontColor:(非必传)文字颜色值,16进制格式:"#ffffff" +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +12. 切换隐藏域页面内核接口 +接口名称 +切换隐藏域页面内核接口 +接口描述 +将隐藏域中指定URL地址的页面通过参数切换为指定的内核,自4.1.6/4.0.50开始可用 +接口地址 +sgHideBrowerserChangeCore(url, core) +管道消息 +[requesturl, "sgHideBrowerserChangeCore", url, core] +传输参数 +url:(不能为空)要执行切换内核的页面地址 +core:(非必传)要使用的内核类型,取值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +13. 切换页面全屏状态接口 +接口名称 +切换页面全屏状态接口 +接口描述 +根据参数指定当前页面的全屏状态,自4.1.8/4.0.52开始可用 +接口地址 +sgBrowserShowFullScreen(isFullScreen) +管道消息 +[requesturl,"sgBrowserShowFullScreen", isFullScreen] +传输参数 +isFullScreen: (不能为空)布尔值,true或false +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +14. 获取浏览器版本号接口 +接口名称 +获取浏览器版本号接口 +接口描述 +获取浏览器版本号,并通过传入的回调函数返回,自4.1.8/4.0.52开始可用 +接口地址 +sgGetBrowserVersion(callback) +管道消息 +[requesturl,"sgGetBrowserVersion", callback] +传输参数 +callback: (不能为空)回调函数名称,通过其参数返回版本号的值,示例: +callback(version) { console.log(version) } +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +15. 关闭文本消息窗接口 +接口名称 +关闭文本消息窗接口 +接口描述 +根据参数匹配文本消息窗的唯一标识并关闭该弹窗,自4.1.10/4.0.55开始可用 +接口地址 +sgBrowserCloseTextMessage(msgId) +管道消息 +[requesturl,"sgBrowserCloseTextMessage", msgId] +传输参数 +msgId: (不能为空)此消息的唯一标识 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +16. 关闭网页弹窗接口 +接口名称 +关闭网页弹窗接口 +接口描述 +根据参数传入的窗口id关闭指定的网页弹窗,自4.1.10/4.0.55开始可用 +接口地址 +sgBrowerserDialogClose(dialogId) +管道消息 +[requesturl,"sgBrowerserDialogClose", dialogId] +传输参数 +dialogId: (不能为空)网页弹窗的窗口id +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +17. 刷新主界面的指定页面接口 +接口名称 +刷新主界面的指定页面接口 +接口描述 +根据参数传入的url查找页面并执行刷新,自4.1.10/4.0.55开始可用 +接口地址 +sgBrowserRefreshPage(url) +管道消息 +[requesturl,"sgBrowserRefreshPage", url] +传输参数 +url: (不能为空)指定网页的url地址 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +18. 刷新隐藏域的指定页面接口 +接口名称 +刷新隐藏域的指定页面接口 +接口描述 +根据参数传入的url查找页面并执行刷新,自4.1.10/4.0.55开始可用 +接口地址 +sgHideBrowserRefreshPage(url) +管道消息 +[requesturl,"sgHideBrowserRefreshPage", url] +传输参数 +url: (不能为空)指定网页的url地址 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +19. 刷新指定的网页弹窗接口 +接口名称 +刷新指定的网页弹窗接口 +接口描述 +根据参数传入的窗口id刷新指定的网页弹窗,自4.1.10/4.0.55开始可用 +接口地址 +sgBrowserDialogRefresh(dialogId) +管道消息 +[requesturl,"sgBrowserDialogRefresh", dialogId] +传输参数 +dialogId: (不能为空)网页弹窗的窗口id +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +20. 获取本地服务路径接口 +接口名称 +获取本地服务路径接口 +接口描述 +通过参数传入回调函数的名称,将本地服务的路径通过回调函数的参数返回给前端,自4.1.11/4.0.56开始可用 +接口地址 +sgGetLocalServicePath(callBack) +管道消息 +[requesturl,"sgGetLocalServicePath", callBack] +传输参数 +callBack: (不能为空)回调函数的名称 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +21. 获取激活辅助服务窗口的页面URL接口 +接口名称 +获取激活辅助服务窗口的页面URL接口 +接口描述 +通过参数传入回调函数的名称,将激活辅助服务窗口的页面URL地址通过回调函数的参数返回给前端,自4.1.12/4.0.57开始可用 +接口地址 +sgGetActiveUrl(callBack) +管道消息 +[requesturl,"sgGetActiveUrl", callBack] +传输参数 +callBack: (不能为空)回调函数的名称 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +22. 模拟鼠标事件接口 +接口名称 +模拟鼠标事件 +接口描述 +在浏览器主窗体(非屏幕)坐标处,模拟鼠标事件(自4.1.15/4.0.60开始可用) +接口地址 +sgBroewserSimulateMouse(x, y, eventType, deltaX, deltay) +管道消息 +[requesturl,"sgBroewserSimulateMouse", x, y, eventType, deltaX, deltay] +传输参数 +x 表示html页面元素(如button)横坐标,不能为空 +y 表示html页面元素(如button)纵坐标,不能为空 +eventType 表示鼠标事件类型,不能为空; +left 鼠标左键单击事件 +wheel 鼠标滚轮事件 +deltaX 表示X方向上的移动增量,事件类型为鼠标左键单击事件时,可以为空;事件类型为滚轮事件时,不能为空 +deltay表示Y方向上的移动增量,事件类型为鼠标左键单击事件时,可以为空;事件类型为滚轮事件时,不能为空 +约束条件 +1.参数数量必须相符; +2.必传参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +23. 模拟键盘字符输入接口 +接口名称 +模拟键盘字符输入 +接口描述 +在浏览器主窗体(非屏幕)坐标处,模拟键盘字符输入(自4.1.15/4.0.60开始可用) +接口地址 +sgBroewserSimulateKeyborad (x, y, txtStr) +管道消息 +[requesturl,"sgBroewserSimulateKeyborad", x, y, txtStr] +传输参数 +x 表示html页面元素(如文本框)横坐标,不能为空 +y 表示html页面元素(如文本框)纵坐标,不能为空 +txtStr: (不能为空)文本消息内容(内容长度不能超过100) +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +24. 模拟键盘功能键输入接口 +接口名称 +模拟键盘功能键 +接口描述 +在浏览器主窗体(非屏幕)坐标处,模拟键盘功能键输入(自4.1.15/4.0.60开始可用) +接口地址 +sgBroewserSimulateKeyboradFunkeys (eventType) +管道消息 +[requesturl,"sgBroewserSimulateKeyboradFunkeys", eventType] +传输参数 +eventType 表示键盘功能键类型,不能为空; +UpArrow方向键上 +DownArrow方向键下 +LeftArrow 方向键左 +RightArrow 方向键右 +Tab Tab键 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +25. 指定domain网页执行JS代码接口 +接口名称 +在指定domain中执行JS代码接口(自4.1.17/4.0.63开始可用) +接口描述 +根据参数指示在主界面或隐藏域,网页url包含指定domain的页面,注入并执行JS代码 +接口地址 +sgBrowserExcuteJsCodeByDomain(domain, jsCode, area) +管道消息 +[requesturl,"sgBrowserExcuteJsCodeByDomain",domain, jsCode, area] +传输参数 +domain:(不能为空) 被注入并执行JS代码网页url的domain +jsCode:(不能为空)要注入并执行的JS代码 +area:(不能为空)指示执行的域,`show`表示在主界面中匹配,`hide`表示在隐藏域中匹配,`all`表示在主界面和隐藏域中匹配 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +26. 获取主界面指定页面的内核类型接口 +接口名称 +获取主界面指定页面的内核类型接口 +接口描述 +获取主界面中指定URL地址的页面的内核类型,自4.1.19/4.0.65开始可用 +接口地址 +sgBrowserGetCoreType (url, callback) +管道消息 +[requesturl,"sgBrowserGetCoreType",url, callback] +传输参数 +url:(不能为空)要获取的页面地址 +callback:(不能为空)回调函数的名称,返回值以回调函数参数形式返回,返回值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +27. 获取隐藏域指定页面的内核类型接口 +接口名称 +获取隐藏域指定页面的内核类型接口 +接口描述 +获取隐藏域中指定URL地址的页面的内核类型,自4.1.19/4.0.65开始可用 +接口地址 +sgHideBrowserGetCoreType (url, callback) +管道消息 +[requesturl,"sgHideBrowserGetCoreType",url, callback] +传输参数 +url:(不能为空)要获取的页面地址 +callback:(不能为空)回调函数的名称,返回值以回调函数参数形式返回,返回值范围:["chromium","ie11","ie10","ie9","ie8","ie7","ie6"] +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +28. 设置浏览器主题接口 +接口名称 +设置浏览器主题接口 +接口描述 +根据参数传入的主题id设置浏览器的主题样式,自4.1.23/4.0.69开始可用 +接口地址 +sgBrowserSetTheme (theme_id) +管道消息 +[requesturl,"sgBrowserSetTheme",theme_id] +传输参数 +theme_id: (不能为空)浏览器主题id,取值范围: +0: 绿色,1: 金色,2: 蓝色,3: 红色 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +29. 获取当前浏览器的主题编号接口 +接口名称 +获取当前浏览器的主题编号接口 +接口描述 +通过参数传入回调函数的名称,将当前浏览器的主题id通过回调函数的参数返回给前端,自4.1.23/4.0.69开始可用 +接口地址 +sgBrowserGetTheme (callBack) +管道消息 +[requesturl,"sgBrowerserJsAjax3", callBack] +传输参数 +callBack: (不能为空)回调函数的名称,主题id取值范围: +0: 绿色,1: 金色,2: 蓝色,3: 红色 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +30. 获取用户闲置状态接口 +接口名称 +获取用户闲置状态接口 +接口描述 +通过参数传入回调函数的名称,将当前用户的闲置状态通过回调函数的参数返回给前端,自4.1.23/4.0.69开始可用 +接口地址 +sgGetUserIdleState (callBack) +管道消息 +[requesturl,"sgGetUserIdleState", callBack] +传输参数 +callBack: (不能为空)回调函数的名称,返回闲置状态值范围: +"true": 用户处于闲置状态,"false": 用户处于非闲置状态 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +31. 打开智能体界面接口 +接口名称 +打开智能体界面接口 +接口描述 +打开浏览器智能体界面,可选参数传入指定的智能体url,不传时使用默认配置地址,传入则会覆盖默认配置,自4.1.25/4.0.71开始可用 +接口地址 +sgOpenAgent (url) +管道消息 +[requesturl,"sgOpenAgent", url] +传输参数 +url: (可选参数)智能体页面的url地址 +约束条件 +1.参数数量必须相符; +2.可选不传参,或传入url,传参时参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +32. 关闭智能体界面接口 +接口名称 +关闭智能体界面接口 +接口描述 +关闭浏览器智能体界面,自4.1.25/4.0.71开始可用 +接口地址 +sgCloseAgent () +管道消息 +[requesturl,"sgCloseAgent"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +33. 设置智能体界面宽度接口 +接口名称 +设置智能体界面宽度接口 +接口描述 +设置浏览器智能体界面宽度,参数传入宽度值,自4.1.25/4.0.71开始可用 +接口地址 +sgSetAgentPageWidth (width) +管道消息 +[requesturl,"sgSetAgentPageWidth", width] +传输参数 +width: (不能为空)智能体页面的宽度 +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +34. 获取当前激活标签页的URL接口 +接口名称 +获取当前激活标签页的URL接口 +接口描述 +通过参数传入回调函数的名称,将当前激活标签页的URL通过回调函数的参数返回给前端,自4.1.26/4.0.72开始可用 +接口地址 +sgGetActiveTabUrl (callBack) +管道消息 +[requesturl,"sgGetActiveTabUrl", callBack] +传输参数 +callBack: (不能为空)回调函数的名称,返回当前激活标签页的URL +约束条件 +1.参数数量必须相符; +2.参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +35. 设置用户鉴权信息接口 +接口名称 +设置用户鉴权信息接口 +接口描述 +前端通过参数传入用户鉴权信息包括ID和token,自4.1.26/4.0.72开始可用 +接口地址 +sgSetAuthInfo (user_id, token) +管道消息 +[requesturl,"sgSetAuthInfo", user_id, token] +传输参数 +user_id:(不能为空)用户鉴权id +token:(不能为空)用户鉴权的token值 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +36. 打开左侧菜单栏接口 +接口名称 +打开左侧菜单栏接口 +接口描述 +打开左侧菜单栏(如果可用),自4.1.29/4.0.75开始可用 +接口地址 +sgBrowserShowLeftMenu () +管道消息 +[requesturl,"sgBrowserShowLeftMenu"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +37. 隐藏左侧菜单栏接口 +接口名称 +隐藏左侧菜单栏接口 +接口描述 +隐藏左侧菜单栏(如果可用),自4.1.29/4.0.75开始可用 +接口地址 +sgBrowserHideLeftMenu () +管道消息 +[requesturl,"sgBrowserHideLeftMenu"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +38. 执行ajax 请求 +接口名称 +执行ajax 请求 +接口描述 +执行ajax 请求 +接口地址 +sgBrowerserJsAjax(callback, targeturl, actionurl, requesttype, contentype, requestparams, accept, xRequestedwith) +管道消息 +[requesturl,"sgBrowerserJsAjax", callback, targeturl, actionurl, requesttype, contentype, requestparams, accept, xRequestedwith] +传输参数 +callback:不能为空。请求完成后的回调函数名称,请求结果以参数返回。 +targeturl:不能为空。被操作网页地址。 +actionurl:不能为空。调用接口URL。 +requesttype:不能为空。请求类型(如POST、GET)。 +contentype:可为空。请求中的Content-Type。 +requestparams:可为空。调用接口传入参数。 +accept:可为空。请求中的Accept。 +xRequestedwith:可为空。请求中的X-Requested-With。 +约束条件 +1.参数数量必须相符; +2.指定参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +39. 执行ajax 请求 (方式二) +接口名称 +执行ajax 请求 (方式二) +接口描述 +执行ajax 请求 (方式二) +接口地址 +sgBrowerserJsAjax2(callback, targeturl, actionurl, requesttype, requestHeader, requestparams) +管道消息 +[requesturl,"sgBrowerserJsAjax3"] +传输参数 +callback:(不能为空) 请求完成后的回调函数名称,请求结果以参数返回,callback(targeturl, actionurl, responseTxt)。 +targeturl:(不能为空) 被操作网页地址。 +actionurl:(不能为空) 调用接口url。 +requesttype:(不能为空) POST、GET。 +requestHeader:(可为空) 请求header,用分号隔开,如。 +requestparams:(可为空) 调用接口传入参数。 +约束条件 +1.参数数量必须相符; +2.指定参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +40. JS 回调函数 +接口名称 +JS 回调函数 +接口描述 +可通过此函数,返回执行js的结果(主要用于目标 URL 执行完成后返回结果)。执行actionurl后将值responseTxt返回给sourceUrl页面的callback函数 +接口地址 +callBackJsToCpp(param) +管道消息 +[requesturl,"callBackJsToCpp",param] +传输参数 +参数必须按格式 sourceUrl@_@targetUrl@_@callback@_@actionUrl @_@responseTxt,不能缺省。 +代码示例: +// 请求 25.215.192.15 中的某个接口的返回: +var xhr = new XMLHttpRequest(); +xhr.open('GET', 'http://25.215.192.15/api/getRoleId'); +xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); +xhr.onreadystatechange = function() { +if (xhr.readyState === 4) { +var textRes = xhr.responseText; +callBackJsToCpp("http://127.0.0.1/b.html@_@http://25.215.192.15/f1-szhxgs-main@_@callBackMethod@_@http://25.215.192.15/api/getRoleId@_@" + textRes); +} +} +xhr.send(); +约束条件 +参数必须符合指定格式 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +41. 执行JS 代码 +接口名称 +执行JS 代码 +接口描述 +执行JS 代码 +接口地址 +sgBrowserExcuteJsCode(targeturl, jsCode) +管道消息 +[requesturl,"sgBrowserExcuteJsCode", targeturl, jsCode] +传输参数 +targeturl:不能为空。被操作网页地址。 +jsCode:不能为空。js 代码 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +42. 注入JS 方法 +接口名称 +注入JS 方法 +接口描述 +注入JS 方法 +接口地址 +sgBrowserRegJsFun(targeturl, funContent) +管道消息 +[requesturl,"sgBrowserRegJsFun", targeturl, funContent] +传输参数 +targeturl:不能为空。被操作网页地址。 +funContent:不能为空。方法内容。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +43. 执行JS 方法 +接口名称 +执行JS 方法 +接口描述 +执行JS 方法 +接口地址 +sgBrowserExcuteJsFun(targeturl, funName) +管道消息 +[requesturl,"sgBrowserExcuteJsFun", targeturl, funName] +传输参数 +targeturl:不能为空。被操作网页地址。 +funName:不能为空。方法名称,不带括号。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +44. 执行sql 语句 +接口名称 +执行sql 语句 +接口描述 +执行sql 语句 +接口地址 +sgBrowserJsSql(callback, sqlStr) +管道消息 +[requesturl,"sgBrowserJsSql", callback, sqlStr] +传输参数 +callback:回调函数,执行结果通过回调返回。 +sqlStr:不能为空。SQL语句。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +45. 登录初始化 +接口名称 +登录初始化 +接口描述 +登录初始化 +接口地址 +sgBrowserLogin(obj) +管道消息 +[requesturl,"sgBrowserLogin",obj] +传输参数 +Obj为json对象 +obj 示例: +{ +appName: '*** 一体化平台 ', +userName: ' 张三 ', +orgName: '*** 供电所 ', +menus: [ +{ +name: ' 我的应用 ', +normalImg: 'AAAAAYlQTkcNChoKA........', // 正常情况下 Base64 编码的图片 +activeImg: 'qYOjo29sh7178BXiB........', // 选中状态图片 +url: 'http://www.baidu.com' +}, +{ +name: ' 我的报表 ', +normalImg: 'AAAAAYlQTkcNChoKA........', +activeImg: 'qYOjo29sh7178BXiB........', +url: 'http://www.qq.com' +} +// ... +] +} +约束条件 +Obj对象格式是系统内部提供的,如示例 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +46. 退出登录 +接口名称 +退出登录 +接口描述 +退出登录 +接口地址 +sgBrowerserLogout() +管道消息 +[requesturl,"sgBrowerserLogout"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +47. 设置缓存数据 +接口名称 +设置缓存数据 +接口描述 +浏览器设置本地数据,需要更新时重新调用此方法即可。 +接口地址 +sgBrowerserSetStorage(key, val) +管道消息 +[requesturl,"sgBrowerserJsAjax3"] +传输参数 +key:键值。 +val:数据。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +48. 获取缓存数据 +接口名称 +获取缓存数据 +接口描述 +获取缓存数据 +接口地址 +sgBrowerserGetStorage(key, callback) +管道消息 +[requesturl,"sgBrowerserGetStorage", key, callback] +传输参数 +key:键值。 +callback:获取值通过此函数回调,包含两个参数。 +function getCallBack(key, val) { +console.log(key, val); +document.getElementById("getVal").value = val; +} +function getTest() { +var key = document.getElementById("getKey").value; +sgBrowerserGetStorage(key, "getCallBack"); +} +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +49. 在新页签打开URL,等第一个url 加载完成后,加载第二个url +接口名称 +在新页签打开URL,等第一个url 加载完成后,加载第二个url +接口描述 +在新页签打开URL,等第一个url 加载完成后,加载第二个url +接口地址 +sgBrowerserOpen(url1, url2) +管道消息 +[requesturl,"sgBrowerserOpen", url1, url2] +传输参数 +url1:先打开。 +url2:加载完成后在当前页签打开。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +50. 获取所有已经打开的url 地址 +接口名称 +获取所有已经打开的url 地址 +接口描述 +获取所有已经打开的url 地址 +接口地址 +sgBrowerserGetUrls(callback) +管道消息 +[requesturl,"sgBrowerserGetUrls", callback] +传输参数 +callback:获取值通过此函数回调,包含一个参数,其中urls是通过分号隔开的字符串。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +51. 打开本地应用 +接口名称 +打开本地应用 +接口描述 +打开本地应用 +接口地址 +sgBrowerserOpenEXE(pathExe) +管道消息 +[requesturl,"sgBrowerserOpenEXE", pathExe] +传输参数 +pathExe:本地应用绝对地址。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +52. 激活页签 +接口名称 +激活页签 +接口描述 +激活页签 +接口地址 +sgBrowerserActiveTab(url, callback) +管道消息 +[requesturl,"sgBrowerserActiveTab", url, callback] +传输参数 +url:页签地址全路径,此处是全路径匹配才会激活页签,如果未发现匹配项,不做任何处理。 +callback:激活后的回调,callBackFun(isActive)参数为激活结果,找到标签为1,未找到未0 +代码示例: +function callBackFun(isActive){ +//isActive 找到标签为 1 ,未找到为 0 +console.log(isActive); +} +function acticeTabTest(){ +var url = "https://www.baidu.com"; +var res = sgBrowerserActiveTab(url,'callBackFun'); +} +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +53. 打开消息框 +接口名称 +打开消息框 +接口描述 +打开消息框 +接口地址 +sgBrowerserOpenMessage(url, width, height) +管道消息 +[requesturl,"sgBrowerserOpenMessage", url, width, height] +传输参数 +url:消息框打开的网页地址。 +width:消息框宽度。 +height:消息框高度。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +54. 关闭消息框 +接口名称 +关闭消息框 +接口描述 +关闭消息框 +接口地址 +sgBrowerserMessageClose() +管道消息 +[requesturl,"sgBrowerserMessageClose"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +55. 消息框隐藏 +接口名称 +消息框隐藏 +接口描述 +消息框隐藏 +接口地址 +sgBrowerserMessageHide() +管道消息 +[requesturl,"sgBrowerserMessageHide"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +56. 消息框显示 +接口名称 +消息框显示 +接口描述 +消息框显示 +接口地址 +sgBrowerserMessageShow() +管道消息 +[requesturl,"sgBrowerserMessageShow"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +57. 打开新页面 +接口名称 +打开新页面 +接口描述 +打开新页面 +接口地址 +sgBrowerserOpenPage(url) +管道消息 +[requesturl,"sgBrowerserOpenPage",url] +传输参数 +url:新页面url地址 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +58. 打开用户信息页面 +接口名称 +打开用户信息页面 +接口描述 +打开用户信息页面 +接口地址 +sgBrowerserOpenUser(url, width, height) +管道消息 +[requesturl,"sgBrowerserOpenUser",url, width, height] +传输参数 +url:打开用户信息页面地址。 +width:用户信息页面框宽度。 +height:用户信息页面框高度。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +59. 隐藏用户信息页面 +接口名称 +隐藏用户信息页面 +接口描述 +隐藏用户信息页面 +接口地址 +sgBrowerserHideUser() +管道消息 +[requesturl,"sgBrowerserHideUser"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +60. 显示用户信息页面 +接口名称 +显示用户信息页面 +接口描述 +显示用户信息页面 +接口地址 +sgBrowerserShowUser() +管道消息 +[requesturl,"sgBrowerserShowUser"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +61. 设置登录后用户显示图标和名称 +接口名称 +设置登录后用户显示图标和名称 +接口描述 +设置登录后用户显示图标和名称 +接口地址 +sgBrowerserSetUser(userImg, username) +管道消息 +[requesturl,"sgBrowerserSetUser",userImg, username] +传输参数 +userImg:图标的 base64 编码。 +username:用户名称。 +代码示例: +var userImg = " 图标的 base64 编码 "; +var res = sgBrowerserSetUser(userImg, ' 张三 '); +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +62. 设置浏览器背景色 +接口名称 +设置浏览器背景色 +接口描述 +设置浏览器背景色 +接口地址 +sgBrowerserSetBackColor(colorvalue) +管道消息 +[requesturl,"sgBrowerserSetBackColor", colorvalue] +传输参数 +colorvalue:颜色值。 +代码示例: +var res = sgBrowerserSetBackColor('#ffffff'); +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +63. 设置登录后浏览器 Logo 图标 +接口名称 +设置登录后浏览器 Logo 图标 +接口描述 +设置登录后浏览器 Logo 图标 +接口地址 +sgBrowerserSetLogoImg(img) +管道消息 +[requesturl,"sgBrowerserSetLogoImg",img] +传输参数 +Img:图标的 base64 编码 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +64. 关闭显示的页面 +接口名称 +关闭显示的页面 +接口描述 +接口地址 +sgBrowserClosePage(url) +管道消息 +[requesturl,"sgBrowserClosePage", url] +传输参数 +url:要关闭的页面的url +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +65. 浏览器最大化 +接口名称 +浏览器最大化 +接口描述 +接口地址 +sgBrowserMaxShow() +管道消息 +[requesturl,"sgBrowserMaxShow"] +传输参数 +无 +约束条件 +无 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +66. 在隐藏域打开网页 +接口名称 +在隐藏域打开网页 +接口描述 +接口地址 +sgHideBrowerserOpenPage(url) +管道消息 +[requesturl,"sgHideBrowerserOpenPage", url] +传输参数 +url:要打开的网页的url +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +67. 获取隐藏域所有已经打开的 url +接口名称 +获取隐藏域所有已经打开的 url +接口描述 +接口地址 +sgHideBrowerserGetUrls(callBackFun) +管道消息 +[requesturl,"sgHideBrowerserGetUrls",callBackFun] +传输参数 +callBackFun:获取值通过此函数回调,包含一个参数,其中urls是通过分号隔开的字符串。 +代码示例: +function callBackFun(urls) { +console.log(urls); +} +var res = sgHideBrowerserGetUrls("callBackFun"); +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +68. 关闭隐藏域网址 +接口名称 +关闭隐藏域网址 +接口描述 +接口地址 +sgHideBrowerserClosePage(url) +管道消息 +[requesturl,"sgHideBrowerserClosePage", url] +传输参数 +url:在隐藏域要关闭的网页的url +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +69. 改变消息框大小 +接口名称 +改变消息框大小 +接口描述 +接口地址 +sgBrowerserMessageResize(w, h) +管道消息 +[requesturl,"sgBrowerserMessageResize", w, h] +传输参数 +w:消息框的宽度 +h:消息框的高度 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +70. 设置浏览器模式 +接口名称 +设置浏览器模式 +接口描述 +接口地址 +sgBroewserSetModel(mode) +管道消息 +[requesturl,"sgBroewserSetModel",mode] +传输参数 +mode: 1 菜单模式,2 原模式。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +3.参数值只能使用1或者2 +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +71. 设置顶部标题颜色 +接口名称 +设置顶部标题颜色 +接口描述 +接口地址 +sgBroewserSetToptcolor(color) +管道消息 +[requesturl,"sgBroewserSetToptcolor", color] +传输参数 +color: 要设置的颜色值。 +示例:sgBroewserSetToptcolor(“#tttt”) +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +72. 设置左侧菜单栏颜色 +接口名称 +设置左侧菜单栏颜色 +接口描述 +接口地址 +sgBroewserSetLeftcolor(color) +管道消息 +[requesturl,"sgBroewserSetLeftcolor", color] +传输参数 +color: 要设置的颜色值。 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +73. 修改已设置菜单属性 +接口名称 +修改已设置菜单属性 +接口描述 +接口地址 +sgBrowerserEditMenu(menuContent) +管道消息 +[requesturl,"sgBrowerserEditMenu", menuContent] +传输参数 +menuContent:菜单实体对象,结构如下:JSON.stringify({ +name: "菜单名字", +normalImg: "图标", +activeImg: "活动图标", +url: "链接地址" +}) +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +74. 新增菜单 +接口名称 +新增菜单 +接口描述 +新增菜单 +接口地址 +sgBrowerserAddMenu(menuContent) +管道消息 +[requesturl,"sgBrowerserAddMenu", menuContent] +传输参数 +menuContent:菜单实体对象,结构如下:JSON.stringify({ +name: "菜单名字", +normalImg: "图标", +activeImg: "活动图标", +url: "链接地址" +}) +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +75. 删除菜单 +接口名称 +删除菜单 +接口描述 +删除菜单 +接口地址 +sgBrowerserDelMenu(menuName) +管道消息 +[requesturl,"sgBrowerserDelMenu", menuName] +传输参数 +menuName:菜单名字 +约束条件 +1.参数数量必须相符; +2.所有参数不能为空; +返回参数 +执行成功返回0 +异常处理 +执行失败返回非0,并记录日志 +三、API接口返回值 +返回值 +执行结果 +0 +执行成功 +-1 +执行失败 +-10001 +参数不能为空 +-10011 +参数长度超出限制 +-20001 +参数不符 \ No newline at end of file diff --git a/docs/_tmp_sgbrowser_ws_probe_transcript.md b/docs/_tmp_sgbrowser_ws_probe_transcript.md new file mode 100644 index 0000000..ee2af73 --- /dev/null +++ b/docs/_tmp_sgbrowser_ws_probe_transcript.md @@ -0,0 +1,145 @@ +# sgBrowser websocket probe transcript + +Endpoint: `ws://127.0.0.1:12345` +Timeout: `1500ms` +Cargo target dir override: `D:/data/ideaSpace/rust/sgClaw/claw-new/target_task4` + +## baseline-open + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "baseline-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'baseline-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 baseline-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: Welcome! You are client #1 +OUTCOME: received +``` + +## open-agent + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "open-agent::[\"about:blank\",\"sgOpenAgent\"]" --step "post-open-agent-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.98s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'open-agent::["about:blank","sgOpenAgent"]' --step 'post-open-agent-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 open-agent +SEND: ["about:blank","sgOpenAgent"] +RECV: Welcome! You are client #1 +OUTCOME: received + +STEP 2 post-open-agent-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: +OUTCOME: timeout +``` + +## set-auth + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "set-auth::[\"about:blank\",\"sgSetAuthInfo\",\"probe-user\",\"probe-token\"]" --step "post-set-auth-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'set-auth::["about:blank","sgSetAuthInfo","probe-user","probe-token"]' --step 'post-set-auth-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 set-auth +SEND: ["about:blank","sgSetAuthInfo","probe-user","probe-token"] +RECV: Welcome! You are client #1 +OUTCOME: received + +STEP 2 post-set-auth-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: +OUTCOME: timeout +``` + +## browser-login + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step 'browser-login::["about:blank","sgBrowserLogin",{"appName":"probe","userName":"probe","orgName":"probe","menus":[{"name":"probe","normalImg":"x","activeImg":"x","url":"https://www.zhihu.com/hot"}]}]' --step 'post-browser-login-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]' + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'browser-login::["about:blank","sgBrowserLogin",{"appName":"probe","userName":"probe","orgName":"probe","menus":[{"name":"probe","normalImg":"x","activeImg":"x","url":"https://www.zhihu.com/hot"}]}]' --step 'post-browser-login-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 browser-login +SEND: ["about:blank","sgBrowserLogin",{"appName":"probe","userName":"probe","orgName":"probe","menus":[{"name":"probe","normalImg":"x","activeImg":"x","url":"https://www.zhihu.com/hot"}]}] +RECV: Welcome! You are client #1 +OUTCOME: received + +STEP 2 post-browser-login-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: +OUTCOME: timeout +``` + +## active-tab + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "active-tab::[\"about:blank\",\"sgBrowerserActiveTab\",\"https://www.zhihu.com/hot\",\"probeCallback\"]" --step "post-active-tab-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'active-tab::["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"]' --step 'post-active-tab-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 active-tab +SEND: ["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"] +RECV: Welcome! You are client #1 +OUTCOME: received + +STEP 2 post-active-tab-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: +OUTCOME: timeout +``` + +## combined-bootstrap + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "combined-open-agent::[\"about:blank\",\"sgOpenAgent\"]" --step "combined-active-tab::[\"about:blank\",\"sgBrowerserActiveTab\",\"https://www.zhihu.com/hot\",\"probeCallback\"]" --step "combined-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.95s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'combined-open-agent::["about:blank","sgOpenAgent"]' --step 'combined-active-tab::["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"]' --step 'combined-open::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 combined-open-agent +SEND: ["about:blank","sgOpenAgent"] +RECV: Welcome! You are client #1 +OUTCOME: received + +STEP 2 combined-active-tab +SEND: ["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"] +RECV: +OUTCOME: timeout + +STEP 3 combined-open +SEND: ["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: +OUTCOME: timeout +``` + +## requesturl-variants + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "target-as-requesturl::[\"https://www.zhihu.com/hot\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.94s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'target-as-requesturl::["https://www.zhihu.com/hot","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 target-as-requesturl +SEND: ["https://www.zhihu.com/hot","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: Welcome! You are client #1 +OUTCOME: received +``` + +```text +$ CARGO_TARGET_DIR="/d/data/ideaSpace/rust/sgClaw/claw-new/target_task4" cargo run --manifest-path "/d/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "baidu-requesturl::[\"https://www.baidu.com\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.94s + Running `target_task4\debug\sgbrowser_ws_probe.exe --ws-url 'ws://127.0.0.1:12345' --timeout-ms 1500 --step 'baidu-requesturl::["https://www.baidu.com","sgBrowerserOpenPage","https://www.zhihu.com/hot"]'` +STEP 1 baidu-requesturl +SEND: ["https://www.baidu.com","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +RECV: Welcome! You are client #1 +OUTCOME: received +``` + +| Sequence | Sent frames | First reply | Final outcome | Decision signal | +| --- | --- | --- | --- | --- | +| baseline-open | `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | received only welcome banner; no numeric status or callback frame captured | does not satisfy Option A rule | +| open-agent | `["about:blank","sgOpenAgent"]` then `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | step 2 timed out with no reply | does not satisfy Option A rule | +| set-auth | `["about:blank","sgSetAuthInfo","probe-user","probe-token"]` then `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | step 2 timed out with no reply | does not satisfy Option A rule | +| browser-login | `["about:blank","sgBrowserLogin",{"appName":"probe","userName":"probe","orgName":"probe","menus":[{"name":"probe","normalImg":"x","activeImg":"x","url":"https://www.zhihu.com/hot"}]}]` then `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | step 2 timed out with no reply | does not satisfy Option A rule | +| active-tab | `["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"]` then `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | step 2 timed out with no reply | does not satisfy Option A rule | +| combined-bootstrap | `["about:blank","sgOpenAgent"]` then `["about:blank","sgBrowerserActiveTab","https://www.zhihu.com/hot","probeCallback"]` then `["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | steps 2 and 3 timed out with no reply | does not satisfy Option A rule | +| requesturl-variants | `["https://www.zhihu.com/hot","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` and `["https://www.baidu.com","sgBrowerserOpenPage","https://www.zhihu.com/hot"]` | `Welcome! You are client #1` | each one-shot run received only the welcome banner; no numeric status or callback frame captured | does not satisfy Option A rule | + +## Final decision + +**Option B wins.** + +Reason: the strict rule says Option A wins only if at least one sequence reproducibly yields real numeric status and/or callback frames for a real business action. Across the full required matrix, the reachable endpoint consistently returned only the websocket welcome banner on the first reply for each fresh connection, and every follow-on business-action step either timed out or produced no numeric status/callback frame. Therefore the evidence does not validate a raw-websocket bootstrap contract, so Option B is the required outcome. diff --git a/docs/superpowers/plans/2026-04-01-claw-ws-execution-cards.md b/docs/superpowers/plans/2026-04-01-claw-ws-execution-cards.md new file mode 100644 index 0000000..9f2a498 --- /dev/null +++ b/docs/superpowers/plans/2026-04-01-claw-ws-execution-cards.md @@ -0,0 +1,425 @@ +# Claw-WS 开发执行顺序卡片 + +> 配套计划:[`2026-04-01-claw-ws-parallel-transport.md`](./2026-04-01-claw-ws-parallel-transport.md) +> +> 使用方式:严格按卡片顺序执行。每张卡片完成后先跑卡片内测试,再进入下一张。不要跳卡,不要提前接线,不要先写 service/client 再回头抽象底层。 + +--- + +## 卡片 0:执行前约束 + +**目标** +先锁定边界,避免实现过程中把 pipe 模式改坏。 + +**必须遵守** +- 现有 pipe 模式必须保持可用 +- 新增的是并行 `claw-ws` 模式,不是替换 pipe +- v1 只做单客户端、单任务串行 +- `browser_action` / `superrpa_browser` 外部命名保持稳定 +- 如果 WS `Eval` 不完整,先禁用相关 browser-script skill 暴露 +- 不要提前做多客户端、任务队列、管理接口 + +**完成标准** +- 开发者明确后续所有改动都围绕“抽象复用 + 并行新增”进行 + +--- + +## 卡片 1:抽共享 SubmitTask Runner + +**目标** +把当前 `BrowserMessage::SubmitTask` 的主执行逻辑从 pipe 入口里抽出来,变成共享执行器。 + +**先做什么** +1. 新增 `tests/task_runner_test.rs` +2. 先写失败用例: + - 空 instruction + - 无 LLM 配置 + - 日志顺序仍然是 `LogEntry` -> `TaskComplete` + +**要改哪些文件** +- `src/agent/mod.rs` +- `src/lib.rs` +- `src/agent/task_runner.rs` +- `tests/task_runner_test.rs` + +**实现动作** +1. 建 `SubmitTaskRequest` +2. 建 `AgentEventSink` +3. 建 `run_submit_task(...)` +4. 让 pipe 入口只做: + - 解包 `BrowserMessage::SubmitTask` + - 转成 `SubmitTaskRequest` + - 调共享 runner + +**绝对不要做** +- 不要在这一张卡里引入 ws backend +- 不要改 tool adapter +- 不要碰 service/client + +**本卡测试命令** + +```bash +cargo test --test runtime_task_flow_test --test task_runner_test +``` + +**通过标准** +- 老的 `runtime_task_flow_test` 继续绿 +- 新的 `task_runner_test` 通过 +- pipe 行为无变化 + +**完成后提交** + +```bash +git commit -m "refactor: extract shared submit-task runner" +``` + +--- + +## 卡片 2:抽 BrowserBackend 抽象 + +**目标** +把上层 runtime / orchestration / tool adapter 从 `BrowserPipeTool` 解耦,统一依赖浏览器后端接口。 + +**先做什么** +1. 新增 `tests/browser_backend_capability_test.rs` +2. 先写失败用例: + - pipe backend 元数据不变 + - pipe backend 支持 `Eval` + - `supports_eval() == false` 时不暴露 browser-script tools + +**要改哪些文件** +- `src/browser/mod.rs` +- `src/browser/backend.rs` +- `src/browser/pipe_backend.rs` +- `src/compat/browser_tool_adapter.rs` +- `src/compat/browser_script_skill_tool.rs` +- `src/compat/runtime.rs` +- `src/compat/orchestration.rs` +- `src/compat/workflow_executor.rs` +- `src/lib.rs` +- `tests/browser_backend_capability_test.rs` + +**实现动作** +1. 定义 `BrowserBackend` +2. 写 `pipe_backend` 包装当前 `BrowserPipeTool` +3. 把上层签名改成 `Arc` +4. 保持工具名不变: + - `browser_action` + - `superrpa_browser` +5. 增加 `supports_eval()` gating + +**绝对不要做** +- 不要在这一张卡里接浏览器 ws 协议 +- 不要建 service +- 不要加 client 协议 + +**本卡测试命令** + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test browser_backend_capability_test +``` + +**通过标准** +- 现有 browser tool 相关测试不回归 +- 新 capability test 通过 +- 上层逻辑已脱离 `BrowserPipeTool` 的硬耦合 + +**完成后提交** + +```bash +git commit -m "refactor: abstract browser backend from pipe transport" +``` + +--- + +## 卡片 3:写死浏览器 WS 协议 Codec + +**目标** +单独做浏览器固定 WebSocket 协议编解码层,不把协议细节散落到 backend 和 service 里。 + +**先做什么** +1. 新增 `tests/browser_ws_protocol_test.rs` +2. 先写失败用例: + - outbound frame 精确编码 + - callback payload 解析 + - 异常格式拒绝 + - v1 action 覆盖完整 + +**要改哪些文件** +- `src/browser/ws_protocol.rs` +- `tests/browser_ws_protocol_test.rs` + +**实现动作** +1. 按浏览器文档编码数组消息 +2. 只支持 v1 必需动作: + - `Navigate` + - `GetText` + - `Click` + - `Type` + - `Eval` +3. 定义 callback 解析和关联规则 +4. 对 unsupported / malformed 早失败 + +**绝对不要做** +- 不要在这张卡里连真实浏览器 +- 不要写 service 协议 +- 不要把网络连接逻辑塞进 codec + +**本卡测试命令** + +```bash +cargo test --test browser_ws_protocol_test +``` + +**通过标准** +- codec 单测全绿 +- 无网络依赖 +- 已能作为 backend 的纯协议层基础 + +**完成后提交** + +```bash +git commit -m "test: codify fixed browser websocket protocol" +``` + +--- + +## 卡片 4:实现 Browser WS Backend + +**目标** +在 codec 之上提供和 pipe backend 类似的阻塞式 `invoke(...)` 能力。 + +**先做什么** +1. 新增 `tests/browser_ws_backend_test.rs` +2. 先写失败用例: + - `0 + 无 callback` 成功 + - 非 `0` 失败 + - `0 + callback` 成功 + - callback timeout + - socket drop + +**要改哪些文件** +- `src/browser/mod.rs` +- `src/browser/ws_backend.rs` +- `tests/browser_ws_backend_test.rs` + +**实现动作** +1. 建长连接管理器 +2. 先做串行单飞请求 +3. 发送前过 `MacPolicy` +4. 统一即时返回和 callback 返回 +5. 输出统一 `CommandOutput` + +**绝对不要做** +- 不要在这一张卡里接 service 层 +- 不要做多并发 browser request +- 不要直接把浏览器 ws 代码散进 runtime + +**本卡测试命令** + +```bash +cargo test --test browser_ws_backend_test +``` + +**通过标准** +- backend 在 mocks/fakes 下稳定通过 +- invoke 语义与 pipe backend 接近 +- 可供上层 runtime 直接替换使用 + +**完成后提交** + +```bash +git commit -m "feat: add browser websocket backend" +``` + +--- + +## 卡片 5:实现 sg_claw Service + +**目标** +新增本地长驻服务端,承接 client 请求并复用共享 task runner。 + +**先做什么** +1. 新增 `tests/service_ws_session_test.rs` +2. 先写失败用例: + - 首个客户端接入成功 + - 第二个客户端收到 busy + - 断开后状态释放 + - 任务重入被拒绝 + +**要改哪些文件** +- `src/service/mod.rs` +- `src/service/protocol.rs` +- `src/service/server.rs` +- `src/bin/sg_claw.rs` +- `src/lib.rs` +- `Cargo.toml` +- `tests/service_ws_session_test.rs` + +**实现动作** +1. 定义 client/service 协议 +2. 实现 service 端事件 sink +3. 建单活 session 状态机: + - `Idle` + - `ClientAttached` + - `TaskRunning` +4. 路由 `SubmitTask` 到共享 runner +5. 保持 pipe 入口不变 + +**绝对不要做** +- 不要在这一张卡里做 client 交互体验优化 +- 不要加任务队列 +- 不要支持多客户端并发 + +**本卡测试命令** + +```bash +cargo test --test service_ws_session_test +``` + +**通过标准** +- 服务端会话锁生效 +- 共享 runner 可被 service 复用 +- pipe 模式入口未受影响 + +**完成后提交** + +```bash +git commit -m "feat: add claw-ws service entrypoint" +``` + +--- + +## 卡片 6:实现 sg_claw_client + +**目标** +新增一个薄客户端,提供类似 `claude/codex` 的交互式命令行体验。 + +**先做什么** +1. 新增 `tests/service_task_flow_test.rs` +2. 先写失败用例: + - submit-task 送达 service + - 日志按顺序流回 + - completion 只到一次 + - 完成后断开处理清晰 + +**要改哪些文件** +- `src/bin/sg_claw_client.rs` +- `Cargo.toml` +- `tests/service_task_flow_test.rs` + +**实现动作** +1. 建立到本地 service 的 ws 连接 +2. 读取用户输入 +3. 发送 `SubmitTask` +4. 实时打印日志 +5. 收到 `TaskComplete` 结束本轮 + +**绝对不要做** +- 不要把 runtime、skills、browser backend 复制进 client +- 不要让 client 直接连浏览器 +- 不要让 client 承担业务逻辑 + +**本卡测试命令** + +```bash +cargo test --test service_task_flow_test +cargo build --bin sg_claw --bin sg_claw_client +``` + +**通过标准** +- client 是薄壳 +- task flow 正常 +- 两个新 binary 可编译 + +**完成后提交** + +```bash +git commit -m "feat: add interactive claw-ws client" +``` + +--- + +## 卡片 7:最终接线与回归验证 + +**目标** +把 ws 路径接起来,同时确认 pipe 路径零回归。 + +**先做什么** +1. 只增加最小配置项: + - `browser_ws_url` + - `service_ws_listen_addr` +2. 检查外部工具命名保持稳定 + +**要改哪些文件** +- `Cargo.toml` +- `src/lib.rs` +- `src/config/settings.rs` +- `src/runtime/engine.rs`(如确有必要) + +**实现动作** +1. 接入最小配置面 +2. 确保 pipe / ws 下工具命名一致 +3. 跑旧 pipe 回归 +4. 跑新 ws 测试 +5. 跑全量 Rust tests +6. 编译所有 binary +7. 做一次真实本地 smoke test + +**本卡 pipe 回归命令** + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test runtime_task_flow_test --test pipe_handshake_test --test pipe_protocol_test --test task_protocol_test +``` + +**本卡 ws 测试命令** + +```bash +cargo test --test task_runner_test --test browser_ws_protocol_test --test browser_ws_backend_test --test browser_backend_capability_test --test service_ws_session_test --test service_task_flow_test +``` + +**本卡全量命令** + +```bash +cargo test --tests +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +**手工验证** +1. 启动浏览器,确认 `ws://127.0.0.1:12345` 可用 +2. `cargo run --bin sg_claw` +3. 新终端运行 `cargo run --bin sg_claw_client` +4. 发一个简单浏览器任务 +5. 确认日志流和单次 completion +6. 确认旧 `cargo run` pipe 入口仍可启动 + +**通过标准** +- pipe 模式零回归 +- ws 模式可独立工作 +- 两套模式并行存在 + +**完成后提交** + +```bash +git commit -m "feat: wire parallel claw-ws transport" +``` + +--- + +## 一句话执行顺序 + +严格按下面顺序做: + +1. 共享 runner +2. browser backend 抽象 +3. ws 协议 codec +4. ws backend +5. service +6. client +7. 配置接线 + 回归 + +如果顺序乱了,最容易出现的问题是: +- 上层重复实现 +- pipe 被误伤 +- ws 协议细节扩散到整个工程 +- service/client 提前写完后又被迫重构 diff --git a/docs/superpowers/plans/2026-04-01-claw-ws-parallel-transport.md b/docs/superpowers/plans/2026-04-01-claw-ws-parallel-transport.md new file mode 100644 index 0000000..8f2b0b0 --- /dev/null +++ b/docs/superpowers/plans/2026-04-01-claw-ws-parallel-transport.md @@ -0,0 +1,687 @@ +# Claw-WS Parallel Transport Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Add a parallel `claw-ws` transport path that keeps the current pipe mode intact while introducing a long-lived `sg_claw` local service, an interactive `sg_claw_client`, and a browser WebSocket backend at `ws://127.0.0.1:12345`. + +**Architecture:** First extract a transport-agnostic submit-task runner and browser backend abstraction from the current pipe-coupled flow. Keep the existing pipe path as one adapter/backend, then add a fixed-protocol browser WebSocket backend plus a small service/session layer and an interactive CLI client that reuse the same runtime, orchestration, and browser-facing tool adapters. + +**Tech Stack:** Rust 2021, current sgclaw compat runtime, zeroclaw runtime engine, `serde`/`serde_json`, existing `MacPolicy`, and a blocking WebSocket crate for v1 (`tungstenite` preferred over a broad async rewrite). + +--- + +## Scope Guardrails + +- Keep the current pipe mode entrypoint and behavior working. +- Do **not** replace the existing browser pipe path. +- Add a **parallel** WebSocket path only. +- v1 supports **one active client session** only. +- Reuse existing tool names and runtime behavior whenever possible. +- If WS `Eval` support is incomplete, disable eval-dependent browser-script skill exposure in WS mode rather than shipping partial behavior. +- Do not broaden v1 with task queues, multi-client support, or admin endpoints. + +--- + +## File Structure + +### Existing files to reuse + +- Modify: `src/lib.rs` — current pipe bootstrap and receive loop; keep as the legacy pipe entrypoint. +- Modify: `src/agent/mod.rs` — current `BrowserMessage::SubmitTask` entrypoint and config-loading flow. +- Modify: `src/compat/runtime.rs` — compat runtime and tool assembly. +- Modify: `src/compat/orchestration.rs` — direct workflow vs compat runtime routing. +- Modify: `src/compat/browser_tool_adapter.rs` — exposes `browser_action` and `superrpa_browser`. +- Modify: `src/compat/browser_script_skill_tool.rs` — browser-script skill execution. +- Modify: `src/compat/workflow_executor.rs` — direct browser workflows such as Zhihu flows. +- Reuse: `src/pipe/browser_tool.rs` — current browser command executor; retain as the pipe backend implementation. +- Reuse: `src/pipe/protocol.rs` — `BrowserMessage`, `AgentMessage`, `Action`, `ExecutionSurfaceMetadata`. +- Reuse: `src/security/mac_policy.rs` — local action/domain guardrails. +- Modify: `src/config/settings.rs` — minimal new config surface for WS mode. +- Optional modify: `src/runtime/engine.rs` — only if backend capability wiring requires it. + +### New files to create + +- Create: `src/agent/task_runner.rs` — shared submit-task execution entrypoint. +- Create: `src/browser/mod.rs` — browser backend exports. +- Create: `src/browser/backend.rs` — `BrowserBackend` trait and helpers. +- Create: `src/browser/pipe_backend.rs` — wrapper around existing `BrowserPipeTool`. +- Create: `src/browser/ws_protocol.rs` — fixed browser WS request/response codec. +- Create: `src/browser/ws_backend.rs` — browser WS backend with blocking invoke semantics. +- Create: `src/service/mod.rs` — service exports. +- Create: `src/service/protocol.rs` — client/service WS message types. +- Create: `src/service/server.rs` — single-session `sg_claw` server. +- Create: `src/bin/sg_claw.rs` — service binary. +- Create: `src/bin/sg_claw_client.rs` — interactive CLI client. +- Create: `tests/task_runner_test.rs` — shared submit-task runner regressions. +- Create: `tests/browser_backend_capability_test.rs` — backend capability/tool exposure tests. +- Create: `tests/browser_ws_protocol_test.rs` — browser WS protocol tests. +- Create: `tests/browser_ws_backend_test.rs` — browser WS backend tests. +- Create: `tests/service_ws_session_test.rs` — single-session server tests. +- Create: `tests/service_task_flow_test.rs` — client/service task flow tests. + +--- + +## Task 1: Extract a shared submit-task runner + +**Files:** +- Create: `src/agent/task_runner.rs` +- Modify: `src/agent/mod.rs` +- Modify: `src/lib.rs` +- Test: `tests/task_runner_test.rs` +- Reuse: `src/compat/runtime.rs`, `src/compat/orchestration.rs` + +- [ ] **Step 1: Write a failing runner regression test** + +Create `tests/task_runner_test.rs` covering: +- empty instruction returns the same `TaskComplete` failure summary +- missing LLM config still returns the same summary shape +- the pipe adapter still emits `LogEntry` before `TaskComplete` + +- [ ] **Step 2: Run the targeted regression tests first** + +Run: + +```bash +cargo test --test runtime_task_flow_test --test task_runner_test +``` + +Expected: `task_runner_test` fails because the shared runner does not exist yet. + +- [ ] **Step 3: Define the transport-neutral request model** + +Create `src/agent/task_runner.rs` with a request struct that mirrors the current pipe payload: + +```rust +pub struct SubmitTaskRequest { + pub instruction: String, + pub conversation_id: Option, + pub messages: Vec, + pub page_url: Option, + pub page_title: Option, +} +``` + +Normalize empty strings to `None` at the adapter boundary. + +- [ ] **Step 4: Define an event sink abstraction** + +Add a small trait that can emit the current agent events without depending on a specific transport: + +```rust +pub trait AgentEventSink { + fn send(&self, message: &AgentMessage) -> Result<(), PipeError>; +} +``` + +The existing pipe transport should implement this first. + +- [ ] **Step 5: Move submit-task execution into a shared function** + +Extract the body currently inside `BrowserMessage::SubmitTask` handling from `src/agent/mod.rs` into a shared function such as: + +```rust +pub fn run_submit_task( + sink: &dyn AgentEventSink, + browser_backend: Arc, + context: &AgentRuntimeContext, + request: SubmitTaskRequest, +) -> Result<(), PipeError> +``` + +This function must still: +- validate empty instruction +- load sgclaw settings +- log runtime/config info +- choose orchestration vs compat runtime +- emit `AgentMessage::TaskComplete` + +- [ ] **Step 6: Keep pipe mode as a thin adapter** + +Refactor `handle_browser_message_with_context(...)` in `src/agent/mod.rs` so it only: +- pattern matches `BrowserMessage` +- converts `SubmitTask` into `SubmitTaskRequest` +- forwards into `run_submit_task(...)` + +- [ ] **Step 7: Re-run the runner regressions** + +Run: + +```bash +cargo test --test runtime_task_flow_test --test task_runner_test +``` + +Expected: both tests pass and pipe behavior remains unchanged. + +- [ ] **Step 8: Commit** + +```bash +git add src/agent/mod.rs src/agent/task_runner.rs src/lib.rs tests/task_runner_test.rs +git commit -m "refactor: extract shared submit-task runner" +``` + +--- + +## Task 2: Introduce a browser backend abstraction and wrap the current pipe implementation + +**Files:** +- Create: `src/browser/mod.rs` +- Create: `src/browser/backend.rs` +- Create: `src/browser/pipe_backend.rs` +- Modify: `src/lib.rs` +- Modify: `src/compat/browser_tool_adapter.rs` +- Modify: `src/compat/browser_script_skill_tool.rs` +- Modify: `src/compat/runtime.rs` +- Modify: `src/compat/orchestration.rs` +- Modify: `src/compat/workflow_executor.rs` +- Test: `tests/browser_backend_capability_test.rs` +- Reuse: `src/pipe/browser_tool.rs`, `src/security/mac_policy.rs` + +- [ ] **Step 1: Add a failing backend capability test** + +Create `tests/browser_backend_capability_test.rs` to verify: +- pipe backend still exposes privileged surface metadata +- pipe backend still supports `Eval` +- browser-script tool exposure is disabled when `supports_eval()` is false + +- [ ] **Step 2: Run the current browser adapter tests first** + +Run: + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test browser_backend_capability_test +``` + +Expected: new capability test fails because the backend abstraction does not exist yet. + +- [ ] **Step 3: Define the shared browser interface** + +Create `src/browser/backend.rs`: + +```rust +pub trait BrowserBackend: Send + Sync { + fn invoke( + &self, + action: Action, + params: Value, + expected_domain: &str, + ) -> Result; + + fn surface_metadata(&self) -> ExecutionSurfaceMetadata; + + fn supports_eval(&self) -> bool { + true + } +} +``` + +- [ ] **Step 4: Implement the pipe backend as a wrapper** + +Create `src/browser/pipe_backend.rs` that stores the current `BrowserPipeTool` and forwards `invoke(...)` and `surface_metadata()` unchanged. + +Pipe mode must continue using: +- `perform_handshake(...)` +- `MacPolicy::load_from_path(...)` +- `BrowserPipeTool::new(...).with_response_timeout(...)` + +- [ ] **Step 5: Refactor runtime and tool adapters to depend on `Arc`** + +Update: +- `src/compat/browser_tool_adapter.rs` +- `src/compat/browser_script_skill_tool.rs` +- `src/compat/runtime.rs` +- `src/compat/orchestration.rs` +- `src/compat/workflow_executor.rs` + +Preserve external tool names: +- `browser_action` +- `superrpa_browser` + +- [ ] **Step 6: Add capability gating for eval-dependent script tools** + +If `supports_eval()` is false, do **not** expose browser-script skill tools from `build_browser_script_skill_tools(...)` in that backend mode. + +- [ ] **Step 7: Re-run browser adapter tests** + +Run: + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test browser_backend_capability_test +``` + +Expected: all three pass. + +- [ ] **Step 8: Commit** + +```bash +git add src/browser src/lib.rs src/compat/browser_tool_adapter.rs src/compat/browser_script_skill_tool.rs src/compat/runtime.rs src/compat/orchestration.rs src/compat/workflow_executor.rs tests/browser_backend_capability_test.rs +git commit -m "refactor: abstract browser backend from pipe transport" +``` + +--- + +## Task 3: Implement the fixed browser WebSocket protocol codec in isolation + +**Files:** +- Create: `src/browser/ws_protocol.rs` +- Test: `tests/browser_ws_protocol_test.rs` +- Reuse: `docs/_tmp_sgbrowser_ws_api_doc.txt` + +- [ ] **Step 1: Write failing protocol codec tests** + +Create `tests/browser_ws_protocol_test.rs` covering: +- exact outbound frame encoding +- callback payload decoding +- unknown callback format rejection +- mapping coverage for every supported v1 action + +- [ ] **Step 2: Run the protocol tests first** + +Run: + +```bash +cargo test --test browser_ws_protocol_test +``` + +Expected: fail because the WS protocol codec does not exist yet. + +- [ ] **Step 3: Encode the exact browser frame shapes** + +Create `src/browser/ws_protocol.rs` so it can build exact array-form payloads such as: + +```rust +[requesturl, "sgBrowserExcuteJsCodeByArea", target_url, js_code, area] +``` + +Serialize to the JSON string format required by the browser service. + +- [ ] **Step 4: Define the v1 action mapping table** + +Support only the actions already needed by current sgclaw flows: +- `Navigate` +- `GetText` +- `Click` +- `Type` +- `Eval` + +Document which browser functions each one maps to and what assumptions they rely on. + +- [ ] **Step 5: Define callback parsing and correlation rules** + +Represent callback-bearing operations explicitly, including the callback function naming or request-correlation strategy the backend will depend on. + +- [ ] **Step 6: Reject unsupported or malformed shapes early** + +Fail fast for: +- unsupported actions +- malformed callback payloads +- missing request correlation metadata + +- [ ] **Step 7: Re-run the protocol tests** + +Run: + +```bash +cargo test --test browser_ws_protocol_test +``` + +Expected: pass with no network dependency. + +- [ ] **Step 8: Commit** + +```bash +git add src/browser/ws_protocol.rs tests/browser_ws_protocol_test.rs +git commit -m "test: codify fixed browser websocket protocol" +``` + +--- + +## Task 4: Build the browser WS backend with synchronous invoke semantics + +**Files:** +- Create: `src/browser/ws_backend.rs` +- Modify: `src/browser/mod.rs` +- Test: `tests/browser_ws_backend_test.rs` +- Reuse: `CommandOutput`, `PipeError`, `ExecutionSurfaceMetadata`, `MacPolicy` + +- [ ] **Step 1: Write failing backend behavior tests** + +Create `tests/browser_ws_backend_test.rs` covering: +- zero return + no callback => success +- non-zero return => failure +- zero return + callback => success with normalized `CommandOutput` +- callback timeout => timeout error +- dropped socket => clear failure + +- [ ] **Step 2: Run backend tests first** + +Run: + +```bash +cargo test --test browser_ws_backend_test +``` + +Expected: fail because the WS backend does not exist yet. + +- [ ] **Step 3: Build a long-lived browser connection manager** + +Implement `src/browser/ws_backend.rs` to connect to `ws://127.0.0.1:12345` and expose blocking `invoke(...)` calls. + +Use a dedicated connection loop plus request/response coordination instead of scattering raw socket calls through the runtime. + +- [ ] **Step 4: Preserve local guardrails before send** + +Validate `MacPolicy` before translating an action into the browser WS protocol, matching current pipe backend behavior. + +- [ ] **Step 5: Normalize immediate status returns and delayed callbacks** + +For each `invoke(...)` call: +- fail immediately on non-zero return codes +- succeed immediately for operations with no data callback +- wait for the matching callback for result-bearing operations +- convert the final outcome into `CommandOutput` + +- [ ] **Step 6: Keep v1 concurrency intentionally serialized** + +Allow only one in-flight browser request at a time unless the browser callback protocol proves a stable request-id guarantee. + +- [ ] **Step 7: Re-run backend tests** + +Run: + +```bash +cargo test --test browser_ws_backend_test +``` + +Expected: pass using mocks/fakes, not the real browser. + +- [ ] **Step 8: Commit** + +```bash +git add src/browser/mod.rs src/browser/ws_backend.rs tests/browser_ws_backend_test.rs +git commit -m "feat: add browser websocket backend" +``` + +--- + +## Task 5: Add the `sg_claw` service protocol and single-session server + +**Files:** +- Create: `src/service/mod.rs` +- Create: `src/service/protocol.rs` +- Create: `src/service/server.rs` +- Create: `src/bin/sg_claw.rs` +- Modify: `src/lib.rs` +- Modify: `Cargo.toml` +- Test: `tests/service_ws_session_test.rs` +- Reuse: `AgentMessage::LogEntry`, `AgentMessage::TaskComplete`, `SubmitTaskRequest`, `run_submit_task(...)` + +- [ ] **Step 1: Write failing service session tests** + +Create `tests/service_ws_session_test.rs` to verify: +- first client attaches +- second client gets `Busy` +- disconnect resets session state +- overlapping task submission is rejected clearly + +- [ ] **Step 2: Run the session tests first** + +Run: + +```bash +cargo test --test service_ws_session_test +``` + +Expected: fail because the service layer does not exist yet. + +- [ ] **Step 3: Define a thin client/service WS protocol** + +In `src/service/protocol.rs`, reuse existing task/event shapes as much as possible: + +```rust +ClientMessage::SubmitTask { instruction, conversation_id, messages, page_url, page_title } +ClientMessage::Ping +ServiceMessage::LogEntry { level, message } +ServiceMessage::TaskComplete { success, summary } +ServiceMessage::Busy { message } +``` + +- [ ] **Step 4: Add the service event sink adapter** + +Implement `AgentEventSink` for the service session writer so the shared task runner can stream `LogEntry` and `TaskComplete` over the service WebSocket. + +- [ ] **Step 5: Implement single-active-client session state** + +Model explicit states such as: +- `Idle` +- `ClientAttached` +- `TaskRunning` + +Reject a second client with `ServiceMessage::Busy` and close the socket. Reject overlapping tasks instead of queueing them. + +- [ ] **Step 6: Add the service binary** + +Create `src/bin/sg_claw.rs` that: +- loads config +- creates the browser WS backend +- listens for local client connections +- routes `SubmitTask` into `run_submit_task(...)` + +Keep `src/main.rs` and the existing `sgclaw::run()` pipe path unchanged. + +- [ ] **Step 7: Re-run the session tests** + +Run: + +```bash +cargo test --test service_ws_session_test +``` + +Expected: pass without the real browser. + +- [ ] **Step 8: Commit** + +```bash +git add src/service src/bin/sg_claw.rs src/lib.rs Cargo.toml tests/service_ws_session_test.rs +git commit -m "feat: add claw-ws service entrypoint" +``` + +--- + +## Task 6: Add the `sg_claw_client` interactive CLI + +**Files:** +- Create: `src/bin/sg_claw_client.rs` +- Modify: `Cargo.toml` +- Test: `tests/service_task_flow_test.rs` +- Reuse: `src/service/protocol.rs` + +- [ ] **Step 1: Write failing client/service task flow tests** + +Create `tests/service_task_flow_test.rs` to verify: +- the submit-task request reaches the service +- log entries stream in order +- the final summary arrives exactly once +- disconnect after task completion is handled cleanly + +- [ ] **Step 2: Run the flow tests first** + +Run: + +```bash +cargo test --test service_task_flow_test +``` + +Expected: fail because the client does not exist yet. + +- [ ] **Step 3: Implement a thin interactive client loop** + +Create `src/bin/sg_claw_client.rs` that: +- connects to the local `sg_claw` service +- reads a line of user input +- sends `ClientMessage::SubmitTask` +- prints streamed `LogEntry` events as they arrive +- ends the turn on `TaskComplete` + +- [ ] **Step 4: Keep the client intentionally dumb** + +Do **not** duplicate runtime logic in the client. Browser access, skills, orchestration, and task execution remain entirely inside the service. + +- [ ] **Step 5: Re-run the flow tests** + +Run: + +```bash +cargo test --test service_task_flow_test +``` + +Expected: pass without the real browser. + +- [ ] **Step 6: Build the new binaries explicitly** + +Run: + +```bash +cargo build --bin sg_claw --bin sg_claw_client +``` + +Expected: both binaries compile successfully. + +- [ ] **Step 7: Commit** + +```bash +git add src/bin/sg_claw_client.rs Cargo.toml tests/service_task_flow_test.rs +git commit -m "feat: add interactive claw-ws client" +``` + +--- + +## Task 7: Finish wiring, preserve pipe mode, and verify end-to-end behavior + +**Files:** +- Modify: `Cargo.toml` +- Modify: `src/lib.rs` +- Modify: `src/config/settings.rs` +- Optional modify: `src/runtime/engine.rs` +- Reuse: `tests/browser_tool_test.rs`, `tests/runtime_task_flow_test.rs`, `tests/compat_runtime_test.rs` + +- [ ] **Step 1: Add only the minimum config surface for v1** + +Add settings such as: +- `browser_ws_url` defaulting to `ws://127.0.0.1:12345` +- `service_ws_listen_addr` defaulting to local loopback + +Do **not** change the meaning of existing browser backend/profile settings just to represent service mode. + +- [ ] **Step 2: Keep external browser tool naming stable** + +Verify that the runtime still exposes: +- `superrpa_browser` +- `browser_action` + +under both pipe and WS modes where the backend supports them. + +- [ ] **Step 3: Re-run the current pipe regression suite** + +Run: + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test runtime_task_flow_test --test pipe_handshake_test --test pipe_protocol_test --test task_protocol_test +``` + +Expected: all existing pipe-oriented tests still pass unchanged. + +- [ ] **Step 4: Run the new WS-focused suite** + +Run: + +```bash +cargo test --test task_runner_test --test browser_ws_protocol_test --test browser_ws_backend_test --test browser_backend_capability_test --test service_ws_session_test --test service_task_flow_test +``` + +Expected: all new tests pass without launching the real browser. + +- [ ] **Step 5: Run a full Rust test sweep** + +Run: + +```bash +cargo test --tests +``` + +Expected: all Rust tests pass. + +- [ ] **Step 6: Build all three binaries** + +Run: + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: all three binaries compile. + +- [ ] **Step 7: Perform a manual local smoke test** + +Manual test: +1. Start the browser app so `ws://127.0.0.1:12345` is available. +2. Run `cargo run --bin sg_claw`. +3. In another terminal, run `cargo run --bin sg_claw_client`. +4. Submit a simple browser task such as opening a page or fetching visible text. +5. Confirm the client prints streaming logs and exactly one final completion summary. +6. Confirm the old pipe-mode entry still starts via `cargo run`. + +Expected: both modes work side-by-side. + +- [ ] **Step 8: Commit** + +```bash +git add Cargo.toml src/lib.rs src/config/settings.rs src/runtime/engine.rs +git commit -m "feat: wire parallel claw-ws transport" +``` + +--- + +## Verification Checklist + +### Fast regression checks + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test runtime_task_flow_test +``` + +Expected: current pipe/browser runtime behavior remains green. + +### Full Rust test sweep + +```bash +cargo test --tests +``` + +Expected: all Rust tests pass. + +### Binary build verification + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: all three binaries compile. + +### Manual end-to-end verification + +- Browser app listening on `ws://127.0.0.1:12345` +- `cargo run --bin sg_claw` +- `cargo run --bin sg_claw_client` +- submit one browser task +- verify streaming logs, final completion, and single-client lock behavior +- verify `cargo run` still preserves old pipe bootstrap + +--- + +## Notes for Implementation + +- Keep the current pipe bootstrap in `src/lib.rs` intact until the shared runner and pipe backend wrapper are both green. +- Prefer small commits at each task boundary. +- Keep the new WS path additive and isolated. +- Do not ship partial browser capabilities under stable tool names. +- Treat `docs/_tmp_sgbrowser_ws_api_doc.txt` as the browser WS protocol source of truth while implementing `src/browser/ws_protocol.rs`. diff --git a/docs/superpowers/plans/2026-04-02-ws-browser-backend-auth-plan.md b/docs/superpowers/plans/2026-04-02-ws-browser-backend-auth-plan.md new file mode 100644 index 0000000..9615ea4 --- /dev/null +++ b/docs/superpowers/plans/2026-04-02-ws-browser-backend-auth-plan.md @@ -0,0 +1,607 @@ +# WS Browser Backend Auth Replacement Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Replace the ws service path’s empty-session-key `BrowserPipeTool` dependency with a ws-native browser backend path so real browser websocket calls work, while preserving legacy pipe behavior exactly. + +**Architecture:** Keep the existing pipe entry untouched and add a ws-only parallel execution seam. The ws service path will construct a `ServiceBrowserWsClient` plus `WsBrowserBackend`, pass that backend through a new ws-only submit-task entry, and let the existing compat/runtime stack consume `Arc` instead of requiring `BrowserPipeTool` on the ws path. + +**Tech Stack:** Rust 2021, current sgclaw agent/task runner, compat runtime/orchestration stack, `tungstenite`, `serde_json`, existing `MacPolicy`, existing `BrowserBackend`/`WsBrowserBackend`, and the current Rust test suite. + +--- + +## Scope Guardrails + +- Only change the ws service path. +- Do **not** change `src/lib.rs` pipe runtime behavior. +- Do **not** change pipe handshake semantics. +- Do **not** introduce fake session keys, fake HMAC seeds, or auth bypasses. +- Keep legacy `run_submit_task(...)` available for the pipe entry. +- If a shared layer must change, add a parallel ws-only entry instead of weakening the pipe path. +- Keep the current single-client, single-task service model. +- Do not broaden this slice into browser process launch, queueing, multi-client support, or protocol extensions. + +--- + +## File Structure + +### Existing files to modify + +- Modify: `src/agent/task_runner.rs` — keep the current pipe-oriented submit path and add the ws-only backend-based submit path. +- Modify: `src/compat/runtime.rs` — add a backend-driven execution entry that accepts `Arc` directly, while keeping the current pipe-oriented public functions behaviorally unchanged. +- Modify: `src/compat/orchestration.rs` — add a matching backend-driven execution entry for orchestration/direct-route flows, while keeping the current pipe-oriented public functions behaviorally unchanged. +- Modify: `src/compat/workflow_executor.rs` — add backend-driven sibling APIs for direct-route/fallback execution, while keeping the current pipe-oriented public functions behaviorally unchanged. +- Modify: `src/service/server.rs` — replace the ws service’s `BrowserPipeTool::new(..., vec![])` path with a ws-native `WsClient` + `WsBrowserBackend` path. +- Modify: `src/service/mod.rs` — only if minimal re-export or call-signature changes are needed around the new ws-only submit path. +- Modify: `src/browser/mod.rs` — only if export cleanup is truly needed for the service wiring. +- Reuse: `src/agent/mod.rs` — keep the current pipe routing unchanged unless a tiny internal refactor is strictly needed to reuse shared code. +- Reuse: `src/browser/backend.rs` — existing shared browser backend trait. +- Reuse: `src/browser/ws_backend.rs` — existing ws-native browser backend implementation. +- Reuse: `src/browser/ws_protocol.rs` — existing browser websocket protocol codec. +- Reuse: `src/compat/browser_tool_adapter.rs` — should already speak `BrowserBackend`; only touch if a narrow ws regression forces it. +- Reuse: `src/compat/browser_script_skill_tool.rs` — eval-capability gating already exists; only touch if a narrow ws regression forces it. +- Reuse: `src/lib.rs` — pipe entrypoint must remain behaviorally unchanged; verify only. + +### Existing tests to extend + +- Modify: `tests/browser_ws_backend_test.rs` — keep existing ws backend coverage green after the service adapter wiring lands. +- Modify: `tests/browser_script_skill_tool_test.rs` — re-verify eval-gating and browser-script behavior after the shared compat/runtime seam changes. +- Modify: `tests/service_ws_session_test.rs` — update service-side unit/session tests to exercise the ws-only submit path. +- Modify: `tests/service_task_flow_test.rs` — add client→service chain coverage proving the ws path reaches a browser websocket and no longer emits `invalid hmac seed`. +- Modify: `src/service/server.rs` under `#[cfg(test)]` if the private service-side ws adapter cannot be exercised from an integration test crate without changing production visibility. + +### New files to create + +- Create: `tests/browser_ws_service_adapter_test.rs` if the adapter can be exercised through a public seam; otherwise keep the deterministic adapter tests as unit tests in `src/service/server.rs` so no production visibility changes are required. + +--- + +## Task 1: Lock the ws-only behavior with deterministic failing tests + +**Files:** +- Create: `tests/browser_ws_service_adapter_test.rs` +- Modify: `tests/service_task_flow_test.rs` +- Reuse: `tests/browser_ws_backend_test.rs`, `src/browser/ws_backend.rs`, `src/service/server.rs` + +- [ ] **Step 1: Write the first failing backend/adapter test** + +Create `tests/browser_ws_service_adapter_test.rs` with one focused test that directly exercises the ws-service adapter layer, without `sg_claw_client`, without LLM planning, and without natural-language tasks. + +Start with the smallest real behavior from the spec: +- fake browser websocket server accepts one connection +- the ws-service adapter builds the same kind of client the service will use +- `WsBrowserBackend.invoke(Action::Navigate, ...)` succeeds on status `0` +- the fake server receives one text frame that decodes as a ws `Navigate` call + +- [ ] **Step 2: Run that single new test and watch it fail** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test ws_service_backend_navigate_reaches_browser_websocket -- --nocapture +``` + +Expected: FAIL because the service-side ws client/adapter does not exist yet. + +- [ ] **Step 3: Add the second failing deterministic test** + +In the same file, add a test for the forced-close path: +- fake browser websocket server accepts a request, then closes/reset the socket before returning a status frame +- observe the error at the `WsBrowserBackend.invoke(...)` call site +- assert the outward error is exactly `PipeError::PipeClosed` + +- [ ] **Step 4: Run only the forced-close test and watch it fail** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test ws_service_backend_maps_browser_disconnect_to_pipe_closed -- --nocapture +``` + +Expected: FAIL because the service-side ws client/adapter still does not exist. + +- [ ] **Step 5: Add the third failing deterministic test** + +In the same file, add a callback-timeout test: +- fake browser websocket server returns status `0` +- it never returns the callback frame +- assert the outward error at `invoke(...)` is exactly `PipeError::Timeout` + +Use a tiny response timeout in the backend under test. + +- [ ] **Step 6: Run only the callback-timeout test and watch it fail** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test ws_service_backend_times_out_waiting_for_callback -- --nocapture +``` + +Expected: FAIL because the service-side ws client/adapter still does not exist. + +- [ ] **Step 7: Add the end-to-end failing regression for the auth bug** + +Extend `tests/service_task_flow_test.rs` with one client→service integration test that: +- starts a fake browser websocket server +- starts the real `sg_claw` service binary with a temp config pointing `browserWsUrl` to that fake server +- starts the real `sg_claw_client` +- submits the fixed instruction `打开知乎热榜并读取页面主区域文本` +- captures service/client output +- asserts the fake browser server received at least one text frame +- asserts output does **not** contain `invalid hmac seed: session key must not be empty` + +Do not assert planner details here. This test only proves the service path no longer goes through the empty-session-key auth failure. + +- [ ] **Step 8: Run the integration regression and watch it fail** + +Run: + +```bash +cargo test --test service_task_flow_test ws_service_submit_task_no_longer_hits_invalid_hmac_seed -- --nocapture +``` + +Expected: FAIL on the current code because the ws service still constructs `BrowserPipeTool::new(..., vec![])`. + +- [ ] **Step 9: Commit the red tests only after they are all in place** + +Do not commit yet if any required red test was skipped. The next task will make them pass. + +--- + +## Task 2: Add a ws-only browser-backend execution seam without changing the pipe path + +**Files:** +- Modify: `src/agent/task_runner.rs` +- Modify: `src/compat/runtime.rs` +- Modify: `src/compat/orchestration.rs` +- Modify: `src/compat/workflow_executor.rs` +- Reuse: `src/agent/mod.rs`, `src/browser/backend.rs` +- Test: `tests/task_runner_test.rs`, `tests/browser_script_skill_tool_test.rs` + +- [ ] **Step 1: Write the smallest failing runner-level ws entry test** + +Extend `tests/task_runner_test.rs` with a focused test that proves there is a ws-only submit entry accepting `Arc` and an arbitrary event sink, while the old `run_submit_task(...)` signature still exists for pipe mode. + +The test can stay on the missing-LLM-config path so it does not need a real browser call. It should compile only once the new ws-only function exists. + +- [ ] **Step 2: Run the targeted runner test and watch it fail** + +Run: + +```bash +cargo test --test task_runner_test ws_only_submit_task_entry_accepts_browser_backend -- --nocapture +``` + +Expected: FAIL to compile or FAIL to link because the ws-only entry does not exist yet. + +- [ ] **Step 3: Add the new ws-only submit-task entry in `src/agent/task_runner.rs`** + +Keep the current pipe function intact: + +```rust +pub fn run_submit_task(... browser_tool: &BrowserPipeTool, ...) +``` + +Add a parallel entry for the service path, for example: + +```rust +pub fn run_submit_task_with_browser_backend( + sink: &dyn AgentEventSink, + browser_backend: Arc, + context: &AgentRuntimeContext, + request: SubmitTaskRequest, +) -> Result<(), PipeError> +``` + +Rules: +- share as much internal logic as possible with the pipe path +- do not change `run_submit_task(...)` behavior +- do not change `src/agent/mod.rs` pipe wiring except, at most, small internal refactoring to reuse common code + +- [ ] **Step 4: Add a backend-driven compat runtime entry** + +In `src/compat/runtime.rs`, add a parallel entry that accepts `Arc` directly instead of `BrowserPipeTool`. + +Keep the existing pipe-oriented public function in place. + +The backend-driven entry must preserve: +- existing log emission order +- tool names (`superrpa_browser`, `browser_action`) +- existing browser-script tool gating behavior +- existing office/screen tool attachment logic +- existing conversation seeding and provider setup + +- [ ] **Step 5: Add backend-driven orchestration and workflow-executor entries** + +In `src/compat/orchestration.rs`, add the matching backend-driven entry so direct-route flows and fallback flows can run with `Arc` on the ws path. + +In `src/compat/workflow_executor.rs`, add backend-driven sibling APIs for any direct-route/fallback execution that is currently hard-wired to `BrowserPipeTool`. + +Keep the existing pipe-oriented orchestration and workflow-executor public functions in place. + +- [ ] **Step 6: Route the new ws-only submit entry through the backend-driven compat/orchestration/workflow-executor path** + +Inside `src/agent/task_runner.rs`, make the new ws-only submit entry call the new backend-based compat/orchestration functions, while the old pipe entry keeps calling the old pipe-based functions. + +This is the core compatibility seam, and it must cover both normal compat-runtime execution and direct-route/fallback workflow execution. + +- [ ] **Step 7: Re-run the new runner test** + +Run: + +```bash +cargo test --test task_runner_test ws_only_submit_task_entry_accepts_browser_backend -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 8: Re-run the full runner, workflow, and browser-script regressions** + +Run: + +```bash +cargo test --test task_runner_test --test browser_script_skill_tool_test -- --nocapture +``` + +Then run the workflow executor unit coverage that protects direct-route behavior: + +```bash +cargo test compat::workflow_executor::tests -- --nocapture +``` + +Expected: all existing runner, workflow, and browser-script tests still pass, proving the pipe-facing path, direct-route behavior, and eval-gating stayed stable. + +- [ ] **Step 9: Commit** + +```bash +git add src/agent/task_runner.rs src/compat/runtime.rs src/compat/orchestration.rs src/compat/workflow_executor.rs tests/task_runner_test.rs tests/browser_script_skill_tool_test.rs +git commit -m "refactor: add ws-only browser backend submit path" +``` + +--- + +## Task 3: Replace the ws service’s empty-session-key browser tool with a ws-native backend + +**Files:** +- Modify: `src/service/server.rs` +- Modify: `src/service/mod.rs` only if minimal re-export or signature cleanup is required +- Modify: `src/browser/mod.rs` only if export cleanup is needed +- Test: `tests/browser_ws_service_adapter_test.rs` +- Reuse: `src/browser/ws_backend.rs`, `src/browser/ws_protocol.rs` + +- [ ] **Step 1: Write the smallest failing service-side adapter compile target** + +Add a compile-level or construction-level assertion in `tests/browser_ws_service_adapter_test.rs` that the service path can construct the new service-side ws client type used by `serve_client(...)`. + +This should fail until the type exists in `src/service/server.rs`. + +- [ ] **Step 2: Run the adapter test group and watch the constructor test fail** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test -- --nocapture +``` + +Expected: FAIL because the service-side ws client type does not exist yet. + +- [ ] **Step 3: Introduce `ServiceBrowserWsClient` in `src/service/server.rs`** + +Create a narrow client type that owns the real websocket connection to `browser_ws_url` and implements `WsClient`: + +Required responsibilities only: +- lazily connect on first use +- send raw text frames +- receive raw text frames with timeout +- map close/reset to exactly `PipeError::PipeClosed` +- map connect failure to exactly `PipeError::Protocol("browser websocket connect failed: ...")` +- map timeouts to exactly `PipeError::Timeout` + +Do **not** duplicate `WsBrowserBackend` responsibilities here. + +- [ ] **Step 4: Remove ws-path use of `BrowserPipeTool::new(..., vec![])`** + +In `serve_client(...)`, replace this shape: + +```rust +let transport = Arc::new(ServiceBrowserTransport::new(...)); +let browser_tool = BrowserPipeTool::new(transport.clone(), mac_policy.clone(), vec![]) +``` + +with the ws-native shape: + +```rust +let ws_client = Arc::new(ServiceBrowserWsClient::new(...)); +let browser_backend: Arc = Arc::new( + WsBrowserBackend::new(ws_client, mac_policy.clone(), initial_request_url(...)) + .with_response_timeout(BROWSER_RESPONSE_TIMEOUT) +); +``` + +Then route the task through the new ws-only submit entry from Task 2. + +- [ ] **Step 5: Delete or narrow old ws-path transport code that duplicated protocol handling** + +Remove the service-only callback polling / response queue logic that existed solely to feed `BrowserPipeTool`. + +Keep only what is still needed for: +- service client websocket I/O (`sg_claw_client` ↔ `sg_claw`) +- browser websocket I/O (`sg_claw` ↔ `browser_ws_url`) + +Do not leave two competing ws protocol implementations in `src/service/server.rs`. + +- [ ] **Step 6: Re-run deterministic adapter/backend tests** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test -- --nocapture +``` + +Expected: PASS, including: +- navigate success +- disconnect => `PipeError::PipeClosed` +- callback timeout => `PipeError::Timeout` + +- [ ] **Step 7: Re-run existing ws backend tests** + +Run: + +```bash +cargo test --test browser_ws_backend_test -- --nocapture +``` + +Expected: PASS, confirming the service adapter change did not break the existing ws backend semantics. + +- [ ] **Step 8: Commit** + +```bash +git add src/service/server.rs src/service/mod.rs src/browser/mod.rs tests/browser_ws_service_adapter_test.rs +git commit -m "feat: switch ws service to ws-native browser backend" +``` + +--- + +## Task 4: Prove the auth bug is gone and pipe mode is unchanged + +**Files:** +- Modify: `tests/service_ws_session_test.rs` +- Modify: `tests/service_task_flow_test.rs` +- Reuse: `src/lib.rs`, `src/service/mod.rs`, `src/compat/workflow_executor.rs` + +- [ ] **Step 1: Update service session tests for the new ws-only call path** + +Adjust any service session tests that still call `handle_client_message(...)` through the old ws-path `BrowserPipeTool` assumption. + +Prefer one of these narrow approaches: +- overload `handle_client_message(...)` with a backend-based service entry used only in ws tests, or +- keep `handle_client_message(...)` pipe-oriented and test the ws path through `serve_client(...)` and the real service binary instead + +Choose the option that changes the fewest existing tests and leaves the pipe path simplest. + +- [ ] **Step 2: Run the focused service session file** + +Run: + +```bash +cargo test --test service_ws_session_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 3: Make the auth-regression integration test pass** + +Re-run the exact end-to-end regression from Task 1: + +```bash +cargo test --test service_task_flow_test ws_service_submit_task_no_longer_hits_invalid_hmac_seed -- --nocapture +``` + +Expected: PASS, with evidence that: +- the fake browser websocket server received at least one frame +- output no longer contains `invalid hmac seed: session key must not be empty` + +- [ ] **Step 4: Add one explicit mandatory assertion for browser websocket connect failures** + +Add one focused assertion that a browser websocket connect failure surfaces outward as: + +```rust +PipeError::Protocol("browser websocket connect failed: ...") +``` + +Do not leave this semantic implied. + +- [ ] **Step 5: Add one explicit ws direct-route regression** + +Add one focused regression that proves a ws-backed browser backend can traverse a direct-route/fallback path that currently flows through `src/compat/workflow_executor.rs`. + +Keep it deterministic and narrow. Prefer a fake backend plus direct function invocation over a planner-dependent natural-language end-to-end test. + +- [ ] **Step 6: Run the ws-focused regression suite** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test --test browser_ws_backend_test --test browser_ws_protocol_test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Then run the workflow-executor direct-route coverage: + +```bash +cargo test compat::workflow_executor::tests -- --nocapture +``` + +Expected: all ws-focused and direct-route workflow tests pass. + +- [ ] **Step 7: Run the required pipe and browser-script regression suite** + +Run: + +```bash +cargo test --test pipe_handshake_test --test browser_tool_test --test compat_browser_tool_test --test browser_script_skill_tool_test --test runtime_task_flow_test -- --nocapture +``` + +Expected: all required pipe and browser-script regressions pass unchanged. + +- [ ] **Step 8: Run the full relevant verification sweep** + +Run: + +```bash +cargo test --test browser_ws_service_adapter_test --test browser_ws_backend_test --test browser_ws_protocol_test --test service_ws_session_test --test service_task_flow_test --test pipe_handshake_test --test browser_tool_test --test compat_browser_tool_test --test browser_script_skill_tool_test --test runtime_task_flow_test -- --nocapture +``` + +Then run: + +```bash +cargo test compat::workflow_executor::tests -- --nocapture +``` + +Expected: full mixed ws+pipe verification passes in fresh runs. + +- [ ] **Step 9: Build the affected binaries** + +Run: + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: all three binaries compile. + +- [ ] **Step 10: Commit** + +```bash +git add tests/service_ws_session_test.rs tests/service_task_flow_test.rs tests/browser_ws_service_adapter_test.rs src/compat/workflow_executor.rs +git commit -m "test: verify ws auth replacement and pipe regressions" +``` + +--- + +## Task 5: Manual smoke verification against the real browser + +**Files:** +- Reuse only: no code changes unless a verified bug is found during smoke work + +- [ ] **Step 1: Start the real browser websocket target** + +Confirm the real sgBrowser endpoint is reachable at the configured `browserWsUrl`. + +- [ ] **Step 2: Start the real ws service** + +Run: + +```bash +cargo run --bin sg_claw -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Expected: service prints the resolved listen address and browser websocket URL. + +- [ ] **Step 3: Run the minimal browser task through the real client** + +Run from a separate terminal with UTF-8-safe input: + +```bash +cargo run --bin sg_claw_client -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Submit: + +```text +打开知乎热榜并读取页面主区域文本 +``` + +Expected: +- browser actions start executing +- no `invalid hmac seed: session key must not be empty` +- one final completion is returned + +- [ ] **Step 4: Run the old Zhihu skill smoke** + +Submit: + +```text +读取知乎热榜数据,并导出 excel 文件 +``` + +Expected: the task enters the real browser action path instead of dying at auth initialization. + +- [ ] **Step 5: Re-check the legacy pipe entry without modifying it** + +Run: + +```bash +cargo run +``` + +Only verify startup behavior appropriate for the current pipe environment. Do not change pipe code during this smoke step. + +- [ ] **Step 6: If a smoke failure appears, stop and debug before editing** + +Any failure found here must be handled with: +- a fresh reproducer +- a failing automated test if feasible +- the smallest scoped fix + +Do not fold speculative smoke fixes into this slice. + +--- + +## Verification Checklist + +### Deterministic ws-only tests + +```bash +cargo test --test browser_ws_service_adapter_test --test browser_ws_backend_test --test browser_ws_protocol_test -- --nocapture +``` + +Expected: ws-native backend and service adapter semantics are green without LLM/planner dependencies. + +### Client→service ws chain tests + +```bash +cargo test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Expected: the ws service path reaches the browser websocket and no longer emits the empty-session-key auth failure. + +### Required pipe and browser-script regressions + +```bash +cargo test --test pipe_handshake_test --test browser_tool_test --test compat_browser_tool_test --test browser_script_skill_tool_test --test runtime_task_flow_test -- --nocapture +``` + +Expected: legacy pipe behavior and browser-script eval-gating remain unchanged. + +### Binary build verification + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: all affected binaries compile. + +### Manual end-to-end verification + +- real sgBrowser running at configured `browserWsUrl` +- `cargo run --bin sg_claw -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json"` +- `cargo run --bin sg_claw_client -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json"` +- run the Zhihu minimal task +- run the old Zhihu export task +- verify no `invalid hmac seed` appears +- verify pipe startup still behaves as before + +--- + +## Notes for Implementation + +- Keep the current pipe bootstrap in `src/lib.rs` untouched. +- Prefer adding ws-only functions over changing existing pipe signatures. +- Reuse `WsBrowserBackend` for protocol semantics; do not re-implement callback handling inside the service. +- Keep `ServiceBrowserWsClient` narrow: connection lifecycle + raw websocket I/O only. +- Preserve exact outward error semantics from the spec: + - connect failure => `PipeError::Protocol("browser websocket connect failed: ...")` + - non-zero status => `PipeError::Protocol("browser returned non-zero status: ...")` + - callback timeout => `PipeError::Timeout` + - close/reset => `PipeError::PipeClosed` +- Do not claim success until the mixed ws+pipe verification commands have been run fresh. diff --git a/docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md b/docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md new file mode 100644 index 0000000..0132bb9 --- /dev/null +++ b/docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md @@ -0,0 +1,482 @@ +# WS Browser Bridge Path Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Replace the raw-ws-direct browser execution assumption with a bridge-backed browser integration path that matches the validated FunctionsUI / BrowserAction / CommandRouter model while preserving existing pipe behavior. + +**Architecture:** Keep the current Rust-side browser orchestration flow centered on `Arc`, but stop treating `WsBrowserBackend` as the real production browser surface. Model the validated bridge as two explicit layers: Layer 1 session/lifecycle calls (`sgclawConnect`, `sgclawStart`, `sgclawStop`, `sgclawSubmitTask`) and Layer 2 browser-action execution (`window.sgFunctionsUI(...)`, `window.BrowserAction(...)`, `CommandRouter`). The new backend targets Layer 2 only through a narrow repo-local `BridgeActionTransport` seam, while lifecycle/session concerns stay separate from per-action browser execution. + +**Tech Stack:** Rust 2021, existing `BrowserBackend` abstraction, compat/runtime/orchestration stack, current service/task runner integration, existing bridge-oriented design docs, existing Rust unit/integration test suite. + +--- + +## Scope Guardrails + +- Do **not** continue extending raw external sgBrowser websocket business-frame handling as the mainline path. +- Do **not** modify `src/lib.rs`, pipe handshake behavior, or the working `BrowserPipeTool` path. +- Do **not** invent a parallel browser-command contract unrelated to the documented bridge surface. +- Do **not** rewrite the whole compat/runtime stack when a narrow adapter will do. +- Do **not** assume access to the full SuperRPA browser-host codebase from this repository; encode the validated contract at the nearest seam available here. + +--- + +## File Structure + +### Existing files to modify + +- Modify: `src/browser/mod.rs` + - export the new bridge contract/transport/backend modules explicitly +- Modify: `src/browser/backend.rs` + - only if a tiny shared helper or trait documentation update is needed for the new bridge-backed backend +- Modify: `src/compat/browser_tool_adapter.rs` + - ensure existing browser action mapping remains reusable with the new backend implementation +- Modify: `src/compat/runtime.rs` + - wire the bridge-backed browser backend into the ws service/browser execution path without changing the pipe path +- Modify: `src/compat/orchestration.rs` + - only where browser backend wiring requires the bridge-backed path to flow through orchestration +- Modify: `src/compat/workflow_executor.rs` + - preserve direct-route/fallback use of `BrowserBackend` when the backend is bridge-backed instead of websocket-backed +- Modify: `src/service/server.rs` + - replace the current real-browser execution assumption with bridge-backend construction plus a repo-local bridge transport provider seam for the relevant service path +- Modify: `tests/compat_browser_tool_test.rs` + - extend browser tool mapping coverage if needed for bridge-backed execution +- Modify: `tests/service_task_flow_test.rs` + - replace raw-ws-direct expectations with bridge-path expectations where appropriate +- Modify: `tests/service_ws_session_test.rs` + - update service-side tests if they currently assume the real browser path is raw websocket driven + +### New files to create + +- Create: `src/browser/bridge_contract.rs` + - narrow, explicit contract types that keep lifecycle/session bridge calls separate from browser-action execution requests/replies +- Create: `src/browser/bridge_transport.rs` + - repo-local `BridgeActionTransport` seam used by the backend and injected by service/runtime wiring +- Create: `src/browser/bridge_backend.rs` + - new `BrowserBackend` implementation that maps browser actions onto the Layer-2 bridge action contract through `BridgeActionTransport` +- Create: `tests/browser_bridge_backend_test.rs` + - deterministic unit tests for action-to-bridge mapping and reply/error normalization using a fake bridge transport +- Create: `tests/browser_bridge_contract_test.rs` + - narrow tests proving the two bridge layers stay explicit and browser-action requests remain semantic rather than raw-websocket-shaped + +### Evidence files to consult during implementation + +- Read: `docs/_tmp_sgbrowser_ws_probe_transcript.md` +- Read: `frontend/archive/sgClaw验证-已归档/testRunner.js` +- Read: `docs/superpowers/specs/2026-03-25-superrpa-sgclaw-browser-control-design.md` +- Read: `docs/archive/项目管理与排期/协作时间表.md` +- Read: `docs/plans/2026-03-27-sgclaw-floating-chat-frontend-design.md` + +--- + +## Task 1: Lock the bridge contract in deterministic tests before adding the backend + +**Files:** +- Create: `src/browser/bridge_contract.rs` +- Create: `tests/browser_bridge_contract_test.rs` +- Reuse as design evidence: + - `frontend/archive/sgClaw验证-已归档/testRunner.js` + - `docs/archive/项目管理与排期/协作时间表.md` + - `docs/plans/2026-03-27-sgclaw-floating-chat-frontend-design.md` + +- [ ] **Step 1: Write the first failing contract test for named bridge calls** + +Create `tests/browser_bridge_contract_test.rs` with one focused test that encodes the bridge naming expectations already evidenced in the repo. + +Start with a test shape like: + +```rust +#[test] +fn bridge_contract_names_match_documented_bridge_surface() { + // assert the contract contains the exact bridge action names +} +``` + +Required expectations: +- `sgclawConnect` +- `sgclawStart` +- `sgclawStop` +- `sgclawSubmitTask` +- these names live in an explicit lifecycle/session contract type, not in the browser-action request type + +Do **not** invent additional action names in this first test. + +- [ ] **Step 2: Run the single contract test and verify it fails** + +Run: + +```bash +cargo test --test browser_bridge_contract_test bridge_contract_names_match_documented_bridge_surface -- --nocapture +``` + +Expected: FAIL because `src/browser/bridge_contract.rs` does not exist yet. + +- [ ] **Step 3: Add the second failing contract test for browser-action request shaping** + +In the same file, add one focused test proving the bridge contract can represent a browser action request without leaking raw websocket business-frame semantics. + +Test shape: + +```rust +#[test] +fn bridge_contract_represents_browser_action_requests_without_ws_business_frames() { + // create a click/navigate/getText style action request and assert shape +} +``` + +Required assertions: +- request shape identifies the intended browser action semantically +- request shape is distinct from the lifecycle/session bridge call type +- request shape does **not** embed `sgBrowerserOpenPage`, `callBackJsToCpp`, or other raw websocket business-frame names + +- [ ] **Step 4: Run the second contract test and verify it fails** + +Run: + +```bash +cargo test --test browser_bridge_contract_test bridge_contract_represents_browser_action_requests_without_ws_business_frames -- --nocapture +``` + +Expected: FAIL because the bridge contract does not exist yet. + +- [ ] **Step 5: Implement the minimal bridge contract module** + +Create `src/browser/bridge_contract.rs` with only the types needed by the tests. + +Recommended shape: + +```rust +pub enum BridgeLifecycleCall { + Connect, + Start, + Stop, + SubmitTask, +} + +impl BridgeLifecycleCall { + pub fn bridge_name(&self) -> &'static str { + match self { + Self::Connect => "sgclawConnect", + Self::Start => "sgclawStart", + Self::Stop => "sgclawStop", + Self::SubmitTask => "sgclawSubmitTask", + } + } +} + +pub struct BridgeBrowserActionRequest { + pub action: String, + pub params: serde_json::Value, + pub expected_domain: String, +} +``` + +Rules: +- model the documented bridge/lifecycle naming explicitly +- keep the browser action request semantic, not websocket-frame-shaped +- keep the module small and repository-local + +- [ ] **Step 6: Re-run the contract tests** + +Run: + +```bash +cargo test --test browser_bridge_contract_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 7: Commit** + +```bash +git add src/browser/bridge_contract.rs tests/browser_bridge_contract_test.rs +git commit -m "test: define sgClaw bridge contract surface" +``` + +--- + +## Task 2: Add the repo-local transport seam and bridge-backed `BrowserBackend` + +**Files:** +- Create: `src/browser/bridge_transport.rs` +- Create: `src/browser/bridge_backend.rs` +- Create: `tests/browser_bridge_backend_test.rs` +- Reuse: `src/browser/backend.rs` +- Reuse: `src/browser/bridge_contract.rs` +- Reuse: `src/compat/browser_tool_adapter.rs` + +- [ ] **Step 1: Write the first failing backend test for action mapping** + +Create `tests/browser_bridge_backend_test.rs` with one focused test proving a `BrowserBackend` action is translated into the bridge contract request shape. + +Start with a narrow action such as `Action::Navigate`. + +Required assertions: +- `Action::Navigate` becomes one semantic bridge browser-action request +- the request preserves action parameters and expected domain +- the test does **not** assert any raw websocket payload strings + +- [ ] **Step 2: Run the first backend test and verify it fails** + +Run: + +```bash +cargo test --test browser_bridge_backend_test bridge_backend_maps_navigate_to_bridge_action_request -- --nocapture +``` + +Expected: FAIL because `src/browser/bridge_backend.rs` does not exist yet. + +- [ ] **Step 3: Add the second failing backend test for reply normalization** + +Add one focused test proving the backend can normalize a successful bridge reply into the existing `CommandOutput` shape expected by `BrowserBackend` callers. + +- [ ] **Step 4: Run the second backend test and verify it fails** + +Run: + +```bash +cargo test --test browser_bridge_backend_test bridge_backend_normalizes_successful_bridge_reply -- --nocapture +``` + +Expected: FAIL because the backend does not exist yet. + +- [ ] **Step 5: Add the third failing backend test for bridge-side errors** + +Add one focused test proving a bridge-side error normalizes into the correct outward `PipeError` semantics for backend callers. + +- [ ] **Step 6: Run the error-path test and verify it fails** + +Run: + +```bash +cargo test --test browser_bridge_backend_test bridge_backend_maps_bridge_failure_to_pipe_error -- --nocapture +``` + +Expected: FAIL because the backend does not exist yet. + +- [ ] **Step 7: Implement the minimal transport seam and bridge backend** + +Create `src/browser/bridge_transport.rs` and `src/browser/bridge_backend.rs`. + +The transport seam must: +- define the repo-local `BridgeActionTransport` contract used for Layer-2 browser-action execution only +- accept semantic `BridgeBrowserActionRequest` values and return semantic success/error replies +- remain small, explicit, and easy to fake in tests + +The backend must: +- implement the existing `BrowserBackend` trait +- translate supported actions into `BridgeBrowserActionRequest` +- depend on `BridgeActionTransport` instead of raw websocket payload building +- normalize success/error replies into existing backend-facing result types + +Rules: +- do not embed raw websocket business-frame names +- do not change `BrowserBackend` semantics for existing callers +- do not pull lifecycle/session bridge calls into this backend layer + +- [ ] **Step 8: Re-run the bridge backend tests** + +Run: + +```bash +cargo test --test browser_bridge_backend_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 9: Re-run browser tool adapter coverage** + +Run: + +```bash +cargo test --test compat_browser_tool_test -- --nocapture +``` + +Expected: PASS, proving the existing browser action mapping remains reusable with the new backend. + +- [ ] **Step 10: Commit** + +```bash +git add src/browser/bridge_transport.rs src/browser/bridge_backend.rs tests/browser_bridge_backend_test.rs src/compat/browser_tool_adapter.rs src/browser/mod.rs +git commit -m "feat: add bridge-backed browser backend" +``` + +--- + +## Task 3: Wire the bridge-backed backend into the real-browser service path + +**Files:** +- Modify: `src/service/server.rs` +- Modify: `src/compat/runtime.rs` +- Modify: `src/compat/orchestration.rs` +- Modify: `src/compat/workflow_executor.rs` +- Modify: `tests/service_task_flow_test.rs` +- Modify: `tests/service_ws_session_test.rs` +- Reuse: `src/browser/bridge_backend.rs` +- Reuse: `src/browser/bridge_contract.rs` +- Reuse: `src/browser/bridge_transport.rs` + +- [ ] **Step 1: Write the first failing service-path test for bridge backend construction** + +Add or update one focused service test proving the real-browser execution path constructs and uses the bridge-backed backend instead of the raw websocket backend assumption. + +The test should observe backend selection at the nearest possible seam. + +- [ ] **Step 2: Run the focused service test and verify it fails** + +Run the narrowest affected service test command. + +Expected: FAIL because the service path is not wired to the bridge backend yet. + +- [ ] **Step 3: Add the minimal service/runtime wiring** + +Change the relevant service/browser execution path so it constructs the new bridge-backed backend, injects the repo-local bridge transport provider at the nearest seam, and passes the backend through the existing runtime/orchestration flow. + +Rules: +- keep the pipe path unchanged +- keep changes localized +- keep lifecycle/session bridge handling separate from per-action browser execution +- preserve existing runtime log and task flow behavior where possible + +- [ ] **Step 4: Add one direct-route/fallback regression** + +Add one focused regression proving a bridge-backed backend still works through the direct-route or fallback path exercised by `src/compat/workflow_executor.rs`. + +- [ ] **Step 5: Run the bridge-focused service tests** + +Run: + +```bash +cargo test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 6: Re-run workflow/runtime regressions** + +Run: + +```bash +cargo test compat::workflow_executor::tests -- --nocapture +cargo test --test compat_browser_tool_test --test browser_script_skill_tool_test --test task_runner_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 7: Commit** + +```bash +git add src/service/server.rs src/compat/runtime.rs src/compat/orchestration.rs src/compat/workflow_executor.rs tests/service_ws_session_test.rs tests/service_task_flow_test.rs +git commit -m "refactor: route real browser path through bridge backend" +``` + +--- + +## Task 4: Verify bridge-path behavior without pipe regression + +**Files:** +- Reuse only unless a failing test proves a minimal fix is still needed + +- [ ] **Step 1: Run bridge/backend unit coverage** + +Run: + +```bash +cargo test --test browser_bridge_contract_test --test browser_bridge_backend_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 2: Run service/runtime bridge-path regressions** + +Run: + +```bash +cargo test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 3: Run required pipe regressions** + +Run: + +```bash +cargo test --test pipe_handshake_test --test browser_tool_test --test compat_browser_tool_test --test browser_script_skill_tool_test --test runtime_task_flow_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 4: Build the affected binaries** + +Run: + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: PASS. + +- [ ] **Step 5: Stop if any regression points back to raw websocket assumptions** + +If any test still encodes raw external websocket business-frame assumptions as the real-browser path, update that test to the bridge-backed design rather than patching production code to satisfy the old assumption. + +- [ ] **Step 6: Commit** + +```bash +git add tests/browser_bridge_contract_test.rs tests/browser_bridge_backend_test.rs tests/service_ws_session_test.rs tests/service_task_flow_test.rs +git commit -m "test: verify bridge path and preserve pipe behavior" +``` + +--- + +## Verification Checklist + +### Bridge contract tests + +```bash +cargo test --test browser_bridge_contract_test -- --nocapture +``` + +Expected: documented bridge names and semantic browser-action request shaping are locked. + +### Bridge backend tests + +```bash +cargo test --test browser_bridge_backend_test -- --nocapture +``` + +Expected: backend action mapping and reply/error normalization are green. + +### Service/runtime integration tests + +```bash +cargo test --test service_ws_session_test --test service_task_flow_test -- --nocapture +cargo test compat::workflow_executor::tests -- --nocapture +``` + +Expected: real-browser path uses the bridge-backed backend and direct-route/fallback behavior remains intact. + +### Pipe regressions + +```bash +cargo test --test pipe_handshake_test --test browser_tool_test --test compat_browser_tool_test --test browser_script_skill_tool_test --test runtime_task_flow_test -- --nocapture +``` + +Expected: pipe path remains unchanged. + +### Binary build verification + +```bash +cargo build --bin sgclaw --bin sg_claw --bin sg_claw_client +``` + +Expected: affected binaries compile. + +--- + +## Notes for Implementation + +- The websocket probe work stays in the repository as diagnostic tooling; do not repurpose it into the bridge adapter. +- `docs/_tmp_sgbrowser_ws_probe_transcript.md` is evidence that rejected the raw-ws-direct assumption, not a contract to keep satisfying. +- Favor one narrow bridge-backed backend over broad runtime rewrites. +- If the nearest repo-local seam is still slightly abstract because the external SuperRPA host code is outside this repository, make that abstraction explicit and test it rather than guessing hidden behavior. \ No newline at end of file diff --git a/docs/superpowers/plans/2026-04-03-ws-browser-integration-surface-correction-plan.md b/docs/superpowers/plans/2026-04-03-ws-browser-integration-surface-correction-plan.md new file mode 100644 index 0000000..d9208ce --- /dev/null +++ b/docs/superpowers/plans/2026-04-03-ws-browser-integration-surface-correction-plan.md @@ -0,0 +1,566 @@ +# WS Browser Integration Surface Correction Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Replace the unvalidated raw-ws-direct assumption with an evidence-backed decision: either prove a minimal sgBrowser bootstrap sequence for raw websocket control, or pivot to the real browser bridge surface. + +**Architecture:** Treat the existing ws-native backend as a protocol/testing asset, not as a validated production integration surface. First build a narrow probe/validation harness that can run candidate bootstrap sequences and capture exact live transcripts from the real endpoint. Then branch decisively: if a reproducible bootstrap sequence yields real status/callback frames, implement that bootstrap path; otherwise stop raw-ws speculation and write the bridge-first implementation slice. + +**Tech Stack:** Rust 2021, existing `src/browser/ws_protocol.rs` / `src/browser/ws_backend.rs`, service websocket infrastructure, `tungstenite`, `serde_json`, current Rust test suite, local sgBrowser websocket documentation. + +--- + +## Scope Guardrails + +- Do **not** add more speculative production fixes to `src/service/server.rs` just to “try one more thing.” +- Do **not** claim raw websocket is the supported path unless the live probe transcript proves it. +- Do **not** modify `src/lib.rs`, pipe handshake behavior, or the pipe browser-tool path. +- Do **not** implement both the bootstrap architecture and the bridge architecture in the same branch. +- Keep the ws-native code unless and until the bridge decision makes specific pieces obsolete. +- Prefer a dedicated probe surface over embedding validation logic into production request handling. + +--- + +## File Structure + +### Existing files to modify + +- Modify: `src/browser/mod.rs` + - export the new `ws_probe` module so both tests and the probe binary use the same library surface +- Modify: `src/browser/ws_protocol.rs` + - only if a tiny helper extraction is required for test/probe readability + - do not change existing protocol semantics in this slice +- Modify: `tests/browser_ws_protocol_test.rs` + - add deterministic coverage for any extracted helper used by the probe harness + +### New files to create + +- Create: `src/bin/sgbrowser_ws_probe.rs` + - standalone diagnostic binary for ordered frame-script probing against a live sgBrowser websocket endpoint +- Create: `src/browser/ws_probe.rs` + - small reusable probe/transcript module, if needed, to keep the binary and tests focused +- Create: `tests/browser_ws_probe_test.rs` + - deterministic fake-server tests for transcript capture, timeout reporting, and scripted sequence execution +- Create: `docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md` **only if Option B wins after probing** + - follow-up bridge design, not part of the initial coding slice +- Create: `docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md` **only if Option B wins after probing** + - follow-up bridge implementation plan, not part of the initial coding slice +- Create: `docs/_tmp_sgbrowser_ws_probe_transcript.md` + - temporary evidence artifact capturing the real endpoint probe matrix and outcomes + +### Files deliberately not changed in the initial slice + +- `src/lib.rs` +- `src/agent/task_runner.rs` +- `src/compat/runtime.rs` +- `src/compat/orchestration.rs` +- `src/compat/workflow_executor.rs` +- `src/browser/ws_backend.rs` + +Unless the probe results prove a real bootstrap contract, these files stay untouched. + +--- + +## Task 1: Build a deterministic websocket probe harness before touching production behavior + +**Files:** +- Create: `src/browser/ws_probe.rs` +- Create: `tests/browser_ws_probe_test.rs` +- Reuse: `src/browser/ws_protocol.rs` + +- [ ] **Step 1: Write the first failing transcript test** + +Create `tests/browser_ws_probe_test.rs` with one focused fake-server test that executes a scripted sequence of outgoing text frames and records all received text frames in order. + +Start with this shape: + +```rust +#[test] +fn probe_records_welcome_then_silence_transcript() { + // fake server sends one welcome frame and then stays silent + // probe result should preserve that exact transcript and mark timeout/silence explicitly +} +``` + +Required assertions: +- the probe can connect to the fake websocket server +- it can send a scripted first frame +- it records the first inbound text frame exactly +- it returns a transcript/result object that distinguishes timeout from protocol parse failure + +- [ ] **Step 2: Run the single new test and verify it fails** + +Run: + +```bash +cargo test --test browser_ws_probe_test probe_records_welcome_then_silence_transcript -- --nocapture +``` + +Expected: FAIL because the probe harness does not exist yet. + +- [ ] **Step 3: Add the second failing probe test for ordered multi-step scripts** + +In the same file, add a test proving the harness can run multiple outgoing frames in a fixed order and keep the transcript segmented by step. + +Test shape: + +```rust +#[test] +fn probe_runs_ordered_frame_script_and_records_per_step_results() { + // send bootstrap frame 1, bootstrap frame 2, then minimal action + // fake server replies differently at each step + // probe result preserves exact order and outcomes +} +``` + +Required assertions: +- outgoing frames are sent in the configured order +- inbound frames are attached to the correct step +- the probe can stop the sequence on timeout/close if configured + +- [ ] **Step 4: Run the ordered-script test and verify it fails** + +Run: + +```bash +cargo test --test browser_ws_probe_test probe_runs_ordered_frame_script_and_records_per_step_results -- --nocapture +``` + +Expected: FAIL because the probe harness does not exist yet. + +- [ ] **Step 5: Add the third failing probe test for close/reset visibility** + +Add one focused fake-server test that closes the connection after a script step and asserts the transcript reports close/reset rather than generic timeout. + +- [ ] **Step 6: Run the close/reset test and verify it fails** + +Run: + +```bash +cargo test --test browser_ws_probe_test probe_reports_socket_close_separately_from_timeout -- --nocapture +``` + +Expected: FAIL because the probe harness does not exist yet. + +- [ ] **Step 7: Implement the minimal probe module** + +Create `src/browser/ws_probe.rs` with only the types and behavior needed by the tests. + +Recommended shape: + +```rust +pub struct ProbeStep { + pub label: String, + pub payload: String, + pub expect_reply: bool, +} + +pub enum ProbeOutcome { + Received(Vec), + TimedOut, + Closed, + ConnectFailed(String), +} + +pub struct ProbeStepResult { + pub label: String, + pub sent: String, + pub outcome: ProbeOutcome, +} + +pub fn run_probe_script(/* ws url, timeout, steps */) -> Result, ProbeError> { + // connect, send ordered frames, collect exact transcript +} +``` + +Rules: +- do not parse business meaning yet +- do not mix this into normal task execution +- preserve exact raw text frames in transcript results +- keep the module small and diagnostic-oriented + +- [ ] **Step 8: Re-run the new probe tests** + +Run: + +```bash +cargo test --test browser_ws_probe_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 9: Commit** + +```bash +git add src/browser/ws_probe.rs tests/browser_ws_probe_test.rs +git commit -m "test: add sgBrowser websocket probe harness" +``` + +--- + +## Task 2: Add a standalone probe binary for live sgBrowser evidence collection + +**Files:** +- Create: `src/bin/sgbrowser_ws_probe.rs` +- Create: `src/browser/ws_probe.rs` +- Modify: `src/browser/mod.rs` +- Create: `tests/browser_ws_probe_test.rs` + +- [ ] **Step 1: Write the failing helper parser test** + +In `tests/browser_ws_probe_test.rs`, add one focused test for a new helper function in `src/browser/ws_probe.rs`: + +```rust +#[test] +fn parse_probe_args_accepts_ws_url_timeout_and_ordered_steps() { + // parse a fixed argv-style slice into a ProbeCliConfig +} +``` + +Create and use this exact helper shape: + +```rust +pub struct ProbeCliConfig { + pub ws_url: String, + pub timeout_ms: u64, + pub steps: Vec, +} + +pub fn parse_probe_args(args: &[String]) -> Result +``` + +The test must assert that these exact arguments parse successfully and preserve step order: + +```text +--ws-url ws://127.0.0.1:12345 +--timeout-ms 1500 +--step open-agent::["about:blank","sgOpenAgent"] +--step open-hot::["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +``` + +- [ ] **Step 2: Run the parser test and verify it fails** + +Run: + +```bash +cargo test --test browser_ws_probe_test parse_probe_args_accepts_ws_url_timeout_and_ordered_steps -- --nocapture +``` + +Expected: FAIL because `parse_probe_args(...)` and `ProbeCliConfig` do not exist yet. + +- [ ] **Step 3: Implement the helper and binary together** + +In `src/browser/ws_probe.rs`, add `ProbeCliConfig` and `parse_probe_args(...)`. + +In `src/browser/mod.rs`, add the module export: + +```rust +pub mod ws_probe; +``` + +In `src/bin/sgbrowser_ws_probe.rs`, implement the binary using only `std::env::args()` plus `parse_probe_args(...)`. + +Required behavior: +- accepts a websocket URL +- accepts a timeout in milliseconds +- accepts repeated ordered steps +- runs the probe harness +- prints a markdown-friendly transcript including: + - step label + - exact sent payload + - exact received frames, if any + - timeout/close outcome + +Output shape can be simple, for example: + +```text +STEP 1 bootstrap-open-agent +SEND: ["about:blank","sgOpenAgent"] +RECV: Welcome! You are client #1 +OUTCOME: timeout +``` + +Rules: +- no production/browser-runtime integration +- no hidden fallback logic +- no “best effort” guessing of next steps + +- [ ] **Step 4: Re-run the parser/helper test** + +Run: + +```bash +cargo test --test browser_ws_probe_test parse_probe_args_accepts_ws_url_timeout_and_ordered_steps -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 5: Build the probe binary** + +Run: + +```bash +cargo build --bin sgbrowser_ws_probe +``` + +Expected: PASS. + +- [ ] **Step 6: Commit** + +```bash +git add src/bin/sgbrowser_ws_probe.rs src/browser/ws_probe.rs src/browser/mod.rs tests/browser_ws_probe_test.rs +git commit -m "feat: add live sgBrowser websocket probe binary" +``` + +--- + +## Task 3: Run the real endpoint probe matrix and write the evidence transcript + +**Files:** +- Create: `docs/_tmp_sgbrowser_ws_probe_transcript.md` +- Reuse only: `src/bin/sgbrowser_ws_probe.rs`, `docs/_tmp_sgbrowser_ws_api_doc.txt` + +- [ ] **Step 1: Run the no-bootstrap baseline probe** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "baseline-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact output under a `## baseline-open` heading in `docs/_tmp_sgbrowser_ws_probe_transcript.md`. + +- [ ] **Step 2: Run the documented `sgOpenAgent` candidate** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "open-agent::[\"about:blank\",\"sgOpenAgent\"]" --step "post-open-agent-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact output under a `## open-agent` heading. + +- [ ] **Step 3: Run the documented `sgSetAuthInfo` candidate** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "set-auth::[\"about:blank\",\"sgSetAuthInfo\",\"probe-user\",\"probe-token\"]" --step "post-set-auth-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact output under a `## set-auth` heading. + +- [ ] **Step 4: Run the documented `sgBrowserLogin` candidate** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "browser-login::{\"request\":\"use-json-helper\"}" +``` + +Before running, replace the placeholder payload with the exact JSON-array frame produced by the helper for: + +```json +["about:blank","sgBrowserLogin",{"appName":"probe","userName":"probe","orgName":"probe","menus":[{"name":"probe","normalImg":"x","activeImg":"x","url":"https://www.zhihu.com/hot"}]}] +``` + +Then add a second step in the same command: + +```json +["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"] +``` + +Append the exact output under a `## browser-login` heading. + +- [ ] **Step 5: Run the documented `sgBrowerserActiveTab` candidate** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "active-tab::[\"about:blank\",\"sgBrowerserActiveTab\",\"https://www.zhihu.com/hot\",\"probeCallback\"]" --step "post-active-tab-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact output under a `## active-tab` heading. + +- [ ] **Step 6: Run one combined bootstrap candidate** + +Run exactly: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "combined-open-agent::[\"about:blank\",\"sgOpenAgent\"]" --step "combined-active-tab::[\"about:blank\",\"sgBrowerserActiveTab\",\"https://www.zhihu.com/hot\",\"probeCallback\"]" --step "combined-open::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact output under a `## combined-bootstrap` heading. + +- [ ] **Step 7: Run `requesturl` variants for the minimal action** + +Run exactly these two additional commands: + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "target-as-requesturl::[\"https://www.zhihu.com/hot\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +```bash +cargo run --bin sgbrowser_ws_probe -- --ws-url "ws://127.0.0.1:12345" --timeout-ms 1500 --step "baidu-requesturl::[\"https://www.baidu.com\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" +``` + +Append the exact outputs under `## requesturl-variants`. + +- [ ] **Step 8: Summarize the matrix in the transcript file** + +At the end of `docs/_tmp_sgbrowser_ws_probe_transcript.md`, add this exact table template and fill it in: + +```markdown +| Sequence | Sent frames | First reply | Final outcome | Decision signal | +| --- | --- | --- | --- | --- | +``` + +- [ ] **Step 9: Determine which architecture option wins** + +Decision rule: +- if at least one sequence reproducibly yields real numeric status and/or callback frames for a real business action, Option A (bootstrap-validated raw websocket) wins +- otherwise, Option B (bridge-first) wins + +Do not weaken this decision rule. + +- [ ] **Step 10: Commit the evidence artifact** + +```bash +git add docs/_tmp_sgbrowser_ws_probe_transcript.md +git commit -m "docs: capture sgBrowser websocket probe evidence" +``` + +--- + +## Task 4A: If Option A wins, write the narrow bootstrap implementation slice + +**Files:** +- Create: `docs/superpowers/specs/2026-04-03-ws-browser-bootstrap-contract-design.md` +- Create: `docs/superpowers/plans/2026-04-03-ws-browser-bootstrap-contract-plan.md` +- Reuse as evidence input: + - `docs/_tmp_sgbrowser_ws_probe_transcript.md` + - `docs/_tmp_sgbrowser_ws_api_doc.txt` + - `src/browser/ws_backend.rs` + - `src/browser/ws_protocol.rs` + +- [ ] **Step 1: Write one new design doc capturing the proven bootstrap contract** + +Create: + +```text +docs/superpowers/specs/2026-04-03-ws-browser-bootstrap-contract-design.md +``` + +Include: +- exact validated sequence +- exact required state (`requesturl`, active tab, agent page, auth payload) +- exact failure semantics +- why this is sufficient evidence to keep raw websocket as the product surface + +- [ ] **Step 2: Write one new implementation plan for the bootstrap path** + +Create: + +```text +docs/superpowers/plans/2026-04-03-ws-browser-bootstrap-contract-plan.md +``` + +Plan only the minimal production changes required to embed the validated bootstrap sequence into the service/browser path. + +- [ ] **Step 3: Commit the bootstrap decision docs** + +```bash +git add docs/superpowers/specs/2026-04-03-ws-browser-bootstrap-contract-design.md docs/superpowers/plans/2026-04-03-ws-browser-bootstrap-contract-plan.md +git commit -m "docs: capture ws browser bootstrap contract" +``` + +- [ ] **Step 4: Stop after writing the bootstrap plan** + +Do not begin production implementation in the same slice unless the user explicitly asks for execution. + +--- + +## Task 4B: If Option B wins, write the bridge-first implementation slice + +**Files:** +- Create: `docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md` +- Create: `docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md` +- Reuse as evidence input: + - `docs/_tmp_sgbrowser_ws_probe_transcript.md` + - `frontend/archive/sgClaw验证-已归档/testRunner.js` + - `docs/superpowers/specs/2026-03-25-superrpa-sgclaw-browser-control-design.md` + - `docs/archive/项目管理与排期/协作时间表.md` + - `docs/plans/2026-03-27-sgclaw-floating-chat-frontend-design.md` + +- [ ] **Step 1: Write the bridge-path design doc** + +Create `docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md`. + +The design must specify: +- why raw websocket is considered non-validated for external control +- which bridge surface becomes authoritative +- where sgClaw should integrate (`FunctionsUI`, host bridge, `BrowserAction`, `CommandRouter`, or the nearest validated seam in this repo) +- how to preserve pipe behavior and existing abstractions where practical + +- [ ] **Step 2: Write the bridge-path implementation plan** + +Create `docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md`. + +The plan must: +- identify exact files to touch +- describe the narrowest adapter implementation +- keep TDD/task granularity as in this document +- avoid speculative work outside the bridge slice + +- [ ] **Step 3: Commit the bridge decision docs** + +```bash +git add docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md docs/superpowers/plans/2026-04-03-ws-browser-bridge-path-plan.md +git commit -m "docs: define bridge-first sgBrowser integration" +``` + +- [ ] **Step 4: Stop after writing the bridge plan** + +Do not start the bridge implementation in the same slice unless the user explicitly asks for execution. + +--- + +## Verification Checklist + +### Deterministic probe harness tests + +```bash +cargo test --test browser_ws_probe_test -- --nocapture +``` + +Expected: transcript capture, ordered scripts, timeout reporting, and close/reset reporting all pass. + +### Probe binary build + +```bash +cargo build --bin sgbrowser_ws_probe +``` + +Expected: PASS. + +### Live evidence collection + +- run the probe matrix against the real configured endpoint +- save exact transcripts to `docs/_tmp_sgbrowser_ws_probe_transcript.md` +- make the architecture decision using the documented rule + +### Follow-up branch condition + +- if Option A wins, repository contains a bootstrap-contract design + plan +- if Option B wins, repository contains a bridge-path design + plan +- no production runtime changes are made until that decision is written down + +--- + +## Notes for Implementation + +- The existing `WsBrowserBackend` fix that remembers the last navigated URL remains valid; do not revert it. +- The previous auth-replacement work also remains valid; it removed a real bug but did not prove the raw websocket architecture. +- Keep the probe tool brutally literal: exact sent frames, exact received frames, explicit timeout/close outcomes. +- Resist the temptation to make the probe “smart.” Smart probes hide evidence. +- If the real endpoint still replies only with the welcome banner and then silence across the matrix, treat that as a decision, not as an excuse for more guessing. diff --git a/docs/superpowers/plans/2026-04-03-ws-browser-welcome-frame-plan.md b/docs/superpowers/plans/2026-04-03-ws-browser-welcome-frame-plan.md new file mode 100644 index 0000000..3b910b3 --- /dev/null +++ b/docs/superpowers/plans/2026-04-03-ws-browser-welcome-frame-plan.md @@ -0,0 +1,362 @@ +# WS Browser Welcome Frame Compatibility Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Make the ws service path tolerate the real sgBrowser welcome banner (`Welcome! You are client #...`) without weakening general ws protocol validation or changing pipe behavior. + +**Architecture:** Keep the shared `WsBrowserBackend` strict and implement the compatibility shim only in `ServiceBrowserWsClient`, which is already the real-browser adapter for the ws service path. Add one positive red test for the known welcome frame and one negative red test proving non-matching first text frames still fail as protocol errors, then make the minimal stateful change in `src/service/server.rs` and verify ws + pipe regressions. + +**Tech Stack:** Rust 2021, tungstenite websocket client/server, existing `WsBrowserBackend`, existing `ServiceBrowserWsClient`, existing Rust unit/integration test suite. + +--- + +## File Structure + +### Existing files to modify + +- Modify: `src/service/server.rs` + - Add the one-time per-connection welcome-skip state to `ServiceBrowserWsClient` + - Add the minimal helper(s) for detecting and discarding the first known welcome frame + - Add focused service-adapter unit tests in the existing `#[cfg(test)]` module +- Reuse: `src/browser/ws_backend.rs` + - Do not change protocol parsing rules; only verify behavior remains strict for all non-service callers +- Reuse: `tests/service_task_flow_test.rs` + - Re-run to confirm the ws service path still reaches the browser websocket after the service-side shim +- Reuse: `tests/browser_ws_backend_test.rs` + - Re-run to prove the shared backend semantics remain unchanged + +### Files deliberately not changed + +- `src/browser/ws_backend.rs` +- `src/browser/ws_protocol.rs` +- `src/agent/task_runner.rs` +- `src/compat/runtime.rs` +- `src/compat/orchestration.rs` +- `src/compat/workflow_executor.rs` +- `src/lib.rs` + +The design explicitly keeps the welcome-banner workaround out of the shared backend and out of the pipe path. + +--- + +## Task 1: Reproduce the real welcome-frame failure with focused unit tests + +**Files:** +- Modify: `src/service/server.rs` + +- [ ] **Step 1: Add the positive failing test for the known welcome frame** + +In the existing `#[cfg(test)] mod tests` inside `src/service/server.rs`, add one focused test next to the current ws adapter tests. + +Test shape: + +```rust +#[test] +fn future_server_side_ws_native_adapter_skips_initial_known_welcome_frame() { + // fake server sends: + // 1. "Welcome! You are client #1" + // 2. "0" + // backend.invoke(Action::Navigate, ...) should succeed +} +``` + +Required assertions: +- the fake websocket server accepts one connection +- it sends the welcome banner first, then the numeric success status +- `WsBrowserBackend.invoke(Action::Navigate, ...)` returns `Ok(CommandOutput { success: true, .. })` + +- [ ] **Step 2: Run only the positive new test and watch it fail** + +Run: + +```bash +cargo test service::server::tests::future_server_side_ws_native_adapter_skips_initial_known_welcome_frame -- --nocapture +``` + +Expected: FAIL with a protocol error containing `invalid browser status frame: Welcome! You are client #1`. + +- [ ] **Step 3: Add the negative failing test for arbitrary first text** + +In the same `#[cfg(test)]` module, add one negative test proving we do **not** silently skip arbitrary first text frames. + +Test shape: + +```rust +#[test] +fn future_server_side_ws_native_adapter_does_not_skip_unknown_first_text_frame() { + // fake server sends: + // 1. "Hello from server" + // assert invoke(...) fails as PipeError::Protocol(...) +} +``` + +Required assertions: +- the first frame is a non-matching text frame such as `Hello from server` +- `invoke(...)` fails +- the failure remains a protocol error rather than success or timeout + +- [ ] **Step 4: Run only the negative new test and verify the current behavior is already strict** + +Run: + +```bash +cargo test service::server::tests::future_server_side_ws_native_adapter_does_not_skip_unknown_first_text_frame -- --nocapture +``` + +Expected: PASS, proving the current implementation already treats arbitrary first text as a protocol error. Keep that assertion in place before any production change. + +- [ ] **Step 5: Confirm the TDD gate before implementation** + +Do not implement production code before both tests exist and the positive test has failed on current behavior. + +--- + +## Task 2: Add the minimal per-connection welcome-skip state in the service adapter + +**Files:** +- Modify: `src/service/server.rs` + +- [ ] **Step 1: Add one-time per-connection state to `ServiceBrowserWsClient`** + +Extend `ServiceBrowserWsClient` with one extra state field that tracks whether the initial welcome candidate has already been consumed for the current websocket connection. + +Allowed shape: + +```rust +struct ServiceBrowserWsClient { + browser_ws_url: String, + browser_socket: Mutex>>>, + initial_text_frame_checked: Mutex, +} +``` + +or an equally small equivalent. + +Rules: +- state is per connection, not per request +- state must survive multiple `invoke(...)` calls while reusing the same socket +- do not add broader protocol state machines + +- [ ] **Step 2: Add a narrow welcome-frame matcher** + +In `src/service/server.rs`, add one small helper that recognizes only the known banner prefix: + +```rust +fn is_known_welcome_frame(frame: &str) -> bool { + frame.starts_with("Welcome! You are client #") +} +``` + +Rules: +- no regex needed +- no generic “ignore arbitrary text” logic +- keep the matcher local to `src/service/server.rs` + +- [ ] **Step 3: Update `recv_text_timeout(...)` to skip at most one initial known banner** + +Modify `impl WsClient for ServiceBrowserWsClient` so that the first text frame received after connection establishment is handled like this: + +1. read the next text frame +2. if the initial-frame state is still false: + - mark the first-frame check as consumed + - if the frame matches `is_known_welcome_frame(...)`, read the next frame and return that next frame instead +3. otherwise, return the frame unchanged + +Rules: +- skip only once per connection +- do not loop indefinitely over multiple text frames +- do not swallow unknown first text frames +- do not change timeout / close / reset / connect-failure behavior + +- [ ] **Step 4: Reset the one-time state when a fresh socket is created** + +When `with_socket(...)` establishes a brand-new websocket connection, ensure the one-time banner-check state is reset so a new connection can tolerate its own first welcome frame. + +- [ ] **Step 5: Add one reconnect regression in the service adapter tests** + +Add one focused test proving the welcome skip resets on a fresh connection after socket close/reset. + +Test shape: + +```rust +#[test] +fn future_server_side_ws_native_adapter_skips_welcome_again_after_reconnect() { + // first connection closes after use + // second fresh connection sends the same welcome banner again + // both invocations succeed +} +``` + +Required assertion: +- the one-time skip is per connection, not global for the client instance + +- [ ] **Step 6: Run the positive new test** + +Run: + +```bash +cargo test service::server::tests::future_server_side_ws_native_adapter_skips_initial_known_welcome_frame -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 7: Run the negative new test** + +Run: + +```bash +cargo test service::server::tests::future_server_side_ws_native_adapter_does_not_skip_unknown_first_text_frame -- --nocapture +``` + +Expected: PASS, proving unknown first text is still treated as a protocol error. + +- [ ] **Step 8: Run the reconnect regression** + +Run: + +```bash +cargo test service::server::tests::future_server_side_ws_native_adapter_skips_welcome_again_after_reconnect -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 9: Run the full service adapter unit group** + +Run: + +```bash +cargo test service::server::tests -- --nocapture +``` + +Expected: PASS, including the existing tests for: +- status `0` success +- connect failure => `PipeError::Protocol("browser websocket connect failed: ...")` +- disconnect/reset => `PipeError::PipeClosed` +- callback timeout => `PipeError::Timeout` +- new known-welcome success path +- new unknown-first-frame strictness path +- new reconnect reset behavior + +--- + +## Task 3: Verify the shared backend stayed strict and the ws service path still works + +**Files:** +- Reuse: `tests/browser_ws_backend_test.rs` +- Reuse: `tests/service_task_flow_test.rs` +- Reuse: `src/browser/ws_backend.rs` + +- [ ] **Step 1: Re-run the shared ws backend tests unchanged** + +Run: + +```bash +cargo test --test browser_ws_backend_test -- --nocapture +``` + +Expected: PASS. This proves `WsBrowserBackend` semantics remain unchanged for its existing deterministic callers. + +- [ ] **Step 2: Re-run the service task-flow regression** + +Run: + +```bash +cargo test --test service_task_flow_test -- --nocapture +``` + +Expected: PASS, including the auth-regression test that proves the ws service path reaches the browser websocket and no longer emits `invalid hmac seed: session key must not be empty`. + +- [ ] **Step 3: Re-run the ws-focused mixed verification** + +Run: + +```bash +cargo test --test browser_ws_backend_test --test browser_ws_protocol_test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Expected: PASS. + +--- + +## Task 4: Re-run the real manual smoke that originally failed + +**Files:** +- Reuse only: no code changes unless a fresh reproducer proves another bug + +- [ ] **Step 1: Confirm real browser websocket reachability** + +Run a reachability check for `ws://127.0.0.1:12345` (or the configured `browserWsUrl`) before starting smoke. + +Expected: reachable. + +- [ ] **Step 2: Start the real ws service** + +Run: + +```bash +cargo run --bin sg_claw -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Expected: the service prints: +- `sg_claw ready: ...` +- the resolved `service_ws_listen_addr` +- the configured `browser_ws_url` + +- [ ] **Step 3: Re-run the original failing manual smoke** + +Run: + +```bash +printf '打开知乎热榜并读取页面主区域文本\n' | cargo run --bin sg_claw_client -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Expected: +- no `invalid browser status frame: Welcome! You are client #1` +- browser actions proceed past the first status frame +- if the browser later fails for another reason, capture that new reason exactly + +- [ ] **Step 4: Re-run the old Zhihu export task smoke** + +Run: + +```bash +printf '读取知乎热榜数据,并导出 excel 文件\n' | cargo run --bin sg_claw_client -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Expected: +- no `invalid browser status frame: Welcome! You are client #1` +- the task reaches the real browser action path beyond connection banner handling + +- [ ] **Step 5: Stop and debug if a new real-browser issue appears** + +If smoke now fails for a different reason, do not piggyback a second fix into this slice without: +- capturing the exact new output +- writing a new focused spec/plan if the issue is materially different + +--- + +## Verification Checklist + +### Service adapter unit tests + +```bash +cargo test service::server::tests -- --nocapture +``` + +Expected: all service-side ws adapter tests pass, including the new welcome-frame positive/negative cases and reconnect reset case. + +### Shared ws backend + ws service regressions + +```bash +cargo test --test browser_ws_backend_test --test browser_ws_protocol_test --test service_ws_session_test --test service_task_flow_test -- --nocapture +``` + +Expected: PASS. + +### Real smoke verification + +- `browserWsUrl` reachable +- `sg_claw` starts with real config +- `sg_claw_client` no longer fails on `Welcome! You are client #...` +- Zhihu minimal read task gets past the first status frame +- Zhihu export task gets past the first status frame diff --git a/docs/superpowers/plans/2026-04-03-zhihu-release-ws-function-callback-plan.md b/docs/superpowers/plans/2026-04-03-zhihu-release-ws-function-callback-plan.md new file mode 100644 index 0000000..f7f872e --- /dev/null +++ b/docs/superpowers/plans/2026-04-03-zhihu-release-ws-function-callback-plan.md @@ -0,0 +1,564 @@ +# Zhihu Release WS Function-Callback Migration Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Move only the Zhihu direct-execution path to the new Release browser websocket interaction style while keeping the existing pipe protocol and non-Zhihu submit behavior unchanged. + +**Architecture:** Keep `ClientMessage` / `ServiceMessage`, `run_submit_task_with_browser_backend(...)`, and the high-level Zhihu workflow steps unchanged. First prove the exact Release browser interaction contract with transcript-backed probes. Then implement the smallest Zhihu-scoped backend path that follows that proven contract. Do not globally rewire the submit path unless the probe evidence proves there is no narrower safe seam. + +**Tech Stack:** Rust, tungstenite, existing sgclaw service/client pipe protocol, `docs/_tmp_sgbrowser_ws_api_doc.txt`, Release browser websocket at `ws://127.0.0.1:12345`, current Zhihu direct-execution workflow. + +--- + +## Context + +The user has now made the target behavior explicit: + +- the browser has changed and the working reference behavior is the user-provided HTML page that connects to `ws://127.0.0.1:12345` +- that page sends a bootstrap registration frame: `{"type":"register","role":"web"}` +- browser requests are still JSON arrays such as `[window.location.href, "sgBrowserSetTheme", "1"]` and `[window.location.href, "sgBrowerserGetUrls", "showUrls"]` +- callback-bearing browser behavior is now centered on page-defined JS callback functions like `showUrls`, not on Rust directly reading a websocket callback frame as the final business result +- the existing sgclaw pipe protocol must remain unchanged + +The current sgclaw drift that must be corrected is visible in: + +- `src/browser/ws_protocol.rs` + - `Action::Navigate` currently emits `sgHideBrowserCallAfterLoaded` with an inline `callBackJsToCpp(...)` string +- `src/browser/ws_backend.rs` + - Rust currently waits for a browser websocket callback frame and treats that as the action result +- `tests/service_ws_session_test.rs:498-605` +- `tests/service_task_flow_test.rs:499-635` + - existing **generic submit-flow** regressions still lock in the old direct raw-websocket callback-frame assumption + - these are useful as non-regression guardrails, but they are not themselves Zhihu-specific regressions + +Zhihu-specific verification must therefore be added explicitly instead of assuming those Baidu-path tests already cover Zhihu. + +The new browser style proves these facts and only these facts so far: + +1. sgclaw must handle a register-first websocket handshake +2. browser requests are still `[requesturl, action, ...args]` +3. some browser capabilities now return through page-defined callback functions like `showUrls` +4. the current direct raw-websocket callback expectation in Zhihu path is no longer a safe assumption + +The production seam is **not** pre-decided here. Task 1 must determine whether Zhihu can be integrated by: +- a direct Zhihu-scoped backend with no helper page, or +- a helper page / relay design because named page callbacks are the only reliable result path + +Until Task 1 evidence is captured, both remain hypotheses. + +## Evidence to preserve in the implementation + +### Browser websocket API doc +From `docs/_tmp_sgbrowser_ws_api_doc.txt`: +- `ws://localhost:12345` is the browser websocket endpoint +- request frames are array payloads with `requesturl` +- `sgBrowerserGetUrls(callback)` uses a callback **function name**: `[requesturl,"sgBrowerserGetUrls", callback]` +- `sgBrowserCallAfterLoaded(targetUrl, callback)` and `sgHideBrowserCallAfterLoaded(targetUrl, callback)` use callback strings with parentheses +- `callBackJsToCpp(param)` uses `sourceUrl@_@targetUrl@_@callback@_@actionUrl@_@responseTxt` +- `sgBrowserRegJsFun(targeturl, funContent)` and `sgBrowserExcuteJsFun(targeturl, funName)` exist and may be useful when the helper page needs durable callback helpers + +### Current working HTML pattern from the user +The now-working reference interaction is: + +```html +const socket = new WebSocket('ws://127.0.0.1:12345'); +socket.onopen = () => { + socket.send(JSON.stringify({type: 'register', role: 'web'})); +}; +socket.send(JSON.stringify([window.location.href,"sgBrowerserGetUrls","showUrls"])); +function showUrls(urls) { + // browser invokes this page-defined callback +} +``` + +That is the browser behavior sgclaw now needs to follow. + +--- + +## Critical files + +### Production files to modify +- `src/browser/ws_protocol.rs` +- `src/compat/workflow_executor.rs` (only if a narrow Zhihu-specific correction is required after backend swap) +- `src/service/server.rs` (only if the chosen Zhihu-scoped integration seam must be wired here) +- `src/service/mod.rs` (only if startup plumbing changes are truly required) +- `src/browser/mod.rs` + +### New production files likely needed +- `src/browser/zhihu_release_backend.rs` + - a Zhihu-scoped `BrowserBackend` adapter that follows the proven Release browser interaction style without changing non-Zhihu routes +- `src/service/browser_callback_host.rs` **only if the probe proves a service-controlled helper page is actually required** + - service-local helper-page lifecycle and callback relay, if evidence shows the browser cannot be driven safely without it + +### Existing files to preserve +- `src/agent/task_runner.rs` +- `src/service/protocol.rs` +- `src/compat/orchestration.rs` +- `src/compat/runtime.rs` +- `src/pipe/*` + +### Existing direct-ws files to review explicitly +- `src/browser/ws_backend.rs` +- `tests/browser_ws_backend_test.rs` + +These files currently encode the old direct raw-websocket callback expectation. The implementation must either: +- leave them untouched as legacy/direct-contract coverage with no Zhihu production callers, or +- update/remove the Zhihu-specific assumptions they currently lock in. + +### Primary test files +- `tests/browser_ws_probe_test.rs` +- `tests/browser_ws_protocol_test.rs` +- `tests/service_ws_session_test.rs` +- `tests/service_task_flow_test.rs` +- `tests/task_runner_test.rs` +- `tests/browser_ws_backend_test.rs` + +--- + +## File structure decisions + +### `src/browser/zhihu_release_backend.rs` +Prefer a Zhihu-scoped backend first. + +Responsibilities: +- keep the same `BrowserBackend` trait surface +- implement only the behavior needed by the current Zhihu direct-execution route +- translate `Action::Navigate`, `Action::GetText`, and `Action::Eval` into the proven Release-browser interaction style +- normalize results back into `CommandOutput` +- avoid affecting non-Zhihu callers + +This is the preferred seam because the user asked to change the current Zhihu flow, not to redesign the whole submit pipeline. + +### `src/service/browser_callback_host.rs` (conditional) +Create this file only if Task 1 probe evidence proves that sgclaw must host or control a page in order to receive named callback-function results. + +If it is needed, the plan must keep the design minimal and specific: +- one concrete transport only (choose websocket or HTTP, not “websocket or HTTP”) +- explicit readiness handshake +- explicit request correlation by `request_id` +- explicit cleanup when the submit task ends + +If Task 1 shows a simpler seam, do not create this file. + +### `src/browser/ws_protocol.rs` +Do not let this file keep only the old direct-callback assumption. + +It should become the shared place for doc-native request builders such as: +- browser bootstrap frames proven by the transcript +- `sgBrowserCallAfterLoaded` / `sgHideBrowserCallAfterLoaded` +- `sgBrowserExcuteJsCodeByArea` +- optional `sgBrowserRegJsFun` / `sgBrowserExcuteJsFun` + +But do **not** let `ws_protocol.rs` absorb service-host lifecycle logic. + +### `src/browser/ws_backend.rs` and `tests/browser_ws_backend_test.rs` +Handle these explicitly in the implementation: +- if they still describe a valid direct browser contract, keep them as isolated legacy/direct-ws coverage only +- if their current navigate/callback assumptions conflict with the proven Release Zhihu path, update or narrow those tests so they no longer describe the active Zhihu integration path + +Do not leave the old direct-callback assumptions ambiguously “reviewed”; the implementation must make their status explicit. + +--- + +## Task 1: Capture the new Release browser contract in a reproducible probe transcript + +**Files:** +- Review/modify: `src/browser/ws_probe.rs` +- Review/modify: `src/bin/sgbrowser_ws_probe.rs` +- Review/modify: `tests/browser_ws_probe_test.rs` +- Create: `docs/_tmp_release_ws_callback_host_transcript.md` + +- [ ] **Step 1: Verify current probe coverage against the Release-browser questions** + +Read the existing probe module and tests and check whether they already prove all of the following: +- a register-first websocket script can be expressed +- a later array action frame can be expressed in the same script +- per-step inbound frames/outcomes are preserved separately +- timeout/close remain distinguishable in the transcript + +Required result: +- identify the exact existing tests that already prove these behaviors +- identify the smallest missing Release-specific coverage, if any + +- [ ] **Step 2: Add only the missing regression coverage** + +If current tests do **not** already prove the Release-browser bootstrap shape, add the narrowest failing regression in `tests/browser_ws_probe_test.rs`. + +Preferred shape if coverage is missing: + +```rust +#[test] +fn probe_supports_register_then_array_action_script() { + // fake server expects: + // 1. {"type":"register","role":"web"} + // 2. ["http://127.0.0.1/helper.html","sgBrowerserGetUrls","showUrls"] +} +``` + +And, if still missing, add one regression proving per-step transcript separation for the register reply and later action reply. + +If those behaviors are already covered, skip new test creation and record the exact test names to rely on. + +- [ ] **Step 3: Run the relevant probe tests** + +Run the narrowest exact tests that prove the Release bootstrap behavior, or the full file if multiple areas changed: + +```bash +cargo test --test browser_ws_probe_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 4: Make the probe binary ergonomic for the Release transcript if needed** + +Only if the current CLI cannot conveniently express the real Release-browser script, make the smallest change needed in `src/bin/sgbrowser_ws_probe.rs` / `src/browser/ws_probe.rs` so it can capture: +- register frame behavior +- minimal `sgBrowserSetTheme` +- minimal `sgBrowerserGetUrls` +- exact inbound websocket text per step + +Do not redesign the probe if it already supports this. + +- [ ] **Step 5: Run the live probe against the Release browser and record the real bootstrap** + +Use the probe binary against the real endpoint to capture at minimum: +- register frame behavior +- minimal `sgBrowserSetTheme` +- minimal `sgBrowerserGetUrls` +- whether replies come back as websocket text, page-function invocation only, or both + +Save the exact transcript in `docs/_tmp_release_ws_callback_host_transcript.md`. + +Required output in that temp doc: +- exact sent frames +- exact received websocket frames +- the observed rule for when named callback functions are invoked +- whether Option A or Option B is supported by evidence + +- [ ] **Step 6: Commit the probe-only slice if code changed** + +If probe code/tests changed: + +```bash +git add src/browser/ws_probe.rs src/bin/sgbrowser_ws_probe.rs tests/browser_ws_probe_test.rs docs/_tmp_release_ws_callback_host_transcript.md +git commit -m "test: capture release browser ws bootstrap contract" +``` + +If only the transcript doc changed, stage only that file and use a docs/test-appropriate commit message. + +--- + +## Task 2: Choose the narrowest Zhihu-only production seam from the probe evidence + +**Files:** +- Modify: `src/service/server.rs` (only if required) +- Modify: `src/browser/mod.rs` +- Modify: `src/compat/workflow_executor.rs` (only if required) +- Create: `src/browser/zhihu_release_backend.rs` +- Create: `src/service/browser_callback_host.rs` **only if required** +- Test: `tests/service_ws_session_test.rs` +- Test: `tests/service_task_flow_test.rs` + +- [ ] **Step 1: Write down the seam decision in the plan notes before coding** + +Based on the transcript from Task 1, record which one of these is supported by evidence: +- Option A: a Zhihu-scoped backend can talk to the Release browser directly with no service-hosted helper page +- Option B: a Zhihu-scoped backend needs a service-controlled helper page because named page callbacks are the only reliable way to get business results + +Do not proceed until one option is chosen explicitly from evidence. + +- [ ] **Step 2: Add a failing service/task-flow regression that proves only the Zhihu path changes** + +Update or add focused tests so that: +- Zhihu submit flow uses the new Release-browser interaction seam +- non-Zhihu behavior is unchanged +- pipe messages remain unchanged + +Required assertions: +- the new path is activated only for Zhihu route detection +- `ClientMessage` / `ServiceMessage` stay identical +- existing non-Zhihu submit behavior is not accidentally rerouted + +- [ ] **Step 3: Run the new focused regression and confirm failure first** + +Run the narrowest exact test names you added in: +```bash +cargo test --test service_ws_session_test -- --nocapture +cargo test --test service_task_flow_test -- --nocapture +``` + +Expected: FAIL because the Zhihu-specific seam does not exist yet. + +- [ ] **Step 4: Implement the chosen seam with the smallest blast radius** + +If Option A won: +- add `src/browser/zhihu_release_backend.rs` +- wire it only where the Zhihu direct-execution route is selected +- leave global submit-path wiring alone + +If Option B won: +- add `src/service/browser_callback_host.rs` with one specific transport and one explicit readiness/correlation model +- add `src/browser/zhihu_release_backend.rs` to talk to that helper path +- wire it only for the Zhihu route + +In both cases: +- do not change non-Zhihu callers +- do not redesign `run_submit_task_with_browser_backend(...)` +- do not change the pipe protocol + +- [ ] **Step 5: Make the status of old direct-ws code explicit** + +Update `src/browser/ws_backend.rs` / `tests/browser_ws_backend_test.rs` only as needed so they no longer ambiguously describe the active Zhihu path. + +Allowed outcomes: +- keep them untouched as legacy/direct-ws coverage with no Zhihu production caller +- narrow/update the tests so they no longer claim the active Zhihu integration path + +Not allowed: +- leaving the plan and code in a state where both old and new paths appear to be the active Zhihu contract + +- [ ] **Step 6: Run focused integration tests** + +Run: +```bash +cargo test --test service_ws_session_test -- --nocapture +cargo test --test service_task_flow_test -- --nocapture +cargo test --test task_runner_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 7: Commit the seam-selection slice** + +Adjust staged files to match the option actually implemented, for example: + +```bash +git add src/browser/zhihu_release_backend.rs src/browser/mod.rs src/service/server.rs src/service/browser_callback_host.rs tests/service_ws_session_test.rs tests/service_task_flow_test.rs tests/browser_ws_backend_test.rs +git commit -m "feat: route zhihu flow through release browser ws contract" +``` + +Only stage files that were truly changed. + +--- + +## Task 3: Implement Zhihu action mapping on the chosen Release-browser seam + +**Files:** +- Modify: `src/browser/ws_protocol.rs` +- Modify: `src/browser/zhihu_release_backend.rs` +- Test: `tests/browser_ws_protocol_test.rs` +- Create: `tests/browser_zhihu_release_backend_test.rs` + +- [ ] **Step 1: Write the first failing backend test for Zhihu navigate mapping** + +Create `tests/browser_zhihu_release_backend_test.rs` with a fake transport/relay and assert that `Action::Navigate` for the Zhihu path becomes the exact browser request shape proven by Task 1. + +Start with this shape: + +```rust +#[test] +fn zhihu_release_backend_maps_navigate_to_proven_release_frame() { + // invoke Action::Navigate + // assert exact outbound frame/opcode chosen from transcript evidence +} +``` + +Required assertions: +- the call site still uses `BrowserBackend::invoke(...)` +- the exact outbound frame matches the recorded Release-browser evidence +- request correlation stays deterministic + +- [ ] **Step 2: Run the single new backend test and verify it fails** + +Run: +```bash +cargo test --test browser_zhihu_release_backend_test zhihu_release_backend_maps_navigate_to_proven_release_frame -- --nocapture +``` + +Expected: FAIL because the backend does not exist yet. + +- [ ] **Step 3: Implement minimal `Navigate` support** + +In `src/browser/zhihu_release_backend.rs`: +- implement `BrowserBackend` +- support `Action::Navigate` first +- use `ws_protocol.rs` helpers for exact browser-frame construction +- do not hardcode speculative opcodes; follow the transcript from Task 1 + +- [ ] **Step 4: Add failing tests for `GetText` and `Eval`** + +Add tests proving: +- `Action::GetText` returns `CommandOutput.data == {"text": "..."}` +- `Action::Eval` returns `CommandOutput.data == {"text": "..."}` +- callback or relay failures become `PipeError::Protocol(...)` + +- [ ] **Step 5: Implement `GetText` and `Eval` on the chosen seam** + +Use the smallest proven mechanism: +- if the transcript proves page-defined callback functions are required, route through them +- if `callBackJsToCpp(...)` to a page context is still part of the proven path, use it deliberately +- if `sgBrowserRegJsFun` / `sgBrowserExcuteJsFun` becomes necessary, add it only with test coverage and only for the Zhihu path + +- [ ] **Step 6: Run focused backend/protocol tests** + +Run: +```bash +cargo test --test browser_zhihu_release_backend_test -- --nocapture +cargo test --test browser_ws_protocol_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 7: Commit the Zhihu backend slice** + +```bash +git add src/browser/ws_protocol.rs src/browser/zhihu_release_backend.rs src/browser/mod.rs tests/browser_ws_protocol_test.rs tests/browser_zhihu_release_backend_test.rs +git commit -m "feat: add zhihu release ws backend" +``` + +--- + +## Task 4: Keep the Zhihu workflow logic stable and patch only proven mismatches + +**Files:** +- Review: `src/compat/workflow_executor.rs` +- Test: `tests/service_task_flow_test.rs` +- Test: `tests/compat_runtime_test.rs` (only if a focused direct-execution regression is needed) + +- [ ] **Step 1: Write a failing Zhihu-specific regression only if the chosen seam changes route assumptions** + +If the new Zhihu backend changes request-url or target-url handling enough to break hotlist flow, add one focused failing regression for that exact behavior. + +Candidate assertions: +- hotlist navigate still logs `navigate https://www.zhihu.com/hot` +- follow-up `GetText body` still targets the Zhihu page, not any helper page +- extractor `Eval` still runs against Zhihu, not any helper page + +- [ ] **Step 2: Keep the current high-level Zhihu action sequence unless a test proves otherwise** + +`src/compat/workflow_executor.rs` currently does the right high-level work: +- navigate to Zhihu hotlist +- poll body text until ready +- run the extractor script + +Prefer to keep this file unchanged. Only patch it if the new backend needs a narrow explicit `target_url` fix or similar evidence-backed adjustment. + +- [ ] **Step 3: Run the smallest Zhihu-focused verification sweep** + +Run: +```bash +cargo test --test service_task_flow_test -- --nocapture +cargo test --test compat_runtime_test zhihu -- --nocapture +``` + +If the `compat_runtime_test zhihu` filter is too broad or unstable, run the exact focused Zhihu cases that cover hotlist extraction. + +- [ ] **Step 4: Commit only if a Zhihu-specific code change was actually required** + +```bash +git add src/compat/workflow_executor.rs tests/service_task_flow_test.rs tests/compat_runtime_test.rs +git commit -m "fix: keep zhihu workflow aligned with release ws backend" +``` + +Skip this commit if no production change in `workflow_executor.rs` was needed. + +--- + +## Task 5: Prove that pipe behavior and non-Zhihu behavior stayed unchanged + +**Files:** +- Test: `tests/service_ws_session_test.rs` +- Test: `tests/service_task_flow_test.rs` +- Test: `tests/task_runner_test.rs` + +- [ ] **Step 1: Add or update one regression that proves pipe messages are unchanged** + +Use the smallest existing test seam to assert that `ClientMessage` / `ServiceMessage` payloads remain unchanged while the Zhihu route uses the new browser integration path internally. + +- [ ] **Step 2: Add or update one regression that proves non-Zhihu behavior is unchanged** + +Use a non-Zhihu submit or service-session case and assert it does not take the new Zhihu-specific backend path. + +- [ ] **Step 3: Preserve current runtime regression guards** + +The end-to-end tests must continue asserting that output does **not** contain: +- `invalid hmac seed: session key must not be empty` +- `Cannot drop a runtime in a context where blocking is not allowed` + +- [ ] **Step 4: Run the final focused verification sweep** + +Run: +```bash +cargo test --test service_ws_session_test -- --nocapture +cargo test --test service_task_flow_test -- --nocapture +cargo test --test task_runner_test -- --nocapture +``` + +Expected: PASS. + +- [ ] **Step 5: Commit the verification sweep** + +```bash +git add tests/service_ws_session_test.rs tests/service_task_flow_test.rs tests/task_runner_test.rs tests/browser_ws_backend_test.rs +git commit -m "test: constrain zhihu release ws migration scope" +``` + +Only stage files that were truly changed. + +--- + +## Out of scope + +Do **not** do these in this slice: +- change the pipe protocol +- change `ClientMessage` / `ServiceMessage` +- redesign `run_submit_task_with_browser_backend(...)` +- reintroduce any browser bridge surface +- keep adding speculative direct-raw-websocket callback patches to `ws_backend.rs` +- redesign non-Zhihu workflows unless the new backend abstraction forces a shared fix +- create a long-lived external dependency or third-party server just to host the helper page + +--- + +## Verification checklist + +Run at minimum: + +```bash +cargo test --test browser_ws_probe_test -- --nocapture +cargo test --test browser_zhihu_release_backend_test -- --nocapture +cargo test --test browser_ws_protocol_test -- --nocapture +cargo test --test service_ws_session_test -- --nocapture +cargo test --test service_task_flow_test -- --nocapture +cargo test --test task_runner_test -- --nocapture +``` + +If Task 2 chose the helper-page / relay design, also run the helper-page-specific backend tests you added for that path. + +Manual verification after code changes: + +1. start the real Release browser/runtime that exposes `ws://127.0.0.1:12345` +2. start `sg_claw` with real config +3. start `sg_claw_client` +4. submit: + - `打开知乎热榜,获取前10条数据,并导出 Excel` +5. confirm the Zhihu path uses the exact Release-browser interaction seam proven by Task 1 +6. if Task 2 chose Option B, confirm the helper page / relay path is used only for the Zhihu integration seam +7. confirm non-Zhihu behavior is unchanged +8. confirm the task completes without: + - `timeout while waiting for browser message` + - `invalid browser status frame: Welcome! You are client #1` + - `invalid hmac seed: session key must not be empty` + - `Cannot drop a runtime in a context where blocking is not allowed` + +--- + +## Expected outcome + +After this slice: +- sgclaw still exposes the same pipe/service contract +- Zhihu hotlist execution uses the Release-browser websocket contract proven by Task 1 +- non-Zhihu behavior remains unchanged +- old direct-ws Zhihu assumptions are no longer ambiguous in production/tests +- if Option A won, Zhihu uses a direct Release-browser backend +- if Option B won, Zhihu uses the minimal helper-page / relay seam justified by the probe evidence diff --git a/docs/superpowers/plans/2026-04-04-zhihu-ws-submit-realignment.md b/docs/superpowers/plans/2026-04-04-zhihu-ws-submit-realignment.md new file mode 100644 index 0000000..8065787 --- /dev/null +++ b/docs/superpowers/plans/2026-04-04-zhihu-ws-submit-realignment.md @@ -0,0 +1,322 @@ +# Zhihu WS Submit Realignment Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Realign Zhihu submit routes to the documented websocket callback model, removing helper-page bootstrap from the mainline while keeping the existing pipe/service contract unchanged. + +**Architecture:** The change stays inside the existing submit-path backend selection and websocket protocol flow. Zhihu routes stop choosing `BrowserCallbackBackend` and instead use `WsBrowserBackend` when a real browser websocket is configured, preserving the existing pipe fallback in direct runtime when no websocket URL is available. + +**Tech Stack:** Rust, tungstenite websocket client/server, serde_json, cargo test + +--- + +## File Map + +- Modify: `src/service/server.rs` + - Change only the Zhihu route-gated submit-path backend selection + - Remove Zhihu submit mainline use of `LiveBrowserCallbackHost` / `BrowserCallbackBackend` + - Keep service submit path on `WsBrowserBackend` + - Preserve initial request URL derivation for Zhihu routes +- Modify: `src/agent/mod.rs` + - Change only the Zhihu route-gated submit-path backend selection + - Remove Zhihu submit mainline use of `LiveBrowserCallbackHost` / `BrowserCallbackBackend` + - Keep direct runtime pipe fallback when browser websocket URL is absent +- Modify: `tests/agent_runtime_test.rs` + - Replace helper-page bootstrap regression with direct websocket submit regression + - Assert no `/sgclaw/browser-helper.html` bootstrap frames are emitted + - Assert real-page request ownership on follow-up Zhihu actions +- Modify: `src/browser/callback_host.rs` + - Remove or rewrite the now-wrong red test that preserves Option-B callback-host startup behavior +- Verify: `tests/browser_ws_backend_test.rs` + - Reuse existing websocket request-url behavior coverage; extend only if the new regression proves insufficient +- Reference: `docs/superpowers/specs/2026-04-04-zhihu-ws-submit-realignment-design.md` + +### Task 1: Rewrite the stale submit regression around the real websocket mainline + +**Files:** +- Modify: `tests/agent_runtime_test.rs:507-660` +- Test: `tests/agent_runtime_test.rs` + +- [ ] **Step 1: Write the failing test** + +Rename and rewrite the existing helper-page regression so it asserts the new behavior: + +```rust +#[test] +fn production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap() { + // arrange runtime context and fake browser ws server + // submit Zhihu hotlist request + // assert ws frames never contain "/sgclaw/browser-helper.html" + // assert first action is navigate to https://www.zhihu.com/hot + // assert follow-up action uses real-page requesturl instead of helper page +} +``` + +Use the existing fake ws helpers in the file where possible. Do not add localhost callback-host HTTP plumbing to this rewritten test. + +- [ ] **Step 2: Run test to verify it fails** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test agent_runtime_test -- --nocapture` + +Expected: FAIL because current production code still routes Zhihu submit into `BrowserCallbackBackend` and emits helper-page bootstrap frames. + +- [ ] **Step 3: Keep the regression focused** + +Before touching production code, confirm the rewritten test checks only these behaviors: + +```text +- no callback-host bootstrap frame +- no helper-page URL +- navigate frame still targets https://www.zhihu.com/hot +- follow-up websocket action uses real-page request ownership +``` + +Do not assert unrelated workflow details beyond what is needed to prove the route correction. + +- [ ] **Step 4: Commit the red test** + +```bash +git add tests/agent_runtime_test.rs +git commit -m "test: rewrite zhihu submit ws routing regression" +``` + +### Task 2: Switch service Zhihu submit routes off the callback-host backend + +**Files:** +- Modify: `src/service/server.rs:287-328` +- Test: `tests/agent_runtime_test.rs` + +- [ ] **Step 1: Write the minimal production change** + +Replace only the Zhihu-route callback-host branch with direct websocket backend selection. + +Minimal target shape: + +```rust +fn browser_backend_for_submit( + browser_ws_url: &str, + mac_policy: &MacPolicy, + request: &SubmitTaskRequest, +) -> Result, PipeError> { + if should_use_callback_host_backend(request) { + return Ok(Arc::new(WsBrowserBackend::new( + Arc::new(ServiceWsClient::connect(browser_ws_url)?), + mac_policy.clone(), + initial_request_url_for_submit_task(request), + ))); + } + + Ok(Arc::new(WsBrowserBackend::new( + Arc::new(ServiceWsClient::connect(browser_ws_url)?), + mac_policy.clone(), + initial_request_url_for_submit_task(request), + ))) +} +``` + +After the route-gated branch is removed, simplify further only if the branch becomes redundant without changing non-Zhihu behavior. + +- [ ] **Step 2: Run the rewritten regression** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test agent_runtime_test -- --nocapture` + +Expected: still FAIL or advance to a later assertion until the direct-runtime path is corrected too. + +- [ ] **Step 3: Add or update a service-specific regression if needed** + +If the rewritten `agent_runtime_test` does not exercise the service submit path directly, add one narrow service regression before continuing. + +Target shape: + +```rust +#[test] +fn service_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap() { + // fake browser ws + // submit Zhihu route through service path + // assert no helper bootstrap frame +} +``` + +Run the exact test you end up using: + +`cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" service_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test -- --nocapture` + +Expected: PASS + +- [ ] **Step 4: Commit the service-path fix** + +```bash +git add src/service/server.rs tests/agent_runtime_test.rs +git commit -m "fix: route zhihu submit through ws backend" +``` + +### Task 3: Switch direct runtime Zhihu submit routes off the callback-host backend while keeping pipe fallback + +**Files:** +- Modify: `src/agent/mod.rs:49-100` +- Test: `tests/agent_runtime_test.rs` + +- [ ] **Step 1: Write the minimal production change** + +Remove callback-host backend selection from `browser_backend_for_submit(...)`. + +Minimal target behavior: + +```rust +if let Some(browser_ws_url) = configured_browser_ws_url(context) { + return Ok(Arc::new(WsBrowserBackend::new( + Arc::new(ServiceWsClient::connect(&browser_ws_url)?), + browser_tool.mac_policy().clone(), + initial_request_url_for_submit_task(request), + ).with_response_timeout(browser_tool.response_timeout()))); +} + +Ok(Arc::new(PipeBrowserBackend::from_inner(browser_tool.clone()))) +``` + +If `ServiceWsClient` is not reusable from `src/service/server.rs`, extract the smallest shared websocket client helper into the browser module instead of inventing a new abstraction. + +- [ ] **Step 2: Add a focused fallback assertion only if needed** + +If the rewritten regression does not cover the direct-runtime no-websocket case, add one small test: + +```rust +#[test] +fn production_submit_task_keeps_pipe_fallback_when_browser_ws_url_is_unset() { + // no SGCLAW_BROWSER_WS_URL + // blank/no ws config + // assert no websocket bootstrap attempt occurs +} +``` + +Only add this test if current coverage is insufficient. + +- [ ] **Step 3: Run tests to verify green** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test agent_runtime_test -- --nocapture` + +Expected: PASS + +If a fallback test was added, run it immediately after and expect PASS. + +- [ ] **Step 4: Commit the direct-runtime fix** + +```bash +git add src/agent/mod.rs tests/agent_runtime_test.rs +git commit -m "fix: align runtime zhihu submit with ws contract" +``` + +### Task 4: Reassess stale callback-host regression coverage only if it blocks the approved slice + +**Files:** +- Maybe modify: `src/browser/callback_host.rs:793-810` +- Test: `src/browser/callback_host.rs` + +- [ ] **Step 1: Check whether the callback-host red test still blocks the approved Option A slice** + +Inspect whether this test still preserves rejected Option-B behavior and whether it fails or becomes misleading after Tasks 1-3: + +```rust +#[test] +fn live_callback_host_starts_without_bootstrapping_external_helper_page() { + // inspect before editing +} +``` + +If the test is unrelated to the approved Zhihu mainline or remains harmless, leave it unchanged in this slice. + +- [ ] **Step 2: Remove or rewrite only if required by the changed submit-path behavior** + +If the test blocks the approved slice, make the smallest change needed: + +- delete it if it exists only to preserve rejected Option B behavior, or +- rewrite it so it no longer asserts callback-host startup as the accepted Zhihu mainline + +- [ ] **Step 3: Run focused callback-host tests only if Step 2 changed code** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_host --lib -- --nocapture` + +Expected: PASS + +- [ ] **Step 4: Commit only if Step 2 changed code** + +```bash +git add src/browser/callback_host.rs +git commit -m "test: clean up stale callback host regression" +``` + +### Task 5: Run the focused verification sweep + +**Files:** +- Verify: `tests/agent_runtime_test.rs` +- Verify: `tests/compat_runtime_test.rs` +- Verify: any directly affected service/browser websocket tests + +- [ ] **Step 1: Run submit-path regression coverage** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test agent_runtime_test -- --nocapture` + +Expected: PASS + +- [ ] **Step 2: Run websocket backend request-url coverage** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" ws_backend_reuses_last_navigated_url_for_followup_requests --test browser_ws_backend_test -- --nocapture` + +Expected: PASS + +- [ ] **Step 3: Run Zhihu compat runtime coverage** + +Run: `cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" zhihu --test compat_runtime_test -- --nocapture` + +Expected: PASS for the changed submit-path surface or clear, directly related failures only. + +- [ ] **Step 4: Run affected service submit regression coverage** + +Run the exact service-specific regression from Task 2 if you added one. + +Otherwise, run the narrowest existing service submit test that covers `ClientMessage::SubmitTask` handling for browser routes. + +Expected: PASS + +- [ ] **Step 5: Commit the verified slice** + +```bash +git add src/service/server.rs src/agent/mod.rs tests/agent_runtime_test.rs src/browser/callback_host.rs +git commit -m "fix: realign zhihu submit with browser ws callbacks" +``` + +### Task 6: Run stronger real-browser validation + +**Files:** +- Verify live behavior through existing binaries and real config only + +- [ ] **Step 1: Start the service with the real config** + +Run: `cargo run --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sg_claw -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json"` + +Expected: service starts without failing at callback-host readiness timeout. + +- [ ] **Step 2: Run the client against the started service** + +Run: `cargo run --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sg_claw_client` + +Expected: for `打开知乎热榜,获取前10条数据,并导出 Excel`, the browser proceeds into real Zhihu page work instead of stalling before page open. + +- [ ] **Step 3: Capture the narrow acceptance evidence** + +Verify all of the following from logs/frames/observed behavior: + +```text +- no callback-host readiness timeout +- no helper-page bootstrap frame +- at least one real-page follow-up browser action after navigate +``` + +- [ ] **Step 4: Commit only if live verification required code changes** + +```bash +git add +git commit -m "fix: tighten zhihu ws submit live validation follow-up" +``` + +If no further code changes were needed, do not create an extra commit. diff --git a/docs/superpowers/plans/2026-04-06-service-chat-web-console-plan.md b/docs/superpowers/plans/2026-04-06-service-chat-web-console-plan.md new file mode 100644 index 0000000..467cc73 --- /dev/null +++ b/docs/superpowers/plans/2026-04-06-service-chat-web-console-plan.md @@ -0,0 +1,406 @@ +# Service Chat Web Console Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Add a standalone local HTML console that connects to the existing service websocket, submits natural-language tasks with the current `submit_task` payload, and leaves the browser-helper/runtime path untouched. + +**Architecture:** The change stays fully at the presentation edge. A new self-contained HTML file under `frontend/service-console/` reuses the current websocket protocol from `src/service/protocol.rs`, while one narrow Rust integration test guards the page's protocol shape and forbids any reference to `browser-helper.html`, callback-host endpoints, or the browser websocket. No Rust runtime logic changes are part of this slice. + +**Tech Stack:** HTML, CSS, vanilla JavaScript, Rust integration tests, std::fs, Cargo test + +--- + +## File Map + +- Create: `frontend/service-console/sg_claw_service_console.html` + - Standalone local page with inline CSS and JavaScript + - Connects to the existing service websocket at `ws://127.0.0.1:42321` by default + - Sends existing `ClientMessage::SubmitTask` JSON + - Renders inbound `ServiceMessage` rows only +- Create: `tests/service_console_html_test.rs` + - Source guard for the standalone page + - Verifies file location, allowed protocol usage, and forbidden helper/callback references +- Reference: `src/service/protocol.rs` + - Existing websocket message shape to mirror exactly +- Reference: `src/bin/sg_claw_client.rs` + - Existing terminal client behavior to mirror for `submit_task` +- Reference: `docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md` + +## Scope Guardrails + +- Do not modify `src/service/server.rs`. +- Do not modify `src/browser/callback_host.rs`. +- Do not modify `src/browser/callback_backend.rs`. +- Do not modify `src/bin/sg_claw_client.rs`. +- Do not add an HTTP server. +- Do not connect the new page to `ws://127.0.0.1:12345`. +- Do not reference `/sgclaw/browser-helper.html` or `/sgclaw/callback/*` anywhere in the new page. + +### Task 1: Add a failing source-guard test for the standalone page + +**Files:** +- Create: `tests/service_console_html_test.rs` +- Reference: `docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md` + +- [ ] **Step 1: Write the failing test** + +Create a focused integration test that resolves the HTML path from `CARGO_MANIFEST_DIR` and asserts the file contract. + +```rust +use std::fs; +use std::path::PathBuf; + +#[test] +fn service_console_html_stays_on_service_ws_boundary() { + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let html_path = manifest_dir + .join("frontend") + .join("service-console") + .join("sg_claw_service_console.html"); + let source = fs::read_to_string(&html_path) + .expect("service console html should exist"); + + assert!(source.contains("ws://127.0.0.1:42321")); + assert!(source.contains("submit_task")); + assert!(!source.contains("/sgclaw/browser-helper.html")); + assert!(!source.contains("/sgclaw/callback/ready")); + assert!(!source.contains("/sgclaw/callback/events")); + assert!(!source.contains("/sgclaw/callback/commands/next")); + assert!(!source.contains("/sgclaw/callback/commands/ack")); + assert!(!source.contains("ws://127.0.0.1:12345")); +} +``` + +- [ ] **Step 2: Run test to verify it fails** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" service_console_html_stays_on_service_ws_boundary --test service_console_html_test -- --exact +``` + +Expected: FAIL because the HTML file does not exist yet. + +- [ ] **Step 3: Keep the test narrow** + +Before writing production code, confirm the test guards only the approved boundary: + +```text +- file exists at frontend/service-console/sg_claw_service_console.html +- service websocket default is present +- submit_task payload marker is present +- no helper-page path +- no callback-host endpoints +- no browser websocket URL +``` + +Do not turn this into an end-to-end browser test. + +- [ ] **Step 4: Commit the red test** + +```bash +git add tests/service_console_html_test.rs +git commit -m "test: add service console html boundary guard" +``` + +### Task 2: Implement the standalone HTML console with the approved boundary + +**Files:** +- Create: `frontend/service-console/sg_claw_service_console.html` +- Reference: `src/service/protocol.rs:6` +- Reference: `src/bin/sg_claw_client.rs:16` +- Test: `tests/service_console_html_test.rs` + +- [ ] **Step 1: Create the HTML file with the minimal structure** + +Write one self-contained page with: + +```html + + + + + + sgClaw Service Console + + +
+ + +
未连接
+
+ +
+ +
+ + +``` + +Keep all CSS and JavaScript inline. Do not add external assets or a build step. + +- [ ] **Step 2: Implement websocket connect/disconnect behavior** + +Add the smallest possible JS behavior, including explicit disconnect on the same button so the UI +matches the approved connect/disconnect contract: + +```javascript +let socket = null; + +function appendRow(kind, text) { + // append a visible row to #messageStream +} + +function updateUiState() { + const connected = socket && socket.readyState === WebSocket.OPEN; + document.getElementById('connectBtn').textContent = connected ? '断开' : '连接'; + document.getElementById('sendBtn').disabled = !connected; + document.getElementById('connectionState').textContent = connected ? '已连接' : '未连接'; +} + +function connectOrDisconnectService() { + if (socket && socket.readyState === WebSocket.OPEN) { + socket.close(); + return; + } + + const url = document.getElementById('wsUrl').value.trim() || 'ws://127.0.0.1:42321'; + socket = new WebSocket(url); + updateUiState(); + socket.addEventListener('open', () => { + appendRow('status', 'service websocket connected'); + updateUiState(); + }); + socket.addEventListener('close', () => { + appendRow('status', 'service websocket disconnected'); + updateUiState(); + }); + socket.addEventListener('error', () => appendRow('error', 'service websocket error')); + socket.addEventListener('message', handleMessage); +} +``` + +Do not add retry loops or background reconnect logic. + +- [ ] **Step 3: Implement submit_task sending with the current message shape** + +Mirror the terminal client payload shape exactly and show inline validation for empty input: + +```javascript +function setValidation(message) { + document.getElementById('validationText').textContent = message; +} + +function sendTask() { + const instruction = document.getElementById('instructionInput').value.trim(); + if (!socket || socket.readyState !== WebSocket.OPEN) { + return; + } + if (!instruction) { + setValidation('请输入任务内容。'); + return; + } + + setValidation(''); + socket.send(JSON.stringify({ + type: 'submit_task', + instruction, + conversation_id: '', + messages: [], + page_url: '', + page_title: '' + })); +} +``` + +Do not add new fields. Do not add conversation replay logic in this slice. + +- [ ] **Step 4: Render existing inbound service messages only** + +Handle the current `ServiceMessage` variants with a minimal dispatcher: + +```javascript +function handleMessage(event) { + const message = JSON.parse(event.data); + switch (message.type) { + case 'status_changed': + appendRow('status', message.state); + break; + case 'log_entry': + appendRow('log', message.message); + break; + case 'task_complete': + appendRow(message.success ? 'complete' : 'error', message.summary); + break; + case 'busy': + appendRow('error', message.message); + break; + default: + appendRow('error', 'unknown service message: ' + event.data); + } +} +``` + +Keep the composer enabled during in-flight work so repeated submits surface the existing `busy` response instead of inventing a frontend queue. + +- [ ] **Step 5: Keep the helper boundary explicit in the source** + +Before running tests, inspect the HTML source and confirm: + +```text +- no /sgclaw/browser-helper.html +- no /sgclaw/callback/* +- no ws://127.0.0.1:12345 +- no browser websocket register frame logic +``` + +If any such string appears, remove it before testing. + +- [ ] **Step 6: Run the source-guard test to verify green** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" service_console_html_stays_on_service_ws_boundary --test service_console_html_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 7: Commit the standalone page** + +```bash +git add frontend/service-console/sg_claw_service_console.html tests/service_console_html_test.rs +git commit -m "feat: add standalone service chat console" +``` + +### Task 3: Run the focused verification sweep + +**Files:** +- Verify: `tests/service_console_html_test.rs` +- Reference: `src/service/protocol.rs` +- Reference: `src/bin/sg_claw_client.rs` + +- [ ] **Step 1: Re-run the source-guard test** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" service_console_html_stays_on_service_ws_boundary --test service_console_html_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 2: Manually inspect disconnected-send and validation markers in the HTML source** + +Before broader verification, confirm the page source clearly contains all three UI-local rules: + +```text +- connect button can disconnect an open websocket +- send button starts disabled while disconnected +- empty instruction shows inline validation text +``` + +This inspection stays source-level; do not add extra backend tests for it in this slice. + +- [ ] **Step 3: Run an existing service protocol regression for safety** + +Run the narrow existing protocol coverage to prove the page did not require backend changes: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" submit_task_client_message_converts_into_shared_runner_request --test service_ws_session_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 4: Run an existing terminal-client regression for safety** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" client_submits_first_user_line_to_service --test service_task_flow_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 5: Commit only if verification required any code change** + +```bash +git add frontend/service-console/sg_claw_service_console.html tests/service_console_html_test.rs +git commit -m "test: tighten service console verification" +``` + +If verification required no code changes, do not create an extra commit. + +### Task 4: Perform the manual smoke check + +**Files:** +- Verify live behavior only; no new code required + +- [ ] **Step 1: Start the existing service binary** + +Run: + +```bash +cargo run --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" --bin sg_claw -- --config-path "D:/data/ideaSpace/rust/sgClaw/sgclaw_config.json" +``` + +Expected: service starts and prints its ready line with the service websocket listen address. + +- [ ] **Step 2: Open the standalone page directly** + +Open: + +```text +D:/data/ideaSpace/rust/sgClaw/claw-new/frontend/service-console/sg_claw_service_console.html +``` + +Expected: the page loads through the browser as a local file and shows the default websocket URL `ws://127.0.0.1:42321`. + +- [ ] **Step 3: Connect, disconnect, and reconnect once** + +Expected: + +```text +- message stream shows websocket connected +- clicking the same button disconnects the websocket cleanly +- message stream shows websocket disconnected +- send button is disabled again while disconnected +- reconnect succeeds without reloading the page +``` + +- [ ] **Step 4: Submit one natural-language task** + +Use a small harmless instruction such as: + +```text +打开百度 +``` + +Expected: + +```text +- empty textarea send attempt first shows inline validation without sending a websocket frame +- page sends one submit_task payload after valid input +- page receives and renders status/log/task_complete or busy rows +``` + +- [ ] **Step 5: Confirm the helper boundary stayed untouched** + +Verify from the page source and observed behavior: + +```text +- the page never loads /sgclaw/browser-helper.html +- the page never calls /sgclaw/callback/* +- the page never connects to ws://127.0.0.1:12345 +``` + +If the task itself triggers browser automation, that remains owned by the existing Rust runtime rather than by the page. + +- [ ] **Step 6: Commit only if the manual pass required code changes** + +```bash +git add frontend/service-console/sg_claw_service_console.html tests/service_console_html_test.rs +git commit -m "fix: tighten standalone service console smoke flow" +``` + +If the manual pass required no code changes, do not create an extra commit. diff --git a/docs/superpowers/plans/2026-04-06-zhihu-hotlist-post-export-auto-open-plan.md b/docs/superpowers/plans/2026-04-06-zhihu-hotlist-post-export-auto-open-plan.md new file mode 100644 index 0000000..9bc5655 --- /dev/null +++ b/docs/superpowers/plans/2026-04-06-zhihu-hotlist-post-export-auto-open-plan.md @@ -0,0 +1,637 @@ +# Zhihu Hotlist Post-Export Auto-Open Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Extend the existing Zhihu hotlist Excel and dashboard routes so each route can auto-open its own generated artifact after export, while preserving the current callback-host-backed browser boundary and route exclusivity. + +**Architecture:** Keep orchestration in `src/compat/workflow_executor.rs`, but move post-export side effects into a new `src/compat/artifact_open.rs` helper so workflow routing stays readable. Excel auto-open is a local OS-launch side effect; dashboard auto-open reuses `screen_html_export`'s existing `presentation.url` and sends one narrow, marker-based `Action::Navigate` request through `BrowserCallbackBackend`, with a matching special-case validator in `MacPolicy` so arbitrary `file://` navigation remains blocked. + +**Tech Stack:** Rust, serde_json, std::process::Command, std::path, Cargo tests + +--- + +## File Map + +- Create: `src/compat/artifact_open.rs` + - Define the narrow post-export helper surface for this slice only + - Parse and validate generated artifact payload fields passed in by the workflow layer + - Open generated `.xlsx` files with the local default app + - Build the exact approved local-dashboard navigate payload + - Keep one testable internal seam, `open_exported_xlsx_with(output_path, opener)`, so unit tests can prove the generated `.xlsx` path is handed to the launcher without starting a real spreadsheet app + - Include unit tests in the same file for exact Excel path handoff and launcher-failure reporting +- Modify: `src/compat/mod.rs` + - Export the new `artifact_open` module +- Modify: `src/compat/workflow_executor.rs` + - Keep route detection and artifact generation where they are now + - Change `export_xlsx(...)` and `export_screen(...)` so they parse tool payloads, call the route-specific opener, and produce the new success/failure summaries +- Modify: `src/browser/callback_backend.rs` + - Recognize only the approved local-dashboard navigate request shape at `Action::Navigate` + - Keep normal remote navigate behavior unchanged + - Continue emitting `sgBrowerserOpenPage` for the approved local-dashboard case so the helper page stays alive and the dashboard opens in a new visible tab + - Add focused callback-backend unit tests in the existing test module for approved and malformed local-dashboard requests +- Modify: `src/security/mac_policy.rs` + - Add a narrow validator for the approved local-dashboard presentation case + - Keep `validate(...)` unchanged for ordinary remote-domain flow + - Reject malformed marker payloads, non-HTML local paths, and mismatched `file://` / output-path combinations +- Modify: `tests/compat_runtime_test.rs` + - Keep the concrete hotlist workflow regressions in this existing integration test file + - Extend existing Zhihu hotlist export/screen regressions to assert the new summaries and the dashboard marker payload + - Keep the Excel route workflow assertion limited to summary plus “no dashboard navigate marker,” because exact launcher handoff is covered in `src/compat/artifact_open.rs` unit tests +- Modify: `tests/browser_tool_test.rs` + - Add `MacPolicy` coverage for approved local-dashboard presentation, rejected malformed presentation, and unchanged normal-domain validation in one exact file + - Extend the existing `default_rules_allow_zhihu_navigation` area with the new local-dashboard validation tests rather than creating a second policy test location +- Reference only if summary wording ripples outward: `tests/agent_runtime_test.rs:173-258` + - Existing direct-runtime user-visible summary assertion for Zhihu Excel export +- Reference only if summary wording ripples outward: `tests/service_task_flow_test.rs:704-839` + - Existing CLI-to-service user-visible summary assertion for Zhihu Excel export +- Reference only if summary wording ripples outward: `tests/service_ws_session_test.rs:755-869` + - Existing service-binary user-visible summary assertion for Zhihu Excel export +- Reference: `tests/compat_screen_html_export_tool_test.rs` + - Reuse the exact test seam `screen_html_export_tool_renders_dashboard_html_with_presentation_contract` + - Existing proof that `screen_html_export` already returns `presentation.url` +- Reference: `docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md` + +## Scope Guardrails + +- Do not modify `frontend/service-console/sg_claw_service_console.html`. +- Do not modify `src/service/protocol.rs`. +- Do not modify `browser-helper.html`. +- Do not modify `/sgclaw/callback/*` endpoint contracts. +- Do not modify websocket protocol framing or `src/browser/ws_protocol.rs`. +- Do not turn Excel-open and dashboard-open into a combined mode. +- Do not add a general-purpose local file browser or generic `file://` allowlist. +- Do not move post-export decisions into the frontend service console. +- Do not require websocket-backend parity in this slice. + +### Task 1: Add failing workflow tests for route-specific post-export actions + +**Files:** +- Modify: `tests/compat_runtime_test.rs:2154-2304` +- Reference: `src/compat/workflow_executor.rs:375-446` +- Reference: `docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md` + +- [ ] **Step 1: Rewrite the Excel hotlist assertion as a red test for the new summary only** + +Keep the current flow setup, but tighten the expectation so it proves the workflow route now reports post-export open success while staying exclusive from the dashboard path. + +Target shape: + +```rust +#[test] +fn handle_browser_message_chains_hotlist_skill_into_xlsx_export_and_auto_open() { + // existing setup + assert!(summary.contains("已导出并打开知乎热榜 Excel")); + assert!(generated.exists()); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, params, .. } + if action == &Action::Navigate + && params.get("sgclaw_local_dashboard_open").is_some() + ) + })); +} +``` + +Do not try to prove real OS launching in this workflow test. The exact `.xlsx` path handoff to the launcher belongs in `src/compat/artifact_open.rs` unit tests from Task 2. + +- [ ] **Step 2: Rewrite the dashboard hotlist assertion as a red test for browser auto-open** + +Tighten the existing dashboard test so it proves the workflow consumes `presentation.url` and emits the approved compat marker payload. + +Target shape: + +```rust +#[test] +fn handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open() { + // existing setup + assert!(summary.contains("已在浏览器中打开知乎热榜大屏")); + let navigate = sent.iter().find_map(|message| match message { + AgentMessage::Command { action, params, security, .. } + if action == &Action::Navigate + && security.expected_domain == "__sgclaw_local_dashboard__" => Some((params, security)), + _ => None, + }).expect("dashboard route should emit local-dashboard navigate request"); + + assert!(navigate.0["url"].as_str().unwrap().starts_with("file://")); + assert_eq!(navigate.0["sgclaw_local_dashboard_open"]["source"], json!("compat.workflow_executor")); + assert_eq!(navigate.0["sgclaw_local_dashboard_open"]["kind"], json!("zhihu_hotlist_screen")); + assert_eq!(navigate.0["sgclaw_local_dashboard_open"]["presentation_url"], navigate.0["url"]); +} +``` + +Also assert that this route still logs `call screen_html_export` and does not invoke the Excel opener path. + +- [ ] **Step 3: Add a missing-`presentation.url` regression in the workflow test module if none exists** + +Put this close to the existing hotlist tests and keep it narrow: + +```rust +#[test] +fn handle_browser_message_reports_dashboard_auto_open_protocol_error_when_presentation_url_is_missing() { + // mock screen_html_export success payload with output_path but no presentation.url + // assert summary contains 已生成知乎热榜大屏 ,但浏览器自动打开失败: +} +``` + +Use the existing summary/path helpers in the file instead of inventing new parsing helpers. + +- [ ] **Step 4: Run the focused compat runtime tests to verify they fail** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_xlsx_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_reports_dashboard_auto_open_protocol_error_when_presentation_url_is_missing --test compat_runtime_test -- --exact +``` + +Expected: FAIL because the workflow still returns artifact-only summaries and has no post-export open handling. + +- [ ] **Step 5: Commit the red workflow tests** + +```bash +git add tests/compat_runtime_test.rs +git commit -m "test: add hotlist post-export auto-open regressions" +``` + +### Task 2: Implement the compat post-export opener and update workflow summaries + +**Files:** +- Create: `src/compat/artifact_open.rs` +- Modify: `src/compat/mod.rs` +- Modify: `src/compat/workflow_executor.rs:375-446` +- Test: `src/compat/artifact_open.rs` +- Test: `tests/compat_runtime_test.rs` + +- [ ] **Step 1: Add the red unit tests in `src/compat/artifact_open.rs` before writing production code** + +Create the new module with a `#[cfg(test)]` block first so the Excel opener has an exact, non-UI verification seam. + +Target tests: + +```rust +#[test] +fn open_exported_xlsx_with_passes_generated_path_to_launcher() { + let mut seen = None; + let result = open_exported_xlsx_with(Path::new("C:/tmp/zhihu-hotlist.xlsx"), |path| { + seen = Some(path.to_path_buf()); + Ok(()) + }); + assert!(matches!(result, PostExportOpen::Opened)); + assert_eq!(seen.unwrap(), PathBuf::from("C:/tmp/zhihu-hotlist.xlsx")); +} + +#[test] +fn open_exported_xlsx_with_reports_launcher_failure() { + let result = open_exported_xlsx_with(Path::new("C:/tmp/zhihu-hotlist.xlsx"), |_path| { + Err("launcher failed".to_string()) + }); + assert!(matches!(result, PostExportOpen::Failed(reason) if reason.contains("launcher failed"))); +} +``` + +Add one matching dashboard payload test in the same file: + +```rust +#[test] +fn open_local_dashboard_uses_exact_approved_marker_payload() { + // FakeBrowserBackend records invoke(action, params, expected_domain) + // assert expected_domain == "__sgclaw_local_dashboard__" + // assert params.url == params.sgclaw_local_dashboard_open.presentation_url + // assert source/kind/output_path all match the approved contract +} +``` + +This step is mandatory so the Excel route is proven to hand the generated path to the opener without launching a real application. + +- [ ] **Step 2: Run the new unit tests to verify they fail** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_passes_generated_path_to_launcher --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_reports_launcher_failure --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_local_dashboard_uses_exact_approved_marker_payload --lib -- --exact +``` + +Expected: FAIL because `src/compat/artifact_open.rs` does not exist yet. + +- [ ] **Step 3: Create the small compat opener module** + +Add one focused helper module rather than embedding side effects directly into `workflow_executor.rs`. + +Target shape: + +```rust +pub const LOCAL_DASHBOARD_EXPECTED_DOMAIN: &str = "__sgclaw_local_dashboard__"; +pub const LOCAL_DASHBOARD_SOURCE: &str = "compat.workflow_executor"; +pub const LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN: &str = "zhihu_hotlist_screen"; + +pub enum PostExportOpen { + Opened, + Failed(String), +} + +pub fn open_exported_xlsx(output_path: &Path) -> PostExportOpen { + open_exported_xlsx_with(output_path, launch_with_default_xlsx_app) +} + +fn open_exported_xlsx_with(output_path: &Path, opener: F) -> PostExportOpen +where + F: FnOnce(&Path) -> Result<(), String>, +{ /* test seam */ } + +pub fn open_local_dashboard( + browser_backend: &dyn BrowserBackend, + output_path: &Path, + presentation_url: &str, +) -> PostExportOpen { /* invoke Action::Navigate with exact marker payload */ } +``` + +Keep the module tiny. The only dedicated test seam in this file should be `open_exported_xlsx_with(...)`; do not introduce a general launcher trait. + +- [ ] **Step 4: Implement the Windows-first `.xlsx` opener minimally** + +Use a focused local launcher that targets the current environment first. + +Preferred target shape: + +```rust +Command::new("cmd") + .args(["/C", "start", "", output_path_as_windows_string]) +``` + +Requirements: + +```text +- fail if the path does not exist +- do not swallow command-spawn errors +- do not open arbitrary user-selected files from outside this workflow +- keep cross-platform behavior minimal; only add a fallback branch if required to keep tests/build portable +``` + +If you need a non-Windows fallback for compilation, keep it obviously minimal and out of the hot path. + +- [ ] **Step 5: Parse payloads in `workflow_executor.rs` and call the new helper** + +Refactor `export_xlsx(...)` and `export_screen(...)` just enough to separate: + +```text +- tool execution +- payload parsing +- route-specific post-export open +- summary formatting +``` + +Minimal target behavior: + +```rust +match open_exported_xlsx(&output_path) { + PostExportOpen::Opened => format!("已导出并打开知乎热榜 Excel {output_path}"), + PostExportOpen::Failed(reason) => format!("已导出知乎热榜 Excel {output_path},但自动打开失败:{reason}"), +} +``` + +```rust +match open_local_dashboard(browser_backend, &output_path, &presentation_url) { + PostExportOpen::Opened => format!("已在浏览器中打开知乎热榜大屏 {output_path}"), + PostExportOpen::Failed(reason) => format!("已生成知乎热榜大屏 {output_path},但浏览器自动打开失败:{reason}"), +} +``` + +Change signatures only as much as needed to pass `browser_backend` into the dashboard route. Do not broaden unrelated call chains. + +- [ ] **Step 6: Export the helper module** + +Update `src/compat/mod.rs`: + +```rust +pub mod artifact_open; +``` + +Do not reorder unrelated module exports unless rustfmt does it. + +- [ ] **Step 7: Run the focused library and workflow regressions to verify green** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_passes_generated_path_to_launcher --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_reports_launcher_failure --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_local_dashboard_uses_exact_approved_marker_payload --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_xlsx_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_reports_dashboard_auto_open_protocol_error_when_presentation_url_is_missing --test compat_runtime_test -- --exact +``` + +Expected: PASS for the new library tests and the workflow regressions, unless the dashboard-open path still fails at backend/policy validation. + +- [ ] **Step 8: Commit the compat opener and workflow changes** + +```bash +git add src/compat/artifact_open.rs src/compat/mod.rs src/compat/workflow_executor.rs tests/compat_runtime_test.rs +git commit -m "feat: auto-open zhihu hotlist export artifacts" +``` + +### Task 3: Add failing backend and security tests for the narrow local-dashboard allowance + +**Files:** +- Modify: `src/browser/callback_backend.rs:536-840` +- Modify: `tests/browser_tool_test.rs` (`default_rules_allow_zhihu_navigation` section plus new local-dashboard validation tests) +- Reference: `src/security/mac_policy.rs:56-132` + +- [ ] **Step 1: Add a red callback-backend acceptance test for the approved local-dashboard request shape** + +Extend the existing `src/browser/callback_backend.rs` test module with one focused navigate test. + +Target shape: + +```rust +#[test] +fn callback_backend_accepts_approved_local_dashboard_navigate_request() { + let host = Arc::new(FakeCallbackHost::new(vec![success_reply(json!({ "navigated": true }))])); + let backend = BrowserCallbackBackend::new( + host.clone(), + test_policy(), + "http://127.0.0.1:17888/sgclaw/browser-helper.html", + ); + + let output = backend.invoke( + Action::Navigate, + json!({ + "url": "file:///C:/tmp/zhihu-hotlist-screen.html", + "sgclaw_local_dashboard_open": { + "source": "compat.workflow_executor", + "kind": "zhihu_hotlist_screen", + "output_path": "C:/tmp/zhihu-hotlist-screen.html", + "presentation_url": "file:///C:/tmp/zhihu-hotlist-screen.html" + } + }), + "__sgclaw_local_dashboard__", + ); + + assert!(output.unwrap().success); + assert_eq!(host.requests()[0].command, json!([ + "http://127.0.0.1:17888/sgclaw/browser-helper.html", + "sgBrowerserOpenPage", + "file:///C:/tmp/zhihu-hotlist-screen.html" + ])); +} +``` + +Do not weaken any existing normal-domain tests. + +- [ ] **Step 2: Add red rejection tests in exact files** + +Put malformed-request rejection in `src/browser/callback_backend.rs` next to the acceptance test: + +```rust +#[test] +fn callback_backend_rejects_local_dashboard_navigate_without_required_marker_fields() {} +``` + +Put policy-only validation in `tests/browser_tool_test.rs` so all public `MacPolicy` assertions stay in one place: + +```rust +#[test] +fn mac_policy_rejects_non_html_local_dashboard_presentation() {} + +#[test] +fn default_rules_allow_zhihu_navigation() { + let policy = MacPolicy::load_from_path(...).unwrap(); + policy.validate(&Action::Navigate, "www.zhihu.com").unwrap(); +} +``` + +Do not create a second `MacPolicy` regression location. + +- [ ] **Step 3: Run the focused backend/policy tests to verify red** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_accepts_approved_local_dashboard_navigate_request --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_rejects_local_dashboard_navigate_without_required_marker_fields --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" mac_policy_rejects_non_html_local_dashboard_presentation --test browser_tool_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" default_rules_allow_zhihu_navigation --test browser_tool_test -- --exact +``` + +Expected: the new local-dashboard tests FAIL; `default_rules_allow_zhihu_navigation` should still PASS. + +- [ ] **Step 4: Commit the red backend/security tests** + +```bash +git add src/browser/callback_backend.rs tests/browser_tool_test.rs +git commit -m "test: lock local dashboard navigate boundary" +``` + +### Task 4: Implement the narrow callback-backend and MacPolicy allowance + +**Files:** +- Modify: `src/browser/callback_backend.rs:300-351` +- Modify: `src/security/mac_policy.rs:56-132` +- Maybe modify: `src/security/mod.rs:9-27` +- Test: `src/browser/callback_backend.rs:536-840` +- Test: `tests/browser_tool_test.rs` (`default_rules_allow_zhihu_navigation` section plus new local-dashboard validation tests) + +- [ ] **Step 1: Add a narrow local-dashboard validation helper in `MacPolicy`** + +Keep `validate(...)` unchanged for ordinary domain flow. Add one small explicit helper instead. + +Target shape: + +```rust +pub fn validate_local_dashboard_presentation( + &self, + action: &Action, + expected_domain: &str, + presentation_url: &str, + output_path: &str, +) -> Result<(), SecurityError> { + // require Action::Navigate + // require expected_domain == "__sgclaw_local_dashboard__" + // require file:// URL + // require .html path + // require normalized file URL path matches output_path +} +``` + +If you need a new `SecurityError` variant for malformed local-dashboard input, add the smallest one that keeps error text clear. + +- [ ] **Step 2: Recognize only the exact approved request shape in `BrowserCallbackBackend::invoke(...)`** + +Before the normal `self.mac_policy.validate(&action, expected_domain)?` path runs, detect the one approved special case. + +Minimal target behavior: + +```rust +if let Some(local_dashboard) = approved_local_dashboard_request(&action, ¶ms, expected_domain) { + self.mac_policy.validate_local_dashboard_presentation( + &action, + expected_domain, + &local_dashboard.presentation_url, + &local_dashboard.output_path, + )?; +} else { + self.mac_policy.validate(&action, expected_domain)?; +} +``` + +The helper should require all of these fields exactly: + +```text +- action == Action::Navigate +- expected_domain == "__sgclaw_local_dashboard__" +- params.url exists +- params.sgclaw_local_dashboard_open.source == "compat.workflow_executor" +- params.sgclaw_local_dashboard_open.kind == "zhihu_hotlist_screen" +- params.sgclaw_local_dashboard_open.output_path exists +- params.sgclaw_local_dashboard_open.presentation_url exists and equals params.url +``` + +Anything else must continue down the normal rejection path. + +- [ ] **Step 3: Keep `build_command(Action::Navigate, ...)` simple** + +Do not add a second browser opcode or change the callback-host runtime contract. The approved local-dashboard case should still flow into the existing navigate command builder so the emitted command stays: + +```rust +json!([ + self.helper_page_url, + "sgBrowerserOpenPage", + target_url, +]) +``` + +- [ ] **Step 4: Run the focused backend/security tests to verify green** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_accepts_approved_local_dashboard_navigate_request --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_rejects_local_dashboard_navigate_without_required_marker_fields --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" mac_policy_rejects_non_html_local_dashboard_presentation --test browser_tool_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" default_rules_allow_zhihu_navigation --test browser_tool_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 5: Re-run the dashboard workflow regression after backend validation lands** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open --test compat_runtime_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 6: Commit the backend/security implementation** + +```bash +git add src/browser/callback_backend.rs src/security/mac_policy.rs src/security/mod.rs tests/browser_tool_test.rs tests/compat_runtime_test.rs +git commit -m "fix: allow approved local dashboard auto-open" +``` + +If `src/security/mod.rs` did not change, omit it from the commit. + +### Task 5: Run the focused verification sweep + +**Files:** +- Verify: `src/compat/artifact_open.rs` +- Verify: `tests/compat_runtime_test.rs` +- Verify: `tests/compat_screen_html_export_tool_test.rs` +- Verify: `tests/browser_tool_test.rs` +- Verify: `src/browser/callback_backend.rs` test module +- Reference only if summary wording ripples outward: `tests/agent_runtime_test.rs:173-258` +- Reference only if summary wording ripples outward: `tests/service_task_flow_test.rs:704-839` +- Reference only if summary wording ripples outward: `tests/service_ws_session_test.rs:755-869` + +- [ ] **Step 1: Re-run the library and workflow regressions** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_passes_generated_path_to_launcher --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_exported_xlsx_with_reports_launcher_failure --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" open_local_dashboard_uses_exact_approved_marker_payload --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_xlsx_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open --test compat_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" handle_browser_message_reports_dashboard_auto_open_protocol_error_when_presentation_url_is_missing --test compat_runtime_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 2: Re-run the tool contract regression that the dashboard route depends on** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" screen_html_export_tool_renders_dashboard_html_with_presentation_contract --test compat_screen_html_export_tool_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 3: Re-run the callback-backend and policy boundary tests** + +Run: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_accepts_approved_local_dashboard_navigate_request --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" callback_backend_rejects_local_dashboard_navigate_without_required_marker_fields --lib -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" mac_policy_rejects_non_html_local_dashboard_presentation --test browser_tool_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" default_rules_allow_zhihu_navigation --test browser_tool_test -- --exact +``` + +Expected: PASS + +- [ ] **Step 4: Re-run outward-facing summary regressions only if needed** + +Only if the updated summary text breaks existing assertions, run exactly these existing regressions and adjust only the affected expectation text: + +```bash +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap --test agent_runtime_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" client_to_service_regression_routes_zhihu_without_helper_bootstrap_or_invalid_hmac_seed_output --test service_task_flow_test -- --exact +cargo test --manifest-path "D:/data/ideaSpace/rust/sgClaw/claw-new/Cargo.toml" service_binary_submit_flow_routes_zhihu_without_helper_bootstrap --test service_ws_session_test -- --exact +``` + +Expected: PASS for any test you had to touch. Skip this step entirely if those files needed no edits. + +- [ ] **Step 5: Inspect scope before finishing with exact git commands** + +Run: + +```bash +git diff --name-only -- src/compat/artifact_open.rs src/compat/mod.rs src/compat/workflow_executor.rs src/browser/callback_backend.rs src/security/mac_policy.rs src/security/mod.rs tests/compat_runtime_test.rs tests/browser_tool_test.rs tests/agent_runtime_test.rs tests/service_task_flow_test.rs tests/service_ws_session_test.rs +git diff --stat -- src/compat/artifact_open.rs src/compat/mod.rs src/compat/workflow_executor.rs src/browser/callback_backend.rs src/security/mac_policy.rs src/security/mod.rs tests/compat_runtime_test.rs tests/browser_tool_test.rs tests/agent_runtime_test.rs tests/service_task_flow_test.rs tests/service_ws_session_test.rs +``` + +Confirm the diff only touches: + +```text +- compat workflow/orchestration +- compat post-export helper module +- callback backend narrow local-dashboard acceptance +- MacPolicy narrow local-dashboard validation +- focused related tests +``` + +Confirm it does **not** touch: + +```text +- frontend/service-console/ +- src/service/protocol.rs +- browser-helper.html +- callback-host endpoint contracts +- websocket transport/protocol files +``` + +- [ ] **Step 6: Commit only if verification required additional code changes** + +```bash +git add src/compat/artifact_open.rs src/compat/mod.rs src/compat/workflow_executor.rs src/browser/callback_backend.rs src/security/mac_policy.rs tests/compat_runtime_test.rs tests/browser_tool_test.rs tests/agent_runtime_test.rs tests/service_task_flow_test.rs tests/service_ws_session_test.rs +git commit -m "test: tighten hotlist post-export auto-open verification" +``` + +If verification required no further code changes, do not create an extra commit. diff --git a/docs/superpowers/specs/2026-04-02-ws-browser-backend-auth-design.md b/docs/superpowers/specs/2026-04-02-ws-browser-backend-auth-design.md new file mode 100644 index 0000000..5e80c2d --- /dev/null +++ b/docs/superpowers/specs/2026-04-02-ws-browser-backend-auth-design.md @@ -0,0 +1,506 @@ +# WS 浏览器后端认证替换设计 + +## 背景 + +当前 `sg_claw` 的 websocket service 路径已经能接收 `sg_claw_client` 请求、复用共享 task runner、连接真实浏览器 websocket 地址 `browser_ws_url`,并进入真实 skill 执行链路。但真实联调时,所有浏览器相关调用都会失败并返回: + +- `invalid hmac seed: session key must not be empty` + +根因已经定位: + +- pipe 模式在 [src/lib.rs](src/lib.rs) 中通过 handshake 拿到 `session_key`,并用它构造 `BrowserPipeTool` +- ws service 模式在 [src/service/server.rs](src/service/server.rs) 中仍然构造 `BrowserPipeTool::new(..., vec![])` +- `BrowserPipeTool` 的认证模型要求非空 session key,因此 ws service 路径虽然使用的是浏览器 websocket 协议,仍错误地依赖了 pipe 特有的 HMAC/session-key 语义 + +这会导致: + +1. `sg_claw_client -> sg_claw` 连接正常 +2. skill 加载与模型调用正常 +3. 真实浏览器动作开始执行 +4. 但所有 browser tool 调用在认证层统一失败 + +## 目标 + +在 **仅限 ws 模式改动** 的前提下,让 `sg_claw` service 路径改为使用 **ws-native browser backend**,不再依赖 `BrowserPipeTool` 的 pipe session-key 认证模型,从而让真实浏览器联调可用。 + +## 约束 + +必须满足: + +- 只改 ws 模式相关实现 +- 不破坏 legacy pipe 模式 +- 不修改 pipe handshake 语义 +- 不修改 `src/lib.rs` 的 pipe 主入口行为 +- 不引入临时绕过认证或 fake seed +- 不扩大到多客户端、多任务、队列、守护进程管理 + +## 非目标 + +本次不做: + +- 自动拉起 sgBrowser +- 浏览器进程管理 +- 多浏览器实例支持 +- service/client UX 优化 +- browser ws 协议扩展 +- pipe 模式重构 +- 统一重构所有 runtime 层去完全依赖 `BrowserBackend` + +## 现状分析 + +### 正常 pipe 路径 + +pipe 模式当前在 [src/lib.rs](src/lib.rs) 中: + +1. 通过 `perform_handshake(...)` 读取浏览器侧初始化消息 +2. 从 handshake 中拿到 `session_key` +3. 用 `BrowserPipeTool::new(transport.clone(), mac_policy, handshake.session_key)` 构造浏览器工具 +4. 后续 browser action 使用 pipe/HMAC 语义 + +该路径已经可用,本次不能动。 + +### 当前 ws service 路径 + +当前 ws 模式在 [src/service/server.rs](src/service/server.rs) 中: + +1. `sg_claw_client` 将任务发给 `sg_claw` service +2. service 构造 `ServiceBrowserTransport` +3. service 用 `BrowserPipeTool::new(transport.clone(), mac_policy.clone(), vec![])` +4. browser action 经 `ServiceBrowserTransport` 编码为 browser websocket 请求并发给 `browser_ws_url` + +问题在于第 3 步: + +- service 走的是 browser websocket 协议 +- 但却仍使用 `BrowserPipeTool` +- `BrowserPipeTool` 内部仍坚持要求 pipe session key +- 因此真实 ws 联调时直接失败 + +### 现有 ws-native 能力 + +代码中已经存在: + +- [src/browser/ws_protocol.rs](src/browser/ws_protocol.rs):固定 browser websocket 协议 codec +- [src/browser/ws_backend.rs](src/browser/ws_backend.rs):`WsBrowserBackend` +- [src/browser/mod.rs](src/browser/mod.rs):已导出 `WsBrowserBackend` + +`WsBrowserBackend` 本身不依赖 pipe session key,而是: + +- 使用 `WsClient` 发送/接收文本帧 +- 使用 `MacPolicy` 做动作级校验 +- 通过 `encode_v1_action(...)` 与 `decode_callback_frame(...)` 处理 ws 协议 + +这正是 ws service 模式应该使用的模型。 + +## 关键集成缝隙 + +当前共享 runner 的真实缝隙已经确认: + +- [src/agent/task_runner.rs](src/agent/task_runner.rs) 的 `run_submit_task(...)` 仍直接要求 `&BrowserPipeTool` +- [src/compat/runtime.rs](src/compat/runtime.rs) 与 [src/compat/orchestration.rs](src/compat/orchestration.rs) 也继续以 `BrowserPipeTool` 作为主浏览器调用对象 +- 同时 compat runtime 内部已经存在 `Arc` 的工具适配层,只是它目前是从 `PipeBrowserBackend::from_inner(browser_tool)` 包出来的 + +这意味着本次实现不能只在 `src/service/server.rs` 里替换构造逻辑,而必须在 **ws 专用调用面** 增加一个最小适配缝隙,让 service 模式能把 `WsBrowserBackend` 传入 compat/runtime/orchestration,而 pipe 继续保持 `BrowserPipeTool` 原样。 + +允许的最小缝隙定义如下: + +1. `run_submit_task(...)` 的 pipe 版本保持不动,供 pipe 入口继续使用 +2. 新增一个 **仅供 ws service 使用** 的并行入口,例如: + - `run_submit_task_with_browser_backend(...)` + - 或 service 侧调用的等价 ws-only adapter +3. ws-only 入口内部允许把浏览器依赖类型降到 `Arc` +4. `src/lib.rs`、pipe handshake、pipe `BrowserPipeTool` 构造逻辑不允许改行为 + +## 设计决策 + +### 决策 1:ws service 路径弃用 `BrowserPipeTool` + +在 ws service 路径中,不再构造 `BrowserPipeTool`。 + +替代方案: + +- service 侧提供一个 `WsClient` 实现 +- 直接构造 `WsBrowserBackend` +- 让 ws service 的 browser action 通过 `WsBrowserBackend` 执行 + +### 决策 2:pipe 路径保持原样 + +pipe 模式继续: + +- handshake +- `session_key` +- `BrowserPipeTool` + +不做语义调整,不引入兼容层,不改动已存在的验证路径。 + +### 决策 3:runner 只在 ws 调用面做最小接线 + +当前共享 task runner 复用已经存在,本次不做大重构。 + +策略是: + +- 只在 ws service 用到的调用面,改成可使用 `WsBrowserBackend` +- 如果必须扩共享调用接口,则仅做**最小、兼容、对 pipe 零影响**的改动 +- 任何涉及 pipe 行为变更的改动都不允许 + +### 决策 4:保留现有 browser websocket 连接生命周期 + +本次不重做连接管理架构。 + +继续维持: + +- 单客户端 +- 单任务串行 +- 按现有 service 生命周期维护 browser websocket 连接 + +只替换认证错误的执行路径,不顺手做生命周期优化。 + +## 目标架构 + +### 目标调用链 + +```text +sg_claw_client + -> sg_claw service + -> ws-native browser backend + -> browser_ws_url + -> sgBrowser +``` + +### 与 pipe 的并行关系 + +```text +pipe mode: + browser process <-> stdio/pipe <-> sgclaw::run() <-> BrowserPipeTool + +ws mode: + sg_claw_client <-> sg_claw service <-> WsBrowserBackend <-> sgBrowser websocket +``` + +两条路径并行存在,互不混用认证模型。 + +## 模块设计 + +### 1. `src/service/server.rs` + +这是本次核心改动文件。 + +#### 当前职责 + +- 管理 service client websocket 收发 +- 将 service 请求转入共享 runner +- 维护 service->browser 的 websocket 传输桥 + +#### 本次改动 + +- 将“service->browser 的桥”从 `Transport + BrowserPipeTool` 组合改为 `WsClient + WsBrowserBackend` +- 删除 ws service 路径中对空 `session_key` 的依赖 +- 继续保留 service socket 生命周期与 session 状态机 + +#### 目标结构 + +可接受的目标形态: + +- `ServiceBrowserWsClient`:实现 `WsClient` +- 内部继续维护真实 browser websocket 连接 +- `serve_client(...)` 在处理任务时构造 `WsBrowserBackend` +- 共享 runner 或其 ws 调用包装层通过该 backend 执行 browser action + +### 2. 共享 runner / ws 调用包装层 + +本次不要求把全项目统一改成 `BrowserBackend`。 + +但 ws service 模式必须能把 browser action 接到 `WsBrowserBackend`。 + +可接受的最小方案: + +- 在 ws service 使用的一层引入一个只服务 ws 模式的 adapter +- 该 adapter 把 runner 所需的 browser 调用能力委托给 `WsBrowserBackend` + +要求: + +- pipe 现有调用签名不变,或即使扩展也必须保证 pipe 行为完全一致 +- 不允许为了 ws 把 pipe 入口重写 + +### 3. `src/browser/ws_backend.rs` + +原则上复用现有实现。 + +只有在以下情况下才允许最小补改: + +- service 真实联调发现它缺一个 ws service 必需但当前未暴露的能力 +- 该补改只服务 ws-native 路径 +- 不影响现有测试语义 + +## 连接职责与边界 + +为避免 service 侧与 `WsBrowserBackend` 重复实现责任,本次显式约束如下: + +### `WsBrowserBackend` 负责 + +- 单次 `invoke(...)` 的请求串行化 +- 调用 `encode_v1_action(...)` +- 发送 websocket 文本帧 +- 等待即时状态帧 +- 如有 callback,等待 callback 帧并做名称匹配 +- 将结果统一为 `CommandOutput` +- 按现有 `WsBrowserBackend` 语义产出 timeout / protocol 错误 + +### service 侧 `WsClient` 适配器负责 + +- 持有真实 browser websocket 连接 +- 在第一次请求时建立到 `browser_ws_url` 的连接 +- 把 `send_text(...)` / `recv_text_timeout(...)` 委托到真实 websocket +- 将底层关闭、reset、timeout 统一映射为既有 `PipeError` 语义 +- 不实现 request/response correlation,不解析 browser ws 协议 payload + +### 明确不允许 + +- service 侧继续手写 callback 轮询逻辑 +- service 侧继续直接调用 `encode_v1_action(...)` 组包作为主路径 +- 在 service 侧复制 `WsBrowserBackend` 的协议处理逻辑 + +这样可以保证: + +- `src/service/server.rs` 只负责“连线” +- `src/browser/ws_backend.rs` 继续负责“ws 浏览器调用语义” + +## 数据流设计 + +### 成功路径 + +1. `sg_claw_client` 向 `sg_claw` 发 `SubmitTask` +2. service 收到任务并进入共享 runner +3. 当 runner 需要浏览器动作时: + - ws service 调用 `WsBrowserBackend.invoke(...)` +4. `WsBrowserBackend`: + - 用 `MacPolicy` 校验动作 + - 用 `encode_v1_action(...)` 编码请求 + - 发往 `browser_ws_url` + - 等待状态帧 + - 如有 callback,继续等 callback 帧 +5. 结果返回到 runner +6. runner 继续执行并向 client 流式输出日志和 completion + +### 失败路径 + +#### browser websocket 不可连 + +- 返回明确的 browser websocket connect 错误 +- 不冒充认证错误 + +#### 浏览器返回非 0 状态 + +- 返回明确协议错误:`browser returned non-zero status` + +#### callback 超时 + +- 返回 timeout + +#### websocket 断开 + +- 返回 `PipeError::PipeClosed` +- 由 service 生命周期逻辑处理 + +#### 不再允许的错误 + +- `invalid hmac seed: session key must not be empty` + +该错误在 ws 模式下应彻底消失。 + +## 失败语义 + +为便于测试与实现,ws-only 路径的 outward error 语义固定如下: + +### browser websocket connect 失败 + +- outward: `PipeError::Protocol("browser websocket connect failed: ...")` + +### 浏览器返回非 0 状态码 + +- outward: `PipeError::Protocol("browser returned non-zero status: ...")` + +### callback 超时 + +- outward: `PipeError::Timeout` +- timeout 来源:沿用 `WsBrowserBackend` / ws service 当前 response timeout 配置,默认 30 秒 + +### websocket 被对端正常关闭或 reset + +- outward: `PipeError::PipeClosed` +- 不允许使用“等价错误”这类不精确表述 + +### 本次必须消除的错误 + +- `invalid hmac seed: session key must not be empty` + +任何 ws service 联调路径再出现该错误,都视为实现未完成。 + +## 测试设计 + +### 分层测试策略 + +为避免依赖 LLM/planner 的非确定性行为,本次测试必须分成两层,且各自断言不同目标: + +#### A. backend / adapter 层测试(确定性) + +这一层不经过 `sg_claw_client`、不经过真实模型规划,直接验证 ws-only 技术行为。 + +目标: + +1. `ServiceBrowserWsClient` 与 `WsBrowserBackend` 的组合可以: + - 发送 `Navigate` + - 接收 `0` 状态 + - 在 callback 场景下读取 callback 文本 +2. 当 fake browser server 主动关闭/reset 时: + - 在 `WsClient` / `WsBrowserBackend.invoke(...)` 观察层断言 outward error 必须是 `PipeError::PipeClosed` +3. 当 fake browser server 不返回 callback 时: + - 在 `WsBrowserBackend.invoke(...)` 观察层断言 outward error 必须是 `PipeError::Timeout` +4. 该层测试完全不依赖 LLM、planner、skills 路由 + +建议: + +- 新增 focused ws service/backend test +- 输入动作固定为代码直接调用 `invoke(Action::Navigate, ...)` 等,而不是自然语言任务 + +#### B. client -> service 集成测试(链路验证) + +这一层验证 ws-only 接线已经替换掉空 session key 路径,但不承担细粒度协议语义断言。 + +目标: + +1. 通过真实 `sg_claw_client -> sg_claw service` 发起一个最小自然语言任务 +2. fake browser websocket server 至少收到一个来自 ws-only 路径的文本帧 +3. client/service 输出中不再出现: + - `invalid hmac seed: session key must not be empty` +4. 该层只证明: + - ws service 已不再走空 session key 的 pipe 认证路径 + - 真实端到端链路已能到达 browser websocket + +该层不用于断言精确 enum 身份,也不用于覆盖 callback timeout / reset 细节。 + +### 新增红测 1:ws-only backend/adapter 基本调用可用 + +目标: + +- 不走自然语言任务 +- 直接构造 ws service 使用的 `WsClient` + `WsBrowserBackend` +- 调用固定动作:`Action::Navigate`,目标 url 固定为 `https://www.zhihu.com/hot` +- fake browser websocket server 返回 `0` +- 断言: + - `invoke(...)` 成功 + - fake server 收到的首个文本帧可按 `ws_protocol` 语义解释为 `Navigate` + +### 新增红测 2:ws-only backend/adapter 断链语义固定 + +目标: + +- 不走自然语言任务 +- fake browser websocket server 在接受请求后主动关闭或 reset +- 在 `invoke(...)` 观察层断言: + - outward error 固定为 `PipeError::PipeClosed` + +### 新增红测 3:ws-only backend/adapter callback timeout 语义固定 + +目标: + +- 不走自然语言任务 +- fake browser websocket server 返回 `0` 但不返回 callback 帧 +- 在 `invoke(...)` 观察层断言: + - outward error 固定为 `PipeError::Timeout` + +### 新增红测 4:client->service 链路不再触发空 session key 错误 + +目标: + +- 通过真实 `sg_claw_client -> sg_claw service` 链路触发浏览器动作 +- 用 fake browser websocket 服务端接住请求 +- 任务输入固定为:`打开知乎热榜并读取页面主区域文本` +- 断言 client/service 输出中不再出现: + - `invalid hmac seed: session key must not be empty` +- 断言 fake browser server 至少收到了一个文本帧 + +### 回归测试 + +必须重新运行并保持通过: + +#### pipe 回归 + +```bash +cargo test --test pipe_handshake_test -- --nocapture +``` + +如实现涉及 browser tool 上层接线,还需补跑: + +```bash +cargo test --test browser_tool_test --test compat_browser_tool_test --test runtime_task_flow_test -- --nocapture +``` + +#### ws 回归 + +```bash +cargo test --test service_ws_session_test --test service_task_flow_test --test browser_ws_protocol_test --test browser_ws_backend_test -- --nocapture +``` + +## 手工验收 + +使用真实配置和真实已启动 sgBrowser: + +1. 启动 sgBrowser,并确保 `browserWsUrl` 可用 +2. 启动 `sg_claw` +3. 运行: + - `sg_claw_client` +4. 发送知乎最小任务: + - 打开知乎热榜并读取页面主区域文本 +5. 观察: + - 不再出现 `invalid hmac seed` + - 出现真实 browser action 日志 + - 能返回单次 completion +6. 再运行旧知乎 skill: + - `读取知乎热榜数据,并导出 excel 文件` +7. 验证旧知乎 skill 进入真实 browser 执行路径 +8. 最后确认 legacy pipe 入口仍可启动(仅验证,不允许为此修改 pipe 实现) + +## 风险 + +### 风险 1:ws service 与共享 runner 接口耦合过深 + +控制: + +- 只在 ws 使用面做 adapter +- 不对 pipe 主入口做结构性改造 + +### 风险 2:为适配 ws-native backend 误改 pipe 调用链 + +控制: + +- 所有 pipe 回归必须在每轮修改后重跑 +- `src/lib.rs` 不允许改行为 + +### 风险 3:ws service 内联连接逻辑与 `WsBrowserBackend` 责任重复 + +控制: + +- 本次先以最小变更消除认证阻塞 +- 不顺手做大规模整理 + +## 通过标准 + +满足以下全部条件才算完成: + +1. ws service 路径不再依赖空 session key +2. 不再出现 `invalid hmac seed: session key must not be empty` +3. 真实 browser websocket 请求能发到 sgBrowser/fake browser server +4. 旧知乎 skill 至少能进入真实 browser action 执行链路 +5. pipe 模式零回归 +6. 所有新增/相关测试通过 + +## 实施建议 + +按以下顺序实施: + +1. 先补红测,锁定“ws 不再触发 invalid hmac seed” +2. 再把 ws service 路径切到 `WsBrowserBackend` +3. 跑 ws 测试 +4. 跑 pipe 回归 +5. 做真实知乎最小任务 smoke +6. 再做旧知乎 skill smoke diff --git a/docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md b/docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md new file mode 100644 index 0000000..435a36f --- /dev/null +++ b/docs/superpowers/specs/2026-04-03-ws-browser-bridge-path-design.md @@ -0,0 +1,276 @@ +# WS Browser Bridge Path Design + +## Background + +The repository now has explicit live evidence that the real sgBrowser websocket endpoint at `ws://127.0.0.1:12345` is **reachable** but is **not validated as an external-control surface**. + +The probe transcript in `docs/_tmp_sgbrowser_ws_probe_transcript.md` shows a stable outcome across the full bootstrap matrix: + +- direct open-page frame +- `sgOpenAgent` +- `sgSetAuthInfo` +- `sgBrowserLogin` +- `sgBrowerserActiveTab` +- combined bootstrap attempts +- alternate `requesturl` values + +Across all of those sequences, the endpoint behaved like this: + +1. websocket connection succeeds +2. first inbound text frame is always the banner `Welcome! You are client #1` +3. no sequence produced a reproducible numeric status frame for a real business action +4. no sequence produced a reproducible callback frame for a real business action +5. follow-on business frames timed out or produced no further usable protocol traffic + +That means the current project can no longer treat raw external websocket business frames as the default production integration surface. + +## Why the raw websocket path is now considered non-validated + +The decision is not based on a guess. It is based on both live evidence and repository evidence. + +### Live evidence + +`docs/_tmp_sgbrowser_ws_probe_transcript.md` proves that the real endpoint did **not** yield the one thing raw external control needs: + +- a reproducible status/callback response for a real browser action + +Because that never happened, the bootstrap hypothesis did not clear the acceptance bar. + +### Repository evidence + +The rest of the repository already points to a different product integration model. + +#### 1. Historical frontend code uses browser-host bridge surfaces + +In `frontend/archive/sgClaw验证-已归档/testRunner.js:15-26`: + +- the runtime checks for `window.sgFunctionsUI` +- the runtime checks for `window.BrowserAction` +- the working path uses `window.sgFunctionsUI(action, params, callback)` + +That is a host/browser bridge contract, not an external raw websocket RPC contract. + +#### 2. Prior architecture docs make `CommandRouter` the execution entry + +In `docs/superpowers/specs/2026-03-25-superrpa-sgclaw-browser-control-design.md:16-18` and `:36-50`: + +- reuse SuperRPA `CommandRouter` as the browser execution entry +- keep browser-side hosting, security re-check, and dispatch in SuperRPA +- avoid building parallel browser automation APIs + +That is directly incompatible with treating raw external websocket business frames as the primary control plane. + +#### 3. Project planning docs describe FunctionsUI IPC as the supported frontend seam + +In `docs/archive/项目管理与排期/协作时间表.md:419-430`: + +- Vue/FunctionsUI calls browser-host methods such as `window.superrpa.sgclaw.start()` and `sendCommand(...)` +- browser host pushes callbacks such as `onStatusChange(...)` and `onLog(...)` + +Again, this is a bridge and host IPC model. + +#### 4. Floating-chat planning already preserves named bridge calls + +In `docs/plans/2026-03-27-sgclaw-floating-chat-frontend-design.md:289-293`: + +- `connect()` issues `sgclawConnect` +- `start()` issues `sgclawStart` +- `stop()` issues `sgclawStop` +- `submitTask()` issues `sgclawSubmitTask` + +That design work assumes a named browser bridge, not direct raw websocket frames. + +## Decision + +**Authoritative browser integration surface: the browser-host bridge path, not the raw external sgBrowser websocket business-frame path.** + +More concretely, sgClaw should target this chain: + +```text +sgClaw runtime + -> existing browser-facing bridge contract + -> FunctionsUI / host IPC + -> BrowserAction / sgclaw host callbacks + -> existing SuperRPA CommandRouter dispatch +``` + +## Authoritative seams for future implementation + +Because this repository does not contain the full SuperRPA browser host source tree, the bridge-first implementation must integrate at the **nearest validated seam available in this repo**, while staying aligned with the external browser-host contract already documented. + +The future implementation must model **two different bridge layers** explicitly instead of mixing them together. + +### Layer 1: session/lifecycle bridge contract + +This layer is evidenced by the named calls already present in repo documentation: + +- `sgclawConnect` +- `sgclawStart` +- `sgclawStop` +- `sgclawSubmitTask` + +This layer manages session setup, task submission, and host/UI lifecycle behavior. + +It is important evidence that a browser-host bridge exists, but it is **not** the per-browser-action contract that a new `BrowserBackend` implementation should target. + +### Layer 2: browser-action execution contract + +This is the authoritative target for the new browser backend. + +It is evidenced by: + +- `window.BrowserAction(...)` in archived frontend code +- `FunctionsUI` / host IPC integration in archived planning docs +- browser-side dispatch through `CommandRouter` in `docs/superpowers/specs/2026-03-25-superrpa-sgclaw-browser-control-design.md` + +In this repository, the concrete boundary must be a **repo-local semantic transport seam** that can be implemented and tested without access to the external SuperRPA host code. + +That seam should be a narrow Rust-side contract such as `BridgeActionTransport`: + +- input: semantic browser action request (`navigate`, `click`, `getText`, etc.) plus params and expected domain +- output: semantic success/error reply that can be normalized back into `BrowserBackend` results + +`BridgeBrowserBackend` should target **Layer 2 only**. + +### Explicit out-of-scope boundary + +The following are outside this repository and therefore outside the immediate Rust implementation slice: + +- actual SuperRPA C++ host/browser code +- actual `FunctionsUI` TypeScript host plumbing in the external browser repository +- actual `CommandRouter` implementation in the external browser repository + +This repository should implement only: + +- the Rust-side bridge contract types +- the Rust-side bridge transport/provider seam +- the Rust-side bridge-backed browser adapter +- deterministic tests against those seams + +### What this means practically + +The next implementation slice should **not** continue trying to make `WsBrowserBackend` drive the real browser endpoint directly. + +Instead, the next implementation slice should introduce a **bridge-backed browser adapter** that: + +- preserves the Rust-side `BrowserBackend` contract where practical +- translates browser actions onto the Layer-2 semantic bridge surface +- keeps lifecycle/session bridge calls separate from per-action browser execution +- leaves the raw websocket probe code as diagnostic infrastructure only + +## Chosen architecture + +Use a bridge-backed adapter design. + +### Target shape + +```text +compat/runtime/orchestration + -> Arc + -> BridgeBrowserBackend (new) + -> BridgeActionTransport (new repo-local seam) + -> external browser-host bridge / FunctionsUI IPC + -> BrowserAction / CommandRouter path +``` + +### Why this shape + +- It preserves the already-useful Rust-side browser abstraction (`BrowserBackend`) instead of re-plumbing the entire runtime. +- It keeps raw websocket probing available for diagnostics without letting it dictate production architecture. +- It matches the architecture already documented for SuperRPA integration. +- It keeps future work narrow: one new adapter layer instead of rewriting all runtime behavior. + +## What stays the same + +### Pipe path remains unchanged + +The existing pipe path must remain behaviorally unchanged: + +- `src/lib.rs` +- pipe handshake behavior +- `BrowserPipeTool` +- existing HMAC/domain validation semantics + +The bridge-first work is about the **ws service / real browser integration path**, not about replacing or weakening the pipe path. + +### Existing compat/runtime abstractions should be preserved where practical + +The next slice should reuse: + +- `BrowserBackend` +- existing browser tool adapters in compat/runtime +- existing task runner/orchestration flow + +The new work should be concentrated in a bridge adapter and its wiring, not spread through unrelated layers. + +## What does not stay the same + +### Raw websocket is no longer the mainline production assumption + +The repository may keep: + +- `src/browser/ws_backend.rs` +- `src/browser/ws_protocol.rs` +- `src/browser/ws_probe.rs` +- `src/bin/sgbrowser_ws_probe.rs` + +But those should now be treated as: + +- protocol tooling +- fake-server test tooling +- live diagnostic/probe tooling +- possibly constrained compatibility code + +They should remain diagnostic-only in this repository and must not be treated as the production path for reaching the real browser. + +## Design constraints for the bridge slice + +The bridge-path implementation must follow these constraints: + +1. **No parallel browser API invention.** Reuse the real bridge/browser action surface already evidenced in docs and archived frontend code. +2. **No pipe regression.** Do not alter the working pipe entry path. +3. **Adapter-first design.** Prefer one bridge-backed backend implementation over broad runtime rewrites. +4. **TDD first.** Add focused bridge adapter tests before production wiring. +5. **Repository-local seam only.** Where external SuperRPA browser-host code is unavailable here, encode the contract in narrow adapters and tests instead of guessing internals. + +## Testing implications + +The bridge path changes what “proof” looks like. + +### Required proof for the next slice + +The next implementation slice must prove: + +- a browser action can be emitted onto the bridge contract deterministically +- the bridge adapter maps replies/errors back into `BrowserBackend` semantics +- compat/runtime can use the bridge-backed backend without pipe regression + +### No longer required for acceptance + +The next slice does **not** need to prove that raw websocket business frames work directly against `ws://127.0.0.1:12345`, because the current evidence rejected that path as the mainline assumption. + +## Acceptance criteria for this design decision + +This design is correct only if future implementation follows all of these: + +1. The next production slice targets the browser-host bridge path rather than raw external websocket business frames. +2. The raw websocket probe tooling remains diagnostic only. +3. Existing pipe behavior stays unchanged. +4. The next implementation plan identifies a narrow bridge-backed adapter, not a broad architecture rewrite. +5. Future success claims are based on bridge-path execution evidence, not on reinterpreting the existing raw-websocket transcript. + +## Consequences + +### Positive + +- Aligns implementation with the strongest evidence already in the repo +- Stops further speculative coding on the wrong control surface +- Preserves existing ws probe work as useful diagnostics +- Keeps the next slice narrow and testable + +### Trade-off + +- Requires an additional adapter design step before more production code can land +- Defers any hope that a small websocket tweak alone will unlock the real browser path + +That trade-off is correct, because the current blocker is no longer a small protocol bug. It is an integration-surface mismatch. \ No newline at end of file diff --git a/docs/superpowers/specs/2026-04-03-ws-browser-integration-surface-correction-design.md b/docs/superpowers/specs/2026-04-03-ws-browser-integration-surface-correction-design.md new file mode 100644 index 0000000..53876e0 --- /dev/null +++ b/docs/superpowers/specs/2026-04-03-ws-browser-integration-surface-correction-design.md @@ -0,0 +1,288 @@ +# WS Browser Integration Surface Correction Design + +## Background + +The current websocket service path already proved two things: + +1. `sg_claw_client -> sg_claw` request handling works. +2. The ws-native backend/auth replacement removed the old pipe/HMAC mismatch that produced `invalid hmac seed: session key must not be empty`. + +However, real sgBrowser smoke still does not work. + +Manual probing against the configured real browser websocket endpoint (`ws://127.0.0.1:12345`) produced a stable pattern: + +- the connection succeeds +- the server sends one banner text frame such as `Welcome! You are client #1` +- after that, business frames receive no status frame and no callback frame +- this remains true for: + - valid-looking `sgBrowerserOpenPage` frames + - callback-based APIs + - no-arg/context-light APIs + - malformed or obviously wrong frames + +At the same time, local documentation and archived frontend code point to a different integration model: + +- the websocket API doc describes the websocket service as a transport replacement for page-context JavaScript calls, and requires the current page URL (`requesturl`) in each message +- archived frontend/product code uses `window.sgFunctionsUI(...)` and `window.BrowserAction(...)` +- archived architecture docs describe the supported product path as `FunctionsUI -> browser host bridge -> BrowserAction/CommandRouter`, not an arbitrary external process speaking raw browser websocket frames + +This means the current assumption is no longer acceptable as the default architecture hypothesis: + +- **Rejected default assumption:** `sg_claw` can directly control the real browser by speaking raw business frames to `browserWsUrl` as an external client, with no additional browser-host bridge, page context, or bootstrap/session contract. + +That assumption may still turn out to be partially true, but it is no longer justified enough to continue coding against as the mainline design. + +## Problem Statement + +The project currently has a functioning ws-native transport implementation, but it does **not** have a validated real integration surface for sgBrowser. + +The unresolved question is now architectural rather than syntactic: + +### Possibility A: raw websocket is valid, but requires hidden bootstrap/preconditions + +Examples suggested by the local API document: + +- a real browser page must already exist and `requesturl` must refer to that page +- one or more setup calls such as `sgSetAuthInfo`, `sgBrowserLogin`, `sgOpenAgent`, or `sgBrowerserActiveTab` must happen first +- callbacks may require a browser-side JS/page context that an external process does not automatically have +- some APIs may only work against agent/show/hide areas after browser-side initialization + +### Possibility B: raw websocket is not the supported external control surface + +Instead, the real product path may require: + +- `FunctionsUI` / browser-host IPC +- host-side security and routing +- `BrowserAction` / `CommandRouter` dispatch +- page-injected or browser-embedded execution context + +If this is true, continuing to invest in raw external websocket business-frame handling as the main integration surface would be architectural drift. + +## Goal + +Replace the current unvalidated ws-native-direct assumption with a decision-backed integration strategy. + +The next implementation slice must do exactly one of these two things based on evidence: + +1. **Bootstrap path:** prove that raw websocket control is real and supported once the missing bootstrap/precondition sequence is performed, then codify that bootstrap sequence and keep `WsBrowserBackend` as the execution surface. +2. **Bridge path:** prove that raw websocket is not the real supported surface for external control, then pivot the runtime design so sgClaw targets the actual browser-host bridge / `BrowserAction` surface instead of pretending the raw websocket is enough. + +## Non-goals + +This correction slice does **not** include: + +- broad feature work on the floating chat UI +- multi-client service redesign +- browser process lifecycle management +- speculative protocol expansion +- generic reconnection/backoff work +- rewriting the entire compat/runtime stack without evidence +- landing both bootstrap and bridge implementations in one branch + +The purpose of this slice is to choose the correct integration surface first. + +## Evidence Summary + +### Evidence that the current raw-ws-direct assumption is weak + +1. Real endpoint accepts connections but stays silent after the welcome/banner frame. +2. Silence occurs even for malformed frames, which suggests the endpoint is not acting like an openly documented RPC surface for arbitrary external clients. +3. The API documentation frames websocket use as a replacement for page-side JS invocation, not as a standalone public automation API. +4. The documentation repeatedly depends on `requesturl`, callback function names, target pages, and browser areas (`show`, `hide`, `agent`). +5. Historical frontend/product code uses `window.sgFunctionsUI(...)` and `window.BrowserAction(...)`, not raw external websocket business calls. +6. Historical architecture docs emphasize `FunctionsUI`, `CommandRouter`, and browser-host bridge seams. + +### Evidence that the current ws-native work is still useful + +1. The ws-native auth replacement removed a real bug. +2. The ws backend now correctly carries forward the last navigated request URL. +3. `WsBrowserBackend` and `ws_protocol` remain valuable as deterministic protocol tooling for fake-server tests and any future bootstrap validation. + +So the conclusion is **not** “delete ws-native work.” + +The conclusion is: + +- do not treat raw external websocket control as validated product architecture yet +- use the ws-native code only behind a decision gate + +## Design Decision + +Adopt a **decision-gated integration strategy**. + +### Decision Gate 1: Validate bootstrap viability first + +Before any more production architecture changes, add a focused, deterministic validation harness that can exercise a candidate raw-websocket bootstrap sequence against a live endpoint. + +The harness must support: + +- ordered frame scripts +- exact frame logging +- exact timeout/silence observation +- trying candidate setup sequences such as: + - `sgSetAuthInfo` + - `sgBrowserLogin` + - `sgOpenAgent` + - `sgBrowerserActiveTab` + - then a minimal action such as `sgBrowerserOpenPage` or `sgBrowserExcuteJsCodeByArea` +- trying the same action with different `requesturl` assumptions +- distinguishing these outcomes: + - numeric status returned + - callback returned + - welcome only, then silence + - close/reset + - protocol error + +This harness is not product code. It is an evidence tool that prevents blind implementation. + +### Decision Gate 2: Make bridge pivot the default fallback + +If the validation harness cannot demonstrate a reproducible bootstrap sequence that yields real status/callback frames from the live browser endpoint, then raw websocket must be considered **non-validated for external control**. + +At that point, the design must pivot to the bridge path: + +- sgClaw browser control targets the real browser-host integration surface +- use the bridge already evidenced in docs/code (`FunctionsUI`, browser host IPC, `BrowserAction`, `CommandRouter`) +- keep raw websocket support, if retained at all, as a diagnostic or highly constrained adapter rather than the primary product path + +## Architecture Options + +## Option A: Bootstrap-validated raw websocket path + +Choose this only if the live validation harness produces repeatable evidence. + +### Resulting architecture + +```text +sg_claw_client + -> sg_claw service + -> bootstrap sequence executor + -> WsBrowserBackend + -> browserWsUrl + -> sgBrowser +``` + +### Required conditions + +- a reproducible bootstrap sequence exists +- the sequence yields status/callback traffic for real business actions +- the sequence can be encoded as a narrow service-side precondition layer +- the sequence does not require unowned browser UI/manual setup outside a documented contract + +### Allowed production changes if Option A wins + +- add explicit bootstrap calls before first browser action +- persist validated session/context state needed by the real endpoint +- tighten `request_url` / target-page handling around the proven contract + +### Not allowed even if Option A wins + +- guessing bootstrap steps without evidence +- silently sprinkling many setup calls into random locations +- broadening the compat/runtime API before the bootstrap contract is known + +## Option B: Bridge-first integration path + +Choose this if live validation does not prove a workable raw websocket bootstrap. + +### Resulting architecture + +```text +sg_claw_client + -> sg_claw service + -> bridge adapter + -> browser host / FunctionsUI / BrowserAction / CommandRouter + -> sgBrowser page actions +``` + +### Required conditions + +- local docs/code show a stable supported bridge path +- raw websocket remains non-validated or only page-context-scoped +- the bridge surface can be wrapped behind the existing `BrowserBackend` abstraction or a sibling adapter without weakening pipe behavior + +### Allowed production changes if Option B wins + +- add a new browser backend implementation that targets the real bridge surface +- redirect ws service/browser execution away from raw business frames +- preserve ws-native code only for tests, probes, or intentionally constrained cases + +### Not allowed even if Option B wins + +- pretending the old raw-ws mainline still works “well enough” +- leaving the service path ambiguously split between two competing primary surfaces + +## Scope Guardrails for the Next Implementation Plan + +The next implementation plan must obey these guardrails: + +1. **One branch, one decision.** Do not implement both architecture options at once. +2. **Evidence before code.** If bootstrap is unproven, the next coding task is probe/validation tooling, not another speculative service/runtime refactor. +3. **Keep pipe untouched.** `src/lib.rs`, pipe handshake, and the pipe `BrowserPipeTool` path remain behaviorally unchanged. +4. **Do not delete ws-native code prematurely.** It still has value for protocol tests and validation tooling. +5. **Do not broaden success claims.** Removing `invalid hmac seed` did not make real browser control work. + +## Testing Strategy + +### Stage 1: Evidence tooling tests + +Add deterministic tests for the live-probe/validation harness so it can: + +- send an ordered frame script +- record exact received frames +- report silence/timeout precisely +- expose transcript output suitable for comparing candidate bootstrap sequences + +These tests use a fake websocket server, not sgBrowser. + +### Stage 2: Live validation runs + +Use the harness against the real endpoint with a fixed matrix of candidate sequences. + +At minimum, compare: + +1. no bootstrap -> minimal action +2. `sgOpenAgent` -> minimal action +3. `sgSetAuthInfo` -> minimal action +4. `sgBrowserLogin` -> minimal action +5. `sgBrowerserActiveTab` -> minimal action +6. combined documented bootstrap candidates -> minimal action +7. alternate `requesturl` values representing: + - `about:blank` + - target page URL + - a currently open page URL if known + +### Stage 3: Architecture-branch acceptance + +If Option A wins: + +- add one automated regression that proves the validated bootstrap sequence produces the first real status frame in a controlled integration test +- then continue with the narrowest production implementation plan + +If Option B wins: + +- write a new bridge-integration implementation plan before changing production code +- base all production tasks on the documented bridge surface + +## Acceptance Criteria for This Design Correction + +This design correction is successful only if future work follows these rules: + +1. The repository has an explicit design document recording that raw ws-native direct control is **not currently validated**. +2. The next engineering slice starts with validation or bridge selection, not another speculative runtime refactor. +3. Any future claim that raw websocket is the supported production path must be backed by a reproducible live bootstrap transcript. +4. If that evidence does not appear, the project pivots to the bridge path rather than continuing to guess. + +## Consequences + +### Positive + +- stops further speculative coding against an unproven surface +- preserves useful ws-native work without over-committing to it +- creates a clean decision point for the next implementation branch + +### Trade-off + +- this does not immediately unblock real browser control +- it intentionally inserts an evidence phase before more production changes + +That trade-off is acceptable because the current failure mode is architectural uncertainty, not a missing two-line fix. diff --git a/docs/superpowers/specs/2026-04-03-ws-browser-welcome-frame-design.md b/docs/superpowers/specs/2026-04-03-ws-browser-welcome-frame-design.md new file mode 100644 index 0000000..7261c2b --- /dev/null +++ b/docs/superpowers/specs/2026-04-03-ws-browser-welcome-frame-design.md @@ -0,0 +1,105 @@ +# WS Browser Welcome Frame Compatibility Design + +## Background + +Manual smoke verification after the ws-native browser backend auth replacement showed that real `sgBrowser` sends a banner text frame immediately after the websocket connection is established: + +- `Welcome! You are client #1` + +The current ws-native path treats the first received text frame as a protocol status frame. In `src/browser/ws_backend.rs`, `WsBrowserBackend::invoke(...)` reads one text frame and immediately parses it as an integer status code. That works for the existing deterministic tests, but it fails against the real browser because the first frame is a human-readable welcome banner rather than `0` or another numeric status. + +This means the auth replacement is working — the old `invalid hmac seed: session key must not be empty` error no longer appears — but real smoke still fails on protocol parsing. + +## Goal + +Make the ws service path tolerate exactly one initial welcome/banner text frame from the real browser websocket, without weakening the general ws protocol semantics. + +## Non-goals + +This change must not: + +- Relax parsing of arbitrary non-protocol text frames +- Change `WsBrowserBackend` into a browser-specific parser for banners +- Affect the legacy pipe path +- Add retry loops or broader reconnection logic +- Change callback handling semantics + +## Chosen approach + +Handle the welcome banner only in `ServiceBrowserWsClient`. + +### Why this layer + +`ServiceBrowserWsClient` is already the real-browser adapter used only by the ws service path in `src/service/server.rs`. The welcome frame is a quirk of the real browser endpoint rather than a property of the shared ws protocol abstraction. Keeping the compatibility behavior in the service-side client preserves the stricter semantics of `WsBrowserBackend` for all other callers and test doubles. + +## Behavioral rules + +1. Only the first received text frame after establishing a browser websocket connection may be treated as a welcome/banner candidate. +2. If that first text frame matches the real banner shape (currently observed as `Welcome! You are client #1`), the client discards it and continues waiting for the actual protocol frame. +3. The welcome skip is one-time only per connection, not per request. Because `ServiceBrowserWsClient` holds a persistent socket, this state must survive multiple `invoke(...)` calls on the same underlying websocket. +4. After the welcome skip: + - status frames must still be numeric strings + - callback frames must still match the existing JSON-array callback protocol + - any other malformed frame remains a protocol error +5. Timeout, close/reset, and connect-failure semantics remain unchanged. + +## Matching strategy + +Use a narrow string check in `ServiceBrowserWsClient` for a welcome/banner frame: + +- starts with `Welcome! You are client #` + +This is intentionally strict. We are adapting one known real-browser behavior, not introducing a generic “ignore garbage text” mode. + +## Tests + +### New red tests + +Add focused unit tests under `src/service/server.rs` tests: + +1. Positive case: + - fake websocket server sends: + 1. `Welcome! You are client #1` + 2. `0` + - then `WsBrowserBackend.invoke(Action::Navigate, ...)` succeeds + +2. Negative case: + - fake websocket server sends a different first text frame that does **not** match the known welcome prefix + - assert the call still fails as a protocol error rather than silently skipping the frame + +The positive test must fail before the implementation change and pass after it. The negative test guards the non-goal that we are not introducing a generic “ignore arbitrary text” mode. + +### Regression coverage + +Re-run: + +- `cargo test service::server::tests -- --nocapture` +- `cargo test --test browser_ws_backend_test -- --nocapture` +- `cargo test --test service_task_flow_test -- --nocapture` + +If those pass, re-run the earlier mixed ws+pipe sweep to confirm no unexpected regression escaped the targeted checks. + +## Risks and controls + +### Risk: swallowing a legitimate protocol error + +Control: +- only allow the one-time skip on the first received text frame +- only skip frames matching the known welcome prefix + +### Risk: broadening behavior beyond service ws path + +Control: +- keep the change entirely inside `ServiceBrowserWsClient` +- do not modify `WsBrowserBackend` parsing rules + +## Acceptance criteria + +The fix is complete only if all of the following are true: + +1. The positive welcome-banner test fails before the change and passes after it. +2. The negative malformed-first-frame test proves that non-matching first text frames still fail as protocol errors. +3. Real ws service smoke no longer fails with `invalid browser status frame: Welcome! You are client #1` when using the configured real sgBrowser endpoint. +4. Existing ws backend tests remain green. +5. Existing service task-flow regression remains green. +6. Pipe behavior remains unchanged, verified by the mixed ws+pipe regression suite. diff --git a/docs/superpowers/specs/2026-04-04-zhihu-ws-submit-realignment-design.md b/docs/superpowers/specs/2026-04-04-zhihu-ws-submit-realignment-design.md new file mode 100644 index 0000000..9434bb2 --- /dev/null +++ b/docs/superpowers/specs/2026-04-04-zhihu-ws-submit-realignment-design.md @@ -0,0 +1,182 @@ +# Zhihu WS Submit Realignment Design + +## Background + +The current Zhihu submit path drifted away from the documented browser websocket contract. + +The authoritative contract for this repository is `docs/_tmp_sgbrowser_ws_api_doc.txt`. + +For this slice, the spec anchors to these documented invariants only: + +- connect to `ws://127.0.0.1:12345` +- send `{"type":"register","role":"web"}` +- send browser actions as JSON arrays `[requesturl, action, ...args]` +- let browser results come back through documented callback semantics such as `callBackJsToCpp(...)` +- keep the current page URL as the request owner instead of inventing an external helper page + +The current production path does not follow that shape for Zhihu routes. + +Instead, the submit path selects `BrowserCallbackBackend`, which starts `LiveBrowserCallbackHost` and attempts to bootstrap a local helper page at `/sgclaw/browser-helper.html`. That helper-page bootstrap is not part of the user's confirmed production model, and live evidence already shows it is the wrong assumption for the Release browser. + +## Problem Statement + +Zhihu submit currently fails before real work begins because the service path depends on a helper-page callback host bootstrap that the Release browser does not use. + +That drift shows up in three ways: + +1. Zhihu submit routes select the callback-host backend instead of the direct websocket backend. +2. The mainline request URL becomes the local helper page URL instead of the real browser page URL. +3. The submit path waits for helper-page readiness rather than proceeding through the documented websocket callback model. + +This causes the observable failure: + +- `timeout while waiting for browser message` +- no real Zhihu page open/action in the browser + +## Goal + +Realign the Zhihu submit path to the documented websocket callback model without changing the existing pipe/service contract. + +Concretely, the target behavior is: + +- Zhihu submit routes use the websocket browser backend directly +- browser messages keep the real page URL as `requesturl` +- browser actions continue to use documented websocket opcodes +- callback-bearing results continue to use the documented callback payload model +- the browser no longer depends on opening a local helper page before Zhihu work starts + +## Non-goals + +This slice does not include: + +- changing `ClientMessage` or `ServiceMessage` +- changing `run_submit_task_with_browser_backend(...)` +- rewriting the Zhihu workflow itself +- adding a new browser bridge abstraction +- redesigning the pipe path +- deleting callback-host code that is outside the Zhihu submit mainline +- speculative protocol expansion beyond the documented websocket contract + +## Chosen Approach + +Choose **Option A**: withdraw Zhihu submit from the helper-page callback-host path and return it to the documented websocket callback model. + +Rejected alternatives: + +- Keep callback host but remove helper bootstrap: still preserves the wrong abstraction in the mainline. +- Build a new orchestration layer: exceeds the requested scope. + +## Mainline Architecture After Realignment + +```text +sg_claw_client + -> sg_claw service / runtime submit path + -> existing BrowserBackend seam + -> WsBrowserBackend + -> ws://127.0.0.1:12345 + -> documented browser opcodes and callback semantics +``` + +For Zhihu submit routes, the callback-host helper page is no longer part of the mainline execution chain. + +## Required Production Changes + +### 1. Route selection + +Update submit-route backend selection so these routes no longer instantiate `BrowserCallbackBackend`: + +- `WorkflowRoute::ZhihuHotlistExportXlsx` +- `WorkflowRoute::ZhihuHotlistScreen` +- `WorkflowRoute::ZhihuArticleEntry` +- `WorkflowRoute::ZhihuArticleDraft` +- `WorkflowRoute::ZhihuArticlePublish` + +The change applies in both: + +- service submit path in `src/service/server.rs` +- direct runtime submit path in `src/agent/mod.rs` + +Direct runtime fallback behavior stays unchanged when no browser websocket URL is configured: + +- if a real browser websocket URL is configured, use `WsBrowserBackend` for the listed Zhihu routes +- if no browser websocket URL is configured, keep the existing pipe fallback instead of failing fast + +### 2. Request URL ownership + +Keep `requesturl` aligned with the real browser page instead of the helper page. + +Expected behavior: + +- initial request URL comes from the existing submit-path request context +- after a successful navigate call, the websocket backend continues to update its request URL to the navigated target page +- later `getText` and `eval` calls run against the real Zhihu page URL + +This preserves the documented page-owned websocket model. + +### 3. Callback semantics + +Keep callback-bearing actions on the existing websocket protocol path, using the documented callback payload shape. + +Required invariants: + +- action frames remain `[requesturl, action, ...args]` +- navigate uses the documented opcode `sgHideBrowserCallAfterLoaded` +- `getText` and `eval` continue to emit `callBackJsToCpp(...)` payloads in the documented `sourceUrl@_@targetUrl@_@callback@_@actionUrl@_@responseTxt` form +- callback decoding remains on the websocket path instead of moving through localhost helper-page HTTP endpoints + +### 4. Callback-host removal from the Zhihu mainline + +For this slice, callback-host code is removed from the Zhihu submit mainline, not redesigned. + +Practical meaning: + +- Zhihu submit must not start `LiveBrowserCallbackHost` +- Zhihu submit must not emit `sgBrowerserOpenPage` for `/sgclaw/browser-helper.html` +- Zhihu submit must not block on `/sgclaw/callback/ready` + +Code outside the Zhihu submit mainline can remain unchanged unless tests require cleanup. + +## Test Strategy + +This slice follows TDD and replaces the stale helper-page assumptions with direct websocket submit-path assertions. + +### Red tests to add or rewrite + +1. Rewrite the current submit regression that asserts helper-page bootstrap. + - old behavior under test: Zhihu submit bootstraps callback host + - new behavior under test: Zhihu submit does **not** bootstrap callback host and does **not** emit helper-page frames + +2. Add or update a focused submit-path regression proving request ownership stays on the real page. + - after navigate, subsequent Zhihu browser actions must use the real target page URL rather than `/sgclaw/browser-helper.html` + +3. Remove or rewrite any newly added red test whose only purpose was to preserve callback-host-without-helper behavior. + - that test belongs to the rejected Option B path, not the chosen Option A path + +### Green verification + +After the minimal code change, run focused verification in this order: + +1. `agent_runtime_test` coverage for the submit path +2. relevant Zhihu `compat_runtime_test` coverage +3. submit/service websocket regressions impacted by route selection +4. stronger real-browser validation after focused tests pass + +## Scope Guardrails + +The implementation plan for this spec must obey all of the following: + +1. Do not modify the pipe contract. +2. Do not add a new browser abstraction. +3. Do not broaden the change beyond the Zhihu submit path and its directly affected websocket protocol tests. +4. Do not keep the helper-page path as a second competing Zhihu mainline. +5. If live validation still reveals a callback-payload mismatch, only adjust the websocket protocol encoding/decoding at the exact mismatch point. + +## Acceptance Criteria + +The slice is complete when all of the following are true: + +1. Zhihu submit routes no longer select the helper-page callback-host backend. +2. No Zhihu submit regression expects or observes `/sgclaw/browser-helper.html` bootstrap. +3. The websocket backend sends Zhihu follow-up actions with the real page URL as `requesturl`. +4. Focused automated tests covering the changed submit path pass. +5. Real-browser validation no longer fails at callback-host readiness timeout, emits no helper-page bootstrap frames, and emits at least one real-page follow-up browser action after navigate. diff --git a/docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md b/docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md new file mode 100644 index 0000000..9d00904 --- /dev/null +++ b/docs/superpowers/specs/2026-04-06-service-chat-web-console-design.md @@ -0,0 +1,219 @@ +# Service Chat Web Console Design + +## Background + +The current natural-language entrypoint is the terminal client in `src/bin/sg_claw_client.rs`. +That client already talks to the existing service websocket, sends `ClientMessage`, and prints +`ServiceMessage` responses. + +The repository also contains a separate browser callback helper at +`http://127.0.0.1:61058/sgclaw/browser-helper.html`. That page is part of the browser backend +execution path and must remain untouched. + +For this slice, the authoritative boundary is: + +- the new page may talk to the existing service websocket only +- the page must not talk to the browser websocket directly +- the page must not reuse or replace `browser-helper.html` +- the page must not change the service protocol or browser execution logic + +## Problem Statement + +Running `cargo run --bin sg_claw_client` and typing into stdin works, but it is inconvenient for +routine usage. The user wants a simple local HTML page with a websocket connection field, a natural- +language input box, and a send button. + +The risk is scope drift: if the new page reaches into the browser-helper flow or changes backend +logic, it could damage the working Zhihu/browser path. + +## Goal + +Add a standalone local HTML console that connects to the existing service websocket and submits +natural-language tasks using the current `submit_task` message shape. + +The page should be usable without changing `sg_claw`, `sg_claw_client`, `browser-helper.html`, or +any existing service/browser runtime behavior. + +## Non-goals + +This slice does not include: + +- serving the page from the Rust service +- changing `ClientMessage` or `ServiceMessage` +- changing `src/service/server.rs` +- changing `src/browser/callback_host.rs` +- changing `src/browser/callback_backend.rs` +- changing the helper-page bootstrap flow +- adding authentication, persistence, or multi-session orchestration +- replacing the terminal client + +## Chosen Approach + +Choose Option A: add one standalone HTML file that opens in a normal browser and talks to the +existing service websocket at `ws://127.0.0.1:42321` by default. + +Why this option: + +- it is the narrowest possible change +- it reuses the already-working service protocol +- it does not alter the browser-helper path +- it keeps all runtime ownership in the existing Rust service + +Rejected alternatives: + +- extend `browser-helper.html` into a chat UI: wrong boundary; that page belongs to browser + callback orchestration, not user task entry +- add a new HTTP server inside `sg_claw`: unnecessary for the requested scope +- replace the terminal client binary: not required; both clients can coexist + +## File Placement + +Create the page outside `frontend/runtime-host/`. + +Chosen location: + +- `frontend/service-console/sg_claw_service_console.html` + +Reason: + +- `frontend/runtime-host/` is reserved for SuperRPA runtime-host bundles +- the new page is a standalone local tool, not a Chromium-hosted bundle +- keeping it in its own directory makes the isolation explicit + +## Page Architecture + +The page is a single self-contained HTML file with inline CSS and inline JavaScript. +No build step and no frontend framework are required. + +The page has three UI regions: + +1. Connection bar + - websocket URL input + - connect/disconnect button + - current connection state label + +2. Message stream + - appends service logs in arrival order + - distinguishes connection info, task logs, errors, and final completion + - keeps the current session visible until the page is refreshed + +3. Task composer + - one textarea for natural-language input + - one send button + - send disabled while the websocket is disconnected + - while a task is in flight, keep the composer enabled and let repeated submits surface the + existing service-side `busy` response rather than adding a new frontend queue + +## Protocol Contract + +The page must reuse the existing service protocol exactly. + +### Outbound message + +When the user clicks send, the page sends: + +```json +{ + "type": "submit_task", + "instruction": "", + "conversation_id": "", + "messages": [], + "page_url": "", + "page_title": "" +} +``` + +This matches the current terminal client shape in `src/bin/sg_claw_client.rs`. + +### Inbound messages + +The page displays these existing `ServiceMessage` variants: + +- `status_changed` -> render as a compact connection/runtime status row +- `log_entry` -> append as a chronological task log row +- `task_complete` -> append as the terminal result row for that submission +- `busy` -> append as a visible refusal/error row without automatic retry + +No new message type is introduced. + +## Interaction Flow + +1. User opens the local HTML file with a normal browser, typically via `file://`. +2. User connects to the service websocket. +3. The page shows websocket connection status locally. +4. User enters a natural-language instruction and clicks send. +5. The page sends one `submit_task` payload over the service websocket. +6. The service continues to execute tasks exactly as it already does. +7. Incoming service messages are appended to the message stream. +8. After `task_complete`, the websocket remains open so the user can send another task. + +## Error Handling + +The page handles only UI-local failures: + +- websocket connect failure -> show connection error and keep send disabled +- websocket disconnect mid-session -> mark disconnected and require reconnect +- empty instruction -> block send and show inline validation +- `busy` response -> show as a visible service-side refusal without retry logic + +The page does not add retries, protocol fallbacks, or browser-runtime recovery logic. + +## Isolation From `browser-helper.html` + +This is the critical constraint. + +The new page must never: + +- reference `/sgclaw/browser-helper.html` +- reference `/sgclaw/callback/ready` +- reference `/sgclaw/callback/events` +- reference `/sgclaw/callback/commands/next` +- reference `/sgclaw/callback/commands/ack` +- connect to `ws://127.0.0.1:12345` + +The only network target owned by the page is the service websocket, defaulting to +`ws://127.0.0.1:42321`. + +Because of that boundary, the page does not interfere with the helper-page bootstrap path. + +## Test Strategy + +This slice stays minimal, so the automated guard is also minimal. + +### Automated regression + +Add one focused integration test in `tests/service_console_html_test.rs` that reads the standalone +HTML source and asserts: + +- the file exists at the agreed path and is resolved from `CARGO_MANIFEST_DIR` so the test is + stable across working directories +- it contains the service websocket default URL +- it contains `submit_task` payload construction +- it does not contain helper-page URLs or callback-host endpoints +- it does not contain the browser websocket URL + +This test is a scope guard, not a browser-E2E suite. + +### Manual smoke verification + +With the existing service binary running: + +1. open the HTML file in a browser +2. connect to the service websocket +3. confirm local websocket open/close events and service `status_changed` messages both appear in the message stream +4. submit a natural-language task +5. confirm logs and completion render in the page +6. confirm the helper-page path remains unchanged because the page never references it + +## Acceptance Criteria + +The slice is complete when all of the following are true: + +1. `frontend/service-console/sg_claw_service_console.html` exists. +2. The page connects to the existing service websocket without backend changes. +3. The page sends the existing `submit_task` shape and receives existing `ServiceMessage` events. +4. The page does not reference `browser-helper.html`, callback-host endpoints, or the browser + websocket URL. +5. Existing browser-helper logic remains untouched. +6. The automated source guard passes. +7. Manual smoke verification confirms a task can be submitted from the HTML page. diff --git a/docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md b/docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md new file mode 100644 index 0000000..4c2f455 --- /dev/null +++ b/docs/superpowers/specs/2026-04-06-zhihu-hotlist-post-export-auto-open-design.md @@ -0,0 +1,373 @@ +# Zhihu Hotlist Post-Export Auto-Open Design + +## Background + +The current Zhihu hotlist workflows already support two separate artifact outputs: + +- `openxml_office` generates a local `.xlsx` file for hotlist export +- `screen_html_export` generates a local `.html` dashboard for presentation + +Today, the workflow stops after artifact generation and returns a summary string such as: + +- `已导出知乎热榜 Excel ` +- `已生成知乎热榜大屏 ` + +That means the user still has to manually open the generated file. + +The user wants one additional post-export action, but only one at a time: + +1. for Excel-oriented tasks, automatically open the generated `.xlsx` with the system default spreadsheet application +2. for dashboard-oriented tasks, automatically open the generated local dashboard HTML inside the running sgBrowser session + +This is an exclusive choice, not a combined mode. + +## Current Runtime Facts + +The implementation must match the current browser/runtime boundary that already exists in the repo: + +- the active service submit path in `src/service/server.rs` constructs `BrowserCallbackBackend` +- `BrowserCallbackBackend::invoke(Action::Navigate, ...)` currently emits `sgBrowerserOpenPage`, which opens a new visible browser tab and keeps the helper page alive +- `WsBrowserBackend::invoke(Action::Navigate, ...)` has different semantics and a different transport path from the callback-host service path +- `MacPolicy::validate(...)` currently rejects empty or non-domain values, so a raw `file://...` navigation cannot pass through the normal domain validation path today +- `screen_html_export` already returns `presentation.url`, which is the existing `file://` presentation URL contract for the generated dashboard + +Those facts mean the design must not promise "replace the helper page" or "reuse identical tab behavior across all backends". The required success path for this slice is narrower: open the generated dashboard automatically in the current callback-host-backed sgBrowser service session without adding a new user-facing surface. + +## Problem Statement + +The existing workflow logic in `src/compat/workflow_executor.rs` already separates hotlist export from dashboard generation, but it treats both routes as artifact-only flows. The last mile is missing: + +- the Excel route does not auto-open the generated file +- the dashboard route does not consume the generated dashboard presentation URL and open it automatically in the browser runtime + +The risk is scope drift. This change must not: + +- turn Excel-open and dashboard-open into a combined workflow +- add new help/help-like user-visible surfaces +- move orchestration into `frontend/service-console/` +- modify the websocket protocol +- modify `browser-helper.html` +- modify callback-host HTTP endpoints or their contracts +- change the artifact-generation contract of `openxml_office` or `screen_html_export` + +## Goal + +Extend the existing Zhihu hotlist post-export behavior so that: + +- Excel tasks generate `.xlsx` and then auto-open it with the local system default spreadsheet application +- dashboard tasks generate `.html` and then auto-open that generated dashboard inside sgBrowser + +On the current callback-host service path, "inside sgBrowser" means opening the generated dashboard in a new visible browser tab while the helper page stays alive. The user does not need to open the file manually. + +## Non-goals + +This slice does not include: + +- opening Excel and dashboard in the same run +- adding a new combined route that auto-opens both artifacts +- adding any new help, helper, or user-visible assistance surface +- modifying `frontend/service-console/sg_claw_service_console.html` +- modifying `src/service/protocol.rs` +- modifying `browser-helper.html` +- modifying `/sgclaw/callback/*` contracts +- turning the browser backend into a general-purpose local filesystem browser +- changing the artifact-generation JSON contract of `openxml_office` or `screen_html_export` + +## Chosen Approach + +Keep the current two workflow routes, but add one route-specific post-export action to each: + +- `ZhihuHotlistExportXlsx` -> generate `.xlsx`, then open it locally with the OS default app +- `ZhihuHotlistScreen` -> generate `.html`, then open the generated dashboard presentation URL in the browser runtime + +For the dashboard route, use the existing `presentation.url` returned by `screen_html_export` as the authoritative browser-open URL. Do not invent a separate normal-path URL conversion layer when the tool already returns the presentation contract. + +The compat opener must emit one exact navigate request shape for this case. + +- `action`: `Action::Navigate` +- `expected_domain`: the exact literal `__sgclaw_local_dashboard__` +- `params.url`: the exact `presentation.url` returned by `screen_html_export` +- `params.sgclaw_local_dashboard_open.source`: the exact literal `compat.workflow_executor` +- `params.sgclaw_local_dashboard_open.kind`: the exact literal `zhihu_hotlist_screen` +- `params.sgclaw_local_dashboard_open.output_path`: the generated local dashboard artifact path +- `params.sgclaw_local_dashboard_open.presentation_url`: the same `file://` URL stored in `params.url` + +On the current callback-host-backed service path, only that exact request shape is approved for the local-dashboard special case. A plain `Action::Navigate` with an arbitrary `file://...` URL, or a request missing any one of the required marker fields above, must continue to be rejected. + +Because normal `MacPolicy` domain validation cannot accept `file://...`, add a narrow local-dashboard presentation allowance in the browser backend/security boundary. That allowance must be limited to this one case: + +- only for `Action::Navigate` +- only for generated local dashboard presentation URLs +- only for local HTML presentation, not arbitrary local paths or generic file browsing + +Why this approach: + +- it preserves the existing mutual exclusivity between Excel export and dashboard presentation +- it keeps artifact generation in the existing tools +- it keeps browser opening inside the existing browser backend boundary +- it uses the existing `screen_html_export` presentation contract instead of duplicating it +- it avoids pushing orchestration into the service console or protocol layer +- it stays compatible with the current callback-host runtime, where visible navigation is new-tab based +- it limits the guaranteed browser-open behavior in this slice to the callback-host-backed service path that the user is using today + +Rejected alternatives: + +- add a combined "Excel + dashboard" route: explicitly rejected by user behavior +- let `frontend/service-console/` decide when to open generated files: wrong layer; the console is only a submit/view surface +- add help UI to expose output choices: explicitly unwanted by the user +- change `browser-helper.html` so the helper page itself becomes the dashboard: this would break the current helper-page persistence model +- promise a backend-agnostic "replace the current page" behavior: inaccurate because callback-host and websocket backends do not share identical navigate semantics +- require the websocket backend to gain matching local-dashboard visible-open behavior in this slice: outside the narrow current-service-path goal + +## File Responsibilities + +### `src/compat/workflow_executor.rs` + +Continue to own: + +- route detection for Zhihu hotlist workflows +- artifact generation orchestration +- post-export summary construction + +New responsibilities in this slice: + +- parse the successful artifact payloads after `openxml_office` and `screen_html_export` +- call the route-specific post-export opener only after artifact creation succeeds +- for the dashboard route, consume `presentation.url` from the `screen_html_export` result payload +- keep generation success and post-export open success/failure distinct in the returned summary + +### `src/compat/artifact_open.rs` + +New helper module to keep side effects out of `workflow_executor.rs`. + +Responsibilities: + +- open a generated local `.xlsx` with the system default application +- open a generated local dashboard presentation URL through the existing `BrowserBackend` +- construct the exact approved dashboard navigate request shape used by this slice +- define the narrow local-dashboard presentation token/constants used by the compat layer and backend compatibility path +- return narrow success/failure results so `workflow_executor.rs` can produce accurate summaries + +This module must stay small and focused. It is not a general launcher framework. + +### `src/browser/callback_backend.rs` + +New narrow responsibility in this slice: + +- at the `BrowserCallbackBackend::invoke(Action::Navigate, params, expected_domain)` entrypoint, recognize only the exact approved local-dashboard presentation request shape +- preserve the current callback-host behavior of using `sgBrowerserOpenPage`, which opens a new visible tab and keeps the helper page alive +- reject local-file navigate attempts that do not include the exact post-export marker payload from the compat layer + +This slice must not change callback-host polling, helper bootstrap, or callback endpoint behavior. + +### `src/browser/ws_backend.rs` + +No required behavior change in this slice. + +Notes: + +- websocket transport semantics differ from the callback-host service path +- this spec does not require websocket backend local-dashboard visible-open support +- websocket-specific parity can be designed later as a separate slice if needed + +### `src/security/mac_policy.rs` + +New narrow responsibility in this slice: + +- expose a small validation helper for the approved local-dashboard presentation case +- validate the real local presentation URL and artifact path for that case rather than treating `file://` as a normal allowed domain +- keep the normal domain-based validation path unchanged for ordinary remote navigation + +The policy layer must not turn `file://` into a generally allowed "domain". This is an explicit special case for generated local dashboard presentation only. + +### `src/compat/mod.rs` + +Expose the new helper module. + +## Route Semantics + +### Excel export route + +Trigger examples: + +- `读取知乎热榜数据,并导出 excel 文件` +- `导出知乎热榜 xlsx` + +Expected behavior: + +1. collect hotlist rows +2. call `openxml_office` +3. obtain `output_path` +4. open the generated `.xlsx` using the local OS default spreadsheet application +5. return a success summary reflecting both generation and open state + +Summary rules: + +- open succeeded -> `已导出并打开知乎热榜 Excel ` +- open failed but file exists -> `已导出知乎热榜 Excel ,但自动打开失败:` + +The workflow still counts artifact generation as successful even if the post-export open step fails. + +### Dashboard route + +Trigger examples: + +- `读取知乎热榜数据并生成领导演示大屏` +- `生成知乎热榜 dashboard` +- `展示知乎热榜大屏` + +Expected behavior: + +1. collect hotlist rows +2. call `screen_html_export` +3. obtain `output_path` +4. obtain `presentation.url` from the tool result payload +5. invoke the browser opener through the existing `BrowserBackend` +6. return a success summary reflecting both generation and browser-open state + +Summary rules: + +- browser open succeeded -> `已在浏览器中打开知乎热榜大屏 ` +- browser open failed but file exists -> `已生成知乎热榜大屏 ,但浏览器自动打开失败:` + +The workflow still counts artifact generation as successful even if the browser-open step fails. + +## Browser Boundary + +This slice must preserve the current browser/runtime boundary. + +Allowed: + +- use the existing `BrowserBackend` +- use the existing `Action::Navigate` +- use the existing `screen_html_export` `presentation.url` +- add a narrow compatibility path so local generated dashboard presentation can pass backend validation + +Not allowed: + +- change `browser-helper.html` +- introduce a new callback-host endpoint +- move file-opening responsibility into the frontend service console +- add a new browser-side bootstrap flow +- require websocket protocol changes + +Important semantic note: + +- on the current service callback-host path, dashboard open is expected to use `sgBrowerserOpenPage`, so the generated dashboard appears in a new visible browser tab while the helper page remains available for later tasks +- websocket-backed browser execution may continue to differ; this slice does not require matching visible-open semantics there + +## Local Dashboard Presentation Allowance + +The local dashboard browser-open path needs an explicit narrow validation rule because `file://...` cannot pass the normal domain allowlist. + +Requirements for the narrow allowance: + +- only approved for `Action::Navigate` +- only approved for the exact compat marker payload described above +- only approved for generated local dashboard presentation URLs +- only approved when the validated local artifact path points to the generated dashboard HTML artifact returned by the same `screen_html_export` success payload +- only approved for local HTML presentation, not arbitrary executables or unrelated local files +- ordinary remote navigation must continue using the existing `MacPolicy::validate(...)` domain rules unchanged + +This keeps the behavior small and auditable while still satisfying the user-visible dashboard auto-open requirement. + +## Local File Opening Boundary + +The Excel auto-open action is a local runtime side effect, not a browser action. + +Requirements: + +- use the system default application for `.xlsx` +- support the current Windows environment first +- keep the implementation minimal and focused on the generated artifact path + +Not required in this slice: + +- a cross-platform abstraction beyond the minimal shape needed for the current repo environment +- opening arbitrary user-selected files +- exposing local file opening to the service websocket protocol + +## Error Handling + +### Excel route + +If `.xlsx` generation fails: + +- return the existing export failure + +If `.xlsx` generation succeeds but auto-open fails: + +- keep the artifact path in the summary +- mark only the auto-open step as failed +- do not delete the generated file + +### Dashboard route + +If `.html` generation fails: + +- return the existing screen export failure + +If `.html` generation succeeds but browser open fails: + +- keep the artifact path in the summary +- mark only the browser-open step as failed +- do not delete the generated file + +If the tool result is missing `presentation.url`: + +- treat that as a protocol error in the post-export open step for this route +- keep the generated artifact path in the summary if it is available +- do not silently invent a different contract in the normal path + +## Test Strategy + +### Workflow tests + +Update or add focused workflow coverage so that: + +- Excel workflow still calls `openxml_office` +- dashboard workflow still calls `screen_html_export` +- the two routes remain mutually exclusive +- dashboard workflow consumes the tool's existing `presentation.url` + +### New Excel post-export test + +Add a focused regression proving: + +- an Excel-oriented hotlist request triggers export +- the generated `.xlsx` path is passed into the local default-app opener +- no browser dashboard navigate is triggered for that route + +### New dashboard post-export test + +Add a focused regression proving: + +- a dashboard-oriented hotlist request triggers HTML generation +- the generated tool payload `presentation.url` is used for browser open +- the browser backend receives a local-dashboard navigate request through the approved compat path +- no local spreadsheet opener is triggered for that route + +### Backend/security compatibility tests + +Add focused regressions proving: + +- callback backend accepts the approved local-dashboard navigate case and still emits `sgBrowerserOpenPage` +- the narrow local-dashboard allowance rejects non-local or malformed URLs +- ordinary domain validation behavior remains unchanged for normal remote navigation + +### Existing boundary tests remain unchanged + +Do not change the service-console boundary guard. This slice is runtime behavior only. + +## Acceptance Criteria + +The slice is complete when all of the following are true: + +1. Excel hotlist export still generates a local `.xlsx` artifact. +2. Excel hotlist export auto-opens that `.xlsx` with the system default spreadsheet application. +3. Dashboard hotlist export still generates a local `.html` artifact. +4. Dashboard hotlist export consumes the existing `screen_html_export` `presentation.url` and auto-opens it in the current callback-host-backed sgBrowser service session. +5. On the current callback-host service path, the dashboard opens automatically in a visible browser tab without breaking the helper-page runtime. +6. Excel-open and dashboard-open remain separate user-chosen flows, not a combined mode. +7. No new help/help-like user-visible surface is added. +8. The service console, websocket protocol, `browser-helper.html`, and callback-host endpoint surface remain untouched. diff --git a/skills-lock.json b/skills-lock.json new file mode 100644 index 0000000..ce93f52 --- /dev/null +++ b/skills-lock.json @@ -0,0 +1,10 @@ +{ + "version": 1, + "skills": { + "ui-ux-pro-max": { + "source": "nextlevelbuilder/ui-ux-pro-max-skill", + "sourceType": "github", + "computedHash": "6337038fe1fe6bbe1b9f252ab678ee575859190bab6f0f246f4061824eb40875" + } + } +} diff --git a/src/agent/mod.rs b/src/agent/mod.rs index 0a0defe..7bc1c98 100644 --- a/src/agent/mod.rs +++ b/src/agent/mod.rs @@ -1,45 +1,16 @@ -pub mod planner; -pub mod runtime; pub mod task_runner; use std::sync::Arc; use crate::browser::ws_backend::WsBrowserBackend; use crate::browser::{BrowserBackend, PipeBrowserBackend}; -use crate::pipe::{AgentMessage, BrowserMessage, BrowserPipeTool, PipeError, Transport}; +use crate::pipe::{BrowserMessage, BrowserPipeTool, PipeError, Transport}; pub use task_runner::{ run_submit_task, run_submit_task_with_browser_backend, AgentEventSink, AgentRuntimeContext, SubmitTaskRequest, }; -fn execute_plan( - transport: &T, - browser_tool: &BrowserPipeTool, - plan: &planner::TaskPlan, -) -> Result { - for step in &plan.steps { - transport.send(&AgentMessage::LogEntry { - level: "info".to_string(), - message: step.log_message.clone(), - })?; - - let result = browser_tool.invoke( - step.action.clone(), - step.params.clone(), - &step.expected_domain, - )?; - if !result.success { - return Err(PipeError::Protocol(format!( - "browser action failed: {}", - result.data - ))); - } - } - - Ok(plan.summary.clone()) -} - fn normalize_optional_submit_field(value: String) -> Option { let trimmed = value.trim(); (!trimmed.is_empty()).then(|| trimmed.to_string()) @@ -82,21 +53,11 @@ fn configured_browser_ws_url(context: &AgentRuntimeContext) -> Option { } fn send_status_changed(transport: &T, state: &str) -> Result<(), PipeError> { - transport.send(&AgentMessage::StatusChanged { + transport.send(&crate::pipe::AgentMessage::StatusChanged { state: state.to_string(), }) } -pub fn execute_task( - transport: &T, - browser_tool: &BrowserPipeTool, - instruction: &str, -) -> Result { - let plan = planner::plan_instruction(instruction) - .map_err(|err| PipeError::Protocol(err.to_string()))?; - execute_plan(transport, browser_tool, &plan) -} - pub fn handle_browser_message( transport: &T, browser_tool: &BrowserPipeTool, @@ -151,6 +112,8 @@ pub fn handle_browser_message_with_context( #[cfg(test)] mod tests { use super::normalize_optional_submit_field; + use std::fs; + use std::path::PathBuf; #[test] fn normalize_optional_submit_field_trims_and_drops_blank_values() { @@ -160,4 +123,21 @@ mod tests { Some("https://example.com/page".to_string()) ); } + + #[test] + fn agent_module_cleanup_removes_legacy_runtime_and_planner_sources() { + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let agent_module = fs::read_to_string(manifest_dir.join("src/agent/mod.rs")).unwrap(); + let top_lines = agent_module + .lines() + .take(10) + .map(str::trim) + .collect::>(); + + assert!(!manifest_dir.join("src/agent/runtime.rs").exists()); + assert!(!manifest_dir.join("src/agent/planner.rs").exists()); + assert!(!top_lines.iter().any(|line| *line == "pub mod runtime;")); + assert!(!top_lines.iter().any(|line| *line == "pub mod planner;")); + assert!(top_lines.iter().any(|line| *line == "pub mod task_runner;")); + } } diff --git a/src/agent/planner.rs b/src/agent/planner.rs deleted file mode 100644 index e38b80c..0000000 --- a/src/agent/planner.rs +++ /dev/null @@ -1,216 +0,0 @@ -use reqwest::Url; -use serde_json::{json, Value}; -use thiserror::Error; - -use crate::config::PlannerMode; -use crate::pipe::Action; - -/// Legacy deterministic planner kept for dev-only verification and fixture coverage. -/// Production browser submit flow no longer routes into this planner. -pub const LEGACY_DEV_ONLY: bool = true; - -const BAIDU_URL: &str = "https://www.baidu.com"; -const BAIDU_DOMAIN: &str = "www.baidu.com"; -const BAIDU_INPUT_SELECTOR: &str = "#kw"; -const BAIDU_SEARCH_BUTTON_SELECTOR: &str = "#su"; -const ZHIHU_HOME_URL: &str = "https://www.zhihu.com"; -const ZHIHU_SEARCH_URL: &str = "https://www.zhihu.com/search"; -const ZHIHU_DOMAIN: &str = "www.zhihu.com"; - -#[derive(Debug, Clone, PartialEq)] -pub struct PlannedStep { - pub action: Action, - pub params: Value, - pub expected_domain: String, - pub log_message: String, -} - -#[derive(Debug, Clone, PartialEq)] -pub struct TaskPlan { - pub summary: String, - pub steps: Vec, -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct ExecutionPreview { - pub summary: String, - pub steps: Vec, -} - -#[derive(Debug, Error, Clone, PartialEq, Eq)] -pub enum PlannerError { - #[error("unsupported instruction: {0}")] - UnsupportedInstruction(String), - #[error("missing search query in instruction")] - MissingQuery, -} - -pub fn plan_instruction(instruction: &str) -> Result { - let trimmed = instruction.trim(); - if matches_exact(trimmed, &["打开百度"]) { - return Ok(plan_homepage("已打开百度首页", BAIDU_URL, BAIDU_DOMAIN)); - } - - if let Some(query) = extract_query(trimmed, &["打开百度搜索", "打开百度并搜索"])? { - return Ok(plan_baidu_search(query)); - } - - if matches_exact(trimmed, &["打开知乎"]) { - return Ok(plan_homepage( - "已打开知乎首页", - ZHIHU_HOME_URL, - ZHIHU_DOMAIN, - )); - } - - if let Some(query) = extract_query(trimmed, &["打开知乎搜索", "打开知乎并搜索"])? { - return Ok(plan_zhihu_search(query)); - } - - Err(PlannerError::UnsupportedInstruction(trimmed.to_string())) -} - -pub fn build_execution_preview( - mode: PlannerMode, - instruction: &str, - page_url: Option<&str>, - page_title: Option<&str>, -) -> Option { - if matches!(mode, PlannerMode::LegacyDeterministic) { - return None; - } - - let trimmed = instruction.trim(); - if crate::runtime::is_zhihu_hotlist_task(trimmed, page_url, page_title) { - return Some(build_zhihu_hotlist_preview(trimmed)); - } - - if let Ok(plan) = plan_instruction(trimmed) { - return Some(ExecutionPreview { - summary: format!("先规划再执行:{}", plan.summary), - steps: plan - .steps - .into_iter() - .map(|step| step.log_message) - .collect(), - }); - } - - Some(ExecutionPreview { - summary: "先规划再执行当前任务".to_string(), - steps: vec![ - "inspect current browser context".to_string(), - "choose the required sgclaw runtime tools".to_string(), - "execute and return the concrete result".to_string(), - ], - }) -} - -fn extract_query<'a>( - instruction: &'a str, - prefixes: &[&str], -) -> Result, PlannerError> { - let Some(query) = prefixes - .iter() - .find_map(|prefix| instruction.strip_prefix(prefix)) - else { - return Ok(None); - }; - - let query = query.trim(); - if query.is_empty() { - return Err(PlannerError::MissingQuery); - } - - Ok(Some(query)) -} - -fn matches_exact(instruction: &str, candidates: &[&str]) -> bool { - candidates.iter().any(|candidate| instruction == *candidate) -} - -fn plan_homepage(summary: &str, url: &str, domain: &str) -> TaskPlan { - TaskPlan { - summary: summary.to_string(), - steps: vec![PlannedStep { - action: Action::Navigate, - params: json!({ "url": url }), - expected_domain: domain.to_string(), - log_message: format!("navigate {url}"), - }], - } -} - -fn plan_baidu_search(query: &str) -> TaskPlan { - TaskPlan { - summary: format!("已在百度搜索{query}"), - steps: vec![ - PlannedStep { - action: Action::Navigate, - params: json!({ "url": BAIDU_URL }), - expected_domain: BAIDU_DOMAIN.to_string(), - log_message: "navigate https://www.baidu.com".to_string(), - }, - PlannedStep { - action: Action::Type, - params: json!({ - "selector": BAIDU_INPUT_SELECTOR, - "text": query, - "clear_first": true - }), - expected_domain: BAIDU_DOMAIN.to_string(), - log_message: format!("type {query} into {BAIDU_INPUT_SELECTOR}"), - }, - PlannedStep { - action: Action::Click, - params: json!({ "selector": BAIDU_SEARCH_BUTTON_SELECTOR }), - expected_domain: BAIDU_DOMAIN.to_string(), - log_message: format!("click {BAIDU_SEARCH_BUTTON_SELECTOR}"), - }, - ], - } -} - -fn plan_zhihu_search(query: &str) -> TaskPlan { - let url = Url::parse_with_params(ZHIHU_SEARCH_URL, &[("type", "content"), ("q", query)]) - .expect("valid Zhihu search URL"); - let url: String = url.into(); - - TaskPlan { - summary: format!("已在知乎搜索{query}"), - steps: vec![PlannedStep { - action: Action::Navigate, - params: json!({ "url": url }), - expected_domain: ZHIHU_DOMAIN.to_string(), - log_message: format!("navigate {url}"), - }], - } -} - -fn build_zhihu_hotlist_preview(instruction: &str) -> ExecutionPreview { - let normalized = instruction.to_ascii_lowercase(); - if normalized.contains("dashboard") - || instruction.contains("大屏") - || instruction.contains("新标签页") - { - return ExecutionPreview { - summary: "先规划再执行知乎热榜大屏生成".to_string(), - steps: vec![ - "navigate https://www.zhihu.com/hot".to_string(), - "getText main".to_string(), - "call screen_html_export".to_string(), - "return generated local .html path".to_string(), - ], - }; - } - - ExecutionPreview { - summary: "先规划再执行知乎热榜 Excel 导出".to_string(), - steps: vec![ - "navigate https://www.zhihu.com/hot".to_string(), - "getText main".to_string(), - "call openxml_office".to_string(), - "return generated local .xlsx path".to_string(), - ], - } -} diff --git a/src/agent/runtime.rs b/src/agent/runtime.rs deleted file mode 100644 index 9eec5aa..0000000 --- a/src/agent/runtime.rs +++ /dev/null @@ -1,155 +0,0 @@ -use serde_json::{json, Map, Value}; - -use crate::llm::{ChatMessage, LlmError, LlmProvider, ToolDefinition, ToolFunctionCall}; -use crate::pipe::{Action, AgentMessage, BrowserPipeTool, PipeError, Transport}; - -/// Legacy browser-only runtime kept for dev-only validation and narrow regression coverage. -/// Production browser submit flow uses `compat::runtime` plus `runtime::engine`. -pub const LEGACY_DEV_ONLY: bool = true; - -const BROWSER_ACTION_TOOL_NAME: &str = "browser_action"; - -#[derive(Debug, Clone, PartialEq)] -struct BrowserActionCall { - action: Action, - expected_domain: String, - params: Value, -} - -pub fn execute_task_with_provider( - transport: &T, - browser_tool: &BrowserPipeTool, - provider: &P, - instruction: &str, -) -> Result { - let messages = vec![ - ChatMessage { - role: "system".to_string(), - content: "You are sgClaw. Use browser_action to complete the browser task.".to_string(), - }, - ChatMessage { - role: "user".to_string(), - content: instruction.to_string(), - }, - ]; - let tools = vec![browser_action_tool_definition()]; - let calls = provider - .chat(&messages, &tools) - .map_err(map_llm_error_to_pipe_error)?; - - for call in calls { - let browser_call = - parse_browser_action_call(call).map_err(|err| PipeError::Protocol(err.to_string()))?; - - transport.send(&AgentMessage::LogEntry { - level: "info".to_string(), - message: format!( - "{} {}", - browser_call.action.as_str(), - browser_call.expected_domain - ), - })?; - - let result = browser_tool.invoke( - browser_call.action, - browser_call.params, - &browser_call.expected_domain, - )?; - if !result.success { - return Err(PipeError::Protocol(format!( - "browser action failed: {}", - result.data - ))); - } - } - - Ok(format!("已通过 Agent 执行任务: {instruction}")) -} - -pub fn browser_action_tool_definition() -> ToolDefinition { - ToolDefinition { - name: BROWSER_ACTION_TOOL_NAME.to_string(), - description: "Execute browser actions in SuperRPA".to_string(), - parameters: json!({ - "type": "object", - "required": ["action", "expected_domain"], - "properties": { - "action": { "type": "string", "enum": ["click", "type", "navigate", "getText"] }, - "expected_domain": { "type": "string" }, - "selector": { "type": "string" }, - "text": { "type": "string" }, - "url": { "type": "string" }, - "clear_first": { "type": "boolean" } - } - }), - } -} - -fn parse_browser_action_call(call: ToolFunctionCall) -> Result { - if call.name != BROWSER_ACTION_TOOL_NAME { - return Err(RuntimeError::UnsupportedTool(call.name)); - } - - let mut args = match call.arguments { - Value::Object(args) => args, - other => { - return Err(RuntimeError::InvalidArguments(format!( - "expected object arguments, got {other}" - ))) - } - }; - - let action_name = take_required_string(&mut args, "action")?; - let expected_domain = take_required_string(&mut args, "expected_domain")?; - let action = parse_action(&action_name)?; - let params = Value::Object(action_params_from_args(args)); - - Ok(BrowserActionCall { - action, - expected_domain, - params, - }) -} - -fn map_llm_error_to_pipe_error(err: LlmError) -> PipeError { - PipeError::Protocol(err.to_string()) -} - -fn parse_action(action_name: &str) -> Result { - match action_name { - "click" => Ok(Action::Click), - "type" => Ok(Action::Type), - "navigate" => Ok(Action::Navigate), - "getText" => Ok(Action::GetText), - other => Err(RuntimeError::UnsupportedAction(other.to_string())), - } -} - -fn take_required_string( - args: &mut Map, - key: &'static str, -) -> Result { - match args.remove(key) { - Some(Value::String(value)) if !value.trim().is_empty() => Ok(value), - Some(other) => Err(RuntimeError::InvalidArguments(format!( - "{key} must be a non-empty string, got {other}" - ))), - None => Err(RuntimeError::MissingField(key)), - } -} - -fn action_params_from_args(args: Map) -> Map { - args -} - -#[derive(Debug, thiserror::Error)] -enum RuntimeError { - #[error("unsupported tool: {0}")] - UnsupportedTool(String), - #[error("unsupported action: {0}")] - UnsupportedAction(String), - #[error("missing required field: {0}")] - MissingField(&'static str), - #[error("invalid tool arguments: {0}")] - InvalidArguments(String), -} diff --git a/src/agent/task_runner.rs b/src/agent/task_runner.rs index c1df853..3dc6cbe 100644 --- a/src/agent/task_runner.rs +++ b/src/agent/task_runner.rs @@ -40,13 +40,13 @@ impl AgentRuntimeContext { "missing value for --config-path".to_string(), )); }; - config_path = Some(PathBuf::from(value)); + config_path = Some(resolve_process_path(PathBuf::from(value))); continue; } let arg_string = arg.to_string_lossy(); if let Some(value) = arg_string.strip_prefix("--config-path=") { - config_path = Some(PathBuf::from(value)); + config_path = Some(resolve_process_path(PathBuf::from(value))); } } @@ -81,6 +81,37 @@ fn default_workspace_root() -> PathBuf { std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")) } +fn resolve_process_path(path: PathBuf) -> PathBuf { + if path.is_absolute() { + path + } else { + default_workspace_root().join(path) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn from_process_args_resolves_relative_config_path_against_current_dir() { + let current_dir = std::env::current_dir().unwrap(); + let context = AgentRuntimeContext::from_process_args([ + OsString::from("sg_claw"), + OsString::from("--config-path"), + OsString::from("../tmp/sgclaw_config.json"), + ]) + .unwrap(); + + assert_eq!( + context.config_path, + Some(current_dir.join("../tmp/sgclaw_config.json")) + ); + assert_eq!(context.workspace_root, current_dir.join("../tmp")); + assert!(context.workspace_root.is_absolute()); + } +} + #[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct SubmitTaskRequest { pub instruction: String, diff --git a/src/browser/callback_backend.rs b/src/browser/callback_backend.rs index 4c3df4e..f66fbae 100644 --- a/src/browser/callback_backend.rs +++ b/src/browser/callback_backend.rs @@ -13,6 +13,9 @@ const TYPE_PROBE_CALLBACK_NAME: &str = "sgclawOnTypeProbe"; const GET_TEXT_CALLBACK_NAME: &str = "sgclawOnGetText"; const EVAL_CALLBACK_NAME: &str = "sgclawOnEval"; const SHOW_AREA: &str = "show"; +const LOCAL_DASHBOARD_EXPECTED_DOMAIN: &str = "__sgclaw_local_dashboard__"; +const LOCAL_DASHBOARD_SOURCE: &str = "compat.workflow_executor"; +const LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN: &str = "zhihu_hotlist_screen"; pub trait BrowserCallbackHost: Send + Sync { fn execute(&self, request: BrowserCallbackRequest) -> Result; @@ -304,7 +307,21 @@ impl BrowserBackend for BrowserCallbackBackend { params: Value, expected_domain: &str, ) -> Result { - self.mac_policy.validate(&action, expected_domain)?; + if let Some(local_dashboard) = approved_local_dashboard_request(&action, ¶ms, expected_domain) + { + self.mac_policy + .validate_local_dashboard_presentation( + &action, + expected_domain, + &local_dashboard.presentation_url, + &local_dashboard.output_path, + ) + .map_err(PipeError::Security)?; + } else { + self.mac_policy + .validate(&action, expected_domain) + .map_err(PipeError::Security)?; + } let seq = self.next_seq.fetch_add(1, Ordering::Relaxed); let reply = self.host.execute(BrowserCallbackRequest { @@ -532,6 +549,42 @@ fn escape_js_single_quoted(raw: &str) -> String { .replace('\u{2029}', "\\u2029") } +struct LocalDashboardRequest { + presentation_url: String, + output_path: String, +} + +fn approved_local_dashboard_request( + action: &Action, + params: &Value, + expected_domain: &str, +) -> Option { + if action != &Action::Navigate || expected_domain != LOCAL_DASHBOARD_EXPECTED_DOMAIN { + return None; + } + + let presentation_url = params.get("url")?.as_str()?.trim(); + let marker = params.get("sgclaw_local_dashboard_open")?.as_object()?; + let source = marker.get("source")?.as_str()?.trim(); + let kind = marker.get("kind")?.as_str()?.trim(); + let output_path = marker.get("output_path")?.as_str()?.trim(); + let marker_presentation_url = marker.get("presentation_url")?.as_str()?.trim(); + + if source != LOCAL_DASHBOARD_SOURCE + || kind != LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN + || output_path.is_empty() + || presentation_url.is_empty() + || marker_presentation_url != presentation_url + { + return None; + } + + Some(LocalDashboardRequest { + presentation_url: presentation_url.to_string(), + output_path: output_path.to_string(), + }) +} + #[cfg(test)] mod tests { use super::*; @@ -818,6 +871,71 @@ mod tests { ])); } + #[test] + fn callback_backend_accepts_approved_local_dashboard_navigate_request() { + let host = Arc::new(FakeCallbackHost::new(vec![success_reply(json!({ + "navigated": true + }))])); + let backend = BrowserCallbackBackend::new( + host.clone(), + test_policy(), + "http://127.0.0.1:17888/sgclaw/browser-helper.html", + ); + + let output = backend + .invoke( + Action::Navigate, + json!({ + "url": "file:///C:/tmp/zhihu-hotlist-screen.html", + "sgclaw_local_dashboard_open": { + "source": "compat.workflow_executor", + "kind": "zhihu_hotlist_screen", + "output_path": "C:/tmp/zhihu-hotlist-screen.html", + "presentation_url": "file:///C:/tmp/zhihu-hotlist-screen.html" + } + }), + "__sgclaw_local_dashboard__", + ) + .expect("approved local dashboard request should be accepted"); + + assert!(output.success); + let requests = host.requests(); + assert_eq!(requests.len(), 1); + assert_eq!(requests[0].command, json!([ + "http://127.0.0.1:17888/sgclaw/browser-helper.html", + "sgBrowerserOpenPage", + "file:///C:/tmp/zhihu-hotlist-screen.html" + ])); + } + + #[test] + fn callback_backend_rejects_local_dashboard_navigate_without_required_marker_fields() { + let host = Arc::new(FakeCallbackHost::new(vec![])); + let backend = BrowserCallbackBackend::new( + host.clone(), + test_policy(), + "http://127.0.0.1:17888/sgclaw/browser-helper.html", + ); + + let err = backend + .invoke( + Action::Navigate, + json!({ + "url": "file:///C:/tmp/zhihu-hotlist-screen.html", + "sgclaw_local_dashboard_open": { + "source": "compat.workflow_executor", + "kind": "zhihu_hotlist_screen", + "presentation_url": "file:///C:/tmp/zhihu-hotlist-screen.html" + } + }), + "__sgclaw_local_dashboard__", + ) + .unwrap_err(); + + assert!(host.requests().is_empty()); + assert!(err.to_string().contains("domain is not allowed")); + } + #[test] fn escape_js_single_quoted_escapes_newlines_and_control_chars() { let raw = "第一行\n第二行\r\n第三行"; diff --git a/src/browser/callback_host.rs b/src/browser/callback_host.rs index 437da8a..a51c060 100644 --- a/src/browser/callback_host.rs +++ b/src/browser/callback_host.rs @@ -293,25 +293,14 @@ impl BrowserCallbackExecutor for LiveBrowserCallbackHost { self.result_timeout }; - eprintln!( - "callback_host: execute action={} fire_and_forget={} timeout={:?}", - request.action, is_fire_and_forget, timeout - ); - let started = Instant::now(); while started.elapsed() < timeout { if let Some(result) = self.host.take_result() { - eprintln!( - "callback_host: received callback={} payload_keys={:?}", - result.callback, - result.payload.as_object().map(|m| m.keys().collect::>()) - ); if let Some(response) = normalize_callback_result(&request, result, started.elapsed()) { return Ok(response); } - eprintln!("callback_host: callback did not match action={}, continuing to wait", request.action); } thread::sleep(COMMAND_POLL_INTERVAL); } @@ -325,11 +314,6 @@ impl BrowserCallbackExecutor for LiveBrowserCallbackHost { })); } - eprintln!( - "callback_host: timeout waiting for callback on action={} after {:?}", - request.action, - started.elapsed() - ); Err(PipeError::Timeout) } } @@ -354,7 +338,6 @@ fn normalize_loopback_origin(origin: &str) -> String { } fn bootstrap_helper_page(browser_ws_url: &str, request_url: &str, helper_url: &str) -> Result<(), PipeError> { - eprintln!("callback_host: connecting to browser ws {browser_ws_url}"); let (mut websocket, _) = connect(browser_ws_url) .map_err(|err| PipeError::Protocol(format!("browser websocket connect failed: {err}")))?; configure_bootstrap_socket(&mut websocket)?; @@ -370,11 +353,9 @@ fn bootstrap_helper_page(browser_ws_url: &str, request_url: &str, helper_url: &s helper_url, ]) .to_string(); - eprintln!("callback_host: sending bootstrap command: {payload}"); websocket .send(Message::Text(payload.into())) .map_err(|err| PipeError::Protocol(format!("helper bootstrap send failed: {err}")))?; - eprintln!("callback_host: bootstrap command sent, waiting for helper page at {helper_url}"); Ok(()) } @@ -426,17 +407,11 @@ fn wait_for_helper_ready(host: &BrowserCallbackHost, ready_timeout: Duration) -> let started = Instant::now(); while started.elapsed() < ready_timeout { if host.is_ready() { - eprintln!("callback_host: helper page ready after {:?}", started.elapsed()); return Ok(()); } thread::sleep(HELPER_POLL_INTERVAL); } - eprintln!( - "callback_host: helper page did NOT become ready within {:?} — the browser may have \ - ignored the sgBrowerserOpenPage command or could not reach the helper URL", - ready_timeout, - ); Err(PipeError::Timeout) } @@ -483,11 +458,6 @@ fn handle_request(stream: &mut TcpStream, host: &BrowserCallbackHost) -> Result< let payload: IncomingCallbackEvent = serde_json::from_slice(&request.body).map_err(|err| { PipeError::Protocol(format!("invalid callback host event payload: {err}")) })?; - eprintln!( - "callback_host: received event callback={} request_url={}", - payload.callback, - payload.request_url - ); host.push_result(CallbackResult { callback: payload.callback, request_url: payload.request_url, @@ -499,22 +469,10 @@ fn handle_request(stream: &mut TcpStream, host: &BrowserCallbackHost) -> Result< } ("GET", COMMANDS_ENDPOINT_PATH) => { let envelope = host.current_command_envelope(); - if envelope.ok { - if let Some(ref cmd) = envelope.command { - eprintln!( - "callback_host: delivering command to helper action={} args_count={}", - cmd.action, - cmd.args.len() - ); - } - } write_json_response(stream, &envelope) } ("POST", COMMAND_ACK_ENDPOINT_PATH) => { - let acked = host.acknowledge_in_flight_command(); - if let Some(ref cmd) = acked { - eprintln!("callback_host: command ACKed by helper action={}", cmd.action); - } + host.acknowledge_in_flight_command(); write_json_response(stream, &json!({ "ok": true })) } _ => write_http_response(stream, 404, "text/plain; charset=utf-8", b"not found"), @@ -1063,8 +1021,7 @@ mod tests { { break; } - Err(err) => { - eprintln!("fake browser ws server read: {err}"); + Err(_) => { break; } } diff --git a/src/compat/artifact_open.rs b/src/compat/artifact_open.rs new file mode 100644 index 0000000..f18bc78 --- /dev/null +++ b/src/compat/artifact_open.rs @@ -0,0 +1,267 @@ +use std::path::Path; +use std::process::Command; + +use serde_json::{json, Value}; + +use crate::browser::BrowserBackend; +use crate::pipe::{Action, CommandOutput}; + +pub const LOCAL_DASHBOARD_EXPECTED_DOMAIN: &str = "__sgclaw_local_dashboard__"; +pub const LOCAL_DASHBOARD_SOURCE: &str = "compat.workflow_executor"; +pub const LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN: &str = "zhihu_hotlist_screen"; +const DISABLE_POST_EXPORT_OPEN_ENV: &str = "SGCLAW_DISABLE_POST_EXPORT_OPEN"; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum PostExportOpen { + Opened, + Failed(String), +} + +pub fn open_exported_xlsx(output_path: &Path) -> PostExportOpen { + open_exported_xlsx_with(output_path, launch_with_default_xlsx_app) +} + +fn open_exported_xlsx_with(output_path: &Path, opener: F) -> PostExportOpen +where + F: FnOnce(&Path) -> Result<(), String>, +{ + if !output_path.exists() { + return PostExportOpen::Failed(format!( + "导出的 Excel 文件不存在:{}", + output_path.display() + )); + } + + match opener(output_path) { + Ok(()) => PostExportOpen::Opened, + Err(reason) => PostExportOpen::Failed(reason), + } +} + +pub fn open_local_dashboard( + browser_backend: &dyn BrowserBackend, + output_path: &Path, + presentation_url: &str, +) -> PostExportOpen { + if !output_path.exists() { + return PostExportOpen::Failed(format!( + "生成的大屏文件不存在:{}", + output_path.display() + )); + } + if presentation_url.trim().is_empty() { + return PostExportOpen::Failed("screen_html_export did not return presentation.url".to_string()); + } + + let params = json!({ + "url": presentation_url, + "sgclaw_local_dashboard_open": { + "source": LOCAL_DASHBOARD_SOURCE, + "kind": LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN, + "output_path": output_path.to_string_lossy(), + "presentation_url": presentation_url, + } + }); + + match browser_backend.invoke(Action::Navigate, params, LOCAL_DASHBOARD_EXPECTED_DOMAIN) { + Ok(output) if output.success => PostExportOpen::Opened, + Ok(output) => PostExportOpen::Failed(command_output_reason(&output)), + Err(err) => PostExportOpen::Failed(err.to_string()), + } +} + +#[cfg(windows)] +fn launch_with_default_xlsx_app(output_path: &Path) -> Result<(), String> { + if std::env::var_os(DISABLE_POST_EXPORT_OPEN_ENV).is_some() { + return Ok(()); + } + + let output = Command::new("cmd") + .args(["/C", "start", "", &output_path.display().to_string()]) + .output() + .map_err(|err| format!("启动 Excel 默认程序失败:{err}"))?; + if output.status.success() { + Ok(()) + } else { + let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string(); + if stderr.is_empty() { + Err(format!( + "启动 Excel 默认程序失败:exit status {}", + output.status + )) + } else { + Err(format!("启动 Excel 默认程序失败:{stderr}")) + } + } +} + +#[cfg(target_os = "macos")] +fn launch_with_default_xlsx_app(output_path: &Path) -> Result<(), String> { + if std::env::var_os(DISABLE_POST_EXPORT_OPEN_ENV).is_some() { + return Ok(()); + } + + let status = Command::new("open") + .arg(output_path) + .status() + .map_err(|err| format!("启动 Excel 默认程序失败:{err}"))?; + if status.success() { + Ok(()) + } else { + Err(format!("启动 Excel 默认程序失败:exit status {status}")) + } +} + +#[cfg(all(unix, not(target_os = "macos")))] +fn launch_with_default_xlsx_app(output_path: &Path) -> Result<(), String> { + if std::env::var_os(DISABLE_POST_EXPORT_OPEN_ENV).is_some() { + return Ok(()); + } + + let status = Command::new("xdg-open") + .arg(output_path) + .status() + .map_err(|err| format!("启动 Excel 默认程序失败:{err}"))?; + if status.success() { + Ok(()) + } else { + Err(format!("启动 Excel 默认程序失败:exit status {status}")) + } +} + +fn command_output_reason(output: &CommandOutput) -> String { + output + .data + .get("error") + .and_then(Value::as_str) + .or_else(|| output.data.get("message").and_then(Value::as_str)) + .map(str::to_string) + .unwrap_or_else(|| output.data.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::VecDeque; + use std::path::PathBuf; + use std::sync::Mutex; + + use serde_json::json; + + use crate::pipe::{ExecutionSurfaceMetadata, PipeError, Timing}; + + fn temp_file_path(name: &str) -> PathBuf { + let root = std::env::temp_dir().join(format!( + "sgclaw-artifact-open-{}-{}", + std::process::id(), + uuid::Uuid::new_v4() + )); + std::fs::create_dir_all(&root).expect("temp root should exist"); + root.join(name) + } + + #[test] + fn open_exported_xlsx_with_passes_generated_path_to_launcher() { + let output_path = temp_file_path("zhihu-hotlist.xlsx"); + std::fs::write(&output_path, b"xlsx").expect("xlsx fixture should be writable"); + let seen = Mutex::new(None::); + + let result = open_exported_xlsx_with(&output_path, |path| { + *seen.lock().unwrap() = Some(path.to_path_buf()); + Ok(()) + }); + + assert!(matches!(result, PostExportOpen::Opened)); + assert_eq!(seen.lock().unwrap().clone().unwrap(), output_path); + } + + #[test] + fn open_exported_xlsx_with_reports_launcher_failure() { + let output_path = temp_file_path("zhihu-hotlist.xlsx"); + std::fs::write(&output_path, b"xlsx").expect("xlsx fixture should be writable"); + + let result = open_exported_xlsx_with(&output_path, |_path| Err("launcher failed".to_string())); + + assert!(matches!(result, PostExportOpen::Failed(reason) if reason.contains("launcher failed"))); + } + + #[derive(Default)] + struct FakeBrowserBackend { + responses: Mutex>>, + invocations: Mutex>, + } + + impl FakeBrowserBackend { + fn new(responses: Vec>) -> Self { + Self { + responses: Mutex::new(VecDeque::from(responses)), + invocations: Mutex::new(Vec::new()), + } + } + } + + impl BrowserBackend for FakeBrowserBackend { + fn invoke( + &self, + action: Action, + params: Value, + expected_domain: &str, + ) -> Result { + self.invocations + .lock() + .unwrap() + .push((action, params, expected_domain.to_string())); + self.responses + .lock() + .unwrap() + .pop_front() + .unwrap_or_else(|| Err(PipeError::Timeout)) + } + + fn surface_metadata(&self) -> ExecutionSurfaceMetadata { + ExecutionSurfaceMetadata::privileged_browser_pipe("fake_backend") + } + } + + #[test] + fn open_local_dashboard_uses_exact_approved_marker_payload() { + let output_path = temp_file_path("zhihu-hotlist-screen.html"); + std::fs::write(&output_path, "").expect("dashboard fixture should be writable"); + let presentation_url = format!("file:///{}", output_path.display().to_string().replace('\\', "/")); + let backend = FakeBrowserBackend::new(vec![Ok(CommandOutput { + seq: 1, + success: true, + data: json!({ "navigated": true }), + aom_snapshot: vec![], + timing: Timing { + queue_ms: 1, + exec_ms: 1, + }, + })]); + + let result = open_local_dashboard(&backend, &output_path, &presentation_url); + let invocations = backend.invocations.lock().unwrap().clone(); + + assert!(matches!(result, PostExportOpen::Opened)); + assert_eq!(invocations.len(), 1); + assert_eq!(invocations[0].0, Action::Navigate); + assert_eq!(invocations[0].2, LOCAL_DASHBOARD_EXPECTED_DOMAIN.to_string()); + assert_eq!(invocations[0].1["url"], json!(presentation_url)); + assert_eq!( + invocations[0].1["sgclaw_local_dashboard_open"]["source"], + json!(LOCAL_DASHBOARD_SOURCE) + ); + assert_eq!( + invocations[0].1["sgclaw_local_dashboard_open"]["kind"], + json!(LOCAL_DASHBOARD_KIND_ZHIHU_HOTLIST_SCREEN) + ); + assert_eq!( + invocations[0].1["sgclaw_local_dashboard_open"]["output_path"], + json!(output_path.to_string_lossy().to_string()) + ); + assert_eq!( + invocations[0].1["sgclaw_local_dashboard_open"]["presentation_url"], + invocations[0].1["url"] + ); + } +} diff --git a/src/compat/mod.rs b/src/compat/mod.rs index 39eef2e..595298a 100644 --- a/src/compat/mod.rs +++ b/src/compat/mod.rs @@ -1,3 +1,4 @@ +pub mod artifact_open; pub mod browser_script_skill_tool; pub mod browser_tool_adapter; pub mod config_adapter; diff --git a/src/compat/runtime.rs b/src/compat/runtime.rs index b366dc5..b9236d3 100644 --- a/src/compat/runtime.rs +++ b/src/compat/runtime.rs @@ -151,22 +151,6 @@ pub async fn execute_task_with_provider( ) -> Result { let engine = RuntimeEngine::new(settings.runtime_profile); let browser_surface_present = engine.browser_surface_enabled(); - if let Some(preview) = crate::agent::planner::build_execution_preview( - settings.planner_mode, - instruction, - task_context.page_url.as_deref(), - task_context.page_title.as_deref(), - ) { - let mut message = preview.summary; - if !preview.steps.is_empty() { - message.push('\n'); - message.push_str(&preview.steps.join("\n")); - } - transport.send(&crate::pipe::AgentMessage::LogEntry { - level: "plan".to_string(), - message, - })?; - } let loaded_skills = engine.loaded_skills(&config, &skills_dir); let loaded_skill_versions = loaded_skills .iter() @@ -376,3 +360,22 @@ fn to_chat_message(message: &ConversationMessage) -> Option { _ => None, } } + +#[cfg(test)] +mod tests { + use std::fs; + use std::path::PathBuf; + + #[test] + fn compat_runtime_source_no_longer_references_legacy_planner_preview() { + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let source = fs::read_to_string(manifest_dir.join("src/compat/runtime.rs")).unwrap(); + let preview_prefix = ["if let Some(preview) = crate::agent::", "planner::build_execution_preview("].concat(); + let plan_level_expr = ["level: ", "\"plan\".to_string(),"].concat(); + + assert!(!source + .lines() + .any(|line| line.trim_start().starts_with(&preview_prefix))); + assert!(!source.lines().any(|line| line.trim() == plan_level_expr)); + } +} diff --git a/src/compat/screen_html_export_tool.rs b/src/compat/screen_html_export_tool.rs index 7ac0cd9..e4a54c3 100644 --- a/src/compat/screen_html_export_tool.rs +++ b/src/compat/screen_html_export_tool.rs @@ -150,11 +150,7 @@ impl Tool for ScreenHtmlExportTool { }; let rendered = render_template(&payload)?; - let output_path = parsed - .output_path - .as_deref() - .map(PathBuf::from) - .unwrap_or_else(|| default_output_path(&self.workspace_root)); + let output_path = resolve_output_path(&self.workspace_root, parsed.output_path.as_deref()); write_output_html(&output_path, &rendered)?; let presentation_url = file_url_for_path(&output_path); @@ -375,6 +371,21 @@ fn default_output_path(workspace_root: &Path) -> PathBuf { .join(format!("zhihu-hotlist-screen-{nanos}.html")) } +fn resolve_output_path(workspace_root: &Path, output_path: Option<&str>) -> PathBuf { + output_path + .map(str::trim) + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + .map(|path| { + if path.is_absolute() { + path + } else { + workspace_root.join(path) + } + }) + .unwrap_or_else(|| default_output_path(workspace_root)) +} + fn default_snapshot_id() -> String { format!("zhihu-hotlist-screen-{}", now_ms()) } @@ -391,3 +402,67 @@ fn file_url_for_path(path: &Path) -> String { .map(|url| url.to_string()) .unwrap_or_else(|_| format!("file://{}", path.display())) } + +#[cfg(test)] +mod tests { + use super::*; + use serde_json::{json, Value}; + + use crate::pipe::Action; + use crate::security::MacPolicy; + + fn temp_workspace_root() -> PathBuf { + let root = std::env::temp_dir().join(format!("sgclaw-screen-html-{}", now_ms())); + std::fs::create_dir_all(&root).unwrap(); + root + } + + #[tokio::test] + async fn screen_html_export_resolves_relative_output_path_to_absolute_file_url() { + let workspace_root = temp_workspace_root(); + let tool = ScreenHtmlExportTool::new(workspace_root.clone()); + + let result = tool + .execute(json!({ + "snapshot_id": "snapshot-relative-path", + "generated_at_ms": 1774713600000u64, + "rows": [ + [1, "问题一", "344万"], + [2, "问题二", "266万"] + ], + "output_path": "../out/zhihu-hotlist-screen-relative.html" + })) + .await + .unwrap(); + + assert!(result.success, "{result:?}"); + + let payload: Value = serde_json::from_str(&result.output).unwrap(); + let output_path = PathBuf::from(payload["output_path"].as_str().unwrap()); + let presentation_url = payload["presentation"]["url"].as_str().unwrap(); + let expected_output_path = workspace_root.join("../out/zhihu-hotlist-screen-relative.html"); + let expected_presentation_url = Url::from_file_path(&expected_output_path) + .unwrap() + .to_string(); + let policy = MacPolicy::load_from_path( + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("resources") + .join("rules.json"), + ) + .unwrap(); + + assert!(output_path.is_absolute()); + assert_eq!(output_path, expected_output_path); + assert!(output_path.exists()); + assert_eq!(presentation_url, expected_presentation_url); + assert!(presentation_url.starts_with("file:///")); + policy + .validate_local_dashboard_presentation( + &Action::Navigate, + "__sgclaw_local_dashboard__", + presentation_url, + output_path.to_string_lossy().as_ref(), + ) + .unwrap(); + } +} diff --git a/src/compat/workflow_executor.rs b/src/compat/workflow_executor.rs index d4c0d31..250beab 100644 --- a/src/compat/workflow_executor.rs +++ b/src/compat/workflow_executor.rs @@ -9,6 +9,7 @@ use serde_json::{json, Value}; use zeroclaw::tools::Tool; use crate::browser::{BrowserBackend, PipeBrowserBackend}; +use crate::compat::artifact_open::{open_exported_xlsx, open_local_dashboard, PostExportOpen}; use crate::compat::openxml_office_tool::OpenXmlOfficeTool; use crate::compat::runtime::CompatTaskContext; use crate::compat::screen_html_export_tool::ScreenHtmlExportTool; @@ -147,7 +148,9 @@ pub fn execute_route_with_browser_backend( } match route { WorkflowRoute::ZhihuHotlistExportXlsx => export_xlsx(transport, workspace_root, &items), - WorkflowRoute::ZhihuHotlistScreen => export_screen(transport, workspace_root, &items), + WorkflowRoute::ZhihuHotlistScreen => { + export_screen(transport, browser_backend.as_ref(), workspace_root, &items) + } _ => unreachable!("handled by outer match"), } } @@ -297,21 +300,10 @@ fn probe_hotlist_extractor( ZHIHU_DOMAIN, )?; if !response.success { - eprintln!("probe_hotlist_extractor: eval not successful data={}", response.data); return Ok(None); } let eval_text = response.data.get("text").unwrap_or(&response.data); - let eval_preview: String = eval_text - .as_str() - .unwrap_or_default() - .chars() - .take(300) - .collect(); - eprintln!( - "probe_hotlist_extractor: eval_len={} preview={eval_preview:?}", - eval_text.as_str().unwrap_or_default().len() - ); match parse_hotlist_items_payload(eval_text) { Ok(items) if !items.is_empty() => Ok(Some(items)), @@ -366,11 +358,6 @@ fn poll_for_hotlist_readiness(browser_tool: &dyn BrowserBackend) -> Result format!("已导出并打开知乎热榜 Excel {output_path}"), + PostExportOpen::Failed(reason) => { + format!("已导出知乎热榜 Excel {output_path},但自动打开失败:{reason}") + } + }) } fn export_screen( transport: &dyn crate::agent::AgentEventSink, + browser_backend: &dyn BrowserBackend, workspace_root: &Path, items: &[HotlistItem], ) -> Result { @@ -454,12 +447,25 @@ fn export_screen( )); } - let payload: Value = serde_json::from_str(&result.output) + finalize_screen_export(browser_backend, &result.output) +} + +pub fn finalize_screen_export( + browser_backend: &dyn BrowserBackend, + output: &str, +) -> Result { + let payload: Value = serde_json::from_str(output) .map_err(|err| PipeError::Protocol(format!("invalid screen_html_export output: {err}")))?; let output_path = payload["output_path"].as_str().ok_or_else(|| { PipeError::Protocol("screen_html_export did not return output_path".to_string()) })?; - Ok(format!("已生成知乎热榜大屏 {output_path}")) + let presentation_url = payload["presentation"]["url"].as_str().unwrap_or_default(); + Ok(match open_local_dashboard(browser_backend, Path::new(output_path), presentation_url) { + PostExportOpen::Opened => format!("已在浏览器中打开知乎热榜大屏 {output_path}"), + PostExportOpen::Failed(reason) => { + format!("已生成知乎热榜大屏 {output_path},但浏览器自动打开失败:{reason}") + } + }) } fn execute_zhihu_article_route( @@ -823,7 +829,6 @@ fn execute_zhihu_fill_via_live_input( ]); // ── Step 1: Click title field ────────────────────────────── - eprintln!("live_input: step 1 — click title field"); browser_tool.invoke( Action::Click, json!({ @@ -871,7 +876,6 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}}); title_chunk = title_chunk, title_delay = title_delay, ); - eprintln!("live_input: step 2 — animated title typing ({title_chars} chars, ~{title_wait}ms)"); browser_tool.invoke( Action::Eval, json!({ "script": title_script }), @@ -880,7 +884,6 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}}); std::thread::sleep(std::time::Duration::from_millis(title_wait)); // ── Step 3: Click body field ──────────────────────────────── - eprintln!("live_input: step 3 — click body field"); browser_tool.invoke( Action::Click, json!({ @@ -930,7 +933,6 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}}); body_chunk = body_chunk, body_delay = body_delay, ); - eprintln!("live_input: step 4 — animated body typing ({body_chars} chars, ~{body_wait}ms)"); browser_tool.invoke( Action::Eval, json!({ "script": body_script }), @@ -941,7 +943,6 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}}); // Step 5: Fill content only. The publish-button click is split into a // separate eval (step 6) because React needs a full render cycle to // enable the button after the content fill updates the editor state. - eprintln!("live_input: step 5 — eval fill_article_draft.js (fill only, publish_mode=false)"); let fill_result = execute_browser_skill_script( browser_tool, "zhihu-write", @@ -960,7 +961,6 @@ return JSON.stringify({{status:'ok',chunks:chunks.length}}); } // Step 6: After React has rendered the enabled publish button, click it. - eprintln!("live_input: step 6 — waiting 1.5s for React render, then clicking publish"); std::thread::sleep(std::time::Duration::from_millis(1500)); let publish_script = r#"(function(){ diff --git a/src/pipe/browser_tool.rs b/src/pipe/browser_tool.rs index 7d92bb4..287a6b8 100644 --- a/src/pipe/browser_tool.rs +++ b/src/pipe/browser_tool.rs @@ -10,6 +10,8 @@ use crate::pipe::protocol::{ use crate::pipe::{PipeError, Transport}; use crate::security::{sign_command, MacPolicy}; +const LOCAL_DASHBOARD_EXPECTED_DOMAIN: &str = "__sgclaw_local_dashboard__"; + #[derive(Debug, Clone, PartialEq)] pub struct CommandOutput { pub seq: u64, @@ -77,7 +79,24 @@ impl BrowserPipeTool { params: Value, expected_domain: &str, ) -> Result { - self.mac_policy.validate(&action, expected_domain)?; + if let Some((presentation_url, output_path)) = approved_local_dashboard_request( + &action, + ¶ms, + expected_domain, + ) { + self.mac_policy + .validate_local_dashboard_presentation( + &action, + expected_domain, + &presentation_url, + &output_path, + ) + .map_err(PipeError::Security)?; + } else { + self.mac_policy + .validate(&action, expected_domain) + .map_err(PipeError::Security)?; + } let seq = self.next_seq.fetch_add(1, Ordering::Relaxed); let hmac = sign_command(&self.session_key, seq, &action, ¶ms, expected_domain)?; @@ -151,3 +170,31 @@ impl BrowserPipeTool { } } } + +fn approved_local_dashboard_request( + action: &Action, + params: &Value, + expected_domain: &str, +) -> Option<(String, String)> { + if action != &Action::Navigate || expected_domain != LOCAL_DASHBOARD_EXPECTED_DOMAIN { + return None; + } + + let presentation_url = params.get("url")?.as_str()?.trim(); + let marker = params.get("sgclaw_local_dashboard_open")?.as_object()?; + let source = marker.get("source")?.as_str()?.trim(); + let kind = marker.get("kind")?.as_str()?.trim(); + let output_path = marker.get("output_path")?.as_str()?.trim(); + let marker_presentation_url = marker.get("presentation_url")?.as_str()?.trim(); + + if source != "compat.workflow_executor" + || kind != "zhihu_hotlist_screen" + || output_path.is_empty() + || presentation_url.is_empty() + || marker_presentation_url != presentation_url + { + return None; + } + + Some((presentation_url.to_string(), output_path.to_string())) +} diff --git a/src/runtime/engine.rs b/src/runtime/engine.rs index 6a56e62..b69d088 100644 --- a/src/runtime/engine.rs +++ b/src/runtime/engine.rs @@ -179,17 +179,6 @@ impl RuntimeEngine { skills } - pub fn loaded_skill_names(&self, config: &ZeroClawConfig, skills_dir: &Path) -> Vec { - let mut names = self - .loaded_skills(config, skills_dir) - .into_iter() - .map(|skill| skill.name) - .collect::>(); - names.sort(); - names.dedup(); - names - } - pub fn should_attach_openxml_office_tool(&self, instruction: &str) -> bool { task_needs_office_export(instruction) } diff --git a/src/security/mac_policy.rs b/src/security/mac_policy.rs index 3c7faff..77feeec 100644 --- a/src/security/mac_policy.rs +++ b/src/security/mac_policy.rs @@ -25,6 +25,8 @@ pub struct PipeActionRules { pub blocked: Vec, } +const LOCAL_DASHBOARD_EXPECTED_DOMAIN: &str = "__sgclaw_local_dashboard__"; + impl MacPolicy { pub fn load_from_path(path: impl AsRef) -> Result { let contents = fs::read_to_string(path)?; @@ -91,6 +93,64 @@ impl MacPolicy { Ok(()) } + pub fn validate_local_dashboard_presentation( + &self, + action: &Action, + expected_domain: &str, + presentation_url: &str, + output_path: &str, + ) -> Result<(), SecurityError> { + let action_name = action.as_str(); + if self + .pipe_actions + .blocked + .iter() + .any(|blocked| blocked == action_name) + { + return Err(SecurityError::ActionNotAllowed(action_name.to_string())); + } + + if !self + .pipe_actions + .allowed + .iter() + .any(|allowed| allowed == action_name) + { + return Err(SecurityError::ActionNotAllowed(action_name.to_string())); + } + + if action != &Action::Navigate { + return Err(SecurityError::InvalidLocalDashboard( + "local dashboard open only supports navigate".to_string(), + )); + } + if expected_domain != LOCAL_DASHBOARD_EXPECTED_DOMAIN { + return Err(SecurityError::InvalidLocalDashboard( + "local dashboard expected_domain is invalid".to_string(), + )); + } + if !presentation_url.starts_with("file:///") { + return Err(SecurityError::InvalidLocalDashboard( + "local dashboard presentation_url must be file:///".to_string(), + )); + } + if !output_path.to_ascii_lowercase().ends_with(".html") { + return Err(SecurityError::InvalidLocalDashboard( + "local dashboard output_path must point to .html".to_string(), + )); + } + + let normalized_output = normalize_local_dashboard_path(output_path); + let normalized_presentation = normalize_local_dashboard_file_url(presentation_url)?; + if normalized_output != normalized_presentation { + return Err(SecurityError::InvalidLocalDashboard( + "local dashboard presentation_url does not match output_path".to_string(), + )); + } + + Ok(()) + } + pub fn privileged_surface_metadata(&self) -> ExecutionSurfaceMetadata { let mut metadata = ExecutionSurfaceMetadata::privileged_browser_pipe("mac_policy"); metadata.allowed_domains = self.domains.allowed.clone(); @@ -130,3 +190,19 @@ fn normalize_domain(raw: &str) -> String { .unwrap_or_default() .to_ascii_lowercase() } + +fn normalize_local_dashboard_path(raw: &str) -> String { + raw.trim().replace('\\', "/").to_ascii_lowercase() +} + +fn normalize_local_dashboard_file_url(raw: &str) -> Result { + let path = raw + .trim() + .strip_prefix("file:///") + .ok_or_else(|| { + SecurityError::InvalidLocalDashboard( + "local dashboard presentation_url must be file:///".to_string(), + ) + })?; + Ok(normalize_local_dashboard_path(path)) +} diff --git a/src/security/mod.rs b/src/security/mod.rs index b82727e..88de4fe 100644 --- a/src/security/mod.rs +++ b/src/security/mod.rs @@ -14,6 +14,8 @@ pub enum SecurityError { ActionNotAllowed(String), #[error("domain is not allowed: {0}")] DomainNotAllowed(String), + #[error("invalid local dashboard request: {0}")] + InvalidLocalDashboard(String), #[error("invalid rules: {0}")] InvalidRules(String), #[error("hmac error: {0}")] diff --git a/tests/agent_runtime_test.rs b/tests/agent_runtime_test.rs index d3ca2b9..6852b34 100644 --- a/tests/agent_runtime_test.rs +++ b/tests/agent_runtime_test.rs @@ -1,36 +1,167 @@ mod common; -use std::sync::Arc; +use std::fs; +use std::net::TcpListener; +use std::path::PathBuf; +use std::sync::{Arc, Mutex, OnceLock}; +use std::thread; use std::time::Duration; use common::MockTransport; -use sgclaw::agent::handle_browser_message; -use sgclaw::agent::runtime::{browser_action_tool_definition, execute_task_with_provider}; -use sgclaw::llm::{ChatMessage, LlmError, LlmProvider, ToolDefinition, ToolFunctionCall}; -use sgclaw::pipe::{Action, AgentMessage, BrowserMessage, BrowserPipeTool, Timing}; +use serde_json::{json, Value}; +use sgclaw::agent::{ + handle_browser_message, handle_browser_message_with_context, AgentRuntimeContext, +}; +use sgclaw::pipe::{AgentMessage, BrowserMessage, BrowserPipeTool, Timing}; use sgclaw::security::MacPolicy; +use tungstenite::{accept, Message}; +use uuid::Uuid; -struct FakeProvider { - calls: Vec, +fn env_lock() -> &'static Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) } -impl LlmProvider for FakeProvider { - fn chat( - &self, - _messages: &[ChatMessage], - _tools: &[ToolDefinition], - ) -> Result, LlmError> { - Ok(self.calls.clone()) +fn temp_workspace_root() -> PathBuf { + let root = std::env::temp_dir().join(format!("sgclaw-agent-runtime-{}", Uuid::new_v4())); + fs::create_dir_all(&root).unwrap(); + root +} + +fn write_config( + root: &PathBuf, + api_key: &str, + base_url: &str, + model: &str, + skills_dir: Option<&str>, +) -> PathBuf { + let config_path = root.join("sgclaw_config.json"); + let mut payload = json!({ + "apiKey": api_key, + "baseUrl": base_url, + "model": model, + "runtimeProfile": "BrowserAttached" + }); + if let Some(skills_dir) = skills_dir { + payload["skillsDir"] = json!(skills_dir); } + fs::write(&config_path, serde_json::to_string_pretty(&payload).unwrap()).unwrap(); + config_path +} + +fn real_skill_lib_root() -> PathBuf { + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .ancestors() + .find_map(|ancestor| { + let candidate = ancestor.join("skill_lib"); + candidate.is_dir().then_some(candidate) + }) + .expect("workspace should have sgClaw skill_lib ancestor") +} + +fn start_browser_ws_server() -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let frames = Arc::new(Mutex::new(Vec::new())); + let frames_for_thread = Arc::clone(&frames); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + stream + .set_read_timeout(Some(Duration::from_secs(1))) + .unwrap(); + stream + .set_write_timeout(Some(Duration::from_secs(1))) + .unwrap(); + let mut socket = accept(stream).unwrap(); + let mut action_count = 0_u64; + + loop { + let message = match socket.read() { + Ok(message) => message, + Err(tungstenite::Error::ConnectionClosed) + | Err(tungstenite::Error::AlreadyClosed) => break, + Err(err) => panic!("browser ws test server read failed: {err}"), + }; + let payload = match message { + Message::Text(text) => text.to_string(), + Message::Ping(payload) => { + socket.send(Message::Pong(payload)).unwrap(); + continue; + } + Message::Close(_) => break, + other => panic!("expected text frame, got {other:?}"), + }; + frames_for_thread.lock().unwrap().push(payload.clone()); + + let parsed: Value = serde_json::from_str(&payload).unwrap(); + if parsed.get("type").and_then(Value::as_str) == Some("register") { + continue; + } + + let values = parsed.as_array().expect("browser action frame should be an array"); + let request_url = values[0].as_str().expect("request_url should be a string"); + let action = values[1].as_str().expect("action should be a string"); + action_count += 1; + + socket + .send(Message::Text( + r#"{"type":"welcome","client_id":1,"server_time":"2026-04-04T00:00:00"}"# + .to_string() + .into(), + )) + .unwrap(); + socket.send(Message::Text("0".into())).unwrap(); + + let callback_frame = match action { + "sgHideBrowserCallAfterLoaded" => { + let target_url = values[2].as_str().expect("navigate target_url should be a string"); + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgHideBrowserCallAfterLoaded@_@" + ) + ]) + } + "sgBrowserExcuteJsCodeByArea" => { + let target_url = values[2].as_str().expect("script target_url should be a string"); + let response_text = if action_count == 2 { + "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度".to_string() + } else { + r#"{"source":"https://www.zhihu.com/hot","sheet_name":"知乎热榜","columns":["rank","title","heat"],"rows":[[1,"问题一","344万"],[2,"问题二","266万"]]}"#.to_string() + }; + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgBrowserExcuteJsCodeByArea@_@{response_text}" + ) + ]) + } + other => panic!("unexpected browser action {other}"), + }; + + socket + .send(Message::Text(callback_frame.to_string().into())) + .unwrap(); + + if action_count >= 3 { + break; + } + } + }); + + (format!("ws://{address}"), frames, handle) } fn test_policy() -> MacPolicy { MacPolicy::from_json_str( r#"{ "version": "1.0", - "domains": { "allowed": ["www.baidu.com"] }, + "domains": { "allowed": ["www.baidu.com", "www.zhihu.com"] }, "pipe_actions": { - "allowed": ["click", "type", "navigate", "getText"], + "allowed": ["click", "type", "navigate", "getText", "eval"], "blocked": [] } }"#, @@ -39,104 +170,131 @@ fn test_policy() -> MacPolicy { } #[test] -fn browser_action_tool_definition_uses_expected_name() { - let tool = browser_action_tool_definition(); +fn production_submit_task_routes_zhihu_through_ws_backend_without_helper_bootstrap() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::set_var("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1"); - assert_eq!(tool.name, "browser_action"); - assert_eq!(tool.parameters["required"][0], "action"); - assert_eq!(tool.parameters["required"][1], "expected_domain"); -} + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); -#[test] -fn runtime_executes_provider_tool_calls_and_returns_summary() { - let transport = Arc::new(MockTransport::new(vec![ - BrowserMessage::Response { - seq: 1, - success: true, - data: serde_json::json!({ "navigated": true }), - aom_snapshot: vec![], - timing: Timing { - queue_ms: 1, - exec_ms: 10, - }, - }, - BrowserMessage::Response { - seq: 2, - success: true, - data: serde_json::json!({ "typed": true }), - aom_snapshot: vec![], - timing: Timing { - queue_ms: 1, - exec_ms: 10, - }, - }, - ])); + let workspace_root = temp_workspace_root(); + let config_path = write_config( + &workspace_root, + "deepseek-test-key", + "http://127.0.0.1:9", + "deepseek-chat", + Some(real_skill_lib_root().to_str().unwrap()), + ); + + let (ws_url, frames, ws_handle) = start_browser_ws_server(); + std::env::set_var("SGCLAW_BROWSER_WS_URL", &ws_url); + + let transport = Arc::new(MockTransport::new(vec![])); let browser_tool = BrowserPipeTool::new( transport.clone(), test_policy(), vec![1, 2, 3, 4, 5, 6, 7, 8], ) .with_response_timeout(Duration::from_secs(1)); - let provider = FakeProvider { - calls: vec![ - ToolFunctionCall { - id: "call-1".to_string(), - name: "browser_action".to_string(), - arguments: serde_json::json!({ - "action": "navigate", - "expected_domain": "www.baidu.com", - "url": "https://www.baidu.com" - }), - }, - ToolFunctionCall { - id: "call-2".to_string(), - name: "browser_action".to_string(), - arguments: serde_json::json!({ - "action": "type", - "expected_domain": "www.baidu.com", - "selector": "#kw", - "text": "天气", - "clear_first": true - }), - }, - ], - }; + let runtime_context = AgentRuntimeContext::new(Some(config_path), workspace_root.clone()); - let summary = execute_task_with_provider( + handle_browser_message_with_context( transport.as_ref(), &browser_tool, - &provider, - "打开百度搜索天气", + &runtime_context, + BrowserMessage::SubmitTask { + instruction: "打开知乎热榜,获取前10条数据,并导出 Excel".to_string(), + conversation_id: String::new(), + messages: vec![], + page_url: String::new(), + page_title: String::new(), + }, ) .unwrap(); - let sent = transport.sent_messages(); - assert_eq!(summary, "已通过 Agent 执行任务: 打开百度搜索天气"); - assert!(matches!( - &sent[0], - AgentMessage::LogEntry { level, message } - if level == "info" && message == "navigate www.baidu.com" - )); - assert!(matches!( - &sent[1], - AgentMessage::Command { seq, action, .. } - if *seq == 1 && action == &Action::Navigate - )); - assert!(matches!( - &sent[2], - AgentMessage::LogEntry { level, message } - if level == "info" && message == "type www.baidu.com" - )); - assert!(matches!( - &sent[3], - AgentMessage::Command { seq, action, .. } - if *seq == 2 && action == &Action::Type - )); + ws_handle.join().unwrap(); + + let sent = transport.sent_messages(); + let websocket_frames = frames.lock().unwrap().clone(); + + assert_eq!(websocket_frames.len(), 4, "{websocket_frames:?}"); + assert_eq!(websocket_frames[0], r#"{"type":"register","role":"web"}"#); + assert!(!websocket_frames + .iter() + .any(|frame| frame.contains("/sgclaw/browser-helper.html"))); + assert!(!websocket_frames + .iter() + .any(|frame| frame.contains("\"sgBrowerserOpenPage\""))); + + let navigate: Value = serde_json::from_str(&websocket_frames[1]).unwrap(); + assert_eq!(navigate[0], json!("https://www.zhihu.com")); + assert_eq!(navigate[1], json!("sgHideBrowserCallAfterLoaded")); + assert_eq!(navigate[2], json!("https://www.zhihu.com/hot")); + + let get_text: Value = serde_json::from_str(&websocket_frames[2]).unwrap(); + assert_eq!(get_text[0], json!("https://www.zhihu.com/hot")); + assert_eq!(get_text[1], json!("sgBrowserExcuteJsCodeByArea")); + assert_eq!(get_text[2], json!("https://www.zhihu.com/hot")); + + let eval: Value = serde_json::from_str(&websocket_frames[3]).unwrap(); + assert_eq!(eval[0], json!("https://www.zhihu.com/hot")); + assert_eq!(eval[1], json!("sgBrowserExcuteJsCodeByArea")); + assert_eq!(eval[2], json!("https://www.zhihu.com/hot")); + + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && message == "zeroclaw_process_message_primary" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::TaskComplete { success, summary } + if *success && summary.contains("已导出并打开知乎热榜 Excel") && summary.contains(".xlsx") + ) + })); + std::env::remove_var("SGCLAW_DISABLE_POST_EXPORT_OPEN"); + assert!(!sent.iter().any(|message| matches!(message, AgentMessage::Command { .. }))); } #[test] -fn legacy_agent_runtime_is_explicitly_dev_only() { - assert!(sgclaw::agent::runtime::LEGACY_DEV_ONLY); +fn lifecycle_messages_emit_status_events_without_browser_commands() { + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = BrowserPipeTool::new( + transport.clone(), + test_policy(), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ) + .with_response_timeout(Duration::from_secs(1)); + + sgclaw::agent::handle_browser_message(transport.as_ref(), &browser_tool, BrowserMessage::Connect) + .unwrap(); + sgclaw::agent::handle_browser_message(transport.as_ref(), &browser_tool, BrowserMessage::Start) + .unwrap(); + sgclaw::agent::handle_browser_message(transport.as_ref(), &browser_tool, BrowserMessage::Stop) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!( + sent, + vec![ + AgentMessage::StatusChanged { + state: "connected".to_string(), + }, + AgentMessage::StatusChanged { + state: "started".to_string(), + }, + AgentMessage::StatusChanged { + state: "stopped".to_string(), + }, + ] + ); + assert!(!sent + .iter() + .any(|message| { matches!(message, AgentMessage::Command { .. }) })); } #[test] diff --git a/tests/browser_backend_capability_test.rs b/tests/browser_backend_capability_test.rs new file mode 100644 index 0000000..f714ade --- /dev/null +++ b/tests/browser_backend_capability_test.rs @@ -0,0 +1,145 @@ +mod common; + +use std::collections::HashMap; +use std::fs; +use std::path::PathBuf; +use std::sync::Arc; +use std::time::{SystemTime, UNIX_EPOCH}; + +use common::MockTransport; +use sgclaw::browser::{BrowserBackend, PipeBrowserBackend}; +use sgclaw::compat::browser_script_skill_tool::build_browser_script_skill_tools; +use sgclaw::pipe::{Action, CommandOutput, ExecutionSurfaceKind, ExecutionSurfaceMetadata}; +use sgclaw::security::MacPolicy; +use zeroclaw::skills::{Skill, SkillTool}; + +fn backend_policy() -> MacPolicy { + MacPolicy::from_json_str( + r#"{ + "version": "1.0", + "domains": { "allowed": ["oa.example.com", "erp.example.com"] }, + "pipe_actions": { + "allowed": ["click", "type", "navigate", "getText"], + "blocked": ["eval", "executeJsInPage"] + } + }"#, + ) + .unwrap() +} + +fn eval_policy() -> MacPolicy { + MacPolicy::from_json_str( + r#"{ + "version": "1.0", + "domains": { "allowed": ["www.zhihu.com"] }, + "pipe_actions": { + "allowed": ["click", "type", "navigate", "getText", "eval"], + "blocked": [] + } + }"#, + ) + .unwrap() +} + +#[test] +fn pipe_browser_backend_keeps_privileged_pipe_surface_metadata() { + let transport = Arc::new(MockTransport::new(vec![])); + let backend = PipeBrowserBackend::new(transport, backend_policy(), vec![1, 2, 3, 4]); + + let metadata = backend.surface_metadata(); + + assert_eq!(metadata.kind, ExecutionSurfaceKind::PrivilegedBrowserPipe); + assert!(metadata.privileged); + assert!(!metadata.defines_runtime_identity); + assert_eq!(metadata.guard, "mac_policy"); + assert_eq!( + metadata.allowed_domains, + vec!["oa.example.com", "erp.example.com"] + ); + assert_eq!( + metadata.allowed_actions, + vec!["click", "type", "navigate", "getText"] + ); +} + +#[test] +fn pipe_browser_backend_reports_eval_capability_from_mac_policy() { + let transport = Arc::new(MockTransport::new(vec![])); + let backend = PipeBrowserBackend::new(transport, eval_policy(), vec![1, 2, 3, 4]); + + assert!(backend.supports_eval()); +} + +#[test] +fn browser_script_tools_are_hidden_when_backend_cannot_eval() { + let skill_root = unique_temp_dir("sgclaw-browser-backend-capability"); + let scripts_dir = skill_root.join("scripts"); + fs::create_dir_all(&scripts_dir).unwrap(); + fs::write( + scripts_dir.join("extract_hotlist.js"), + "return { rows: [[1, '标题', '10万热度']] };", + ) + .unwrap(); + + let skills = vec![Skill { + name: "zhihu-hotlist".to_string(), + description: "Zhihu hotlist helpers".to_string(), + version: "1.0.0".to_string(), + author: None, + tags: vec![], + tools: vec![SkillTool { + name: "extract_hotlist".to_string(), + description: "Extract structured hotlist rows".to_string(), + kind: "browser_script".to_string(), + command: "scripts/extract_hotlist.js".to_string(), + args: HashMap::new(), + }], + prompts: vec![], + location: Some(skill_root.join("skill.json")), + }]; + let backend: Arc = Arc::new(FakeBrowserBackend::new(false)); + + let tools = build_browser_script_skill_tools(&skills, backend).unwrap(); + + assert!(tools.is_empty()); +} + +#[derive(Default)] +struct FakeBrowserBackend { + supports_eval: bool, +} + +impl FakeBrowserBackend { + fn new(supports_eval: bool) -> Self { + Self { supports_eval } + } +} + +impl BrowserBackend for FakeBrowserBackend { + fn invoke( + &self, + _action: Action, + _params: serde_json::Value, + _expected_domain: &str, + ) -> Result { + panic!("invoke should not be called in this capability-gating test") + } + + fn surface_metadata(&self) -> ExecutionSurfaceMetadata { + ExecutionSurfaceMetadata::privileged_browser_pipe("fake_backend") + } + + fn supports_eval(&self) -> bool { + self.supports_eval + } +} + +fn unique_temp_dir(prefix: &str) -> PathBuf { + let nanos = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_nanos(); + let path = std::env::temp_dir().join(format!("{prefix}-{nanos}")); + fs::create_dir_all(&path).unwrap(); + path +} diff --git a/tests/browser_bridge_backend_test.rs b/tests/browser_bridge_backend_test.rs new file mode 100644 index 0000000..229144e --- /dev/null +++ b/tests/browser_bridge_backend_test.rs @@ -0,0 +1,151 @@ +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; + +use serde_json::json; +use sgclaw::browser::bridge_contract::{ + BridgeBrowserActionError, BridgeBrowserActionReply, BridgeBrowserActionRequest, + BridgeBrowserActionSuccess, +}; +use sgclaw::browser::bridge_transport::BridgeActionTransport; +use sgclaw::browser::{BridgeBrowserBackend, BrowserBackend}; +use sgclaw::pipe::{Action, PipeError, Timing}; +use sgclaw::security::MacPolicy; + +fn test_policy() -> MacPolicy { + MacPolicy::from_json_str( + r#"{ + "version": "1.0", + "domains": { "allowed": ["www.baidu.com"] }, + "pipe_actions": { + "allowed": ["click", "type", "navigate", "getText", "eval"], + "blocked": [] + } + }"#, + ) + .unwrap() +} + +struct FakeBridgeTransport { + requests: Mutex>, + replies: Mutex>>, +} + +impl FakeBridgeTransport { + fn new(replies: Vec>) -> Self { + Self { + requests: Mutex::new(Vec::new()), + replies: Mutex::new(replies.into()), + } + } + + fn recorded_requests(&self) -> Vec { + self.requests.lock().unwrap().clone() + } +} + +impl BridgeActionTransport for FakeBridgeTransport { + fn execute( + &self, + request: BridgeBrowserActionRequest, + ) -> Result { + self.requests.lock().unwrap().push(request); + self.replies + .lock() + .unwrap() + .pop_front() + .unwrap_or(Err(PipeError::Timeout)) + } +} + +#[test] +fn bridge_backend_maps_navigate_to_bridge_action_request() { + let transport = Arc::new(FakeBridgeTransport::new(vec![Ok( + BridgeBrowserActionReply::Success(BridgeBrowserActionSuccess { + data: json!({ "navigated": true }), + aom_snapshot: vec![], + timing: Timing { + queue_ms: 1, + exec_ms: 11, + }, + }), + )])); + let backend = BridgeBrowserBackend::new(transport.clone(), test_policy()); + + let output = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ) + .unwrap(); + + assert_eq!( + transport.recorded_requests(), + vec![BridgeBrowserActionRequest::new( + "navigate", + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + )] + ); + assert_eq!(output.seq, 1); + assert!(output.success); +} + +#[test] +fn bridge_backend_normalizes_successful_bridge_reply() { + let transport = Arc::new(FakeBridgeTransport::new(vec![Ok( + BridgeBrowserActionReply::Success(BridgeBrowserActionSuccess { + data: json!({ "text": "天气" }), + aom_snapshot: vec![json!({ "role": "textbox", "name": "百度一下" })], + timing: Timing { + queue_ms: 4, + exec_ms: 14, + }, + }), + )])); + let backend = BridgeBrowserBackend::new(transport, test_policy()); + + let output = backend + .invoke( + Action::GetText, + json!({ "selector": "#content_left" }), + "www.baidu.com", + ) + .unwrap(); + + assert_eq!(output.seq, 1); + assert!(output.success); + assert_eq!(output.data, json!({ "text": "天气" })); + assert_eq!( + output.aom_snapshot, + vec![json!({ "role": "textbox", "name": "百度一下" })] + ); + assert_eq!( + output.timing, + Timing { + queue_ms: 4, + exec_ms: 14, + } + ); +} + +#[test] +fn bridge_backend_maps_bridge_failure_to_pipe_error() { + let transport = Arc::new(FakeBridgeTransport::new(vec![Ok( + BridgeBrowserActionReply::Error(BridgeBrowserActionError { + message: "selector not found".to_string(), + details: json!({ "selector": "#missing" }), + }), + )])); + let backend = BridgeBrowserBackend::new(transport, test_policy()); + + let error = backend + .invoke( + Action::Click, + json!({ "selector": "#missing" }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(matches!(error, PipeError::Protocol(message) if message == "bridge action failed: selector not found")); +} diff --git a/tests/browser_bridge_contract_test.rs b/tests/browser_bridge_contract_test.rs new file mode 100644 index 0000000..bfee86a --- /dev/null +++ b/tests/browser_bridge_contract_test.rs @@ -0,0 +1,80 @@ +use serde_json::{json, Value}; +use sgclaw::browser::bridge_contract::{BridgeBrowserActionRequest, BridgeLifecycleCall}; + +#[test] +fn bridge_contract_names_match_documented_bridge_surface() { + let lifecycle_names = [ + BridgeLifecycleCall::Connect.bridge_name(), + BridgeLifecycleCall::Start.bridge_name(), + BridgeLifecycleCall::Stop.bridge_name(), + BridgeLifecycleCall::SubmitTask.bridge_name(), + ]; + + assert_eq!( + lifecycle_names, + [ + "sgclawConnect", + "sgclawStart", + "sgclawStop", + "sgclawSubmitTask", + ] + ); +} + +#[test] +fn bridge_contract_represents_browser_action_requests_without_ws_business_frames() { + let requests = vec![ + BridgeBrowserActionRequest::new( + "navigate", + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ), + BridgeBrowserActionRequest::new( + "click", + json!({ "selector": "#submit" }), + "www.zhihu.com", + ), + BridgeBrowserActionRequest::new( + "getText", + json!({ "selector": "#content" }), + "www.zhihu.com", + ), + ]; + + let serialized = serde_json::to_value(&requests).unwrap(); + let entries = serialized.as_array().unwrap(); + let actions = entries + .iter() + .map(|entry| entry["action"].as_str().unwrap()) + .collect::>(); + + assert_eq!( + serialized, + json!([ + { + "action": "navigate", + "params": { "url": "https://www.baidu.com" }, + "expected_domain": "www.baidu.com" + }, + { + "action": "click", + "params": { "selector": "#submit" }, + "expected_domain": "www.zhihu.com" + }, + { + "action": "getText", + "params": { "selector": "#content" }, + "expected_domain": "www.zhihu.com" + } + ]) + ); + assert_eq!(actions, vec!["navigate", "click", "getText"]); + + let first = entries.first().unwrap(); + let object = first.as_object().unwrap(); + assert_eq!(object.len(), 3); + assert!(object.contains_key("action")); + assert!(object.contains_key("params")); + assert!(object.contains_key("expected_domain")); + assert_eq!(first["expected_domain"], Value::String("www.baidu.com".to_string())); +} diff --git a/tests/browser_script_skill_tool_test.rs b/tests/browser_script_skill_tool_test.rs index ef9f338..333f12f 100644 --- a/tests/browser_script_skill_tool_test.rs +++ b/tests/browser_script_skill_tool_test.rs @@ -9,6 +9,7 @@ use std::time::{SystemTime, UNIX_EPOCH}; use common::MockTransport; use serde_json::json; +use sgclaw::browser::{BrowserBackend, PipeBrowserBackend}; use sgclaw::compat::browser_script_skill_tool::BrowserScriptSkillTool; use sgclaw::pipe::{Action, AgentMessage, BrowserMessage, BrowserPipeTool, Timing}; use sgclaw::security::MacPolicy; @@ -67,6 +68,7 @@ return { vec![1, 2, 3, 4, 5, 6, 7, 8], ) .with_response_timeout(Duration::from_secs(1)); + let backend: Arc = Arc::new(PipeBrowserBackend::from_inner(browser_tool)); let mut args = HashMap::new(); args.insert("top_n".to_string(), "How many rows to extract".to_string()); @@ -77,7 +79,7 @@ return { command: "scripts/extract_hotlist.js".to_string(), args, }; - let tool = BrowserScriptSkillTool::new("zhihu-hotlist", &skill_tool, &skill_dir, browser_tool) + let tool = BrowserScriptSkillTool::new("zhihu-hotlist", &skill_tool, &skill_dir, backend) .unwrap(); let result = tool diff --git a/tests/browser_tool_test.rs b/tests/browser_tool_test.rs index 6867397..6286f5b 100644 --- a/tests/browser_tool_test.rs +++ b/tests/browser_tool_test.rs @@ -106,6 +106,53 @@ fn browser_tool_exposes_privileged_surface_metadata_backed_by_mac_policy() { ); } +#[test] +fn browser_tool_accepts_approved_local_dashboard_navigate_request() { + let transport = Arc::new(MockTransport::new(vec![BrowserMessage::Response { + seq: 1, + success: true, + data: serde_json::json!({"navigated": true}), + aom_snapshot: vec![], + timing: Timing { + queue_ms: 1, + exec_ms: 20, + }, + }])); + let tool = BrowserPipeTool::new(transport.clone(), test_policy(), vec![1, 2, 3, 4]) + .with_response_timeout(Duration::from_secs(1)); + + let result = tool + .invoke( + Action::Navigate, + serde_json::json!({ + "url": "file:///C:/tmp/zhihu-hotlist-screen.html", + "sgclaw_local_dashboard_open": { + "source": "compat.workflow_executor", + "kind": "zhihu_hotlist_screen", + "output_path": "C:/tmp/zhihu-hotlist-screen.html", + "presentation_url": "file:///C:/tmp/zhihu-hotlist-screen.html" + } + }), + "__sgclaw_local_dashboard__", + ) + .unwrap(); + let sent = transport.sent_messages(); + + assert!(result.success); + assert!(matches!( + &sent[0], + AgentMessage::Command { + action, + params, + security, + .. + } if action == &Action::Navigate + && security.expected_domain == "__sgclaw_local_dashboard__" + && params["url"] == serde_json::json!("file:///C:/tmp/zhihu-hotlist-screen.html") + && params["sgclaw_local_dashboard_open"]["kind"] == serde_json::json!("zhihu_hotlist_screen") + )); +} + #[test] fn default_rules_allow_zhihu_navigation() { let rules_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) @@ -115,3 +162,22 @@ fn default_rules_allow_zhihu_navigation() { policy.validate(&Action::Navigate, "www.zhihu.com").unwrap(); } + +#[test] +fn mac_policy_rejects_non_html_local_dashboard_presentation() { + let rules_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("resources") + .join("rules.json"); + let policy = MacPolicy::load_from_path(rules_path).unwrap(); + + let err = policy + .validate_local_dashboard_presentation( + &Action::Navigate, + "__sgclaw_local_dashboard__", + "file:///C:/tmp/zhihu-hotlist-screen.txt", + "C:/tmp/zhihu-hotlist-screen.txt", + ) + .unwrap_err(); + + assert!(err.to_string().contains("local dashboard")); +} diff --git a/tests/browser_ws_backend_test.rs b/tests/browser_ws_backend_test.rs new file mode 100644 index 0000000..7bba175 --- /dev/null +++ b/tests/browser_ws_backend_test.rs @@ -0,0 +1,356 @@ +use std::collections::VecDeque; +use std::sync::{Arc, Mutex}; +use std::time::Duration; + +use serde_json::{json, Value}; +use sgclaw::browser::ws_backend::WsClient; +use sgclaw::browser::{BrowserBackend, WsBrowserBackend}; +use sgclaw::pipe::{Action, PipeError}; +use sgclaw::security::MacPolicy; + +fn test_policy() -> MacPolicy { + MacPolicy::from_json_str( + r#"{ + "version": "1.0", + "domains": { "allowed": ["www.baidu.com"] }, + "pipe_actions": { + "allowed": ["click", "type", "navigate", "getText", "eval"], + "blocked": [] + } + }"#, + ) + .unwrap() +} + +struct FakeWsClient { + incoming: Mutex>>, + sent: Mutex>, +} + +impl FakeWsClient { + fn new(frames: Vec>) -> Self { + Self { + incoming: Mutex::new( + frames + .into_iter() + .map(|frame| frame.map(str::to_string)) + .collect(), + ), + sent: Mutex::new(Vec::new()), + } + } + + fn sent_frames(&self) -> Vec { + self.sent.lock().unwrap().clone() + } +} + +impl WsClient for FakeWsClient { + fn send_text(&self, payload: &str) -> Result<(), PipeError> { + self.sent.lock().unwrap().push(payload.to_string()); + Ok(()) + } + + fn recv_text_timeout(&self, _timeout: Duration) -> Result { + self.incoming + .lock() + .unwrap() + .pop_front() + .unwrap_or(Err(PipeError::Timeout)) + } +} + +#[test] +fn ws_backend_ignores_welcome_frame_before_zero_status() { + let client = Arc::new(FakeWsClient::new(vec![ + Ok("Welcome! You are client #1"), + Ok("0"), + Ok( + r#"["https://www.baidu.com/current","callBackJsToCpp","https://www.baidu.com/current@_@https://www.baidu.com@_@sgclaw_cb_1@_@sgHideBrowserCallAfterLoaded@_@"]"#, + ), + ])); + let backend = WsBrowserBackend::new( + client.clone(), + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let output = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ) + .unwrap(); + + assert!(output.success); + let sent = client.sent_frames(); + assert_eq!(sent.len(), 1); +} + +#[test] +fn ws_backend_ignores_json_welcome_frame_before_zero_status() { + let client = Arc::new(FakeWsClient::new(vec![ + Ok(r#"{"type":"welcome","client_id":17,"server_time":"2026-04-04T11:04:54"}"#), + Ok("0"), + Ok( + r#"["https://www.baidu.com/current","callBackJsToCpp","https://www.baidu.com/current@_@https://www.baidu.com@_@sgclaw_cb_1@_@sgHideBrowserCallAfterLoaded@_@"]"#, + ), + ])); + let backend = WsBrowserBackend::new( + client.clone(), + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let output = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ) + .unwrap(); + + assert!(output.success); + let sent = client.sent_frames(); + assert_eq!(sent.len(), 1); +} + +#[test] +fn ws_backend_fails_on_non_numeric_non_welcome_status_frame() { + let client = Arc::new(FakeWsClient::new(vec![Ok("not-a-status") ])); + let backend = WsBrowserBackend::new( + client, + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let error = backend + .invoke( + Action::Click, + json!({ + "target_url": "https://www.baidu.com/current", + "selector": "#submit" + }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(error.to_string().contains("invalid browser status frame: not-a-status")); +} +#[test] +fn ws_backend_returns_success_for_zero_without_callback() { + let client = Arc::new(FakeWsClient::new(vec![ + Ok("0"), + Ok( + r#"["https://www.baidu.com/current","callBackJsToCpp","https://www.baidu.com/current@_@https://www.baidu.com@_@sgclaw_cb_1@_@sgHideBrowserCallAfterLoaded@_@"]"#, + ), + ])); + let backend = WsBrowserBackend::new( + client.clone(), + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let output = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ) + .unwrap(); + + assert_eq!(output.seq, 1); + assert!(output.success); + assert_eq!(output.data, json!({ "text": "" })); + assert!(output.aom_snapshot.is_empty()); + + let sent = client.sent_frames(); + assert_eq!(sent.len(), 1); + let payload: Value = serde_json::from_str(&sent[0]).unwrap(); + assert_eq!(payload[1], json!("sgHideBrowserCallAfterLoaded")); + assert_eq!(payload[2], json!("https://www.baidu.com")); +} + +#[test] +fn ws_backend_fails_immediately_on_non_zero_return_code() { + let client = Arc::new(FakeWsClient::new(vec![Ok("7")])); + let backend = WsBrowserBackend::new( + client, + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let error = backend + .invoke( + Action::Click, + json!({ + "target_url": "https://www.baidu.com/current", + "selector": "#submit" + }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(error.to_string().contains("browser returned non-zero status: 7")); +} + +#[test] +fn ws_backend_waits_for_callback_and_normalizes_result_payload() { + let client = Arc::new(FakeWsClient::new(vec![ + Ok("0"), + Ok( + r#"["https://www.baidu.com/current","callBackJsToCpp","https://www.baidu.com/current@_@https://www.baidu.com/current@_@sgclaw_cb_1@_@sgBrowserExcuteJsCodeByArea@_@天气"]"#, + ), + ])); + let backend = WsBrowserBackend::new( + client.clone(), + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let output = backend + .invoke( + Action::GetText, + json!({ + "target_url": "https://www.baidu.com/current", + "selector": "#content" + }), + "www.baidu.com", + ) + .unwrap(); + + assert_eq!(output.seq, 1); + assert!(output.success); + assert_eq!(output.data, json!({ "text": "天气" })); + assert!(output.aom_snapshot.is_empty()); + + let sent = client.sent_frames(); + assert_eq!(sent.len(), 1); + let payload: Value = serde_json::from_str(&sent[0]).unwrap(); + assert_eq!(payload[1], json!("sgBrowserExcuteJsCodeByArea")); +} + +#[test] +fn ws_backend_times_out_while_waiting_for_callback_after_zero_status() { + let client = Arc::new(FakeWsClient::new(vec![Ok("0")])); + let backend = WsBrowserBackend::new( + client, + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_millis(1)); + + let error = backend + .invoke( + Action::Eval, + json!({ + "target_url": "https://www.baidu.com/current", + "script": "2 + 2" + }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(matches!(error, PipeError::Timeout)); +} + +#[test] +fn ws_backend_times_out_when_navigate_callback_never_arrives() { + let client = Arc::new(FakeWsClient::new(vec![ + Err(PipeError::Timeout), + Err(PipeError::Timeout), + ])); + let backend = WsBrowserBackend::new(client.clone(), test_policy(), "https://www.zhihu.com") + .with_response_timeout(Duration::from_millis(1)); + + let error = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.zhihu.com/hot" }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(matches!(error, PipeError::Timeout)); + let sent = client.sent_frames(); + let payload: Value = serde_json::from_str(&sent[0]).unwrap(); + assert_eq!(payload[1], json!("sgHideBrowserCallAfterLoaded")); + assert_eq!(payload[2], json!("https://www.zhihu.com/hot")); +} + +#[test] +fn ws_backend_reuses_last_navigated_url_for_followup_requests() { + let client = Arc::new(FakeWsClient::new(vec![ + Ok("0"), + Ok( + r#"["https://www.baidu.com/current","callBackJsToCpp","https://www.baidu.com/current@_@https://www.baidu.com@_@sgclaw_cb_1@_@sgHideBrowserCallAfterLoaded@_@"]"#, + ), + Ok("0"), + Ok( + r#"["https://www.zhihu.com/hot","callBackJsToCpp","https://www.zhihu.com/hot@_@https://www.zhihu.com/hot@_@sgclaw_cb_2@_@sgBrowserExcuteJsCodeByArea@_@热榜文本"]"#, + ), + ])); + let backend = WsBrowserBackend::new(client.clone(), test_policy(), "about:blank") + .with_response_timeout(Duration::from_secs(1)); + + backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.zhihu.com/hot" }), + "www.baidu.com", + ) + .unwrap(); + + let output = backend + .invoke( + Action::GetText, + json!({ "selector": "body" }), + "www.baidu.com", + ) + .unwrap(); + + assert!(output.success); + assert_eq!(output.data, json!({ "text": "热榜文本" })); + + let sent = client.sent_frames(); + assert_eq!(sent.len(), 2); + + let navigate_payload: Value = serde_json::from_str(&sent[0]).unwrap(); + assert_eq!(navigate_payload[0], json!("about:blank")); + assert_eq!(navigate_payload[1], json!("sgHideBrowserCallAfterLoaded")); + assert_eq!(navigate_payload[2], json!("https://www.zhihu.com/hot")); + + let followup_payload: Value = serde_json::from_str(&sent[1]).unwrap(); + assert_eq!(followup_payload[0], json!("https://www.zhihu.com/hot")); + assert_eq!(followup_payload[1], json!("sgBrowserExcuteJsCodeByArea")); + assert_eq!(followup_payload[2], json!("https://www.zhihu.com/hot")); + assert_eq!(followup_payload[4], json!("hide")); +} + +#[test] +fn ws_backend_propagates_socket_drop_after_navigate_send() { + let client = Arc::new(FakeWsClient::new(vec![Err(PipeError::PipeClosed)])); + let backend = WsBrowserBackend::new( + client, + test_policy(), + "https://www.baidu.com/current", + ) + .with_response_timeout(Duration::from_secs(1)); + + let error = backend + .invoke( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + "www.baidu.com", + ) + .unwrap_err(); + + assert!(matches!(error, PipeError::PipeClosed)); +} diff --git a/tests/browser_ws_probe_test.rs b/tests/browser_ws_probe_test.rs new file mode 100644 index 0000000..c70580f --- /dev/null +++ b/tests/browser_ws_probe_test.rs @@ -0,0 +1,422 @@ +use std::net::TcpListener; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::time::Duration; + +use tungstenite::{accept, Message}; + +#[path = "../src/browser/ws_probe.rs"] +mod ws_probe; + +use ws_probe::{ + parse_probe_args, run_probe_script, ProbeCliConfig, ProbeOutcome, ProbeStep, ProbeStepResult, +}; + +#[derive(Clone)] +enum ServerStep { + ReceiveThenReply { expected: String, reply: String }, + ReceiveThenReplyFrames { expected: String, replies: Vec }, + ReceiveThenStaySilent { expected: String }, + ReceiveThenClose { expected: String }, + CloseBeforeReceive, +} + +fn spawn_fake_server(script: Vec) -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = listener.local_addr().unwrap(); + let received = Arc::new(Mutex::new(Vec::new())); + let received_for_thread = received.clone(); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut socket = accept(stream).unwrap(); + + for step in script { + match step { + ServerStep::CloseBeforeReceive => { + socket.close(None).unwrap(); + return; + } + ServerStep::ReceiveThenReply { expected, reply } => { + let message = socket.read().unwrap(); + let payload = match message { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + }; + received_for_thread.lock().unwrap().push(payload.clone()); + assert_eq!(payload, expected); + socket.send(Message::Text(reply.into())).unwrap(); + } + ServerStep::ReceiveThenReplyFrames { expected, replies } => { + let message = socket.read().unwrap(); + let payload = match message { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + }; + received_for_thread.lock().unwrap().push(payload.clone()); + assert_eq!(payload, expected); + for reply in replies { + socket.send(Message::Text(reply.into())).unwrap(); + } + } + ServerStep::ReceiveThenStaySilent { expected } => { + let message = socket.read().unwrap(); + let payload = match message { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + }; + received_for_thread.lock().unwrap().push(payload.clone()); + assert_eq!(payload, expected); + thread::sleep(Duration::from_millis(120)); + } + ServerStep::ReceiveThenClose { expected } => { + let message = socket.read().unwrap(); + let payload = match message { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + }; + received_for_thread.lock().unwrap().push(payload.clone()); + assert_eq!(payload, expected); + socket.close(None).unwrap(); + return; + } + } + } + }); + + (format!("ws://{addr}"), received, handle) +} + +#[test] +fn parse_probe_args_rejects_non_ws_schemes() { + let cases = [ + "wss://127.0.0.1:12345", + "http://127.0.0.1:12345", + "127.0.0.1:12345", + ]; + + for ws_url in cases { + let args = vec![ + "--ws-url".to_string(), + ws_url.to_string(), + "--timeout-ms".to_string(), + "1500".to_string(), + "--step".to_string(), + "open-agent::[\"about:blank\",\"sgOpenAgent\"]".to_string(), + ]; + + let err = parse_probe_args(&args).unwrap_err(); + + assert_eq!( + err.to_string(), + format!( + "probe argument error: unsupported --ws-url scheme (only ws:// is supported for this probe): {ws_url}" + ) + ); + } +} + +#[test] +fn parse_probe_args_accepts_ws_url_timeout_and_ordered_steps() { + let args = vec![ + "--ws-url".to_string(), + "ws://127.0.0.1:12345".to_string(), + "--timeout-ms".to_string(), + "1500".to_string(), + "--step".to_string(), + "open-agent::[\"about:blank\",\"sgOpenAgent\"]".to_string(), + "--step".to_string(), + "open-hot::[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + .to_string(), + ]; + + let parsed = parse_probe_args(&args).unwrap(); + + assert_eq!( + parsed, + ProbeCliConfig { + ws_url: "ws://127.0.0.1:12345".to_string(), + timeout_ms: 1500, + steps: vec![ + ProbeStep { + label: "open-agent".to_string(), + payload: "[\"about:blank\",\"sgOpenAgent\"]".to_string(), + expect_reply: true, + }, + ProbeStep { + label: "open-hot".to_string(), + payload: + "[\"about:blank\",\"sgBrowerserOpenPage\",\"https://www.zhihu.com/hot\"]" + .to_string(), + expect_reply: true, + }, + ], + } + ); +} + +#[test] +fn parse_probe_args_defaults_register_step_when_step_is_omitted() { + let args = vec![ + "--ws-url".to_string(), + "ws://127.0.0.1:12345".to_string(), + ]; + + let parsed = parse_probe_args(&args).unwrap(); + + assert_eq!(parsed.ws_url, "ws://127.0.0.1:12345"); + assert_eq!(parsed.timeout_ms, 1500); + assert_eq!( + parsed.steps, + vec![ProbeStep { + label: "register".to_string(), + payload: r#"{"type":"register","role":"web"}"#.to_string(), + expect_reply: true, + }] + ); +} + +#[test] +fn parse_probe_args_defaults_timeout_when_flag_is_omitted() { + let args = vec![ + "--ws-url".to_string(), + "ws://127.0.0.1:12345".to_string(), + "--step".to_string(), + "open-agent::[\"about:blank\",\"sgOpenAgent\"]".to_string(), + ]; + + let parsed = parse_probe_args(&args).unwrap(); + + assert_eq!(parsed.ws_url, "ws://127.0.0.1:12345"); + assert_eq!(parsed.timeout_ms, 1500); + assert_eq!( + parsed.steps, + vec![ProbeStep { + label: "open-agent".to_string(), + payload: "[\"about:blank\",\"sgOpenAgent\"]".to_string(), + expect_reply: true, + }] + ); +} + +#[test] +fn probe_records_welcome_then_silence_transcript() { + let steps = vec![ + ProbeStep { + label: "open-agent".to_string(), + payload: r#"["about:blank","sgOpenAgent"]"#.to_string(), + expect_reply: true, + }, + ProbeStep { + label: "await-followup".to_string(), + payload: r#"["about:blank","sgNoop"]"#.to_string(), + expect_reply: true, + }, + ]; + let (ws_url, received, handle) = spawn_fake_server(vec![ + ServerStep::ReceiveThenReply { + expected: steps[0].payload.clone(), + reply: "Welcome! You are client #1".to_string(), + }, + ServerStep::ReceiveThenStaySilent { + expected: steps[1].payload.clone(), + }, + ]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), steps.clone()).unwrap(); + + assert_eq!( + received.lock().unwrap().clone(), + steps.iter().map(|step| step.payload.clone()).collect::>() + ); + assert_eq!( + results, + vec![ + ProbeStepResult { + label: "open-agent".to_string(), + sent: r#"["about:blank","sgOpenAgent"]"#.to_string(), + outcome: ProbeOutcome::Received(vec!["Welcome! You are client #1".to_string()]), + }, + ProbeStepResult { + label: "await-followup".to_string(), + sent: r#"["about:blank","sgNoop"]"#.to_string(), + outcome: ProbeOutcome::TimedOut, + }, + ] + ); + + handle.join().unwrap(); +} + +#[test] +fn probe_runs_ordered_frame_script_and_records_per_step_results() { + let steps = vec![ + ProbeStep { + label: "bootstrap-1".to_string(), + payload: r#"["about:blank","sgOpenAgent"]"#.to_string(), + expect_reply: true, + }, + ProbeStep { + label: "bootstrap-2".to_string(), + payload: r#"["about:blank","sgSetAuthInfo","probe-user","probe-token"]"#.to_string(), + expect_reply: true, + }, + ProbeStep { + label: "action".to_string(), + payload: r#"["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]"#.to_string(), + expect_reply: true, + }, + ]; + let (ws_url, received, handle) = spawn_fake_server(vec![ + ServerStep::ReceiveThenReply { + expected: steps[0].payload.clone(), + reply: "welcome".to_string(), + }, + ServerStep::ReceiveThenReply { + expected: steps[1].payload.clone(), + reply: "0".to_string(), + }, + ServerStep::ReceiveThenStaySilent { + expected: steps[2].payload.clone(), + }, + ]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), steps.clone()).unwrap(); + + assert_eq!( + received.lock().unwrap().clone(), + steps.iter().map(|step| step.payload.clone()).collect::>() + ); + assert_eq!(results.len(), 3); + assert_eq!(results[0].label, "bootstrap-1"); + assert_eq!(results[0].outcome, ProbeOutcome::Received(vec!["welcome".to_string()])); + assert_eq!(results[1].label, "bootstrap-2"); + assert_eq!(results[1].outcome, ProbeOutcome::Received(vec!["0".to_string()])); + assert_eq!(results[2].label, "action"); + assert_eq!(results[2].sent, r#"["about:blank","sgBrowerserOpenPage","https://www.zhihu.com/hot"]"#); + assert_eq!(results[2].outcome, ProbeOutcome::TimedOut); + + handle.join().unwrap(); +} + +#[test] +fn probe_records_multiple_frames_for_one_step_within_timeout_window() { + let steps = vec![ProbeStep { + label: "bootstrap".to_string(), + payload: r#"["about:blank","sgOpenAgent"]"#.to_string(), + expect_reply: true, + }]; + let (ws_url, received, handle) = spawn_fake_server(vec![ServerStep::ReceiveThenReplyFrames { + expected: steps[0].payload.clone(), + replies: vec!["welcome".to_string(), "status:ready".to_string()], + }]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), steps.clone()).unwrap(); + + assert_eq!(received.lock().unwrap().as_slice(), [steps[0].payload.as_str()]); + assert_eq!( + results, + vec![ProbeStepResult { + label: "bootstrap".to_string(), + sent: r#"["about:blank","sgOpenAgent"]"#.to_string(), + outcome: ProbeOutcome::Received(vec![ + "welcome".to_string(), + "status:ready".to_string(), + ]), + }] + ); + + handle.join().unwrap(); +} + +#[test] +fn probe_records_steps_that_do_not_wait_for_reply_without_ambiguity() { + let steps = vec![ProbeStep { + label: "fire-and-forget".to_string(), + payload: r#"["about:blank","sgNoop"]"#.to_string(), + expect_reply: false, + }]; + let (ws_url, received, handle) = + spawn_fake_server(vec![ServerStep::ReceiveThenStaySilent { + expected: steps[0].payload.clone(), + }]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), steps.clone()).unwrap(); + + handle.join().unwrap(); + + assert_eq!(received.lock().unwrap().as_slice(), [steps[0].payload.as_str()]); + assert_eq!( + results, + vec![ProbeStepResult { + label: "fire-and-forget".to_string(), + sent: r#"["about:blank","sgNoop"]"#.to_string(), + outcome: ProbeOutcome::NoReplyExpected, + }] + ); +} + +#[test] +fn probe_records_close_when_server_closes_before_next_send() { + let steps = vec![ + ProbeStep { + label: "open-agent".to_string(), + payload: r#"["about:blank","sgOpenAgent"]"#.to_string(), + expect_reply: true, + }, + ProbeStep { + label: "follow-up".to_string(), + payload: r#"["about:blank","sgNoop"]"#.to_string(), + expect_reply: true, + }, + ]; + let (ws_url, received, handle) = spawn_fake_server(vec![ + ServerStep::ReceiveThenReply { + expected: steps[0].payload.clone(), + reply: "welcome".to_string(), + }, + ServerStep::CloseBeforeReceive, + ]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), steps.clone()).unwrap(); + + assert_eq!(received.lock().unwrap().as_slice(), [steps[0].payload.as_str()]); + assert_eq!( + results, + vec![ + ProbeStepResult { + label: "open-agent".to_string(), + sent: r#"["about:blank","sgOpenAgent"]"#.to_string(), + outcome: ProbeOutcome::Received(vec!["welcome".to_string()]), + }, + ProbeStepResult { + label: "follow-up".to_string(), + sent: r#"["about:blank","sgNoop"]"#.to_string(), + outcome: ProbeOutcome::Closed, + }, + ] + ); + + handle.join().unwrap(); +} + +#[test] +fn probe_reports_socket_close_separately_from_timeout() { + let step = ProbeStep { + label: "close-case".to_string(), + payload: r#"["about:blank","sgOpenAgent"]"#.to_string(), + expect_reply: true, + }; + let (ws_url, received, handle) = spawn_fake_server(vec![ServerStep::ReceiveThenClose { + expected: step.payload.clone(), + }]); + + let results = run_probe_script(&ws_url, Duration::from_millis(40), vec![step]).unwrap(); + + assert_eq!(received.lock().unwrap().as_slice(), [r#"["about:blank","sgOpenAgent"]"#]); + assert_eq!(results.len(), 1); + assert_eq!(results[0].label, "close-case"); + assert_eq!(results[0].outcome, ProbeOutcome::Closed); + + handle.join().unwrap(); +} diff --git a/tests/browser_ws_protocol_test.rs b/tests/browser_ws_protocol_test.rs new file mode 100644 index 0000000..d9c3f6d --- /dev/null +++ b/tests/browser_ws_protocol_test.rs @@ -0,0 +1,195 @@ +use serde_json::{json, Value}; +use sgclaw::browser::ws_protocol::{decode_callback_frame, encode_v1_action}; +use sgclaw::pipe::Action; + +#[test] +fn encodes_navigate_frame_exactly_as_browser_array() { + let request = encode_v1_action( + &Action::Navigate, + &json!({ "url": "https://www.baidu.com" }), + "https://www.zhihu.com/hot", + Some("req42"), + ) + .unwrap(); + + assert_eq!( + request.payload, + r#"["https://www.zhihu.com/hot","sgHideBrowserCallAfterLoaded","https://www.baidu.com","callBackJsToCpp(\"https://www.zhihu.com/hot@_@https://www.baidu.com@_@sgclaw_cb_req42@_@sgHideBrowserCallAfterLoaded@_@\")"]"# + ); + let callback = request.callback.unwrap(); + assert_eq!(callback.request_id, "req42"); + assert_eq!(callback.callback_name, "sgclaw_cb_req42"); + assert_eq!(callback.source_url, "https://www.zhihu.com/hot"); + assert_eq!(callback.target_url, "https://www.baidu.com"); + assert_eq!(callback.action_url, "sgHideBrowserCallAfterLoaded"); +} + +#[test] +fn encodes_get_text_frame_with_documented_callback_action_url() { + let request = encode_v1_action( + &Action::GetText, + &json!({ + "target_url": "https://www.zhihu.com/hot", + "selector": "#content" + }), + "https://www.zhihu.com/hot", + Some("req42"), + ) + .unwrap(); + + let payload: Value = serde_json::from_str(&request.payload).unwrap(); + assert_eq!( + payload, + json!([ + "https://www.zhihu.com/hot", + "sgBrowserExcuteJsCodeByArea", + "https://www.zhihu.com/hot", + "(function(){const el=document.querySelector(\"#content\");if(!el){throw new Error(\"selector not found: #content\");}const text=el.innerText ?? el.textContent ?? \"\";callBackJsToCpp(\"https://www.zhihu.com/hot@_@https://www.zhihu.com/hot@_@sgclaw_cb_req42@_@sgBrowserExcuteJsCodeByArea@_@\"+String(text));})();", + "hide" + ]) + ); + let callback = request.callback.unwrap(); + assert_eq!(callback.request_id, "req42"); + assert_eq!(callback.callback_name, "sgclaw_cb_req42"); + assert_eq!(callback.source_url, "https://www.zhihu.com/hot"); + assert_eq!(callback.target_url, "https://www.zhihu.com/hot"); + assert_eq!(callback.action_url, "sgBrowserExcuteJsCodeByArea"); +} + +#[test] +fn decodes_callback_payload_from_browser_frame() { + let callback = decode_callback_frame( + r#"["https://www.zhihu.com/hot","callBackJsToCpp","https://www.zhihu.com/hot@_@https://www.zhihu.com/hot@_@sgclaw_cb_req42@_@sgBrowserExcuteJsCodeByArea@_@天气"]"#, + ) + .unwrap(); + + assert_eq!(callback.source_url, "https://www.zhihu.com/hot"); + assert_eq!(callback.target_url, "https://www.zhihu.com/hot"); + assert_eq!(callback.callback_name, "sgclaw_cb_req42"); + assert_eq!(callback.action_url, "sgBrowserExcuteJsCodeByArea"); + assert_eq!(callback.response_text, "天气"); +} + +#[test] +fn rejects_malformed_callback_frames_and_missing_request_ids() { + let malformed = decode_callback_frame( + r#"["https://www.zhihu.com/hot","callBackJsToCpp","https://www.zhihu.com/hot@_@too-short"]"#, + ) + .unwrap_err(); + assert!(malformed.to_string().contains("malformed callback payload")); + + let wrong_function = decode_callback_frame( + r#"["https://www.zhihu.com/hot","sgBrowerserOpenPage","0"]"#, + ) + .unwrap_err(); + assert!(wrong_function + .to_string() + .contains("callback frame must target callBackJsToCpp")); + + let missing_request_id = encode_v1_action( + &Action::Eval, + &json!({ + "target_url": "https://www.zhihu.com/hot", + "script": "2 + 2" + }), + "https://www.zhihu.com/hot", + None, + ) + .unwrap_err(); + assert!(missing_request_id + .to_string() + .contains("request_id is required")); +} + +#[test] +fn eval_uses_documented_js_opcode_for_callback_action_url() { + let request = encode_v1_action( + &Action::Eval, + &json!({ + "target_url": "https://www.zhihu.com/hot", + "script": "2 + 2" + }), + "https://www.zhihu.com/hot", + Some("req-eval"), + ) + .unwrap(); + + let callback = request.callback.unwrap(); + assert_eq!(callback.callback_name, "sgclaw_cb_req-eval"); + assert_eq!(callback.action_url, "sgBrowserExcuteJsCodeByArea"); + + let payload: Value = serde_json::from_str(&request.payload).unwrap(); + let js = payload[3].as_str().unwrap(); + assert!(js.contains("callBackJsToCpp(\"https://www.zhihu.com/hot@_@https://www.zhihu.com/hot@_@sgclaw_cb_req-eval@_@sgBrowserExcuteJsCodeByArea@_@\"+String(result))")); +} + +#[test] +fn covers_supported_v1_action_mapping_and_rejects_unsupported_actions() { + let cases = vec![ + ( + Action::Navigate, + json!({ "url": "https://www.baidu.com" }), + Some("req-nav"), + "sgHideBrowserCallAfterLoaded", + true, + ), + ( + Action::Click, + json!({ + "target_url": "https://www.zhihu.com/hot", + "selector": "#submit" + }), + None, + "sgBrowserExcuteJsCodeByArea", + false, + ), + ( + Action::Type, + json!({ + "target_url": "https://www.zhihu.com/hot", + "selector": "#kw", + "text": "天气" + }), + None, + "sgBrowserExcuteJsCodeByArea", + false, + ), + ( + Action::GetText, + json!({ + "target_url": "https://www.zhihu.com/hot", + "selector": "#content" + }), + Some("req-get-text"), + "sgBrowserExcuteJsCodeByArea", + true, + ), + ( + Action::Eval, + json!({ + "target_url": "https://www.zhihu.com/hot", + "script": "2 + 2" + }), + Some("req-eval"), + "sgBrowserExcuteJsCodeByArea", + true, + ), + ]; + + for (action, params, request_id, browser_function, expects_callback) in cases { + let request = encode_v1_action(&action, ¶ms, "https://www.zhihu.com/hot", request_id) + .unwrap(); + let payload: Value = serde_json::from_str(&request.payload).unwrap(); + assert_eq!(payload[1], json!(browser_function), "action={action:?}"); + assert_eq!(request.callback.is_some(), expects_callback, "action={action:?}"); + } + + let unsupported = encode_v1_action( + &Action::GetHtml, + &json!({ "selector": "body" }), + "https://www.zhihu.com/hot", + None, + ) + .unwrap_err(); + assert!(unsupported.to_string().contains("unsupported browser ws action")); +} diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f79a5c6..654dbf0 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -17,6 +17,7 @@ impl MockTransport { } } + #[allow(dead_code)] pub fn sent_messages(&self) -> Vec { self.sent.lock().unwrap().clone() } diff --git a/tests/compat_browser_tool_test.rs b/tests/compat_browser_tool_test.rs index 7af5716..b9b8329 100644 --- a/tests/compat_browser_tool_test.rs +++ b/tests/compat_browser_tool_test.rs @@ -7,6 +7,7 @@ use common::MockTransport; use serde_json::{json, Value}; use sgclaw::security::MacPolicy; use sgclaw::{ + browser::{BrowserBackend, PipeBrowserBackend}, compat::browser_tool_adapter::ZeroClawBrowserTool, pipe::{Action, AgentMessage, BrowserMessage, BrowserPipeTool, ExecutionSurfaceKind, Timing}, }; @@ -28,7 +29,7 @@ fn test_policy() -> MacPolicy { fn build_adapter( messages: Vec, -) -> (Arc, ZeroClawBrowserTool) { +) -> (Arc, ZeroClawBrowserTool) { let transport = Arc::new(MockTransport::new(messages)); let browser_tool = BrowserPipeTool::new( transport.clone(), @@ -36,8 +37,9 @@ fn build_adapter( vec![1, 2, 3, 4, 5, 6, 7, 8], ) .with_response_timeout(Duration::from_secs(1)); + let backend: Arc = Arc::new(PipeBrowserBackend::from_inner(browser_tool)); - (transport, ZeroClawBrowserTool::new(browser_tool)) + (transport, ZeroClawBrowserTool::new(backend)) } #[test] diff --git a/tests/compat_config_test.rs b/tests/compat_config_test.rs index d952853..e8a0fe6 100644 --- a/tests/compat_config_test.rs +++ b/tests/compat_config_test.rs @@ -191,6 +191,60 @@ fn sgclaw_settings_load_new_runtime_fields_from_browser_config() { assert_eq!(config.skills.prompt_injection_mode, SkillsPromptMode::Full); } +#[test] +fn sgclaw_settings_load_browser_ws_url_from_browser_config() { + let root = std::env::temp_dir().join(format!("sgclaw-browser-ws-config-{}", Uuid::new_v4())); + fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + + fs::write( + &config_path, + r#"{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345" +}"#, + ) + .unwrap(); + + let settings = SgClawSettings::load(Some(config_path.as_path())) + .unwrap() + .expect("expected sgclaw settings from config file"); + + assert_eq!( + settings.browser_ws_url.as_deref(), + Some("ws://127.0.0.1:12345") + ); +} + +#[test] +fn sgclaw_settings_load_service_ws_listen_addr_from_browser_config() { + let root = std::env::temp_dir().join(format!("sgclaw-service-ws-config-{}", Uuid::new_v4())); + fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + + fs::write( + &config_path, + r#"{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "serviceWsListenAddr": "127.0.0.1:42321" +}"#, + ) + .unwrap(); + + let settings = SgClawSettings::load(Some(config_path.as_path())) + .unwrap() + .expect("expected sgclaw settings from config file"); + + assert_eq!( + settings.service_ws_listen_addr.as_deref(), + Some("127.0.0.1:42321") + ); +} + #[test] fn browser_attached_config_uses_low_temperature_for_deterministic_execution() { let settings = SgClawSettings::from_legacy_deepseek_fields( diff --git a/tests/compat_openxml_office_tool_test.rs b/tests/compat_openxml_office_tool_test.rs index a15dce7..8649b35 100644 --- a/tests/compat_openxml_office_tool_test.rs +++ b/tests/compat_openxml_office_tool_test.rs @@ -33,7 +33,8 @@ async fn openxml_office_tool_renders_hotlist_xlsx_from_rows() { assert!(result.success, "{result:?}"); assert!(output_path.exists()); - assert!(result.output.contains(output_path.to_str().unwrap())); + let payload: serde_json::Value = serde_json::from_str(&result.output).unwrap(); + assert_eq!(payload["output_path"], json!(output_path.to_str().unwrap())); let unzip = ProcessCommand::new("unzip") .args([ diff --git a/tests/compat_runtime_test.rs b/tests/compat_runtime_test.rs index 5e9e1fb..0bc4fa2 100644 --- a/tests/compat_runtime_test.rs +++ b/tests/compat_runtime_test.rs @@ -13,6 +13,7 @@ use serde_json::{json, Value}; use sgclaw::agent::{ handle_browser_message, handle_browser_message_with_context, AgentRuntimeContext, }; +use sgclaw::compat::workflow_executor::finalize_screen_export; use sgclaw::compat::runtime::{execute_task, execute_task_with_sgclaw_settings, CompatTaskContext}; use sgclaw::config::{DeepSeekSettings, SgClawSettings}; use sgclaw::pipe::{ @@ -176,6 +177,7 @@ fn start_fake_deepseek_server( Err(err) => panic!("failed to accept provider request: {err}"), } }; + stream.set_nonblocking(false).unwrap(); let body = read_http_json_body(&mut stream); request_log.lock().unwrap().push(body); @@ -1861,6 +1863,15 @@ fn handle_browser_message_exposes_real_zhihu_skill_lib_to_provider_request() { let request_bodies = requests.lock().unwrap().clone(); let first_request = request_bodies[0].to_string(); let tool_names = request_tool_names(&request_bodies[0]); + let loaded_skills_message = sent + .iter() + .find_map(|message| match message { + AgentMessage::LogEntry { level, message } if level == "info" && message.starts_with("loaded skills: ") => { + Some(message.clone()) + } + _ => None, + }) + .expect("expected loaded skills log entry"); assert!(sent.iter().any(|message| { matches!( @@ -1869,15 +1880,11 @@ fn handle_browser_message_exposes_real_zhihu_skill_lib_to_provider_request() { if *success && summary == "已看到真实知乎 skill" ) })); - assert!(sent.iter().any(|message| { - matches!( - message, - AgentMessage::LogEntry { level, message } - if level == "info" && - message == - "loaded skills: office-export-xlsx@0.1.0, zhihu-hotlist@0.1.0, zhihu-hotlist-screen@0.1.0, zhihu-navigate@0.1.0, zhihu-write@0.1.0" - ) - })); + assert!(loaded_skills_message.contains("office-export-xlsx@0.1.0")); + assert!(loaded_skills_message.contains("zhihu-hotlist@0.1.0")); + assert!(loaded_skills_message.contains("zhihu-hotlist-screen@0.1.0")); + assert!(loaded_skills_message.contains("zhihu-navigate@0.1.0")); + assert!(loaded_skills_message.contains("zhihu-write@0.1.0")); assert_eq!(request_bodies.len(), 1); assert!(first_request.contains("office-export-xlsx")); assert!(first_request.contains("zhihu-hotlist")); @@ -2107,145 +2114,9 @@ fn handle_browser_message_executes_real_zhihu_hotlist_skill_flow() { } #[test] -fn handle_browser_message_chains_hotlist_skill_into_office_export_tool() { - let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); - - let workspace_root = temp_workspace_root(); - let output_path = workspace_root.join("out/zhihu-hotlist.xlsx"); - let output_path_str = output_path.to_string_lossy().to_string(); - let first_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_1", - "type": "function", - "function": { - "name": "zhihu-hotlist_extract_hotlist", - "arguments": serde_json::to_string(&json!({ - "expected_domain": "www.zhihu.com", - "top_n": "10" - })).unwrap() - } - }] - } - }] - }); - let third_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_3", - "type": "function", - "function": { - "name": "openxml_office", - "arguments": serde_json::to_string(&json!({ - "sheet_name": "知乎热榜", - "columns": ["rank", "title", "heat"], - "rows": [ - [1, "问题一", "344万"], - [2, "问题二", "266万"] - ], - "output_path": output_path_str - })).unwrap() - } - }] - } - }] - }); - let fourth_response = json!({ - "choices": [{ - "message": { - "content": format!("已导出知乎热榜 Excel {output_path_str}") - } - }] - }); - let (base_url, _requests, server_handle) = - start_fake_deepseek_server(vec![first_response, third_response, fourth_response]); - let config_path = write_deepseek_config_with_skills_dir( - &workspace_root, - "deepseek-test-key", - &base_url, - "deepseek-chat", - Some(real_skill_lib_root().to_str().unwrap()), - ); - let runtime_context = AgentRuntimeContext::new(Some(config_path), workspace_root.clone()); - - let transport = Arc::new(MockTransport::new(vec![success_browser_response( - 1, - json!({ - "text": { - "source": "https://www.zhihu.com/hot", - "sheet_name": "知乎热榜", - "columns": ["rank", "title", "heat"], - "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] - } - }), - )])); - let browser_tool = BrowserPipeTool::new( - transport.clone(), - zhihu_test_policy(), - vec![1, 2, 3, 4, 5, 6, 7, 8], - ) - .with_response_timeout(Duration::from_secs(1)); - - handle_browser_message_with_context( - transport.as_ref(), - &browser_tool, - &runtime_context, - BrowserMessage::SubmitTask { - instruction: "读取知乎热榜数据,并导出 excel 文件".to_string(), - conversation_id: String::new(), - messages: vec![], - page_url: "https://www.zhihu.com/".to_string(), - page_title: "知乎".to_string(), - }, - ) - .unwrap(); - server_handle.join().unwrap(); - - let sent = transport.sent_messages(); - - assert!(sent.iter().any(|message| { - matches!( - message, - AgentMessage::TaskComplete { success, summary } - if *success && summary.contains("已导出知乎热榜 Excel") && summary.contains(".xlsx") - ) - })); - assert!(sent.iter().any(|message| { - matches!( - message, - AgentMessage::LogEntry { level, message } - if level == "mode" && message == "zeroclaw_process_message_primary" - ) - })); - assert!(sent.iter().any(|message| { - matches!( - message, - AgentMessage::LogEntry { level, message } - if level == "info" && message == "call zhihu-hotlist.extract_hotlist" - ) - })); - assert!(sent.iter().any(|message| { - matches!( - message, - AgentMessage::Command { action, .. } if action == &Action::Eval - ) - })); - assert!(!sent.iter().any(|message| { - matches!( - message, - AgentMessage::LogEntry { level, message } - if level == "mode" && (message == "compat_llm_primary" || message == "compat_skill_runner_primary") - ) - })); -} - -#[test] -fn handle_browser_message_chains_hotlist_skill_into_screen_export_tool() { +fn handle_browser_message_chains_hotlist_skill_into_xlsx_export_and_auto_open() { let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::set_var("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1"); let workspace_root = temp_workspace_root(); let config_path = write_deepseek_config_with_skills_dir( @@ -2282,6 +2153,118 @@ fn handle_browser_message_chains_hotlist_skill_into_screen_export_tool() { ) .with_response_timeout(Duration::from_secs(1)); + handle_browser_message_with_context( + transport.as_ref(), + &browser_tool, + &runtime_context, + BrowserMessage::SubmitTask { + instruction: "读取知乎热榜数据,并导出 excel 文件".to_string(), + conversation_id: String::new(), + messages: vec![], + page_url: "https://www.zhihu.com/".to_string(), + page_title: "知乎".to_string(), + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + let summary = task_complete_summary(&sent); + let generated = extract_generated_artifact_path(&summary, ".xlsx"); + + assert!(summary.contains("已导出并打开知乎热榜 Excel")); + assert!(summary.contains(".xlsx")); + assert!(generated.exists()); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::TaskComplete { success, summary } + if *success && summary.contains("已导出并打开知乎热榜 Excel") && summary.contains(".xlsx") + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && message == "zeroclaw_process_message_primary" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call zhihu-hotlist.extract_hotlist" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call openxml_office" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, .. } if action == &Action::Eval + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, params, .. } + if action == &Action::Navigate && params.get("sgclaw_local_dashboard_open").is_some() + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && (message == "compat_llm_primary" || message == "compat_skill_runner_primary") + ) + })); + std::env::remove_var("SGCLAW_DISABLE_POST_EXPORT_OPEN"); +} + +#[test] +fn handle_browser_message_chains_hotlist_skill_into_screen_export_and_auto_open() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + + let workspace_root = temp_workspace_root(); + let config_path = write_deepseek_config_with_skills_dir( + &workspace_root, + "deepseek-test-key", + "http://127.0.0.1:9", + "deepseek-chat", + Some(real_skill_lib_root().to_str().unwrap()), + ); + let runtime_context = AgentRuntimeContext::new(Some(config_path), workspace_root.clone()); + + let transport = Arc::new(MockTransport::new(vec![ + success_browser_response(1, json!({ "navigated": true })), + success_browser_response( + 2, + json!({ "text": "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度" }), + ), + success_browser_response( + 3, + json!({ + "text": { + "source": "https://www.zhihu.com/hot", + "sheet_name": "知乎热榜", + "columns": ["rank", "title", "heat"], + "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] + } + }), + ), + success_browser_response(4, json!({ "navigated": true })), + ])); + let browser_tool = BrowserPipeTool::new( + transport.clone(), + zhihu_test_policy(), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ) + .with_response_timeout(Duration::from_secs(1)); + handle_browser_message_with_context( transport.as_ref(), &browser_tool, @@ -2299,10 +2282,43 @@ fn handle_browser_message_chains_hotlist_skill_into_screen_export_tool() { let sent = transport.sent_messages(); let summary = task_complete_summary(&sent); let generated = extract_generated_artifact_path(&summary, ".html"); + let navigate = sent + .iter() + .find_map(|message| match message { + AgentMessage::Command { + action, + params, + security, + .. + } if action == &Action::Navigate + && security.expected_domain == "__sgclaw_local_dashboard__" => Some((params, security)), + _ => None, + }) + .expect("dashboard route should emit local-dashboard navigate request"); - assert!(summary.contains("已生成知乎热榜大屏")); + assert!(summary.contains("已在浏览器中打开知乎热榜大屏")); assert!(summary.contains(".html")); assert!(generated.exists()); + assert_eq!( + navigate.0["sgclaw_local_dashboard_open"]["output_path"].as_str(), + generated.to_str() + ); + assert!(navigate.0["url"] + .as_str() + .expect("dashboard open url should be present") + .starts_with("file://")); + assert_eq!( + navigate.0["sgclaw_local_dashboard_open"]["source"], + json!("compat.workflow_executor") + ); + assert_eq!( + navigate.0["sgclaw_local_dashboard_open"]["kind"], + json!("zhihu_hotlist_screen") + ); + assert_eq!( + navigate.0["sgclaw_local_dashboard_open"]["presentation_url"], + navigate.0["url"] + ); assert!(sent.iter().any(|message| { matches!( message, @@ -2330,6 +2346,13 @@ fn handle_browser_message_chains_hotlist_skill_into_screen_export_tool() { AgentMessage::Command { action, .. } if action == &Action::Eval ) })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call openxml_office" + ) + })); assert!(!sent.iter().any(|message| { matches!( message, @@ -2339,9 +2362,55 @@ fn handle_browser_message_chains_hotlist_skill_into_screen_export_tool() { })); } +#[test] +fn handle_browser_message_reports_dashboard_auto_open_protocol_error_when_presentation_url_is_missing() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = BrowserPipeTool::new( + transport.clone(), + zhihu_test_policy(), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ) + .with_response_timeout(Duration::from_secs(1)); + let browser_backend = sgclaw::browser::PipeBrowserBackend::from_inner(browser_tool); + + let workspace_root = temp_workspace_root(); + let output_path = workspace_root.join("zhihu-hotlist-screen.html"); + fs::write(&output_path, "fixture").unwrap(); + let payload = json!({ + "title": "知乎热榜大屏", + "output_path": output_path, + "renderer": "screen_html_export", + "row_count": 2, + "snapshot_id": "snapshot-test", + "presentation": { + "mode": "new_tab", + "title": "知乎热榜大屏", + "open_in_new_tab": true + } + }); + + let summary = finalize_screen_export(&browser_backend, &payload.to_string()).unwrap(); + + assert!(summary.contains("已生成知乎热榜大屏")); + assert!(summary.contains(output_path.to_string_lossy().as_ref())); + assert!(summary.contains("但浏览器自动打开失败:screen_html_export did not return presentation.url")); + + let sent = transport.sent_messages(); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, params, .. } + if action == &Action::Navigate && params.get("sgclaw_local_dashboard_open").is_some() + ) + })); +} + #[test] fn handle_browser_message_runs_zhihu_hotlist_export_via_zeroclaw_primary_orchestration() { let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::set_var("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1"); let workspace_root = temp_workspace_root(); let config_path = write_deepseek_config_with_skills_dir( @@ -2416,6 +2485,7 @@ fn handle_browser_message_runs_zhihu_hotlist_export_via_zeroclaw_primary_orchest if level == "mode" && (message == "compat_llm_primary" || message == "compat_skill_runner_primary") ) })); + std::env::remove_var("SGCLAW_DISABLE_POST_EXPORT_OPEN"); } #[test] @@ -2527,6 +2597,143 @@ fn browser_submit_path_prefers_zeroclaw_process_message_orchestrator_for_zhihu_p })); } +#[test] +fn zhihu_generated_auto_publish_matches_primary_orchestration_gate() { + assert!( + sgclaw::compat::orchestration::should_use_primary_orchestration( + "在知乎自动发表一篇名称为人工智能技能大全", + Some("https://www.zhihu.com/"), + Some("知乎"), + ) + ); +} + +#[test] +fn zhihu_hotlist_export_route_stays_ahead_of_generated_article_publish() { + use sgclaw::compat::workflow_executor::{detect_route, WorkflowRoute}; + + assert_eq!( + detect_route( + "打开知乎热榜,获取前10条数据,并导出 Excel", + Some("https://www.zhihu.com/"), + Some("知乎") + ), + Some(WorkflowRoute::ZhihuHotlistExportXlsx) + ); +} + +#[test] +fn zhihu_generated_auto_publish_uses_provider_and_submits_publish_without_confirmation() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + + let response = json!({ + "choices": [{ + "message": { + "content": "标题:人工智能技能大全\n正文:第一段内容。\n\n第二段内容。" + } + }] + }); + let (base_url, requests, server_handle) = start_fake_deepseek_server(vec![response]); + + let workspace_root = temp_workspace_root(); + let config_path = write_deepseek_config_with_skills_dir( + &workspace_root, + "deepseek-test-key", + &base_url, + "deepseek-chat", + Some(real_skill_lib_root().to_str().unwrap()), + ); + let runtime_context = AgentRuntimeContext::new(Some(config_path), workspace_root.clone()); + + let transport = Arc::new(MockTransport::new(vec![ + success_browser_response(1, json!({ "navigated": true })), + success_browser_response( + 2, + json!({ + "text": { + "status": "creator_entry_clicked", + "current_url": "https://www.zhihu.com/creator", + "next_url": "https://zhuanlan.zhihu.com/write" + } + }), + ), + success_browser_response(3, json!({ "navigated": true })), + success_browser_response( + 4, + json!({ + "text": { + "status": "editor_ready", + "current_url": "https://zhuanlan.zhihu.com/write" + } + }), + ), + success_browser_response( + 5, + json!({ + "text": { + "status": "publish_submitted", + "current_url": "https://zhuanlan.zhihu.com/write", + "title": "人工智能技能大全" + } + }), + ), + ])); + let browser_tool = BrowserPipeTool::new( + transport.clone(), + zhihu_test_policy(), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ) + .with_response_timeout(Duration::from_secs(1)); + + handle_browser_message_with_context( + transport.as_ref(), + &browser_tool, + &runtime_context, + BrowserMessage::SubmitTask { + instruction: "在知乎自动发表一篇名称为人工智能技能大全".to_string(), + conversation_id: String::new(), + messages: vec![], + page_url: "https://www.zhihu.com/".to_string(), + page_title: "知乎".to_string(), + }, + ) + .unwrap(); + server_handle.join().unwrap(); + + let sent = transport.sent_messages(); + let request_bodies = requests.lock().unwrap().clone(); + + assert_eq!(request_bodies.len(), 1); + assert!(request_bodies[0].to_string().contains("人工智能技能大全")); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::TaskComplete { success, summary } + if *success && summary == "已提交知乎文章发布流程《人工智能技能大全》" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call zhihu-write.fill_article_draft" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, .. } if action == &Action::Navigate + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::TaskComplete { success, summary } + if *success && summary.contains("确认发布") + ) + })); +} + #[test] fn zhihu_publish_task_matches_primary_orchestration_gate() { assert!( @@ -3078,71 +3285,37 @@ fn zhihu_publish_after_confirmation_reports_login_block_without_selector_probing } #[test] -fn browser_orchestration_registers_superrpa_tools_natively() { +fn browser_orchestration_executes_hotlist_export_natively_from_hotlist_page() { let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); - - let first_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_1", - "type": "function", - "function": { - "name": "superrpa_browser", - "arguments": serde_json::to_string(&json!({ - "action": "getText", - "expected_domain": "www.zhihu.com", - "selector": "main" - })).unwrap() - } - }] - } - }] - }); - let second_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_2", - "type": "function", - "function": { - "name": "openxml_office", - "arguments": serde_json::to_string(&json!({ - "sheet_name": "知乎热榜", - "columns": ["rank", "title", "heat"], - "rows": [[1, "问题一", "344万"]] - })).unwrap() - } - }] - } - }] - }); - let third_response = json!({ - "choices": [{ - "message": { - "content": "已导出知乎热榜 Excel" - } - }] - }); - let (base_url, requests, server_handle) = - start_fake_deepseek_server(vec![first_response, second_response, third_response]); + std::env::set_var("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1"); let workspace_root = temp_workspace_root(); let config_path = write_deepseek_config_with_skills_dir( &workspace_root, "deepseek-test-key", - &base_url, + "http://127.0.0.1:9", "deepseek-chat", Some(real_skill_lib_root().to_str().unwrap()), ); let runtime_context = AgentRuntimeContext::new(Some(config_path), workspace_root.clone()); - let transport = Arc::new(MockTransport::new(vec![success_browser_response( - 1, - json!({ "text": "知乎热榜\n1\n问题一\n344万热度" }), - )])); + let transport = Arc::new(MockTransport::new(vec![ + success_browser_response( + 1, + json!({ "text": "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度" }), + ), + success_browser_response( + 2, + json!({ + "text": { + "source": "https://www.zhihu.com/hot", + "sheet_name": "知乎热榜", + "columns": ["rank", "title", "heat"], + "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] + } + }), + ), + ])); let browser_tool = BrowserPipeTool::new( transport.clone(), zhihu_test_policy(), @@ -3164,22 +3337,60 @@ fn browser_orchestration_registers_superrpa_tools_natively() { ) .unwrap(); - let request_bodies = requests.lock().unwrap().clone(); let sent = transport.sent_messages(); - assert!( - !request_bodies.is_empty(), - "expected provider request, sent messages were: {sent:?}" - ); - server_handle.join().unwrap(); - let first_request = request_bodies - .first() - .expect("expected first provider request") - .to_string(); - let tool_names = request_tool_names(&request_bodies[0]); + let summary = task_complete_summary(&sent); + let generated = extract_generated_artifact_path(&summary, ".xlsx"); - assert!(first_request.contains("superrpa_browser")); - assert!(tool_names.contains(&"superrpa_browser".to_string())); - assert!(tool_names.contains(&"openxml_office".to_string())); + assert!(summary.contains(".xlsx")); + assert!(generated.exists()); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && message == "zeroclaw_process_message_primary" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call zhihu-hotlist.extract_hotlist" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call openxml_office" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, .. } if action == &Action::GetText + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, .. } if action == &Action::Eval + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::Command { action, .. } if action == &Action::Navigate + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && + (message == "compat_llm_primary" || message == "compat_skill_runner_primary") + ) + })); + std::env::remove_var("SGCLAW_DISABLE_POST_EXPORT_OPEN"); } #[test] @@ -3240,88 +3451,13 @@ fn zhihu_export_does_not_use_frontend_owned_mainline() { #[test] fn browser_skill_usage_is_execution_not_prompt_only() { let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::set_var("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1"); let workspace_root = temp_workspace_root(); - let output_path = workspace_root.join("out/zhihu-hotlist-execution.xlsx"); - let output_path_str = output_path.to_string_lossy().to_string(); - let first_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_1", - "type": "function", - "function": { - "name": "superrpa_browser", - "arguments": serde_json::to_string(&json!({ - "action": "navigate", - "expected_domain": "www.zhihu.com", - "url": "https://www.zhihu.com/hot" - })).unwrap() - } - }] - } - }] - }); - let second_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_2", - "type": "function", - "function": { - "name": "superrpa_browser", - "arguments": serde_json::to_string(&json!({ - "action": "getText", - "expected_domain": "www.zhihu.com", - "selector": "main" - })).unwrap() - } - }] - } - }] - }); - let third_response = json!({ - "choices": [{ - "message": { - "content": "", - "tool_calls": [{ - "id": "call_3", - "type": "function", - "function": { - "name": "openxml_office", - "arguments": serde_json::to_string(&json!({ - "sheet_name": "知乎热榜", - "columns": ["rank", "title", "heat"], - "rows": [ - [1, "问题一", "344万"], - [2, "问题二", "266万"] - ], - "output_path": output_path_str - })).unwrap() - } - }] - } - }] - }); - let fourth_response = json!({ - "choices": [{ - "message": { - "content": format!("已导出知乎热榜 Excel {output_path_str}") - } - }] - }); - let (base_url, requests, server_handle) = start_fake_deepseek_server(vec![ - first_response, - second_response, - third_response, - fourth_response, - ]); let config_path = write_deepseek_config_with_skills_dir( &workspace_root, "deepseek-test-key", - &base_url, + "http://127.0.0.1:9", "deepseek-chat", Some(real_skill_lib_root().to_str().unwrap()), ); @@ -3331,7 +3467,18 @@ fn browser_skill_usage_is_execution_not_prompt_only() { success_browser_response(1, json!({ "navigated": true })), success_browser_response( 2, - json!({ "text": "知乎热榜\n1\n问题一\n344万热度\n2\n问题二\n266万热度" }), + json!({ "text": "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度" }), + ), + success_browser_response( + 3, + json!({ + "text": { + "source": "https://www.zhihu.com/hot", + "sheet_name": "知乎热榜", + "columns": ["rank", "title", "heat"], + "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] + } + }), ), ])); let browser_tool = BrowserPipeTool::new( @@ -3354,15 +3501,13 @@ fn browser_skill_usage_is_execution_not_prompt_only() { }, ) .unwrap(); - server_handle.join().unwrap(); - let request_bodies = requests.lock().unwrap().clone(); let sent = transport.sent_messages(); - let first_request = request_bodies - .first() - .expect("expected first provider request") - .to_string(); + let summary = task_complete_summary(&sent); + let generated = extract_generated_artifact_path(&summary, ".xlsx"); + assert!(summary.contains(".xlsx")); + assert!(generated.exists()); assert!(sent.iter().any(|message| { matches!( message, @@ -3370,6 +3515,29 @@ fn browser_skill_usage_is_execution_not_prompt_only() { if *success && summary.contains(".xlsx") ) })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && message == "zeroclaw_process_message_primary" + ) + })); + assert!(sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "info" && message == "call openxml_office" + ) + })); + assert!(!sent.iter().any(|message| { + matches!( + message, + AgentMessage::LogEntry { level, message } + if level == "mode" && + (message == "compat_llm_primary" || message == "compat_skill_runner_primary") + ) + })); + std::env::remove_var("SGCLAW_DISABLE_POST_EXPORT_OPEN"); assert!(!sent.iter().any(|message| { matches!( message, @@ -3387,7 +3555,6 @@ fn browser_skill_usage_is_execution_not_prompt_only() { message == "getText ol li") ) })); - assert!(!first_request.contains("Preloaded skill context:")); } #[test] diff --git a/tests/compat_screen_html_export_tool_test.rs b/tests/compat_screen_html_export_tool_test.rs index 155dae4..59b214a 100644 --- a/tests/compat_screen_html_export_tool_test.rs +++ b/tests/compat_screen_html_export_tool_test.rs @@ -47,4 +47,13 @@ async fn screen_html_export_tool_renders_dashboard_html_with_presentation_contra assert!(html.contains("问题一")); assert!(html.contains("344万")); assert!(html.contains("const defaultPayload =")); + assert!(html.contains("汇报摘要")); + assert!(html.contains("fitScreenToViewport")); + assert!(html.contains("dashboard-canvas")); + assert!(html.contains("themeSwitcher")); + assert!(html.contains("gov_blue_gold")); + assert!(html.contains("tech_cyan_blue")); + assert!(html.contains("industry_ink_green")); + assert!(html.contains("meeting_red_gold")); + assert!(html.contains("localStorage.setItem(\"zhihu-hotlist-theme\"")); } diff --git a/tests/pipe_protocol_test.rs b/tests/pipe_protocol_test.rs index 2acdc6c..95bdbb9 100644 --- a/tests/pipe_protocol_test.rs +++ b/tests/pipe_protocol_test.rs @@ -21,6 +21,24 @@ fn browser_init_round_trip_uses_frozen_wire_format() { assert_eq!(serde_json::to_string(&message).unwrap(), raw); } +#[test] +fn browser_lifecycle_messages_use_frozen_wire_tags() { + let connect_raw = r#"{"type":"connect"}"#; + let start_raw = r#"{"type":"start"}"#; + let stop_raw = r#"{"type":"stop"}"#; + + let connect: BrowserMessage = serde_json::from_str(connect_raw).unwrap(); + let start: BrowserMessage = serde_json::from_str(start_raw).unwrap(); + let stop: BrowserMessage = serde_json::from_str(stop_raw).unwrap(); + + assert_eq!(connect, BrowserMessage::Connect); + assert_eq!(start, BrowserMessage::Start); + assert_eq!(stop, BrowserMessage::Stop); + assert_eq!(serde_json::to_string(&connect).unwrap(), connect_raw); + assert_eq!(serde_json::to_string(&start).unwrap(), start_raw); + assert_eq!(serde_json::to_string(&stop).unwrap(), stop_raw); +} + #[test] fn command_serializes_action_and_security_fields() { let message = AgentMessage::Command { @@ -40,6 +58,16 @@ fn command_serializes_action_and_security_fields() { assert!(raw.contains(r#""expected_domain":"oa.example.com""#)); } +#[test] +fn agent_status_changed_serializes_with_expected_tag() { + let raw = serde_json::to_string(&AgentMessage::StatusChanged { + state: "started".to_string(), + }) + .unwrap(); + + assert_eq!(raw, r#"{"type":"status_changed","state":"started"}"#); +} + #[test] fn response_deserializes_timing_and_payload() { let raw = r#"{"type":"response","seq":7,"success":true,"data":{"text":"提交成功"},"aom_snapshot":[],"timing":{"queue_ms":2,"exec_ms":38}}"#; diff --git a/tests/planner_test.rs b/tests/planner_test.rs deleted file mode 100644 index 515ba78..0000000 --- a/tests/planner_test.rs +++ /dev/null @@ -1,132 +0,0 @@ -use serde_json::json; -use sgclaw::agent::planner::{build_execution_preview, plan_instruction, PlannerError}; -use sgclaw::config::PlannerMode; -use sgclaw::pipe::Action; - -#[test] -fn planner_module_is_explicitly_legacy_dev_only() { - assert!(sgclaw::agent::planner::LEGACY_DEV_ONLY); -} - -#[test] -fn planner_converts_baidu_search_instruction_into_three_steps() { - let plan = plan_instruction("打开百度搜索天气").unwrap(); - - assert_eq!(plan.summary, "已在百度搜索天气"); - assert_eq!(plan.steps.len(), 3); - assert_eq!(plan.steps[0].action, Action::Navigate); - assert_eq!( - plan.steps[0].params, - json!({ "url": "https://www.baidu.com" }) - ); - assert_eq!(plan.steps[1].action, Action::Type); - assert_eq!( - plan.steps[1].params, - json!({ "selector": "#kw", "text": "天气", "clear_first": true }) - ); - assert_eq!(plan.steps[2].action, Action::Click); - assert_eq!(plan.steps[2].params, json!({ "selector": "#su" })); -} - -#[test] -fn planner_supports_baidu_search_variant_with_conjunction() { - let plan = plan_instruction("打开百度并搜索电网调度").unwrap(); - - assert_eq!(plan.summary, "已在百度搜索电网调度"); - assert_eq!(plan.steps[1].params["text"], "电网调度"); -} - -#[test] -fn planner_supports_zhihu_search_instruction_with_direct_search_url() { - let plan = plan_instruction("打开知乎搜索天气").unwrap(); - - assert_eq!(plan.summary, "已在知乎搜索天气"); - assert_eq!(plan.steps.len(), 1); - assert_eq!(plan.steps[0].action, Action::Navigate); - assert_eq!( - plan.steps[0].params, - json!({ "url": "https://www.zhihu.com/search?type=content&q=%E5%A4%A9%E6%B0%94" }) - ); - assert_eq!(plan.steps[0].expected_domain, "www.zhihu.com"); - assert_eq!( - plan.steps[0].log_message, - "navigate https://www.zhihu.com/search?type=content&q=%E5%A4%A9%E6%B0%94" - ); -} - -#[test] -fn planner_supports_open_zhihu_homepage_instruction() { - let plan = plan_instruction("打开知乎").unwrap(); - - assert_eq!(plan.summary, "已打开知乎首页"); - assert_eq!(plan.steps.len(), 1); - assert_eq!(plan.steps[0].action, Action::Navigate); - assert_eq!( - plan.steps[0].params, - json!({ "url": "https://www.zhihu.com" }) - ); - assert_eq!(plan.steps[0].expected_domain, "www.zhihu.com"); - assert_eq!(plan.steps[0].log_message, "navigate https://www.zhihu.com"); -} - -#[test] -fn planner_supports_open_baidu_homepage_instruction() { - let plan = plan_instruction("打开百度").unwrap(); - - assert_eq!(plan.summary, "已打开百度首页"); - assert_eq!(plan.steps.len(), 1); - assert_eq!(plan.steps[0].action, Action::Navigate); - assert_eq!( - plan.steps[0].params, - json!({ "url": "https://www.baidu.com" }) - ); - assert_eq!(plan.steps[0].expected_domain, "www.baidu.com"); - assert_eq!(plan.steps[0].log_message, "navigate https://www.baidu.com"); -} - -#[test] -fn planner_rejects_unrelated_instruction() { - let err = plan_instruction("打开谷歌搜索天气").unwrap_err(); - - assert_eq!( - err, - PlannerError::UnsupportedInstruction("打开谷歌搜索天气".to_string()) - ); -} - -#[test] -fn plan_first_mode_builds_visible_preview_for_zhihu_excel_flow() { - let preview = build_execution_preview( - PlannerMode::ZeroclawPlanFirst, - "读取知乎热榜数据,并导出 excel 文件", - Some("https://www.zhihu.com/hot"), - Some("知乎热榜"), - ) - .expect("expected plan preview"); - - assert_eq!(preview.summary, "先规划再执行知乎热榜 Excel 导出"); - assert!(preview - .steps - .iter() - .any(|step| step.contains("navigate https://www.zhihu.com/hot"))); - assert!(preview - .steps - .iter() - .any(|step| step.contains("getText main"))); - assert!(preview - .steps - .iter() - .any(|step| step.contains("call openxml_office"))); -} - -#[test] -fn legacy_planner_mode_skips_runtime_preview() { - let preview = build_execution_preview( - PlannerMode::LegacyDeterministic, - "打开百度搜索天气", - None, - None, - ); - - assert!(preview.is_none()); -} diff --git a/tests/service_task_flow_test.rs b/tests/service_task_flow_test.rs new file mode 100644 index 0000000..297f016 --- /dev/null +++ b/tests/service_task_flow_test.rs @@ -0,0 +1,922 @@ +use std::io::{BufRead, BufReader, Read as _, Write}; +use std::net::TcpListener; +use std::sync::{mpsc, Arc, Mutex}; +use std::thread; +use std::time::{Duration, Instant}; + +use reqwest::blocking::Client; +use serde_json::{json, Value}; +use sgclaw::service::{ClientMessage, ServiceMessage}; +use tungstenite::{accept, Message}; + +const RUNTIME_DROP_PANIC_TEXT: &str = + "Cannot drop a runtime in a context where blocking is not allowed"; + +fn read_ws_text(stream: &mut tungstenite::WebSocket) -> String { + match stream.read().unwrap() { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + } +} + +fn start_fake_deepseek_server( + responses: Vec, +) -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let address = format!("http://{}", listener.local_addr().unwrap()); + let requests = Arc::new(Mutex::new(Vec::new())); + let request_log = requests.clone(); + + let handle = thread::spawn(move || { + for response in responses { + let deadline = std::time::Instant::now() + Duration::from_secs(5); + let (mut stream, _) = loop { + match listener.accept() { + Ok(pair) => break pair, + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + assert!( + std::time::Instant::now() < deadline, + "timed out waiting for provider request" + ); + thread::sleep(Duration::from_millis(10)); + } + Err(err) => panic!("failed to accept provider request: {err}"), + } + }; + stream.set_nonblocking(false).unwrap(); + let body = match read_http_json_body(&mut stream) { + Ok(body) => body, + Err(_) => continue, + }; + request_log.lock().unwrap().push(body); + + let payload = response.to_string(); + let reply = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + payload.as_bytes().len(), + payload + ); + stream.write_all(reply.as_bytes()).unwrap(); + stream.flush().unwrap(); + } + }); + + (address, requests, handle) +} + +fn read_http_json_body(stream: &mut impl std::io::Read) -> Result { + let mut buffer = Vec::new(); + let mut headers_end = None; + + while headers_end.is_none() { + let mut chunk = [0_u8; 1024]; + let bytes = stream.read(&mut chunk).unwrap(); + if bytes == 0 { + return Err("unexpected EOF while reading headers"); + } + buffer.extend_from_slice(&chunk[..bytes]); + headers_end = buffer.windows(4).position(|window| window == b"\r\n\r\n"); + } + + let headers_end = headers_end.unwrap() + 4; + let headers = String::from_utf8(buffer[..headers_end].to_vec()).unwrap(); + let Some(content_length) = headers.lines().find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().unwrap()) + }) else { + return Err("missing content-length header"); + }; + + while buffer.len() < headers_end + content_length { + let mut chunk = vec![0_u8; content_length]; + let bytes = stream.read(&mut chunk).unwrap(); + if bytes == 0 { + return Err("unexpected EOF while reading body"); + } + buffer.extend_from_slice(&chunk[..bytes]); + } + + Ok(serde_json::from_slice(&buffer[headers_end..headers_end + content_length]).unwrap()) +} + +#[derive(Debug)] +enum CallbackHostBrowserEvent { + BrowserFrame(Value), + CommandEnvelope(Value), +} + +fn start_callback_host_hotlist_browser_server( + event_tx: mpsc::Sender, +) -> (String, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); + stream.set_write_timeout(Some(Duration::from_secs(2))).unwrap(); + let mut websocket = accept(stream).unwrap(); + + let register = match websocket.read().unwrap() { + Message::Text(text) => serde_json::from_str::(&text).unwrap(), + other => panic!("expected register frame, got {other:?}"), + }; + event_tx + .send(CallbackHostBrowserEvent::BrowserFrame(register)) + .unwrap(); + websocket + .send(Message::Text( + r#"{"type":"welcome","client_id":1,"server_time":"2026-04-04T00:00:00"}"# + .to_string() + .into(), + )) + .unwrap(); + + let first_action = match websocket.read().unwrap() { + Message::Text(text) => serde_json::from_str::(&text).unwrap(), + other => panic!("expected browser action frame, got {other:?}"), + }; + event_tx + .send(CallbackHostBrowserEvent::BrowserFrame(first_action.clone())) + .unwrap(); + + let Some(values) = first_action.as_array() else { + websocket.close(None).ok(); + return; + }; + let is_helper_open = values.len() >= 3 + && values[1] == json!("sgBrowerserOpenPage") + && values[2] + .as_str() + .is_some_and(|url| url.ends_with("/sgclaw/browser-helper.html")); + if !is_helper_open { + websocket.close(None).ok(); + return; + } + + let helper_url = values[2].as_str().unwrap().to_string(); + let helper_origin = helper_url + .trim_end_matches("/sgclaw/browser-helper.html") + .to_string(); + let helper_client = Client::builder() + .timeout(Duration::from_secs(2)) + .build() + .unwrap(); + let helper_html = helper_client + .get(&helper_url) + .send() + .unwrap() + .error_for_status() + .unwrap() + .text() + .unwrap(); + assert!(helper_html.contains("sgclawReady")); + assert!(helper_html.contains("sgclawOnLoaded")); + assert!(helper_html.contains("sgclawOnGetText")); + assert!(helper_html.contains("sgclawOnEval")); + + let pre_ready_command: Value = helper_client + .get(format!("{helper_origin}/sgclaw/callback/commands/next")) + .send() + .unwrap() + .error_for_status() + .unwrap() + .json() + .unwrap(); + event_tx + .send(CallbackHostBrowserEvent::CommandEnvelope(pre_ready_command)) + .unwrap(); + + helper_client + .post(format!("{helper_origin}/sgclaw/callback/ready")) + .json(&json!({ + "type": "ready", + "helper_url": helper_url, + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + + let hotlist_text = "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度"; + let hotlist_payload = json!({ + "source": "https://www.zhihu.com/hot", + "sheet_name": "知乎热榜", + "columns": ["rank", "title", "heat"], + "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] + }) + .to_string(); + let deadline = Instant::now() + Duration::from_secs(10); + let mut saw_get_text = false; + let mut saw_eval = false; + + while Instant::now() < deadline { + let envelope: Value = helper_client + .get(format!("{helper_origin}/sgclaw/callback/commands/next")) + .send() + .unwrap() + .error_for_status() + .unwrap() + .json() + .unwrap(); + let Some(command) = envelope.get("command").and_then(Value::as_object) else { + thread::sleep(Duration::from_millis(20)); + continue; + }; + event_tx + .send(CallbackHostBrowserEvent::CommandEnvelope(envelope.clone())) + .unwrap(); + + let action_name = command + .get("action") + .and_then(Value::as_str) + .unwrap_or_default() + .to_string(); + + helper_client + .post(format!("{helper_origin}/sgclaw/callback/commands/ack")) + .json(&json!({ "type": "command_ack" })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + + let args = command + .get("args") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default(); + match action_name.as_str() { + "sgBrowerserOpenPage" => {} + "sgBrowserExcuteJsCodeByDomain" => { + let script = args.get(1).and_then(Value::as_str).unwrap_or_default(); + if script.contains("sgclawOnGetText") { + saw_get_text = true; + helper_client + .post(format!("{helper_origin}/sgclaw/callback/events")) + .json(&json!({ + "callback": "sgclawOnGetText", + "request_url": helper_url, + "target_url": "https://www.zhihu.com/hot", + "action": action_name, + "payload": { "text": hotlist_text } + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + } else if script.contains("sgclawOnEval") { + saw_eval = true; + helper_client + .post(format!("{helper_origin}/sgclaw/callback/events")) + .json(&json!({ + "callback": "sgclawOnEval", + "request_url": helper_url, + "target_url": "https://www.zhihu.com/hot", + "action": action_name, + "payload": { "value": hotlist_payload } + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + break; + } else { + panic!("unexpected callback-host domain command: {script}"); + } + } + other => panic!("unexpected callback-host command action {other}"), + } + } + + assert!(saw_get_text, "expected callback-host getText command"); + assert!(saw_eval, "expected callback-host eval command"); + websocket.close(None).ok(); + }); + + (format!("ws://{address}"), handle) +} + +fn start_direct_zhihu_browser_ws_server() -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let frames = Arc::new(Mutex::new(Vec::new())); + let frames_for_thread = Arc::clone(&frames); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + stream.set_write_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut socket = accept(stream).unwrap(); + let mut action_count = 0_u64; + + loop { + let message = match socket.read() { + Ok(message) => message, + Err(tungstenite::Error::ConnectionClosed) + | Err(tungstenite::Error::AlreadyClosed) => break, + Err(err) => panic!("browser ws test server read failed: {err}"), + }; + let payload = match message { + Message::Text(text) => text.to_string(), + Message::Ping(payload) => { + socket.send(Message::Pong(payload)).unwrap(); + continue; + } + Message::Close(_) => break, + other => panic!("expected text frame, got {other:?}"), + }; + frames_for_thread.lock().unwrap().push(payload.clone()); + + let parsed: Value = serde_json::from_str(&payload).unwrap(); + if parsed.get("type").and_then(Value::as_str) == Some("register") { + continue; + } + + let values = parsed.as_array().expect("browser action frame should be an array"); + let request_url = values[0].as_str().expect("request_url should be a string"); + let action = values[1].as_str().expect("action should be a string"); + action_count += 1; + + socket + .send(Message::Text( + r#"{"type":"welcome","client_id":1,"server_time":"2026-04-04T00:00:00"}"# + .to_string() + .into(), + )) + .unwrap(); + socket.send(Message::Text("0".into())).unwrap(); + + let callback_frame = match action { + "sgHideBrowserCallAfterLoaded" => { + let target_url = values[2].as_str().expect("navigate target_url should be a string"); + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgHideBrowserCallAfterLoaded@_@" + ) + ]) + } + "sgBrowserExcuteJsCodeByArea" => { + let target_url = values[2].as_str().expect("script target_url should be a string"); + let response_text = if action_count == 2 { + "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度".to_string() + } else { + r#"{"source":"https://www.zhihu.com/hot","sheet_name":"知乎热榜","columns":["rank","title","heat"],"rows":[[1,"问题一","344万"],[2,"问题二","266万"]]}"#.to_string() + }; + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgBrowserExcuteJsCodeByArea@_@{response_text}" + ) + ]) + } + other => panic!("unexpected browser action {other}"), + }; + + socket + .send(Message::Text(callback_frame.to_string().into())) + .unwrap(); + + if action_count >= 3 { + break; + } + } + + socket.close(None).ok(); + }); + + (format!("ws://{address}"), frames, handle) +} + +#[test] +fn client_submits_first_user_line_to_service() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::TaskComplete { + success: true, + summary: "done".to_string(), + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket.close(None).unwrap(); + request + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all("打开百度搜索天气\n".as_bytes()) + .unwrap(); + + let status = child.wait().unwrap(); + assert!(status.success()); + + let request = server.join().unwrap(); + assert_eq!( + request, + ClientMessage::SubmitTask { + instruction: "打开百度搜索天气".to_string(), + conversation_id: "".to_string(), + messages: vec![], + page_url: "".to_string(), + page_title: "".to_string(), + } + ); +} + +#[test] +fn client_sends_connect_request_and_exits_after_status() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::StatusChanged { + state: "connected".to_string(), + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::StatusChanged { + state: "connected again".to_string(), + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket.close(None).unwrap(); + request + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all("/connect\n".as_bytes()) + .unwrap(); + + let output = child.wait_with_output().unwrap(); + let request = server.join().unwrap(); + + assert!(output.status.success()); + assert_eq!(request, ClientMessage::Connect); + let stdout = String::from_utf8(output.stdout).unwrap(); + assert_eq!(stdout.lines().collect::>(), vec!["status: connected"]); +} + +#[test] +fn client_sends_start_and_stop_requests_with_explicit_commands() { + for (input, expected_request, expected_status) in [ + ("/start\n", ClientMessage::Start, "status: started"), + ("/stop\n", ClientMessage::Stop, "status: stopped"), + ] { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + let expected_state = expected_status.trim_start_matches("status: ").to_string(); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::StatusChanged { + state: expected_state, + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket.close(None).unwrap(); + request + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all(input.as_bytes()) + .unwrap(); + + let output = child.wait_with_output().unwrap(); + let request = server.join().unwrap(); + + assert!(output.status.success()); + assert_eq!(request, expected_request); + let stdout = String::from_utf8(output.stdout).unwrap(); + assert_eq!(stdout.lines().collect::>(), vec![expected_status]); + } +} + +#[test] +fn client_prints_completion_only_once() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + assert_eq!(request.into_submit_task_request().unwrap().instruction, "打开百度搜索天气"); + + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::TaskComplete { + success: true, + summary: "done".to_string(), + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket + .send(Message::Text( + serde_json::to_string(&ServiceMessage::TaskComplete { + success: true, + summary: "done again".to_string(), + }) + .unwrap() + .into(), + )) + .unwrap(); + websocket.close(None).unwrap(); + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all("打开百度搜索天气\n".as_bytes()) + .unwrap(); + + let output = child.wait_with_output().unwrap(); + server.join().unwrap(); + + assert!(output.status.success()); + let stdout = String::from_utf8(output.stdout).unwrap(); + assert_eq!(stdout.lines().collect::>(), vec!["done"]); +} + +#[test] +fn client_prints_log_entries_in_order_before_completion() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + assert_eq!(request.into_submit_task_request().unwrap().instruction, "打开百度搜索天气"); + + for message in [ + ServiceMessage::LogEntry { + level: "info".to_string(), + message: "step 1".to_string(), + }, + ServiceMessage::LogEntry { + level: "info".to_string(), + message: "step 2".to_string(), + }, + ServiceMessage::TaskComplete { + success: true, + summary: "done".to_string(), + }, + ] { + websocket + .send(Message::Text(serde_json::to_string(&message).unwrap().into())) + .unwrap(); + } + websocket.close(None).unwrap(); + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all("打开百度搜索天气\n".as_bytes()) + .unwrap(); + + let stdout = child.stdout.take().unwrap(); + let (tx, rx) = mpsc::channel(); + let reader = thread::spawn(move || { + let reader = BufReader::new(stdout); + for line in reader.lines() { + tx.send(line.unwrap()).unwrap(); + } + }); + + let first = rx.recv_timeout(Duration::from_secs(1)).unwrap(); + let second = rx.recv_timeout(Duration::from_secs(1)).unwrap(); + let third = rx.recv_timeout(Duration::from_secs(1)).unwrap(); + + let status = child.wait().unwrap(); + reader.join().unwrap(); + server.join().unwrap(); + + assert!(status.success()); + assert_eq!(vec![first, second, third], vec!["step 1", "step 2", "done"]); +} + +#[test] +fn client_exits_with_failure_when_service_disconnects_before_completion() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let ws_url = format!("ws://{address}"); + + let server = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + let mut websocket = accept(stream).unwrap(); + let payload = read_ws_text(&mut websocket); + let request: ClientMessage = serde_json::from_str(&payload).unwrap(); + websocket.close(None).unwrap(); + request + }); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + child + .stdin + .as_mut() + .unwrap() + .write_all("打开百度搜索天气\n".as_bytes()) + .unwrap(); + + let status = child.wait().unwrap(); + assert!(!status.success()); + + let request = server.join().unwrap(); + assert_eq!(request.into_submit_task_request().unwrap().instruction, "打开百度搜索天气"); +} + +#[test] +fn client_to_service_regression_routes_zhihu_through_callback_host_without_invalid_hmac_seed_output() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + + let (event_tx, event_rx) = mpsc::channel(); + let (browser_ws_url, browser_server) = start_callback_host_hotlist_browser_server(event_tx); + + let root = std::env::temp_dir().join(format!("sgclaw-service-task-flow-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "http://127.0.0.1:9", + "model": "deepseek-chat", + "browserWsUrl": "{browser_ws_url}", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut service = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let ready_deadline = Instant::now() + Duration::from_secs(2); + let mut service_stderr_boot = String::new(); + while Instant::now() < ready_deadline { + if let Some(stream) = service.stderr.as_mut() { + let mut buf = [0_u8; 1024]; + match stream.read(&mut buf) { + Ok(0) => {} + Ok(n) => { + service_stderr_boot.push_str(&String::from_utf8_lossy(&buf[..n])); + if service_stderr_boot.contains("sg_claw ready:") { + break; + } + } + Err(_) => {} + } + } + if service.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(20)); + } + assert!( + service_stderr_boot.contains("sg_claw ready:"), + "service did not report readiness; stderr={service_stderr_boot}" + ); + + let mut client = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + client + .stdin + .as_mut() + .unwrap() + .write_all("打开知乎热榜,获取前10条数据,并导出 Excel\n".as_bytes()) + .unwrap(); + + let client_output = client.wait_with_output().unwrap(); + browser_server.join().unwrap(); + + let register = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let bootstrap = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let pre_ready = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let open_page = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let get_text = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let eval = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + + let exit_deadline = Instant::now() + Duration::from_secs(1); + let mut service_status = None; + while Instant::now() < exit_deadline { + if let Some(status) = service.try_wait().unwrap() { + service_status = Some(status); + break; + } + thread::sleep(Duration::from_millis(20)); + } + if service_status.is_none() { + service.kill().unwrap(); + let _ = service.wait(); + } + let service_stdout = service + .stdout + .take() + .map(|mut stream| { + let mut buf = Vec::new(); + let _ = stream.read_to_end(&mut buf); + String::from_utf8_lossy(&buf).into_owned() + }) + .unwrap_or_default(); + let service_stderr = service + .stderr + .take() + .map(|mut stream| { + let mut buf = Vec::new(); + let _ = stream.read_to_end(&mut buf); + String::from_utf8_lossy(&buf).into_owned() + }) + .unwrap_or_default(); + + let client_stdout = String::from_utf8_lossy(&client_output.stdout).into_owned(); + let client_stderr = String::from_utf8_lossy(&client_output.stderr).into_owned(); + let combined_output = format!("{client_stdout}\n{client_stderr}\n{service_stdout}\n{service_stderr}"); + + let register = match register { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected register browser frame, got {other:?}"), + }; + assert_eq!(register, json!({ "type": "register", "role": "web" })); + + let bootstrap = match bootstrap { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected helper bootstrap frame, got {other:?}"), + }; + assert_eq!(bootstrap[0], json!("https://www.zhihu.com")); + assert_eq!(bootstrap[1], json!("sgBrowerserOpenPage")); + assert!(bootstrap[2] + .as_str() + .is_some_and(|url| url.ends_with("/sgclaw/browser-helper.html"))); + + let pre_ready = match pre_ready { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected pre-ready command envelope, got {other:?}"), + }; + assert_eq!(pre_ready, json!({ "ok": false, "command": null })); + + let open_page = match open_page { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected open-page command envelope, got {other:?}"), + }; + assert_eq!(open_page["command"]["action"], json!("sgBrowerserOpenPage")); + assert_eq!(open_page["command"]["args"][0], json!("https://www.zhihu.com/hot")); + + let get_text = match get_text { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected getText command envelope, got {other:?}"), + }; + assert_eq!(get_text["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(get_text["command"]["args"][0], json!("www.zhihu.com")); + assert!(get_text["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnGetText"))); + + let eval = match eval { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected eval command envelope, got {other:?}"), + }; + assert_eq!(eval["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(eval["command"]["args"][0], json!("www.zhihu.com")); + assert!(eval["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnEval"))); + + assert!(client_output.status.success()); + assert!(client_stdout.contains("已导出并打开知乎热榜 Excel"), "client stdout={client_stdout}"); + assert!(client_stdout.contains(".xlsx"), "client stdout={client_stdout}"); + assert!( + !combined_output.contains("invalid hmac seed: session key must not be empty"), + "target behavior must avoid the invalid hmac seed failure; combined_output={combined_output}" + ); + assert!( + !combined_output.contains(RUNTIME_DROP_PANIC_TEXT), + "target behavior must avoid the runtime-drop panic; combined_output={combined_output}" + ); +} diff --git a/tests/service_ws_session_test.rs b/tests/service_ws_session_test.rs new file mode 100644 index 0000000..96a9c9a --- /dev/null +++ b/tests/service_ws_session_test.rs @@ -0,0 +1,1380 @@ +use std::io::{Read as _, Write}; +use std::net::TcpListener; +use std::sync::{mpsc, Arc, Mutex}; +use std::thread; +use std::time::{Duration, Instant}; + +use reqwest::blocking::Client; +use serde_json::{json, Value}; +use tungstenite::{accept, connect, Message}; + +use sgclaw::agent::AgentRuntimeContext; +use sgclaw::pipe::AgentMessage; +use sgclaw::service::{ClientMessage, ServiceEventSink, ServiceMessage, ServiceSession}; + +const RUNTIME_DROP_PANIC_TEXT: &str = + "Cannot drop a runtime in a context where blocking is not allowed"; + +fn read_ws_text(stream: &mut tungstenite::WebSocket) -> String +where + S: std::io::Read + std::io::Write, +{ + match stream.read().unwrap() { + Message::Text(text) => text.to_string(), + other => panic!("expected text frame, got {other:?}"), + } +} + +fn start_fake_deepseek_server( + responses: Vec, +) -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let address = format!("http://{}", listener.local_addr().unwrap()); + let requests = Arc::new(Mutex::new(Vec::new())); + let request_log = requests.clone(); + + let handle = thread::spawn(move || { + for response in responses { + let deadline = Instant::now() + Duration::from_secs(5); + let (mut stream, _) = loop { + match listener.accept() { + Ok(pair) => break pair, + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + assert!(Instant::now() < deadline, "timed out waiting for provider request"); + thread::sleep(Duration::from_millis(10)); + } + Err(err) => panic!("failed to accept provider request: {err}"), + } + }; + stream.set_nonblocking(false).unwrap(); + let body = match read_http_json_body(&mut stream) { + Ok(body) => body, + Err(_) => continue, + }; + request_log.lock().unwrap().push(body); + + let payload = response.to_string(); + let reply = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + payload.as_bytes().len(), + payload + ); + stream.write_all(reply.as_bytes()).unwrap(); + stream.flush().unwrap(); + } + }); + + (address, requests, handle) +} + +fn start_lenient_deepseek_server( + responses: Vec, +) -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + listener.set_nonblocking(true).unwrap(); + let address = format!("http://{}", listener.local_addr().unwrap()); + let requests = Arc::new(Mutex::new(Vec::new())); + let request_log = requests.clone(); + + let handle = thread::spawn(move || { + for response in responses { + let deadline = Instant::now() + Duration::from_millis(750); + let accepted = loop { + match listener.accept() { + Ok(pair) => break Some(pair), + Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => { + if Instant::now() >= deadline { + break None; + } + thread::sleep(Duration::from_millis(10)); + } + Err(err) => panic!("failed to accept provider request: {err}"), + } + }; + let Some((mut stream, _)) = accepted else { + break; + }; + stream.set_nonblocking(false).unwrap(); + let body = match read_http_json_body(&mut stream) { + Ok(body) => body, + Err(_) => continue, + }; + request_log.lock().unwrap().push(body); + + let payload = response.to_string(); + let reply = format!( + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}", + payload.as_bytes().len(), + payload + ); + stream.write_all(reply.as_bytes()).unwrap(); + stream.flush().unwrap(); + } + }); + + (address, requests, handle) +} + +fn read_http_json_body(stream: &mut impl std::io::Read) -> Result { + let mut buffer = Vec::new(); + let mut headers_end = None; + + while headers_end.is_none() { + let mut chunk = [0_u8; 1024]; + let bytes = stream.read(&mut chunk).unwrap(); + if bytes == 0 { + return Err("unexpected EOF while reading headers"); + } + buffer.extend_from_slice(&chunk[..bytes]); + headers_end = buffer.windows(4).position(|window| window == b"\r\n\r\n"); + } + + let headers_end = headers_end.unwrap() + 4; + let headers = String::from_utf8(buffer[..headers_end].to_vec()).unwrap(); + let Some(content_length) = headers.lines().find_map(|line| { + let (name, value) = line.split_once(':')?; + name.eq_ignore_ascii_case("content-length") + .then(|| value.trim().parse::().unwrap()) + }) else { + return Err("missing content-length header"); + }; + + while buffer.len() < headers_end + content_length { + let mut chunk = vec![0_u8; content_length]; + let bytes = stream.read(&mut chunk).unwrap(); + if bytes == 0 { + return Err("unexpected EOF while reading body"); + } + buffer.extend_from_slice(&chunk[..bytes]); + } + + Ok(serde_json::from_slice(&buffer[headers_end..headers_end + content_length]).unwrap()) +} + +#[derive(Debug)] +enum CallbackHostBrowserEvent { + BrowserFrame(Value), + CommandEnvelope(Value), +} + +fn start_callback_host_hotlist_browser_server( + event_tx: mpsc::Sender, +) -> (String, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(2))).unwrap(); + stream.set_write_timeout(Some(Duration::from_secs(2))).unwrap(); + let mut websocket = accept(stream).unwrap(); + + let register = match websocket.read().unwrap() { + Message::Text(text) => serde_json::from_str::(&text).unwrap(), + other => panic!("expected register frame, got {other:?}"), + }; + event_tx + .send(CallbackHostBrowserEvent::BrowserFrame(register)) + .unwrap(); + websocket + .send(Message::Text( + r#"{"type":"welcome","client_id":1,"server_time":"2026-04-04T00:00:00"}"# + .to_string() + .into(), + )) + .unwrap(); + + let first_action = match websocket.read().unwrap() { + Message::Text(text) => serde_json::from_str::(&text).unwrap(), + other => panic!("expected browser action frame, got {other:?}"), + }; + event_tx + .send(CallbackHostBrowserEvent::BrowserFrame(first_action.clone())) + .unwrap(); + + let Some(values) = first_action.as_array() else { + websocket.close(None).ok(); + return; + }; + let is_helper_open = values.len() >= 3 + && values[1] == json!("sgBrowerserOpenPage") + && values[2] + .as_str() + .is_some_and(|url| url.ends_with("/sgclaw/browser-helper.html")); + if !is_helper_open { + websocket.close(None).ok(); + return; + } + + let helper_url = values[2].as_str().unwrap().to_string(); + let helper_origin = helper_url + .trim_end_matches("/sgclaw/browser-helper.html") + .to_string(); + let helper_client = Client::builder() + .timeout(Duration::from_secs(2)) + .build() + .unwrap(); + let helper_html = helper_client + .get(&helper_url) + .send() + .unwrap() + .error_for_status() + .unwrap() + .text() + .unwrap(); + assert!(helper_html.contains("sgclawReady")); + assert!(helper_html.contains("sgclawOnLoaded")); + assert!(helper_html.contains("sgclawOnGetText")); + assert!(helper_html.contains("sgclawOnEval")); + + let pre_ready_command: Value = helper_client + .get(format!("{helper_origin}/sgclaw/callback/commands/next")) + .send() + .unwrap() + .error_for_status() + .unwrap() + .json() + .unwrap(); + event_tx + .send(CallbackHostBrowserEvent::CommandEnvelope(pre_ready_command)) + .unwrap(); + + helper_client + .post(format!("{helper_origin}/sgclaw/callback/ready")) + .json(&json!({ + "type": "ready", + "helper_url": helper_url, + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + + let hotlist_text = "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度"; + let hotlist_payload = json!({ + "source": "https://www.zhihu.com/hot", + "sheet_name": "知乎热榜", + "columns": ["rank", "title", "heat"], + "rows": [[1, "问题一", "344万"], [2, "问题二", "266万"]] + }) + .to_string(); + let deadline = Instant::now() + Duration::from_secs(10); + let mut saw_get_text = false; + let mut saw_eval = false; + + while Instant::now() < deadline { + let envelope: Value = helper_client + .get(format!("{helper_origin}/sgclaw/callback/commands/next")) + .send() + .unwrap() + .error_for_status() + .unwrap() + .json() + .unwrap(); + let Some(command) = envelope.get("command").and_then(Value::as_object) else { + thread::sleep(Duration::from_millis(20)); + continue; + }; + event_tx + .send(CallbackHostBrowserEvent::CommandEnvelope(envelope.clone())) + .unwrap(); + + let action_name = command + .get("action") + .and_then(Value::as_str) + .unwrap_or_default() + .to_string(); + + helper_client + .post(format!("{helper_origin}/sgclaw/callback/commands/ack")) + .json(&json!({ "type": "command_ack" })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + + let args = command + .get("args") + .and_then(Value::as_array) + .cloned() + .unwrap_or_default(); + match action_name.as_str() { + "sgBrowerserOpenPage" => {} + "sgBrowserExcuteJsCodeByDomain" => { + let script = args.get(1).and_then(Value::as_str).unwrap_or_default(); + if script.contains("sgclawOnGetText") { + saw_get_text = true; + helper_client + .post(format!("{helper_origin}/sgclaw/callback/events")) + .json(&json!({ + "callback": "sgclawOnGetText", + "request_url": helper_url, + "target_url": "https://www.zhihu.com/hot", + "action": action_name, + "payload": { "text": hotlist_text } + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + } else if script.contains("sgclawOnEval") { + saw_eval = true; + helper_client + .post(format!("{helper_origin}/sgclaw/callback/events")) + .json(&json!({ + "callback": "sgclawOnEval", + "request_url": helper_url, + "target_url": "https://www.zhihu.com/hot", + "action": action_name, + "payload": { "value": hotlist_payload } + })) + .send() + .unwrap() + .error_for_status() + .unwrap(); + break; + } else { + panic!("unexpected callback-host domain command: {script}"); + } + } + other => panic!("unexpected callback-host command action {other}"), + } + } + + assert!(saw_get_text, "expected callback-host getText command"); + assert!(saw_eval, "expected callback-host eval command"); + websocket.close(None).ok(); + }); + + (format!("ws://{address}"), handle) +} + +fn start_direct_zhihu_browser_ws_server() -> (String, Arc>>, thread::JoinHandle<()>) { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + let frames = Arc::new(Mutex::new(Vec::new())); + let frames_for_thread = Arc::clone(&frames); + + let handle = thread::spawn(move || { + let (stream, _) = listener.accept().unwrap(); + stream.set_read_timeout(Some(Duration::from_secs(5))).unwrap(); + stream.set_write_timeout(Some(Duration::from_secs(5))).unwrap(); + let mut socket = accept(stream).unwrap(); + let mut action_count = 0_u64; + + loop { + let message = match socket.read() { + Ok(message) => message, + Err(tungstenite::Error::ConnectionClosed) + | Err(tungstenite::Error::AlreadyClosed) => break, + Err(err) => panic!("browser ws test server read failed: {err}"), + }; + let payload = match message { + Message::Text(text) => text.to_string(), + Message::Ping(payload) => { + socket.send(Message::Pong(payload)).unwrap(); + continue; + } + Message::Close(_) => break, + other => panic!("expected text frame, got {other:?}"), + }; + frames_for_thread.lock().unwrap().push(payload.clone()); + + let parsed: Value = serde_json::from_str(&payload).unwrap(); + if parsed.get("type").and_then(Value::as_str) == Some("register") { + continue; + } + + let values = parsed.as_array().expect("browser action frame should be an array"); + let request_url = values[0].as_str().expect("request_url should be a string"); + let action = values[1].as_str().expect("action should be a string"); + action_count += 1; + + socket + .send(Message::Text( + r#"{"type":"welcome","client_id":1,"server_time":"2026-04-04T00:00:00"}"# + .to_string() + .into(), + )) + .unwrap(); + socket.send(Message::Text("0".into())).unwrap(); + + let callback_frame = match action { + "sgHideBrowserCallAfterLoaded" => { + let target_url = values[2].as_str().expect("navigate target_url should be a string"); + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgHideBrowserCallAfterLoaded@_@" + ) + ]) + } + "sgBrowserExcuteJsCodeByArea" => { + let target_url = values[2].as_str().expect("script target_url should be a string"); + let response_text = if action_count == 2 { + "知乎热榜\n1 问题一 344万热度\n2 问题二 266万热度".to_string() + } else { + r#"{"source":"https://www.zhihu.com/hot","sheet_name":"知乎热榜","columns":["rank","title","heat"],"rows":[[1,"问题一","344万"],[2,"问题二","266万"]]}"#.to_string() + }; + json!([ + request_url, + "callBackJsToCpp", + format!( + "{request_url}@_@{target_url}@_@sgclaw_cb_{action_count}@_@sgBrowserExcuteJsCodeByArea@_@{response_text}" + ) + ]) + } + other => panic!("unexpected browser action {other}"), + }; + + socket + .send(Message::Text(callback_frame.to_string().into())) + .unwrap(); + + if action_count >= 3 { + break; + } + } + + socket.close(None).ok(); + }); + + (format!("ws://{address}"), frames, handle) +} + +#[test] +fn service_entrypoint_function_is_exported() { + let entry: fn() -> Result<(), sgclaw::pipe::PipeError> = sgclaw::service::run; + + let _ = entry; +} + +#[test] +fn service_run_requires_llm_config_for_startup() { + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let result = sgclaw::service::run(); + + assert!(matches!( + result, + Err(sgclaw::pipe::PipeError::Protocol(message)) + if message.contains("missing environment variable: DEEPSEEK_API_KEY") + )); +} + +#[test] +fn service_startup_config_loads_ws_endpoints_from_browser_config() { + let root = std::env::temp_dir().join(format!("sgclaw-service-startup-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + r#"{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345", + "serviceWsListenAddr": "127.0.0.1:42321" +}"#, + ) + .unwrap(); + + let startup = sgclaw::service::load_startup_config(&AgentRuntimeContext::new( + Some(config_path), + root, + )) + .unwrap(); + + assert_eq!(startup.browser_ws_url.as_deref(), Some("ws://127.0.0.1:12345")); + assert_eq!( + startup.service_ws_listen_addr.as_deref(), + Some("127.0.0.1:42321") + ); +} + +#[test] +fn service_startup_config_uses_default_ws_endpoints_when_not_configured() { + let root = std::env::temp_dir().join(format!("sgclaw-service-defaults-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + r#"{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat" +}"#, + ) + .unwrap(); + + let startup = sgclaw::service::load_startup_config(&AgentRuntimeContext::new( + Some(config_path), + root, + )) + .unwrap(); + + assert_eq!( + startup.browser_ws_url.as_deref(), + Some("ws://127.0.0.1:12345") + ); + assert_eq!( + startup.service_ws_listen_addr.as_deref(), + Some("127.0.0.1:42321") + ); +} + +#[test] +fn service_binary_reports_resolved_startup_endpoints() { + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = listener.local_addr().unwrap(); + drop(listener); + let root = std::env::temp_dir().join(format!("sgclaw-service-report-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let deadline = Instant::now() + Duration::from_secs(2); + let mut stderr = String::new(); + while Instant::now() < deadline { + if let Some(stream) = child.stderr.as_mut() { + use std::io::Read; + let mut buf = [0_u8; 1024]; + match stream.read(&mut buf) { + Ok(0) => {} + Ok(n) => { + stderr.push_str(&String::from_utf8_lossy(&buf[..n])); + if stderr.contains("sg_claw ready:") { + break; + } + } + Err(_) => {} + } + } + if child.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(20)); + } + + let status = child.try_wait().unwrap(); + if status.is_none() { + child.kill().unwrap(); + let _ = child.wait(); + } + if let Some(mut stream) = child.stderr.take() { + use std::io::Read; + let mut buf = Vec::new(); + let _ = stream.read_to_end(&mut buf); + stderr.push_str(&String::from_utf8_lossy(&buf)); + } + + assert!(stderr.contains("sg_claw ready:")); + assert!(stderr.contains(&service_addr.to_string())); + assert!(stderr.contains("ws://127.0.0.1:12345")); +} + +#[test] +fn service_binary_keeps_service_ws_listener_available_for_client_connections() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + + let root = std::env::temp_dir().join(format!("sgclaw-service-live-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut child = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let deadline = Instant::now() + Duration::from_secs(2); + let mut connected = false; + while Instant::now() < deadline { + match connect(ws_url.as_str()) { + Ok((socket, _)) => { + connected = true; + drop(socket); + break; + } + Err(_) => { + if child.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(50)); + } + } + } + + let status = child.try_wait().unwrap(); + if status.is_none() { + child.kill().unwrap(); + let _ = child.wait(); + } + let stderr = child + .stderr + .take() + .map(|mut stream| { + let mut buf = Vec::new(); + use std::io::Read; + let _ = stream.read_to_end(&mut buf); + String::from_utf8_lossy(&buf).into_owned() + }) + .unwrap_or_default(); + + assert!(connected, "service ws listener never became available; stderr={stderr}"); + assert!(status.is_none(), "sg_claw exited before client could connect; stderr={stderr}"); +} + +#[test] +fn service_binary_survives_real_client_disconnect_after_task_complete() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + let root = std::env::temp_dir().join(format!("sgclaw-service-disconnect-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut service = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let ready_deadline = Instant::now() + Duration::from_secs(2); + let mut stderr = String::new(); + while Instant::now() < ready_deadline { + if let Some(stream) = service.stderr.as_mut() { + let mut buf = [0_u8; 1024]; + match stream.read(&mut buf) { + Ok(0) => {} + Ok(n) => { + stderr.push_str(&String::from_utf8_lossy(&buf[..n])); + if stderr.contains("sg_claw ready:") { + break; + } + } + Err(_) => {} + } + } + if service.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(20)); + } + assert!(stderr.contains("sg_claw ready:"), "service did not report readiness; stderr={stderr}"); + + let mut client = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + client.stdin.as_mut().unwrap().write_all(" \n".as_bytes()).unwrap(); + let client_output = client.wait_with_output().unwrap(); + + assert!( + client_output.status.success(), + "client failed: stdout={} stderr={}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr) + ); + assert!( + String::from_utf8_lossy(&client_output.stdout).contains("请输入任务内容。"), + "client did not receive TaskComplete summary: stdout={} stderr={}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr) + ); + + let exit_deadline = Instant::now() + Duration::from_secs(1); + let mut service_status = None; + while Instant::now() < exit_deadline { + if let Some(status) = service.try_wait().unwrap() { + service_status = Some(status); + break; + } + thread::sleep(Duration::from_millis(20)); + } + + if service_status.is_none() { + service.kill().unwrap(); + let _ = service.wait(); + } + let stderr = service + .stderr + .take() + .map(|mut stream| { + let mut buf = Vec::new(); + use std::io::Read; + let _ = stream.read_to_end(&mut buf); + String::from_utf8_lossy(&buf).into_owned() + }) + .unwrap_or_default(); + + assert!( + service_status.is_none(), + "sg_claw exited after client disconnect; stderr={stderr}" + ); +} + +#[test] +fn service_binary_submit_flow_routes_zhihu_through_callback_host() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + + let (event_tx, event_rx) = mpsc::channel(); + let (browser_ws_url, browser_server) = start_callback_host_hotlist_browser_server(event_tx); + + let root = std::env::temp_dir().join(format!("sgclaw-service-zhihu-submit-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "http://127.0.0.1:9", + "model": "deepseek-chat", + "browserWsUrl": "{browser_ws_url}", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut service = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let ready_deadline = Instant::now() + Duration::from_secs(2); + let mut stderr = String::new(); + while Instant::now() < ready_deadline { + if let Some(stream) = service.stderr.as_mut() { + let mut buf = [0_u8; 1024]; + match stream.read(&mut buf) { + Ok(0) => {} + Ok(n) => { + stderr.push_str(&String::from_utf8_lossy(&buf[..n])); + if stderr.contains("sg_claw ready:") { + break; + } + } + Err(_) => {} + } + } + if service.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(20)); + } + assert!(stderr.contains("sg_claw ready:"), "service did not report readiness; stderr={stderr}"); + + let mut client = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + client + .stdin + .as_mut() + .unwrap() + .write_all("打开知乎热榜,获取前10条数据,并导出 Excel\n".as_bytes()) + .unwrap(); + + let client_output = client.wait_with_output().unwrap(); + browser_server.join().unwrap(); + + let register = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let bootstrap = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let pre_ready = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let open_page = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let get_text = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let eval = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + + let service_status = service.try_wait().unwrap(); + if service_status.is_none() { + service.kill().unwrap(); + let _ = service.wait(); + } + if let Some(mut stream) = service.stderr.take() { + let mut buf = Vec::new(); + let _ = stream.read_to_end(&mut buf); + stderr.push_str(&String::from_utf8_lossy(&buf)); + } + let combined_output = format!( + "{}\n{}\n{}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr), + stderr + ); + + let register = match register { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected register browser frame, got {other:?}"), + }; + assert_eq!(register, json!({ "type": "register", "role": "web" })); + + let bootstrap = match bootstrap { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected helper bootstrap frame, got {other:?}"), + }; + assert_eq!(bootstrap[0], json!("https://www.zhihu.com")); + assert_eq!(bootstrap[1], json!("sgBrowerserOpenPage")); + assert!(bootstrap[2] + .as_str() + .is_some_and(|url| url.ends_with("/sgclaw/browser-helper.html"))); + + let pre_ready = match pre_ready { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected pre-ready command envelope, got {other:?}"), + }; + assert_eq!(pre_ready, json!({ "ok": false, "command": null })); + + let open_page = match open_page { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected open-page command envelope, got {other:?}"), + }; + assert_eq!(open_page["command"]["action"], json!("sgBrowerserOpenPage")); + assert_eq!(open_page["command"]["args"][0], json!("https://www.zhihu.com/hot")); + + let get_text = match get_text { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected getText command envelope, got {other:?}"), + }; + assert_eq!(get_text["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(get_text["command"]["args"][0], json!("www.zhihu.com")); + assert!(get_text["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnGetText"))); + + let eval = match eval { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected eval command envelope, got {other:?}"), + }; + assert_eq!(eval["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(eval["command"]["args"][0], json!("www.zhihu.com")); + assert!(eval["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnEval"))); + + assert!(client_output.status.success()); + assert!( + String::from_utf8_lossy(&client_output.stdout).contains("已导出并打开知乎热榜 Excel"), + "client stdout={} stderr={}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr) + ); + assert!( + String::from_utf8_lossy(&client_output.stdout).contains(".xlsx"), + "client stdout={} stderr={}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr) + ); + assert!( + !combined_output.contains(RUNTIME_DROP_PANIC_TEXT), + "service submit flow still contains runtime-drop panic: {combined_output}" + ); +} + +#[test] +fn service_binary_submit_flow_uses_callback_host_command_semantics_for_zhihu() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + + let (event_tx, event_rx) = mpsc::channel(); + let (browser_ws_url, browser_server) = start_callback_host_hotlist_browser_server(event_tx); + + let root = std::env::temp_dir().join(format!("sgclaw-service-session-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "http://127.0.0.1:9", + "model": "deepseek-chat", + "browserWsUrl": "{browser_ws_url}", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut service = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let ready_deadline = Instant::now() + Duration::from_secs(2); + let mut stderr = String::new(); + while Instant::now() < ready_deadline { + if let Some(stream) = service.stderr.as_mut() { + let mut buf = [0_u8; 1024]; + match stream.read(&mut buf) { + Ok(0) => {} + Ok(n) => { + stderr.push_str(&String::from_utf8_lossy(&buf[..n])); + if stderr.contains("sg_claw ready:") { + break; + } + } + Err(_) => {} + } + } + if service.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(20)); + } + assert!(stderr.contains("sg_claw ready:"), "service did not report readiness; stderr={stderr}"); + + let mut client = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw_client").expect("sg_claw_client test binary path"), + ) + .env("SG_CLAW_SERVICE_WS_URL", &ws_url) + .stdin(std::process::Stdio::piped()) + .stdout(std::process::Stdio::piped()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + client + .stdin + .as_mut() + .unwrap() + .write_all("打开知乎热榜,获取前10条数据,并导出 Excel\n".as_bytes()) + .unwrap(); + + let client_output = client.wait_with_output().unwrap(); + browser_server.join().unwrap(); + + let register = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let bootstrap = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let pre_ready = event_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + let open_page = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let get_text = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + let eval = event_rx.recv_timeout(Duration::from_secs(4)).unwrap(); + + let service_status = service.try_wait().unwrap(); + if service_status.is_none() { + service.kill().unwrap(); + let _ = service.wait(); + } + if let Some(mut stream) = service.stderr.take() { + let mut buf = Vec::new(); + let _ = stream.read_to_end(&mut buf); + stderr.push_str(&String::from_utf8_lossy(&buf)); + } + let combined_output = format!( + "{}\n{}\n{}", + String::from_utf8_lossy(&client_output.stdout), + String::from_utf8_lossy(&client_output.stderr), + stderr + ); + + let register = match register { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected register browser frame, got {other:?}"), + }; + assert_eq!(register, json!({ "type": "register", "role": "web" })); + + let bootstrap = match bootstrap { + CallbackHostBrowserEvent::BrowserFrame(value) => value, + other => panic!("expected helper bootstrap frame, got {other:?}"), + }; + assert_eq!(bootstrap[0], json!("https://www.zhihu.com")); + assert_eq!(bootstrap[1], json!("sgBrowerserOpenPage")); + assert!(bootstrap[2] + .as_str() + .is_some_and(|url| url.ends_with("/sgclaw/browser-helper.html"))); + + let pre_ready = match pre_ready { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected pre-ready command envelope, got {other:?}"), + }; + assert_eq!(pre_ready, json!({ "ok": false, "command": null })); + + let open_page = match open_page { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected open-page command envelope, got {other:?}"), + }; + assert_eq!(open_page["command"]["action"], json!("sgBrowerserOpenPage")); + assert_eq!(open_page["command"]["args"][0], json!("https://www.zhihu.com/hot")); + + let get_text = match get_text { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected getText command envelope, got {other:?}"), + }; + assert_eq!(get_text["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(get_text["command"]["args"][0], json!("www.zhihu.com")); + assert!(get_text["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnGetText"))); + + let eval = match eval { + CallbackHostBrowserEvent::CommandEnvelope(value) => value, + other => panic!("expected eval command envelope, got {other:?}"), + }; + assert_eq!(eval["command"]["action"], json!("sgBrowserExcuteJsCodeByDomain")); + assert_eq!(eval["command"]["args"][0], json!("www.zhihu.com")); + assert!(eval["command"]["args"][1] + .as_str() + .is_some_and(|script| script.contains("sgclawOnEval"))); + + assert!(client_output.status.success()); + assert!( + !combined_output.contains(RUNTIME_DROP_PANIC_TEXT), + "service submit flow still contains runtime-drop panic: {combined_output}" + ); +} + +#[test] +fn first_client_attaches_without_busy_error() { + let session = ServiceSession::new(); + + let result = session.try_attach_client(); + + assert_eq!(result, Ok(())); +} + +#[test] +fn second_client_gets_busy_message() { + let session = ServiceSession::new(); + assert_eq!(session.try_attach_client(), Ok(())); + + let result = session.try_attach_client(); + + assert_eq!( + result, + Err(ServiceMessage::Busy { + message: "service already has an attached client".to_string(), + }) + ); +} + +#[test] +fn disconnect_releases_the_session_for_a_new_client() { + let session = ServiceSession::new(); + assert_eq!(session.try_attach_client(), Ok(())); + + session.detach_client(); + + assert_eq!(session.try_attach_client(), Ok(())); +} + +#[test] +fn task_cannot_start_without_an_attached_client() { + let session = ServiceSession::new(); + + let result = session.try_start_task(); + + assert_eq!( + result, + Err(ServiceMessage::Busy { + message: "service has no attached client".to_string(), + }) + ); +} + +#[test] +fn overlapping_task_submission_is_rejected() { + let session = ServiceSession::new(); + assert_eq!(session.try_attach_client(), Ok(())); + assert_eq!(session.try_start_task(), Ok(())); + + let result = session.try_start_task(); + + assert_eq!( + result, + Err(ServiceMessage::Busy { + message: "service already has a running task".to_string(), + }) + ); +} + +#[test] +fn service_binary_accepts_connect_request_without_starting_browser_task() { + let service_listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let service_addr = service_listener.local_addr().unwrap(); + drop(service_listener); + + let root = std::env::temp_dir().join(format!("sgclaw-service-connect-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&root).unwrap(); + let config_path = root.join("sgclaw_config.json"); + std::fs::write( + &config_path, + format!( + r#"{{ + "apiKey": "sk-runtime", + "baseUrl": "https://api.deepseek.com", + "model": "deepseek-chat", + "browserWsUrl": "ws://127.0.0.1:12345", + "serviceWsListenAddr": "{service_addr}" +}}"# + ), + ) + .unwrap(); + + let mut service = std::process::Command::new( + std::env::var("CARGO_BIN_EXE_sg_claw").expect("sg_claw test binary path"), + ) + .env("SGCLAW_DISABLE_POST_EXPORT_OPEN", "1") + .arg("--config-path") + .arg(&config_path) + .stdout(std::process::Stdio::null()) + .stderr(std::process::Stdio::piped()) + .spawn() + .unwrap(); + + let ws_url = format!("ws://{service_addr}"); + let connect_deadline = Instant::now() + Duration::from_secs(2); + let mut websocket = None; + while Instant::now() < connect_deadline { + match connect(ws_url.as_str()) { + Ok((socket, _)) => { + websocket = Some(socket); + break; + } + Err(_) => { + if service.try_wait().unwrap().is_some() { + break; + } + thread::sleep(Duration::from_millis(50)); + } + } + } + + let mut websocket = websocket.expect("service ws listener never became available"); + websocket + .send(Message::Text( + serde_json::to_string(&ClientMessage::Connect).unwrap().into(), + )) + .unwrap(); + + let frame = read_ws_text(&mut websocket); + let message: ServiceMessage = serde_json::from_str(&frame).unwrap(); + assert_eq!( + message, + ServiceMessage::StatusChanged { + state: "connected".to_string(), + } + ); + + websocket.close(None).unwrap(); + + let exit_deadline = Instant::now() + Duration::from_secs(1); + let mut service_status = None; + while Instant::now() < exit_deadline { + if let Some(status) = service.try_wait().unwrap() { + service_status = Some(status); + break; + } + thread::sleep(Duration::from_millis(20)); + } + if service_status.is_none() { + service.kill().unwrap(); + let _ = service.wait(); + } + + let stderr = service + .stderr + .take() + .map(|mut stream| { + let mut buf = Vec::new(); + use std::io::Read; + let _ = stream.read_to_end(&mut buf); + String::from_utf8_lossy(&buf).into_owned() + }) + .unwrap_or_default(); + + assert!( + service_status.is_none(), + "sg_claw exited after connect lifecycle request; stderr={stderr}" + ); +} + +#[test] +fn submit_task_client_message_converts_into_shared_runner_request() { + let message = ClientMessage::SubmitTask { + instruction: "continue task".to_string(), + conversation_id: "conv-1".to_string(), + messages: vec![sgclaw::pipe::ConversationMessage { + role: "user".to_string(), + content: "prior turn".to_string(), + }], + page_url: "https://example.com".to_string(), + page_title: "Example".to_string(), + }; + + let request = message.into_submit_task_request().expect("submit task request"); + + assert_eq!(request.instruction, "continue task"); + assert_eq!(request.conversation_id.as_deref(), Some("conv-1")); + assert_eq!(request.messages.len(), 1); + assert_eq!(request.page_url.as_deref(), Some("https://example.com")); + assert_eq!(request.page_title.as_deref(), Some("Example")); +} + +#[test] +fn ping_client_message_does_not_convert_into_submit_task_request() { + let message = ClientMessage::Ping; + + assert!(message.into_submit_task_request().is_none()); +} + +#[test] +fn lifecycle_client_messages_do_not_convert_into_submit_task_request() { + for message in [ClientMessage::Connect, ClientMessage::Start, ClientMessage::Stop] { + assert!(message.into_submit_task_request().is_none()); + } +} + +#[test] +fn lifecycle_client_messages_round_trip_with_stable_tags() { + let cases = [ + (ClientMessage::Connect, r#"{"type":"connect"}"#), + (ClientMessage::Start, r#"{"type":"start"}"#), + (ClientMessage::Stop, r#"{"type":"stop"}"#), + ]; + + for (message, raw) in cases { + assert_eq!(serde_json::to_string(&message).unwrap(), raw); + let decoded: ClientMessage = serde_json::from_str(raw).unwrap(); + assert_eq!(decoded, message); + } +} + +#[test] +fn service_event_sink_maps_log_completion_and_status_messages() { + let sink = ServiceEventSink::default(); + + sgclaw::agent::AgentEventSink::send( + &sink, + &AgentMessage::StatusChanged { + state: "started".to_string(), + }, + ) + .unwrap(); + sgclaw::agent::AgentEventSink::send( + &sink, + &AgentMessage::LogEntry { + level: "info".to_string(), + message: "hello".to_string(), + }, + ) + .unwrap(); + sgclaw::agent::AgentEventSink::send( + &sink, + &AgentMessage::TaskComplete { + success: true, + summary: "done".to_string(), + }, + ) + .unwrap(); + + assert_eq!( + sink.sent_messages(), + vec![ + ServiceMessage::StatusChanged { + state: "started".to_string(), + }, + ServiceMessage::LogEntry { + level: "info".to_string(), + message: "hello".to_string(), + }, + ServiceMessage::TaskComplete { + success: true, + summary: "done".to_string(), + }, + ] + ); +} diff --git a/tests/task_runner_test.rs b/tests/task_runner_test.rs new file mode 100644 index 0000000..441bf74 --- /dev/null +++ b/tests/task_runner_test.rs @@ -0,0 +1,471 @@ +mod common; + +use std::sync::{Arc, Mutex, OnceLock}; +use std::time::Duration; + +use common::MockTransport; +use serde_json::Value; +use sgclaw::agent::{run_submit_task, AgentEventSink, AgentRuntimeContext, SubmitTaskRequest}; +use sgclaw::agent::task_runner::run_submit_task_with_browser_backend; +use sgclaw::browser::BrowserBackend; +use sgclaw::pipe::{ + Action, AgentMessage, BrowserMessage, BrowserPipeTool, CommandOutput, ConversationMessage, + ExecutionSurfaceMetadata, PipeError, Timing, +}; +use sgclaw::security::MacPolicy; +use uuid::Uuid; + +fn env_lock() -> &'static Mutex<()> { + static LOCK: OnceLock> = OnceLock::new(); + LOCK.get_or_init(|| Mutex::new(())) +} + +fn test_policy() -> MacPolicy { + MacPolicy::from_json_str( + r#"{ + "version": "1.0", + "domains": { "allowed": ["oa.example.com", "www.baidu.com"] }, + "pipe_actions": { + "allowed": ["click", "type", "navigate", "getText"], + "blocked": ["eval", "executeJsInPage"] + } + }"#, + ) + .unwrap() +} + +fn test_browser_tool(transport: Arc) -> BrowserPipeTool { + BrowserPipeTool::new( + transport, + test_policy(), + vec![1, 2, 3, 4, 5, 6, 7, 8], + ) + .with_response_timeout(Duration::from_secs(1)) +} + +#[derive(Clone, Default)] +struct StubBrowserBackend; + +impl BrowserBackend for StubBrowserBackend { + fn invoke( + &self, + _action: Action, + _params: Value, + _expected_domain: &str, + ) -> Result { + Err(PipeError::Protocol( + "stub backend should not be invoked in this test".to_string(), + )) + } + + fn surface_metadata(&self) -> ExecutionSurfaceMetadata { + ExecutionSurfaceMetadata::privileged_browser_pipe("stub-backend") + } +} + +#[derive(Default)] +struct RecordingSink { + sent: Mutex>, +} + +impl RecordingSink { + fn sent_messages(&self) -> Vec { + self.sent.lock().unwrap().clone() + } +} + +impl AgentEventSink for RecordingSink { + fn send(&self, message: &AgentMessage) -> Result<(), PipeError> { + self.sent.lock().unwrap().push(message.clone()); + Ok(()) + } +} + +fn temp_workspace_root() -> PathBuf { + let root = std::env::temp_dir().join(format!("sgclaw-task-runner-{}", Uuid::new_v4())); + fs::create_dir_all(&root).unwrap(); + root +} + +#[test] +fn run_submit_task_with_browser_backend_accepts_ws_only_backend_and_preserves_existing_entry() { + let _existing_entry: fn( + &MockTransport, + &dyn AgentEventSink, + &BrowserPipeTool, + &AgentRuntimeContext, + SubmitTaskRequest, + ) -> Result<(), PipeError> = run_submit_task::; + let _ws_only_entry: fn( + &MockTransport, + &dyn AgentEventSink, + Arc, + &AgentRuntimeContext, + SubmitTaskRequest, + ) -> Result<(), PipeError> = run_submit_task_with_browser_backend::; + + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let transport = Arc::new(MockTransport::new(vec![])); + let sink = RecordingSink::default(); + let backend: Arc = Arc::new(StubBrowserBackend); + + run_submit_task_with_browser_backend( + transport.as_ref(), + &sink, + backend, + &AgentRuntimeContext::default(), + SubmitTaskRequest { + instruction: "打开百度搜索天气".to_string(), + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = sink.sent_messages(); + assert!(transport.sent_messages().is_empty()); + assert_eq!(sent.len(), 2); + assert!(matches!(&sent[0], AgentMessage::LogEntry { level, .. } if level == "info")); + assert!(matches!( + &sent[1], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("未配置大语言模型") + )); +} + +#[test] +fn run_submit_task_rejects_blank_instruction_without_emitting_logs() { + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + run_submit_task( + transport.as_ref(), + transport.as_ref(), + &browser_tool, + &AgentRuntimeContext::default(), + SubmitTaskRequest { + instruction: " ".to_string(), + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!(sent.len(), 1); + assert_eq!( + sent[0], + AgentMessage::TaskComplete { + success: false, + summary: "请输入任务内容。".to_string(), + } + ); +} + +#[test] +fn run_submit_task_can_emit_to_custom_sink() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + let sink = RecordingSink::default(); + + run_submit_task( + transport.as_ref(), + &sink, + &browser_tool, + &AgentRuntimeContext::default(), + SubmitTaskRequest { + instruction: "打开百度搜索天气".to_string(), + messages: vec![ConversationMessage { + role: "user".to_string(), + content: "上一轮问题".to_string(), + }], + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = sink.sent_messages(); + assert!(transport.sent_messages().is_empty()); + assert_eq!(sent.len(), 3); + assert!(matches!( + &sent[0], + AgentMessage::LogEntry { level, message } + if level == "info" + && message + == &format!( + "sgclaw runtime version={} protocol={}", + env!("CARGO_PKG_VERSION"), + sgclaw::pipe::protocol::PROTOCOL_VERSION + ) + )); + assert!(matches!( + &sent[1], + AgentMessage::LogEntry { level, message } + if level == "info" && message == "continuing conversation with 1 prior turns" + )); + assert!(matches!( + &sent[2], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("未配置大语言模型") + )); +} + +#[test] +fn run_submit_task_without_llm_config_emits_runtime_version_then_failure() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + run_submit_task( + transport.as_ref(), + transport.as_ref(), + &browser_tool, + &AgentRuntimeContext::default(), + SubmitTaskRequest { + instruction: "打开百度搜索天气".to_string(), + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!(sent.len(), 2); + assert!(matches!( + &sent[0], + AgentMessage::LogEntry { level, message } + if level == "info" + && message + == &format!( + "sgclaw runtime version={} protocol={}", + env!("CARGO_PKG_VERSION"), + sgclaw::pipe::protocol::PROTOCOL_VERSION + ) + )); + assert!(matches!( + &sent[1], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("未配置大语言模型") + )); +} + +#[test] +fn run_submit_task_logs_prior_turn_count_before_completion() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + run_submit_task( + transport.as_ref(), + transport.as_ref(), + &browser_tool, + &AgentRuntimeContext::default(), + SubmitTaskRequest { + instruction: "继续处理当前页面".to_string(), + messages: vec![ + ConversationMessage { + role: "user".to_string(), + content: "上一轮问题".to_string(), + }, + ConversationMessage { + role: "assistant".to_string(), + content: "上一轮回答".to_string(), + }, + ], + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!(sent.len(), 3); + assert!(matches!( + &sent[0], + AgentMessage::LogEntry { level, message } + if level == "info" + && message + == &format!( + "sgclaw runtime version={} protocol={}", + env!("CARGO_PKG_VERSION"), + sgclaw::pipe::protocol::PROTOCOL_VERSION + ) + )); + assert!(matches!( + &sent[1], + AgentMessage::LogEntry { level, message } + if level == "info" && message == "continuing conversation with 2 prior turns" + )); + assert!(matches!( + &sent[2], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("未配置大语言模型") + )); +} + +#[test] +fn run_submit_task_reports_settings_load_error_and_final_failure() { + let workspace_root = temp_workspace_root(); + let config_path = workspace_root.join("sgclaw_config.json"); + fs::write(&config_path, "{").unwrap(); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + let context = AgentRuntimeContext::new(Some(config_path), workspace_root); + + run_submit_task( + transport.as_ref(), + transport.as_ref(), + &browser_tool, + &context, + SubmitTaskRequest { + instruction: "打开百度".to_string(), + ..SubmitTaskRequest::default() + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!(sent.len(), 3); + assert!(matches!( + &sent[0], + AgentMessage::LogEntry { level, message } + if level == "info" + && message + == &format!( + "sgclaw runtime version={} protocol={}", + env!("CARGO_PKG_VERSION"), + sgclaw::pipe::protocol::PROTOCOL_VERSION + ) + )); + assert!(matches!( + &sent[1], + AgentMessage::LogEntry { level, message } + if level == "error" && message.starts_with("failed to load DeepSeek config:") + )); + assert!(matches!( + &sent[2], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("invalid DeepSeek config JSON") + )); +} + +#[test] +fn handle_browser_message_normalizes_empty_optional_submit_fields() { + let _guard = env_lock().lock().unwrap_or_else(|err| err.into_inner()); + std::env::remove_var("DEEPSEEK_API_KEY"); + std::env::remove_var("DEEPSEEK_BASE_URL"); + std::env::remove_var("DEEPSEEK_MODEL"); + + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::SubmitTask { + instruction: "打开百度".to_string(), + conversation_id: " ".to_string(), + messages: vec![], + page_url: "".to_string(), + page_title: "\n\t".to_string(), + }, + ) + .unwrap(); + + let sent = transport.sent_messages(); + assert_eq!(sent.len(), 2); + assert!(matches!(&sent[0], AgentMessage::LogEntry { .. })); + assert!(matches!( + &sent[1], + AgentMessage::TaskComplete { success, summary } + if !success && summary.contains("未配置大语言模型") + )); +} + +#[test] +fn handle_browser_message_emits_status_for_lifecycle_messages() { + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::Connect, + ) + .unwrap(); + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::Start, + ) + .unwrap(); + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::Stop, + ) + .unwrap(); + + assert_eq!( + transport.sent_messages(), + vec![ + AgentMessage::StatusChanged { + state: "connected".to_string(), + }, + AgentMessage::StatusChanged { + state: "started".to_string(), + }, + AgentMessage::StatusChanged { + state: "stopped".to_string(), + }, + ] + ); +} + +#[test] +fn handle_browser_message_still_ignores_init_and_unsolicited_response() { + let transport = Arc::new(MockTransport::new(vec![])); + let browser_tool = test_browser_tool(transport.clone()); + + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::Init { + version: "1.0".to_string(), + hmac_seed: "seed".to_string(), + capabilities: vec![], + }, + ) + .unwrap(); + sgclaw::agent::handle_browser_message( + transport.as_ref(), + &browser_tool, + BrowserMessage::Response { + seq: 1, + success: true, + data: serde_json::json!({}), + aom_snapshot: vec![], + timing: Timing { + queue_ms: 1, + exec_ms: 1, + }, + }, + ) + .unwrap(); + + assert!(transport.sent_messages().is_empty()); +}