Public: anyone can read this; the crew can change it.
The game agent runs compose under its time cap on Linux: timeout(1) cannot exec a shell function #17206
Merged 45h 24m ago, live in production
sold/agent-capped → main 0 commits
2 files
changed +13
−4
src/games/agent.sh +2 −1
| @@ -82,7 +82,8 @@ now() { date -u +%Y-%m-%dT%H:%M:%SZ; } | ||
| 82 | 82 | capped() { |
| 83 | 83 | local secs="$1" pid i |
| 84 | 84 | shift |
| 85 | if command -v timeout >/dev/null 2>&1; then | |
| 85 | # timeout(1) runs a program, never a shell function (`compose` is one): a function takes the loop below. | |
| 86 | if ! declare -F "$1" >/dev/null && command -v timeout >/dev/null 2>&1; then | |
| 86 | 87 | timeout "" "$@" |
| 87 | 88 | return |
| 88 | 89 | fi |
test/game-box.test.ts +11 −3
| @@ -8,7 +8,7 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | import { yamlString } from "../src/cloud/boot_ascii.ts"; |
| 10 | 10 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 11 | import { chmodSync, existsSync, mkdtempSync, readFileSync, statSync, writeFileSync } from "node:fs"; | |
| 11 | import { chmodSync, existsSync, mkdtempSync, readFileSync, statSync, writeFileSync, mkdirSync } from "node:fs"; | |
| 12 | 12 | import { rm } from "node:fs/promises"; |
| 13 | 13 | import { tmpdir } from "node:os"; |
| 14 | 14 | import { join } from "node:path"; |
| @@ -166,8 +166,16 @@ describe("the agent", () => { | ||
| 166 | 166 | |
| 167 | 167 | // The agent's own tools, which the box installs; a machine without them cannot run it. |
| 168 | 168 | const lacks = ["bash", "curl", "jq"].filter((t) => Bun.which(t) === null); |
| 169 | test.skipIf(lacks.length > 0)(`one sync against a stand-in forge with a fake docker: reports, deploys, runs a command, and reports its result${lacks.length > 0 ? ` (skipped: no ${lacks.join(", ")} here)` : ""}`, async () => { | |
| 169 | // Twice: as a Mac runs it (no timeout(1)) and as the box does (timeout(1) on PATH, which runs | |
| 170 | // programs only: the agent's `compose` function must still be reached). | |
| 171 | for (const withTimeout of [false, true]) | |
| 172 | test.skipIf(lacks.length > 0)(`one sync against a stand-in forge with a fake docker${withTimeout ? ", timeout(1) on PATH" : ""}: reports, deploys, runs a command, and reports its result${lacks.length > 0 ? ` (skipped: no ${lacks.join(", ")} here)` : ""}`, async () => { | |
| 170 | 173 | const box = mkdtempSync(join(tmpdir(), "forge-game-agent-")); |
| 174 | // A timeout(1) as coreutils' behaves for this: it execs its program, so a shell function is "not found". | |
| 175 | const bin = join(box, "bin"); | |
| 176 | mkdirSync(bin); | |
| 177 | writeFileSync(join(bin, "timeout"), `#!/usr/bin/env bash\nshift\nexec "$@"\n`); | |
| 178 | chmodSync(join(bin, "timeout"), 0o755); | |
| 171 | 179 | const calls = join(box, "docker-calls"); |
| 172 | 180 | // A docker that answers ps, stats and logs as the real one does, and records every call. |
| 173 | 181 | const fake = join(box, "docker"); |
| @@ -210,7 +218,7 @@ esac | ||
| 210 | 218 | const run = async () => { |
| 211 | 219 | const p = Bun.spawn(["bash", AGENT], { |
| 212 | 220 | env: { |
| 213 | PATH: process.env["PATH"] ?? "", | |
| 221 | PATH: `${withTimeout ? `${bin}:` : ""}${process.env["PATH"] ?? ""}`, | |
| 214 | 222 | FORGE_URL: `http://127.0.0.1:${String(forge.port)}`, |
| 215 | 223 | FORGE_GAME_TOKEN: TOKEN, |
| 216 | 224 | FORGE_GAME_DIR: box, |
new above
offline