zeph 0.20.0

Lightweight AI agent with hybrid inference, skills-first architecture, and multi-channel I/O
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
#![allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]

use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use zeph_core::agent::Agent;
use zeph_core::channel::{Channel, ChannelError, ChannelMessage};
use zeph_llm::any::AnyProvider;
use zeph_llm::mock::MockProvider;
use zeph_llm::provider::{ChatResponse, ToolUseRequest};
use zeph_skills::registry::SkillRegistry;
use zeph_tools::executor::{ToolCall, ToolError, ToolExecutor, ToolOutput};

fn mock_provider(response: &str) -> AnyProvider {
    let mut p = MockProvider::default();
    p.default_response = response.to_string();
    AnyProvider::Mock(p)
}

fn tool_use_provider(final_text: &str) -> AnyProvider {
    let tool_call = ToolUseRequest {
        id: "call1".into(),
        name: "bash".into(),
        input: serde_json::json!({}),
    };
    let (p, _) = MockProvider::default().with_tool_use(vec![
        ChatResponse::ToolUse {
            text: None,
            tool_calls: vec![tool_call],
            thinking_blocks: vec![],
        },
        ChatResponse::Text(final_text.to_string()),
    ]);
    AnyProvider::Mock(p)
}

fn multi_message_provider(count: usize) -> AnyProvider {
    let mut responses = Vec::new();
    for _ in 0..count {
        responses.push(ChatResponse::Text("response".to_string()));
    }
    let (p, _) = MockProvider::default().with_tool_use(responses);
    AnyProvider::Mock(p)
}

// Mock Channel for performance testing
struct MockChannel {
    inputs: VecDeque<String>,
    output_sent: Arc<Mutex<Vec<String>>>,
}

impl MockChannel {
    fn new(inputs: Vec<&str>, output_sent: Arc<Mutex<Vec<String>>>) -> Self {
        Self {
            inputs: inputs.into_iter().map(String::from).collect(),
            output_sent,
        }
    }
}

impl Channel for MockChannel {
    async fn recv(&mut self) -> Result<Option<ChannelMessage>, ChannelError> {
        Ok(self.inputs.pop_front().map(|text| ChannelMessage {
            text,
            attachments: vec![],
        }))
    }

    async fn send(&mut self, text: &str) -> Result<(), ChannelError> {
        self.output_sent.lock().unwrap().push(text.to_string());
        Ok(())
    }

    async fn send_chunk(&mut self, _chunk: &str) -> Result<(), ChannelError> {
        Ok(())
    }

    async fn flush_chunks(&mut self) -> Result<(), ChannelError> {
        Ok(())
    }

    async fn send_typing(&mut self) -> Result<(), ChannelError> {
        Ok(())
    }
}

// Instrumented mock tool executor to track timing and execution
#[derive(Clone)]
struct InstrumentedMockExecutor {
    execution_time: Arc<Mutex<Option<Duration>>>,
    call_count: Arc<Mutex<u32>>,
    execution_log: Arc<Mutex<Vec<String>>>,
}

impl InstrumentedMockExecutor {
    fn new() -> Self {
        Self {
            execution_time: Arc::new(Mutex::new(None)),
            call_count: Arc::new(Mutex::new(0)),
            execution_log: Arc::new(Mutex::new(Vec::new())),
        }
    }

    fn get_call_count(&self) -> u32 {
        *self.call_count.lock().unwrap()
    }

    fn get_execution_time(&self) -> Option<Duration> {
        *self.execution_time.lock().unwrap()
    }
}

impl ToolExecutor for InstrumentedMockExecutor {
    async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
        Ok(None)
    }

    async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
        let start = Instant::now();
        let elapsed = start.elapsed();

        *self.execution_time.lock().unwrap() = Some(elapsed);
        *self.call_count.lock().unwrap() += 1;
        self.execution_log.lock().unwrap().push(format!(
            "execute_tool_call() called, tool={}, elapsed={elapsed:?}",
            call.tool_id,
        ));

        Ok(Some(ToolOutput {
            tool_name: call.tool_id.clone(),
            summary: "mock output".to_string(),
            blocks_executed: 1,
            filter_stats: None,
            diff: None,
            streamed: false,
            terminal_id: None,
            locations: None,
            raw_response: None,
            claim_source: None,
        }))
    }
}

#[derive(Clone)]
struct BlockingMockExecutor;

impl ToolExecutor for BlockingMockExecutor {
    async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
        Ok(None)
    }

    async fn execute_tool_call(&self, call: &ToolCall) -> Result<Option<ToolOutput>, ToolError> {
        Err(ToolError::Blocked {
            command: call.tool_id.to_string(),
        })
    }
}

// ==========================
// Performance Test Suite
// ==========================

