obzenflow_runtime 0.2.5

Runtime services for ObzenFlow - execution and coordination business logic
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-FileCopyrightText: 2025-2026 ObzenFlow Contributors
// https://obzenflow.dev

//! The immutable resolved snapshot (FLOWIP-010 §7): built once at startup,
//! owned by `FlowApplication`, handed to the flow build and the read
//! surface as `Arc` handles. Rebuild-and-swap is the only mutation model
//! (§7 snapshot lock); nothing here mutates in place.

use super::candidates::{CandidateSet, ConfigValue};
use super::error::ConfigResolveError;
use super::schema::{knob, knob_registry, KnobDefault, KnobSpec, Redaction};
use obzenflow_core::config::{
    ConfigScope, ConfigSource, ConfigSubject, ConfigValueMeta, ResolvedForDoc, ResolvedValueDoc,
    SecretRef,
};

/// A resolved value with both provenance axes.
#[derive(Debug, Clone, PartialEq)]
pub struct Resolved<T> {
    pub value: T,
    pub meta: ConfigValueMeta,
}

/// The non-persisted runtime patch layer. No write path exists in this
/// slice (FLOWIP-010b owns mutation), so the overlay is truthfully empty;
/// the read surface reports it as such rather than omitting it (gap 12).
#[derive(Debug, Clone, Default, PartialEq)]
pub struct RuntimeConfigOverlay {
    entries: Vec<OverlayEntry>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct OverlayEntry {
    pub key_path: String,
    pub scope: ConfigScope,
    pub value: ConfigValue,
}

impl RuntimeConfigOverlay {
    pub fn entries(&self) -> &[OverlayEntry] {
        &self.entries
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

/// The immutable startup snapshot: the scoped candidate table plus the
/// (empty) overlay. Global- and flow-rung views resolve on demand through
/// the one ladder; per-stage and per-edge resolution happens at flow build
/// (`materialize_flow_config`), where the DSL tier and the topology exist.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ResolvedRuntimeConfig {
    candidates: CandidateSet,
    overlay: RuntimeConfigOverlay,
}

impl ResolvedRuntimeConfig {
    pub fn new(candidates: CandidateSet) -> Self {
        Self {
            candidates,
            overlay: RuntimeConfigOverlay::default(),
        }
    }

    /// Built-in defaults only; the explicit test-harness construction
    /// (`FlowBuildContext::for_tests`), never an ambient fallback.
    pub fn builtin_defaults() -> Self {
        Self::default()
    }

    pub fn candidates(&self) -> &CandidateSet {
        &self.candidates
    }

    pub fn overlay(&self) -> &RuntimeConfigOverlay {
        &self.overlay
    }

    /// Resolve one registered knob for a named stage before the complete
    /// per-flow effective view exists.
    ///
    /// This narrow pre-topology view is used by build-only authoring choices
    /// whose selected descriptor must exist before topology admission. It
    /// follows the same stage, flow, global, default ladder as Phase B and
    /// returns the ordinary provenance-bearing value.
    pub fn resolve_stage_value(
        &self,
        key_path: &str,
        stage: impl Into<obzenflow_core::StageKey>,
    ) -> Result<Option<Resolved<ConfigValue>>, ConfigResolveError> {
        let spec = knob(key_path).ok_or_else(|| ConfigResolveError::UnknownKnob {
            key_path: key_path.to_string(),
        })?;
        super::resolve::resolve_at(
            spec,
            &super::resolve::ResolutionPoint::Stage(stage.into()),
            &self.candidates,
        )
    }

    /// Resolve one knob's non-structural rungs (flow, then global, then
    /// default). This is the offline and pre-build view: the DSL tier and
    /// stage/edge scopes do not exist here.
    pub fn resolve_view(
        &self,
        key_path: &str,
    ) -> Result<Option<Resolved<ConfigValue>>, ConfigResolveError> {
        let spec = knob(key_path).ok_or_else(|| ConfigResolveError::UnknownKnob {
            key_path: key_path.to_string(),
        })?;
        Ok(self.resolve_view_spec(spec))
    }

    fn resolve_view_spec(&self, spec: &KnobSpec) -> Option<Resolved<ConfigValue>> {
        for scope in [ConfigScope::Flow, ConfigScope::Global] {
            if let Some(slots) = self.candidates.get(spec.key_path, &scope) {
                for (source, slot) in [
                    (ConfigSource::Cli, &slots.cli),
                    (ConfigSource::File, &slots.file),
                    (ConfigSource::Env, &slots.env),
                    (ConfigSource::Dsl, &slots.dsl),
                ] {
                    if let Some(value) = slot {
                        return Some(Resolved {
                            value: value.clone(),
                            meta: ConfigValueMeta {
                                source,
                                scope,
                                subject: ConfigSubject::Unqualified,
                                key_path: spec.key_path.to_string(),
                            },
                        });
                    }
                }
            }
        }
        match &spec.default {
            KnobDefault::Value(value) => Some(Resolved {
                value: value.clone(),
                meta: ConfigValueMeta {
                    source: ConfigSource::Default,
                    scope: ConfigScope::Global,
                    subject: ConfigSubject::Unqualified,
                    key_path: spec.key_path.to_string(),
                },
            }),
            KnobDefault::OptionalAbsent | KnobDefault::Required => None,
        }
    }

