Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/tasks/T-1200/T-1264-map-instabox-frames/brief.md
Original file line number Diff line number Diff line change
@@ -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 .`(或仓库现有类型检查命令)通过;只跑改动相关的测试文件。
5 changes: 2 additions & 3 deletions server/cloud-backend.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
24 changes: 24 additions & 0 deletions server/routes/computer-frames.e2e.test.ts
Original file line number Diff line number Diff line change
@@ -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();
}
});
20 changes: 16 additions & 4 deletions server/routes/computer-frames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof mapComputers>[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" },
Expand All @@ -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" };
Expand Down
3 changes: 2 additions & 1 deletion server/routes/computer-frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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],
Expand Down
16 changes: 16 additions & 0 deletions shared/cloud-backend.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
5 changes: 5 additions & 0 deletions shared/cloud-backend.ts
Original file line number Diff line number Diff line change
@@ -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";
}
16 changes: 16 additions & 0 deletions src/components/TeamMapPage.edges.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});
3 changes: 2 additions & 1 deletion src/components/TeamMapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<string[]>([]);
Expand Down
Loading