ralph-proto 2.7.0

Shared types, error definitions, and traits for Ralph Orchestrator
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
//! JSON-RPC protocol types for Ralph's stdin/stdout communication.
//!
//! This module defines the wire format for Ralph's JSON-lines protocol,
//! enabling IPC between the orchestration loop and frontends (TUI, IDE
//! integrations, custom UIs). The protocol follows pi's `--mode rpc`
//! conventions but is tailored for Ralph's multi-hat, iteration-based model.
//!
//! ## Protocol Overview
//!
//! - **Transport**: Newline-delimited JSON over stdin (commands) and stdout (events)
//! - **Commands**: Sent to Ralph via stdin to control the loop
//! - **Events**: Emitted by Ralph via stdout to report state changes
//! - **Responses**: Command replies with success/failure and optional data

use serde::{Deserialize, Serialize};
use serde_json::Value;

// ============================================================================
// Commands (stdin → Ralph)
// ============================================================================

/// Commands sent to Ralph via stdin.
///
/// Each command is a single JSON line. Commands with an `id` field receive
/// correlated responses.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RpcCommand {
    /// Start the loop with a prompt (must be sent before loop starts).
    Prompt {
        #[serde(default)]
        id: Option<String>,
        /// The prompt text to execute.
        prompt: String,
        /// Optional backend override.
        #[serde(default)]
        backend: Option<String>,
        /// Optional max iterations override.
        #[serde(default)]
        max_iterations: Option<u32>,
    },

    /// Inject guidance that affects the current or next iteration.
    /// Equivalent to the TUI's guidance input.
    Guidance {
        #[serde(default)]
        id: Option<String>,
        /// The guidance message to inject.
        message: String,
    },

    /// Steer the agent immediately during the current iteration.
    /// The guidance is injected into the running context.
    Steer {
        #[serde(default)]
        id: Option<String>,
        /// The steering message.
        message: String,
    },

    /// Queue a follow-up message for the next iteration.
    FollowUp {
        #[serde(default)]
        id: Option<String>,
        /// The follow-up message.
        message: String,
    },

    /// Request immediate termination of the loop.
    Abort {
        #[serde(default)]
        id: Option<String>,
        /// Optional reason for the abort.
        #[serde(default)]
        reason: Option<String>,
    },

    /// Request the current loop state snapshot.
    GetState {
        #[serde(default)]
        id: Option<String>,
    },

    /// Request iteration history and metadata.
    GetIterations {
        #[serde(default)]
        id: Option<String>,
        /// If true, include full iteration content. Default: false (metadata only).
        #[serde(default)]
        include_content: bool,
    },

    /// Force a hat change for the next iteration.
    SetHat {
        #[serde(default)]
        id: Option<String>,
        /// The hat ID to switch to.
        hat: String,
    },

    /// Response to an extension UI prompt (future support).
    ExtensionUiResponse {
        #[serde(default)]
        id: Option<String>,
        /// The extension request ID being responded to.
        request_id: String,
        /// The user's response data.
        response: Value,
    },
}

impl RpcCommand {
    /// Returns the command's correlation ID if present.
    pub fn id(&self) -> Option<&str> {
        match self {
            RpcCommand::Prompt { id, .. } => id.as_deref(),
            RpcCommand::Guidance { id, .. } => id.as_deref(),
            RpcCommand::Steer { id, .. } => id.as_deref(),
            RpcCommand::FollowUp { id, .. } => id.as_deref(),
            RpcCommand::Abort { id, .. } => id.as_deref(),
            RpcCommand::GetState { id } => id.as_deref(),
            RpcCommand::GetIterations { id, .. } => id.as_deref(),
            RpcCommand::SetHat { id, .. } => id.as_deref(),
            RpcCommand::ExtensionUiResponse { id, .. } => id.as_deref(),
        }
    }

    /// Returns the command type name (for response correlation).
    pub fn command_type(&self) -> &'static str {
        match self {
            RpcCommand::Prompt { .. } => "prompt",
            RpcCommand::Guidance { .. } => "guidance",
            RpcCommand::Steer { .. } => "steer",
            RpcCommand::FollowUp { .. } => "follow_up",
            RpcCommand::Abort { .. } => "abort",
            RpcCommand::GetState { .. } => "get_state",
            RpcCommand::GetIterations { .. } => "get_iterations",
            RpcCommand::SetHat { .. } => "set_hat",
            RpcCommand::ExtensionUiResponse { .. } => "extension_ui_response",
        }
    }
}

