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
24 changes: 24 additions & 0 deletions docs/tasks/T-1200/T-1271-lease-ttl/brief.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## 运行环境(云沙箱,必读)
- 开工先在 worktree 根目录运行 依赖准备脚本(镜像自带,把预装依赖链进 node_modules),之后 `pnpm exec vitest` / `pnpm exec tsc` 才可用。
- 这里**没有** systemd、Docker,说明里出现的 `systemd-run --scope ... --` 前缀一律去掉直接运行;机器 8G/4 核,一次只跑一个测试文件。
- 若本轮沙箱已放开(能联网、能监听端口),交回前必须把改动相关的测试全部跑过,包括需要端口的 e2e;涉及的 e2e 连跑 3 次都要过。跑不了的写明原因。
- 报告只写三样:改了什么(文件列表与 git status 一致)、为什么、测试命令与结果摘要。不要写长篇分析;完整输出放 verification 文本文件即可。
- 不提交 git。

# T-1271 活动租约时长超过接口上限,PUT 返回 400

## 背景
T-1269 已把活动租约改成 `PUT /v1/machines/{id}/activity-leases/{leaseID}`,body `{ ttlSeconds }`。线上实测这个 PUT 返回 400:接口文档写明 ttlSeconds 范围是 30–3600 秒,而 server/instabox/computer-tools.ts 发的是 `BACKGROUND_MAX_MS / 1000`,等于 7200。

## 要改
1. 租约的 ttlSeconds 单独定一个常量,取 3600,即接口上限。不要改 BACKGROUND_MAX_MS 本身:后台任务的总时长上限仍是 2 小时,由现有的 expiry 定时器负责。
2. 续期间隔现在是 30 分钟,小于 3600 秒,保持不变。确认在 2 小时内,租约会一直被续上,不会中途过期。
3. 测试:
- 更新 computer-tools.e2e.test.ts 和假接口里断言 ttlSeconds 的地方。
- 假接口对 ttlSeconds 超过 3600 的请求返回 400,和真接口一致。
- 加一条断言:首次 PUT 和续期 PUT 的 ttlSeconds 都等于 3600。

## 验收
- server/instabox 下全部测试通过;`pnpm exec tsc -p tsconfig.server.json` 通过;改动的文件过 `oxlint --deny-warnings`。
- report.md 和 verification.txt 放在本任务文件夹。verification.txt 只写命令和汇总结果。
- 不写 IP、地址、域名、uuid、任何密钥。文件名不要用反引号单独包住带 md 后缀的写法。
22 changes: 22 additions & 0 deletions docs/tasks/T-1200/T-1271-lease-ttl/report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
改了什么:

- docs/tasks/T-1200/T-1271-lease-ttl/report.md
- docs/tasks/T-1200/T-1271-lease-ttl/verification.txt
- server/instabox/computer-tools.e2e.test.ts
- server/instabox/computer-tools.ts
- server/testing/fake-instabox-tools.ts

为什么:

活动租约使用接口允许的最大时长 3600 秒,后台任务总时长仍由 2 小时的过期定时器控制。假接口现在拒绝超出 30–3600 秒范围的 TTL;测试覆盖首次 PUT、30 分钟续期 PUT 和超上限请求。

测试命令与结果摘要:

- 依赖准备脚本:通过。
- server/instabox 下 8 个非 e2e 测试文件:逐文件通过,共 68 项。
- server/instabox 下 8 个 e2e 测试文件:逐文件连续三轮通过,共 339 项次;VNC 使用显式 Chromium 路径,三轮均 32 项通过。
- pnpm exec tsc -p tsconfig.server.json:通过。
- pnpm exec oxlint --deny-warnings server/instabox/computer-tools.ts server/instabox/computer-tools.e2e.test.ts server/testing/fake-instabox-tools.ts:通过,0 警告。
- git diff --check:通过。

未提交 git。
20 changes: 20 additions & 0 deletions docs/tasks/T-1200/T-1271-lease-ttl/verification.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
crybot-link-deps
通过。

TMPDIR=/tmp pnpm exec vitest run server/instabox/<非 e2e 测试文件> --maxWorkers=1
8 个文件逐文件通过,共 68 项。

TMPDIR=/tmp pnpm exec vitest run server/instabox/<e2e 测试文件> --maxWorkers=1
8 个 e2e 文件逐文件连续三轮通过,共 339 项次;VNC 三轮均 32 项通过。

TMPDIR=/tmp CRYBOT_CDP_CHROME=/usr/bin/chromium pnpm exec vitest run server/instabox/vnc.e2e.test.ts --maxWorkers=1
连续三轮通过,每轮 32 项。

pnpm exec tsc -p tsconfig.server.json
通过。

pnpm exec oxlint --deny-warnings server/instabox/computer-tools.ts server/instabox/computer-tools.e2e.test.ts server/testing/fake-instabox-tools.ts
通过,0 警告。

git diff --check
通过。
25 changes: 23 additions & 2 deletions server/instabox/computer-tools.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,18 @@ describe("InstaBox tools through an isolated HTTP machine fixture", () => {
const started = json(await runInstaboxComputerTool(fixture.machines, machineId, "exec", { command: "true", background: true }, signal(), folder, fixture.request));
const path = fixture.leaseRequests[0].path;
expect(path).toMatch(/^\/v1\/machines\/fixture-machine\/activity-leases\/crybot-[0-9a-f-]{36}$/);
expect(fixture.leaseRequests[0]).toMatchObject({ body: { ttlSeconds: 7200 }, idempotencyKey: undefined });
expect(fixture.leaseRequests[0]).toMatchObject({ body: { ttlSeconds: 3600 }, idempotencyKey: undefined });
await runInstaboxComputerTool(fixture.machines, machineId, "exec_status", { process_id: started.processId, wait_seconds: 3 }, signal(), folder, fixture.request);
expect(fixture.leaseRequests.map(request => `${request.method} ${request.path}`)).toEqual([
`PUT ${path}`,
`DELETE ${path}`,
]);
});

