From 1604bb295a514ac0292bbfc3657422b5171b4c05 Mon Sep 17 00:00:00 2001 From: JustKira Date: Fri, 20 Nov 2026 14:14:35 +0200 Subject: [PATCH 1/4] Build and reload indexed plugins with the Tokamak CLI --- app/scripts/integration-fixture.ts | 34 ++-- packages/cli/README.md | 20 ++- packages/cli/src/command.ts | 1 + packages/cli/src/commands.ts | 3 +- packages/cli/src/commands/add.ts | 4 +- packages/cli/src/commands/reload.ts | 104 ++++++++++++ packages/cli/src/commands/remove.ts | 4 +- packages/cli/src/main.ts | 6 +- packages/cli/src/server.ts | 38 ++++- packages/cli/tests/cli.test.mjs | 143 +++++++++++++++- packages/runtime/PLUGINS.md | 4 +- packages/server/PLUGINS.md | 9 +- packages/server/src/main.rs | 45 ++--- packages/server/src/plugins/catalog.rs | 158 +++++++++++++----- packages/server/src/plugins/mod.rs | 2 +- packages/server/src/service/mod.rs | 1 + packages/server/src/service/plugin_control.rs | 31 +++- packages/server/src/service/server.rs | 13 ++ packages/server/src/websocket.rs | 23 +++ packages/server/tests/plugins.rs | 33 ++-- 20 files changed, 544 insertions(+), 132 deletions(-) create mode 100644 packages/cli/src/commands/reload.ts diff --git a/app/scripts/integration-fixture.ts b/app/scripts/integration-fixture.ts index f4120f5e..c67fbbcc 100644 --- a/app/scripts/integration-fixture.ts +++ b/app/scripts/integration-fixture.ts @@ -4,10 +4,25 @@ import { resolve } from "node:path"; const root = resolve(import.meta.dirname, "../.."); const directory = resolve(process.env.TOKAMAK_PLUGIN_DIR ?? `${root}/app/.dev/integration/plugins`); -for (const plugin of [ - "plugins/chat", - process.env.TOKAMAK_FIXTURE_SOURCE ?? "app/tests/fixtures/integration", -]) { +const fixture = process.env.TOKAMAK_FIXTURE_SOURCE ?? "app/tests/fixtures/integration"; +if (process.argv.includes("--reload")) { + if (!process.env.TOKAMAK_SERVER_DIR) + throw new Error("Set TOKAMAK_SERVER_DIR to the disposable local server directory"); + for (const args of [ + ["add", fixture], + ["reload", "integration-fixture"], + ]) { + const result = spawnSync("proto", ["run", "bun", "--", "run", "tokamak", "plugin", ...args], { + cwd: root, + env: { ...process.env, TOKAMAK_SERVER: undefined, TOKAMAK_TOKEN: undefined }, + stdio: "inherit", + }); + if (result.error) throw result.error; + if (result.status !== 0) process.exit(result.status ?? 1); + } + process.exit(0); +} +for (const plugin of ["plugins/chat", fixture]) { const result = spawnSync("proto", ["run", "bun", "--", "packages/build/src/build.ts", plugin], { cwd: root, stdio: "inherit", @@ -18,15 +33,4 @@ for (const plugin of [ await mkdir(`${directory}/${id}`, { recursive: true }); await cp(resolve(root, plugin, "dist"), `${directory}/${id}/dist`, { recursive: true }); } -if (process.argv.includes("--reload")) { - const result = spawnSync( - `${root}/target/debug/tokamak-server`, - ["plugin", "reload", "integration-fixture"], - { - stdio: "inherit", - }, - ); - if (result.error) throw result.error; - process.exitCode = result.status ?? 1; -} console.log(`Integration plugins: ${directory}`); diff --git a/packages/cli/README.md b/packages/cli/README.md index 2f76d852..860890e2 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -8,13 +8,18 @@ proto run bun -- run tokamak plugin build /path/to/my-plugin --json proto run bun -- run tokamak plugin add /path/to/my-plugin proto run bun -- run tokamak plugin list --json proto run bun -- run tokamak plugin remove my-plugin +proto run bun -- run tokamak plugin reload my-plugin --json +proto run bun -- run tokamak plugin reload --json ``` `build` runs the artifact build once, defaults to the current folder, and doesn't need a server. `add` reads the plugin's static identity from `plugin.ts` (or the build's `package.json` convention), and doesn't build it. `add` and -`remove` only edit the index; the local server picks up membership changes on -its next start. +`remove` edit the index. `reload ` builds that plugin and asks the running +local server to load it. `reload` without an id builds all indexed plugins and +also applies removals. Failed builds and refused reloads leave the old version +serving, and successful builds can still load when another plugin fails. +Indexed plugins must build into their source folder's `dist` directory. The app data folder contains `plugin-index.json`, a JSON object mapping each plugin id to its absolute source folder, for example: @@ -42,14 +47,19 @@ nonzero on failure. The CLI never prompts. Success has `ok: true` and: | Command | Additional result fields | | --------------- | --------------------------------------------------- | | `plugin build` | `id`, `outputFolder` | -| `plugin add` | `id`, `folder`, `restartRequired: true` | -| `plugin remove` | `id`, `restartRequired: true` | +| `plugin add` | `id`, `folder`, `reloadRequired: true` | +| `plugin remove` | `id`, `reloadRequired: true` | +| `plugin reload` | `plugins`: one result per indexed plugin selected | | `plugin list` | `plugins`: an array of `{ id, version, fromIndex }` | | `--help` | `help` | Failures return `{ ok: false, error: { message, next } }`. Build failures with TypeScript locations also return `error.diagnostics`, an array of -`{ file, line, column, code, message }`. +`{ file, line, column, code, message }`. Reload returns `plugins` on success +and on build or load failure. Each result is `{ id, status: "loaded" }` or +`{ id, status, error: { message, diagnostics? } }` with status `build-failed` +or `load-failed`. A failed plugin makes the command exit nonzero even when +others loaded. To add a command, create a file under `src/commands/` that satisfies `Command`, then register it in `src/commands.ts`. The entry point handles flags, help, diff --git a/packages/cli/src/command.ts b/packages/cli/src/command.ts index 1e43140c..af52b7bc 100644 --- a/packages/cli/src/command.ts +++ b/packages/cli/src/command.ts @@ -11,6 +11,7 @@ export class CommandError extends Error { message: string, readonly next: string, readonly diagnostics?: object[], + readonly data?: { plugins: object[] }, ) { super(message); } diff --git a/packages/cli/src/commands.ts b/packages/cli/src/commands.ts index c914d9fc..6c98cc3b 100644 --- a/packages/cli/src/commands.ts +++ b/packages/cli/src/commands.ts @@ -3,5 +3,6 @@ import build from "./commands/build"; import add from "./commands/add"; import remove from "./commands/remove"; import list from "./commands/list"; +import reload from "./commands/reload"; -export const commands: readonly Command[] = [build, add, remove, list]; +export const commands: readonly Command[] = [build, add, remove, list, reload]; diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts index c961c666..7f6d3574 100644 --- a/packages/cli/src/commands/add.ts +++ b/packages/cli/src/commands/add.ts @@ -21,8 +21,8 @@ export default { await writeIndex(entries); } return { - data: { id, folder, restartRequired: true }, - text: `Indexed ${id} at ${folder}. The local server picks this up at its next start.`, + data: { id, folder, reloadRequired: true }, + text: `Indexed ${id} at ${folder}. Run tokamak plugin reload ${id} to apply it now.`, }; }, } satisfies Command; diff --git a/packages/cli/src/commands/reload.ts b/packages/cli/src/commands/reload.ts new file mode 100644 index 00000000..749e2ed4 --- /dev/null +++ b/packages/cli/src/commands/reload.ts @@ -0,0 +1,104 @@ +import { resolve } from "node:path"; +import { CommandError, type Command } from "../command"; +import { readIndex } from "../plugin-index"; +import { ServerRefusal, serverPost } from "../server"; +import build from "./build"; + +type PluginResult = + | { id: string; status: "loaded" } + | { + id: string; + status: "build-failed" | "load-failed"; + error: { message: string; diagnostics?: object[] }; + }; + +function failure(id: string, status: "build-failed" | "load-failed", cause: unknown): PluginResult { + const message = cause instanceof Error ? cause.message : String(cause); + const diagnostics = cause instanceof CommandError ? cause.diagnostics : undefined; + return { id, status, error: { message, ...(diagnostics ? { diagnostics } : {}) } }; +} + +async function applyBuilds( + ids: string[], + removeMissing: boolean, + results: Map, + server?: string, +) { + if (!ids.length && !removeMissing) return {}; + const reload = (selected: string[], remove: boolean) => + serverPost("/plugin-index/reload", { ids: selected, removeMissing: remove }, server); + const loaded = (id: string) => results.set(id, { id, status: "loaded" }); + try { + await reload(ids, removeMissing); + ids.forEach(loaded); + return {}; + } catch (error) { + const next = error instanceof CommandError ? error.next : undefined; + if (!(error instanceof ServerRefusal) || !removeMissing || !ids.length) { + for (const id of ids) results.set(id, failure(id, "load-failed", error)); + return { next, removalsError: removeMissing ? error : undefined }; + } + // Try a batch first so related versions can change together. If it is + // refused, isolate the builds so unrelated plugins can still load. + for (const id of ids) { + try { + await reload([id], false); + loaded(id); + } catch (error) { + results.set(id, failure(id, "load-failed", error)); + } + } + try { + await reload([], true); + return { next }; + } catch (removalsError) { + return { next, removalsError }; + } + } +} + +export default { + path: "plugin reload", + summary: "Build and reload indexed plugins in the running local server.", + args: { name: "id", optional: true }, + example: "tokamak plugin reload my-plugin --json", + async run([id], { server }) { + const index = await readIndex(); + const selected = id === undefined ? [...index.keys()] : [id]; + const results = new Map(); + const built: string[] = []; + for (const plugin of selected) { + const folder = index.get(plugin); + if (folder === undefined) + throw new CommandError(`Unknown indexed plugin: ${plugin}`, "tokamak plugin add"); + try { + const { data } = await build.run([folder]); + if (data.id !== plugin) throw new Error(`Build produced plugin ${data.id}, expected ${plugin}`); + if (data.outputFolder !== resolve(folder, "dist")) + throw new Error(`Indexed plugins must build into ${resolve(folder, "dist")}`); + built.push(plugin); + } catch (error) { + results.set(plugin, failure(plugin, "build-failed", error)); + } + } + + const { next = "tokamak plugin reload --help", removalsError } = await applyBuilds( + built, + id === undefined, + results, + server, + ); + const plugins = selected.map((plugin) => results.get(plugin)!); + const lines = plugins.map((plugin) => + plugin.status === "loaded" ? `Loaded ${plugin.id}` : `${plugin.id}: ${plugin.error.message}`, + ); + if (removalsError) lines.push(`Could not apply index removals: ${String(removalsError)}`); + if (plugins.some((plugin) => plugin.status !== "loaded") || removalsError) { + throw new CommandError(lines.join("\n"), next, undefined, { plugins }); + } + return { + data: { plugins }, + text: lines.join("\n") || "Applied index removals. No indexed plugins to build.", + }; + }, +} satisfies Command; diff --git a/packages/cli/src/commands/remove.ts b/packages/cli/src/commands/remove.ts index ff285181..96348512 100644 --- a/packages/cli/src/commands/remove.ts +++ b/packages/cli/src/commands/remove.ts @@ -11,8 +11,8 @@ export default { if (!entries.delete(id!)) throw new Error(`Unknown indexed plugin: ${id}`); await writeIndex(entries); return { - data: { id, restartRequired: true }, - text: `Removed ${id} from the index. The local server picks this up at its next start.`, + data: { id, reloadRequired: true }, + text: `Removed ${id} from the index. Run tokamak plugin reload to apply it now.`, }; }, } satisfies Command; diff --git a/packages/cli/src/main.ts b/packages/cli/src/main.ts index 06153cc5..c46aba67 100644 --- a/packages/cli/src/main.ts +++ b/packages/cli/src/main.ts @@ -65,7 +65,11 @@ try { if (error instanceof CommandError) next = error.next; const diagnostics = error instanceof CommandError ? error.diagnostics : undefined; output( - { ok: false, error: { message, next, ...(diagnostics ? { diagnostics } : {}) } }, + { + ok: false, + error: { message, next, ...(diagnostics ? { diagnostics } : {}) }, + ...(error instanceof CommandError ? error.data : undefined), + }, `${message}\nTry: ${next}`, ); process.exitCode = 1; diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index d6c46616..e857c21c 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -34,7 +34,18 @@ async function localEndpoint() { return { url: httpAddress(address), token }; } -export async function serverGet(path: string, explicit?: string): Promise { +export function serverGet(path: string, explicit?: string): Promise { + return serverRequest(path, explicit); +} + +export function serverPost(path: string, body: object, explicit?: string): Promise { + return serverRequest(path, explicit, body); +} + +/** A validated candidate was refused; the running installation is unchanged. */ +export class ServerRefusal extends CommandError {} + +async function serverRequest(path: string, explicit?: string, body?: object): Promise { const address = explicit ?? process.env.TOKAMAK_SERVER; const next = address ? "tokamak --help" : "tokamak server start"; try { @@ -43,18 +54,33 @@ export async function serverGet(path: string, explicit?: string): Promise { expect(JSON.parse(result.stdout)).toMatchObject({ ok: true, id: "outside-fixture", - restartRequired: true, + reloadRequired: true, }); } expect(JSON.parse(await readFile(index, "utf8"))).toEqual({ "outside-fixture": plugin }); @@ -73,7 +74,7 @@ test("adds and removes folders and protects an existing id", async () => { expect(JSON.parse(removed.stdout)).toMatchObject({ ok: true, id: "outside-fixture", - restartRequired: true, + reloadRequired: true, }); expect(JSON.parse(await readFile(index, "utf8"))).toEqual({}); expect(remove().code).not.toBe(0); @@ -92,7 +93,7 @@ test("every registered command shares help and JSON output rules", async () => { const { commands } = await import("../src/commands"); await withPlugin((_, root) => { expect(commands.map(({ path }) => path)).toEqual( - expect.arrayContaining(["plugin build", "plugin add", "plugin remove", "plugin list"]), + expect.arrayContaining(["plugin build", "plugin add", "plugin remove", "plugin list", "plugin reload"]), ); for (const path of ["", "plugin", ...commands.map((command) => command.path)]) { const words = path ? path.split(" ") : []; @@ -145,7 +146,7 @@ async function withServer(root, check) { let address; const serverErrors = new Response(server.stderr).text(); try { - const deadline = Date.now() + 35_000; + const deadline = Date.now() + 90_000; while (true) { const ready = await readFile(join(data, "server.json"), "utf8").catch((error) => { if (error.code !== "ENOENT") throw error; @@ -208,4 +209,136 @@ test("a real local server loads and removes an indexed plugin on restart", async expect(run(["list"])).toEqual({ ok: true, plugins: [] }); }); }); -}, 120_000); +}, 180_000); + +test("reload tells the user how to add an unknown plugin", async () => { + await withPlugin((_, root) => { + const result = cli(root, ["plugin", "reload", "missing", "--json"]); + expect(result.code).not.toBe(0); + expect(JSON.parse(result.stdout)).toEqual({ + ok: false, + error: { message: expect.stringContaining("missing"), next: "tokamak plugin add" }, + }); + }); +}); + +test("reload applies index additions and removals without a restart", async () => { + await withPlugin(async (plugin, root) => { + const run = (...args) => { + const result = cli(root, ["plugin", ...args, "--json"]); + expect(result.code, result.stderr).toBe(0); + return JSON.parse(result.stdout); + }; + await withServer(root, () => { + run("add", plugin); + expect(run("reload", "outside-fixture")).toMatchObject({ + plugins: [{ id: "outside-fixture", status: "loaded" }], + }); + expect(run("list").plugins).toEqual([{ id: "outside-fixture", version: "0.1.0", fromIndex: true }]); + run("remove", "outside-fixture"); + expect(run("reload").plugins).toEqual([]); + expect(run("list").plugins).toEqual([]); + }); + }); +}, 180_000); + +test("reload updates an existing connection and preserves old code on build or load failure", async () => { + await withPlugin(async (plugin, root) => { + const peer = join(root, "peer"); + await cp(plugin, peer, { recursive: true }); + const descriptor = join(peer, "plugin.ts"); + await writeFile( + descriptor, + (await readFile(descriptor, "utf8")).replace("outside-fixture", "reload-peer"), + ); + const notes = join(plugin, "src/server/notes.ts"); + const original = await readFile(notes, "utf8"); + const marker = (source, value) => + source + `\nexport const marker = query({ handler: () => ${JSON.stringify(value)} });\n`; + await writeFile(notes, marker(original, "old")); + await writeFile(join(peer, "src/server/notes.ts"), marker(original, "peer")); + const run = (...args) => { + const result = cli(root, ["plugin", ...args, "--json"]); + expect(result.code, result.stderr).toBe(0); + return JSON.parse(result.stdout); + }; + run("build", plugin); + run("add", plugin); + await withServer(root, async (address, token) => { + const socket = new WebSocket(address.replace(/^http:/, "ws:") + "/ws?token=" + token); + let transport; + try { + await new Promise((resolve, reject) => { + const signal = AbortSignal.timeout(30_000); + signal.addEventListener("abort", () => reject(signal.reason), { once: true }); + socket.addEventListener("open", resolve, { once: true, signal }); + socket.addEventListener("error", () => reject(new Error("WebSocket startup failed")), { + once: true, + signal, + }); + }); + transport = new WebSocketTransport(socket); + const client = new TokamakClient(transport); + const api = createApi("outside-fixture"); + const read = () => client.query(api.notes.marker, {}); + expect(await read()).toBe("old"); + await writeFile(notes, marker(original, "edited")); + expect(run("reload", "outside-fixture")).toEqual({ + ok: true, + plugins: [{ id: "outside-fixture", status: "loaded" }], + }); + expect(await read()).toBe("edited"); + + for (let i = 0; i < 2; i++) await client.mutation(api.notes.add, { text: "duplicate" }); + const schema = join(plugin, "src/server/schema.ts"); + await writeFile( + schema, + (await readFile(schema, "utf8")).replace( + "defineTable({ text: v.string() })", + 'defineTable({ text: v.string() }).unique("by_text", ["text"])', + ), + ); + await writeFile(notes, marker(original, "refused")); + const refused = cli(root, ["plugin", "reload", "outside-fixture", "--json"]); + expect(refused.code).not.toBe(0); + expect(JSON.parse(refused.stdout)).toMatchObject({ + ok: false, + plugins: [ + { + id: "outside-fixture", + status: "load-failed", + error: { message: expect.stringContaining("by_text") }, + }, + ], + }); + expect(await read()).toBe("edited"); + + await writeFile(join(plugin, "src/server/broken.ts"), "\nexport const broken: string = 42;\n"); + const broken = cli(root, ["plugin", "reload", "outside-fixture", "--json"]); + const diagnostic = { file: expect.stringMatching(/broken\.ts$/), line: 2, code: "TS2322" }; + expect(broken.code).not.toBe(0); + expect(JSON.parse(broken.stdout)).toMatchObject({ + ok: false, + plugins: [{ id: "outside-fixture", status: "build-failed", error: { diagnostics: [diagnostic] } }], + }); + expect(await read()).toBe("edited"); + + run("add", peer); + const batch = cli(root, ["plugin", "reload", "--json"]); + expect(batch.code).not.toBe(0); + expect(JSON.parse(batch.stdout)).toMatchObject({ + ok: false, + plugins: [ + { id: "outside-fixture", status: "build-failed", error: { diagnostics: [diagnostic] } }, + { id: "reload-peer", status: "loaded" }, + ], + }); + expect(await read()).toBe("edited"); + expect(await client.query(createApi("reload-peer").notes.marker, {})).toBe("peer"); + } finally { + if (transport) transport.close(); + else socket.close(); + } + }); + }); +}, 180_000); diff --git a/packages/runtime/PLUGINS.md b/packages/runtime/PLUGINS.md index 95ff7172..a43d3a6b 100644 --- a/packages/runtime/PLUGINS.md +++ b/packages/runtime/PLUGINS.md @@ -37,8 +37,8 @@ and reload/invalidation. The app's `WebPluginHost` fetches modules and rewrites cross-plugin and relative imports to blobs owned by the plugin build. Host SDK/Solid contexts are shared through one import map; no eval is needed. Reload remounts the views of changed plugins and their window importers, releasing their blobs, -stylesheets and query owners. Publish complete builds before requesting `tokamak plugin reload `; -there is no automatic file watcher. The server rejects asset requests for retired builds. +stylesheets and query owners. Run `tokamak plugin reload ` to build and reload an indexed plugin. +There is no automatic file watcher. The server rejects asset requests for retired builds. `createPluginShell` and `PluginViews` in `app/src/plugins/views.tsx` expose a reactive view list, navigation and stage rendering, and register namespaced diff --git a/packages/server/PLUGINS.md b/packages/server/PLUGINS.md index df5403d3..9a66d988 100644 --- a/packages/server/PLUGINS.md +++ b/packages/server/PLUGINS.md @@ -23,7 +23,8 @@ launcher. Never pass the token in arguments. The launcher now uses the workspace ```sh TOKAMAK_SERVER_DIR=path/to/private-server target/debug/tokamak-server plugin list -TOKAMAK_SERVER_DIR=path/to/private-server target/debug/tokamak-server plugin reload acme.notes +TOKAMAK_SERVER_DIR=path/to/private-server proto run bun -- run tokamak plugin add path/to/plugins/notes +TOKAMAK_SERVER_DIR=path/to/private-server proto run bun -- run tokamak plugin reload acme.notes ``` The control CLI validates loopback discovery, uses bearer authentication, disables @@ -31,6 +32,7 @@ proxies and redirects, and does not replay requests. Control endpoints are only registered in capability-protected local mode: - `GET /plugins`: live manifest/function metadata and generation revision. +- `POST /plugin-index/reload`: validate and load the selected indexed builds and optionally apply index removals. - `POST /plugins/{id}/reload`: validate and reload the configured installation. - `GET /plugins/{id}/assets/{name}`: declared UI files from the verified snapshot. Never arbitrary files, backend source, capability tokens or generated backend API. @@ -75,7 +77,8 @@ first. The full candidate installation resolves and is inspected before one generation swap. Failed reload leaves old code, subscriptions and data usable. Successful reload dirties all subscriptions; removed or changed-kind queries produce explicit subscription errors. The named ID must be present in the old or -new installation; dependent callers reload together. This is not stateful HMR. +new installation for `/plugins/{id}/reload`. The CLI submits only indexed builds +that succeeded. Unchanged plugin workers keep running. On Unix, unchanged files reuse verified snapshots only when canonical path, device/inode, size, mtime and ctime still match. Changed files are rehashed, so an unchanged manifest cannot conceal an in-place write or atomic replacement. @@ -138,7 +141,7 @@ wildcard (see `packages/engine/WEB_APIS.md`). Actions run on a separate worker lane and reach data only through `ctx.runQuery` / `ctx.runMutation`, which apply the same dependency, interface and export checks as nested calls. Queries and mutations cannot call actions or `fetch`, so they gain no external effects that -could repeat during transaction retries. Reload cancels old-generation actions. +could repeat during transaction retries. Reload cancels actions belonging to changed or removed plugin builds. An action result is one wire response under the same inclusive 1 MiB limit: an oversized result fails only that call. No Node/Bun runtime, plugin grant prompt, marketplace, signing, Wasm or untrusted sandbox ships here. diff --git a/packages/server/src/main.rs b/packages/server/src/main.rs index a074b0e6..0ac9cd00 100644 --- a/packages/server/src/main.rs +++ b/packages/server/src/main.rs @@ -73,31 +73,16 @@ fn capability(directory: &Path) -> Result> { } async fn plugin_command(args: Vec) -> Result<(), Box> { - let path = match args.as_slice() { - [command] if command == "list" => "/plugins".to_owned(), - [command, id] if command == "reload" => { - server::data::DataSpace::plugin(id)?; - format!("/plugins/{id}/reload") - } - _ => { - return Err( - "Expected: tokamak-server plugin list | tokamak-server plugin reload ".into(), - ); - } - }; - let body = if args[0] == "list" { - None - } else { - Some(serde_json::json!({})) - }; - println!("{}", local_request(&path, body).await?); + if args != ["list"] { + return Err( + "Expected: tokamak-server plugin list. For reload, run tokamak plugin reload.".into(), + ); + } + println!("{}", local_request("/plugins").await?); Ok(()) } -async fn local_request( - path: &str, - body: Option, -) -> Result> { +async fn local_request(path: &str) -> Result> { let directory = local_directory()?; let identity: serde_json::Value = serde_json::from_slice(&std::fs::read(directory.join("server.json"))?)?; @@ -125,15 +110,11 @@ async fn local_request( .redirect(reqwest::redirect::Policy::none()) .timeout(Duration::from_secs(30)) .build()?; - let request = if let Some(body) = body { - client - .post(url) - .header("content-type", "application/json") - .body(body.to_string()) - } else { - client.get(url) - }; - let response = request.bearer_auth(capability(&directory)?).send().await?; + let response = client + .get(url) + .bearer_auth(capability(&directory)?) + .send() + .await?; let status = response.status(); let body = response.text().await?; if !status.is_success() { @@ -180,7 +161,7 @@ async fn configuration() -> Result, Box | --bundle ) [--local-dir ] [--local-path] [--build-id] [--data ] [--listen <127.0.0.1:3000>] [--web ] [--workers ] [--queue ] [--heap-bytes ] [--call-ms ] [--load-ms ]\nInvites: tokamak-server invite | members | revoke --server [--token ] (or TOKAMAK_TOKEN)\nControl: tokamak-server plugin list | tokamak-server plugin reload (TOKAMAK_SERVER_DIR selects local server)" + "tokamak-server (--plugins | --bundle ) [--local-dir ] [--local-path] [--build-id] [--data ] [--listen <127.0.0.1:3000>] [--web ] [--workers ] [--queue ] [--heap-bytes ] [--call-ms ] [--load-ms ]\nInvites: tokamak-server invite | members | revoke --server [--token ] (or TOKAMAK_TOKEN)\nControl: tokamak-server plugin list (TOKAMAK_SERVER_DIR selects local server)" ); return Ok(None); } diff --git a/packages/server/src/plugins/catalog.rs b/packages/server/src/plugins/catalog.rs index e2ce257a..35de0953 100644 --- a/packages/server/src/plugins/catalog.rs +++ b/packages/server/src/plugins/catalog.rs @@ -12,7 +12,7 @@ impl Catalog { previous .map(|p| p.directory.as_path()) .unwrap_or(root.parent().unwrap_or(root)), - None, + previous.and_then(|p| p.index_path.as_deref()), ) } @@ -22,52 +22,75 @@ impl Catalog { directory: &Path, index_path: Option<&Path>, ) -> Result { - let directories = if let Some(directory) = artifact_directory(root) { - vec![directory] - } else { - let mut result = Vec::new(); - - for entry in std::fs::read_dir(root).map_err(|e| invalid(e.to_string()))? { - let entry = entry.map_err(|e| invalid(e.to_string()))?; - - if entry.file_name().to_string_lossy().starts_with('.') { - continue; - } + Self::read_selection(root, previous, directory, index_path, None) + } - let path = entry.path(); + pub(crate) fn read_indexed_reload( + root: &Path, + previous: &Self, + ids: &[String], + remove_missing: bool, + ) -> Result { + if previous.index_path.is_none() { + return Err(invalid("Server has no local plugin index")); + } + Self::read_selection( + root, + Some(previous), + &previous.directory, + previous.index_path.as_deref(), + Some((ids, remove_missing)), + ) + } - if let Some(directory) = artifact_directory(&path) { - result.push(directory); - } else if path.join("package.json").is_file() { - return Err(invalid(format!( - "Plugin has not been built: {}", - path.display() - ))); + fn read_selection( + root: &Path, + previous: Option<&Self>, + directory: &Path, + index_path: Option<&Path>, + selection: Option<(&[String], bool)>, + ) -> Result { + let index = read_index(index_path)?; + let removed: BTreeSet<_> = previous + .into_iter() + .filter(|_| selection.is_some_and(|(_, remove)| remove)) + .flat_map(|old| &old.indexed) + .filter(|id| !index.contains_key(*id)) + .collect(); + let mut sources = installed_sources(root, selection.map(|_| &removed))?; + let mut plugins = BTreeMap::new(); + let indexed = if let Some((ids, _)) = selection { + let mut indexed = BTreeSet::new(); + let selected: BTreeSet<_> = ids.iter().collect(); + if let Some(old) = previous { + // A plugin that did not build keeps its verified artifact even + // if its source folder or on-disk build no longer exists. + let untouched = old + .plugins + .iter() + .filter(|(id, _)| !selected.contains(id) && !removed.contains(id)); + for (id, artifact) in untouched { + plugins.insert(id.clone(), Arc::clone(artifact)); } + indexed.extend( + old.indexed + .iter() + .filter(|id| plugins.contains_key(*id)) + .cloned(), + ); } - result.sort(); - result - }; - - // Keep index membership fixed for this server lifetime, even across a - // reload through the server's existing plugin controls. - let index = match previous { - Some(catalog) => Arc::clone(&catalog.index), - None => Arc::new(read_index(index_path)?), - }; - let mut sources = BTreeMap::new(); - for directory in directories { - let id = Manifest::read(&directory)?.0.plugin.id; - if sources.insert(id.clone(), directory).is_some() { - return Err(invalid(format!( - "Duplicate installed plugin identity: {id}" - ))); + for id in ids { + let folder = index + .get(id) + .ok_or_else(|| invalid(format!("Plugin is not indexed: {id}")))?; + sources.insert(id.clone(), folder.join("dist")); + indexed.insert(id.clone()); } - } - - let indexed = select_indexed_sources(&index, &mut sources)?; + indexed + } else { + select_indexed_sources(&index, &mut sources)? + }; - let mut plugins = BTreeMap::new(); for (id, directory) in sources { let old = previous.and_then(|p| p.plugins.get(&id)); let artifact = Artifact::read_reusing( @@ -90,6 +113,7 @@ impl Catalog { for id in &order { hash.update(plugins[id].build_id.as_bytes()); + hash.update([u8::from(indexed.contains(id))]); } let mut providers = BTreeMap::>::new(); @@ -110,7 +134,7 @@ impl Catalog { plugins, revision: format!("{:x}", hash.finalize()), providers, - index, + index_path: index_path.map(Path::to_owned), indexed, environment, directory: directory.to_owned(), @@ -182,6 +206,56 @@ fn artifact_directory(root: &Path) -> Option { } } +fn installed_sources( + root: &Path, + only: Option<&BTreeSet<&String>>, +) -> Result, Error> { + if only.is_some_and(BTreeSet::is_empty) { + return Ok(BTreeMap::new()); + } + let directories = if let Some(directory) = artifact_directory(root) { + vec![directory] + } else { + let mut result = Vec::new(); + + for entry in std::fs::read_dir(root).map_err(|e| invalid(e.to_string()))? { + let entry = entry.map_err(|e| invalid(e.to_string()))?; + + if entry.file_name().to_string_lossy().starts_with('.') { + continue; + } + + let path = entry.path(); + + if let Some(directory) = artifact_directory(&path) { + result.push(directory); + } else if only.is_none() && path.join("package.json").is_file() { + return Err(invalid(format!( + "Plugin has not been built: {}", + path.display() + ))); + } + } + result.sort(); + result + }; + + let mut sources = BTreeMap::new(); + for directory in directories { + let id = Manifest::read(&directory)?.0.plugin.id; + if only.is_some_and(|ids| !ids.contains(&id)) { + continue; + } + if sources.insert(id.clone(), directory).is_some() { + return Err(invalid(format!( + "Duplicate installed plugin identity: {id}" + ))); + } + } + + Ok(sources) +} + fn read_index(path: Option<&Path>) -> Result, Error> { let Some(path) = path else { return Ok(BTreeMap::new()); diff --git a/packages/server/src/plugins/mod.rs b/packages/server/src/plugins/mod.rs index d4ac069b..5ac2ab9a 100644 --- a/packages/server/src/plugins/mod.rs +++ b/packages/server/src/plugins/mod.rs @@ -27,7 +27,7 @@ pub struct Catalog { pub plugins: BTreeMap>, pub revision: String, providers: BTreeMap>, - index: Arc>, + index_path: Option, indexed: BTreeSet, pub(crate) environment: PathBuf, pub(crate) directory: PathBuf, diff --git a/packages/server/src/service/mod.rs b/packages/server/src/service/mod.rs index 257aca2c..f0ff0206 100644 --- a/packages/server/src/service/mod.rs +++ b/packages/server/src/service/mod.rs @@ -112,6 +112,7 @@ pub(crate) enum Command { Action(u64, crate::identity::User, Action), Plugins(oneshot::Sender>), Reload(String, oneshot::Sender>), + ReloadIndexed(Vec, bool, oneshot::Sender>), RunFunction(crate::actions::NestedCall), Asset( Option, diff --git a/packages/server/src/service/plugin_control.rs b/packages/server/src/service/plugin_control.rs index 728fb5b7..cf485e25 100644 --- a/packages/server/src/service/plugin_control.rs +++ b/packages/server/src/service/plugin_control.rs @@ -79,11 +79,36 @@ impl Coordinator { let _ = reply.send(result); } + Command::ReloadIndexed(ids, remove_missing, reply) => { + let _ = reply.send(self.reload_indexed(&ids, remove_missing).await); + } _ => unreachable!("application commands have their own admission path"), } } - pub(super) async fn reload(&mut self, id: &str) -> Result { + async fn reload_indexed( + &mut self, + ids: &[String], + remove_missing: bool, + ) -> Result { + let root = self + .plugin_root + .as_ref() + .ok_or_else(|| Error::Invalid("No plugin installation to reload".into()))?; + let previous = self + .plugins + .as_ref() + .ok_or_else(|| Error::Invalid("No plugin installation to reload".into()))?; + let catalog = crate::plugins::Catalog::read_indexed_reload( + root, + &previous.catalog, + ids, + remove_missing, + )?; + self.swap_catalog(catalog).await + } + + async fn reload(&mut self, id: &str) -> Result { let root = self .plugin_root .as_ref() @@ -101,6 +126,10 @@ impl Coordinator { return Err(Error::Invalid(format!("Plugin is not installed: {id}"))); } + self.swap_catalog(catalog).await + } + + async fn swap_catalog(&mut self, catalog: crate::plugins::Catalog) -> Result { if self .plugins .as_ref() diff --git a/packages/server/src/service/server.rs b/packages/server/src/service/server.rs index 5b9ae124..ab21193c 100644 --- a/packages/server/src/service/server.rs +++ b/packages/server/src/service/server.rs @@ -19,6 +19,19 @@ impl Server { wait.await.map_err(|_| Error::Closed)? } + pub(crate) async fn reload_indexed( + &self, + ids: Vec, + remove_missing: bool, + ) -> Result { + let (reply, wait) = oneshot::channel(); + admit( + &self.commands, + Command::ReloadIndexed(ids, remove_missing, reply), + )?; + wait.await.map_err(|_| Error::Closed)? + } + pub async fn plugin_asset( &self, id: String, diff --git a/packages/server/src/websocket.rs b/packages/server/src/websocket.rs index 891666ed..f574ee10 100644 --- a/packages/server/src/websocket.rs +++ b/packages/server/src/websocket.rs @@ -184,6 +184,9 @@ pub async fn serve_with_identities( .route("/shutdown", post(stop)) .route("/plugins/{id}/reload", post(reload_plugin)); } + if !hosted && access.is_some() { + app = app.route("/plugin-index/reload", post(reload_indexed)); + } let app = app .layer(middleware::from_fn_with_state(adapter.clone(), authorize)) .with_state(adapter); @@ -399,6 +402,26 @@ async fn plugins(State(adapter): State) -> Response { async fn reload_plugin(State(adapter): State, Path(id): Path) -> Response { plugin_result(adapter.server.reload_plugin(id).await) } +#[derive(serde::Deserialize)] +#[serde(rename_all = "camelCase")] +struct IndexedReload { + ids: Vec, + remove_missing: bool, +} + +async fn reload_indexed(State(adapter): State, body: String) -> Response { + let input: IndexedReload = match serde_json::from_str(&body) { + Ok(input) => input, + Err(error) => return plugin_result(Err(crate::Error::Invalid(error.to_string()))), + }; + plugin_result( + adapter + .server + .reload_indexed(input.ids, input.remove_missing) + .await, + ) +} + async fn plugin_asset( State(adapter): State, Path((id, revision, name)): Path<(String, String, String)>, diff --git a/packages/server/tests/plugins.rs b/packages/server/tests/plugins.rs index 06d1444e..bdf12185 100644 --- a/packages/server/tests/plugins.rs +++ b/packages/server/tests/plugins.rs @@ -804,20 +804,25 @@ fn assert_authenticated_plugin_controls(base: &str, binary: &str, local: &std::p 404, "backend source must never be a client asset" ); - for command in [vec!["plugin", "list"], vec!["plugin", "reload", "b"]] { - let output = std::process::Command::new(binary) - .args(command) - .env("TOKAMAK_SERVER_DIR", local) - .output() - .unwrap(); - assert!( - output.status.success(), - "{}", - String::from_utf8_lossy(&output.stderr) - ); - let metadata: Value = serde_json::from_slice(&output.stdout).unwrap(); - assert_eq!(metadata["plugins"].as_array().unwrap().len(), 2); - } + let output = std::process::Command::new(binary) + .args(["plugin", "list"]) + .env("TOKAMAK_SERVER_DIR", local) + .output() + .unwrap(); + assert!( + output.status.success(), + "{}", + String::from_utf8_lossy(&output.stderr) + ); + let metadata: Value = serde_json::from_slice(&output.stdout).unwrap(); + assert_eq!(metadata["plugins"].as_array().unwrap().len(), 2); + let old_command = std::process::Command::new(binary) + .args(["plugin", "reload", "b"]) + .env("TOKAMAK_SERVER_DIR", local) + .output() + .unwrap(); + assert!(!old_command.status.success()); + assert!(String::from_utf8_lossy(&old_command.stderr).contains("tokamak plugin reload")); // Shutdown may close the HTTP acknowledgement while stopping sockets. let _ = client .post(format!("{base}/shutdown")) From 63ddda55b8146b60ae627db1c817b49ffd22c036 Mon Sep 17 00:00:00 2001 From: JustKira Date: Fri, 20 Nov 2026 15:26:30 +0200 Subject: [PATCH 2/4] Verify the exact build and reconcile uncertain reload replies --- app/scripts/integration-fixture.ts | 2 +- e2e/browser/app.spec.ts | 32 +-- packages/cli/src/command.ts | 2 +- packages/cli/src/commands/build.ts | 44 ++- packages/cli/src/commands/reload.ts | 261 ++++++++++++------ packages/cli/src/plugin-index.ts | 30 +- packages/cli/src/server.ts | 35 ++- packages/cli/tests/cli.test.mjs | 253 ++++++++++++++++- packages/server/src/plugins/artifact.rs | 1 + packages/server/src/plugins/catalog.rs | 76 ++++- packages/server/src/plugins/loading.rs | 8 +- packages/server/src/plugins/mod.rs | 21 +- packages/server/src/service/mod.rs | 5 +- packages/server/src/service/plugin_control.rs | 39 ++- packages/server/src/service/server.rs | 8 +- packages/server/src/websocket.rs | 16 +- 16 files changed, 643 insertions(+), 190 deletions(-) diff --git a/app/scripts/integration-fixture.ts b/app/scripts/integration-fixture.ts index c67fbbcc..a576cefd 100644 --- a/app/scripts/integration-fixture.ts +++ b/app/scripts/integration-fixture.ts @@ -12,7 +12,7 @@ if (process.argv.includes("--reload")) { ["add", fixture], ["reload", "integration-fixture"], ]) { - const result = spawnSync("proto", ["run", "bun", "--", "run", "tokamak", "plugin", ...args], { + const result = spawnSync("proto", ["run", "bun", "--", "run", "--silent", "tokamak", "plugin", ...args], { cwd: root, env: { ...process.env, TOKAMAK_SERVER: undefined, TOKAMAK_TOKEN: undefined }, stdio: "inherit", diff --git a/e2e/browser/app.spec.ts b/e2e/browser/app.spec.ts index 9ab6cce5..3837dd98 100644 --- a/e2e/browser/app.spec.ts +++ b/e2e/browser/app.spec.ts @@ -1,6 +1,6 @@ import { test, expect, type Page } from "@playwright/test"; import { spawnSync } from "node:child_process"; -import { cp, readFile, writeFile } from "node:fs/promises"; +import { readFile, writeFile } from "node:fs/promises"; const connected = (page: Page) => expect(page.getByRole("status", { name: "Connection status" })).toHaveText("127.0.0.1:43123"); @@ -14,7 +14,6 @@ const fixtureHome = "/p/integration-fixture"; test("live threads, compiler views, regions, per-plugin reload and error containment", async ({ page, context, - request, }) => { await page.goto(fixtureHome); await connected(page); @@ -54,27 +53,18 @@ test("live threads, compiler views, regions, per-plugin reload and error contain }, sentinel); const source = `${process.env.TOKAMAK_FIXTURE_SOURCE}/src/client/api.tsx`; const revision = `Fixture revision ${crypto.randomUUID()}`; - await writeFile(source, (await readFile(source, "utf8")).replace(/Fixture revision [^<]+/, revision)); - const built = spawnSync( - "proto", - ["run", "bun", "--", "packages/build/src/build.ts", process.env.TOKAMAK_FIXTURE_SOURCE!], - { stdio: "inherit" }, + // The new contribution name proves the other window applied the catalog. + const throwingTitle = `ThrowingView${crypto.randomUUID().replaceAll("-", "")}`; + await writeFile( + source, + (await readFile(source, "utf8")) + .replace(/Fixture revision [^<]+/, revision) + .replace(/export const ThrowingView\w* = view/, `export const ${throwingTitle} = view`), ); - expect(built.status).toBe(0); - await cp(`${process.env.TOKAMAK_FIXTURE_SOURCE}/dist`, `${process.env.TOKAMAK_PLUGIN_DIR}/fixture/dist`, { - recursive: true, + const reload = spawnSync("proto", ["run", "bun", "--", "app/scripts/integration-fixture.ts", "--reload"], { + encoding: "utf8", }); - const manifestPath = `${process.env.TOKAMAK_PLUGIN_DIR}/fixture/dist/manifest.json`; - const manifest = JSON.parse(await readFile(manifestPath, "utf8")); - // This link shows that the other window has applied the new catalog. - const throwingTitle = `ThrowingView ${crypto.randomUUID()}`; - manifest.plugin.contributes.view.find((view: { id: string }) => view.id === "ThrowingView").title = - throwingTitle; - await writeFile(manifestPath, JSON.stringify(manifest)); - const reload = await request.post("http://127.0.0.1:43123/plugins/integration-fixture/reload", { - headers: { Authorization: `Bearer ${process.env.VITE_TOKAMAK_TOKEN}` }, - }); - expect(reload.ok()).toBe(true); + expect(reload.status, reload.stderr + reload.stdout).toBe(0); await expect(fixture.getByRole("heading", { name: revision, exact: true })).toBeVisible(); expect( await page.evaluate(() => (window as unknown as { documentSentinel: string }).documentSentinel), diff --git a/packages/cli/src/command.ts b/packages/cli/src/command.ts index 7a2c76dd..a71e8c30 100644 --- a/packages/cli/src/command.ts +++ b/packages/cli/src/command.ts @@ -29,7 +29,7 @@ export class CommandError extends Error { message: string, readonly next: string, readonly diagnostics?: object[], - readonly data?: { plugins: object[] }, + readonly data?: object, ) { super(message); } diff --git a/packages/cli/src/commands/build.ts b/packages/cli/src/commands/build.ts index b8356205..e334a98f 100644 --- a/packages/cli/src/commands/build.ts +++ b/packages/cli/src/commands/build.ts @@ -1,7 +1,8 @@ -import { readFile } from "node:fs/promises"; +import { readFile, realpath } from "node:fs/promises"; import { resolve } from "node:path"; import { CommandError, type Command } from "../command"; import { runBuilder } from "../plugin-build"; +import { acquireLock } from "../plugin-index"; export default { path: "plugin build", @@ -9,19 +10,32 @@ export default { args: { name: "folder", optional: true }, example: "tokamak plugin build . --json", async run([folder]) { - const { outputFolder } = await runBuilder(resolve(folder ?? "."), "--result-json"); - if (!outputFolder) throw new Error("The build did not report its output folder"); - const id = await readFile(resolve(outputFolder, "manifest.json"), "utf8") - .then((source) => { - const id: unknown = JSON.parse(source)?.plugin?.id; - if (typeof id !== "string" || !id) throw new Error("The build manifest has no plugin id"); - return id; - }) - .catch((error: Error) => { - throw new CommandError("Plugin build failed", "tokamak plugin build --help", [ - { message: `Could not read the build manifest: ${error.message}` }, - ]); - }); - return { data: { id, outputFolder }, text: `Built ${id}: ${outputFolder}` }; + const root = await realpath(resolve(folder ?? ".")).catch(() => resolve(folder ?? ".")); + // Let the builder report missing folders using its structured diagnostics. + const release = await acquireLock(root, ".tokamak-build").catch((error) => { + if (error.code !== "ENOENT") throw error; + }); + try { + return await buildInFolder(root); + } finally { + await release?.(); + } }, } satisfies Command; + +export async function buildInFolder(folder: string) { + const { outputFolder } = await runBuilder(folder, "--result-json"); + if (!outputFolder) throw new Error("The build did not report its output folder"); + const id = await readFile(resolve(outputFolder, "manifest.json"), "utf8") + .then((source) => { + const id: unknown = JSON.parse(source)?.plugin?.id; + if (typeof id !== "string" || !id) throw new Error("The build manifest has no plugin id"); + return id; + }) + .catch((error: Error) => { + throw new CommandError("Plugin build failed", "tokamak plugin build --help", [ + { message: `Could not read the build manifest: ${error.message}` }, + ]); + }); + return { data: { id, outputFolder }, text: `Built ${id}: ${outputFolder}` }; +} diff --git a/packages/cli/src/commands/reload.ts b/packages/cli/src/commands/reload.ts index 749e2ed4..05136783 100644 --- a/packages/cli/src/commands/reload.ts +++ b/packages/cli/src/commands/reload.ts @@ -1,104 +1,209 @@ -import { resolve } from "node:path"; +import { createHash } from "node:crypto"; +import { readFile, realpath } from "node:fs/promises"; +import { join } from "node:path"; import { CommandError, type Command } from "../command"; -import { readIndex } from "../plugin-index"; -import { ServerRefusal, serverPost } from "../server"; -import build from "./build"; +import { acquireLock, readIndex } from "../plugin-index"; +import { ServerRefusal, serverGet, serverPost } from "../server"; +import { buildInFolder } from "./build"; -type PluginResult = - | { id: string; status: "loaded" } - | { - id: string; - status: "build-failed" | "load-failed"; - error: { message: string; diagnostics?: object[] }; - }; +type Build = { id: string; buildId: string; depends: string[] }; +type Result = { + id: string; + buildId: string | null; + status: "loaded" | "unchanged" | "build-failed" | "load-failed" | "unknown"; + error?: { message: string; diagnostics?: object[] }; +}; +type Catalog = { + plugins: { plugin: { id: string }; revision: string }[]; + unchanged?: string[]; + removed?: string[]; + skipped?: Record; +}; +type Report = { + results: Map; + removed: Set; + skipped: Record; + removalError?: string; +}; -function failure(id: string, status: "build-failed" | "load-failed", cause: unknown): PluginResult { - const message = cause instanceof Error ? cause.message : String(cause); +const messageOf = (cause: unknown) => (cause instanceof Error ? cause.message : String(cause)); +const uncertain = "The reload outcome is not known; run tokamak plugin list"; + +function failed( + id: string, + buildId: string | null, + status: "build-failed" | "load-failed" | "unknown", + cause: unknown, +): Result { const diagnostics = cause instanceof CommandError ? cause.diagnostics : undefined; - return { id, status, error: { message, ...(diagnostics ? { diagnostics } : {}) } }; + return { id, buildId, status, error: { message: messageOf(cause), ...(diagnostics && { diagnostics }) } }; +} + +async function build(id: string, folder: string): Promise { + const { data } = await buildInFolder(folder); + if (data.id !== id) throw new Error(`Build produced plugin ${data.id}, expected ${id}`); + const bytes = await readFile(join(data.outputFolder, "manifest.json")); + return { + id, + buildId: createHash("sha256").update(bytes).digest("hex"), + depends: Object.keys(JSON.parse(bytes.toString()).plugin.depends ?? {}), + }; +} + +function applyAnswer(report: Report, builds: Build[], answer: Catalog, reconciled: boolean) { + const running = new Map(answer.plugins.map(({ plugin, revision }) => [plugin.id, revision])); + report.skipped = answer.skipped ?? {}; + for (const id of answer.removed ?? []) report.removed.add(id); + for (const { id, buildId } of builds) { + const skipped = report.skipped[id]; + if (skipped || running.get(id) !== buildId) { + report.results.set( + id, + failed(id, buildId, "load-failed", skipped ?? "The server did not apply this build"), + ); + } else { + const status = !reconciled && answer.unchanged?.includes(id) ? "unchanged" : "loaded"; + report.results.set(id, { id, buildId, status }); + } + } } -async function applyBuilds( - ids: string[], - removeMissing: boolean, - results: Map, - server?: string, -) { - if (!ids.length && !removeMissing) return {}; - const reload = (selected: string[], remove: boolean) => - serverPost("/plugin-index/reload", { ids: selected, removeMissing: remove }, server); - const loaded = (id: string) => results.set(id, { id, status: "loaded" }); +async function submit(report: Report, builds: Build[], removeMissing: boolean, server?: string) { + const payload = { builds: builds.map(({ id, buildId }) => ({ id, buildId })), removeMissing }; + let answer: Catalog; try { - await reload(ids, removeMissing); - ids.forEach(loaded); - return {}; + answer = (await serverPost("/plugin-index/reload", payload, server)) as Catalog; + if (!Array.isArray(answer?.plugins)) throw new Error("Server returned an invalid reload answer"); } catch (error) { - const next = error instanceof CommandError ? error.next : undefined; - if (!(error instanceof ServerRefusal) || !removeMissing || !ids.length) { - for (const id of ids) results.set(id, failure(id, "load-failed", error)); - return { next, removalsError: removeMissing ? error : undefined }; + if (error instanceof ServerRefusal) throw error; + try { + const running = (await serverGet("/plugins", server)) as Catalog; + applyAnswer(report, builds, running, true); + if (removeMissing) report.removalError = uncertain; + } catch { + for (const { id, buildId } of builds) report.results.set(id, failed(id, buildId, "unknown", uncertain)); + if (removeMissing) report.removalError = uncertain; } - // Try a batch first so related versions can change together. If it is - // refused, isolate the builds so unrelated plugins can still load. - for (const id of ids) { + return; + } + applyAnswer(report, builds, answer, false); +} + +function dependencyOrder(builds: Build[]) { + const pending = new Map(builds.map((build) => [build.id, build])); + const ordered: Build[] = []; + while (pending.size) { + const ready = [...pending.values()].filter(({ depends }) => depends.every((id) => !pending.has(id))); + if (!ready.length) return [...ordered, ...pending.values()]; + for (const build of ready) { + pending.delete(build.id); + ordered.push(build); + } + } + return ordered; +} + +async function retryBuilds(report: Report, builds: Build[], server?: string) { + let pending = dependencyOrder(builds); + while (pending.length) { + const waiting: Build[] = []; + let progress = false; + for (const build of pending) { try { - await reload([id], false); - loaded(id); + await submit(report, [build], false, server); + progress ||= report.results.get(build.id)?.status === "loaded"; } catch (error) { - results.set(id, failure(id, "load-failed", error)); + report.results.set(build.id, failed(build.id, build.buildId, "load-failed", error)); + if (/missing dependency/i.test(messageOf(error))) waiting.push(build); } } + if (!progress) break; + pending = waiting; + } +} + +async function applyBuilds(report: Report, builds: Build[], all: boolean, server?: string) { + if (!builds.length && !all) return; + try { + await submit(report, builds, all, server); + } catch (error) { + if (!all) { + for (const { id, buildId } of builds) report.results.set(id, failed(id, buildId, "load-failed", error)); + return; + } + await retryBuilds(report, builds, server); try { - await reload([], true); - return { next }; - } catch (removalsError) { - return { next, removalsError }; + await submit(report, [], true, server); + } catch (removalError) { + report.removalError = messageOf(removalError); + } + } +} + +async function runReload(id: string | undefined, server?: string) { + const index = await readIndex(); + if (id !== undefined && !index.has(id)) + throw new CommandError(`Unknown indexed plugin: ${id}`, "tokamak plugin add"); + const selected = id === undefined ? [...index] : [[id, index.get(id)!] as const]; + const report: Report = { results: new Map(), removed: new Set(), skipped: {} }; + const releases = new Map Promise>(); + const builds: Build[] = []; + try { + // Take locks in folder order so overlapping reloads cannot deadlock. + for (const [plugin, folder] of [...selected].sort((a, b) => a[1].localeCompare(b[1]))) { + try { + const canonical = await realpath(folder); + if (!releases.has(canonical)) releases.set(canonical, await acquireLock(canonical, ".tokamak-build")); + builds.push(await build(plugin, canonical)); + } catch (error) { + report.results.set(plugin, failed(plugin, null, "build-failed", error)); + } } + await applyBuilds(report, builds, id === undefined, server); + } finally { + await Promise.all([...releases.values()].map((release) => release())); } + return reportOutput( + report, + selected.map(([plugin]) => plugin), + ); +} + +function reportOutput(report: Report, selected: string[]) { + const plugins = selected.map((id) => report.results.get(id)!); + const removed = [...report.removed]; + const skipped = Object.fromEntries(Object.entries(report.skipped).filter(([id]) => !selected.includes(id))); + const data = { plugins, removed, skipped }; + const lines = [ + ...plugins.map(({ id, status, error }) => + error + ? `${id}: ${error.message}${ + error.diagnostics + ?.map((entry) => { + const { file, line, column, code, message } = entry as Record; + return `\n${file ? `${file}:${line}:${column}: ` : ""}${code ? `${code}: ` : ""}${message}`; + }) + .join("") ?? "" + }` + : `${id}: ${status}`, + ), + ...removed.map((id) => `Removed ${id} from the running index`), + ...Object.entries(skipped).map(([id, reason]) => `Skipped ${id}: ${reason}`), + ]; + if (report.removalError) lines.push(`Could not confirm index removals: ${report.removalError}`); + const text = lines.join("\n") || "No indexed plugins to reload."; + if (report.removalError || plugins.some(({ error }) => error)) + throw new CommandError(text, "tokamak plugin list", undefined, data); + return { data, text }; } export default { path: "plugin reload", summary: "Build and reload indexed plugins in the running local server.", args: { name: "id", optional: true }, + usesServer: true, example: "tokamak plugin reload my-plugin --json", async run([id], { server }) { - const index = await readIndex(); - const selected = id === undefined ? [...index.keys()] : [id]; - const results = new Map(); - const built: string[] = []; - for (const plugin of selected) { - const folder = index.get(plugin); - if (folder === undefined) - throw new CommandError(`Unknown indexed plugin: ${plugin}`, "tokamak plugin add"); - try { - const { data } = await build.run([folder]); - if (data.id !== plugin) throw new Error(`Build produced plugin ${data.id}, expected ${plugin}`); - if (data.outputFolder !== resolve(folder, "dist")) - throw new Error(`Indexed plugins must build into ${resolve(folder, "dist")}`); - built.push(plugin); - } catch (error) { - results.set(plugin, failure(plugin, "build-failed", error)); - } - } - - const { next = "tokamak plugin reload --help", removalsError } = await applyBuilds( - built, - id === undefined, - results, - server, - ); - const plugins = selected.map((plugin) => results.get(plugin)!); - const lines = plugins.map((plugin) => - plugin.status === "loaded" ? `Loaded ${plugin.id}` : `${plugin.id}: ${plugin.error.message}`, - ); - if (removalsError) lines.push(`Could not apply index removals: ${String(removalsError)}`); - if (plugins.some((plugin) => plugin.status !== "loaded") || removalsError) { - throw new CommandError(lines.join("\n"), next, undefined, { plugins }); - } - return { - data: { plugins }, - text: lines.join("\n") || "Applied index removals. No indexed plugins to build.", - }; + return runReload(id, server); }, } satisfies Command; diff --git a/packages/cli/src/plugin-index.ts b/packages/cli/src/plugin-index.ts index 9f5eb313..45f112e4 100644 --- a/packages/cli/src/plugin-index.ts +++ b/packages/cli/src/plugin-index.ts @@ -57,9 +57,19 @@ async function reapLock(lock: string) { export async function updateIndex(change: (entries: Map) => boolean) { const folder = dataFolder(); await mkdir(folder, { recursive: true, mode: 0o700 }); - const lock = join(folder, `${name}.lock`); + const release = await acquireLock(folder, name); + try { + const entries = await readIndex(folder); + if (change(entries)) await writeIndex(folder, entries); + } finally { + await release(); + } +} + +export async function acquireLock(folder: string, label: string) { + const lock = join(folder, `${label}.lock`); const owner = `${process.pid}.${randomUUID()}`; - const candidate = await mkdtemp(join(folder, `.${name}.lock-`)); + const candidate = await mkdtemp(join(folder, `.${label}.lock-`)); let acquired = false; try { // Publish a nonempty lock atomically. A reaper can unlink only the dead @@ -73,16 +83,18 @@ export async function updateIndex(change: (entries: Map) => bool } catch (error) { if (!["EEXIST", "ENOTEMPTY", "EPERM", "EACCES"].includes(codeOf(error) ?? "")) throw error; await reapLock(lock); - if (performance.now() >= deadline) - throw new Error(`Timed out waiting for plugin index lock: ${lock}`); + if (performance.now() >= deadline) throw new Error(`Timed out waiting for lock: ${lock}`); await new Promise((done) => setTimeout(done, 25 + Math.random() * 50)); } } - const entries = await readIndex(folder); - if (change(entries)) await writeIndex(folder, entries); + return async () => { + await rm(join(lock, owner), { force: true }); + await removeEmptyLock(lock); + }; } finally { - const directory = acquired ? lock : candidate; - await rm(join(directory, owner), { force: true }); - await removeEmptyLock(directory); + if (!acquired) { + await rm(join(candidate, owner), { force: true }); + await removeEmptyLock(candidate); + } } } diff --git a/packages/cli/src/server.ts b/packages/cli/src/server.ts index a2b74cbd..4e494aa4 100644 --- a/packages/cli/src/server.ts +++ b/packages/cli/src/server.ts @@ -66,22 +66,33 @@ async function localEndpoint() { throw new Error("Invalid local server discovery address"); const token = await readToken(folder); const endpoint = { url: httpAddress(identity.url), token }; - const health = discovery(await get("/health", endpoint)); + const health = discovery(await request("/health", endpoint)); if (JSON.stringify(health) !== JSON.stringify(identity)) throw new Error("Local server identity changed"); return endpoint; } -async function get(path: string, endpoint: { url: URL; token: string }): Promise { +export class ServerRefusal extends CommandError {} + +async function request(path: string, endpoint: { url: URL; token: string }, body?: object): Promise { let response: Response; try { response = await fetch(new URL(path, endpoint.url), { - headers: { Authorization: `Bearer ${endpoint.token}` }, - signal: AbortSignal.timeout(5000), + headers: { + Authorization: `Bearer ${endpoint.token}`, + ...(body && { "Content-Type": "application/json" }), + }, + method: body ? "POST" : "GET", + body: body && JSON.stringify(body), + signal: AbortSignal.timeout(body ? 30_000 : 5000), redirect: "error", }); } catch { throw new Error("Could not connect to the server"); } + if (body && response.status === 400) { + const result = (await response.json()) as { error?: string }; + if (typeof result?.error === "string") throw new ServerRefusal(result.error, "tokamak plugin list"); + } if (!response.ok) throw new Error(`Server returned HTTP ${response.status}`); try { return await response.json(); @@ -90,13 +101,22 @@ async function get(path: string, endpoint: { url: URL; token: string }): Promise } } -export async function serverGet(path: string, explicit?: string): Promise { +export function serverGet(path: string, explicit?: string) { + return serverRequest(path, explicit); +} + +export function serverPost(path: string, body: object, explicit?: string) { + return serverRequest(path, explicit, body); +} + +async function serverRequest(path: string, explicit?: string, body?: object): Promise { const address = explicit ?? process.env.TOKAMAK_SERVER; if (!address) { const endpoint = await localEndpoint().catch(() => { throw new CommandError("No local server is running", "tokamak server start"); }); - return get(path, endpoint).catch((error: Error) => { + return request(path, endpoint, body).catch((error: Error) => { + if (error instanceof ServerRefusal) throw error; throw new CommandError( `Could not read ${path} from the local server: ${error.message}`, "tokamak server start", @@ -106,8 +126,9 @@ export async function serverGet(path: string, explicit?: string): Promise= deadline) throw new Error("Server did not publish readiness"); await Bun.sleep(20); } - await check(address, token); + await check(address, token, server); } finally { try { - if (address) { + if (address && server.exitCode === null && server.signalCode === null) { // The server can close the connection before writing the shutdown reply. const response = await fetch(`${address}/shutdown`, { headers: { Authorization: `Bearer ${token}` }, @@ -349,14 +349,22 @@ test("reload applies index additions and removals without a restart", async () = expect(result.code, result.stderr).toBe(0); return JSON.parse(result.stdout); }; - await withServer(root, () => { + await withServer(root, async () => { run("add", plugin); expect(run("reload", "outside-fixture")).toMatchObject({ - plugins: [{ id: "outside-fixture", status: "loaded" }], + plugins: [ + { id: "outside-fixture", status: "loaded", buildId: expect.stringMatching(/^[a-f0-9]{64}$/) }, + ], }); expect(run("list").plugins).toEqual([{ id: "outside-fixture", version: "0.1.0", fromIndex: true }]); + expect(run("reload", "outside-fixture").plugins).toEqual([ + { id: "outside-fixture", status: "unchanged", buildId: expect.stringMatching(/^[a-f0-9]{64}$/) }, + ]); + const unrelated = join(root, "installed", "unrelated", "dist"); + await mkdir(unrelated, { recursive: true }); + await writeFile(join(unrelated, "manifest.json"), "{}"); run("remove", "outside-fixture"); - expect(run("reload").plugins).toEqual([]); + expect(run("reload")).toMatchObject({ plugins: [], removed: ["outside-fixture"] }); expect(run("list").plugins).toEqual([]); }); }); @@ -403,9 +411,11 @@ test("reload updates an existing connection and preserves old code on build or l const read = () => client.query(api.notes.marker, {}); expect(await read()).toBe("old"); await writeFile(notes, marker(original, "edited")); - expect(run("reload", "outside-fixture")).toEqual({ + expect(run("reload", "outside-fixture")).toMatchObject({ ok: true, - plugins: [{ id: "outside-fixture", status: "loaded" }], + plugins: [ + { id: "outside-fixture", status: "loaded", buildId: expect.stringMatching(/^[a-f0-9]{64}$/) }, + ], }); expect(await read()).toBe("edited"); @@ -462,3 +472,230 @@ test("reload updates an existing connection and preserves old code on build or l }); }); }, 180_000); + +test("reload refuses a different manifest and verifies the bytes under a matching manifest", async () => { + await withPlugin(async (folder, root) => { + const build = () => { + const result = cli(root, ["plugin", "build", folder, "--json"]); + expect(result.code, result.stderr).toBe(0); + }; + expect(cli(root, ["plugin", "add", folder]).code).toBe(0); + build(); + const manifest = join(folder, "dist", "manifest.json"); + const old = await readFile(manifest); + const hash = (bytes) => new Bun.CryptoHasher("sha256").update(bytes).digest("hex"); + await withServer(root, async (address, token) => { + const submit = (buildId) => + fetch(address + "/plugin-index/reload", { + method: "POST", + headers: { Authorization: "Bearer " + token, "Content-Type": "application/json" }, + body: JSON.stringify({ builds: [{ id: "outside-fixture", buildId }], removeMissing: false }), + }); + const notes = join(folder, "src/server/notes.ts"); + await writeFile( + notes, + (await readFile(notes, "utf8")) + "\nexport const marker = query({ handler: () => 42 });", + ); + build(); + const stale = await submit(hash(old)); + expect(stale.status).toBe(400); + expect((await stale.json()).error).toContain("build changed after this reload started"); + await writeFile(manifest, old); + const mixed = await submit(hash(old)); + expect(mixed.status).toBe(400); + expect((await mixed.json()).error).toContain("hash mismatch"); + }); + }); +}, 180_000); + +test("legacy reload leaves indexed snapshots alone and ignores new index entries", async () => { + await withPlugin(async (folder, root) => { + const installed = join(root, "installed", "base"); + await mkdir(join(root, "installed")); + await cp(folder, installed, { recursive: true }); + const id = join(installed, "plugin.ts"); + await writeFile(id, (await readFile(id, "utf8")).replace("outside-fixture", "base-plugin")); + expect(cli(root, ["plugin", "build", installed]).code).toBe(0); + await withServer(root, async (address, token) => { + expect(cli(root, ["plugin", "add", folder]).code).toBe(0); + await mkdir(join(folder, "dist"), { recursive: true }); + await writeFile(join(folder, "dist", "manifest.json"), "{}"); + const res = await fetch(address + "/plugins/base-plugin/reload", { + method: "POST", + headers: { Authorization: "Bearer " + token }, + }); + expect(res.status).toBe(200); + expect((await res.json()).plugins.map(({ plugin }) => plugin.id)).toEqual(["base-plugin"]); + }); + }); +}, 180_000); + +function spawnReload(root, token, address) { + const args = ["plugin", "reload", "outside-fixture", "--json"]; + if (address) args.push("--server", address); + const child = Bun.spawn([process.execPath, "packages/cli/src/main.ts", ...args], { + cwd: workspace, + env: { + ...process.env, + TOKAMAK_SERVER_DIR: join(root, "data"), + TOKAMAK_SERVER: undefined, + TOKAMAK_TOKEN: token, + }, + stdout: "pipe", + stderr: "pipe", + timeout: 90_000, + }); + return Promise.all([child.exited, new Response(child.stdout).json(), new Response(child.stderr).text()]); +} + +for (const scenario of ["race", "timeout", "death"]) { + test(`reload ${scenario}: reports only what the server is known to run`, async () => { + await withPlugin(async (folder, root) => { + expect(cli(root, ["plugin", "add", folder]).code).toBe(0); + const notes = join(folder, "src/server/notes.ts"); + const original = await readFile(notes, "utf8"); + const edit = (n) => + writeFile(notes, original + `\nexport const marker = query({ handler: () => ${n} });`); + await withServer(root, async (address, token, server) => { + const arrived = Promise.withResolvers(); + const release = Promise.withResolvers(); + let applied; + const proxy = Bun.serve({ + hostname: "127.0.0.1", + port: 0, + idleTimeout: 0, + async fetch(req, self) { + const path = new URL(req.url).pathname; + if (req.method === "GET") return fetch(address + path, { headers: req.headers }); + const body = await req.text(); + if (scenario === "race") { + arrived.resolve(JSON.parse(body).builds[0].buildId); + await release.promise; + } + const res = await fetch(address + path, { method: "POST", headers: req.headers, body }); + applied = (await res.clone().json()).plugins?.[0]?.revision; + if (scenario === "timeout") await release.promise; + if (scenario === "death") { + server.kill("SIGKILL"); + await server.exited; + self.stop(true); + } + return res; + }, + }); + try { + await edit(1); + const first = spawnReload(root, token, proxy.url.origin); + if (scenario === "race") { + const firstId = await Promise.race([ + arrived.promise, + first.then((result) => { + throw new Error(JSON.stringify(result)); + }), + ]); + await edit(2); + const second = spawnReload(root, token); + expect( + await Promise.race([second.then(() => "completed"), Bun.sleep(500).then(() => "waiting")]), + ).toBe("waiting"); + expect( + new Bun.CryptoHasher("sha256") + .update(await readFile(join(folder, "dist/manifest.json"))) + .digest("hex"), + ).toBe(firstId); + release.resolve(); + const [firstCode, firstResult] = await first; + const [secondCode, secondResult] = await second; + expect(firstCode).toBe(0); + expect(secondCode).toBe(0); + expect(firstResult.plugins[0]).toMatchObject({ buildId: firstId, status: "loaded" }); + expect(secondResult.plugins[0].buildId).not.toBe(firstId); + } else { + const [code, result, stderr] = await first; + expect(applied).toMatch(/^[a-f0-9]{64}$/); + expect(code === 0).toBe(scenario === "timeout"); + expect(result.plugins[0]).toMatchObject({ + buildId: applied, + status: scenario === "timeout" ? "loaded" : "unknown", + }); + if (scenario === "death") expect(stderr).toContain("tokamak plugin list"); + } + } finally { + release.resolve(); + proxy.stop(true); + } + }); + }); + }, 180_000); +} + +test("batch fallback loads a provider before its dependent after a third plugin is refused", async () => { + await withPlugin(async (folder, root) => { + const create = async (id, extra = "") => { + const path = join(root, id); + await cp(folder, path, { recursive: true }); + await writeFile( + join(path, "plugin.ts"), + `import { definePlugin } from "@tokamak/sdk"; export default definePlugin({ id: "${id}", version:"1.0.0"${extra} });`, + ); + expect(cli(root, ["plugin", "add", path]).code).toBe(0); + return path; + }; + await create("dependent", ', depends: ["provider"]'); + await create("provider"); + const refused = await create("refused"); + const notes = join(refused, "src/server/notes.ts"); + await writeFile(notes, (await readFile(notes, "utf8")) + '\nthrow new Error("refused at import");'); + await withServer(root, () => { + const output = cli(root, ["plugin", "reload", "--json"]); + expect(output.code).not.toBe(0); + expect(JSON.parse(output.stdout).plugins).toEqual([ + expect.objectContaining({ id: "dependent", status: "loaded" }), + expect.objectContaining({ id: "provider", status: "loaded" }), + expect.objectContaining({ + id: "refused", + status: "load-failed", + error: { message: expect.stringContaining("refused at import") }, + }), + ]); + }); + }); +}, 180_000); + +test("reload refuses a worker that throws during import and keeps the old worker", async () => { + await withPlugin(async (_, root) => { + const folder = join(root, "worker-test"); + await cp(join(workspace, "packages/server/tests/fixtures/worker"), folder, { recursive: true }); + await symlink(join(workspace, "node_modules"), join(folder, "node_modules"), "dir"); + expect(cli(root, ["plugin", "add", folder]).code).toBe(0); + await withServer(root, async (address, token) => { + const reload = () => cli(root, ["plugin", "reload", "worker-test", "--json"]); + expect(reload().code).toBe(0); + const socket = new WebSocket(address.replace(/^http:/, "ws:") + "/ws?token=" + token); + await new Promise((done, reject) => { + socket.addEventListener("open", done, { once: true }); + socket.addEventListener("error", reject, { once: true }); + }); + const transport = new WebSocketTransport(socket); + try { + const state = () => new TokamakClient(transport).action(createApi("worker-test").actions.state, {}); + const old = await state(); + const actions = join(folder, "src/server/actions.ts"); + await writeFile( + actions, + 'if (typeof process !== "undefined") throw new Error("worker import failed intentionally");\n' + + (await readFile(actions, "utf8")), + ); + const refused = reload(); + expect(refused.code).not.toBe(0); + expect(JSON.parse(refused.stdout).plugins[0]).toMatchObject({ + status: "load-failed", + error: { message: expect.stringContaining("worker import failed intentionally") }, + }); + expect((await state()).pid).toBe(old.pid); + } finally { + transport.close(); + } + }); + }); +}, 180_000); diff --git a/packages/server/src/plugins/artifact.rs b/packages/server/src/plugins/artifact.rs index 28e24ece..0a3dc2b9 100644 --- a/packages/server/src/plugins/artifact.rs +++ b/packages/server/src/plugins/artifact.rs @@ -63,6 +63,7 @@ impl Artifact { Ok(Self { manifest, build_id, + source: root, bundle, assets, provides, diff --git a/packages/server/src/plugins/catalog.rs b/packages/server/src/plugins/catalog.rs index 1d1e2adf..005453ee 100644 --- a/packages/server/src/plugins/catalog.rs +++ b/packages/server/src/plugins/catalog.rs @@ -11,26 +11,33 @@ impl Catalog { .unwrap_or(root.parent().unwrap_or(root)); let mut plugins = read_installed(root, previous)?; let index = previous.map(|p| Arc::clone(&p.index)).unwrap_or_default(); - for (id, folder) in index.iter() { - let old = previous.and_then(|p| p.plugins.get(id)); - plugins.insert( - id.clone(), - read_indexed(id, folder, old.map(AsRef::as_ref))?, - ); + let installed = installed_paths(&plugins); + if let Some(previous) = previous { + for id in index.keys() { + if let Some(artifact) = previous.plugins.get(id) { + plugins.insert(id.clone(), Arc::clone(artifact)); + } + } } - Self::assemble(root, directory, plugins, index) + let mut catalog = Self::assemble(root, directory, plugins, index)?; + catalog.installed = installed; + catalog.index_path = previous.and_then(|old| old.index_path.clone()); + Ok(catalog) } pub(crate) fn read_indexed_reload( root: &Path, previous: &Self, - ids: &[String], - remove_missing: bool, + request: &IndexedReload, ) -> Result { - let entries = index_entries(&previous.directory.join("plugin-index.json"))?; + let path = previous + .index_path + .as_deref() + .ok_or_else(|| invalid("Server has no local plugin index"))?; + let entries = index_entries(path)?; let mut plugins = previous.plugins.clone(); let mut index = (*previous.index).clone(); - if remove_missing { + if request.remove_missing { for id in previous .index .keys() @@ -38,18 +45,31 @@ impl Catalog { { index.remove(id); plugins.remove(id); + if let Some(source) = previous.installed.get(id) { + let fallback = Artifact::read(source)?; + if fallback.manifest.plugin.id != *id { + return Err(invalid(format!("Installed copy of {id} changed identity"))); + } + plugins.insert(id.clone(), Arc::new(fallback)); + } } } - for id in ids { + for IndexedBuild { id, build_id } in &request.builds { let folder = entries .get(id) .and_then(Value::as_str) .ok_or_else(|| invalid(format!("Plugin is not indexed: {id}")))?; let old = previous.plugins.get(id).map(AsRef::as_ref); - plugins.insert(id.clone(), read_indexed(id, Path::new(folder), old)?); + plugins.insert( + id.clone(), + read_indexed_build(id, Path::new(folder), old, Some(build_id))?, + ); index.insert(id.clone(), PathBuf::from(folder)); } - Self::assemble(root, &previous.directory, plugins, Arc::new(index)) + let mut catalog = Self::assemble(root, &previous.directory, plugins, Arc::new(index))?; + catalog.installed.clone_from(&previous.installed); + catalog.index_path.clone_from(&previous.index_path); + Ok(catalog) } pub(super) fn assemble( @@ -86,6 +106,8 @@ impl Catalog { revision: format!("{:x}", hash.finalize()), providers, index, + installed: BTreeMap::new(), + index_path: None, environment, directory: directory.to_owned(), }) @@ -203,11 +225,26 @@ fn index_entries(path: &Path) -> Result, Error> { } fn read_indexed(id: &str, folder: &Path, old: Option<&Artifact>) -> Result, Error> { + read_indexed_build(id, folder, old, None) +} + +fn read_indexed_build( + id: &str, + folder: &Path, + old: Option<&Artifact>, + expected: Option<&str>, +) -> Result, Error> { if !folder.is_absolute() { return Err(invalid("Expected an absolute source folder")); } let dist = folder.join("dist"); - let artifact = Artifact::read_reusing(&dist, Manifest::read(&dist)?, old)?; + let manifest = Manifest::read(&dist)?; + if expected.is_some_and(|build_id| build_id != manifest.1) { + return Err(invalid(format!( + "{id}: the build changed after this reload started; run the command again" + ))); + } + let artifact = Artifact::read_reusing(&dist, manifest, old)?; if artifact.manifest.plugin.id != id { return Err(invalid(format!( "Build has plugin identity {}", @@ -263,3 +300,12 @@ pub(super) fn read_installed( } Ok(plugins) } + +pub(super) fn installed_paths( + plugins: &BTreeMap>, +) -> BTreeMap { + plugins + .iter() + .map(|(id, artifact)| (id.clone(), artifact.source.clone())) + .collect() +} diff --git a/packages/server/src/plugins/loading.rs b/packages/server/src/plugins/loading.rs index cb4b421a..9ccab807 100644 --- a/packages/server/src/plugins/loading.rs +++ b/packages/server/src/plugins/loading.rs @@ -1,5 +1,5 @@ use super::{ - catalog::{IndexedArtifacts, read_index, read_installed}, + catalog::{IndexedArtifacts, installed_paths, read_index, read_installed}, *, }; @@ -18,6 +18,7 @@ impl Loaded { directory, pool, database, + index_path: index, }; let loaded = match config.commit(&installed, &candidates, None).await { Ok(loaded) => loaded, @@ -304,6 +305,7 @@ struct StartupCatalog<'a> { directory: &'a Path, pool: &'a engine::Pool, database: &'a crate::data::Database, + index_path: Option<&'a Path>, } impl StartupCatalog<'_> { @@ -321,7 +323,9 @@ impl StartupCatalog<'_> { (id.clone(), folder.clone()) }) .collect(); - let catalog = Catalog::assemble(self.root, self.directory, plugins, Arc::new(index))?; + let mut catalog = Catalog::assemble(self.root, self.directory, plugins, Arc::new(index))?; + catalog.installed = installed_paths(installed); + catalog.index_path = self.index_path.map(Path::to_owned); Loaded::load_reusing(catalog, self.pool, previous).await } diff --git a/packages/server/src/plugins/mod.rs b/packages/server/src/plugins/mod.rs index 3fe02d36..3b234cae 100644 --- a/packages/server/src/plugins/mod.rs +++ b/packages/server/src/plugins/mod.rs @@ -15,6 +15,8 @@ use std::{ pub struct Artifact { pub manifest: Manifest, + pub(crate) build_id: String, + source: PathBuf, pub bundle: Option>, assets: BTreeMap>, provides: BTreeSet, @@ -26,7 +28,9 @@ pub struct Catalog { pub plugins: BTreeMap>, pub revision: String, providers: BTreeMap>, - index: Arc>, + pub(crate) index: Arc>, + installed: BTreeMap, + index_path: Option, pub(crate) environment: PathBuf, pub(crate) directory: PathBuf, } @@ -38,6 +42,7 @@ pub(crate) struct Loaded { pub targets: BTreeMap, pub descriptor: Value, pub catalog_text: String, + pub(crate) action_lifetimes: BTreeMap, pub(crate) workers: BTreeMap>, inspected: BTreeMap)>, } @@ -71,3 +76,17 @@ pub use manifest::*; #[cfg(test)] mod tests; + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct IndexedBuild { + pub id: String, + pub build_id: String, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub(crate) struct IndexedReload { + pub builds: Vec, + pub remove_missing: bool, +} diff --git a/packages/server/src/service/mod.rs b/packages/server/src/service/mod.rs index f0ff0206..16293cfb 100644 --- a/packages/server/src/service/mod.rs +++ b/packages/server/src/service/mod.rs @@ -112,7 +112,10 @@ pub(crate) enum Command { Action(u64, crate::identity::User, Action), Plugins(oneshot::Sender>), Reload(String, oneshot::Sender>), - ReloadIndexed(Vec, bool, oneshot::Sender>), + ReloadIndexed( + crate::plugins::IndexedReload, + oneshot::Sender>, + ), RunFunction(crate::actions::NestedCall), Asset( Option, diff --git a/packages/server/src/service/plugin_control.rs b/packages/server/src/service/plugin_control.rs index cf485e25..553c2b9d 100644 --- a/packages/server/src/service/plugin_control.rs +++ b/packages/server/src/service/plugin_control.rs @@ -79,8 +79,8 @@ impl Coordinator { let _ = reply.send(result); } - Command::ReloadIndexed(ids, remove_missing, reply) => { - let _ = reply.send(self.reload_indexed(&ids, remove_missing).await); + Command::ReloadIndexed(request, reply) => { + let _ = reply.send(self.reload_indexed(&request).await); } _ => unreachable!("application commands have their own admission path"), } @@ -88,8 +88,7 @@ impl Coordinator { async fn reload_indexed( &mut self, - ids: &[String], - remove_missing: bool, + request: &crate::plugins::IndexedReload, ) -> Result { let root = self .plugin_root @@ -99,13 +98,31 @@ impl Coordinator { .plugins .as_ref() .ok_or_else(|| Error::Invalid("No plugin installation to reload".into()))?; - let catalog = crate::plugins::Catalog::read_indexed_reload( - root, - &previous.catalog, - ids, - remove_missing, - )?; - self.swap_catalog(catalog).await + let catalog = + crate::plugins::Catalog::read_indexed_reload(root, &previous.catalog, request)?; + let unchanged: Vec<_> = request + .builds + .iter() + .filter(|build| { + previous + .catalog + .plugins + .get(&build.id) + .is_some_and(|artifact| artifact.build_id == build.build_id) + }) + .map(|build| build.id.clone()) + .collect(); + let removed: Vec<_> = previous + .catalog + .index + .keys() + .filter(|id| !catalog.index.contains_key(*id)) + .cloned() + .collect(); + let mut response = self.swap_catalog(catalog).await?; + response["removed"] = serde_json::json!(removed); + response["unchanged"] = serde_json::json!(unchanged); + Ok(response) } async fn reload(&mut self, id: &str) -> Result { diff --git a/packages/server/src/service/server.rs b/packages/server/src/service/server.rs index ab21193c..be81e6fd 100644 --- a/packages/server/src/service/server.rs +++ b/packages/server/src/service/server.rs @@ -21,14 +21,10 @@ impl Server { pub(crate) async fn reload_indexed( &self, - ids: Vec, - remove_missing: bool, + request: crate::plugins::IndexedReload, ) -> Result { let (reply, wait) = oneshot::channel(); - admit( - &self.commands, - Command::ReloadIndexed(ids, remove_missing, reply), - )?; + admit(&self.commands, Command::ReloadIndexed(request, reply))?; wait.await.map_err(|_| Error::Closed)? } diff --git a/packages/server/src/websocket.rs b/packages/server/src/websocket.rs index f574ee10..debd8de5 100644 --- a/packages/server/src/websocket.rs +++ b/packages/server/src/websocket.rs @@ -402,24 +402,12 @@ async fn plugins(State(adapter): State) -> Response { async fn reload_plugin(State(adapter): State, Path(id): Path) -> Response { plugin_result(adapter.server.reload_plugin(id).await) } -#[derive(serde::Deserialize)] -#[serde(rename_all = "camelCase")] -struct IndexedReload { - ids: Vec, - remove_missing: bool, -} - async fn reload_indexed(State(adapter): State, body: String) -> Response { - let input: IndexedReload = match serde_json::from_str(&body) { + let input: crate::plugins::IndexedReload = match serde_json::from_str(&body) { Ok(input) => input, Err(error) => return plugin_result(Err(crate::Error::Invalid(error.to_string()))), }; - plugin_result( - adapter - .server - .reload_indexed(input.ids, input.remove_missing) - .await, - ) + plugin_result(adapter.server.reload_indexed(input).await) } async fn plugin_asset( From 99d8e96a2ad507f51ba1ded6dc4927f2932f5fa6 Mon Sep 17 00:00:00 2001 From: JustKira Date: Fri, 20 Nov 2026 15:35:05 +0200 Subject: [PATCH 3/4] Report unrelated installation errors after index removal --- packages/cli/README.md | 3 +- packages/cli/src/commands/reload.ts | 9 ++++-- packages/cli/tests/cli.test.mjs | 6 +++- packages/server/src/plugins/catalog.rs | 29 +++++++++++++++---- packages/server/src/service/plugin_control.rs | 10 +++++++ 5 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/cli/README.md b/packages/cli/README.md index d79d1519..e3c411cc 100644 --- a/packages/cli/README.md +++ b/packages/cli/README.md @@ -63,7 +63,8 @@ Reload returns `plugins`, `removed` (ids removed from the running index), and and `status`: `loaded`, `unchanged`, `build-failed`, `load-failed`, or `unknown`. Failed or uncertain entries carry `error.message` and may include `error.diagnostics`. The CLI locks each source folder through build and -submission. If a reload reply is lost, it asks the server which build is +submission. Any unrelated installed manifest errors found after a removal are +listed under `warnings` without failing the command. If a reload reply is lost, it asks the server which build is running; when that cannot be confirmed, run `tokamak plugin list`. To add a command, create a file under `src/commands/` that satisfies `Command`, diff --git a/packages/cli/src/commands/reload.ts b/packages/cli/src/commands/reload.ts index 127e0d8a..2476c46d 100644 --- a/packages/cli/src/commands/reload.ts +++ b/packages/cli/src/commands/reload.ts @@ -18,12 +18,14 @@ type Catalog = { unchanged?: string[]; removed?: string[]; skipped?: Record; + warnings?: string[]; }; type Report = { results: Map; removed: Set; skipped: Record; removalError?: string; + warnings: Set; }; const messageOf = (cause: unknown) => (cause instanceof Error ? cause.message : String(cause)); @@ -54,6 +56,7 @@ function applyAnswer(report: Report, builds: Build[], answer: Catalog, reconcile const running = new Map(answer.plugins.map(({ plugin, revision }) => [plugin.id, revision])); report.skipped = answer.skipped ?? {}; for (const id of answer.removed ?? []) report.removed.add(id); + for (const warning of answer.warnings ?? []) report.warnings.add(warning); for (const { id, buildId } of builds) { const skipped = Object.hasOwn(report.skipped, id) ? report.skipped[id] : undefined; if (skipped || running.get(id) !== buildId) { @@ -171,7 +174,7 @@ async function runReload(id: string | undefined, server?: string) { if (id !== undefined && !index.has(id)) throw new CommandError(`Unknown indexed plugin: ${id}`, "tokamak plugin add"); const selected = id === undefined ? [...index] : [[id, index.get(id)!] as const]; - const report: Report = { results: new Map(), removed: new Set(), skipped: {} }; + const report: Report = { results: new Map(), removed: new Set(), skipped: {}, warnings: new Set() }; const releases = new Map Promise>(); const builds: Build[] = []; try { @@ -199,7 +202,8 @@ function reportOutput(report: Report, selected: string[]) { const plugins = selected.map((id) => report.results.get(id)!); const removed = [...report.removed]; const skipped = Object.fromEntries(Object.entries(report.skipped).filter(([id]) => !selected.includes(id))); - const data = { plugins, removed, skipped }; + const warnings = [...report.warnings]; + const data = { plugins, removed, skipped, ...(warnings.length && { warnings }) }; const lines = [ ...plugins.map(({ id, status, error }) => error @@ -215,6 +219,7 @@ function reportOutput(report: Report, selected: string[]) { ), ...removed.map((id) => `Removed ${id} from the running index`), ...Object.entries(skipped).map(([id, reason]) => `Skipped ${id}: ${reason}`), + ...warnings.map((warning) => `Warning: ${warning}`), ]; if (report.removalError) lines.push(`Could not confirm index removals: ${report.removalError}`); const text = lines.join("\n") || "No indexed plugins to reload."; diff --git a/packages/cli/tests/cli.test.mjs b/packages/cli/tests/cli.test.mjs index 2b8bbf6e..6cd1eb84 100644 --- a/packages/cli/tests/cli.test.mjs +++ b/packages/cli/tests/cli.test.mjs @@ -364,7 +364,11 @@ test("reload applies index additions and removals without a restart", async () = await mkdir(unrelated, { recursive: true }); await writeFile(join(unrelated, "manifest.json"), "{}"); run("remove", "outside-fixture"); - expect(run("reload")).toMatchObject({ plugins: [], removed: ["outside-fixture"] }); + expect(run("reload")).toMatchObject({ + plugins: [], + removed: ["outside-fixture"], + warnings: [expect.stringContaining("unrelated")], + }); expect(run("list").plugins).toEqual([]); }); }); diff --git a/packages/server/src/plugins/catalog.rs b/packages/server/src/plugins/catalog.rs index 37104455..c4991587 100644 --- a/packages/server/src/plugins/catalog.rs +++ b/packages/server/src/plugins/catalog.rs @@ -148,6 +148,20 @@ impl Catalog { }) } + pub(crate) fn installation_warnings(root: &Path) -> Vec { + match installed_directories(root) { + Ok(directories) => directories + .iter() + .filter_map(|path| { + Manifest::read(path) + .err() + .map(|error| format!("{}: {error}", path.display())) + }) + .collect(), + Err(error) => vec![error.to_string()], + } + } + pub fn metadata(&self) -> Value { json!({"revision":self.revision,"plugins":self.order.iter().map(|id| { let artifact = &self.plugins[id]; @@ -292,11 +306,8 @@ fn read_indexed_build( type Artifacts = BTreeMap>; type ArtifactFailures = BTreeMap; -pub(super) fn read_installed( - root: &Path, - previous: Option<&Loaded>, -) -> Result<(Artifacts, ArtifactFailures), Error> { - let directories = if let Some(directory) = artifact_directory(root) { +fn installed_directories(root: &Path) -> Result, Error> { + Ok(if let Some(directory) = artifact_directory(root) { vec![directory] } else { let mut result = Vec::new(); @@ -321,8 +332,14 @@ pub(super) fn read_installed( } result.sort(); result - }; + }) +} +pub(super) fn read_installed( + root: &Path, + previous: Option<&Loaded>, +) -> Result<(Artifacts, ArtifactFailures), Error> { + let directories = installed_directories(root)?; let mut plugins = BTreeMap::new(); let mut skipped = BTreeMap::new(); diff --git a/packages/server/src/service/plugin_control.rs b/packages/server/src/service/plugin_control.rs index 7e6327bd..0b0484b6 100644 --- a/packages/server/src/service/plugin_control.rs +++ b/packages/server/src/service/plugin_control.rs @@ -123,6 +123,16 @@ impl Coordinator { .cloned() .collect(); let mut response = self.swap_catalog(catalog, failures).await?; + // Diagnostics run after the swap. Unrelated on-disk errors do not + // participate in deciding or applying an index removal. + if !removed.is_empty() + && let Some(root) = &self.plugin_root + { + let warnings = crate::plugins::Catalog::installation_warnings(root); + if !warnings.is_empty() { + response["warnings"] = serde_json::json!(warnings); + } + } response["removed"] = serde_json::json!(removed); response["unchanged"] = serde_json::json!(unchanged); Ok(response) From 1ad4cb298096c4188859b4761db5c852898f0906 Mon Sep 17 00:00:00 2001 From: JustKira Date: Fri, 20 Nov 2026 17:03:23 +0200 Subject: [PATCH 4/4] Build selected providers before their dependents --- packages/cli/src/commands/reload.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/commands/reload.ts b/packages/cli/src/commands/reload.ts index 2476c46d..b549e3e4 100644 --- a/packages/cli/src/commands/reload.ts +++ b/packages/cli/src/commands/reload.ts @@ -1,6 +1,7 @@ import { createHash } from "node:crypto"; import { readFile, realpath } from "node:fs/promises"; import { join } from "node:path"; +import { readSourcePlugin } from "../../../build/src/descriptor"; import { CommandError, type Command } from "../command"; import { acquireLock, readIndex } from "../plugin-index"; import { ServerRefusal, serverGet, serverPost } from "../server"; @@ -117,9 +118,9 @@ async function submit(report: Report, builds: Build[], removeMissing: boolean, s applyAnswer(report, builds, answer, false); } -function dependencyOrder(builds: Build[]) { +function dependencyOrder(builds: T[]) { const pending = new Map(builds.map((build) => [build.id, build])); - const ordered: Build[] = []; + const ordered: T[] = []; while (pending.size) { const ready = [...pending.values()].filter(({ depends }) => depends.every((id) => !pending.has(id))); if (!ready.length) return [...ordered, ...pending.values()]; @@ -177,13 +178,24 @@ async function runReload(id: string | undefined, server?: string) { const report: Report = { results: new Map(), removed: new Set(), skipped: {}, warnings: new Set() }; const releases = new Map Promise>(); const builds: Build[] = []; + const sources: { id: string; folder: string; depends: string[] }[] = []; try { // Take locks in folder order so overlapping reloads cannot deadlock. for (const [plugin, folder] of [...selected].sort((a, b) => a[1].localeCompare(b[1]))) { try { const canonical = await realpath(folder); if (!releases.has(canonical)) releases.set(canonical, await acquireLock(canonical, ".tokamak-build")); - builds.push(await build(plugin, canonical)); + const descriptor = await readSourcePlugin(canonical).catch(() => undefined); + // The builder reports malformed descriptors with source diagnostics. + sources.push({ id: plugin, folder: canonical, depends: Object.keys(descriptor?.depends ?? {}) }); + } catch (error) { + report.results.set(plugin, failed(plugin, null, "build-failed", error)); + } + } + // Providers must publish their types before selected dependents build. + for (const { id: plugin, folder } of dependencyOrder(sources)) { + try { + builds.push(await build(plugin, folder)); } catch (error) { report.results.set(plugin, failed(plugin, null, "build-failed", error)); }