/**
 * Node.js child_process module shim
 * Uses just-bash for command execution in browser with VirtualFS adapter
 */
import { Bash } from "just-bash";
import type { Command as JustBashCommand, ExecResult as JustBashExecResult } from "just-bash";
import { EventEmitter } from "./events";
import { Readable, Writable, Buffer } from "./stream";
import type { Process } from "./process";
import type { ShellCommandDefinition } from "../shell-commands";
import type { VirtualFS } from "../virtual-fs";
import { VirtualFSAdapter } from "./vfs-adapter";
import type { InstallMode, PackageManagerMutationSummary } from "../npm";
type ManagedFrameworkDevServer = {
    key: string;
    framework: "next" | "vite" | "wrangler" | "wrangler-pages";
    port: number;
    stop: () => void;
    clearInstalledPackagesCache?: () => void;
    setHMRTarget?: (targetWindow: Window | null) => void;
};
export interface WorkspaceSearchMatch {
    lineNumber: number;
    lineText: string;
    matchStart: number;
    matchEnd: number;
}
export interface WorkspaceSearchFileResult {
    filePath: string;
    matches: WorkspaceSearchMatch[];
}
export interface WorkspaceSearchResult {
    files: WorkspaceSearchFileResult[];
    limitHit: boolean;
}
export interface WorkspaceSearchOptions {
    pattern: string;
    isRegExp: boolean;
    isCaseSensitive: boolean;
    isWordMatch: boolean;
    folderPath: string;
    includePattern?: string;
    excludePattern?: string;
    maxResults?: number;
    surroundingContext?: number;
}
export interface WorkspaceSearchProvider {
    search(options: WorkspaceSearchOptions): Promise<WorkspaceSearchResult>;
}
export declare const DEFAULT_BROWSER_CLAUDE_CODE_PACKAGE = "@anthropic-ai/claude-code@2.1.112";
interface ActiveProcessStdin {
    emit: (event: string, ...args: unknown[]) => void;
    listenerCount?: (event: string) => number;
}
interface RegisteredShellCommand {
    name: string;
    trusted: boolean;
    interceptShellParsing: boolean;
    command: JustBashCommand;
}
export interface ChildProcessExecutionContext {
    id: string;
    controllerId: string;
    onStdout: ((data: string) => void) | null;
    onStderr: ((data: string) => void) | null;
    signal: AbortSignal | null;
    /** Aborted by the `kill` shell command (independent of the caller's signal). */
    killController: AbortController;
    /** Keep the execution alive until it explicitly exits or is aborted. */
    interactive: boolean;
    activeProcessStdin: ActiveProcessStdin | null;
    activeProcess: Process | null;
    activeForkedChildren: number;
    onForkedChildExit: (() => void) | null;
    activeShellChildren: number;
    /** Set to true by command handlers (e.g. node) that stream their own output via onStdout/onStderr */
    outputStreamed: boolean;
    columns: number;
    rows: number;
}
export type ProcessPidTarget = {
    kind: "server";
    key: string;
} | {
    kind: "execution";
    executionId: string;
};
export interface ChildProcessStreamingCallbacks {
    onStdout: ((data: string) => void) | null;
    onStderr: ((data: string) => void) | null;
    signal: AbortSignal | null;
}
export interface ChildProcessController {
    id: string;
    /** Owning ContainerInstance.id (matches ServerRegistrationMetadata.ownerId), if any. */
    containerId: string | null;
    vfs: VirtualFS;
    vfsAdapter: VirtualFSAdapter;
    bashInstance: Bash;
    installMode: InstallMode;
    onInstallMutation: ((summary: PackageManagerMutationSummary) => void | Promise<void>) | null;
    /** Default streaming hooks applied to executions created without explicit callbacks. */
    legacyStreamingCallbacks: ChildProcessStreamingCallbacks;
    /** Stable synthetic PIDs shared by the `ps` and `kill` shell commands. */
    processPids: Map<number, ProcessPidTarget>;
    processPidByKey: Map<string, number>;
    nextProcessPid: number;
    frameworkDevServers: Map<string, ManagedFrameworkDevServer>;
    executions: Map<string, ChildProcessExecutionContext>;
    builtinShellCommands: Map<string, RegisteredShellCommand>;
    userShellCommands: Map<string, RegisteredShellCommand>;
    activeShellCommands: Map<string, RegisteredShellCommand>;
    keychain?: {
        persistCurrentState(): Promise<void>;
    } | null;
    searchProvider?: WorkspaceSearchProvider | null;
    createExecution: (opts?: {
        onStdout?: (data: string) => void;
        onStderr?: (data: string) => void;
        signal?: AbortSignal;
        interactive?: boolean;
        cols?: number;
        rows?: number;
    }) => ChildProcessExecutionContext;
    destroyExecution: (executionId: string | null | undefined) => void;
    runCommand: (command: string, options?: {
        cwd?: string;
        env?: Record<string, string>;
    }, executionId?: string | null) => Promise<JustBashExecResult>;
    sendInput: (executionId: string | null | undefined, data: string) => void;
    updateExecutionSize: (executionId: string | null | undefined, cols: number, rows: number) => void;
    registerShellCommand: (definition: ShellCommandDefinition) => void;
    unregisterShellCommand: (name: string) => boolean;
}
export interface ChildProcessModuleBinding {
    controller?: ChildProcessController | null;
    getDefaultCwd?: () => string;
    getDefaultEnv?: () => Record<string, string>;
    getExecutionId?: () => string | null;
}
/**
 * Set streaming callbacks for the next command execution.
 * Used by container.run() to enable streaming output from custom commands.
 *
 * @deprecated Module-level alias kept for back-compat — it writes to the
 * default controller's per-controller streaming callbacks. Multi-container
 * callers should pass callbacks via createExecution() instead.
 */
