ironclaw 0.4.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! Submission types for the turn-based agent loop.
//!
//! Submissions are the different types of input the agent can receive
//! and process as part of the turn-based development loop.

use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Parses user input into Submission types.
pub struct SubmissionParser;

impl SubmissionParser {
    /// Parse message content into a Submission.
    pub fn parse(content: &str) -> Submission {
        let trimmed = content.trim();
        let lower = trimmed.to_lowercase();

        // Control commands (exact match or prefix)
        if lower == "/undo" {
            return Submission::Undo;
        }
        if lower == "/redo" {
            return Submission::Redo;
        }
        if lower == "/interrupt" || lower == "/stop" {
            return Submission::Interrupt;
        }
        if lower == "/compact" {
            return Submission::Compact;
        }
        if lower == "/clear" {
            return Submission::Clear;
        }
        if lower == "/heartbeat" {
            return Submission::Heartbeat;
        }
        if lower == "/summarize" || lower == "/summary" {
            return Submission::Summarize;
        }
        if lower == "/suggest" {
            return Submission::Suggest;
        }
        if lower == "/thread new" || lower == "/new" {
            return Submission::NewThread;
        }
        // System commands (bypass thread-state checks)
        if lower == "/help" || lower == "/?" {
            return Submission::SystemCommand {
                command: "help".to_string(),
                args: vec![],
            };
        }
        if lower == "/version" {
            return Submission::SystemCommand {
                command: "version".to_string(),
                args: vec![],
            };
        }
        if lower == "/tools" {
            return Submission::SystemCommand {
                command: "tools".to_string(),
                args: vec![],
            };
        }
        if lower == "/ping" {
            return Submission::SystemCommand {
                command: "ping".to_string(),
                args: vec![],
            };
        }
        if lower == "/debug" {
            return Submission::SystemCommand {
                command: "debug".to_string(),
                args: vec![],
            };
        }
        if lower.starts_with("/model") {
            let args: Vec<String> = trimmed
                .split_whitespace()
                .skip(1)
                .map(|s| s.to_string())
                .collect();
            return Submission::SystemCommand {
                command: "model".to_string(),
                args,
            };
        }

        if lower == "/quit" || lower == "/exit" || lower == "/shutdown" {
            return Submission::Quit;
        }

        // /thread <uuid> - switch thread
        if let Some(rest) = lower.strip_prefix("/thread ") {
            let rest = rest.trim();
            if rest != "new"
                && let Ok(id) = Uuid::parse_str(rest)
            {
                return Submission::SwitchThread { thread_id: id };
            }
        }

        // /resume <uuid> - resume from checkpoint
        if let Some(rest) = lower.strip_prefix("/resume ")
            && let Ok(id) = Uuid::parse_str(rest.trim())
        {
            return Submission::Resume { checkpoint_id: id };
        }

        // Try structured JSON approval (from web gateway's /api/chat/approval endpoint)
        if trimmed.starts_with('{')
            && let Ok(submission) = serde_json::from_str::<Submission>(trimmed)
            && matches!(submission, Submission::ExecApproval { .. })
        {
            return submission;
        }

        // Approval responses (simple yes/no/always for pending approvals)
        // These are short enough to check explicitly
        match lower.as_str() {
            "yes" | "y" | "approve" | "ok" => {
                return Submission::ApprovalResponse {
                    approved: true,
                    always: false,
                };
            }
            "always" | "yes always" | "approve always" => {
                return Submission::ApprovalResponse {
                    approved: true,
                    always: true,
                };
            }
            "no" | "n" | "deny" | "reject" | "cancel" => {
                return Submission::ApprovalResponse {
                    approved: false,
                    always: false,
                };
            }
            _ => {}
        }

        // Default: user input
        Submission::UserInput {
            content: content.to_string(),
        }
    }
}

