lean-ctx-cloud 3.12.13

The LeanCTX connector: enrollment, bus, knowledge sync, tasks and policy on top of LeanCTX | OSS.
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
// SPDX-License-Identifier: Apache-2.0
//! Project → vault mapping and the first-share confirmation (CHECKLIST
//! 2.1.2, 2.1.3; design `ee/delivery/cloud-v5-project-mapping.md`).
//!
//! For a checkout this machine has not mapped yet:
//! - A Cloud with `POST /v1/vaults/match` gets the identity candidates
//!   ([`super::identity_v2`]). `bound` maps at once. `suggested` and `none`
//!   (with `can_create`) go to the existing resolve, which binds or creates.
//! - Without the route (1.x prod; a 404 is remembered for a day) resolve
//!   runs as before.
//!
//! The person confirms once before knowledge first crosses a new boundary:
//! a vault this machine never shared with (`suggested`, `none`, a committed
//! link file, or any first resolve on a Cloud without the route, since
//! resolve creates a vault on first sight), and a vault that moved into
//! another workspace (personal → team). It is a `link_vault` consent,
//! answered only by the person (`lean-ctx cloud consent`), never by an
//! agent. The gate holds for machines whose person answered the sharing
//! question at `cloud connect`; machines connected without that answer
//! (scripts, harnesses, earlier engines) keep the earlier behaviour.

use std::fmt;
use std::path::Path;

use lean_ctx_time::UtcDateTime;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::auth::Session;
use super::config::CloudConfig;
use super::consent::{self, Broker, ConsentError, Kind, Trust};
use super::identity_v2;
use super::state::MATCH_FILE;
use super::vault::{VaultRecord, identity_digest, resolve_request};
use super::{CloudError, CloudStore};
use lean_ctx_protocol::cloud::vault::{VaultRefV1, VaultV1};

pub use lean_ctx_protocol::cloud::vault::{
    VAULT_MATCH_REQUEST_SCHEMA as MATCH_REQUEST_SCHEMA, VAULT_MATCH_SCHEMA as MATCH_SCHEMA,
};
/// A remembered "no match route" is checked again after this long.
const REPROBE_SECONDS: i64 = 24 * 60 * 60;
/// Workspace part of the consent address of a vault Cloud may create.
pub const NEW_VAULT_WS: &str = "new";

/// Why a checkout is not mapped (yet).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum MapError {
    Cloud(CloudError),
    /// The person has not confirmed the first share (or declined it).
    Consent(ConsentError),
}

impl fmt::Display for MapError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Cloud(error) => error.fmt(f),
            Self::Consent(error) => write!(f, "{}: {error}", error.code()),
        }
    }
}

impl From<CloudError> for MapError {
    fn from(error: CloudError) -> Self {
        Self::Cloud(error)
    }
}

impl From<ConsentError> for MapError {
    fn from(error: ConsentError) -> Self {
        Self::Consent(error)
    }
}

impl From<MapError> for CloudError {
    fn from(error: MapError) -> Self {
        match error {
            MapError::Cloud(error) => error,
            MapError::Consent(error) => error.into(),
        }
    }
}

#[derive(Serialize)]
struct MatchCandidate {
    kind: &'static str,
    digest: String,
}

/// `leanctx.cloud.vault-match-request/v1`: 1-3 candidates, each kind once.
/// No label: it goes with the resolve the person confirmed.
#[derive(Serialize)]
struct MatchRequest<'a> {
    schema: &'static str,
    org_salt_id: &'a str,
    candidates: &'a [MatchCandidate],
}

/// The org-salted candidates of the checkout at `root`, strongest first
/// (a weak legacy name is left out next to an exact candidate).
fn match_candidates(root: &Path, salt: &[u8]) -> Vec<MatchCandidate> {
    salted(&identity_v2::match_candidates(root), salt)
}

