Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

fix(daemon): isolate npm package selection from bot tools (#1082)

Gustavo Ye601bdd87dac1
diff --git a/src/daemon/src/cli/daemonLifecycle.real.test.ts b/src/daemon/src/cli/daemonLifecycle.real.test.ts
index b93f50a28..d6afccbff 100644
--- a/src/daemon/src/cli/daemonLifecycle.real.test.ts
+++ b/src/daemon/src/cli/daemonLifecycle.real.test.ts
@@ -217,9 +217,30 @@ describe("daemon lifecycle real processes", () => {
it("starts detached while offline, persists no secret, and stops by stable machine id", async () => {
const baseDir = makeBaseDir();
- const started = await runCli(cliArgs(baseDir));
+ const envProbe = path.join(baseDir, "runner-env.cjs");
+ const envResult = path.join(baseDir, "runner-env.json");
+ const registryKey = Object.keys(process.env).find((key) => key.toLowerCase() === "npm_config_registry") ?? "npm_config_registry";
+ fs.writeFileSync(envProbe, [
+ 'if (process.argv.slice(2).join(" ") === "daemon run") {',
+ `require("node:fs").writeFileSync(${JSON.stringify(envResult)}, JSON.stringify({`,
+ 'package: process.env.npm_config_package ?? null,',
+ 'upperPackage: process.env.NPM_CONFIG_PACKAGE ?? null,',
+ 'registry: process.env.npm_config_registry,',
+ '})); }',
+ ].join("\n"));
+ const started = await runCli(cliArgs(baseDir), {
+ NODE_OPTIONS: [process.env.NODE_OPTIONS, `--require ${JSON.stringify(envProbe)}`].filter(Boolean).join(" "),
+ npm_config_package: "@alook/daemon@latest",
+ NPM_CONFIG_PACKAGE: "@alook/daemon@latest",
+ [registryKey]: "https://registry.example.test",
+ });
expect(started.code).toBe(0);
expect(started.output).toContain("started in background");
+ expect(JSON.parse(fs.readFileSync(envResult, "utf8"))).toEqual({
+ package: null,
+ upperPackage: null,
+ registry: "https://registry.example.test",
+ });
const owner = readOwner(baseDir);
expect(alive(owner.pid)).toBe(true);
expect(owner).toMatchObject({ machineId });
diff --git a/src/daemon/src/cli/daemonStart.ts b/src/daemon/src/cli/daemonStart.ts
index 8d069aa2f..62f7ec715 100644
--- a/src/daemon/src/cli/daemonStart.ts
+++ b/src/daemon/src/cli/daemonStart.ts
@@ -13,6 +13,7 @@ import type { DaemonStatusSnapshot } from "../util/statusFile.js";
import { resolveAlookCliPathWithFallback, detectRuntimes, type RuntimeInfo } from "../discovery.js";
import { createLogger } from "../logger.js";
import { readDaemonVersion } from "../version.js";
+import { withoutNpmExecPackage } from "../npmEnvironment.js";
import { runPreparedDaemon, type DaemonReadyReceipt, type PreparedDaemon } from "./daemonRunner.js";
import { spawn, type ChildProcess } from "node:child_process";
import {
@@ -1241,6 +1242,7 @@ function runnerArguments(): string[] {
function spawnBlockedRunner(): ChildProcess {
return spawn(process.execPath, runnerArguments(), {
+ env: withoutNpmExecPackage(process.env),
detached: true,
stdio: ["ignore", "ignore", "ignore", "ipc"],
});
diff --git a/src/daemon/src/manager/agentDriverHost.test.ts b/src/daemon/src/manager/agentDriverHost.test.ts
index 85fcb1a47..442f83908 100644
--- a/src/daemon/src/manager/agentDriverHost.test.ts
+++ b/src/daemon/src/manager/agentDriverHost.test.ts
@@ -14,6 +14,7 @@ function tmp(): string {
}
afterEach(() => {
vi.restoreAllMocks();
+ vi.unstubAllEnvs();
for (const dir of tmpDirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
});
@@ -44,6 +45,21 @@ function broker(): CredentialBroker {
}
describe("createDaemonAgentDriverHost", () => {
+ it("does not pass the daemon launcher's npm package selection to bot tools", async () => {
+ const packageKey = Object.keys(process.env).find((key) => key.toLowerCase() === "npm_config_package") ?? "npm_config_package";
+ const registryKey = Object.keys(process.env).find((key) => key.toLowerCase() === "npm_config_registry") ?? "npm_config_registry";
+ vi.stubEnv(packageKey, "@alook/daemon@latest");
+ vi.stubEnv(registryKey, "https://registry.example.test");
+ const ctx = context(broker());
+ const result = await createDaemonAgentDriverHost(ctx).prepareExecution({ backend: "claude", launchId: "l", workingDirectory: ctx.workingDirectory });
+ if (!result.ok) throw new Error(result.error.message);
+ expect(result.resource.environmentLayers.base).not.toHaveProperty("npm_config_package");
+ expect(result.resource.environmentLayers.base).not.toHaveProperty("NPM_CONFIG_PACKAGE");
+ expect(result.resource.environmentLayers.base[registryKey]).toBe("https://registry.example.test");
+ expect(process.env[packageKey]).toBe("@alook/daemon@latest");
+ await result.resource.release();
+ });
+
it("fails closed without a credential proxy", async () => {
const ctx = context(broker());
delete ctx.credentialProxy;
diff --git a/src/daemon/src/manager/agentDriverHost.ts b/src/daemon/src/manager/agentDriverHost.ts
index f93cdd0bc..4cb90cb95 100644
--- a/src/daemon/src/manager/agentDriverHost.ts
+++ b/src/daemon/src/manager/agentDriverHost.ts
@@ -4,6 +4,7 @@ import { join } from "node:path";
import type { AgentDriverHost, PreparedExecutionResource } from "@alook/agent-driver/host";
import type { HostLaunchContext } from "./hostContext.js";
import { buildGitIdentityEnv, readHostGitIdentity } from "../drivers/gitIdentityEnv.js";
+import { withoutNpmExecPackage } from "../npmEnvironment.js";
function runtimeEnvironment(ctx: HostLaunchContext): Record<string, string | undefined> {
const runtime = ctx.config.runtimeContext;
@@ -70,7 +71,7 @@ export function createDaemonAgentDriverHost(
const resource: PreparedExecutionResource = {
executablePath: ctx.agentCliPath,
environmentLayers: {
- base: { ...process.env },
+ base: withoutNpmExecPackage(process.env),
hostStatic: {},
identityProtected: buildGitIdentityEnv({
agentName: ctx.config.agentName,
diff --git a/src/daemon/src/npmEnvironment.test.ts b/src/daemon/src/npmEnvironment.test.ts
new file mode 100644
index 000000000..53465a2dd
--- /dev/null
+++ b/src/daemon/src/npmEnvironment.test.ts
@@ -0,0 +1,29 @@
+import { describe, expect, it } from "vitest";
+import { withoutNpmExecPackage } from "./npmEnvironment.js";
+
+describe("withoutNpmExecPackage", () => {
+ it("removes inherited package selection case-insensitively without mutating its input", () => {
+ const source = {
+ npm_config_package: "@alook/daemon@latest",
+ NPM_CONFIG_PACKAGE: "@alook/[email protected]",
+ npm_config_registry: "https://registry.example.test",
+ npm_config_cache: "/custom/cache",
+ npm_config_proxy: "http://proxy.example.test",
+ npm_config_userconfig: "/custom/npmrc",
+ NODE_AUTH_TOKEN: "fixture-token",
+ PATH: "/custom/bin",
+ npm_config_yes: "true",
+ };
+ expect(withoutNpmExecPackage(source)).toEqual({
+ npm_config_registry: source.npm_config_registry,
+ npm_config_cache: source.npm_config_cache,
+ npm_config_proxy: source.npm_config_proxy,
+ npm_config_userconfig: source.npm_config_userconfig,
+ NODE_AUTH_TOKEN: source.NODE_AUTH_TOKEN,
+ PATH: source.PATH,
+ npm_config_yes: source.npm_config_yes,
+ });
+ expect(source.npm_config_package).toBe("@alook/daemon@latest");
+ expect(source.NPM_CONFIG_PACKAGE).toBe("@alook/[email protected]");
+ });
+});
diff --git a/src/daemon/src/npmEnvironment.ts b/src/daemon/src/npmEnvironment.ts
new file mode 100644
index 000000000..131437d3b
--- /dev/null
+++ b/src/daemon/src/npmEnvironment.ts
@@ -0,0 +1,5 @@
+export function withoutNpmExecPackage(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv {
+ return Object.fromEntries(
+ Object.entries(env).filter(([key]) => key.toLowerCase() !== "npm_config_package"),
+ );
+}