subc-daemon 0.50.32

Embeddable subc daemon: bootstrap, module supervision, and opaque-byte splice routing.
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
//! Settings reads separate admitted schema metadata from approved delivery values.

use super::settings_observation::{SettingsFiles, SettingsObservation};
use super::*;
use crate::config_gate::{reviewed_build, GateSnapshot, InputKey};
use crate::config_store::{HistoryEntry, StoreBody};
use serde_json::Value;
use subc_config::{Layer, Schema};
use subc_control::config::{
    ConfigControlResponse, ConfigModule, ConfigSettingsLayer, ConfigThrough,
};

#[path = "settings_projects.rs"]
pub(super) mod projects;

/// Owner inputs that can change a settings row without a file or catalog edit.
/// The publication separately compares the ungated module IDs from `config.status`.
#[derive(PartialEq, Eq)]
pub(super) struct SettingsVersion {
    store: (StoreState, u64),
    active: Vec<(ConnectionId, Option<u64>)>,
    plans: (u64, usize),
    live_projects: u64,
}

impl SettingsVersion {
    pub(super) fn from_state(state: &OwnerState) -> Self {
        Self {
            store: (
                state.store.loaded_admission_state().0,
                state.store.publication_sequence(),
            ),
            // Declarations are immutable per connection. Track their identity
            // and fallback acknowledgement without comparing the schemas.
            active: state
                .active_declarations
                .values()
                .map(|connection| {
                    (
                        *connection,
                        state.delivery.fallback_applied.get(connection).copied(),
                    )
                })
                .collect(),
            // Insertion (including replacement) advances the sequence; removal
            // alone changes the count. Neither requires inspecting plan bodies.
            plans: (state.gate.next_plan_sequence, state.gate.plans.len()),
            live_projects: state.delivery.live_projects.settings_revision,
        }
    }
}

struct SettingsSnapshot {
    declaration_pending: BTreeMap<String, subc_protocol::config::ConfigDeclarationPending>,
    fallback_applied: BTreeSet<String>,
    stamped: bool,
    declarations: BTreeMap<String, ConfigDeclareRequest>,
    pending: BTreeMap<String, Digest>,
    known: BTreeSet<String>,
}

#[derive(Default)]
struct ModuleApproval {
    approved: Option<u64>,
    active: Option<u64>,
    pending: bool,
    failed: Option<u64>,
}

impl RuntimeOwner {
    pub(crate) fn subscribe_settings(&self) -> tokio::sync::watch::Receiver<u64> {
        self.settings_changed.subscribe()
    }

    pub(crate) fn settings_projection_stale(&self) -> bool {
        // A disconnect can remove a declaration before a new version is published.
        self.projection_stale
            .load(std::sync::atomic::Ordering::Acquire)
    }

    fn settings_snapshot(
        &self,
        state: &OwnerState,
        approved: &GateSnapshot,
        mut known: BTreeSet<String>,
    ) -> Result<SettingsSnapshot> {
        let (_, body) = state.store.loaded_admission_state();
        let declarations = Self::loaded_declarations(state);
        known.extend(approved.inputs.keys().filter_map(|key| match key {
            InputKey::Entry(module) | InputKey::Declaration(module) => Some(module.clone()),
            _ => None,
        }));
        known.extend(declarations.keys().cloned());
        // The polling projection also knows configured modules without schemas.
        if !body.stamped {
            known.extend(
                self.polled_snapshot()?
                    .projection
                    .status
                    .ungated_modules
                    .iter()
                    .cloned(),
            );
        }
        let pending = known
            .iter()
            .filter_map(|module| {
                state
                    .gate
                    .pending_file_plan(module)
                    .map(|digest| (module.clone(), digest))
            })
            .collect();
        let declaration_pending = declarations
            .iter()
            .filter_map(|(module, declaration)| {
                Self::declaration_pending(state, module, declaration)
                    .map(|marker| (module.clone(), marker))
            })
            .collect();
        let fallback_applied = state
            .active_declarations
            .iter()
            .filter_map(|(module, connection)| {
                let generation = state.delivery.fallback_applied.get(connection)?;
                body.versions
                    .iter()
                    .any(|(tuple, versions)| {
                        tuple.module == *module
                            && tuple.project.is_none()
                            && versions.active == Some(*generation)
                    })
                    .then(|| module.clone())
            })
            .collect();
        Ok(SettingsSnapshot {
            declaration_pending,
            fallback_applied,
            stamped: body.stamped,
            declarations,
            pending,
            known,
        })
    }

