swarm-engine-eval 0.1.6

Evaluation framework for SwarmEngine
Documentation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! InternalDiagnosisEnvironment - 独自DSL + カスタムログの診断環境
//!
//! Worker Fine-tuning の効果検証用シナリオ。
//! 汎用 SLM では解けない独自ドメイン知識が必要。
//!
//! # 特徴
//!
//! - **独自 DSL**: SwarmEngine 固有の設定言語(YAML ベース)
//! - **カスタムエラーコード**: `[SW-XXXX]` 形式
//! - **依存関係グラフ**: 設定間の依存を理解する必要
//!
//! # アクション
//!
//! - `ParseConfig`: 設定ファイルをパース、構文/意味エラーを検出
//! - `AnalyzeLog`: ログからエラーパターンを検出
//! - `TraceError`: エラーコードから根本原因を特定
//! - `ApplyFix`: 設定を修正
//!
//! # DependencyGraph
//!
//! ```text
//! ParseConfig → AnalyzeLog | TraceError
//! AnalyzeLog → TraceError
//! TraceError → ApplyFix (terminal)
//! ```

use std::collections::HashMap;
use std::sync::RwLock;

use swarm_engine_core::actions::ParamResolver;
use swarm_engine_core::agent::WorkResult;
use swarm_engine_core::environment::Environment;
use swarm_engine_core::types::{Action, WorkerId};

// ============================================================================
// Error Code System
// ============================================================================

/// SwarmEngine 独自エラーコード
#[derive(Debug, Clone, PartialEq)]
pub enum SwarmErrorCode {
    /// SW-1001: Routing configuration invalid
    Sw1001RoutingInvalid,
    /// SW-1002: Failover target not found
    Sw1002FailoverNotFound,
    /// SW-1003: Circuit breaker threshold exceeded
    Sw1003CircuitBreakerTriggered,
    /// SW-2001: Worker pool exhausted
    Sw2001WorkerPoolExhausted,
    /// SW-2002: Manager timeout
    Sw2002ManagerTimeout,
    /// SW-3001: Strategy mismatch
    Sw3001StrategyMismatch,
    /// SW-3002: Exploration depth limit
    Sw3002ExplorationDepthLimit,
}

impl SwarmErrorCode {
    fn code(&self) -> &str {
        match self {
            Self::Sw1001RoutingInvalid => "SW-1001",
            Self::Sw1002FailoverNotFound => "SW-1002",
            Self::Sw1003CircuitBreakerTriggered => "SW-1003",
            Self::Sw2001WorkerPoolExhausted => "SW-2001",
            Self::Sw2002ManagerTimeout => "SW-2002",
            Self::Sw3001StrategyMismatch => "SW-3001",
            Self::Sw3002ExplorationDepthLimit => "SW-3002",
        }
    }

    fn description(&self) -> &str {
        match self {
            Self::Sw1001RoutingInvalid => {
                "Routing configuration is invalid. Check swarm.routing.targets format."
            }
            Self::Sw1002FailoverNotFound => {
                "Failover target service not found. Verify swarm.failover.target exists."
            }
            Self::Sw1003CircuitBreakerTriggered => {
                "Circuit breaker triggered due to high failure rate."
            }
            Self::Sw2001WorkerPoolExhausted => {
                "Worker pool exhausted. Increase swarm.workers.max_count."
            }
            Self::Sw2002ManagerTimeout => {
                "Manager decision timeout. Check swarm.manager.timeout_ms."
            }
            Self::Sw3001StrategyMismatch => {
                "Strategy mismatch between manager and workers. Align swarm.strategy.type."
            }
            Self::Sw3002ExplorationDepthLimit => {
                "Exploration depth limit reached. Adjust swarm.exploration.max_depth."
            }
        }
    }

    fn fix_hint(&self) -> &str {
        match self {
            Self::Sw1001RoutingInvalid => "routing.targets",
            Self::Sw1002FailoverNotFound => "failover.target",
            Self::Sw1003CircuitBreakerTriggered => "circuit_breaker.threshold",
            Self::Sw2001WorkerPoolExhausted => "workers.max_count",
            Self::Sw2002ManagerTimeout => "manager.timeout_ms",
            Self::Sw3001StrategyMismatch => "strategy.type",
            Self::Sw3002ExplorationDepthLimit => "exploration.max_depth",
        }
    }

