lean-ctx 3.12.9

LeanCTX | OSS: the open-source runtime for the Context Gateway for AI agents. CLI, MCP server, and supported proxy integrations.
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
// SPDX-License-Identifier: Apache-2.0
//! Background sync and policy enforcement evidence (3.12.3), as Local Read
//! kinds. Read only, from local files; nothing contacts Cloud.
//!
//! - `cloud.sync`: `lean-ctx cloud sync status --json`, the daemon's
//!   background vault sync (`<data>/cloud/sync/status.json`).
//! - `policy.sessions`: `lean-ctx cloud policy sessions --json`, the policy
//!   installed here and which version each app session ran under.
//! - `policy.session`: `lean-ctx cloud policy session --session ID --json`,
//!   one session's evidence with its receipt ids.
//!
//! Wired as one `local_read::Part`: its routes, kinds and commands are
//! chained into the route table and the capabilities builder.

use serde::Serialize;

use lean_ctx_cloud::CloudStore;
use lean_ctx_cloud::capture::sharing::RejectedFact;
use lean_ctx_cloud::policy_sessions::{self, AppliedRow, Installed};
use lean_ctx_cloud::sync::auto::{SyncStatus, VaultSyncStatus};

use super::routes::{Method, Project, Route};
use super::{Answer, Code, Failure, Via};

/// This part's routes (Route as in `routes.rs`).
pub(crate) const ROUTES: &[Route] = &[
    Route {
        kind: "cloud.sync",
        method: Method::Get,
        path: "/v1/local/cloud/sync",
        cli: &["cloud", "sync", "status", "--json"],
        action: &["status"],
        params: &[],
        project: Project::None,
        names: None,
    },
    Route {
        kind: "policy.sessions",
        method: Method::Get,
        path: "/v1/local/cloud/policy/sessions",
        cli: &["cloud", "policy", "sessions", "--json"],
        action: &["sessions"],
        params: &[],
        project: Project::None,
        names: None,
    },
    Route {
        kind: "policy.session",
        method: Method::Get,
        path: "/v1/local/cloud/policy/sessions/{id}",
        cli: &["cloud", "policy", "session", "<id>", "--json"],
        action: &["session"],
        params: &[],
        project: Project::None,
        names: None,
    },
];

/// This part's document kinds.
pub(crate) const KINDS: &[&str] = &["cloud.sync", "policy.session", "policy.sessions"];

/// Command path → CLI verbs, as `capabilities::COMMANDS`.
pub(crate) const COMMANDS: &[(&str, &[&str])] = &[
    ("cloud.policy", &["session", "sessions"]),
    ("cloud.sync", &["status"]),
];

#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct SyncDoc {
    pub running: bool,
    pub interval_s: u64,
    pub last_pass_at: Option<String>,
    /// What leaves with this machine's Cloud data on a sign out (3.12.8):
    /// session drafts in the outbox (queued or failed) plus every vault's
    /// signed events still waiting for Cloud (`vaults[].pending`).
    pub unsent_drafts: u64,
    pub vaults: Vec<VaultRow>,
}

/// One vault's sync state, with the facts the capture gate kept on this
/// machine (fix them and remember again; the next sync shares them).
#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct VaultRow {
    #[serde(flatten)]
    pub status: VaultSyncStatus,
    pub rejected: Vec<RejectedFact>,
}

/// The `cloud.sync` body of `status` at `now`; `rejected` gives a vault's
/// rejected facts still waiting for a fix, `outbox` the session drafts not
/// yet sent.
pub(crate) fn sync_doc(
    status: SyncStatus,
    now: lean_ctx_time::UtcDateTime,
    rejected: &dyn Fn(&str) -> Vec<RejectedFact>,
    outbox: u64,
) -> SyncDoc {
    SyncDoc {
        running: status.running_at(now),
        interval_s: status.interval_s,
        unsent_drafts: outbox + status.vaults.iter().map(|v| v.pending).sum::<u64>(),
        last_pass_at: status.last_pass_at,
        vaults: status
            .vaults
            .into_iter()
            .map(|status| VaultRow {
                rejected: rejected(&status.vault_id),
                status,
            })
            .collect(),
    }
}