fn salted(candidates: &[identity_v2::Candidate], salt: &[u8]) -> Vec<MatchCandidate> {
    candidates
        .iter()
        .map(|c| MatchCandidate {
            kind: c.kind.as_str(),
            digest: identity_digest(salt, &c.stable),
        })
        .collect()
}

/// `leanctx.cloud.vault-match/v1`. Members this engine does not know are
/// ignored, so the server may add some.
#[derive(Debug, Clone, Deserialize)]
pub struct MatchAnswer {
    pub schema: String,
    #[serde(rename = "match")]
    pub outcome: String,
    #[serde(default)]
    pub vault: Option<VaultV1>,
    #[serde(default)]
    pub suggestion: Option<VaultRefV1>,
    #[serde(default)]
    pub can_create: bool,
}

/// What an answer of the match route lets the engine do.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
    /// Cloud knows the project and this instance may read its vault.
    Mapped(VaultV1),
    /// Only the root commit matched (forks share it): bind after a yes.
    Suggested(VaultRefV1),
    /// Unknown project that this instance may create a vault for.
    Create,
    /// Unknown project, and this instance may not create a vault.
    NoVault,
}

/// The step an answer names, or why the answer breaks the contract.
pub fn step_from_answer(answer: MatchAnswer) -> Result<Step, CloudError> {
    let bad = |why: &str| CloudError::Protocol(format!("vault match: {why}"));
    if answer.schema != MATCH_SCHEMA {
        return Err(bad("unknown schema"));
    }
    match answer.outcome.as_str() {
        "bound" => {
            let vault = answer.vault.ok_or_else(|| bad("bound without a vault"))?;
            vault.validate().map_err(|e| bad(&e.to_string()))?;
            Ok(Step::Mapped(vault))
        }
        "suggested" => {
            let vault = answer
                .suggestion
                .ok_or_else(|| bad("suggested without a suggestion"))?;
            vault.validate().map_err(|e| bad(&e.to_string()))?;
            Ok(Step::Suggested(vault))
        }
        "none" if answer.can_create => Ok(Step::Create),
        "none" => Ok(Step::NoVault),
        _ => Err(bad("unknown match")),
    }
}

/// A Cloud without the route: 404 (axum's own, or a `not_found` body),
/// 405, or 426 (a Cloud that refuses the protocol version).
pub fn route_missing(error: &CloudError) -> bool {
    matches!(
        error,
        CloudError::NotFound
            | CloudError::Server {
                status: 404 | 405 | 426,
                ..
            }
    )
}

#[derive(Serialize, Deserialize)]
struct MatchProbe {
    /// Unix seconds of the last "no route" answer.
    unsupported_at: i64,
}

/// Whether to ask the match route: a C0 2.0 Cloud that did not answer
/// "no route" within the last day.
pub fn match_available(store: &CloudStore, now: i64) -> bool {
    if !super::bus::relay_v2::speaks_v2(store) {
        return false;
    }
    store
        .read_config::<MatchProbe>(MATCH_FILE)
        .ok()
        .flatten()
        .is_none_or(|probe| {
            now < probe.unsupported_at || now - probe.unsupported_at >= REPROBE_SECONDS
        })
}

fn remember_missing(store: &CloudStore, now: i64) {
    // A lost note only costs one more 404 on the next pass.
    let _ = store.write_config(
        MATCH_FILE,
        &MatchProbe {
            unsupported_at: now,
        },
    );
}

/// The person answered the sharing question at connect for this workspace:
/// from then on a first share waits for their yes.
pub fn first_share_gated(session: &Session) -> bool {
    super::connect::decision(&session.store, &session.instance.org_id).is_some()
}

/// The vault a first share goes to, as the person confirms it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
    /// An existing vault.
    Bind(VaultRefV1),
    /// The project's own vault in the workspace: the one Cloud already
    /// binds to it, or a new private one.
    Own,
}