// ============================================================================
// Events (Ralph → stdout)
// ============================================================================

/// Events emitted by Ralph via stdout.
///
/// Each event is a single JSON line. Events are emitted in real-time as the
/// orchestration loop progresses.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum RpcEvent {
    /// The orchestration loop has started.
    LoopStarted {
        /// The prompt being executed.
        prompt: String,
        /// Maximum iterations configured.
        max_iterations: Option<u32>,
        /// Backend being used.
        backend: String,
        /// Unix timestamp (milliseconds) when the loop started.
        started_at: u64,
    },

    /// A new iteration is beginning.
    IterationStart {
        /// Iteration number (1-indexed).
        iteration: u32,
        /// Maximum iterations configured.
        max_iterations: Option<u32>,
        /// The hat being worn for this iteration.
        hat: String,
        /// Hat display name (with emoji).
        hat_display: String,
        /// Backend being used.
        backend: String,
        /// Unix timestamp (milliseconds).
        started_at: u64,
    },

    /// An iteration has completed.
    IterationEnd {
        /// Iteration number.
        iteration: u32,
        /// Duration in milliseconds.
        duration_ms: u64,
        /// Estimated cost in USD.
        cost_usd: f64,
        /// Input tokens used.
        input_tokens: u64,
        /// Output tokens used.
        output_tokens: u64,
        /// Cache read tokens.
        cache_read_tokens: u64,
        /// Cache write tokens.
        cache_write_tokens: u64,
        /// Whether this iteration triggered LOOP_COMPLETE.
        loop_complete_triggered: bool,
    },

    /// Streaming text delta from the agent.
    TextDelta {
        /// Iteration number.
        iteration: u32,
        /// The text chunk.
        delta: String,
    },

    /// A tool invocation is starting.
    ToolCallStart {
        /// Iteration number.
        iteration: u32,
        /// Tool name (e.g., "Bash", "Read", "Grep").
        tool_name: String,
        /// Unique tool call ID.
        tool_call_id: String,
        /// Tool input parameters.
        input: Value,
    },

    /// A tool invocation has completed.
    ToolCallEnd {
        /// Iteration number.
        iteration: u32,
        /// Tool call ID (matches ToolCallStart).
        tool_call_id: String,
        /// Tool output (may be truncated for large outputs).
        output: String,
        /// Whether this was an error result.
        is_error: bool,
        /// Duration in milliseconds.
        duration_ms: u64,
    },

    /// An error occurred during execution.
    Error {
        /// Iteration number (0 if loop-level error).
        iteration: u32,
        /// Error code (e.g., "TIMEOUT", "API_ERROR", "PARSE_ERROR").
        code: String,
        /// Human-readable error message.
        message: String,
        /// Whether the error is recoverable.
        recoverable: bool,
    },

    /// The hat has changed.
    HatChanged {
        /// Iteration number where the change takes effect.
        iteration: u32,
        /// Previous hat ID.
        from_hat: String,
        /// New hat ID.
        to_hat: String,
        /// New hat display name.
        to_hat_display: String,
        /// Reason for the change.
        reason: String,
    },

    /// Task status has changed.
    TaskStatusChanged {
        /// Task ID.
        task_id: String,
        /// Previous status.
        from_status: String,
        /// New status.
        to_status: String,
        /// Task title.
        title: String,
    },

    /// Current task counts have been updated.
    TaskCountsUpdated {
        /// Total number of tasks.
        total: usize,
        /// Number of open tasks.
        open: usize,
        /// Number of closed tasks.
        closed: usize,
        /// Number of ready (unblocked) tasks.
        ready: usize,
    },

    /// Acknowledgment that guidance was received.
    GuidanceAck {
        /// The guidance message that was received.
        message: String,
        /// Whether the guidance will be applied to the current or next iteration.
        applies_to: GuidanceTarget,
    },

    /// The orchestration loop has terminated.
    LoopTerminated {
        /// Reason for termination.
        reason: TerminationReason,
        /// Total iterations completed.
        total_iterations: u32,
        /// Total duration in milliseconds.
        duration_ms: u64,
        /// Total estimated cost in USD.
        total_cost_usd: f64,
        /// Unix timestamp (milliseconds).
        terminated_at: u64,
    },

    /// Response to a command.
    Response {
        /// The command type this responds to.
        command: String,
        /// Correlation ID from the command (if provided).
        #[serde(skip_serializing_if = "Option::is_none")]
        id: Option<String>,
        /// Whether the command succeeded.
        success: bool,
        /// Response data (command-specific).
        #[serde(skip_serializing_if = "Option::is_none")]
        data: Option<Value>,
        /// Error message if success is false.
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<String>,
    },

    /// A generic orchestration event from the EventBus.
    /// Maps ralph_proto::Event topics to RPC for observability.
    OrchestrationEvent {
        /// Event topic (e.g., "build.task", "build.done", "loop.terminate").
        topic: String,
        /// Event payload.
        payload: String,
        /// Source hat ID (if any).
        #[serde(skip_serializing_if = "Option::is_none")]
        source: Option<String>,
        /// Target hat ID (if any).
        #[serde(skip_serializing_if = "Option::is_none")]
        target: Option<String>,
    },
}

