Skip to main content

harn_vm/
environment_registry.rs

1//! Authoritative registry and startup validation for Harn-owned environment
2//! variables.
3//!
4//! Readers remain at their owning boundaries, where values have the context
5//! needed for full validation. This module owns names, coarse value shapes,
6//! sensitivity metadata, extension policy, and unknown-name diagnostics. The
7//! registry drift test keeps production readers from creating an unregistered
8//! parallel namespace.
9
10use std::ffi::{OsStr, OsString};
11use std::fmt;
12
13const REGISTERED_NAMES: &str = include_str!("environment_registry_names.txt");
14const UNKNOWN_CODE: &str = "HARN-ENV-001";
15const INVALID_VALUE_CODE: &str = "HARN-ENV-002";
16
17/// The subsystem that owns an environment variable.
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub enum EnvironmentConsumer {
20    Runtime,
21    Cli,
22    BuildTooling,
23    TestHarness,
24    EmbedderExtension,
25}
26
27/// The shape enforced at startup, before the owning reader sees the value.
28///
29/// `OwnerValidated` means the value needs domain context and remains validated
30/// by its consumer. The registry still owns and checks the name.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub enum EnvironmentValueShape {
33    OwnerValidated,
34    Boolean,
35    UnsignedInteger,
36    NonNegativeNumber,
37    UnitInterval,
38}
39
40/// Whether a value may contain credential material.
41#[derive(Clone, Copy, Debug, PartialEq, Eq)]
42pub enum EnvironmentSensitivity {
43    Public,
44    Credential,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq)]
48pub struct EnvironmentVariableSpec {
49    pub name: String,
50    pub consumer: EnvironmentConsumer,
51    pub value_shape: EnvironmentValueShape,
52    pub sensitivity: EnvironmentSensitivity,
53}
54
55#[derive(Clone, Debug, PartialEq, Eq)]
56pub enum EnvironmentDiagnosticKind {
57    UnknownName { suggestion: Option<String> },
58    InvalidValue { expected: EnvironmentValueShape },
59}
60
61/// A key-only diagnostic. Values are intentionally absent from the type, so
62/// rendering cannot accidentally disclose credential material.
63#[derive(Clone, Debug, PartialEq, Eq)]
64pub struct EnvironmentDiagnostic {
65    pub code: &'static str,
66    pub key: String,
67    pub kind: EnvironmentDiagnosticKind,
68}
69
70impl fmt::Display for EnvironmentDiagnostic {
71    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
72        match &self.kind {
73            EnvironmentDiagnosticKind::UnknownName { suggestion } => {
74                write!(
75                    formatter,
76                    "{}: unknown Harn environment variable `{}`",
77                    self.code, self.key
78                )?;
79                if let Some(suggestion) = suggestion {
80                    write!(formatter, "; did you mean `{suggestion}`?")?;
81                }
82                Ok(())
83            }
84            EnvironmentDiagnosticKind::InvalidValue { expected } => write!(
85                formatter,
86                "{}: environment variable `{}` must be {}",
87                self.code,
88                self.key,
89                expected.description()
90            ),
91        }
92    }
93}
94
95#[derive(Clone, Debug, PartialEq, Eq)]
96pub struct EnvironmentValidationError {
97    diagnostics: Vec<EnvironmentDiagnostic>,
98}
99
100impl EnvironmentValidationError {
101    pub fn diagnostics(&self) -> &[EnvironmentDiagnostic] {
102        &self.diagnostics
103    }
104}
105
106impl fmt::Display for EnvironmentValidationError {
107    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
108        for (index, diagnostic) in self.diagnostics.iter().enumerate() {
109            if index > 0 {
110                formatter.write_str("\n")?;
111            }
112            diagnostic.fmt(formatter)?;
113        }
114        Ok(())
115    }
116}
117
118impl std::error::Error for EnvironmentValidationError {}
119
120impl EnvironmentValueShape {
121    fn description(self) -> &'static str {
122        match self {
123            Self::OwnerValidated => "valid for its owning subsystem",
124            Self::Boolean => "a boolean (`true`, `false`, `yes`, `no`, `on`, `off`, `1`, or `0`)",
125            Self::UnsignedInteger => "an unsigned integer",
126            Self::NonNegativeNumber => "a non-negative number",
127            Self::UnitInterval => "a number between 0 and 1",
128        }
129    }
130
131    fn accepts(self, value: &OsStr) -> bool {
132        let Some(value) = value.to_str() else {
133            return matches!(self, Self::OwnerValidated);
134        };
135        match self {
136            Self::OwnerValidated => true,
137            Self::Boolean => matches!(
138                value.trim().to_ascii_lowercase().as_str(),
139                "1" | "0" | "true" | "false" | "yes" | "no" | "on" | "off"
140            ),
141            Self::UnsignedInteger => value.trim().parse::<u64>().is_ok(),
142            Self::NonNegativeNumber => value
143                .trim()
144                .parse::<f64>()
145                .is_ok_and(|parsed| parsed.is_finite() && parsed >= 0.0),
146            Self::UnitInterval => value
147                .trim()
148                .parse::<f64>()
149                .is_ok_and(|parsed| parsed.is_finite() && (0.0..=1.0).contains(&parsed)),
150        }
151    }
152}
153
154/// Look up registry metadata for a Harn-owned key.
155pub fn variable_spec(name: &str) -> Option<EnvironmentVariableSpec> {
156    if registered_names().binary_search(&name).is_ok() {
157        return Some(spec_for_registered_name(name));
158    }
159    if is_extension_name(name) {
160        return Some(EnvironmentVariableSpec {
161            name: name.to_string(),
162            consumer: EnvironmentConsumer::EmbedderExtension,
163            value_shape: EnvironmentValueShape::OwnerValidated,
164            sensitivity: sensitivity_for(name),
165        });
166    }
167    if is_structured_runtime_name(name) {
168        return Some(spec_for_registered_name(name));
169    }
170    None
171}
172
173/// Validate the live process environment at the CLI/embedded-runtime startup
174/// boundary.
175pub fn validate_startup_environment() -> Result<(), EnvironmentValidationError> {
176    validate_environment(std::env::vars_os())
177}
178
179/// Validate a supplied environment snapshot. This is the deterministic core
180/// used by hosts and tests; values never enter diagnostics.
181pub fn validate_environment<I, K, V>(vars: I) -> Result<(), EnvironmentValidationError>
182where
183    I: IntoIterator<Item = (K, V)>,
184    K: Into<OsString>,
185    V: Into<OsString>,
186{
187    let mut diagnostics = Vec::new();
188    for (key, value) in vars {
189        let key = key.into();
190        let Some(key) = key.to_str() else {
191            continue;
192        };
193        if !key.starts_with("HARN_") {
194            continue;
195        }
196        let Some(spec) = variable_spec(key) else {
197            diagnostics.push(EnvironmentDiagnostic {
198                code: UNKNOWN_CODE,
199                key: key.to_string(),
200                kind: EnvironmentDiagnosticKind::UnknownName {
201                    suggestion: nearest_registered_name(key),
202                },
203            });
204            continue;
205        };
206        let value = value.into();
207        if !spec.value_shape.accepts(&value) {
208            diagnostics.push(EnvironmentDiagnostic {
209                code: INVALID_VALUE_CODE,
210                key: key.to_string(),
211                kind: EnvironmentDiagnosticKind::InvalidValue {
212                    expected: spec.value_shape,
213                },
214            });
215        }
216    }
217    diagnostics.sort_by(|left, right| left.key.cmp(&right.key));
218    if diagnostics.is_empty() {
219        Ok(())
220    } else {
221        Err(EnvironmentValidationError { diagnostics })
222    }
223}
224
225fn registered_names() -> &'static [&'static str] {
226    static NAMES: std::sync::OnceLock<Vec<&'static str>> = std::sync::OnceLock::new();
227    NAMES
228        .get_or_init(|| {
229            REGISTERED_NAMES
230                .lines()
231                .filter(|name| !name.is_empty())
232                .collect()
233        })
234        .as_slice()
235}
236
237fn spec_for_registered_name(name: &str) -> EnvironmentVariableSpec {
238    EnvironmentVariableSpec {
239        name: name.to_string(),
240        consumer: consumer_for(name),
241        value_shape: value_shape_for(name),
242        sensitivity: sensitivity_for(name),
243    }
244}
245
246fn consumer_for(name: &str) -> EnvironmentConsumer {
247    if name.starts_with("HARN_TEST_") || name.contains("_TEST_") || name.starts_with("HARN_E2E_") {
248        EnvironmentConsumer::TestHarness
249    } else if [
250        "HARN_BUILD_",
251        "HARN_CARGO_",
252        "HARN_CHECK_",
253        "HARN_CI_",
254        "HARN_CODEGEN_",
255        "HARN_DEV_",
256        "HARN_RELEASE_",
257    ]
258    .iter()
259    .any(|prefix| name.starts_with(prefix))
260    {
261        EnvironmentConsumer::BuildTooling
262    } else if name.starts_with("HARN_CLI_")
263        || name.starts_with("HARN_DOCTOR_")
264        || name.starts_with("HARN_INIT_")
265    {
266        EnvironmentConsumer::Cli
267    } else {
268        EnvironmentConsumer::Runtime
269    }
270}
271
272fn value_shape_for(name: &str) -> EnvironmentValueShape {
273    match name {
274        "HARN_LLM_TIMEOUT"
275        | "HARN_LLM_IDLE_TIMEOUT"
276        | "HARN_LLM_FIRST_TOKEN_TIMEOUT"
277        | "HARN_MAX_CONCURRENCY"
278        | "HARN_RETENTION_DAYS"
279        | "HARN_TOKEN_BUDGET"
280        | "HARN_EVENT_LOG_QUEUE_DEPTH" => EnvironmentValueShape::UnsignedInteger,
281        "HARN_BUDGET_USD" => EnvironmentValueShape::NonNegativeNumber,
282        "HARN_OTEL_SAMPLE_RATIO" => EnvironmentValueShape::UnitInterval,
283        "HARN_BYTECODE_CACHE"
284        | "HARN_LLM_STREAM"
285        | "HARN_REPLAY_ENABLED"
286        | "HARN_REQUIRE_SIGNED_SKILLS"
287        | "HARN_TRACE"
288        | "HARN_VERBOSE_CONFIG" => EnvironmentValueShape::Boolean,
289        _ => EnvironmentValueShape::OwnerValidated,
290    }
291}
292
293fn sensitivity_for(name: &str) -> EnvironmentSensitivity {
294    if [
295        "TOKEN",
296        "SECRET",
297        "PASSWORD",
298        "API_KEY",
299        "OAUTH_KEY",
300        "HEADERS",
301        "PRIVATE_KEY",
302    ]
303    .iter()
304    .any(|fragment| name.contains(fragment))
305    {
306        EnvironmentSensitivity::Credential
307    } else {
308        EnvironmentSensitivity::Public
309    }
310}
311
312/// Downstream embedders own this one explicit namespace. A nonempty
313/// uppercase-identifier suffix prevents `HARN_EXT_` from becoming a blanket
314/// bypass for malformed names.
315fn is_extension_name(name: &str) -> bool {
316    name.strip_prefix("HARN_EXT_")
317        .is_some_and(is_upper_identifier)
318}
319
320/// Runtime-generated families have a structural grammar instead of a broad
321/// prefix exception. This admits model-role, rate-limit, and secret-provider
322/// keys without accepting near-miss fixed keys such as `HARN_LLM_TIMOUT`.
323fn is_structured_runtime_name(name: &str) -> bool {
324    is_secret_name(name)
325        || is_rate_limit_name(name)
326        || is_model_role_name(name)
327        || is_agent_model_option_name(name)
328}
329
330fn is_secret_name(name: &str) -> bool {
331    name.strip_prefix("HARN_SECRET_")
332        .is_some_and(is_upper_identifier)
333}
334
335fn is_rate_limit_name(name: &str) -> bool {
336    let Some(suffix) = name.strip_prefix("HARN_RATE_LIMIT_") else {
337        return false;
338    };
339    let Some((provider, field)) = suffix.rsplit_once('_') else {
340        return false;
341    };
342    is_upper_identifier(provider) && matches!(field, "QUEUE" | "RPM" | "TPM" | "CONCURRENCY")
343}
344
345fn is_model_role_name(name: &str) -> bool {
346    let suffix = name
347        .strip_prefix("HARN_LLM_ROLE_")
348        .or_else(|| name.strip_prefix("HARN_LLM_"));
349    let Some(suffix) = suffix else {
350        return false;
351    };
352    ["_MODEL", "_PROVIDER", "_ROUTE_POLICY"]
353        .iter()
354        .find_map(|ending| suffix.strip_suffix(ending))
355        .is_some_and(is_upper_identifier)
356}
357
358/// `std/agent/options` derives role-specific configuration keys from a role
359/// token and a closed suffix vocabulary. Keep that dynamic reader family
360/// structural so custom roles do not require per-role registry entries.
361fn is_agent_model_option_name(name: &str) -> bool {
362    const SUFFIXES: &[&str] = &[
363        "_EFFORT",
364        "_MODEL",
365        "_MODEL_ROLE",
366        "_PROVIDER",
367        "_REASONING_TASK",
368        "_TOOL_FORMAT",
369    ];
370    let Some(prefix) = SUFFIXES.iter().find_map(|suffix| name.strip_suffix(suffix)) else {
371        return false;
372    };
373    let Some(prefix) = prefix.strip_prefix("HARN_") else {
374        return false;
375    };
376    let role = prefix
377        .strip_prefix("AGENT_")
378        .or_else(|| prefix.strip_prefix("LLM_"))
379        .unwrap_or(prefix);
380    matches!(prefix, "AGENT" | "LLM") || is_upper_identifier(role)
381}
382
383fn is_upper_identifier(value: &str) -> bool {
384    !value.is_empty()
385        && value
386            .bytes()
387            .all(|byte| byte.is_ascii_uppercase() || byte.is_ascii_digit() || byte == b'_')
388        && !value.starts_with('_')
389        && !value.ends_with('_')
390}
391
392fn nearest_registered_name(name: &str) -> Option<String> {
393    registered_names()
394        .iter()
395        .copied()
396        .filter(|candidate| !candidate.ends_with('_'))
397        .map(|candidate| (strsim::levenshtein(name, candidate), candidate))
398        .min_by_key(|(distance, candidate)| (*distance, *candidate))
399        .filter(|(distance, _)| *distance <= 3)
400        .map(|(_, candidate)| candidate.to_string())
401}
402
403#[cfg(test)]
404mod tests {
405    use super::*;
406
407    #[test]
408    fn typo_is_typed_and_suggests_registered_name() {
409        let error = validate_environment([("HARN_LLM_TIMOUT", "30")]).unwrap_err();
410        assert_eq!(
411            error.diagnostics(),
412            &[EnvironmentDiagnostic {
413                code: UNKNOWN_CODE,
414                key: "HARN_LLM_TIMOUT".to_string(),
415                kind: EnvironmentDiagnosticKind::UnknownName {
416                    suggestion: Some("HARN_LLM_TIMEOUT".to_string()),
417                },
418            }]
419        );
420    }
421
422    #[test]
423    fn known_and_structured_extension_names_are_accepted() {
424        validate_environment([
425            ("HARN_LLM_TIMEOUT", "30"),
426            ("HARN_EXT_ACME_MODE", "custom"),
427            ("HARN_LLM_ROLE_REVIEW_MODEL", "reviewer"),
428            ("HARN_SECRET_ACME_TOKEN", "credential"),
429        ])
430        .unwrap();
431    }
432
433    #[test]
434    fn malformed_extension_name_is_not_a_prefix_bypass() {
435        let error = validate_environment([("HARN_EXT_", "anything")]).unwrap_err();
436        assert!(matches!(
437            error.diagnostics()[0].kind,
438            EnvironmentDiagnosticKind::UnknownName { .. }
439        ));
440    }
441
442    #[test]
443    fn dynamic_agent_role_options_follow_a_closed_structural_grammar() {
444        for name in [
445            "HARN_AGENT_MODEL",
446            "HARN_LLM_TOOL_FORMAT",
447            "HARN_AGENT_REVIEW_PROVIDER",
448            "HARN_LLM_PLANNER_REASONING_TASK",
449            "HARN_RELEASE_EFFORT",
450        ] {
451            assert!(variable_spec(name).is_some(), "{name}");
452        }
453        for name in [
454            "HARN_AGENT_REVIEW_UNKNOWN",
455            "HARN_LLM_TIMOUT",
456            "HARN_RELEASE_",
457        ] {
458            assert!(variable_spec(name).is_none(), "{name}");
459        }
460    }
461
462    #[test]
463    fn credential_metadata_covers_non_api_oauth_keys() {
464        assert_eq!(
465            variable_spec("HARN_OAUTH_KEY").unwrap().sensitivity,
466            EnvironmentSensitivity::Credential
467        );
468    }
469
470    #[test]
471    fn invalid_known_value_is_rejected_at_startup() {
472        let error = validate_environment([("HARN_LLM_TIMEOUT", "soon")]).unwrap_err();
473        assert_eq!(
474            error.diagnostics()[0].kind,
475            EnvironmentDiagnosticKind::InvalidValue {
476                expected: EnvironmentValueShape::UnsignedInteger
477            }
478        );
479    }
480
481    #[test]
482    fn diagnostics_cannot_render_values_even_for_credentials() {
483        let secret = "must-never-appear";
484        let error = validate_environment([("HARN_CLOUD_API_KEZ", secret)]).unwrap_err();
485        let rendered = error.to_string();
486        assert!(rendered.contains("HARN_CLOUD_API_KEZ"));
487        assert!(!rendered.contains(secret));
488    }
489
490    #[test]
491    fn registry_is_sorted_unique_and_contains_metadata() {
492        let names = registered_names();
493        assert!(
494            names.windows(2).all(|pair| pair[0] < pair[1]),
495            "environment registry must remain sorted and unique"
496        );
497        let timeout = variable_spec("HARN_LLM_TIMEOUT").unwrap();
498        assert_eq!(timeout.consumer, EnvironmentConsumer::Runtime);
499        assert_eq!(timeout.value_shape, EnvironmentValueShape::UnsignedInteger);
500        let token = variable_spec("HARN_PACKAGE_REGISTRY_TOKEN").unwrap();
501        assert_eq!(token.sensitivity, EnvironmentSensitivity::Credential);
502    }
503
504    #[test]
505    fn embedded_runtime_bootstrap_accepts_registered_process_environment() {
506        crate::initialize_runtime().expect("registered process environment");
507    }
508
509    #[test]
510    fn every_compiled_harn_name_is_registered_or_structurally_owned() {
511        let crates_dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
512            .parent()
513            .expect("harn-vm lives below crates");
514        let mut missing = std::collections::BTreeSet::new();
515        for entry in walkdir::WalkDir::new(crates_dir)
516            .into_iter()
517            .filter_map(Result::ok)
518            .filter(|entry| {
519                entry.file_type().is_file()
520                    && entry.path().extension().and_then(OsStr::to_str) == Some("rs")
521                    && entry
522                        .path()
523                        .components()
524                        .any(|component| component.as_os_str() == "src")
525                    && entry.file_name() != "environment_registry.rs"
526            })
527        {
528            let source = std::fs::read_to_string(entry.path()).expect("read Rust source");
529            for token in harn_name_tokens(&source) {
530                if variable_spec(token).is_none()
531                    && !matches!(token, "HARN_LLM_" | "HARN_LLM_ROLE_" | "HARN_SECRET_")
532                {
533                    missing.insert(format!("{}: {token}", entry.path().display()));
534                }
535            }
536        }
537        assert!(
538            missing.is_empty(),
539            "compiled HARN_* names missing from environment_registry_names.txt:\n{}",
540            missing.into_iter().collect::<Vec<_>>().join("\n")
541        );
542    }
543
544    #[test]
545    fn every_harn_script_owned_name_is_registered_or_structurally_owned() {
546        let workspace_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
547            .ancestors()
548            .nth(2)
549            .expect("harn-vm lives below workspace/crates");
550        let source_roots = [
551            "benchmarks",
552            "conformance",
553            "crates",
554            "evals",
555            "examples",
556            "experiments",
557            "perf",
558            "personas",
559            "scripts",
560            "tests",
561        ];
562        let mut missing = std::collections::BTreeSet::new();
563        for source_root in source_roots {
564            let source_root = workspace_root.join(source_root);
565            if !source_root.exists() {
566                continue;
567            }
568            for entry in walkdir::WalkDir::new(source_root)
569                .into_iter()
570                .filter_map(Result::ok)
571                .filter(|entry| {
572                    entry.file_type().is_file()
573                        && entry.path().extension().and_then(OsStr::to_str) == Some("harn")
574                })
575            {
576                let source = std::fs::read_to_string(entry.path()).expect("read Harn source");
577                for token in harn_name_tokens(&source) {
578                    if variable_spec(token).is_none()
579                        && !matches!(
580                            token,
581                            "HARN_AGENT"
582                                | "HARN_AGENT_"
583                                | "HARN_LLM"
584                                | "HARN_LLM_"
585                                | "HARN_PLANNER"
586                                | "HARN_RELEASE"
587                        )
588                    {
589                        missing.insert(format!("{}: {token}", entry.path().display()));
590                    }
591                }
592            }
593        }
594        assert!(
595            missing.is_empty(),
596            "Harn-script HARN_* names missing from environment_registry_names.txt:\n{}",
597            missing.into_iter().collect::<Vec<_>>().join("\n")
598        );
599    }
600
601    fn harn_name_tokens(source: &str) -> impl Iterator<Item = &str> {
602        source.match_indices("\"HARN_").filter_map(|(quote, _)| {
603            let start = quote + 1;
604            let bytes = source.as_bytes();
605            let mut end = start + "HARN_".len();
606            while end < bytes.len()
607                && (bytes[end].is_ascii_uppercase()
608                    || bytes[end].is_ascii_digit()
609                    || bytes[end] == b'_')
610            {
611                end += 1;
612            }
613            (end > start + "HARN_".len()).then(|| &source[start..end])
614        })
615    }
616}