export declare function setStreamingCallbacks(opts: {
    onStdout?: (data: string) => void;
    onStderr?: (data: string) => void;
    signal?: AbortSignal;
}): void;
/**
 * Clear streaming callbacks after command execution.
 *
 * @deprecated See setStreamingCallbacks.
 */
export declare function clearStreamingCallbacks(): void;
export declare function stripInternalChildProcessEnv(env: Record<string, string>): Record<string, string>;
/**
 * Tear down a controller when its container is disposed: stop its framework
 * dev servers, drop its executions, and remove it from the module registries.
 */
export declare function disposeChildProcessController(controller: ChildProcessController): void;
/**
 * Agent process bridges set this env var so managed framework dev servers
 * DETACH: the command returns right after startup instead of holding the
 * shell until abort (interactive-terminal Ctrl+C semantics). The server
 * keeps running under `controller.frameworkDevServers` either way — an
 * agent must be able to start a dev server and move on with its turn.
 */
export declare const DETACH_DEV_SERVERS_ENV = "ALMOSTNODE_DETACH_DEV_SERVERS";
/**
 * Send data to the stdin of the currently running node process.
 * Emits both 'data' and 'keypress' events (vitest uses readline keypress events).
 * The module-level export targets the default controller; runtime-bound
 * modules (createChildProcessModule) target their own controller.
 */
export declare function sendStdin(data: string): void;
/**
 * Initialize the child_process shim with a VirtualFS instance
 * Creates or reuses a controller-scoped Bash instance with VirtualFSAdapter.
 */
export declare function initChildProcess(vfs: VirtualFS, options?: {
    installMode?: InstallMode;
    onInstallMutation?: (summary: PackageManagerMutationSummary) => void | Promise<void>;
    /** ContainerInstance.id of the owning container, for server ownership tagging. */
    containerId?: string;
}): ChildProcessController;
/**
 * Handle `npm install [pkg]` — bridge to PackageManager
 */
/**
 * Map an `npm create` / `npm init` initializer to the package npm would run, per
 * npm's own rules: `foo` → `create-foo`, `@scope` → `@scope/create`,
 * `@scope/foo` → `@scope/create-foo`. A trailing `@version`/`@tag` is preserved.
 */
export declare function initializerToCreatePackage(initializer: string): string;
export interface ExecOptions {
    cwd?: string;
    env?: Record<string, string>;
    encoding?: BufferEncoding | "buffer";
    timeout?: number;
    maxBuffer?: number;
    shell?: string | boolean;
}
export interface ExecResult {
    stdout: string | Buffer;
    stderr: string | Buffer;
}
export type ExecCallback = (error: Error | null, stdout: string | Buffer, stderr: string | Buffer) => void;
export declare function splitCommandArgs(command: string): string[];
/**
 * Execute a command in a shell
 */
