toha 0.4.0

Generate projects and files from templates through one interview for people, scripts, agents, and Rust callers.
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
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
// ---
// relationships:
//   implements:
//     - template-format
//     - interview-protocol
//   references:
//     - error-attribution
// ---
//! Toha's immutable, typed invocation context.
//!
//! The command adapter resolves the selected identity, canonical target, host
//! facts, execution facts, and an environment snapshot once, before the first
//! interview evaluation. The pure interview engine carries the resulting
//! [`InvocationContext`] unchanged, staging stores and restores it, and planning
//! consumes it. The engine never reads the process, registry, terminal, host
//! files, privilege state, or trust state.
//!
//! This module owns the typed facts, their projection into the seventeen
//! reserved Jinja names, redaction of captured environment values, the
//! environment-admission matrix, and the versioned staged wire form. Template
//! reference analysis (which produces an [`EnvironmentNeed`]) lives in
//! `template`/`jinja`; the command adapter owns live host and process capture.

use std::collections::BTreeMap;
use std::path::{Component, Path};

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

use crate::staging::CanonicalTarget;

/// The seventeen reserved Toha Jinja names, in table order.
///
/// Every current-context template exposes each name as a variable. Only these
/// exact names are reserved against template data keys, question ids, computed
/// ids, and `each` bindings; neighbouring `toha_` names remain legal.
pub const RESERVED_NAMES: [&str; 17] = [
    "toha_target_name",
    "toha_template_name",
    "toha_template_formal_name",
    "toha_template_aliases",
    "toha_template_source",
    "toha_host_os",
    "toha_host_arch",
    "toha_host_os_name",
    "toha_host_os_id",
    "toha_host_os_id_like",
    "toha_is_admin",
    "toha_is_interactive",
    "toha_env_user",
    "toha_env_hostname",
    "toha_env_editor",
    "toha_env_shell",
    "toha_env_visual",
];

/// The five fixed environment names, the closed vocabulary the trust gate
/// governs. Every value is captured together or not at all.
pub const ENVIRONMENT_NAMES: [&str; 5] = [
    "toha_env_user",
    "toha_env_hostname",
    "toha_env_editor",
    "toha_env_shell",
    "toha_env_visual",
];

/// The immutable per-invocation context carried through the interview engine.
///
/// A current context projects the seventeen reserved names; a legacy context
/// (restored from a pre-context staged record) projects none of them and keeps
/// the pre-context Jinja contract.
#[derive(Clone)]
pub struct InvocationContext {
    target: CanonicalTarget,
    contract: ContextContract,
    recorded: BTreeMap<String, Value>,
}

impl std::fmt::Debug for InvocationContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("InvocationContext")
            .field("target", &self.target)
            .field("contract", &self.contract)
            .field("recorded", &self.recorded.keys().collect::<Vec<_>>())
            .finish()
    }
}

#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
enum ContextContract {
    Legacy,
    Current(CurrentContext),
}

#[derive(Clone, Debug)]
struct CurrentContext {
    selected: SelectedTemplate,
    host: HostFacts,
    execution: ExecutionFacts,
    environment: EnvironmentSnapshot,
}

/// A failure to assemble an [`InvocationContext`] from supplied facts.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum ContextError {
    #[error("selected template has an empty formal name")]
    EmptyFormalName,
    #[error("selected template has an empty short name")]
    EmptyShortName,
}

impl InvocationContext {
    /// Assembles a current context from resolved facts.
    pub fn new(
        target: CanonicalTarget,
        selected: SelectedTemplate,
        host: HostFacts,
        execution: ExecutionFacts,
        environment: EnvironmentSnapshot,
    ) -> Result<Self, ContextError> {
        if selected.formal_name.is_empty() {
            return Err(ContextError::EmptyFormalName);
        }
        if selected.short_name.is_empty() {
            return Err(ContextError::EmptyShortName);
        }
        Ok(Self {
            target,
            recorded: BTreeMap::new(),
            contract: ContextContract::Current(CurrentContext {
                selected,
                host,
                execution,
                environment,
            }),
        })
    }

