mobius-gateway 0.11.5

Headless authenticated gateway for möbius frontends
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
633
use std::collections::BTreeMap;
use std::time::Duration;

use mobius::protocol::{MessageDelivery, MessageEvent};

use super::*;

#[test]
fn peer_submission_uses_the_board_id_and_defers_delivery_policy() {
    let message_id = Uuid::new_v4().to_string();
    let entry = BoardEntry {
        id: message_id.clone(),
        sequence: 1,
        created_at_ms: 1,
        author: crate::bots::swarm::SwarmMember {
            bot_id: "source-bot".into(),
            handle: "source".into(),
            joined_at_ms: 1,
        },
        source_session_id: "source-session".into(),
        text: "Review this".into(),
        mentioned_recipient_bot_ids: vec!["target-bot".into()],
        pending_recipient_bot_ids: vec!["target-bot".into()],
        assigned_recipient_session_ids: BTreeMap::new(),
        in_reply_to_message_id: None,
        reply_depth: 0,
    };

    let submission = swarm_message_submission(entry);

    assert_eq!(submission.id, message_id);
    assert!(matches!(
        submission.op,
        Op::Message {
            message: MessageSubmission {
                author: MessageAuthor::Peer {
                    message_id: peer_message_id,
                    session_id,
                    handle,
                },
                text,
                requested_delivery: None,
                ..
            },
        } if peer_message_id == message_id
            && session_id == "source-session"
            && handle == "source"
            && text == "Review this"
    ));
}

#[tokio::test]
async fn stale_acknowledgement_does_not_clear_a_newer_delivery_attempt() {
    let root = tempfile::tempdir().expect("root");
    let listen = "127.0.0.1:8741".parse().expect("listen address");
    let (store, config) =
        ConfigStore::initialize(root.path().join("state"), listen, None).expect("config");
    let credentials =
        Arc::new(CredentialStore::open(store.credentials_path()).expect("credentials"));
    let bots = Arc::new(BotStore::open(store.state_dir()).expect("Bots"));
    let gateway = GatewayHost::start(store, config, credentials, bots)
        .await
        .expect("gateway");
    let mut attempts = HashMap::from([(
        "target-bot".into(),
        SwarmDeliveryAttempt::Submitted("new-message".into()),
    )]);
    gateway
        .handle_swarm_delivery(
            SwarmDelivery::Acknowledged {
                target_bot_id: "target-bot".into(),
                message_id: "old-message".into(),
            },
            &mut attempts,
        )
        .await;

    assert_eq!(
        attempts.get("target-bot"),
        Some(&SwarmDeliveryAttempt::Submitted("new-message".into()))
    );
}

#[tokio::test]
async fn rejected_delivery_waits_for_bot_capacity_before_retrying() {
    let root = tempfile::tempdir().expect("root");
    let listen = "127.0.0.1:8741".parse().expect("listen address");
    let (store, config) =
        ConfigStore::initialize(root.path().join("state"), listen, None).expect("config");
    let credentials =
        Arc::new(CredentialStore::open(store.credentials_path()).expect("credentials"));
    let bots = Arc::new(BotStore::open(store.state_dir()).expect("Bots"));
    let gateway = GatewayHost::start(store, config, credentials, bots)
        .await
        .expect("gateway");
    let mut attempts = HashMap::from([(
        "target-bot".into(),
        SwarmDeliveryAttempt::Submitted("message-1".into()),
    )]);
    gateway
        .handle_swarm_delivery(
            SwarmDelivery::Rejected {
                target_bot_id: "target-bot".into(),
                message_id: "message-1".into(),
            },
            &mut attempts,
        )
        .await;
    assert_eq!(
        attempts.get("target-bot"),
        Some(&SwarmDeliveryAttempt::Rejected("message-1".into()))
    );

    gateway
        .handle_swarm_delivery(SwarmDelivery::RetryPending, &mut attempts)
        .await;
    assert!(matches!(
        attempts.get("target-bot"),
        Some(SwarmDeliveryAttempt::Rejected(message_id)) if message_id == "message-1"
    ));

    gateway
        .handle_swarm_delivery(
            SwarmDelivery::CapacityAvailable {
                target_bot_id: "target-bot".into(),
            },
            &mut attempts,
        )
        .await;
    assert!(!attempts.contains_key("target-bot"));
}

