gent-daemon 0.1.0

Local daemon, storage and synchronization for Gent chat clusters.
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
//! A logical web node uses only its parent's existing encrypted native link.
use super::*;

#[tokio::test]
async fn dashboard_congestion_retries_after_drain_and_refreshes_a_replaced_key() -> Result<()> {
    let mut pair = Pair::new().await;
    super::directory_push_tests::negotiate(&mut pair);
    let recipient = pair.stores[1].identity.node.id().to_owned();
    let key = pair.stores[1].identity.node.public_key();
    let agent = pair.agents[0].clone();
    let version = crate::messages::wire::DASHBOARD_VERSION;
    let board = serde_json::from_value(json!({"title":"Queued","widgets":[
        {"id":"status","kind":"text","label":"State","value":"old"}]}))?;
    pair.stores[0].db.execute_batch("WITH RECURSIVE slots(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM slots WHERE i<16384)
        INSERT INTO network_events(id,peer,body,traffic) SELECT 'filler-'||i,'capacity-test',X'7b7d','control' FROM slots;")?;
    assert_eq!(
        pair.agent(
            0,
            AgentOp::DashboardDefine {
                expected_revision: 0,
                board
            }
        )?["revision"],
        1
    );
    assert_eq!(
        pair.agent(
            0,
            AgentOp::DashboardUpdate {
                expected_revision: 1,
                values: BTreeMap::from([("status".into(), json!("new"))])
            }
        )?["revision"],
        2
    );
    let s = &pair.stores[0];
    let confirmed = |s: &Store| -> Result<()> {
        let tx = s.db.unchecked_transaction()?;
        s.network_session(&tx, s.identity.state(&tx)?)
            .confirm_wire_version(&recipient, version)?;
        tx.commit()?;
        Ok(())
    };
    s.db.execute("DELETE FROM wire_peers WHERE peer=?1", [&recipient])?;
    confirmed(s)?;
    assert_eq!(
        s.db.query_row(
            "SELECT version FROM wire_peers WHERE peer=?1",
            [&recipient],
            |r| r.get::<_, u8>(0)
        )?,
        version
    );
    assert_eq!(
        s.db.query_row("SELECT count(*) FROM dashboard_sent", [], |r| r
            .get::<_, u32>(0))?,
        0
    );
    assert_eq!(
        s.db.query_row(
            "SELECT count(*) FROM presence_requests WHERE peer=?1",
            [&recipient],
            |r| r.get::<_, u32>(0)
        )?,
        1
    );
    s.db.execute("DELETE FROM network_events WHERE peer='capacity-test'", [])?;
    pair.reopen(0);
    pair.stores[0]
        .db
        .execute("UPDATE node_presence SET next_probe_at=?1", [now() - 1])?;
    pair.stores[0].presence_tick()?;
    pair.pump();
    let body: String = pair.stores[1].db.query_row(
        "SELECT body FROM dashboards WHERE agent=?1 AND revision=2",
        [&agent],
        |r| r.get(0),
    )?;
    assert_eq!(
        serde_json::from_str::<Value>(&body)?["widgets"][0]["value"],
        "new"
    );
    let sent = |s: &Store| -> Result<String> {
        Ok(s.db.query_row(
            "SELECT event FROM dashboard_sent WHERE agent=?1",
            [&agent],
            |r| r.get(0),
        )?)
    };
    let s = &pair.stores[0];
    let old = sent(s)?;
    s.db.execute("UPDATE dashboard_sent SET peer_key=zeroblob(32)", [])?;
    s.db.execute("DELETE FROM wire_peers WHERE peer=?1", [&recipient])?;
    s.db.execute(
        "UPDATE network_links SET peer_key=zeroblob(32) WHERE peer=?1",
        [&recipient],
    )?;
    confirmed(s)?;
    assert_eq!(sent(s)?, old);
    assert!(
        !s.db
            .prepare("SELECT 1 FROM wire_peers WHERE peer=?1")?
            .exists([&recipient])?
    );
    s.db.execute(
        "UPDATE network_links SET peer_key=?2 WHERE peer=?1",
        params![recipient, key],
    )?;
    confirmed(s)?;
    let replacement = sent(s)?;
    assert_ne!(replacement, old);
    assert_eq!(
        s.db.query_row(
            "SELECT peer_key FROM dashboard_sent WHERE agent=?1",
            [&agent],
            |r| r.get::<_, Vec<u8>>(0)
        )?,
        key
    );
    confirmed(s)?;
    assert_eq!(sent(s)?, replacement);
    // While the returning peer's replay is in flight, intermediate
    // revisions coalesce. Even deletion must survive that interval.
    pair.agent(
        0,
        AgentOp::DashboardUpdate {
            expected_revision: 2,
            values: BTreeMap::from([("status".into(), json!("superseded"))]),
        },
    )?;
    pair.agent(
        0,
        AgentOp::DashboardDelete {
            expected_revision: 3,
        },
    )?;
    assert_eq!(sent(&pair.stores[0])?, replacement);
    pair.pump();
    pair.stores[0]
        .db
        .execute("UPDATE node_presence SET next_probe_at=?1", [now() - 1])?;
    pair.stores[0].presence_tick()?;
    pair.pump();
    assert!(
        pair.stores[1]
            .db
            .prepare("SELECT 1 FROM dashboards WHERE agent=?1 AND revision=4 AND body IS NULL")?
            .exists([&agent])?
    );
    Ok(())
}

#[tokio::test]
async fn unchanged_dashboard_survives_cleanup_and_restart_until_peer_returns() -> Result<()> {
    let mut pair = Pair::new().await;
    super::directory_push_tests::negotiate(&mut pair);
    let board = serde_json::from_value(json!({"title":"Offline","widgets":[
        {"id":"s","label":"State","kind":"text","value":"pending"}]}))?;
    pair.agent(
        0,
        AgentOp::DashboardDefine {
            expected_revision: 0,
            board,
        },
    )?;
    let s = &pair.stores[0];
    let queued = |s: &Store| -> Result<(String, Option<i64>, bool)> {
        Ok(s.db.query_row(
            "SELECT e.id,e.retire_after,e.announcement FROM network_events e
             JOIN dashboard_sent d ON d.event=e.id",
            [],
            |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
        )?)
    };
    let (id, expiry, watermarked) = queued(s)?;
    assert_eq!((expiry, watermarked), (None, true));
    // Recreate the prior on-disk cursor and retention deadline with an
    // undelivered dashboard. Startup must migrate both in one transaction.
    s.db.execute("ALTER TABLE dashboard_sent DROP COLUMN peer_key", [])?;
    let old_deadline = now() + 86400 + history::RETENTION;
    s.db.execute(
        "UPDATE network_events SET announcement=0,retire_after=?2 WHERE id=?1",
        params![id, old_deadline],
    )?;
    pair.reopen(0);
    assert_eq!(queued(&pair.stores[0])?, (id.clone(), None, true));
    super::super::retention::clean(&pair.stores[0].db, old_deadline + 1, false)?;
    pair.reopen(0);
    assert!(
        pair.stores[0]
            .db
            .prepare("SELECT 1 FROM network_events WHERE id=?1")?
            .exists([&id])?
    );
    pair.pump();
    assert!(
        !pair.stores[0]
            .db
            .prepare("SELECT 1 FROM network_sent WHERE id=?1")?
            .exists([id])?
    );
    let revision: u64 = pair.stores[1].db.query_row(
        "SELECT revision FROM dashboards WHERE agent=?1",
        [&pair.agents[0]],
        |r| r.get(0),
    )?;
    assert_eq!(revision, 1);
    Ok(())
}

fn owner(store: &mut Store, home: &Path, action: OwnerOp) -> Value {
    store
        .execute(Request::Owner {
            credential: config::read_private(&home.join("owner.key")).unwrap(),
            action,
        })
        .unwrap()
}

#[tokio::test]
async fn hosted_chat_uses_parent_directory_and_native_transport_with_original_identity() {
    let mut pair = Pair::new().await;
    let parent = pair.stores[0].identity.node.id().to_owned();
    let remote = pair.stores[1].identity.node.id().to_owned();
    let policy = Policy {
        rules: [Permission::Enumerate, Permission::Send, Permission::Receive]
            .map(|permission| Rule {
                actor: Selector::All,
                peer: Selector::All,
                permission,
                effect: Effect::Allow,
            })
            .into(),
        ..Default::default()
    };
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::SetPolicy { policy },
    );
    let child_home = tempfile::tempdir().unwrap();
    std::fs::set_permissions(child_home.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
    let child = gent_crypto::NodeIdentity::generate().unwrap();
    {
        let store = &pair.stores[0];
        let tx = store.db.unchecked_transaction().unwrap();
        let state = store.identity.state(&tx).unwrap();
        let enrollment = child
            .request_enrollment(state.anchor(), state.checkpoint(), "web", now() as u64)
            .unwrap();
        let signed = state
            .enroll(
                &*store.identity.signing_owner(&tx).unwrap(),
                &enrollment,
                enrollment.fingerprint().unwrap(),
                now() as u64,
            )
            .unwrap();
        let state = store.identity.apply(&tx, signed).unwrap();
        tx.commit().unwrap();
        Store::install_joined(child_home.path(), &child, &state).unwrap();
    }
    let key = config::secret();
    let mut child_store = Store::open(child_home.path()).unwrap();
    let original = child_store
        .execute(Request::Agent {
            credential: key.clone(),
            action: AgentOp::Register {
                name: "walking-web".into(),
                provider: "claude.ai".into(),
            },
        })
        .unwrap()["agent"]["id"]
        .as_str()
        .unwrap()
        .to_owned();
    drop(child_store);
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::PrepareHostedNode {
            node_id: child.id().into(),
            home: child_home.path().into(),
        },
    );
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::Configure {
            request_id: Uuid::new_v4().to_string(),
            edit: ConfigurationEdit::HostNode {
                node_id: child.id().into(),
                parent: parent.clone(),
            },
        },
    );
    pair.stores[0].reconcile_hosted_routes().unwrap();
    pair.pump();
    // Resolve the logical child's last transport from the carrier's pinned
    // stream. The evidence remains diagnostic even without fresh presence.
    let group = pair.group();
    pair.stores[1].sync_health.record_transport(
        &group,
        crate::sync::health::TransportPath::ClusterHelper {
            helper_id: "helper".into(),
        },
        crate::sync::health::TransportEvidence::ReceivedPacket,
        1,
    );
    let nodes = owner(&mut pair.stores[1], pair.homes[1].path(), OwnerOp::Nodes);
    let rows = nodes.as_array().unwrap();
    let carrier = &rows.iter().find(|n| n["id"] == parent).unwrap()["transport"];
    let hosted = &rows.iter().find(|n| n["id"] == child.id()).unwrap()["transport"];
    assert_eq!(hosted, carrier);
    assert_eq!(hosted["carrier_id"], parent);
    assert_eq!(hosted["last"]["helper_id"], "helper");
    assert_eq!(
        pair.stores[1].synchronization_status().unwrap()["peer_transports"][0]["last"],
        hosted["last"]
    );
    // The native parent's Merkle directory also repairs a lost web chat.
    for store in &mut pair.stores {
        store.presence_tick().unwrap();
    }
    pair.pump();
    pair.stores[1]
        .db
        .execute("DELETE FROM network_origins WHERE agent=?1", [&original])
        .unwrap();
    pair.stores[1]
        .db
        .execute("DELETE FROM agents WHERE id=?1", [&original])
        .unwrap();
    // V4 reconciles on ordinary traffic, including the periodic heartbeat.
    pair.stores[1]
        .db
        .execute("UPDATE node_presence SET next_probe_at=0", [])
        .unwrap();
    pair.stores[1].presence_tick().unwrap();
    pair.pump();
    assert_eq!(
        pair.stores[1].synchronization_status().unwrap()["directory"]["peers"][0]["state"],
        "matched"
    );
    let gateway = config::read_private(&child_home.path().join("owner.key")).unwrap();
    let call = |store: &mut Store, action| {
        store.execute(Request::HostedAgent {
            epoch: None,
            node_id: child.id().into(),
            gateway_credential: gateway.clone(),
            credential: key.clone(),
            action,
        })
    };
    let identity = call(&mut pair.stores[0], AgentOp::Identity).unwrap();
    assert_eq!(identity["agent"]["id"], original);
    assert_eq!(identity["agent"]["node_id"], child.id());
    assert_eq!(identity["transport_node"], parent);
    let directory = call(&mut pair.stores[0], AgentOp::Agents).unwrap();
    assert_eq!(directory.as_array().unwrap().len(), 3);
    let remote_directory = pair.agent(1, AgentOp::Agents).unwrap();
    assert!(
        remote_directory
            .as_array()
            .unwrap()
            .iter()
            .any(|a| a["id"] == original && a["node_id"] == child.id())
    );
    assert!(
        !pair.stores[0]
            .db
            .query_row(
                "SELECT EXISTS(SELECT 1 FROM network_links WHERE peer=?1)",
                [child.id()],
                |r| r.get::<_, bool>(0)
            )
            .unwrap()
    );
    let send = AgentOp::Send {
        recipient: pair.agents[1].clone(),
        request_id: Uuid::now_v7().to_string(),
        text: "remote through native host".into(),
        files: vec![FilePart {
            name: "sample.txt".into(),
            media_type: "text/plain".into(),
            data_base64: STANDARD.encode(b"file bytes"),
        }],
        file_ids: vec![],
    };
    let message = call(&mut pair.stores[0], send.clone()).unwrap();
    assert_eq!(
        call(&mut pair.stores[0], send.clone()).unwrap()["message_id"],
        message["message_id"]
    );
    pair.pump();
    let inbox = pair.agent(1, AgentOp::Inbox).unwrap();
    assert_eq!(inbox[0]["sender"], original);
    assert_eq!(
        inbox[0]["files"][0]["data_base64"],
        STANDARD.encode(b"file bytes")
    );
    let message_id = message["message_id"].as_str().unwrap().to_owned();
    pair.agent(
        1,
        AgentOp::Ack {
            message_id: message_id.clone(),
        },
    )
    .unwrap();
    pair.agent(
        1,
        AgentOp::Reply {
            message_id: message_id.clone(),
            request_id: Uuid::now_v7().to_string(),
            text: "reply through parent".into(),
            files: vec![],
            file_ids: vec![],
        },
    )
    .unwrap();
    pair.pump();
    assert_eq!(
        call(&mut pair.stores[0], AgentOp::Inbox).unwrap()[0]["text"],
        "reply through parent"
    );
    assert_eq!(
        call(&mut pair.stores[0], AgentOp::MessageStatus { message_id }).unwrap()["state"],
        "delivered"
    );
    pair.reopen(0);
    assert_eq!(
        call(&mut pair.stores[0], send).unwrap()["message_id"],
        message["message_id"]
    );
    assert!(Store::open(child_home.path()).is_err());
    // The authenticated native peer cannot impersonate the hosted origin, nor
    // can the child's old independent transport authenticate after delegation.
    let state = pair.stores[1].identity.state(&pair.stores[1].db).unwrap();
    let source = pair.stores[0].network_session(
        &pair.stores[0].db,
        pair.stores[0].identity.state(&pair.stores[0].db).unwrap(),
    );
    let registration = source.registration(&original).unwrap();
    let destination = pair.stores[1].network_session(&pair.stores[1].db, state);
    assert!(
        destination
            .import_registration(&remote, &registration)
            .is_err()
    );
    assert!(
        destination
            .import_registration(child.id(), &registration)
            .is_err()
    );
    // A new pending turn survives a carrier change while its native target has
    // already answered but the original carrier has not received that answer.
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::WebLease {
            node_id: child.id().into(),
            seconds: 20,
        },
    );
    let pending = AgentOp::RequestReply {
        recipient: pair.agents[1].clone(),
        request_id: Uuid::now_v7().to_string(),
        text: "reply during takeover".into(),
        files: vec![],
        file_ids: vec![],
    };
    let turn = call(&mut pair.stores[0], pending.clone()).unwrap()["message_id"]
        .as_str()
        .unwrap()
        .to_owned();
    let checkpoint =
        crate::store::hosted::snapshot::export(pair.homes[0].path(), child.id()).unwrap();
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::WebCommit {
            epoch: None,
            node_id: child.id().into(),
            revision: checkpoint.revision,
        },
    );
    let committed_identity = call(&mut pair.stores[0], AgentOp::Identity).unwrap();
    call(
        &mut pair.stores[0],
        AgentOp::Rename {
            name: "unpublished-before-carrier-loss".into(),
            expected_generation: committed_identity["registration_generation"]
                .as_u64()
                .unwrap(),
        },
    )
    .unwrap();
    pair.pump();
    let reply = pair
        .agent(
            1,
            AgentOp::Reply {
                message_id: turn.clone(),
                request_id: Uuid::now_v7().to_string(),
                text: "answer retained on the new carrier".into(),
                files: vec![],
                file_ids: vec![],
            },
        )
        .unwrap();
    let new_home = tempfile::tempdir().unwrap();
    let demand = OwnerOp::WebDeliveryDemand {
        node_id: child.id().into(),
    };
    let current = owner(&mut pair.stores[1], pair.homes[1].path(), demand.clone());
    assert_eq!(current["message_id"], reply["message_id"]);
    // A late status response for an earlier send must leave this reply pending.
    let late = OwnerOp::WebDeliveryChecked {
        node_id: child.id().into(),
        message_id: turn.clone(),
    };
    assert_eq!(
        owner(&mut pair.stores[1], pair.homes[1].path(), late)["cleared"],
        false
    );
    assert_eq!(
        owner(&mut pair.stores[1], pair.homes[1].path(), demand)["message_id"],
        reply["message_id"]
    );
    std::fs::set_permissions(new_home.path(), std::fs::Permissions::from_mode(0o700)).unwrap();
    let mut cfg = config::load_for_runtime(child_home.path()).unwrap();
    cfg.set_hosted_parent(config::HostedParent {
        home: pair.homes[1].path().into(),
        node_id: remote.clone(),
    })
    .unwrap();
    config::save(new_home.path(), &cfg).unwrap();
    let new_gateway = config::secret();
    config::write_private(&new_home.path().join("owner.key"), new_gateway.as_bytes()).unwrap();
    {
        let anchor = pair.stores[1]
            .identity
            .state(&pair.stores[1].db)
            .unwrap()
            .anchor()
            .clone();
        let tx = pair.stores[1].db.transaction().unwrap();
        crate::store::hosted::snapshot::restore(&tx, &checkpoint, &anchor, child.id()).unwrap();
        tx.execute(
            "INSERT INTO hosted_nodes VALUES(?1,?2,?3)",
            params![
                child.id(),
                new_home.path().to_str().unwrap(),
                hash(&new_gateway)
            ],
        )
        .unwrap();
        tx.execute(
            "INSERT INTO hosted_checkpoints VALUES(?1,?2,?2,?3)",
            params![child.id(), checkpoint.revision, now() + 20],
        )
        .unwrap();
        tx.commit().unwrap();
    }
    owner(
        &mut pair.stores[0],
        pair.homes[0].path(),
        OwnerOp::Configure {
            request_id: Uuid::new_v4().to_string(),
            edit: ConfigurationEdit::HostNode {
                node_id: child.id().into(),
                parent: remote.clone(),
            },
        },
    );
    pair.pump();
    pair.stores[1].reconcile_hosted_routes().unwrap();
    pair.pump();
    let state = pair.stores[0].identity.state(&pair.stores[0].db).unwrap();
    let old = pair.stores[0].network_session(&pair.stores[0].db, state);
    assert_eq!(
        old.stored_registration(&original).unwrap().name,
        committed_identity["agent"]["name"]
    );
    let next = |store: &mut Store, action| {
        store.execute(Request::HostedAgent {
            epoch: None,
            node_id: child.id().into(),
            gateway_credential: new_gateway.clone(),
            credential: key.clone(),
            action,
        })
    };
    assert_eq!(
        next(&mut pair.stores[1], AgentOp::Identity).unwrap()["agent"]["id"],
        original
    );
    assert!(call(&mut pair.stores[0], AgentOp::Identity).is_err());
    assert_eq!(
        owner(
            &mut pair.stores[0],
            pair.homes[0].path(),
            OwnerOp::WebCheckpoint {
                node_id: child.id().into(),
            }
        )["hosting_here"],
        false
    );
    let result = next(
        &mut pair.stores[1],
        AgentOp::RelayTurnStatus {
            turn_id: turn.clone(),
            after_message_id: None,
        },
    )
    .unwrap();
    assert_eq!(result["state"], "completed", "{result}");
    assert_eq!(result["responses"][0]["id"], reply["message_id"]);
    assert_eq!(
        next(&mut pair.stores[1], pending).unwrap()["message_id"],
        turn
    );
}