Public: anyone can read this; the crew can change it.
Games: heal what is safe alone and say so: retry a stuck agent or a stuck wake, with a limit (#17610) #17621
Merged 8h 46m ago, live in production
torque/forge:torque/17618-games-heal-what-is-safe-alone-and-say-so → main 0 commits fork 1 thread · 1 unresolved
11 files
changed +328
−2
- docs/features/games.md
- src/core/migration-pins/20261202200000-game-heal.pin
- src/core/migrations/20261202200000-game-heal.ts
- src/games/heal.ts
- src/http/games_routes.ts
- src/http/mcp/tools/games.ts
- src/tui/screens/games.ts
- src/web/pages/games.ts
- test/game-diag.test.ts
- test/game-heal.test.ts
- test/tui-games.test.ts
docs/features/games.md +4
| @@ -83,6 +83,10 @@ How the forge hears chat: the agent reads each server's `logs/latest.log` and se | ||
| 83 | 83 | |
| 84 | 84 | A name in chat is trusted only because players reach the backends through Velocity in online mode with modern forwarding, so the proxy has checked the name with Mojang. A box whose compose publishes any port on a service other than the proxy (or runs one with `network_mode: host`) takes no `!forge` at all: a player connecting to that backend directly could use any name. On Fabric, a mod that broadcasts lines in vanilla's chat format (some chat bridges do) can speak as any name; on such a server, link no one who operates it. Each later command runs only while the server lists that name with the bound UUID. The name not listed (not on, or the list not read yet): refused, the link kept. The name listed with another UUID (someone else holds the name now): refused, and the link is dropped, so a name given up cannot speak for the account. A link made before UUIDs were bound has none and goes the same way: link it again. |
| 85 | 85 | |
| 86 | ## Healing | |
| 87 | ||
| 88 | What is safe is mended alone and said (#17618, `src/games/heal.ts`), after each sync. Only `start`: a sleeping server that players joined the proxy for and that was still asleep 90 s later (the one asleep server; with two asleep the forge does not guess), and a `start` the agent was handed and never answered in 5 min (closed as failed, started again). Each service gets two tries in six hours, 2 min apart; then the forge says it gave up, once. Every try is a row (`game_heal`) and a line on the box screen of the page, the terminal (`src/tui/screens/games.ts`) and the `healed` field of the `games` tool's `list`, for the box's owners. An agent silent for 3 min is said too: the forge cannot restart it. Mods, settings, worlds and roles are never changed alone. | |
| 89 | ||
| 86 | 90 | ## Secrets |
| 87 | 91 | |
| 88 | 92 | The Velocity forwarding secret (`forwarding.env`) and an RCON password a service (`rcon-<service>.env`) are generated once, sealed like every forge secret and sent only in the sync's `env`, never committed. A box token is shown once and stored hashed. An uploaded jar is kept by the forge at `/games/jar/<id>/<name>`; the compose a box gets carries it signed for 6 hours, and only for the box that uploaded it. A jar must be a `.jar` (a zip), at most 64 MiB, and a box keeps at most 20 jars and 256 MiB. |
src/core/migration-pins/20261202200000-game-heal.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | 15b13037b2769ee7fa20beab83d2de23ec1d14c548e8f52ea1c398daa04e7639 |
src/core/migrations/20261202200000-game-heal.ts added +13
| @@ -0,0 +1,13 @@ | ||
| 1 | // What the forge did alone for a game box (#17618): a stuck wake or a lost start, retried with a | |
| 2 | // limit, each try a row so every door says it and the limit counts it. | |
| 3 | export default ` | |
| 4 | CREATE TABLE IF NOT EXISTS game_heal ( | |
| 5 | id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| 6 | box_id TEXT NOT NULL, | |
| 7 | service TEXT NOT NULL, | |
| 8 | kind TEXT NOT NULL, | |
| 9 | note TEXT NOT NULL, | |
| 10 | at TEXT NOT NULL | |
| 11 | ); | |
| 12 | CREATE INDEX IF NOT EXISTS game_heal_box ON game_heal (box_id, service, id); | |
| 13 | `; |
src/games/heal.ts added +124
| @@ -0,0 +1,124 @@ | ||
| 1 | /** | |
| 2 | * What a game box mends alone, and says (#17618). Only what is safe, and only `start`: a server a | |
| 3 | * player joined and that stayed asleep (a stuck wake), and a `start` the agent was handed and never | |
| 4 | * answered (a stuck agent job). Each is tried at most `TRIES` times a service in `WINDOW_MS`, the | |
| 5 | * try a row (`game_heal`) every door reads, and after the last the forge says it gave up. What | |
| 6 | * changes a person's server (mods, settings, worlds, roles) is never done here: the lines only | |
| 7 | * name it. Log text is data: the signal is the proxy's join events and the box's own states. | |
| 8 | */ | |
| 9 | ||
| 10 | import type { Database } from "bun:sqlite"; | |
| 11 | import { playerEvents } from "./metrics.ts"; | |
| 12 | import { networkOf } from "./people.ts"; | |
| 13 | import { proxyOf } from "./sleep.ts"; | |
| 14 | import { queueCommand, servicesOf, SERVICE_NAME, type GameBox } from "./store.ts"; | |
| 15 | ||
| 16 | /** Tries a service gets in `WINDOW_MS`. */ | |
| 17 | export const TRIES = 2; | |
| 18 | export const WINDOW_MS = 6 * 3_600_000; | |
| 19 | /** A measured wake takes 13 to 52 s: a join still asleep after this has not been woken. */ | |
| 20 | export const WAKE_GRACE_MS = 90_000; | |
| 21 | /** A join older than this is a player who has long gone. */ | |
| 22 | const JOIN_FRESH_MS = 15 * 60_000; | |
| 23 | /** Between two tries: the first start needs time to show. */ | |
| 24 | export const GAP_MS = 120_000; | |
| 25 | /** A handed `start` unanswered this long is lost. */ | |
| 26 | export const LOST_MS = 5 * 60_000; | |
| 27 | /** An agent silent this long is stuck or down. */ | |
| 28 | export const SILENT_MS = 3 * 60_000; | |
| 29 | export const HEALER = "forge heal"; | |
| 30 | ||
| 31 | export interface JoinEvent { | |
| 32 | readonly service: string; | |
| 33 | readonly player: string; | |
| 34 | readonly kind: string; | |
| 35 | readonly at: string; | |
| 36 | } | |
| 37 | ||
| 38 | /** | |
| 39 | * The players who joined the proxy `GRACE` or more ago, are on no server and have not left: they | |
| 40 | * are waiting for something. Events newest first, as `playerEvents` reads them. | |
| 41 | */ | |
| 42 | export function waiting(events: readonly JoinEvent[], network: Record<string, string[]>, now: number): string[] { | |
| 43 | const placed = new Set(Object.values(network).flat()); | |
| 44 | const left = new Set<string>(); | |
| 45 | const out: string[] = []; | |
| 46 | for (const e of events) { | |
| 47 | if (e.kind === "leave") left.add(e.player); | |
| 48 | else if (e.kind === "join" && !left.has(e.player) && !placed.has(e.player)) { | |
| 49 | const age = now - Date.parse(e.at); | |
| 50 | if (age >= WAKE_GRACE_MS && age <= JOIN_FRESH_MS && !out.includes(e.player)) out.push(e.player); | |
| 51 | } | |
| 52 | } | |
| 53 | return out; | |
| 54 | } | |
| 55 | ||
| 56 | const tried = (db: Database, boxId: string, service: string, since: string): { n: number; last: string } => { | |
| 57 | const r = db | |
| 58 | .query<{ n: number; last: string | null }, [string, string, string]>( | |
| 59 | "SELECT COUNT(*) AS n, MAX(at) AS last FROM game_heal WHERE box_id = ?1 AND service = ?2 AND kind != 'gave_up' AND at >= ?3", | |
| 60 | ) | |
| 61 | .get(boxId, service, since)!; | |
| 62 | return { n: r.n, last: r.last ?? "" }; | |
| 63 | }; | |
| 64 | ||
| 65 | function startOnce(db: Database, box: Pick<GameBox, "id">, service: string, kind: string, why: string, now: number): boolean { | |
| 66 | const at = new Date(now).toISOString(); | |
| 67 | const since = new Date(now - WINDOW_MS).toISOString(); | |
| 68 | const have = tried(db, box.id, service, since); | |
| 69 | if (have.last !== "" && now - Date.parse(have.last) < GAP_MS) return false; | |
| 70 | if (have.n >= TRIES) { | |
| 71 | const said = db.query("SELECT 1 FROM game_heal WHERE box_id = ?1 AND service = ?2 AND kind = 'gave_up' AND at >= ?3").get(box.id, service, since); | |
| 72 | if (said === null) db.query("INSERT INTO game_heal (box_id, service, kind, note, at) VALUES (?1, ?2, 'gave_up', ?3, ?4)").run(box.id, service, why, at); | |
| 73 | return false; | |
| 74 | } | |
| 75 | queueCommand(db, box.id, service, "start", "", HEALER); | |
| 76 | db.query("INSERT INTO game_heal (box_id, service, kind, note, at) VALUES (?1, ?2, ?3, ?4, ?5)").run(box.id, service, kind, `${why} (try ${have.n + 1} of ${TRIES})`, at); | |
| 77 | return true; | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * After a sync: start the one sleeping server players are waiting on, and start again a `start` | |
| 82 | * the agent lost. Several servers asleep with someone waiting is not guessed: it is said, not done. | |
| 83 | */ | |
| 84 | export function heal(db: Database, box: Pick<GameBox, "id">, composeText: string | undefined, now = Date.now()): void { | |
| 85 | const services = servicesOf(db, box.id); | |
| 86 | const asleep = services.filter((s) => s.asleep).map((s) => s.name); | |
| 87 | const proxy = composeText === undefined ? undefined : proxyOf(composeText); | |
| 88 | if (asleep.length === 1 && proxy !== undefined) { | |
| 89 | const who = waiting(playerEvents(db, box.id, proxy, 20), networkOf(db, box.id), now); | |
| 90 | if (who.length > 0) startOnce(db, box, asleep[0]!, "wake", `${who.length === 1 ? "a player joined" : `${who.length} players joined`} and ${asleep[0]} stayed asleep`, now); | |
| 91 | } | |
| 92 | const lost = db | |
| 93 | .query<{ id: string; service: string }, [string, string, string]>( | |
| 94 | "SELECT id, service FROM game_command WHERE box_id = ?1 AND kind = 'start' AND handed_at != '' AND handed_at < ?2 AND handed_at > ?3 AND done_at = '' LIMIT 8", | |
| 95 | ) | |
| 96 | .all(box.id, new Date(now - LOST_MS).toISOString(), new Date(now - WINDOW_MS).toISOString()); | |
| 97 | for (const c of lost) { | |
| 98 | if (!SERVICE_NAME.test(c.service)) continue; | |
| 99 | const again = startOnce(db, box, c.service, "retry", `the agent never answered a start of ${c.service}`, now); | |
| 100 | const output = again ? "the agent never answered; the forge started it again" : "the agent never answered; the forge did not start it again (limit or wait between tries)"; | |
| 101 | db.query("UPDATE game_command SET done_at = ?2, ok = 0, output = ?3 WHERE id = ?1").run(c.id, new Date(now).toISOString(), output); | |
| 102 | } | |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * The plain lines every door says, newest first: what the forge did alone in the last window, that | |
| 107 | * it gave up, a silent agent (which the forge cannot restart), and what stays the person's. | |
| 108 | */ | |
| 109 | export function healWords(db: Database, box: Pick<GameBox, "id" | "seenAt">, now = Date.now()): string[] { | |
| 110 | const rows = db | |
| 111 | .query<{ service: string; kind: string; note: string; at: string }, [string, string]>( | |
| 112 | "SELECT service, kind, note, at FROM game_heal WHERE box_id = ?1 AND at >= ?2 ORDER BY id DESC LIMIT 6", | |
| 113 | ) | |
| 114 | .all(box.id, new Date(now - WINDOW_MS).toISOString()); | |
| 115 | const out = rows.map((r) => | |
| 116 | r.kind === "gave_up" | |
| 117 | ? `Gave up on ${r.service} after ${TRIES} tries: ${r.note}. Start it yourself, or check the box's run log.` | |
| 118 | : `Started ${r.service} again, alone, at ${r.at.slice(11, 16)} UTC: ${r.note}.`, | |
| 119 | ); | |
| 120 | const silent = box.seenAt === "" ? 0 : now - Date.parse(box.seenAt); | |
| 121 | if (silent >= SILENT_MS) out.unshift(`The box's agent has not synced for ${Math.floor(silent / 60_000)} min. The forge cannot restart it; reboot the machine from its cloud page.`); | |
| 122 | if (out.length > 0) out.push("Mods, settings, worlds and roles are never changed alone: they stay yours to apply."); | |
| 123 | return out; | |
| 124 | } |
src/http/games_routes.ts +4
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | |
| 6 | 6 | import { composeServices, readCompose } from "../games/compose.ts"; |
| 7 | 7 | import { AGENT_SH, AGENT_SHA } from "../games/boot.ts"; |
| 8 | import { heal } from "../games/heal.ts"; | |
| 8 | 9 | import { synced } from "../games/notify.ts"; |
| 9 | 10 | import { auditShellResults } from "../games/shell.ts"; |
| 10 | 11 | import { recordMetrics } from "../games/metrics.ts"; |
| @@ -50,6 +51,9 @@ export async function gameAgentSync(ctx: Ctx): Promise<Response> { | ||
| 50 | 51 | const compose = await readCompose(ctx.app, box); |
| 51 | 52 | // A server its sleep stopped is asleep, not down: the compose file says which sleep. |
| 52 | 53 | markAsleep(ctx.app.db, box.id, compose === undefined ? [] : sleeping(compose.text)); |
| 54 | // What is safe is mended alone and said (`games/heal.ts`): a stuck wake, a lost start, with a | |
| 55 | // limit. | |
| 56 | if (body.boot === undefined) heal(ctx.app.db, box, compose?.text); | |
| 53 | 57 | // `!forge` said in game chat: checked and kept here; a plan, a commit or the crew is never waited |
| 54 | 58 | // for (`games/chat.ts`). |
| 55 | 59 | gameChat(ctx.app, box, body.chat, compose?.text, ctx.url.origin, Date.now(), body.chatId); |
src/http/mcp/tools/games.ts +3
| @@ -3,6 +3,7 @@ import { commitServiceEnv } from "../../../games/compose.ts"; | ||
| 3 | 3 | import { search, searchPacks } from "../../../games/modrinth.ts"; |
| 4 | 4 | import { packCheckLines } from "../../../games/pack_check.ts"; |
| 5 | 5 | import { commitPlan, modsOf, packUpdateOf, planInstall, planPack, planPackUpdate, planRemove, planUpdate, updatesOf } from "../../../games/mods.ts"; |
| 6 | import { healWords } from "../../../games/heal.ts"; | |
| 6 | 7 | import { boxStatus, boxStatusShared } from "../../../games/diag.ts"; |
| 7 | 8 | import { commitSettings, previewSettings, serviceSettings } from "../../../games/settings.ts"; |
| 8 | 9 | import { compactMetrics, compactSeries } from "../../../games/metrics_answer.ts"; |
| @@ -77,6 +78,8 @@ export const tool: Tool = { | ||
| 77 | 78 | // no container, image, error or journal. |
| 78 | 79 | status: (mayDo(roleOn(db, user, b.id), "owner") ? boxStatus(b.diag) : boxStatusShared(b.diag)).slice(0, 3), |
| 79 | 80 | packFiles: packCheckLines(ctx.app, b.id), |
| 81 | // What the forge mended alone and could not (`games/heal.ts`), the owner's to read. | |
| 82 | ...(mayDo(roleOn(db, user, b.id), "owner") ? { healed: healWords(db, b) } : {}), | |
| 80 | 83 | ...(cloud[i] === undefined ? {} : { cloud: cloud[i] }), |
| 81 | 84 | services: servicesOf(db, b.id).filter((s) => mayDo(roleOn(db, user, b.id, s.name), "viewer")), |
| 82 | 85 | snapshots: Object.fromEntries(Object.entries(b.snapshots).filter(([s]) => mayDo(roleOn(db, user, b.id, s), "viewer")).map(([s, list]) => [s, list.slice(0, 10)])), |
src/tui/screens/games.ts +2 −1
| @@ -21,6 +21,7 @@ import { eventsOf, isBedrock, peopleOf, peopleWords, queuePeople } from "../../g | ||
| 21 | 21 | import { queueWorld, worldWords, type WorldOp } from "../../games/worlds.ts"; |
| 22 | 22 | import { lendWords, lentRunnerOf, setLend } from "../../games/lend.ts"; |
| 23 | 23 | import { spareRow } from "../../games/spare_row.ts"; |
| 24 | import { healWords } from "../../games/heal.ts"; | |
| 24 | 25 | import { boxStatus, boxStatusShared, boxTails } from "../../games/diag.ts"; |
| 25 | 26 | import { cloudLine, pinnedOfBox } from "../../actions/cloud_says.ts"; |
| 26 | 27 | import { getUserByName } from "../../core/users.ts"; |
| @@ -525,7 +526,7 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 525 | 526 | const shared = boxStatusShared(b.diag); |
| 526 | 527 | return [...spare, ...(shared.length === 0 ? ["The box has said nothing yet."] : shared)]; |
| 527 | 528 | } |
| 528 | const plain = boxStatus(b.diag); | |
| 529 | const plain = [...boxStatus(b.diag), ...healWords(f.app.db, b)]; | |
| 529 | 530 | const tails = boxTails(b.diag); |
| 530 | 531 | if (plain.length + tails.length === 0) return [...lend, ...spare, "The box has sent no diagnosis yet: its agent sends one with every sync."]; |
| 531 | 532 | return [...lend, ...spare, ...plain, ...tails.flatMap(([k, v]) => ["", `== ${k}`, ...v.split("\n")])]; |
src/web/pages/games.ts +2 −1
| @@ -15,6 +15,7 @@ import { SHELL_SERVICE, queueShell } from "../../games/shell.ts"; | ||
| 15 | 15 | import { statusRow } from "./security.ts"; |
| 16 | 16 | import { lendState, lentRunnerOf, setLend } from "../../games/lend.ts"; |
| 17 | 17 | import { spareRow } from "../../games/spare_row.ts"; |
| 18 | import { healWords } from "../../games/heal.ts"; | |
| 18 | 19 | import { boxStatus, boxStatusShared, boxTails } from "../../games/diag.ts"; |
| 19 | 20 | import { commitSettings, planSettings, previewSettings, settingsOf, SETTINGS, type SettingValue, type SettingsPlan } from "../../games/settings.ts"; |
| 20 | 21 | import { |
| @@ -292,7 +293,7 @@ export async function gameBoxPage(ctx: Ctx): Promise<Response> { | ||
| 292 | 293 | ${operator ? newServerBlock(box, words, preview) : null} |
| 293 | 294 | ${chatBlock(ctx, box, role === "owner")} |
| 294 | 295 | ${spareBlock(ctx, box)} |
| 295 | ${role === "owner" ? boxBlock(box, packCheckLines(ctx.app, box.id)) : sharedBoxBlock(box, packCheckLines(ctx.app, box.id))} ${role === "owner" ? shellBlock(ctx, box) : null} | |
| 296 | ${role === "owner" ? boxBlock(box, [...healWords(ctx.app.db, box), ...packCheckLines(ctx.app, box.id)]) : sharedBoxBlock(box, packCheckLines(ctx.app, box.id))} ${role === "owner" ? shellBlock(ctx, box) : null} | |
| 296 | 297 | ${role === "owner" ? lendBlock(ctx, box) : null} |
| 297 | 298 | ${role === "owner" ? grantsBlock(ctx, box) : null}`, |
| 298 | 299 | })); |
test/game-diag.test.ts +9
| @@ -443,4 +443,13 @@ describe("the doors", () => { | ||
| 443 | 443 | "2 containers: 1 running, 1 not", |
| 444 | 444 | ]); |
| 445 | 445 | }); |
| 446 | ||
| 447 | test("what the forge started again alone is the owner's to read in the MCP games list, not a viewer's (#17618)", async () => { | |
| 448 | const box = boxNamed(app.db, "mc")!; | |
| 449 | app.db.query("INSERT INTO game_heal (box_id, service, kind, note, at) VALUES (?1, 'hub', 'wake', 'a player joined and hub stayed asleep (try 1 of 2)', ?2)").run(box.id, new Date().toISOString()); | |
| 450 | const own = await mcpTool(servers.url, createToken(app.db, alice.id, "admin", "admin").token, "games", { action: "list" }); | |
| 451 | expect(own.value.items[0].healed.join("\n")).toContain("Started hub again, alone"); | |
| 452 | const view = await mcpTool(servers.url, createToken(app.db, bob.id, "rw", "rw").token, "games", { action: "list" }); | |
| 453 | expect(view.value.items[0].healed).toBeUndefined(); | |
| 454 | }); | |
| 446 | 455 | }); |
test/game-heal.test.ts added +149
| @@ -0,0 +1,149 @@ | ||
| 1 | /** | |
| 2 | * Healing (#17618): a server a player joined that stayed asleep, and a `start` the agent never | |
| 3 | * answered, are started again alone, at most twice a service, each try said on every door; what | |
| 4 | * changes a server is never done. Built by hand to the numbers: a join at 12:00:00, a grace of 90 | |
| 5 | * s. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtempSync } from "node:fs"; | |
| 9 | import { rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { GAP_MS, heal, healWords, LOST_MS, SILENT_MS, TRIES, waiting, WAKE_GRACE_MS } from "../src/games/heal.ts"; | |
| 14 | import { boxNamed, createGameBox, markAsleep, queueCommand, recordSync, recentCommands, type GameBox } from "../src/games/store.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | const COMPOSE = `services: | |
| 20 | velocity: | |
| 21 | image: itzg/mc-proxy:latest | |
| 22 | hunting: | |
| 23 | image: itzg/minecraft-server:latest | |
| 24 | restart: "no" | |
| 25 | labels: | |
| 26 | forge.sleep: "true" | |
| 27 | `; | |
| 28 | const T0 = Date.parse("2026-12-02T12:00:00Z"); | |
| 29 | const iso = (ms: number) => new Date(ms).toISOString(); | |
| 30 | ||
| 31 | let app: App; | |
| 32 | let dir: string; | |
| 33 | let n = 0; | |
| 34 | beforeAll(() => { | |
| 35 | dir = mkdtempSync(join(tmpdir(), "forge-game-heal-")); | |
| 36 | app = createApp({ dataDir: dir, port: 0 }); | |
| 37 | }); | |
| 38 | afterAll(async () => { | |
| 39 | app.close(); | |
| 40 | await rm(dir, { recursive: true, force: true }); | |
| 41 | }); | |
| 42 | ||
| 43 | /** A box whose `hunting` sleeps, and Alice who joined the proxy at T0 and is on no server. */ | |
| 44 | function box(opts: { join?: boolean; asleep?: string[] } = {}): GameBox { | |
| 45 | const name = `heal${n++}`; | |
| 46 | createGameBox(app.db, { name }); | |
| 47 | const b = boxNamed(app.db, name)!; | |
| 48 | recordSync(app.db, b, { | |
| 49 | services: [ | |
| 50 | { name: "velocity", state: "running" }, | |
| 51 | { name: "hunting", state: "exited" }, | |
| 52 | ], | |
| 53 | }); | |
| 54 | markAsleep(app.db, b.id, opts.asleep ?? ["hunting"]); | |
| 55 | if (opts.join !== false) | |
| 56 | app.db.query("INSERT INTO game_player_event (box_id, service, player, kind, at, text) VALUES (?1, 'velocity', 'Alice', 'join', ?2, '')").run(b.id, iso(T0)); | |
| 57 | return b; | |
| 58 | } | |
| 59 | const starts = (b: GameBox) => recentCommands(app.db, b.id, "hunting", 20).filter((c) => c.kind === "start"); | |
| 60 | ||
| 61 | describe("the signal", () => { | |
| 62 | const join = (at: number, kind = "join", player = "Alice") => ({ service: "velocity", player, kind, at: iso(at) }); | |
| 63 | test("a join is waiting after the grace, on no server, not left, and not older than 15 min", () => { | |
| 64 | expect(waiting([join(T0)], {}, T0 + WAKE_GRACE_MS - 1)).toEqual([]); | |
| 65 | expect(waiting([join(T0)], {}, T0 + WAKE_GRACE_MS)).toEqual(["Alice"]); | |
| 66 | expect(waiting([join(T0)], { hub: ["Alice"] }, T0 + WAKE_GRACE_MS)).toEqual([]); | |
| 67 | expect(waiting([join(T0 + 1000, "leave"), join(T0)], {}, T0 + WAKE_GRACE_MS + 2000)).toEqual([]); | |
| 68 | expect(waiting([join(T0)], {}, T0 + 15 * 60_000 + 1)).toEqual([]); | |
| 69 | }); | |
| 70 | }); | |
| 71 | ||
| 72 | describe("a stuck wake", () => { | |
| 73 | test("is started alone, said, and not again within the gap", () => { | |
| 74 | const b = box(); | |
| 75 | heal(app.db, b, COMPOSE, T0 + 60_000); | |
| 76 | expect(starts(b)).toHaveLength(0); | |
| 77 | heal(app.db, b, COMPOSE, T0 + WAKE_GRACE_MS); | |
| 78 | expect(starts(b)).toHaveLength(1); | |
| 79 | expect(starts(b)[0]!.service).toBe("hunting"); | |
| 80 | heal(app.db, b, COMPOSE, T0 + WAKE_GRACE_MS + GAP_MS - 1); | |
| 81 | expect(starts(b)).toHaveLength(1); | |
| 82 | const words = healWords(app.db, b, T0 + WAKE_GRACE_MS + 1000).join("\n"); | |
| 83 | expect(words).toContain("Started hunting again, alone"); | |
| 84 | expect(words).toContain("try 1 of 2"); | |
| 85 | expect(words).toContain("never changed alone"); | |
| 86 | }); | |
| 87 | ||
| 88 | test("is tried at most TRIES times, then given up on, once, in words", () => { | |
| 89 | const b = box(); | |
| 90 | let at = T0 + WAKE_GRACE_MS; | |
| 91 | for (let i = 0; i < TRIES + 3; i++, at += GAP_MS) heal(app.db, b, COMPOSE, at); | |
| 92 | expect(starts(b)).toHaveLength(TRIES); | |
| 93 | const gave = healWords(app.db, b, at).filter((l) => l.startsWith("Gave up on hunting")); | |
| 94 | expect(gave).toHaveLength(1); | |
| 95 | expect(gave[0]).toContain("Start it yourself"); | |
| 96 | }); | |
| 97 | ||
| 98 | test("is not guessed when two servers sleep, nor when the server is awake or nobody waits", () => { | |
| 99 | const two = box(); | |
| 100 | app.db.query("UPDATE game_service SET asleep = 1 WHERE box_id = ?1").run(two.id); | |
| 101 | heal(app.db, two, COMPOSE, T0 + WAKE_GRACE_MS); | |
| 102 | expect(starts(two)).toHaveLength(0); | |
| 103 | const awake = box({ asleep: [] }); | |
| 104 | heal(app.db, awake, COMPOSE, T0 + WAKE_GRACE_MS); | |
| 105 | expect(starts(awake)).toHaveLength(0); | |
| 106 | const quiet = box({ join: false }); | |
| 107 | heal(app.db, quiet, COMPOSE, T0 + WAKE_GRACE_MS); | |
| 108 | expect(starts(quiet)).toHaveLength(0); | |
| 109 | }); | |
| 110 | }); | |
| 111 | ||
| 112 | describe("a stuck agent", () => { | |
| 113 | test("a start handed and never answered is closed as failed and started again", () => { | |
| 114 | const b = box({ join: false }); | |
| 115 | const id = queueCommand(app.db, b.id, "hunting", "start", "", "alice"); | |
| 116 | recordSync(app.db, b, {}, new Date(T0)); | |
| 117 | heal(app.db, b, COMPOSE, T0 + LOST_MS - 1000); | |
| 118 | expect(starts(b)).toHaveLength(1); | |
| 119 | heal(app.db, b, COMPOSE, T0 + LOST_MS + 1000); | |
| 120 | const all = starts(b); | |
| 121 | expect(all).toHaveLength(2); | |
| 122 | const old = all.find((c) => c.id === id)!; | |
| 123 | expect(old.done).toBe(true); | |
| 124 | expect(old.ok).toBe(false); | |
| 125 | expect(old.output).toContain("never answered"); | |
| 126 | expect(healWords(app.db, b, T0 + LOST_MS + 2000).join("\n")).toContain("never answered a start of hunting"); | |
| 127 | }); | |
| 128 | ||
| 129 | test("a lost start past the limit is closed saying the forge did not retry", () => { | |
| 130 | const b = box({ join: false }); | |
| 131 | const id = queueCommand(app.db, b.id, "hunting", "start", "", "alice"); | |
| 132 | recordSync(app.db, b, {}, new Date(T0)); | |
| 133 | for (let i = 0; i < TRIES; i++) { | |
| 134 | app.db.query("INSERT INTO game_heal (box_id, service, kind, note, at) VALUES (?1, 'hunting', 'retry', 'earlier', ?2)").run(b.id, iso(T0 + LOST_MS - 10 * 60_000 + i)); | |
| 135 | } | |
| 136 | heal(app.db, b, COMPOSE, T0 + LOST_MS + 1000); | |
| 137 | expect(starts(b)).toHaveLength(1); | |
| 138 | const old = starts(b).find((c) => c.id === id)!; | |
| 139 | expect(old.ok).toBe(false); | |
| 140 | expect(old.output).toContain("did not start it again"); | |
| 141 | expect(old.output).not.toContain("forge started it again"); | |
| 142 | }); | |
| 143 | ||
| 144 | test("a silent agent is said, and that the forge cannot restart it", () => { | |
| 145 | const b = { id: "none", seenAt: iso(T0) }; | |
| 146 | expect(healWords(app.db, b, T0 + SILENT_MS - 1)).toEqual([]); | |
| 147 | expect(healWords(app.db, b, T0 + SILENT_MS)[0]).toContain("has not synced for 3 min"); | |
| 148 | }); | |
| 149 | }); |
test/tui-games.test.ts +17
| @@ -14,6 +14,7 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | ||
| 14 | 14 | import { createApp, type App } from "../src/app.ts"; |
| 15 | 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 16 | 16 | import { readCompose, serviceEnv } from "../src/games/compose.ts"; |
| 17 | import { WAKE_GRACE_MS } from "../src/games/heal.ts"; | |
| 17 | 18 | import { lentRunnerOf } from "../src/games/lend.ts"; |
| 18 | 19 | import { boxNamed, createGameBox, grant, grantsOf, recentCommands, recordSync } from "../src/games/store.ts"; |
| 19 | 20 | import { queueWorld } from "../src/games/worlds.ts"; |
| @@ -274,6 +275,22 @@ test("the terminal's box screen says what the page says about spare, unknown bef | ||
| 274 | 275 | tui.stop?.(); |
| 275 | 276 | }); |
| 276 | 277 | |
| 278 | test("the terminal's box screen says what the forge started again alone, as the page does (#17618)", async () => { | |
| 279 | const box = boxNamed(app.db, "net")!; | |
| 280 | const T = Date.now() - WAKE_GRACE_MS - 1000; | |
| 281 | app.db.query("INSERT INTO game_heal (box_id, service, kind, note, at) VALUES (?1, 'hunting', 'wake', 'a player joined and hunting stayed asleep (try 1 of 2)', ?2)").run(box.id, new Date(T).toISOString()); | |
| 282 | const { tui, session } = drive(alice, 200); | |
| 283 | try { | |
| 284 | tui.ctx.push(gameBoxScreen(session, tui.ctx, box)); | |
| 285 | await settleApp(tui); | |
| 286 | const text = tui.screen.toText(); | |
| 287 | expect(text).toContain("Started hunting again, alone"); | |
| 288 | expect(text).toContain("never changed alone"); | |
| 289 | } finally { | |
| 290 | tui.stop?.(); | |
| 291 | } | |
| 292 | }); | |
| 293 | ||
| 277 | 294 | test("the terminal's box screen shows the switch Run checks while servers sleep and l flips it, for an owner alone (#17577)", async () => { |
| 278 | 295 | const box = boxNamed(app.db, "net")!; |
| 279 | 296 | const owner = drive(alice, 200); |
new above
offline
The lost command is closed with 'the forge started it again' before startOnce decides. When startOnce returns false (inside GAP_MS, or TRIES used up and it gave up), that output is false and is what the person reads on the command row. Fix: call startOnce first and write the output from its result ('...the forge started it again' vs 'the agent never answered; the forge did not retry (limit reached)'), and add a test for the limit case.