    /// A legacy context carries only the target; it projects none of the
    /// seventeen names. Restored by staging for a pre-context record and used
    /// by direct callers that opt out of the current contract.
    pub(crate) fn legacy(target: CanonicalTarget) -> Self {
        Self {
            target,
            recorded: BTreeMap::new(),
            contract: ContextContract::Legacy,
        }
    }

    pub fn target(&self) -> &CanonicalTarget {
        &self.target
    }

    /// A direct-caller convenience: a current context bound to `target` with a
    /// generic identity, captured host facts, no administrative or interactive
    /// status, and no environment snapshot. For callers and tests that carry a
    /// context through the engine without exercising the seventeen values.
    pub fn for_target(target: CanonicalTarget) -> Self {
        Self {
            target,
            recorded: BTreeMap::new(),
            contract: ContextContract::Current(CurrentContext {
                selected: SelectedTemplate::new(
                    "template".to_owned(),
                    "template".to_owned(),
                    Vec::new(),
                    None,
                ),
                host: HostFacts::capture(),
                execution: ExecutionFacts::new(false, false),
                environment: EnvironmentSnapshot::Unavailable,
            }),
        }
    }

    /// The selected template's stable formal name, or `None` for a legacy
    /// context. Used by staging to serialize the record's formal name.
    pub(crate) fn formal_name(&self) -> Option<&str> {
        match &self.contract {
            ContextContract::Legacy => None,
            ContextContract::Current(current) => Some(&current.selected.formal_name),
        }
    }

    /// The target, formal name, and versioned wire for staging. A legacy
    /// context has no formal name of its own; staging keeps the record's
    /// existing formal name in that case.
    pub(crate) fn staged_parts(&self) -> (CanonicalTarget, Option<String>, InvocationContextWire) {
        (
            self.target.clone(),
            self.formal_name().map(str::to_owned),
            self.wire(),
        )
    }

    fn wire(&self) -> InvocationContextWire {
        match &self.contract {
            ContextContract::Legacy => InvocationContextWire::Legacy,
            ContextContract::Current(current) => InvocationContextWire::Current {
                short_name: current.selected.short_name.clone(),
                aliases: current.selected.aliases.clone(),
                source: current.selected.source.clone(),
                host: current.host.wire(),
                execution: current.execution.wire(),
                environment: current.environment.wire(),
            },
        }
    }

    /// Rebuilds a context from a producer-created target carrier, the record's
    /// formal name, and the restored wire. Used only by staging replay.
    pub(crate) fn from_wire(
        target: CanonicalTarget,
        formal_name: &str,
        wire: InvocationContextWire,
    ) -> Self {
        match wire {
            InvocationContextWire::Legacy => Self::legacy(target),
            InvocationContextWire::Current {
                short_name,
                aliases,
                source,
                host,
                execution,
                environment,
            } => Self {
                target,
                recorded: BTreeMap::new(),
                contract: ContextContract::Current(CurrentContext {
                    selected: SelectedTemplate {
                        formal_name: formal_name.to_owned(),
                        short_name,
                        aliases,
                        source,
                    },
                    host: HostFacts::from_wire(host),
                    execution: ExecutionFacts::from_wire(execution),
                    environment: EnvironmentSnapshot::from_wire(environment),
                }),
            },
        }
    }

    /// Restore the inputs that the recorded template read for a merge-base render.
    pub fn with_recorded(mut self, values: BTreeMap<String, Value>) -> Self {
        self.recorded = values;
        self
    }

