Public: anyone can read this; the crew can change it.
Games: a new pin for the forge's proxy plugin replaces the old jar, in PLUGINS and on disk, and the page says which one loaded #18111
Merged 9h 11m ago, live in production
scout/forge:scout/18110-games-a-new-pin-for-the-forge-s-proxy-pl → main 0 commits fork 3 threads · 3 unresolved
10 files
changed +314
−8
docs/features/games.md +2
| @@ -142,6 +142,8 @@ If the page says the proxy is left as it is: | ||
| 142 | 142 | - If the jar it fetched had the wrong bytes, commit a change to the compose file; a comment is enough. The box does not try the same commit and the same jars twice. |
| 143 | 143 | - If a jar it already had has the wrong bytes and cannot be fetched, the box keeps the file while the proxy runs and removes it once the proxy is stopped. The proxy then does not start until the jar is back. The box's run log says which of the two happened. |
| 144 | 144 | |
| 145 | A new pin of a plugin the proxy already has (another version of forge-velocity) replaces that plugin's entry in `PLUGINS` in the same commit; the old jar is never left beside it. After the proxy's start succeeds, the box takes out of `/server/plugins` each jar of the forge's own that `PLUGINS` no longer names (told by name, `<plugin>-<12 hex>.jar`, and by its bytes matching that hash). A jar of a pinned plugin's name that is not that (a person's) stays, and the box's page and the terminal's box screen name it, since Velocity may load it instead of the pinned one. The same lines say which version the proxy loaded, read from its log ("Loaded plugin forge 0.1.0 by …", at most once a minute): "The proxy loaded forge 0.1.0, the pinned version.", or "…forge 0.0.9, not the pinned forge-velocity 0.1.0." An agent from before this sends none of it (#18110). | |
| 146 | ||
| 145 | 147 | The other servers come up as usual meanwhile. An address for a jar runs out after 6 hours; each full sync brings a new one, and the next try uses it. |
| 146 | 148 | |
| 147 | 149 | If the page says that no change reaches the box until its agent updates, wait: nothing is lost. The agent updates itself when it syncs, and the box then takes the newest commit. The agent it replaced stays beside the new one until the new one has completed a sync: a new agent that completes none within 3 minutes, or exits 3 times within 10, is replaced by the old one, and the box's page says "agent update rolled back" and why. That update is not tried again until the forge answers a different agent. |
src/games/agent.sh +45 −4
| @@ -926,7 +926,7 @@ jar_wrong() { | ||
| 926 | 926 | # Every jar in place with its sha256, or 1. The image skips a file of the same name and size, so a name |
| 927 | 927 | # holds its sha256's first 12 characters. A link can be signed: it is never on a command line or said. |
| 928 | 928 | jars_ready() { |
| 929 | local name url sha next list bad=0 none=0 n=0 rc waits | |
| 929 | local name url sha next list bad=0 none=0 n=0 rc waits named | |
| 930 | 930 | list="$(cat "/.jars.json")" |
| 931 | 931 | # How often this was tried already: .jars.retry is when it is due, and that count. |
| 932 | 932 | [ "$(jars_key <<<"")" = "$(cat "/.jars.tried" 2>/dev/null)" ] && n="$(cut -s -d' ' -f2 "/.jars.retry" 2>/dev/null)" |
| @@ -959,6 +959,13 @@ jars_ready() { | ||
| 959 | 959 | fi |
| 960 | 960 | chmod 0644 "" && mv "" "/jars/" || return 1 |
| 961 | 961 | done 3< <(jq -r '.[] | [.name, .sha256, .url] | @tsv' <<<"") |
| 962 | # A jar of ours the compose file's PLUGINS no longer names goes: the image copies by name, so a stale one is no pin's. | |
| 963 | for name in ""/jars/*.jar; do | |
| 964 | [ -f "" ] && [ ! -L "" ] || continue | |
| 965 | # Only a jq that ran and said false removes: an empty or broken list, or no jq, removes nothing. | |
| 966 | named="$(jq -e --arg n "" 'any(.[]; .name == )' <<<"" 2>/dev/null)" | |
| 967 | [ "$?" = 1 ] && [ "" = false ] && rm -f "" | |
| 968 | done | |
| 962 | 969 | if [ "" = 1 ]; then |
| 963 | 970 | read -ra waits <<<"" |
| 964 | 971 | n="" |
| @@ -996,6 +1003,40 @@ autostopper_back() { | ||
| 996 | 1003 | [ -f "/.jar.aside" ] || return 0 |
| 997 | 1004 | autostopper_mv .jar.aside "\${f%.aside}" "put back" && rm -f "/.jar.aside" |
| 998 | 1005 | } |
| 1006 | # After an up that worked: from the proxy's plugins folder (/server/plugins) the forge's own jars that PLUGINS no | |
| 1007 | # longer names go, told by name and hash ("<plugin>-<first 12 of its sha256>.jar" whose bytes match). A jar of a pinned | |
| 1008 | # plugin's name that is not that is a person's: it stays, and is named. What was done is kept in .plugins.tidy. | |
| 1009 | plugins_tidy() { | |
| 1010 | local cid names out | |
| 1011 | names="$(jq -er 'if type == "array" and length > 0 then [.[].name] | join(" ") else empty end' "/.jars.json" 2>/dev/null)" || return 0 | |
| 1012 | cid="$(compose ps -a -q velocity 2>/dev/null | head -n 1)" | |
| 1013 | [ -n "" ] || return 0 | |
| 1014 | out="$("" run --rm --network none -e NAMES="" --volumes-from "" -w /server "" sh -euc '[ ! -L plugins ] && cd plugins 2>/dev/null || exit 0 | |
| 1015 | for f in *.jar; do | |
| 1016 | [ -f "" ] && [ ! -L "" ] || continue | |
| 1017 | case " " in *" "*) continue ;; esac | |
| 1018 | case "" in forge-velocity-*|AutoStopper-*) ;; *) continue ;; esac | |
| 1019 | h="$(echo "" | sed -n "s/.*-\([0-9a-f]\{12\}\)\.jar\$/\1/p")" | |
| 1020 | if [ -n "" ] && [ "$(sha256sum < "" | cut -c1-12)" = "" ]; then rm -f ""; echo "removed "; else echo "stray "; fi | |
| 1021 | done' 2>/dev/null)" || return 0 | |
| 1022 | printf '%s\n' "" | jq -R -s -c '[split("\n")[] | select(test("^(removed|stray) [A-Za-z0-9._-]+$"))] | {removed: [.[] | select(startswith("removed ")) | .[8:]], stray: [.[] | select(startswith("stray ")) | .[6:]]}' >"/.plugins.tidy" 2>/dev/null || rm -f "/.plugins.tidy" | |
| 1023 | } | |
| 1024 | # What the proxy's log says it loaded ("Loaded plugin forge 0.1.0 by …"), with what plugins_tidy did. Read at most | |
| 1025 | # once a minute: the log can be long. An older forge ignores the field. | |
| 1026 | plugins_report() { | |
| 1027 | local loaded tidy='{}' | |
| 1028 | [ -s "/.jars.json" ] || { echo '{}'; return 0; } | |
| 1029 | if [ ! -f "/.plugins.loaded" ] || [ -gt 60 ]; then | |
| 1030 | capped 10 "" compose -p game --project-directory "" -f "/compose.yml" logs --no-color --no-log-prefix velocity 2>/dev/null | | |
| 1031 | grep -a 'Loaded plugin ' | tail -n 40 | | |
| 1032 | jq -R -s -c '[split("\n")[] | capture("Loaded plugin (?<id>[A-Za-z0-9_.-]{1,64}) (?<version>[^ ]{1,64}) by ")]' >"/.plugins.loaded.next" 2>/dev/null && | |
| 1033 | mv "/.plugins.loaded.next" "/.plugins.loaded" || rm -f "/.plugins.loaded.next" | |
| 1034 | fi | |
| 1035 | loaded="$(cat "/.plugins.loaded" 2>/dev/null)" | |
| 1036 | [ -s "/.plugins.tidy" ] && tidy="$(cat "/.plugins.tidy")" | |
| 1037 | jq -c -n --argjson loaded "" --argjson tidy "" --slurpfile jars "/.jars.json" \ | |
| 1038 | '{named: [$jars[0][]?.name], loaded: $loaded, removed: ($tidy.removed // []), stray: ($tidy.stray // [])}' 2>/dev/null || echo '{}' | |
| 1039 | } | |
| 999 | 1040 | # Whether nobody is on the network: the proxy is stopped, or its own count in the people sync (`glist all`) is |
| 1000 | 1041 | # a fresh 0. Not known is someone. An old 0 is read again at the next sync. |
| 1001 | 1042 | nobody_on() { |
| @@ -1024,7 +1065,7 @@ up_job() { | ||
| 1024 | 1065 | compose "$@" |
| 1025 | 1066 | rc= |
| 1026 | 1067 | rm -f "/.jars.rest" |
| 1027 | if [ "" = 0 ]; then rm -f "/.jar.aside"; else autostopper_back; fi | |
| 1068 | if [ "" = 0 ]; then rm -f "/.jar.aside" "/.plugins.loaded"; plugins_tidy; else autostopper_back; fi | |
| 1028 | 1069 | if [ "" = 0 ] && [ -f "/.jars.late" ]; then |
| 1029 | 1070 | # A person's recreate of the proxy ends the wait too: the next up makes the commit current. |
| 1030 | 1071 | [ "" != velocity ] || rm -f "/.up.failed" |
| @@ -1051,7 +1092,7 @@ up_job() { | ||
| 1051 | 1092 | [ "" = 0 ] || echo "the jar is here; the proxy restarts when nobody is on" |
| 1052 | 1093 | return 1 |
| 1053 | 1094 | } |
| 1054 | export -f capped nobody_on jars_key jar_wrong jars_ready autostopper_mv autostopper_aside autostopper_back up_job | |
| 1095 | export -f capped plugins_tidy nobody_on jars_key jar_wrong jars_ready autostopper_mv autostopper_aside autostopper_back up_job | |
| 1055 | 1096 | export AUTOSTOPPER_PROJECT AUTOSTOPPER_SHA512 JAR_RETRY JAR_CURL_RETRY |
| 1056 | 1097 | |
| 1057 | 1098 | # ---- the owner's shell (#17151 V4): `bash -lc` under `capped`, at most FORGE_GAME_SHELL_S |
| @@ -1510,7 +1551,7 @@ sync() { | ||
| 1510 | 1551 | logs "" | json_or '{}' >"/.sync.logs" |
| 1511 | 1552 | between |
| 1512 | 1553 | host | json_or '{}' >"/.sync.host" |
| 1513 | bounded_diag | jq -c --argjson hold "$(held)" --argjson waits "$([ -f "/.jars.waits" ] && echo true || echo false)" --arg back "$(sed -n 2p "" 2>/dev/null)" '. + {hold: $hold} + (if $waits then {jarsWait: true} else {} end) + (if $back == "" then {} else {rolledBack: $back} end)' 2>/dev/null | json_or '{}' >"/.sync.diag" | |
| 1554 | bounded_diag | jq -c --argjson plugins "$(plugins_report)" --argjson hold "$(held)" --argjson waits "$([ -f "/.jars.waits" ] && echo true || echo false)" --arg back "$(sed -n 2p "" 2>/dev/null)" '. + {hold: $hold} + (if $plugins == {} then {} else {plugins: $plugins} end) + (if $waits then {jarsWait: true} else {} end) + (if $back == "" then {} else {rolledBack: $back} end)' 2>/dev/null | json_or '{}' >"/.sync.diag" | |
| 1514 | 1555 | between |
| 1515 | 1556 | people | json_or '{}' >"/.sync.people" |
| 1516 | 1557 | between |
src/games/diag.ts +13
| @@ -29,6 +29,19 @@ export interface Diag { | ||
| 29 | 29 | readonly hold?: Hold | null; |
| 30 | 30 | /** The proxy's jars are on the box, and the proxy restarts for them when nobody is on. */ |
| 31 | 31 | readonly jarsWait?: boolean; |
| 32 | /** The proxy's plugins as the agent saw them (#18110); an older agent does not say. */ | |
| 33 | readonly plugins?: { | |
| 34 | /** The jar names the forge's last answer pinned. */ | |
| 35 | readonly named?: readonly string[]; | |
| 36 | /** Each "Loaded plugin <id> <version>" in the proxy's log. */ | |
| 37 | readonly loaded?: readonly { readonly id?: string; readonly version?: string }[]; | |
| 38 | /** | |
| 39 | * Jars of the forge's own taken out of the plugins folder, and jars a person put there that | |
| 40 | * were left. | |
| 41 | */ | |
| 42 | readonly removed?: readonly string[]; | |
| 43 | readonly stray?: readonly string[]; | |
| 44 | }; | |
| 32 | 45 | /** The agent put the previous script back after an update that did not check in, and says why (#18019). */ |
| 33 | 46 | readonly rolledBack?: string; |
| 34 | 47 | } |
src/games/plugins_loaded.ts added +47
| @@ -0,0 +1,47 @@ | ||
| 1 | /** | |
| 2 | * Which version of the forge's own plugins the proxy loaded (#18110), in words: the agent reads the | |
| 3 | * proxy's log ("Loaded plugin forge 0.1.0 by …") and says it in the sync's diag, with the jars it | |
| 4 | * took out of the plugins folder and the ones it left there. The page and the terminal print these | |
| 5 | * lines, so a person need not read the log to learn whether the pinned jar is the one that runs. | |
| 6 | */ | |
| 7 | import type { Diag } from "./diag.ts"; | |
| 8 | import { PINS } from "./proxy_jars.ts"; | |
| 9 | ||
| 10 | /** The id each pinned plugin has in the proxy's log, by the start of its jar's name. */ | |
| 11 | const IDS: readonly (readonly [prefix: string, id: string])[] = [ | |
| 12 | ["forge-velocity-", "forge"], | |
| 13 | ["AutoStopper-", "autostopper"], | |
| 14 | ]; | |
| 15 | ||
| 16 | /** Text the box sent, cut to a short plain word: it is shown, never trusted. */ | |
| 17 | const plain = (s: unknown, most = 80): string => (typeof s === "string" ? s.replace(/[^\x20-\x7e]/g, "").slice(0, most) : ""); | |
| 18 | ||
| 19 | /** The pinned version a title ends with ("forge-velocity 0.1.0" is "0.1.0"). */ | |
| 20 | const versionOf = (title: string): string => title.split(" ").at(-1) ?? ""; | |
| 21 | ||
| 22 | /** The lines for the proxy's plugins; none for an agent that sent none. */ | |
| 23 | export function pluginLines(d: Pick<Diag, "plugins">): string[] { | |
| 24 | const p = d.plugins; | |
| 25 | if (p === undefined) return []; | |
| 26 | const out: string[] = []; | |
| 27 | const loaded = (p.loaded ?? []).map((l) => ({ id: plain(l.id, 64).toLowerCase(), version: plain(l.version, 64) })); | |
| 28 | for (const name of p.named ?? []) { | |
| 29 | const pin = PINS.find((one) => one.name === name); | |
| 30 | const id = IDS.find(([prefix]) => name.startsWith(prefix))?.[1]; | |
| 31 | if (pin === undefined || id === undefined) continue; | |
| 32 | const ran = loaded.filter((l) => l.id === id).at(-1); | |
| 33 | const want = versionOf(pin.title); | |
| 34 | out.push( | |
| 35 | ran === undefined | |
| 36 | ? `The proxy's log does not say it loaded ${id} yet: ${pin.title} is pinned.` | |
| 37 | : ran.version === want | |
| 38 | ? `The proxy loaded ${id} ${ran.version}, the pinned version.` | |
| 39 | : `The proxy loaded ${id} ${ran.version}, not the pinned ${pin.title}.`, | |
| 40 | ); | |
| 41 | } | |
| 42 | const removed = (p.removed ?? []).map((n) => plain(n, 100)).filter((n) => n !== ""); | |
| 43 | if (removed.length > 0) out.push(`Taken out of the proxy's plugins, as the forge's own and no longer in PLUGINS: ${removed.join(", ")}.`); | |
| 44 | const stray = (p.stray ?? []).map((n) => plain(n, 100)).filter((n) => n !== ""); | |
| 45 | if (stray.length > 0) out.push(`Left in the proxy's plugins, because the forge did not put ${stray.length === 1 ? "it" : "them"} there: ${stray.join(", ")}. Velocity may load ${stray.length === 1 ? "it" : "one of them"} instead of the pinned jar.`); | |
| 46 | return out; | |
| 47 | } |
src/games/proxy_jars.ts +10 −2
| @@ -72,6 +72,11 @@ const HELD = "velocity"; | ||
| 72 | 72 | const PROJECT = "PG4gqnzX"; |
| 73 | 73 | /** The proxy image whose start script and copying tool were read for the undo (mc-image-helper 1.81.1). */ |
| 74 | 74 | const READ_IMAGE = "sha256:8e49050326bd8d588830cfd7fd0b9a92e3993259f93261d926266ec2ba4adde0"; |
| 75 | /** | |
| 76 | * A jar's plugin, by its file name: what comes before the version ("forge-velocity-0.1.0-ab.jar" is | |
| 77 | * "forge-velocity"). | |
| 78 | */ | |
| 79 | const stemOf = (url: string): string => url.slice(AT.length).replace(/(?:-\d.*|-[0-9a-f]{12}\.jar)$/, "").toLowerCase(); | |
| 75 | 80 | const isStopper = (pin: Pin): boolean => (AT + pin.name).startsWith(OWN_AUTOSTOPPER); |
| 76 | 81 | |
| 77 | 82 | /** How many AutoStoppers the proxy would be given: Velocity loads one of two, whichever is first. */ |
| @@ -167,15 +172,18 @@ function withJar(text: string, pin: Pin): string { | ||
| 167 | 172 | const plugins = pluginUrls(text, proxy); |
| 168 | 173 | if (plugins.includes(AT + pin.name)) |
| 169 | 174 | throw new Error(`${pin.title} is already there.`); |
| 175 | // What stays in PLUGINS: the forge's jar of the same plugin (another hash or version) is | |
| 176 | // replaced, never kept beside. | |
| 177 | const kept = plugins.filter((p) => !(p.startsWith(AT) && stemOf(p) === stemOf(AT + pin.name))); | |
| 170 | 178 | const before = volumesOf(text, proxy); |
| 171 | 179 | const mounted = before.includes(MOUNT); |
| 172 | 180 | if (before.some((v) => v !== MOUNT && JSON.stringify(v).includes("/forge-jars"))) throw new Error(`${proxy} mounts something else at /forge-jars: it must be ${MOUNT}.`); |
| 173 | const named = setServiceEnv(text, proxy, { PLUGINS: [...plugins, AT + pin.name].join(",") }); | |
| 181 | const named = setServiceEnv(text, proxy, { PLUGINS: [...kept, AT + pin.name].join(",") }); | |
| 174 | 182 | const held = mounted ? named : setServiceListItem(named, proxy, "volumes", MOUNT, [`- ${MOUNT}`]); |
| 175 | 183 | if (isStopper(pin) && sleeping(text).length === 0) |
| 176 | 184 | throw new Error("No server of this network sleeps, so it has no AutoStopper to replace. Set a server to sleep first."); |
| 177 | 185 | // The image removes a jar PLUGINS no longer names only while PLUGINS names something. |
| 178 | if (isStopper(pin) && plugins.length === 0) | |
| 186 | if (isStopper(pin) && kept.length === 0) | |
| 179 | 187 | throw new Error("Give the proxy forge-velocity first. With nothing else in PLUGINS, taking this AutoStopper out again would leave its jar beside the one from Modrinth."); |
| 180 | 188 | const out = isStopper(pin) ? syncSleep(held) : held; |
| 181 | 189 | if (autoStoppers(out, proxy) > 1) |
src/tui/screens/games.ts +2 −1
| @@ -34,6 +34,7 @@ import { lendCannot } from "../../games/lend_why.ts"; | ||
| 34 | 34 | import { spareRow } from "../../games/spare_row.ts"; |
| 35 | 35 | import { healWords } from "../../games/heal.ts"; |
| 36 | 36 | import { boxStatus, boxStatusShared, boxTails } from "../../games/diag.ts"; |
| 37 | import { pluginLines } from "../../games/plugins_loaded.ts"; | |
| 37 | 38 | import { heldLines, heldOn } from "../../games/hold.ts"; |
| 38 | 39 | import { askPower, type Power } from "../../games/power.ts"; |
| 39 | 40 | import { cloudLine, pinnedOfBox } from "../../actions/cloud_says.ts"; |
| @@ -628,7 +629,7 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 628 | 629 | const shared = boxStatusShared(b.diag); |
| 629 | 630 | return [...held, ...spare, ...(shared.length === 0 ? ["The box has said nothing yet."] : shared)]; |
| 630 | 631 | } |
| 631 | const plain = [...boxStatus(b.diag), ...healWords(f.app.db, b)]; | |
| 632 | const plain = [...boxStatus(b.diag), ...pluginLines(b.diag), ...healWords(f.app.db, b)]; | |
| 632 | 633 | const tails = boxTails(b.diag); |
| 633 | 634 | if (plain.length + tails.length === 0) return [...held, ...lend, ...spare, "The box has sent no diagnosis yet: its agent sends one with every check-in."]; |
| 634 | 635 | return [...held, ...lend, ...spare, ...plain, ...tails.flatMap(([k, v]) => ["", `== ${k}`, ...v.split("\n")])]; |
src/web/pages/games.ts +2 −1
| @@ -24,6 +24,7 @@ import { lendCannot } from "../../games/lend_why.ts"; | ||
| 24 | 24 | import { spareRow } from "../../games/spare_row.ts"; |
| 25 | 25 | import { healWords } from "../../games/heal.ts"; |
| 26 | 26 | import { boxStatus, boxStatusShared, boxTails } from "../../games/diag.ts"; |
| 27 | import { pluginLines } from "../../games/plugins_loaded.ts"; | |
| 27 | 28 | import { HELD_DOC, heldOn, startsNothing } from "../../games/hold.ts"; |
| 28 | 29 | import { askPower, isPower, refusesNow, type Power } from "../../games/power.ts"; |
| 29 | 30 | import { docHref } from "./docs_link.ts"; |
| @@ -448,7 +449,7 @@ function sharedBoxBlock(box: GameBox, packs: readonly string[]): Html | null { | ||
| 448 | 449 | /** The box itself, from its latest diagnosis: plain lines first, the raw tails folded under them. The owner's. */ |
| 449 | 450 | function boxBlock(box: GameBox, packs: readonly string[]): Html { |
| 450 | 451 | const d = box.diag; |
| 451 | const lines = [...boxStatus(d), ...packs]; | |
| 452 | const lines = [...boxStatus(d), ...pluginLines(d), ...packs]; | |
| 452 | 453 | const raw = boxTails(d); |
| 453 | 454 | return html`<section class="settings-block"> |
| 454 | 455 | <h2>Box</h2> |
test/game-agent-plugins.test.ts added +147
| @@ -0,0 +1,147 @@ | ||
| 1 | /** | |
| 2 | * The proxy's plugins folder after a new pin (#18110): the forge's own old jar goes, a jar a person | |
| 3 | * put there stays and is named, the log's "Loaded plugin" lines are reported, and the agent's copy | |
| 4 | * folder loses only what a good list does not name. The agent's functions are read out of agent.sh | |
| 5 | * and run against a fake docker that runs the helper's script on a folder. | |
| 6 | */ | |
| 7 | import { expect, test } from "bun:test"; | |
| 8 | import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, writeFileSync } from "node:fs"; | |
| 9 | import { rm } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | const AGENT = join(import.meta.dir, "../src/games/agent.sh"); | |
| 17 | const lacks = ["bash", "jq", "sha256sum", "sh"].filter((t) => Bun.which(t) === null); | |
| 18 | const on = test.skipIf(lacks.length > 0); | |
| 19 | const sha = (s: string): string => new Bun.CryptoHasher("sha256").update(s).digest("hex"); | |
| 20 | ||
| 21 | /** The pinned jar's name for `body`, as the forge names them. */ | |
| 22 | const jarName = (stem: string, body: string): string => `${stem}-${sha(body).slice(0, 12)}.jar`; | |
| 23 | ||
| 24 | async function withBox<T>(log: string, run: (box: string, call: (fn: string) => string) => T): Promise<T> { | |
| 25 | const box = mkdtempSync(join(tmpdir(), "forge-game-plugins-")); | |
| 26 | mkdirSync(join(box, "server/plugins"), { recursive: true }); | |
| 27 | mkdirSync(join(box, "jars")); | |
| 28 | writeFileSync(join(box, "log"), log); | |
| 29 | // `docker compose … ps -a -q velocity` → an id; `… logs` → the log; `run … sh -euc SCRIPT` runs | |
| 30 | // SCRIPT in server/. | |
| 31 | writeFileSync( | |
| 32 | join(box, "docker"), | |
| 33 | `#!/usr/bin/env bash | |
| 34 | case "$*" in | |
| 35 | *"ps -a -q velocity"*) echo cid ;; | |
| 36 | *" logs "*) cat "${box}/log" ;; | |
| 37 | run*) for a in "$@"; do case "$a" in NAMES=*) export "$a" ;; esac; done | |
| 38 | cd "${box}/server" && sh -euc "\${@: -1}" ;; | |
| 39 | esac | |
| 40 | `, | |
| 41 | ); | |
| 42 | chmodSync(join(box, "docker"), 0o755); | |
| 43 | const script = (names: string[]): string => | |
| 44 | names.map((n) => `sed -n '/^${n}() {/,/^}/p' "${AGENT}"`).join("; "); | |
| 45 | try { | |
| 46 | return run(box, (fn) => { | |
| 47 | const p = Bun.spawnSync( | |
| 48 | [ | |
| 49 | "bash", | |
| 50 | "-c", | |
| 51 | `export DIR="${box}" DOCKER="${box}/docker" HELPER=img | |
| 52 | capped() { shift; "$@"; } | |
| 53 | compose() { "$DOCKER" compose -p game "$@"; } | |
| 54 | jars_key() { jq -c '[.[] | [.name, .sha256, .url != ""]]' 2>/dev/null; } | |
| 55 | jar_wrong() { :; } | |
| 56 | source <(${script(["plugins_tidy", "plugins_report", "jars_ready"])}) | |
| 57 | ${fn}`, | |
| 58 | ], | |
| 59 | { stdout: "pipe", stderr: "pipe" }, | |
| 60 | ); | |
| 61 | return `${p.stdout.toString()}${p.stderr.toString()}`; | |
| 62 | }); | |
| 63 | } finally { | |
| 64 | await rm(box, { recursive: true, force: true }); | |
| 65 | } | |
| 66 | } | |
| 67 | ||
| 68 | const OLD = "PK old forge"; | |
| 69 | const NEW = "PK new forge"; | |
| 70 | const OLD_JAR = jarName("forge-velocity-0.0.9", OLD); | |
| 71 | const NEW_JAR = jarName("forge-velocity-0.1.0", NEW); | |
| 72 | const list = (...names: string[]): string => JSON.stringify(names.map((name) => ({ name, sha256: "0".repeat(64), url: "" }))); | |
| 73 | ||
| 74 | on("a jar the forge put in plugins and PLUGINS no longer names is removed; the pinned one stays", async () => { | |
| 75 | await withBox("", (box, call) => { | |
| 76 | writeFileSync(join(box, ".jars.json"), list(NEW_JAR)); | |
| 77 | writeFileSync(join(box, "server/plugins", OLD_JAR), OLD); | |
| 78 | writeFileSync(join(box, "server/plugins", NEW_JAR), NEW); | |
| 79 | call("plugins_tidy"); | |
| 80 | expect(readdirSync(join(box, "server/plugins"))).toEqual([NEW_JAR]); | |
| 81 | expect(JSON.parse(readFileSync(join(box, ".plugins.tidy"), "utf8"))).toEqual({ removed: [OLD_JAR], stray: [] }); | |
| 82 | }); | |
| 83 | }); | |
| 84 | ||
| 85 | on("a hand-placed jar of a pinned plugin's name is kept and named; other jars are not touched", async () => { | |
| 86 | await withBox("", (box, call) => { | |
| 87 | writeFileSync(join(box, ".jars.json"), list(NEW_JAR)); | |
| 88 | writeFileSync(join(box, "server/plugins", "forge-velocity-custom.jar"), "mine"); | |
| 89 | // The right name shape with other bytes is a person's too. | |
| 90 | const faked = "forge-velocity-0.0.5-abcdef012345.jar"; | |
| 91 | writeFileSync(join(box, "server/plugins", faked), "not that hash"); | |
| 92 | writeFileSync(join(box, "server/plugins", "WorldEdit.jar"), "we"); | |
| 93 | call("plugins_tidy"); | |
| 94 | expect(readdirSync(join(box, "server/plugins")).sort()).toEqual([faked, "WorldEdit.jar", "forge-velocity-custom.jar"].sort()); | |
| 95 | expect(JSON.parse(readFileSync(join(box, ".plugins.tidy"), "utf8"))).toEqual({ removed: [], stray: ["forge-velocity-0.0.5-abcdef012345.jar", "forge-velocity-custom.jar"].sort() }); | |
| 96 | }); | |
| 97 | }); | |
| 98 | ||
| 99 | on("nothing is removed from plugins when the list of jars is empty or broken", async () => { | |
| 100 | await withBox("", (box, call) => { | |
| 101 | writeFileSync(join(box, "server/plugins", OLD_JAR), OLD); | |
| 102 | for (const text of ["", "[]", "not json"]) { | |
| 103 | writeFileSync(join(box, ".jars.json"), text); | |
| 104 | call("plugins_tidy"); | |
| 105 | expect(existsSync(join(box, "server/plugins", OLD_JAR))).toBe(true); | |
| 106 | } | |
| 107 | }); | |
| 108 | }); | |
| 109 | ||
| 110 | on("the report carries what the proxy's log says it loaded, and what was tidied", async () => { | |
| 111 | await withBox("[06:45:12 INFO]: Loaded plugin forge 0.0.9 by Forge\n[06:45:12 INFO]: Loaded plugin other 1 by Me\nnoise\n", (box, call) => { | |
| 112 | writeFileSync(join(box, ".jars.json"), list(NEW_JAR)); | |
| 113 | writeFileSync(join(box, ".plugins.tidy"), JSON.stringify({ removed: [OLD_JAR], stray: [] })); | |
| 114 | const said = JSON.parse(call("plugins_report")) as Record<string, unknown>; | |
| 115 | expect(said).toEqual({ | |
| 116 | named: [NEW_JAR], | |
| 117 | loaded: [ | |
| 118 | { id: "forge", version: "0.0.9" }, | |
| 119 | { id: "other", version: "1" }, | |
| 120 | ], | |
| 121 | removed: [OLD_JAR], | |
| 122 | stray: [], | |
| 123 | }); | |
| 124 | }); | |
| 125 | }); | |
| 126 | ||
| 127 | on("the report is empty for a proxy with no forge jars", async () => { | |
| 128 | await withBox("", (box, call) => { | |
| 129 | writeFileSync(join(box, ".jars.json"), ""); | |
| 130 | expect(call("plugins_report").trim()).toBe("{}"); | |
| 131 | }); | |
| 132 | }); | |
| 133 | ||
| 134 | on("the copy folder keeps what the list names and loses the rest", async () => { | |
| 135 | await withBox("", (box, call) => { | |
| 136 | const body = "PK the jar"; | |
| 137 | const name = jarName("forge-velocity-0.1.0", body); | |
| 138 | writeFileSync(join(box, "jars", name), body); | |
| 139 | writeFileSync(join(box, "jars", OLD_JAR), OLD); | |
| 140 | writeFileSync( | |
| 141 | join(box, ".jars.json"), | |
| 142 | JSON.stringify([{ name, sha256: sha(body), url: "" }]), | |
| 143 | ); | |
| 144 | call("jars_ready"); | |
| 145 | expect(readdirSync(join(box, "jars"))).toEqual([name]); | |
| 146 | }); | |
| 147 | }); |
test/game-proxy-jars.test.ts +29
| @@ -449,3 +449,32 @@ test("what the forge pins today, each file named by its sha256", () => { | ||
| 449 | 449 | expect(PINS.map((p) => p.title)).toEqual(["forge-velocity 0.1.0", "AutoStopper 3.0.0-forge.1"]); |
| 450 | 450 | for (const p of PINS) expect(p.name).toContain(p.sha256.slice(0, 12)); |
| 451 | 451 | }); |
| 452 | ||
| 453 | test("a new pin replaces the old jar of the same plugin in PLUGINS, and leaves other entries", async () => { | |
| 454 | const oldJar = pin("forge-velocity-0.0.9", "PK\u0003\u0004 old"); | |
| 455 | await put(COMPOSE.replace("a.jar", `a.jar,/forge-jars/${oldJar.name}`)); | |
| 456 | expect((await storeJar(app, box.id, new Blob([GOOD]), "x.jar")).ok).toBe(true); | |
| 457 | box = boxNamed(app.db, "mc")!; | |
| 458 | const plan = await planJar(app, box, TALK); | |
| 459 | if (!plan.ok) throw new Error(plan.error); | |
| 460 | expect(plan.value.lines).toContain(`PLUGINS: "https://example.test/a.jar,/forge-jars/${TALK.name}"`); | |
| 461 | expect(plan.value.gone.join("\n")).toContain(oldJar.name); | |
| 462 | }); | |
| 463 | ||
| 464 | test("the box's page says which version of the plugin the proxy loaded, and says so when it is not the pinned one (#18110)", async () => { | |
| 465 | const tok = createToken(app.db, ada.id, "r", "ro").token; | |
| 466 | const page = async () => (await http.handle(new Request(`${ORIGIN}/games/mc`, { headers: { authorization: `Bearer ${tok}` } }))).text(); | |
| 467 | const real = PINS[0]!; | |
| 468 | const said = (loaded: string) => ({ jars: true, diag: { plugins: { named: [real.name], loaded: [{ id: "forge", version: loaded }], removed: ["forge-velocity-0.0.9-aaaaaaaaaaaa.jar"], stray: ["forge-velocity-<mine>.jar"] } } }); | |
| 469 | await sync(said("0.0.9")); | |
| 470 | let html = await page(); | |
| 471 | expect(html).toContain("The proxy loaded forge 0.0.9, not the pinned forge-velocity 0.1.0."); | |
| 472 | expect(html).toContain("Taken out of the proxy's plugins"); | |
| 473 | expect(html).toContain("forge-velocity-0.0.9-aaaaaaaaaaaa.jar"); | |
| 474 | // A jar a person put there is left and named, escaped. | |
| 475 | expect(html).toContain("Left in the proxy's plugins"); | |
| 476 | await sync(said("0.1.0")); | |
| 477 | html = await page(); | |
| 478 | expect(html).toContain("The proxy loaded forge 0.1.0, the pinned version."); | |
| 479 | expect(html).not.toContain("not the pinned"); | |
| 480 | }); |
test/tui-games.test.ts +17
| @@ -18,6 +18,7 @@ import { WAKE_GRACE_MS } from "../src/games/heal.ts"; | ||
| 18 | 18 | import { lentRunnerOf } from "../src/games/lend.ts"; |
| 19 | 19 | import { boxNamed, createGameBox, grant, grantsOf, recentCommands, recordSync } from "../src/games/store.ts"; |
| 20 | 20 | import { queueWorld } from "../src/games/worlds.ts"; |
| 21 | import { PINS } from "../src/games/proxy_jars.ts"; | |
| 21 | 22 | import { createForgeApp } from "../src/tui/forge.ts"; |
| 22 | 23 | import { sha256 } from "../src/kernel/hash.ts"; |
| 23 | 24 | import { synced } from "../src/games/notify.ts"; |
| @@ -370,3 +371,19 @@ test("the terminal's box screen shows the switch Run checks while servers sleep | ||
| 370 | 371 | operator.tui.stop?.(); |
| 371 | 372 | } |
| 372 | 373 | }); |
| 374 | ||
| 375 | test("the terminal's box screen says which version of the forge's plugin the proxy loaded, as the page does (#18110)", async () => { | |
| 376 | const box = boxNamed(app.db, "net")!; | |
| 377 | const pinned = PINS[0]!; | |
| 378 | recordSync(app.db, box, { host: {}, diag: { plugins: { named: [pinned.name], loaded: [{ id: "forge", version: "0.0.9" }], removed: [], stray: ["forge-velocity-mine.jar"] } } }); | |
| 379 | const owner = drive(alice, 200); | |
| 380 | try { | |
| 381 | owner.tui.ctx.push(gameBoxScreen(owner.session, owner.tui.ctx, box)); | |
| 382 | await settleApp(owner.tui); | |
| 383 | const text = owner.tui.screen.toText(); | |
| 384 | expect(text).toContain("The proxy loaded forge 0.0.9, not the pinned forge-velocity 0.1.0."); | |
| 385 | expect(text).toContain("forge-velocity-mine.jar"); | |
| 386 | } finally { | |
| 387 | owner.tui.stop?.(); | |
| 388 | } | |
| 389 | }); |
new above
offline
If jq fails (empty or invalid $list, jq missing),
||runs rm and every jar is deleted. Remove only when jq ran fine and said the name is absent, e.g.r=$(jq ... ) && [ "$r" = false ] && rm -f. Also this prunes the forge'sjars/copy folder, not /server/plugins as box 2 asks.