synaps-engine 0.10.1

Runtime engine — streaming, tools, MCP, skills, extensions, sidecar
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
//! Actor-side session driver state machine (E §2).
//!
//! `DriverState` lives on `SessionActor.driver`. It observes `StreamEvent`s the
//! actor already receives and issues the actor's own `start_turn`/`steer`/`cancel`.
//! Nothing here touches a client, a terminal, or `App`. Clients see only
//! `SessionEventWire::Driver*` events.

use std::collections::VecDeque;
use std::sync::Arc;
use std::time::{Duration, Instant};

use crate::extensions::feedback;
use crate::extensions::runtime::{ExtensionHandler, ExtensionHealth};
use crate::extensions::session_driver::{
    Grant, Outcome, PollRequest, PrepareError, Proposal, Reply, Selection,
};
use crate::{CancellationToken, Runtime, SharedMessage};

type Workers = Arc<std::sync::Mutex<crate::runtime::subagent::SubagentRegistry>>;
type Manager = Arc<tokio::sync::RwLock<crate::extensions::manager::ExtensionManager>>;

pub(crate) fn cancel_workers(workers: &Workers, epoch: u64) {
    workers
        .lock()
        .unwrap_or_else(|p| p.into_inner())
        .cancel_spawn_epoch(epoch);
}

// ── Abort-on-drop task wrapper ───────────────────────────────────────────────

pub(crate) struct Task<T>(pub tokio::task::JoinHandle<T>);
impl<T> Drop for Task<T> {
    fn drop(&mut self) {
        self.0.abort();
    }
}

// ── Scheduled proposal ──────────────────────────────────────────────────────

pub(crate) struct Scheduled {
    pub proposal: Proposal,
    pub due: Instant,
}

// ── Pending async work ──────────────────────────────────────────────────────

pub(crate) struct DriverPending {
    pub generation: u64,
    pub session_id: String,
    pub task: Task<TaskResult>,
}

pub(crate) enum TaskResult {
    Command {
        owner: String,
        #[allow(dead_code)] // stored for diagnostics, matched as `_`
        command: String,
        handler: Arc<dyn ExtensionHandler>,
        handler_generation: Option<u64>,
        result: Result<serde_json::Value, String>,
        report: crate::extensions::invoke_output::InvokeOutputReport,
    },
    Poll(Result<Reply, String>),
    Prepared {
        proposal: Proposal,
        result: Box<Result<Runtime, PrepareError>>,
    },
    Started(Result<super::actor::ActiveStream, String>),
}

// ── Core state ──────────────────────────────────────────────────────────────

/// The driver state lives on the actor; dropping it cancels all owned work.
pub(crate) struct DriverState {
    pub grant: Grant,
    pub handler: Arc<dyn ExtensionHandler>,
    pub handler_generation: u64,
    pub cancel: CancellationToken,
    pub workers: Workers,
    pub worker_epoch: u64,
    #[allow(dead_code)] // consumed by P7 deadline enforcement
    pub deadline_task: Option<Task<()>>,
    pub proposal: Option<Scheduled>,
    pub selection: Selection,
    pub awaiting_terminal: bool,
    pub outcome: Option<(Outcome, String)>,
    pub feedback: feedback::Tracker,
    pub completed_feedback: &'static str,
    /// Only explicit human submissions, never draft text. Entries remain until
    /// delivery acknowledgement or an atomic next-turn commit. Bounded: 16 msgs
    /// / 256 KiB total UTF-8 bytes (§2).
    pub steering: VecDeque<String>,
    pub auto_wakes_blocked: bool,
    #[allow(dead_code)] // consumed by P7 cost gate
    pub cost_at_arm: f64,
}

impl Drop for DriverState {
    fn drop(&mut self) {
        self.cancel.cancel();
        cancel_workers(&self.workers, self.worker_epoch);
    }
}

// ── Steering bounds ─────────────────────────────────────────────────────────

pub(crate) const STEERING_MAX_MESSAGES: usize = 16;
pub(crate) const STEERING_MAX_BYTES: usize = 256 * 1024;

// ── Lifecycle helpers ───────────────────────────────────────────────────────

