/**
 * ag-spawn command - Start a sub-agent in the background
 *
 * Usage: ag-spawn <id> <command>
 */
import { sanitizeErrorMessage } from "../../fs/sanitize-error.js";
export const agSpawn = {
    name: "ag-spawn",
    execute: async (args, ctx) => {
        const id = args[0];
        const command = args.slice(1).join(" ");
        if (!id || !command) {
            return {
                stdout: "",
                stderr: "Usage: ag-spawn <id> <command>\n",
                exitCode: 1,
            };
        }
        if (!ctx.bash) {
            return {
                stdout: "",
                stderr: "Error: Bash reference missing in context\n",
                exitCode: 1,
            };
        }
        try {
            const manager = ctx.bash.services.agentManager;
            // AgentManager.spawn consumes only the BashHost surface (fs, limits,
            // nestingDepth, getEnv, getCwd), so ctx.bash is passed directly with no cast.
            await manager.spawn(id, command, ctx.bash);
            return {
                stdout: `Spawned sub-agent ${id} in background.\n`,
                stderr: "",
                exitCode: 0,
            };
        }
        catch (e) {
            return {
                stdout: "",
                stderr: `Spawn failed: ${sanitizeErrorMessage(e.message)}\n`,
                exitCode: 1,
            };
        }
    },
};