/// A submission to the agent.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Submission {
    /// User text input (starts a new turn).
    UserInput {
        /// The user's message content.
        content: String,
    },

    /// Response to an execution approval request (with explicit request ID).
    ExecApproval {
        /// ID of the approval request being responded to.
        request_id: Uuid,
        /// Whether the execution was approved.
        approved: bool,
        /// If true, auto-approve this tool for the rest of the session.
        always: bool,
    },

    /// Simple approval response (yes/no/always) for the current pending approval.
    ApprovalResponse {
        /// Whether the execution was approved.
        approved: bool,
        /// If true, auto-approve this tool for the rest of the session.
        always: bool,
    },

    /// Interrupt the current turn.
    Interrupt,

    /// Request context compaction.
    Compact,

    /// Undo the last turn.
    Undo,

    /// Redo a previously undone turn (if available).
    Redo,

    /// Resume from a specific checkpoint.
    Resume {
        /// ID of the checkpoint to resume from.
        checkpoint_id: Uuid,
    },

    /// Clear the current thread and start fresh.
    Clear,

    /// Switch to a different thread.
    SwitchThread {
        /// ID of the thread to switch to.
        thread_id: Uuid,
    },

    /// Create a new thread.
    NewThread,

    /// Trigger a manual heartbeat check.
    Heartbeat,

    /// Summarize the current thread.
    Summarize,

    /// Suggest next steps based on the current thread.
    Suggest,

    /// Quit the agent. Bypasses thread-state checks.
    Quit,

    /// System command (help, model, version, tools, ping, debug).
    /// Bypasses thread-state checks and safety validation.
    SystemCommand {
        /// The command name (e.g. "help", "model", "version").
        command: String,
        /// Arguments to the command.
        args: Vec<String>,
    },
}

impl Submission {
    /// Create a user input submission.
    pub fn user_input(content: impl Into<String>) -> Self {
        Self::UserInput {
            content: content.into(),
        }
    }

    /// Create an approval submission.
    pub fn approval(request_id: Uuid, approved: bool) -> Self {
        Self::ExecApproval {
            request_id,
            approved,
            always: false,
        }
    }

    /// Create an "always approve" submission.
    pub fn always_approve(request_id: Uuid) -> Self {
        Self::ExecApproval {
            request_id,
            approved: true,
            always: true,
        }
    }

    /// Create an interrupt submission.
    pub fn interrupt() -> Self {
        Self::Interrupt
    }

    /// Create a compact submission.
    pub fn compact() -> Self {
        Self::Compact
    }

    /// Create an undo submission.
    pub fn undo() -> Self {
        Self::Undo
    }

    /// Create a redo submission.
    pub fn redo() -> Self {
        Self::Redo
    }

    /// Check if this submission starts a new turn.
    pub fn starts_turn(&self) -> bool {
        matches!(self, Self::UserInput { .. })
    }

    /// Check if this submission is a control command.
    pub fn is_control(&self) -> bool {
        matches!(
            self,
            Self::Interrupt
                | Self::Compact
                | Self::Undo
                | Self::Redo
                | Self::Clear
                | Self::NewThread
                | Self::Heartbeat
                | Self::Summarize
                | Self::Suggest
                | Self::SystemCommand { .. }
        )
    }
}

/// Result of processing a submission.
#[derive(Debug, Clone)]
pub enum SubmissionResult {
    /// Turn completed with a response.
    Response {
        /// The agent's response.
        content: String,
    },

    /// Need approval before continuing.
    NeedApproval {
        /// ID of the approval request.
        request_id: Uuid,
        /// Tool that needs approval.
        tool_name: String,
        /// Description of what the tool will do.
        description: String,
        /// Parameters being passed.
        parameters: serde_json::Value,
    },

    /// Successfully processed (for control commands).
    Ok {
        /// Optional message.
        message: Option<String>,
    },

    /// Error occurred.
    Error {
        /// Error message.
        message: String,
    },

    /// Turn was interrupted.
    Interrupted,
}

impl SubmissionResult {
    /// Create a response result.
    pub fn response(content: impl Into<String>) -> Self {
        Self::Response {
            content: content.into(),
        }
    }

    /// Create an OK result.
    pub fn ok() -> Self {
        Self::Ok { message: None }
    }

    /// Create an OK result with a message.
    pub fn ok_with_message(message: impl Into<String>) -> Self {
        Self::Ok {
            message: Some(message.into()),
        }
    }