pub(crate) fn live_generation(handler: &Arc<dyn ExtensionHandler>) -> Result<u64, String> {
    handler
        .lifecycle_snapshot()
        .filter(|s| {
            matches!(
                s.health,
                ExtensionHealth::Running | ExtensionHealth::Degraded
            )
        })
        .map(|s| s.generation)
        .ok_or_else(|| "owning extension is not live or its lifecycle is unavailable".into())
}

pub(crate) fn same_lifecycle(
    handler: &Arc<dyn ExtensionHandler>,
    generation: u64,
) -> Result<(), String> {
    if live_generation(handler)? == generation {
        Ok(())
    } else {
        Err("owning extension process restarted or transport was lost".into())
    }
}

pub(crate) fn same_handler(
    manager: &Manager,
    owner: &str,
    handler: &Arc<dyn ExtensionHandler>,
    generation: u64,
) -> Result<(), String> {
    let current = manager
        .try_read()
        .map_err(|_| "extension lifecycle busy".to_string())?
        .session_driver_handler(owner)?;
    if Arc::ptr_eq(&current, handler) {
        same_lifecycle(handler, generation)
    } else {
        Err("owning extension was replaced".into())
    }
}

pub(crate) fn schedule(active: &mut DriverState, proposal: Proposal) -> Result<(), String> {
    if proposal.delay < Duration::from_secs(1) || proposal.delay > Duration::from_secs(300) {
        return Err("invalid proposal delay".into());
    }
    let due = Instant::now() + proposal.delay;
    if active
        .grant
        .deadline()
        .is_some_and(|deadline| due >= deadline)
    {
        return Err("proposal would run past the grant deadline".into());
    }
    active.selection = proposal.selection.clone();
    active.proposal = Some(Scheduled { proposal, due });
    Ok(())
}

/// Build the pending-turn history by appending undelivered steering to
/// the current api_messages snapshot — read-only, never drains the queue.
pub(crate) fn history_with_steering(
    api_messages: &[SharedMessage],
    steering: &VecDeque<String>,
) -> Vec<SharedMessage> {
    let mut history = api_messages.to_vec();
    for text in steering {
        history.push(Arc::new(serde_json::json!({"role": "user", "content": text})));
    }
    history
}

/// Drain queued steering into api_messages before a driver turn starts.
/// An interrupted previous turn is already recorded in `api_messages` (its
/// partial history + the interruption marker), so the prompt is appended
/// verbatim — nothing is folded into it.
pub(crate) fn commit_submission(
    api_messages: &mut Vec<SharedMessage>,
    steering: &mut VecDeque<String>,
    prompt: &str,
) {
    for text in steering.drain(..) {
        api_messages.push(Arc::new(serde_json::json!({"role": "user", "content": text})));
    }
    api_messages.push(Arc::new(serde_json::json!({"role": "user", "content": prompt})));
}

/// Build the PollRequest for the next poll.
pub(crate) fn poll_request(
    active: &DriverState,
    outcome: Outcome,
    error_kind: String,
    session_cost_so_far: f64,
) -> PollRequest {
    PollRequest {
        run_id: active.grant.run_id.clone(),
        decision_id: uuid::Uuid::new_v4().to_string(),
        outcome,
        error_kind,
        model: active.selection.model.clone(),
        effort: active.selection.effort.clone(),
        feedback: active.grant.feedback_enabled().then(|| {
            if outcome == Outcome::Success {
                active.completed_feedback
            } else {
                "unknown"
            }
            .into()
        }),
        session_id: Some(active.grant.session_id.clone()),
        // (E-P7, §S3) let the plugin see the running spend.
        session_cost_so_far: Some(session_cost_so_far),
    }
}

/// Deadline remaining in ms, for wire events.
pub(crate) fn deadline_ms(grant: &Grant) -> Option<u64> {
    grant.deadline().map(|d| {
        d.saturating_duration_since(Instant::now())
            .as_millis()
            .min(u64::MAX as u128) as u64
    })
}

// ── P5: terminal classification ─────────────────────────────────────────────