#[tokio::test]
async fn gateway_busy_delivery_waits_for_mutation_completion_before_retrying() {
    let root = tempfile::tempdir().expect("root");
    let checkpoints: Arc<dyn CheckpointStore> = Arc::new(
        SqliteCheckpoint::new(root.path().join("checkpoints.sqlite3")).expect("checkpoints"),
    );
    let bots = Arc::new(BotStore::open(root.path()).expect("Bots"));
    let gateway = Arc::new(StdMutex::new(
        GatewayConfig::new(crate::config::DEFAULT_LISTEN, None).expect("gateway config"),
    ));
    let (swarm, mut deliveries) = SwarmStore::new(checkpoints, bots, gateway);
    let swarm = Arc::new(swarm);
    let session_mutations = Arc::new(RwLock::new(()));
    let mutation = Arc::clone(&session_mutations).write_owned().await;
    let retry = notify_swarm_delivery_after_mutation(
        Arc::clone(&session_mutations),
        Arc::clone(&swarm),
        "target-bot".into(),
    );
    tokio::pin!(retry);
    tokio::select! {
        biased;
        () = &mut retry => panic!("delivery retried before the mutation completed"),
        () = std::future::ready(()) => {}
    }
    assert!(matches!(
        deliveries.try_recv(),
        Err(mpsc::error::TryRecvError::Empty)
    ));

    drop(mutation);
    retry.await;

    assert_eq!(
        deliveries.recv().await,
        Some(SwarmDelivery::Pending {
            target_bot_id: "target-bot".into(),
        })
    );
}

async fn gateway_with_swarm(
    root: &tempfile::TempDir,
) -> (
    GatewayHost,
    PathBuf,
    HostHandle,
    crate::wire::BotRecord,
    HostHandle,
    crate::wire::BotRecord,
    String,
) {
    let workspace = root.path().join("workspace");
    std::fs::create_dir(&workspace).expect("workspace");
    let listen = "127.0.0.1:8741".parse().expect("listen address");
    let (store, config) =
        ConfigStore::initialize(root.path().join("state"), listen, None).expect("config");
    let config = config
        .registering_provider(
            AgentComposition::default().provider,
            "Test".into(),
            Default::default(),
            Vec::new(),
            Vec::new(),
        )
        .expect("register provider");
    store.save(&config).expect("save config");
    let credentials =
        Arc::new(CredentialStore::open(store.credentials_path()).expect("credentials"));
    let bots = Arc::new(BotStore::open(store.state_dir()).expect("Bots"));
    let gateway = GatewayHost::start(store, config, credentials, bots)
        .await
        .expect("gateway");
    let source_bot = ensure_test_bot(&gateway).await.expect("source Bot");
    let source = gateway
        .create_session(&workspace, &source_bot.id)
        .await
        .expect("source chat");
    let (target, target_bot) = create_distinct_test_session(&gateway, &workspace, "target_bot")
        .await
        .expect("target chat");
    let swarm = gateway
        .create_swarm(
            "Review team".into(),
            source_bot.id.clone(),
            vec![target_bot.id.clone()],
        )
        .await
        .expect("create swarm");
    (
        gateway,
        workspace,
        source,
        source_bot,
        target,
        target_bot,
        swarm[0].id.clone(),
    )
}

