Files
yuez 7e780515ac 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`.
2026-10-22 15:47:48 +08:00

49 lines
1.9 KiB
TypeScript

import { mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
export interface SessionSpec {
/** File name inside the encoded session directory. */
name: string;
/** cwd written into the session header (authoritative). */
cwd: string;
id?: string;
timestamp?: string;
/** Extra JSONL lines appended after the header (never parsed by discovery). */
body?: string;
/** File mtime, used to test newest-file selection. */
mtime?: Date | number;
}
export function makeSandbox(prefix = "pi2-daemon-ws-"): string {
return mkdtempSync(join(tmpdir(), prefix));
}
export function cleanup(dir: string | undefined): void {
if (dir) rmSync(dir, { recursive: true, force: true });
}
/** Mimics pi's `--<cwd with slashes replaced by dashes>--` directory naming. */
export function encodedDirName(cwd: string): string {
return `--${cwd.replace(/^\/+/, "").replace(/\//g, "-")}--`;
}
export function sessionHeader(cwd: string, id: string, timestamp = "2026-10-10T06:40:33.032Z"): string {
return JSON.stringify({ type: "session", version: 3, id, timestamp, cwd });
}
/** Creates an encoded sessions directory with real-looking JSONL files. */
export function writeSessionDir(root: string, dirName: string, files: SessionSpec[]): string {
const dir = join(root, dirName);
mkdirSync(dir, { recursive: true });
for (const file of files) {
const id = file.id ?? "01a00000-0000-7000-8000-000000000000";
const timestamp = file.timestamp ?? "2026-10-10T06:40:33.032Z";
const body = file.body ?? '{"type":"message","id":"m1","message":{"role":"user","content":"hi"}}\n';
const path = join(dir, file.name);
writeFileSync(path, `${sessionHeader(file.cwd, id, timestamp)}\n${body}`);
if (file.mtime !== undefined) utimesSync(path, file.mtime, file.mtime);
}
return dir;
}