    /// The global-rung view of every registered knob, in evidence form.
    /// Serves the base/effective endpoints' global sections and the offline
    /// CLI (whose honest coverage this is, per §9).
    pub fn global_view(&self) -> Vec<ResolvedValueDoc> {
        knob_registry()
            .iter()
            .filter_map(|spec| self.resolve_view_spec(spec).map(|r| doc_for(spec, &r)))
            .collect()
    }

    /// Typed `ai.models` section (global-target knobs, so the view rung is
    /// final for them).
    pub fn ai_models(&self) -> AiModelsConfig {
        let text = |key: &str| {
            self.resolve_view(key)
                .expect("registry key")
                .map(|resolved| Resolved {
                    value: resolved
                        .value
                        .as_text()
                        .expect("token/text knob resolves text")
                        .to_string(),
                    meta: resolved.meta,
                })
        };
        AiModelsConfig {
            provider: text("ai.models.provider").expect("provider has a built-in default"),
            model: text("ai.models.model"),
            base_url: text("ai.models.base_url"),
            api_key_env: {
                let resolved =
                    text("ai.models.api_key_env").expect("api_key_env has a built-in default");
                Resolved {
                    value: SecretRef::new(resolved.value),
                    meta: resolved.meta,
                }
            },
        }
    }
}

/// Typed `[ai.models]` view (FLOWIP-010 absorption of `ModelConfig`'s env
/// surface). `api_key_env` is the visible secret REFERENCE (§13).
#[derive(Debug, Clone, PartialEq)]
pub struct AiModelsConfig {
    pub provider: Resolved<String>,
    pub model: Option<Resolved<String>>,
    pub base_url: Option<Resolved<String>>,
    pub api_key_env: Resolved<SecretRef>,
}

/// Evidence/DTO rendering with schema-driven redaction (§13: resolved
/// secret values redact; reference names stay visible).
pub fn doc_for(spec: &KnobSpec, resolved: &Resolved<ConfigValue>) -> ResolvedValueDoc {
    doc_for_at(spec, resolved, None)
}

/// Evidence/DTO rendering for a concrete application point. Effect rows carry
/// both the point and the winning subject; topology-free rows retain the
/// version-1-compatible compact shape.
pub fn doc_for_at(
    spec: &KnobSpec,
    resolved: &Resolved<ConfigValue>,
    resolved_for: Option<ResolvedForDoc>,
) -> ResolvedValueDoc {
    let redacted = matches!(spec.redaction, Redaction::SecretValue);
    let winner_subject = resolved_for.as_ref().map(|_| resolved.meta.subject.clone());
    ResolvedValueDoc {
        key_path: spec.key_path.to_string(),
        resolved_for,
        scope: resolved.meta.scope.to_string(),
        source: resolved.meta.source.to_string(),
        winner_subject,
        value: if redacted {
            serde_json::json!("***redacted***")
        } else {
            resolved.value.to_json()
        },
        redacted,
    }
}

/// One difference between two doc sets, compared per
/// `(key_path, resolved_for, scope)`.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub struct ConfigDiffEntry {
    pub key_path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub resolved_for: Option<ResolvedForDoc>,
    pub scope: String,
    pub base: Option<serde_json::Value>,
    pub effective: Option<serde_json::Value>,
}

/// Diff two doc sets (base vs effective). With the overlay empty in this
/// slice the result is truthfully empty; asserted, not assumed (gap 12).
pub fn diff(base: &[ResolvedValueDoc], effective: &[ResolvedValueDoc]) -> Vec<ConfigDiffEntry> {
    use std::collections::BTreeMap;
    let index = |docs: &[ResolvedValueDoc]| -> BTreeMap<
        (String, Option<ResolvedForDoc>, String),
        serde_json::Value,
    > {
        docs.iter()
            .map(|d| {
                (
                    (
                        d.key_path.clone(),
                        d.resolved_for.clone(),
                        d.scope.clone(),
                    ),
                    d.value.clone(),
                )
            })
            .collect()
    };
    let base_index = index(base);
    let effective_index = index(effective);
    let mut keys: Vec<_> = base_index.keys().chain(effective_index.keys()).collect();
    keys.sort();
    keys.dedup();
    keys.into_iter()
        .filter_map(|key| {
            let base_value = base_index.get(key);
            let effective_value = effective_index.get(key);
            if base_value == effective_value {
                None
            } else {
                Some(ConfigDiffEntry {
                    key_path: key.0.clone(),
                    resolved_for: key.1.clone(),
                    scope: key.2.clone(),
                    base: base_value.cloned(),
                    effective: effective_value.cloned(),
                })
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime_config::candidates::ScopedCandidate;
    use crate::runtime_config::schema::{
        EdgeEndpoint, EnvBinding, KnobTarget, KnobType, Mutability,
    };

    #[test]
    fn builtin_defaults_view_reports_default_source() {
        let snapshot = ResolvedRuntimeConfig::builtin_defaults();
        let docs = snapshot.global_view();
        let lineage = docs
            .iter()
            .find(|d| d.key_path == "runtime.max_lineage_depth")
            .unwrap();
        assert_eq!(lineage.value, serde_json::json!(100));
        assert_eq!(lineage.source, "default");
        assert_eq!(lineage.scope, "global");
        // OptionalAbsent knobs produce no doc.
        assert!(!docs
            .iter()
            .any(|d| d.key_path == "runtime.backpressure.window"));
    }

    #[test]
    fn ai_models_typed_view_carries_provenance_and_the_visible_reference() {
        let mut set = CandidateSet::default();
        set.admit(ScopedCandidate::unqualified(
            "ai.models.provider",
            ConfigScope::Global,
            ConfigSource::File,
            ConfigValue::Text("openai".to_string()),
        ))
        .unwrap();
        let snapshot = ResolvedRuntimeConfig::new(set);
        let ai = snapshot.ai_models();
        assert_eq!(ai.provider.value, "openai");
        assert_eq!(ai.provider.meta.source, ConfigSource::File);
        assert_eq!(ai.api_key_env.value.env_name(), "OPENAI_API_KEY");
        assert_eq!(ai.api_key_env.meta.source, ConfigSource::Default);
    }

    #[test]
    fn secret_value_redaction_machinery_works_via_a_synthetic_spec() {
        let spec = KnobSpec {
            key_path: "ai.endpoints.resolved_api_key",
            file_path: None,
            value_type: KnobType::Text,
            target: KnobTarget::StageOrEffect,
            default: KnobDefault::OptionalAbsent,
            mutability: Mutability::Restartful,
            redaction: Redaction::SecretValue,
            env: EnvBinding::None,
        };
        let resolved = Resolved {
            value: ConfigValue::Text("hunter2".to_string()),
            meta: ConfigValueMeta {
                source: ConfigSource::File,
                scope: ConfigScope::Global,
                subject: ConfigSubject::Unqualified,
                key_path: spec.key_path.to_string(),
            },
        };
        let doc = doc_for(&spec, &resolved);
        assert!(doc.redacted);
        assert_eq!(doc.value, serde_json::json!("***redacted***"));

        let effect_type = obzenflow_core::event::EffectType::from("payments.authorize");
        let effect_resolved = Resolved {
            value: ConfigValue::Text("still-secret".to_string()),
            meta: ConfigValueMeta {
                source: ConfigSource::File,
                scope: ConfigScope::stage("authorize_payment"),
                subject: ConfigSubject::Effect {
                    effect_type: effect_type.clone(),
                },
                key_path: spec.key_path.to_string(),
            },
        };
        let effect_doc = doc_for_at(
            &spec,
            &effect_resolved,
            Some(ResolvedForDoc::Effect {
                stage: "authorize_payment".to_string(),
                effect_type: effect_type.as_str().to_string(),
            }),
        );
        assert!(effect_doc.redacted);
        assert_eq!(effect_doc.value, serde_json::json!("***redacted***"));
        assert_eq!(
            effect_doc.winner_subject,
            Some(ConfigSubject::Effect { effect_type })
        );
        assert!(effect_doc.resolved_for.is_some());
        let _ = EdgeEndpoint::Upstream; // silence unused import in cfg(test)
    }

    #[test]
    fn overlay_is_truthfully_empty_and_diff_is_empty() {
        let snapshot = ResolvedRuntimeConfig::builtin_defaults();
        assert!(snapshot.overlay().is_empty());
        let docs = snapshot.global_view();
        assert!(diff(&docs, &docs).is_empty());
    }

    #[test]
    fn diff_identity_keeps_equal_key_and_scope_effect_points_separate() {
        let row = |effect_type: &str, value: u64| ResolvedValueDoc {
            key_path: "effects.resilience.breaker.minimum_calls".to_string(),
            resolved_for: Some(ResolvedForDoc::Effect {
                stage: "authorize_payment".to_string(),
                effect_type: effect_type.to_string(),
            }),
            scope: "stage:authorize_payment".to_string(),
            source: "file".to_string(),
            winner_subject: Some(ConfigSubject::Unqualified),
            value: serde_json::json!(value),
            redacted: false,
        };
        let base = vec![row("payments.authorize", 8), row("payments.refund", 8)];
        let effective = vec![row("payments.authorize", 8), row("payments.refund", 5)];
        let changes = diff(&base, &effective);
        assert_eq!(changes.len(), 1);
        assert!(matches!(
            changes[0].resolved_for.as_ref(),
            Some(ResolvedForDoc::Effect { effect_type, .. }) if effect_type == "payments.refund"
        ));
        assert_eq!(changes[0].base, Some(serde_json::json!(8)));
        assert_eq!(changes[0].effective, Some(serde_json::json!(5)));
    }
}