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
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
//! Deterministic in-memory FakeConnector for tests and architecture gates.

use crate::control::{ConnectionControlHandle, ControlState};
use crate::descriptor::ConnectorDescriptor;
use crate::handles::{
    ConnectionCompletionHandle, ConnectionEndKind, ConnectionOwner, EndInitiator, RawInputHandle,
    RawInputMessage, RawOutputHandle,
};
use crate::instance::ConnectorInstanceId;
use crate::open::{OpenConnection, OpenedRawConnection, PendingRawConnection};
use crate::session::validate_open_attachment_owner;
use crate::traits::Connector;
use bytes::Bytes;
use monoloop_contracts::{
    ConnectionId, ConnectorError, ConnectorErrorKind, DialectBinding, DialectDescriptor,
    ExternalSessionId,
};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use tokio::sync::{mpsc, oneshot};
use tokio::time::timeout;

/// Behaviour of a fake endpoint.
#[derive(Clone, Debug)]
pub enum FakeEndpoint {
    /// Echo each input chunk back on output; finish closes output after drain.
    Echo,
    /// Scripted output chunks delivered after open (independent of input).
    Scripted {
        /// Chunks to emit on output in order.
        chunks: Vec<Bytes>,
    },
    /// Pair two connection opens with the same `pair_key`: A's input → B's output.
    Pair {
        /// Shared pairing key.
        pair_key: String,
    },
    /// Accept input but never produce output until cancel/terminate (D-012 response-wait).
    Hang,
}

/// Configuration for [`FakeConnector`].
#[derive(Clone, Debug)]
pub struct FakeConnectorConfig {
    /// Default endpoint when `endpoint_ref` is empty or `"default"`.
    pub default_endpoint: FakeEndpoint,
    /// Named endpoints keyed by `OpenConnection.endpoint_ref`.
    pub endpoints: HashMap<String, FakeEndpoint>,
    /// Artificial open delay (tests cancel-during-open).
    pub open_delay: Duration,
    /// If true, open fails with connection_failed.
    pub fail_open: bool,
}

impl Default for FakeConnectorConfig {
    fn default() -> Self {
        Self {
            default_endpoint: FakeEndpoint::Echo,
            endpoints: HashMap::new(),
            open_delay: Duration::ZERO,
            fail_open: false,
        }
    }
}

#[derive(Default)]
struct PairState {
    waiting: HashMap<String, PairHalf>,
}

struct PairHalf {
    out_tx: mpsc::Sender<Bytes>,
    ready: oneshot::Sender<mpsc::Sender<Bytes>>,
}

/// Deterministic connector: no network, no semantic parsing.
pub struct FakeConnector {
    descriptor: ConnectorDescriptor,
    config: FakeConnectorConfig,
    pairs: Arc<Mutex<PairState>>,
    /// Instance identity for session-attachment ownership checks.
    instance_id: ConnectorInstanceId,
}

impl FakeConnector {
    /// Create a fake connector with the given config (fresh instance id).
    pub fn new(config: FakeConnectorConfig) -> Self {
        Self::with_instance_id_and_config(ConnectorInstanceId::generate(), config)
    }

    /// Create with an explicit instance id (matched SessionAdapter ownership).
    pub fn with_instance_id(instance_id: ConnectorInstanceId) -> Self {
        Self::with_instance_id_and_config(instance_id, FakeConnectorConfig::default())
    }

    /// Create with instance id and config.
    pub fn with_instance_id_and_config(
        instance_id: ConnectorInstanceId,
        config: FakeConnectorConfig,
    ) -> Self {
        Self {
            descriptor: ConnectorDescriptor::fake(),
            config,
            pairs: Arc::new(Mutex::new(PairState::default())),
            instance_id,
        }
    }

    /// Echo-only defaults.
    pub fn echo() -> Self {
        Self::new(FakeConnectorConfig::default())
    }

    /// Borrow instance id.
    pub fn instance_id(&self) -> &ConnectorInstanceId {
        &self.instance_id
    }