    fn correct_value(&self) -> &str {
        match self {
            Self::Sw1001RoutingInvalid => "[\"worker-1\", \"worker-2\"]",
            Self::Sw1002FailoverNotFound => "backup-service",
            Self::Sw1003CircuitBreakerTriggered => "0.7",
            Self::Sw2001WorkerPoolExhausted => "16",
            Self::Sw2002ManagerTimeout => "5000",
            Self::Sw3001StrategyMismatch => "ucb1",
            Self::Sw3002ExplorationDepthLimit => "10",
        }
    }
}

// ============================================================================
// Configuration Problem
// ============================================================================

/// 設定問題の種類
#[derive(Debug, Clone)]
pub struct ConfigProblem {
    /// エラーコード
    pub error_code: SwarmErrorCode,
    /// 問題のある設定キー
    pub config_key: String,
    /// 現在の(誤った)値
    pub current_value: String,
    /// 関連するログ
    pub logs: Vec<String>,
}

impl ConfigProblem {
    fn new(error_code: SwarmErrorCode, config_key: &str, current_value: &str) -> Self {
        let logs = Self::generate_logs(&error_code);
        Self {
            error_code,
            config_key: config_key.to_string(),
            current_value: current_value.to_string(),
            logs,
        }
    }

    fn generate_logs(error_code: &SwarmErrorCode) -> Vec<String> {
        let timestamp = "2024-01-15T10:30:45.123Z";
        let code = error_code.code();

        vec![
            format!("[{}] ERROR [{}] {}", timestamp, code, error_code.description()),
            format!("[{}] WARN  [{}] Attempting recovery...", timestamp, code),
            format!("[{}] ERROR [{}] Recovery failed, escalating", timestamp, code),
            format!("[{}] INFO  {{\"error_code\":\"{}\",\"component\":\"swarm-core\",\"action\":\"shutdown\"}}", timestamp, code),
        ]
    }
}

// ============================================================================
// InternalDiagnosisEnvironment
// ============================================================================

/// 内部診断環境
pub struct InternalDiagnosisEnvironment {
    /// 設定問題
    problem: ConfigProblem,
    /// 全設定(正常 + 問題)
    config: HashMap<String, String>,
    /// 内部状態
    state: RwLock<DiagnosisState>,
}

#[derive(Debug, Default)]
struct DiagnosisState {
    /// ParseConfig を実行したか
    parsed_config: bool,
    /// AnalyzeLog を実行したか
    analyzed_logs: bool,
    /// TraceError を実行したか(特定したエラーコード)
    traced_error: Option<String>,
    /// 完了した Worker
    completed: Vec<WorkerId>,
}

impl InternalDiagnosisEnvironment {
    /// 新しい環境を作成
    pub fn new(problem: ConfigProblem) -> Self {
        let mut config = Self::default_config();
        // 問題のある設定を上書き
        config.insert(problem.config_key.clone(), problem.current_value.clone());

        Self {
            problem,
            config,
            state: RwLock::new(DiagnosisState::default()),
        }
    }

    /// デフォルト設定を生成
    fn default_config() -> HashMap<String, String> {
        let mut config = HashMap::new();
        // 正常な設定値
        config.insert(
            "swarm.routing.targets".into(),
            "[\"worker-1\", \"worker-2\", \"worker-3\"]".into(),
        );
        config.insert("swarm.failover.target".into(), "backup-service".into());
        config.insert("swarm.failover.enabled".into(), "true".into());
        config.insert("swarm.circuit_breaker.threshold".into(), "0.5".into());
        config.insert("swarm.circuit_breaker.window_ms".into(), "10000".into());
        config.insert("swarm.workers.max_count".into(), "8".into());
        config.insert("swarm.workers.min_count".into(), "2".into());
        config.insert("swarm.manager.timeout_ms".into(), "3000".into());
        config.insert("swarm.manager.retry_count".into(), "3".into());
        config.insert("swarm.strategy.type".into(), "ucb1".into());
        config.insert("swarm.strategy.exploration_c".into(), "1.414".into());
        config.insert("swarm.exploration.max_depth".into(), "5".into());
        config.insert("swarm.exploration.pruning".into(), "true".into());
        config
    }