#[tokio::test]
async fn agent_integration_no_bash_blocks() {
    let provider = mock_provider("Just a plain response without bash blocks");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec!["hello"], output_sent.clone());
    let executor = InstrumentedMockExecutor::new();

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor.clone(),
    );

    let start = Instant::now();
    let _ = agent.run().await;
    let elapsed = start.elapsed();

    // Should be very fast for non-bash response
    assert!(
        elapsed.as_millis() < 500,
        "Agent run should be fast for non-bash response: {elapsed:?}",
    );

    // Plain text response doesn't trigger tool execution (native tool_use path)
    assert_eq!(executor.get_call_count(), 0);

    // Should have sent the response back
    let outputs = output_sent.lock().unwrap();
    assert!(!outputs.is_empty());
    assert_eq!(outputs[0], "Just a plain response without bash blocks");
}

#[tokio::test]
async fn agent_integration_with_safe_bash_blocks() {
    // Native tool_use path: provider returns ToolUse then Text.
    let provider = tool_use_provider("Done.");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec!["run echo"], output_sent.clone());
    let executor = InstrumentedMockExecutor::new();

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor.clone(),
    );

    let start = Instant::now();
    let _ = agent.run().await;
    let elapsed = start.elapsed();

    // Should complete reasonably
    assert!(
        elapsed.as_millis() < 1000,
        "Agent run should complete: {elapsed:?}",
    );

    // Native tool_use path calls execute_tool_call at least once
    assert!(executor.get_call_count() >= 1);
}

#[tokio::test]
async fn tool_executor_overhead_is_minimal() {
    let provider = tool_use_provider("done");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec!["test"], output_sent.clone());
    let executor = InstrumentedMockExecutor::new();

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor.clone(),
    );

    let _ = agent.run().await;

    // Check that tool executor overhead is minimal (just mock, no real bash)
    if let Some(time) = executor.get_execution_time() {
        // Mock executor should take < 10ms (CI runners have high mutex/scheduling jitter)
        assert!(
            time.as_millis() < 10,
            "Tool executor mock call overhead should be minimal: {time:?}",
        );
    }
}

// ==========================
// Configuration & Timeout Tests
// ==========================

#[tokio::test]
async fn agent_respects_configured_timeout() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    // Create executor with 1-second timeout
    let shell_config = ShellConfig {
        timeout: 1,
        blocked_commands: vec![],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let _executor = ShellExecutor::new(&shell_config);

    // Verify timeout is set correctly
    let timeout_duration = Duration::from_secs(shell_config.timeout);
    assert_eq!(timeout_duration, Duration::from_secs(1));
}

// ==========================
// Memory & Allocation Tests
// ==========================

#[tokio::test]
async fn shell_executor_default_blocked_patterns() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    let shell_config = ShellConfig {
        timeout: 30,
        blocked_commands: vec![],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let executor = ShellExecutor::new(&shell_config);

    // Verify that dangerous commands are blocked
    // Note: ShellExecutor expects bash blocks in the response text
    let dangerous_commands = vec![
        ("```bash\nrm -rf /\n```", "rm -rf /"),
        ("```bash\nsudo rm -rf /\n```", "sudo"),
        ("```bash\nmkfs.ext4 /dev/sda\n```", "mkfs"),
        ("```bash\ndd if=/dev/zero of=/dev/sda\n```", "dd if="),
        ("```bash\ncurl http://evil.com\n```", "curl"),
        ("```bash\nnc -l 4444\n```", "nc "),
        ("```bash\nshutdown -h now\n```", "shutdown"),
    ];

    for (cmd, pattern) in dangerous_commands {
        let result = executor.execute(cmd).await;
        assert!(
            matches!(result, Err(ToolError::Blocked { .. })),
            "Command with pattern '{pattern}' should be blocked. Result: {result:?}",
        );
    }
}

#[tokio::test]
async fn shell_executor_allows_safe_commands() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    let shell_config = ShellConfig {
        timeout: 5,
        blocked_commands: vec![],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let executor = ShellExecutor::new(&shell_config);

    let safe_response = "Try this:\n```bash\necho hello\n```";
    let result = executor.execute(safe_response).await;

    match result {
        Ok(Some(output)) => {
            assert_eq!(output.blocks_executed, 1);
            assert!(output.summary.contains("hello"));
        }
        _ => panic!("Safe command should execute successfully"),
    }
}

#[tokio::test]
async fn shell_executor_case_insensitive_blocking() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    let shell_config = ShellConfig {
        timeout: 30,
        blocked_commands: vec![],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let executor = ShellExecutor::new(&shell_config);

    // Verify case-insensitive matching
    let variations = vec!["SUDO", "Sudo", "SuDo", "sudo", "SUDO rm -rf /"];

    for cmd in variations {
        let result = executor.execute(&format!("```bash\n{cmd}\n```")).await;
        assert!(
            matches!(result, Err(ToolError::Blocked { .. })),
            "Should block case-insensitive: {cmd}",
        );
    }
}