    fn resolve_endpoint(&self, endpoint_ref: &str) -> FakeEndpoint {
        if endpoint_ref.is_empty() || endpoint_ref == "default" {
            return self.config.default_endpoint.clone();
        }
        self.config
            .endpoints
            .get(endpoint_ref)
            .cloned()
            .unwrap_or_else(|| self.config.default_endpoint.clone())
    }
}

impl Connector for FakeConnector {
    fn descriptor(&self) -> &ConnectorDescriptor {
        &self.descriptor
    }

    fn begin_open(&self, request: OpenConnection) -> PendingRawConnection {
        let control_state = ControlState::new();
        let control = ConnectionControlHandle::new(Arc::clone(&control_state));
        let connection_id = request.connection_id.clone();
        let config = self.config.clone();
        let endpoint = self.resolve_endpoint(&request.endpoint_ref);
        let pairs = Arc::clone(&self.pairs);
        let control_for_open = control.clone();
        let control_state_for_open = Arc::clone(&control_state);
        let instance_id = self.instance_id.clone();

        let opened = Box::pin(async move {
            open_fake(
                request,
                config,
                endpoint,
                pairs,
                control_for_open,
                control_state_for_open,
                instance_id,
            )
            .await
        });

        PendingRawConnection {
            connection_id,
            control,
            opened,
        }
    }
}

async fn open_fake(
    request: OpenConnection,
    config: FakeConnectorConfig,
    endpoint: FakeEndpoint,
    pairs: Arc<Mutex<PairState>>,
    control: ConnectionControlHandle,
    control_state: Arc<ControlState>,
    instance_id: ConnectorInstanceId,
) -> Result<OpenedRawConnection, ConnectorError> {
    validate_open_attachment_owner(&instance_id, request.session_attachment.as_deref())?;

    if config.fail_open {
        return Err(
            ConnectorError::connection_failed("fake configured to fail open")
                .with_connection_id(request.connection_id.as_str()),
        );
    }

    if config.open_delay > Duration::ZERO {
        tokio::select! {
            _ = tokio::time::sleep(config.open_delay) => {}
            _ = control.interrupted() => {
                return Err(control
                    .interrupt_error()
                    .unwrap_or_else(ConnectorError::cancelled)
                    .with_connection_id(request.connection_id.as_str()));
            }
        }
    }

    if let Some(err) = control.interrupt_error() {
        return Err(err.with_connection_id(request.connection_id.as_str()));
    }

    let buf = request.limits.buffers.max_queued_input_bytes.max(1);
    // Channel capacity in messages (approx); enforce chunk size on send.
    let capacity = (buf / request.limits.buffers.max_chunk_bytes.max(1)).max(1);

    let (in_tx, in_rx) = mpsc::channel::<RawInputMessage>(capacity);
    let (out_tx, out_rx) = mpsc::channel::<Bytes>(capacity);
    let (end_tx, end_rx) = oneshot::channel();

    let connection_id = request.connection_id.clone();
    let max_chunk = request.limits.buffers.max_chunk_bytes;
    // D-013: create_mode → allocate authoritative id; load uses attachment/request id.
    let external_session_id = if request
        .session_attachment
        .as_ref()
        .is_some_and(|a| a.create_mode)
    {
        Some(ExternalSessionId::new(format!(
            "fake-created-{}",
            uuid::Uuid::new_v4()
        )))
    } else {
        request
            .session_attachment
            .as_ref()
            .map(|a| a.external_session_id.clone())
            .or(request.external_session_id.clone())
    };

    let input = RawInputHandle::new(
        connection_id.clone(),
        in_tx,
        Arc::clone(&control_state),
        max_chunk,
    );
    let output = Arc::new(RawOutputHandle::new(
        connection_id.clone(),
        out_rx,
        Arc::clone(&control_state),
    ));
    let completion = ConnectionCompletionHandle::new(end_rx);

    match endpoint {
        FakeEndpoint::Echo => {
            tokio::spawn(run_echo_owner(
                connection_id.clone(),
                control_state,
                in_rx,
                out_tx,
                end_tx,
            ));
        }
        FakeEndpoint::Scripted { chunks } => {
            tokio::spawn(run_scripted_owner(
                connection_id.clone(),
                control_state,
                in_rx,
                out_tx,
                end_tx,
                chunks,
            ));
        }
        FakeEndpoint::Pair { pair_key } => {
            let peer_tx = register_pair(&pairs, &pair_key, out_tx.clone()).await?;
            tokio::spawn(run_pair_owner(
                connection_id.clone(),
                control_state,
                in_rx,
                peer_tx,
                out_tx,
                end_tx,
            ));
        }
        FakeEndpoint::Hang => {
            // Never emit output; hold until local cancel/terminate.
            drop(out_tx);
            tokio::spawn(run_hang_owner(
                connection_id.clone(),
                control_state,
                in_rx,
                end_tx,
            ));
        }
    }

    Ok(OpenedRawConnection {
        connection_id,
        external_session_id,
        dialect: DialectBinding::fixed(DialectDescriptor::test_raw()),
        input,
        output,
        control,
        completion,
    })
}