/// Classified end of a driver-owned turn. Only a final `Done` is `Success`.
#[derive(Debug)]
pub(crate) enum Terminal {
    Success,
    Failure(Outcome, String),
    /// Cancel/unexpected-EOF: revoke WITHOUT producing an outcome for polling.
    Canceled,
}

/// Capture the terminal before the stream handler consumes it. Called for
/// `Done`, `Error`, or `None` (EOF / stream dropped).
pub(crate) fn capture_terminal(
    event: Option<&crate::StreamEvent>,
    canceled: bool,
) -> Option<Terminal> {
    use crate::{SessionEvent, StreamEvent};
    use crate::extensions::session_driver::{classify_turn_error, Outcome as O};
    if canceled {
        return Some(Terminal::Canceled);
    }
    match event {
        Some(StreamEvent::Session(SessionEvent::Done)) => Some(Terminal::Success),
        Some(StreamEvent::Session(SessionEvent::Error(error))) => {
            if matches!(error.outcome, agent_core::TurnOutcome::Canceled) {
                return Some(Terminal::Canceled);
            }
            let (outcome, kind) = classify_turn_error(error);
            Some(Terminal::Failure(outcome, kind))
        }
        None => Some(Terminal::Failure(O::Blocked, "unknown".into())),
        _ => None,
    }
}

/// Apply the terminal to the driver state. Sets `outcome` for Success /
/// classified failure. Revokes on Blocked, Canceled, or idle conflict.
/// Returns `Some(reason)` if the caller must revoke.
pub(crate) fn observe_terminal(
    driver: &mut DriverState,
    runtime: &crate::Runtime,
    terminal: Terminal,
) -> Option<String> {
    if !driver.awaiting_terminal {
        return None;
    }
    driver.awaiting_terminal = false;
    match terminal {
        Terminal::Canceled => {
            return Some("canceled".into());
        }
        Terminal::Success => {
            driver.completed_feedback = if driver.grant.feedback_enabled() {
                driver.feedback.finish()
            } else {
                "unknown"
            };
            driver.outcome = Some((Outcome::Success, "none".into()));
        }
        Terminal::Failure(Outcome::TimeCheckpoint, kind)
            if driver.grant.time_checkpoints_enabled()
                && !runtime.turn_budget().max_elapsed.is_zero() =>
        {
            driver.completed_feedback = "unknown";
            driver.feedback = feedback::Tracker::default();
            driver.outcome = Some((Outcome::TimeCheckpoint, kind));
        }
        Terminal::Failure(Outcome::TimeCheckpoint, _) => {
            return Some("blocked or unexpected end of stream; explicit user action required".into());
        }
        Terminal::Failure(outcome, kind) => {
            if matches!(outcome, Outcome::Blocked) {
                return Some("blocked or unexpected end of stream; explicit user action required".into());
            }
            driver.outcome = Some((outcome, kind));
        }
    }
    None
}

