monoloop-connector 0.1.1

Abstract Connector contract, shared transport handles, FakeConnector, and ConnectorProxy
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
//! Deterministic FakeSessionAdapter and FakeConnectorFactory for lifecycle tests.

use crate::control::ControlDisposition;
use crate::fake::FakeConnector;
use crate::instance::{
    ConnectorBuildError, ConnectorFactory, ConnectorInstance, ConnectorInstanceId,
};
use crate::session::{
    validate_session_id_match, McpServerDescriptor, PendingOperationControl,
    PendingSessionAttachment, PendingSessionConfiguration, SessionAdapter, SessionAttachError,
    SessionAttachRequest, SessionAttachment, SessionAttachmentCompletion,
    SessionConfigurationCompletion, SessionConfigurationError, SessionRoute,
};
use monoloop_contracts::{ExternalSessionId, SessionConfig};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::Notify;
use uuid::Uuid;

/// Opaque fake route bound to one instance.
#[derive(Debug)]
pub struct FakeSessionRoute {
    owner: ConnectorInstanceId,
    /// Stable route token (tests only; not a secret).
    pub route_token: String,
}

impl FakeSessionRoute {
    /// Create a route for `owner`.
    pub fn new(owner: ConnectorInstanceId) -> Self {
        Self {
            owner,
            route_token: Uuid::new_v4().to_string(),
        }
    }
}

impl SessionRoute for FakeSessionRoute {
    fn owner(&self) -> &ConnectorInstanceId {
        &self.owner
    }
}

/// Shared control for a pending fake session operation.
struct FakePendingControl {
    cancel: AtomicBool,
    terminate: AtomicBool,
    terminal: AtomicBool,
    notify: Notify,
}

impl FakePendingControl {
    fn new() -> Arc<Self> {
        Arc::new(Self {
            cancel: AtomicBool::new(false),
            terminate: AtomicBool::new(false),
            terminal: AtomicBool::new(false),
            notify: Notify::new(),
        })
    }

    fn mark_terminal(&self) {
        self.terminal.store(true, Ordering::SeqCst);
    }

    async fn wait_interrupt_or_delay(&self, delay: Duration) -> Result<(), SessionAttachError> {
        if delay.is_zero() {
            if self.terminate.load(Ordering::SeqCst) {
                return Err(SessionAttachError::Terminated);
            }
            if self.cancel.load(Ordering::SeqCst) {
                return Err(SessionAttachError::Cancelled);
            }
            return Ok(());
        }
        tokio::select! {
            _ = tokio::time::sleep(delay) => {
                if self.terminate.load(Ordering::SeqCst) {
                    Err(SessionAttachError::Terminated)
                } else if self.cancel.load(Ordering::SeqCst) {
                    Err(SessionAttachError::Cancelled)
                } else {
                    Ok(())
                }
            }
            _ = self.interrupted() => {
                if self.terminate.load(Ordering::SeqCst) {
                    Err(SessionAttachError::Terminated)
                } else {
                    Err(SessionAttachError::Cancelled)
                }
            }
        }
    }

    async fn interrupted(&self) {
        loop {
            if self.cancel.load(Ordering::SeqCst) || self.terminate.load(Ordering::SeqCst) {
                return;
            }
            self.notify.notified().await;
        }
    }
}

impl PendingOperationControl for FakePendingControl {
    fn cancel(&self) -> ControlDisposition {
        if self.terminal.load(Ordering::SeqCst) {
            return ControlDisposition::AlreadyTerminal;
        }
        if self
            .cancel
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return ControlDisposition::AlreadyRequested;
        }
        self.notify.notify_waiters();
        ControlDisposition::Accepted
    }

    fn force_terminate(&self) -> ControlDisposition {
        if self.terminal.load(Ordering::SeqCst) {
            return ControlDisposition::AlreadyTerminal;
        }
        if self
            .terminate
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_err()
        {
            return ControlDisposition::AlreadyRequested;
        }
        self.notify.notify_waiters();
        ControlDisposition::Accepted
    }
}

/// Configuration for [`FakeSessionAdapter`].
#[derive(Clone, Debug)]
pub struct FakeSessionAdapterConfig {
    /// Artificial attach delay (cancel/terminate race tests).
    pub attach_delay: Duration,
    /// Fail attach with SessionFailed.
    pub fail_attach: bool,
    /// Maximum concurrent in-flight attach operations (0 = unlimited).
    pub max_in_flight: usize,
    /// When true, `begin_refresh_mcp` is unsupported.
    pub mcp_refresh_unsupported: bool,
}