/// `[a-z0-9-]{2,40}` from the checkout folder name, else `project`.
pub fn new_vault_name(root: &Path) -> String {
    let raw = root
        .file_name()
        .map(|n| n.to_string_lossy().to_lowercase())
        .unwrap_or_default();
    let slug: String = raw
        .chars()
        .map(|c| {
            if c.is_ascii_lowercase() || c.is_ascii_digit() {
                c
            } else {
                '-'
            }
        })
        .collect();
    let slug: String = slug.trim_matches('-').chars().take(40).collect();
    let slug = slug.trim_end_matches('-');
    if slug.len() >= 2 {
        slug.to_string()
    } else {
        "project".into()
    }
}

/// The `link_vault` subject for sharing the checkout at `root` (identity
/// `stable`) with `target`. `vault` is what the person types to approve.
pub fn consent_subject(target: &Target, stable: &str, root: &Path) -> Value {
    let vault = match target {
        Target::Bind(vault) => format!("{}/{}", vault.ns, vault.name),
        Target::Own => format!("{NEW_VAULT_WS}/{}", new_vault_name(root)),
    };
    let text = |value: Option<String>| {
        value
            .filter(|v| (1..=500).contains(&v.chars().count()))
            .map_or(Value::Null, Value::String)
    };
    consent::subject(&[
        (
            "bound_repo",
            text(root.file_name().map(|n| n.to_string_lossy().into_owned())),
        ),
        ("link_digest", Value::String(stable.to_string())),
        ("remote_seen", text(identity_v2::origin_remote(root))),
        ("target_vault", Value::Null),
        ("vault", Value::String(vault)),
    ])
}

fn require(
    session: &Session,
    config: &CloudConfig,
    target: &Target,
    stable: &str,
    root: &Path,
) -> Result<(), MapError> {
    let broker = Broker::open(&session.store, Trust::load(&session.store, config));
    let now = u64::try_from(UtcDateTime::now().timestamp_millis()).unwrap_or_default();
    broker.require(Kind::LinkVault, &consent_subject(target, stable, root), now)?;
    Ok(())
}

/// A first-share request for a checkout `map` already knows: nothing to
/// answer, so `cloud consent` and `cloud status` never list it as waiting
/// (the next sync closes it here and in Cloud, [`close_satisfied`]).
pub fn satisfied(record: &consent::Record, map: &super::vault::VaultMap) -> bool {
    record
        .subject
        .get("link_digest")
        .and_then(Value::as_str)
        .and_then(|stable| map.vaults.get(stable))
        .is_some_and(|mapped| satisfied_by(record, mapped))
}

/// `record` asks to share into what `mapped` already is: the vault it is
/// mapped to, or a new vault when one exists now. A move into another
/// vault (personal → team) is not satisfied and keeps waiting.
fn satisfied_by(record: &consent::Record, mapped: &VaultRecord) -> bool {
    if record.kind != Kind::LinkVault || !record.subject["target_vault"].is_null() {
        return false;
    }
    let vault = record.subject["vault"].as_str().unwrap_or_default();
    vault == format!("{}/{}", mapped.ns, mapped.name)
        || vault.starts_with(&format!("{NEW_VAULT_WS}/"))
}

/// The checkout with identity `stable` is mapped: a first-share request
/// still open for it (opened before another engine's sync created the
/// vault) asks for nothing any more. It is closed here and withdrawn in
/// Cloud, so neither `cloud consent`, `cloud status` nor a web page lists
/// it as waiting. Returns the closed ids.
pub fn close_satisfied(
    session: &Session,
    config: &CloudConfig,
    stable: &str,
    mapped: &VaultRecord,
) -> Vec<String> {
    let broker = Broker::open(&session.store, Trust::load(&session.store, config));
    let closed = broker
        .close_satisfied(&|r| r.subject["link_digest"] == stable && satisfied_by(r, mapped))
        .unwrap_or_else(|error| {
            tracing::info!("cloud consent close: {error}");
            Vec::new()
        });
    for id in &closed {
        let _ = session.with_client(|client| {
            consent::web::withdraw_relayed(client, id);
            Ok(())
        });
    }
    closed
}