    // ========================================================================
    // Preset Scenarios
    // ========================================================================

    /// シナリオ: ルーティング設定エラー
    pub fn routing_error_scenario() -> Self {
        let problem = ConfigProblem::new(
            SwarmErrorCode::Sw1001RoutingInvalid,
            "swarm.routing.targets",
            "invalid-format", // 配列であるべきが文字列
        );
        Self::new(problem)
    }

    /// シナリオ: フェイルオーバー設定エラー
    pub fn failover_error_scenario() -> Self {
        let problem = ConfigProblem::new(
            SwarmErrorCode::Sw1002FailoverNotFound,
            "swarm.failover.target",
            "nonexistent-service",
        );
        Self::new(problem)
    }

    /// シナリオ: ワーカープール枯渇
    pub fn worker_pool_scenario() -> Self {
        let problem = ConfigProblem::new(
            SwarmErrorCode::Sw2001WorkerPoolExhausted,
            "swarm.workers.max_count",
            "2", // 少なすぎる
        );
        Self::new(problem)
    }

    /// シナリオ: 戦略ミスマッチ
    pub fn strategy_mismatch_scenario() -> Self {
        let problem = ConfigProblem::new(
            SwarmErrorCode::Sw3001StrategyMismatch,
            "swarm.strategy.type",
            "unknown_strategy",
        );
        Self::new(problem)
    }

    /// シナリオ: 探索深度制限
    pub fn exploration_depth_scenario() -> Self {
        let problem = ConfigProblem::new(
            SwarmErrorCode::Sw3002ExplorationDepthLimit,
            "swarm.exploration.max_depth",
            "1", // 浅すぎる
        );
        Self::new(problem)
    }

    /// 複雑シナリオ(カスケード障害)
    pub fn complex_scenario(seed: u64) -> Self {
        // シード値に応じてシナリオを選択
        let scenarios = [
            Self::routing_error_scenario,
            Self::failover_error_scenario,
            Self::worker_pool_scenario,
            Self::strategy_mismatch_scenario,
            Self::exploration_depth_scenario,
        ];
        let idx = (seed as usize) % scenarios.len();
        scenarios[idx]()
    }

    // ========================================================================
    // Action Handlers
    // ========================================================================

    fn handle_parse_config(&self, _worker_id: WorkerId, action: &Action) -> WorkResult {
        let section = action
            .params
            .args
            .get("section")
            .or(action.params.target.as_ref())
            .cloned()
            .filter(|s| !s.is_empty());

        let mut state = self.state.write().unwrap();
        state.parsed_config = true;

        match section {
            Some(section) => {
                // 特定セクションをパース
                let prefix = format!("swarm.{}.", section);
                let matching: Vec<_> = self
                    .config
                    .iter()
                    .filter(|(k, _)| k.starts_with(&prefix))
                    .collect();

                if matching.is_empty() {
                    // Unknown section: show all configuration (lenient fallback)
                    let mut output = format!(
                        "Section '{}' not found. Showing all configuration:\n\n=== SwarmEngine Configuration ===\n\n",
                        section
                    );

                    let sections = [
                        "routing",
                        "failover",
                        "circuit_breaker",
                        "workers",
                        "manager",
                        "strategy",
                        "exploration",
                    ];
                    for sec in sections {
                        let sec_prefix = format!("swarm.{}.", sec);
                        let sec_matching: Vec<_> = self
                            .config
                            .iter()
                            .filter(|(k, _)| k.starts_with(&sec_prefix))
                            .collect();

                        if !sec_matching.is_empty() {
                            output.push_str(&format!("[{}]\n", sec));
                            for (key, value) in sec_matching {
                                let short_key = key.strip_prefix(&sec_prefix).unwrap_or(key);
                                output.push_str(&format!("  {}: {}\n", short_key, value));
                            }
                            output.push('\n');
                        }
                    }

                    WorkResult::env_success(output)
                } else {
                    let mut output = format!("=== Configuration: swarm.{} ===\n", section);
                    for (key, value) in matching {
                        let short_key = key.strip_prefix("swarm.").unwrap_or(key);
                        output.push_str(&format!("{}: {}\n", short_key, value));
                    }

                    // 問題のあるセクションならヒントを出す
                    if self.problem.config_key.starts_with(&prefix) {
                        output.push_str("\n[!] Potential issue detected in this section");
                    }

                    WorkResult::env_success(output)
                }
            }
            None => {
                // 全設定を表示
                let mut output = String::from("=== SwarmEngine Configuration ===\n\n");

                // セクションごとにグループ化
                let sections = [
                    "routing",
                    "failover",
                    "circuit_breaker",
                    "workers",
                    "manager",
                    "strategy",
                    "exploration",
                ];
                for section in sections {
                    let prefix = format!("swarm.{}.", section);
                    let matching: Vec<_> = self
                        .config
                        .iter()
                        .filter(|(k, _)| k.starts_with(&prefix))
                        .collect();

                    if !matching.is_empty() {
                        output.push_str(&format!("[{}]\n", section));
                        for (key, value) in matching {
                            let short_key = key.strip_prefix(&prefix).unwrap_or(key);
                            output.push_str(&format!("  {}: {}\n", short_key, value));
                        }
                        output.push('\n');
                    }
                }

                WorkResult::env_success(output)
            }
        }
    }

