synaps-engine 0.7.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
//! Nonblocking, bounded dispatch of completed memory captures.

use super::chat_capture::{
    build_chat_turn_capture, build_conversation_summary_capture, CaptureBuildError,
    ChatTurnCapture, ConversationSummaryCapture, SummaryCaptureBuildError, TerminalTurnHistory,
};
use super::memory_context::{ContextProviderId, MemoryContextLease, RetentionClass, TurnCapture};
use std::collections::HashSet;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{mpsc, Arc};
use std::thread;

/// Outcome of an idempotency-key query after an ambiguous capture call.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptureCommitState {
    Committed,
    Absent,
}

/// Provider seam. Implementations must treat every capture id as an
/// idempotency key. Summary capture is additive so existing turn providers
/// keep source compatibility while adopting the C4 record class.
pub trait CaptureProvider: Send + Sync + 'static {
    fn capture(&self, capture: ChatTurnCapture) -> Result<(), CaptureFailure>;

    /// Query whether this idempotency key is already durable after an
    /// interrupted/ambiguous call. Providers that cannot query return false;
    /// no automatic blind retry is performed in either case.
    fn contains_capture(
        &self,
        _capture_id: &[u8; 32],
    ) -> Result<CaptureCommitState, CaptureFailure> {
        Ok(CaptureCommitState::Absent)
    }

    fn capture_summary(&self, _capture: ConversationSummaryCapture) -> Result<(), CaptureFailure> {
        Err(CaptureFailure {
            code: "summary_capture_unsupported",
        })
    }
}

/// Content-free failure metadata suitable for diagnostics.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CaptureFailure {
    pub code: &'static str,
}

enum CapturePayload {
    Turn(Box<ChatTurnCapture>),
    Summary(Box<ConversationSummaryCapture>),
}

impl CapturePayload {
    fn id_bytes(&self) -> [u8; 32] {
        match self {
            Self::Turn(capture) => *capture.capture_id.as_bytes(),
            Self::Summary(capture) => *capture.capture_id.as_bytes(),
        }
    }

    fn dispatch(&self, provider: &dyn CaptureProvider) -> Result<(), CaptureFailure> {
        match self {
            Self::Turn(capture) => provider.capture(capture.as_ref().clone()),
            Self::Summary(capture) => provider.capture_summary(capture.as_ref().clone()),
        }
    }

    fn query_committed(
        &self,
        provider: &dyn CaptureProvider,
    ) -> Result<CaptureCommitState, CaptureFailure> {
        match self {
            Self::Turn(capture) => provider.contains_capture(capture.capture_id.as_bytes()),
            // Summary retry/query support is additive and not part of the C5
            // terminal-turn crash gate.
            Self::Summary(_) => Ok(CaptureCommitState::Absent),
        }
    }
}

struct CaptureJob {
    provider_id: ContextProviderId,
    provider: Arc<dyn CaptureProvider>,
    payload: CapturePayload,
    /// Present for imports that may advance a durable host checkpoint only
    /// after the provider has acknowledged (or reconciled) the capture.
    committed: Option<mpsc::Sender<Result<(), CaptureFailure>>>,
}

/// Fixed-capacity worker. Enqueue is always `try_send`; persistence never runs
/// on the turn-completion or compaction-transition path.
pub struct CaptureWorker {
    sender: mpsc::SyncSender<CaptureJob>,
    dropped: Arc<AtomicU64>,
    failures: Arc<AtomicU64>,
}

impl CaptureWorker {
    pub fn new(capacity: usize) -> Self {
        assert!(capacity > 0, "capture queue capacity must be nonzero");
        let (sender, receiver) = mpsc::sync_channel::<CaptureJob>(capacity);
        let dropped = Arc::new(AtomicU64::new(0));
        let failures = Arc::new(AtomicU64::new(0));
        let worker_failures = Arc::clone(&failures);
        thread::Builder::new()
            .name("memory-capture".into())
            .spawn(move || {
                let mut submitted = HashSet::new();
                while let Ok(job) = receiver.recv() {
                    let id = (job.provider_id.clone(), job.payload.id_bytes());
                    let result = if submitted.contains(&id) {
                        Ok(())
                    } else if let Err(error) = job.payload.dispatch(job.provider.as_ref()) {
                        // The call may have committed before its acknowledgement
                        // was lost. Reconcile by idempotency key before accepting
                        // an explicit retry; never blindly dispatch it again.
                        match job.payload.query_committed(job.provider.as_ref()) {
                            Ok(CaptureCommitState::Committed) => {
                                submitted.insert(id);
                                Ok(())
                            }
                            Ok(CaptureCommitState::Absent) | Err(_) => {
                                worker_failures.fetch_add(1, Ordering::Relaxed);
                                Err(error)
                            }
                        }
                    } else {
                        submitted.insert(id);
                        Ok(())
                    };
                    if let Some(committed) = job.committed {
                        let _ = committed.send(result);
                    }
                }
            })
            .expect("capture worker spawn");
        Self {
            sender,
            dropped,
            failures,
        }
    }