/// The vault of the checkout at `root` (identity `stable`), which this
/// machine has not mapped there. `known` is this machine's record of the
/// same repository from another checkout, if any.
pub fn map_root(
    session: &Session,
    config: &CloudConfig,
    salt: &(String, Vec<u8>),
    stable: &str,
    root: &Path,
    known: Option<&VaultRecord>,
) -> Result<VaultV1, MapError> {
    // sharing.personal_workspace: a team project of a forbidding company
    // gets no vault in a Personal workspace (and is recorded in the team).
    super::company_bound::on_map(&session.store, &session.instance, stable)?;
    let (resolved, mut request) = resolve_request(root, &salt.0, &salt.1, config.vault_labels)?;
    if resolved != stable {
        return Err(CloudError::Config(format!(
            "{} changed its identity since the draft was made",
            root.display()
        ))
        .into());
    }
    let gated = first_share_gated(session) && known.is_none();
    let now = UtcDateTime::now().timestamp();
    let candidates = match_candidates(root, &salt.1);
    // Set once the match route answered: resolve then carries the exact
    // digest (C0 vault_match), and match runs once more after it.
    let mut matched = false;
    // A committed link file is a signal of the repository, not this
    // person's yes: it names the target, the person still confirms.
    let target = match request.vault.clone() {
        Some(vault) => Target::Bind(vault),
        None if match_available(&session.store, now) => {
            let step = ask_match(session, &salt.0, &candidates, now)?;
            matched = step.is_some();
            match step {
                Some(Step::Mapped(vault)) => {
                    return confirm_move(session, config, stable, root, known, vault);
                }
                Some(Step::Suggested(vault)) if gated => Target::Bind(vault),
                Some(Step::NoVault) => {
                    return Err(CloudError::Refused(
                        "this project has no vault in the workspace and this instance may not create one; ask a maintainer, or name one with `lean-ctx cloud link <workspace>/<vault>`".into(),
                    )
                    .into());
                }
                // Without the gate a suggestion is not bound: like before
                // the route existed, resolve creates the project's vault.
                Some(Step::Suggested(_) | Step::Create) | None => Target::Own,
            }
        }
        None => Target::Own,
    };
    if gated {
        require(session, config, &target, stable, root)?;
    }
    if let Target::Bind(vault) = &target {
        request.vault = Some(vault.clone());
    }
    if matched && let Some(exact) = candidates.iter().find(|c| c.kind == "exact") {
        request.identity_digest.clone_from(&exact.digest);
    }
    let body = serde_json::to_value(&request).map_err(|e| CloudError::Local(e.to_string()))?;
    let answer = session.with_client(|c| c.post("/vaults/resolve", &body))?;
    let vault: VaultV1 =
        serde_json::from_value(answer).map_err(|e| CloudError::Protocol(format!("vault: {e}")))?;
    vault
        .validate()
        .map_err(|e| CloudError::Protocol(e.to_string()))?;
    if matched {
        // The bound match records the history alias of the new binding;
        // the resolve already succeeded, so its outcome does not matter.
        let _ = ask_match(session, &salt.0, &candidates, now);
    }
    confirm_move(session, config, stable, root, known, vault)
}

/// Personal → team: a repository this machine shared into one workspace
/// whose vault now answers from another waits for the person's yes.
fn confirm_move(
    session: &Session,
    config: &CloudConfig,
    stable: &str,
    root: &Path,
    known: Option<&VaultRecord>,
    vault: VaultV1,
) -> Result<VaultV1, MapError> {
    if first_share_gated(session)
        && let Some(known) = known
        && known.ns != vault.vault.ns
    {
        require(
            session,
            config,
            &Target::Bind(vault.vault.clone()),
            stable,
            root,
        )?;
    }
    Ok(vault)
}