    pub(crate) fn settings_modules(
        &self,
        known: BTreeSet<String>,
    ) -> Result<ConfigControlResponse> {
        self.observe_settings_modules(known)?
            .ok_or_else(GateError::capture_race)
    }

    fn observe_settings_modules(
        &self,
        known: BTreeSet<String>,
    ) -> Result<Option<ConfigControlResponse>> {
        Ok(self
            .observe_settings(known, false)?
            .map(|observation| observation.response))
    }

    // None means that concurrent edits prevented a consistent observation, not
    // that a settings file is invalid. Watches retain their previous rows.
    pub(crate) fn observe_settings(
        &self,
        known: BTreeSet<String>,
        watch_files: bool,
    ) -> Result<Option<SettingsObservation>> {
        self.observe_settings_rows(known, watch_files, false)
    }

    pub(crate) fn observe_settings_rows(
        &self,
        known: BTreeSet<String>,
        watch_files: bool,
        projects: bool,
    ) -> Result<Option<SettingsObservation>> {
        #[cfg(test)]
        if let Some(hook) = self.settings_projection_hook.lock().unwrap().as_mut() {
            hook()?;
        }
        let mut state = self.lock()?;
        state.check_authority_for_read()?;
        let (_, body) = state.store.loaded_admission_state();
        let approved = GateSnapshot::from_store(&body.approved)?;
        let snapshot = self.settings_snapshot(&state, &approved, known)?;
        let mut observation = None;
        for _ in 0..3 {
            let write_back = edits::loaded_write_back_status(&state)?;
            #[cfg(test)]
            if let Some(hook) = self.settings_status_hook.lock().unwrap().as_mut() {
                hook()?;
            }
            let mut files = SettingsFiles::default();
            let captured = if watch_files {
                self.capturer
                    .capture_settings_with(&snapshot.declarations, |path| files.capture(path))?
            } else {
                self.capturer.capture_settings(&snapshot.declarations)?
            };
            // External editors can still write while the daemon holds its lock.
            // Compare status on either side of capture using the same approval.
            if write_back == edits::loaded_write_back_status(&state)? {
                files.write_back_pending = !write_back.is_empty();
                observation = Some((write_back, captured, files));
                break;
            }
        }
        let Some((write_back, captured, files)) = observation else {
            return Ok(None);
        };
        let rereview = if snapshot.stamped {
            reviewed_build::pending(body, snapshot.declarations.keys())?
        } else {
            BTreeMap::new()
        };
        let mut approvals = BTreeMap::new();
        for module in &snapshot.known {
            let entry = approved_entry(body, module);
            let versions = body.versions.iter().find_map(|(tuple, versions)| {
                (tuple.module == *module && tuple.project.is_none()).then_some(versions)
            });
            approvals.insert(
                module.as_str(),
                ModuleApproval {
                    approved: entry.map(|entry| entry.generation.id),
                    active: versions.and_then(|versions| versions.active),
                    pending: versions.is_some_and(|versions| versions.pending.is_some()),
                    failed: entry
                        .filter(|entry| entry.generation.never_redeliver.contains(module))
                        .map(|entry| entry.generation.id),
                },
            );
        }
        let project_state = projects.then_some(state);
        let observed = snapshot
            .stamped
            .then(|| captured.proposal_snapshot(&approved));
        let mut modules = Vec::new();
        for module in &snapshot.known {
            let mut row = ConfigModule::default();
            row.module_id = module.clone();
            row.gated = snapshot.declarations.contains_key(module);
            let mut module_error = captured.module_errors.get(module).cloned();
            if module_error.is_none() {
                if let Some(declaration) = snapshot.declarations.get(module) {
                    match schema_with_digest(declaration) {
                        Ok((schema, digest)) => {
                            row.display_name = schema
                                .normalized()
                                .get("title")
                                .and_then(Value::as_str)
                                .map(str::to_owned);
                            row.schema_digest = Some(digest);
                        }
                        Err(error) => module_error = Some(error),
                    }
                }
            }
            let approval = &approvals[module.as_str()];
            row.approved_generation = approval.approved;
            row.active_generation = approval.active;
            if !snapshot.stamped && row.gated && module_error.is_none() {
                row.approved_generation = Some(0);
                row.active_generation = Some(0);
            }
            row.pending_plan = snapshot.pending.get(module).cloned();
            row.declaration_pending = snapshot.declaration_pending.get(module).cloned();
            row.file_error = captured.file_errors.get(module).cloned();
            let mut proposal = false;
            let mut formatting = false;
            if let Some(observed) = observed.as_ref().filter(|_| row.file_error.is_none()) {
                for key in approved.changed_keys(observed).into_iter().filter(|key| {
                    key.module() == Some(module.as_str()) && !key.is_program() && !key.is_project()
                }) {
                    if approved.inputs.get(key).map(|input| &input.value)
                        == observed.inputs.get(key).map(|input| &input.value)
                    {
                        formatting = true;
                    } else {
                        proposal = true;
                    }
                }
            }
            let behind = write_back.get(module).cloned();
            row.status = settings_status(
                row.gated,
                approval.failed.is_some(),
                rereview.contains_key(module),
                approval.pending,
                behind.is_some(),
                proposal,
                formatting,
            )
            .into();
            row.status_reason = match row.status.as_str() {
                "failed_delivery" => approval
                    .failed
                    .map(|generation| format!("Generation {generation} failed delivery.")),
                "needs_rereview" => {
                    Some("The admitted program awaits configuration review.".into())
                }
                "write_back_behind" => behind,
                _ => None,
            };
            if row.declaration_pending.is_some() && snapshot.fallback_applied.contains(module) {
                row.status = "active".into();
                row.status_reason =
                    Some("Running last approved settings; declaration awaits approval.".into());
            }
            if let Some(error) = module_error {
                row.status = error.code;
                row.status_reason = Some(error.detail);
            }
            modules.push(row);
        }
        if let Some(state) = project_state {
            self.append_project_settings_rows(
                &state,
                &snapshot,
                &rereview,
                &write_back,
                &mut modules,
            )?;
        }
        Ok(Some(SettingsObservation {
            response: ConfigControlResponse::Modules {
                modules,
                stamped: snapshot.stamped,
            },
            files,
        }))
    }