async fn register_pair(
    pairs: &Arc<Mutex<PairState>>,
    pair_key: &str,
    my_out_tx: mpsc::Sender<Bytes>,
) -> Result<mpsc::Sender<Bytes>, ConnectorError> {
    let waiter = {
        let mut state = pairs.lock().map_err(|_| {
            ConnectorError::new(ConnectorErrorKind::InvariantViolation, "pair lock")
        })?;
        if let Some(half) = state.waiting.remove(pair_key) {
            // Second half: give first half our out_tx; take their out_tx as peer.
            let _ = half.ready.send(my_out_tx);
            return Ok(half.out_tx);
        }
        let (ready_tx, ready_rx) = oneshot::channel();
        state.waiting.insert(
            pair_key.to_string(),
            PairHalf {
                out_tx: my_out_tx,
                ready: ready_tx,
            },
        );
        ready_rx
    };
    timeout(Duration::from_secs(5), waiter)
        .await
        .map_err(|_| ConnectorError::connection_failed("pair timeout"))?
        .map_err(|_| ConnectorError::connection_failed("pair cancelled"))
}

async fn run_echo_owner(
    connection_id: ConnectionId,
    control: Arc<ControlState>,
    mut in_rx: mpsc::Receiver<RawInputMessage>,
    out_tx: mpsc::Sender<Bytes>,
    end_tx: oneshot::Sender<crate::handles::ConnectionEnd>,
) {
    let mut owner = ConnectionOwner::new(connection_id, Arc::clone(&control), end_tx);
    loop {
        tokio::select! {
            biased;
            _ = wait_control(&control) => {
                let kind = if control.terminate_requested() {
                    ConnectionEndKind::Terminated
                } else {
                    ConnectionEndKind::Cancelled
                };
                owner.finish(kind, EndInitiator::LocalControl, None);
                return;
            }
            msg = in_rx.recv() => {
                match msg {
                    Some(RawInputMessage::Bytes(bytes)) => {
                        owner.bytes_accepted += bytes.len() as u64;
                        let len = bytes.len() as u64;
                        if out_tx.send(bytes).await.is_err() {
                            owner.finish(
                                ConnectionEndKind::TransportFailure,
                                EndInitiator::LocalTransport,
                                Some("output closed".into()),
                            );
                            return;
                        }
                        owner.bytes_received += len;
                    }
                    Some(RawInputMessage::Finish) | None => {
                        owner.finish(
                            ConnectionEndKind::RemoteEof,
                            EndInitiator::LocalTransport,
                            None,
                        );
                        return;
                    }
                }
            }
        }
    }
}

