diff --git a/docs/tasks/T-1200/T-1246-instabox-wiring/brief.md b/docs/tasks/T-1200/T-1246-instabox-wiring/brief.md new file mode 100644 index 00000000..82952c63 --- /dev/null +++ b/docs/tasks/T-1200/T-1246-instabox-wiring/brief.md @@ -0,0 +1,106 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1246 T-1239⑤ 云电脑:把核心、工具、画面、设置/快照接进服务端,端到端可用 + +## 背景 +父任务 T-1239:新增云电脑后端 `instabox`。①–④ 已分别合并进 main,但为避免互相冲突,都没有接进服务端入口,现在处于「代码在、没接线」状态: +- ① T-1242 核心:`server/instabox/` 下机器生命周期(ensureRunning/suspend/remove/exec/readFile/writeFile/vncUpstream)、回合接入、策略与删除。报告:`docs/tasks/T-1200/T-1242-instabox-core/report.md`。 +- ② T-1243 工具:`server/instabox/computer-tools.ts`(智能体操作电脑的工具、复制上限、lease)。报告:`docs/tasks/T-1200/T-1243-instabox-tools/report.md`。 +- ③ T-1244 画面:`server/instabox/vnc.ts` 远程桌面中继。报告里「供⑤接线的现有确切签名」一节:`docs/tasks/T-1200/T-1244-instabox-vnc/report.md`。 +- ④ T-1245 设置/快照:钥匙设置项、三后端选择 `CloudBackendPicker`、快照面板 `InstaboxSnapshots`、快照路由与假服务。报告里「⑤ 接线的确切签名」和「尚未独立闭环」两节:`docs/tasks/T-1200/T-1245-instabox-settings/report.md`。 + +先完整读这四份报告和下面的共同约定,再动手。 + +## 要做的事(必改) +1. 配置:`instabox.apiKey` 接进配置 schema、saveConfig 的 section 合并列表、加载、`INSTABOX_API_KEY` 环境变量覆盖、全局 configStatus 和 config 广播。`/api/config` 和广播里只给「已配置/末四位/来源」,绝不回显钥匙原文(④第 4 轮已有的约束保持)。 +2. 路由:注册④的快照路由;注册③的画面中继:浏览器 target id 沿用 `vps/`,服务端按智能体实际后端选 provider,instabox 时调 `machines.ensureRunning(botId)` + `machines.vncUpstream(id)`,服务端 key 用 `instabox//`,换机后旧连接失效。 +3. 工具:智能体后端为 instabox 时,回合里的电脑工具走②的 `computer-tools`,不再落到 Boat/VPS;box/vps 行为不变。 +4. 前端:电脑面板里④的后端选择和快照面板在接好后正常可用;没配钥匙时 instabox 选项置灰并提示去设置页填钥匙(用④已加的文案键,缺的键补进 zh/en/pt-br)。 +5. 共用电脑(team computer / 部门桌面)、本地 VM 私有桌面、多屏一律不动。 + +## 一起改(来自④第 2 轮复审的建议) +6. 快照接口同步等待每段实际约 205 秒(180 + 一次 25 秒长轮询):前端请求超时和服务端反代/路由超时按此留余量,或把剩余时间截成 `wait` 参数,不能让前端先超时而后台还在跑。 +7. 假服务快照对象改成真实 OpenAPI 形状:status 为 `pending|ready|failed`,带 `sourceMachineId`;操作单判别只认 `type/phase`,不认 `machineId`。 +8. 快照 e2e 在 restore 前断言创建时已调过 operations 和 `GET /v1/snapshots`。 +9. pt-br 里「智能体」译法统一,照文件里已有的多数译法。 + +## 端到端测试(必须新增,进仓库) +新增 `server/instabox/wiring.e2e.test.ts`(名字可调整),用真实服务端入口 + 本地假 (不连真实服务)走完一条链: +设置钥匙(含环境变量覆盖)→ `/api/config` 不含钥匙原文 → 新建智能体选 instabox → 回合里调一次电脑工具(exec + 截图/读文件)落到假 → 画面中继以 `vps/` 连上并收到 RFB 握手 → 建快照、列快照、restore → 删智能体时机器被删除。 +另加一条:box/vps 智能体的同一路径不碰假 。 +e2e 连跑 3 次都要过。 + +## 约束 +- 行数门禁(`scripts/testing/file-size-ratchet.test.ts`):`server/index.ts`、`ComputerPanel.tsx`、`config.ts`、`store.ts` 余量很少,先查余量;接线逻辑放 `server/instabox/wiring.ts` 等新文件,大文件里只加挂载调用。 +- 公开仓库:代码、测试、文档、verification.txt 不得出现内部域名、IP、邮箱、人名、真实钥匙、uuid;假钥匙用 `instabox-e2e-fixture` 这类明显的假值。 +- 钥匙只在服务端,不进机器、前端、日志、智能体上下文。 + +## 验收 +改动相关测试逐文件跑过(含①–④已有的 instabox 测试、`server/index.test.ts`、`server/bot-box/env.test.ts`、file-size-ratchet);`pnpm exec tsc -p tsconfig.server.json`、前端 tsc;改动文件 `oxlint --deny-warnings`。report.md 写短:改了哪些文件、怎么接的、测试结果;列出还需要协调者用真实钥匙核对的点(例如真实快照耗时、真实画面)。 + +## 顺手修(main 上已有) +10. `server/routes/config-instabox.test.ts` 第 20 行 `configForAccess: (status, auth) =>` 两个参数隐式 any,`tsc -p tsconfig.server.json` 报 TS7006;补上类型,让服务端 tsc 全绿。 +# T-1239 云电脑后端:共同约定(4 个并行子任务都必须遵守) + +## 目标与范围(用户已定) +- 新增云电脑后端 `instabox`,与 Boat(box)、VPS(vps)并列,供单个智能体使用自己的云电脑。范围:Linux 桌面 + Android 模板,不含苹果。 +- 共用电脑(team computer / 部门桌面)、本地 VM 私有桌面、多屏都**不改**,仍走原来的路。 +- 所有机器用同一把 钥匙:配置 `instabox.apiKey`,环境变量 `INSTABOX_API_KEY` 覆盖;钥匙只在服务端,绝不进入机器、前端、日志或智能体上下文。 +- 本仓库是**公开仓库**:代码、测试、文档里不得出现内部域名、IP、邮箱、人名、真实钥匙。公共接口域名 `api.instabox.sh`、`connect.instabox.sh` 可以出现。 + +## 接口要点(实测) +- 基址 `https://api.instabox.sh`,请求头 `X-API-Key: `。 +- 生命周期调用(POST /v1/machines、PATCH、start/stop/suspend/resume/restore、POST snapshots、DELETE)**必须**带 `Idempotency-Key`;删机器另带 `X-Confirm-Delete: `。都返回 202 + 操作单 `{id,machineId,type,state,phase,error}`,用 `GET /v1/operations/{id}?wait=25s` 长轮询到 succeeded/failed。 +- 机器 state:creating、starting、running、stopping、stopped、suspending、suspended、resuming、deleting、deleted、error。 +- 创建:`{"name","templateId":"agent-browser"|"android","spec":{"vcpus":4,"memoryGiB":8},"autoSuspendAfterSeconds":600,"metadata":{...}}`。按名字找回机器:`GET /v1/machines`(按 name 过滤)。 +- 新建、冷启动、restore、从快照新建之后桌面不运行:必须 `POST /v1/machines/{id}/desktop {"action":"start","width":1280,"height":800}`。 +- exec:`POST /v1/machines/{id}/exec {"cmd"|"argv","envs","cwd","stdin","timeoutSeconds":1..600}` → `{stdout,stderr,exitCode,timedOut,truncated,...}`;非流式输出各 1 MiB 截断。默认没有 DISPLAY,桌面在 `:1`(Boat 是 :0)。后台任务用 `nohup setsid ... >log 2>&1 `,metadata 带 `{"app":"crybot","botId":...}`。 + +## 共享文件(各任务需要时原样创建,内容必须一字不差;已存在就不要改) +`server/instabox/machine.ts`: +```ts +/** cloud computer: the contract the four backend pieces share. */ +export type InstaboxTemplate = "linux" | "android"; +export type InstaboxMachineState = "creating" | "starting" | "running" | "stopping" | "stopped" | "suspending" | "suspended" | "resuming" | "deleting" | "deleted" | "error"; + +export interface InstaboxMachine { id: string; name: string; state: InstaboxMachineState; template: InstaboxTemplate } + +export interface InstaboxExecRequest { cmd?: string; argv?: string[]; envs?: Record; cwd?: string; stdin?: string; timeoutSeconds?: number } +export interface InstaboxExecResult { stdout: string; stderr: string; exitCode: number | null; timedOut: boolean; truncated: boolean } + +/** One authenticated API call. `idempotent: true` adds an Idempotency-Key; lifecycle calls need it. */ +export type InstaboxRequest = (method: string, path: string, opts?: { body?: unknown; raw?: Uint8Array; idempotent?: boolean; headers?: Record; query?: Record }) => Promise; + +export interface InstaboxMachines { + find(botId: string): Promise; + /** Create, resume or start the bot's machine as needed, then make sure its desktop runs. */ + ensureRunning(botId: string, opts?: { template?: InstaboxTemplate }): Promise; + suspend(botId: string): Promise; + remove(botId: string): Promise; + exec(machineId: string, req: InstaboxExecRequest): Promise; + readFile(machineId: string, path: string): Promise; + writeFile(machineId: string, path: string, data: Uint8Array, opts?: { mode?: string }): Promise; + /** Where the server connects for this machine's VNC (RFB, no password); headers carry the key and never leave the server. */ + vncUpstream(machineId: string): Promise<{ url: string; headers: Record }>; +} +``` +`shared/wire.ts:62` 改成(原行上改,不增行;文件正好 1000 行,多一行会挂行数门禁): +`export type CloudBackend = "box" | "vps" | "instabox";` + +## 行数门禁 +`scripts/testing/file-size-ratchet.test.ts`:登记文件不许变长,未登记文件不许超过 1000 行。`server/index.ts` 四个任务合计只剩约 110 行余量(由任务①统一占用),`ComputerPanel.tsx` 余量 25、`config.ts` 10、`store.ts` 17、`boat.ts` 1。新逻辑一律放 `server/instabox/` 下的新文件。 + +## 测试 +- 不连真实 。写 `server/testing/fake-instabox.ts` 风格的假服务(`createServer` 起本地 HTTP/WS,`vi.stubEnv` 或依赖注入指过去);各任务若都需要假服务,各自放在自己命名的文件里(如 `fake-instabox-power.ts`),避免同名冲突。 +- 本轮沙箱已放开,可监听端口:改动相关的测试全部跑过,涉及的 e2e 连跑 3 次。另跑 `pnpm exec tsc -p tsconfig.server.json`、前端有改动时跑前端 tsc,对改动文件跑 `oxlint --deny-warnings`。 +- 文案只加 `src/locales/zh.json` 和 `en.json`(且只有任务④加;其他任务需要文案时在报告里列出键名和中英文,由④统一加,代码里先用这些键)。 diff --git a/docs/tasks/T-1200/T-1246-instabox-wiring/r2.md b/docs/tasks/T-1200/T-1246-instabox-wiring/r2.md new file mode 100644 index 00000000..32acbb0c --- /dev/null +++ b/docs/tasks/T-1200/T-1246-instabox-wiring/r2.md @@ -0,0 +1,30 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1246 第 2 轮:审核 3 条必改(生命周期互斥、快照总截止时间)+ 1 条建议 + +原任务见本文件夹 brief.md,工作区里已有改动保留,在此基础上改。下面是第 1 轮 Codex 只读审核的原文意见,必改 3 条全部修,建议 1 条一起做。 + +必改 + +1. [server/instabox/wiring.ts:56](/root/workspace/crybot/.worktrees/T-1246-instabox-wiring/server/instabox/wiring.ts:56) — 快照路由没有接入电脑生命周期锁,也不检查活跃回合。路由内部的 `busy` 只能排斥其他快照操作;智能体执行命令期间仍可恢复磁盘,恢复期间也能开始新回合或删除智能体。可能中断工作、覆盖正在写入的数据。应接入统一生命周期互斥,恢复前检查普通回合、群聊和控制占用,并补双向并发测试。 + +2. [server/instabox/wiring.ts:50](/root/workspace/crybot/.worktrees/T-1246-instabox-wiring/server/instabox/wiring.ts:50) — VNC 解析直接调用 `ensureRunning`,绕过删机流程持有的生命周期锁。删除机器完成、智能体记录尚未删除时,打开画面会重新创建机器;随后智能体删除成功,留下无人管理的机器。应让 VNC 启动与删除互斥,并在取得锁后重新检查智能体是否有效。补充删除过程中打开画面的回归测试。 + +3. [server/instabox/wiring.ts:28](/root/workspace/crybot/.worktrees/T-1246-instabox-wiring/server/instabox/wiring.ts:28) — 快照的 30 秒请求限制没有覆盖 `machineIdFor → machines.find` 和恢复后的 `machines.ensureRunning`,两者仍使用单请求 650 秒的客户端。例如查机耗时 600 秒、操作轮询再耗时 180 秒,已超过服务端 700 秒和前端 720 秒预算;连接结束也不会取消后台流程。应给完整快照流程统一截止时间,并约束查机、分页、轮询和开桌面的剩余预算,补充慢响应测试。 + +建议 + +1. [server/instabox/wiring.e2e.test.ts:96](/root/workspace/crybot/.worktrees/T-1246-instabox-wiring/server/instabox/wiring.e2e.test.ts:96) — box/vps 用例没有写入电脑工具执行计划,也没有断言消息请求成功;目前只能证明这段流程未访问假 。建议让两个后端实际执行电脑工具,并断言请求落到各自假后端。 + + +## 补充要求 +- 生命周期互斥复用服务端现有 Boat/VPS 用的那把锁和活跃回合检查,不另造一套;新逻辑仍放 `server/instabox/` 下,大文件只加挂载调用,注意行数门禁。 +- 每条必改都要有对应回归测试(快照与回合双向并发、删除过程中打开画面、慢响应超过总截止时间),e2e 连跑 3 次。 + +## 验收 +改动相关测试逐文件跑过(含 `server/instabox/` 全部、`server/index.test.ts`、file-size-ratchet);`pnpm exec tsc -p tsconfig.server.json`、前端 tsc;改动文件 `oxlint --deny-warnings`。report.md 追加第 2 轮一节(短);verification.txt 不写 IP、地址、域名、uuid、Object 相等判定写法。 diff --git a/docs/tasks/T-1200/T-1246-instabox-wiring/r3.md b/docs/tasks/T-1200/T-1246-instabox-wiring/r3.md new file mode 100644 index 00000000..4e082177 --- /dev/null +++ b/docs/tasks/T-1200/T-1246-instabox-wiring/r3.md @@ -0,0 +1,20 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。 +- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。 +- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。 +- 不提交 git。 + +# T-1246 第 3 轮:CI 一条分支独有失败 + 一条安全审查低级项 + +原任务见本文件夹 brief.md、r2.md,工作区已提交的改动保留。第 2 轮审核已通过,只改下面两条。 + +## 必改 +1. GitHub Actions 分支独有失败 `src/locales/vocabulary.test.ts`「hard-coded strings in src, server, electron and shared use the same words」: + `server/instabox/wiring-backends-fixture.ts:25` 的字符串 `"Boat fixture completed"` 违反用词约定(Boat 只用来称呼个人自己的钥匙,云桌面要说 cloud desktop)。改成不含 Boat 的说法(如 `"box fixture completed"`),同步改引用它的断言;不要往 ALLOWED 白名单里加条目。 + +## 一起改(安全审查低级) +2. `server/instabox/wiring.ts` 读取 `CRYBOT_TEST_INSTABOX_API`(把 基址改到本机并允许不安全的本机画面上游)在生产进程里也生效。改为只在测试环境生效(沿用仓库已有的测试环境判定方式,如 `NODE_ENV === "test"` 或现有的测试开关),生产进程忽略该变量;补一条单测。 + +## 验收 +`src/locales/vocabulary.test.ts`、`server/instabox/` 下全部测试逐文件跑过,wiring e2e 连跑 3 次;`pnpm exec tsc -p tsconfig.server.json`;改动文件 `oxlint --deny-warnings`。report.md 追加第 3 轮一节(短);verification 不写 IP、地址、域名、uuid、Object 相等判定写法。 diff --git a/docs/tasks/T-1200/T-1246-instabox-wiring/report.md b/docs/tasks/T-1200/T-1246-instabox-wiring/report.md new file mode 100644 index 00000000..7b34b556 --- /dev/null +++ b/docs/tasks/T-1200/T-1246-instabox-wiring/report.md @@ -0,0 +1,96 @@ +改了什么 + +接通服务端回合工具、VNC、快照路由与电脑面板;补齐缺钥匙禁选、设置保存后刷新、截图预览、快照等待预算、真实快照对象形状和 pt-br 的 bot 译法;修复配置测试隐式 any。文件清单与 `git status --short --untracked-files=all` 一致,未提交 git: + +```text + M server/index.ts + M server/instabox/client.ts + M server/instabox/power.test.ts + M server/instabox/power.ts + M server/routes/config-instabox.test.ts + M server/routes/instabox-snapshots.e2e.test.ts + M server/routes/instabox-snapshots.ts + M server/testing/fake-instabox-core.ts + M server/testing/fake-instabox-snapshots.ts + M shared/place-view.ts + M src/components/CloudBackendPicker.test.ts + M src/components/CloudBackendPicker.tsx + M src/components/ComputerPanel.tsx + M src/components/InstaboxSnapshots.test.tsx + M src/components/InstaboxSnapshots.tsx + M src/lib/place-view.ts + M src/locales/pt-br.json +?? docs/tasks/T-1200/T-1246-instabox-wiring/report.md +?? docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt +?? server/instabox/wiring.e2e.test.ts +?? server/instabox/wiring.ts +?? src/components/ComputerPanel.instabox.test.ts +?? src/components/instabox-panel.test.ts +?? src/components/instabox-panel.ts +``` + +为什么 + +已有配置 schema、section 合并、加载及环境覆盖继续复用,并由真实入口验证保存、读回、广播和末四位脱敏。新增 `wiring.ts` 统一机器缓存与凭据;单个智能体的普通/群聊回合绑定 工具,画面沿用 `vps/`,服务端按机器编号区分连接,换机后旧连接失效。共用电脑、本地 VM、多屏保持原路径。 + +快照 API 单次请求上限 30 秒,轮询按剩余时间缩短 wait;服务端路由留 700 秒、前端留 720 秒。创建快照的 operations/list 断言移至 restore 之前。入口为 15463/15515 行,电脑面板为 1774/1782 行。 + +测试命令与结果摘要 + +- 开工运行 依赖准备脚本。验证 README 缺失;服务验证均使用临时数据目录、隔离入口和本地假服务。 +- `pnpm exec vitest run <文件> --maxWorkers=1`:48 个相关文件最终全部通过,包含①–④全部 测试、`server/index.test.ts`(338)、`server/config.test.ts`(257)、`server/bot-box/env.test.ts`(11)、行数门禁及前端回归。完整逐文件命令见日志。 +- 8 个 e2e 文件均连续 3 次通过:core(15)、computer-tools(29)、vnc(32)、wiring(3)、snapshots(12)、request-auth(2)、key-readback(2)、place-view(7)。VNC 设置 `CRYBOT_CDP_CHROME=/usr/bin/chromium`,无浏览器用例跳过;桌面代理 HTTP 测试另连续 3 次通过(38)。 +- `pnpm exec tsc -p tsconfig.server.json`、`pnpm exec tsc -p tsconfig.json`:最终均通过。 +- 改动 TS/TSX/JSON 的 `pnpm exec oxlint --deny-warnings`、`node scripts/generate-locale.mjs --check`、`git diff --check`:通过。 +- 协调者仍需用真实钥匙核对真实快照耗时和部署反代的等待预算、真实 VNC 画面/输入、Android 冷启动,以及后台 activity lease 字段与续租行为。 + +完整输出(含修正前的 fixture/type-check 失败)见 [verification.txt](verification.txt)。 + + +## 第 2 轮 + +改了什么(本轮文件清单,与 `git status --short --untracked-files=all` 一致;未提交): + +```text + M docs/tasks/T-1200/T-1246-instabox-wiring/report.md + M docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt + M server/index.ts + M server/instabox/client.ts + M server/instabox/wiring.e2e.test.ts + M server/instabox/wiring.ts + M server/routes/instabox-snapshots.e2e.test.ts + M server/routes/instabox-snapshots.ts + M server/testing/fake-instabox-core.ts +?? server/instabox/snapshot-wiring.e2e.test.ts +?? server/instabox/snapshot-wiring.ts +?? server/instabox/wiring-backends-fixture.ts +?? server/instabox/wiring.test.ts +``` + +为什么:快照变更与 VNC 启动复用 Boat/VPS 生命周期锁;快照复用普通回合、群聊、例行任务和控制占用检查,VNC 取锁后复查智能体及凭据。快照全流程共用 600 秒截止时间,单请求仍限 30 秒,覆盖查机、分页、轮询、正文读取与恢复后的桌面启动,并在断连时取消。回归覆盖双向并发、删机窗口及慢响应;box/vps 用例执行电脑工具并断言各自假后端收到命令。入口仍为 15463 行。 + +测试命令与结果摘要: + +- `pnpm exec vitest run <文件> --maxWorkers=1`:22 个文件逐个通过,含 `server/instabox/` 全部、`server/index.test.ts`、快照路由、Boat/env 与行数门禁;7 个 e2e 文件及桌面代理 HTTP 测试均连续 3 次通过,VNC 设置 `CRYBOT_CDP_CHROME=/usr/bin/chromium`,浏览器用例实际执行。 +- `pnpm exec tsc -p tsconfig.server.json`、`pnpm exec tsc -p tsconfig.json`、改动文件 `pnpm exec oxlint --deny-warnings`、`git diff --check`:全部通过。 +- 验证 README 缺失;全部服务验证使用隔离 fixture。完整脱敏输出见 [verification.txt](verification.txt)。 + +## 第 3 轮 + +改了什么(未提交): + +```text + M docs/tasks/T-1200/T-1246-instabox-wiring/report.md + M docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt + M server/instabox/wiring-backends-fixture.ts + M server/instabox/wiring.test.ts + M server/instabox/wiring.ts +``` + +为什么:假后端输出改用 cloud desktop 的统一词汇;`CRYBOT_TEST_INSTABOX_API` 现在仅在测试环境开关启用时生效,生产进程忽略它,并新增了回归单测。 + +测试命令与结果摘要: + +- `src/locales/vocabulary.test.ts` 通过 6 项;`server/instabox/` 下全部测试逐文件执行,147 项通过、1 项既有跳过;wiring e2e 连续 3 次通过(每次 6 项)。 +- `pnpm exec tsc -p tsconfig.server.json`、改动文件 `pnpm exec oxlint --deny-warnings`、`git diff --check`:通过。 +- 验证使用隔离 fixture;仓库未提供 `docs/verification/README.md`。完整输出见 [verification.txt](verification.txt)。 diff --git a/docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt b/docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt new file mode 100644 index 00000000..63800139 --- /dev/null +++ b/docs/tasks/T-1200/T-1246-instabox-wiring/verification.txt @@ -0,0 +1,268 @@ +T-1246 第 3 轮验证记录 + +环境准备 +COMMAND: crybot-link-deps +RESULT: linked 73 entries + +词汇回归 +COMMAND: pnpm exec vitest run src/locales/vocabulary.test.ts --maxWorkers=1 +RESULT: 1 file passed, 6 tests passed + +server/instabox 逐文件回归 +COMMAND: pnpm exec vitest run server/instabox/client.test.ts --maxWorkers=1 +RESULT: 1 file passed, 6 tests passed +COMMAND: pnpm exec vitest run server/instabox/computer-tools.e2e.test.ts --maxWorkers=1 +RESULT: 1 file passed, 29 tests passed +COMMAND: pnpm exec vitest run server/instabox/core.e2e.test.ts --maxWorkers=1 +RESULT: 1 file passed, 15 tests passed +COMMAND: pnpm exec vitest run server/instabox/key.test.ts --maxWorkers=1 +RESULT: 1 file passed, 4 tests passed +COMMAND: pnpm exec vitest run server/instabox/machines.test.ts --maxWorkers=1 +RESULT: 1 file passed, 24 tests passed +COMMAND: pnpm exec vitest run server/instabox/persistence.test.ts --maxWorkers=1 +RESULT: 1 file passed, 1 test passed +COMMAND: pnpm exec vitest run server/instabox/power.test.ts --maxWorkers=1 +RESULT: 1 file passed, 7 tests passed +COMMAND: pnpm exec vitest run server/instabox/snapshot-wiring.e2e.test.ts --maxWorkers=1 +RESULT: 1 file passed, 6 tests passed +COMMAND: pnpm exec vitest run server/instabox/vnc.e2e.test.ts --maxWorkers=1 +RESULT: 1 file passed, 31 tests passed, 1 existing test skipped +COMMAND: pnpm exec vitest run server/instabox/vnc.test.ts --maxWorkers=1 +RESULT: 1 file passed, 2 tests passed +COMMAND: pnpm exec vitest run server/instabox/wiring.e2e.test.ts --maxWorkers=1 (run 1/3) +RESULT: 1 file passed, 6 tests passed +COMMAND: pnpm exec vitest run server/instabox/wiring.e2e.test.ts --maxWorkers=1 (run 2/3) +RESULT: 1 file passed, 6 tests passed +COMMAND: pnpm exec vitest run server/instabox/wiring.e2e.test.ts --maxWorkers=1 (run 3/3) +RESULT: 1 file passed, 6 tests passed +COMMAND: pnpm exec vitest run server/instabox/wiring.test.ts --maxWorkers=1 +RESULT: 1 file passed, 4 tests passed + +类型与 lint +COMMAND: pnpm exec tsc -p tsconfig.server.json +RESULT: exit 0 +COMMAND: pnpm exec oxlint --deny-warnings server/instabox/wiring-backends-fixture.ts server/instabox/wiring.test.ts server/instabox/wiring.ts +RESULT: exit 0 +COMMAND: git diff --check +RESULT: exit 0 + +验证均使用隔离 fixture;未连接用户应用或用户数据。仓库未提供 docs/verification/README.md。 + +逐文件运行输出(原始摘要) + +>>> server/instabox/client.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +······ + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 10:07:41 + Duration 1.09s (transform 671ms, setup 855ms, import 34ms, tests 73ms, environment 0ms) + + +>>> server/instabox/computer-tools.e2e.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +····························· + + Test Files 1 passed (1) + Tests 29 passed (29) + Start at 10:07:42 + Duration 10.73s (transform 892ms, setup 842ms, import 310ms, tests 9.44s, environment 0ms) + + +>>> server/instabox/core.e2e.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +··············· + + Test Files 1 passed (1) + Tests 15 passed (15) + Start at 10:07:53 + Duration 59.48s (transform 1.61s, setup 947ms, import 1.23s, tests 57.17s, environment 0ms) + + +>>> server/instabox/key.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +···· + + Test Files 1 passed (1) + Tests 4 passed (4) + Start at 10:08:54 + Duration 1.03s (transform 666ms, setup 862ms, import 8ms, tests 20ms, environment 0ms) + + +>>> server/instabox/machines.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +························ + + Test Files 1 passed (1) + Tests 24 passed (24) + Start at 10:08:55 + Duration 1.77s (transform 689ms, setup 851ms, import 48ms, tests 733ms, environment 0ms) + + +>>> server/instabox/persistence.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +· + + Test Files 1 passed (1) + Tests 1 passed (1) + Start at 10:08:57 + Duration 1.49s (transform 1.04s, setup 880ms, import 459ms, tests 25ms, environment 0ms) + + +>>> server/instabox/power.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +stderr | server/instabox/power.test.ts > runs failed suspension in the background without misreporting a saved backend switch as a failed patch +InstaBox machine could not be suspended after a backend change Add an InstaBox API key to use this computer. + +······· + + Test Files 1 passed (1) + Tests 7 passed (7) + Start at 10:08:59 + Duration 2.46s (transform 1.80s, setup 1.06s, import 1.26s, tests 12ms, environment 0ms) + + +>>> server/instabox/snapshot-wiring.e2e.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +······ + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 10:09:02 + Duration 7.45s (transform 1.71s, setup 1.03s, import 1.16s, tests 5.12s, environment 0ms) + + +>>> server/instabox/vnc.e2e.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +·······························- + + Test Files 1 passed (1) + Tests 31 passed | 1 skipped (32) + Start at 10:09:10 + Duration 3.26s (transform 1.37s, setup 961ms, import 789ms, tests 1.37s, environment 0ms) + + +>>> server/instabox/vnc.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +·· + + Test Files 1 passed (1) + Tests 2 passed (2) + Start at 10:09:14 + Duration 1.22s (transform 824ms, setup 1.04s, import 17ms, tests 5ms, environment 0ms) + + +>>> server/instabox/wiring.e2e.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +······ + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 10:09:16 + Duration 30.67s (transform 1.57s, setup 988ms, import 1.10s, tests 28.42s, environment 0ms) + + +>>> server/instabox/wiring.test.ts + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +···· + + Test Files 1 passed (1) + Tests 4 passed (4) + Start at 10:09:47 + Duration 1.81s (transform 1.30s, setup 858ms, import 812ms, tests 7ms, environment 0ms) + + +All instabox files passed + +>>> wiring e2e run 2 + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +······ + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 10:09:56 + Duration 28.96s (transform 1.31s, setup 950ms, import 805ms, tests 27.06s, environment 0ms) + + +>>> wiring e2e run 3 + + RUN v4.1.11 /data/home/codex-t-1246-instabox-wiring-b90ca37edf34 + +stderr | unknown test +Importing from "vitest/runners" is deprecated since Vitest 4.1. Please use "vitest" instead. + +······ + + Test Files 1 passed (1) + Tests 6 passed (6) + Start at 10:10:25 + Duration 28.59s (transform 1.43s, setup 1.04s, import 866ms, tests 26.55s, environment 0ms) + + +Wiring e2e passed three total runs diff --git a/server/index.ts b/server/index.ts index 54f3a7e2..d1988280 100644 --- a/server/index.ts +++ b/server/index.ts @@ -132,10 +132,10 @@ import { boatCreateRecoverySnapshot } from "./boat-create-idempotency.ts"; import { boatDeletionSnapshot } from "./boat-delete-journal.ts"; import { liveDecisionRefusal } from "../shared/live-approval.ts"; import type { PersonWaitKind } from "../shared/person-wait.ts"; import { cloudKindOf, cloudPolicyKindOf, vpsLeftBehindNotice } from "./cloud-backend.ts"; -import { createInstaboxRequest } from "./instabox/client.ts"; +import { createInstaboxWiring } from "./instabox/wiring.ts"; import { instaboxConfigured } from "./instabox/key.ts"; import type { InstaboxMachines } from "./instabox/machine.ts"; -import { createInstaboxMachineCache, instaboxComputerAction, instaboxFailureResponse, instaboxStatus, requireInstabox, withInstaboxBackendPower } from "./instabox/power.ts"; +import { instaboxComputerAction, instaboxFailureResponse, instaboxStatus, requireInstabox, withInstaboxBackendPower } from "./instabox/power.ts"; import * as composio from "./composio.ts"; import { appSandboxRule, connectorCallFromFrame, connectorRefusalText, connectorScopeRefusalText, connectorUnrecognizedText, evaluateConnectorTools } from "./connector-verdict.ts"; import { signInCardText } from "./connector-card-text.ts"; @@ -4621,11 +4621,14 @@ function audienceChanged(): void { // Before any revocation callback: Cloud boot can revoke sessions before routes are registered. const upgrades = createUpgrades(); const phoneScreen = createPhoneScreenRoutes({ upgrades, hold: phoneHold, enabled: usbPhoneTakeoverOn, live: (auth) => auth.kind === "loopback" || sessions.isLive(auth.session.id), dataDir: DATA_DIR }); +const instaboxWiring = createInstaboxWiring({ cfg: () => cfg, claim: claimBotComputerLifecycle, inUse: id => managedBoatOwners().some(owner => owner.botId === id && owner.inUse), eligible: id => { const bot = store.bot(id); return !!bot && !inheritedTeamComputer(bot) && cloudKindOf(bot) === "instabox"; } }); const desktopViewer = createDesktopViewer({ + allowInsecureLoopbackUpstream: instaboxWiring.allowInsecureLoopbackUpstream, upgrades, target: (id) => { if (id.startsWith("vps/")) { const botId = id.slice(4); + const instabox = instaboxWiring.target(botId); if (instabox) return instabox; if (store.bot(botId)?.cloudBackend !== "vps") return; return { key: id, resolve: async () => { const connection = vps.vpsDesktopConnection(botId); @@ -5627,9 +5630,7 @@ const computerPreviews = createComputerPreviews(); const vpsJoinRequests = new Map>(); const orphanBoatLifecycleBusyIds = new Set(); const boatInventoryRequestsBusyIds = new Set(); -const cachedInstaboxMachines = createInstaboxMachineCache(key => createInstaboxRequest(key, - /^http:\/\/127\.0\.0\.1:[1-9]\d{0,4}$/.test(process.env.CRYBOT_TEST_INSTABOX_API ?? "") ? process.env.CRYBOT_TEST_INSTABOX_API : undefined)); -function instaboxMachinesFor(cfg: AppConfig): InstaboxMachines | null { return cachedInstaboxMachines(cfg); } +function instaboxMachinesFor(_cfg: AppConfig): InstaboxMachines | null { return instaboxWiring.machines(); } type RemoteComputerProvider = "box" | "vps"; const computerProviderConfigTransitions = new Set(); const localVmIdleMs = () => localVmIdleTimeoutMinutes(cfg) * 60_000; @@ -8210,7 +8211,9 @@ async function beginTurn( computerKind = "box"; } if (!teamComputer && cloudBackend === "instabox" && wants === "cloud") { - await requireInstabox(instaboxMachinesFor(cfg)).ensureRunning(bot.id); + await bindTurnComputer(resourceOwner, `computer:box-bot:${bot.id}`, true); + const machine = await requireInstabox(instaboxMachinesFor(cfg)).ensureRunning(bot.id); + integrations.localComputer = cloudComputerTools(bot.id, resourceOwner, `instabox/${machine.id}`); computerKind = "box"; } if (!teamComputer && cloudBackend === "box" && boat.boatOffered(cfg) && wants === "cloud") { integrations.localComputer = mountBotBoat(bot, resourceOwner, { @@ -10071,7 +10074,9 @@ async function runClaimedGroupMemberTurn( } if (!roomTeamComputer && roomPlan.computer === "cloud") { if (turnProvider(readyBot) === "instabox") { - await requireInstabox(instaboxMachinesFor(cfg)).ensureRunning(readyBot.id); + await bindTurnComputer(resourceOwner, `computer:box-bot:${readyBot.id}`, true); + const machine = await requireInstabox(instaboxMachinesFor(cfg)).ensureRunning(readyBot.id); + integrations.localComputer = cloudComputerTools(readyBot.id, resourceOwner, `instabox/${machine.id}`); roomComputerKind = "box"; } else if (turnProvider(readyBot) === "vps") { vpsThreadStarted(readyBot.id, threadId); roomVpsBotId = readyBot.id; @@ -12451,7 +12456,7 @@ ROUTES.push(createStatusRoutes({ browserEngineInstallError: { set: (error) => { browserEngineInstallError = error; } }, })); -ROUTES.push(desktopViewer.route, phoneScreen.route); +ROUTES.push(instaboxWiring.routes, desktopViewer.route, phoneScreen.route); ROUTES.push(createBrowserViewerRoutes({ upgrades, viewers: browserLive, dataDir: DATA_DIR, person: (auth) => (auth.kind === "session" ? actorKey(auth) : "local-owner"), audience: (auth, threadId) => teamHost.viewerBrowserAudience({ threadId, sees: (id) => visibleTo(viewerFor(auth)).thread(id), person: auth.kind === "session" ? actorKey(auth) : null }), @@ -13183,7 +13188,7 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => { const abort = new AbortController(); res.once("close", () => { if (!res.writableEnded) abort.abort(); }); return json(res, 200, { result: await cloudComputerRpc(body, { - cfg, signal: abort.signal, folder: () => messageFileRootsForThread(internalCapability.botId, internalCapability.threadId)[0], + cfg, instabox: instaboxWiring.tools(internalCapability.boxId), signal: abort.signal, folder: () => messageFileRootsForThread(internalCapability.botId, internalCapability.threadId)[0], boxId: () => internalCapability.boxId ?? claimedBoatId(internalCapability), gate: () => computerCallGate(internalCapability, abort.signal), assertActive: requireActiveInternalCapability, recovery: boatRecovery, diff --git a/server/instabox/client.ts b/server/instabox/client.ts index 2108ecdd..671b09ab 100644 --- a/server/instabox/client.ts +++ b/server/instabox/client.ts @@ -19,10 +19,11 @@ function errorCode(value: unknown, apiKey?: string): string { ? value : "request_failed"; } -export function createInstaboxRequest(apiKey: string, baseUrl = "https://api.instabox.sh"): InstaboxRequest { +export function createInstaboxRequest(apiKey: string, baseUrl = "https://api.instabox.sh", requestTimeoutMs = 650_000, signal?: AbortSignal): InstaboxRequest { if (!apiKey || /[^\x21-\x7e]/.test(apiKey)) throw new Error("Invalid InstaBox API key"); const origin = new URL(baseUrl); const request: InstaboxRequest = async (method, path, opts = {}) => { + signal?.throwIfAborted(); const url = new URL(path, origin); if (url.origin !== origin.origin || url.username || url.password) throw new Error("Invalid InstaBox API path"); for (const [name, value] of Object.entries(opts.query ?? {})) url.searchParams.set(name, value); @@ -39,7 +40,7 @@ export function createInstaboxRequest(apiKey: string, baseUrl = "https://api.ins } let response: Response; try { - response = await fetch(url, { method: method.toUpperCase(), headers, ...(body === undefined ? {} : { body }), redirect: "error", signal: AbortSignal.timeout(650_000) }); + response = await fetch(url, { method: method.toUpperCase(), headers, ...(body === undefined ? {} : { body }), redirect: "error", signal: signal ? AbortSignal.any([signal, AbortSignal.timeout(requestTimeoutMs)]) : AbortSignal.timeout(requestTimeoutMs) }); } catch { throw new InstaboxError(0, "network_error"); } diff --git a/server/instabox/power.test.ts b/server/instabox/power.test.ts index 3052b559..2ba491a2 100644 --- a/server/instabox/power.test.ts +++ b/server/instabox/power.test.ts @@ -61,7 +61,7 @@ it("sends translation keys for user errors while preserving provider failures", } const service = {} as InstaboxMachines; await expect(instaboxComputerAction(service, "bot", "provision", { template: "apple" })).rejects.toMatchObject({ status: 400, message: "Choose a Linux or Android computer.", errorKey: "serverError.instabox.template" }); - await expect(instaboxComputerAction(service, "bot", "join", {})).rejects.toMatchObject({ status: 501, message: "This InstaBox computer is unavailable.", errorKey: "serverError.instabox.unavailable" }); + await expect(instaboxComputerAction(service, "bot", "unsupported", {})).rejects.toMatchObject({ status: 501, message: "This InstaBox computer is unavailable.", errorKey: "serverError.instabox.unavailable" }); const providerFailure = new Error("provider failed"); expect(() => instaboxFailureResponse(providerFailure)).toThrow(providerFailure); }); @@ -69,7 +69,7 @@ it("sends translation keys for user errors while preserving provider failures", it("leaves the template unspecified when provisioning an existing machine", async () => { const ensureRunning = vi.fn().mockResolvedValue({ id: "machine", template: "android", state: "running" }); await expect(instaboxComputerAction({ ensureRunning } as unknown as InstaboxMachines, "bot", "provision", {})).resolves.toEqual({ - machine: { id: "machine", template: "android", state: "running" }, + ready: true, machine: { id: "machine", template: "android", state: "running" }, }); expect(ensureRunning).toHaveBeenCalledWith("bot", undefined); }); diff --git a/server/instabox/power.ts b/server/instabox/power.ts index 38f48a8a..567a28bc 100644 --- a/server/instabox/power.ts +++ b/server/instabox/power.ts @@ -1,3 +1,5 @@ +import { runInstaboxComputerTool } from "./computer-tools.ts"; +import { desktopViewerUrl } from "../routes/desktop-viewer.ts"; import type { AppConfig } from "../config.ts"; import type { RouteHandler } from "../routes/table.ts"; import type { BotRecord } from "../store.ts"; @@ -58,8 +60,17 @@ export async function instaboxComputerAction(machines: InstaboxMachines | null, if (template !== undefined && template !== "linux" && template !== "android") { throw new InstaboxUserError(400, "serverError.instabox.template"); } - return { machine: await service.ensureRunning(botId, template === undefined ? undefined : { template }) }; + return { ready: true, machine: await service.ensureRunning(botId, template === undefined ? undefined : { template }) }; } + if (action === "screenshot") { + const machine = await service.find(botId); + if (machine?.state !== "running") throw new InstaboxUserError(409, "serverError.instabox.unavailable"); + const result = await runInstaboxComputerTool(service, machine.id, "screenshot", {}, AbortSignal.timeout(90_000)); + const image = result.content.find(part => part.type === "image"); + if (!image || image.type !== "image") throw new InstaboxUserError(502, "serverError.instabox.unavailable"); + return { png: image.data, format: "jpeg" }; + } + if (action === "join") { await service.ensureRunning(botId); return { joinUrl: desktopViewerUrl(`vps/${botId}`) }; } if (action === "sleep") { await service.suspend(botId); return { ok: true }; } if (action === "remove") { await service.remove(botId); return { ok: true }; } throw new InstaboxUserError(501, "serverError.instabox.unavailable"); diff --git a/server/instabox/snapshot-wiring.e2e.test.ts b/server/instabox/snapshot-wiring.e2e.test.ts new file mode 100644 index 00000000..ba437f6b --- /dev/null +++ b/server/instabox/snapshot-wiring.e2e.test.ts @@ -0,0 +1,101 @@ +import { createServer, type Server } from "node:http"; +import { setTimeout as delay } from "node:timers/promises"; +import { afterEach, expect, it, vi } from "vitest"; +import { json, readBody } from "../harness/http.ts"; +import { createInstaboxWiring } from "./wiring.ts"; + +const botId = "snapshot-budget-bot"; +const snapshot = { id: "saved", name: "backup", status: "ready", metadata: { app: "crybot", botId } }; +const machine = { id: "machine", name: `crybot-${botId.slice(0, 12)}`, metadata: { botId }, state: "running" }; +const basePath = `/api/bots/${botId}/instabox/snapshots`; +const servers: Server[] = []; +afterEach(async () => { + for (const server of servers.splice(0)) await new Promise(resolve => { server.close(() => resolve()); server.closeAllConnections(); }); + vi.unstubAllEnvs(); +}); +async function listen(server: Server) { + servers.push(server); + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Fixture did not listen"); + return `http://127.0.0.1:${address.port}`; +} + +async function fixture(stall: "lookup" | "pages" | "poll" | "desktop" | "body", timeoutMs = 600) { + const calls: Array<{ path: string; wait: string | null }> = []; + let cancelled = false; + let locked = false; + let reached!: () => void; + const stalled = new Promise(resolve => { reached = resolve; }); + const upstream = await listen(createServer(async (req, res) => { + for await (const chunk of req) void chunk; + const url = new URL(req.url!, "http://fixture"); + const path = url.pathname; + calls.push({ path, wait: url.searchParams.get("wait") }); + const hanging = stall === "lookup" && path === "/v1/machines" || stall === "poll" && path.startsWith("/v1/operations/") || stall === "desktop" && path.endsWith("/desktop") || stall === "body"; + if (hanging) { + res.on("close", () => { cancelled = !res.writableEnded; }); + if (stall === "body") { res.writeHead(200, { "content-type": "application/json" }); res.write('{"snapshots":['); } + reached(); + return; + } + if (stall === "pages") { + await delay(220); + return json(res, 200, { snapshots: [], nextCursor: `page-${calls.length}` }); + } + if (path === "/v1/machines") { await delay(120); return json(res, 200, { machines: [machine] }); } + if (path === "/v1/snapshots/saved") { await delay(120); return json(res, 200, snapshot); } + if (req.method === "POST" && path.endsWith("/restore")) return json(res, 202, { id: "restore", state: "pending" }); + if (path.startsWith("/v1/operations/")) return json(res, 200, { id: "restore", state: "succeeded" }); + json(res, 500, { error: "unexpected_fixture_request" }); + })); + vi.stubEnv("CRYBOT_TEST_INSTABOX_API", upstream); + vi.stubEnv("INSTABOX_API_KEY", "instabox-e2e-fixture"); + const wiring = createInstaboxWiring({ cfg: () => ({}), eligible: id => id === botId, + inUse: () => false, snapshotTimeoutMs: timeoutMs, + claim: () => { if (locked) throw Object.assign(new Error("busy"), { status: 409 }); locked = true; return () => { locked = false; }; }, + }); + const app = await listen(createServer(async (req, res) => { + const url = new URL(req.url!, "http://fixture"); + await wiring.routes({ req, res, url, path: url.pathname, method: req.method!, json, readBody, auth: { kind: "loopback", scopes: ["admin", "client"] } }); + })); + const send = (signal?: AbortSignal) => fetch(app + basePath + (["poll", "desktop"].includes(stall) ? "/saved/restore" : ""), { + method: ["pages", "body"].includes(stall) ? "GET" : "POST", headers: { origin: app, "content-type": "application/json" }, + ...(["pages", "body"].includes(stall) ? {} : { body: "{}" }), signal, + }); + return { send, calls, stalled, cancelled: () => cancelled, locked: () => locked }; +} + +it.each(["lookup", "pages", "poll", "desktop", "body"] as const)("bounds the whole snapshot flow including slow %s responses", async stage => { + const f = await fixture(stage); + const start = Date.now(); + const response = await f.send(); + expect(response.status).toBe(504); + expect(await response.json()).toMatchObject({ code: "instabox_snapshot_timeout" }); + expect(Date.now() - start).toBeLessThan(1_200); + expect(f.locked()).toBe(false); + if (stage === "desktop") expect(f.calls.map(call => call.path)).toContain("/v1/machines/machine/desktop"); + if (stage !== "pages") await expect.poll(f.cancelled).toBe(true); + if (stage === "pages") expect(f.calls.length).toBeLessThanOrEqual(3); + if (stage === "poll" || stage === "desktop") { + const wait = f.calls.find(call => call.path.startsWith("/v1/operations/"))!.wait!; + expect(parseFloat(wait)).toBeLessThan(0.45); + } + const count = f.calls.length; + await delay(300); + expect(f.calls).toHaveLength(count); +}); + +it("cancels the upstream flow and releases the lifecycle lock when the caller disconnects", async () => { + const f = await fixture("lookup", 10_000); + const controller = new AbortController(); + const response = f.send(controller.signal).catch(() => null); + await f.stalled; + expect(f.locked()).toBe(true); + controller.abort(); + expect(await response).toBeNull(); + await expect.poll(f.cancelled).toBe(true); + await expect.poll(f.locked).toBe(false); + await delay(300); + expect(f.calls).toHaveLength(1); +}); diff --git a/server/instabox/snapshot-wiring.ts b/server/instabox/snapshot-wiring.ts new file mode 100644 index 00000000..2f02c1c5 --- /dev/null +++ b/server/instabox/snapshot-wiring.ts @@ -0,0 +1,64 @@ +import type { AppConfig } from "../config.ts"; +import { createInstaboxSnapshotRoutes } from "../routes/instabox-snapshots.ts"; +import { PASS, type RouteHandler } from "../routes/table.ts"; +import { createInstaboxRequest } from "./client.ts"; +import { instaboxApiKey } from "./key.ts"; +import type { InstaboxRequest } from "./machine.ts"; +import { createInstaboxMachines } from "./machines.ts"; + +export function createSnapshotWiring(deps: { + cfg(): AppConfig; + eligible(botId: string): boolean; + claim(botId: string): () => void; + inUse(botId: string): boolean; + baseUrl?: string; + snapshotTimeoutMs?: number; +}): RouteHandler { + return async ctx => { + if (!/^\/api\/bots\/[\w-]+\/instabox\/snapshots(?:\/|$)/.test(ctx.path)) return PASS; + const controller = new AbortController(); + const deadline = Date.now() + (deps.snapshotTimeoutMs ?? 600_000); + const cancel = () => controller.abort(); + const timer = setTimeout(cancel, deadline - Date.now()); + ctx.req.once("aborted", cancel); + ctx.res.once("close", cancel); + try { + const key = instaboxApiKey(deps.cfg()); + const upstream = key ? createInstaboxRequest(key, deps.baseUrl, 30_000, controller.signal) : null; + const request: InstaboxRequest | null = upstream && ((method, path, opts) => { + if (Date.now() >= deadline) cancel(); + controller.signal.throwIfAborted(); + const query = opts?.query?.wait ? { ...opts.query, wait: `${Math.min(parseFloat(opts.query.wait), 25, (deadline - Date.now()) / 1000)}s` } : opts?.query; + return upstream(method, path, { ...opts, query }); + }); + const machines = request ? createInstaboxMachines(request) : null; + return await createInstaboxSnapshotRoutes({ + request: () => request, machines: () => machines, + machineIdFor: async botId => (await machines?.find(botId))?.id ?? null, + botExists: deps.eligible, signal: controller.signal, + claim: botId => { + const release = deps.claim(botId); + if (deps.inUse(botId)) { + release(); + throw Object.assign(new Error("Computer is in use"), { status: 409 }); + } + return release; + }, + })({ ...ctx, readBody: async req => { + controller.signal.throwIfAborted(); + let abort!: () => void; + const aborted = new Promise((_resolve, reject) => { + abort = () => reject(controller.signal.reason); + controller.signal.addEventListener("abort", abort, { once: true }); + }); + try { return await Promise.race([ctx.readBody(req), aborted]); } + finally { controller.signal.removeEventListener("abort", abort); } + } }); + } finally { + clearTimeout(timer); + ctx.req.off("aborted", cancel); + ctx.res.off("close", cancel); + cancel(); + } + }; +} diff --git a/server/instabox/wiring-backends-fixture.ts b/server/instabox/wiring-backends-fixture.ts new file mode 100644 index 00000000..f101ba00 --- /dev/null +++ b/server/instabox/wiring-backends-fixture.ts @@ -0,0 +1,72 @@ +import { createServer } from "node:http"; +import { mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { pathToFileURL } from "node:url"; +import { removeTempDir } from "../testing/cleanup.ts"; + +export async function wiringBackendsFixture() { + const root = mkdtempSync(join(tmpdir(), "cry-wiring-backends-")); + const calls: Array<{ path: string; body: any }> = []; + const rows: Array<{ id: string; name: string; state: string }> = []; + const server = createServer(async (req, res) => { + let raw = ""; + for await (const chunk of req) raw += chunk; + const body = raw ? JSON.parse(raw) : {}; + const path = new URL(req.url!, "http://fixture").pathname; + calls.push({ path, body }); + const reply = (value: unknown) => { res.setHeader("content-type", "application/json"); res.end(JSON.stringify(value)); }; + if (path === "/vps/exec") return reply({ content: [{ type: "text", text: "VPS fixture completed" }] }); + if (path === "/sandboxes" && req.method === "POST") { + const sandbox = { id: "bx_23456789", name: body.name, state: "ready" }; rows.push(sandbox); return reply({ sandbox }); + } + if (path === "/sandboxes") return reply({ sandboxes: rows }); + if (path.endsWith("/desktop")) return reply({ desktopUrl: "https://desktop.fixture.invalid" }); + if (path.endsWith("/commands")) return reply({ exitCode: 0, stdout: "box fixture completed", stderr: "" }); + const row = rows.find(row => path === `/sandboxes/${row.id}`); + if (row) { if (body.name) row.name = body.name; return reply({ sandbox: row }); } + reply({}); + }); + await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Fixture did not listen"); + const base = `http://127.0.0.1:${address.port}`; + const mcp = join(root, "vps-mcp.mjs"); + writeFileSync(mcp, ` +import { createInterface } from 'node:readline'; +for await (const line of createInterface({ input: process.stdin })) { + const request = JSON.parse(line); + if (request.id === undefined) continue; + let result = {}; + if (request.method === 'initialize') result = { protocolVersion: '2024-11-05', capabilities: { tools: {} }, serverInfo: { name: 'fixture-vps', version: '1' } }; + if (request.method === 'tools/list') result = { tools: [{ name: 'exec', description: 'Fixture command', inputSchema: { type: 'object', properties: { command: { type: 'string' } } } }] }; + if (request.method === 'tools/call') result = await (await fetch(${JSON.stringify(base + "/vps/exec")}, { method: 'POST', body: JSON.stringify(request.params) })).json(); + process.stdout.write(JSON.stringify({ jsonrpc: '2.0', id: request.id, result }) + '\\n'); +} +`); + const actual = new URL("../vps-computer.ts?actual", import.meta.url).href; + const hook = join(root, "vps-hooks.mjs"); + const mock = ` +export * from ${JSON.stringify(actual)}; +const status = { ready: true, daemonUp: true, configured: true, sshAlias: 'fixture-vps', container_id: 'fixture-container', container: 'running' }; +export async function vpsComputerStatus() { return status; } +export async function inspectVpsForAuto() { return status; } +export async function vpsComputerAction() { return status; } +export function vpsComputerMcp() { return { command: process.execPath, args: [${JSON.stringify(mcp)}], env: {} }; } +`; + writeFileSync(hook, ` +import { registerHooks } from 'node:module'; +registerHooks({ + load(url, context, next) { + if (url.endsWith('/vps-computer.ts')) return { format: 'module', shortCircuit: true, source: ${JSON.stringify(mock)} }; + return next(url, context); + } +}); +`); + return { calls, env: { CRYBOT_BOAT_API: base, NODE_OPTIONS: `--import=${pathToFileURL(hook).href}` }, + close: async () => { + await new Promise(resolve => { server.close(() => resolve()); server.closeAllConnections(); }); + await removeTempDir(root); + }, + }; +} diff --git a/server/instabox/wiring.e2e.test.ts b/server/instabox/wiring.e2e.test.ts new file mode 100644 index 00000000..422b17fb --- /dev/null +++ b/server/instabox/wiring.e2e.test.ts @@ -0,0 +1,186 @@ +import { readFileSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { WebSocket } from "ws"; +import { expect, it } from "vitest"; +import { launchRestartableServer } from "../testing/restartable-server.ts"; +import { fakeInstaboxCore } from "../testing/fake-instabox-core.ts"; + +import { wiringBackendsFixture } from "./wiring-backends-fixture.ts"; + +async function fixture(extraEnv: NodeJS.ProcessEnv = {}) { + const fake = await fakeInstaboxCore(); + const env = { ...extraEnv, INSTABOX_API_KEY: "", CRYBOT_TEST_INSTABOX_API: fake.url }; + const app = await launchRestartableServer({ scripted: true, env }); + const request = async (method: string, path: string, body?: unknown) => { + const response = await fetch(app.url + path, { method, headers: { origin: app.url, "content-type": "application/json" }, + ...(body === undefined ? {} : { body: JSON.stringify(body) }), signal: AbortSignal.timeout(30_000) }); + const data = await response.json() as any; + expect(JSON.stringify(data)).not.toContain("instabox-e2e-fixture"); + return { status: response.status, body: data }; + }; + return { fake, env, app, request, close: async () => { await app.close(); await fake.close(); } }; +} + +it("wires saved and environment keys, turn tools, VNC, snapshots, replacement and deletion through the real entry", async () => { + const f = await fixture(); + const { request, fake, app, env } = f; + let socket: WebSocket | undefined; + try { + const stream = await fetch(app.url + "/api/events", { headers: { origin: app.url }, signal: AbortSignal.timeout(10_000) }); + const reader = stream.body!.getReader(); + let frames = ""; + const readFrames = (async () => { for (;;) { const chunk = await reader.read(); if (chunk.done) break; frames += new TextDecoder().decode(chunk.value); } })(); + expect((await request("PUT", "/api/config", { instabox: { apiKey: "instabox-e2e-fixture-file" } })).status).toBe(200); + expect((await request("GET", "/api/config")).body.instabox).toEqual({ configured: true, last4: "file", fromEnv: false }); + await expect.poll(() => frames.includes('"last4":"file"'), { timeout: 5_000 }).toBe(true); + expect(frames).not.toContain("instabox-e2e-fixture"); + await reader.cancel(); await readFrames; + await app.stop(); + env.INSTABOX_API_KEY = "instabox-e2e-fixture-env"; + await app.start(); + expect((await request("GET", "/api/config")).body.instabox).toEqual({ configured: true, last4: "-env", fromEnv: true }); + expect((await request("PATCH", "/api/config", { instabox: { apiKey: "" } })).status).toBe(409); + expect(JSON.parse(readFileSync(join(app.dataDir, "config.json"), "utf8")).instabox.apiKey).toBe("instabox-e2e-fixture-file"); + const created = await request("POST", "/api/bots", { name: "Wiring fixture", computer: "cloud", cloudBackend: "instabox", modelSelection: { instanceId: "claude", model: "claude-sonnet-5" } }); + expect(created.status).toBe(201); + const bot = created.body.bot; + const path = `/api/bots/${bot.id}`; + expect((await request("PATCH", path, { computer: "cloud", cloudBackend: "instabox" })).status).toBe(200); + expect((await request("GET", path + "/computer")).body.backend).toBe("instabox"); + writeFileSync(app.planPath, JSON.stringify({ [bot.id]: { steps: [ + { server: "computer", tool: "exec", arguments: { command: "printf wiring-fixture" } }, + { server: "computer", tool: "screenshot", arguments: {} }, + ], reply: "Wiring complete." } })); + expect((await request("POST", path + "/messages", { text: "Use the computer.", threadId: bot.threadId })).status).toBe(202); + await expect.poll(() => fake.calls.some(call => call.path.endsWith("/files/content")), { timeout: 30_000 }).toBe(true); + await expect.poll(async () => (await request("GET", "/api/bots")).body.bots.find((entry: { id: string }) => entry.id === bot.id)?.busy, { timeout: 30_000 }).toBe(false); + expect(fake.calls.some(call => call.path.endsWith("/exec") && call.body.cmd?.includes("printf wiring-fixture"))).toBe(true); + expect(fake.calls.filter(call => call.path.endsWith("/exec")).every(call => call.body.envs.DISPLAY === ":1")).toBe(true); + expect(fake.calls.every(call => call.headers["x-api-key"] === env.INSTABOX_API_KEY)).toBe(true); + expect(JSON.stringify(fake.calls.map(call => call.body))).not.toContain("instabox-e2e-fixture"); + expect((await request("POST", path + "/computer/screenshot", {})).body).toMatchObject({ format: "jpeg", png: expect.any(String) }); + expect((await request("POST", path + "/computer/join", {})).body.joinUrl).toContain("vps%2F" + bot.id); + expect((await request("GET", `/api/desktop-viewer/vps/${bot.id}`)).body).toEqual({ password: null }); + socket = new WebSocket(app.url.replace("http:", "ws:") + `/api/desktop-viewer/vps/${bot.id}/websockify`, { origin: app.url }); + const handshake = await new Promise((resolve, reject) => { socket!.once("message", bytes => resolve(bytes.toString())); socket!.once("error", reject); }); + expect(handshake).toBe("RFB 003.008\n"); + expect(fake.vncRequests[0]["x-api-key"]).toBe(env.INSTABOX_API_KEY); + const snapshots = path + "/instabox/snapshots"; + const start = fake.calls.length; + const saved = await request("POST", snapshots, {}); + expect(saved.status).toBe(201); + expect(fake.calls.slice(start).some(call => call.path.startsWith("/v1/operations/"))).toBe(true); + expect(fake.calls.slice(start).some(call => call.method === "GET" && call.path === "/v1/snapshots")).toBe(true); + expect((await request("GET", snapshots)).body.snapshots).toEqual([saved.body.snapshot]); + const beforeRestore = fake.calls.length; + expect((await request("POST", `${snapshots}/${saved.body.snapshot.id}/restore`, {})).status).toBe(200); + expect(fake.calls.slice(beforeRestore).some(call => call.path.endsWith("/desktop"))).toBe(true); + const closed = new Promise(resolve => socket!.once("close", () => resolve())); + expect((await request("POST", path + "/computer/remove", {})).status).toBe(200); + expect((await request("POST", path + "/computer/provision", {})).status).toBe(200); + await closed; + expect(fake.machines.size).toBe(2); + expect((await request("DELETE", path)).status).toBe(200); + expect([...fake.machines.values()].every(machine => machine.state === "deleted")).toBe(true); + for (const file of [app.logPath, join(app.dataDir, "fake-claude-dump.json"), app.planPath + ".evidence.jsonl"]) { + expect(readFileSync(file, "utf8")).not.toContain("instabox-e2e-fixture"); + } + } finally { socket?.terminate(); await f.close(); } +}, 120_000); + +it.each(["box", "vps"])("keeps %s computer and snapshot/viewer paths away from InstaBox", async backend => { + const backends = await wiringBackendsFixture(); + const f = await fixture(backends.env); + try { + expect((await f.request("PUT", "/api/config", { instabox: { apiKey: "instabox-e2e-fixture-file" }, box: { token: "boat-e2e-fixture" }, vps: { sshAlias: "fixture-vps" } })).status).toBe(200); + const bot = (await f.request("POST", "/api/bots", { computer: "cloud", cloudBackend: backend, modelSelection: { instanceId: "claude", model: "claude-sonnet-5" } })).body.bot; + const path = `/api/bots/${bot.id}`; + expect((await f.request("PATCH", path, { computer: "cloud", cloudBackend: backend })).status).toBe(200); + await f.request("GET", path + "/computer"); + writeFileSync(f.app.planPath, JSON.stringify({ [bot.id]: { steps: [ + { server: "computer", tool: "exec", arguments: { command: "printf backend-fixture" } }, + ], reply: "Backend complete." } })); + expect((await f.request("POST", path + "/messages", { text: "Use the computer.", threadId: bot.threadId })).status).toBe(202); + await expect.poll(() => backends.calls.some(call => backend === "box" + ? call.path.endsWith("/commands") && call.body.command?.includes("printf backend-fixture") + : call.path === "/vps/exec" && call.body.arguments?.command === "printf backend-fixture"), { timeout: 30_000 }).toBe(true); + await expect.poll(async () => (await f.request("GET", "/api/bots")).body.bots.find((entry: { id: string }) => entry.id === bot.id)?.busy, { timeout: 20_000 }).toBe(false); + expect((await f.request("GET", path + "/instabox/snapshots")).status).toBe(404); + await f.request("GET", `/api/desktop-viewer/vps/${bot.id}`); + expect(f.fake.calls).toEqual([]); + expect(f.fake.vncRequests).toEqual([]); + } finally { await f.close(); await backends.close(); } +}, 60_000); + +it.each(["direct", "group"])("excludes snapshot restore and %s turns in both directions", async kind => { + const f = await fixture(); + const holds: Array<{ release(): void }> = []; + try { + await f.request("PUT", "/api/config", { instabox: { apiKey: "instabox-e2e-fixture" } }); + const bot = (await f.request("POST", "/api/bots", { name: "Lifecycle fixture", modelSelection: { instanceId: "claude", model: "claude-sonnet-5" } })).body.bot; + const path = `/api/bots/${bot.id}`; + expect((await f.request("PATCH", path, { computer: "cloud", cloudBackend: "instabox" })).status).toBe(200); + expect((await f.request("POST", path + "/computer/provision", {})).status).toBe(200); + const saved = await f.request("POST", path + "/instabox/snapshots", {}); + expect(saved.status).toBe(201); + const restore = `${path}/instabox/snapshots/${saved.body.snapshot.id}/restore`; + const room = kind === "group" ? (await f.request("POST", "/api/groups", { name: "Lifecycle room", botIds: [bot.id] })).body.group : null; + const messages = room ? `/api/groups/${room.id}/messages` : path + "/messages"; + writeFileSync(f.app.planPath, JSON.stringify({ [bot.id]: { steps: [ + { server: "computer", tool: "exec", arguments: { command: "printf lifecycle-fixture" } }, + ], reply: "Lifecycle complete." } })); + const exec = f.fake.holdNext("POST", "/v1/machines/machine-1/exec"); holds.push(exec); + expect((await f.request("POST", messages, { text: "Use the computer.", threadId: room?.threadId ?? bot.threadId })).status).toBe(202); + await exec.reached; + const before = f.fake.calls.length; + expect((await f.request("POST", restore, {})).status).toBe(409); + expect(f.fake.calls.slice(before).some(call => call.path.endsWith("/restore"))).toBe(false); + exec.release(); + await expect.poll(async () => { + const state = (await f.request("GET", "/api/bots")).body; + return room ? state.groups.find((entry: { id: string }) => entry.id === room.id)?.working : state.bots.find((entry: { id: string }) => entry.id === bot.id)?.busy; + }, { timeout: 30_000 }).toBe(false); + const heldRestore = f.fake.holdNext("POST", "/v1/machines/machine-1/restore"); holds.push(heldRestore); + const restoring = f.request("POST", restore, {}); + await heldRestore.reached; + const execCount = f.fake.calls.filter(call => call.path.endsWith("/exec")).length; + expect((await f.request("POST", messages, { text: "Try during restore.", threadId: room?.threadId ?? bot.threadId })).status).toBe(409); + expect((await f.request("DELETE", path)).status).toBe(409); + expect((await f.request("POST", path + "/computer/control", { action: "take" })).status).toBe(409); + expect(f.fake.calls.filter(call => call.path.endsWith("/exec"))).toHaveLength(execCount); + heldRestore.release(); + expect((await restoring).status).toBe(200); + expect((await f.request("POST", path + "/computer/control", { action: "take" })).status).toBe(200); + expect((await f.request("POST", restore, {})).status).toBe(409); + expect((await f.request("POST", path + "/computer/control", { action: "release" })).status).toBe(200); + expect((await f.request("POST", restore, {})).status).toBe(200); + } finally { for (const hold of holds) hold.release(); await f.close(); } +}, 90_000); + +it("does not recreate a deleted machine when the viewer opens before bot deletion finishes", async () => { + const f = await fixture(); + const holds: Array<{ release(): void }> = []; + try { + await f.request("PUT", "/api/config", { instabox: { apiKey: "instabox-e2e-fixture" } }); + const bot = (await f.request("POST", "/api/bots", { name: "Deletion fixture" })).body.bot; + const path = `/api/bots/${bot.id}`; + expect((await f.request("PATCH", path, { computer: "cloud", cloudBackend: "instabox" })).status).toBe(200); + expect((await f.request("POST", path + "/computer/provision", {})).status).toBe(200); + const deleting = f.fake.holdNext("DELETE", "/v1/machines/machine-1", true); holds.push(deleting); + const deletion = f.request("DELETE", path); + await deleting.reached; + const operation = [...f.fake.operations.values()].find(op => op.finalState === "deleted")!; + operation.polls = 1; + const deleted = f.fake.holdNext("GET", `/v1/operations/${operation.id}`, true); holds.push(deleted); + deleting.release(); + await deleted.reached; + expect(f.fake.machines.get("machine-1")?.state).toBe("deleted"); + expect((await f.request("GET", "/api/bots")).body.bots.some((entry: { id: string }) => entry.id === bot.id)).toBe(true); + expect((await f.request("GET", `/api/desktop-viewer/vps/${bot.id}`)).status).toBe(409); + expect(f.fake.machines.size).toBe(1); + deleted.release(); + expect((await deletion).status).toBe(200); + expect((await f.request("GET", `/api/desktop-viewer/vps/${bot.id}`)).status).toBe(404); + expect([...f.fake.machines.values()].every(machine => machine.state === "deleted")).toBe(true); + } finally { for (const hold of holds) hold.release(); await f.close(); } +}, 60_000); diff --git a/server/instabox/wiring.test.ts b/server/instabox/wiring.test.ts new file mode 100644 index 00000000..4d7c6361 --- /dev/null +++ b/server/instabox/wiring.test.ts @@ -0,0 +1,32 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { createInstaboxWiring } from "./wiring.ts"; + +afterEach(() => vi.unstubAllEnvs()); + +it.each(["deleted", "backend changed", "key changed"])("revalidates a retained viewer target after the lifecycle claim when %s", async reason => { + vi.stubEnv("INSTABOX_API_KEY", "instabox-e2e-fixture"); + let eligible = true; + const release = vi.fn(); + const claim = vi.fn(() => { + if (reason === "key changed") vi.stubEnv("INSTABOX_API_KEY", "instabox-e2e-replacement"); + else eligible = false; + return release; + }); + const wiring = createInstaboxWiring({ cfg: () => ({}), eligible: () => eligible, claim, inUse: () => false }); + const ensure = vi.spyOn(wiring.machines()!, "ensureRunning").mockRejectedValue(new Error("Unexpected machine startup")); + const target = wiring.target("fixture-bot")!; + await expect(target.resolve()).rejects.toThrow("unavailable"); + expect(claim).toHaveBeenCalledWith("fixture-bot", true); + expect(release).toHaveBeenCalledOnce(); + expect(ensure).not.toHaveBeenCalled(); +}); + +it("ignores the loopback API override outside a test environment", () => { + vi.stubEnv("NODE_ENV", "production"); + vi.stubEnv("VITEST", ""); + vi.stubEnv("CRYBOT_TEST_SEALED_PATH", ""); + vi.stubEnv("INSTABOX_API_KEY", "instabox-e2e-fixture"); + vi.stubEnv("CRYBOT_TEST_INSTABOX_API", "http://127.0.0.1:43210"); + const wiring = createInstaboxWiring({ cfg: () => ({}), eligible: () => true, claim: () => () => {}, inUse: () => false }); + expect(wiring.allowInsecureLoopbackUpstream).toBe(false); +}); diff --git a/server/instabox/wiring.ts b/server/instabox/wiring.ts new file mode 100644 index 00000000..37cd51c7 --- /dev/null +++ b/server/instabox/wiring.ts @@ -0,0 +1,64 @@ +import type { AppConfig } from "../config.ts"; +import { sealedFixture } from "../env-path.ts"; +import type { DesktopTarget } from "../routes/desktop-viewer.ts"; +import { createSnapshotWiring } from "./snapshot-wiring.ts"; +import { createInstaboxRequest } from "./client.ts"; +import { instaboxApiKey } from "./key.ts"; +import type { InstaboxMachine, InstaboxMachines, InstaboxRequest } from "./machine.ts"; +import { createInstaboxMachines } from "./machines.ts"; +import { requireInstabox } from "./power.ts"; +import { instaboxDesktopTarget } from "./vnc.ts"; + +export function createInstaboxWiring(deps: { cfg(): AppConfig; eligible(botId: string): boolean; claim(botId: string, allowPreview?: boolean): () => void; inUse(botId: string): boolean; snapshotTimeoutMs?: number }) { + const testEnvironment = process.env.NODE_ENV === "test" || process.env.VITEST === "true" || sealedFixture(); + const fixtureApi = testEnvironment ? process.env.CRYBOT_TEST_INSTABOX_API : undefined; + const fixture = /^http:\/\/127\.0\.0\.1:[1-9]\d{0,4}$/.test(fixtureApi ?? ""); + let cached: { key: string; machines: InstaboxMachines; request: InstaboxRequest } | undefined; + const current = new Map(); + function service() { + const key = instaboxApiKey(deps.cfg()); + if (!key) { cached = undefined; current.clear(); return null; } + if (cached?.key !== key) { + current.clear(); + const request = createInstaboxRequest(key, fixture ? fixtureApi : undefined); + const machines = createInstaboxMachines(request); + const remember = (botId: string, machine: InstaboxMachine | null) => { + if (machine && machine.state !== "deleted") current.set(botId, machine.id); + else current.delete(botId); + return machine; + }; + cached = { key, request, machines: { ...machines, + find: async botId => remember(botId, await machines.find(botId)), + ensureRunning: async (botId, opts) => remember(botId, await machines.ensureRunning(botId, opts))!, + remove: async botId => { await machines.remove(botId); current.delete(botId); }, + } }; + } + return cached; + } + const machines = () => service()?.machines ?? null; + return { + machines, + allowInsecureLoopbackUpstream: fixture, + tools(boxId: string | null | undefined) { + if (!boxId?.startsWith("instabox/")) return undefined; + const active = service(); + return { machines: requireInstabox(active?.machines ?? null), request: active!.request, machineId: () => boxId.slice(9) }; + }, + target(botId: string): DesktopTarget | undefined { + if (!deps.eligible(botId)) return; + const active = service(); + if (!active) return; + const target = instaboxDesktopTarget(`instabox/${botId}/${current.get(botId) ?? "pending"}`, async () => { + const release = deps.claim(botId, true); + try { + if (!deps.eligible(botId) || service() !== active) throw new Error("InstaBox computer is unavailable"); + const machine = await active.machines.ensureRunning(botId); + target.key = `instabox/${botId}/${machine.id}`; + return await active.machines.vncUpstream(machine.id); + } finally { release(); } + }, { live: () => deps.eligible(botId) && service() === active }); + return target; + }, + routes: createSnapshotWiring({ ...deps, baseUrl: fixture ? fixtureApi : undefined }), + }; +} diff --git a/server/routes/config-instabox.test.ts b/server/routes/config-instabox.test.ts index 57af0576..ce67d1bb 100644 --- a/server/routes/config-instabox.test.ts +++ b/server/routes/config-instabox.test.ts @@ -17,7 +17,7 @@ function fixture(reported?: unknown) { providerConfigs: () => ({}), registry: { forgetSnapshots: vi.fn() }, lendingEnabled: () => true, revokeEngineCapabilities: vi.fn(), broadcast, configStatus: () => ({ instabox: reported ?? { ...keyStatus(cfg.instabox?.apiKey), fromEnv: false } }), - configForAccess: (status, auth) => auth.scopes.includes("admin") ? status : withoutKeyHints(status), + configForAccess: (status: Parameters[0], auth: Parameters[1]) => auth.scopes.includes("admin") ? status : withoutKeyHints(status), } as unknown as ConfigRouteDeps); return { cfg, broadcast, request: async (method = "GET", body?: unknown, admin = true) => { let response!: { status: number; body: Record }; diff --git a/server/routes/instabox-snapshots.e2e.test.ts b/server/routes/instabox-snapshots.e2e.test.ts index fcbba34f..162c2780 100644 --- a/server/routes/instabox-snapshots.e2e.test.ts +++ b/server/routes/instabox-snapshots.e2e.test.ts @@ -27,6 +27,7 @@ beforeEach(async () => { machines: () => configured ? { ensureRunning } as unknown as InstaboxMachines : null, machineIdFor: async () => machineId, botExists: (id) => id === botId, + claim: () => () => {}, }); const configRoute = createConfigRoutes({ cfg: { instabox: { apiKey: "fixture-only-key" } }, configStatus: () => ({ instabox: { apiKey: "never-return-this", last4: "this" } }), configForAccess: (status: unknown) => status } as unknown as ConfigRouteDeps); server = createServer(async (req, res) => { @@ -50,13 +51,13 @@ const send = (path = basePath, method = "GET", body?: unknown, headers?: Record< method, headers: { origin: base, "content-type": "application/json", ...headers }, ...(body === undefined ? {} : { body: JSON.stringify(body) }), }); -const ownSnapshot = () => fake.snapshots.set("own", { id: "own", name: "crybot-bot-snapshot-backup", state: "ready" }); +const ownSnapshot = () => fake.snapshots.set("own", { id: "own", name: "crybot-bot-snapshot-backup", status: "ready", sourceMachineId: "machine-one" }); describe("isolated InstaBox snapshot HTTP routes", () => { it("lists only owned snapshots by metadata or prefix and never relays metadata", async () => { ownSnapshot(); - fake.snapshots.set("meta", { id: "meta", name: "backup", state: "ready", metadata: { app: "crybot", botId } }); - fake.snapshots.set("other", { id: "other", name: "crybot-bot-snapshot-spoof", state: "ready", metadata: { botId: "other-bot" } }); + fake.snapshots.set("meta", { id: "meta", name: "backup", status: "ready", sourceMachineId: "machine-one", metadata: { app: "crybot", botId } }); + fake.snapshots.set("other", { id: "other", name: "crybot-bot-snapshot-spoof", status: "ready", sourceMachineId: "machine-one", metadata: { botId: "other-bot" } }); const response = await send(); expect(response.status).toBe(200); const body = await response.json() as { snapshots: Array<{ id: string }> }; @@ -78,6 +79,8 @@ describe("isolated InstaBox snapshot HTTP routes", () => { expect(snapshot.name.startsWith("crybot-bot-snapshot-")).toBe(true); } expect(names[0]).not.toBe(names[1]); + expect(fake.calls.some(call => call.path.startsWith("/v1/operations/"))).toBe(true); + expect(fake.calls.some(call => call.method === "GET" && call.path === "/v1/snapshots")).toBe(true); expect((await send(`${basePath}/${ids[0]}/restore`, "POST")).status).toBe(200); expect(ensureRunning).toHaveBeenCalledWith(botId); const restore = fake.calls.find((call) => call.path.endsWith("/restore")); @@ -99,7 +102,7 @@ describe("isolated InstaBox snapshot HTTP routes", () => { }); it.each(["DELETE", "POST"])("refuses another bot's snapshot before %s", async (method) => { - fake.snapshots.set("other", { id: "other", name: "crybot-other-bot-backup", state: "ready", metadata: { botId: "other-bot" } }); + fake.snapshots.set("other", { id: "other", name: "crybot-other-bot-backup", status: "ready", sourceMachineId: "machine-one", metadata: { botId: "other-bot" } }); const response = await send(`${basePath}/other${method === "POST" ? "/restore" : ""}`, method); expect(response.status).toBe(403); expect(fake.calls).toHaveLength(1); diff --git a/server/routes/instabox-snapshots.ts b/server/routes/instabox-snapshots.ts index 70f7c184..10d48307 100644 --- a/server/routes/instabox-snapshots.ts +++ b/server/routes/instabox-snapshots.ts @@ -41,13 +41,13 @@ async function call(request: InstaboxRequest, method: string, path: string, body return response; } -async function readySnapshot(request: InstaboxRequest, id: string): Promise { +async function readySnapshot(request: InstaboxRequest, id: string, signal?: AbortSignal): Promise { const deadline = Date.now() + 180_000; while (Date.now() < deadline) { const snapshot = snapshotSchema.parse(await (await call(request, "GET", `/v1/snapshots/${encodeURIComponent(id)}`)).json()); if (snapshot.state === "ready") return snapshot; if (["failed", "error", "deleted"].includes(snapshot.state)) throw new SnapshotError(502, "failed"); - await delay(500); + await delay(500, undefined, { signal }); } throw new SnapshotError(504, "timeout"); } @@ -67,35 +67,34 @@ async function listSnapshots(request: InstaboxRequest): Promise { return snapshots; } -async function findSnapshotByName(request: InstaboxRequest, name: string): Promise { +async function findSnapshotByName(request: InstaboxRequest, name: string, signal?: AbortSignal): Promise { const deadline = Date.now() + 180_000; while (Date.now() < deadline) { const snapshot = (await listSnapshots(request)).find((candidate) => candidate.name === name); if (snapshot) return snapshot; - await delay(500); + await delay(500, undefined, { signal }); } throw new SnapshotError(504, "timeout"); } -async function finishOperation(request: InstaboxRequest, response: Response): Promise { +async function finishOperation(request: InstaboxRequest, response: Response, signal?: AbortSignal): Promise { if (response.status === 204) return; - await waitOperation(request, await response.json()); + await waitOperation(request, await response.json(), signal); } -async function waitOperation(request: InstaboxRequest, initial: unknown): Promise { +async function waitOperation(request: InstaboxRequest, initial: unknown, signal?: AbortSignal): Promise { let operation = operationSchema.parse(initial); const deadline = Date.now() + 180_000; while (operation.state !== "succeeded") { if (["failed", "cancelled", "canceled"].includes(operation.state)) throw new SnapshotError(502, "failed"); if (Date.now() >= deadline) throw new SnapshotError(504, "timeout"); - const next = await call(request, "GET", `/v1/operations/${encodeURIComponent(operation.id)}`, undefined, { wait: "25s" }); + const next = await call(request, "GET", `/v1/operations/${encodeURIComponent(operation.id)}`, undefined, { wait: `${Math.max(1, Math.min(25, Math.ceil((deadline - Date.now()) / 1000)))}s` }); operation = operationSchema.parse(await next.json()); - if (operation.state !== "succeeded") await delay(500); + if (operation.state !== "succeeded") await delay(500, undefined, { signal }); } } -export function createInstaboxSnapshotRoutes(deps: { machines(): InstaboxMachines | null; request(): InstaboxRequest | null; machineIdFor(botId: string): Promise; botExists(botId: string): boolean }): RouteHandler { - const busy = new Set(); +export function createInstaboxSnapshotRoutes(deps: { machines(): InstaboxMachines | null; request(): InstaboxRequest | null; machineIdFor(botId: string): Promise; botExists(botId: string): boolean; claim(botId: string): () => void; signal?: AbortSignal }): RouteHandler { return async ({ req, res, path, method, auth, json, readBody }) => { const match = /^\/api\/bots\/([\w-]+)\/instabox\/snapshots(?:\/([\w-]+)(\/restore)?)?$/.exec(path); if (!match) return PASS; @@ -109,10 +108,12 @@ export function createInstaboxSnapshotRoutes(deps: { machines(): InstaboxMachine if (!deps.botExists(botId)) return json(res, 404, { error: "Bot not found", code: "bot_missing" }); const request = deps.request(), machines = deps.machines(); if (!request || !machines) return json(res, 503, { error: "InstaBox key is not set", code: "instabox_key_missing" }); - if (!listing && busy.has(botId)) return json(res, 409, { error: "Snapshot operation in progress", code: "instabox_snapshot_busy" }); + res.setTimeout(700_000); res.setHeader("cache-control", "private, no-store"); - if (!listing) busy.add(botId); + let release: (() => void) | undefined; try { + if (!listing) release = deps.claim(botId); + if (!deps.botExists(botId)) throw new SnapshotError(404, "bot_missing"); if (listing) { const snapshots = await listSnapshots(request); return json(res, 200, { snapshots: snapshots.filter((snapshot) => belongsTo(snapshot, botId)).map(({ id, name, state }) => ({ id, name, state })) }); @@ -128,15 +129,15 @@ export function createInstaboxSnapshotRoutes(deps: { machines(): InstaboxMachine name, description: `Cry Bot snapshot for ${botId}`, }); const createdPayload: unknown = await response.json(); - const isOperation = typeof createdPayload === "object" && createdPayload !== null && ("type" in createdPayload || "phase" in createdPayload || "machineId" in createdPayload); + const isOperation = typeof createdPayload === "object" && createdPayload !== null && ("type" in createdPayload || "phase" in createdPayload); let created: Snapshot; if (isOperation) { - await waitOperation(request, createdPayload); - created = await findSnapshotByName(request, name); + await waitOperation(request, createdPayload, deps.signal); + created = await findSnapshotByName(request, name, deps.signal); } else { created = snapshotSchema.parse(createdPayload); } - const { id, name: readyName, state } = await readySnapshot(request, created.id); + const { id, name: readyName, state } = await readySnapshot(request, created.id, deps.signal); return json(res, 201, { snapshot: { id, name: readyName, state } }); } const response = await call(request, "GET", `/v1/snapshots/${encodeURIComponent(snapshotId!)}`); @@ -146,17 +147,19 @@ export function createInstaboxSnapshotRoutes(deps: { machines(): InstaboxMachine if (snapshot.state !== "ready") throw new SnapshotError(409, "not_ready"); const machineId = await deps.machineIdFor(botId); if (!machineId) throw new SnapshotError(409, "machine_missing"); - await finishOperation(request, await call(request, "POST", `/v1/machines/${encodeURIComponent(machineId)}/restore`, { snapshotId, desiredState: "running" })); + await finishOperation(request, await call(request, "POST", `/v1/machines/${encodeURIComponent(machineId)}/restore`, { snapshotId, desiredState: "running" }), deps.signal); await machines.ensureRunning(botId); } else { - await finishOperation(request, await call(request, "DELETE", `/v1/snapshots/${encodeURIComponent(snapshotId!)}`)); + await finishOperation(request, await call(request, "DELETE", `/v1/snapshots/${encodeURIComponent(snapshotId!)}`), deps.signal); } return json(res, 200, { ok: true }); } catch (error) { + if (deps.signal?.aborted) return json(res, 504, { error: "InstaBox snapshot request timed out or disconnected", code: "instabox_snapshot_timeout" }); + if (!(error instanceof SnapshotError) && error instanceof Error && "status" in error && error.status === 409) return json(res, 409, { error: "Computer is in use", code: "instabox_snapshot_busy" }); const known = error instanceof SnapshotError; return json(res, known ? error.status : 502, { error: "InstaBox snapshot request failed", code: `instabox_snapshot_${known ? error.code : "upstream"}` }); } finally { - if (!listing) busy.delete(botId); + release?.(); } }; } diff --git a/server/testing/fake-instabox-core.ts b/server/testing/fake-instabox-core.ts index 4546584d..6fa8a2c0 100644 --- a/server/testing/fake-instabox-core.ts +++ b/server/testing/fake-instabox-core.ts @@ -1,7 +1,11 @@ import { createServer, type IncomingHttpHeaders } from "node:http"; +import { WebSocketServer } from "ws"; import type { InstaboxMachineState } from "../instabox/machine.ts"; export async function fakeInstaboxCore() { + const snapshots = new Map(); + const vncRequests: IncomingHttpHeaders[] = []; + const sockets = new WebSocketServer({ noServer: true }); const machines = new Map }>(); const operations = new Map(); const files = new Map(); @@ -13,6 +17,7 @@ export async function fakeInstaboxCore() { const calls: Array<{ method: string; path: string; query: URLSearchParams; headers: IncomingHttpHeaders; body: any }> = []; let failure: { status: number; code: string; message?: string } | undefined; let operationFailure = false; + const holds: Array<{ method: string; path: string; response: boolean; entered(): void; wait: Promise; release(): void; cancelled: boolean }> = []; const server = createServer(async (req, res) => { const url = new URL(req.url!, "http://localhost"); const chunks: Buffer[] = []; @@ -20,11 +25,24 @@ export async function fakeInstaboxCore() { const raw = Buffer.concat(chunks); const body = req.headers["content-type"] === "application/json" && raw.length ? JSON.parse(raw.toString()) : raw; calls.push({ method: req.method!, path: url.pathname, query: url.searchParams, headers: req.headers, body }); + const hold = holds.find(entry => entry.method === req.method && entry.path === url.pathname); + if (hold) holds.splice(holds.indexOf(hold), 1); + if (hold) res.on("close", () => { hold.cancelled = !res.writableEnded; }); + if (hold && !hold.response) { hold.entered(); await hold.wait; } const send = (status: number, value: unknown) => { - res.writeHead(status, { "Content-Type": "application/json" }); res.end(JSON.stringify(value)); + const finish = () => { res.writeHead(status, { "Content-Type": "application/json" }); res.end(JSON.stringify(value)); }; + if (hold?.response) { hold.entered(); void hold.wait.then(finish); } + else finish(); }; if (failure) { const next = failure; failure = undefined; return send(next.status, { error: next }); } if (!req.headers["x-api-key"]) return send(401, { error: { code: "unauthorized" } }); + if (url.pathname === "/v1/snapshots") return send(200, { snapshots: [...snapshots.values()] }); + if (url.pathname.startsWith("/v1/snapshots/")) { + const snapshot = snapshots.get(url.pathname.split("/").at(-1)!); + if (!snapshot) return send(404, { error: { code: "not_found" } }); + snapshot.status = "ready"; + return send(200, snapshot); + } const op = url.pathname.match(/^\/v1\/operations\/(.+)$/); if (op) { const operation = operations.get(op[1]); @@ -37,7 +55,7 @@ export async function fakeInstaboxCore() { } const finish = (machineId: string, finalState: InstaboxMachineState) => { if (!req.headers["idempotency-key"]) return send(400, { error: { code: "idempotency_required" } }); - const operation = { id: `op-${operations.size + 1}`, machineId, state: "pending", finalState, polls: 0 }; + const operation = { id: `op-${operations.size + 1}`, machineId, type: "fixture", phase: "pending", state: "pending", finalState, polls: 0 }; operations.set(operation.id, operation); send(202, operation); }; @@ -66,6 +84,12 @@ export async function fakeInstaboxCore() { if (desktop.has(machine.id)) return send(409, { error: { code: "desktop_already_running" } }); desktop.add(machine.id); return send(200, { running: true }); } + if (action === "snapshots") { + const snapshot = { id: `snapshot-${snapshots.size + 1}`, name: body.name, status: "pending" as const, sourceMachineId: machine.id }; + snapshots.set(snapshot.id, snapshot); + return finish(machine.id, "running"); + } + if (action === "restore") { desktop.delete(machine.id); return finish(machine.id, "running"); } if (action === "exec") return send(200, { stdout: "fixture output", stderr: "", exitCode: 0, timedOut: false, truncated: false }); const fileKey = `${machine.id}:${url.searchParams.get("path")}`; if (action === "files" && req.method === "PUT") { files.set(fileKey, raw); res.writeHead(204); return res.end(); } @@ -86,14 +110,27 @@ export async function fakeInstaboxCore() { res.on("close", () => { clearInterval(timer); stream.cancelled = !stream.completed; }); return; } - res.writeHead(200); return res.end(files.get(fileKey) ?? Buffer.alloc(0)); + res.writeHead(200); return res.end(files.get(fileKey) ?? (url.searchParams.get("path")?.startsWith("/tmp/cry-instabox-") ? Buffer.from("/9j/4AAQSkZJRgABAQAAAQABAAD/2Q==", "base64") : Buffer.alloc(0))); } send(404, { error: { code: "not_found" } }); }); + server.on("upgrade", (req, socket, head) => { + vncRequests.push(req.headers); + sockets.handleUpgrade(req, socket, head, peer => { peer.on("error", () => {}); peer.send(Buffer.from("RFB 003.008\n")); }); + }); await new Promise(resolve => server.listen(0, "127.0.0.1", resolve)); const address = server.address(); if (!address || typeof address === "string") throw new Error("Fake InstaBox failed to listen"); - return { url: `http://127.0.0.1:${address.port}`, calls, machines, operations, desktop, + return { url: `http://127.0.0.1:${address.port}`, calls, machines, operations, desktop, snapshots, vncRequests, + holdNext: (method: string, path: string, response = false) => { + let release!: () => void; + let entered!: () => void; + const wait = new Promise(resolve => { release = resolve; }); + const reached = new Promise(resolve => { entered = resolve; }); + const hold = { method, path, response, wait, entered, release, cancelled: false }; + holds.push(hold); + return { reached, release, cancelled: () => hold.cancelled }; + }, streamFile: (machineId: string, path: string, options: { chunk: Buffer; chunkCount: number; contentLength?: number }) => { const stream = { ...options, chunksSent: 0, cancelled: false, completed: false }; fileStreams.set(`${machineId}:${path}`, stream); @@ -101,6 +138,6 @@ export async function fakeInstaboxCore() { }, failNext: (next: NonNullable) => { failure = next; }, failOperations: () => { operationFailure = true; }, - close: () => new Promise((resolve, reject) => { server.close(error => error ? reject(error) : resolve()); server.closeAllConnections(); }), + close: () => new Promise((resolve, reject) => { for (const peer of sockets.clients) peer.terminate(); sockets.close(); server.close(error => error ? reject(error) : resolve()); server.closeAllConnections(); }), }; } diff --git a/server/testing/fake-instabox-snapshots.ts b/server/testing/fake-instabox-snapshots.ts index 3fa1efa9..c47ec43f 100644 --- a/server/testing/fake-instabox-snapshots.ts +++ b/server/testing/fake-instabox-snapshots.ts @@ -3,7 +3,7 @@ import { randomUUID } from "node:crypto"; import { json, readBody } from "../harness/http.ts"; import type { InstaboxRequest } from "../instabox/machine.ts"; -export interface FakeSnapshot { id: string; name: string; state: string; metadata?: { app?: string; botId?: string } } +export interface FakeSnapshot { id: string; name: string; status: "pending" | "ready" | "failed"; sourceMachineId: string; metadata?: { app?: string; botId?: string } } type CreateResponseShape = "operation" | "snapshot"; export async function fakeInstaboxSnapshots(options: { createResponse?: CreateResponseShape } = {}) { @@ -25,7 +25,7 @@ export async function fakeInstaboxSnapshots(options: { createResponse?: CreateRe if (url.pathname === "/v1/snapshots") return json(res, 200, { snapshots: [...snapshots.values()] }); const id = url.pathname.split("/").at(-1)!; if (method === "POST" && url.pathname.endsWith("/snapshots")) { - const snapshot = { id: randomUUID(), name: body.name, state: "creating" }; + const snapshot = { id: randomUUID(), name: body.name, status: "pending" as const, sourceMachineId: "machine-one" }; snapshots.set(snapshot.id, snapshot); if (createResponse === "snapshot") return json(res, 202, snapshot); const operation = { id: randomUUID(), machineId: "machine-one", type: "create", state: "pending", phase: "snapshotting", error: null }; @@ -35,7 +35,7 @@ export async function fakeInstaboxSnapshots(options: { createResponse?: CreateRe if (method === "GET" && url.pathname.startsWith("/v1/snapshots/")) { const snapshot = snapshots.get(id); if (!snapshot) return json(res, 404, { error: "not found" }); - if (snapshot.state === "creating") snapshot.state = snapshotFails ? "failed" : "ready"; + if (snapshot.status === "pending") snapshot.status = snapshotFails ? "failed" : "ready"; return json(res, 200, snapshot); } if (method === "DELETE" || (method === "POST" && url.pathname.endsWith("/restore"))) { diff --git a/shared/place-view.ts b/shared/place-view.ts index 1584f759..3ee5d5a6 100644 --- a/shared/place-view.ts +++ b/shared/place-view.ts @@ -88,7 +88,7 @@ export interface PlaceFacts { /** Boat desktops are unavailable in provider-locked installations. */ providerLock?: boolean; /** The cloud computer's provider: Boat, or the person's own server. */ - backend?: "box" | "vps"; + backend?: "box" | "vps" | "instabox"; engine: { name: string; model?: string; diff --git a/src/components/CloudBackendPicker.test.ts b/src/components/CloudBackendPicker.test.ts index b9ffeb7d..30b388ec 100644 --- a/src/components/CloudBackendPicker.test.ts +++ b/src/components/CloudBackendPicker.test.ts @@ -32,11 +32,12 @@ describe("CloudBackendPicker", () => { }); it.each(["box", "vps", "instabox"] as const)("offers all three backends whichever is chosen (%s)", (value) => { + fixture.config = { instabox: { configured: true } }; const markup = picker(value); expect(markup).toContain("Boat"); expect(markup).toContain("InstaBox"); expect(markup).toContain("Self-hosted VPS"); - expect(markup).not.toContain("disabled"); + expect(markup).not.toContain('disabled=""'); expect(markup).not.toMatch(/ACP model provider/); }); @@ -46,8 +47,9 @@ describe("CloudBackendPicker", () => { }); }); -it("prompts for an InstaBox key without disabling selection", () => { - expect(picker("instabox")).toContain("Add your InstaBox key in Settings first"); +it("disables InstaBox and prompts for a key even when another backend is selected", () => { + expect(picker("box")).toContain("Add your InstaBox key in Settings first"); + expect(picker("box")).toContain('disabled=""'); fixture.config = { instabox: { configured: true } }; expect(picker("instabox")).not.toContain("Add your InstaBox key in Settings first"); }); diff --git a/src/components/CloudBackendPicker.tsx b/src/components/CloudBackendPicker.tsx index cae784a1..abc909fe 100644 --- a/src/components/CloudBackendPicker.tsx +++ b/src/components/CloudBackendPicker.tsx @@ -30,10 +30,12 @@ export function CloudBackendPicker({ ))} - {value === "instabox" && !instaboxKeyStatus(state.config).configured &&