it("rejects activity lease TTLs above the API limit", async () => {
await expect(fixture.request("PUT", `/v1/machines/${machineId}/activity-leases/fixture-lease`, { body: { ttlSeconds: 3601 } })).rejects.toMatchObject({ status: 400 });
});

it("keeps the lease while a background job is still running", async () => {
const started = json(await runInstaboxComputerTool(fixture.machines, machineId, "exec", { command: "sleep 0.5", background: true }, signal(), folder, fixture.request));
const path = fixture.leaseRequests[0].path;
Expand All @@ -303,13 +307,30 @@ describe("InstaBox tools through an isolated HTTP machine fixture", () => {
]);
});

it("renews the lease with the maximum API TTL", async () => {
vi.useFakeTimers({ toFake: ["setInterval", "clearInterval"] });
try {
const started = json(await runInstaboxComputerTool(fixture.machines, machineId, "exec", { command: "sleep 1", background: true }, signal(), folder, fixture.request));
const path = fixture.leaseRequests[0].path;
await vi.advanceTimersByTimeAsync(30 * 60_000);
await expect.poll(() => fixture.leaseRequests.length).toBe(2);
expect(fixture.leaseRequests).toEqual([
{ method: "PUT", path, body: { ttlSeconds: 3600 }, idempotencyKey: undefined },
{ method: "PUT", path, body: { ttlSeconds: 3600 }, idempotencyKey: undefined },
]);
await runInstaboxComputerTool(fixture.machines, machineId, "exec_status", { process_id: started.processId, wait_seconds: 3 }, signal(), folder, fixture.request);
} finally {
vi.useRealTimers();
}
});

it.each([false, true])("shares one lease across five background commands (concurrent %s)", async concurrent => {
const start = () => runInstaboxComputerTool(fixture.machines, machineId, "exec", { command: "true", background: true }, signal(), folder, fixture.request);
const started: CloudComputerResult[] = [];
if (concurrent) started.push(...await Promise.all(Array.from({ length: 5 }, start)));
else for (let index = 0; index < 5; index++) started.push(await start());
const path = fixture.leaseRequests[0].path;
expect(fixture.leaseRequests).toEqual(Array.from({ length: 5 }, () => ({ method: "PUT", path, body: { ttlSeconds: 7200 }, idempotencyKey: undefined })));
expect(fixture.leaseRequests).toEqual(Array.from({ length: 5 }, () => ({ method: "PUT", path, body: { ttlSeconds: 3600 }, idempotencyKey: undefined })));
for (const [index, result] of started.entries()) {
expect(fixture.leaseRequests.filter(request => request.method === "DELETE")).toHaveLength(0);
const status = json(await call("exec_status", { process_id: json(result).processId, wait_seconds: 3 }));
Expand Down
3 changes: 2 additions & 1 deletion server/instabox/computer-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const INSTABOX_COPY_MAX_BYTES = 256 * MiB;
const INSTABOX_SCREENSHOT_MAX_BYTES = 16 * MiB;
let activeCopies = 0;
const BACKGROUND_MAX_MS = 2 * 60 * 60_000;
const ACTIVITY_LEASE_TTL_SECONDS = 3600;
const resultText = (result: InstaboxExecResult): CloudComputerResult => text(JSON.stringify({
ok: result.exitCode === 0 && !result.timedOut, ...result,
}), result.exitCode !== 0 || result.timedOut);
Expand Down Expand Up @@ -132,7 +133,7 @@ const activityLeases = new Map<string, ActivityLease>();
const activityLeasePath = (lease: ActivityLease) => `/v1/machines/${lease.machineId}/activity-leases/${encodeURIComponent(lease.id)}`;

async function putActivityLease(lease: ActivityLease): Promise<void> {
const response = await lease.request("PUT", activityLeasePath(lease), { body: { ttlSeconds: BACKGROUND_MAX_MS / 1000 } });
const response = await lease.request("PUT", activityLeasePath(lease), { body: { ttlSeconds: ACTIVITY_LEASE_TTL_SECONDS } });
await response.body?.cancel();
if (!response.ok) throw new Error("Activity lease rejected");
}
Expand Down
2 changes: 1 addition & 1 deletion server/testing/fake-instabox-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function fakeInstaboxTools(options: Options = {}) {
leaseRequests.push({ method: req.method!, path: url.pathname, body, idempotencyKey: req.headers["idempotency-key"] });
if (!parts[5] || parts.length !== 6 || !["PUT", "DELETE"].includes(req.method!)) { res.writeHead(404); res.end(); return; }
if (req.method === "PUT") {
if (!body || Object.keys(body).length !== 1 || !Number.isInteger(body.ttlSeconds) || body.ttlSeconds <= 0) { res.writeHead(400); res.end(); return; }
if (!body || Object.keys(body).length !== 1 || !Number.isInteger(body.ttlSeconds) || body.ttlSeconds < 30 || body.ttlSeconds > 3600) { res.writeHead(400); res.end(); return; }
state.suspended = false;
}
res.writeHead(204); res.end(); return;
Expand Down
Loading