impl RpcEvent {
    /// Creates a successful response event.
    pub fn success_response(command: &str, id: Option<String>, data: Option<Value>) -> Self {
        RpcEvent::Response {
            command: command.to_string(),
            id,
            success: true,
            data,
            error: None,
        }
    }

    /// Creates a failed response event.
    pub fn error_response(command: &str, id: Option<String>, error: impl Into<String>) -> Self {
        RpcEvent::Response {
            command: command.to_string(),
            id,
            success: false,
            data: None,
            error: Some(error.into()),
        }
    }
}

// ============================================================================
// Supporting types
// ============================================================================

/// Target for guidance application.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum GuidanceTarget {
    /// Guidance will be applied to the current iteration (steer).
    Current,
    /// Guidance will be applied to the next iteration (follow-up).
    Next,
}

/// Reason for loop termination.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum TerminationReason {
    /// Loop completed successfully (LOOP_COMPLETE detected).
    Completed,
    /// Maximum iterations reached.
    MaxIterations,
    /// User requested abort.
    Interrupted,
    /// An unrecoverable error occurred.
    Error,
    /// All tasks completed.
    AllTasksClosed,
    /// Backpressure blocked too many times.
    BackpressureLimit,
}

/// Snapshot of loop state (returned by get_state command).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RpcState {
    /// Current iteration number.
    pub iteration: u32,
    /// Maximum iterations configured.
    pub max_iterations: Option<u32>,
    /// Current hat ID.
    pub hat: String,
    /// Current hat display name.
    pub hat_display: String,
    /// Backend being used.
    pub backend: String,
    /// Whether the loop has completed.
    pub completed: bool,
    /// Loop start time (Unix ms).
    pub started_at: u64,
    /// Current iteration start time (Unix ms).
    pub iteration_started_at: Option<u64>,
    /// Task counts.
    pub task_counts: RpcTaskCounts,
    /// Active task (if any).
    pub active_task: Option<RpcTaskSummary>,
    /// Total cost so far.
    pub total_cost_usd: f64,
}

/// Task counts for RPC state.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct RpcTaskCounts {
    pub total: usize,
    pub open: usize,
    pub closed: usize,
    pub ready: usize,
}

/// Task summary for RPC state.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RpcTaskSummary {
    pub id: String,
    pub title: String,
    pub status: String,
}

/// Iteration metadata (returned by get_iterations command).
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct RpcIterationInfo {
    /// Iteration number.
    pub iteration: u32,
    /// Hat used for this iteration.
    pub hat: String,
    /// Backend used.
    pub backend: String,
    /// Duration in milliseconds.
    pub duration_ms: u64,
    /// Cost in USD.
    pub cost_usd: f64,
    /// Whether LOOP_COMPLETE was triggered.
    pub loop_complete_triggered: bool,
    /// Full content (only if requested).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<String>,
}

// ============================================================================
// Parsing and serialization helpers
// ============================================================================

