warhorn 0.1.0

Protocol types for agent communication - signals between goblins
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
//! Operations sent from UI to Goblin orchestrator
//!
//! These are commands that the UI sends to control agent behavior.

use serde::{Deserialize, Serialize};

use crate::ids::*;
use crate::models::*;

/// Operations sent FROM Lair UI TO Goblin orchestrator
///
/// Each operation has an associated `SubmissionId` for correlation with events.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum Op {
    /// Configure or reconfigure the session
    ConfigureSession {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Session configuration
        config: SessionConfig,
    },

    /// Start a new task with user input
    UserInput {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// User's prompt/request
        prompt: String,
        /// Optional images attached to prompt
        #[serde(default)]
        images: Vec<ImageAttachment>,
        /// Optional context to include
        #[serde(default)]
        context: TaskContext,
        /// Resume from specific checkpoint
        #[serde(default)]
        checkpoint_id: Option<CheckpointId>,
    },

    /// Interrupt the current running task
    Interrupt {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Task to interrupt (None = current task)
        task_id: Option<TaskId>,
    },

    /// Approve or deny a tool execution request
    ExecApproval {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// The call being approved/denied
        call_id: CallId,
        /// Whether to approve
        approved: bool,
        /// Optional modification to command
        #[serde(default)]
        modified_command: Option<String>,
    },

    /// Approve or deny an MCP tool call
    McpApproval {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// The call being approved/denied
        call_id: CallId,
        /// Whether to approve
        approved: bool,
    },

    /// Request to spawn a new agent (typically from orchestrator)
    SpawnAgent {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Agent configuration
        config: AgentConfig,
        /// Parent agent (None = root orchestrator)
        parent_id: Option<AgentId>,
        /// Task to assign
        task: TaskAssignment,
    },

    /// Terminate a specific agent
    TerminateAgent {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Agent to terminate
        agent_id: AgentId,
        /// Reason for termination
        #[serde(default)]
        reason: Option<String>,
    },

    /// Send a message to a specific agent
    RouteMessage {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Target agent
        agent_id: AgentId,
        /// Message content
        content: String,
    },

    /// Save a checkpoint
    SaveCheckpoint {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Optional name for checkpoint
        #[serde(default)]
        name: Option<String>,
    },

    /// Restore from a checkpoint
    RestoreCheckpoint {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Checkpoint to restore
        checkpoint_id: CheckpointId,
    },

    /// List available checkpoints
    ListCheckpoints {
        /// Submission ID for correlation
        sub_id: SubmissionId,
    },

    /// Undo to last auto-checkpoint
    Undo {
        /// Submission ID for correlation
        sub_id: SubmissionId,
    },

    /// Toggle plan mode
    TogglePlanMode {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Enable or disable
        enabled: bool,
        /// Plan granularity
        #[serde(default)]
        granularity: PlanGranularity,
    },

    /// Update session settings
    UpdateSettings {
        /// Submission ID for correlation
        sub_id: SubmissionId,
        /// Settings to update
        settings: SessionSettings,
    },
}

impl Op {
    /// Get the submission ID for this operation
    pub fn sub_id(&self) -> &SubmissionId {
        match self {
            Op::ConfigureSession { sub_id, .. } => sub_id,
            Op::UserInput { sub_id, .. } => sub_id,
            Op::Interrupt { sub_id, .. } => sub_id,
            Op::ExecApproval { sub_id, .. } => sub_id,
            Op::McpApproval { sub_id, .. } => sub_id,
            Op::SpawnAgent { sub_id, .. } => sub_id,
            Op::TerminateAgent { sub_id, .. } => sub_id,
            Op::RouteMessage { sub_id, .. } => sub_id,
            Op::SaveCheckpoint { sub_id, .. } => sub_id,
            Op::RestoreCheckpoint { sub_id, .. } => sub_id,
            Op::ListCheckpoints { sub_id, .. } => sub_id,
            Op::Undo { sub_id, .. } => sub_id,
            Op::TogglePlanMode { sub_id, .. } => sub_id,
            Op::UpdateSettings { sub_id, .. } => sub_id,
        }
    }

