From 94d7abe804506d91dac199283ac496159db45610 Mon Sep 17 00:00:00 2001 From: tangzhixiong613-jpg <340458552+tangzhixiong613-jpg@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:28:26 +0000 Subject: [PATCH 1/2] =?UTF-8?q?T-1253=20=E5=85=B1=E7=94=A8=E7=94=B5?= =?UTF-8?q?=E8=84=91=EF=BC=9A=E6=9C=AA=E5=88=86=E9=85=8D=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E6=97=B6=E5=8F=AF=E4=B8=80=E9=94=AE=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .../T-1200/T-1253-screen-assign-now/brief.md | 38 ++ .../T-1200/T-1253-screen-assign-now/r2.md | 25 + .../T-1200/T-1253-screen-assign-now/r3.md | 27 ++ .../T-1200/T-1253-screen-assign-now/report.md | 53 +++ .../T-1253-screen-assign-now/verification.txt | 427 ++++++++++++++++++ server/index.ts | 4 +- server/local-vm-screen-runtime.test.ts | 19 + server/local-vm-screen-runtime.ts | 16 +- server/local-vm-screens.e2e.test.ts | 43 ++ server/local-vm-screens.test.ts | 26 ++ server/local-vm-screens.ts | 13 + server/routes/computers.test.ts | 45 +- server/routes/computers.ts | 17 +- src/components/AssignLocalVmScreen.tsx | 66 +++ src/components/ComputerPanel.takeover.test.ts | 161 ++++++- src/components/ComputerPanel.tsx | 46 +- .../remote-desktop-panel.open.test.ts | 2 +- .../remote-desktop-panel.screen.test.ts | 116 ++++- src/components/remote-desktop-panel.tsx | 27 +- src/locales/en.json | 3 +- src/locales/zh.json | 3 +- 21 files changed, 1137 insertions(+), 40 deletions(-) create mode 100644 docs/tasks/T-1200/T-1253-screen-assign-now/brief.md create mode 100644 docs/tasks/T-1200/T-1253-screen-assign-now/r2.md create mode 100644 docs/tasks/T-1200/T-1253-screen-assign-now/r3.md create mode 100644 docs/tasks/T-1200/T-1253-screen-assign-now/report.md create mode 100644 docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt create mode 100644 src/components/AssignLocalVmScreen.tsx diff --git a/docs/tasks/T-1200/T-1253-screen-assign-now/brief.md b/docs/tasks/T-1200/T-1253-screen-assign-now/brief.md new file mode 100644 index 00000000..f7597412 --- /dev/null +++ b/docs/tasks/T-1200/T-1253-screen-assign-now/brief.md @@ -0,0 +1,38 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1253 共用电脑:未分配屏幕时可一键分配 + +## 背景 +共用电脑(服务器上的 Local VM,多屏模式)最多 4 块虚拟屏。屏幕只在智能体开始跑任务时由 `server/local-vm-screens.ts` 的 `LocalVmScreenPool.request()` / `waitFor()` 分配,闲置后收回。用户在桌面面板打开一个当前没跑任务的智能体,会看到 `vm.screens.unassignedBot` 提示「这个智能体目前没有分配屏幕,下次运行时会自动分配。」,只能等着,什么也做不了。出现这条提示的地方:`src/components/remote-desktop-panel.tsx` 约 212、241 行;`src/lib/local-vm-screen-state.ts` 约 65 行;`server/routes/computers.ts` 约 384、456、480 行的 409 `screen_not_assigned`。 + +## 要做 +1. 服务端新增一个接口(放在现有 local-computer 路由旁边,权限和现有查看/加入接口一致):替当前智能体申请一块屏幕。 + - 有空屏,或能收回别人的闲置屏,就立即分配,返回屏幕号。 + - 4 块都忙,就进入现有排队(复用 pool 的排队机制,不另造),返回排队位置。 + - 用户主动申请的屏幕同样按现有闲置规则到时收回。不能抢正在跑任务的屏幕,也不能改变已有智能体的分配。 +2. 前端:所有显示「未分配屏幕」的地方,在提示下面加一个按钮「现在分配屏幕」(英文 Assign a screen now)。 + - 点了以后调用上面的接口。 + - 拿到屏幕就接着走原来的打开桌面流程。 + - 在排队时显示现有的排队文案 `vm.screens.queued`(第几位),并轮询状态,轮到后自动打开。 + - 提示文案改为「这个智能体目前没有分配屏幕。」,后半句去掉,改由按钮说明。 + - zh/en 都要改,不出现 Boat 字样。 +3. 单屏模式或没开多屏时,行为和现在完全一致。 + +## 不要动 +`server/instabox/`、`server/cloud-backend.ts`、`CloudBackendPicker.tsx`、`AccessSection.tsx`、快照相关文件(这些另一个分支正在改)。改动尽量集中在屏幕池、computers 路由、remote-desktop-panel 和 local-vm-screen-state 这几处。 + +## 验收 +- 补隔离测试,覆盖以下情况: + - 有空屏时立即分配; + - 屏满时进入排队并返回位置; + - 不抢正在跑任务的屏幕; + - 非多屏模式下行为不变; + - 前端按钮点击后的流程(组件测试)。 +- 改动涉及的测试文件逐个跑过,包括 `server/local-vm-screens*.test.ts`、`server/routes/computers.test.ts`、相关前端测试,以及 `scripts/testing/file-size-ratchet.test.ts`。 +- `pnpm exec tsc -p tsconfig.server.json` 和前端 tsc 都要通过;改动过的文件跑 `oxlint --deny-warnings`。 +- 写 report.md(短)和 verification.txt。verification.txt 里不写 IP、地址、域名、uuid。 diff --git a/docs/tasks/T-1200/T-1253-screen-assign-now/r2.md b/docs/tasks/T-1200/T-1253-screen-assign-now/r2.md new file mode 100644 index 00000000..48125cf4 --- /dev/null +++ b/docs/tasks/T-1200/T-1253-screen-assign-now/r2.md @@ -0,0 +1,25 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1253 第 2 轮:按第 1 轮审核修 2 项必改 + +原任务见本文件夹 brief.md,工作区已有改动全部保留,只修下面两项。 + +## 必改 +1. **起屏期间申请流程被提前隐藏,失败后看不到重试入口。** 涉及 `src/components/AssignLocalVmScreen.tsx` 约 53 行、`src/components/ComputerPanel.tsx` 约 1300 行、`src/components/remote-desktop-panel.tsx` 约 97 行。 + - 问题:排队结束后,服务端可能返回 `screen` 非空、`ready:false`、`desktopReady:true`。这时主面板进入 `vm-unavailable`,远程面板清空 `screenWait`,两处都把申请组件隐藏了。之后如果起屏失败,子组件里记了错误、停了轮询,但错误和重试按钮都看不到;父面板这时也已经停止状态轮询。 + - 要求:申请流程从点击开始一直显示,直到成功打开桌面或明确失败为止。失败时显示错误和「重试」按钮。父面板在这期间继续轮询状态。 + - 补组件测试,覆盖三种情况:排队后屏幕在启动中、启动失败、重试成功。 +2. **verification.txt 脱敏有遗漏。** + - 第 314、320 行及之后重复出现的错误文本里有 IPv6 地址,结构化的 `address` 字段里也有,都要脱敏。 + - 第 10 行等处,测试文件名被误替换成 `[redacted-host].ts`,要恢复成仓库内的相对文件名,只脱敏真实地址。 + +## 验收 +- 改动涉及的测试逐个跑过。 +- 前端 tsc 通过;改动过的文件跑 `oxlint --deny-warnings`。 +- report.md 追加一节第 2 轮(写短)。 +- 用 grep 检查 verification.txt:不能有 IPv4、IPv6、域名、uuid。 diff --git a/docs/tasks/T-1200/T-1253-screen-assign-now/r3.md b/docs/tasks/T-1200/T-1253-screen-assign-now/r3.md new file mode 100644 index 00000000..82d4f822 --- /dev/null +++ b/docs/tasks/T-1200/T-1253-screen-assign-now/r3.md @@ -0,0 +1,27 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1253 第 3 轮:修第 2 轮审核的 1 项必改 + +原任务见本文件夹 brief.md 和 r2.md。工作区里已有的改动全部保留。这一轮只修下面一项,再处理一条整理,不做别的改动。 + +## 必改 +在 `src/components/ComputerPanel.tsx` 约 456 行,已经拿到屏幕之后,如果状态轮询收到 `screen_not_assigned`,这个分支会直接 return,没有 reject `openAssignedScreen` 的 Promise。结果 `AssignLocalVmScreen.tsx` 约 37 行一直卡在 `await assigned.current()`: +- 按钮一直停在「进行中」; +- 用户看不到错误,也没有重试入口。 + +改法:在这个分支里,先取出 `openAssignedScreen.current` 并把它清空,再调用 `reject(e)`。然后检查同一个 Promise 的其他退出路径,确认每条都会 resolve 或 reject,包括组件卸载和切换智能体。 + +补回归测试:拿到屏幕后,轮询返回 `screen_not_assigned`,断言按钮回到可点状态,并显示错误和「重试」。 + +## 整理 +把 `verification.txt` 里已经修正过的早期失败测试记录删掉,只保留最终有效的记录。 + +## 验收 +- 改动涉及的测试文件逐个跑过,包括 `AssignLocalVmScreen`、`ComputerPanel` 和 `remote-desktop-panel` 的相关测试。 +- 前端 tsc 通过。改动过的文件跑 `oxlint --deny-warnings`。 +- report.md 追加一节「第 3 轮」,写短。verification.txt 里不写 IP、地址、域名、uuid。 diff --git a/docs/tasks/T-1200/T-1253-screen-assign-now/report.md b/docs/tasks/T-1200/T-1253-screen-assign-now/report.md new file mode 100644 index 00000000..262df5c5 --- /dev/null +++ b/docs/tasks/T-1200/T-1253-screen-assign-now/report.md @@ -0,0 +1,53 @@ +改了什么:新增申请屏幕接口,复用原队列与闲置回收;两个桌面入口增加中英文按钮,排队后自动打开。文件清单: + +- `docs/tasks/T-1200/T-1253-screen-assign-now/report.md` +- `docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt` +- `server/index.ts` +- `server/local-vm-screen-runtime.test.ts` +- `server/local-vm-screen-runtime.ts` +- `server/local-vm-screens.e2e.test.ts` +- `server/local-vm-screens.test.ts` +- `server/local-vm-screens.ts` +- `server/routes/computers.test.ts` +- `server/routes/computers.ts` +- `src/components/AssignLocalVmScreen.tsx` +- `src/components/ComputerPanel.takeover.test.ts` +- `src/components/ComputerPanel.tsx` +- `src/components/remote-desktop-panel.open.test.ts` +- `src/components/remote-desktop-panel.screen.test.ts` +- `src/components/remote-desktop-panel.tsx` +- `src/locales/en.json` +- `src/locales/zh.json` + +为什么:闲置智能体无需运行任务即可打开桌面;保留已有分配、忙屏保护和单屏行为。 + +测试:23 个文件、283 个测试逐个通过(`pnpm exec vitest run <文件> --maxWorkers=1`);屏幕 e2e 最终连续三轮均为 16/16 通过。`pnpm exec tsc -p tsconfig.server.json`、`pnpm exec tsc -p tsconfig.json`、`pnpm exec oxlint --deny-warnings <全部改动代码文件>`、`git diff --check` 均通过;完整命令与脱敏输出见 verification.txt。验证说明文档在工作树缺失,使用现有隔离 fixture 完成验证。 + +## 第 2 轮 + +改了什么:保留排队、起屏、打开桌面期间的申请状态;失败显示错误和「重试」,父面板持续轮询未就绪的屏幕。补齐两个入口的起屏、失败重试及打开失败测试;恢复验证记录中的测试路径,补齐 IPv6 脱敏。本轮文件(与 git status 一致): + +- `src/components/AssignLocalVmScreen.tsx` +- `src/components/ComputerPanel.tsx` +- `src/components/ComputerPanel.takeover.test.ts` +- `src/components/remote-desktop-panel.tsx` +- `src/components/remote-desktop-panel.screen.test.ts` +- `docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt` +- `docs/tasks/T-1200/T-1253-screen-assign-now/report.md` + +为什么:拿到屏幕号不代表桌面就绪,不能因此隐藏申请和重试入口;日志只脱敏真实地址,不破坏测试文件名。 + +测试:`pnpm exec vitest run <文件> --maxWorkers=1` 逐文件通过:23 个文件、289 项;其中屏幕 e2e 连续 3 轮各 16/16。`pnpm exec tsc -p tsconfig.json`、`pnpm exec tsc -p tsconfig.server.json`、`pnpm exec oxlint --deny-warnings <上述 5 个代码文件>`、`git diff --check` 通过;`grep -niP` 检查 IPv4、IPv6、域名、UUID 无匹配,并额外核验公共域名后缀及仓库文件路径。完整命令与脱敏输出见 verification.txt。仓库缺少验证 README,e2e 使用独立临时数据目录和模拟运行时;未提交 git。 + +## 第 3 轮 + +改了什么:统一先清空申请引用再 reject,覆盖 `screen_not_assigned`、其他终止错误、卸载及切换智能体/会话/面板;取得控制权期间仍保留可取消的 Promise。补 10 项回归,验证错误、可点击的「重试」、重试成功及取消。验证记录仅保留最终有效输出。本轮文件(与 git status 一致): + +- `src/components/ComputerPanel.tsx` +- `src/components/ComputerPanel.takeover.test.ts` +- `docs/tasks/T-1200/T-1253-screen-assign-now/report.md` +- `docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt` + +为什么:避免拿到屏幕后 Promise 悬空、按钮卡在「进行中」,确保失败可以重试,离开当前面板时结束等待。 + +测试:`pnpm exec vitest run <文件> --maxWorkers=1` 逐文件通过 23 个文件、299 项;AssignLocalVmScreen 由两个面板的真实组件集成测试覆盖,屏幕 e2e 使用隔离 fixture 连续 3 轮各 16/16。`pnpm exec tsc -p tsconfig.json`、`pnpm exec tsc -p tsconfig.server.json`、`pnpm exec oxlint --deny-warnings src/components/ComputerPanel.tsx src/components/ComputerPanel.takeover.test.ts`、`git diff --check` 均通过。`grep -niP` 脱敏检查无匹配;live 测试通过但有已有连接拒绝诊断,完整脱敏输出见 verification.txt。未提交 git。 diff --git a/docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt b/docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt new file mode 100644 index 00000000..b8230ab9 --- /dev/null +++ b/docs/tasks/T-1200/T-1253-screen-assign-now/verification.txt @@ -0,0 +1,427 @@ +第 3 轮最终验证(替换已被修正或复跑取代的早期记录) +所有测试逐文件执行;AssignLocalVmScreen 使用真实组件,由两个面板的集成测试覆盖。 +仓库缺少 docs/verification/README.md;服务器 e2e 使用现有独立临时数据目录、模拟运行时和子进程 fixture,不使用用户应用或数据。 +ComputerPanel.live.test.ts 通过但仍有连接拒绝诊断,完整输出已脱敏保留。 + +$ crybot-link-deps +crybot-link-deps: linked 73 entries from [preinstalled-dependencies] into [worktree]/node_modules +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.android.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 7 passed (7) + Start at 14:03:30 + Duration 2.37s (transform 1.26s, setup 646ms, import 1.01s, tests 465ms, environment 130ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.browser.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 7 passed (7) + Start at 14:03:33 + Duration 2.24s (transform 1.60s, setup 673ms, import 1.42s, tests 37ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.engine.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 18 passed (18) + Start at 14:03:36 + Duration 2.35s (transform 1.34s, setup 689ms, import 1.07s, tests 354ms, environment 122ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.focus.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 12 passed (12) + Start at 14:03:39 + Duration 2.45s (transform 1.25s, setup 611ms, import 1.05s, tests 546ms, environment 129ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.i18n.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 3 passed (3) + Start at 14:03:42 + Duration 892ms (transform 540ms, setup 656ms, import 112ms, tests 15ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.instabox.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 1 passed (1) + Start at 14:03:43 + Duration 2.10s (transform 1.34s, setup 637ms, import 1.12s, tests 98ms, environment 134ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.lazy.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 9 passed (9) + Start at 14:03:46 + Duration 2.59s (transform 1.43s, setup 651ms, import 1.23s, tests 472ms, environment 128ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.live.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + +AggregateError: + at internalConnectMultiple (node:net:1492:18) + at afterConnectMultiple (node:net:2161:7) { + code: 'ECONNREFUSED', + [errors]: [ + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + }, + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + } + ] +} +AggregateError: + at internalConnectMultiple (node:net:1492:18) + at afterConnectMultiple (node:net:2161:7) { + code: 'ECONNREFUSED', + [errors]: [ + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + }, + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + } + ] +} +AggregateError: + at internalConnectMultiple (node:net:1492:18) + at afterConnectMultiple (node:net:2161:7) { + code: 'ECONNREFUSED', + [errors]: [ + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + }, + Error: connect ECONNREFUSED [redacted-address] + at createConnectionError (node:net:2124:14) + at afterConnectMultiple (node:net:2154:16) { + errno: -111, + code: 'ECONNREFUSED', + syscall: 'connect', + address: '[redacted-address]', + port: 3000 + } + ] +} + + Test Files 1 passed (1) + Tests 5 passed (5) + Start at 14:03:49 + Duration 2.08s (transform 1.31s, setup 712ms, import 1.02s, tests 84ms, environment 138ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.simple.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 45 passed (45) + Start at 14:03:51 + Duration 2.18s (transform 1.42s, setup 745ms, import 1.13s, tests 191ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.tabs.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 11 passed (11) + Start at 14:03:54 + Duration 3.52s (transform 1.48s, setup 728ms, import 1.20s, tests 1.35s, environment 128ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.takeover.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 44 passed (44) + Start at 14:03:58 + Duration 28.23s (transform 1.42s, setup 633ms, import 1.21s, tests 26.13s, environment 138ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/ComputerPanel.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 5 passed (5) + Start at 14:04:27 + Duration 860ms (transform 565ms, setup 720ms, import 26ms, tests 5ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/remote-desktop-panel.focus.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 4 passed (4) + Start at 14:04:28 + Duration 1.78s (transform 998ms, setup 702ms, import 635ms, tests 162ms, environment 141ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/remote-desktop-panel.open.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 11 passed (11) + Start at 14:04:30 + Duration 2.03s (transform 967ms, setup 683ms, import 613ms, tests 481ms, environment 134ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/remote-desktop-panel.screen.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 14 passed (14) + Start at 14:04:33 + Duration 1.62s (transform 917ms, setup 632ms, import 598ms, tests 128ms, environment 143ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/components/remote-desktop-panel.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 14:04:35 + Duration 871ms (transform 572ms, setup 731ms, import 17ms, tests 4ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run src/lib/local-vm-screen-state.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 5 passed (5) + Start at 14:04:36 + Duration 801ms (transform 520ms, setup 666ms, import 18ms, tests 3ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run scripts/testing/file-size-ratchet.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 1 passed (1) + Start at 14:04:37 + Duration 1.52s (transform 529ms, setup 689ms, import 8ms, tests 711ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screens.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 35 passed (35) + Start at 14:04:39 + Duration 848ms (transform 524ms, setup 669ms, import 24ms, tests 45ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screen-runtime.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 20 passed (20) + Start at 14:04:41 + Duration 2.15s (transform 1.41s, setup 651ms, import 1.12s, tests 276ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screen-layout.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 3 passed (3) + Start at 14:04:43 + Duration 1.09s (transform 733ms, setup 689ms, import 281ms, tests 5ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/routes/computers.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 17 passed (17) + Start at 14:04:45 + Duration 1.98s (transform 1.41s, setup 661ms, import 1.14s, tests 75ms, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screens.e2e.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 16 passed (16) + Start at 14:04:47 + Duration 65.22s (transform 770ms, setup 675ms, import 304ms, tests 64.13s, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screens.e2e.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 16 passed (16) + Start at 14:05:53 + Duration 63.77s (transform 802ms, setup 709ms, import 312ms, tests 62.64s, environment 0ms) + + +Exit code: 0 + +$ pnpm exec vitest run server/local-vm-screens.e2e.test.ts --maxWorkers=1 + + RUN v4.1.11 [worktree] + + + Test Files 1 passed (1) + Tests 16 passed (16) + Start at 14:06:57 + Duration 63.80s (transform 693ms, setup 655ms, import 235ms, tests 62.79s, environment 0ms) + + +Exit code: 0 + +$ pnpm exec tsc -p tsconfig.json + +Exit code: 0 + +$ pnpm exec tsc -p tsconfig.server.json + +Exit code: 0 + +$ pnpm exec oxlint --deny-warnings src/components/ComputerPanel.tsx src/components/ComputerPanel.takeover.test.ts + +Exit code: 0 + +$ grep -niP '(? computerControl.snapshot(screenControlKey(n)).held, bot: id => { const bot = store.bot(id); return bot ? { id: bot.id, name: bot.name } : null; }, prepare: async (botId, owner) => { - await waitForLocalVmUpdate(SHARED_LOCAL_VM_TARGET, owner.threadId, () => computerWaitHooks.active(owner)); - const status = await readyLocalVmForTurn(botId, SHARED_LOCAL_VM_TARGET, () => computerWaitHooks.active(owner), true); + if (owner) await waitForLocalVmUpdate(SHARED_LOCAL_VM_TARGET, owner.threadId, () => computerWaitHooks.active(owner)); + const status = await readyLocalVmForTurn(botId, SHARED_LOCAL_VM_TARGET, () => !owner || computerWaitHooks.active(owner), true); if (!status.ready || !status.runtime) throw new Error("The multi-screen desktop is not ready"); if (status.runtime === "container" || status.screens_fallback) return { runtime: status.runtime, single: true }; if (status.screens_layout !== "multi") throw new Error("The multi-screen layout is not ready"); diff --git a/server/local-vm-screen-runtime.test.ts b/server/local-vm-screen-runtime.test.ts index 661d271f..de61d298 100644 --- a/server/local-vm-screen-runtime.test.ts +++ b/server/local-vm-screen-runtime.test.ts @@ -341,3 +341,22 @@ it("stops orphaned running screens during server recovery before allocating them expect(f.runtime.password(2)).toBeNull(); expect(f.runtime.payload().slots.every(slot => slot.bot === null && slot.state === "off")).toBe(true); }); + +it("manual claims prepare the VM, share the task queue, and retire while idle", async () => { + const f = fixture(2); + expect(await f.runtime.requestScreen("a")).toEqual({ screen: 1 }); + await vi.advanceTimersByTimeAsync(0); + expect(f.runtime.ready(1)).toBe(true); + await f.runtime.claim("b", f.owner("b")); + expect(await f.runtime.requestScreen("c")).toEqual({ queued: true, position: 1 }); + expect(await f.runtime.requestScreen("c")).toEqual({ queued: true, position: 1 }); + await vi.advanceTimersByTimeAsync(1500); + expect(f.runtime.assigned("a")).toBeNull(); + expect(f.runtime.assigned("b")).toBe(2); + expect(f.runtime.assigned("c")).toBe(1); +}); +it("manual claims leave the single desktop alone", async () => { + const f = fixture(1); + expect(await f.runtime.requestScreen("a")).toBeNull(); + expect(startScreen).not.toHaveBeenCalled(); +}); diff --git a/server/local-vm-screen-runtime.ts b/server/local-vm-screen-runtime.ts index 733d2631..cd77b914 100644 --- a/server/local-vm-screen-runtime.ts +++ b/server/local-vm-screen-runtime.ts @@ -42,7 +42,7 @@ export function createLocalVmScreenRuntime(deps: { busy(screen: number): boolean; held(screen: number): boolean; bot(botId: string): { id: string; name: string } | null; - prepare(botId: string, owner: TurnOwner): Promise; + prepare(botId: string, owner?: TurnOwner): Promise; bind(owner: TurnOwner, resource: string): Promise; wait: ComputerWaitHooks; estimate(resource: string): number | undefined; @@ -169,6 +169,18 @@ export function createLocalVmScreenRuntime(deps: { } } + async function requestScreen(botId: string) { + pendingClaims++; + try { + await recover(); + if (!enabled()) return null; + const prepared = await deps.prepare(botId); + if (typeof prepared !== "string") { runtime = prepared.runtime; fallback = true; return null; } + runtime = prepared; + return await pool.requestAvailable({ botId, threadId: "viewer", generation: 0, enqueuedAt: Date.now() }); + } finally { pendingClaims--; } + } + function payload(): ScreenListPayload { if (!enabled()) return { layout: "single", screens: 1, slots: [], queue: [] }; const now = Date.now(), snapshot = pool.snapshot(); @@ -187,7 +199,7 @@ export function createLocalVmScreenRuntime(deps: { } return { - enabled, pool, claim, recover, payload, assigned, targetFor, touch, active, + enabled, pool, claim, requestScreen, recover, payload, assigned, targetFor, touch, active, claiming: () => pendingClaims > 0, password: (n: number) => passwords.get(n) ?? null, ready: (n: number) => enabled() && pool.snapshot().slots.some(slot => slot.screen === n && slot.state === "ready"), diff --git a/server/local-vm-screens.e2e.test.ts b/server/local-vm-screens.e2e.test.ts index 9a28a696..17a5091a 100644 --- a/server/local-vm-screens.e2e.test.ts +++ b/server/local-vm-screens.e2e.test.ts @@ -288,3 +288,46 @@ describe("shared Local VM screens through the server", () => { }); }); + +it("manually assigns an idle bot, joins its ready desktop, and reclaims it on idle", async () => { + const f = await fixture(); + const bot = await f.bot(0); + const path = `/api/bots/${bot.id}/local-computer`; + const assigned = await f.request("POST", `${path}/assign-screen`, {}); + expect(assigned.screen.screen).toBe(1); + await f.until(() => f.request("GET", path), status => status.ready); + const repeated = await f.request("POST", `${path}/assign-screen`, {}); + expect(repeated.screen.screen).toBe(1); + const controlLeaseId = "fixture-manual-screen-lease"; + await f.request("POST", `/api/bots/${bot.id}/computer/control`, { action: "take", target: "local/shared-s1", controlLeaseId }); + const joined = await f.request("POST", `${path}/join?controlLeaseId=${controlLeaseId}`, {}); + expect(new URLSearchParams(new URL(joined.joinUrl).hash.slice(1)).get("path")).toBe("websockify?token=s1"); + await f.request("POST", `/api/bots/${bot.id}/computer/control`, { action: "release", controlLeaseId }); + await f.until(f.screenList, list => list.slots.every((slot: any) => !slot.bot)); +}); + +it("manual requests queue behind four running tasks and automatically get the released screen", async () => { + const f = await fixture(); + const bots = []; + for (let i = 0; i < 5; i++) bots.push(await f.bot(i)); + for (let i = 0; i < 4; i++) { await f.send(bots[i].id); await f.dump(i); } + const path = `/api/bots/${bots[4].id}/local-computer`; + for (let attempt = 0; attempt < 2; attempt++) { + expect(await f.request("POST", `${path}/assign-screen`, {})).toEqual({ screen: null, queue_position: 1 }); + } + expect(await f.request("GET", path)).toEqual({ screen: null, queue_position: 1 }); + expect((await f.screenList()).slots.map((slot: any) => slot.bot.id)).toEqual(bots.slice(0, 4).map(bot => bot.id)); + f.finish(1); await f.idle(bots[1].id); + const status = await f.until(() => f.request("GET", path), status => status.ready); + expect(status.screen.screen).toBe(2); + expect((await f.screenList()).slots.filter((slot: any) => slot.screen !== 2).map((slot: any) => slot.bot.id)).toEqual([bots[0].id, bots[2].id, bots[3].id]); +}); + +it("manual assignment is a no-op for the single desktop", async () => { + const f = await fixture(1); + const bot = await f.bot(0); + const status = await f.request("POST", `/api/bots/${bot.id}/local-computer/assign-screen`, {}); + expect(status).not.toHaveProperty("screen"); + expect((await f.screenList()).layout).toBe("single"); + expect(f.rows(".actions").some(row => row.action === "run" || row.action === "start")).toBe(false); +}); diff --git a/server/local-vm-screens.test.ts b/server/local-vm-screens.test.ts index 5676f4a9..5187374f 100644 --- a/server/local-vm-screens.test.ts +++ b/server/local-vm-screens.test.ts @@ -516,3 +516,29 @@ describe("screen affinity file", () => { } }); }); + +it("manual requests reclaim only expired idle screens and keep FIFO order", async () => { + const { pool, busy } = fixture(2); + await pool.waitFor(waiter("a"), signal()); + await pool.waitFor(waiter("b"), signal()); + busy.add(1); + expect(await pool.requestAvailable(waiter("c"))).toEqual({ queued: true, position: 1 }); + vi.setSystemTime(Date.now() + IDLE_MS); + expect(await pool.requestAvailable(waiter("d"))).toEqual({ queued: true, position: 1 }); + expect(pool.assigned("a")).toBe(1); + expect(pool.assigned("c")).toBe(2); + expect(pool.assigned("d")).toBeNull(); + await vi.advanceTimersByTimeAsync(IDLE_MS); + expect(pool.assigned("c")).toBeNull(); + expect(pool.assigned("d")).toBe(2); +}); + +it("reclaims enough expired screens to serve earlier waiters before a manual request", async () => { + const { pool } = fixture(2); + await pool.waitFor(waiter("a"), signal()); + await pool.waitFor(waiter("b"), signal()); + pool.request(waiter("c")); + vi.setSystemTime(Date.now() + IDLE_MS); + expect(await pool.requestAvailable(waiter("d"))).toEqual({ screen: 2 }); + expect(pool.assigned("c")).toBe(1); +}); diff --git a/server/local-vm-screens.ts b/server/local-vm-screens.ts index 0c55f1b1..0ca1b5f4 100644 --- a/server/local-vm-screens.ts +++ b/server/local-vm-screens.ts @@ -143,6 +143,19 @@ export class LocalVmScreenPool { return screen === null ? { queued: true, position: this.waitPosition(waiter.botId, waiter.threadId)! } : { screen }; } + async requestAvailable(waiter: ScreenWaiter): Promise<{ screen: ScreenNumber } | { queued: true; position: number }> { + let result = this.request(waiter); + while (!("screen" in result)) { + const expired = this.slots.find(slot => slot.screen <= this.screens && slot.state === "ready" + && !this.deps.isBusy(slot.screen) && !this.busySeen.has(slot.screen) + && this.now() >= slot.lastActiveAt + this.idleMs); + if (!expired) break; + await this.stop(expired); + result = this.request(waiter); + } + return result; + } + waitFor(waiter: ScreenWaiter, signal: AbortSignal): Promise { if (signal.aborted) return Promise.reject(cancelled()); return new Promise((resolve, reject) => { diff --git a/server/routes/computers.test.ts b/server/routes/computers.test.ts index 5e87ad1c..1110088e 100644 --- a/server/routes/computers.test.ts +++ b/server/routes/computers.test.ts @@ -2,6 +2,8 @@ import type { IncomingMessage, ServerResponse } from "node:http"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { LocalVmScreenPool, type ScreenNumber } from "../local-vm-screens.ts"; +import { requiredScope } from "../request-auth.ts"; import type { AppConfig } from "../config.ts"; import type { RequestAuth } from "../request-auth.ts"; import type { BotRecord } from "../store.ts"; @@ -117,7 +119,7 @@ it("refuses a user request to create a team computer while locked", async () => }); describe("shared screen routes", () => { - async function setup(assigned: number | null = 2, multi = true) { + async function setup(assigned: number | null = 2, multi = true, pool?: LocalVmScreenPool) { const container = await import("../container-computer.ts"); const { z } = await import("zod"); vi.spyOn(container, "containerComputerStatus").mockResolvedValue({ @@ -129,6 +131,8 @@ describe("shared screen routes", () => { const bot = { id: "screen-bot", threadId: "screen-thread", name: "Screen bot" } as BotRecord; const beat = vi.fn(() => true); const touch = vi.fn(); + const assignedFor = () => pool ? pool.assigned(bot.id) : assigned; + const requestScreen = vi.fn(async () => pool!.requestAvailable({ botId: bot.id, threadId: "viewer", generation: 0, enqueuedAt: Date.now() })); const handler = createComputerRoutes({ cfg: { localVm: { mode: "shared", screens: multi ? 4 : 1 } }, store: { bot: () => bot }, computerPreviewBot: () => bot, computerPreviewSurface: async () => "vm", @@ -138,8 +142,8 @@ describe("shared screen routes", () => { localVmContainerBusy: () => true, localVmImageBusy: { get: () => false }, localVmModeChangeBusy: { get: () => false }, localVmLifecycleBusy: new Set(), localVmLeaseFor: () => ({ current: () => null }), localVmActiveThreads: new Map(), localVmSeen: new Set(), localVmIdleFor: () => ({ touch, cancel: vi.fn() }), localVmIdleMs: () => 600000, - screens: { enabled: () => multi, payload: () => ({ layout: multi ? "multi" : "single" }), assigned: () => assigned, ready: (n: number) => n === assigned, password: () => "screen-password", touch }, - localVmPayload: async () => ({ screens: { layout: "multi", screens: 4, slots: [], queue: [] } }), + screens: { enabled: () => multi, payload: () => ({ layout: multi ? "multi" : "single", queue: pool?.snapshot().waiters.map(w => ({ bot: { id: w.botId }, position: w.position })) ?? [] }), assigned: assignedFor, requestScreen, ready: (n: number) => n === assigned, password: () => "screen-password", touch }, + localVmPayload: async () => ({ ready: true, ...(multi ? { screen: assignedFor() === null ? null : { screen: assignedFor() } } : {}), screens: { layout: "multi", screens: 4, slots: [], queue: [] } }), routines: () => null, } as unknown as ComputerRouteDeps); const request = async (path: string, method = "POST", scopes = ["admin"], remote = false) => { @@ -153,9 +157,42 @@ describe("shared screen routes", () => { } as unknown as RouteContext); return result!; }; - return { request, beat, touch, screenshot }; + return { request, beat, touch, screenshot, requestScreen }; } + it("assigns an available screen, queues behind busy screens, and preserves existing assignments", async () => { + const busy = new Set(); + const pool = new LocalVmScreenPool(4, { idleMs: 600000, isBusy: n => busy.has(n), startScreen: async () => {}, stopScreen: async () => {} }); + try { + for (const [index, id] of ["a", "b", "c", "d"].entries()) { + await pool.waitFor({ botId: id, threadId: id, generation: 1, enqueuedAt: Date.now() }, new AbortController().signal); + busy.add((index + 1) as ScreenNumber); + } + const { request } = await setup(null, true, pool); + const path = "/api/bots/screen-bot/local-computer/assign-screen"; + for (let n = 0; n < 2; n++) expect(await request(path)).toEqual({ status: 200, body: { screen: null, queue_position: 1 } }); + expect(await request("/api/bots/screen-bot/local-computer", "GET")).toEqual({ status: 200, body: { screen: null, queue_position: 1 } }); + expect(pool.snapshot().slots.map(slot => slot.botId)).toEqual(["a", "b", "c", "d"]); + busy.delete(2); + pool.release("b", "idle"); + await vi.waitFor(() => expect(pool.assigned("screen-bot")).toBe(2)); + expect((await request(path)).body.screen.screen).toBe(2); + expect(pool.snapshot().slots.map(slot => slot.botId)).toEqual(["a", "screen-bot", "c", "d"]); + } finally { await pool.stopAll("shutdown"); } + }); + it("immediately assigns a free screen", async () => { + const pool = new LocalVmScreenPool(4, { idleMs: 600000, isBusy: () => false, startScreen: async () => {}, stopScreen: async () => {} }); + try { + const { request } = await setup(null, true, pool); + expect((await request("/api/bots/screen-bot/local-computer/assign-screen")).body.screen.screen).toBe(1); + } finally { await pool.stopAll("shutdown"); } + }); + it("does not allocate in single-screen mode and uses the same scope as joins", async () => { + const { request, requestScreen } = await setup(null, false); + expect((await request("/api/bots/screen-bot/local-computer/assign-screen")).body).not.toHaveProperty("screen"); + expect(requestScreen).not.toHaveBeenCalled(); + expect(requiredScope("POST", "/api/bots/screen-bot/local-computer/assign-screen")).toBe(requiredScope("POST", "/api/bots/screen-bot/local-computer/join")); + }); it("returns the shared screen list without credentials", async () => { const { request } = await setup(); const result = await request("/api/local-computer", "GET"); diff --git a/server/routes/computers.ts b/server/routes/computers.ts index 1c038470..701cbd16 100644 --- a/server/routes/computers.ts +++ b/server/routes/computers.ts @@ -377,11 +377,26 @@ export function createComputerRoutes(deps: ComputerRouteDeps): RouteHandler { }); } + m = path.match(/^\/api\/bots\/([\w-]+)\/local-computer\/assign-screen$/); + if (m && method === "POST") { + if (!String(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) return json(res, 415, { error: "content-type must be application/json" }); + const bot = computerPreviewBot(m[1], url); + if (!bot) return json(res, 404, { error: "no such bot" }); + if (await computerPreviewSurface(bot, bot.threadId) !== "vm") return json(res, 409, { error: "This conversation is not using the Local VM" }); + if (!deps.screens?.enabled()) return json(res, 200, await localVmPayload(localVmTargetForStatus(bot.id, bot.threadId), auth)); + if (localVmImageBusy.get() || localVmModeChangeBusy.get() || localVmLifecycleBusy.has(SHARED_LOCAL_VM_TARGET.key)) return json(res, 409, { error: "Local VM setup is still running" }); + const result = await deps.screens.requestScreen(bot.id); + if (result && "queued" in result) return json(res, 200, { screen: null, queue_position: result.position }); + return json(res, 200, await localVmPayload(localVmTargetForStatus(bot.id, bot.threadId), auth)); + } m = path.match(/^\/api\/bots\/([\w-]+)\/local-computer$/); if (m && method === "GET") { const bot = computerPreviewBot(m[1], url); if (!bot) return json(res, 404, { error: "no such bot" }); - if (deps.screens?.enabled() && !deps.screens.assigned(bot.id)) return json(res, 409, { error: "screen_not_assigned" }); + if (deps.screens?.enabled() && !deps.screens.assigned(bot.id)) { + const position = deps.screens.payload().queue.find(item => item.bot.id === bot.id)?.position; + return position ? json(res, 200, { screen: null, queue_position: position }) : json(res, 409, { error: "screen_not_assigned" }); + } return json(res, 200, await localVmPayload(localVmTargetForStatus(bot.id, bot.threadId), auth)); } m = path.match(/^\/api\/bots\/([\w-]+)\/local-computer\/(run|start|stop|remove)$/); diff --git a/src/components/AssignLocalVmScreen.tsx b/src/components/AssignLocalVmScreen.tsx new file mode 100644 index 00000000..a7b9ecf8 --- /dev/null +++ b/src/components/AssignLocalVmScreen.tsx @@ -0,0 +1,66 @@ +import { useEffect, useRef, useState } from "react"; +import { api } from "@/state/store"; +import { t } from "@/lib/i18n"; +import { panelErrorText } from "@/lib/computer-messages"; +import type { LocalVmStatus } from "@/lib/local-vm-screen-state"; + +export function AssignLocalVmScreen({ path, position, visible = true, onAssigned }: { + path: string; + position?: number; + visible?: boolean; + onAssigned(): Promise; +}) { + const [pending, setPending] = useState(false); + const [queuePosition, setQueuePosition] = useState(position); + const [error, setError] = useState(null); + const request = useRef(null); + const timer = useRef | undefined>(undefined); + const assigned = useRef(onAssigned); + assigned.current = onAssigned; + useEffect(() => { if (!request.current) setQueuePosition(position); }, [position]); + useEffect(() => () => { request.current?.abort(); clearTimeout(timer.current); request.current = null; }, [path]); + const assign = async () => { + if (request.current) return; + const controller = new AbortController(); + request.current = controller; + setPending(true); + setError(null); + const poll = async (first: boolean): Promise => { + try { + const url = new URL(path, window.location.origin); + if (first) url.pathname += "/assign-screen"; + const status: LocalVmStatus = await api(url.pathname + url.search, { + ...(first ? { method: "POST", body: "{}" } : { background: true }), signal: controller.signal, + }); + if (controller.signal.aborted) return; + setQueuePosition(status.queue_position); + if (status.ready && status.screen !== null) { + await assigned.current(); + if (controller.signal.aborted) return; + request.current = null; + setPending(false); + } else { + timer.current = setTimeout(() => void poll(false), 2000); + } + } catch (cause) { + if (controller.signal.aborted) return; + request.current = null; + setPending(false); + setError(panelErrorText(cause instanceof Error ? cause : String(cause))); + } + }; + await poll(true); + }; + if (!visible && !pending && !error) return null; + return
+

