Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ stdout; a refusal is `{"ok":false,"code":...,"error":...}` with exit status 1.
already answers is left as it is, and answered the same way with
`alreadyRunning: true`; one a VM runs that does not answer is refused
(`image-running`). It fails as soon as the VM reports a failure or exits,
naming the launcher's logs. It never creates: a missing image is refused.
naming the launcher's logs, which the launcher keeps for the image's last
few starts. It never creates: a missing image is refused.
- **stop** does not save: the image starts again as it was last saved. The
image is asked to quit, by evaluating `Smalltalk snapshot: false andQuit:
true` with the `evaluate` tool its MCP server lists, so that it does what it
Expand All @@ -83,8 +84,9 @@ stdout; a refusal is `{"ok":false,"code":...,"error":...}` with exit status 1.
lists no evaluate tool. Evaluating `Smalltalk saveSession`
in the image saves it without stopping it. An image that is not running is
already stopped.
- **delete** removes the image's directory, and refuses while a VM runs it or
a create or a start is still at work on it.
- **delete** removes the image's directory and the launcher's logs of its
starts, and refuses while a VM runs it or a create or a start is still at
work on it.
- **list** shows each image, its state, its pids, its endpoint, its
template, Pharo version, how MCP got into it, and whether it records its
calls when that is known (see [Telemetry](#telemetry)). It pings nothing and
Expand Down
18 changes: 15 additions & 3 deletions packages/plexus/src/images/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type { ImagesContext } from "./context.js";
import { buildingState, stillBuilding } from "./create.js";
import { hostProcesses, vmsOn } from "./processes.js";
import { beingStarted, launchLockState } from "./start.js";
import { exists, imageFiles, noSuchImage, removeDirectory } from "./store.js";
import { exists, imageFiles, noSuchImage, removeDirectory, type ImageFiles } from "./store.js";

/**
* Remove the image's directory, complete or not, unless a VM runs it, or a
* create or a start is still working on it. An image whose create ended
* before it was done is removed like any other.
* create or a start is still working on it, and then the launcher's logs of
* its starts. An image whose create ended before it was done is removed like
* any other.
*/
export async function deleteImage(
context: ImagesContext,
Expand All @@ -32,5 +33,16 @@ export async function deleteImage(
throw beingStarted(files, lock.holder);
}
removeDirectory(context.home, files.dir);
deleteLaunchLogs(context, files);
return { name };
}

// The image is gone whatever happens to its logs: a launcher that cannot be
// opened leaves them to the sweep of the next start.
function deleteLaunchLogs(context: ImagesContext, files: ImageFiles): void {
try {
context.launcher().deleteLaunchLogs({ imageName: files.diskName });
} catch {
// Left for the launcher's next start.
}
}
2 changes: 2 additions & 0 deletions packages/plexus/src/launcher/pharoLauncherMcpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type LauncherPort = Pick<
PharoLauncher,
| "createImage"
| "startImage"
| "deleteLaunchLogs"
| "listProcesses"
| "killProcess"
| "listTemplateCategories"
Expand All @@ -44,6 +45,7 @@ export function openLauncherPort(home: string): LauncherPort {
createImage: (input) => longLauncher().createImage(input),
startImage: (input) =>
(input.detached ? quickLauncher() : longLauncher()).startImage(input),
deleteLaunchLogs: (input) => quickLauncher().deleteLaunchLogs(input),
listProcesses: (input) => quickLauncher().listProcesses(input),
killProcess: (input) => quickLauncher().killProcess(input),
listTemplateCategories: (input) => quickLauncher().listTemplateCategories(input),
Expand Down
27 changes: 25 additions & 2 deletions packages/plexus/test/images/stopAndDelete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,33 @@ describe("stop with save", () => {
});

describe("delete", () => {
it("removes the image's directory", async () => {
const { home, context } = setup();
it("removes the image's directory, then the launcher's logs of its starts", async () => {
const { home, launcher, dir, context } = setup();
launcher.deleteLaunchLogs = (input) => {
launcher.calls.push({ method: "deleteLaunchLogs", input });
expect(fs.existsSync(dir)).toBe(false);
};
expect(await deleteImage(context, "dev")).toEqual({ name: "dev" });
expect(fs.readdirSync(storeRoot(home))).toEqual([]);
expect(fs.readdirSync(tmpRoot(home))).toEqual([]);
expect(launcher.called("deleteLaunchLogs")).toEqual([{ imageName: "dev" }]);
});

it("names the logs by the image's name on disk, which holds its scope", async () => {
const home = tempHome();
const launcher = new FakeLauncher(home);
writeLauncherImage(launcher.imageDir("shop-dev"), "shop-dev");
const context = testContext(home, launcher, { scope: "shop" });

await deleteImage(context, "dev");
expect(launcher.called("deleteLaunchLogs")).toEqual([{ imageName: "shop-dev" }]);
});

it("is done when the launcher cannot delete the logs", async () => {
const { home, launcher, context } = setup();
launcher.deleteLogsFailure = new Error("no launcher installation");
expect(await deleteImage(context, "dev")).toEqual({ name: "dev" });
expect(fs.readdirSync(storeRoot(home))).toEqual([]);
});

it("removes an image whose create ended before it was done", async () => {
Expand Down Expand Up @@ -572,6 +594,7 @@ describe("delete", () => {
details: { pids: [vm.pid] },
});
expect(fs.existsSync(dir)).toBe(true);
expect(launcher.called("deleteLaunchLogs")).toEqual([]);
});

it("refuses while a start is launching it", async () => {
Expand Down
21 changes: 21 additions & 0 deletions packages/plexus/test/launcher/pharoLauncherMcpClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { openPharoLauncher } from "@evref-bl/pharo-launcher-mcp/launcher";
Expand All @@ -6,8 +7,10 @@ import { plexusHome, storeRoot } from "../../src/home.js";
import {
launcherFailureMessage,
launchLogPaths,
openLauncherPort,
} from "../../src/launcher/pharoLauncherMcpClient.js";
import { homeProfileEnvironment } from "../../src/launcher/pharoLauncherProfile.js";
import { tempHome } from "../support/fakeLauncher.js";

describe("the home", () => {
it("is PLEXUS_HOME, or ~/.plexus", () => {
Expand Down Expand Up @@ -136,3 +139,21 @@ describe("a detached launch", () => {
expect(launchLogPaths({ ok: false })).toEqual([]);
});
});

describe("a deleted image's logs", () => {
it("go from the home's logs directory, and no other image's", () => {
const home = tempHome();
const logs = path.join(home, "profiles", "logs");
fs.mkdirSync(logs, { recursive: true });
fs.mkdirSync(path.join(storeRoot(home), "shop-kept"), { recursive: true });
const log = (image: string) => path.join(logs, `2026-11-23T10-00-00-000Z-${image}-launch.out.log`);
for (const image of ["shop-gone", "shop-kept"]) {
fs.writeFileSync(log(image), "");
}

openLauncherPort(home).deleteLaunchLogs({ imageName: "shop-gone" });

expect(fs.existsSync(log("shop-gone"))).toBe(false);
expect(fs.existsSync(log("shop-kept"))).toBe(true);
});
});
10 changes: 10 additions & 0 deletions packages/plexus/test/support/fakeLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ export class FakeLauncher implements LauncherPort {
return launched(vm.pid, this.home);
}

/** Throws this instead of deleting nothing. */
deleteLogsFailure?: Error;

deleteLaunchLogs(input: { imageName: string }): void {
this.calls.push({ method: "deleteLaunchLogs", input });
if (this.deleteLogsFailure) {
throw this.deleteLogsFailure;
}
}

async listProcesses(): Promise<LauncherCommandResult<LauncherProcess[]>> {
this.calls.push({ method: "listProcesses", input: undefined });
return ok([...this.processes]);
Expand Down
Loading