    /// Inserts the seventeen reserved names into a Jinja evaluation context. A
    /// legacy context inserts none of them.
    pub(crate) fn project(&self, out: &mut BTreeMap<String, Value>) {
        let ContextContract::Current(current) = &self.contract else {
            return;
        };
        let mut set = |name: &str, value: Value| {
            out.insert(name.to_owned(), value);
        };
        set(
            "toha_target_name",
            optional(target_name(self.target.as_path())),
        );
        set(
            "toha_template_name",
            Value::String(current.selected.short_name.clone()),
        );
        set(
            "toha_template_formal_name",
            Value::String(current.selected.formal_name.clone()),
        );
        set(
            "toha_template_aliases",
            Value::Array(
                current
                    .selected
                    .aliases
                    .iter()
                    .map(|a| Value::String(a.clone()))
                    .collect(),
            ),
        );
        set(
            "toha_template_source",
            optional(current.selected.source.clone()),
        );
        set("toha_host_os", Value::String(current.host.os.clone()));
        set("toha_host_arch", Value::String(current.host.arch.clone()));
        set("toha_host_os_name", optional(current.host.os_name.clone()));
        set("toha_host_os_id", optional(current.host.os_id.clone()));
        set(
            "toha_host_os_id_like",
            Value::Array(
                current
                    .host
                    .os_id_like
                    .iter()
                    .map(|s| Value::String(s.clone()))
                    .collect(),
            ),
        );
        set("toha_is_admin", Value::Bool(current.execution.is_admin));
        set(
            "toha_is_interactive",
            Value::Bool(current.execution.is_interactive),
        );
        let env = &current.environment;
        set("toha_env_user", optional(env.field(EnvField::User)));
        set("toha_env_hostname", optional(env.field(EnvField::Hostname)));
        set("toha_env_editor", optional(env.field(EnvField::Editor)));
        set("toha_env_shell", optional(env.field(EnvField::Shell)));
        set("toha_env_visual", optional(env.field(EnvField::Visual)));
        out.extend(self.recorded.clone());
    }
}

fn optional(value: Option<String>) -> Value {
    value.map(Value::String).unwrap_or(Value::Null)
}

/// The final Unicode component of a canonical target, or `None` for a
/// filesystem root, prefix, or non-Unicode final component.
fn target_name(path: &Path) -> Option<String> {
    match path.components().next_back() {
        Some(Component::Normal(name)) => name.to_str().map(str::to_owned),
        _ => None,
    }
}

/// The selected template's identity, assembled at the resolution boundary. A
/// name/alias/short-name selection carries the registry entry's effective alias
/// list and source; a folder, direct Git, or bundled selection carries `[]` and
/// `None`.
#[derive(Clone, Debug)]
pub struct SelectedTemplate {
    formal_name: String,
    short_name: String,
    aliases: Vec<String>,
    source: Option<String>,
}

impl SelectedTemplate {
    pub fn new(
        formal_name: String,
        short_name: String,
        aliases: Vec<String>,
        source: Option<String>,
    ) -> Self {
        Self {
            formal_name,
            short_name,
            aliases,
            source,
        }
    }
}

/// Host facts captured by the command adapter. Never fails template load,
/// interview, or planning.
#[derive(Clone, Debug)]
pub struct HostFacts {
    os: String,
    arch: String,
    os_name: Option<String>,
    os_id: Option<String>,
    os_id_like: Vec<String>,
}

impl HostFacts {
    pub fn new(
        os: String,
        arch: String,
        os_name: Option<String>,
        os_id: Option<String>,
        os_id_like: Vec<String>,
    ) -> Self {
        Self {
            os,
            arch,
            os_name,
            os_id,
            os_id_like,
        }
    }

    /// Captures the Rust target OS/arch vocabulary and, on Linux, the
    /// `/etc/os-release` fallbacks. No file is executed and no subprocess runs.
    pub fn capture() -> Self {
        let (os_name, os_id, os_id_like) = if std::env::consts::OS == "linux" {
            match std::fs::read("/etc/os-release") {
                Ok(bytes) => match String::from_utf8(bytes) {
                    Ok(text) => parse_os_release(&text),
                    Err(_) => (None, None, Vec::new()),
                },
                Err(_) => (None, None, Vec::new()),
            }
        } else {
            (None, None, Vec::new())
        };
        Self {
            os: std::env::consts::OS.to_owned(),
            arch: std::env::consts::ARCH.to_owned(),
            os_name,
            os_id,
            os_id_like,
        }
    }