    /// Create an error result.
    pub fn error(message: impl Into<String>) -> Self {
        Self::Error {
            message: message.into(),
        }
    }
}

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

    #[test]
    fn test_submission_types() {
        let input = Submission::user_input("Hello");
        assert!(input.starts_turn());
        assert!(!input.is_control());

        let undo = Submission::undo();
        assert!(!undo.starts_turn());
        assert!(undo.is_control());
    }

    #[test]
    fn test_parser_user_input() {
        let submission = SubmissionParser::parse("Hello, how are you?");
        assert!(
            matches!(submission, Submission::UserInput { content } if content == "Hello, how are you?")
        );
    }

    #[test]
    fn test_parser_undo() {
        let submission = SubmissionParser::parse("/undo");
        assert!(matches!(submission, Submission::Undo));

        let submission = SubmissionParser::parse("/UNDO");
        assert!(matches!(submission, Submission::Undo));
    }

    #[test]
    fn test_parser_redo() {
        let submission = SubmissionParser::parse("/redo");
        assert!(matches!(submission, Submission::Redo));
    }

    #[test]
    fn test_parser_interrupt() {
        let submission = SubmissionParser::parse("/interrupt");
        assert!(matches!(submission, Submission::Interrupt));

        let submission = SubmissionParser::parse("/stop");
        assert!(matches!(submission, Submission::Interrupt));
    }

    #[test]
    fn test_parser_compact() {
        let submission = SubmissionParser::parse("/compact");
        assert!(matches!(submission, Submission::Compact));
    }

    #[test]
    fn test_parser_clear() {
        let submission = SubmissionParser::parse("/clear");
        assert!(matches!(submission, Submission::Clear));
    }

    #[test]
    fn test_parser_new_thread() {
        let submission = SubmissionParser::parse("/thread new");
        assert!(matches!(submission, Submission::NewThread));

        let submission = SubmissionParser::parse("/new");
        assert!(matches!(submission, Submission::NewThread));
    }

    #[test]
    fn test_parser_switch_thread() {
        let uuid = Uuid::new_v4();
        let submission = SubmissionParser::parse(&format!("/thread {}", uuid));
        assert!(matches!(submission, Submission::SwitchThread { thread_id } if thread_id == uuid));
    }

    #[test]
    fn test_parser_resume() {
        let uuid = Uuid::new_v4();
        let submission = SubmissionParser::parse(&format!("/resume {}", uuid));
        assert!(
            matches!(submission, Submission::Resume { checkpoint_id } if checkpoint_id == uuid)
        );
    }

    #[test]
    fn test_parser_heartbeat() {
        let submission = SubmissionParser::parse("/heartbeat");
        assert!(matches!(submission, Submission::Heartbeat));
    }

    #[test]
    fn test_parser_summarize() {
        let submission = SubmissionParser::parse("/summarize");
        assert!(matches!(submission, Submission::Summarize));

        let submission = SubmissionParser::parse("/summary");
        assert!(matches!(submission, Submission::Summarize));
    }

    #[test]
    fn test_parser_suggest() {
        let submission = SubmissionParser::parse("/suggest");
        assert!(matches!(submission, Submission::Suggest));
    }

    #[test]
    fn test_parser_invalid_commands_become_user_input() {
        // Invalid UUID should become user input
        let submission = SubmissionParser::parse("/thread not-a-uuid");
        assert!(matches!(submission, Submission::UserInput { .. }));

        // Unknown command should become user input
        let submission = SubmissionParser::parse("/unknown");
        assert!(matches!(submission, Submission::UserInput { content } if content == "/unknown"));
    }

    #[test]
    fn test_parser_json_exec_approval() {
        let req_id = Uuid::new_v4();
        let json = serde_json::to_string(&Submission::ExecApproval {
            request_id: req_id,
            approved: true,
            always: false,
        })
        .expect("serialize");

        let submission = SubmissionParser::parse(&json);
        assert!(
            matches!(submission, Submission::ExecApproval { request_id, approved, always }
                if request_id == req_id && approved && !always)
        );
    }

    #[test]
    fn test_parser_json_exec_approval_always() {
        let req_id = Uuid::new_v4();
        let json = serde_json::to_string(&Submission::ExecApproval {
            request_id: req_id,
            approved: true,
            always: true,
        })
        .expect("serialize");

        let submission = SubmissionParser::parse(&json);
        assert!(
            matches!(submission, Submission::ExecApproval { request_id, approved, always }
                if request_id == req_id && approved && always)
        );
    }

    #[test]
    fn test_parser_json_exec_approval_deny() {
        let req_id = Uuid::new_v4();
        let json = serde_json::to_string(&Submission::ExecApproval {
            request_id: req_id,
            approved: false,
            always: false,
        })
        .expect("serialize");

        let submission = SubmissionParser::parse(&json);
        assert!(
            matches!(submission, Submission::ExecApproval { request_id, approved, always }
                if request_id == req_id && !approved && !always)
        );
    }

    #[test]
    fn test_parser_json_non_approval_stays_user_input() {
        // A JSON UserInput should NOT be intercepted, it should be treated as text
        let json = r#"{"UserInput":{"content":"hello"}}"#;
        let submission = SubmissionParser::parse(json);
        assert!(matches!(submission, Submission::UserInput { .. }));
    }

    #[test]
    fn test_parser_json_roundtrip_matches_approval_handler() {
        // Simulate exactly what chat_approval_handler does: serialize a Submission::ExecApproval
        // and verify the parser picks it up correctly.
        let request_id = Uuid::new_v4();
        let approval = Submission::ExecApproval {
            request_id,
            approved: true,
            always: false,
        };
        let json = serde_json::to_string(&approval).expect("serialize");
        eprintln!("Serialized approval JSON: {}", json);

        let parsed = SubmissionParser::parse(&json);
        assert!(
            matches!(parsed, Submission::ExecApproval { request_id: rid, approved, always }
                if rid == request_id && approved && !always),
            "Expected ExecApproval, got {:?}",
            parsed
        );
    }

    #[test]
    fn test_parser_system_command_help() {
        let submission = SubmissionParser::parse("/help");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "help" && args.is_empty())
        );

        let submission = SubmissionParser::parse("/?");
        assert!(
            matches!(submission, Submission::SystemCommand { command, .. } if command == "help")
        );

        let submission = SubmissionParser::parse("/HELP");
        assert!(
            matches!(submission, Submission::SystemCommand { command, .. } if command == "help")
        );
    }

    #[test]
    fn test_parser_system_command_model() {
        // No args: show current model
        let submission = SubmissionParser::parse("/model");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "model" && args.is_empty())
        );

        // With args: switch model
        let submission = SubmissionParser::parse("/model gpt-4o");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "model" && args == vec!["gpt-4o"])
        );

        // Case insensitive command, preserves arg case
        let submission = SubmissionParser::parse("/MODEL Claude-3.5");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "model" && args == vec!["Claude-3.5"])
        );
    }

    #[test]
    fn test_parser_system_command_version() {
        let submission = SubmissionParser::parse("/version");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "version" && args.is_empty())
        );
    }

    #[test]
    fn test_parser_system_command_tools() {
        let submission = SubmissionParser::parse("/tools");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "tools" && args.is_empty())
        );
    }

    #[test]
    fn test_parser_system_command_ping() {
        let submission = SubmissionParser::parse("/ping");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "ping" && args.is_empty())
        );
    }

    #[test]
    fn test_parser_system_command_debug() {
        let submission = SubmissionParser::parse("/debug");
        assert!(
            matches!(submission, Submission::SystemCommand { command, args } if command == "debug" && args.is_empty())
        );
    }

    #[test]
    fn test_parser_system_command_is_control() {
        let submission = SubmissionParser::parse("/help");
        assert!(submission.is_control());
        assert!(!submission.starts_turn());
    }

    #[test]
    fn test_parser_quit() {
        assert!(matches!(SubmissionParser::parse("/quit"), Submission::Quit));
        assert!(matches!(SubmissionParser::parse("/exit"), Submission::Quit));
        assert!(matches!(
            SubmissionParser::parse("/shutdown"),
            Submission::Quit
        ));
        assert!(matches!(SubmissionParser::parse("/QUIT"), Submission::Quit));
        assert!(matches!(SubmissionParser::parse("/Exit"), Submission::Quit));
    }
}