Skip to main content

agentmux_srv_core/registry/
unreadable.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Records this build cannot fully read, said instead of skipped.
5//!
6//! `DefinitionStore::list_active` and `Registry::list_active` leave a file out when its `schema_version` is outside what this
7//! build supports (a newer AgentMux wrote it), when it is not JSON, or when it fails validation. The file stays on disk and a
8//! later build reads it back, but until then the agent it describes is simply not there. [`explain`] reads such a file as far as
9//! it goes and says why in one sentence, so every surface that lists agents can show the agent and its reason, and a start can
10//! be refused with it. Nothing here writes: a record that cannot be read is never rewritten (`merge_for_write` refuses a
11//! downgrade), so its unknown fields stay exactly as the newer build wrote them.
12
13use serde::Serialize;
14use serde_json::Value;
15
16/// One record that is on disk and was not loaded.
17#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
18pub struct UnreadableRecord {
19    /// The file's name without `.json`: the agent's id for a definition, the instance id for a registry record.
20    pub id: String,
21    /// What the record calls itself, when that much could be read; else empty.
22    pub name: String,
23    /// The harness it names, when that much could be read; else empty.
24    pub provider: String,
25    /// The record format it declares, when it declares one.
26    pub schema_version: Option<u64>,
27    /// Why it is not loaded, in a sentence a person can read.
28    pub reason: String,
29}
30
31/// Where a record keeps what [`explain`] reads: the object holding the fields, and their names there.
32pub(super) struct Shape {
33    pub name_key: &'static str,
34    pub provider_key: Option<&'static str>,
35    /// The record's own note of the AgentMux version that wrote it (instance records have one, definitions do not).
36    pub version_keys: &'static [&'static str],
37    pub min: u32,
38    pub max: u32,
39}
40
41/// Reads the bytes of a record that did not load, as far as they go. `why_not` is the loader's own error text, used only when
42/// the file is well formed and in a supported format (so it is the record's content that is wrong).
43pub(super) fn explain(bytes: &[u8], stem: &str, shape: &Shape, why_not: &str) -> UnreadableRecord {
44    let mut out = UnreadableRecord { id: stem.to_string(), name: String::new(), provider: String::new(), schema_version: None, reason: String::new() };
45    let value: Value = match serde_json::from_slice(bytes) {
46        Ok(v) => v,
47        Err(e) => {
48            out.reason = format!("its record file is damaged and could not be read ({e}); it is left as it is");
49            return out;
50        }
51    };
52    let data = value.get("data");
53    let text = |key: &str| data.and_then(|d| d.get(key)).and_then(Value::as_str).unwrap_or_default().to_string();
54    out.name = text(shape.name_key);
55    if let Some(key) = shape.provider_key {
56        out.provider = text(key);
57    }
58    out.schema_version = value.get("schema_version").and_then(Value::as_u64);
59    out.reason = match out.schema_version {
60        Some(v) if v > u64::from(shape.max) => {
61            let by = shape.version_keys.iter().map(|k| text(k)).find(|s| !s.is_empty());
62            match by {
63                Some(version) => format!("made by a newer AgentMux, version {version} (record format {v}; this version reads up to {})", shape.max),
64                None => format!("made by a newer AgentMux (record format {v}; this version reads up to {})", shape.max),
65            }
66        }
67        Some(v) if v < u64::from(shape.min) => format!("made by an older AgentMux whose record format ({v}) this version no longer reads"),
68        Some(_) => format!("its record is incomplete or does not make sense to this version ({why_not})"),
69        None => "its record says nothing of its format, so this version does not touch it".to_string(),
70    };
71    out
72}
73
74#[cfg(test)]
75#[path = "unreadable_tests.rs"]
76mod tests;