    fn wire(&self) -> HostFactsWire {
        HostFactsWire {
            os: self.os.clone(),
            arch: self.arch.clone(),
            os_name: self.os_name.clone(),
            os_id: self.os_id.clone(),
            os_id_like: self.os_id_like.clone(),
        }
    }

    fn from_wire(wire: HostFactsWire) -> Self {
        Self {
            os: wire.os,
            arch: wire.arch,
            os_name: wire.os_name,
            os_id: wire.os_id,
            os_id_like: wire.os_id_like,
        }
    }
}

/// Parses `/etc/os-release` content, returning `NAME`, `ID`, and `ID_LIKE`.
///
/// Accepts standard quoted and unquoted assignments, unescapes double-quoted
/// values without executing the file, and uses the last valid assignment for a
/// duplicate key. Empty `NAME`/`ID` become `None`; empty `ID_LIKE` becomes
/// `[]`. `ID_LIKE` is split on ASCII whitespace in source order.
pub(crate) fn parse_os_release(text: &str) -> (Option<String>, Option<String>, Vec<String>) {
    let mut name = None;
    let mut id = None;
    let mut id_like: Vec<String> = Vec::new();
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let Some((key, raw)) = line.split_once('=') else {
            continue;
        };
        let key = key.trim();
        let value = unquote(raw.trim());
        match key {
            "NAME" => name = Some(value),
            "ID" => id = Some(value),
            "ID_LIKE" => {
                id_like = value.split_ascii_whitespace().map(str::to_owned).collect();
            }
            _ => {}
        }
    }
    (
        name.filter(|v| !v.is_empty()),
        id.filter(|v| !v.is_empty()),
        id_like,
    )
}

/// Unquotes an os-release value: double quotes honour `\\`, `\"`, `\$`, and
/// `` \` `` escapes; single quotes are literal; unquoted text is taken as is.
fn unquote(raw: &str) -> String {
    let bytes = raw.as_bytes();
    if raw.len() >= 2 && bytes[0] == b'"' && bytes[bytes.len() - 1] == b'"' {
        let inner = &raw[1..raw.len() - 1];
        let mut out = String::with_capacity(inner.len());
        let mut chars = inner.chars();
        while let Some(ch) = chars.next() {
            if ch == '\\' {
                match chars.next() {
                    Some(next @ ('\\' | '"' | '$' | '`')) => out.push(next),
                    Some(other) => {
                        out.push('\\');
                        out.push(other);
                    }
                    None => out.push('\\'),
                }
            } else {
                out.push(ch);
            }
        }
        out
    } else if raw.len() >= 2 && bytes[0] == b'\'' && bytes[bytes.len() - 1] == b'\'' {
        raw[1..raw.len() - 1].to_owned()
    } else {
        raw.to_owned()
    }
}

/// Execution facts captured by the command adapter, frozen on resume.
#[derive(Clone, Copy, Debug)]
pub struct ExecutionFacts {
    is_admin: bool,
    is_interactive: bool,
}

impl ExecutionFacts {
    pub fn new(is_admin: bool, is_interactive: bool) -> Self {
        Self {
            is_admin,
            is_interactive,
        }
    }

    fn wire(&self) -> ExecutionFactsWire {
        ExecutionFactsWire {
            is_admin: self.is_admin,
            is_interactive: self.is_interactive,
        }
    }

    fn from_wire(wire: ExecutionFactsWire) -> Self {
        Self {
            is_admin: wire.is_admin,
            is_interactive: wire.is_interactive,
        }
    }
}

/// The five fixed environment names, used to read a captured snapshot field
/// without exposing the underlying struct.
#[derive(Clone, Copy)]
enum EnvField {
    User,
    Hostname,
    Editor,
    Shell,
    Visual,
}