impl Default for FakeSessionAdapterConfig {
    fn default() -> Self {
        Self {
            attach_delay: Duration::ZERO,
            fail_attach: false,
            max_in_flight: 64,
            mcp_refresh_unsupported: false,
        }
    }
}

struct SessionTable {
    by_id: HashMap<String, SessionConfig>,
}

struct AdapterState {
    owner: ConnectorInstanceId,
    config: FakeSessionAdapterConfig,
    table: Mutex<SessionTable>,
    in_flight: AtomicUsize,
    completed_attaches: AtomicUsize,
}

/// Deterministic session adapter for external-agent Channel tests.
pub struct FakeSessionAdapter {
    state: Arc<AdapterState>,
}

impl FakeSessionAdapter {
    /// Create an adapter owned by `owner`.
    pub fn new(owner: ConnectorInstanceId, config: FakeSessionAdapterConfig) -> Self {
        Self {
            state: Arc::new(AdapterState {
                owner,
                config,
                table: Mutex::new(SessionTable {
                    by_id: HashMap::new(),
                }),
                in_flight: AtomicUsize::new(0),
                completed_attaches: AtomicUsize::new(0),
            }),
        }
    }

    /// Owning instance id.
    pub fn owner(&self) -> &ConnectorInstanceId {
        &self.state.owner
    }

    /// Pre-register an existing session for load tests.
    pub fn register_existing(&self, session_id: &str, config: SessionConfig) {
        let mut t = self.state.table.lock().expect("session table");
        t.by_id.insert(session_id.to_string(), config);
    }

    /// Number of successfully completed attaches.
    pub fn completed_attaches(&self) -> usize {
        self.state.completed_attaches.load(Ordering::SeqCst)
    }

    /// Current in-flight attach operations.
    pub fn in_flight(&self) -> usize {
        self.state.in_flight.load(Ordering::SeqCst)
    }
}

impl SessionAdapter for FakeSessionAdapter {
    fn begin_attach(
        &self,
        request: SessionAttachRequest,
    ) -> Result<PendingSessionAttachment, SessionAttachError> {
        let max = self.state.config.max_in_flight;
        if max > 0 {
            let cur = self.state.in_flight.load(Ordering::SeqCst);
            if cur >= max {
                return Err(SessionAttachError::CapacityExceeded);
            }
        }
        self.state.in_flight.fetch_add(1, Ordering::SeqCst);

        let control = FakePendingControl::new();
        let control_api: Arc<dyn PendingOperationControl> = control.clone();
        let state = Arc::clone(&self.state);

        let completion: SessionAttachmentCompletion = Box::pin(async move {
            let result = run_attach(state.clone(), request, control.clone()).await;
            state.in_flight.fetch_sub(1, Ordering::SeqCst);
            if result.is_ok() {
                state.completed_attaches.fetch_add(1, Ordering::SeqCst);
            }
            control.mark_terminal();
            result
        });

        Ok(PendingSessionAttachment {
            control: control_api,
            completion,
        })
    }

    fn begin_refresh_mcp(
        &self,
        attachment: Arc<SessionAttachment>,
        descriptor: Option<McpServerDescriptor>,
    ) -> Result<PendingSessionConfiguration, SessionConfigurationError> {
        if attachment.owner != self.state.owner {
            return Err(SessionConfigurationError::OwnerMismatch);
        }
        if self.state.config.mcp_refresh_unsupported {
            return Err(SessionConfigurationError::Unsupported);
        }
        let control = FakePendingControl::new();
        let control_api: Arc<dyn PendingOperationControl> = control.clone();
        let completion: SessionConfigurationCompletion = Box::pin(async move {
            let _name = descriptor.as_ref().map(|d| d.server_name.clone());
            if control.terminate.load(Ordering::SeqCst) {
                control.mark_terminal();
                return Err(SessionConfigurationError::Terminated);
            }
            if control.cancel.load(Ordering::SeqCst) {
                control.mark_terminal();
                return Err(SessionConfigurationError::Cancelled);
            }
            control.mark_terminal();
            Ok(())
        });
        Ok(PendingSessionConfiguration {
            control: control_api,
            completion,
        })
    }
}