+ {queuePosition ? t("vm.screens.queued", { position: queuePosition }) : t(pending ? "settings.updates.opening" : "vm.screens.unassignedBot")} + {queuePosition ? ` ${t("vm.screens.queueDetail")}` : null} +

+ {(!queuePosition || error) && } + {error &&

{error}

} +
; +} diff --git a/src/components/ComputerPanel.takeover.test.ts b/src/components/ComputerPanel.takeover.test.ts index 95dc1ed3..46d0f131 100644 --- a/src/components/ComputerPanel.takeover.test.ts +++ b/src/components/ComputerPanel.takeover.test.ts @@ -11,7 +11,12 @@ import type { Action, AppState, Bot, InstanceInfo } from "@/state/store"; const fixture = vi.hoisted(() => ({ vm: {} as Record, vmRequests: [] as string[], + vmForegroundRequests: 0, + assignment: null as Record | null, + assignPaths: [] as string[], nextVmStatus: null as Promise> | null, + assignedOpen: null as Promise | null, + nextTake: null as Promise | null, takeoverRenders: [] as Array<{ botId: string; session: string }>, liveRenders: [] as Array<{ name: string; target: string }>, held: false, @@ -25,6 +30,16 @@ const fixture = vi.hoisted(() => ({ beacons: [] as Array<{ url: string; body: Record; keepalive: boolean }>, live: null as null | { target: string; expanded: boolean; onUnreachable: () => void }, })); +vi.mock("./AssignLocalVmScreen", async (importOriginal) => { + const { AssignLocalVmScreen } = await importOriginal(); + return { AssignLocalVmScreen: (props: Parameters[0]) => createElement(AssignLocalVmScreen, { + ...props, + onAssigned: () => { + fixture.assignedOpen = props.onAssigned(); + return fixture.assignedOpen; + }, + }) }; +}); vi.mock("./DesktopCapabilities", async (importOriginal) => ({ ...await importOriginal(), useCaptionChrome: () => ({ padClass: undefined }), @@ -66,7 +81,7 @@ vi.mock("./CloudBackendPicker", () => ({ CloudBackendPicker: () => null })); vi.mock("./bot-settings/RoutinesSection", () => ({ RoutinesSection: () => null })); vi.mock("@/state/store", async (importOriginal) => ({ ...await importOriginal(), - api: async (path: string, init?: RequestInit) => { + api: async (path: string, init?: RequestInit & { background?: boolean }) => { const route = path.split("?")[0]!; if (init?.method === "POST") fixture.posts.push(route); if (route === "/api/bots/scout/computer/control") { @@ -75,6 +90,7 @@ vi.mock("@/state/store", async (importOriginal) => ({ const body = JSON.parse(String(init.body)); fixture.controls.push(body); if (body.action === "take") { + if (fixture.nextTake) await fixture.nextTake; if (fixture.refuse) return { held: false, helpReason: null }; const owned = !fixture.held || fixture.owned; fixture.held = true; @@ -83,9 +99,16 @@ vi.mock("@/state/store", async (importOriginal) => ({ fixture.held = false; return { held: false, helpReason: null }; } + if (route === "/api/bots/scout/local-computer/assign-screen") { + fixture.assignPaths.push(path); + fixture.vm = fixture.assignment!; + return fixture.vm; + } if (route === "/api/bots/scout/local-computer" || route === "/api/bots/rover/local-computer") { fixture.vmRequests.push(path); + if (!init?.background) fixture.vmForegroundRequests += 1; if (fixture.nextVmStatus) return fixture.nextVmStatus; + if (fixture.vm.statusError) throw new Error(String(fixture.vm.statusError)); if (fixture.vm.unassigned) throw new ApiError("screen_not_assigned", 409, { error: "screen_not_assigned" }); return { mode: "per-bot", max_instances: 2, image: true, create_supported: true, container: "running", imageMatches: true, @@ -130,7 +153,7 @@ const draw = () => flushSync(() => root!.render(createElement(BotEditorStore, { dispatch: (action: Action) => { if (action.type !== "computerControl") return; state = { ...state, computerControl: { ...state.computerControl, [action.botId]: { held: action.held, helpReason: action.helpReason, help: action.help } } }; - queueMicrotask(draw); + queueMicrotask(() => { if (root) draw(); }); }, flushBotPatches: async () => null, refreshInstances: async () => {}, @@ -182,7 +205,10 @@ beforeEach(() => { }); fixture.vm = {}; fixture.vmRequests = []; + fixture.vmForegroundRequests = 0; fixture.nextVmStatus = null; + fixture.assignedOpen = null; + fixture.nextTake = null; fixture.takeoverRenders = []; fixture.liveRenders = []; fixture.held = false; @@ -602,3 +628,134 @@ it("treats a screen_not_assigned conflict as waiting and automatically retries", await shownLive(); expect(liveDesktop()!.dataset.target).toBe("local/shared-s4"); }); + +it.each([false, true])("assigns from the main panel and automatically takes control (queued=%s)", async queued => { + fixture.vm = { unassigned: true }; + const ready = { mode: "shared", ready: true, screen: { screen: 3, viewerTarget: "local/shared-s3" } }; + fixture.assignment = queued ? { screen: null, queue_position: 2 } : ready; + fixture.assignPaths = []; + await open(makeBot({ computer: "vm" })); + await press("Assign a screen now"); + expect(fixture.assignPaths[0]).toContain("/local-computer/assign-screen?threadId="); + if (queued) { + expect(document.body.textContent).toContain("Queued · Position 2"); + expect(fixture.controls).toEqual([]); + fixture.vm = ready; + } + await vi.waitFor(() => expect(liveDesktop()?.dataset.target).toBe("local/shared-s3"), { timeout: 5000 }); + expect(fixture.assignPaths).toHaveLength(1); +}); + + +it.each([false, true])("keeps assignment visible while starting, then opens or retries a failure (fail=%s)", async fail => { + vi.useFakeTimers(); + fixture.vm = { unassigned: true }; + fixture.assignment = { screen: null, queue_position: 2 }; + fixture.assignPaths = []; + await open(makeBot({ computer: "vm" })); + await press("Assign a screen now"); + expect(document.body.textContent).toContain("Queued · Position 2"); + const ready = { mode: "shared", ready: true, desktopReady: true, screen: { screen: 3, viewerTarget: "local/shared-s3" } }; + fixture.vm = { ...ready, ready: false }; + await vi.advanceTimersByTimeAsync(20_000); + await settle(); + expect(document.querySelector('[role="status"]')?.textContent).toContain(t("settings.updates.opening")); + expect(button(t("settings.updates.opening"))?.disabled).toBe(true); + expect(fixture.vmForegroundRequests).toBeGreaterThanOrEqual(3); + expect(liveDesktop()).toBeNull(); + if (fail) { + fixture.vm = { statusError: "Screen startup failed" }; + await vi.advanceTimersByTimeAsync(4000); + await settle(); + expect(document.querySelector('[role="alert"]')?.textContent).toContain("Screen startup failed"); + expect(button("Retry")?.disabled).toBe(false); + fixture.assignment = ready; + await press("Retry"); + expect(fixture.assignPaths).toHaveLength(2); + } else { + fixture.vm = ready; + await vi.advanceTimersByTimeAsync(2000); + await settle(); + } + expect(liveDesktop()?.dataset.target).toBe("local/shared-s3"); + expect(document.querySelector('[role="status"]')).toBeNull(); + expect(button(t("settings.updates.opening"))).toBeUndefined(); +}); + + +it("keeps a failed desktop opening retryable after the screen becomes ready", async () => { + vi.useFakeTimers(); + fixture.vm = { unassigned: true }; + fixture.assignment = { ready: true, screen: { screen: 3, viewerTarget: "local/shared-s3" } }; + fixture.refuse = true; + await open(makeBot({ computer: "vm" })); + await press("Assign a screen now"); + expect(liveDesktop()).toBeNull(); + expect(document.querySelector('[role="alert"]')?.textContent).toBe(t("computer.err.confirmControl")); + expect(button("Retry")?.disabled).toBe(false); + fixture.refuse = false; + await press("Retry"); + expect(liveDesktop()?.dataset.target).toBe("local/shared-s3"); + expect(document.querySelector('[role="alert"]')).toBeNull(); +}); + +it.each(["unassigned", "error", "unavailable", "missing-target"])("makes an assigned screen retryable when the next status is %s", async outcome => { + vi.useFakeTimers(); + fixture.vm = { unassigned: true }; + fixture.assignment = { ready: true, screen: { screen: 3, viewerTarget: "local/shared-s3" } }; + await open(makeBot({ computer: "vm" })); + let resolveStatus!: (status: Record) => void; + let rejectStatus!: (cause: Error) => void; + fixture.nextVmStatus = new Promise((resolve, reject) => { resolveStatus = resolve; rejectStatus = reject; }); + await press("Assign a screen now"); + expect(fixture.assignedOpen).not.toBeNull(); + expect(button(t("settings.updates.opening"))?.disabled).toBe(true); + if (outcome === "unassigned") rejectStatus(new ApiError("screen_not_assigned", 409, { error: "screen_not_assigned" })); + else if (outcome === "error") rejectStatus(new Error("Screen startup failed")); + else resolveStatus(outcome === "unavailable" ? { ready: false, problem: "Screen startup failed" } : { ready: true }); + await settle(); + const message = outcome === "unassigned" ? "screen_not_assigned" : outcome === "missing-target" ? t("computer.err.screenUnavailable") : "Screen startup failed"; + expect(document.querySelector('[role="alert"]')?.textContent).toContain(message); + expect(button(t("settings.updates.opening"))).toBeUndefined(); + const retry = document.querySelector('[role="alert"]')!.parentElement!.querySelector("button")!; + expect(retry.textContent).toBe(t("chat.retry")); + expect(retry.disabled).toBe(false); + expect(liveDesktop()).toBeNull(); + fixture.nextVmStatus = null; + flushSync(() => retry.click()); + await settle(); + expect(liveDesktop()?.dataset.target).toBe("local/shared-s3"); + expect(document.querySelector('[role="alert"]')).toBeNull(); +}); + +it.each([ + ["unmount", false], ["bot", false], ["thread", false], + ["unmount", true], ["bot", true], ["thread", true], +] as const)("rejects the pending assigned-screen opening on %s (taking=%s)", async (change, taking) => { + vi.useFakeTimers(); + fixture.vm = { unassigned: true }; + fixture.assignment = { ready: true, screen: { screen: 3, viewerTarget: "local/shared-s3" } }; + await open(makeBot({ computer: "vm" })); + let resolveStatus!: (status: Record) => void; + let resolveTake!: () => void; + if (taking) fixture.nextTake = new Promise(resolve => { resolveTake = resolve; }); + else fixture.nextVmStatus = new Promise(resolve => { resolveStatus = resolve; }); + await press("Assign a screen now"); + expect(fixture.assignedOpen).not.toBeNull(); + const resolved = vi.fn(), rejected = vi.fn(); + void fixture.assignedOpen!.then(resolved, rejected); + if (change === "unmount") { + flushSync(() => root!.unmount()); + root = null; + } else { + shown = change === "bot" ? makeBot({ id: "rover", computer: "vm" }) : makeBot({ threadId: "next", computer: "vm" }); + draw(); + } + await settle(); + expect(rejected).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ name: "AbortError" })); + if (taking) resolveTake(); + else resolveStatus(fixture.assignment!); + await settle(); + expect(resolved).not.toHaveBeenCalled(); + expect(fixture.controls).toHaveLength(taking ? 1 : 0); +}); diff --git a/src/components/ComputerPanel.tsx b/src/components/ComputerPanel.tsx index 8de30b5e..c7dd0ba7 100644 --- a/src/components/ComputerPanel.tsx +++ b/src/components/ComputerPanel.tsx @@ -1,3 +1,4 @@ +import { AssignLocalVmScreen } from "./AssignLocalVmScreen"; import { cloudPanelStatus } from "./instabox-panel"; import { InstaboxSnapshots } from "./InstaboxSnapshots"; import { boatCapableEngine, cloudEngineOf } from "@/lib/remote-desktop"; @@ -302,7 +303,6 @@ export function ComputerPanel({ writeComputerPanelView(bot.id, "data"); dispatch({ type: "dataResultFocusConsumed", requestId: requestedDataResult.requestId }); }, [bot.id, dispatch, requestedDataResult]); - const requestedSignIn = state.signInFocus?.botId === bot.id ? state.signInFocus : undefined; const [signInRequest, setSignInRequest] = useState<{ url: string; site: string; nonce: number }>(); // A published page's Open (usePageOpener): this bot's browser shows, with the page in a new tab. @@ -317,7 +317,6 @@ export function ComputerPanel({ if (view === "browser") setSignInRequest({ url: requestedSignIn.url, site: requestedSignIn.site, nonce: requestedSignIn.requestId }); dispatch({ type: "signInFocusConsumed", requestId: requestedSignIn.requestId }); }, [bot.id, dispatch, requestedSignIn]); - useEffect(() => { let alive = true; const dv = window.cry?.desktopViewer; @@ -345,6 +344,12 @@ export function ComputerPanel({ lastVmScreenTarget.current = { botId: bot.id, computer: bot.computer, target: null }; } const vmSupported = Boolean(selectedInstance?.capabilities?.computerMcp); + const openAssignedScreen = useRef<{ opening: boolean; resolve(): void; reject(cause: unknown): void } | null>(null); + const rejectAssignedScreen = useCallback((cause: unknown) => { + const completion = openAssignedScreen.current; + openAssignedScreen.current = null; + completion?.reject(cause); + }, []); const engineUnavailable = selectedInstance !== undefined && selectedInstance.snapshot.state !== "available"; const engineReason = selectedInstance?.snapshot.reason?.trim().replace(/[.。\s]+$/, ""); const cloudSupported = canWorkOnCloud(cloudEngineOf(selectedInstance)); @@ -378,7 +383,6 @@ export function ComputerPanel({ setPhase("auto-unavailable"); return; } - if (bot.computer === "off") { setPhase("off"); return; @@ -397,11 +401,13 @@ export function ComputerPanel({ } if (bot.computer === "vm") { if (engineUnavailable) { + rejectAssignedScreen(new LocalizedPanelError("computer.err.engineUnavailable", engineReason, "computer.err.engineUnknown")); setError(new LocalizedPanelError("computer.err.engineUnavailable", engineReason, "computer.err.engineUnknown")); setPhase("vm-unavailable"); return; } if (!vmSupported) { + rejectAssignedScreen(new LocalizedPanelError("computer.err.vmEngine")); setError(new LocalizedPanelError("computer.err.vmEngine")); setPhase("vm-unavailable"); return; @@ -416,11 +422,12 @@ export function ComputerPanel({ const waitState = localVmWaitState(status); const screenTarget = status.screen?.viewerTarget; if (screenTarget) lastVmScreenTarget.current.target = screenTarget; - if (waitState) { + if (waitState || (status.screen && !status.ready)) { setPhase("vm-waiting"); setError(null); retryTimer = window.setTimeout(() => setRetry((n) => n + 1), localVmPollInterval(status)); } else if (status.ready) { + if (openAssignedScreen.current && !localVmTakeoverTarget("vm", status, true)) throw new LocalizedPanelError("computer.err.screenUnavailable"); vmReadinessAttempts.current = 0; setPhase("vm"); } else if ( @@ -439,6 +446,7 @@ export function ComputerPanel({ retryTimer = window.setTimeout(() => setRetry((n) => n + 1), 2000); } else { + rejectAssignedScreen(new LocalizedPanelError("computer.err.vmOpenSettings", status.problem, "computer.err.vmNotReady")); const canCreateHere = status.mode === "per-bot" && status.container === "missing" && @@ -452,6 +460,7 @@ export function ComputerPanel({ }) .catch((e) => { if (!alive) return; + rejectAssignedScreen(e); if (e instanceof ApiError && e.status === 409 && e.body?.error === "screen_not_assigned") { setVmStatus((current) => localVmUnassignedStatus(current)); setError(null); @@ -609,6 +618,7 @@ export function ComputerPanel({ canManageCloud, livePlace, threadPath, + rejectAssignedScreen, ]); useEffect(() => { if (phase !== "busy-boat") return; @@ -679,7 +689,6 @@ export function ComputerPanel({ }, }); }, [cloudPreviewReady, bot.id, bot.threadId]); - useEffect(() => { if (panelView !== "computer" || !cloudPreviewReady || viewerOpen || liveHere || !pageVisible || pending || controlPending) return; let inFlight = false; @@ -736,7 +745,6 @@ export function ComputerPanel({ clearInterval(timer); }; }, [panelView, cloudPreviewReady, threadPath, cloudBackend, viewerOpen, liveHere, pageVisible, pending, controlPending, previewRetry]); - useEffect(() => { if (panelView !== "computer" || phase !== "vm" || !computerStatusCurrent || viewerOpen || liveHere || !pageVisible) return; const controller = new AbortController(); @@ -774,7 +782,6 @@ export function ComputerPanel({ window.clearInterval(timer); }; }, [panelView, phase, computerStatusCurrent, threadPath, viewerOpen, liveHere, pageVisible, bot.busy, setError, setPreviewError, setVmFrame]); - // The first capture attempt is what makes macOS show the Screen Recording prompt (no reliable pre-grant on macOS // 15+), so repeated empty frames mean denied. const [localMisses, setLocalMisses] = useState(0); @@ -798,7 +805,6 @@ export function ComputerPanel({ clearInterval(timer); }; }, [panelView, phase, computerStatusCurrent, isLinux, pageVisible, bot.busy, bot.id, bot.threadId]); - const frameSrc = !computerStatusCurrent ? null : phase === "vm" ? vmFrame @@ -808,7 +814,6 @@ export function ComputerPanel({ ? polledFrame && `data:${polledFrame.mime};base64,${polledFrame.png}` : null; const previewOpensDesktop = Boolean(frameSrc && (liveTarget || cloudPreviewReady)); - const signInHelp = control.help ?? null; const signInAsker = signInHelp ? state.bots.find((candidate) => candidate.id === signInHelp.botId)?.name ?? bot.name : bot.name; useEffect(() => { @@ -831,9 +836,7 @@ export function ComputerPanel({ }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [bot.id, threadPath]); - const setNativeBrowserControl = useCallback(async (): Promise => true, []); - const transitionControl = useCallback(async (action: ComputerControlAction) => { // Security: every computer surface must gate Electron's direct browser host; the server hold is bot-wide, and a // shell-capable agent could otherwise bypass the server proxy while the person drives. @@ -844,7 +847,6 @@ export function ComputerPanel({ setNativeBrowserControl, }); }, [panelView, requestControl, setNativeBrowserControl]); - const controlAction = useCallback(async (action: ComputerControlAction): Promise => { setControlPending(true); setError(null); @@ -858,7 +860,6 @@ export function ComputerPanel({ setControlPending(false); } }, [transitionControl]); - const bannerTakesControl = Boolean(signInHelp && signInHelp.place !== "browser" && signInHelp.place !== "local" && (cloudPreviewReady || phase === "vm" || currentTeamComputer)); const signInBanner = (take?: () => void) => signInHelp && ( @@ -869,7 +870,6 @@ export function ComputerPanel({ onError={setError} /> ); - const openDesktop = async () => { const controller = new AbortController(); desktopJoin.current = controller; @@ -893,7 +893,17 @@ export function ComputerPanel({ } } }; - + useEffect(() => () => rejectAssignedScreen(new DOMException("Screen assignment cancelled", "AbortError")), [connectionKey, panelView, rejectAssignedScreen]); + useEffect(() => { + if (!openAssignedScreen.current || openAssignedScreen.current.opening || phase !== "vm" || !liveTarget) return; + const completion = openAssignedScreen.current; + completion.opening = true; + setControlPending(true); + void takeover.take().then(completion.resolve, completion.reject).finally(() => { + if (openAssignedScreen.current === completion) openAssignedScreen.current = null; + setControlPending(false); + }); + }); const takeBlocked = pending === "join" || (phase === "vm" && !liveTarget); const takeControl = async (expand = false) => { if (!liveTarget) return cloudPreviewReady ? openDesktop() : undefined; @@ -908,7 +918,6 @@ export function ComputerPanel({ setControlPending(false); } }; - const handBack = async () => { setError(null); void window.cry?.desktopViewer?.close(bot.id); @@ -1291,9 +1300,8 @@ export function ComputerPanel({ )} {computerStatusCurrent && bot.computer === "cloud" && cloudBackend === "vps" && (phase === "ready" || phase === "starting") && !liveHere && {t("computer.badge.vps")}} - {phase === "vm-waiting" &&

- {emptyState["vm-waiting"]} {vmStatus && localVmWaitState(vmStatus) === "queued" && t("vm.screens.queueDetail")} -

} + {bot.computer === "vm" && new Promise((resolve, reject) => { openAssignedScreen.current = { opening: false, resolve, reject }; setRetry(n => n + 1); })} />}
{liveHere && liveDesktopTarget ? ( }> diff --git a/src/components/remote-desktop-panel.open.test.ts b/src/components/remote-desktop-panel.open.test.ts index 8cfa50e3..2a5b34ed 100644 --- a/src/components/remote-desktop-panel.open.test.ts +++ b/src/components/remote-desktop-panel.open.test.ts @@ -161,7 +161,7 @@ it.each([2, null, "conflict"])("resolves a remote Local VM screen before taking expect(fixture.controls[0]).toMatchObject({ action: "take", target: "local/shared-s2" }); expect(fixture.joinBodies).toEqual([{ target: "local/shared-s2" }]); } else { - await vi.waitFor(() => expect(document.querySelector("[role=alert]")?.textContent).toContain(screen === "conflict" ? "This bot has no screen assigned yet" : "Position 3")); + await vi.waitFor(() => expect(document.querySelector("[role=status]")?.textContent).toContain(screen === "conflict" ? "This bot has no screen assigned yet" : "Position 3")); expect(fixture.controls).toEqual([]); expect(fixture.joins).toEqual([]); expect(fixture.viewerCloses).toBe(0); diff --git a/src/components/remote-desktop-panel.screen.test.ts b/src/components/remote-desktop-panel.screen.test.ts index a5449ae7..278630ce 100644 --- a/src/components/remote-desktop-panel.screen.test.ts +++ b/src/components/remote-desktop-panel.screen.test.ts @@ -5,7 +5,8 @@ import { afterEach, beforeEach, expect, it, vi } from "vitest"; import { setLocale } from "@/lib/i18n"; import type { Bot } from "@/state/store"; -const fixture = vi.hoisted(() => ({ status: vi.fn() })); +const fixture = vi.hoisted(() => ({ status: vi.fn(), assign: vi.fn(), openDesktop: vi.fn() })); +vi.mock("./computer-takeover", () => ({ openProviderDesktop: fixture.openDesktop })); vi.mock("./DesktopCapabilities", () => ({ useCaptionChrome: () => ({ dragProps: {} }) })); vi.mock("@/lib/page-visible", () => ({ usePageVisible: () => false })); vi.mock("./BrowserPanel", () => ({ BrowserPanel: () => null })); @@ -13,7 +14,9 @@ vi.mock("./RoutinesPage", () => ({ RoutineEditor: () => null })); vi.mock("@/state/store", async (importOriginal) => ({ ...await importOriginal(), api: (path: string, init?: RequestInit & { background?: boolean }) => { + if (path === "/api/bots/scout/local-computer/assign-screen") return fixture.assign(init); if (path === "/api/bots/scout/local-computer") return fixture.status(init); + if (path === "/api/bots/scout/computer/viewer-close") return Promise.resolve({}); throw new Error(`Unexpected request: ${path}`); }, })); @@ -32,6 +35,8 @@ beforeEach(() => { vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); vi.stubGlobal("cry", undefined); setLocale("zh"); + fixture.assign.mockReset(); + fixture.openDesktop.mockReset().mockResolvedValue(undefined); fixture.status.mockReset().mockRejectedValue(new ApiError("screen_not_assigned", 409, { error: "screen_not_assigned" })); host = document.createElement("div"); document.body.append(host); @@ -116,3 +121,112 @@ it.each([ expect(host.textContent).toContain("私有桌面"); expect(fixture.status).toHaveBeenCalledTimes(1); }); + +const assign = async () => { + const button = [...host.querySelectorAll("button")].find(node => node.textContent === "现在分配屏幕"); + expect(button).toBeDefined(); + await act(async () => button!.click()); +}; +const assignedStatus = { ready: true, screen: { screen: 2, viewerTarget: "local/shared-s2" } }; +const viewer = () => vi.stubGlobal("cry", { desktopViewer: { currentState: async () => ({ open: false }), onState: () => () => {} } }); + +it("assigns an idle bot a screen and continues the existing desktop open flow", async () => { + viewer(); + await open(); + expect(host.textContent).toContain("这个智能体目前没有分配屏幕。"); + fixture.assign.mockImplementation(async () => { fixture.status.mockResolvedValue(assignedStatus); return assignedStatus; }); + await assign(); + expect(fixture.assign).toHaveBeenCalledWith(expect.objectContaining({ method: "POST", body: "{}" })); + expect(fixture.openDesktop).toHaveBeenCalledOnce(); + expect(fixture.openDesktop).toHaveBeenCalledWith(expect.objectContaining({ botId: "scout" })); + expect(host.textContent).toContain("第 2 块屏"); +}); + +it("shows the queue position, waits for readiness, and opens automatically", async () => { + viewer(); + await open(); + fixture.assign.mockResolvedValue({ screen: null, queue_position: 2 }); + fixture.status.mockResolvedValue({ screen: null, queue_position: 2 }); + await assign(); + expect(host.textContent).toContain("正在排队,第 2 位"); + expect(fixture.openDesktop).not.toHaveBeenCalled(); + fixture.status.mockResolvedValue({ ...assignedStatus, ready: false }); + await advance(2000); + expect(fixture.openDesktop).not.toHaveBeenCalled(); + fixture.status.mockResolvedValue(assignedStatus); + await advance(2000); + expect(fixture.openDesktop).toHaveBeenCalledOnce(); + await advance(8000); + expect(fixture.openDesktop).toHaveBeenCalledOnce(); +}); + +it("shows assignment failures and lets the user retry", async () => { + await open(); + fixture.assign.mockRejectedValue(new Error("Assignment unavailable")); + await assign(); + expect(host.textContent).toContain("Assignment unavailable"); + expect([...host.querySelectorAll("button")].find(node => node.textContent === "重试")?.disabled).toBe(false); +}); + +it("never opens a desktop after the panel closes during an assignment", async () => { + viewer(); + await open(); + let resolve!: (value: unknown) => void; + fixture.assign.mockImplementation(() => new Promise(done => { resolve = done; })); + await assign(); + const signal = fixture.assign.mock.calls[0][0].signal as AbortSignal; + act(() => root!.unmount()); root = null; + await act(async () => resolve(assignedStatus)); + expect(signal.aborted).toBe(true); + expect(fixture.openDesktop).not.toHaveBeenCalled(); +}); + + +it.each([false, true])("keeps polling and showing assignment while the screen starts, including failure and retry (fail=%s)", async fail => { + viewer(); + await open(); + fixture.assign.mockResolvedValue({ screen: null, queue_position: 2 }); + await assign(); + expect(host.textContent).toContain("正在排队,第 2 位"); + fixture.status.mockResolvedValue({ ...assignedStatus, ready: false, desktopReady: true }); + await advance(8000); + expect(host.querySelector('[role="status"]')?.textContent).toContain("正在打开"); + expect(fixture.status).toHaveBeenCalledTimes(7); + expect(fixture.openDesktop).not.toHaveBeenCalled(); + if (fail) { + fixture.status.mockRejectedValue(new Error("Screen startup failed")); + await advance(4000); + expect(host.querySelector('[role="alert"]')?.textContent).toBe("Screen startup failed"); + const retry = [...host.querySelectorAll("button")].find(node => node.textContent === "重试"); + expect(retry?.disabled).toBe(false); + fixture.status.mockResolvedValue(assignedStatus); + fixture.assign.mockResolvedValue(assignedStatus); + await act(async () => retry!.click()); + expect(fixture.assign).toHaveBeenCalledTimes(2); + } else { + fixture.status.mockResolvedValue(assignedStatus); + await advance(2000); + } + expect(fixture.openDesktop).toHaveBeenCalledOnce(); + expect(host.querySelector('[role="status"]')).toBeNull(); + expect(host.querySelector('[role="alert"]')).toBeNull(); +}); + +it("keeps the assignment visible until the viewer opens and offers retry when opening fails", async () => { + viewer(); + await open(); + let rejectOpen!: (cause: Error) => void; + fixture.openDesktop.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectOpen = reject; })); + fixture.status.mockResolvedValue(assignedStatus); + fixture.assign.mockResolvedValue(assignedStatus); + await assign(); + await advance(4000); + expect(host.querySelector('[role="status"]')?.textContent).toContain("正在打开"); + await act(async () => rejectOpen(new Error("Viewer failed"))); + expect(host.querySelector('[role="alert"]')?.textContent).toBe("Viewer failed"); + const retry = [...host.querySelectorAll("button")].find(node => node.textContent === "重试"); + expect(retry?.disabled).toBe(false); + await act(async () => retry!.click()); + expect(fixture.openDesktop).toHaveBeenCalledTimes(2); + expect(host.querySelector('[role="status"]')).toBeNull(); +}); diff --git a/src/components/remote-desktop-panel.tsx b/src/components/remote-desktop-panel.tsx index fc9ecd6a..d1f8b3f5 100644 --- a/src/components/remote-desktop-panel.tsx +++ b/src/components/remote-desktop-panel.tsx @@ -1,3 +1,4 @@ +import { AssignLocalVmScreen } from "./AssignLocalVmScreen"; import { boatCapableEngine } from "@/lib/remote-desktop"; import { useEffect, useRef, useState } from "react"; import { CalendarClock, CalendarDays, Globe, ImageOff, Loader2, Monitor, Plus, X } from "lucide-react"; @@ -80,18 +81,26 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) { // The host runs this bot on its Local VM: preview, control and viewer all go to that desktop, not the cloud computer. const localVm = bot.computer === "vm"; const [view, setView] = useState<"desktop" | "browser">(() => takeComputerPanelBrowserView(bot.id) ? "browser" : "desktop"); + const [screenWait, setScreenWait] = useState<{ position?: number } | null>(null); const [screenNumber, setScreenNumber] = useState(null); useEffect(() => { setScreenNumber(null); + setScreenWait(null); if (!localVm) return; const controller = new AbortController(); let timer: ReturnType | undefined; const refreshScreen = async (background = false) => { try { const status = await api(`/api/bots/${bot.id}/local-computer`, { signal: controller.signal, background }); - if (!controller.signal.aborted) setScreenNumber(status?.screen?.screen ?? null); + if (!controller.signal.aborted) { + setScreenNumber(status?.screen?.screen ?? null); + const waiting = status.screen === null || (status.screen && status.ready === false); + setScreenWait(waiting ? { position: status.queue_position } : null); + if (waiting) timer = setTimeout(() => void refreshScreen(true), 4000); + } } catch (cause) { if (!controller.signal.aborted && cause instanceof ApiError && cause.status === 409 && cause.body?.error === "screen_not_assigned") { + setScreenWait({}); timer = setTimeout(() => void refreshScreen(true), 4000); } } @@ -198,7 +207,7 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) { }; }, [bot.id, localVm, pageVisible, viewerOpen, pending, view]); - const open = async () => { + const open = async (assignment = false) => { setPending(true); setError(null); // Set when another view holds the Local VM: this attempt opened nothing, so it must not close that view's viewer. @@ -208,8 +217,11 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) { if (!window.cry?.desktopViewer) throw new Error(t("remoteDesktop.viewerUnavailable")); const status = localVm ? await api(`/api/bots/${bot.id}/local-computer`) : null; if (localVm) setScreenNumber(status?.screen?.screen ?? null); - if (status?.queue_position) throw new Error(t("vm.screens.queued", { position: status.queue_position })); - if (status?.screen === null) throw new Error(t("vm.screens.unassignedBot")); + if (status?.screen === null) { + setScreenWait({ position: status.queue_position }); + if (assignment) throw new ApiError("screen_not_assigned", 409, { error: "screen_not_assigned" }); + return; + } const target = localVm ? status?.screen?.viewerTarget : bot.cloudBackend === "vps" ? `vps/${bot.id}` : undefined; viewerRequested = true; await openProviderDesktop({ @@ -237,8 +249,9 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) { body: "{}", }).catch(() => {}); } - setError(cause instanceof ApiError && cause.status === 409 && cause.body?.error === "screen_not_assigned" - ? t("vm.screens.unassignedBot") : panelErrorText(cause instanceof Error ? cause : String(cause))); + if (assignment) throw cause; + if (cause instanceof ApiError && cause.status === 409 && cause.body?.error === "screen_not_assigned") setScreenWait({}); + else setError(panelErrorText(cause instanceof Error ? cause : String(cause))); } finally { setPending(false); } @@ -333,6 +346,8 @@ export function RemoteDesktopPanel({ bot }: { bot: Bot }) { {pending ? : } {t(pending ? "settings.updates.opening" : "computer.takeControl")} + {localVm && { await open(true); setScreenWait(null); }} />} {error && (
{error} diff --git a/src/locales/en.json b/src/locales/en.json index 319fc847..bdc891a1 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -10596,7 +10596,8 @@ "vm.screens.number": "Screen {screen}", "vm.screens.unassigned": "Waiting for a screen", "vm.screens.available": "Available", - "vm.screens.unassignedBot": "This bot has no screen assigned yet. A screen will be assigned when it runs again.", + "vm.screens.unassignedBot": "This bot has no screen assigned yet.", + "vm.screens.assignNow": "Assign a screen now", "vm.screens.controlled": "Controlled by a person", "vm.screens.idle": "Idle for {seconds}s", "vm.screens.idleMinutes": "Idle for {minutes} minutes", diff --git a/src/locales/zh.json b/src/locales/zh.json index da26808a..009d74e7 100644 --- a/src/locales/zh.json +++ b/src/locales/zh.json @@ -8399,7 +8399,8 @@ "vm.screens.number": "第 {screen} 块屏", "vm.screens.unassigned": "等待分配屏幕", "vm.screens.available": "空闲", - "vm.screens.unassignedBot": "这个智能体目前没有分配屏幕,下次运行时会自动分配。", + "vm.screens.unassignedBot": "这个智能体目前没有分配屏幕。", + "vm.screens.assignNow": "现在分配屏幕", "vm.screens.controlled": "正在由人接管", "vm.screens.idle": "已空闲 {seconds} 秒", "vm.screens.idleMinutes": "已空闲 {minutes} 分钟", From 1ab56671e5055c72ca069b9871184b6c14cf8673 Mon Sep 17 00:00:00 2001 From: tangzhixiong613-jpg <340458552+tangzhixiong613-jpg@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:43:13 +0000 Subject: [PATCH 2/2] =?UTF-8?q?T-1253=20=E5=85=B1=E7=94=A8=E7=94=B5?= =?UTF-8?q?=E8=84=91=EF=BC=9A=E6=9C=AA=E5=88=86=E9=85=8D=E5=B1=8F=E5=B9=95?= =?UTF-8?q?=E6=97=B6=E5=8F=AF=E4=B8=80=E9=94=AE=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- src/locales/source-hashes.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/locales/source-hashes.json b/src/locales/source-hashes.json index 6c706a89..4fb8c604 100644 --- a/src/locales/source-hashes.json +++ b/src/locales/source-hashes.json @@ -7926,7 +7926,7 @@ "vm.screens.count": "c2df57933feb0ebab2e8229e66e3e90dd6773fa65773e56827a366f48dcc2ceb", "vm.screens.resources": "47d4639bee0bc493095cf9af6421abb7c2a4b3d757ddfbd68e5305254d3c73f9", "vm.screens.available": "e674447337e83c1346f6122ed69f35bf5526e2a11842b2f2b788f3fb67d714ca", - "vm.screens.unassignedBot": "52c944a185eb075e083867fcd863dfaabf1fff4337d4f21540ee6965c16fee63", + "vm.screens.unassignedBot": "7e5f4cdb0c279ddaa8812a362091baaebae77b5ed52a715c222c4e65775b1c46", "vm.screens.idleMinutes": "0252d2901ebbb484d4ecd31243f52d15006f8965e6a25734d1f8cef97c082dbd", "vm.screens.waitedMinutes": "de03856548fec6e6d4bc5dde917bedb8850adb4867fa44b85cf6ae289f9ffde7", "server.computer.wakeFailed": "dd7d27bc53190f40633c91f81a0757aa73edffb0908e42b75e23d88a99cedb30",