/// The environment snapshot: either no captured values, or the closed set of
/// five optional plaintext values. Its `Debug` is redacted.
#[derive(Clone)]
pub enum EnvironmentSnapshot {
    Unavailable,
    Captured(FixedEnvironment),
}

impl EnvironmentSnapshot {
    fn field(&self, field: EnvField) -> Option<String> {
        match self {
            Self::Unavailable => None,
            Self::Captured(env) => env.field(field),
        }
    }

    fn wire(&self) -> EnvironmentWire {
        match self {
            Self::Unavailable => EnvironmentWire::Unavailable,
            Self::Captured(env) => EnvironmentWire::Captured {
                user: env.user.clone(),
                hostname: env.hostname.clone(),
                editor: env.editor.clone(),
                shell: env.shell.clone(),
                visual: env.visual.clone(),
            },
        }
    }

    fn from_wire(wire: EnvironmentWire) -> Self {
        match wire {
            EnvironmentWire::Unavailable => Self::Unavailable,
            EnvironmentWire::Captured {
                user,
                hostname,
                editor,
                shell,
                visual,
            } => Self::Captured(FixedEnvironment {
                user,
                hostname,
                editor,
                shell,
                visual,
            }),
        }
    }
}

impl std::fmt::Debug for EnvironmentSnapshot {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unavailable => f.write_str("Unavailable"),
            Self::Captured(_) => f.write_str("Captured(<redacted>)"),
        }
    }
}

/// The closed set of five optional fixed environment values. Absent, empty, or
/// non-Unicode input is `None`. Its `Debug` is redacted so captured values
/// never enter diagnostics, protocol output, logs, or evidence.
#[derive(Clone)]
pub struct FixedEnvironment {
    user: Option<String>,
    hostname: Option<String>,
    editor: Option<String>,
    shell: Option<String>,
    visual: Option<String>,
}

impl FixedEnvironment {
    pub fn new(
        user: Option<String>,
        hostname: Option<String>,
        editor: Option<String>,
        shell: Option<String>,
        visual: Option<String>,
    ) -> Self {
        Self {
            user,
            hostname,
            editor,
            shell,
            visual,
        }
    }

    fn field(&self, field: EnvField) -> Option<String> {
        match field {
            EnvField::User => self.user.clone(),
            EnvField::Hostname => self.hostname.clone(),
            EnvField::Editor => self.editor.clone(),
            EnvField::Shell => self.shell.clone(),
            EnvField::Visual => self.visual.clone(),
        }
    }
}

impl std::fmt::Debug for FixedEnvironment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("FixedEnvironment(<redacted>)")
    }
}

/// A source of the five fixed environment values. The command adapter's
/// implementation reads the process environment and native hostname; tests and
/// direct callers supply their own.
pub trait FixedEnvironmentSource {
    fn capture(&mut self) -> FixedEnvironment;
}

/// One caller decision about environment access, combined with the immutable
/// analysis need by [`admit`].
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EnvironmentDecision {
    /// No access; project five nulls.
    Deny,
    /// Capture only when the loaded program references a fixed value.
    GrantIfNeeded,
    /// Stage without the flag: refuse if a need exists, else record unavailable.
    RequireStageGrant,
    /// Stage with the flag: capture all five once even without an initial need.
    CarryStageGrant,
}

impl EnvironmentDecision {
    /// The new-apply decision: capture when granted and the program needs a
    /// value; deny outright when the effective-trust gate does not hold.
    pub fn grant_if_needed(granted: bool) -> Self {
        if granted {
            Self::GrantIfNeeded
        } else {
            Self::Deny
        }
    }
}

/// The first deterministic location that references a fixed environment value,
/// carried by a refusal without any captured value.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenderOrigin {
    location: String,
}

impl RenderOrigin {
    pub fn new(location: impl Into<String>) -> Self {
        Self {
            location: location.into(),
        }
    }