/// Session drafts in the knowledge outbox, queued or failed.
fn outbox_drafts(cloud_data: &std::path::Path) -> u64 {
    use lean_ctx_cloud::outbox::{CHANNEL_KNOWLEDGE, Outbox};
    let depth = Outbox::at(cloud_data).depth(CHANNEL_KNOWLEDGE);
    (depth.pending + depth.failed) as u64
}

/// The rejected facts of the vault `vault_id` (a place id) on this machine.
fn rejected_of(cloud_data: &std::path::Path, vault_id: &str) -> Vec<RejectedFact> {
    use lean_ctx_cloud::sync::place::place_id;
    let map = lean_ctx_cloud::vault::VaultMap::load(cloud_data);
    let knowledge = map
        .vaults
        .values()
        .find(|record| place_id(&record.vault_id) == vault_id)
        .and_then(lean_ctx_cloud::sync::auto::root_of)
        .and_then(|root| lean_ctx::core::knowledge::ProjectKnowledge::load(&root));
    lean_ctx_cloud::capture::sharing::rejected_now(cloud_data, vault_id, knowledge.as_ref())
}

#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct InstalledDoc {
    pub version: Option<u64>,
    pub digest: Option<String>,
    pub key_id: Option<String>,
    pub signature: String,
    pub state: String,
    pub acked: bool,
    pub acked_at: Option<String>,
    /// `nudge`, `poll` or `manual`: what started the last policy pull.
    pub pulled_via: Option<String>,
    pub last_pull_at: Option<String>,
    /// Cloud sends policy nudges: a change applies at once; otherwise the
    /// periodic pull brings it (within `pull_interval_minutes`).
    pub nudges: bool,
    /// One row per vault the policy reaches on this machine (sorted by
    /// `vault_id`): every vault linked here, and every vault with a
    /// vault-level entry in the installed policy.
    pub vaults: Vec<PolicyVault>,
    /// Whether this policy is enforced here, and by which sessions.
    pub enforcement: Enforcement,
}

/// The installed policy as enforced on this machine (D4).
#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct Enforcement {
    /// The version enforced: verified, in force and acknowledged by Cloud
    /// for this version and digest; `null` otherwise (an expired,
    /// unverified or unacknowledged policy is not reported as enforced).
    pub applied_version: Option<u64>,
    /// When Cloud acknowledged that version (`null` with it).
    pub acknowledged_at: Option<String>,
    /// The live app sessions (a receipt within the last 15 minutes) whose
    /// newest receipt was made under the installed verified policy in
    /// force; sorted.
    pub sessions: Vec<String>,
    /// When this Instance reported `applied_version` to Cloud on the bus
    /// (`policy.applied`); `null` until then.
    pub reported_at: Option<String>,
}

fn enforcement(
    cloud_data: &std::path::Path,
    installed: &Installed,
    now: lean_ctx_time::UtcDateTime,
) -> Enforcement {
    let enforced =
        installed.signature == "verified" && installed.state == "in_force" && installed.acked;
    let applied_version = installed.version.filter(|_| enforced);
    let reported_at = policy_sessions::load_reported(cloud_data)
        .filter(|r| {
            Some(r.version) == applied_version && installed.digest.as_ref() == Some(&r.digest)
        })
        .map(|r| r.reported_at);
    Enforcement {
        applied_version,
        acknowledged_at: installed.acked_at.clone().filter(|_| enforced),
        sessions: policy_sessions::enforcing(cloud_data, installed, now),
        reported_at,
    }
}

/// One vault under the installed policy, so a reader matches a project
/// (one of `roots`) to the policy that governs it.
#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct PolicyVault {
    pub vault_id: String,
    /// The vault's workspace namespace and name; `null` for a vault only
    /// the policy names (no checkout here is linked to it).
    pub ns: Option<String>,
    pub name: Option<String>,
    /// The local checkouts linked to it (sorted).
    pub roots: Vec<String>,
    /// `vault`: the policy's own entry for this vault applies; `default`:
    /// the workspace default applies; `unverified`: the installed policy
    /// files cannot be checked (fails closed, see `signature`).
    pub policy: &'static str,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct SessionRow {
    pub session_id: String,
    pub version: Option<u64>,
    pub digest: Option<String>,
    pub signature: String,
    pub state: String,
    pub receipts: u64,
    pub last_at: String,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct SessionsDoc {
    /// `null`: no Cloud policy installed on this machine.
    pub installed: Option<InstalledDoc>,
    pub sessions: Vec<SessionRow>,
}

