import { execa } from 'execa';
import { config } from '../config.js';
import { assertAllowedBinary } from './sandbox.js';
/**
 * Runs a command to completion (foreground) and returns its combined,
 * length-capped output. Intended for install/build/lint/test-style commands
 * that the agent needs the final result of, as opposed to long-running dev
 * servers (see processManager.ts for those).
 */
export async function runCommand(command, args, options) {
    assertAllowedBinary(command);
    const timeoutMs = options.timeoutMs ?? config.defaultCommandTimeoutMs;
    const result = await execa(command, args, {
        cwd: options.cwd,
        env: options.env,
        timeout: timeoutMs,
        reject: false,
        all: true,
    });
    const rawOutput = result.all ?? [result.stdout, result.stderr].filter(Boolean).join('\n');
    return {
        command,
        args,
        cwd: options.cwd,
        exitCode: result.exitCode ?? null,
        timedOut: result.timedOut,
        failed: result.failed,
        output: truncate(String(rawOutput ?? ''), config.maxToolOutputChars),
    };
}
function truncate(text, max) {
    if (text.length <= max)
        return text;
    const omitted = text.length - max;
    return `${text.slice(0, max)}\n... [truncated ${omitted} more characters] ...`;
}
//# sourceMappingURL=runCommand.js.map