1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! Migration Validation Test - Claude Code Execution Consolidation
//!
//! # Purpose
//!
//! Validates the successful migration from duplicated execution logic to single
//! execution point with builder pattern.
//!
//! # Why This Test Exists
//!
//! **Problem:** Before migration, Claude Code execution logic was duplicated in
//! `claude_profile` (2x occurrences of `Command::new("claude")`). This caused:
//! - Difficulty updating execution behavior (had to change in multiple places)
//! - Token limit bug (32K default caused "exceeded maximum" errors)
//! - Mixed responsibilities (`claude_profile` handled both storage AND execution)
//!
//! **Solution:** Consolidated to single execution point in `claude_runner_core` with
//! builder pattern API.
//!
//! # Architecture After Migration
//!
//! ```text
//! dream_agent (orchestration)
//! └→ uses ClaudeCommand::new()
//! .with_working_directory()
//! .with_max_output_tokens(200_000) ← BUG FIX
//! .with_message()
//! .execute()
//! └→ uses check_session_exists() from claude_profile
//!
//! claude_runner_core (execution ONLY)
//! └→ Command::new("claude") ← SINGLE POINT
//!
//! claude_profile (session storage ONLY)
//! └→ NO Command::new("claude")
//! ```
//!
//! # Key Metrics Validated
//!
//! - Single execution point: 1 (was 2)
//! - Old API usage: 0 (was 16)
//! - Builder pattern adoption: ✓
//! - Token limit bug fixed: ✓ (200K explicit)
//!
//! # Root Cause of Token Limit Bug
//!
//! Claude Code defaults to 32K `max_output_tokens` when `CLAUDE_CODE_MAX_OUTPUT_TOKENS`
//! environment variable is not set. Complex conversations exceed 32K, causing:
//! "Error: Maximum output tokens exceeded"
//!
//! # Why Not Caught Earlier
//!
//! - No integration tests with real Claude Code execution
//! - Tests used mock outputs under 32K
//! - No visibility into Claude Code's internal token counting
//!
//! # Fix Applied
//!
//! Set explicit `CLAUDE_CODE_MAX_OUTPUT_TOKENS=200000` via:
//! ```rust
//! ClaudeCommand::new()
//! .with_max_output_tokens(200_000)
//! ```
//!
//! # Prevention
//!
//! - Single execution point means token limit set in ONE place
//! - Builder pattern makes configuration explicit and visible
//! - Default of 200K in `ClaudeCommand::new()` prevents regression
//!
//! # Pitfall
//!
//! Don't rely on system defaults for critical configuration. Make all important
//! settings explicit in code.