/** * A command this extension spawned and is still waiting for. * * The built-in shell tools run a command inside `exec` and cannot be interrupted * from outside: while pi waits, there is no handle to the process. A foreground * run keeps that handle (ADR-0020), so the wait can end in two ways — the process * exits, or it is let go and adopted as an ordinary process task. Nothing is * signalled in between: the pid, the output file and the command stay the same. * * Only the process that spawned a command can hand it over, which is why the * takeover (`PI_PRETTY_TASK_ADOPT_FOREGROUND`) exists at all: a tool this * extension does not own has its own process somewhere else. * * @author mystery4f <40482524+mystery4f@users.noreply.github.com> */ import type { BashOperations } from "@earendil-works/pi-coding-agent"; import type { TaskScope } from "../base/runtime-state.js"; import type { ProcessTasks } from "./process-tasks.js"; import type { RunningProcess } from "./supervisor.js"; /** How long the output poll waits between reads, in milliseconds. */ export declare const FOREGROUND_POLL_MS = 200; /** Why the wait for a foreground command ended. */ export type ForegroundOutcome = /** The command exited on its own; the shell's code is the call's result. */ { readonly kind: "exited"; readonly exitCode: number; } /** The command is a task now and keeps running; the call returns without it. */ | { readonly kind: "detached"; readonly taskId: string; } /** The caller withdrew (Esc); the tree was stopped. */ | { readonly kind: "aborted"; } /** The call's own `timeout` ran out; the tree was stopped. */ | { readonly kind: "timedOut"; readonly seconds: number; } /** The run could not continue at all — the output cap, or a spawn error. */ | { readonly kind: "failed"; readonly message: string; }; /** What one detach attempt answered; the caller reports it and nothing else. */ export type DetachResult = { readonly kind: "detached"; readonly taskId: string; } /** Nothing was in the foreground to move. */ | { readonly kind: "nothing"; } /** More than one foreground command is running, so the key cannot pick one. */ | { readonly kind: "ambiguous"; readonly running: number; } /** The task that would have been created was refused; the command keeps waiting. */ | { readonly kind: "refused"; readonly message: string; } /** A detach for this run is already under way. */ | { readonly kind: "pending"; }; /** What a caller supplies to put one command in the foreground. */ export interface StartForegroundInput extends TaskScope { readonly command: string; /** Directory to run in; the session's cwd unless the call names another. */ readonly workingDirectory?: string; /** The environment pi prepared for this call, session variables included. */ readonly env?: NodeJS.ProcessEnv; /** The call's own `timeout`, in seconds: this one kills. */ readonly timeoutSeconds?: number; } export interface ForegroundRunnerOptions { readonly tasks: ProcessTasks; /** * Milliseconds a run may hold the turn before it detaches; `0` never detaches * on a timer and leaves the shortcuts as the only trigger. */ readonly detachAfterMs: number; readonly now?: () => number; readonly platform?: NodeJS.Platform; /** Overrides the output poll interval, mostly for tests. */ readonly pollMs?: number; } /** The line a detached call ends with, in pi's own status-line shape. */ export declare function detachedNotice(taskId: string): string; /** What the shortcut's notification says, one line per outcome. */ export declare function describeDetach(result: DetachResult): string; /** * One foreground command: the process, the file it writes to, and the single * outcome that ends the wait. * * `settled` resolves exactly once, so both a natural exit and a detach can race * for it and the loser does nothing. That is the whole safety story of a detach: * whoever wins leaves the process where it is. */ export declare class ForegroundRun { readonly id: string; readonly sessionId: string; /** Directory the session lives in; a detached record belongs here, not in the command's. */ readonly sessionCwd: string; readonly command: string; readonly workingDirectory: string; readonly outputPath: string; readonly startedAt: number; /** Milliseconds the run may hold the turn; `0` never detaches on a timer. */ readonly detachAfterMs: number; private process; private outcome; private killIntent; private detaching; private settle; /** Resolves once, with the outcome that ended this run. */ readonly settled: Promise; constructor(fields: { readonly id: string; readonly sessionId: string; readonly sessionCwd: string; readonly command: string; readonly workingDirectory: string; readonly outputPath: string; readonly startedAt: number; readonly detachAfterMs: number; }); get running(): RunningProcess | undefined; /** True once the wait has ended, whichever way it ended. */ get done(): boolean; /** True while a detach is between its decision and its task record. */ get detachInFlight(): boolean; /** Attach the process, and end the run when it ends. */ bind(running: RunningProcess): void; /** Record this run's one outcome; a second caller changes nothing. */ finish(outcome: ForegroundOutcome): boolean; /** Claim the detach; false when one is under way or the run already ended. */ beginDetach(): boolean; /** The detach was refused, so the run keeps waiting as a foreground command. */ abandonDetach(): void; /** The caller withdrew: stop the tree and report the withdrawal. */ abort(): void; /** The call's own timeout ran out: stop the tree and report the timeout. */ expire(seconds: number): void; } /** * The foreground commands of one extension instance, and the two triggers that * can let one go: a shortcut, or the configured timeout. */ export declare class ForegroundRunner { private readonly tasks; private readonly detachAfterMs; private readonly now; private readonly platform; private readonly pollMs; private readonly runs; private scope; constructor(options: ForegroundRunnerOptions); /** * The session these runs belong to. `BashOperations.exec` gets no context, so * the scope arrives from the events that do have one. */ setScope(scope: TaskScope): void; /** Foreground commands that are still being waited for. */ live(): ForegroundRun[]; /** How many runs the runner still holds; a finished one must not stay. */ get held(): number; /** * Spawn a command in the foreground and hand back the handle. * * The reservation comes first so the record that a detach may create keeps the * id the output file already carries; the process is spawned into that file. * The configured timeout arms here, which is what makes "run a long command, * stop waiting after N minutes" work without any key press. */ start(input: StartForegroundInput): Promise; /** * The operations pi's own shell tools and `!` commands run on when the takeover * is on. Output streams from the run's file into pi's accumulator, so a * command looks the same in the transcript as it did before the takeover. */ operations(): BashOperations; /** Let exactly one foreground command go; nothing happens with zero or several. */ detachOnlyOne(): Promise; /** * Let one run go: it becomes a task under the id it already had, and the call * that was waiting returns without an exit code. * * A refusal — the session's task ceiling — leaves the run in the foreground, * because a command that could not become a task must not vanish from the call * that is still waiting for it. A second trigger while the first is asking * answers `pending` rather than creating a second record. */ detach(run: ForegroundRun): Promise; /** * Let every command still in front go, for a shutdown. A detached run leaves a * record and an id to follow after the reload; a run nobody adopted leaves * neither, and its tree outlives the extension as an orphan (ADR-0020). */ releaseAll(): Promise; /** Arm the call's own timeout and the configured detach deadline, in that order. */ private armTimers; /** * Wait for the run's outcome and translate it into what pi's shell tool expects: * an exit code, or the exception its own message is built from. The output is * pumped from the file until the wait ends, so the transcript keeps filling * while the command runs. */ private awaitRun; }