    /// Apply lease capability and capture gates, build the bounded payload, and
    /// attempt a nonblocking enqueue. `Ok(false)` means capture was gated off.
    pub fn submit_terminal(
        &self,
        lease: &MemoryContextLease,
        history: TerminalTurnHistory,
        retention: RetentionClass,
        provider: Arc<dyn CaptureProvider>,
    ) -> Result<bool, CaptureBuildError> {
        if lease.mode.turn_capture() != TurnCapture::Enabled
            || lease.project_id != history.project_id
            || lease.session_id != history.session_id
        {
            return Ok(false);
        }
        let capture = build_chat_turn_capture(&lease.project_id, history, retention)?;
        let job = CaptureJob {
            provider_id: lease.provider_id.clone(),
            provider,
            payload: CapturePayload::Turn(Box::new(capture)),
            committed: None,
        };
        match self.sender.try_send(job) {
            Ok(()) => Ok(true),
            Err(mpsc::TrySendError::Full(_)) | Err(mpsc::TrySendError::Disconnected(_)) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
                Ok(true)
            }
        }
    }

    /// Submit a capture already built by the C1 builder. Historical import
    /// uses this narrow seam so it shares the same bounded C3 queue and exact
    /// leased provider dispatch as live terminal capture.
    pub(crate) fn submit_built(
        &self,
        lease: &MemoryContextLease,
        capture: ChatTurnCapture,
        provider: Arc<dyn CaptureProvider>,
    ) -> bool {
        self.submit_built_job(lease, capture, provider, None)
    }

    /// Submit one historical capture with a completion receipt. Waiting for the
    /// receipt happens only on the explicit import path, never turn completion.
    pub(crate) fn submit_built_with_receipt(
        &self,
        lease: &MemoryContextLease,
        capture: ChatTurnCapture,
        provider: Arc<dyn CaptureProvider>,
        committed: mpsc::Sender<Result<(), CaptureFailure>>,
    ) -> bool {
        self.submit_built_job(lease, capture, provider, Some(committed))
    }

    fn submit_built_job(
        &self,
        lease: &MemoryContextLease,
        capture: ChatTurnCapture,
        provider: Arc<dyn CaptureProvider>,
        committed: Option<mpsc::Sender<Result<(), CaptureFailure>>>,
    ) -> bool {
        if lease.mode.turn_capture() != TurnCapture::Enabled
            || lease.project_id != capture.project_id
        {
            return false;
        }
        let job = CaptureJob {
            provider_id: lease.provider_id.clone(),
            provider,
            payload: CapturePayload::Turn(Box::new(capture)),
            committed,
        };
        match self.sender.try_send(job) {
            Ok(()) => true,
            Err(mpsc::TrySendError::Full(_)) | Err(mpsc::TrySendError::Disconnected(_)) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
                false
            }
        }
    }

    /// Apply the same exact lease gate as turn capture, build a bounded
    /// first-class compaction memory, and attempt a nonblocking enqueue.
    #[allow(clippy::too_many_arguments)] // first-class summary metadata stays explicit at this boundary
    pub fn submit_summary(
        &self,
        lease: &MemoryContextLease,
        source: super::chat_capture::CompactionSource,
        source_message_count: usize,
        summary_text: &str,
        redaction_policy: agent_core::compaction::RedactionPolicy,
        retention: RetentionClass,
        provider: Arc<dyn CaptureProvider>,
    ) -> Result<bool, SummaryCaptureBuildError> {
        if lease.mode.turn_capture() != TurnCapture::Enabled
            || lease.project_id != source.project_id
        {
            return Ok(false);
        }
        let capture = build_conversation_summary_capture(
            &lease.project_id,
            source,
            source_message_count,
            summary_text,
            redaction_policy,
            retention,
        )?;
        let job = CaptureJob {
            provider_id: lease.provider_id.clone(),
            provider,
            payload: CapturePayload::Summary(Box::new(capture)),
            committed: None,
        };
        match self.sender.try_send(job) {
            Ok(()) => Ok(true),
            Err(mpsc::TrySendError::Full(_)) | Err(mpsc::TrySendError::Disconnected(_)) => {
                self.dropped.fetch_add(1, Ordering::Relaxed);
                Ok(true)
            }
        }
    }

    pub fn overflow_drops(&self) -> u64 {
        self.dropped.load(Ordering::Relaxed)
    }
    pub fn provider_failures(&self) -> u64 {
        self.failures.load(Ordering::Relaxed)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::chat_capture::{CanonicalCaptureItem, CaptureContentClass};
    use crate::runtime::memory_context::{
        mint_explicit_command_proof, CapturePolicy, MemoryContextMode, MemoryLeaseId, ProjectId,
        RecallPolicy, SessionId,
    };
    use agent_core::core::disclosure::DisclosureClass;
    use agent_core::TurnOutcome;
    use std::sync::atomic::AtomicBool;
    use std::sync::Mutex;
    use std::time::{Duration, Instant, SystemTime};

    struct ProviderDouble {
        calls: AtomicU64,
        queries: AtomicU64,
        fail: AtomicBool,
        committed: AtomicBool,
        block: Mutex<Option<mpsc::Receiver<()>>>,
    }

    impl CaptureProvider for ProviderDouble {
        fn capture(&self, _capture: ChatTurnCapture) -> Result<(), CaptureFailure> {
            self.calls.fetch_add(1, Ordering::SeqCst);
            if let Some(receiver) = self.block.lock().expect("lock").take() {
                let _ = receiver.recv();
            }
            if self.fail.load(Ordering::SeqCst) {
                Err(CaptureFailure {
                    code: "fixture_failure",
                })
            } else {
                self.committed.store(true, Ordering::SeqCst);
                Ok(())
            }
        }

        fn contains_capture(
            &self,
            _capture_id: &[u8; 32],
        ) -> Result<CaptureCommitState, CaptureFailure> {
            self.queries.fetch_add(1, Ordering::SeqCst);
            Ok(if self.committed.load(Ordering::SeqCst) {
                CaptureCommitState::Committed
            } else {
                CaptureCommitState::Absent
            })
        }
    }

    fn lease() -> MemoryContextLease {
        let now = SystemTime::now();
        MemoryContextLease::grant(
            MemoryLeaseId::parse("lease-c3").unwrap(),
            SessionId::parse("session-c3").unwrap(),
            ProjectId::parse("project-c3").unwrap(),
            ContextProviderId::parse("provider-c3").unwrap(),
            MemoryContextMode::CaptureOnly,
            CapturePolicy::EligibleTurnsOnly,
            RecallPolicy::BoundedPerPrompt,
            mint_explicit_command_proof(),
            now,
            None,
        )
        .unwrap()
    }

    fn history(turn: u64) -> TerminalTurnHistory {
        let project_id = ProjectId::parse("project-c3").unwrap();
        let started_at = SystemTime::UNIX_EPOCH + Duration::from_secs(turn);
        TerminalTurnHistory {
            project_id: project_id.clone(),
            session_id: SessionId::parse("session-c3").unwrap(),
            turn_id: super::super::memory_context::TurnId::parse(&format!("turn-{turn}")).unwrap(),
            turn_ordinal: turn,
            started_at,
            completed_at: started_at + Duration::from_millis(1),
            outcome: TurnOutcome::Completed,
            items: vec![
                CanonicalCaptureItem {
                    project_id: project_id.clone(),
                    class: CaptureContentClass::UserMessage,
                    disclosure: DisclosureClass::ModelVisible,
                    sensitivity: super::super::chat_capture::Sensitivity::Normal,
                    text: "user".into(),
                    tool_name: None,
                },
                CanonicalCaptureItem {
                    project_id,
                    class: CaptureContentClass::AssistantFinal,
                    disclosure: DisclosureClass::ModelVisible,
                    sensitivity: super::super::chat_capture::Sensitivity::Normal,
                    text: "assistant".into(),
                    tool_name: None,
                },
            ],
            compaction: None,
        }
    }

    fn wait_until(predicate: impl Fn() -> bool) {
        let deadline = Instant::now() + Duration::from_secs(1);
        while !predicate() && Instant::now() < deadline {
            thread::sleep(Duration::from_millis(2));
        }
        assert!(predicate());
    }

    #[test]
    fn memory_duplicate_capture_id_idempotent_at_engine_seam() {
        let provider = Arc::new(ProviderDouble {
            calls: AtomicU64::new(0),
            queries: AtomicU64::new(0),
            fail: AtomicBool::new(false),
            committed: AtomicBool::new(false),
            block: Mutex::new(None),
        });
        let worker = CaptureWorker::new(4);
        worker
            .submit_terminal(
                &lease(),
                history(1),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        worker
            .submit_terminal(
                &lease(),
                history(1),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        wait_until(|| provider.calls.load(Ordering::SeqCst) == 1);
        thread::sleep(Duration::from_millis(10));
        assert_eq!(provider.calls.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn memory_overflow_drops_exactly_and_never_blocks_completion() {
        let (release_tx, release_rx) = mpsc::channel();
        let provider = Arc::new(ProviderDouble {
            calls: AtomicU64::new(0),
            queries: AtomicU64::new(0),
            fail: AtomicBool::new(false),
            committed: AtomicBool::new(false),
            block: Mutex::new(Some(release_rx)),
        });
        let worker = CaptureWorker::new(1);
        worker
            .submit_terminal(
                &lease(),
                history(1),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        wait_until(|| provider.calls.load(Ordering::SeqCst) == 1);
        worker
            .submit_terminal(
                &lease(),
                history(2),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        let started = Instant::now();
        for turn in 3..103 {
            worker
                .submit_terminal(
                    &lease(),
                    history(turn),
                    RetentionClass::Standard,
                    provider.clone(),
                )
                .unwrap();
        }
        assert!(started.elapsed() < Duration::from_millis(50));
        assert_eq!(worker.overflow_drops(), 100);
        release_tx.send(()).unwrap();
    }

    #[test]
    fn memory_possibly_committed_capture_is_queried_before_retry() {
        struct AckLostAfterCommit {
            calls: AtomicU64,
            queries: AtomicU64,
            committed: AtomicBool,
        }
        impl CaptureProvider for AckLostAfterCommit {
            fn capture(&self, _capture: ChatTurnCapture) -> Result<(), CaptureFailure> {
                self.calls.fetch_add(1, Ordering::SeqCst);
                self.committed.store(true, Ordering::SeqCst);
                Err(CaptureFailure { code: "ack_lost" })
            }

            fn contains_capture(
                &self,
                _capture_id: &[u8; 32],
            ) -> Result<CaptureCommitState, CaptureFailure> {
                self.queries.fetch_add(1, Ordering::SeqCst);
                Ok(if self.committed.load(Ordering::SeqCst) {
                    CaptureCommitState::Committed
                } else {
                    CaptureCommitState::Absent
                })
            }
        }

        let provider = Arc::new(AckLostAfterCommit {
            calls: AtomicU64::new(0),
            queries: AtomicU64::new(0),
            committed: AtomicBool::new(false),
        });
        let worker = CaptureWorker::new(2);
        for _ in 0..2 {
            worker
                .submit_terminal(
                    &lease(),
                    history(99),
                    RetentionClass::Standard,
                    provider.clone(),
                )
                .unwrap();
            wait_until(|| provider.queries.load(Ordering::SeqCst) == 1);
        }
        thread::sleep(Duration::from_millis(10));
        assert_eq!(
            provider.calls.load(Ordering::SeqCst),
            1,
            "must not blindly retry"
        );
        assert_eq!(provider.queries.load(Ordering::SeqCst), 1);
        assert_eq!(worker.provider_failures(), 0, "query proved the commit");
    }

    #[test]
    fn memory_failed_capture_id_can_be_explicitly_retried() {
        let provider = Arc::new(ProviderDouble {
            calls: AtomicU64::new(0),
            queries: AtomicU64::new(0),
            fail: AtomicBool::new(true),
            committed: AtomicBool::new(false),
            block: Mutex::new(None),
        });
        let worker = CaptureWorker::new(2);
        worker
            .submit_terminal(
                &lease(),
                history(7),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        wait_until(|| worker.provider_failures() == 1);
        provider.fail.store(false, Ordering::SeqCst);
        worker
            .submit_terminal(
                &lease(),
                history(7),
                RetentionClass::Standard,
                provider.clone(),
            )
            .unwrap();
        wait_until(|| provider.calls.load(Ordering::SeqCst) == 2);
    }

    #[test]
    fn memory_provider_capture_failure_leaves_typed_terminal_turn_completed() {
        let provider = Arc::new(ProviderDouble {
            calls: AtomicU64::new(0),
            queries: AtomicU64::new(0),
            fail: AtomicBool::new(true),
            committed: AtomicBool::new(false),
            block: Mutex::new(None),
        });
        let worker = CaptureWorker::new(1);
        let terminal = history(1);
        assert_eq!(terminal.outcome, TurnOutcome::Completed);
        assert!(worker
            .submit_terminal(
                &lease(),
                terminal,
                RetentionClass::Standard,
                provider.clone()
            )
            .unwrap());
        wait_until(|| worker.provider_failures() == 1);
    }
}