#[derive(Debug, Serialize, PartialEq, Eq)]
pub(crate) struct SessionDoc {
    pub session_id: String,
    pub applied: Vec<AppliedRow>,
}

/// The vaults of `map` and of the installed policy's vault-level entries
/// (`vault_entries`: `None` when the installed files cannot be checked).
fn policy_vaults(
    map: &lean_ctx::core::vault_map::VaultMap,
    vault_entries: Option<&[String]>,
) -> Vec<PolicyVault> {
    let policy_of = |vault_id: &str| match vault_entries {
        Some(ids) if ids.iter().any(|id| id == vault_id) => "vault",
        Some(_) => "default",
        None => "unverified",
    };
    let mut rows: std::collections::BTreeMap<String, PolicyVault> =
        std::collections::BTreeMap::new();
    for record in map.vaults.values() {
        let row = rows
            .entry(record.vault_id.clone())
            .or_insert_with(|| PolicyVault {
                vault_id: record.vault_id.clone(),
                ns: Some(record.ns.clone()),
                name: Some(record.name.clone()),
                roots: Vec::new(),
                policy: policy_of(&record.vault_id),
            });
        for root in &record.roots {
            if !row.roots.contains(root) {
                row.roots.push(root.clone());
            }
        }
        row.roots.sort();
    }
    for vault_id in vault_entries.unwrap_or_default() {
        rows.entry(vault_id.clone()).or_insert_with(|| PolicyVault {
            vault_id: vault_id.clone(),
            ns: None,
            name: None,
            roots: Vec::new(),
            policy: "vault",
        });
    }
    rows.into_values().collect()
}

/// The vault ids with a vault-level entry in the policy installed in
/// `cloud_data` (empty: none installed); `None` when the installed files
/// cannot be checked.
fn vault_entries(cloud_data: &std::path::Path) -> Option<Vec<String>> {
    use lean_ctx_cloud::effective_floor;
    let (floor, required) =
        effective_floor::read(&effective_floor::floor_dir_in(cloud_data)).ok()?;
    let floor = effective_floor::check(floor.as_deref(), required.as_deref()).ok()?;
    Some(floor.map_or_else(Vec::new, |floor| {
        floor
            .policy
            .vaults
            .into_iter()
            .map(|v| v.vault_id)
            .collect()
    }))
}

fn installed_doc(
    installed: Installed,
    pulls: &lean_ctx_cloud::policy::PullRecord,
    vaults: Vec<PolicyVault>,
    enforcement: Enforcement,
) -> Option<InstalledDoc> {
    if installed.signature == "none" {
        return None;
    }
    let via = pulls.pulled_via.and_then(|via| {
        serde_json::to_value(via)
            .ok()
            .and_then(|v| v.as_str().map(str::to_owned))
    });
    Some(InstalledDoc {
        version: installed.version,
        digest: installed.digest,
        key_id: installed.key_id,
        signature: installed.signature,
        state: installed.state,
        acked: installed.acked,
        acked_at: installed.acked_at,
        pulled_via: via,
        last_pull_at: pulls.last_pull_at.clone(),
        nudges: pulls.nudges_seen,
        vaults,
        enforcement,
    })
}