export declare function exec(command: string, optionsOrCallback?: ExecOptions | ExecCallback, callback?: ExecCallback): ChildProcess;
export declare function execSync(command: string, options?: ExecOptions): string | Buffer;
export interface SpawnOptions {
    cwd?: string;
    env?: Record<string, string>;
    shell?: boolean | string;
    stdio?: "pipe" | "inherit" | "ignore" | Array<"pipe" | "inherit" | "ignore" | number>;
    encoding?: BufferEncoding | "buffer" | null;
    timeout?: number;
    detached?: boolean;
    windowsHide?: boolean;
}
type SpawnSyncOutput = string | Buffer;
type SpawnSyncResult = {
    stdout: SpawnSyncOutput;
    stderr: SpawnSyncOutput;
    status: number;
    error?: Error;
};
/**
 * Spawn a new process
 */
export declare function spawn(command: string, args?: string[] | SpawnOptions, options?: SpawnOptions): ChildProcess;
export declare function spawnSync(command: string, args?: string[], options?: SpawnOptions): SpawnSyncResult;
export declare function execFile(file: string, args?: string[] | ExecOptions | ExecCallback, options?: ExecOptions | ExecCallback, callback?: ExecCallback): ChildProcess;
export declare function execFileSync(file: string, argsOrOptions?: string[] | ExecOptions, options?: ExecOptions): string | Buffer;
export declare function fork(modulePath: string, argsOrOptions?: string[] | Record<string, unknown>, options?: Record<string, unknown>): ChildProcess;
/**
 * ChildProcess class
 */
export declare class ChildProcess extends EventEmitter {
    pid: number;
    connected: boolean;
    killed: boolean;
    exitCode: number | null;
    signalCode: string | null;
    spawnargs: string[];
    spawnfile: string;
    stdin: Writable | null;
    stdout: Readable | null;
    stderr: Readable | null;
    stdio: [Writable | null, Readable | null, Readable | null];
    constructor();
    kill(signal?: string): boolean;
    disconnect(): void;
    send(message: unknown, callback?: (error: Error | null) => void): boolean;
    ref(): this;
    unref(): this;
}
export declare function getDefaultChildProcessController(): ChildProcessController | null;
export declare function createChildProcessModule(binding?: ChildProcessModuleBinding): {
    exec: (command: string, optionsOrCallback?: ExecOptions | ExecCallback, callback?: ExecCallback) => ChildProcess;
    execSync: (command: string, options?: ExecOptions) => string | Buffer;
    execFile: (file: string, args?: string[] | ExecOptions | ExecCallback, options?: ExecOptions | ExecCallback, callback?: ExecCallback) => ChildProcess;
    execFileSync: (file: string, argsOrOptions?: string[] | ExecOptions, options?: ExecOptions) => string | Buffer;
    spawn: (command: string, args?: string[] | SpawnOptions, options?: SpawnOptions) => ChildProcess;
    spawnSync: (command: string, args?: string[], options?: SpawnOptions) => SpawnSyncResult;
    fork: (modulePath: string, argsOrOptions?: string[] | Record<string, unknown>, options?: Record<string, unknown>) => ChildProcess;
    ChildProcess: typeof ChildProcess;
    initChildProcess: typeof initChildProcess;
    setStreamingCallbacks: typeof setStreamingCallbacks;
    clearStreamingCallbacks: typeof clearStreamingCallbacks;
    sendStdin: (data: string) => void;
};
declare const _default: {
    exec: typeof exec;
    execSync: typeof execSync;
    execFile: typeof execFile;
    execFileSync: typeof execFileSync;
    spawn: typeof spawn;
    spawnSync: typeof spawnSync;
    fork: typeof fork;
    ChildProcess: typeof ChildProcess;
    initChildProcess: typeof initChildProcess;
    setStreamingCallbacks: typeof setStreamingCallbacks;
    clearStreamingCallbacks: typeof clearStreamingCallbacks;
};
export default _default;
//# sourceMappingURL=child_process.d.ts.map