zeph-core 0.22.4

Core agent loop, configuration, context builder, metrics, and vault for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Shared test infrastructure for `crate::agent` tests.
//!
//! Provides `MockChannel`, `MockToolExecutor`, `QuickTestAgent`, and helper
//! functions re-exported through `agent_tests` for cross-module visibility.

pub(crate) use std::sync::{Arc, Mutex};

type ToolOutputResult = Result<Option<ToolOutput>, ToolError>;
type EnvSnapshot = Option<std::collections::HashMap<String, String>>;

#[allow(unused_imports)]
pub(crate) use sqlx::prelude::*;
pub(crate) use tokio::sync::{Notify, mpsc, watch};
pub(crate) use zeph_llm::any::AnyProvider;
pub(crate) use zeph_llm::mock::MockProvider;
pub(crate) use zeph_llm::provider::{Message, MessageMetadata, Role};
pub(crate) use zeph_memory::semantic::SemanticMemory;
pub(crate) use zeph_skills::registry::SkillRegistry;
pub(crate) use zeph_skills::watcher::SkillEvent;
pub(crate) use zeph_tools::executor::{ToolError, ToolExecutor, ToolOutput};

pub(crate) use crate::agent::message_queue::{MAX_AUDIO_BYTES, MAX_IMAGE_BYTES, detect_image_mime};
pub(crate) use crate::agent::{
    Agent, TOOL_OUTPUT_SUFFIX, format_tool_output, recv_optional, shutdown_signal,
};
pub(crate) use crate::channel::{
    Attachment, AttachmentKind, Channel, ChannelMessage, ToolStartEvent,
};
pub(crate) use crate::config::{SecurityConfig, TimeoutConfig};
pub(crate) use crate::metrics::MetricsSnapshot;

/// Minimal test harness: an Agent wired with a `MockChannel` and `MockToolExecutor`.
/// Use `QuickTestAgent::minimal()` to get a ready-to-use agent for unit tests.
pub(crate) struct QuickTestAgent {
    pub(crate) agent: Agent<MockChannel>,
}

impl QuickTestAgent {
    /// Create a minimal agent with a single mock response and no tools.
    pub(crate) fn minimal(response: &str) -> Self {
        let provider = mock_provider(vec![response.to_owned()]);
        let channel = MockChannel::new(vec![]);
        let registry = create_test_registry();
        let executor = MockToolExecutor::no_tools();
        Self {
            agent: Agent::new(provider, channel, registry, None, 5, executor),
        }
    }

    /// Create a minimal agent with multiple mock responses and no tools.
    pub(crate) fn with_responses(responses: Vec<String>) -> Self {
        let provider = mock_provider(responses);
        let channel = MockChannel::new(vec![]);
        let registry = create_test_registry();
        let executor = MockToolExecutor::no_tools();
        Self {
            agent: Agent::new(provider, channel, registry, None, 5, executor),
        }
    }

    /// Return the messages sent to the channel so far.
    pub(crate) fn sent_messages(&self) -> Vec<String> {
        self.agent.channel.sent_messages()
    }
}

pub(crate) fn mock_provider(responses: Vec<String>) -> AnyProvider {
    AnyProvider::Mock(MockProvider::with_responses(responses))
}

pub(crate) fn mock_provider_streaming(responses: Vec<String>) -> AnyProvider {
    AnyProvider::Mock(MockProvider::with_responses(responses).with_streaming())
}

/// Vision-capable mock provider (spec-072): used to test the vision-tier emission gate in
/// `process_one_tool_result`/`emit_media_parts` without needing a real provider.
pub(crate) fn mock_provider_with_vision(responses: Vec<String>) -> AnyProvider {
    AnyProvider::Mock(MockProvider::with_responses(responses).with_vision())
}

pub(crate) fn mock_provider_failing() -> AnyProvider {
    AnyProvider::Mock(MockProvider::failing())
}

