zeph-core 0.21.0

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
427
428
429
430
431
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Decoding-level speculative dispatch: `SpeculativeStreamDrainer`.
//!
//! Consumes a [`ToolSseStream`] from the Claude SSE tool-use path, fires speculative
//! dispatches via [`SpeculationEngine::try_dispatch`] as soon as all required JSON fields
//! are present in the partial input buffer, and assembles a complete [`ChatResponse`] at
//! stream end — including thinking blocks.
//!
//! Mode gate: only active when `speculative.mode` is `Decoding` or `Both`.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use tokio_stream::StreamExt;
use tracing::debug;
use zeph_llm::provider::{ChatResponse, ThinkingBlock, ToolUseRequest};
use zeph_llm::sse::{ToolSseEvent, ToolSseStream};

use super::SpeculationEngine;
use super::partial_json::{PartialJsonParser, PrefixState};
use super::prediction::{Prediction, PredictionSource};

/// Drives a `ToolSseStream`, intercepts `InputJsonDelta` events for speculative dispatch,
/// and reassembles a `ChatResponse` for the normal tool-dispatch path.
///
/// `ToolBlockStart` events populate tool metadata before any `InputJsonDelta` arrives,
/// ensuring speculative dispatch can fire incrementally during streaming.
pub struct SpeculativeStreamDrainer {
    stream: ToolSseStream,
    engine: Arc<SpeculationEngine>,
    confidence_threshold: f32,
    /// Per-tool-index partial JSON parser.
    parsers: HashMap<usize, PartialJsonParser>,
}

impl SpeculativeStreamDrainer {
    /// Create a new drainer.
    ///
    /// `confidence_threshold` gates speculation. Trust level is enforced internally
    /// by `engine.try_dispatch` — no separate parameter is needed here.
    #[must_use]
    pub fn new(
        stream: ToolSseStream,
        engine: Arc<SpeculationEngine>,
        confidence_threshold: f32,
    ) -> Self {
        Self {
            stream,
            engine,
            confidence_threshold,
            parsers: HashMap::new(),
        }
    }

    /// Drain the stream, firing speculative dispatches and building the final `ChatResponse`.
    ///
    /// Returns the assembled `ChatResponse` — identical contract to
    /// `LlmProvider::chat_with_tools`. On stream error, returns the first error encountered.
    ///
    /// # Errors
    ///
    /// Returns an `LlmError` if the SSE stream signals a parse or API error.
    pub async fn drive(mut self) -> Result<ChatResponse, zeph_llm::LlmError> {
        let mut tool_calls: Vec<ToolUseRequest> = Vec::new();
        let mut thinking_blocks: Vec<ThinkingBlock> = Vec::new();
        let mut text_buf = String::new();
        // Map from tool index → (id, name) collected from ToolCallComplete.
        let mut tool_meta: HashMap<usize, (String, String)> = HashMap::new();
        // Track which tool indices have had their speculation fired.
        let mut dispatched: std::collections::HashSet<usize> = std::collections::HashSet::new();

        while let Some(event) = self.stream.next().await {
            match event {
                ToolSseEvent::ToolBlockStart { index, id, name } => {
                    // Populate tool_meta immediately at block open so InputJsonDelta handlers
                    // can look up id+name before content_block_stop fires ToolCallComplete.
                    tool_meta.insert(index, (id, name));
                }
                ToolSseEvent::InputJsonDelta { index, delta } => {
                    let parser = self.parsers.entry(index).or_default();
                    if let PrefixState::ValidPrefix {
                        known_leaves,
                        missing_required,
                    } = parser.push(&delta)
                        && missing_required.is_empty()
                        && !dispatched.contains(&index)
                        && let Some((_llm_id, name)) = tool_meta.get(&index)
                    {
                        let pred = Prediction {
                            tool_id: name.as_str().into(),
                            args: known_leaves,
                            confidence: self.confidence_threshold,
                            source: PredictionSource::StreamPartial,
                        };
                        if self
                            .engine
                            .try_dispatch(&pred, zeph_common::SkillTrustLevel::Trusted)
                        {
                            dispatched.insert(index);
                            debug!(tool = %name, index, "speculative dispatch fired from SSE delta");
                        }
                    }
                }
                ToolSseEvent::ToolCallComplete {
                    index,
                    id,
                    name,
                    full_json,
                } => {
                    // tool_meta already populated by ToolBlockStart; update in case of re-order.
                    tool_meta.insert(index, (id.clone(), name.clone()));

                    let input = serde_json::from_str(&full_json)
                        .unwrap_or(serde_json::Value::Object(serde_json::Map::new()));

                    tool_calls.push(ToolUseRequest {
                        id,
                        name: name.into(),
                        input,
                    });
                }
                ToolSseEvent::ThinkingBlockDone(block) => {
                    thinking_blocks.push(block);
                }
                ToolSseEvent::ThinkingChunk(_) => {
                    // Pass-through; full block assembled via ThinkingBlockDone.
                }
                ToolSseEvent::ContentChunk(text) => {
                    text_buf.push_str(&text);
                }
                ToolSseEvent::Compaction(_summary) => {
                    // TODO: surface compaction summaries to the caller (follow-up).
                    tracing::debug!(
                        "compaction summary received during tool stream (not yet surfaced)"
                    );
                }
                ToolSseEvent::Error(e) => {
                    return Err(e);
                }
            }
        }

        let text = if text_buf.is_empty() {
            None
        } else {
            Some(text_buf)
        };

        if tool_calls.is_empty() {
            Ok(ChatResponse::Text(text.unwrap_or_default()))
        } else {
            Ok(ChatResponse::ToolUse {
                text,
                tool_calls,
                thinking_blocks,
            })
        }
    }
}

