Public: anyone can read this; the crew can change it.
Every full sync says the agent reads the proxy's jars #17688
Merged 20h 4m ago, live in production
sold/jars-flag → main 0 commits
3 files
changed +14
−3
docs/features/game-box.md +1
| @@ -16,6 +16,7 @@ experiment. | ||
| 16 | 16 | - **The agent:** `/opt/forge-game/agent.sh` (`src/games/agent.sh`) under systemd (`forge-game-agent`). Every 5 s it POSTs the services, their CPU and memory, new log lines, command results and the host to `/api/v1/games/agent/sync`, and does what the answer asks: writes `compose.yml` and the answer's `.env` beside it (only that name: `FORWARDING_SECRET` and an `RCON_<SERVICE>` for every service; any other name is ignored and logged, so nothing can land on the agent's own files), runs `docker compose -p game --project-directory /opt/forge-game up -d --remove-orphans` when they change, and runs queued commands (rcon through `docker compose exec -T <service> rcon-cli -- <text>`, by service name, or restart, stop, start, pull, recreate). Nothing dials in to the box; a failed sync is tried again in 5 s, forever. |
| 17 | 17 | - **When it is up but not playing:** the boot installs Docker, its compose plugin and jq in `runcmd`, each apt call waiting up to ten minutes for the lock a first boot's unattended upgrade holds, three tries (a `packages:` list failed on that lock and left box `mc` with no jq). Its last step, `agent.sh --boot-report`, posts cloud-init's status to the sync once. An agent missing jq or docker still posts `{"agent":1,"boot":"missing jq: <cloud-init status>"}`, built by hand with curl, every 30 s until they are there. The forge keeps that word until a full sync clears it, and `/games` and the terminal's Games say "Box is up but missing jq: …". |
| 18 | 18 | - **Never blind:** compose up, pull and the power verbs run in a background job queue, one at a time, output to `.up.log`; the agent keeps syncing every 5 s whatever runs, and a failed up is not retried for the same compose. Every sync carries `diag`, about 8 KB: the job (label, since, running, last exit) and the tail of `.up.log`, `docker ps -a` and `docker images` (bounded), the agent's and Docker's journal, cloud-init's status, `df -h /`, where Docker's data root is mounted and whether the volume is, and the versions. The whole diag runs within one deadline (8 s; every docker read in a sync is capped at 5 s), and over it the sync carries `{"note":"diag ran over 8 s"}`, so a docker that hangs never holds the sync. A job interrupted under the agent (a stale pid with no exit status, or a pid that is now another program's, by its command line) is queued again, never marked failed. The forge keeps the latest (16 KB at most). The box's owner (and forge admins) read it whole; anyone else who may see one of its services reads only what it is doing ("Applying since …", "N of M running"), naming no container, image, error or journal. For the owner, `/games/<box>` has a Box section of plain lines ("Pulling …", "compose up failed: <its last error line>", "Docker's data is on the root disk, not the volume", "The volume is not mounted at …") over a folded run log of the raw tails; the terminal's Games shows the top line (`b` there opens the box: the same plain lines and raw tails) and the MCP games tool's `list` up to three. |
| 19 | - **The proxy's own jars:** a compose file that mounts `./jars:/forge-jars:ro` in the proxy and names the files in its `PLUGINS` comes with `jars: [{ name, url, sha256 }]` in the same answer. The agent fetches each into `/opt/forge-game/jars` and checks its sha256 before the up; one that is missing or wrong holds back the proxy alone. An up that succeeds does not prove that the plugin loaded: read the proxy's log for that. Every full sync's body says `"jars": true`, which an agent from before this does not. | |
| 19 | 20 | - **The volume:** the box's worlds live on its cloud volume, never on the root disk a replace destroys. The boot runs `agent.sh --mount-volume` before Docker is installed, and the agent runs the same check once at each start: |
| 20 | 21 | - **Find:** the volume is the device the cloud attaches (`/dev/disk/by-id/scsi-0DO_Volume_*` on DigitalOcean, `scsi-0HC_Volume_*` on Hetzner), waited for up to a minute at boot. With none, nothing is mounted and the log says so. |
| 21 | 22 | - **Mounted means the volume holds the data directory:** `findmnt -n -o SOURCE --target <dir>` is the device, by-id links resolved. Another device mounted there is not the volume. |
src/games/agent.sh +1 −1
| @@ -1230,7 +1230,7 @@ sync() { | ||
| 1230 | 1230 | else [.[0]] + (.[1:] | codes) end; |
| 1231 | 1231 | (if . == "" then ["0"] else split(";") end) | [codes[] | [.] // empty | "\u00a7" + .] | join(""); |
| 1232 | 1232 | def plain: gsub("\u001b\\[(?<p>[0-9;]*)m"; (.p // "" | sect)) | gsub("^(\u00a7r)+|(\u00a7r)+$"; "") | gsub("(\u00a7r)+"; "\u00a7r") | gsub("\u001b\\[[0-9;?]*[ -/]*[@-~]"; "") | gsub("\u001b[@-_]?"; "") | gsub("[\u0001-\u0008\u000b\u000c\u000e-\u001f\u007f]"; ""); |
| 1233 | {agent: 1, composeSha: , services: [0], logs: [0], results: [0], host: [0], diag: [0], people: [0], metrics: [0], snapshots: [0], chat: [0].lines, chatId: [0].id, lend: , retry: } | |
| 1233 | {agent: 1, composeSha: , services: [0], logs: [0], results: [0], host: [0], diag: [0], people: [0], metrics: [0], snapshots: [0], chat: [0].lines, chatId: [0].id, lend: , retry: , jars: true} | |
| 1234 | 1234 | + (if == "" then {} else {tokenSha: } end) |
| 1235 | 1235 | + (if [0] == [] then {} else {ask: [0]} end) |
| 1236 | 1236 | | walk(if type == "string" then plain else . end)' >"$DIR/.sync.body" || return 1 |
test/game-jars.test.ts +12 −2
| @@ -51,11 +51,13 @@ exit 0 | ||
| 51 | 51 | ); |
| 52 | 52 | chmodSync(fake, 0o755); |
| 53 | 53 | const compose = o.compose ?? "services: {}\n"; |
| 54 | const bodies: Record<string, any>[] = []; | |
| 54 | 55 | const forge: ReturnType<typeof Bun.serve> = Bun.serve({ |
| 55 | 56 | port: 0, |
| 56 | 57 | hostname: "127.0.0.1", |
| 57 | fetch(req): Response { | |
| 58 | async fetch(req): Promise<Response> { | |
| 58 | 59 | if (new URL(req.url).pathname === "/jar") return new Response(o.served ?? JAR); |
| 60 | bodies.push((await req.json()) as Record<string, any>); | |
| 59 | 61 | const jars = [{ name: o.name ?? NAME, url: `http://127.0.0.1:${String(forge.port)}/jar?sig=secret`, sha256: sha("sha256", JAR) }]; |
| 60 | 62 | return Response.json({ composeSha: sha("sha256", compose), compose, commands: [], ...(o.jars === false ? {} : { jars }) }); |
| 61 | 63 | }, |
| @@ -89,7 +91,7 @@ exit 0 | ||
| 89 | 91 | } |
| 90 | 92 | }; |
| 91 | 93 | const read = (f: string): string => (existsSync(join(root, f)) ? readFileSync(join(root, f), "utf8") : ""); |
| 92 | return { dir, plugins, run, read, log: () => readFileSync(join(dir, ".up.log"), "utf8") }; | |
| 94 | return { dir, plugins, bodies, run, read, log: () => readFileSync(join(dir, ".up.log"), "utf8") }; | |
| 93 | 95 | } |
| 94 | 96 | |
| 95 | 97 | test("the agent moves aside the jar the forge pins, and no other", () => { |
| @@ -98,6 +100,14 @@ test("the agent moves aside the jar the forge pins, and no other", () => { | ||
| 98 | 100 | expect(agent.includes(`AUTOSTOPPER_PROJECT="${AUTOSTOPPER.split(":")[0]!}"`)).toBe(true); |
| 99 | 101 | }); |
| 100 | 102 | |
| 103 | test("every full sync says this agent reads jars, so the forge may send a compose file that names one", async () => { | |
| 104 | const box = made({ jars: false }); | |
| 105 | await box.run(); | |
| 106 | const full = box.bodies.filter((b) => b["light"] !== true); | |
| 107 | expect(full.length).toBeGreaterThan(0); | |
| 108 | expect(full.every((b) => b["jars"] === true)).toBe(true); | |
| 109 | }); | |
| 110 | ||
| 101 | 111 | test("without jars, an up is what it was", async () => { |
| 102 | 112 | const box = made({ jars: false }); |
| 103 | 113 | await box.run(); |
new above
offline