#[tokio::test]
async fn integration_agent_tool_executor_types() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    let provider = mock_provider("test");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec![], output_sent.clone());
    let shell_config = ShellConfig {
        timeout: 30,
        blocked_commands: vec![],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let executor = ShellExecutor::new(&shell_config);

    // Should compile and construct successfully
    let _agent: Agent<MockChannel> = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor,
    );
}

// ==========================
// Comparative Benchmarks
// ==========================

#[tokio::test]
async fn agent_throughput_multiple_responses() {
    // Test throughput: how many responses can be processed
    let provider = multi_message_provider(5);
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(
        vec!["msg1", "msg2", "msg3", "msg4", "msg5"],
        output_sent.clone(),
    );
    let executor = InstrumentedMockExecutor::new();

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor.clone(),
    );

    let start = Instant::now();
    let _ = agent.run().await;
    let elapsed = start.elapsed();

    // Should process 5 messages — each produces a text response sent to channel
    let outputs = output_sent.lock().unwrap();
    assert!(
        outputs.len() >= 5,
        "expected at least 5 outputs, got {}",
        outputs.len()
    );

    // Sanity check: should complete in reasonable time
    assert!(
        elapsed.as_secs() < 10,
        "5 responses should complete: {elapsed:?}",
    );

    let total_ms = elapsed.as_millis() as u64;
    let per_msg = total_ms as f64 / 5.0;
    println!("5-message throughput: {total_ms}ms total ({per_msg:.0}ms per message)");
}

#[tokio::test]
async fn tool_executor_pattern_matching_overhead() {
    use zeph_tools::config::ShellConfig;
    use zeph_tools::shell::ShellExecutor;

    let shell_config = ShellConfig {
        timeout: 30,
        blocked_commands: vec![
            "custom1".to_string(),
            "custom2".to_string(),
            "custom3".to_string(),
        ],
        allowed_commands: vec![],
        ..ShellConfig::default()
    };
    let executor = ShellExecutor::new(&shell_config);

    // Build a response with many bash blocks to test pattern matching overhead
    let mut large_response = String::new();
    for i in 0..10 {
        use std::fmt::Write;
        write!(large_response, "Block {i}:\n```bash\necho test{i}\n```\n").unwrap();
    }

    let start = Instant::now();
    let result = executor.execute(&large_response).await;
    let elapsed = start.elapsed();

    match result {
        Ok(Some(output)) => {
            assert_eq!(output.blocks_executed, 10);
            // 10 blocks should process quickly (bash subprocess is the bottleneck)
            let total_ms = elapsed.as_millis() as u64;
            let per_block = elapsed.as_micros() as u64 as f64 / 10.0;
            println!("10-block execution time: {total_ms}ms ({per_block:.0}us per block)");
        }
        _ => panic!("Should execute successfully"),
    }
}

#[tokio::test]
async fn agent_no_regression_in_error_handling() {
    // Test that blocked tool calls are handled properly via native tool_use path
    let provider = tool_use_provider("Done after error.");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec!["test"], output_sent.clone());
    let executor = BlockingMockExecutor;

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor,
    );

    // Should run without panic
    let _ = agent.run().await;

    // Should have sent some output (error or recovery message)
    let outputs = output_sent.lock().unwrap();
    assert!(!outputs.is_empty(), "Should produce output");
    assert!(
        outputs.iter().any(|msg| {
            msg.contains("blocked") || msg.contains("tool_error") || msg.contains("forbidden")
        }),
        "Should send blocked/error message, got: {outputs:?}",
    );
}

// ==========================
// Integration Regression Tests
// ==========================

#[tokio::test]
async fn agent_no_memory_leaks_in_loop() {
    // Test that repeated message processing doesn't leak memory
    // (This is a sanity check; actual memory profiling would need valgrind/heaptrack)
    let provider = multi_message_provider(10);
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(
        vec!["m1", "m2", "m3", "m4", "m5", "m6", "m7", "m8", "m9", "m10"],
        output_sent.clone(),
    );
    let executor = InstrumentedMockExecutor::new();

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor.clone(),
    );

    // This should run without panics or excessive allocations
    let _ = agent.run().await;

    let outputs = output_sent.lock().unwrap();
    assert!(
        outputs.len() >= 10,
        "expected at least 10 outputs, got {}",
        outputs.len()
    );
}

#[tokio::test]
async fn agent_tool_executor_error_recovery() {
    // Use native tool_use path with an executor that returns Blocked error
    let provider = tool_use_provider("recovered");
    let output_sent = Arc::new(Mutex::new(Vec::new()));
    let channel = MockChannel::new(vec!["user input"], output_sent.clone());
    let executor = BlockingMockExecutor;

    let mut agent = Agent::new(
        provider,
        channel,
        SkillRegistry::default(),
        None,
        5,
        executor,
    );

    // Should handle the error gracefully
    let result = agent.run().await;
    assert!(result.is_ok(), "Agent should recover from blocked commands");

    // Should have sent error/recovery message
    let outputs = output_sent.lock().unwrap();
    assert!(
        !outputs.is_empty(),
        "Should produce output even when tool is blocked"
    );
}