Public: anyone can read this; the crew can change it.
Game box agent: put a container left as <12 hex>_<service> back to its service name #18052
Merged 9h 44m ago, live in production
sold/box-rename → main 0 commits
2 files
changed +101
−0
src/games/agent.sh +24
| @@ -1088,6 +1088,7 @@ diag() { | ||
| 1088 | 1088 | --arg last "$(cat "/.job.last" 2>/dev/null)" \ |
| 1089 | 1089 | --arg metadata "" \ |
| 1090 | 1090 | --arg queued "$(wc -l <"" 2>/dev/null | tr -dc 0-9)" \ |
| 1091 | --arg renamed "$(tail -n 5 "/.renamed" 2>/dev/null | tail -c 600)" \ | |
| 1091 | 1092 | --arg up "$(tail -n 40 "/.up.log" 2>/dev/null | tail -c 2000)" \ |
| 1092 | 1093 | --arg vlog "$(tail -n 12 "" 2>/dev/null | tail -c 1200)" \ |
| 1093 | 1094 | --arg vdev "$(volume_device)" \ |
| @@ -1106,6 +1107,7 @@ diag() { | ||
| 1106 | 1107 | '{ |
| 1107 | 1108 | job: {label: , since: , running: , last: , queued: ( | tonumber? // 0)}, |
| 1108 | 1109 | upLog: , |
| 1110 | renamed: , | |
| 1109 | 1111 | ps: [( | split("\n")[] | fromjson? | {name: (.Names // ""), state: (.State // ""), status: (.Status // ""), image: (.Image // "")})][0:30], |
| 1110 | 1112 | images: [( | split("\n")[] | fromjson? | {image: ((.Repository // "") + ":" + (.Tag // "")), size: (.Size // "")})][0:20], |
| 1111 | 1113 | journal: {agent: , docker: }, |
| @@ -1394,6 +1396,27 @@ desk_told() { | ||
| 1394 | 1396 | return 0 |
| 1395 | 1397 | } |
| 1396 | 1398 | |
| 1399 | # An interrupted `docker compose up` leaves a container named `<12 hex>_<service>` (compose's | |
| 1400 | # temporary name while it recreates one): everything that addresses it by name then fails. Put each | |
| 1401 | # back to `<service>` when no container has that name, and say so in the diag (.renamed). Never while | |
| 1402 | # a job runs: compose may be mid-recreate and the name is its own. | |
| 1403 | rename_strays() { | |
| 1404 | job_running && return 0 | |
| 1405 | local names n want | |
| 1406 | names="$(capped 5 "" ps -a --format '{{.Names}}' 2>/dev/null)" || return 0 | |
| 1407 | while IFS= read -r n; do | |
| 1408 | [[ "" =~ ^[0-9a-f]{12}_(.+)$ ]] || continue | |
| 1409 | want="" | |
| 1410 | grep -qxF -- "" <<<"" && continue | |
| 1411 | if capped 5 "" rename "" "" >/dev/null 2>&1; then | |
| 1412 | names="$(grep -vxF -- "" <<<""; printf '%s\n' "")" | |
| 1413 | printf '%s renamed %s to %s\n' "$(date -u +%FT%TZ)" "" "" >>"/.renamed" | |
| 1414 | tail -n 20 "/.renamed" >"/.renamed.next" 2>/dev/null && mv "/.renamed.next" "/.renamed" | |
| 1415 | fi | |
| 1416 | done <<<"" | |
| 1417 | return 0 | |
| 1418 | } | |
| 1419 | ||
| 1397 | 1420 | sync() { |
| 1398 | 1421 | local next answer code |
| 1399 | 1422 | next="$(now)" |
| @@ -1971,6 +1994,7 @@ while :; do | ||
| 1971 | 1994 | ! grep -q '^no-volume ' "" 2>/dev/null || [ -z "$(volume_device)" ] || exit 0 |
| 1972 | 1995 | job_tick |
| 1973 | 1996 | shell_tick |
| 1997 | rename_strays | |
| 1974 | 1998 | sync || echo "forge-game-agent: sync failed; again in s" >&2 |
| 1975 | 1999 | job_tick |
| 1976 | 2000 | lend_tick |
test/game-agent-rename.test.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | /** | |
| 2 | * An interrupted `docker compose up` leaves containers named `<12 hex>_<service>` (box `mc`, | |
| 3 | * 2026-12-02). The agent puts each back to its service name when nothing else has it, before it | |
| 4 | * acts on services, and says so in the sync's diag (#18025). | |
| 5 | */ | |
| 6 | import { expect, test } from "bun:test"; | |
| 7 | import { chmodSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; | |
| 8 | import { rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { quietLogs } from "./helpers/globals.ts"; | |
| 12 | ||
| 13 | quietLogs(); | |
| 14 | ||
| 15 | const AGENT = join(import.meta.dir, "../src/games/agent.sh"); | |
| 16 | const lacks = ["bash", "curl", "jq", "timeout"].filter((t) => Bun.which(t) === null); | |
| 17 | const on = test.skipIf(lacks.length > 0); | |
| 18 | ||
| 19 | /** One sync against a docker that lists `names`: the renames it was asked for, and the diag it sent. */ | |
| 20 | async function syncWith(names: string[]): Promise<{ renames: string[]; renamed: string }> { | |
| 21 | const box = mkdtempSync(join(tmpdir(), "forge-game-rename-")); | |
| 22 | const docker = join(box, "docker"); | |
| 23 | writeFileSync(join(box, "names"), `${names.join("\n")}\n`); | |
| 24 | writeFileSync( | |
| 25 | docker, | |
| 26 | `#!/usr/bin/env bash | |
| 27 | case "$*" in | |
| 28 | "ps -a --format {{.Names}}") cat "${box}/names" ;; | |
| 29 | rename*) printf '%s\\n' "$*" >>"${box}/renames" ;; | |
| 30 | esac | |
| 31 | `, | |
| 32 | ); | |
| 33 | chmodSync(docker, 0o755); | |
| 34 | const bodies: string[] = []; | |
| 35 | const forge = Bun.serve({ | |
| 36 | port: 0, | |
| 37 | hostname: "127.0.0.1", | |
| 38 | async fetch(req) { | |
| 39 | bodies.push(await req.text()); | |
| 40 | return Response.json({ composeSha: "", commands: [] }); | |
| 41 | }, | |
| 42 | }); | |
| 43 | try { | |
| 44 | const p = Bun.spawn(["bash", AGENT], { | |
| 45 | env: { PATH: `${box}:${process.env["PATH"] ?? ""}`, FORGE_URL: `http://127.0.0.1:${String(forge.port)}`, FORGE_GAME_TOKEN: "fgb_abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG", FORGE_GAME_DIR: box, FORGE_GAME_DOCKER: docker, FORGE_GAME_ONCE: "1" }, | |
| 46 | stdout: "ignore", | |
| 47 | stderr: "pipe", | |
| 48 | }); | |
| 49 | const said = await new Response(p.stderr).text(); | |
| 50 | await p.exited; | |
| 51 | expect(bodies.length, `the agent said: ${said}`).toBe(1); | |
| 52 | const diag = (JSON.parse(bodies[0]!) as { diag: { renamed?: string } }).diag; | |
| 53 | let renames: string[] = []; | |
| 54 | try { | |
| 55 | renames = readFileSync(join(box, "renames"), "utf8").split("\n").filter(Boolean); | |
| 56 | } catch { | |
| 57 | // none asked | |
| 58 | } | |
| 59 | return { renames, renamed: diag.renamed ?? "" }; | |
| 60 | } finally { | |
| 61 | forge.stop(true); | |
| 62 | await rm(box, { recursive: true, force: true }); | |
| 63 | } | |
| 64 | } | |
| 65 | ||
| 66 | on("a container left as <12 hex>_<service> is renamed to the service, and the diag says so", async () => { | |
| 67 | const { renames, renamed } = await syncWith(["4f3c9a1b2d4e_survival", "9a8b7c6d5e4f_hub", "velocity"]); | |
| 68 | expect(renames).toEqual(["rename 4f3c9a1b2d4e_survival survival", "rename 9a8b7c6d5e4f_hub hub"]); | |
| 69 | expect(renamed).toContain("renamed 4f3c9a1b2d4e_survival to survival"); | |
| 70 | expect(renamed).toContain("renamed 9a8b7c6d5e4f_hub to hub"); | |
| 71 | }); | |
| 72 | ||
| 73 | on("a container is left alone when its service name is taken, or the name is not compose's", async () => { | |
| 74 | const { renames, renamed } = await syncWith(["4f3c9a1b2d4e_survival", "survival", "abc_hub", "4f3c9a1b2d4e5_hub", "my_survival"]); | |
| 75 | expect(renames).toEqual([]); | |
| 76 | expect(renamed).toBe(""); | |
| 77 | }); |
new above
offline