async fn run_attach(
    state: Arc<AdapterState>,
    request: SessionAttachRequest,
    control: Arc<FakePendingControl>,
) -> Result<Arc<SessionAttachment>, SessionAttachError> {
    let now = std::time::Instant::now();
    if now >= request.deadline {
        return Err(SessionAttachError::DeadlineExceeded);
    }

    control
        .wait_interrupt_or_delay(state.config.attach_delay)
        .await?;

    if state.config.fail_attach {
        return Err(SessionAttachError::SessionFailed);
    }

    let route = Arc::new(FakeSessionRoute::new(state.owner.clone()));
    if let Some(ref requested) = request.requested_session_id {
        // Explicit load only — never create a replacement session (D-013).
        let table = state.table.lock().expect("session table");
        let known = table
            .by_id
            .get(requested.as_str())
            .cloned()
            .ok_or(SessionAttachError::SessionFailed)?;
        if configs_conflict(&request.session_config, &known) {
            return Err(SessionAttachError::ConfigurationMismatch);
        }
        let external = ExternalSessionId::try_new(requested.as_str())
            .map_err(|_| SessionAttachError::SessionFailed)?;
        validate_session_id_match(Some(requested), &external)?;
        Ok(Arc::new(SessionAttachment::new(
            state.owner.clone(),
            external,
            known,
            route,
        )))
    } else {
        // Create: provisional placeholder; Connector returns authoritative id on open.
        let provisional = format!("fake-pending-{}", Uuid::new_v4());
        let external = ExternalSessionId::try_new(&provisional)
            .map_err(|_| SessionAttachError::SessionFailed)?;
        let effective = request.session_config.clone();
        // Reserve a create slot so concurrent reuse keys are distinct; open will
        // register the provider id (see FakeConnector create_mode).
        state
            .table
            .lock()
            .expect("session table")
            .by_id
            .insert(provisional, effective.clone());
        Ok(Arc::new(SessionAttachment::new_create(
            state.owner.clone(),
            external,
            effective,
            route,
            request.initial_mcp,
        )))
    }
}

fn configs_conflict(requested: &SessionConfig, known: &SessionConfig) -> bool {
    if let (Some(a), Some(b)) = (&requested.mode, &known.mode) {
        if a != b {
            return true;
        }
    }
    if let (Some(a), Some(b)) = (&requested.specialist_profile, &known.specialist_profile) {
        if a != b {
            return true;
        }
    }
    if let (Some(a), Some(b)) = (&requested.permission_profile, &known.permission_profile) {
        if a != b {
            return true;
        }
    }
    false
}

/// Factory producing a matched FakeConnector + FakeSessionAdapter instance.
pub struct FakeConnectorFactory {
    /// When true, instance includes a SessionAdapter (external agent).
    pub with_sessions: bool,
    session_config: FakeSessionAdapterConfig,
    connector_config: crate::fake::FakeConnectorConfig,
}

impl FakeConnectorFactory {
    /// Direct-LLM style (no session adapter).
    pub fn direct_llm() -> Self {
        Self {
            with_sessions: false,
            session_config: FakeSessionAdapterConfig::default(),
            connector_config: crate::fake::FakeConnectorConfig::default(),
        }
    }

    /// Direct-LLM with custom FakeConnector timing/endpoint config (tests).
    pub fn direct_llm_with_config(connector_config: crate::fake::FakeConnectorConfig) -> Self {
        Self {
            with_sessions: false,
            session_config: FakeSessionAdapterConfig::default(),
            connector_config,
        }
    }

    /// External-agent style with session adapter.
    pub fn external_agent(session_config: FakeSessionAdapterConfig) -> Self {
        Self {
            with_sessions: true,
            session_config,
            connector_config: crate::fake::FakeConnectorConfig::default(),
        }
    }
}

impl ConnectorFactory for FakeConnectorFactory {
    fn create(&self) -> Result<ConnectorInstance, ConnectorBuildError> {
        let instance_id = ConnectorInstanceId::generate();
        let connector = Arc::new(FakeConnector::with_instance_id_and_config(
            instance_id.clone(),
            self.connector_config.clone(),
        ));
        let sessions = if self.with_sessions {
            Some(Arc::new(FakeSessionAdapter::new(
                instance_id.clone(),
                self.session_config.clone(),
            )) as Arc<dyn SessionAdapter>)
        } else {
            None
        };
        Ok(ConnectorInstance::new(instance_id, connector, sessions))
    }
}

/// Build a pending attach whose completion sender is dropped (invariant path).
pub fn pending_attach_with_dropped_completion() -> PendingSessionAttachment {
    let control = FakePendingControl::new();
    let control_api: Arc<dyn PendingOperationControl> = control;
    let (tx, rx) =
        tokio::sync::oneshot::channel::<Result<Arc<SessionAttachment>, SessionAttachError>>();
    drop(tx);
    let completion: SessionAttachmentCompletion = Box::pin(async move {
        match rx.await {
            Ok(r) => r,
            Err(_) => Err(SessionAttachError::InvariantFailed),
        }
    });
    PendingSessionAttachment {
        control: control_api,
        completion,
    }
}