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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
//! Owner-local admission journal. Service approval never substitutes for this
//! state. The only peer-facing entry points accept authenticated Noise traffic.
use super::*;

pub(super) fn validate_queued_enrollment(
    db: &Connection,
    state: &VerifiedState,
    identity: &Identity,
    id: &str,
    request: &gent_crypto::SignedEnrollment,
    can_configure: bool,
    timestamp: u64,
) -> Result<()> {
    let row = record(db, id)?;
    row.offer.verify_current(state, timestamp)?;
    let claim = row.claim.as_ref().context("invitation claim is missing")?;
    ensure!(
        row.phase == "claimed"
            && row.offer.offer.issuer_id == identity.node.id()
            && row.offer.offer.issuer_key == identity.node.public_key()
            && row.offer.offer.can_configure == can_configure
            && row.offer.offer.replaces == request.request.replaces
            && claim.id == request.request.node_id
            && claim.key == request.request.member.signing_key
            && claim.name == request.request.member.name,
        "enrollment invitation is no longer authorized"
    );
    if matches!(&row.offer.offer.purpose, Purpose::NamedNode { .. }) {
        ensure!(
            state
                .state()
                .nodes
                .iter()
                .all(|(id, n)| id == &claim.id || n.name != claim.name),
            "node name is already enrolled"
        );
    }
    Ok(())
}
use gent_crypto::invitation::{
    ClaimedNode, Options, OwnerInvitation, Purpose, ServiceBinding, SignedOffer,
    channel::{Channel, Hello, PendingClaim},
};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use zeroize::Zeroizing;
mod dispatch;
mod replacement;
#[cfg(test)]
mod tests;

const MAX_INVITATIONS: u64 = 128;
const MAX_ATTEMPTS: u64 = 64;
const MAX_OPERATIONS: u64 = 32_768;
const MAX_INVITATION_OPERATIONS: u64 = 8192;
const MAX_GRANTS: u64 = 4;

pub(crate) fn schema(db: &Connection, previous: i64) -> Result<()> {
    super::node_invites::schema(db)?;
    if previous >= 14 {
        for table in ["invitations", "invitation_operations", "invitation_clock"] {
            let exists: bool = db.query_row(
                "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name=?1)",
                [table],
                |r| r.get(0),
            )?;
            ensure!(
                exists,
                "invitation journal is missing; restore this node's state"
            );
        }
        ensure!(
            db.query_row(
                "SELECT COUNT(*) FROM invitation_clock WHERE singleton=1",
                [],
                |r| r.get::<_, u64>(0)
            )? == 1,
            "invitation clock is missing; restore this node's state"
        );
    } else {
        db.execute_batch("CREATE TABLE IF NOT EXISTS invitation_clock(singleton INTEGER PRIMARY KEY CHECK(singleton=1),floor INTEGER NOT NULL);
        INSERT OR IGNORE INTO invitation_clock VALUES(1,0);
        CREATE TABLE IF NOT EXISTS invitations(
          id TEXT PRIMARY KEY,create_request TEXT UNIQUE NOT NULL,intent BLOB NOT NULL CHECK(length(intent)=32),
          offer BLOB NOT NULL CHECK(length(offer)<=8192),secret BLOB CHECK(length(secret)<=16384),
          phase TEXT NOT NULL CHECK(phase IN ('draft','issued','claimed','complete','revoked')),
          expires INTEGER NOT NULL,claim BLOB CHECK(length(claim)<=1024),history_floor INTEGER,
          attempts INTEGER NOT NULL DEFAULT 0 CHECK(attempts BETWEEN 0 AND 64));
        CREATE INDEX IF NOT EXISTS invitations_expiry ON invitations(expires);
        CREATE TABLE IF NOT EXISTS invitation_operations(
          invitation TEXT NOT NULL REFERENCES invitations(id) ON DELETE CASCADE,id TEXT NOT NULL,
          fingerprint BLOB NOT NULL CHECK(length(fingerprint)=32),kind TEXT NOT NULL,
          done INTEGER NOT NULL DEFAULT 0 CHECK(done IN (0,1)),group_id BLOB CHECK(length(group_id)<=256),
          PRIMARY KEY(invitation,id));
        CREATE INDEX IF NOT EXISTS invitation_operation_kind ON invitation_operations(invitation,kind);")?;
    }
    if previous >= 15 {
        ensure!(db.query_row(
            "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type='table' AND name='invitation_attempts')",
            [], |r| r.get::<_, bool>(0)
        )?, "invitation attempt journal is missing; restore this node's state");
    } else {
        // Version 14 did not retain attempt IDs. It cannot safely distinguish
        // old handshakes after an upgrade. Keep membership/keys, but close used
        // offers; partial joins can use an explicit continuation invitation.
        db.execute_batch("DELETE FROM invitation_operations WHERE invitation IN (SELECT id FROM invitations WHERE attempts>0);
            UPDATE invitations SET phase='revoked',secret=NULL WHERE attempts>0;
            CREATE TABLE IF NOT EXISTS invitation_attempts(
              invitation TEXT NOT NULL REFERENCES invitations(id) ON DELETE CASCADE,
              id TEXT NOT NULL CHECK(length(id)=36),PRIMARY KEY(invitation,id));")?;
    }
    Ok(())
}

struct Record {
    offer: SignedOffer,
    secret: Option<Zeroizing<Vec<u8>>>,
    phase: String,
    claim: Option<ClaimedNode>,
    history_floor: Option<u64>,
    attempts: u64,
}
type RecordRow = (
    Vec<u8>,
    Option<Vec<u8>>,
    String,
    Option<Vec<u8>>,
    Option<u64>,
    u64,
    u64,
);
fn record(db: &Connection, id: &str) -> Result<Record> {
    let (offer, secret, phase, claim, history_floor, attempts, expires): RecordRow = db.query_row("SELECT offer,secret,phase,claim,history_floor,attempts,expires FROM invitations WHERE id=?1", [id], |r| {
        Ok((r.get(0)?,r.get(1)?,r.get(2)?,r.get(3)?,r.get(4)?,r.get(5)?,r.get(6)?))
    }).optional()?.context("invitation unavailable")?;
    let secret = secret.map(Zeroizing::new);
    let offer = SignedOffer::decode(&offer)?;
    ensure!(
        offer.offer.id == id && expires == offer.offer.expires_at && attempts <= MAX_ATTEMPTS,
        "invalid invitation journal binding"
    );
    let claim = claim
        .map(|bytes| -> Result<ClaimedNode> {
            ensure!(bytes.len() <= 1024, "invalid saved invitation claim");
            serde_json::from_slice(&bytes).context("invalid saved invitation claim")
        })
        .transpose()?;
    ensure!(
        match phase.as_str() {
            "draft" | "issued" => claim.is_none() && history_floor.is_none(),
            "claimed" => claim.is_some(),
            "complete" => claim.is_some() && history_floor.is_some(),
            "revoked" => secret.is_none(),
            _ => false,
        },
        "invalid invitation journal phase"
    );
    Ok(Record {
        offer,
        secret,
        phase,
        claim,
        history_floor,
        attempts,
    })
}

/// Clock advancement commits independently of a rejected operation. Observing
/// expiry and then rolling the clock back must not reactivate its credential.
fn observe(db: &Connection, wall: u64) -> Result<u64> {
    ensure!(wall > 0 && wall < (1 << 53), "invalid invitation clock");
    db.execute(
        "UPDATE invitation_clock SET floor=?1 WHERE singleton=1 AND floor<?1",
        [wall],
    )?;
    let floor: u64 = db.query_row(
        "SELECT floor FROM invitation_clock WHERE singleton=1",
        [],
        |r| r.get(0),
    )?;
    // Explicit child deletion also works when an OrderedEndpoint connection has
    // SQLite foreign-key enforcement disabled. Both deletions commit together.
    let tx = db.unchecked_transaction()?;
    tx.execute("DELETE FROM invitation_operations WHERE invitation IN (SELECT id FROM invitations WHERE expires<=?1)", [floor])?;
    tx.execute("DELETE FROM invitation_attempts WHERE invitation IN (SELECT id FROM invitations WHERE expires<=?1)", [floor])?;
    tx.execute("DELETE FROM invitations WHERE expires<=?1", [floor])?;
    tx.commit()?;
    Ok(floor)
}

pub struct PendingSession {
    pending: PendingClaim,
}
pub struct Session {
    channel: Channel,
}

/// A bounded encrypted request, distinct from owner RPC. UUIDv4 operation IDs
/// are scoped to this invitation; retries must preserve their complete input.
#[derive(Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Invocation {
    pub id: String,
    pub action: Action,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum Action {
    Pair { action: PairingOp },
    Finish,
}
impl Invocation {
    pub fn encode(&self) -> Result<Zeroizing<Vec<u8>>> {
        let bytes = Zeroizing::new(serde_jcs::to_vec(self)?);
        ensure!(
            bytes.len() <= MAX_FRAME_BYTES,
            "invitation request too large"
        );
        Ok(bytes)
    }
    fn decode(bytes: &[u8]) -> Result<Self> {
        ensure!(
            bytes.len() <= MAX_FRAME_BYTES,
            "invitation request too large"
        );
        let value: Self = serde_json::from_slice(bytes)
            .map_err(|_| anyhow::anyhow!("invalid invitation request"))?;
        ensure!(
            *value.encode()? == bytes,
            "invitation request must use canonical JSON"
        );
        canonical_v4(&value.id)?;
        Ok(value)
    }
}
fn canonical_v4(value: &str) -> Result<Uuid> {
    let id = Uuid::parse_str(value).context("invalid invitation operation ID")?;
    ensure!(
        id.get_version_num() == 4
            && id.get_variant() == uuid::Variant::RFC4122
            && id.to_string() == value,
        "invitation operation ID must be UUIDv4"
    );
    Ok(id)
}

impl Store {
    pub(super) fn manage_invitation(
        &mut self,
        credential: &str,
        action: InvitationOp,
    ) -> Result<Value> {
        self.invitation_owner(credential)?;
        match action {
            InvitationOp::Create {
                request_id,
                can_configure,
                continuation,
                replace_node,
            } => {
                let bytes = self.create_invitation_with_replacement(
                    credential,
                    &request_id,
                    can_configure,
                    continuation.as_deref(),
                    replace_node.as_deref(),
                )?;
                Ok(json!({"private_invitation": STANDARD.encode(bytes.as_slice())}))
            }
            InvitationOp::Activate { id } => {
                self.activate_invitation(credential, &id)?;
                Ok(json!({"id":id,"active":true}))
            }
            InvitationOp::Revoke { id } => {
                self.revoke_invitation(credential, &id)?;
                Ok(json!({"id":id,"revoked":true}))
            }
            InvitationOp::List => {
                let _lock = config::state_lock(&self.home, "invitation.lock")?;
                observe(&self.db, now() as u64)?;
                let mut query = self
                    .db
                    .prepare("SELECT id FROM invitations ORDER BY id LIMIT 129")?;
                let ids = query
                    .query_map([], |r| r.get::<_, String>(0))?
                    .collect::<rusqlite::Result<Vec<_>>>()?;
                ensure!(
                    ids.len() <= MAX_INVITATIONS as usize,
                    "invalid invitation journal size"
                );
                let records = ids.iter().map(|id| {
                    let row = record(&self.db, id)?;
                    let mut value = json!({"id":id,"phase":row.phase,"expires_at":row.offer.offer.expires_at,
                        "can_configure":row.offer.offer.can_configure,"node":row.claim});
                    if let Some(target) = &row.offer.offer.replaces {
                        value["replaces"] = json!(target);
                    }
                    Ok(value)
                }).collect::<Result<Vec<_>>>()?;
                Ok(json!({"invitations":records}))
            }
        }
    }

    /// Local scheduling metadata only. No private offer material leaves Store.
    /// This rechecks current native authority even when service access survives.
    pub(crate) fn invitation_jobs(&mut self) -> Result<Vec<SignedOffer>> {
        self.process_node_invites()?;
        if self.config.service.is_none()
            || !self
                .identity
                .can_administer(&self.identity.state(&self.db)?)
        {
            return Ok(Vec::new());
        }
        if !self
            .db
            .query_row("SELECT EXISTS(SELECT 1 FROM invitations)", [], |r| {
                r.get::<_, bool>(0)
            })?
        {
            return Ok(Vec::new());
        }
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let ids = self.db.prepare(
            "SELECT id FROM invitations WHERE phase IN ('issued','claimed','complete') ORDER BY id LIMIT 129"
        )?.query_map([], |r| r.get::<_, String>(0))?.collect::<rusqlite::Result<Vec<_>>>()?;
        ensure!(
            ids.len() <= MAX_INVITATIONS as usize,
            "invalid invitation journal size"
        );
        let mut offers = Vec::new();
        for id in ids {
            let row = record(&self.db, &id)?;
            if self.invitation_authority(&row, timestamp).is_ok() {
                offers.push(row.offer);
            }
        }
        Ok(offers)
    }

    pub(crate) fn invitation_bearer(&self) -> Result<Zeroizing<String>> {
        let settings = self
            .config
            .relay
            .as_ref()
            .context("invitation relay unavailable")?;
        Ok(Zeroizing::new(config::read_relay_credential(
            &self.home.join(&settings.credential_file),
        )?))
    }

    /// Recheck before sending a cached response, including a completed Finish.
    /// Revocation cannot retract bytes already released to a network request.
    pub(crate) fn invitation_live(&mut self, offer: &SignedOffer) -> Result<()> {
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let row = record(&self.db, &offer.offer.id)?;
        self.invitation_authority(&row, timestamp)?;
        ensure!(
            row.offer == *offer && matches!(row.phase.as_str(), "issued" | "claimed" | "complete"),
            "invitation unavailable"
        );
        Ok(())
    }

    pub(crate) fn invitation_claim_receipt(
        &mut self,
        session: &mut Session,
    ) -> Result<Vec<Vec<u8>>> {
        self.invitation_live(session.channel.offer())?;
        session
            .channel
            .seal(crate::invitation_transport::CLAIM_RECEIPT, now() as u64)
    }

    fn invitation_owner(&self, credential: &str) -> Result<()> {
        ensure!(hash(credential) == self.owner_hash, "unauthorized");
        self.identity.require_administrator(&self.db)?;
        Ok(())
    }
    fn invitation_service(&self) -> Result<ServiceBinding> {
        let account = self
            .config
            .service
            .as_ref()
            .context("invitation enrollment requires service setup")?;
        ensure!(
            !self.config.cluster.local_only,
            "invitation enrollment requires a service cluster"
        );
        let relay = self
            .config
            .relay
            .as_ref()
            .context("invitation enrollment requires a service relay")?;
        // Config origins historically include a trailing slash. Sign the
        // canonical origins, then compare the same representation on every use.
        let service = ServiceBinding {
            account_id: account.account_id.clone(),
            account_origin: account.origin.trim_end_matches('/').into(),
            relay_origin: relay.endpoint.trim_end_matches('/').into(),
        };
        service.validate()?;
        Ok(service)
    }
    fn invitation_authority(&self, record: &Record, timestamp: u64) -> Result<VerifiedState> {
        self.identity.require_administrator(&self.db)?;
        let state = self.identity.state(&self.db)?;
        record.offer.verify_current(&state, timestamp)?;
        // Continue an exact enrolled identity, including a lost Finish reply.
        // A draft or claim for a new identity still needs a current paid lease.
        if record.offer.offer.purpose.continuation().is_none()
            && !record.claim.as_ref().is_some_and(|node| {
                state
                    .state()
                    .nodes
                    .get(&node.id)
                    .is_some_and(|m| m.signing_key == node.key)
            })
        {
            require_pro(&self.home, &self.config, "Inviting another node")?;
        }
        ensure!(
            record.offer.offer.issuer_id == self.identity.node.id()
                && record.offer.offer.issuer_key == self.identity.node.public_key()
                && record.offer.offer.service == self.invitation_service()?,
            "invitation owner or service changed"
        );
        state.authorize_node(
            self.identity.node.id(),
            &self.identity.node.public_key(),
            timestamp,
        )?;
        ensure!(record.phase != "revoked", "invitation revoked");
        self.invitation_replacement(record, &state, timestamp)?;
        if let Some(node) = &record.claim {
            ensure!(
                !state.state().retired_nodes.contains(&node.id)
                    && !state.state().retired_keys.contains(&node.key),
                "invitation node was retired"
            );
            if let Some(member) = state.state().nodes.get(&node.id) {
                ensure!(
                    member.signing_key == node.key
                        && member.name == node.name
                        && member.can_configure == record.offer.offer.can_configure,
                    "invitation node identity or privileges changed"
                );
            } else {
                ensure!(
                    record.history_floor.is_none()
                        && record.offer.offer.purpose.continuation().is_none()
                        && matches!(
                            record.offer.offer.purpose,
                            Purpose::NewNode | Purpose::NamedNode { .. }
                        ),
                    "invitation node is no longer enrolled"
                );
            }
        }
        Ok(state)
    }

    /// Owner-only local API. The returned bytes are for protected transfer,
    /// never an MCP response or a public account-service request.
    pub fn create_invitation(
        &mut self,
        credential: &str,
        request: &str,
        can_configure: bool,
        continuation: Option<&str>,
    ) -> Result<Zeroizing<Vec<u8>>> {
        self.create_invitation_with_replacement(
            credential,
            request,
            can_configure,
            continuation,
            None,
        )
    }

    pub fn create_invitation_with_replacement(
        &mut self,
        credential: &str,
        request: &str,
        can_configure: bool,
        continuation: Option<&str>,
        replace_node: Option<&str>,
    ) -> Result<Zeroizing<Vec<u8>>> {
        self.invitation_owner(credential)?;
        self.maintain()?;
        self.create_invitation_at(
            request,
            can_configure,
            continuation,
            replace_node,
            None,
            now() as u64,
        )
    }
    pub(super) fn create_invitation_at(
        &mut self,
        request: &str,
        can_configure: bool,
        continuation: Option<&str>,
        replace_node: Option<&str>,
        name: Option<(&str, &str)>,
        wall: u64,
    ) -> Result<Zeroizing<Vec<u8>>> {
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, wall)?;
        let service = self.invitation_service()?;
        ensure!(
            continuation.is_none() || replace_node.is_none(),
            "choose replacement or continuation, not both"
        );
        // Preserve the original ordinary-invitation retry fingerprint.
        ensure!(
            name.is_none() || (continuation.is_none() && replace_node.is_none()),
            "named invitations must enroll a new node"
        );
        let intent: [u8; 32] = Sha256::digest(match (replace_node, name) {
            (_, Some(name)) => serde_jcs::to_vec(&("named", can_configure, name, &service))?,
            (None, None) => serde_jcs::to_vec(&(can_configure, continuation, &service))?,
            (Some(target), None) => {
                serde_jcs::to_vec(&(can_configure, continuation, &service, target))?
            }
        })
        .into();
        let previous: Option<(String, Vec<u8>)> = self
            .db
            .query_row(
                "SELECT id,intent FROM invitations WHERE create_request=?1",
                [request],
                |r| Ok((r.get(0)?, r.get(1)?)),
            )
            .optional()?;
        if let Some((id, saved)) = previous {
            ensure!(
                saved == intent,
                "invitation creation request already used with different options"
            );
            let row = record(&self.db, &id)?;
            self.invitation_authority(&row, timestamp)?;
            let private = row.secret.context("invitation secret unavailable")?;
            return OwnerInvitation::decode(&private)?.invitation().encode();
        }
        let issued =
            requests::issued(request).context("invitation creation request must be UUIDv7")? as u64;
        ensure!(
            issued <= timestamp && timestamp - issued < gent_crypto::invitation::OFFER_LIFETIME,
            "invitation creation request expired or is from the future"
        );
        let state = self.identity.state(&self.db)?;
        let purpose = match (continuation, name) {
            (None, Some((name, inviter))) => {
                let mut matching = state
                    .state()
                    .nodes
                    .iter()
                    .filter(|(_, member)| member.name == name);
                let existing = matching.next();
                ensure!(
                    matching.next().is_none(),
                    "multiple nodes have this name; use an explicit node ID invitation"
                );
                let continuation = existing.map(|(id, member)| ClaimedNode {
                    id: id.clone(),
                    key: member.signing_key,
                    name: member.name.clone(),
                });
                Purpose::NamedNode {
                    name: name.into(),
                    inviter: inviter.into(),
                    continuation,
                }
            }
            (None, None) => Purpose::NewNode,
            (Some(id), _) => {
                let member = state
                    .state()
                    .nodes
                    .get(id)
                    .context("continuation node unavailable")?;
                Purpose::Continue {
                    node: ClaimedNode {
                        id: id.into(),
                        key: member.signing_key,
                        name: member.name.clone(),
                    },
                }
            }
        };
        if purpose.continuation().is_none() {
            require_pro(&self.home, &self.config, "Inviting another node")?;
        }
        let (replaces, can_configure) = match (&purpose, replace_node) {
            (Purpose::NewNode, Some(id)) => {
                ensure!(
                    id != self.identity.node.id(),
                    "cannot replace the signing owner through an invitation"
                );
                let member = state
                    .state()
                    .nodes
                    .get(id)
                    .context("replacement target is unavailable")?;
                (
                    Some(gent_crypto::ReplacedNode {
                        id: id.into(),
                        signing_key: member.signing_key,
                    }),
                    can_configure && member.can_configure,
                )
            }
            (Purpose::Continue { node }, None) => (
                removals::replacement_target(&self.db, &state, &node.id, &node.key)?,
                can_configure,
            ),
            (Purpose::NewNode | Purpose::NamedNode { .. }, None) => (None, can_configure),
            _ => unreachable!("exclusive replacement and continuation checked"),
        };
        let tx = self.db.unchecked_transaction()?;
        ensure!(
            tx.query_row("SELECT COUNT(*) FROM invitations", [], |r| r
                .get::<_, u64>(0))?
                < MAX_INVITATIONS,
            "invitation capacity reached; wait for existing offers to expire"
        );
        let options = Options {
            service,
            purpose,
            can_configure,
            replaces,
        };
        let owner = if state.state().peer.is_some() {
            OwnerInvitation::issue_peer(&state, &self.identity.node, options, timestamp)?
        } else {
            OwnerInvitation::issue(
                &*self.identity.signing_owner(&self.db)?,
                &state,
                &self.identity.node,
                options,
                timestamp,
            )?
        };
        let offer = owner.invitation().offer();
        let private = owner.encode()?;
        tx.execute("INSERT INTO invitations(id,create_request,intent,offer,secret,phase,expires) VALUES(?1,?2,?3,?4,?5,'draft',?6)", params![offer.offer.id,request,intent.as_slice(),offer.encode()?,private.as_slice(),offer.offer.expires_at])?;
        tx.commit()?;
        owner.invitation().encode()
    }

    /// Called after the account service confirms the exact persisted draft.
    /// The activation itself does not make an account or cryptographic member.
    pub fn activate_invitation(&mut self, credential: &str, id: &str) -> Result<()> {
        self.invitation_owner(credential)?;
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let row = record(&self.db, id)?;
        self.invitation_authority(&row, timestamp)?;
        self.db.execute(
            "UPDATE invitations SET phase='issued' WHERE id=?1 AND phase='draft'",
            [id],
        )?;
        Ok(())
    }
    pub fn revoke_invitation(&mut self, credential: &str, id: &str) -> Result<()> {
        self.invitation_owner(credential)?;
        let issued =
            gent_protocol::service::invitations::issued_at(id).context("invalid invitation ID")?;
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let tx = self.db.unchecked_transaction()?;
        let changed = tx.execute(
            "UPDATE invitations SET phase='revoked',secret=NULL WHERE id=?1",
            [id],
        )?;
        ensure!(
            changed == 1 || timestamp >= issued + gent_crypto::invitation::OFFER_LIFETIME,
            "invitation unavailable"
        );
        tx.execute(
            "DELETE FROM invitation_operations WHERE invitation=?1",
            [id],
        )?;
        tx.commit()?;
        Ok(())
    }

    pub fn begin_invitation(
        &mut self,
        id: &str,
        hello: &Hello,
    ) -> Result<(PendingSession, Vec<u8>)> {
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let row = record(&self.db, id)?;
        self.invitation_authority(&row, timestamp)?;
        canonical_v4(&hello.attempt)?;
        ensure!(
            !self.invitation_attempt_seen(id, &hello.attempt)?,
            "invitation attempt already admitted; use a fresh handshake"
        );
        ensure!(
            row.phase != "draft" && row.attempts < MAX_ATTEMPTS,
            "invitation unavailable or attempt budget exhausted"
        );
        let private = row.secret.context("invitation secret unavailable")?;
        let owner = OwnerInvitation::decode(&private)?;
        ensure!(
            owner.invitation().offer() == &row.offer,
            "invitation private record changed"
        );
        let (pending, reply) = owner.respond(hello, timestamp)?;
        let tx = self.db.unchecked_transaction()?;
        tx.execute(
            "INSERT INTO invitation_attempts(invitation,id) VALUES(?1,?2)",
            params![id, hello.attempt],
        )?;
        tx.execute(
            "UPDATE invitations SET attempts=attempts+1 WHERE id=?1",
            [id],
        )?;
        tx.commit()?;
        Ok((PendingSession { pending }, reply))
    }

    /// Scheduling hint only. Admission rechecks under the invitation lock and
    /// commits the ID before releasing a handshake response. A lost in-memory
    /// session must never be reconstructed from an already admitted Hello.
    pub fn invitation_attempt_seen(&self, id: &str, attempt: &str) -> Result<bool> {
        canonical_v4(attempt)?;
        Ok(self.db.query_row(
            "SELECT EXISTS(SELECT 1 FROM invitation_attempts WHERE invitation=?1 AND id=?2)",
            params![id, attempt],
            |r| r.get(0),
        )?)
    }
    pub fn accept_invitation(
        &mut self,
        pending: PendingSession,
        frames: &[Vec<u8>],
    ) -> Result<Session> {
        let _lock = config::state_lock(&self.home, "invitation.lock")?;
        let timestamp = observe(&self.db, now() as u64)?;
        let channel = pending.pending.accept(frames, timestamp, |offer, node| {
            self.claim_invitation(offer, node, timestamp)
        })?;
        Ok(Session { channel })
    }
    fn claim_invitation(
        &mut self,
        offer: &SignedOffer,
        node: &ClaimedNode,
        timestamp: u64,
    ) -> Result<()> {
        if let Purpose::NamedNode { name, .. } = &offer.offer.purpose {
            ensure!(
                &node.name == name,
                "invitation requires its assigned node name"
            );
        }
        let row = record(&self.db, &offer.offer.id)?;
        let state = self.invitation_authority(&row, timestamp)?;
        ensure!(
            &row.offer == offer && row.phase != "draft",
            "invitation claim changed"
        );
        if let Some(saved) = &row.claim {
            ensure!(saved == node, "invitation already claimed by another node");
            return Ok(());
        }
        ensure!(row.phase == "issued", "invitation is not claimable");
        let floor = match offer.offer.purpose.continuation() {
            None => {
                if matches!(&offer.offer.purpose, Purpose::NamedNode { .. }) {
                    ensure!(
                        !state
                            .state()
                            .nodes
                            .values()
                            .any(|member| member.name == node.name),
                        "this node name is already enrolled; run gent node add with the same name for a continuation code"
                    );
                }
                ensure!(
                    !state.state().nodes.contains_key(&node.id)
                        && !state.state().retired_nodes.contains(&node.id)
                        && !state.state().retired_keys.contains(&node.key)
                        && !state
                            .state()
                            .nodes
                            .values()
                            .any(|n| n.signing_key == node.key),
                    "node already enrolled or retired; use an explicit continuation"
                );
                None
            }
            Some(expected) => {
                ensure!(node == expected, "continuation identity changed");
                state.authorize_node(&node.id, &node.key, timestamp)?;
                let member = &state.state().nodes[&node.id];
                ensure!(
                    member.name == node.name && member.can_configure == offer.offer.can_configure,
                    "continuation privileges changed"
                );
                Some(
                    self.db
                        .query_row("SELECT MIN(revision) FROM authority_history", [], |r| {
                            r.get::<_, u64>(0)
                        })?,
                )
            }
        };
        let tx = self.db.unchecked_transaction()?;
        let changed = tx.execute("UPDATE invitations SET phase='claimed',claim=?2,history_floor=?3 WHERE id=?1 AND phase='issued' AND claim IS NULL", params![offer.offer.id,serde_jcs::to_vec(node)?,floor])?;
        ensure!(changed == 1, "invitation claim raced another writer");
        tx.commit()?;
        Ok(())
    }

    pub fn invitation_call(
        &mut self,
        session: &mut Session,
        frames: &[Vec<u8>],
    ) -> Result<Vec<Vec<u8>>> {
        let timestamp = now() as u64;
        let bytes = session.channel.open(frames, timestamp)?;
        let response = match Invocation::decode(&bytes)
            .and_then(|request| self.dispatch_invitation(&session.channel, request, timestamp))
        {
            Ok(value) => Response::Ok(value),
            Err(error) => Response::Error(error.to_string().chars().take(1024).collect()),
        };
        let bytes = Zeroizing::new(serde_json::to_vec(&response)?);
        session.channel.seal(&bytes, now() as u64)
    }

    /// Startup/periodic maintenance also retires expired secrets and retry rows.
    pub(super) fn clean_invitations(&mut self) -> Result<()> {
        let any: bool = self
            .db
            .query_row("SELECT EXISTS(SELECT 1 FROM invitations)", [], |r| r.get(0))?;
        if any {
            let _lock = config::state_lock(&self.home, "invitation.lock")?;
            observe(&self.db, now() as u64)?;
        }
        Ok(())
    }
}