/// The `policy.sessions` body of the Cloud data in `cloud_data` at `now`.
pub(crate) fn sessions_doc(
    cloud_data: &std::path::Path,
    now: lean_ctx_time::UtcDateTime,
) -> SessionsDoc {
    let policy = policy_sessions::installed(cloud_data, now);
    let enforced = enforcement(cloud_data, &policy, now);
    let installed = installed_doc(
        policy,
        &lean_ctx_cloud::policy::load_pulls(cloud_data),
        policy_vaults(
            &lean_ctx::core::vault_map::VaultMap::load(cloud_data),
            vault_entries(cloud_data).as_deref(),
        ),
        enforced,
    );
    let sessions = policy_sessions::list(cloud_data)
        .into_iter()
        .filter_map(|evidence| {
            let row = evidence.applied.last()?;
            Some(SessionRow {
                session_id: evidence.session_id.clone(),
                version: row.version,
                digest: row.digest.clone(),
                signature: row.signature.clone(),
                state: row.state.clone(),
                receipts: evidence.applied.iter().map(|r| r.receipts).sum(),
                last_at: row.last_at.clone(),
            })
        })
        .collect();
    SessionsDoc {
        installed,
        sessions,
    }
}

/// The Cloud store, its data scoped to the enrolled account first (3.12.8):
/// sync status, drafts and session evidence of another account are removed
/// before they are read.
fn store() -> Result<CloudStore, Failure> {
    let store = CloudStore::open().map_err(Failure::from)?;
    store.instance().map_err(Failure::from)?;
    Ok(store)
}

/// The session a `policy session` request names: `--session ID`, or the
/// positional after `session` (the route's `{id}`).
fn named_session(args: &[String]) -> Result<String, Failure> {
    let raw = match super::flag(args, "--session")? {
        Some(id) => id.to_owned(),
        None => super::positionals(args)
            .into_iter()
            .skip_while(|word| *word != "session")
            .nth(1)
            .map(str::to_owned)
            .ok_or_else(|| Failure::invalid("policy session needs --session ID"))?,
    };
    policy_sessions::session_id(&raw)
        .ok_or_else(|| Failure::invalid("a session id is 1-128 characters of A-Z a-z 0-9 . _ : -"))
}

/// The release root of these machine-wide documents: no single project.
const NO_PROJECT: &str = "";

/// The document of `kind` for `args` (same bytes on the CLI and the
/// socket), released through the tool run of its tool (one receipt for the
/// bytes sent).
pub(crate) fn answer(kind: &str, args: &[String], via: Via) -> Answer {
    let now = lean_ctx_time::UtcDateTime::now();
    match kind {
        "cloud.sync" => super::answer_released("ctx_agent", NO_PROJECT, kind, via, || {
            let store = store()?;
            Ok(sync_doc(
                lean_ctx_cloud::sync::auto::load_status(store.data_dir()),
                now,
                &|vault| rejected_of(store.data_dir(), vault),
                outbox_drafts(store.data_dir()),
            ))
        }),
        "policy.sessions" => super::answer_released("ctx_policy", NO_PROJECT, kind, via, || {
            let store = store()?;
            Ok(sessions_doc(store.data_dir(), now))
        }),
        "policy.session" => super::answer_released("ctx_policy", NO_PROJECT, kind, via, || {
            let id = named_session(args)?;
            let store = store()?;
            let evidence = policy_sessions::load(store.data_dir(), &id).ok_or_else(|| {
                Failure::new(Code::NotFound, format!("no evidence for session {id}"))
            })?;
            Ok(SessionDoc {
                session_id: evidence.session_id,
                applied: evidence.applied,
            })
        }),
        other => Answer::failure(&Failure::invalid(format!("unknown kind {other}"))),
    }
}

/// `lean-ctx cloud sync status --json`.
pub(crate) fn run_sync_status(args: &[String]) {
    super::print(&answer("cloud.sync", args, Via::Cli));
}

/// `lean-ctx cloud policy sessions|session … --json`.
pub(crate) fn run_policy(args: &[String]) {
    let kind = match super::positionals(args).first().copied() {
        Some("sessions") => "policy.sessions",
        Some("session") => "policy.session",
        _ => super::fail(&Failure::invalid(
            "usage: lean-ctx cloud policy sessions --json | session --session ID --json",
        )),
    };
    super::print(&answer(kind, args, Via::Cli));
}

#[cfg(test)]
mod tests {
    use super::*;
    use lean_ctx::core::vault_map::{VaultMap, VaultRecord};

