Public: anyone can read this; the crew can change it.
An answer to /forge is kept until the box says it has it #17694
Merged 15h 26m ago, live in production
sold/game-told → main 0 commits
7 files
changed +98
−13
docs/features/games.md +1 −1
| @@ -87,7 +87,7 @@ Anyone else gets a line saying how to link, and nothing runs. Replies are RCON ` | ||
| 87 | 87 | |
| 88 | 88 | The sync only checks and keeps a command (`game_chat_job`); a plan, a commit or a message to the crew runs after it, one at a time for each box, and the next sync takes up what a restart left. The agent sends a batch of chat again under the same `chatId` until a sync is answered, and the forge skips one it has; a yes whose commit landed is not committed again. |
| 89 | 89 | |
| 90 | **Through the proxy (`/forge`, #17610).** The forge's side of a command on the proxy itself (`src/games/door.ts`); the plugin and the agent's part are not on a box yet. A request comes in the sync's `ask` (`{ id, uuid, name, server, words, confirms? }`, at most 20) and its answers go back in `told` (`{ id, uuid, text, confirm }`, at most 20, each handed out once, dropped after an hour); a quick poll (`light`) that says `told: true` gets them too. It runs the same rules as `!forge`: the link, the role and the limits; the UUID is the proxy's own. Each request is kept once by its `id` for a day, so one sent again does nothing. Whenever the sync carries `ask`, the answer's `asked` lists the ids the forge took, now or before; the agent deletes those and sends the rest again. Not taken: past the first 20, or no compose file could be read. One that names no player cannot be answered: it is taken, and the forge's log counts it. | |
| 90 | **Through the proxy (`/forge`, #17610).** The forge's side of a command on the proxy itself (`src/games/door.ts`); the plugin and the agent's part are not on a box yet. A request comes in the sync's `ask` (`{ id, uuid, name, server, words, confirms? }`, at most 20) and its answers go back in `told` (`{ n, id, uuid, text, confirm }`, at most 20, dropped after an hour); a quick poll (`light`) that says `told: true` gets them too. An agent that sends `got`, the numbers `n` it has (an empty list too), is handed each answer until it has it, and skips a number it had; one that never sends it, each once. A question's three minutes count from its first handing out: after the first of them it is handed out again only as words to ask again. It runs the same rules as `!forge`: the link, the role and the limits; the UUID is the proxy's own. Each request is kept once by its `id` for a day, so one sent again does nothing. Whenever the sync carries `ask`, the answer's `asked` lists the ids the forge took, now or before; the agent deletes those and sends the rest again. Not taken: past the first 20, or no compose file could be read. One that names no player cannot be answered: it is taken, and the forge's log counts it. | |
| 91 | 91 | |
| 92 | 92 | A server can show text whose click runs a command as the player, so a request that links a name, builds or drops a plan, or asks the crew changes nothing: it is answered with `confirm: true`, and the proxy shows the player a one-time code. Only a later request whose `confirms` names it acts, from the same UUID on the same box, once, within three minutes of the answer going to the box (the code itself lives two). A plan is built only at the door it was asked at: a `!forge yes` in chat never builds a plan made through the proxy, nor the other way round. Questions, help and the making of a plan need no code. |
| 93 | 93 |
src/core/migration-pins/20261202220002-game-told-at.pin added +1
| @@ -0,0 +1,1 @@ | ||
| 1 | da64ac550add0a65ddc4658f954ec65eecf514186f471bc8eedf773f2dd7efd1 |
src/core/migrations/20261202220002-game-told-at.ts added +5
src/games/door.ts +35 −6
| @@ -30,6 +30,7 @@ export interface Confirmed { | ||
| 30 | 30 | |
| 31 | 31 | /** The plugin's code lives two minutes from when it is shown; a minute more carries it here. */ |
| 32 | 32 | export const CONFIRM_MS = 3 * 60_000; |
| 33 | const CODE_MS = 2 * 60_000; | |
| 33 | 34 | const ASK_KEPT_MS = 24 * 3_600_000; |
| 34 | 35 | /** The plugin remembers a request for an hour: a later answer would be shown to nobody. */ |
| 35 | 36 | const TOLD_KEPT_MS = 3_600_000; |
| @@ -102,20 +103,48 @@ export function say(db: Database, box: Pick<GameBox, "id">, at: Where, lines: re | ||
| 102 | 103 | ).run(box.id, at.ask, lines.join("\n").replaceAll("!forge", "/forge").slice(0, 2_000), confirm === undefined ? 0 : 1, new Date().toISOString()); |
| 103 | 104 | } |
| 104 | 105 | |
| 105 | /** The answers for a box's agent to carry, each handed out once; a code's time starts here. */ | |
| 106 | export function toldTo(db: Database, box: GameBox, now = Date.now()): { id: string; uuid: string; text: string; confirm: boolean }[] { | |
| 106 | export interface Told { | |
| 107 | /** The answer's own number: one request can have two. */ | |
| 108 | readonly n: number; | |
| 109 | readonly id: string; | |
| 110 | readonly uuid: string; | |
| 111 | readonly text: string; | |
| 112 | readonly confirm: boolean; | |
| 113 | } | |
| 114 | ||
| 115 | /** Forgets the answers the box says it has; only ones it was handed. */ | |
| 116 | export function toldGot(db: Database, box: GameBox, got: unknown): void { | |
| 117 | if (!Array.isArray(got)) return; | |
| 118 | for (const n of got.slice(0, 100) as unknown[]) if (Number.isSafeInteger(n)) db.query("DELETE FROM game_told WHERE id = ?1 AND box_id = ?2 AND told_at != ''").run(n as number, box.id); | |
| 119 | } | |
| 120 | ||
| 121 | /** | |
| 122 | * The answers for a box's agent to carry; a code's time starts when it is first handed out, and no | |
| 123 | * later handing starts it again. An agent that sends `got` (the numbers it has, or none) is handed | |
| 124 | * each until it has it; one that never does, once. | |
| 125 | */ | |
| 126 | export function toldTo(db: Database, box: GameBox, now = Date.now(), got?: unknown): Told[] { | |
| 127 | const keeps = Array.isArray(got); | |
| 107 | 128 | return db.transaction(() => { |
| 129 | toldGot(db, box, got); | |
| 108 | 130 | db.query("DELETE FROM game_told WHERE created_at < ?1").run(new Date(now - TOLD_KEPT_MS).toISOString()); |
| 109 | 131 | const rows = db |
| 110 | 132 | .query< |
| 111 | { n: number; id: string; uuid: string; text: string; confirm: number }, | |
| 133 | { n: number; id: string; uuid: string; text: string; confirm: number; told_at: string }, | |
| 112 | 134 | [string, number] |
| 113 | >("SELECT id AS n, ask_id AS id, uuid, text, confirm FROM game_told WHERE box_id = ?1 ORDER BY game_told.id LIMIT ?2") | |
| 135 | >("SELECT id AS n, ask_id AS id, uuid, text, confirm, told_at FROM game_told WHERE box_id = ?1 ORDER BY game_told.id LIMIT ?2") | |
| 114 | 136 | .all(box.id, TOLD_A_SYNC); |
| 115 | 137 | for (const r of rows) { |
| 116 | db.query("DELETE FROM game_told WHERE id = ?1").run(r.n); | |
| 138 | if (!keeps) db.query("DELETE FROM game_told WHERE id = ?1").run(r.n); | |
| 139 | if (r.told_at !== "") continue; | |
| 140 | db.query("UPDATE game_told SET told_at = ?2 WHERE id = ?1").run(r.n, new Date(now).toISOString()); | |
| 117 | 141 | if (r.confirm === 1) db.query("UPDATE game_ask SET shown_at = ?3 WHERE box_id = ?1 AND id = ?2").run(box.id, r.id, new Date(now).toISOString()); |
| 118 | 142 | } |
| 119 | return rows.map(({ id, uuid, text, confirm }) => ({ id, uuid, text, confirm: confirm === 1 })); | |
| 143 | return rows.map(({ n, id, uuid, text, confirm, told_at }) => | |
| 144 | // The plugin's code would outlive what the forge still takes. | |
| 145 | confirm === 1 && told_at !== "" && now - Date.parse(told_at) > CONFIRM_MS - CODE_MS | |
| 146 | ? { n, id, uuid, text: "The forge's question reached you too late to answer: ask again.", confirm: false } | |
| 147 | : { n, id, uuid, text, confirm: confirm === 1 }, | |
| 148 | ); | |
| 120 | 149 | })(); |
| 121 | 150 | } |
src/games/store.ts +2
| @@ -575,6 +575,8 @@ export interface SyncBody { | ||
| 575 | 575 | readonly light?: boolean; |
| 576 | 576 | /** In a light sync: the agent reads `told` in its answer. */ |
| 577 | 577 | readonly told?: unknown; |
| 578 | /** The numbers of the `told` answers the agent has; an agent that sends it is handed each until then. */ | |
| 579 | readonly got?: unknown; | |
| 578 | 580 | /** |
| 579 | 581 | * The agent's last full sync did not get an answer: its log lines may be ones the forge already |
| 580 | 582 | * kept. |
src/http/games_routes.ts +5 −4
| @@ -10,7 +10,7 @@ import { auditShellResults } from "../games/shell.ts"; | ||
| 10 | 10 | import { recordMetrics } from "../games/metrics.ts"; |
| 11 | 11 | import { recordPeople } from "../games/people.ts"; |
| 12 | 12 | import { gameAsk, gameChat, runChat } from "../games/chat.ts"; |
| 13 | import { toldTo } from "../games/door.ts"; | |
| 13 | import { toldGot, toldTo } from "../games/door.ts"; | |
| 14 | 14 | import { packChecks } from "../games/pack_check.ts"; |
| 15 | 15 | import { lendAnswer } from "../games/lend.ts"; |
| 16 | 16 | import { recordProxyEvents, tellRefused } from "../games/player_events.ts"; |
| @@ -48,8 +48,9 @@ export async function gameAgentSync(ctx: Ctx): Promise<Response> { | ||
| 48 | 48 | if (body.light === true) { |
| 49 | 49 | auditShellResults(ctx.app, box, (body.results ?? []).map((r) => r.id)); |
| 50 | 50 | synced(ctx.app, box.id); |
| 51 | // Each answer is handed out once: only to an agent that says it reads them here. | |
| 52 | const told = body.told === true ? toldTo(ctx.app.db, box) : []; | |
| 51 | // Only to an agent that says it reads them here; what it got is heard either way. | |
| 52 | if (body.told !== true) toldGot(ctx.app.db, box, body.got); | |
| 53 | const told = body.told === true ? toldTo(ctx.app.db, box, Date.now(), body.got) : []; | |
| 53 | 54 | return json(told.length > 0 ? { commands, told } : { commands }); |
| 54 | 55 | } |
| 55 | 56 | // Every shell command the box answered, its output with it, into the audit. |
| @@ -80,7 +81,7 @@ export async function gameAgentSync(ctx: Ctx): Promise<Response> { | ||
| 80 | 81 | if (compose !== undefined && compose.sha !== (typeof body.composeSha === "string" ? body.composeSha : "")) reply["compose"] = signJarUrls(ctx.app, box, compose.text, ctx.url.origin); |
| 81 | 82 | // Which requests it took, whenever the box sent any: it deletes those and no other. |
| 82 | 83 | if (Array.isArray(body.ask)) reply["asked"] = asked; |
| 83 | const told = toldTo(ctx.app.db, box); | |
| 84 | const told = toldTo(ctx.app.db, box, Date.now(), body.got); | |
| 84 | 85 | if (told.length > 0) reply["told"] = told; |
| 85 | 86 | // The agent this forge runs: a box whose own differs fetches it (`gameAgentScript`) and restarts. |
| 86 | 87 | reply["agentSha"] = AGENT_SHA; |
test/game-door.test.ts +49 −2
| @@ -98,7 +98,7 @@ afterAll(async () => { | ||
| 98 | 98 | |
| 99 | 99 | test("a link waits for the player's confirmation, then binds the proxy's UUID", async () => { |
| 100 | 100 | const first = request(`link ${newLinkCode(app, ada)}`); |
| 101 | expect(await send([first])).toEqual([{ id: first.id, uuid: ADA, text: "Link ada to the forge account ada?", confirm: true }]); | |
| 101 | expect(await send([first])).toEqual([{ n: expect.any(Number), id: first.id, uuid: ADA, text: "Link ada to the forge account ada?", confirm: true }]); | |
| 102 | 102 | expect(linksOf(app, box.id)).toEqual([]); |
| 103 | 103 | const yes = request("yes", { confirms: first.id }); |
| 104 | 104 | const [done] = await send([yes]); |
| @@ -125,7 +125,7 @@ test("every request in a sync is answered, a repeated refusal too", async () => | ||
| 125 | 125 | |
| 126 | 126 | test("crew work starts only on the same player's confirmation, on the same box, once, in time", async () => { |
| 127 | 127 | const before = request("when does the hub restart?"); |
| 128 | expect(await send([before])).toEqual([{ id: before.id, uuid: ADA, text: 'Ask the crew: "when does the hub restart?"?', confirm: true }]); | |
| 128 | expect(await send([before])).toEqual([{ n: expect.any(Number), id: before.id, uuid: ADA, text: 'Ask the crew: "when does the hub restart?"?', confirm: true }]); | |
| 129 | 129 | const refused = "That is no longer waiting for your yes: ask again."; |
| 130 | 130 | const confirm = (more: Record<string, unknown> = {}) => request("yes", { confirms: before.id, ...more }); |
| 131 | 131 | expect((await send([confirm({ uuid: EVE, name: "eve" })]))[0]!.text).toBe(refused); |
| @@ -219,6 +219,44 @@ test("a request over the account's limit is taken and its player is told", async | ||
| 219 | 219 | expect(told.some((a) => /commands a minute|try again/i.test(a.text))).toBe(true); |
| 220 | 220 | }); |
| 221 | 221 | |
| 222 | test("an agent that says what it got is told again until it did; one that never says is told once", async () => { | |
| 223 | const ask = request("help", { uuid: EVE, name: "eve" }); | |
| 224 | gameAsk(app, box, [ask], COMPOSE, "http://forge.test", now); | |
| 225 | const [first] = toldTo(app.db, box, now, []); | |
| 226 | expect(first).toMatchObject({ id: ask.id, confirm: false }); | |
| 227 | // The answer was lost: the same one, under the same number. | |
| 228 | expect(toldTo(app.db, box, now + 1_000, [])).toEqual([first!]); | |
| 229 | // Another box's word, or a number that is none, confirms nothing. | |
| 230 | toldTo(app.db, other, now, [first!.n]); | |
| 231 | expect(toldTo(app.db, box, now, ["x", -1, null, 1.5])).toEqual([first!]); | |
| 232 | expect(toldTo(app.db, box, now, [first!.n])).toEqual([]); | |
| 233 | // Never confirmed: gone after an hour. | |
| 234 | gameAsk(app, box, [request("help", { uuid: EVE, name: "eve" })], COMPOSE, "http://forge.test", now); | |
| 235 | expect(toldTo(app.db, box, now, [])).toHaveLength(1); | |
| 236 | expect(toldTo(app.db, box, Date.now() + 3_700_000, [])).toEqual([]); | |
| 237 | // An older agent. | |
| 238 | gameAsk(app, box, [request("help", { uuid: EVE, name: "eve" })], COMPOSE, "http://forge.test", now); | |
| 239 | expect(toldTo(app.db, box, now)).toHaveLength(1); | |
| 240 | expect(toldTo(app.db, box, now)).toEqual([]); | |
| 241 | }); | |
| 242 | ||
| 243 | test("a question told again keeps its first three minutes, and once its code could outlive them it only says to ask again", async () => { | |
| 244 | const ask = request(`link ${newLinkCode(app, ada)}`); | |
| 245 | gameAsk(app, box, [ask], COMPOSE, "http://forge.test", now); | |
| 246 | const [first] = toldTo(app.db, box, now, []); | |
| 247 | expect(first).toMatchObject({ id: ask.id, confirm: true }); | |
| 248 | const shown = () => app.db.query<{ shown_at: string }, [string]>("SELECT shown_at FROM game_ask WHERE id = ?1").get(ask.id)!.shown_at; | |
| 249 | expect(shown()).toBe(new Date(now).toISOString()); | |
| 250 | expect(toldTo(app.db, box, now + 50_000, [])).toEqual([first!]); | |
| 251 | const [late] = toldTo(app.db, box, now + 61_000, []); | |
| 252 | expect(late).toMatchObject({ n: first!.n, id: ask.id, confirm: false }); | |
| 253 | expect(late!.text).toContain("ask again"); | |
| 254 | expect(shown()).toBe(new Date(now).toISOString()); | |
| 255 | now += CONFIRM_MS + 1; | |
| 256 | const [no] = await send([request("yes", { confirms: ask.id })]).then((a) => a.filter((x) => x.id !== ask.id)); | |
| 257 | expect(no!.text).toBe("That is no longer waiting for your yes: ask again."); | |
| 258 | }); | |
| 259 | ||
| 222 | 260 | test("the sync carries a request in and its answer out, once", async () => { |
| 223 | 261 | const ask = request("help"); |
| 224 | 262 | const sync = async (more: object) => { |
| @@ -244,4 +282,13 @@ test("the sync carries a request in and its answer out, once", async () => { | ||
| 244 | 282 | expect((await sync({ light: true })).told).toBeUndefined(); |
| 245 | 283 | expect((await sync({ light: true, told: true })).told).toEqual([next.id]); |
| 246 | 284 | expect((await sync({ light: true, told: true })).told).toBeUndefined(); |
| 285 | ||
| 286 | // Told again until `got`, in either sync. | |
| 287 | const kept = request("help"); | |
| 288 | expect((await sync({ ask: [kept], got: [] })).told).toEqual([kept.id]); | |
| 289 | expect((await sync({ light: true, told: true, got: [] })).told).toEqual([kept.id]); | |
| 290 | const n = app.db.query<{ id: number }, [string]>("SELECT id FROM game_told WHERE ask_id = ?1").get(kept.id)!.id; | |
| 291 | // A quick poll confirms whether or not it reads answers. | |
| 292 | expect((await sync({ light: true, got: [n] })).told).toBeUndefined(); | |
| 293 | expect((await sync({ got: [] })).told).toBeUndefined(); | |
| 247 | 294 | }); |
new above
offline