Adds Agent Console (@magpie-community/page-agent-console 1.0.0), a community page plugin that drives the Agent CLIs on this computer (Claude Code, Codex, ACP agents) through a local daemon on 127.0.0.1, with its source in packages/agent-console and a registry entry. Claude Code is the user's own `claude` CLI driven over stream-json (daemon/src/providers/claude/cli.ts); no Anthropic SDK is installed, bundled or in the branch's history. Cold install ~45 MB (node-pty ~26 MB), stated in the README and registry summary in en and zh. By qingnian108 (#535). What was run at 8e8f910 (mote, macOS, throwaway HOME, no real accounts): - packages/agent-console: `npm ci --ignore-scripts --workspaces=false`; `node scripts/verify-build.mjs`: all five bundles byte-identical (daemon/dist/main.js 08c95aa3…, app/app.js 9a6911f3…); `vitest run`: 45 files, 254 passed, 17 skipped; `npm run check` clean. - Real Claude Code 2.1.316 (npm @anthropic-ai/claude-code, sandbox HOME/CLAUDE_CONFIG_DIR, local fake Messages API): `AC_REAL_CLAUDE=… vitest run --config vitest.real.config.ts test/real/claude-cli.real.test.ts`: 4 of 4 (initialize lists 51 slash commands, streaming turn, Bash permission approved and denied, interrupt, --resume with set_model). - Probe: the driver's exact argv with `--permission-mode bypassPermissions --allow-dangerously-skip-permissions`: the tool runs with no can_use_tool request. - Repo root: `HOME=$(mktemp -d) bun test`: 4006 tests, 0 fail; `bun scripts/check.mjs` exit 0. - Install as magpie does: `npm pack --ignore-scripts` (552 KB) then `bun add --ignore-scripts`: 45 MB node_modules, no @anthropic-ai/*, index.mjs imports. - Read every changed line since 41c7a94: argv values limited to SAFE_ARG tokens (no cmd.exe metacharacters reach the shim path), no new network hosts, no install scripts, no eval; no commit in main..8e8f910 contains claude-agent-sdk. Not tried: magpie's desktop window loading the page; a Windows .cmd/.ps1 claude shim end to end.
104 lines
4.0 KiB
TypeScript
104 lines
4.0 KiB
TypeScript
// Daemon lifeline: --port 0 ready line, Host checked against the actual port, exits when stdin closes, exits when the parent disappears.
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { spawn, type ChildProcess } from "node:child_process";
|
|
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import { join, resolve } from "node:path";
|
|
import { pidAlive, request, waitFor } from "./helpers-net.js";
|
|
|
|
const repo = resolve(__dirname, "..", "..");
|
|
const mainJs = join(repo, "daemon", "dist", "main.js");
|
|
const hasDist = existsSync(mainJs);
|
|
const TOKEN = "lifeline-token";
|
|
|
|
let children: ChildProcess[] = [];
|
|
let dirs: string[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const child of children) {
|
|
if (child.exitCode === null && child.signalCode === null) child.kill("SIGKILL");
|
|
}
|
|
children = [];
|
|
for (const dir of dirs) rmSync(dir, { recursive: true, force: true });
|
|
dirs = [];
|
|
});
|
|
|
|
function startDaemon(parentPid: number): ChildProcess {
|
|
const data = mkdtempSync(join(tmpdir(), "ac-life-"));
|
|
dirs.push(data);
|
|
const child = spawn(process.execPath, [mainJs, "--port", "0", "--data", data, "--parent-pid", String(parentPid)], {
|
|
env: { ...process.env, AGENT_CONSOLE_TOKEN: TOKEN, AGENT_CONSOLE_MOCK: "1" },
|
|
stdio: ["pipe", "pipe", "pipe"],
|
|
});
|
|
children.push(child);
|
|
child.stderr!.resume();
|
|
return child;
|
|
}
|
|
|
|
function readyLine(child: ChildProcess): Promise<{ agentConsoleReady: boolean; port: number; pid: number }> {
|
|
return new Promise((resolvePromise, reject) => {
|
|
let buffer = "";
|
|
const onData = (chunk: Buffer) => {
|
|
buffer += String(chunk);
|
|
for (const line of buffer.split(/\r?\n/)) {
|
|
try {
|
|
const parsed = JSON.parse(line);
|
|
if (parsed.agentConsoleReady) {
|
|
child.stdout!.off("data", onData);
|
|
child.stdout!.resume();
|
|
resolvePromise(parsed);
|
|
return;
|
|
}
|
|
} catch {}
|
|
}
|
|
};
|
|
child.stdout!.on("data", onData);
|
|
child.once("exit", (code) => reject(new Error(`daemon exited early (${code})`)));
|
|
});
|
|
}
|
|
|
|
function exited(child: ChildProcess): Promise<number | null> {
|
|
if (child.exitCode !== null) return Promise.resolve(child.exitCode);
|
|
return new Promise((resolvePromise) => child.once("exit", (code) => resolvePromise(code)));
|
|
}
|
|
|
|
describe.skipIf(!hasDist)("daemon lifeline (daemon/dist/main.js)", () => {
|
|
it("prints a ready line with the real port, checks Host against it, exits when stdin closes", async () => {
|
|
const child = startDaemon(process.pid);
|
|
const ready = await readyLine(child);
|
|
expect(ready.agentConsoleReady).toBe(true);
|
|
expect(ready.port).toBeGreaterThan(0);
|
|
expect(ready.pid).toBe(child.pid);
|
|
|
|
const good = await request(ready.port, "/", { host: `127.0.0.1:${ready.port}` });
|
|
expect(good.status).toBe(200);
|
|
const bad = await request(ready.port, "/", { host: `127.0.0.1:${ready.port + 1}` });
|
|
expect(bad.status).toBe(421);
|
|
|
|
const started = Date.now();
|
|
child.stdin!.end();
|
|
const code = await Promise.race([exited(child), new Promise((r) => setTimeout(() => r("timeout"), 5000))]);
|
|
expect(code).toBe(0);
|
|
expect(Date.now() - started).toBeLessThan(5000);
|
|
}, 15000);
|
|
|
|
it("exits within 7 s when the parent pid dies (stdin kept open)", async () => {
|
|
const dummy = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], { stdio: "ignore" });
|
|
children.push(dummy);
|
|
expect(await waitFor(() => typeof dummy.pid === "number", 2000)).toBe(true);
|
|
|
|
const child = startDaemon(dummy.pid!);
|
|
const ready = await readyLine(child);
|
|
expect(ready.port).toBeGreaterThan(0);
|
|
|
|
dummy.kill("SIGKILL");
|
|
await exited(dummy);
|
|
const started = Date.now();
|
|
const gone = await waitFor(() => child.exitCode !== null || !pidAlive(child.pid!), 7000);
|
|
expect(gone).toBe(true);
|
|
expect(Date.now() - started).toBeLessThan(7000);
|
|
// stdin never closed: the exit really comes from parent detection
|
|
expect(child.stdin!.writableEnded).toBe(false);
|
|
}, 15000);
|
|
});
|