    fn handle_analyze_log(&self, _worker_id: WorkerId, action: &Action) -> WorkResult {
        let resolver = ParamResolver::new(action);
        let filter = resolver.get("filter").unwrap_or("");

        let mut state = self.state.write().unwrap();

        // ParseConfig を実行済みか
        if !state.parsed_config {
            return WorkResult::env_failure(
                "Cannot analyze logs without parsing configuration first. Run ParseConfig first.",
            );
        }

        state.analyzed_logs = true;

        let logs: Vec<_> = if filter.is_empty() {
            self.problem.logs.clone()
        } else {
            self.problem
                .logs
                .iter()
                .filter(|log| log.contains(filter))
                .cloned()
                .collect()
        };

        if logs.is_empty() {
            WorkResult::env_success("=== Log Analysis ===\nNo matching logs found.")
        } else {
            let error_code = self.problem.error_code.code();
            let mut output = String::from("=== Log Analysis ===\n\n");
            for log in &logs {
                output.push_str(log);
                output.push('\n');
            }
            output.push_str(&format!("\n[!] Detected error code: {}\n", error_code));
            output.push_str("Use TraceError to investigate this error code.");

            WorkResult::env_success(output)
        }
    }

    fn handle_trace_error(&self, _worker_id: WorkerId, action: &Action) -> WorkResult {
        let resolver = ParamResolver::new(action);
        let error_code_input = resolver.get("code").unwrap_or("");

        let mut state = self.state.write().unwrap();

        // AnalyzeLog を実行済みか
        if !state.analyzed_logs {
            return WorkResult::env_failure(
                "Cannot trace error without analyzing logs first. Run AnalyzeLog first.",
            );
        }

        let expected_code = self.problem.error_code.code();

        // If input is not a valid error code format (SW-XXXX), use the detected error code
        let error_code: String =
            if error_code_input.starts_with("SW-") && error_code_input.len() == 7 {
                error_code_input.to_string()
            } else if error_code_input.is_empty() {
                return WorkResult::env_failure(
                    "TraceError requires an error code. Usage: TraceError(SW-XXXX)",
                );
            } else {
                // Auto-detect: use the error code from logs
                expected_code.to_string()
            };

        if error_code == expected_code {
            state.traced_error = Some(error_code.clone());

            let desc = self.problem.error_code.description();
            let fix_hint = self.problem.error_code.fix_hint();
            let correct_value = self.problem.error_code.correct_value();

            let output = format!(
                "=== Error Trace: {} ===\n\n\
                Description: {}\n\n\
                Root Cause:\n\
                  Configuration key: swarm.{}\n\
                  Current value: {}\n\
                  Expected format: {}\n\n\
                Recommended Fix:\n\
                  ApplyFix(key=\"{}\", value=\"{}\")",
                error_code,
                desc,
                fix_hint,
                self.problem.current_value,
                correct_value,
                fix_hint,
                correct_value
            );

            WorkResult::env_success(output)
        } else {
            WorkResult::env_failure(format!(
                "Error code {} not found in current logs.\nHint: Check the error code from AnalyzeLog output.",
                error_code
            ))
        }
    }