    #[test]
    fn a_stale_status_is_not_running_and_vaults_pass_through() {
        let now = lean_ctx_time::UtcDateTime::now();
        let status = SyncStatus {
            interval_s: 300,
            heartbeat_at: Some(
                (now - lean_ctx_time::Duration::seconds(600))
                    .to_rfc3339_opts(lean_ctx_time::SecondsFormat::Secs, true),
            ),
            vaults: vec![VaultSyncStatus {
                vault_id: "v".into(),
                pending: 4,
                ..VaultSyncStatus::default()
            }],
            ..SyncStatus::default()
        };
        let doc = sync_doc(status, now, &|_| Vec::new(), 2);
        assert!(!doc.running);
        assert_eq!(doc.vaults[0].status.pending, 4);
        assert_eq!(doc.unsent_drafts, 6, "2 outbox drafts + 4 vault events");
        let text = super::super::document("cloud.sync", &doc);
        let value: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(value["contract"], "local-read/1");
        assert_eq!(value["kind"], "cloud.sync");
        assert!(value["last_pass_at"].is_null(), "nulls are printed: {text}");
        assert_eq!(value["vaults"][0]["pending"], 4, "the row is flat: {text}");
        assert_eq!(value["vaults"][0]["rejected"], serde_json::json!([]));
        assert!(value["vaults"][0]["code"].is_null(), "{text}");
        assert!(value["vaults"][0]["retry_after_s"].is_null(), "{text}");
    }

    /// Cloud contact findings D: a failed vault names its failure as a
    /// machine-readable `code` and the cloud's wait as `retry_after_s`.
    #[test]
    fn a_failed_vault_carries_its_code_and_retry_after() {
        let now = lean_ctx_time::UtcDateTime::now();
        let status = SyncStatus {
            vaults: vec![VaultSyncStatus {
                vault_id: "v".into(),
                failures: 2,
                error: Some("the cloud asks to retry in 420 s".into()),
                code: Some("rate_limited".into()),
                retry_after_s: Some(420),
                ..VaultSyncStatus::default()
            }],
            ..SyncStatus::default()
        };
        let doc = sync_doc(status, now, &|_| Vec::new(), 2);
        let text = super::super::document("cloud.sync", &doc);
        let value: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(value["vaults"][0]["code"], "rate_limited", "{text}");
        assert_eq!(value["vaults"][0]["retry_after_s"], 420, "{text}");
    }

    #[test]
    fn no_policy_installed_is_null_and_sessions_list_empty() {
        let dir = tempfile::tempdir().unwrap();
        let doc = sessions_doc(dir.path(), lean_ctx_time::UtcDateTime::now());
        assert_eq!(
            doc,
            SessionsDoc {
                installed: None,
                sessions: Vec::new()
            }
        );
    }

    #[test]
    fn a_session_is_named_by_flag_or_segment_and_checked() {
        let args = |words: &[&str]| words.iter().map(|w| (*w).to_owned()).collect::<Vec<_>>();
        assert_eq!(
            named_session(&args(&["session", "--session", "app-1", "--json"])).unwrap(),
            "app-1"
        );
        assert_eq!(
            named_session(&args(&["session", "app-2", "--json"])).unwrap(),
            "app-2"
        );
        assert!(named_session(&args(&["session", "--json"])).is_err());
        assert!(named_session(&args(&["session", "../x", "--json"])).is_err());
    }