    pub(crate) fn settings_schema(
        &self,
        known: BTreeSet<String>,
        module: String,
    ) -> Result<ConfigControlResponse> {
        let mut state = self.lock()?;
        state.check_authority_for_read()?;
        let (_, body) = state.store.loaded_admission_state();
        let approved = GateSnapshot::from_store(&body.approved)?;
        let snapshot = self.settings_snapshot(&state, &approved, known)?;
        drop(state);
        let declaration = snapshot.declaration(&module)?;
        crate::config_gate::strict_json::check_canonical(&declaration.schema, "/schema")?;
        let (schema, schema_digest) = schema_with_digest(declaration)?;
        Ok(ConfigControlResponse::Schema {
            module_id: module,
            schema_digest,
            schema: schema.normalized().clone(),
        })
    }

    pub(crate) fn settings_show(
        &self,
        known: BTreeSet<String>,
        module: String,
        through: Option<ConfigThrough>,
    ) -> Result<ConfigControlResponse> {
        self.user_show_view(known, &module)?
            .response(module, None, through)
    }

    fn user_show_view(&self, known: BTreeSet<String>, module: &str) -> Result<ShowView> {
        let mut state = self.lock()?;
        state.check_authority_for_read()?;
        let (_, body) = state.store.loaded_admission_state();
        let approved = GateSnapshot::from_store(&body.approved)?;
        let snapshot = self.settings_snapshot(&state, &approved, known)?;
        let declaration = snapshot.declaration(module)?;
        let (generation, view, approved_declaration) = if snapshot.stamped {
            let entry = approved_entry(body, module).ok_or_else(|| {
                GateError::new("config_gated", "module has no approved configuration")
            })?;
            let delivery = entry.delivery.as_ref().ok_or_else(|| {
                GateError::new("config_gated", "approved delivery is unavailable")
            })?;
            let view = delivery.modules.get(module).cloned().ok_or_else(|| {
                GateError::new("config_gated", "approved module view is unavailable")
            })?;
            let declaration =
                delivery.declarations.get(module).cloned().ok_or_else(|| {
                    GateError::new("config_gated", "approved schema is unavailable")
                })?;
            let generation = crate::config_gate::module_base_generation(body, module);
            drop(state);
            (generation, view, declaration)
        } else {
            drop(state);
            let captured = self.capturer.capture_modules(
                &BTreeMap::from([(module.into(), declaration.clone())]),
                &BTreeSet::from([module.into()]),
            )?;
            let captured = captured
                .modules
                .get(module)
                .ok_or_else(|| GateError::new("config_gated", "module view is unavailable"))?;
            (0, captured.view.clone(), declaration.clone())
        };
        let file_error = if snapshot.stamped {
            self.capturer.settings_file_error(module, declaration)?
        } else {
            None
        };
        Ok(ShowView {
            declaration: approved_declaration,
            view,
            stamped: snapshot.stamped,
            base_generation: generation,
            file_error,
            pending_plan: snapshot.pending.get(module).cloned(),
            declaration_pending: snapshot.declaration_pending.get(module).cloned(),
        })
    }
}