    fn handle_apply_fix(&self, worker_id: WorkerId, action: &Action) -> WorkResult {
        let resolver = ParamResolver::new(action);
        let key_input = resolver.get("key").unwrap_or("");
        let value = resolver.arg("value").unwrap_or("");

        let mut state = self.state.write().unwrap();

        // TraceError を実行済みか
        if state.traced_error.is_none() {
            return WorkResult::env_failure(
                "Cannot apply fix without tracing the error first. Run TraceError first.",
            );
        }

        let fix_hint = self.problem.error_code.fix_hint();
        let correct_value = self.problem.error_code.correct_value();

        // Auto-detect key if input is not a valid config key format
        let key: String = if key_input.is_empty() {
            return WorkResult::env_failure(
                "ApplyFix requires a configuration key. Usage: ApplyFix(key=\"...\", value=\"...\")"
            );
        } else if key_input.contains('.') || key_input.contains('_') {
            // Looks like a config key
            key_input.to_string()
        } else {
            // Not a config key format (e.g., "swarm-config"), use the traced fix hint
            fix_hint.to_string()
        };

        // 正しいキーを修正しているか
        let full_key = if key.starts_with("swarm.") {
            key.clone()
        } else {
            format!("swarm.{}", key)
        };

        let expected_key = format!("swarm.{}", fix_hint);

        if full_key != expected_key {
            return WorkResult::env_failure(format!(
                "Incorrect configuration key: {}\nHint: The issue is in swarm.{}",
                key, fix_hint
            ));
        }

        // 正しい値で修正しているか(厳密には完全一致でなくても許容)
        let value_is_correct = if value.is_empty() {
            false
        } else {
            // 数値の場合は値をパースして比較
            match (value.parse::<f64>(), correct_value.parse::<f64>()) {
                (Ok(v), Ok(c)) => (v - c).abs() < 0.001 || v >= c,
                _ => value.contains(correct_value) || correct_value.contains(value),
            }
        };

        if !value_is_correct && !value.is_empty() {
            // 値が違っても、キーが正しければ部分点
            return WorkResult::env_success(format!(
                "=== Configuration Updated ===\n\
                Key: {}\n\
                Value: {}\n\n\
                [!] Warning: Value may not fully resolve the issue.\n\
                Recommended value: {}",
                full_key, value, correct_value
            ));
        }

        // 成功!
        if !state.completed.contains(&worker_id) {
            state.completed.push(worker_id);
        }

        WorkResult::done_success(format!(
            "=== Configuration Fixed ===\n\
            Key: {}\n\
            Value: {}\n\n\
            [OK] Error {} resolved.\n\
            SwarmEngine is now running normally.",
            full_key,
            if value.is_empty() {
                correct_value
            } else {
                &value
            },
            self.problem.error_code.code()
        ))
    }
}

impl Environment for InternalDiagnosisEnvironment {
    fn step(&self, worker_id: WorkerId, action: &Action) -> WorkResult {
        match action.name.to_lowercase().as_str() {
            "parseconfig" | "parse_config" | "config" => {
                self.handle_parse_config(worker_id, action)
            }
            "analyzelog" | "analyze_log" | "logs" | "log" => {
                self.handle_analyze_log(worker_id, action)
            }
            "traceerror" | "trace_error" | "trace" => self.handle_trace_error(worker_id, action),
            "applyfix" | "apply_fix" | "fix" => self.handle_apply_fix(worker_id, action),
            "continue" => WorkResult::env_success("Continuing..."),
            _ => WorkResult::unsupported(&action.name),
        }
    }

    fn reset(&self) {
        let mut state = self.state.write().unwrap();
        state.parsed_config = false;
        state.analyzed_logs = false;
        state.traced_error = None;
        state.completed.clear();
    }