pub(crate) fn mock_provider_with_models(
    responses: Vec<String>,
    models: Vec<zeph_llm::model_cache::RemoteModelInfo>,
) -> AnyProvider {
    AnyProvider::Mock(MockProvider::with_responses(responses).with_models(models))
}

pub(crate) struct MockChannel {
    pub(crate) messages: Arc<Mutex<Vec<String>>>,
    pub(crate) sent: Arc<Mutex<Vec<String>>>,
    pub(crate) chunks: Arc<Mutex<Vec<String>>>,
    pub(crate) confirmations: Arc<Mutex<Vec<bool>>>,
    /// Records the exact prompt text passed to every `Channel::confirm` call, in order — lets
    /// tests assert on prompt content (e.g. that a real command is shown, not a generic string).
    pub(crate) confirmed_prompts: Arc<Mutex<Vec<String>>>,
    pub(crate) statuses: Arc<Mutex<Vec<String>>>,
    pub(crate) tool_starts: Arc<Mutex<Vec<ToolStartEvent>>>,
    /// Records every `Channel::notify_foreground_subagent_completed` call as
    /// `(task_id, name, success)`, in order — lets tests assert on TUI-completion-event count
    /// and content (e.g. that a durable replay does not re-fire it, #6027).
    pub(crate) notify_completed_calls: Arc<Mutex<Vec<(String, String, bool)>>>,
    /// Records every `Channel::notify_background_subagent_completed` call as
    /// `(task_id, name, success)`, in order — lets tests assert that background sub-agent
    /// completions notify the view layer even though the parent turn was not blocking on
    /// them (#6570).
    pub(crate) notify_background_completed_calls: Arc<Mutex<Vec<(String, String, bool)>>>,
    pub(crate) exit_supported: bool,
    pub(crate) input_sanitization_required: bool,
    /// When `true`, `send()` fails with `ChannelError::ChannelClosed` instead of recording the
    /// message — simulates a disconnected adapter (closed mpsc receiver, dropped
    /// Telegram/Discord connection) for testing that callers do not depend on the notification
    /// send succeeding (#5717).
    pub(crate) fail_send: bool,
}

impl MockChannel {
    pub(crate) fn new(messages: Vec<String>) -> Self {
        Self {
            messages: Arc::new(Mutex::new(messages)),
            sent: Arc::new(Mutex::new(Vec::new())),
            chunks: Arc::new(Mutex::new(Vec::new())),
            confirmations: Arc::new(Mutex::new(Vec::new())),
            confirmed_prompts: Arc::new(Mutex::new(Vec::new())),
            statuses: Arc::new(Mutex::new(Vec::new())),
            tool_starts: Arc::new(Mutex::new(Vec::new())),
            notify_completed_calls: Arc::new(Mutex::new(Vec::new())),
            notify_background_completed_calls: Arc::new(Mutex::new(Vec::new())),
            exit_supported: true,
            input_sanitization_required: false,
            fail_send: false,
        }
    }

    /// Make every call to `Channel::send` fail with `ChannelError::ChannelClosed` (#5717).
    pub(crate) fn with_failing_send(mut self) -> Self {
        self.fail_send = true;
        self
    }

    pub(crate) fn without_exit_support(mut self) -> Self {
        self.exit_supported = false;
        self
    }

    /// Simulates a bot-adapter channel (Telegram/Discord/Slack) whose input is external and
    /// untrusted, exercising the `Channel::requires_input_sanitization` central-loop path.
    pub(crate) fn with_input_sanitization_required(mut self) -> Self {
        self.input_sanitization_required = true;
        self
    }

    pub(crate) fn with_confirmations(mut self, confirmations: Vec<bool>) -> Self {
        self.confirmations = Arc::new(Mutex::new(confirmations));
        self
    }

    pub(crate) fn sent_messages(&self) -> Vec<String> {
        self.sent.lock().unwrap().clone()
    }

