/** * `peaks playwright start | ls | stop` — slice 2.5.0 sub-fix C (Prob 3). * * Multi-terminal resolution for the Playwright MCP. When two or more * terminals / IDE sessions each spawn a `playwright` MCP server, they * fight over the same default port (8931) and the same user data * dir. This command: * * 1. `peaks playwright start [--port N] [--browser ]` * Allocates a unique port (default 8931; walks 8931→8949 if busy), * spawns `npx playwright-mcp@latest` with the chosen port, and * writes a session file at * `/.peaks/_runtime/playwright-sessions/.json` * with `{ port, userDataDir, startedAt, pid }`. * * 2. `peaks playwright ls` — lists all running sessions (reads * `.peaks/_runtime/playwright-sessions/*.json`). * * 3. `peaks playwright stop [--terminal ]` — best-effort kills * the server process and removes the session file. * * Terminal ID derivation (R4): * 1. process.env.TERM_SESSION_ID (macOS Terminal / iTerm2) * 2. process.env.WT_SESSION (Windows Terminal) * 3. hash(process.ppid + process.env.SSH_TTY || 'no-tty') * * The CLI does NOT bundle `playwright-mcp`; it shells out to * `npx playwright-mcp@latest` (G22 / NG3). peaks-cli is the lifecycle * orchestrator, not the install medium. */ import { spawn } from 'node:child_process'; import type { Command } from 'commander'; import { type ProgramIO } from '../cli-helpers.js'; export declare const DEFAULT_PORT = 8931; export declare const MAX_PORT = 8949; declare const BROWSERS: readonly ["chromium", "firefox", "webkit"]; type Browser = typeof BROWSERS[number]; export interface PlaywrightSession { terminalId: string; port: number; browser: Browser; userDataDir: string; startedAt: string; pid?: number; } export declare const PLAYWRIGHT_SESSIONS_DIR = "playwright-sessions"; export declare function playwrightSessionsDir(projectRoot: string): string; export declare function sessionFilePath(projectRoot: string, terminalId: string): string; /** * Derive a stable terminal id from the process environment. Prefers * the platform's own terminal-session id (macOS Terminal, Windows * Terminal) and falls back to a hash of (ppid, tty). */ export declare function deriveTerminalId(env?: NodeJS.ProcessEnv, ppid?: number): string; /** * Walk port range starting at `start` and return the first port * that is NOT bound. Defaults: 8931 → 8949. If the range is exhausted, * returns null. */ export declare function findFreePort(start?: number, max?: number, probe?: (port: number) => Promise): Promise; export declare function listSessions(projectRoot: string): PlaywrightSession[]; export declare function readSession(projectRoot: string, terminalId: string): PlaywrightSession | null; export declare function writeSession(projectRoot: string, session: PlaywrightSession): void; export declare function removeSession(projectRoot: string, terminalId: string): boolean; /** * Spawn the playwright MCP via npx. We do NOT bundle the binary; * we just orchestrate the lifecycle. Returns the child PID so the * session file can record it. */ export declare function spawnPlaywrightMcp(port: number, browser: Browser, userDataDir: string, projectRoot: string): { pid: number | undefined; child: ReturnType; }; export declare function registerPlaywrightCommands(program: Command, _io: ProgramIO): void; /** * Wire `peaks browser action `. The wrapper is a thin adapter * for the 5 intents. The real MCP caller is provided by the IDE / agent * harness — this CLI surface is invoked by tests and skills to drive * browser flows without hand-shelling the MCP protocol. */ export declare function registerBrowserActionCommand(program: Command): void; /** * Resolve and validate a caller-supplied `--user-data-dir`. The dir must * live under `projectRoot` so a malicious `--user-data-dir /etc/foo` * cannot coerce the playwright-mcp browser into writing state to an * arbitrary filesystem path (slice 2026-06-23-audit-3rd #5). * * Resolves `..` segments before checking, so a path like * `/../escape` is correctly normalized and rejected. * * Throws `Error('INVALID_USER_DATA_DIR: ...')` on validation failure; * the action handler catches and emits the JSON envelope. */ export declare function resolveUserDataDir(raw: string, projectRoot: string): string; export {};