// ── F11: pure-fn unit tests ────────────────────────────────────────────────

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

    fn test_selection() -> Selection {
        Selection {
            model: "test/model".into(),
            effort: "high".into(),
        }
    }

    fn test_reply() -> Reply {
        Reply::Start {
            run_id: "test-run".into(),
            models: vec![test_selection()],
            prompt: "do the work".into(),
            delay_ms: 2000,
            max_duration_ms: Some(120_000),
            feedback_version: Some(1),
            time_checkpoint_version: Some(1),
            context_mode: None,
            notice: "test notice".into(),
            max_cost_usd: None,
        }
    }

    fn test_grant_and_state() -> (Grant, DriverState) {
        let reply = test_reply();
        let (grant, proposal) =
            Grant::from_start("test-plugin", "test-session", reply).unwrap();
        let state = DriverState {
            selection: proposal.selection.clone(),
            grant,
            handler: std::sync::Arc::new(crate::extensions::runtime::tests::DummyHandler),
            handler_generation: 0,
            cancel: CancellationToken::new(),
            workers: Arc::new(std::sync::Mutex::new(
                crate::runtime::subagent::SubagentRegistry::new(),
            )),
            worker_epoch: 0,
            deadline_task: None,
            proposal: Some(Scheduled { proposal, due: Instant::now() + Duration::from_secs(2) }),
            awaiting_terminal: false,
            outcome: None,
            feedback: feedback::Tracker::default(),
            completed_feedback: "unknown",
            steering: VecDeque::new(),
            auto_wakes_blocked: false,
            cost_at_arm: 0.0,
        };
        let grant_copy = Grant::from_start(
            "test-plugin",
            "test-session",
            test_reply(),
        )
        .unwrap()
        .0;
        (grant_copy, state)
    }

    // ── schedule tests ──────────────────────────────────────────────────

    #[test]
    fn schedule_rejects_too_short_delay() {
        let (_, mut state) = test_grant_and_state();
        let proposal = crate::extensions::session_driver::Proposal {
            selection: test_selection(),
            prompt: "x".into(),
            delay: Duration::from_millis(500),
            notice: String::new(),
        };
        assert!(schedule(&mut state, proposal).is_err());
    }

    #[test]
    fn schedule_rejects_past_deadline() {
        let (_, mut state) = test_grant_and_state();
        // Proposal delay exceeds the grant's max_duration_ms (120 s).
        let proposal = crate::extensions::session_driver::Proposal {
            selection: test_selection(),
            prompt: "x".into(),
            delay: Duration::from_secs(300),
            notice: String::new(),
        };
        assert!(schedule(&mut state, proposal).is_err());
    }

    #[test]
    fn schedule_accepts_valid_proposal() {
        let (_, mut state) = test_grant_and_state();
        let proposal = crate::extensions::session_driver::Proposal {
            selection: Selection {
                model: "other/model".into(),
                effort: "low".into(),
            },
            prompt: "continue".into(),
            delay: Duration::from_secs(2),
            notice: "notice".into(),
        };
        assert!(schedule(&mut state, proposal).is_ok());
        assert_eq!(state.selection.model, "other/model");
        assert!(state.proposal.is_some());
    }

    // ── poll_request ────────────────────────────────────────────────────

    #[test]
    fn poll_request_includes_feedback_when_enabled() {
        let (_, state) = test_grant_and_state();
        let req = poll_request(
            &state,
            crate::extensions::session_driver::Outcome::Success,
            "none".into(),
            1.25,
        );
        assert!(req.feedback.is_some());
        assert_eq!(req.run_id, "test-run");
        assert!(req.session_id.is_some());
        // (E-P7, §S3) running spend is reported to the plugin.
        assert_eq!(req.session_cost_so_far, Some(1.25));
    }

    // ── same_lifecycle ──────────────────────────────────────────────────

    #[test]
    fn same_lifecycle_matches_same_generation() {
        let handler: Arc<dyn crate::extensions::runtime::ExtensionHandler> =
            Arc::new(crate::extensions::runtime::tests::DummyHandler);
        let gen = live_generation(&handler).unwrap();
        assert!(same_lifecycle(&handler, gen).is_ok());
    }

    #[test]
    fn same_lifecycle_rejects_different_generation() {
        let handler: Arc<dyn crate::extensions::runtime::ExtensionHandler> =
            Arc::new(crate::extensions::runtime::tests::DummyHandler);
        assert!(same_lifecycle(&handler, 999).is_err());
    }

    // ── history_with_steering ───────────────────────────────────────────

    #[test]
    fn history_with_steering_appends_user_messages() {
        let base: Vec<SharedMessage> =
            vec![Arc::new(serde_json::json!({"role":"assistant","content":"hi"}))];
        let mut steering = VecDeque::new();
        steering.push_back("one".into());
        steering.push_back("two".into());
        let result = history_with_steering(&base, &steering);
        assert_eq!(result.len(), 3);
        assert_eq!(result[1]["role"], "user");
        assert_eq!(result[1]["content"], "one");
        assert_eq!(result[2]["content"], "two");
    }

    // ── commit_submission ───────────────────────────────────────────────

    #[test]
    fn commit_submission_drains_steering_and_appends_prompt() {
        let mut messages: Vec<SharedMessage> = Vec::new();
        let mut steering = VecDeque::new();
        steering.push_back("steer1".into());
        steering.push_back("steer2".into());
        commit_submission(&mut messages, &mut steering, "go");
        assert!(steering.is_empty());
        assert_eq!(messages.len(), 3);
        assert_eq!(messages[0]["content"], "steer1");
        assert_eq!(messages[1]["content"], "steer2");
        assert_eq!(messages[2]["content"], "go");
    }

    // ── capture_terminal / observe_terminal ─────────────────────────────

    #[test]
    fn capture_terminal_done_is_success() {
        let event = crate::StreamEvent::Session(crate::SessionEvent::Done);
        match capture_terminal(Some(&event), false) {
            Some(Terminal::Success) => {}
            other => panic!("expected Success, got: {other:?}"),
        }
    }

    #[test]
    fn capture_terminal_canceled_flag() {
        let event = crate::StreamEvent::Session(crate::SessionEvent::Done);
        match capture_terminal(Some(&event), true) {
            Some(Terminal::Canceled) => {}
            other => panic!("expected Canceled, got: {other:?}"),
        }
    }

    #[test]
    fn capture_terminal_eof_is_blocked() {
        match capture_terminal(None, false) {
            Some(Terminal::Failure(Outcome::Blocked, _)) => {}
            other => panic!("expected Blocked, got: {other:?}"),
        }
    }

    #[test]
    fn capture_terminal_error_with_canceled_outcome() {
        let error = agent_core::TurnError {
            message: "canceled".into(),
            outcome: agent_core::TurnOutcome::Canceled,
        };
        let event = crate::StreamEvent::Session(crate::SessionEvent::Error(error));
        match capture_terminal(Some(&event), false) {
            Some(Terminal::Canceled) => {}
            other => panic!("expected Canceled from TurnOutcome::Canceled, got: {other:?}"),
        }
    }

    #[test]
    fn capture_terminal_llm_event_is_none() {
        let event = crate::StreamEvent::Llm(crate::LlmEvent::Text("hi".into()));
        assert!(capture_terminal(Some(&event), false).is_none());
    }

    #[test]
    fn observe_terminal_success_sets_outcome() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = true;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let result = observe_terminal(&mut state, &rt, Terminal::Success);
            assert!(result.is_none(), "success should not revoke");
            assert!(state.outcome.is_some());
            let (outcome, _) = state.outcome.clone().unwrap();
            assert_eq!(outcome, Outcome::Success);
        });
    }

    #[test]
    fn observe_terminal_canceled_returns_revoke_reason() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = true;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let result = observe_terminal(&mut state, &rt, Terminal::Canceled);
            assert!(result.is_some());
            assert!(result.unwrap().contains("canceled"));
            assert!(state.outcome.is_none());
        });
    }

    #[test]
    fn observe_terminal_blocked_returns_revoke_reason() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = true;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let result = observe_terminal(
                &mut state,
                &rt,
                Terminal::Failure(Outcome::Blocked, "unknown".into()),
            );
            assert!(result.is_some());
            assert!(result.unwrap().contains("blocked"));
        });
    }

    #[test]
    fn observe_terminal_provider_error_sets_outcome() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = true;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let result = observe_terminal(
                &mut state,
                &rt,
                Terminal::Failure(Outcome::ProviderError, "transient".into()),
            );
            assert!(result.is_none(), "provider error should not revoke");
            let (outcome, kind) = state.outcome.clone().unwrap();
            assert_eq!(outcome, Outcome::ProviderError);
            assert_eq!(kind, "transient");
        });
    }

    #[test]
    fn observe_terminal_not_awaiting_is_noop() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = false;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let result = observe_terminal(&mut state, &rt, Terminal::Success);
            assert!(result.is_none());
            assert!(state.outcome.is_none());
        });
    }

    #[test]
    fn observe_terminal_only_fires_once() {
        let (_, mut state) = test_grant_and_state();
        state.awaiting_terminal = true;
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let rt = crate::Runtime::new().await.unwrap();
            let r1 = observe_terminal(&mut state, &rt, Terminal::Success);
            assert!(r1.is_none());
            assert!(state.outcome.is_some());
            // Second call should be no-op (awaiting_terminal is false now).
            state.outcome = None;
            let r2 = observe_terminal(&mut state, &rt, Terminal::Success);
            assert!(r2.is_none());
            assert!(state.outcome.is_none());
        });
    }
}