{t("botAccess.cloud.instaboxKeyHint")}

} + {!instaboxKeyStatus(state.config).configured &&

{t("botAccess.cloud.instaboxKeyHint")}

} ); } diff --git a/src/components/ComputerPanel.instabox.test.ts b/src/components/ComputerPanel.instabox.test.ts new file mode 100644 index 00000000..472b2bfb --- /dev/null +++ b/src/components/ComputerPanel.instabox.test.ts @@ -0,0 +1,65 @@ +// @vitest-environment happy-dom +import { createElement } from "react"; +import { flushSync } from "react-dom"; +import { createRoot } from "react-dom/client"; +import { expect, it, vi } from "vitest"; +import type { AppState, Bot, InstanceInfo } from "@/state/store"; + +const fixture = vi.hoisted(() => ({ configured: false, reads: 0 })); +vi.mock("./DesktopCapabilities", async original => ({ + ...await original(), + useCaptionChrome: () => ({ padClass: undefined }), + useDesktopCapabilities: () => ({ ready: true, capabilities: { + host: { platform: "linux", label: "Fixture", session: "unknown", packaged: false, homeDir: "/tmp" }, + windowChrome: "native", screenPreview: { available: false, interaction: "none" }, + dictation: { available: false, engine: "none", onDevice: false }, + localComputer: { available: false, support: "unsupported", enabled: false, status: "unavailable" }, + } }), +})); +vi.mock("@/lib/interface-mode", () => ({ useAdvancedMode: () => true, setAdvancedMode: () => {} })); +vi.mock("./AndroidDevicePanel", () => ({ AndroidDevicePanel: () => null, useAndroidUsbDevices: () => ({ devices: [] }) })); +vi.mock("./BrowserPanel", () => ({ BrowserPanel: () => null })); +vi.mock("./InstaboxSnapshots", () => ({ InstaboxSnapshots: () => null })); +vi.mock("./bot-settings/RoutinesSection", () => ({ RoutinesSection: () => null })); +vi.mock("@/state/store", async original => ({ + ...await original(), + api: async (path: string) => { + if (path.startsWith("/api/bots/fixture-bot/computer?")) { + fixture.reads++; + return { backend: "instabox", configured: fixture.configured, ready: false, machine: null }; + } + if (path.includes("computer/control")) return { held: false, helpReason: null }; + return {}; + }, +})); +const { ComputerPanel } = await import("./ComputerPanel"); +const { BotEditorStore, initialState } = await import("@/state/store"); + +it("refreshes the selected InstaBox panel when its key is saved in Settings", async () => { + const bot = { id: "fixture-bot", threadId: "fixture-thread", name: "Fixture", title: "", description: "", color: "green", + computer: "cloud", cloudBackend: "instabox", modelSelection: { instanceId: "fixture", model: "fixture" }, + messages: [], tasks: [], busy: false } as unknown as Bot; + const instance = { instanceId: "fixture", driverKind: "claudeAgent", displayName: "Fixture", access: "subscription", + snapshot: { state: "available", authenticated: true }, capabilities: { computerMcp: true, browserMcp: true }, + models: { default: "fixture", options: [{ id: "fixture", label: "Fixture" }] } } as InstanceInfo; + vi.stubGlobal("fetch", vi.fn(async () => new Response("{}", { headers: { "content-type": "application/json" } }))); + const host = document.createElement("div"); document.body.append(host); + const root = createRoot(host); + const render = () => { + const state = { ...initialState, bots: [bot], selectedId: bot.id, instances: [instance], + config: { box: { configured: false }, vps: { configured: false }, instabox: { configured: fixture.configured, fromEnv: false } } } as AppState; + flushSync(() => root.render(createElement(BotEditorStore, { value: { state, dispatch: vi.fn(), flushBotPatches: async () => null, + refreshInstances: async () => {}, refreshModels: async () => {} }, children: createElement(ComputerPanel, { bot }) }))); + }; + try { + render(); + await vi.waitFor(() => expect(fixture.reads).toBeGreaterThan(0)); + await vi.waitFor(() => expect(host.textContent).toContain("Add your InstaBox key in Settings first")); + const reads = fixture.reads; + fixture.configured = true; + render(); + await vi.waitFor(() => expect(fixture.reads).toBeGreaterThan(reads)); + await vi.waitFor(() => expect(host.textContent).not.toContain("Add your InstaBox key in Settings first")); + expect(host.textContent).not.toContain("Connect Boat"); + } finally { root.unmount(); host.remove(); vi.unstubAllGlobals(); } +}); diff --git a/src/components/ComputerPanel.tsx b/src/components/ComputerPanel.tsx index 0cfe0052..8de30b5e 100644 --- a/src/components/ComputerPanel.tsx +++ b/src/components/ComputerPanel.tsx @@ -1,3 +1,4 @@ +import { cloudPanelStatus } from "./instabox-panel"; import { InstaboxSnapshots } from "./InstaboxSnapshots"; import { boatCapableEngine, cloudEngineOf } from "@/lib/remote-desktop"; import { canWorkOnCloud } from "../../shared/cloud-computer"; @@ -537,8 +538,9 @@ export function ComputerPanel({ } // Opening the panel only looks: it never creates or wakes a Boat. api(threadPath("computer")) - .then((status) => { + .then((rawStatus) => { if (!alive) return; + const status = cloudPanelStatus(rawStatus); const action = resolveBoatPanelAction({ computer: livePlace === "cloud" ? "cloud" : undefined, configured: Boolean(status.configured), @@ -600,7 +602,7 @@ export function ComputerPanel({ engineReason, cloudSupported, providerLock, - state.config?.vps?.sshAlias, + state.config?.vps?.sshAlias, state.config?.instabox?.configured, panelView, computerSelectionPersisted, surfaceReady, @@ -619,7 +621,7 @@ export function ComputerPanel({ api(threadPath("computer"), { background: true }) .then((status) => { if (!alive) return; - const state = typeof status.box?.state === "string" ? status.box.state : null; + const state = cloudPanelStatus(status).box?.state ?? null; setBoatState(state); if (isReadyBoatState(state)) setPhase("ready"); }) @@ -634,7 +636,7 @@ export function ComputerPanel({ const control = state.computerControl[bot.id] ?? { held: false, helpReason: null }; const liveTarget = bot.computer === "vm" ? localVmTakeoverTarget(phase, vmStatus, computerStatusCurrent) - : cloudPreviewReady && cloudBackend === "vps" ? `vps/${bot.id}` : null; + : cloudPreviewReady && (cloudBackend === "vps" || cloudBackend === "instabox") ? `vps/${bot.id}` : null; const requestControl = useCallback(async (action: ComputerControlAction, controlLeaseId?: string) => { const snap = computerControlSnapshotSchema.parse(await api(threadPath("computer/control"), { method: "POST", @@ -654,9 +656,9 @@ export function ComputerPanel({ session: localVmTakeoverSession(viewerConnectionKey, panelView, bot.computer === "vm" ? lastVmScreenTarget.current.target : null), held: control.held, request: requestControl, - viewerClose: bot.computer === "cloud" && cloudBackend === "vps" ? threadPath("computer/viewer-close") : null, + viewerClose: bot.computer === "cloud" && (cloudBackend === "vps" || cloudBackend === "instabox") ? threadPath("computer/viewer-close") : null, }); - const vpsTakeoverTarget = bot.computer === "cloud" && cloudBackend === "vps" ? `vps/${bot.id}` : null; + const vpsTakeoverTarget = bot.computer === "cloud" && (cloudBackend === "vps" || cloudBackend === "instabox") ? `vps/${bot.id}` : null; const liveDesktopTarget = liveTarget ?? (takeover.here ? vpsTakeoverTarget : null); const liveHere = takeover.here && Boolean(liveDesktopTarget); useEffect(() => { if (liveTarget) void loadLiveDesktop(); }, [liveTarget]); @@ -950,7 +952,7 @@ export function ComputerPanel({ setPhase("starting"); api(`/api/bots/${bot.id}/computer/provision`, { method: "POST" }) .then((result) => { - setBoatState(result.state ?? null); + setBoatState(result.machine?.state ?? result.state ?? null); setResolvedComputerSelection({ botId: bot.id, threadId: bot.threadId, computer: bot.computer, cloudBackend }); setPhase("ready"); }) @@ -1091,7 +1093,7 @@ export function ComputerPanel({ const managedPolicy = state.config?.managedPolicy; const busyBoat = busyBoatView(boatState, profileBot.busy === true); - const cloudComputer: PlaceFacts["computer"] = !computerStatusCurrent || livePlace !== "cloud" || cloudBackend !== "box" ? undefined + const cloudComputer: PlaceFacts["computer"] = !computerStatusCurrent || livePlace !== "cloud" || cloudBackend === "vps" ? undefined : phase === "starting" ? "starting" : phase === "busy-boat" ? (busyBoat.spinner ? "starting" : busyBoat.line === "computer.cloud.asleep" ? "asleep" : "none") : phase === "ready" ? "on" @@ -1502,9 +1504,9 @@ export function ComputerPanel({ {errorText} )} - {!providerLock && (boatKeyShown || (phase === "unconfigured" && cloudView.action?.id === "add-boat-key")) && ( + {!providerLock && (boatKeyShown || (phase === "unconfigured" && (cloudView.action?.id === "add-boat-key" || cloudBackend === "instabox"))) && (
- { setBoatKeyShown(true); if (configured) setRetry((n) => n + 1); }} />
@@ -1650,7 +1652,7 @@ export function ComputerPanel({ )} )} - {!providerLock && canManageCloud && cloudBackend !== "vps" && phase !== "unconfigured" && phase !== "team-boat" && teamComputer?.botId !== bot.id && } + {!providerLock && canManageCloud && cloudBackend === "box" && phase !== "unconfigured" && phase !== "team-boat" && teamComputer?.botId !== bot.id && } {phase !== "team-boat" && (bot.computer !== undefined || computerStatusCurrent) && <> diff --git a/src/components/InstaboxSnapshots.test.tsx b/src/components/InstaboxSnapshots.test.tsx index ffcb9a9a..f2583097 100644 --- a/src/components/InstaboxSnapshots.test.tsx +++ b/src/components/InstaboxSnapshots.test.tsx @@ -34,7 +34,7 @@ describe("InstaBox snapshots", () => { expect(host.textContent).toContain("No snapshots yet"); fixture.api.mockResolvedValueOnce({ snapshot }).mockResolvedValueOnce({ snapshots: [snapshot] }); await click("Save a snapshot"); - expect(fixture.api).toHaveBeenCalledWith("/api/bots/bot-one/instabox/snapshots", { method: "POST", body: "{}" }); + expect(fixture.api).toHaveBeenCalledWith("/api/bots/bot-one/instabox/snapshots", { timeoutMs: 720_000, method: "POST", body: "{}" }); expect(host.textContent).toContain(snapshot.name); expect(host.textContent).not.toContain("No snapshots yet"); }); @@ -50,7 +50,7 @@ describe("InstaBox snapshots", () => { expect(fixture.api).toHaveBeenCalledTimes(1); await click(label); await click(label, document.querySelector('[role="alertdialog"]')!); - expect(fixture.api).toHaveBeenCalledWith(`/api/bots/bot-one/instabox/snapshots/snapshot-one${label === "Delete" ? "" : "/restore"}`, { method: label === "Delete" ? "DELETE" : "POST" }); + expect(fixture.api).toHaveBeenCalledWith(`/api/bots/bot-one/instabox/snapshots/snapshot-one${label === "Delete" ? "" : "/restore"}`, { timeoutMs: 720_000, method: label === "Delete" ? "DELETE" : "POST" }); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); expect(fixture.api).toHaveBeenCalledTimes(3); }); diff --git a/src/components/InstaboxSnapshots.tsx b/src/components/InstaboxSnapshots.tsx index 7ec19892..5e551436 100644 --- a/src/components/InstaboxSnapshots.tsx +++ b/src/components/InstaboxSnapshots.tsx @@ -48,7 +48,7 @@ function SnapshotPanel({ botId }: { botId: string }) { setError(null); try { const path = chosen ? `${base}/${encodeURIComponent(chosen.snapshot.id)}${chosen.kind === "restore" ? "/restore" : ""}` : base; - await api(path, { method: chosen?.kind === "delete" ? "DELETE" : "POST", ...(!chosen ? { body: "{}" } : {}) }); + await api(path, { timeoutMs: 720_000, method: chosen?.kind === "delete" ? "DELETE" : "POST", ...(!chosen ? { body: "{}" } : {}) }); const data = await api<{ snapshots: Snapshot[] }>(base); if (mounted.current) { setSnapshots(data.snapshots); setAction(null); } } catch (cause) { diff --git a/src/components/instabox-panel.test.ts b/src/components/instabox-panel.test.ts new file mode 100644 index 00000000..b3e98391 --- /dev/null +++ b/src/components/instabox-panel.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, it } from "vitest"; +import { cloudPanelStatus } from "./instabox-panel"; +import { resolveBoatPanelAction } from "@/lib/local-computer"; +import { placeFacts, placeViewFor } from "@/lib/place-view"; +import type { Bot, InstanceInfo } from "@/state/store"; + +describe("InstaBox computer panel", () => { + it.each([ + ["running", true, "attach-ready-boat"], + ["suspended", false, "cloud-asleep"], + ["stopped", false, "cloud-asleep"], + [undefined, false, "cloud-new"], + ] as const)("maps %s to the existing cloud controls", (state, ready, expected) => { + const status = cloudPanelStatus({ backend: "instabox", configured: true, ready, machine: state ? { state } : null }); + expect(resolveBoatPanelAction({ computer: "cloud", configured: true, boatState: status.box!.state, canUseCloud: true, autoLocal: false, teamComputer: false, busy: false })).toBe(expected); + }); + + it("uses the InstaBox key and a settings hint independently of Boat or the Cloud plan", () => { + const bot = { name: "Fixture", cloudBackend: "instabox", modelSelection: { instanceId: "fixture", model: "fixture" } } as Bot; + const instances = [{ instanceId: "fixture", capabilities: { computerMcp: true }, snapshot: { authenticated: true, state: "available" } }] as InstanceInfo[]; + const input = { bot, instances, place: "cloud" as const, seat: { server: "linux" as const, plan: true, role: "admin" as const, mode: "simple" as const } }; + const missing = placeViewFor(placeFacts({ ...input, config: { box: { configured: true }, instabox: { configured: false, fromEnv: false } } })); + expect(missing.line).toContain("Add your InstaBox key in Settings first"); + expect(missing.action).toBeNull(); + const ready = placeViewFor(placeFacts({ ...input, computer: "on", config: { instabox: { configured: true, fromEnv: false }, box: { configured: false } } })); + expect(ready.state).toBe("cc-on"); + expect(ready.line).not.toContain("Boat"); + }); +}); diff --git a/src/components/instabox-panel.ts b/src/components/instabox-panel.ts new file mode 100644 index 00000000..90faf718 --- /dev/null +++ b/src/components/instabox-panel.ts @@ -0,0 +1,12 @@ +interface InstaboxPanelStatus { + backend?: string; + machine?: { state: string } | null; + configured?: boolean; + ready?: boolean; +} + +export function cloudPanelStatus(status: T): T & { box?: { state: string | null } } { + if (status.backend !== "instabox") return status; + const state = status.machine?.state; + return { ...status, box: { state: status.ready ? "ready" : state === "suspended" || state === "stopped" ? "archived" : state ?? null } }; +} diff --git a/src/lib/place-view.ts b/src/lib/place-view.ts index f764fa67..7a1a9fc3 100644 --- a/src/lib/place-view.ts +++ b/src/lib/place-view.ts @@ -57,7 +57,7 @@ export function placeFacts(input: { bot: Pick; place: PlaceFacts["place"]; seat: PlaceSeat; - config: (FeatureFlagConfig & Pick) | null | undefined; + config: (FeatureFlagConfig & Pick) | null | undefined; instances: InstanceInfo[]; local?: PlaceFacts["local"]; computer?: PlaceFacts["computer"]; @@ -70,9 +70,9 @@ export function placeFacts(input: { providerLock: config?.providerLock, server: seat.server, bot: bot.name, - plan: seat.plan, - boat: config?.box?.configured ? (config.box.included ? "included" : "own-key") : "none", - backend: bot.cloudBackend === "vps" ? "vps" : "box", + plan: bot.cloudBackend === "instabox" ? false : seat.plan, + boat: bot.cloudBackend === "instabox" ? (config?.instabox?.configured ? "own-key" : "none") : config?.box?.configured ? (config.box.included ? "included" : "own-key") : "none", + backend: bot.cloudBackend ?? "box", engine: engineFacts(instance, bot.modelSelection.model), toolsAllowComputer: canUseMcpServer(bot.toolScope, "computer"), browserOn: builtInBrowserEnabled(config) && browserAvailable(config), @@ -85,7 +85,9 @@ export function placeFacts(input: { } export function placeViewFor(facts: PlaceFacts): PlaceView { - return placeView(facts, { translate: placeTranslate, locale: activeLocale() }); + const view = placeView(facts, { translate: placeTranslate, locale: activeLocale() }); + return facts.backend === "instabox" && view.state === "cc-needs-key" + ? { ...view, short: t("computer.instabox.keyMissing"), line: t("botAccess.cloud.instaboxKeyHint"), action: null } : view; } export function placeRowViewFor(row: PlaceRow, seat: PlaceSeat, worksOnLabel?: string): PlaceView { diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json index ba9a76cb..02ee441d 100644 --- a/src/locales/pt-br.json +++ b/src/locales/pt-br.json @@ -10625,5 +10625,5 @@ "computer.instabox.snapshots.deleteConfirm": "Este instantâneo não poderá ser recuperado depois da exclusão. Excluir?", "computer.instabox.snapshots.busy": "Processando instantâneos, aguarde", "computer.instabox.snapshots.error": "Não foi possível processar o instantâneo. Tente novamente.", - "computer.instabox.snapshots.machineMissing": "Inicie primeiro o computador em nuvem InstaBox deste agente" + "computer.instabox.snapshots.machineMissing": "Inicie primeiro o computador em nuvem InstaBox deste bot" }