    /// pp7: the policy evidence is on the local service and in the one
    /// capabilities document (route, kind, command verb on both transports).
    #[test]
    fn the_policy_and_sync_routes_are_served_and_listed() {
        use super::super::{capabilities, routes};
        let table = routes::table_json();
        for (kind, path, command, verb) in [
            (
                "policy.sessions",
                "/v1/local/cloud/policy/sessions",
                "cloud.policy",
                "sessions",
            ),
            (
                "policy.session",
                "/v1/local/cloud/policy/sessions/{id}",
                "cloud.policy",
                "session",
            ),
            ("cloud.sync", "/v1/local/cloud/sync", "cloud.sync", "status"),
        ] {
            let route = routes::all()
                .find(|r| r.path == path)
                .unwrap_or_else(|| panic!("{path} is routed"));
            assert_eq!(route.kind, kind);
            let row = table
                .as_array()
                .unwrap()
                .iter()
                .find(|r| r["path"] == path)
                .unwrap_or_else(|| panic!("{path} is in capabilities.routes"));
            assert_eq!(row["kind"], kind);
            assert_eq!(row["method"], "GET");
            assert_eq!(row["command"], command, "{row}");
            assert_eq!(row["verb"], verb, "{row}");
            let commands = capabilities::commands_of(&table);
            assert!(
                commands[command].as_array().is_some_and(|verbs| verbs
                    .contains(&serde_json::json!({"verb": verb, "via": ["cli", "socket"]}))),
                "{command} {verb}: {commands:?}"
            );
            assert!(
                capabilities::build_with(table.clone(), routes::events_json())
                    .kinds
                    .contains(&kind),
                "{kind} in capabilities.kinds"
            );
        }
        // The `{id}` segment becomes the session the CLI reads.
        let one = routes::all()
            .find(|r| r.kind == "policy.session")
            .expect("routed");
        let args = routes::args_of(one, Some("app-1"), &[]).expect("args");
        assert_eq!(named_session(&args).unwrap(), "app-1");
        // The socket dispatch reaches this part's builder.
        assert!(super::super::part_of("policy.sessions").is_some());
    }

    fn record(vault_id: &str, ns: &str, name: &str, roots: &[&str]) -> VaultRecord {
        VaultRecord {
            vault_id: vault_id.into(),
            ns: ns.into(),
            name: name.into(),
            sealed: false,
            roots: roots.iter().map(|r| (*r).to_owned()).collect(),
        }
    }

    /// Install a verified floor with vault-level entries for `vaults`.
    fn install_floor(cloud_data: &std::path::Path, vaults: &[&str]) {
        use lean_ctx_cloud::effective_floor::{
            FLOOR_FILE, FLOOR_SCHEMA, Floor, REQUIRED_FILE, floor_dir_in,
        };
        use lean_ctx_protocol::cloud::effective_policy::{
            BundlePolicyV1, EffectivePolicyV1, POLICY_DOCUMENT_SCHEMA, VaultEffectivePolicyV1,
        };
        let effective = || EffectivePolicyV1 {
            schema: POLICY_DOCUMENT_SCHEMA.into(),
            security: Default::default(),
            context: Default::default(),
            sharing: Default::default(),
            agents: None,
            orchestrator: None,
            other: Default::default(),
        };
        let floor = Floor {
            schema: FLOOR_SCHEMA.into(),
            version: 3,
            digest: format!("sha256:{}", "a".repeat(64)),
            not_after: "2999-01-01T00:00:00Z".into(),
            policy: BundlePolicyV1 {
                effective: effective(),
                vaults: vaults
                    .iter()
                    .map(|id| VaultEffectivePolicyV1 {
                        vault_id: (*id).into(),
                        effective: effective(),
                    })
                    .collect(),
            },
        };
        let dir = floor_dir_in(cloud_data);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join(FLOOR_FILE), serde_json::to_vec(&floor).unwrap()).unwrap();
        std::fs::write(dir.join(REQUIRED_FILE), br#"{"version":3}"#).unwrap();
    }