/// Wraps `engine.try_commit` with a 2-second timeout cap (critic M4).
///
/// The TTL deadline (up to 30 s) is too long for the tool dispatch hot path.
/// This helper caps the wait to avoid blocking normal execution unnecessarily.
pub async fn try_commit_with_timeout(
    engine: &SpeculationEngine,
    call: &zeph_tools::ToolCall,
) -> Option<Result<Option<zeph_tools::ToolOutput>, zeph_tools::ToolError>> {
    const COMMIT_TIMEOUT: Duration = Duration::from_secs(2);
    match tokio::time::timeout(COMMIT_TIMEOUT, engine.try_commit(call)).await {
        Ok(result) => result,
        Err(_elapsed) => {
            debug!(tool_id = %call.tool_id, "speculative try_commit timed out after 2s");
            None
        }
    }
}

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

    #[tokio::test]
    async fn drainer_new_has_empty_parsers() {
        use std::sync::Arc;
        use zeph_config::tools::SpeculativeConfig;
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct NullExec;
        impl ToolExecutor for NullExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                false
            }
        }
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(NullExec),
            SpeculativeConfig::default(),
        ));
        let drainer = SpeculativeStreamDrainer::new(Box::pin(tokio_stream::empty()), engine, 0.8);
        assert!(drainer.parsers.is_empty());
    }

    /// Verifies the BUG-2 fix: `ToolBlockStart` populates `tool_meta` before any
    /// `InputJsonDelta` arrives, allowing speculative dispatch to fire on the first
    /// complete-args delta rather than waiting until `ToolCallComplete` (block stop).
    #[tokio::test]
    async fn tool_block_start_enables_incremental_dispatch() {
        use std::sync::Arc;
        use std::sync::atomic::{AtomicUsize, Ordering};
        use zeph_config::tools::{SpeculationMode, SpeculativeConfig};
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct SpyExec {
            count: Arc<AtomicUsize>,
        }
        impl ToolExecutor for SpyExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                self.count.fetch_add(1, Ordering::Relaxed);
                true
            }
        }

        // Executor that records how many times is_tool_speculatable returns true.
        let dispatch_count = Arc::new(AtomicUsize::new(0));
        let dispatch_count_clone = Arc::clone(&dispatch_count);

        let config = SpeculativeConfig {
            mode: SpeculationMode::Decoding,
            ..Default::default()
        };
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(SpyExec {
                count: dispatch_count_clone,
            }),
            config,
        ));

        // Sequence: ToolBlockStart (id+name known) → InputJsonDelta (complete args) → ToolCallComplete.
        // Without the fix, tool_meta is empty when InputJsonDelta arrives → dispatch never fires.
        let events = vec![
            ToolSseEvent::ToolBlockStart {
                index: 0,
                id: "toolu_01".into(),
                name: "bash".into(),
            },
            ToolSseEvent::InputJsonDelta {
                index: 0,
                delta: r#"{"command":"ls"}"#.into(),
            },
            ToolSseEvent::ToolCallComplete {
                index: 0,
                id: "toolu_01".into(),
                name: "bash".into(),
                full_json: r#"{"command":"ls"}"#.into(),
            },
        ];

        let drainer =
            SpeculativeStreamDrainer::new(Box::pin(tokio_stream::iter(events)), engine, 0.0);
        let result = drainer.drive().await.unwrap();
        // Drainer assembled a ToolUse response.
        assert!(matches!(result, ChatResponse::ToolUse { .. }));
        // SpyExec.is_tool_speculatable was called at least once (dispatch was attempted).
        assert!(
            dispatch_count.load(Ordering::Relaxed) > 0,
            "dispatch should have been attempted"
        );
    }

    #[tokio::test]
    async fn drive_empty_stream_returns_text_empty() {
        use std::sync::Arc;
        use zeph_config::tools::SpeculativeConfig;
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct NullExec;
        impl ToolExecutor for NullExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                false
            }
        }
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(NullExec),
            SpeculativeConfig::default(),
        ));
        let drainer = SpeculativeStreamDrainer::new(Box::pin(tokio_stream::empty()), engine, 0.8);
        let result = drainer.drive().await.unwrap();
        assert!(matches!(result, ChatResponse::Text(s) if s.is_empty()));
    }

    #[tokio::test]
    async fn drive_content_chunk_returns_text() {
        use std::sync::Arc;
        use zeph_config::tools::SpeculativeConfig;
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct NullExec;
        impl ToolExecutor for NullExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                false
            }
        }
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(NullExec),
            SpeculativeConfig::default(),
        ));
        let events = vec![ToolSseEvent::ContentChunk("Hello world".into())];
        let drainer =
            SpeculativeStreamDrainer::new(Box::pin(tokio_stream::iter(events)), engine, 0.8);
        let result = drainer.drive().await.unwrap();
        assert!(matches!(result, ChatResponse::Text(s) if s == "Hello world"));
    }

    #[tokio::test]
    async fn drive_tool_call_complete_returns_tool_use() {
        use std::sync::Arc;
        use zeph_config::tools::SpeculativeConfig;
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct NullExec;
        impl ToolExecutor for NullExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                false
            }
        }
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(NullExec),
            SpeculativeConfig::default(),
        ));
        let events = vec![ToolSseEvent::ToolCallComplete {
            index: 0,
            id: "toolu_01".into(),
            name: "bash".into(),
            full_json: r#"{"command":"ls"}"#.into(),
        }];
        let drainer =
            SpeculativeStreamDrainer::new(Box::pin(tokio_stream::iter(events)), engine, 0.8);
        let result = drainer.drive().await.unwrap();
        match result {
            ChatResponse::ToolUse { tool_calls, .. } => {
                assert_eq!(tool_calls.len(), 1);
                assert_eq!(tool_calls[0].id, "toolu_01");
                assert_eq!(tool_calls[0].name, "bash");
            }
            other @ ChatResponse::Text(_) => panic!("expected ToolUse, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn drive_error_event_propagates() {
        use std::sync::Arc;
        use zeph_config::tools::SpeculativeConfig;
        use zeph_tools::{ToolCall, ToolError, ToolExecutor, ToolOutput};

        struct NullExec;
        impl ToolExecutor for NullExec {
            async fn execute(&self, _: &str) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            async fn execute_tool_call(
                &self,
                _: &ToolCall,
            ) -> Result<Option<ToolOutput>, ToolError> {
                Ok(None)
            }
            fn is_tool_speculatable(&self, _: &str) -> bool {
                false
            }
        }
        let engine = Arc::new(super::super::SpeculationEngine::new(
            Arc::new(NullExec),
            SpeculativeConfig::default(),
        ));
        let events = vec![ToolSseEvent::Error(zeph_llm::LlmError::SseParse(
            "boom".into(),
        ))];
        let drainer =
            SpeculativeStreamDrainer::new(Box::pin(tokio_stream::iter(events)), engine, 0.8);
        let result = drainer.drive().await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("boom"));
    }
}