async fn run_scripted_owner(
    connection_id: ConnectionId,
    control: Arc<ControlState>,
    mut in_rx: mpsc::Receiver<RawInputMessage>,
    out_tx: mpsc::Sender<Bytes>,
    end_tx: oneshot::Sender<crate::handles::ConnectionEnd>,
    chunks: Vec<Bytes>,
) {
    let mut owner = ConnectionOwner::new(connection_id, Arc::clone(&control), end_tx);
    for chunk in chunks {
        if control.cancel_requested() || control.terminate_requested() {
            break;
        }
        let len = chunk.len() as u64;
        if out_tx.send(chunk).await.is_err() {
            owner.finish(
                ConnectionEndKind::TransportFailure,
                EndInitiator::LocalTransport,
                Some("output closed".into()),
            );
            return;
        }
        owner.bytes_received += len;
    }
    // Drain input until finish/cancel while leaving output closed after script.
    drop(out_tx);
    loop {
        tokio::select! {
            biased;
            _ = wait_control(&control) => {
                let kind = if control.terminate_requested() {
                    ConnectionEndKind::Terminated
                } else {
                    ConnectionEndKind::Cancelled
                };
                owner.finish(kind, EndInitiator::LocalControl, None);
                return;
            }
            msg = in_rx.recv() => {
                match msg {
                    Some(RawInputMessage::Bytes(bytes)) => {
                        owner.bytes_accepted += bytes.len() as u64;
                    }
                    Some(RawInputMessage::Finish) | None => {
                        owner.finish(
                            ConnectionEndKind::RemoteEof,
                            EndInitiator::Remote,
                            None,
                        );
                        return;
                    }
                }
            }
        }
    }
}

async fn run_pair_owner(
    connection_id: ConnectionId,
    control: Arc<ControlState>,
    mut in_rx: mpsc::Receiver<RawInputMessage>,
    peer_tx: mpsc::Sender<Bytes>,
    _my_out_tx: mpsc::Sender<Bytes>,
    end_tx: oneshot::Sender<crate::handles::ConnectionEnd>,
) {
    let mut owner = ConnectionOwner::new(connection_id, Arc::clone(&control), end_tx);
    loop {
        tokio::select! {
            biased;
            _ = wait_control(&control) => {
                let kind = if control.terminate_requested() {
                    ConnectionEndKind::Terminated
                } else {
                    ConnectionEndKind::Cancelled
                };
                owner.finish(kind, EndInitiator::LocalControl, None);
                return;
            }
            msg = in_rx.recv() => {
                match msg {
                    Some(RawInputMessage::Bytes(bytes)) => {
                        owner.bytes_accepted += bytes.len() as u64;
                        if peer_tx.send(bytes).await.is_err() {
                            owner.finish(
                                ConnectionEndKind::RemoteEof,
                                EndInitiator::Remote,
                                None,
                            );
                            return;
                        }
                    }
                    Some(RawInputMessage::Finish) | None => {
                        owner.finish(
                            ConnectionEndKind::LocalShutdown,
                            EndInitiator::LocalTransport,
                            None,
                        );
                        return;
                    }
                }
            }
        }
    }
}

/// Drain input while producing no output; finish only on cancel/terminate (D-012).
async fn run_hang_owner(
    connection_id: ConnectionId,
    control: Arc<ControlState>,
    mut in_rx: mpsc::Receiver<RawInputMessage>,
    end_tx: oneshot::Sender<crate::handles::ConnectionEnd>,
) {
    let mut owner = ConnectionOwner::new(connection_id, Arc::clone(&control), end_tx);
    loop {
        tokio::select! {
            biased;
            _ = wait_control(&control) => {
                let kind = if control.terminate_requested() {
                    ConnectionEndKind::Terminated
                } else {
                    ConnectionEndKind::Cancelled
                };
                owner.finish(kind, EndInitiator::LocalControl, None);
                return;
            }
            msg = in_rx.recv() => {
                match msg {
                    Some(RawInputMessage::Bytes(bytes)) => {
                        owner.bytes_accepted += bytes.len() as u64;
                        // Deliberately do not emit on output.
                    }
                    Some(RawInputMessage::Finish) | None => {
                        // Stay open without EOF: hang until cancel/terminate only.
                        wait_control(&control).await;
                        let kind = if control.terminate_requested() {
                            ConnectionEndKind::Terminated
                        } else {
                            ConnectionEndKind::Cancelled
                        };
                        owner.finish(kind, EndInitiator::LocalControl, None);
                        return;
                    }
                }
            }
        }
    }
}

async fn wait_control(control: &ControlState) {
    loop {
        if control.cancel_requested() || control.terminate_requested() {
            return;
        }
        control.notify().notified().await;
    }
}

// Silence unused import warning path for ExternalSessionId in docs.
#[allow(dead_code)]
fn _types() {
    let _: Option<ExternalSessionId> = None;
}