Skip to content

A personal assistant that lives on your desktop, remembers what matters, and can use your Mac for you.

TypeScript

feat(daemon): build the Windows runtime with the stop addon and pi.cmd

Marcelito734431a67bcb
diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml
index d19e74b..c5d5a9b 100644
--- a/.github/workflows/windows.yml
+++ b/.github/workflows/windows.yml
@@ -94,7 +94,7 @@ jobs:
pnpm --filter @gentle-dot/daemon run build:windows-stop -- --compiler "$compiler" --headers "$inputs/node-$version/include/node" --node-lib "$inputs/node.lib"
if ($LASTEXITCODE -ne 0) { throw 'Native addon build failed' }
- # The portable adapter tests, the real-addon refusal tests, the agent shutdown tests, the engine environment tests, the private memory lookup tests, and the daemon lifecycle and CLI tests.
+ # The daemon test files that run on Windows (the runtime build uses the addon built above).
- name: Daemon tests
- run: pnpm exec vitest run packages/daemon/test/windows-stop.test.ts packages/daemon/test/windows-app-channel.test.ts packages/daemon/test/app-channel-platform.test.ts packages/daemon/test/app-channel.test.ts packages/daemon/test/app-channel-fd.test.ts packages/daemon/test/windows-app-channel-addon.test.ts packages/daemon/test/process-tree.test.ts packages/daemon/test/supervisor.test.ts packages/daemon/test/runtime.test.ts packages/daemon/test/config.test.ts packages/daemon/test/isolation.test.ts packages/daemon/test/private-memory.test.ts packages/daemon/test/lifecycle.test.ts packages/daemon/test/cli.test.ts
+ run: pnpm exec vitest run packages/daemon/test/windows-stop.test.ts packages/daemon/test/windows-app-channel.test.ts packages/daemon/test/app-channel-platform.test.ts packages/daemon/test/app-channel.test.ts packages/daemon/test/app-channel-fd.test.ts packages/daemon/test/windows-app-channel-addon.test.ts packages/daemon/test/process-tree.test.ts packages/daemon/test/supervisor.test.ts packages/daemon/test/runtime.test.ts packages/daemon/test/config.test.ts packages/daemon/test/isolation.test.ts packages/daemon/test/private-memory.test.ts packages/daemon/test/lifecycle.test.ts packages/daemon/test/cli.test.ts packages/daemon/test/runtime-build.test.ts
diff --git a/packages/daemon/src/runtime-build.ts b/packages/daemon/src/runtime-build.ts
index 4350abd..ef04a2d 100644
--- a/packages/daemon/src/runtime-build.ts
+++ b/packages/daemon/src/runtime-build.ts
@@ -69,7 +69,30 @@ export function lockedEnginePackages(
}
/** A `pi` command that runs the runtime's own Node on the engine, from wherever the runtime folder is. */
-export function piWrapper(engineEntry: string): string {
+/** Windows staging is explicit and refuses an absent build; Unix never needs the addon. */
+export function runtimeWindowsStopArtifact(
+ platform: NodeJS.Platform,
+ artifact = join(PACKAGE, "native", "windows-stop.node"),
+ present: (path: string) => boolean = existsSync,
+): string | undefined {
+ if (platform !== "win32") return undefined;
+ if (!present(artifact))
+ throw new Error(
+ "Windows stop addon is missing. Run build:windows-stop with existing Node headers and node.lib first.",
+ );
+ return artifact;
+}
+
+export function piWrapper(engineEntry: string, platform: NodeJS.Platform = process.platform): string {
+ if (platform === "win32") {
+ return [
+ "@echo off",
+ "setlocal DisableDelayedExpansion",
+ `"%~dp0..\\node\\bin\\node.exe" "%~dp0..\\daemon\\node_modules\\${engineEntry.replaceAll("/", "\\")}" %*`,
+ "exit /b %errorlevel%",
+ "",
+ ].join("\r\n");
+ }
return `#!/bin/sh
# The engine's \`pi\` command for an installed Gentle Dot runtime (generated by build:runtime).
self=$0
@@ -103,9 +126,11 @@ function engineEntry(): string {
* Builds the daemon part of the installed runtime into `outDir`: `daemon/cli.mjs`
* (the daemon and the protocol bundled; the engine and `ws` external), the files it
* finds next to itself, a `package.json` for a flat install of the external
- * dependencies, and `bin/pi`. Nothing is installed.
+ * dependencies, and `bin/pi`. On Windows the wrapper is `bin/pi.cmd`, and the stop addon goes to
+ * `daemon/native`. Nothing is installed.
*/
export async function buildRuntime(outDir: string): Promise<void> {
+ const nativeArtifact = runtimeWindowsStopArtifact(process.platform);
const daemon = join(outDir, "daemon");
mkdirSync(join(daemon, "extensions"), { recursive: true });
mkdirSync(join(outDir, "bin"), { recursive: true });
@@ -127,6 +152,10 @@ export async function buildRuntime(outDir: string): Promise<void> {
for (const name of ["approval-guard.ts", "screenshot-context.ts"]) {
copyFileSync(join(PACKAGE, "src", "extensions", name), join(daemon, "extensions", name));
}
+ if (nativeArtifact) {
+ mkdirSync(join(daemon, "native"), { recursive: true });
+ copyFileSync(nativeArtifact, join(daemon, "native", "windows-stop.node"));
+ }
const manifest = {
name: "gentle-dot-runtime",
private: true,
@@ -135,5 +164,9 @@ export async function buildRuntime(outDir: string): Promise<void> {
overrides: lockedEnginePackages(dependencies),
};
writeFileSync(join(daemon, "package.json"), `${JSON.stringify(manifest, null, "\t")}\n`);
- writeFileSync(join(outDir, "bin", "pi"), piWrapper(engineEntry()), { mode: 0o755 });
+ writeFileSync(
+ join(outDir, "bin", process.platform === "win32" ? "pi.cmd" : "pi"),
+ piWrapper(engineEntry()),
+ { mode: 0o755 },
+ );
}
diff --git a/packages/daemon/test/runtime-build.test.ts b/packages/daemon/test/runtime-build.test.ts
index 802cad3..f484614 100644
--- a/packages/daemon/test/runtime-build.test.ts
+++ b/packages/daemon/test/runtime-build.test.ts
@@ -13,8 +13,9 @@ import {
import { join } from "node:path";
import { fileURLToPath } from "node:url";
import { beforeAll, describe, expect, it } from "vitest";
-import { lockedEnginePackages } from "../src/runtime-build.ts";
+import { lockedEnginePackages, piWrapper } from "../src/runtime-build.ts";
import { tempDir } from "./helpers.ts";
+import { WINDOWS_STOP_ADDON } from "./support/windows.ts";
const PACKAGE = fileURLToPath(new URL("..", import.meta.url));
const BUILD = join(PACKAGE, "scripts", "build-runtime.ts");
@@ -22,7 +23,14 @@ const ENGINE_CLI = join("@earendil-works", "pi-coding-agent", "dist", "bundle",
let out: string;
+// The real build stages the Windows stop addon, which is built with MSVC (build:windows-stop) and
+// is absent on CI runners; staging rightly refuses without it.
+const addonMissing =
+ process.platform === "win32" && !existsSync(join(PACKAGE, "native", "windows-stop.node"));
+if (addonMissing) console.info(`Windows-only test skip: ${WINDOWS_STOP_ADDON}`);
+
beforeAll(() => {
+ if (addonMissing) return;
out = tempDir();
const build = spawnSync(process.execPath, [BUILD, "--out", out], { encoding: "utf8" });
expect(build.stderr).toBe("");
@@ -37,7 +45,22 @@ function listFiles(dir: string, prefix = ""): string[] {
);
}
-describe("build:runtime", () => {
+describe.skipIf(addonMissing)("build:runtime", () => {
+ it("emits a relocatable Windows cmd wrapper without relying on sh or ambient Node", () => {
+ expect(piWrapper("@earendil-works/pi-coding-agent/dist/bundle/cli.js", "win32")).toBe(
+ '@echo off\r\nsetlocal DisableDelayedExpansion\r\n"%~dp0..\\node\\bin\\node.exe" "%~dp0..\\daemon\\node_modules\\@earendil-works\\pi-coding-agent\\dist\\bundle\\cli.js" %*\r\nexit /b %errorlevel%\r\n',
+ );
+ });
+
+ it("preserves the Unix shell wrapper for Linux and macOS", () => {
+ for (const platform of ["linux", "darwin"] as const) {
+ const wrapper = piWrapper("@earendil-works/pi-coding-agent/dist/bundle/cli.js", platform);
+ expect(wrapper).toContain("#!/bin/sh\n");
+ expect(wrapper).toContain('exec "$runtime/node/bin/node"');
+ expect(wrapper).toContain('"$@"');
+ }
+ });
+
it("emits the daemon part of the runtime layout, without node_modules", () => {
expect(listFiles(out).sort()).toEqual([
"bin/pi",
@@ -61,6 +84,10 @@ describe("build:runtime", () => {
expect(bundle).not.toContain("@gentle-dot/protocol");
expect(bundle).toMatch(/from "ws"/);
expect(bundle).toContain('import("@earendil-works/pi-coding-agent")');
+ // The production addon stays external and resolves beside the staged entry,
+ // not relative to the source checkout; Unix's file layout remains unchanged.
+ expect(bundle).toContain("./native/windows-stop.node");
+ expect(bundle).not.toContain("NAPI_MODULE");
});
it("lists the external dependencies with the lockfile's exact versions", () => {
diff --git a/packages/daemon/test/support/windows.ts b/packages/daemon/test/support/windows.ts
index e6efa98..338eb13 100644
--- a/packages/daemon/test/support/windows.ts
+++ b/packages/daemon/test/support/windows.ts
@@ -18,6 +18,9 @@ export const UNMANAGED_CLI =
export const POSIX_LOGIN_SHELL =
"Windows intentionally ignores POSIX login shells; Linux and macOS cover login-shell Engram discovery";
+export const WINDOWS_STOP_ADDON =
+ "the runtime build needs the Windows stop addon, built with MSVC (build:windows-stop) on the Windows build machine; runs there and on Linux and macOS";
+
const windows = process.platform === "win32";
/** Keep the test and its stat/lstat running; only POSIX permission bits are platform-specific. */
diff --git a/packages/daemon/test/windows-stop.test.ts b/packages/daemon/test/windows-stop.test.ts
index 66ae100..b8eb9dc 100644
--- a/packages/daemon/test/windows-stop.test.ts
+++ b/packages/daemon/test/windows-stop.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
+import { runtimeWindowsStopArtifact } from "../src/runtime-build.ts";
import type { WindowsAppChannelCloseResult } from "../src/windows-app-channel.ts";
import * as stopModule from "../src/windows-stop.ts";
import { startWithWindowsStop, windowsShutdownExitCode, windowsStopArtifact } from "../src/windows-stop.ts";
@@ -521,6 +522,13 @@ describe("Windows production stop bootstrap (S8, S10, S11)", () => {
),
).rejects.toThrow("startup failed");
});
+ it("fails Windows staging without an explicit native build and leaves Unix unchanged", () => {
+ const present = vi.fn(() => false);
+ expect(runtimeWindowsStopArtifact("linux", "/addon.node", present)).toBeUndefined();
+ expect(present).not.toHaveBeenCalled();
+ expect(() => runtimeWindowsStopArtifact("win32", "/addon.node", present)).toThrow("build:windows-stop");
+ expect(runtimeWindowsStopArtifact("win32", "/addon.node", () => true)).toBe("/addon.node");
+ });
it("uses the same native artifact relative to source and staged daemon entry", () => {
expect(windowsStopArtifact(new URL("file:///repo/packages/daemon/src/windows-stop.ts")).pathname).toBe(
"/repo/packages/daemon/native/windows-stop.node",