    /// pp7: `installed` names the vaults it governs, with the checkouts
    /// linked to each, so a project is matched to its policy.
    #[test]
    fn installed_lists_each_vault_with_its_roots_and_which_policy_applies() {
        let dir = tempfile::tempdir().unwrap();
        install_floor(dir.path(), &["v_team", "v_elsewhere"]);
        let mut map = VaultMap::default();
        map.vaults.insert(
            "stable-api".into(),
            record("v_team", "platform", "api", &["/code/api-wt", "/code/api"]),
        );
        map.vaults.insert(
            "stable-web".into(),
            record("v_open", "platform", "web", &["/code/web"]),
        );
        map.save(dir.path()).unwrap();

        let doc = sessions_doc(dir.path(), lean_ctx_time::UtcDateTime::now());
        let installed = doc
            .installed
            .as_ref()
            .expect("a verified policy is installed");
        assert_eq!(installed.version, Some(3));
        assert_eq!(installed.signature, "verified");
        assert_eq!(
            installed.vaults,
            vec![
                PolicyVault {
                    vault_id: "v_elsewhere".into(),
                    ns: None,
                    name: None,
                    roots: Vec::new(),
                    policy: "vault",
                },
                PolicyVault {
                    vault_id: "v_open".into(),
                    ns: Some("platform".into()),
                    name: Some("web".into()),
                    roots: vec!["/code/web".into()],
                    policy: "default",
                },
                PolicyVault {
                    vault_id: "v_team".into(),
                    ns: Some("platform".into()),
                    name: Some("api".into()),
                    roots: vec!["/code/api".into(), "/code/api-wt".into()],
                    policy: "vault",
                },
            ]
        );
        let text = super::super::document("policy.sessions", &doc);
        let value: serde_json::Value = serde_json::from_str(&text).unwrap();
        assert_eq!(
            value["installed"]["vaults"][2]["vault_id"], "v_team",
            "{text}"
        );
        assert!(value["installed"]["vaults"][0]["ns"].is_null(), "{text}");
    }

    /// D4: `installed.enforcement` names the version enforced, when Cloud
    /// acknowledged it, the live sessions under it and the bus report.
    #[test]
    fn installed_says_which_version_is_enforced_and_by_which_sessions() {
        use lean_ctx_cloud::policy_sessions::{Reported, note_reported, record};
        let dir = tempfile::tempdir().unwrap();
        let now = lean_ctx_time::UtcDateTime::now();
        install_floor(dir.path(), &[]);
        record(dir.path(), "app-live", "r1", now);
        record(
            dir.path(),
            "app-old",
            "r2",
            now - lean_ctx_time::Duration::hours(1),
        );
        let enforcement = |doc: &SessionsDoc| {
            let text = super::super::document("policy.sessions", doc);
            serde_json::from_str::<serde_json::Value>(&text).unwrap()["installed"]["enforcement"]
                .clone()
        };
        assert_eq!(
            enforcement(&sessions_doc(dir.path(), now)),
            serde_json::json!({"applied_version": null, "acknowledged_at": null,
                "sessions": ["app-live"], "reported_at": null}),
            "verified and in force, not yet acknowledged by LeanCTX Cloud"
        );
        let digest = format!("sha256:{}", "a".repeat(64));
        let state = serde_json::json!({"effective": {
            "version": 3, "digest": digest, "issued_at": "", "not_after": "",
            "key_id": "k_policy", "unenforced": [],
            "ack": {"version": 3, "digest": digest, "state": "acked",
                    "acked_at": "2026-11-05T11:00:00Z"}}});
        let policy_dir = dir.path().join("policy");
        std::fs::create_dir_all(&policy_dir).unwrap();
        std::fs::write(
            policy_dir.join("state.json"),
            serde_json::to_vec(&state).unwrap(),
        )
        .unwrap();
        note_reported(
            dir.path(),
            &Reported {
                version: 3,
                digest,
                sessions: 1,
                reported_at: "2026-11-05T11:00:05Z".into(),
            },
        );
        assert_eq!(
            enforcement(&sessions_doc(dir.path(), now)),
            serde_json::json!({"applied_version": 3,
                "acknowledged_at": "2026-11-05T11:00:00Z",
                "sessions": ["app-live"], "reported_at": "2026-11-05T11:00:05Z"})
        );
    }

    #[test]
    fn damaged_policy_files_mark_every_linked_vault_unverified() {
        let mut map = VaultMap::default();
        map.vaults
            .insert("s".into(), record("v1", "ns", "n", &["/code/a"]));
        let rows = policy_vaults(&map, None);
        assert_eq!(rows.len(), 1);
        assert_eq!(rows[0].policy, "unverified");
    }

    #[test]
    fn this_part_lists_its_kinds_and_routes_in_step() {
        let mut kinds: Vec<&str> = ROUTES.iter().map(|r| r.kind).collect();
        kinds.sort_unstable();
        assert_eq!(kinds, KINDS);
        assert!(
            ROUTES
                .iter()
                .all(|r| r.path.starts_with("/v1/local/cloud/"))
        );
    }
}