    pub fn location(&self) -> &str {
        &self.location
    }
}

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

/// The immutable analysis result: whether the loaded render program can observe
/// any fixed environment value, and the first location that does. This is the
/// private analysis mask; it is not exposed to callers.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EnvironmentNeed {
    None,
    Needed(RenderOrigin),
}

/// A failure to admit environment access. Only [`Self::TrustRequired`] maps to a
/// stage trust refusal; any other fault retains its typed detail.
#[derive(Debug, thiserror::Error)]
pub enum EnvironmentAdmissionError {
    #[error("environment access at {reference} requires stage trust")]
    TrustRequired { reference: RenderOrigin },
}

/// Combines the immutable analysis need with one caller decision, capturing the
/// closed five-value snapshot exactly once when granted and never otherwise.
pub(crate) fn admit(
    need: &EnvironmentNeed,
    decision: EnvironmentDecision,
    source: &mut impl FixedEnvironmentSource,
) -> Result<EnvironmentSnapshot, EnvironmentAdmissionError> {
    let has_need = matches!(need, EnvironmentNeed::Needed(_));
    match decision {
        EnvironmentDecision::Deny => Ok(EnvironmentSnapshot::Unavailable),
        EnvironmentDecision::CarryStageGrant => Ok(EnvironmentSnapshot::Captured(source.capture())),
        EnvironmentDecision::GrantIfNeeded if has_need => {
            Ok(EnvironmentSnapshot::Captured(source.capture()))
        }
        EnvironmentDecision::GrantIfNeeded => Ok(EnvironmentSnapshot::Unavailable),
        EnvironmentDecision::RequireStageGrant => match need {
            EnvironmentNeed::Needed(origin) => Err(EnvironmentAdmissionError::TrustRequired {
                reference: origin.clone(),
            }),
            EnvironmentNeed::None => Ok(EnvironmentSnapshot::Unavailable),
        },
    }
}

/// The versioned staged wire for the invocation context. Separate from the live
/// domain carrier; it duplicates neither the target nor the formal name.
#[derive(Serialize, Deserialize, Clone)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum InvocationContextWire {
    Legacy,
    Current {
        short_name: String,
        aliases: Vec<String>,
        source: Option<String>,
        host: HostFactsWire,
        execution: ExecutionFactsWire,
        environment: EnvironmentWire,
    },
}

impl std::fmt::Debug for InvocationContextWire {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Legacy => f.write_str("Legacy"),
            Self::Current { .. } => f.write_str("Current { <redacted> }"),
        }
    }
}

impl Default for InvocationContextWire {
    /// A staged record with no context field deserializes as legacy.
    fn default() -> Self {
        Self::Legacy
    }
}

impl InvocationContextWire {
    /// Whether this is the legacy projection. A legacy context is not
    /// serialized, so a record written by [`super::staging::StagedRecord::new`]
    /// stays byte-compatible with a pre-context record.
    pub(crate) fn is_legacy(&self) -> bool {
        matches!(self, Self::Legacy)
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub(crate) struct HostFactsWire {
    os: String,
    arch: String,
    os_name: Option<String>,
    os_id: Option<String>,
    os_id_like: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
pub(crate) struct ExecutionFactsWire {
    is_admin: bool,
    is_interactive: bool,
}

/// The staged environment wire. Its `Debug` is redacted.
#[derive(Serialize, Deserialize, Clone)]
pub(crate) enum EnvironmentWire {
    Unavailable,
    Captured {
        user: Option<String>,
        hostname: Option<String>,
        editor: Option<String>,
        shell: Option<String>,
        visual: Option<String>,
    },
}

impl std::fmt::Debug for EnvironmentWire {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Unavailable => f.write_str("Unavailable"),
            Self::Captured { .. } => f.write_str("Captured { <redacted> }"),
        }
    }
}

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