/// The vault Cloud binds the checkout at `root` to when that is not `ours`:
/// another Instance of the workspace resolved the repository there.
/// `None` when both agree, on a Cloud without the match route, or when
/// Cloud cannot be asked (a warning only, connect goes on).
pub fn bound_elsewhere(session: &Session, root: &Path, ours: &VaultRefV1) -> Option<VaultRefV1> {
    let now = UtcDateTime::now().timestamp();
    if !match_available(&session.store, now) {
        return None;
    }
    let salt = super::knowledge::pull::org_salt(session).ok()?;
    match ask_match(session, &salt.0, &match_candidates(root, &salt.1), now) {
        Ok(Some(Step::Mapped(vault))) if vault.vault != *ours => Some(vault.vault),
        _ => None,
    }
}

/// `POST /v1/vaults/match`; `None` when this Cloud has no such route.
fn ask_match(
    session: &Session,
    org_salt_id: &str,
    candidates: &[MatchCandidate],
    now: i64,
) -> Result<Option<Step>, CloudError> {
    if candidates.is_empty() {
        return Ok(None);
    }
    let body = match_body(org_salt_id, candidates)?;
    match session.with_client(|c| c.post("/vaults/match", &body)) {
        Ok(answer) => {
            let answer: MatchAnswer = serde_json::from_value(answer)
                .map_err(|e| CloudError::Protocol(format!("vault match: {e}")))?;
            step_from_answer(answer).map(Some)
        }
        Err(error) if route_missing(&error) => {
            remember_missing(&session.store, now);
            Ok(None)
        }
        Err(error) => Err(error),
    }
}