#[tokio::test]
async fn mention_delivery_uses_the_bots_private_swarm_conversation() {
    let root = tempfile::tempdir().expect("root");
    let (gateway, _, source, source_bot, target, target_bot, swarm_id) =
        gateway_with_swarm(&root).await;
    let mut gateway_events = gateway.subscribe();
    let source_session_id = source.session_id().to_owned();
    let original_target_session_id = target.session_id().to_owned();
    let swarm = Arc::clone(&gateway.state.lock().await.swarm);
    let text = format!("@{} please review the parser", target_bot.handle);
    let post = swarm
        .post(&source_bot.id, &source_session_id, text.clone(), None)
        .await
        .expect("post mention");

    let assigned_session_id = tokio::time::timeout(Duration::from_secs(15), async {
        loop {
            if let Ok(frame) = gateway_events.try_recv()
                && let ServerMessage::Error { code, message, .. } = frame.message
            {
                panic!("swarm delivery failed ({code}): {message}");
            }
            let entry = swarm
                .board_page(&swarm_id, None, 32)
                .await
                .expect("board")
                .entries
                .into_iter()
                .find(|entry| entry.id == post.entry.id)
                .expect("posted entry");
            if let Some(session_id) = entry.assigned_recipient_session_ids.get(&target_bot.id) {
                let page = gateway
                    .state
                    .lock()
                    .await
                    .checkpoints
                    .event_page(
                        session_id,
                        EventPageRequest {
                            before_sequence: None,
                            limit: 128,
                        },
                    )
                    .await
                    .expect("target journal");
                if page.events.iter().any(|record| {
                    record.event.submission_id.as_deref() == Some(post.entry.id.as_str())
                        && matches!(record.event.msg, EventMsg::Message(_))
                }) {
                    break session_id.clone();
                }
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("peer delivery input");

    assert_ne!(assigned_session_id, original_target_session_id);
    let checkpoint = gateway
        .state
        .lock()
        .await
        .checkpoints
        .load(&assigned_session_id)
        .await
        .expect("load target")
        .expect("fresh target checkpoint");
    assert_eq!(checkpoint.session_context.bot_id, target_bot.id);
    assert!(!checkpoint.catalog_visible);
    assert_eq!(
        checkpoint.session_context.origin_label.as_deref(),
        Some("Swarm Chat · Review team")
    );
    let background_workspace = gateway.state.lock().await.background_workspace.clone();
    assert_eq!(
        checkpoint.session_context.workspace_label.as_deref(),
        Some(background_workspace.to_string_lossy().as_ref())
    );
    let page = gateway
        .state
        .lock()
        .await
        .checkpoints
        .event_page(
            &assigned_session_id,
            EventPageRequest {
                before_sequence: None,
                limit: 128,
            },
        )
        .await
        .expect("target journal");
    assert!(page.events.iter().any(|record| {
        matches!(
            &record.event.msg,
            EventMsg::Message(MessageEvent {
                author: MessageAuthor::Peer { message_id, session_id, handle },
                delivery,
                text: message_text,
                ..
            }) if record.event.submission_id.as_deref() == Some(post.entry.id.as_str())
                && message_id == &post.entry.id
                && session_id == &source_session_id
                && handle == &source_bot.handle
                && delivery == &MessageDelivery::Turn
                && message_text == &text
        )
    }));

    let second = swarm
        .post(
            &source_bot.id,
            &source_session_id,
            format!("@{} review the follow-up", target_bot.handle),
            None,
        )
        .await
        .expect("post follow-up mention");
    let reused_session_id = tokio::time::timeout(Duration::from_secs(15), async {
        loop {
            let entry = swarm
                .board_page(&swarm_id, None, 32)
                .await
                .expect("board")
                .entries
                .into_iter()
                .find(|entry| entry.id == second.entry.id)
                .expect("follow-up entry");
            if let Some(session_id) = entry.assigned_recipient_session_ids.get(&target_bot.id) {
                break session_id.clone();
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("follow-up delivery");
    assert_eq!(reused_session_id, assigned_session_id);
}

#[tokio::test]
async fn startup_ack_reuses_the_reserved_conversation_without_resubmitting() {
    let root = tempfile::tempdir().expect("root");
    let state_dir = root.path().join("state");
    let (gateway, _workspace, source, source_bot, target, target_bot, _) =
        gateway_with_swarm(&root).await;
    let source_session_id = source.session_id().to_owned();
    let visible_tool_count = target
        .snapshot(None)
        .await
        .expect("visible target snapshot")
        .ready
        .tool_count;
    for host in [&source, &target] {
        assert!(host.stop_if_idle().await);
        while host.is_alive() {
            tokio::task::yield_now().await;
        }
    }
    drop(source);
    drop(target);
    drop(gateway);

    let (store, config) = ConfigStore::open(state_dir).expect("reopen config");
    let checkpoints: Arc<dyn CheckpointStore> =
        Arc::new(SqliteCheckpoint::new(store.checkpoints_path()).expect("checkpoints"));
    let bots = Arc::new(BotStore::open(store.state_dir()).expect("Bots"));
    let gateway_config = Arc::new(StdMutex::new(config.clone()));
    let (swarm, _deliveries) = SwarmStore::new(Arc::clone(&checkpoints), bots, gateway_config);
    let text = format!("@{} verify restart delivery", target_bot.handle);
    let post = swarm
        .post(&source_bot.id, &source_session_id, text.clone(), None)
        .await
        .expect("persist mention");
    let claim = swarm
        .claim_next_delivery(&target_bot.id)
        .await
        .expect("claim target conversation")
        .expect("pending target delivery");
    let assigned_session_id = claim.session_id().to_owned();
    drop(claim);
    let approval_id = Uuid::new_v4().to_string();
    let mut checkpoint = Checkpoint::empty(&assigned_session_id);
    checkpoint.catalog_visible = false;
    let background_workspace =
        prepare_background_workspace(store.state_dir(), None).expect("background workspace");
    let target_spec =
        ChatSpec::for_bot(&background_workspace, &target_bot, store.state_dir(), None)
            .expect("target spec");
    let target_workspace = target_spec.workspace_info();
    checkpoint.metadata = target_spec.metadata().expect("target metadata");
    checkpoint.session_context.bot_id = target_bot.id.clone();
    checkpoint.session_context.workspace_id = Some(target_workspace.id);
    checkpoint.session_context.workspace_label = Some(target_workspace.path.display().to_string());
    checkpoint.active_execution = Some(ActiveExecution {
        submission_id: post.entry.id.clone(),
        turn_id: "replayed-turn".into(),
        started_at_ms: 1_000,
        model_calls: 1,
        tool_calls: 0,
        failed_tool_calls: 0,
        usage: Default::default(),
        next_model_step: 1,
        stop_hook_active: false,
        phase: mobius::backend::checkpoint::ExecutionPhase::Model,
    });
    checkpoint.pending_approval = Some(mobius::backend::checkpoint::PendingApproval {
        submission_id: post.entry.id.clone(),
        turn_id: "replayed-turn".into(),
        request_id: approval_id.clone(),
        approval_call_ids: Vec::new(),
        authorized_call_ids: Vec::new(),
        calls: Vec::new(),
        reason: "Approve replayed work".into(),
        sandbox_mode: Default::default(),
        network_access: Default::default(),
        decision_received: false,
    });
    checkpoints
        .save(&checkpoint, &[], None)
        .await
        .expect("save reserved checkpoint");
    checkpoints
        .append_event(
            &assigned_session_id,
            1,
            &Event {
                submission_id: Some(post.entry.id.clone()),
                msg: EventMsg::Message(MessageEvent {
                    author: MessageAuthor::Peer {
                        message_id: post.entry.id.clone(),
                        session_id: source_session_id,
                        handle: source_bot.handle,
                    },
                    delivery: MessageDelivery::Turn,
                    text,
                    attachments: Vec::new(),
                    message_target: None,
                }),
            },
        )
        .await
        .expect("persist peer event");
    checkpoints
        .append_event(
            &assigned_session_id,
            2,
            &Event {
                submission_id: Some(post.entry.id.clone()),
                msg: EventMsg::ExecApprovalRequest(mobius::protocol::ExecApprovalRequestEvent {
                    id: approval_id.clone(),
                    turn_id: "replayed-turn".into(),
                    calls: Vec::new(),
                    reason: "Approve replayed work".into(),
                }),
            },
        )
        .await
        .expect("persist approval request");
    drop(swarm);
    drop(checkpoints);

    let credentials =
        Arc::new(CredentialStore::open(store.credentials_path()).expect("credentials"));
    let bots = Arc::new(BotStore::open(store.state_dir()).expect("Bots"));
    let gateway = GatewayHost::start(store, config, credentials, bots)
        .await
        .expect("restart gateway");
    let mut events = gateway.subscribe();
    tokio::time::timeout(Duration::from_secs(5), async {
        loop {
            if let Ok(frame) = events.try_recv()
                && let ServerMessage::Error { code, message, .. } = frame.message
            {
                panic!("startup delivery failed ({code}): {message}");
            }
            if gateway
                .state
                .lock()
                .await
                .sessions
                .contains_key(&assigned_session_id)
            {
                break;
            }
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    })
    .await
    .expect("startup reopen");

    let reopened = gateway
        .state
        .lock()
        .await
        .sessions
        .get(&assigned_session_id)
        .cloned()
        .expect("reopened hidden conversation");
    assert!(
        !reopened.stop_if_idle().await,
        "approval keeps the actor active"
    );
    let hidden = reopened.snapshot(None).await.expect("hidden snapshot");
    assert_eq!(hidden.ready.tool_count + 1, visible_tool_count);
    assert!(
        gateway
            .sessions()
            .await
            .expect("visible sessions")
            .iter()
            .all(|session| session.session_id != assigned_session_id)
    );
    assert!(
        gateway
            .hidden_bot_sessions(&target_bot.id)
            .await
            .expect("hidden Bot sessions")
            .iter()
            .any(|session| session.session_id == assigned_session_id)
    );

    let checkpoints = Arc::clone(&gateway.state.lock().await.checkpoints);
    let page = checkpoints
        .event_page(
            &assigned_session_id,
            EventPageRequest {
                before_sequence: None,
                limit: 128,
            },
        )
        .await
        .expect("target events");
    assert_eq!(
        page.events
            .iter()
            .filter(|record| matches!(
                &record.event.msg,
                EventMsg::Message(MessageEvent {
                    author: MessageAuthor::Peer { message_id, .. },
                    ..
                }) if message_id.as_str() == post.entry.id.as_str()
            ))
            .count(),
        1,
        "startup acknowledgement must not resubmit the persisted peer message"
    );

    let swarm = Arc::clone(&gateway.state.lock().await.swarm);
    let board = swarm
        .board_page(
            &swarm
                .snapshot_for_bot(&target_bot.id)
                .await
                .expect("swarm snapshot")
                .expect("target membership")
                .swarm
                .id,
            None,
            128,
        )
        .await
        .expect("board");
    assert_eq!(board.entries.len(), 1, "approval replay must not post");
    assert_eq!(board.entries[0].id, post.entry.id);
    assert_eq!(
        swarm
            .pending_deliveries(&target_bot.id)
            .await
            .expect("in-flight delivery")
            .len(),
        1,
        "approval remains nonterminal"
    );
    assert!(
        swarm
            .settle_delivery(
                &post.entry.id,
                &assigned_session_id,
                &target_bot.id,
                SwarmRunOutcome::Succeeded {
                    summary: "Replayed work completed".into(),
                },
            )
            .await
            .expect("settle replayed work")
    );
    assert!(
        !swarm
            .settle_delivery(
                &post.entry.id,
                &assigned_session_id,
                &target_bot.id,
                SwarmRunOutcome::Failed {
                    message: "duplicate terminal".into(),
                },
            )
            .await
            .expect("dedupe terminal")
    );
}