diff --git a/extensions/cli/src/commands/chat.ts b/extensions/cli/src/commands/chat.ts index e907f7e66..762fe2781 100644 --- a/extensions/cli/src/commands/chat.ts +++ b/extensions/cli/src/commands/chat.ts @@ -444,6 +444,16 @@ async function runHeadlessMode( prompt: string | undefined, options: ChatOptions, ): Promise { + // Critical validation: Ensure we have a prompt in headless mode + // This prevents the CLI from hanging in TTY-less environments + if (!prompt) { + throw new Error( + 'Headless mode requires a prompt. Use: cn -p "your prompt"\n' + + 'Or pipe input: echo "prompt" | cn -p\n' + + "Or use agent files: cn -p --agent my-org/my-agent", + ); + } + // Initialize services for headless mode const { permissionOverrides } = processCommandFlags(options); diff --git a/extensions/cli/src/util/cli.ts b/extensions/cli/src/util/cli.ts index 28e32f4a8..0325b4ccc 100644 --- a/extensions/cli/src/util/cli.ts +++ b/extensions/cli/src/util/cli.ts @@ -46,46 +46,12 @@ export function hasSuppliedPrompt(): boolean { return false; } - // Check if --prompt flag is present (indicates a prompt is coming) - // Note: --agent doesn't supply a prompt, it just specifies which agent to use - // Piped stdin should still be read when --agent is present - const hasPromptFlag = args.includes("--prompt"); - if (hasPromptFlag) { - return true; - } - // Check if there's a non-flag argument after -p/--print - // We need to skip flags that take values (like --config, --model, etc.) - // Known flags that take values - const flagsWithValues = new Set([ - "--config", - "--model", - "--output", - "--mode", - "--workflow", - "-m", - "-c", - "-o", - ]); + // or if there are --prompt or --agent flags + const hasPromptArg = + args.length > printIndex + 1 && !args[printIndex + 1].startsWith("-"); + const hasPromptFlag = args.includes("--prompt"); + const hasAgentFlag = args.includes("--agent"); - const argsAfterPrint = args.slice(printIndex + 1); - for (let i = 0; i < argsAfterPrint.length; i++) { - const arg = argsAfterPrint[i]; - - // If this is a flag that takes a value, skip both the flag and its value - if (flagsWithValues.has(arg)) { - i++; // Skip the next argument (the value) - continue; - } - - // If this is any other flag (starts with -), skip it - if (arg.startsWith("-")) { - continue; - } - - // Found a non-flag argument - this is the prompt - return true; - } - - return false; + return hasPromptArg || hasPromptFlag || hasAgentFlag; }