Public: anyone can read this; the crew can change it.

Neutral public names, step 1 of 2 (expand): the new name beside the old for the game_* tables, the games MCP tool, connection.kind and the game and design: minecraft plan options #18513

opened this

Merged 1h 55m ago, live in production

mason/forge:mason/18501-neutral-public-names-step-1-of-2-expand → main 0 commits fork

  • Merged. mason/18501-neutral-public-names-step-1-of-2-expand was merged into main as 6368d15.

Review

Sign in to review this change.
12 files changed +478 −12
docs/MCP.md +21
@@ -375,6 +375,27 @@ can call `apps` and is refused `tell`.
375375
376376 `src/http/mcp.ts`, `src/http/mcp/tools/`, `src/actions/tell.ts`, `test/mcp-forge-*.test.ts`.
377377
378 ## Neutral names beside the game ones (#18501, step 1 of 2)
379
380 Nothing here is a new tool and nothing old is removed; step 2 retires the old
381 spellings once the neutral ones are in use.
382
383 - `games`: `machine` is `box`, `container` is `service`, and the action
384 `machines` is `list`. The old names win when both are sent.
385 - `infra` `plan`: `want: {kind: "machine", ...}` is `{game: true, ...}`, and
386 `{kind: "minecraft", ...}` is `{design: "minecraft", ...}`.
387 - Connections (`POST /api/v1/connections`, `/lookup`): `{kind: "player",
388 ident}` is `{minecraft: ident}`, `{kind: "email", ident}` is `{email: ident}`.
389 A connection still answers `kind: "minecraft"`.
390 - The database: each `game_*` table is also a view, `machine` (over the
391 old box table), `container` (over the old service table) and
392 `machine_<rest>` for the others, with INSTEAD OF triggers, so a
393 write through either name reaches the same row. The `game_*` tables are
394 untouched, so the previous release runs on the same file.
395
396 `src/games/neutral_args.ts`, `src/core/migrations/20261206175001-neutral-machine-names.ts`,
397 `test/neutral-machine-names.test.ts`.
398
378399 ## `plan`
379400
380401 Opens several small issues and their links in one transaction. Each item is
src/core/identities.ts +9
@@ -35,6 +35,15 @@ export const AUDIT_KEEP_MS = 24 * 3_600_000;
3535
3636 export type Kind = "email" | "minecraft";
3737
38 /**
39 * The kind a caller names, as the one kept (#18501, step 1 of 2): `player` is the neutral name of
40 * `minecraft`, and either is taken; what is stored and answered stays `minecraft`. Undefined when
41 * it names neither kind.
42 */
43 export function kindNamed(raw: unknown): Kind | undefined {
44 return raw === "email" ? "email" : raw === "minecraft" || raw === "player" ? "minecraft" : undefined;
45 }
46
3847 export interface Connection {
3948 readonly id: string;
4049 readonly kind: Kind;
src/core/migration-pins/20261206175001-neutral-machine-names.pin added +1
@@ -0,0 +1,1 @@
1 f9fd5355d58e858f2cf5afbfe28b105d7748a5056fd5d840cf122222e175be72
src/core/migrations/20261206175001-neutral-machine-names.ts added +269
@@ -0,0 +1,269 @@
1 // The neutral names beside the game_* ones, step 1 of 2 (#18501): each game_* table is also served
2 // as a view named for what it holds: a machine (game_box), a container (game_service), and
3 // machine_<rest> for the tables that hang off a machine. A view reads the table's rows as they
4 // are, and INSTEAD OF triggers make an insert, update or delete through either name reach the
5 // same row. Nothing is renamed or dropped: the previous release, which knows only the game_*
6 // tables, runs against this database unchanged, and ignores the views. Step 2 will move the code
7 // to the new names; a test fails when a game_* table gains a column its triggers do not carry.
8 export default `
9 CREATE VIEW IF NOT EXISTS machine_agent_seen AS SELECT * FROM game_agent_seen;
10 CREATE TRIGGER IF NOT EXISTS machine_agent_seen_insert INSTEAD OF INSERT ON machine_agent_seen BEGIN
11 INSERT INTO game_agent_seen (sha, box_id, cpu_json, rolled_back) VALUES (NEW.sha, NEW.box_id, COALESCE(NEW.cpu_json, '[]'), COALESCE(NEW.rolled_back, ''));
12 END;
13 CREATE TRIGGER IF NOT EXISTS machine_agent_seen_update INSTEAD OF UPDATE ON machine_agent_seen BEGIN
14 UPDATE game_agent_seen SET sha = NEW.sha, box_id = NEW.box_id, cpu_json = NEW.cpu_json, rolled_back = NEW.rolled_back WHERE sha IS OLD.sha AND box_id IS OLD.box_id;
15 END;
16 CREATE TRIGGER IF NOT EXISTS machine_agent_seen_delete INSTEAD OF DELETE ON machine_agent_seen BEGIN
17 DELETE FROM game_agent_seen WHERE sha IS OLD.sha AND box_id IS OLD.box_id;
18 END;
19 CREATE VIEW IF NOT EXISTS machine_agent_watch AS SELECT * FROM game_agent_watch;
20 CREATE TRIGGER IF NOT EXISTS machine_agent_watch_insert INSTEAD OF INSERT ON machine_agent_watch BEGIN
21 INSERT INTO game_agent_watch (sha, pull_number, started_at, posted_at, box_ids) VALUES (NEW.sha, NEW.pull_number, NEW.started_at, COALESCE(NEW.posted_at, ''), COALESCE(NEW.box_ids, '[]'));
22 END;
23 CREATE TRIGGER IF NOT EXISTS machine_agent_watch_update INSTEAD OF UPDATE ON machine_agent_watch BEGIN
24 UPDATE game_agent_watch SET sha = NEW.sha, pull_number = NEW.pull_number, started_at = NEW.started_at, posted_at = NEW.posted_at, box_ids = NEW.box_ids WHERE sha IS OLD.sha;
25 END;
26 CREATE TRIGGER IF NOT EXISTS machine_agent_watch_delete INSTEAD OF DELETE ON machine_agent_watch BEGIN
27 DELETE FROM game_agent_watch WHERE sha IS OLD.sha;
28 END;
29 CREATE VIEW IF NOT EXISTS machine_ask AS SELECT * FROM game_ask;
30 CREATE TRIGGER IF NOT EXISTS machine_ask_insert INSTEAD OF INSERT ON machine_ask BEGIN
31 INSERT INTO game_ask (box_id, id, uuid, player, service, action, words, shown_at, created_at) VALUES (NEW.box_id, NEW.id, NEW.uuid, NEW.player, NEW.service, COALESCE(NEW.action, ''), COALESCE(NEW.words, ''), COALESCE(NEW.shown_at, ''), NEW.created_at);
32 END;
33 CREATE TRIGGER IF NOT EXISTS machine_ask_update INSTEAD OF UPDATE ON machine_ask BEGIN
34 UPDATE game_ask SET box_id = NEW.box_id, id = NEW.id, uuid = NEW.uuid, player = NEW.player, service = NEW.service, action = NEW.action, words = NEW.words, shown_at = NEW.shown_at, created_at = NEW.created_at WHERE box_id IS OLD.box_id AND id IS OLD.id;
35 END;
36 CREATE TRIGGER IF NOT EXISTS machine_ask_delete INSTEAD OF DELETE ON machine_ask BEGIN
37 DELETE FROM game_ask WHERE box_id IS OLD.box_id AND id IS OLD.id;
38 END;
39 CREATE VIEW IF NOT EXISTS machine AS SELECT * FROM game_box;
40 CREATE TRIGGER IF NOT EXISTS machine_insert INSTEAD OF INSERT ON machine BEGIN
41 INSERT INTO game_box (id, name, token_hash, repo, branch, path, pinned_id, place, monthly_cents, host_json, compose_sha, seen_at, created_at, boot, diag_json, snapshots_json, network_json, restored_from, jars, lend_why, last_tps, last_mspt) VALUES (NEW.id, NEW.name, NEW.token_hash, COALESCE(NEW.repo, 'trevato/mc-network'), COALESCE(NEW.branch, 'main'), COALESCE(NEW.path, 'docker-compose.yml'), COALESCE(NEW.pinned_id, ''), COALESCE(NEW.place, ''), COALESCE(NEW.monthly_cents, 0), COALESCE(NEW.host_json, '{}'), COALESCE(NEW.compose_sha, ''), COALESCE(NEW.seen_at, ''), NEW.created_at, COALESCE(NEW.boot, ''), COALESCE(NEW.diag_json, '{}'), COALESCE(NEW.snapshots_json, '{}'), COALESCE(NEW.network_json, '{}'), COALESCE(NEW.restored_from, ''), COALESCE(NEW.jars, 0), COALESCE(NEW.lend_why, ''), NEW.last_tps, NEW.last_mspt);
42 END;
43 CREATE TRIGGER IF NOT EXISTS machine_update INSTEAD OF UPDATE ON machine BEGIN
44 UPDATE game_box SET id = NEW.id, name = NEW.name, token_hash = NEW.token_hash, repo = NEW.repo, branch = NEW.branch, path = NEW.path, pinned_id = NEW.pinned_id, place = NEW.place, monthly_cents = NEW.monthly_cents, host_json = NEW.host_json, compose_sha = NEW.compose_sha, seen_at = NEW.seen_at, created_at = NEW.created_at, boot = NEW.boot, diag_json = NEW.diag_json, snapshots_json = NEW.snapshots_json, network_json = NEW.network_json, restored_from = NEW.restored_from, jars = NEW.jars, lend_why = NEW.lend_why, last_tps = NEW.last_tps, last_mspt = NEW.last_mspt WHERE id IS OLD.id;
45 END;
46 CREATE TRIGGER IF NOT EXISTS machine_delete INSTEAD OF DELETE ON machine BEGIN
47 DELETE FROM game_box WHERE id IS OLD.id;
48 END;
49 CREATE VIEW IF NOT EXISTS machine_chat AS SELECT * FROM game_chat;
50 CREATE TRIGGER IF NOT EXISTS machine_chat_insert INSTEAD OF INSERT ON machine_chat BEGIN
51 INSERT INTO game_chat (id, box_id, service, player, user_id, kind, words, outcome, at, door, write_window) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.player, COALESCE(NEW.user_id, ''), NEW.kind, NEW.words, NEW.outcome, NEW.at, COALESCE(NEW.door, ''), COALESCE(NEW.write_window, ''));
52 END;
53 CREATE TRIGGER IF NOT EXISTS machine_chat_update INSTEAD OF UPDATE ON machine_chat BEGIN
54 UPDATE game_chat SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, player = NEW.player, user_id = NEW.user_id, kind = NEW.kind, words = NEW.words, outcome = NEW.outcome, at = NEW.at, door = NEW.door, write_window = NEW.write_window WHERE id IS OLD.id;
55 END;
56 CREATE TRIGGER IF NOT EXISTS machine_chat_delete INSTEAD OF DELETE ON machine_chat BEGIN
57 DELETE FROM game_chat WHERE id IS OLD.id;
58 END;
59 CREATE VIEW IF NOT EXISTS machine_chat_batch AS SELECT * FROM game_chat_batch;
60 CREATE TRIGGER IF NOT EXISTS machine_chat_batch_insert INSTEAD OF INSERT ON machine_chat_batch BEGIN
61 INSERT INTO game_chat_batch (box_id, id) VALUES (NEW.box_id, NEW.id);
62 END;
63 CREATE TRIGGER IF NOT EXISTS machine_chat_batch_update INSTEAD OF UPDATE ON machine_chat_batch BEGIN
64 UPDATE game_chat_batch SET box_id = NEW.box_id, id = NEW.id WHERE box_id IS OLD.box_id;
65 END;
66 CREATE TRIGGER IF NOT EXISTS machine_chat_batch_delete INSTEAD OF DELETE ON machine_chat_batch BEGIN
67 DELETE FROM game_chat_batch WHERE box_id IS OLD.box_id;
68 END;
69 CREATE VIEW IF NOT EXISTS machine_chat_job AS SELECT * FROM game_chat_job;
70 CREATE TRIGGER IF NOT EXISTS machine_chat_job_insert INSTEAD OF INSERT ON machine_chat_job BEGIN
71 INSERT INTO game_chat_job (id, box_id, service, player, user_id, kind, words, sha, name, conversation, origin, audit_id, created_at, ask_id) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.player, NEW.user_id, NEW.kind, NEW.words, COALESCE(NEW.sha, ''), COALESCE(NEW.name, ''), COALESCE(NEW.conversation, ''), COALESCE(NEW.origin, ''), NEW.audit_id, NEW.created_at, COALESCE(NEW.ask_id, ''));
72 END;
73 CREATE TRIGGER IF NOT EXISTS machine_chat_job_update INSTEAD OF UPDATE ON machine_chat_job BEGIN
74 UPDATE game_chat_job SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, player = NEW.player, user_id = NEW.user_id, kind = NEW.kind, words = NEW.words, sha = NEW.sha, name = NEW.name, conversation = NEW.conversation, origin = NEW.origin, audit_id = NEW.audit_id, created_at = NEW.created_at, ask_id = NEW.ask_id WHERE id IS OLD.id;
75 END;
76 CREATE TRIGGER IF NOT EXISTS machine_chat_job_delete INSTEAD OF DELETE ON machine_chat_job BEGIN
77 DELETE FROM game_chat_job WHERE id IS OLD.id;
78 END;
79 CREATE VIEW IF NOT EXISTS machine_chat_pending AS SELECT * FROM game_chat_pending;
80 CREATE TRIGGER IF NOT EXISTS machine_chat_pending_insert INSTEAD OF INSERT ON machine_chat_pending BEGIN
81 INSERT INTO game_chat_pending (box_id, player, service, user_id, words, created_at, sha, ask_id) VALUES (NEW.box_id, NEW.player, NEW.service, NEW.user_id, NEW.words, NEW.created_at, COALESCE(NEW.sha, ''), COALESCE(NEW.ask_id, ''));
82 END;
83 CREATE TRIGGER IF NOT EXISTS machine_chat_pending_update INSTEAD OF UPDATE ON machine_chat_pending BEGIN
84 UPDATE game_chat_pending SET box_id = NEW.box_id, player = NEW.player, service = NEW.service, user_id = NEW.user_id, words = NEW.words, created_at = NEW.created_at, sha = NEW.sha, ask_id = NEW.ask_id WHERE box_id IS OLD.box_id AND player IS OLD.player;
85 END;
86 CREATE TRIGGER IF NOT EXISTS machine_chat_pending_delete INSTEAD OF DELETE ON machine_chat_pending BEGIN
87 DELETE FROM game_chat_pending WHERE box_id IS OLD.box_id AND player IS OLD.player;
88 END;
89 CREATE VIEW IF NOT EXISTS machine_client_link AS SELECT * FROM game_client_link;
90 CREATE TRIGGER IF NOT EXISTS machine_client_link_insert INSTEAD OF INSERT ON machine_client_link BEGIN
91 INSERT INTO game_client_link (id, box_id, service, token_hash, created_by, created_at, expires_at, revoked_at) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.token_hash, NEW.created_by, NEW.created_at, NEW.expires_at, COALESCE(NEW.revoked_at, ''));
92 END;
93 CREATE TRIGGER IF NOT EXISTS machine_client_link_update INSTEAD OF UPDATE ON machine_client_link BEGIN
94 UPDATE game_client_link SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, token_hash = NEW.token_hash, created_by = NEW.created_by, created_at = NEW.created_at, expires_at = NEW.expires_at, revoked_at = NEW.revoked_at WHERE id IS OLD.id;
95 END;
96 CREATE TRIGGER IF NOT EXISTS machine_client_link_delete INSTEAD OF DELETE ON machine_client_link BEGIN
97 DELETE FROM game_client_link WHERE id IS OLD.id;
98 END;
99 CREATE VIEW IF NOT EXISTS machine_command AS SELECT * FROM game_command;
100 CREATE TRIGGER IF NOT EXISTS machine_command_insert INSTEAD OF INSERT ON machine_command BEGIN
101 INSERT INTO game_command (id, box_id, service, kind, text, by, created_at, handed_at, done_at, ok, output, after_log) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.kind, COALESCE(NEW.text, ''), COALESCE(NEW.by, ''), NEW.created_at, COALESCE(NEW.handed_at, ''), COALESCE(NEW.done_at, ''), NEW.ok, COALESCE(NEW.output, ''), NEW.after_log);
102 END;
103 CREATE TRIGGER IF NOT EXISTS machine_command_update INSTEAD OF UPDATE ON machine_command BEGIN
104 UPDATE game_command SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, kind = NEW.kind, text = NEW.text, by = NEW.by, created_at = NEW.created_at, handed_at = NEW.handed_at, done_at = NEW.done_at, ok = NEW.ok, output = NEW.output, after_log = NEW.after_log WHERE id IS OLD.id;
105 END;
106 CREATE TRIGGER IF NOT EXISTS machine_command_delete INSTEAD OF DELETE ON machine_command BEGIN
107 DELETE FROM game_command WHERE id IS OLD.id;
108 END;
109 CREATE VIEW IF NOT EXISTS machine_door AS SELECT * FROM game_door;
110 CREATE TRIGGER IF NOT EXISTS machine_door_insert INSTEAD OF INSERT ON machine_door BEGIN
111 INSERT INTO game_door (box_id, minutes, opened_at, open_until, opened_by, locked_at, locked_by, lock_on_leave) VALUES (NEW.box_id, COALESCE(NEW.minutes, 60), COALESCE(NEW.opened_at, ''), COALESCE(NEW.open_until, ''), COALESCE(NEW.opened_by, ''), COALESCE(NEW.locked_at, ''), COALESCE(NEW.locked_by, ''), COALESCE(NEW.lock_on_leave, 0));
112 END;
113 CREATE TRIGGER IF NOT EXISTS machine_door_update INSTEAD OF UPDATE ON machine_door BEGIN
114 UPDATE game_door SET box_id = NEW.box_id, minutes = NEW.minutes, opened_at = NEW.opened_at, open_until = NEW.open_until, opened_by = NEW.opened_by, locked_at = NEW.locked_at, locked_by = NEW.locked_by, lock_on_leave = NEW.lock_on_leave WHERE box_id IS OLD.box_id;
115 END;
116 CREATE TRIGGER IF NOT EXISTS machine_door_delete INSTEAD OF DELETE ON machine_door BEGIN
117 DELETE FROM game_door WHERE box_id IS OLD.box_id;
118 END;
119 CREATE VIEW IF NOT EXISTS machine_grant AS SELECT * FROM game_grant;
120 CREATE TRIGGER IF NOT EXISTS machine_grant_insert INSTEAD OF INSERT ON machine_grant BEGIN
121 INSERT INTO game_grant (box_id, user_id, role, service) VALUES (NEW.box_id, NEW.user_id, NEW.role, COALESCE(NEW.service, ''));
122 END;
123 CREATE TRIGGER IF NOT EXISTS machine_grant_update INSTEAD OF UPDATE ON machine_grant BEGIN
124 UPDATE game_grant SET box_id = NEW.box_id, user_id = NEW.user_id, role = NEW.role, service = NEW.service WHERE box_id IS OLD.box_id AND user_id IS OLD.user_id AND service IS OLD.service;
125 END;
126 CREATE TRIGGER IF NOT EXISTS machine_grant_delete INSTEAD OF DELETE ON machine_grant BEGIN
127 DELETE FROM game_grant WHERE box_id IS OLD.box_id AND user_id IS OLD.user_id AND service IS OLD.service;
128 END;
129 CREATE VIEW IF NOT EXISTS machine_heal AS SELECT * FROM game_heal;
130 CREATE TRIGGER IF NOT EXISTS machine_heal_insert INSTEAD OF INSERT ON machine_heal BEGIN
131 INSERT INTO game_heal (id, box_id, service, kind, note, at) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.kind, NEW.note, NEW.at);
132 END;
133 CREATE TRIGGER IF NOT EXISTS machine_heal_update INSTEAD OF UPDATE ON machine_heal BEGIN
134 UPDATE game_heal SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, kind = NEW.kind, note = NEW.note, at = NEW.at WHERE id IS OLD.id;
135 END;
136 CREATE TRIGGER IF NOT EXISTS machine_heal_delete INSTEAD OF DELETE ON machine_heal BEGIN
137 DELETE FROM game_heal WHERE id IS OLD.id;
138 END;
139 CREATE VIEW IF NOT EXISTS machine_jar AS SELECT * FROM game_jar;
140 CREATE TRIGGER IF NOT EXISTS machine_jar_insert INSTEAD OF INSERT ON machine_jar BEGIN
141 INSERT INTO game_jar (id, box_id, name, sha256, bytes, created_at) VALUES (NEW.id, NEW.box_id, NEW.name, NEW.sha256, NEW.bytes, NEW.created_at);
142 END;
143 CREATE TRIGGER IF NOT EXISTS machine_jar_update INSTEAD OF UPDATE ON machine_jar BEGIN
144 UPDATE game_jar SET id = NEW.id, box_id = NEW.box_id, name = NEW.name, sha256 = NEW.sha256, bytes = NEW.bytes, created_at = NEW.created_at WHERE id IS OLD.id;
145 END;
146 CREATE TRIGGER IF NOT EXISTS machine_jar_delete INSTEAD OF DELETE ON machine_jar BEGIN
147 DELETE FROM game_jar WHERE id IS OLD.id;
148 END;
149 CREATE VIEW IF NOT EXISTS machine_link AS SELECT * FROM game_link;
150 CREATE TRIGGER IF NOT EXISTS machine_link_insert INSTEAD OF INSERT ON machine_link BEGIN
151 INSERT INTO game_link (box_id, player, user_id, created_at, uuid) VALUES (NEW.box_id, NEW.player, NEW.user_id, NEW.created_at, COALESCE(NEW.uuid, ''));
152 END;
153 CREATE TRIGGER IF NOT EXISTS machine_link_update INSTEAD OF UPDATE ON machine_link BEGIN
154 UPDATE game_link SET box_id = NEW.box_id, player = NEW.player, user_id = NEW.user_id, created_at = NEW.created_at, uuid = NEW.uuid WHERE box_id IS OLD.box_id AND player IS OLD.player;
155 END;
156 CREATE TRIGGER IF NOT EXISTS machine_link_delete INSTEAD OF DELETE ON machine_link BEGIN
157 DELETE FROM game_link WHERE box_id IS OLD.box_id AND player IS OLD.player;
158 END;
159 CREATE VIEW IF NOT EXISTS machine_link_code AS SELECT * FROM game_link_code;
160 CREATE TRIGGER IF NOT EXISTS machine_link_code_insert INSTEAD OF INSERT ON machine_link_code BEGIN
161 INSERT INTO game_link_code (code_hash, user_id, expires_at) VALUES (NEW.code_hash, NEW.user_id, NEW.expires_at);
162 END;
163 CREATE TRIGGER IF NOT EXISTS machine_link_code_update INSTEAD OF UPDATE ON machine_link_code BEGIN
164 UPDATE game_link_code SET code_hash = NEW.code_hash, user_id = NEW.user_id, expires_at = NEW.expires_at WHERE code_hash IS OLD.code_hash;
165 END;
166 CREATE TRIGGER IF NOT EXISTS machine_link_code_delete INSTEAD OF DELETE ON machine_link_code BEGIN
167 DELETE FROM game_link_code WHERE code_hash IS OLD.code_hash;
168 END;
169 CREATE VIEW IF NOT EXISTS machine_log AS SELECT * FROM game_log;
170 CREATE TRIGGER IF NOT EXISTS machine_log_insert INSTEAD OF INSERT ON machine_log BEGIN
171 INSERT INTO game_log (id, box_id, service, line, at) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.line, NEW.at);
172 END;
173 CREATE TRIGGER IF NOT EXISTS machine_log_update INSTEAD OF UPDATE ON machine_log BEGIN
174 UPDATE game_log SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, line = NEW.line, at = NEW.at WHERE id IS OLD.id;
175 END;
176 CREATE TRIGGER IF NOT EXISTS machine_log_delete INSTEAD OF DELETE ON machine_log BEGIN
177 DELETE FROM game_log WHERE id IS OLD.id;
178 END;
179 CREATE VIEW IF NOT EXISTS machine_map_check AS SELECT * FROM game_map_check;
180 CREATE TRIGGER IF NOT EXISTS machine_map_check_insert INSTEAD OF INSERT ON machine_map_check BEGIN
181 INSERT INTO game_map_check (box_id, service, at, ok, says) VALUES (NEW.box_id, NEW.service, NEW.at, NEW.ok, COALESCE(NEW.says, ''));
182 END;
183 CREATE TRIGGER IF NOT EXISTS machine_map_check_update INSTEAD OF UPDATE ON machine_map_check BEGIN
184 UPDATE game_map_check SET box_id = NEW.box_id, service = NEW.service, at = NEW.at, ok = NEW.ok, says = NEW.says WHERE box_id IS OLD.box_id AND service IS OLD.service;
185 END;
186 CREATE TRIGGER IF NOT EXISTS machine_map_check_delete INSTEAD OF DELETE ON machine_map_check BEGIN
187 DELETE FROM game_map_check WHERE box_id IS OLD.box_id AND service IS OLD.service;
188 END;
189 CREATE VIEW IF NOT EXISTS machine_metric AS SELECT * FROM game_metric;
190 CREATE TRIGGER IF NOT EXISTS machine_metric_insert INSTEAD OF INSERT ON machine_metric BEGIN
191 INSERT INTO game_metric (box_id, service, minute, n, players_max, players_last, players_n, tps_sum, tps_min, tps_n, mspt_sum, mspt_max, mspt_n, cpu_sum, mem_sum, errors, warns, joins, leaves) VALUES (NEW.box_id, NEW.service, NEW.minute, COALESCE(NEW.n, 0), COALESCE(NEW.players_max, 0), COALESCE(NEW.players_last, 0), COALESCE(NEW.players_n, 0), COALESCE(NEW.tps_sum, 0), NEW.tps_min, COALESCE(NEW.tps_n, 0), COALESCE(NEW.mspt_sum, 0), NEW.mspt_max, COALESCE(NEW.mspt_n, 0), COALESCE(NEW.cpu_sum, 0), COALESCE(NEW.mem_sum, 0), COALESCE(NEW.errors, 0), COALESCE(NEW.warns, 0), COALESCE(NEW.joins, 0), COALESCE(NEW.leaves, 0));
192 END;
193 CREATE TRIGGER IF NOT EXISTS machine_metric_update INSTEAD OF UPDATE ON machine_metric BEGIN
194 UPDATE game_metric SET box_id = NEW.box_id, service = NEW.service, minute = NEW.minute, n = NEW.n, players_max = NEW.players_max, players_last = NEW.players_last, players_n = NEW.players_n, tps_sum = NEW.tps_sum, tps_min = NEW.tps_min, tps_n = NEW.tps_n, mspt_sum = NEW.mspt_sum, mspt_max = NEW.mspt_max, mspt_n = NEW.mspt_n, cpu_sum = NEW.cpu_sum, mem_sum = NEW.mem_sum, errors = NEW.errors, warns = NEW.warns, joins = NEW.joins, leaves = NEW.leaves WHERE box_id IS OLD.box_id AND service IS OLD.service AND minute IS OLD.minute;
195 END;
196 CREATE TRIGGER IF NOT EXISTS machine_metric_delete INSTEAD OF DELETE ON machine_metric BEGIN
197 DELETE FROM game_metric WHERE box_id IS OLD.box_id AND service IS OLD.service AND minute IS OLD.minute;
198 END;
199 CREATE VIEW IF NOT EXISTS machine_player_event AS SELECT * FROM game_player_event;
200 CREATE TRIGGER IF NOT EXISTS machine_player_event_insert INSTEAD OF INSERT ON machine_player_event BEGIN
201 INSERT INTO game_player_event (id, box_id, service, player, kind, at, text, clock, told_at) VALUES (NEW.id, NEW.box_id, NEW.service, NEW.player, NEW.kind, NEW.at, COALESCE(NEW.text, ''), COALESCE(NEW.clock, ''), COALESCE(NEW.told_at, ''));
202 END;
203 CREATE TRIGGER IF NOT EXISTS machine_player_event_update INSTEAD OF UPDATE ON machine_player_event BEGIN
204 UPDATE game_player_event SET id = NEW.id, box_id = NEW.box_id, service = NEW.service, player = NEW.player, kind = NEW.kind, at = NEW.at, text = NEW.text, clock = NEW.clock, told_at = NEW.told_at WHERE id IS OLD.id;
205 END;
206 CREATE TRIGGER IF NOT EXISTS machine_player_event_delete INSTEAD OF DELETE ON machine_player_event BEGIN
207 DELETE FROM game_player_event WHERE id IS OLD.id;
208 END;
209 CREATE VIEW IF NOT EXISTS container AS SELECT * FROM game_service;
210 CREATE TRIGGER IF NOT EXISTS container_insert INSTEAD OF INSERT ON container BEGIN
211 INSERT INTO game_service (box_id, name, state, health, started_at, cpu_pct, mem_mib, seen_at, players, asleep, online_json, people_json, player_set_json, up_since, mem_limit_mib, cpu_limit, online_at) VALUES (NEW.box_id, NEW.name, COALESCE(NEW.state, ''), COALESCE(NEW.health, ''), COALESCE(NEW.started_at, ''), COALESCE(NEW.cpu_pct, 0), COALESCE(NEW.mem_mib, 0), COALESCE(NEW.seen_at, ''), COALESCE(NEW.players, ''), COALESCE(NEW.asleep, 0), COALESCE(NEW.online_json, '[]'), COALESCE(NEW.people_json, '{}'), COALESCE(NEW.player_set_json, ''), NEW.up_since, NEW.mem_limit_mib, NEW.cpu_limit, NEW.online_at);
212 END;
213 CREATE TRIGGER IF NOT EXISTS container_update INSTEAD OF UPDATE ON container BEGIN
214 UPDATE game_service SET box_id = NEW.box_id, name = NEW.name, state = NEW.state, health = NEW.health, started_at = NEW.started_at, cpu_pct = NEW.cpu_pct, mem_mib = NEW.mem_mib, seen_at = NEW.seen_at, players = NEW.players, asleep = NEW.asleep, online_json = NEW.online_json, people_json = NEW.people_json, player_set_json = NEW.player_set_json, up_since = NEW.up_since, mem_limit_mib = NEW.mem_limit_mib, cpu_limit = NEW.cpu_limit, online_at = NEW.online_at WHERE box_id IS OLD.box_id AND name IS OLD.name;
215 END;
216 CREATE TRIGGER IF NOT EXISTS container_delete INSTEAD OF DELETE ON container BEGIN
217 DELETE FROM game_service WHERE box_id IS OLD.box_id AND name IS OLD.name;
218 END;
219 CREATE VIEW IF NOT EXISTS machine_spark_profile AS SELECT * FROM game_spark_profile;
220 CREATE TRIGGER IF NOT EXISTS machine_spark_profile_insert INSTEAD OF INSERT ON machine_spark_profile BEGIN
221 INSERT INTO game_spark_profile (key, box_id, type, body, size, created_at, expires_at) VALUES (NEW.key, NEW.box_id, NEW.type, NEW.body, NEW.size, NEW.created_at, NEW.expires_at);
222 END;
223 CREATE TRIGGER IF NOT EXISTS machine_spark_profile_update INSTEAD OF UPDATE ON machine_spark_profile BEGIN
224 UPDATE game_spark_profile SET key = NEW.key, box_id = NEW.box_id, type = NEW.type, body = NEW.body, size = NEW.size, created_at = NEW.created_at, expires_at = NEW.expires_at WHERE key IS OLD.key;
225 END;
226 CREATE TRIGGER IF NOT EXISTS machine_spark_profile_delete INSTEAD OF DELETE ON machine_spark_profile BEGIN
227 DELETE FROM game_spark_profile WHERE key IS OLD.key;
228 END;
229 CREATE VIEW IF NOT EXISTS machine_spark_secret AS SELECT * FROM game_spark_secret;
230 CREATE TRIGGER IF NOT EXISTS machine_spark_secret_insert INSTEAD OF INSERT ON machine_spark_secret BEGIN
231 INSERT INTO game_spark_secret (box_id, secret_hash, created_at) VALUES (NEW.box_id, NEW.secret_hash, NEW.created_at);
232 END;
233 CREATE TRIGGER IF NOT EXISTS machine_spark_secret_update INSTEAD OF UPDATE ON machine_spark_secret BEGIN
234 UPDATE game_spark_secret SET box_id = NEW.box_id, secret_hash = NEW.secret_hash, created_at = NEW.created_at WHERE box_id IS OLD.box_id;
235 END;
236 CREATE TRIGGER IF NOT EXISTS machine_spark_secret_delete INSTEAD OF DELETE ON machine_spark_secret BEGIN
237 DELETE FROM game_spark_secret WHERE box_id IS OLD.box_id;
238 END;
239 CREATE VIEW IF NOT EXISTS machine_talk AS SELECT * FROM game_talk;
240 CREATE TRIGGER IF NOT EXISTS machine_talk_insert INSTEAD OF INSERT ON machine_talk BEGIN
241 INSERT INTO game_talk (box_id, user_id, conversation, created_at) VALUES (NEW.box_id, NEW.user_id, NEW.conversation, NEW.created_at);
242 END;
243 CREATE TRIGGER IF NOT EXISTS machine_talk_update INSTEAD OF UPDATE ON machine_talk BEGIN
244 UPDATE game_talk SET box_id = NEW.box_id, user_id = NEW.user_id, conversation = NEW.conversation, created_at = NEW.created_at WHERE box_id IS OLD.box_id AND user_id IS OLD.user_id;
245 END;
246 CREATE TRIGGER IF NOT EXISTS machine_talk_delete INSTEAD OF DELETE ON machine_talk BEGIN
247 DELETE FROM game_talk WHERE box_id IS OLD.box_id AND user_id IS OLD.user_id;
248 END;
249 CREATE VIEW IF NOT EXISTS machine_told AS SELECT * FROM game_told;
250 CREATE TRIGGER IF NOT EXISTS machine_told_insert INSTEAD OF INSERT ON machine_told BEGIN
251 INSERT INTO game_told (id, box_id, ask_id, uuid, text, confirm, created_at, told_at) VALUES (NEW.id, NEW.box_id, NEW.ask_id, NEW.uuid, NEW.text, COALESCE(NEW.confirm, 0), NEW.created_at, COALESCE(NEW.told_at, ''));
252 END;
253 CREATE TRIGGER IF NOT EXISTS machine_told_update INSTEAD OF UPDATE ON machine_told BEGIN
254 UPDATE game_told SET id = NEW.id, box_id = NEW.box_id, ask_id = NEW.ask_id, uuid = NEW.uuid, text = NEW.text, confirm = NEW.confirm, created_at = NEW.created_at, told_at = NEW.told_at WHERE id IS OLD.id;
255 END;
256 CREATE TRIGGER IF NOT EXISTS machine_told_delete INSTEAD OF DELETE ON machine_told BEGIN
257 DELETE FROM game_told WHERE id IS OLD.id;
258 END;
259 CREATE VIEW IF NOT EXISTS machine_when_quiet AS SELECT * FROM game_when_quiet;
260 CREATE TRIGGER IF NOT EXISTS machine_when_quiet_insert INSTEAD OF INSERT ON machine_when_quiet BEGIN
261 INSERT INTO game_when_quiet (box_id, kind, ref, user_id, asked_at, error) VALUES (NEW.box_id, NEW.kind, NEW.ref, NEW.user_id, NEW.asked_at, COALESCE(NEW.error, ''));
262 END;
263 CREATE TRIGGER IF NOT EXISTS machine_when_quiet_update INSTEAD OF UPDATE ON machine_when_quiet BEGIN
264 UPDATE game_when_quiet SET box_id = NEW.box_id, kind = NEW.kind, ref = NEW.ref, user_id = NEW.user_id, asked_at = NEW.asked_at, error = NEW.error WHERE box_id IS OLD.box_id AND kind IS OLD.kind;
265 END;
266 CREATE TRIGGER IF NOT EXISTS machine_when_quiet_delete INSTEAD OF DELETE ON machine_when_quiet BEGIN
267 DELETE FROM game_when_quiet WHERE box_id IS OLD.box_id AND kind IS OLD.kind;
268 END;
269 `;
src/games/neutral_args.ts added +13
@@ -0,0 +1,13 @@
1 /**
2 * The games tool's neutral names (#18501, step 1 of 2): `machine` is a `box`, `container` is a
3 * `service`, and the action `machines` is `list`. The old names keep working and win when both
4 * are sent, so a caller that knows only them is answered as before. Plain data in, plain data
5 * out; no new tool.
6 */
7 export function neutralArgs(args: Readonly<Record<string, unknown>>): Record<string, unknown> {
8 const out: Record<string, unknown> = { ...args };
9 if (out["box"] === undefined && out["machine"] !== undefined) out["box"] = out["machine"];
10 if (out["service"] === undefined && out["container"] !== undefined) out["service"] = out["container"];
11 if (out["action"] === "machines") out["action"] = "list";
12 return out;
13 }
src/http/api/identities.ts +15 −4
@@ -14,6 +14,7 @@ import {
1414 addMinecraftConnection, MINECRAFT_PROOF,
1515 dropConnection, dropLookupKey, makeLookupKey, type ConnectionError,
1616 } from "../../actions/identities.ts";
17 import { kindNamed } from "../../core/identities.ts";
1718 import { notePhase, takeSessionTime } from "../../core/route_timing.ts";
1819 import type { Handler } from "../context.ts";
1920 import { apiError, body, json, notFound, requireAccount, str, tryAgain } from "../respond.ts";
@@ -44,11 +45,16 @@ export function identityRoutes(router: Router<Handler>): void {
4445 const denied = requireAccount(ctx, { adminToken: true });
4546 if (denied) return denied;
4647 const input = await body(ctx.request);
47 if (input.minecraft !== undefined) {
48 const kept = addMinecraftConnection(ctx.app, ctx.auth.user!, input.minecraft);
48 // `{kind: "player", ident}` is `{minecraft: ident}`; `{kind: "email", ident}` is
49 // `{email: ident}`.
50 const named = kindNamed(input.kind);
51 if (input.kind !== undefined && named === undefined) return apiError("invalid", 422, "kind is email or player (minecraft, as before)");
52 const minecraft = input.minecraft ?? (named === "minecraft" ? input.ident : undefined);
53 if (minecraft !== undefined) {
54 const kept = addMinecraftConnection(ctx.app, ctx.auth.user!, minecraft);
4955 return kept.ok ? json({ connection: kept.value, says: MINECRAFT_PROOF }, 201, noStore) : refuse(kept.error);
5056 }
51 const made = await addConnection(ctx.app, ctx.auth.user!, input.email);
57 const made = await addConnection(ctx.app, ctx.auth.user!, input.email ?? (named === "email" ? input.ident : undefined));
5258 return made.ok ? json({ connection: made.value, says: "A code was mailed; send it to /confirm." }, 201, noStore) : refuse(made.error);
5359 });
5460
@@ -79,7 +85,12 @@ export function identityRoutes(router: Router<Handler>): void {
7985 // The integrations' lookup: found and not-found answer alike for every way of not knowing.
8086 router.add("POST", "/api/v1/connections/lookup", async (ctx) => {
8187 const input = await body(ctx.request);
82 const asked = askLookup(ctx.app, ctx.request.headers.get("x-forge-lookup-key") ?? "", { email: input.email, minecraft: input.minecraft }, input.use);
88 const named = kindNamed(input.kind);
89 const asked = askLookup(
90 ctx.app, ctx.request.headers.get("x-forge-lookup-key") ?? "",
91 { email: input.email ?? (named === "email" ? input.ident : undefined), minecraft: input.minecraft ?? (named === "minecraft" ? input.ident : undefined) },
92 input.use,
93 );
8394 if (!asked.ok) {
8495 if (asked.error === "unauthorized") return notFound();
8596 if (asked.error === "rate_limited") return tryAgain("rate_limited", 429, "too many lookups; wait a minute", 60);
src/http/mcp/tools/games.ts +13 −5

Diff not shown — a line 3,258 characters wide (limit 2,000). +13 −5

View file
src/http/mcp/tools/infra.ts +3 −3
@@ -67,7 +67,7 @@ const endWant = (args: Args): { act: string; digest: string; typed: string } =>
6767 export const tool: Tool = {
6868 name: "infra",
6969 listedTo: "admin",
70 doc: "`{ action, id?, want?, question?, names?, result?, bucket?, cursor? }`: `list` (its `says` names every machine; with a filter line as `id`, like `kind:volume cost:>10 sort:-created`, the cloud resources it leaves, each with `partOf`, its app, and `attachedTo`; 50 a page, `cursor` for the next), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, repo, name?, host?}` or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against the forge's record), `keep`, `begin`, `attach`, `record_result`; `cloud` a machine's name as `id` (its provider's view: power, size, IPs, actions) or a resource's `id` from `list` (as last read; a game box's machine names its `containers`, each by the `id` this takes; `file` is what declares a machine or a container, env names only); `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`; `cost` (each account against its ceiling and budget, and what costs most); `end` a resource's `id` from `list` with `want` `{act: stop|delete}` for its plan and `digest`, then again with `want` `{act, digest, typed}` to do it (held for the person's yes; `typed` is the resource's name, for a stop as for a delete; a plan expires after 30 minutes).",
70 doc: "`{ action, id?, want?, question?, names?, result?, bucket?, cursor? }`: `list` (its `says` names every machine; with a filter line as `id`, like `kind:volume cost:>10 sort:-created`, the cloud resources it leaves, each with `partOf`, its app, and `attachedTo`; 50 a page, `cursor` for the next), `show` an `id` (a plan, an experiment or a machine's name), `plan` a `want` `{app, where, question?, experiment?, days?}` or a firewall `{firewall: name}` or a DNS record `{dns: true, name, account, type: A|SRV, host, value, port?}` or a game box `{game: true, where, repo, name?, host?}` (`kind: machine` for `game: true`) or Minecraft Java `{design: minecraft, players, where, domain, dns, repo, eula: true, name?, version?}` (`kind: minecraft` for `design: minecraft`; its `compose` comes back with the plan) or Postgres `{database: true, app, where: account@region, size?, secret?}` or dead `names`, `apply` its `id`, `check` DNS records, `code` (infra.json against the forge's record), `keep`, `begin`, `attach`, `record_result`; `cloud` a machine's name as `id` (its provider's view: power, size, IPs, actions) or a resource's `id` from `list` (as last read; a game box's machine names its `containers`, each by the `id` this takes; `file` is what declares a machine or a container, env names only); `price`/`bind` a `bucket` `{region, name, repo?, stored_gib?, out_gib?}`; `cost` (each account against its ceiling and budget, and what costs most); `end` a resource's `id` from `list` with `want` `{act: stop|delete}` for its plan and `digest`, then again with `want` `{act, digest, typed}` to do it (held for the person's yes; `typed` is the resource's name, for a stop as for a delete; a plan expires after 30 minutes).",
7171 description: "Machines, spend.",
7272 inputSchema: {
7373 type: "object",
@@ -259,7 +259,7 @@ export const tool: Tool = {
259259 return good({ id: p.plan.id, says: p.summary, steps: p.plan.steps.map((x) => x.says), refusals: p.fixes, byAccount: p.plan.byAccount, deltaCents: p.plan.deltaCents, etaSeconds: p.plan.etaSeconds });
260260 }
261261 // Minecraft Java as a design (#17076): a game box sized by its players, with A + SRV records.
262 if (w["design"] === "minecraft") {
262 if (w["design"] === "minecraft" || w["kind"] === "minecraft") {
263263 const made = await planMinecraft(ctx.app, user, {
264264 name: s(w["name"], 40) || "mc",
265265 players: String(w["players"] ?? ""),
@@ -275,7 +275,7 @@ export const tool: Tool = {
275275 return good({ id: p.plan.id, says: p.summary, steps: p.plan.steps.map((x) => x.says), refusals: p.fixes, byAccount: p.plan.byAccount, deltaCents: p.plan.deltaCents, compose: made.value.compose, note: made.value.note });
276276 }
277277 // A game box: Docker and the game agent on the owner's cloud, running a compose file.
278 if (w["game"] === true) {
278 if (w["game"] === true || w["kind"] === "machine") {
279279 const want = gameWant({ name: s(w["name"], 40) || nameFor(s(w["repo"], 140)), where: s(w["where"], 260), repo: s(w["repo"], 140), host: s(w["host"], 253) });
280280 if (!want.ok) return bad("incomplete", want.error);
281281 const planned = await planWants(ctx.app, user, [want.value]);
test/cloud-says-doors.test.ts +16
@@ -269,6 +269,22 @@ describe("a game box", () => {
269269 expect(other.text).not.toContain("104.236.196.18");
270270 expect(other.text).not.toContain("203.0.113.9");
271271 });
272
273 test("the neutral names reach the same answers: `machines` is `list`, `machine` is `box`, `container` is `service` (#18501)", async () => {
274 const old = await tool(ada, "games", { action: "list" });
275 const neutral = await tool(ada, "games", { action: "machines" });
276 expect(neutral.value.items.map((b: { name: string }) => b.name)).toEqual(old.value.items.map((b: { name: string }) => b.name));
277 // Asked of a box nobody sees, both spellings say the same.
278 const a = await tool(ada, "games", { action: "console", box: "nope", service: "x", text: "list" });
279 const b = await tool(ada, "games", { action: "console", machine: "nope", container: "x", text: "list" });
280 expect(b.text).toBe(a.text);
281 // A real box and service reached by the neutral spelling: the command lands on its row. The
282 // service is added through its neutral view, the row the old name reads.
283 app.db.query("INSERT INTO container (box_id, name, state) SELECT id, 'lobby', 'running' FROM machine WHERE name = 'net'").run();
284 const said = await tool(ada, "games", { action: "console", machine: "net", container: "lobby", text: "list" });
285 expect(said.value.says).toContain("Queued");
286 expect(app.db.query("SELECT text FROM game_command WHERE id = ?1").get(said.value.id)).toEqual({ text: "list" });
287 });
272288 });
273289
274290 describe("the terminal", () => {
test/identities.test.ts +16
@@ -176,6 +176,22 @@ describe("connections on an account", () => {
176176 expect(after.connections.some((x: any) => x.kind === "minecraft")).toBe(false);
177177 });
178178
179 test("the neutral kind `player` is taken beside `minecraft`, and the connection still answers `minecraft` (#18501)", async () => {
180 const added = await call("POST", "/api/v1/connections", tessCookie, { kind: "player", ident: " Neutral_Tess " });
181 expect(added.status).toBe(201);
182 const c = ((await added.json()) as any).connection;
183 expect(c).toMatchObject({ kind: "minecraft", ident: "neutral_tess", proven: false });
184 expect((await call("POST", "/api/v1/connections", tessCookie, { kind: "player", ident: "no spaces!" })).status).toBe(422);
185 expect((await call("POST", "/api/v1/connections", tessCookie, { kind: "robot", ident: "neutral_tess" })).status).toBe(422);
186 // Either spelling reaches the same row: the old key finds what the neutral kind added.
187 expect(proveMinecraft(app.db, tess.id, "neutral_tess", Date.now())).toBe(true);
188 await call("PUT", `/api/v1/connections/${c.id}/uses/feedback`, tessCookie, { on: true });
189 const look = (body: object) => call("POST", "/api/v1/connections/lookup", "", { use: "feedback", ...body }, { "x-forge-lookup-key": key }).then((r) => r.json() as Promise<any>);
190 expect((await look({ kind: "player", ident: "Neutral_Tess" })).user).toEqual({ username: "tess" });
191 expect((await look({ minecraft: "neutral_tess" })).user).toEqual({ username: "tess" });
192 await call("DELETE", `/api/v1/connections/${c.id}`, tessCookie);
193 });
194
179195 test("a Minecraft name the account already linked in game is proven when it is added", async () => {
180196 app.db.query("INSERT INTO game_link (box_id, player, user_id, uuid, created_at) VALUES ('box1', 'Olive_MC', ?1, 'u-1', ?2)")
181197 .run(admin.id, new Date().toISOString());
test/minecraft-design.test.ts +13
@@ -153,6 +153,19 @@ describe("the doors", () => {
153153 expect(made.value.note).toContain("play.example.com");
154154 });
155155
156 test("`kind: minecraft` plans what `design: minecraft` does, and `kind: machine` what `game: true` does (#18501)", async () => {
157 const { design: _d, ...rest } = full;
158 const made = (await tool.run(ctxOf(alice()), alice(), { action: "plan", want: { ...rest, kind: "minecraft", name: "mc-neutral" } })) as any;
159 expect(made.ok).toBe(true);
160 expect(made.value.compose).toContain("itzg/minecraft-server:");
161 expect(made.value.note).toContain("play.example.com");
162 // A game box with nothing said is refused the same way through either spelling.
163 const old = (await tool.run(ctxOf(alice()), alice(), { action: "plan", want: { game: true } })) as any;
164 const neutral = (await tool.run(ctxOf(alice()), alice(), { action: "plan", want: { kind: "machine" } })) as any;
165 expect(old).toMatchObject({ ok: false, error: "incomplete" });
166 expect(neutral).toEqual(old);
167 });
168
156169 test("the terminal's M asks the name, plans, and shows the compose file", async () => {
157170 const { app: tui, session, terminal } = (() => {
158171 const terminal = headlessTerminal(140, 40);
test/neutral-machine-names.test.ts added +89
@@ -0,0 +1,89 @@
1 /**
2 * The neutral names beside the game_* ones (#18501, step 1 of 2): every game_* table is also a
3 * view named for what it holds, writes through either name reach the same rows, and nothing old
4 * is gone, so a database from before restores and boots and the previous release's code runs
5 * against the changed one.
6 */
7 import { Database } from "bun:sqlite";
8 import { describe, expect, test } from "bun:test";
9 import { migrationFiles } from "../src/core/schema.ts";
10 import { migrate } from "../src/kernel/migrate.ts";
11
12 const NEW = "20261206175001-neutral-machine-names";
13 const before = migrationFiles.filter((m) => m.name !== NEW);
14 const neutralOf = (t: string): string => (t === "game_box" ? "machine" : t === "game_service" ? "container" : `machine_${t.slice(5)}`);
15 const gameTables = (db: Database): string[] =>
16 db.query<{ name: string }, []>("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'game\\_%' ESCAPE '\\' ORDER BY name").all().map((r) => r.name);
17
18 function fresh(): Database {
19 const db = new Database(":memory:");
20 migrate(db, migrationFiles);
21 return db;
22 }
23
24 describe("a snapshot from before this change", () => {
25 test("restores and boots: the new migration is the only one applied, and the old rows read by both names", () => {
26 const db = new Database(":memory:");
27 migrate(db, before);
28 db.query("INSERT INTO game_box (id, name, token_hash, created_at) VALUES ('b1', 'hub', 'h', '2026-12-01')").run();
29 db.query("INSERT INTO game_service (box_id, name, state) VALUES ('b1', 'lobby', 'running')").run();
30 expect(db.query("SELECT 1 FROM sqlite_master WHERE name = 'machine'").get()).toBeNull();
31
32 const booted = migrate(db, migrationFiles);
33 expect(booted.applied).toEqual([NEW]);
34 expect(db.query("SELECT name FROM machine").all()).toEqual([{ name: "hub" }]);
35 expect(db.query("SELECT name, state FROM container").all()).toEqual([{ name: "lobby", state: "running" }]);
36 });
37
38 test("the previous release's code runs against the changed database: the old names are all there and write as before", () => {
39 const db = fresh();
40 db.query("INSERT INTO game_box (id, name, token_hash, created_at) VALUES ('b1', 'hub', 'h', 'now')").run();
41 db.query("UPDATE game_box SET place = 'fsn1' WHERE id = 'b1'").run();
42 db.query("INSERT INTO game_command (id, box_id, service, kind, text, by, created_at) VALUES ('c1', 'b1', 'lobby', 'rcon', 'list', 'u', 'now')").run();
43 expect(db.query("SELECT place FROM machine WHERE id = 'b1'").get()).toEqual({ place: "fsn1" });
44 expect(db.query("SELECT text FROM machine_command").get()).toEqual({ text: "list" });
45 db.query("DELETE FROM game_box WHERE id = 'b1'").run();
46 expect(db.query("SELECT 1 FROM machine").all()).toEqual([]);
47 });
48 });
49
50 describe("writes through either name reach the same rows", () => {
51 test("insert, update and delete through the neutral name land in the game_* table", () => {
52 const db = fresh();
53 db.query("INSERT INTO machine (id, name, token_hash, created_at) VALUES ('b1', 'hub', 'h', 'now')").run();
54 db.query("INSERT INTO container (box_id, name, state) VALUES ('b1', 'lobby', 'running')").run();
55 expect(db.query("SELECT id, name FROM game_box").all()).toEqual([{ id: "b1", name: "hub" }]);
56 db.query("UPDATE container SET state = 'stopped' WHERE box_id = 'b1' AND name = 'lobby'").run();
57 expect(db.query("SELECT state FROM game_service").get()).toEqual({ state: "stopped" });
58 // An autoincrement-free table with a composite key: one row changes, not the table.
59 db.query("INSERT INTO container (box_id, name, state) VALUES ('b1', 'survival', 'running')").run();
60 db.query("UPDATE container SET state = 'asleep' WHERE name = 'survival'").run();
61 expect(db.query("SELECT name, state FROM game_service ORDER BY name").all()).toEqual([
62 { name: "lobby", state: "stopped" },
63 { name: "survival", state: "asleep" },
64 ]);
65 db.query("DELETE FROM container WHERE name = 'lobby'").run();
66 expect(db.query("SELECT name FROM game_service").all()).toEqual([{ name: "survival" }]);
67 });
68
69 test("the old name's write shows through the new name, a row's every column", () => {
70 const db = fresh();
71 db.query("INSERT INTO game_box (id, name, token_hash, repo, monthly_cents, created_at) VALUES ('b2', 'mc', 'h', 'me/mc', 700, 'now')").run();
72 expect(db.query("SELECT repo, monthly_cents FROM machine WHERE id = 'b2'").get()).toEqual({ repo: "me/mc", monthly_cents: 700 });
73 });
74 });
75
76 describe("every game_* table has its neutral view, with triggers that carry every column", () => {
77 test("a table that gains a column the triggers do not copy fails here, naming it", () => {
78 const db = fresh();
79 const lacking: string[] = [];
80 for (const t of gameTables(db)) {
81 const v = neutralOf(t);
82 expect(db.query("SELECT 1 FROM sqlite_master WHERE type = 'view' AND name = ?1").get(v)).not.toBeNull();
83 const cols = db.query<{ name: string }, [string]>("SELECT name FROM pragma_table_info(?1)").all(t).map((c) => c.name);
84 const sql = db.query<{ sql: string }, [string]>("SELECT sql FROM sqlite_master WHERE type = 'trigger' AND name = ?1").get(`${v}_insert`)?.sql ?? "";
85 for (const c of cols) if (!sql.includes(`NEW.${c}`)) lacking.push(`${t}.${c}`);
86 }
87 expect(lacking).toEqual([]);
88 });
89 });

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

Keyboard shortcuts

Docs