fn match_body(org_salt_id: &str, candidates: &[MatchCandidate]) -> Result<Value, CloudError> {
    serde_json::to_value(MatchRequest {
        schema: MATCH_REQUEST_SCHEMA,
        org_salt_id,
        candidates,
    })
    .map_err(|e| CloudError::Local(e.to_string()))
}

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

    fn vault_json() -> Value {
        json!({
            "schema": "leanctx.cloud.vault/v1",
            "vault_id": "3f6c1e9a0b7d4c2e8f5a1b6c9d0e7f42",
            "vault": {"ns": "acme", "name": "payments"},
            "sealed": false,
        })
    }

    fn answer(value: Value) -> MatchAnswer {
        serde_json::from_value(value).unwrap()
    }

    fn route_fixture(name: &str) -> Value {
        let path = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/../.."))
            .join("crates/lean-ctx-protocol/tests/fixtures/cloud-v2/routes")
            .join(name);
        serde_json::from_slice(&std::fs::read(path).unwrap()).unwrap()
    }

    /// C0 `vault_match.request`: the body the engine builds for the first
    /// identity vector is the fixture, byte for byte as JSON (no label).
    #[test]
    fn the_match_request_is_the_contract_fixture() {
        use base64::Engine as _;
        let vectors = identity_v2::contract_vectors();
        let salt = base64::engine::general_purpose::STANDARD
            .decode(vectors["salt"].as_str().unwrap())
            .unwrap();
        let input = &vectors["cases"][0]["input"];
        let candidates = identity_v2::candidates_from(
            &identity_v2::vector_parts(input),
            input["legacy_identity"].as_str(),
        );
        let body = match_body(
            vectors["org_salt_id"].as_str().unwrap(),
            &salted(&candidates, &salt),
        )
        .unwrap();
        assert_eq!(body, route_fixture("vault_match.request.json"));
    }

    /// C0 `vault_match.*.response` and the response cases of `invalid/`
    /// the engine itself must refuse.
    #[test]
    fn the_contract_responses_become_steps() {
        let step = |name: &str| step_from_answer(answer(route_fixture(name)));
        assert!(
            matches!(step("vault_match.bound.response.json"), Ok(Step::Mapped(v)) if v.vault.name == "payments")
        );
        assert!(
            matches!(step("vault_match.bound_legacy.response.json"), Ok(Step::Mapped(v)) if !v.sealed)
        );
        assert!(
            matches!(step("vault_match.suggested.response.json"), Ok(Step::Suggested(v)) if v.ns == "acme")
        );
        assert_eq!(step("vault_match.none.response.json"), Ok(Step::Create));
        // A bound sealed vault is refused, naming it.
        let mut sealed = route_fixture("vault_match.bound.response.json");
        sealed["vault"]["sealed"] = Value::Bool(true);
        let refused = step_from_answer(answer(sealed)).unwrap_err();
        assert!(
            refused
                .to_string()
                .contains("sealed vaults are not supported"),
            "{refused}"
        );
        let mut bound = route_fixture("vault_match.bound.response.json");
        bound.as_object_mut().unwrap().remove("vault");
        assert!(
            step_from_answer(answer(bound)).is_err(),
            "bound_without_vault"
        );
    }

    #[test]
    fn match_answers_become_steps_and_broken_ones_are_refused() {
        let bound = answer(json!({
            "schema": MATCH_SCHEMA, "match": "bound", "matched": "legacy",
            "vault": vault_json(), "something_new": 1,
        }));
        match step_from_answer(bound).unwrap() {
            Step::Mapped(vault) => assert_eq!(vault.vault.name, "payments"),
            other => panic!("{other:?}"),
        }
        let suggested = answer(json!({
            "schema": MATCH_SCHEMA, "match": "suggested", "matched": "history",
            "suggestion": {"ns": "acme", "name": "payments"},
        }));
        assert!(
            matches!(step_from_answer(suggested).unwrap(), Step::Suggested(v) if v.ns == "acme")
        );
        let create = answer(json!({"schema": MATCH_SCHEMA, "match": "none", "can_create": true}));
        assert_eq!(step_from_answer(create).unwrap(), Step::Create);
        let none = answer(json!({"schema": MATCH_SCHEMA, "match": "none"}));
        assert_eq!(step_from_answer(none).unwrap(), Step::NoVault);

        for broken in [
            json!({"schema": MATCH_SCHEMA, "match": "bound"}),
            json!({"schema": MATCH_SCHEMA, "match": "suggested"}),
            json!({"schema": MATCH_SCHEMA, "match": "maybe"}),
            json!({"schema": "leanctx.cloud.vault-match/v0", "match": "none"}),
            json!({"schema": MATCH_SCHEMA, "match": "suggested",
                   "suggestion": {"ns": "acme", "name": "../etc"}}),
        ] {
            assert!(
                step_from_answer(answer(broken.clone())).is_err(),
                "{broken}"
            );
        }
    }

    #[test]
    fn only_a_missing_route_means_no_match() {
        assert!(route_missing(&CloudError::NotFound));
        assert!(route_missing(&CloudError::Server {
            status: 404,
            code: "unknown".into()
        }));
        assert!(route_missing(&CloudError::Server {
            status: 405,
            code: "unknown".into()
        }));
        assert!(!route_missing(&CloudError::Forbidden));
        assert!(!route_missing(&CloudError::Offline("x".into())));
        assert!(!route_missing(&CloudError::Server {
            status: 500,
            code: "internal".into()
        }));
    }

    #[test]
    fn the_match_route_is_asked_on_c0_2_cloudes_and_a_404_rests_for_a_day() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let now = 1_800_000_000;
        assert!(!match_available(&store, now), "1.x: never asked");
        super::super::bus::relay_v2::record_protocol(&store, true).unwrap();
        assert!(match_available(&store, now));
        remember_missing(&store, now);
        assert!(!match_available(&store, now + 60));
        assert!(match_available(&store, now + REPROBE_SECONDS));
        assert!(
            match_available(&store, now - 60),
            "a clock that moved back asks again"
        );
        store.forget(true).unwrap();
        assert!(!store.config_dir().join(MATCH_FILE).exists());
    }

    #[test]
    fn the_consent_subject_names_the_vault_and_stays_on_shape() {
        let base = tempfile::tempdir().unwrap();
        let root = base.path().join("Payments Service");
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let stable =
            lean_ctx_protocol::cloud::vault::stable_identity("git:github.com/acme/payments");
        let own = consent_subject(&Target::Own, &stable, &root);
        consent::check_subject(Kind::LinkVault, &own).unwrap();
        assert_eq!(own["vault"], "new/payments-service");
        assert_eq!(own["bound_repo"], "Payments Service");
        let bind = consent_subject(
            &Target::Bind(VaultRefV1 {
                ns: "acme".into(),
                name: "payments".into(),
            }),
            &stable,
            &root,
        );
        consent::check_subject(Kind::LinkVault, &bind).unwrap();
        assert_eq!(bind["vault"], "acme/payments");
        assert_ne!(
            consent::subject_digest(&own).unwrap(),
            consent::subject_digest(&bind).unwrap(),
            "a yes for one target never covers another"
        );
        assert_eq!(new_vault_name(Path::new("/w/x")), "project");
        assert_eq!(new_vault_name(Path::new("/w/Über_App")), "ber-app");
        assert_eq!(new_vault_name(&base.path().join("a".repeat(60))).len(), 40);
    }

    /// E2E-FINAL: a second engine asked for a first share before the
    /// vault existed; once its checkout maps (another engine created the
    /// vault), that request asks for nothing and is closed, while a move
    /// into another vault keeps waiting.
    #[test]
    fn a_first_share_for_a_mapped_checkout_is_closed_a_move_keeps_waiting() {
        let dir = tempfile::tempdir().unwrap();
        let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
        let broker = Broker::open(
            &store,
            Trust {
                presence: None,
                consent_keys: std::collections::BTreeMap::new(),
                owner_user_id: None,
            },
        );
        let root = dir.path().join("journey-demo");
        std::fs::create_dir_all(root.join(".git")).unwrap();
        let stable =
            lean_ctx_protocol::cloud::vault::stable_identity("git:github.com/acme/journey-demo");
        let pending = |target: &Target| match broker.require(
            Kind::LinkVault,
            &consent_subject(target, &stable, &root),
            1_792_800_000_000,
        ) {
            Err(ConsentError::Required { consent_id, .. }) => broker.get(&consent_id).unwrap(),
            other => panic!("{other:?}"),
        };
        let own = pending(&Target::Own);
        let team = pending(&Target::Bind(VaultRefV1 {
            ns: "acme-team".into(),
            name: "journey-demo".into(),
        }));
        let mut map = super::super::vault::VaultMap::default();
        assert!(!satisfied(&own, &map), "not mapped yet: it waits");
        map.vaults.insert(
            stable.clone(),
            VaultRecord {
                vault_id: "vlt_1".into(),
                ns: "ada".into(),
                name: "journey-demo".into(),
                sealed: false,
                roots: vec![root.to_string_lossy().into_owned()],
            },
        );
        assert!(satisfied(&own, &map), "mapped: nothing to answer");
        assert!(!satisfied(&team, &map), "a move into another vault waits");
        let mapped = map.vaults[&stable].clone();
        let closed = broker
            .close_satisfied(&|r| {
                r.subject["link_digest"] == stable.as_str() && satisfied_by(r, &mapped)
            })
            .unwrap();
        assert_eq!(closed, vec![own.consent_id.clone()]);
        assert!(broker.get(&own.consent_id).is_err(), "closed");
        assert_eq!(
            broker.get(&team.consent_id).unwrap().state,
            consent::State::Pending
        );
    }

    #[test]
    fn consent_errors_keep_their_code_for_the_outbox_and_hide_the_id() {
        let error = MapError::Consent(ConsentError::Required {
            consent_id: "cns_aaaaaaaaaaaaaaaa".into(),
            kind: Kind::LinkVault,
        });
        let shown = error.to_string();
        assert!(shown.starts_with("consent_required"), "{shown}");
        assert!(
            !shown.contains("cns_"),
            "the id is never handed to an agent"
        );
        assert!(matches!(CloudError::from(error), CloudError::Refused(_)));
    }
}