    pub(crate) fn confirmed_prompts(&self) -> Vec<String> {
        self.confirmed_prompts.lock().unwrap().clone()
    }

    pub(crate) fn notify_completed_calls(&self) -> Vec<(String, String, bool)> {
        self.notify_completed_calls.lock().unwrap().clone()
    }

    pub(crate) fn notify_background_completed_calls(&self) -> Vec<(String, String, bool)> {
        self.notify_background_completed_calls
            .lock()
            .unwrap()
            .clone()
    }
}

impl Channel for MockChannel {
    async fn recv(&mut self) -> Result<Option<ChannelMessage>, crate::channel::ChannelError> {
        let mut msgs = self.messages.lock().unwrap();
        if msgs.is_empty() {
            Ok(None)
        } else {
            Ok(Some(ChannelMessage {
                text: msgs.remove(0),
                attachments: vec![],
                is_guest_context: false,
                is_from_bot: false,
                owner_key: None,
            }))
        }
    }

    fn try_recv(&mut self) -> Option<ChannelMessage> {
        let mut msgs = self.messages.lock().unwrap();
        if msgs.is_empty() {
            None
        } else {
            Some(ChannelMessage {
                text: msgs.remove(0),
                attachments: vec![],
                is_guest_context: false,
                is_from_bot: false,
                owner_key: None,
            })
        }
    }

    async fn send(&mut self, text: &str) -> Result<(), crate::channel::ChannelError> {
        if self.fail_send {
            return Err(crate::channel::ChannelError::ChannelClosed);
        }
        self.sent.lock().unwrap().push(text.to_string());
        Ok(())
    }

    async fn send_chunk(&mut self, chunk: &str) -> Result<(), crate::channel::ChannelError> {
        self.chunks.lock().unwrap().push(chunk.to_string());
        Ok(())
    }

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

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

    async fn send_tool_start(
        &mut self,
        event: ToolStartEvent,
    ) -> Result<(), crate::channel::ChannelError> {
        self.tool_starts.lock().unwrap().push(event);
        Ok(())
    }

    async fn confirm(&mut self, prompt: &str) -> Result<bool, crate::channel::ChannelError> {
        self.confirmed_prompts
            .lock()
            .unwrap()
            .push(prompt.to_owned());
        let mut confs = self.confirmations.lock().unwrap();
        Ok(if confs.is_empty() {
            true
        } else {
            confs.remove(0)
        })
    }

    fn supports_exit(&self) -> bool {
        self.exit_supported
    }

    fn requires_input_sanitization(&self) -> bool {
        self.input_sanitization_required
    }

    async fn notify_foreground_subagent_completed(
        &mut self,
        id: &str,
        name: &str,
        success: bool,
    ) -> Result<(), crate::channel::ChannelError> {
        self.notify_completed_calls
            .lock()
            .unwrap()
            .push((id.to_owned(), name.to_owned(), success));
        Ok(())
    }

    async fn notify_background_subagent_completed(
        &mut self,
        id: &str,
        name: &str,
        success: bool,
    ) -> Result<(), crate::channel::ChannelError> {
        self.notify_background_completed_calls
            .lock()
            .unwrap()
            .push((id.to_owned(), name.to_owned(), success));
        Ok(())
    }
}

/// A [`Channel`] whose `recv()` never resolves.
///
/// Unlike `MockChannel` — whose `recv()` on an empty message queue resolves *immediately* to
/// `Ok(None)` (i.e. "closed") — this channel keeps `next_event()`'s `channel.recv()` branch of
/// `tokio::select!` permanently pending. Tests that need to observe a specific non-channel
/// `tokio::select!` arm (e.g. a periodic tick) in isolation, without racing against the
/// immediately-ready channel-closed branch, should use this instead of an empty `MockChannel`.
pub(crate) struct PendingChannel;

impl Channel for PendingChannel {
    async fn recv(&mut self) -> Result<Option<ChannelMessage>, crate::channel::ChannelError> {
        std::future::pending().await
    }

