diff --git a/.claude/checkpoints/1755024638.json b/.claude/checkpoints/1755024638.json new file mode 100644 index 0000000000..55e080697d --- /dev/null +++ b/.claude/checkpoints/1755024638.json @@ -0,0 +1 @@ +{"branch": "checkpoint/pre-edit-20250812-185037", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/stream-chain.js", "timestamp": "2025-08-12T18:50:38Z", "type": "pre-edit", "original_branch": "alpha-89"} diff --git a/.claude/checkpoints/1755024640.json b/.claude/checkpoints/1755024640.json new file mode 100644 index 0000000000..29f28e61aa --- /dev/null +++ b/.claude/checkpoints/1755024640.json @@ -0,0 +1 @@ +{"tag": "checkpoint-20250812-185040", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/stream-chain.js", "timestamp": "2025-08-12T18:50:40Z", "type": "post-edit", "branch": "alpha-89", "diff_summary": " .claude-flow/metrics/performance.json | 2 +- .claude-flow/metrics/system-metrics.json | 526 +++++++++++++++++++++++++++++-- .claude-flow/metrics/task-metrics.json | 6 +- .claude/checkpoints/1755024563.json | 1 + .claude/checkpoints/1755024565.json | 1 + claude-flow-wiki | 0 src/cli/simple-commands/stream-chain.js | 9 +- 7 files changed, 500 insertions(+), 45 deletions(-) "} diff --git a/src/cli/simple-commands/stream-chain-fixed.js b/src/cli/simple-commands/stream-chain-fixed.js new file mode 100644 index 0000000000..82d22d9ab1 --- /dev/null +++ b/src/cli/simple-commands/stream-chain-fixed.js @@ -0,0 +1,78 @@ +/** + * Execute a single step in the stream chain + */ +async function executeStreamStep(prompt, inputStream, isLast, flags = {}) { + return new Promise((resolve) => { + const startTime = Date.now(); + let resolved = false; // Prevent double resolution + + const safeResolve = (result) => { + if (!resolved) { + resolved = true; + resolve(result); + } + }; + + // Check if we should use mock mode + const useMock = flags.mock || !checkClaudeAvailable(); + + if (useMock) { + // Mock implementation when claude CLI isn't available or mock flag is set + return mockStreamStep(prompt, inputStream, isLast, flags, safeResolve, startTime); + } + + // Set a reasonable timeout for real Claude CLI (15 seconds - more realistic) + const stepTimeout = flags.timeout ? parseInt(flags.timeout) * 1000 : 15000; + + // Build command arguments + const args = ['-p']; + + // For now, avoid stream-json input chaining due to format complexity + // Each step runs independently for better reliability + if (!isLast || flags.json) { + args.push('--output-format', 'stream-json'); + // stream-json output requires --verbose + args.push('--verbose'); + } + + // Add the prompt + args.push(prompt); + + if (flags.verbose) { + console.log(` Debug: Executing: claude ${args.join(' ')}`); + } + + // Use exec with built-in timeout for better reliability + const command = `claude ${args.join(' ')}`; + + exec(command, { + timeout: stepTimeout, + maxBuffer: 1024 * 1024 * 10 // 10MB buffer + }, (error, stdout, stderr) => { + if (resolved) { + return; // Already resolved + } + + const duration = Date.now() - startTime; + + if (error && error.code === 'TIMEOUT') { + // Handle timeout via exec + console.log('⚠️ Claude CLI timed out, falling back to mock mode...'); + mockStreamStep(prompt, inputStream, isLast, { ...flags, mock: true }, safeResolve, Date.now()); + return; + } + + if (flags.verbose && stderr) { + console.error('Error output:', stderr); + } + + safeResolve({ + success: !error || error.code === 0, + duration, + output: stdout || '', + stream: (!isLast && stdout) ? stdout : null, + error: stderr || (error ? error.message : null) + }); + }); + }); +} \ No newline at end of file