    /// A fixed-value source that counts captures and returns generic values, so
    /// tests can assert the exact number of reads.
    #[derive(Default)]
    struct SpySource {
        captures: Cell<u32>,
    }
    impl FixedEnvironmentSource for &SpySource {
        fn capture(&mut self) -> FixedEnvironment {
            self.captures.set(self.captures.get() + 1);
            FixedEnvironment::new(
                Some("sample-user".into()),
                Some("sample-host".into()),
                Some("sample-editor".into()),
                None,
                None,
            )
        }
    }

    fn origin() -> RenderOrigin {
        RenderOrigin::new("interview[0].prompt")
    }

    #[test]
    fn admit_matrix_captures_exactly_once_when_granted() {
        // none + Deny/GrantIfNeeded/RequireStageGrant -> Unavailable, zero reads.
        for decision in [
            EnvironmentDecision::Deny,
            EnvironmentDecision::GrantIfNeeded,
            EnvironmentDecision::RequireStageGrant,
        ] {
            let spy = SpySource::default();
            let snapshot = admit(&EnvironmentNeed::None, decision, &mut &spy).unwrap();
            assert!(matches!(snapshot, EnvironmentSnapshot::Unavailable));
            assert_eq!(spy.captures.get(), 0, "{decision:?} read a value");
        }
        // none + CarryStageGrant -> Captured, exactly one read.
        let spy = SpySource::default();
        let snapshot = admit(
            &EnvironmentNeed::None,
            EnvironmentDecision::CarryStageGrant,
            &mut &spy,
        )
        .unwrap();
        assert!(matches!(snapshot, EnvironmentSnapshot::Captured(_)));
        assert_eq!(spy.captures.get(), 1);
        // present + Deny -> Unavailable, zero reads.
        let spy = SpySource::default();
        let snapshot = admit(
            &EnvironmentNeed::Needed(origin()),
            EnvironmentDecision::Deny,
            &mut &spy,
        )
        .unwrap();
        assert!(matches!(snapshot, EnvironmentSnapshot::Unavailable));
        assert_eq!(spy.captures.get(), 0);
        // present + GrantIfNeeded/CarryStageGrant -> Captured, one read.
        for decision in [
            EnvironmentDecision::GrantIfNeeded,
            EnvironmentDecision::CarryStageGrant,
        ] {
            let spy = SpySource::default();
            let snapshot = admit(&EnvironmentNeed::Needed(origin()), decision, &mut &spy).unwrap();
            assert!(matches!(snapshot, EnvironmentSnapshot::Captured(_)));
            assert_eq!(spy.captures.get(), 1, "{decision:?}");
        }
        // present + RequireStageGrant -> TrustRequired, zero reads, carries origin.
        let spy = SpySource::default();
        let error = admit(
            &EnvironmentNeed::Needed(origin()),
            EnvironmentDecision::RequireStageGrant,
            &mut &spy,
        )
        .unwrap_err();
        let EnvironmentAdmissionError::TrustRequired { reference } = error;
        assert_eq!(reference, origin());
        assert_eq!(spy.captures.get(), 0);
    }

    #[test]
    fn parse_os_release_reads_name_id_and_id_like() {
        let text = "\
# a comment
NAME=\"Sample Linux\"
ID=sample
ID_LIKE=\"debian ubuntu\"
";
        let (name, id, id_like) = parse_os_release(text);
        assert_eq!(name.as_deref(), Some("Sample Linux"));
        assert_eq!(id.as_deref(), Some("sample"));
        assert_eq!(id_like, vec!["debian".to_owned(), "ubuntu".to_owned()]);
    }

    #[test]
    fn parse_os_release_last_assignment_wins_and_empties_are_none() {
        let (name, id, id_like) =
            parse_os_release("NAME=first\nNAME=second\nID=\"\"\nID_LIKE=\"\"");
        assert_eq!(name.as_deref(), Some("second"));
        assert_eq!(id, None);
        assert!(id_like.is_empty());
    }