struct ShowView {
    declaration: ConfigDeclareRequest,
    view: ConfigView,
    stamped: bool,
    base_generation: u64,
    file_error: Option<subc_control::config::ConfigFileError>,
    pending_plan: Option<Digest>,
    declaration_pending: Option<subc_protocol::config::ConfigDeclarationPending>,
}

impl ShowView {
    fn response(
        self,
        module: String,
        project_id: Option<String>,
        through: Option<ConfigThrough>,
    ) -> Result<ConfigControlResponse> {
        let defaults = schema(&self.declaration)?.defaults();
        let mut layers = Vec::new();
        if project_id.is_none() {
            layers.push(ConfigSettingsLayer::new(Layer::Default, defaults.clone()));
        }
        for layer in &self.view.layers {
            let layer_name = match layer.layer {
                subc_protocol::config::ConfigLayer::User => Layer::User,
                subc_protocol::config::ConfigLayer::Workspace => Layer::Workspace,
                subc_protocol::config::ConfigLayer::Project => Layer::Project,
                _ => {
                    return Err(GateError::new(
                        "config_gated",
                        "approved layer is unsupported",
                    ))
                }
            };
            if !included(layer_name, through) {
                continue;
            }
            if project_id.is_some() && layer.bytes.is_none() {
                continue;
            }
            let document = match &layer.bytes {
                Some(bytes) => {
                    let text = std::str::from_utf8(bytes).map_err(GateError::invalid)?;
                    let text = subc_jsonc::jsonc_to_json(text).map_err(GateError::invalid)?;
                    crate::config_gate::strict_json::parse(&text)?
                }
                None => serde_json::json!({}),
            };
            let mut document = ConfigSettingsLayer::new(layer_name, document);
            if project_id.is_none() || layer_name == Layer::Workspace {
                document.workspace_id.clone_from(&layer.workspace_id);
            }
            if project_id.is_none() || layer_name == Layer::Project {
                document.project_id.clone_from(&layer.project_id);
            }
            layers.push(document);
        }
        layers.sort_by_key(|layer| match layer.layer {
            Layer::Default => 0,
            Layer::User => 1,
            Layer::Workspace => 2,
            Layer::Project => 3,
            _ => 4,
        });
        let (effective, sources) = if through == Some(ConfigThrough::Default) {
            let mut sources = BTreeMap::new();
            default_sources(&defaults, "", &mut sources);
            (defaults, sources)
        } else {
            (self.view.effective, self.view.sources)
        };
        Ok(ConfigControlResponse::Show {
            module_id: module,
            project_id,
            through,
            stamped: self.stamped,
            base_generation: self.base_generation,
            effective,
            sources,
            layers,
            file_error: self.file_error,
            pending_plan: self.pending_plan,
            declaration_pending: self.declaration_pending,
        })
    }
}

