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

opened this

Merged 45h 24m ago, live in production

sold/agent-capped → main 0 commits

  • Merged. sold/agent-capped was merged into main as 44598f0.

Review

Sign in to review this change.
2 files changed +13 −4
src/games/agent.sh +2 −1
@@ -82,7 +82,8 @@ now() { date -u +%Y-%m-%dT%H:%M:%SZ; }
8282 capped() {
8383 local secs="$1" pid i
8484 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
8687 timeout "$secs" "$@"
8788 return
8889 fi
test/game-box.test.ts +11 −3
@@ -8,7 +8,7 @@
88 */
99 import { yamlString } from "../src/cloud/boot_ascii.ts";
1010 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";
1212 import { rm } from "node:fs/promises";
1313 import { tmpdir } from "node:os";
1414 import { join } from "node:path";
@@ -166,8 +166,16 @@ describe("the agent", () => {
166166
167167 // The agent's own tools, which the box installs; a machine without them cannot run it.
168168 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 () => {
170173 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);
171179 const calls = join(box, "docker-calls");
172180 // A docker that answers ps, stats and logs as the real one does, and records every call.
173181 const fake = join(box, "docker");
@@ -210,7 +218,7 @@ esac
210218 const run = async () => {
211219 const p = Bun.spawn(["bash", AGENT], {
212220 env: {
213 PATH: process.env["PATH"] ?? "",
221 PATH: `${withTimeout ? `${bin}:` : ""}${process.env["PATH"] ?? ""}`,
214222 FORGE_URL: `http://127.0.0.1:${String(forge.port)}`,
215223 FORGE_GAME_TOKEN: TOKEN,
216224 FORGE_GAME_DIR: box,

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs