lonca-core 0.2.0

Deterministic building blocks of Lonca: change ids, the signed operation log, keys and the Git store
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
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
//! The operation vocabulary. Every state change in a Lonca repository is one
//! of these, recorded (allowed or not) in the operation log.

use std::collections::BTreeMap;
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::changeid::ChangeId;
use crate::objects::GitId;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum Actor {
    Human { name: String },
    Agent { name: String, session: String },
    Daemon,
}

impl Actor {
    pub fn label(&self) -> String {
        match self {
            Actor::Human { name } => format!("human:{name}"),
            Actor::Agent { name, session } => format!("agent:{name}/{session}"),
            Actor::Daemon => "daemon".into(),
        }
    }

    pub fn session(&self) -> Option<&str> {
        match self {
            Actor::Agent { session, .. } => Some(session),
            _ => None,
        }
    }
}

impl fmt::Display for Actor {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.label())
    }
}

/// Operation types. Deserialization rejects anything not listed here, which
/// makes every scope check deny-by-default.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum OpKind {
    #[serde(rename = "repo.init")]
    RepoInit,
    #[serde(rename = "human.enroll")]
    HumanEnroll,
    #[serde(rename = "human.rotate")]
    HumanRotate,
    #[serde(rename = "human.revoke")]
    HumanRevoke,
    #[serde(rename = "human.recovery_key")]
    HumanRecoveryKey,
    #[serde(rename = "human.recover")]
    HumanRecover,
    #[serde(rename = "daemon.rotate")]
    DaemonRotate,
    #[serde(rename = "daemon.recover")]
    DaemonRecover,
    #[serde(rename = "policy.update")]
    PolicyUpdate,
    #[serde(rename = "session.start")]
    SessionStart,
    #[serde(rename = "session.bind")]
    SessionBind,
    #[serde(rename = "session.isolate")]
    SessionIsolate,
    #[serde(rename = "session.end")]
    SessionEnd,
    #[serde(rename = "cap.attenuate")]
    CapAttenuate,
    #[serde(rename = "cap.revoke")]
    CapRevoke,
    #[serde(rename = "lease.acquire")]
    LeaseAcquire,
    #[serde(rename = "lease.release")]
    LeaseRelease,
    #[serde(rename = "lease.renew")]
    LeaseRenew,
    #[serde(rename = "lease.interest")]
    LeaseInterest,
    #[serde(rename = "change.propose")]
    ChangePropose,
    #[serde(rename = "change.withdraw")]
    ChangeWithdraw,
    #[serde(rename = "change.integrate")]
    ChangeIntegrate,
    #[serde(rename = "change.integrate_batch")]
    ChangeIntegrateBatch,
    #[serde(rename = "approve")]
    Approve,
    #[serde(rename = "reject")]
    Reject,
    #[serde(rename = "observe")]
    Observe,
    #[serde(rename = "taint")]
    Taint,
    #[serde(rename = "import.external")]
    ImportExternal,
    #[serde(rename = "import.unmediated")]
    ImportUnmediated,
    #[serde(rename = "bridge.push")]
    BridgePush,
    #[serde(rename = "freeze")]
    Freeze,
    #[serde(rename = "unfreeze")]
    Unfreeze,
    #[serde(rename = "negotiate")]
    Negotiate,
    #[serde(rename = "redact")]
    Redact,
}

impl OpKind {
    pub fn as_str(self) -> &'static str {
        match self {
            OpKind::RepoInit => "repo.init",
            OpKind::HumanEnroll => "human.enroll",
            OpKind::HumanRotate => "human.rotate",
            OpKind::HumanRevoke => "human.revoke",
            OpKind::HumanRecoveryKey => "human.recovery_key",
            OpKind::HumanRecover => "human.recover",
            OpKind::DaemonRotate => "daemon.rotate",
            OpKind::DaemonRecover => "daemon.recover",
            OpKind::PolicyUpdate => "policy.update",
            OpKind::SessionStart => "session.start",
            OpKind::SessionBind => "session.bind",
            OpKind::SessionIsolate => "session.isolate",
            OpKind::SessionEnd => "session.end",
            OpKind::CapAttenuate => "cap.attenuate",
            OpKind::CapRevoke => "cap.revoke",
            OpKind::LeaseAcquire => "lease.acquire",
            OpKind::LeaseRelease => "lease.release",
            OpKind::LeaseRenew => "lease.renew",
            OpKind::LeaseInterest => "lease.interest",
            OpKind::ChangePropose => "change.propose",
            OpKind::ChangeWithdraw => "change.withdraw",
            OpKind::ChangeIntegrate => "change.integrate",
            OpKind::ChangeIntegrateBatch => "change.integrate_batch",
            OpKind::Approve => "approve",
            OpKind::Reject => "reject",
            OpKind::Observe => "observe",
            OpKind::Taint => "taint",
            OpKind::ImportExternal => "import.external",
            OpKind::ImportUnmediated => "import.unmediated",
            OpKind::BridgePush => "bridge.push",
            OpKind::Freeze => "freeze",
            OpKind::Unfreeze => "unfreeze",
            OpKind::Negotiate => "negotiate",
            OpKind::Redact => "redact",
        }
    }

    /// The only operation types a capability can ever grant to an agent.
    /// Everything else (policy, keys, approvals, imports, revocation of
    /// others, redaction, freezing) is reserved for humans or the daemon and
    /// cannot be granted by any policy.
    pub fn agent_grantable(self) -> bool {
        matches!(
            self,
            OpKind::ChangePropose
                | OpKind::ChangeWithdraw
                | OpKind::LeaseAcquire
                | OpKind::LeaseRelease
                | OpKind::LeaseRenew
                | OpKind::LeaseInterest
                | OpKind::Negotiate
                | OpKind::CapAttenuate
        )
    }
}

/// What a session may touch. Patterns use the language in [`crate::paths`].
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Scope {
    pub paths: Vec<String>,
    /// `None`: no symbol restriction beyond `paths`.
    pub symbols: Option<Vec<String>>,
    pub ops: Vec<OpKind>,
    pub max_files: Option<u32>,
    pub max_lines: Option<u32>,
    /// Absolute directories outside the workspace the agent may write to
    /// (notes, logs, throwaway files). Writes there are allowed and are
    /// not facts; nothing there can be proposed. Left out of the encoding
    /// when empty, so entries without one keep their bytes (format 5).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub scratch: Vec<String>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ObservationLevel {
    /// Nothing watches the agent. Treated as tainted by default policy.
    Unobserved,
    /// Locked, blocking harness hooks (H1).
    Hooks,
    /// H1 plus a locked shell sandbox whose network goes through the daemon (H2).
    HooksSandbox,
    /// Container / OS sandbox owned by Lonca.
    Isolated,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct BindChecks {
    pub harness: String,
    pub harness_session: String,
    /// Hooks come from root-owned managed settings with managed-only hooks.
    pub managed_hooks: bool,
    /// Shell sandbox enabled, locked, network routed to the Lonca proxy.
    pub locked_sandbox: bool,
    /// `sudo -n true` succeeded without a password.
    pub sudo_without_password: bool,
    /// Hash of the harness configuration that defines the hooks.
    pub config_digest: String,
    /// The hook program and its parent directories are not user-writable.
    pub hook_binary_protected: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LeaseKind {
    /// Exclusive: change the body of one symbol.
    Edit,
    /// Shared: add new members to a container (file or type).
    Insert,
    /// Exclusive: rename or delete a symbol.
    Rename,
    /// Shared: change the body of a symbol alongside others who hold it
    /// `shared`; the merge joins their lines (log format 21, needs
    /// `leases.shared`).
    Shared,
}

impl LeaseKind {
    pub const ALL: [LeaseKind; 4] = [Self::Edit, Self::Insert, Self::Rename, Self::Shared];

    /// The name in entries and requests.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Edit => "edit",
            Self::Insert => "insert",
            Self::Rename => "rename",
            Self::Shared => "shared",
        }
    }

    pub fn from_name(name: &str) -> Option<Self> {
        Self::ALL.into_iter().find(|k| k.as_str() == name)
    }
}

impl LeaseItem {
    /// An item with no reserved names.
    pub fn new(symbol: impl Into<String>, kind: LeaseKind) -> Self {
        Self {
            symbol: symbol.into(),
            kind,
            names: Vec::new(),
        }
    }

    pub fn is_shared(&self) -> bool {
        self.kind == LeaseKind::Shared
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct LeaseItem {
    /// `path#Qualified.name`; for `insert`, the container (`path#Type` or `path#<file>`).
    pub symbol: String,
    pub kind: LeaseKind,
    /// For `insert` only: the names of the members it adds, bare (no key).
    /// Two inserts into one container whose names meet conflict, and an
    /// addition under this item must be one of them. Empty reserves nothing
    /// and allows any addition. Left out of the encoding when empty, so
    /// entries without names keep their bytes (log format 22).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub names: Vec<String>,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestDelta {
    pub added: u32,
    pub modified: u32,
    pub deleted: u32,
    /// Tests that gained a skip/ignore marker.
    pub skipped: u32,
}

/// The agent's structured declaration of what its change does. Compared
/// field by field with [`Facts`]; any mismatch rejects the change.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Claim {
    /// Free text for humans. Never a decision input.
    pub intent: String,
    pub edits: Vec<String>,
    pub adds: Vec<String>,
    pub deletes: Vec<String>,
    pub deleted_files: Vec<String>,
    pub signature_changes: Vec<String>,
    pub tests: TestDelta,
    pub revert_of: Option<ChangeId>,
}

/// The member of a type that stands for its own text: `Type.<def>` is the
/// header, fields, decorators and class-level statements, and for Rust every
/// `impl` header of the type, without its methods.
pub const DEF: &str = "<def>";

/// A file whose symbols are sections: their names are its headings and keys,
/// written by people, and a `(` in one is only that.
pub fn names_are_headings(path: &str) -> bool {
    let ext = path.rsplit_once('.').map(|(_, ext)| ext);
    matches!(
        ext,
        Some("md" | "markdown" | "toml" | "json" | "yml" | "yaml")
    )
}

/// The first symbol engine whose facts name `Type.<def>`. Before it the same
/// text could only be the heading of a document section.
pub const ENGINE_DEFS: u32 = 3;

/// What a change actually does, computed by Lonca from the Git objects.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Facts {
    pub edits: Vec<String>,
    pub adds: Vec<String>,
    pub deletes: Vec<String>,
    pub added_files: Vec<String>,
    pub deleted_files: Vec<String>,
    pub modified_files: Vec<String>,
    pub signature_changes: Vec<String>,
    /// Lines changed inside import blocks only (lease-free, merged as sets).
    pub import_only_files: Vec<String>,
    pub tests: TestDelta,
    pub lines_added: u32,
    pub lines_removed: u32,
    /// Symbols that go on under another name: `[old, new]`. A renamed one is
    /// also in `deletes` and `adds`, one with new parameters in `edits`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub renames: Vec<[String; 2]>,
    /// Text files changed that no parser reads, whole-file edits in `edits`.
    /// Under `leases.unparsed_files = "optimistic"` they need no lease.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unparsed_files: Vec<String>,
    /// Files that a grammar read until this change and can not read after
    /// it. From here on facts about them say nothing of their symbols, so a
    /// person has to agree.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub unread_files: Vec<String>,
    /// Symbols of the base that mention by name what this change gives a new
    /// signature, renames or deletes, and that it leaves as they are. By name
    /// only: a guess that errs towards too many, never a call graph.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub affected: Vec<String>,
    /// How many more there were than `affected` holds.
    #[serde(default, skip_serializing_if = "is_zero")]
    pub affected_more: u32,
}

fn is_zero(n: &u32) -> bool {
    *n == 0
}

impl Facts {
    /// This change can break code it did not touch: what depends on a
    /// signature, or something mentions a name it takes away.
    pub fn reaches_others(&self) -> bool {
        !self.signature_changes.is_empty() || !self.affected.is_empty() || self.affected_more > 0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckKind {
    Syntax,
    Scope,
    ClaimDiff,
    Merge,
    Build,
    Tests,
    TestIntegrity,
    /// `verification.on_signature_change`.
    Extended,
    /// One of `verification.checks`, by the name in `details["name"]`.
    Named,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CheckResult {
    Pass,
    Fail,
    Error,
    Skipped,
}

/// Evidence produced by a (possibly nondeterministic) check. Recorded in the
/// operation log so that decisions can be replayed without rerunning it.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Attestation {
    pub check: CheckKind,
    pub result: CheckResult,
    pub subject_tree: GitId,
    pub details: BTreeMap<String, String>,
    /// Digest of the environment: sandbox profile, commands, tool versions.
    pub env: String,
    pub log: Option<GitId>,
    pub producer: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Phase {
    Pre,
    Post,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ObsKind {
    Read {
        path: String,
    },
    Write {
        path: String,
    },
    /// The program name and a digest of the full command; the command text
    /// itself is not logged because it may contain secrets.
    Shell {
        program: String,
        digest: String,
    },
    Fetch {
        host: String,
    },
    Mcp {
        tool: String,
    },
    Other {
        tool: String,
    },
    Heartbeat,
    /// What the workspace held that no standing proposal of the session
    /// carries, when the agent stopped or ended its session. `paths` lists
    /// at most [`MAX_UNPROPOSED_PATHS`] of `total`; a `total` of 0 says the
    /// work is gone or delivered.
    Unproposed {
        paths: Vec<String>,
        total: u64,
    },
    /// `lonca mcp` ran `tool` and the harness's before-tool event for that
    /// call never reached the daemon: the session's hooks are not running.
    /// `tool` is the MCP tool's name, without the harness's prefix.
    Unhooked {
        tool: String,
    },
}

/// The most paths one [`ObsKind::Unproposed`] names.
pub const MAX_UNPROPOSED_PATHS: usize = 32;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Observation {
    pub phase: Phase,
    pub kind: ObsKind,
    /// The observation is outside the workspace or names untrusted content.
    pub external: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum NegotiationMsg {
    /// "I need these symbols for this reason."
    Claim {
        symbols: Vec<String>,
        reason: String,
    },
    /// Offer to split work: the sender keeps `mine`, the receiver gets `yours`.
    ProposeSplit {
        mine: Vec<String>,
        yours: Vec<String>,
    },
    /// Give up the named lease set in favour of the receiver. With `to` the
    /// set moves to that session in this entry, so nobody can take it in
    /// between; without, it is freed.
    Yield {
        set: String,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        to: Option<String>,
    },
    Counter {
        note: String,
    },
    Accept,
    Decline {
        note: String,
    },
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HumanKey {
    /// `ed25519` (file key, no user presence) or `p256-se` (Secure Enclave,
    /// Touch ID or password on every signature).
    pub alg: String,
    pub public: String,
}

/// A new session taking over the workspace of an earlier one.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Continuation {
    pub session: String,
    /// The human asked for a scope wider than the earlier session's.
    pub widen: bool,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "op")]
pub enum Op {
    #[serde(rename = "repo.init")]
    RepoInit {
        repo: String,
        daemon_key: String,
        human: String,
        human_key: HumanKey,
        policy: GitId,
        trunk: GitId,
    },
    #[serde(rename = "human.enroll")]
    HumanEnroll { human: String, key: HumanKey },
    /// Replaces the signer's own key. Signed with the current key.
    #[serde(rename = "human.rotate")]
    HumanRotate { human: String, key: HumanKey },
    /// Withdraws a human's keys and everything they set in motion.
    #[serde(rename = "human.revoke")]
    HumanRevoke { human: String, reason: String },
    /// Sets (or replaces) the signer's recovery key, kept off the machine.
    /// Signed with the current key.
    #[serde(rename = "human.recovery_key")]
    HumanRecoveryKey { human: String, key: HumanKey },
    /// Replaces a lost key. Signed with the human's recovery key, which is
    /// used up by it.
    #[serde(rename = "human.recover")]
    HumanRecover { human: String, key: HumanKey },
    /// Replaces the daemon key (Ed25519, hex). Signed by the old daemon key
    /// and by a human; later entries are signed by the new key.
    #[serde(rename = "daemon.rotate")]
    DaemonRotate { key: String },
    /// Re-anchors the log on a new daemon key when the old one is gone (or
    /// belongs to a daemon that is being replaced). Signed by the new key
    /// and by a human.
    #[serde(rename = "daemon.recover")]
    DaemonRecover { key: String },
    #[serde(rename = "policy.update")]
    PolicyUpdate { policy: GitId },
    #[serde(rename = "session.start")]
    SessionStart {
        session: String,
        agent: String,
        model: Option<String>,
        task: String,
        scope: Scope,
        expires_ms: i64,
        /// The session whose workspace this one takes over.
        #[serde(default, skip_serializing_if = "Option::is_none")]
        continues: Option<Continuation>,
        /// The harness the human runs the session under. Their word, and the
        /// only one an observation level is read for (format 20).
        #[serde(default, skip_serializing_if = "Option::is_none")]
        harness: Option<String>,
    },
    #[serde(rename = "session.bind")]
    SessionBind {
        session: String,
        checks: BindChecks,
        level: ObservationLevel,
    },
    /// A human's word that the session runs in a sandbox Lonca built: what
    /// gives it `level: isolated`. Never from the agent, and never from the
    /// daemon, which can not see the sandbox: the launcher builds it and the
    /// human who started the session signs for it.
    #[serde(rename = "session.isolate")]
    SessionIsolate {
        session: String,
        /// The kind of sandbox (`seatbelt`, `bwrap`).
        sandbox: String,
        /// SHA-256 of the sandbox's definition, lowercase hex.
        profile: String,
    },
    #[serde(rename = "session.end")]
    SessionEnd { session: String, reason: String },
    #[serde(rename = "cap.attenuate")]
    CapAttenuate {
        parent: String,
        child: String,
        agent: String,
        scope: Scope,
        expires_ms: i64,
        /// Lease sets the parent hands to the child; they come back when
        /// the child ends.
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        leases: Vec<String>,
    },
    #[serde(rename = "cap.revoke")]
    CapRevoke { session: String, reason: String },
    #[serde(rename = "lease.acquire")]
    LeaseAcquire {
        set: String,
        items: Vec<LeaseItem>,
        intent: String,
        ttl_ms: i64,
    },
    #[serde(rename = "lease.release")]
    LeaseRelease { set: String },
    #[serde(rename = "lease.renew")]
    LeaseRenew { set: String, ttl_ms: i64 },
    /// Holds nothing: when what conflicts with `items` is freed, the oldest
    /// interest gets a short reservation. Until then, for a limited time,
    /// no new lease can be taken that conflicts with `items`.
    #[serde(rename = "lease.interest")]
    LeaseInterest {
        items: Vec<LeaseItem>,
        intent: String,
    },
    #[serde(rename = "change.propose")]
    ChangePropose {
        change: ChangeId,
        commit: GitId,
        base: GitId,
        claim: Claim,
    },
    #[serde(rename = "change.withdraw")]
    ChangeWithdraw { change: ChangeId },
    #[serde(rename = "change.integrate")]
    ChangeIntegrate {
        change: ChangeId,
        trunk_before: GitId,
        /// The merged trunk commit, or `None` when the merge conflicted.
        merged: Option<GitId>,
        conflicts: Vec<String>,
    },
    /// Changes at the head of the queue, integrated at once. One entry, so
    /// that a crash can not leave trunk at a state nothing verified: the
    /// evidence is of the last commit, and trunk moves straight to it. The
    /// commits before it are in its history and were never trunk.
    #[serde(rename = "change.integrate_batch")]
    ChangeIntegrateBatch {
        trunk_before: GitId,
        /// Each change, in queue order, with the commit it merged to.
        merged: Vec<(ChangeId, GitId)>,
    },
    /// `checks`: named checks of `verification.checks` the change owes from
    /// here on, besides what it owed already; only on a proposal (log
    /// format 25). Sorted, each name once.
    #[serde(rename = "approve")]
    Approve {
        target_seq: u64,
        note: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        checks: Vec<String>,
    },
    /// A human declines an escalated operation; the reason is for the
    /// requester (a worker fixes and proposes again) and is never
    /// interpreted.
    #[serde(rename = "reject")]
    Reject { target_seq: u64, reason: String },
    #[serde(rename = "observe")]
    Observe {
        session: String,
        events: Vec<Observation>,
    },
    #[serde(rename = "taint")]
    Taint { session: String, reason: String },
    #[serde(rename = "import.external")]
    ImportExternal {
        remote: String,
        commit: GitId,
        trunk_before: GitId,
    },
    #[serde(rename = "import.unmediated")]
    ImportUnmediated {
        refname: String,
        found: Option<GitId>,
        expected: Option<GitId>,
    },
    #[serde(rename = "bridge.push")]
    BridgePush {
        remote: String,
        oplog_seq: u64,
        oplog_hash: String,
    },
    #[serde(rename = "freeze")]
    Freeze { reason: String },
    #[serde(rename = "unfreeze")]
    Unfreeze { reason: String },
    #[serde(rename = "negotiate")]
    Negotiate {
        thread: String,
        to_session: String,
        message: NegotiationMsg,
    },
    #[serde(rename = "redact")]
    Redact { blob: GitId, reason: String },
}

impl Op {
    pub fn kind(&self) -> OpKind {
        match self {
            Op::RepoInit { .. } => OpKind::RepoInit,
            Op::HumanEnroll { .. } => OpKind::HumanEnroll,
            Op::HumanRotate { .. } => OpKind::HumanRotate,
            Op::HumanRevoke { .. } => OpKind::HumanRevoke,
            Op::HumanRecoveryKey { .. } => OpKind::HumanRecoveryKey,
            Op::HumanRecover { .. } => OpKind::HumanRecover,
            Op::DaemonRotate { .. } => OpKind::DaemonRotate,
            Op::DaemonRecover { .. } => OpKind::DaemonRecover,
            Op::PolicyUpdate { .. } => OpKind::PolicyUpdate,
            Op::SessionStart { .. } => OpKind::SessionStart,
            Op::SessionBind { .. } => OpKind::SessionBind,
            Op::SessionIsolate { .. } => OpKind::SessionIsolate,
            Op::SessionEnd { .. } => OpKind::SessionEnd,
            Op::CapAttenuate { .. } => OpKind::CapAttenuate,
            Op::CapRevoke { .. } => OpKind::CapRevoke,
            Op::LeaseAcquire { .. } => OpKind::LeaseAcquire,
            Op::LeaseRelease { .. } => OpKind::LeaseRelease,
            Op::LeaseRenew { .. } => OpKind::LeaseRenew,
            Op::LeaseInterest { .. } => OpKind::LeaseInterest,
            Op::ChangePropose { .. } => OpKind::ChangePropose,
            Op::ChangeWithdraw { .. } => OpKind::ChangeWithdraw,
            Op::ChangeIntegrate { .. } => OpKind::ChangeIntegrate,
            Op::ChangeIntegrateBatch { .. } => OpKind::ChangeIntegrateBatch,
            Op::Approve { .. } => OpKind::Approve,
            Op::Reject { .. } => OpKind::Reject,
            Op::Observe { .. } => OpKind::Observe,
            Op::Taint { .. } => OpKind::Taint,
            Op::ImportExternal { .. } => OpKind::ImportExternal,
            Op::ImportUnmediated { .. } => OpKind::ImportUnmediated,
            Op::BridgePush { .. } => OpKind::BridgePush,
            Op::Freeze { .. } => OpKind::Freeze,
            Op::Unfreeze { .. } => OpKind::Unfreeze,
            Op::Negotiate { .. } => OpKind::Negotiate,
            Op::Redact { .. } => OpKind::Redact,
        }
    }

    /// The daemon key this operation installs, if it is a key switch.
    pub fn daemon_key_switch(&self) -> Option<&str> {
        match self {
            Op::DaemonRotate { key } | Op::DaemonRecover { key } => Some(key),
            _ => None,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
    Allow,
    Deny,
    Escalate,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision {
    pub outcome: Outcome,
    /// Machine-readable reason codes such as `lease_missing:src/a.rs#f`.
    pub reasons: Vec<String>,
}

impl Decision {
    pub fn allow() -> Self {
        Self {
            outcome: Outcome::Allow,
            reasons: vec![],
        }
    }

    pub fn deny(reason: impl Into<String>) -> Self {
        Self {
            outcome: Outcome::Deny,
            reasons: vec![reason.into()],
        }
    }

    pub fn escalate(reason: impl Into<String>) -> Self {
        Self {
            outcome: Outcome::Escalate,
            reasons: vec![reason.into()],
        }
    }

    pub fn is_allow(&self) -> bool {
        self.outcome == Outcome::Allow
    }
}

/// A human's signature over an operation. The signed message is the
/// canonical JSON of [`HumanIntent`].
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct HumanSignature {
    pub human: String,
    pub nonce: String,
    pub issued_ms: i64,
    pub sig: String,
}

#[derive(Serialize)]
pub struct HumanIntent<'a> {
    pub purpose: &'static str,
    pub repo: &'a str,
    pub op: &'a Op,
    pub nonce: &'a str,
    pub issued_ms: i64,
}

impl<'a> HumanIntent<'a> {
    pub fn new(repo: &'a str, op: &'a Op, nonce: &'a str, issued_ms: i64) -> Self {
        Self {
            purpose: "lonca-human-intent-v1",
            repo,
            op,
            nonce,
            issued_ms,
        }
    }
}

/// What a human signs to open a human session (`human.login`). Its own
/// purpose string keeps a login signature from ever passing as an
/// operation signature, and the other way round.
#[derive(Serialize)]
pub struct LoginIntent<'a> {
    pub purpose: &'static str,
    /// The repository whose enrolment verifies the key; `None` lets the
    /// daemon look in every repository.
    pub repo: Option<&'a str>,
    pub human: &'a str,
    pub ttl_ms: i64,
    pub nonce: &'a str,
    pub issued_ms: i64,
}

impl<'a> LoginIntent<'a> {
    pub fn new(
        repo: Option<&'a str>,
        human: &'a str,
        ttl_ms: i64,
        nonce: &'a str,
        issued_ms: i64,
    ) -> Self {
        Self {
            purpose: "lonca-human-login-v1",
            repo,
            human,
            ttl_ms,
            nonce,
            issued_ms,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// The names requests use are the names in entries.
    #[test]
    fn lease_kinds_are_named_as_they_are_written() {
        for kind in LeaseKind::ALL {
            let written = serde_json::to_value(kind).unwrap();
            assert_eq!(written, kind.as_str());
            assert_eq!(LeaseKind::from_name(kind.as_str()), Some(kind));
        }
        assert_eq!(LeaseKind::from_name("Shared"), None);
        assert_eq!(LeaseKind::from_name(""), None);
    }

    #[test]
    fn a_change_reaches_others_by_any_one_of_three() {
        assert!(!Facts::default().reaches_others());
        let signature = Facts {
            signature_changes: vec!["a.rs#f".into()],
            ..Facts::default()
        };
        let mentioned = Facts {
            affected: vec!["a.rs#g".into()],
            ..Facts::default()
        };
        let cut_off = Facts {
            affected_more: 1,
            ..Facts::default()
        };
        assert!(signature.reaches_others());
        assert!(mentioned.reaches_others());
        assert!(cut_off.reaches_others());
    }

    /// Left out when empty, so older entries keep their bytes; kept when not.
    #[test]
    fn what_was_cut_from_affected_is_written_out() {
        let none = serde_json::to_string(&Facts::default()).unwrap();
        assert!(!none.contains("affected"), "{none}");
        let cut_off = Facts {
            affected_more: 3,
            ..Facts::default()
        };
        let text = serde_json::to_string(&cut_off).unwrap();
        assert!(text.contains("\"affected_more\":3"), "{text}");
    }

    #[test]
    fn unknown_op_kind_is_rejected() {
        assert!(serde_json::from_str::<OpKind>("\"change.propose\"").is_ok());
        assert!(serde_json::from_str::<OpKind>("\"admin.everything\"").is_err());
    }

    #[test]
    fn op_kind_names_match_serde() {
        for k in [
            OpKind::RepoInit,
            OpKind::ChangePropose,
            OpKind::LeaseAcquire,
            OpKind::Redact,
            OpKind::SessionBind,
            OpKind::SessionIsolate,
            OpKind::HumanRotate,
            OpKind::HumanRevoke,
            OpKind::HumanRecoveryKey,
            OpKind::HumanRecover,
            OpKind::DaemonRotate,
            OpKind::DaemonRecover,
        ] {
            assert_eq!(
                serde_json::to_string(&k).unwrap(),
                format!("\"{}\"", k.as_str())
            );
        }
    }

    #[test]
    fn reserved_ops_are_not_grantable() {
        for k in [
            OpKind::PolicyUpdate,
            OpKind::Approve,
            OpKind::CapRevoke,
            OpKind::Redact,
            OpKind::HumanEnroll,
            OpKind::HumanRotate,
            OpKind::HumanRevoke,
            OpKind::HumanRecoveryKey,
            OpKind::HumanRecover,
            OpKind::DaemonRotate,
            OpKind::DaemonRecover,
            OpKind::Freeze,
            OpKind::ImportExternal,
        ] {
            assert!(!k.agent_grantable(), "{k:?}");
        }
    }
}