feat(cli)!: merge the daemon into pi2 as the daemon subcommand

`pi2 daemon [start] [flags]` now starts the web-console daemon. The
`pi2d` executable, its bin entry and its entry module are removed: the
daemon keeps its lock -> token -> plugin socket -> listen -> daemon.json
lifecycle, its single stdout line (`pi2 daemon listening on ...`) and the
signal handling that leaves managed nvim instances running.

Service files are renamed to `pi2.service` / `com.pi2.daemon`, and the
smoke and e2e harnesses start the daemon through `pi2 daemon`.

BREAKING CHANGE: the `pi2d` command no longer exists; use `pi2 daemon`
(or `pi2 daemon start`). Existing systemd/launchd units installed under
the old names must be reinstalled with `pi2 install-service --write`.
This commit is contained in:
yuez committed 2026-10-22 15:47:48 +08:00
1 parent b02f4e9070
commit 7e780515ac
38 files changed
+295 -232

No files matched your search

+8 -5
View File
@@ -49,6 +49,9 @@ const STATE_DIR = process.env.PI2_STATE_DIR ?? join(E2E_ROOT, "state");
const WS_URL = `${BASE.replace(/^http/, "ws")}/ws`;
const REPORT_DIR = join(E2E_ROOT, "report");
const RECON_REPORT = process.env.E2E_RECON_REPORT ?? "/tmp/pi2-e2e-recon/e2e-api-run.md";
// Repository root (this driver lives in `<repo>/scripts/e2e/`), so the reports
// can print a reproducible command without hardcoding a checkout path.
const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../..");
const TURN_MS = 120_000;
const ABORT_SETTLE_MS = 10_000;
@@ -2907,12 +2910,12 @@ function collectLogTails() {
const tail = tailOf(path);
if (tail) targets.push({ label, path, tail });
};
push("daemon", join(E2E_ROOT, "logs", "pi2d.log"));
push("daemon.stdout", join(E2E_ROOT, "logs", "pi2d.stdout"));
push("daemon", join(E2E_ROOT, "logs", "daemon.log"));
push("daemon.stdout", join(E2E_ROOT, "logs", "daemon.stdout"));
for (const path of findLogs(join(STATE_DIR, "instances"))) push(`instance ${path.split("/").slice(-2).join("/")}`, path);
for (const path of findLogs(STATE_DIR)) {
if (targets.some((target) => target.path === path)) continue;
if (/pi2-web|pi2d|instance/.test(path)) push(`state ${path.split("/").slice(-2).join("/")}`, path);
if (/pi2-web|pi2-daemon|instance/.test(path)) push(`state ${path.split("/").slice(-2).join("/")}`, path);
}
return targets;
}
@@ -2952,14 +2955,14 @@ function renderSummaryMarkdown(results, counts, durationMs) {
lines.push("## Reproduction");
lines.push("");
lines.push("```bash");
lines.push("cd /home/yuez/Workspace/Neovim/pi2d && E2E_SKIP_BUILD=1 bash scripts/e2e/run-api.sh");
lines.push(`cd "${REPO_ROOT}" && E2E_SKIP_BUILD=1 bash scripts/e2e/run-api.sh`);
lines.push("```");
return `${lines.join("\n")}\n`;
}
function renderReconMarkdown(results, counts, durationMs, logs) {
const lines = [];
lines.push("# pi2d API/WS E2E (A0–A12) — run report");
lines.push("# pi2 daemon API/WS E2E (A0–A12) — run report");
lines.push("");
lines.push(`- driver: \`${join(dirname(fileURLToPath(import.meta.url)), "api-e2e.mjs")}\``);
lines.push(`- base: \`${BASE}\` · root: \`${E2E_ROOT}\``);
+2 -2
View File
@@ -1,5 +1,5 @@
/**
* Test-only pi extension for the pi2d API/WS end-to-end suite (docs/e2e.md A11).
* Test-only pi extension for the pi2 daemon API/WS end-to-end suite (docs/e2e.md A11).
*
* The suite must prove the pre-execution attention/diff contract without a real
* terminal UI:
@@ -18,7 +18,7 @@
*
* The title payload uses the web console's documented diff shape
* `{toolName, files:[{path, before, after}]}` (see
* `pi2d/web/src/features/chat/diff.ts:diffFilesFromPayload`), so the same JSON
* `web/src/features/chat/diff.ts:diffFilesFromPayload`), so the same JSON
* drives both the plugin's attention classification and the web diff view.
*
* Determinism: the select is issued from the tool call (no timing tricks). A
+4 -4
View File
@@ -30,7 +30,7 @@ fail() {
echo "--- daemon log ---"; tail -60 "$LOG" 2>/dev/null || true
echo "--- instance logs ---"; ls -la "$STATE/instances" 2>/dev/null || true; tail -80 "$STATE"/instances/*.log 2>/dev/null || true
echo "--- nvim web log ---"; find "$STATE" -name 'pi2-web.log' -exec tail -80 {} \; 2>/dev/null || true
echo "--- processes ---"; ps -eo pid,ppid,etime,args | grep -E "nvim|pi --mode|node .*pi2d" | grep -v grep || true
echo "--- processes ---"; ps -eo pid,ppid,etime,args | grep -E "nvim|pi --mode|node .*pi2\.js daemon" | grep -v grep || true
echo "--- sessions endpoint ---"; curl -sS -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/workspaces/$WSID/sessions" 2>/dev/null | head -c 2000 || true
echo
echo "--- state ---"; ls -la "$STATE" "$RUN" 2>/dev/null || true
@@ -39,13 +39,13 @@ fail() {
command -v jq >/dev/null || { echo "jq is required"; exit 2; }
command -v pi >/dev/null || { echo "pi CLI is required"; exit 2; }
[ -f "$REPO/dist/cli/pi2d.js" ] || { echo "run: npx tsc -p tsconfig.json"; exit 2; }
[ -f "$REPO/dist/cli/pi2.js" ] || { echo "run: npx tsc -p tsconfig.json"; exit 2; }
echo "==> workspace $WS"
printf '# e2e\n' > "$WS/README.md"
echo "==> starting pi2d"
node "$REPO/dist/cli/pi2d.js" --foreground --port 0 >"$LOG" 2>&1 &
echo "==> starting pi2 daemon"
node "$REPO/dist/cli/pi2.js" daemon --foreground --port 0 >"$LOG" 2>&1 &
DPID=$!
cleanup() {
kill "$DPID" 2>/dev/null || true
+1 -1
View File
@@ -1,4 +1,4 @@
-- Init used by the pi2d end-to-end harness.
-- Init used by the pi2 daemon end-to-end harness.
-- Minimal, hermetic: only the plugin under test on the runtimepath, with the
-- web console enabled and diagnostics enabled. Setting up the plugin here also
-- makes `pi.host` skip its own `setup()` call.
+7 -7
View File
@@ -71,8 +71,8 @@ e2e_init() {
[ -n "$E2E_NVIM_BIN" ] || e2e_fail "nvim not found"
[ -f "$E2E_PLUGIN/lua/pi/web/commands.lua" ] ||
e2e_fail "plugin worktree has no web layer: $E2E_PLUGIN"
[ -f "$E2E_REPO/dist/cli/pi2d.js" ] ||
e2e_fail "dist/cli/pi2d.js missing; run e2e_build"
[ -f "$E2E_REPO/dist/cli/pi2.js" ] ||
e2e_fail "dist/cli/pi2.js missing; run e2e_build"
[ -f "$E2E_REPO/scripts/e2e/host-init.lua" ] || e2e_fail "host-init.lua missing"
e2e_log "==> $suite root: $E2E_ROOT"
@@ -95,12 +95,12 @@ e2e_build() {
# Wait until the daemon publishes run/daemon.json, then environment-ready.
e2e_start_daemon() {
local log="$E2E_ROOT/logs/pi2d.log"
local log="$E2E_ROOT/logs/daemon.log"
: >"$log"
e2e_log "==> starting pi2d (isolated, port 0)"
e2e_log "==> starting pi2 daemon (isolated, port 0)"
# `exec` makes $! the node process itself, not the wrapping subshell, so
# the trap can actually stop the daemon.
(cd "$E2E_REPO" && exec node dist/cli/pi2d.js --foreground >"$E2E_ROOT/logs/pi2d.stdout" 2>"$log") &
(cd "$E2E_REPO" && exec node dist/cli/pi2.js daemon --foreground >"$E2E_ROOT/logs/daemon.stdout" 2>"$log") &
E2E_DAEMON_PID=$!
export E2E_DAEMON_PID
@@ -109,7 +109,7 @@ e2e_start_daemon() {
[ -f "$PI2_RUNTIME_DIR/daemon.json" ] && break
kill -0 "$E2E_DAEMON_PID" 2>/dev/null || {
tail -40 "$log" >&2 || true
e2e_fail "pi2d exited before writing daemon.json"
e2e_fail "pi2 daemon exited before writing daemon.json"
}
sleep 0.05
done
@@ -202,7 +202,7 @@ e2e_pids() {
local pid
{
ps -eo pid=,args= | grep -F -- "$E2E_ROOT" | grep -v -F -- 'grep -F' | awk '{print $1}' || true
for pid in $(pgrep -f 'pi2d|pi\.host|pi --mode rpc|nvim --headless' 2>/dev/null || true); do
for pid in $(pgrep -f 'pi2\.js daemon|pi\.host|pi --mode rpc|nvim --headless' 2>/dev/null || true); do
if tr '\0' '\n' <"/proc/$pid/environ" 2>/dev/null |
grep -Fxq "PI2_RUNTIME_DIR=$PI2_RUNTIME_DIR"; then
echo "$pid"
+2 -2
View File
@@ -62,7 +62,7 @@ fs.mkdirSync(SCREENS, { recursive: true });
fs.mkdirSync(REPORT_DIR, { recursive: true });
/* ------------------------------------------------------------------ */
/* playwright resolution (not a pi2d dependency) */
/* playwright resolution (not a daemon dependency) */
/* ------------------------------------------------------------------ */
const require_ = createRequire(import.meta.url);
@@ -1568,7 +1568,7 @@ scenario("B7", "Abort: slow prompt → Abort button → Idle within 10 s, partia
ctx.note(`abort status label: ${abortStatusLabel || "(none)"}`);
const partial = (await streamArticles(page)).filter((article) => article.role === "assistant");
ctx.check("partial assistant text remains", partial.length >= 1 && partial[0].text.length > 0, {
ctx.check("partial assistant text remains", partial.some((article) => article.text.length > 0), {
expected: ">=1 assistant article with text",
observed: partial.map((article) => short(article.text, 80)),
});
+1 -1
View File
@@ -24,7 +24,7 @@ export PI2_NVIM_BIN="${PI2_NVIM_BIN:-$(command -v nvim)}"
export PI2_NVIM_INIT="$REPO/scripts/e2e/host-init.lua"
export PI2_E2E_PLUGIN="${PI2_E2E_PLUGIN:-/home/yuez/Workspace/Neovim/pi2.nvim-web}"
node "$REPO/dist/cli/pi2d.js" --foreground --port 0 >"$LOG" 2>&1 &
node "$REPO/dist/cli/pi2.js" daemon --foreground --port 0 >"$LOG" 2>&1 &
DPID=$!
cleanup() { kill "$DPID" 2>/dev/null || true; wait "$DPID" 2>/dev/null || true; }
trap cleanup EXIT
+13 -14
View File
@@ -2,8 +2,8 @@
#
# End-to-end smoke test for the pi2 daemon.
#
# Builds the project, starts `pi2d` on an ephemeral port with throwaway runtime
# dirs, then asserts the HTTP surface (info/health/static/404/auth), the
# Builds the project, starts `pi2 daemon` on an ephemeral port with throwaway
# runtime dirs, then asserts the HTTP surface (info/health/static/404/auth), the
# discovery file and a clean SIGTERM shutdown.
#
# Usage: bash scripts/smoke.sh
@@ -20,7 +20,7 @@ export PI2_RUNTIME_DIR PI2_DATA_DIR PI2_STATE_DIR
export PI2_PORT=0
export PI2_LOG_LEVEL="${PI2_LOG_LEVEL:-warn}"
LOG="$TMP/pi2d.log"
LOG="$TMP/pi2-daemon.log"
DAEMON_JSON="$PI2_RUNTIME_DIR/daemon.json"
LOCK="$PI2_RUNTIME_DIR/daemon.lock"
TOKEN_FILE="$PI2_RUNTIME_DIR/cli.token"
@@ -45,7 +45,7 @@ pass() { printf 'PASS %s\n' "$*"; }
fail() {
printf 'FAIL %s\n' "$*" >&2
if [[ -f "$LOG" ]]; then
printf '\n--- pi2d log ---\n' >&2
printf '\n--- pi2 daemon log ---\n' >&2
tail -n 40 "$LOG" >&2 || true
fi
printf '\n--- tmp dir: %s ---\n' "$TMP" >&2
@@ -102,17 +102,16 @@ if ! npx tsc -p tsconfig.json >"$TMP/build.log" 2>&1; then
cat "$TMP/build.log" >&2
fail "build failed"
fi
[[ -f dist/cli/pi2d.js ]] || fail "dist/cli/pi2d.js missing after build"
[[ -f dist/cli/pi2.js ]] || fail "dist/cli/pi2.js missing after build"
pass "build produced dist/cli/pi2d.js and dist/cli/pi2.js"
pass "build produced dist/cli/pi2.js"
printf '==> starting pi2d (port 0)\n'
node dist/cli/pi2d.js --foreground >"$TMP/stdout.log" 2>"$LOG" &
printf '==> starting pi2 daemon (port 0)\n'
node dist/cli/pi2.js daemon --foreground >"$TMP/stdout.log" 2>"$LOG" &
DAEMON_PID=$!
for _ in $(seq 1 200); do
[[ -f "$DAEMON_JSON" ]] && break
kill -0 "$DAEMON_PID" 2>/dev/null || fail "pi2d exited before writing daemon.json"
kill -0 "$DAEMON_PID" 2>/dev/null || fail "pi2 daemon exited before writing daemon.json"
sleep 0.05
done
[[ -f "$DAEMON_JSON" ]] || fail "daemon.json was not written"
@@ -134,8 +133,8 @@ pass "daemon.lock holds pid $DAEMON_PID"
expect_json "body.protocolVersion === 1" "daemon.json protocolVersion === 1" "$DAEMON_JSON"
grep -q "^pi2d listening on http://127.0.0.1:$PORT$" "$TMP/stdout.log" ||
fail "stdout line missing: expected 'pi2d listening on http://127.0.0.1:$PORT' (got: $(cat "$TMP/stdout.log"))"
grep -q "^pi2 daemon listening on http://127.0.0.1:$PORT$" "$TMP/stdout.log" ||
fail "stdout line missing: expected 'pi2 daemon listening on http://127.0.0.1:$PORT' (got: $(cat "$TMP/stdout.log"))"
pass "stdout announced http://127.0.0.1:$PORT"
[[ -f "$TOKEN_FILE" ]] || fail "cli.token missing"
@@ -175,19 +174,19 @@ pass "GET /api/v1/workspaces without a token -> 401"
expect_status 401 GET /api/v1/workspaces -H "authorization: Bearer deadbeef"
pass "GET /api/v1/workspaces with a bogus token -> 401"
printf '==> stopping pi2d (SIGTERM)\n'
printf '==> stopping pi2 daemon (SIGTERM)\n'
kill -TERM "$DAEMON_PID"
for _ in $(seq 1 100); do
kill -0 "$DAEMON_PID" 2>/dev/null || break
sleep 0.05
done
kill -0 "$DAEMON_PID" 2>/dev/null && fail "pi2d did not exit within 5s of SIGTERM"
kill -0 "$DAEMON_PID" 2>/dev/null && fail "pi2 daemon did not exit within 5s of SIGTERM"
set +e
wait "$DAEMON_PID"
EXIT_CODE=$?
set -e
[[ "$EXIT_CODE" -eq 0 ]] || fail "pi2d exited with code $EXIT_CODE"
[[ "$EXIT_CODE" -eq 0 ]] || fail "pi2 daemon exited with code $EXIT_CODE"
pass "SIGTERM -> clean exit (code 0)"
[[ ! -f "$LOCK" ]] || fail "daemon.lock was not removed on shutdown"