diff --git a/docs/tasks/T-1200/T-1264-map-instabox-frames/brief.md b/docs/tasks/T-1200/T-1264-map-instabox-frames/brief.md new file mode 100644 index 00000000..b024a808 --- /dev/null +++ b/docs/tasks/T-1200/T-1264-map-instabox-frames/brief.md @@ -0,0 +1,27 @@ +## 运行环境(云沙箱,必读) +- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec tsc` 才可用。 +- 这里没有 systemd、Docker,`systemd-run --scope ... --` 前缀一律去掉直接运行;一次只跑一个测试文件。 +- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。 +- 不提交 git。 +- 本仓库是公开仓库:代码、注释、报告里不得出现内部域名、IP、邮箱、人名、真实钥匙、沙箱主目录绝对路径。 + +# T-1264:团队地图把 智能体算成地图电脑,但画面接口只查 Boat,出现空电脑卡 + +## 背景 +以 T-1252 合并后的 main 为准(T-1252 把云电脑缺省后端改为 ,并改了 `src/components/TeamMapPage.tsx`)。 +T-1252 第 5 轮审核指出两处不一致: +- 前端 `src/components/TeamMapPage.tsx` 约 290 行 `anyComputer`:只排除了 `box`/`vps`,于是用 (含 cloudBackend 缺省、现在缺省即 )的智能体会让地图认为「有电脑」,打开电脑显示。 +- 服务端 `server/routes/computer-frames.ts` 的 `mapComputers`(约 33 行)把 `cloudBackend !== "vps"` 且 `computer === "cloud"` 的智能体都列成地图电脑;而画面来源 `createComputerFrames` 的 `listMachines`/`capture` 只查 Boat。结果 智能体在地图上是一张永远没画面的空电脑卡。 + +## 要改的 +目标:地图上列出的电脑必须都有画面来源;没有画面来源的后端不列。 +1. 先读懂当前(T-1252 后)`cloudKindOf` / `cloudPolicyKindOf`(`server/cloud-backend.ts`)里「智能体实际用哪个后端」的判定,前后端都用同一套判定,不要各写一份字符串比较。前端如需同样判定,放在 shared/ 里的纯函数中复用。 +2. 服务端 `mapComputers`:只列实际后端是 Boat 的智能体自有电脑;团队共用电脑(kind: "team")保持现状不动。 +3. 前端 `anyComputer`:与服务端同口径,只在存在 Boat 智能体(或已有团队共用电脑,若 T-1252 后仍有此概念)时为真。 +4. 不要给 加画面轮询(会把挂起的沙箱唤醒、产生费用),本任务只做排除。 +5. 补测试:`server/routes/computer-frames` 相关单测里加用例——(显式与缺省两种)智能体不出现在 `mapComputers` 结果里,Boat 智能体照常出现;前端判定若抽成 shared 纯函数,给它补单测。 + +## 验收 +- 智能体(cloudBackend 缺省或显式 )不再出现在 `/api/computers/frames` 的 computers 里,也不会单独让地图打开电脑显示。 +- Boat 智能体与团队共用电脑行为不变,原有相关测试全部通过。 +- `pnpm exec tsc -p .`(或仓库现有类型检查命令)通过;只跑改动相关的测试文件。 diff --git a/server/cloud-backend.ts b/server/cloud-backend.ts index 43a31558..05030f03 100644 --- a/server/cloud-backend.ts +++ b/server/cloud-backend.ts @@ -1,8 +1,7 @@ import type { CloudBackend } from "../shared/wire.ts"; +import { cloudKindOf } from "../shared/cloud-backend.ts"; -export function cloudKindOf(bot: { cloudBackend?: CloudBackend }, teamComputer = false): CloudBackend { - return teamComputer ? "box" : bot.cloudBackend ?? "instabox"; -} +export { cloudKindOf }; export const CLOUD_BACKEND_CHANGE_ERROR = "stop the active turn before changing the cloud backend"; export const BOAT_ACCOUNT_RESOURCES_ERROR = diff --git a/server/routes/computer-frames.e2e.test.ts b/server/routes/computer-frames.e2e.test.ts new file mode 100644 index 00000000..e4cac983 --- /dev/null +++ b/server/routes/computer-frames.e2e.test.ts @@ -0,0 +1,24 @@ +import { expect, it } from "vitest"; +import { launchVerificationServer } from "../../scripts/control-cry.ts"; +import { fixtureApi } from "../../scripts/testing/preview-fixture.ts"; +import type { MapComputersPayload } from "../../shared/map-computer.ts"; + +it("lists only Boat bots through the isolated frames endpoint", async () => { + const fixture = await launchVerificationServer({}); + try { + const api = fixtureApi(fixture.info.url, { headers: { origin: fixture.info.url } }); + let boatBotId: string | undefined; + for (const cloudBackend of [undefined, "instabox", "box"] as const) { + const { bot } = await api("POST", "/api/bots", { + name: `Map fixture ${cloudBackend ?? "default"}`, + settings: { computer: "cloud", ...(cloudBackend ? { cloudBackend } : {}) }, + }); + expect(bot.cloudBackend).toBe(cloudBackend ?? "instabox"); + if (cloudBackend === "box") boatBotId = bot.id; + const payload: MapComputersPayload = await api("GET", "/api/computers/frames"); + expect(payload.computers.map((computer) => computer.id)).toEqual(boatBotId ? [boatBotId] : []); + } + } finally { + await fixture.close(); + } +}); diff --git a/server/routes/computer-frames.test.ts b/server/routes/computer-frames.test.ts index 3e79d83d..6525bde3 100644 --- a/server/routes/computer-frames.test.ts +++ b/server/routes/computer-frames.test.ts @@ -5,14 +5,16 @@ import { PASS } from "./table.ts"; afterEach(() => vi.unstubAllEnvs()); -const bots = [ - { id: "ada", name: "Ada", section: "Research", computer: "cloud" as const }, +const bots: Parameters[0]["bots"] = [ + { id: "ada", name: "Ada", section: "Research", computer: "cloud" as const, cloudBackend: "box" as const }, { id: "ben", name: "Ben", section: "Research" }, { id: "cy", name: "Cy", section: "Design" }, { id: "dee", name: "Dee", section: "Design", computer: "cloud" as const, cloudBackend: "vps" as const }, - { id: "eve", name: "Eve", section: "Design", computer: "cloud" as const, hidden: true }, + { id: "eve", name: "Eve", section: "Design", computer: "cloud" as const, cloudBackend: "box" as const, hidden: true }, { id: "fay", name: "Fay", section: "Ops" }, - { id: "gus", name: "Gus", section: "Ops", computer: "off" as const }, + { id: "gus", name: "Gus", section: "Ops", computer: "off" as const, cloudBackend: "box" as const }, + { id: "instabox-explicit", name: "Explicit InstaBox", section: "Research", computer: "cloud", cloudBackend: "instabox" }, + { id: "instabox-default", name: "Default InstaBox", section: "Research", computer: "cloud" }, ]; const teamComputers = [ { id: "11111111-1111-4111-8111-111111111111", name: "Design desktop", section: "Design" }, @@ -30,6 +32,16 @@ describe("map computers", () => { ]); }); + it.each([undefined, "instabox", "vps"] as const)("excludes %s own computers even with an existing Boat machine", (cloudBackend) => { + const ownBots = (["cloud", "off", undefined] as const).map((computer, index) => ({ + id: `bot-${index}`, name: `Bot ${index}`, computer, cloudBackend, + })); + expect(mapComputers({ + bots: ownBots, teamComputers: [], + machine: (owner) => ({ owner, boxId: "fixture-box", state: "running" }), working: () => false, + })).toEqual([]); + }); + it("reads the have list and sends a turn frame only to a caller without any frame", () => { expect([...parseHave("ada:map-5,computer_x:turn-9,bad")]).toEqual([["ada", "map-5"], ["computer_x", "turn-9"]]); const turn = { data: "big", mime: "image/jpeg", at: 9, source: "turn" as const, version: "turn-9" }; diff --git a/server/routes/computer-frames.ts b/server/routes/computer-frames.ts index 595a0dc8..39256731 100644 --- a/server/routes/computer-frames.ts +++ b/server/routes/computer-frames.ts @@ -3,6 +3,7 @@ import * as boat from "../boat.ts"; import { boatHealth } from "../boat-health.ts"; import type { ComputerControl } from "../computer-control.ts"; import type { MapComputer, MapComputersPayload } from "../../shared/map-computer.ts"; +import { cloudKindOf } from "../../shared/cloud-backend.ts"; import { createComputerFrames, mapComputerState, type ComputerFrames, type FrameMachine } from "../computer-frames.ts"; import type { AppConfig } from "../config.ts"; import { latestTurnFrame } from "../screen-frame-source.ts"; @@ -41,7 +42,7 @@ export function mapComputers(input: { const sharedBy = (computer: (typeof teams)[number]) => bots.filter((bot) => bot.computer === undefined && bot.cloudBackend !== "vps" && sectionKey(bot.section) === computer.section); const usesTeam = new Set(teams.flatMap((computer) => sharedBy(computer).map((bot) => bot.id))); - const own = bots.filter((bot) => bot.cloudBackend !== "vps" && (bot.computer === "cloud" || (!usesTeam.has(bot.id) && input.machine(bot.id) !== undefined))); + const own = bots.filter((bot) => cloudKindOf(bot) === "box" && (bot.computer === "cloud" || (!usesTeam.has(bot.id) && input.machine(bot.id) !== undefined))); return [ ...own.map((bot) => ({ id: bot.id, kind: "bot" as const, name: bot.name, botIds: [bot.id], diff --git a/shared/cloud-backend.test.ts b/shared/cloud-backend.test.ts new file mode 100644 index 00000000..5f9895ab --- /dev/null +++ b/shared/cloud-backend.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { cloudKindOf } from "./cloud-backend.ts"; + +describe("actual cloud backend", () => { + it("defaults an omitted backend to InstaBox", () => { + expect(cloudKindOf({})).toBe("instabox"); + }); + + it.each(["box", "instabox", "vps"] as const)("preserves the explicit %s backend", (cloudBackend) => { + expect(cloudKindOf({ cloudBackend })).toBe(cloudBackend); + }); + + it.each([undefined, "box", "instabox", "vps"] as const)("keeps team computers on Boat for %s bots", (cloudBackend) => { + expect(cloudKindOf({ cloudBackend }, true)).toBe("box"); + }); +}); diff --git a/shared/cloud-backend.ts b/shared/cloud-backend.ts new file mode 100644 index 00000000..71fb6b5a --- /dev/null +++ b/shared/cloud-backend.ts @@ -0,0 +1,5 @@ +import type { CloudBackend } from "./wire.ts"; + +export function cloudKindOf(bot: { cloudBackend?: CloudBackend }, teamComputer = false): CloudBackend { + return teamComputer ? "box" : bot.cloudBackend ?? "instabox"; +} diff --git a/src/components/TeamMapPage.edges.test.ts b/src/components/TeamMapPage.edges.test.ts index 9fa4bc59..6d354297 100644 --- a/src/components/TeamMapPage.edges.test.ts +++ b/src/components/TeamMapPage.edges.test.ts @@ -61,3 +61,19 @@ it("hides retired team cloud management while keeping Local VM setup", () => { expect(markup).not.toContain("InstaBox"); expect(markup).toContain(t("canvas.localVm")); }); + +it.each([ + { cloudBackend: undefined, computer: "cloud", expected: false }, + { cloudBackend: "instabox", computer: "cloud", expected: false }, + { cloudBackend: "vps", computer: "cloud", expected: false }, + { cloudBackend: "box", computer: "cloud", expected: true }, + { cloudBackend: "box", computer: "off", expected: false }, + { cloudBackend: "box", computer: undefined, expected: false }, + { cloudBackend: "box", computer: "cloud", hidden: true, expected: false }, +] as const)("defaults computer display to $expected for $cloudBackend / $computer / hidden=$hidden", ({ expected, ...settings }) => { + fixture.bots = [bot("map-fixture", settings)]; + const container = document.createElement("div"); + container.innerHTML = renderToStaticMarkup(createElement(TeamMapPage)); + const toggle = [...container.querySelectorAll("button")].find((button) => button.title === t("canvas.showComputersHint")); + expect(toggle?.getAttribute("aria-pressed")).toBe(String(expected)); +}); diff --git a/src/components/TeamMapPage.tsx b/src/components/TeamMapPage.tsx index 139db7eb..e505e667 100644 --- a/src/components/TeamMapPage.tsx +++ b/src/components/TeamMapPage.tsx @@ -24,6 +24,7 @@ import { useMapComputers, useShowComputers } from "@/lib/map-computers"; import { MapComposer } from "./MapComposer"; import { useOwnerOrAdmin } from "@/lib/use-owner-or-admin"; import { openBotSettings } from "./bot-panel/panel-link"; +import { cloudKindOf } from "../../shared/cloud-backend"; export function EdgeRow({ edge, bots }: { edge: TeamMapEdge; bots: Bot[] }) { const { state, dispatch } = useStore(); @@ -287,7 +288,7 @@ export function TeamMapPage() { return buildTeamMapSections(bots, names).sort((a, b) => order(a.key) - order(b.key)); }, [bots, state.sections, state.groups]); const edges = useMemo(() => buildTeamMapEdges(bots, snapshot), [bots, snapshot]); - const anyComputer = bots.some((bot) => bot.computer === "cloud" && bot.cloudBackend !== "box" && bot.cloudBackend !== "vps"); + const anyComputer = bots.some((bot) => bot.computer === "cloud" && cloudKindOf(bot) === "box"); const { show: showComputers, loaded: computersChoiceLoaded, choose: setShowComputers } = useShowComputers(anyComputer); const mapComputers = useMapComputers(!state.config?.providerLock && !remoteClient && computersChoiceLoaded && showComputers); const [taggedBotIds, setTaggedBotIds] = useState([]);