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

opened this

Merged 9h 44m ago, live in production

sold/box-rename → main 0 commits

  • Merged. sold/box-rename was merged into main as 99480aa.

Review

Sign in to review this change.
2 files changed +101 −0
src/games/agent.sh +24
@@ -1088,6 +1088,7 @@ diag() {
10881088 --arg last "$(cat "$DIR/.job.last" 2>/dev/null)" \
10891089 --arg metadata "$METADATA" \
10901090 --arg queued "$(wc -l <"$JOBQ" 2>/dev/null | tr -dc 0-9)" \
1091 --arg renamed "$(tail -n 5 "$DIR/.renamed" 2>/dev/null | tail -c 600)" \
10911092 --arg up "$(tail -n 40 "$DIR/.up.log" 2>/dev/null | tail -c 2000)" \
10921093 --arg vlog "$(tail -n 12 "$VOLUME_LOG" 2>/dev/null | tail -c 1200)" \
10931094 --arg vdev "$(volume_device)" \
@@ -1106,6 +1107,7 @@ diag() {
11061107 '{
11071108 job: {label: $label, since: $since, running: $running, last: $last, queued: ($queued | tonumber? // 0)},
11081109 upLog: $up,
1110 renamed: $renamed,
11091111 ps: [($ps | split("\n")[] | fromjson? | {name: (.Names // ""), state: (.State // ""), status: (.Status // ""), image: (.Image // "")})][0:30],
11101112 images: [($images | split("\n")[] | fromjson? | {image: ((.Repository // "") + ":" + (.Tag // "")), size: (.Size // "")})][0:20],
11111113 journal: {agent: $agent, docker: $docker},
@@ -1394,6 +1396,27 @@ desk_told() {
13941396 return 0
13951397 }
13961398
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 "$DOCKER" ps -a --format '{{.Names}}' 2>/dev/null)" || return 0
1407 while IFS= read -r n; do
1408 [[ "$n" =~ ^[0-9a-f]{12}_(.+)$ ]] || continue
1409 want="${BASH_REMATCH[1]}"
1410 grep -qxF -- "$want" <<<"$names" && continue
1411 if capped 5 "$DOCKER" rename "$n" "$want" >/dev/null 2>&1; then
1412 names="$(grep -vxF -- "$n" <<<"$names"; printf '%s\n' "$want")"
1413 printf '%s renamed %s to %s\n' "$(date -u +%FT%TZ)" "$n" "$want" >>"$DIR/.renamed"
1414 tail -n 20 "$DIR/.renamed" >"$DIR/.renamed.next" 2>/dev/null && mv "$DIR/.renamed.next" "$DIR/.renamed"
1415 fi
1416 done <<<"$names"
1417 return 0
1418 }
1419
13971420 sync() {
13981421 local next answer code
13991422 next="$(now)"
@@ -1971,6 +1994,7 @@ while :; do
19711994 ! grep -q '^no-volume ' "$VOLUME_HOLD" 2>/dev/null || [ -z "$(volume_device)" ] || exit 0
19721995 job_tick
19731996 shell_tick
1997 rename_strays
19741998 sync || echo "forge-game-agent: sync failed; again in ${EVERY}s" >&2
19751999 job_tick
19762000 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 });

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

Keyboard shortcuts

Docs