impl SettingsSnapshot {
    fn declaration(&self, module: &str) -> Result<&ConfigDeclareRequest> {
        if !self.known.contains(module) {
            return Err(GateError::new("unknown_module", "module is unknown"));
        }
        self.declarations.get(module).ok_or_else(|| {
            GateError::new("module_ungated", "module declares no configuration schema")
        })
    }
}

fn schema(declaration: &ConfigDeclareRequest) -> Result<Schema> {
    Schema::new(declaration.schema.clone())
        .map_err(|_| GateError::new("schema_invalid", "admitted schema is invalid"))
}

fn schema_with_digest(declaration: &ConfigDeclareRequest) -> Result<(Schema, Digest)> {
    let schema = schema(declaration)?;
    let digest = schema.digest().map_err(|error| {
        crate::config_gate::strict_json::canonical_error(error, schema.normalized(), "/schema")
    })?;
    Ok((schema, digest))
}

fn approved_entry<'a>(body: &'a StoreBody, module: &str) -> Option<&'a HistoryEntry> {
    body.generations.values().rev().find(|entry| {
        !matches!(
            entry.kind,
            crate::config_store::CommitKind::Development
                | crate::config_store::CommitKind::DeclarationApproval
        ) && entry
            .tuples
            .iter()
            .any(|tuple| tuple.module == module && tuple.project.is_none())
    })
}

fn included(layer: Layer, through: Option<ConfigThrough>) -> bool {
    match through {
        Some(ConfigThrough::Default) => layer == Layer::Default,
        Some(ConfigThrough::User) => matches!(layer, Layer::Default | Layer::User),
        Some(ConfigThrough::Workspace) => layer != Layer::Project,
        None => true,
    }
}

fn default_sources(value: &Value, path: &str, sources: &mut BTreeMap<String, Layer>) {
    if let Value::Object(object) = value {
        if !object.is_empty() && !subc_config::is_vault_reference(value) {
            for (key, value) in object {
                default_sources(
                    value,
                    &format!("{path}/{}", key.replace('~', "~0").replace('/', "~1")),
                    sources,
                );
            }
            return;
        }
    }
    if !path.is_empty() {
        sources.insert(path.into(), Layer::Default);
    }
}

/// Inability to run approved settings outranks file drift; a real proposal
/// outranks a formatting-only change. Ungated modules never claim adoption.
#[allow(clippy::too_many_arguments)]
fn settings_status(
    gated: bool,
    failed: bool,
    rereview: bool,
    activation: bool,
    write_back: bool,
    proposal: bool,
    formatting: bool,
) -> &'static str {
    if !gated {
        "ungated"
    } else if failed {
        "failed_delivery"
    } else if rereview {
        "needs_rereview"
    } else if activation {
        "pending_activation"
    } else if write_back {
        "write_back_behind"
    } else if proposal {
        "pending_proposal"
    } else if formatting {
        "formatting_only"
    } else {
        "adopted"
    }
}

#[cfg(test)]
#[path = "settings_tests.rs"]
pub(crate) mod tests;