    /// Create a UserInput operation
    pub fn user_input(prompt: impl Into<String>) -> Self {
        Op::UserInput {
            sub_id: SubmissionId::new(),
            prompt: prompt.into(),
            images: vec![],
            context: TaskContext::default(),
            checkpoint_id: None,
        }
    }

    /// Create an Interrupt operation
    pub fn interrupt() -> Self {
        Op::Interrupt {
            sub_id: SubmissionId::new(),
            task_id: None,
        }
    }

    /// Create an ExecApproval operation
    pub fn approve_exec(call_id: CallId) -> Self {
        Op::ExecApproval {
            sub_id: SubmissionId::new(),
            call_id,
            approved: true,
            modified_command: None,
        }
    }

    /// Create a deny ExecApproval operation
    pub fn deny_exec(call_id: CallId) -> Self {
        Op::ExecApproval {
            sub_id: SubmissionId::new(),
            call_id,
            approved: false,
            modified_command: None,
        }
    }
}

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

    // === UserInput Operation Tests ===

    #[test]
    fn test_user_input_serialization() {
        let op = Op::user_input("Hello, world!");
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("user_input"));
        assert!(json.contains("Hello, world!"));

        let parsed: Op = serde_json::from_str(&json).unwrap();
        match parsed {
            Op::UserInput { prompt, .. } => assert_eq!(prompt, "Hello, world!"),
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_user_input_has_unique_sub_id() {
        let op1 = Op::user_input("test1");
        let op2 = Op::user_input("test2");
        assert_ne!(op1.sub_id().as_str(), op2.sub_id().as_str());
    }

    #[test]
    fn test_user_input_defaults() {
        let op = Op::user_input("test");
        match op {
            Op::UserInput { images, context, checkpoint_id, .. } => {
                assert!(images.is_empty());
                assert!(context.files.is_empty());
                assert!(checkpoint_id.is_none());
            }
            _ => panic!("Wrong variant"),
        }
    }

    // === Interrupt Operation Tests ===

    #[test]
    fn test_interrupt_op() {
        let op = Op::interrupt();
        let sub_id = op.sub_id();
        assert!(!sub_id.as_str().is_empty());
        
        match op {
            Op::Interrupt { task_id, .. } => assert!(task_id.is_none()),
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_interrupt_serialization() {
        let op = Op::interrupt();
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("interrupt"));
        
        let parsed: Op = serde_json::from_str(&json).unwrap();
        match parsed {
            Op::Interrupt { task_id, .. } => assert!(task_id.is_none()),
            _ => panic!("Wrong variant"),
        }
    }

    // === ExecApproval Operation Tests ===

    #[test]
    fn test_approve_exec() {
        let call_id = CallId::new();
        let op = Op::approve_exec(call_id);
        
        match op {
            Op::ExecApproval { approved, modified_command, call_id: cid, .. } => {
                assert!(approved);
                assert!(modified_command.is_none());
                assert_eq!(cid, call_id);
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_deny_exec() {
        let call_id = CallId::new();
        let op = Op::deny_exec(call_id);
        
        match op {
            Op::ExecApproval { approved, .. } => {
                assert!(!approved);
            }
            _ => panic!("Wrong variant"),
        }
    }

    #[test]
    fn test_exec_approval_serialization() {
        let call_id = CallId::new();
        let op = Op::approve_exec(call_id);
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("exec_approval"));
        assert!(json.contains("approved"));
    }

    // === ConfigureSession Operation Tests ===

    #[test]
    fn test_configure_session() {
        let config = SessionConfig::default();
        let op = Op::ConfigureSession {
            sub_id: SubmissionId::new(),
            config: config.clone(),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("configure_session"));
        
        let parsed: Op = serde_json::from_str(&json).unwrap();
        match parsed {
            Op::ConfigureSession { config: c, .. } => {
                assert_eq!(c.max_parallel_agents, config.max_parallel_agents);
            }
            _ => panic!("Wrong variant"),
        }
    }

    // === SpawnAgent Operation Tests ===

    #[test]
    fn test_spawn_agent() {
        let task = TaskAssignment {
            task_id: TaskId::new(),
            description: "Test task".into(),
            deliverables: vec!["result".into()],
            dependencies: vec![],
            context: TaskContext::default(),
        };
        
        let op = Op::SpawnAgent {
            sub_id: SubmissionId::new(),
            config: AgentConfig::default(),
            parent_id: None,
            task,
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("spawn_agent"));
        assert!(json.contains("Test task"));
    }

    // === TerminateAgent Operation Tests ===

    #[test]
    fn test_terminate_agent() {
        let agent_id = AgentId::new();
        let op = Op::TerminateAgent {
            sub_id: SubmissionId::new(),
            agent_id,
            reason: Some("Test termination".into()),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("terminate_agent"));
        assert!(json.contains("Test termination"));
    }

    // === Checkpoint Operations Tests ===

    #[test]
    fn test_save_checkpoint() {
        let op = Op::SaveCheckpoint {
            sub_id: SubmissionId::new(),
            name: Some("manual checkpoint".into()),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("save_checkpoint"));
        assert!(json.contains("manual checkpoint"));
    }

    #[test]
    fn test_restore_checkpoint() {
        let checkpoint_id = CheckpointId::new();
        let op = Op::RestoreCheckpoint {
            sub_id: SubmissionId::new(),
            checkpoint_id,
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("restore_checkpoint"));
    }

    #[test]
    fn test_list_checkpoints() {
        let op = Op::ListCheckpoints {
            sub_id: SubmissionId::new(),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("list_checkpoints"));
    }

    #[test]
    fn test_undo() {
        let op = Op::Undo {
            sub_id: SubmissionId::new(),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("undo"));
    }

    // === Plan Mode Operations Tests ===

    #[test]
    fn test_toggle_plan_mode() {
        let op = Op::TogglePlanMode {
            sub_id: SubmissionId::new(),
            enabled: true,
            granularity: PlanGranularity::Detailed,
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("toggle_plan_mode"));
        assert!(json.contains("detailed"));
    }

    // === Update Settings Tests ===

    #[test]
    fn test_update_settings() {
        let settings = SessionSettings {
            show_rate_limit: true,
            subagent_concurrency: Some(4),
            plan_granularity: PlanGranularity::Auto,
        };
        
        let op = Op::UpdateSettings {
            sub_id: SubmissionId::new(),
            settings,
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("update_settings"));
        assert!(json.contains("show_rate_limit"));
    }

    // === MCP Approval Tests ===

    #[test]
    fn test_mcp_approval() {
        let call_id = CallId::new();
        let op = Op::McpApproval {
            sub_id: SubmissionId::new(),
            call_id,
            approved: true,
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("mcp_approval"));
    }

    // === Route Message Tests ===

    #[test]
    fn test_route_message() {
        let agent_id = AgentId::new();
        let op = Op::RouteMessage {
            sub_id: SubmissionId::new(),
            agent_id,
            content: "Hello agent!".into(),
        };
        
        let json = serde_json::to_string(&op).unwrap();
        assert!(json.contains("route_message"));
        assert!(json.contains("Hello agent!"));
    }

    // === Sub ID Extraction Tests ===

    #[test]
    fn test_sub_id_extraction_all_variants() {
        // Test that sub_id() works for all variants
        let ops = vec![
            Op::user_input("test"),
            Op::interrupt(),
            Op::approve_exec(CallId::new()),
            Op::ConfigureSession {
                sub_id: SubmissionId::new(),
                config: SessionConfig::default(),
            },
            Op::SaveCheckpoint {
                sub_id: SubmissionId::new(),
                name: None,
            },
            Op::ListCheckpoints {
                sub_id: SubmissionId::new(),
            },
            Op::Undo {
                sub_id: SubmissionId::new(),
            },
            Op::TogglePlanMode {
                sub_id: SubmissionId::new(),
                enabled: true,
                granularity: PlanGranularity::Auto,
            },
        ];
        
        for op in ops {
            let sub_id = op.sub_id();
            assert!(!sub_id.as_str().is_empty(), "sub_id should not be empty");
        }
    }
}