/** * Daemon Process Manager * Issue #96: npm install CLI support * Issue #125: Added .env loading and security warnings * SF-1: SRP - Process management only (PID handling delegated to PidManager) */ import { DaemonStatus, StartOptions } from '../types'; import { ServerEnv } from './server-url'; /** How long stop() waits for the server's process group after SIGTERM before escalating. */ export declare const STOP_GRACE_TIMEOUT_MS = 10000; /** * Issue #3731: a server log larger than this is rotated when the next daemon starts. * Same threshold as scripts/build-and-start.sh (MAX_LOG_SIZE_MB=10). */ export declare const SERVER_LOG_MAX_BYTES: number; /** Issue #3731: rotated generations kept (`server.log.1` … `.3`), as in build-and-start.sh. */ export declare const SERVER_LOG_GENERATIONS = 3; /** How long stop() waits for the process group after SIGKILL. */ export declare const STOP_KILL_TIMEOUT_MS = 3000; /** How long stop() waits for the recorded port to be released once the group is gone. */ export declare const PORT_RELEASE_TIMEOUT_MS = 3000; /** * Issue #3731: rotate the server log the way scripts/build-and-start.sh does. * * Runs before the new server is spawned, so nothing holds the file open. When `logPath` has * reached `maxBytes`, the oldest generation is deleted and the rest shift up * (`.2` → `.3`, `.1` → `.2`, current → `.1`). Refuses (throws) on a symbolic link anywhere in * the chain rather than following it. * * @returns true when the log was rotated */ export declare function rotateServerLog(logPath: string, maxBytes?: number, generations?: number): boolean; /** * Issue #3731: open the server log for appending, owner-only (0600), rotating it first. * * `O_NOFOLLOW` refuses a symbolic link planted at the path; fchmod tightens a file created * earlier with looser permissions. The caller hands the descriptor to the child and closes * its own copy. * * @throws when the directory or the file cannot be opened */ export declare function openServerLog(logPath: string): number; /** * What marking a launch reads from the machine; injected so tests never run the * real git, tmux or ps. */ export interface ServerMarkLauncherDeps { /** Directory the record directory is created in. */ base(): string; getuid(): number; /** The CLI's own pid: the start of the parent chain that must reach the tmux pane. */ pid(): number; /** The CLI's own cwd (the server runs in the package root, which says nothing about who launched it). */ cwd(): string; /** The CLI's own environment, for TMUX / TMUX_PANE. */ env(): Readonly>; /** Run a short command; `env` is added to this process's environment. Never throws. */ run(command: string, args: string[], env?: Readonly>): { status: number | null; stdout: string; }; /** 32 hex digits. */ randomHex(): string; now(): Date; } /** A mark decided before the spawn; the record is written once the child's pid is known. */ export interface PreparedServerMark { path: string; launcherCwd: string; worktreeRoot: string | null; session: string; } /** * Decide this launch's mark before the spawn (Issue #4433): make (or check) the * private record directory and read where the launch comes from. Null — the * server then starts unmarked — when the directory is not a directory of this * user's with no access for anyone else, or anything else goes wrong. Never * throws: a mark is never a reason not to start. */ export declare function prepareServerMark(deps: ServerMarkLauncherDeps): PreparedServerMark | null; /** * Write the record of a launch whose detached child is `pid` (its own process * group, so pgid = pid). The leader's start time is read the way the verifier * reads it (`LC_ALL=C TZ=UTC0 ps -o lstart=`). Exclusive create, no symlink * follow, mode 0600. False — the server then carries a mark nobody can verify, * which is judged like no mark — when any of that fails. Never throws. */ export declare function writeServerMarkRecord(mark: PreparedServerMark, pid: number, deps: ServerMarkLauncherDeps): boolean; /** * Daemon manager for background server process */ export declare class DaemonManager { private pidManager; private logger; private envPath?; private effectiveEnv?; private resolveServerLogPath?; private serverMarkDeps; /** * @param pidFilePath - PID file for this server * @param envPath - Issue #1266: the worktree .env this server runs with, so getStatus() * reports the port it actually listens on; omit for the main server * @param resolveServerLogPath - Issue #3731: where start() appends the server's * stdout/stderr (see getServerLogPath()). Resolved at start(), so a config directory that * cannot be resolved only costs the log. Omit to discard the output as before. * @param serverMarkDeps - Issue #4433: overrides for marking the launch (tests) */ constructor(pidFilePath: string, envPath?: string, resolveServerLogPath?: () => string, serverMarkDeps?: Partial); /** * The configuration this server runs with, with .env taking precedence over exported * variables exactly as start() hands it to the child process (Issue #1266). * * Read once per manager: a .env cannot change within a single CLI invocation, and reading * it again would emit dotenv's banner a second time. */ getEffectiveEnv(): ServerEnv; /** * Start daemon process * Issue #125: Load .env file and add security warnings * @returns Process ID of the started daemon * @throws Error if already running */ start(options: StartOptions): Promise; /** * Issue #3731: open the server log for the child and mark where this start begins. * @returns the descriptor, or null (with a warning) when there is no log to write to */ private openLogForChild; /** Throws when the recorded server is running, or an earlier server's group lives on. */ private assertNotRunning; /** Builds the child process env from process.env, .env and the start options. */ private buildChildEnv; /** Decides bind address, port and protocol, warning when exposed without protection. */ private resolveEndpoint; /** Throws when something else already answers on the port the server would bind. */ private assertPortFree; /** Persists the DaemonState; kills the spawned child and throws when the write fails. */ private recordState; /** * Stop daemon process * * Issue #3087: signals the daemon's whole process group, waits for every member to exit * (escalating to SIGKILL after {@link STOP_GRACE_TIMEOUT_MS}), and then requires the recorded * port to be free. "Stopped" is reported only when both hold. A recorded leader that already * exited while its group lives on (the orphaned server of #3087) is stopped the same way. * * @param force Use SIGKILL instead of SIGTERM * @returns true if stopped (or the PID file was stale), false if not running or not stopped */ stop(force?: boolean): Promise; /** * Signal the daemon's process group and wait until no member is left. * * @returns true once the group is gone, false if it outlived SIGKILL */ private terminateGroup; /** * Issue #3087: confirm the port the server was started on is free again. * * A state file from before #1354 records no port; re-deriving one from a .env that may have * changed since could blame an unrelated listener, so that legacy case is not checked. * * @returns true when the port is free (or unknown), false if something still listens on it */ private waitForPortRelease; /** * Get daemon status * @returns Status object or null if no PID file */ getStatus(): Promise; /** * Check if daemon is running */ isRunning(): Promise; /** * Wait until neither the recorded PID nor any member of its process group is alive * (Issue #3087: the group, because the server outlives npm). * * @returns true if everything exited within the timeout */ private waitForExit; } //# sourceMappingURL=daemon.d.ts.map