Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(cli): keep unmodeled ovcli.conf keys through config rewrites (#4590)

t0sakiddcc0052fb82
diff --git a/crates/ov_cli/src/config.rs b/crates/ov_cli/src/config.rs
index 1503e3ef6..b48fb03dd 100644
--- a/crates/ov_cli/src/config.rs
+++ b/crates/ov_cli/src/config.rs
@@ -100,6 +100,38 @@ pub struct Config {
pub oidc_token: Option<String>,
}
+/// Every top-level key `Config` models, deserialization aliases included.
+///
+/// `Config` has no catch-all, so serializing it over ovcli.conf silently drops
+/// any key it does not declare — including the `plugin` section the memory
+/// plugins read. The config writers carry the keys missing from this list over
+/// from the file they replace. `extra_header` is listed because it deserializes
+/// into `extra_headers` and is written back under the new spelling; preserving
+/// the old one would duplicate it.
+pub(crate) const KNOWN_CONFIG_KEYS: &[&str] = &[
+ "url",
+ "api_key",
+ "root_api_key",
+ "account",
+ "user",
+ "actor_peer_id",
+ "agent_id",
+ "timeout",
+ "output",
+ "echo_command",
+ "show_progress",
+ "verbose",
+ "profile",
+ "upload",
+ "extra_headers",
+ "extra_header",
+ "gateway_token",
+ "auth_mode",
+ "ldap_username",
+ "ldap_password",
+ "oidc_token",
+];
+
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct EffectiveAuth {
pub api_key: Option<String>,
@@ -416,7 +448,62 @@ pub fn get_or_create_machine_id() -> Result<String> {
mod tests {
use crate::error::Error;
- use super::{Config, merge_csv_options};
+ use super::{Config, KNOWN_CONFIG_KEYS, UploadConfig, merge_csv_options};
+
+ // A field added to `Config` but missing from KNOWN_CONFIG_KEYS would be
+ // treated as somebody else's key and restored from the file being replaced,
+ // making it impossible to clear.
+ #[test]
+ fn known_config_keys_covers_every_serialized_field() {
+ let config = Config {
+ url: "http://not-the-default".to_string(),
+ api_key: Some("k".into()),
+ root_api_key: Some("k".into()),
+ account: Some("a".into()),
+ user: Some("u".into()),
+ actor_peer_id: Some("p".into()),
+ agent_id: Some("g".into()),
+ timeout: 1.0,
+ output: "json".to_string(),
+ echo_command: false,
+ show_progress: true,
+ verbose: true,
+ profile: true,
+ upload: UploadConfig {
+ ignore_dirs: Some("d".into()),
+ include: Some("i".into()),
+ exclude: Some("e".into()),
+ },
+ extra_headers: Some(std::collections::HashMap::new()),
+ gateway_token: Some("t".into()),
+ auth_mode: Some("m".into()),
+ ldap_username: Some("n".into()),
+ ldap_password: Some("w".into()),
+ oidc_token: Some("o".into()),
+ };
+
+ let value = serde_json::to_value(&config).expect("config should serialize");
+ let serialized: Vec<&str> = value
+ .as_object()
+ .expect("config serializes to an object")
+ .keys()
+ .map(String::as_str)
+ .collect();
+
+ for key in &serialized {
+ assert!(
+ KNOWN_CONFIG_KEYS.contains(key),
+ "Config serializes `{key}` but KNOWN_CONFIG_KEYS omits it",
+ );
+ }
+ for key in KNOWN_CONFIG_KEYS {
+ // `extra_header` is a deserialization alias, never serialized.
+ assert!(
+ serialized.contains(key) || *key == "extra_header",
+ "KNOWN_CONFIG_KEYS lists `{key}` but Config never serializes it",
+ );
+ }
+ }
#[test]
fn load_required_from_path_reports_missing_cli_config() {
diff --git a/crates/ov_cli/src/config_wizard/store.rs b/crates/ov_cli/src/config_wizard/store.rs
index 5ca559a08..905d53158 100644
--- a/crates/ov_cli/src/config_wizard/store.rs
+++ b/crates/ov_cli/src/config_wizard/store.rs
@@ -9,7 +9,7 @@ use url::Url;
use crate::{
base_client::BaseClient,
- config::{Config, DEFAULT_CUSTOM_PORT, default_config_path},
+ config::{Config, DEFAULT_CUSTOM_PORT, KNOWN_CONFIG_KEYS, default_config_path},
error::{Error, Result},
};
@@ -232,7 +232,18 @@ impl ConfigStore {
}
let content = fs::read(&path)
.map_err(|e| Error::Config(format!("Failed to read config '{name}': {e}")))?;
- write_file_atomically(&self.active_path, &content)
+ // The connection fields switch wholesale, but the keys `Config` does not
+ // model are machine-level settings rather than part of the profile, so
+ // they stay with the active file — unless the incoming profile names
+ // them itself.
+ match serde_json::from_slice::<Value>(&content) {
+ Ok(mut value) if value.is_object() => {
+ carry_over_unmodeled_keys(&self.active_path, &mut value);
+ let merged = serde_json::to_string_pretty(&value)?;
+ write_file_atomically(&self.active_path, merged.as_bytes())
+ }
+ _ => write_file_atomically(&self.active_path, &content),
+ }
}
pub fn save_and_activate(&self, name: &str, config: &Config) -> Result<()> {
@@ -258,7 +269,7 @@ impl ConfigStore {
return Err(Error::Config(format!("Config '{new_name}' already exists")));
}
- self.save_named_config(new_name, config)?;
+ write_config_file_inheriting(&new_path, config, &old_path)?;
if was_active {
// Keep the active config consistent before removing the old saved file. If cleanup
// fails, the user may see both names, but the active config still points at the edit.
@@ -729,10 +740,40 @@ pub(crate) fn validation_error_copy_zh(kind: ConfigKind, error: &Error) -> Strin
}
fn write_config_file(path: &Path, config: &Config) -> Result<()> {
- let content = serde_json::to_string_pretty(config)?;
+ write_config_file_inheriting(path, config, path)
+}
+
+/// `inherit_from` is the file whose unmodeled keys survive the write: the file
+/// being replaced, except on a rename, where they live under the old name.
+fn write_config_file_inheriting(path: &Path, config: &Config, inherit_from: &Path) -> Result<()> {
+ let mut value = serde_json::to_value(config)?;
+ carry_over_unmodeled_keys(inherit_from, &mut value);
+ let content = serde_json::to_string_pretty(&value)?;
write_file_atomically(path, content.as_bytes())
}
+/// Re-attach the top-level keys `Config` does not model — above all the
+/// `plugin` section the memory plugins keep in ovcli.conf — to a freshly
+/// serialized config that is about to replace the whole file. Modeled keys are
+/// deliberately not carried over: one that is `None` was cleared on purpose,
+/// and restoring it would make clearing impossible.
+fn carry_over_unmodeled_keys(path: &Path, value: &mut Value) {
+ let Some(object) = value.as_object_mut() else {
+ return;
+ };
+ let Ok(previous) = fs::read_to_string(path) else {
+ return;
+ };
+ let Ok(Value::Object(previous)) = serde_json::from_str::<Value>(&previous) else {
+ return;
+ };
+ for (key, previous_value) in previous {
+ if !KNOWN_CONFIG_KEYS.contains(&key.as_str()) {
+ object.entry(key).or_insert(previous_value);
+ }
+ }
+}
+
fn write_file_atomically(path: &Path, content: &[u8]) -> Result<()> {
let parent = path
.parent()
@@ -803,6 +844,116 @@ mod tests {
config
}
+ fn plugin_section() -> serde_json::Value {
+ serde_json::json!({
+ "recallCompress": "auto",
+ "claude_code": { "recallCompress": "client" },
+ })
+ }
+
+ fn read_json(path: &Path) -> serde_json::Value {
+ serde_json::from_str(&fs::read_to_string(path).expect("config should exist"))
+ .expect("config should be valid JSON")
+ }
+
+ #[test]
+ fn rewriting_a_config_keeps_the_plugin_section() {
+ let dir = unique_dir("plugin-passthrough");
+ fs::create_dir_all(&dir).expect("temp dir");
+ let path = dir.join("ovcli.conf");
+ fs::write(
+ &path,
+ serde_json::json!({
+ "url": "http://old", "api_key": "old-key", "plugin": plugin_section(),
+ })
+ .to_string(),
+ )
+ .expect("seed config");
+
+ super::write_config_file(&path, &sample_config("http://new", Some("new-key"))).unwrap();
+
+ let written = read_json(&path);
+ assert_eq!(written["url"], "http://new");
+ assert_eq!(written["api_key"], "new-key");
+ assert_eq!(written["plugin"], plugin_section());
+ fs::remove_dir_all(&dir).ok();
+ }
+
+ #[test]
+ fn rewriting_a_config_still_clears_a_modeled_key() {
+ let dir = unique_dir("plugin-clear");
+ fs::create_dir_all(&dir).expect("temp dir");
+ let path = dir.join("ovcli.conf");
+ fs::write(
+ &path,
+ serde_json::json!({
+ "url": "http://local", "api_key": "old-key", "gateway_token": "old-token",
+ })
+ .to_string(),
+ )
+ .expect("seed config");
+
+ super::write_config_file(&path, &sample_config("http://local", Some("new-key"))).unwrap();
+
+ let written = read_json(&path);
+ assert_eq!(written["api_key"], "new-key");
+ assert!(
+ written.get("gateway_token").is_none(),
+ "a modeled key cleared to None must not come back from the old file",
+ );
+ fs::remove_dir_all(&dir).ok();
+ }
+
+ #[test]
+ fn renaming_a_config_carries_the_plugin_section_to_the_new_name() {
+ let dir = unique_dir("plugin-rename");
+ fs::create_dir_all(&dir).expect("temp dir");
+ let store = ConfigStore::for_config_dir(dir.clone());
+ let old_path = dir.join("ovcli.conf.before");
+ fs::write(
+ &old_path,
+ serde_json::json!({ "url": "http://local", "plugin": plugin_section() }).to_string(),
+ )
+ .expect("seed config");
+
+ store
+ .save_edited_config("before", "after", &sample_config("http://local", None))
+ .expect("rename should succeed");
+
+ assert_eq!(
+ read_json(&dir.join("ovcli.conf.after"))["plugin"],
+ plugin_section()
+ );
+ assert!(!old_path.exists(), "the old name should be removed");
+ fs::remove_dir_all(&dir).ok();
+ }
+
+ #[test]
+ fn activating_a_config_keeps_the_active_files_plugin_section() {
+ let dir = unique_dir("plugin-activate");
+ fs::create_dir_all(&dir).expect("temp dir");
+ let store = ConfigStore::for_config_dir(dir.clone());
+ fs::write(
+ dir.join("ovcli.conf"),
+ serde_json::json!({ "url": "http://old", "plugin": plugin_section() }).to_string(),
+ )
+ .expect("seed active config");
+ fs::write(
+ dir.join("ovcli.conf.other"),
+ serde_json::json!({ "url": "http://other" }).to_string(),
+ )
+ .expect("seed saved config");
+
+ store
+ .activate_config("other")
+ .expect("activate should succeed");
+
+ let active = read_json(&dir.join("ovcli.conf"));
+ assert_eq!(active["url"], "http://other");
+ assert_eq!(active["plugin"], plugin_section());
+ fs::remove_dir_all(&dir).ok();
+ }
+
#[test]
fn redacted_config_masks_gateway_and_header_secrets() {
let mut config = sample_config("http://local", Some("api-secret"));