    #[test]
    fn parse_os_release_unescapes_double_quotes_without_executing() {
        let (name, _, _) = parse_os_release(r#"NAME="a \"b\" \\ \$c""#);
        assert_eq!(name.as_deref(), Some(r#"a "b" \ $c"#));
    }

    #[test]
    fn absent_or_malformed_os_release_yields_all_unavailable() {
        let (name, id, id_like) = parse_os_release("garbage-with-no-assignments\n\n");
        assert_eq!(name, None);
        assert_eq!(id, None);
        assert!(id_like.is_empty());
    }

    fn current_context() -> InvocationContext {
        let target = crate::staging::canonical_target(std::path::Path::new(".")).unwrap();
        InvocationContext::new(
            target,
            SelectedTemplate::new(
                "github.com/acme/widget".into(),
                "widget".into(),
                vec!["w".into()],
                Some("https://example.invalid/widget".into()),
            ),
            HostFacts::new(
                "linux".into(),
                "x86_64".into(),
                Some("Sample Linux".into()),
                Some("sample".into()),
                vec!["debian".into()],
            ),
            ExecutionFacts::new(true, false),
            EnvironmentSnapshot::Captured(FixedEnvironment::new(
                Some("sample-user".into()),
                None,
                Some("hx".into()),
                None,
                None,
            )),
        )
        .unwrap()
    }

    #[test]
    fn current_context_projects_all_seventeen_names() {
        let mut out = BTreeMap::new();
        current_context().project(&mut out);
        for name in RESERVED_NAMES {
            assert!(out.contains_key(name), "missing {name}");
        }
        assert_eq!(out.len(), 17);
        assert_eq!(out["toha_template_name"], Value::String("widget".into()));
        assert_eq!(out["toha_is_admin"], Value::Bool(true));
        assert_eq!(out["toha_is_interactive"], Value::Bool(false));
        assert_eq!(out["toha_env_user"], Value::String("sample-user".into()));
        // Unavailable fixed values and an absent host field project null.
        assert_eq!(out["toha_env_hostname"], Value::Null);
        assert_eq!(out["toha_env_shell"], Value::Null);
        assert_eq!(
            out["toha_template_aliases"],
            Value::Array(vec![Value::String("w".into())])
        );
    }

    #[test]
    fn legacy_context_projects_no_reserved_names() {
        let target = crate::staging::canonical_target(std::path::Path::new(".")).unwrap();
        let mut out = BTreeMap::new();
        InvocationContext::legacy(target).project(&mut out);
        assert!(out.is_empty());
    }

    #[test]
    fn environment_and_wire_debug_are_redacted() {
        let env = EnvironmentSnapshot::Captured(FixedEnvironment::new(
            Some("secret-user".into()),
            Some("secret-host".into()),
            None,
            None,
            None,
        ));
        let rendered = format!("{env:?}");
        assert!(!rendered.contains("secret-user"));
        assert!(!rendered.contains("secret-host"));
        let wire = env.wire();
        assert!(!format!("{wire:?}").contains("secret"));
    }

    #[test]
    fn context_round_trips_through_wire() {
        let context = current_context();
        let (target, formal, wire) = context.staged_parts();
        assert_eq!(formal.as_deref(), Some("github.com/acme/widget"));
        let restored = InvocationContext::from_wire(target, &formal.unwrap(), wire);
        let mut before = BTreeMap::new();
        let mut after = BTreeMap::new();
        context.project(&mut before);
        restored.project(&mut after);
        assert_eq!(before, after);
    }

    #[test]
    fn empty_identity_is_rejected() {
        let target = crate::staging::canonical_target(std::path::Path::new(".")).unwrap();
        let result = InvocationContext::new(
            target,
            SelectedTemplate::new(String::new(), "x".into(), vec![], None),
            HostFacts::new("linux".into(), "x86_64".into(), None, None, vec![]),
            ExecutionFacts::new(false, true),
            EnvironmentSnapshot::Unavailable,
        );
        assert!(matches!(result, Err(ContextError::EmptyFormalName)));
    }
}