    fn name(&self) -> &str {
        "InternalDiagnosisEnvironment"
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use swarm_engine_core::types::ActionParams;

    fn is_success(result: &WorkResult) -> bool {
        match result {
            WorkResult::Acted { action_result, .. } => action_result.success,
            WorkResult::Done { success, .. } => *success,
            _ => false,
        }
    }

    fn is_done(result: &WorkResult) -> bool {
        matches!(result, WorkResult::Done { .. })
    }

    fn action(name: &str, target: Option<&str>) -> Action {
        Action {
            name: name.into(),
            params: ActionParams {
                target: target.map(|s| s.into()),
                args: HashMap::new(),
                data: vec![],
            },
        }
    }

    fn action_with_args(name: &str, args: HashMap<String, String>) -> Action {
        Action {
            name: name.into(),
            params: ActionParams {
                target: None,
                args,
                data: vec![],
            },
        }
    }

    #[test]
    fn test_parse_config_all() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        let result = env.step(worker, &action("ParseConfig", None));
        assert!(is_success(&result));
    }

    #[test]
    fn test_parse_config_section() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        let result = env.step(worker, &action("ParseConfig", Some("routing")));
        assert!(is_success(&result));
    }

    #[test]
    fn test_analyze_log_requires_parse() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        // ParseConfig なしで AnalyzeLog はエラー
        let result = env.step(worker, &action("AnalyzeLog", None));
        assert!(!is_success(&result));
    }

    #[test]
    fn test_trace_error_requires_analyze() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        // ParseConfig
        env.step(worker, &action("ParseConfig", None));

        // AnalyzeLog なしで TraceError はエラー
        let result = env.step(worker, &action("TraceError", Some("SW-1001")));
        assert!(!is_success(&result));
    }

    #[test]
    fn test_full_diagnosis_flow() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        // 1. ParseConfig
        let result = env.step(worker, &action("ParseConfig", None));
        assert!(is_success(&result));
        assert!(!is_done(&result));

        // 2. AnalyzeLog
        let result = env.step(worker, &action("AnalyzeLog", None));
        assert!(is_success(&result));
        assert!(!is_done(&result));

        // 3. TraceError
        let result = env.step(worker, &action("TraceError", Some("SW-1001")));
        assert!(is_success(&result));
        assert!(!is_done(&result));

        // 4. ApplyFix
        let mut args = HashMap::new();
        args.insert("key".into(), "routing.targets".into());
        args.insert("value".into(), "[\"worker-1\", \"worker-2\"]".into());
        let result = env.step(worker, &action_with_args("ApplyFix", args));
        assert!(is_success(&result));
        assert!(is_done(&result));
    }

    #[test]
    fn test_wrong_error_code_fails() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        env.step(worker, &action("ParseConfig", None));
        env.step(worker, &action("AnalyzeLog", None));

        // 間違ったエラーコード
        let result = env.step(worker, &action("TraceError", Some("SW-9999")));
        assert!(!is_success(&result));
    }

    #[test]
    fn test_wrong_fix_key_fails() {
        let env = InternalDiagnosisEnvironment::routing_error_scenario();
        let worker = WorkerId(0);

        env.step(worker, &action("ParseConfig", None));
        env.step(worker, &action("AnalyzeLog", None));
        env.step(worker, &action("TraceError", Some("SW-1001")));

        // 間違ったキー
        let mut args = HashMap::new();
        args.insert("key".into(), "wrong.key".into());
        args.insert("value".into(), "some-value".into());
        let result = env.step(worker, &action_with_args("ApplyFix", args));
        assert!(!is_success(&result));
    }

    #[test]
    fn test_worker_pool_scenario() {
        let env = InternalDiagnosisEnvironment::worker_pool_scenario();
        let worker = WorkerId(0);

        env.step(worker, &action("ParseConfig", None));
        env.step(worker, &action("AnalyzeLog", None));

        let result = env.step(worker, &action("TraceError", Some("SW-2001")));
        assert!(is_success(&result));

        let mut args = HashMap::new();
        args.insert("key".into(), "workers.max_count".into());
        args.insert("value".into(), "16".into());
        let result = env.step(worker, &action_with_args("ApplyFix", args));
        assert!(is_success(&result));
        assert!(is_done(&result));
    }
}