    async fn send(&mut self, _text: &str) -> Result<(), crate::channel::ChannelError> {
        Ok(())
    }

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

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

pub(crate) struct MockToolExecutor {
    outputs: Arc<Mutex<Vec<ToolOutputResult>>>,
    pub(crate) captured_env: Arc<Mutex<Vec<EnvSnapshot>>>,
    definitions: Vec<zeph_tools::registry::ToolDef>,
    delay_ms: u64,
}

impl MockToolExecutor {
    pub(crate) fn new(outputs: Vec<ToolOutputResult>) -> Self {
        Self {
            outputs: Arc::new(Mutex::new(outputs)),
            captured_env: Arc::new(Mutex::new(Vec::new())),
            definitions: Vec::new(),
            delay_ms: 0,
        }
    }

    pub(crate) fn no_tools() -> Self {
        Self::new(vec![Ok(None)])
    }

    /// Executor that returns a single successful tool output with the given summary.
    pub(crate) fn with_output(
        tool_name: impl Into<zeph_tools::ToolName>,
        summary: impl Into<String>,
    ) -> Self {
        let output = ToolOutput {
            tool_name: tool_name.into(),
            summary: summary.into(),
            blocks_executed: 1,
            filter_stats: None,
            diff: None,
            streamed: false,
            terminal_id: None,
            locations: None,
            raw_response: None,
            claim_source: None,
            ..Default::default()
        };
        Self::new(vec![Ok(Some(output))])
    }

    /// Attach tool definitions returned by `tool_definitions()`/`tool_definitions_erased()`, for
    /// tests that need a schema lookup (e.g. the parameter-reformat phase, #5453).
    pub(crate) fn with_definitions(
        mut self,
        definitions: Vec<zeph_tools::registry::ToolDef>,
    ) -> Self {
        self.definitions = definitions;
        self
    }

    /// Sleep for `ms` milliseconds before returning a dispatch result, so timing-sensitive
    /// tests (e.g. the reformat-phase whole-phase budget guard, #5453) can advance real
    /// wall-clock time deterministically without a background sleep of their own.
    pub(crate) fn with_delay(mut self, ms: u64) -> Self {
        self.delay_ms = ms;
        self
    }
}

impl ToolExecutor for MockToolExecutor {
    async fn execute(&self, _response: &str) -> Result<Option<ToolOutput>, ToolError> {
        let mut outputs = self.outputs.lock().unwrap();
        if outputs.is_empty() {
            Ok(None)
        } else {
            outputs.remove(0)
        }
    }

    async fn execute_tool_call(
        &self,
        _call: &zeph_tools::executor::ToolCall,
    ) -> Result<Option<ToolOutput>, ToolError> {
        if self.delay_ms > 0 {
            tokio::time::sleep(std::time::Duration::from_millis(self.delay_ms)).await;
        }
        let mut outputs = self.outputs.lock().unwrap();
        if outputs.is_empty() {
            Ok(None)
        } else {
            outputs.remove(0)
        }
    }

    fn set_skill_env(&self, env: Option<std::collections::HashMap<String, String>>) {
        self.captured_env.lock().unwrap().push(env);
    }

    fn tool_definitions(&self) -> Vec<zeph_tools::registry::ToolDef> {
        self.definitions.clone()
    }
    zeph_tools::tool_executor_no_inner_defaults!();
}

pub(crate) fn create_test_registry() -> SkillRegistry {
    let temp_dir = tempfile::tempdir().unwrap();
    let skill_dir = temp_dir.path().join("test-skill");
    std::fs::create_dir(&skill_dir).unwrap();
    std::fs::write(
        skill_dir.join("SKILL.md"),
        "---\nname: test-skill\ndescription: A test skill\n---\nTest skill body",
    )
    .unwrap();
    SkillRegistry::load(&[temp_dir.path().to_path_buf()])
}