/// Parses a JSON line into an RpcCommand.
///
/// Returns an error with a descriptive message if parsing fails.
pub fn parse_command(line: &str) -> Result<RpcCommand, String> {
    let trimmed = line.trim();
    if trimmed.is_empty() {
        return Err("empty line".to_string());
    }
    serde_json::from_str(trimmed).map_err(|e| format!("JSON parse error: {e}"))
}

/// Serializes an RpcEvent to a JSON line (no trailing newline).
pub fn emit_event(event: &RpcEvent) -> String {
    // Unwrap is safe: RpcEvent is always serializable
    serde_json::to_string(event).expect("RpcEvent serialization failed")
}

/// Serializes an RpcEvent to a JSON line with trailing newline.
pub fn emit_event_line(event: &RpcEvent) -> String {
    let mut line = emit_event(event);
    line.push('\n');
    line
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    // =========================================================================
    // Command round-trip tests
    // =========================================================================

    #[test]
    fn test_prompt_command_roundtrip() {
        let cmd = RpcCommand::Prompt {
            id: Some("req-1".to_string()),
            prompt: "implement feature X".to_string(),
            backend: Some("claude".to_string()),
            max_iterations: Some(5),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_guidance_command_roundtrip() {
        let cmd = RpcCommand::Guidance {
            id: None,
            message: "focus on tests".to_string(),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_steer_command_roundtrip() {
        let cmd = RpcCommand::Steer {
            id: Some("steer-1".to_string()),
            message: "use async instead".to_string(),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_follow_up_command_roundtrip() {
        let cmd = RpcCommand::FollowUp {
            id: None,
            message: "now run the tests".to_string(),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_abort_command_roundtrip() {
        let cmd = RpcCommand::Abort {
            id: Some("abort-1".to_string()),
            reason: Some("user cancelled".to_string()),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_get_state_command_roundtrip() {
        let cmd = RpcCommand::GetState {
            id: Some("state-1".to_string()),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_get_iterations_command_roundtrip() {
        let cmd = RpcCommand::GetIterations {
            id: Some("iters-1".to_string()),
            include_content: true,
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_set_hat_command_roundtrip() {
        let cmd = RpcCommand::SetHat {
            id: None,
            hat: "confessor".to_string(),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    #[test]
    fn test_extension_ui_response_command_roundtrip() {
        let cmd = RpcCommand::ExtensionUiResponse {
            id: Some("ext-1".to_string()),
            request_id: "ui-req-123".to_string(),
            response: json!({"selected": "option-a"}),
        };
        let json = serde_json::to_string(&cmd).unwrap();
        let parsed: RpcCommand = serde_json::from_str(&json).unwrap();
        assert_eq!(cmd, parsed);
    }

    // =========================================================================
    // Event round-trip tests
    // =========================================================================

    #[test]
    fn test_loop_started_event_roundtrip() {
        let event = RpcEvent::LoopStarted {
            prompt: "test prompt".to_string(),
            max_iterations: Some(10),
            backend: "claude".to_string(),
            started_at: 1_700_000_000_000,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_iteration_start_event_roundtrip() {
        let event = RpcEvent::IterationStart {
            iteration: 3,
            max_iterations: Some(10),
            hat: "builder".to_string(),
            hat_display: "🔨Builder".to_string(),
            backend: "claude".to_string(),
            started_at: 1_700_000_001_000,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_iteration_end_event_roundtrip() {
        let event = RpcEvent::IterationEnd {
            iteration: 3,
            duration_ms: 5432,
            cost_usd: 0.0054,
            input_tokens: 8000,
            output_tokens: 500,
            cache_read_tokens: 7500,
            cache_write_tokens: 100,
            loop_complete_triggered: false,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_text_delta_event_roundtrip() {
        let event = RpcEvent::TextDelta {
            iteration: 2,
            delta: "Hello, world!".to_string(),
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_tool_call_start_event_roundtrip() {
        let event = RpcEvent::ToolCallStart {
            iteration: 1,
            tool_name: "Bash".to_string(),
            tool_call_id: "toolu_123".to_string(),
            input: json!({"command": "ls -la"}),
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_tool_call_end_event_roundtrip() {
        let event = RpcEvent::ToolCallEnd {
            iteration: 1,
            tool_call_id: "toolu_123".to_string(),
            output: "file1.rs\nfile2.rs".to_string(),
            is_error: false,
            duration_ms: 150,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_error_event_roundtrip() {
        let event = RpcEvent::Error {
            iteration: 2,
            code: "TIMEOUT".to_string(),
            message: "API request timed out".to_string(),
            recoverable: true,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_hat_changed_event_roundtrip() {
        let event = RpcEvent::HatChanged {
            iteration: 4,
            from_hat: "builder".to_string(),
            to_hat: "confessor".to_string(),
            to_hat_display: "🙏Confessor".to_string(),
            reason: "build.done received".to_string(),
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_task_status_changed_event_roundtrip() {
        let event = RpcEvent::TaskStatusChanged {
            task_id: "task-123".to_string(),
            from_status: "open".to_string(),
            to_status: "closed".to_string(),
            title: "Implement feature X".to_string(),
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_task_counts_updated_event_roundtrip() {
        let event = RpcEvent::TaskCountsUpdated {
            total: 10,
            open: 3,
            closed: 7,
            ready: 2,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_guidance_ack_event_roundtrip() {
        let event = RpcEvent::GuidanceAck {
            message: "focus on tests".to_string(),
            applies_to: GuidanceTarget::Next,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_loop_terminated_event_roundtrip() {
        let event = RpcEvent::LoopTerminated {
            reason: TerminationReason::Completed,
            total_iterations: 5,
            duration_ms: 120_000,
            total_cost_usd: 0.25,
            terminated_at: 1_700_000_120_000,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_response_event_success_roundtrip() {
        let event = RpcEvent::Response {
            command: "get_state".to_string(),
            id: Some("req-42".to_string()),
            success: true,
            data: Some(json!({"iteration": 3})),
            error: None,
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    #[test]
    fn test_response_event_error_roundtrip() {
        let event = RpcEvent::Response {
            command: "prompt".to_string(),
            id: Some("req-43".to_string()),
            success: false,
            data: None,
            error: Some("loop already running".to_string()),
        };
        let json = serde_json::to_string(&event).unwrap();
        let parsed: RpcEvent = serde_json::from_str(&json).unwrap();
        assert_eq!(event, parsed);
    }

    // =========================================================================
    // Termination reason tests
    // =========================================================================

    #[test]
    fn test_termination_reason_variants() {
        let reasons = [
            TerminationReason::Completed,
            TerminationReason::MaxIterations,
            TerminationReason::Interrupted,
            TerminationReason::Error,
            TerminationReason::AllTasksClosed,
            TerminationReason::BackpressureLimit,
        ];
        for reason in reasons {
            let json = serde_json::to_string(&reason).unwrap();
            let parsed: TerminationReason = serde_json::from_str(&json).unwrap();
            assert_eq!(reason, parsed);
        }
    }

    // =========================================================================
    // Parsing helper tests
    // =========================================================================

    #[test]
    fn test_parse_command_valid() {
        let line = r#"{"type": "get_state", "id": "test-1"}"#;
        let cmd = parse_command(line).unwrap();
        assert!(matches!(cmd, RpcCommand::GetState { id: Some(ref i) } if i == "test-1"));
    }

    #[test]
    fn test_parse_command_empty() {
        assert!(parse_command("").is_err());
        assert!(parse_command("   ").is_err());
    }

    #[test]
    fn test_parse_command_invalid_json() {
        assert!(parse_command("{not valid}").is_err());
    }

    #[test]
    fn test_parse_command_unknown_type() {
        let line = r#"{"type": "unknown_command"}"#;
        assert!(parse_command(line).is_err());
    }

    #[test]
    fn test_emit_event() {
        let event = RpcEvent::TextDelta {
            iteration: 1,
            delta: "hello".to_string(),
        };
        let json = emit_event(&event);
        assert!(!json.ends_with('\n'));
        assert!(json.contains(r#""type":"text_delta""#));
    }

    #[test]
    fn test_emit_event_line() {
        let event = RpcEvent::TextDelta {
            iteration: 1,
            delta: "hello".to_string(),
        };
        let line = emit_event_line(&event);
        assert!(line.ends_with('\n'));
        assert_eq!(line.matches('\n').count(), 1);
    }

    // =========================================================================
    // Helper method tests
    // =========================================================================

    #[test]
    fn test_command_id() {
        let cmd = RpcCommand::GetState {
            id: Some("req-1".to_string()),
        };
        assert_eq!(cmd.id(), Some("req-1"));

        let cmd = RpcCommand::Abort {
            id: None,
            reason: None,
        };
        assert_eq!(cmd.id(), None);
    }

    #[test]
    fn test_command_type() {
        assert_eq!(
            RpcCommand::Prompt {
                id: None,
                prompt: "test".into(),
                backend: None,
                max_iterations: None
            }
            .command_type(),
            "prompt"
        );
        assert_eq!(
            RpcCommand::GetState { id: None }.command_type(),
            "get_state"
        );
        assert_eq!(
            RpcCommand::Abort {
                id: None,
                reason: None
            }
            .command_type(),
            "abort"
        );
    }

    #[test]
    fn test_success_response() {
        let event = RpcEvent::success_response(
            "get_state",
            Some("req-1".into()),
            Some(json!({"ok": true})),
        );
        match event {
            RpcEvent::Response {
                command,
                id,
                success,
                data,
                error,
            } => {
                assert_eq!(command, "get_state");
                assert_eq!(id, Some("req-1".to_string()));
                assert!(success);
                assert!(data.is_some());
                assert!(error.is_none());
            }
            _ => panic!("Expected Response event"),
        }
    }

    #[test]
    fn test_error_response() {
        let event = RpcEvent::error_response("prompt", None, "loop already running");
        match event {
            RpcEvent::Response {
                command,
                id,
                success,
                data,
                error,
            } => {
                assert_eq!(command, "prompt");
                assert!(id.is_none());
                assert!(!success);
                assert!(data.is_none());
                assert_eq!(error, Some("loop already running".to_string()));
            }
            _ => panic!("Expected Response event"),
        }
    }

    // =========================================================================
    // State types tests
    // =========================================================================

    #[test]
    fn test_rpc_state_roundtrip() {
        let state = RpcState {
            iteration: 3,
            max_iterations: Some(10),
            hat: "builder".to_string(),
            hat_display: "🔨Builder".to_string(),
            backend: "claude".to_string(),
            completed: false,
            started_at: 1_700_000_000_000,
            iteration_started_at: Some(1_700_000_005_000),
            task_counts: RpcTaskCounts {
                total: 5,
                open: 2,
                closed: 3,
                ready: 1,
            },
            active_task: Some(RpcTaskSummary {
                id: "task-123".to_string(),
                title: "Fix bug".to_string(),
                status: "running".to_string(),
            }),
            total_cost_usd: 0.15,
        };
        let json = serde_json::to_string(&state).unwrap();
        let parsed: RpcState = serde_json::from_str(&json).unwrap();
        assert_eq!(state, parsed);
    }

    #[test]
    fn test_rpc_iteration_info_roundtrip() {
        let info = RpcIterationInfo {
            iteration: 2,
            hat: "builder".to_string(),
            backend: "claude".to_string(),
            duration_ms: 5000,
            cost_usd: 0.05,
            loop_complete_triggered: false,
            content: Some("iteration content here".to_string()),
        };
        let json = serde_json::to_string(&info).unwrap();
        let parsed: RpcIterationInfo = serde_json::from_str(&json).unwrap();
        assert_eq!(info, parsed);
    }

    // =========================================================================
    // Pi protocol alignment tests (naming conventions)
    // =========================================================================

    #[test]
    fn test_pi_aligned_naming() {
        // Verify our event types follow pi conventions
        let text_delta = RpcEvent::TextDelta {
            iteration: 1,
            delta: "test".to_string(),
        };
        let json = serde_json::to_string(&text_delta).unwrap();
        assert!(json.contains(r#""type":"text_delta""#));

        let tool_start = RpcEvent::ToolCallStart {
            iteration: 1,
            tool_name: "Bash".to_string(),
            tool_call_id: "id".to_string(),
            input: json!({}),
        };
        let json = serde_json::to_string(&tool_start).unwrap();
        assert!(json.contains(r#""type":"tool_call_start""#));

        let tool_end = RpcEvent::ToolCallEnd {
            iteration: 1,
            tool_call_id: "id".to_string(),
            output: String::new(),
            is_error: false,
            duration_ms: 0,
        };
        let json = serde_json::to_string(&tool_end).unwrap();
        assert!(json.contains(r#""type":"tool_call_end""#));
    }
}