Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(config): keep explicit default server_url, skip port 0, and let installers use the derived URL

AkitaOnRails306d8ed3d8f7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 039a8b0a..2e5d0813 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,11 +23,12 @@ version in [`docs/changelog/`](docs/changelog/):
derived from `bind`: an unspecified address maps to loopback (`0.0.0.0` to
`127.0.0.1`, `[::]` to `[::1]`) and any other host is used as given,
hostname included. An explicit value in the file or the environment still
- wins, a `bind` that is not a `host:port` leaves the default URL alone, and
- the default `bind` resolves to the same URL as before, so an install that
- never changed either key is unaffected. A derived URL is not reported as an
- operator-configured one, so `install-hooks` and `install-mcp` behave as
- they always have for someone who only edited `bind`. (#1269)
+ wins, even when it names the default URL; a `bind` that is not a
+ `host:port`, or one on port 0, leaves the default URL alone; and the default
+ `bind` resolves to the same URL as before, so an install that never changed
+ either key is unaffected. `install-hooks` and `install-mcp` still prefer an
+ existing MCP entry's URL, and otherwise write the derived URL instead of the
+ default port. (#1269)
- Fixed Windows capture dropping the body of file and shell tool events
the client had kept. With `[capture] ignore_paths` active, server
re-inspection compiled a POSIX never-match sentinel and treated the
diff --git a/crates/ai-memory-cli/src/commands/install_hooks.rs b/crates/ai-memory-cli/src/commands/install_hooks.rs
index bdce0a89..084a7828 100644
--- a/crates/ai-memory-cli/src/commands/install_hooks.rs
+++ b/crates/ai-memory-cli/src/commands/install_hooks.rs
@@ -1506,6 +1506,10 @@ fn effective_hook_server_url(
config.server_url.clone()
} else if let Some(url) = inferred.and_then(|mcp| mcp.hook_server_url.clone()) {
url
+ } else if config.server_url_derived_from_bind {
+ // Nothing chose a URL and no existing entry names one: reach the
+ // server this config's `bind` starts (#1269).
+ config.server_url.clone()
} else {
return DEFAULT_SERVER_URL.to_string();
};
@@ -8954,6 +8958,35 @@ command = "AI_MEMORY_HOOK_URL=http://h AI_MEMORY_PROJECT_STRATEGY=repo-root /x/a
);
}
+ /// #1269: with only a custom `bind`, hooks reach the server that bind
+ /// starts; an existing MCP entry still wins over the derived URL.
+ #[test]
+ fn hook_server_url_uses_a_bind_derived_url_after_an_existing_entry() {
+ let config = Config {
+ server_url: "http://127.0.0.1:49921".into(),
+ server_url_derived_from_bind: true,
+ ..Config::default()
+ };
+ let args = default_hook_args();
+ assert_eq!(
+ effective_hook_server_url(&config, &args, None),
+ "http://127.0.0.1:49921"
+ );
+ let inferred = InferredMcpConfig {
+ hook_server_url: Some("http://homelab:49374".into()),
+ auth_token: None,
+ };
+ assert_eq!(
+ effective_hook_server_url(&config, &args, Some(&inferred)),
+ "http://homelab:49374"
+ );
+ assert_eq!(
+ effective_hook_server_url(&Config::default(), &args, None),
+ DEFAULT_SERVER_URL,
+ "nothing derived and nothing inferred keeps the default"
+ );
+ }
+
#[test]
fn resolve_hooks_dir_uses_grok_bundle_for_grok() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/ai-memory-cli/src/commands/install_mcp.rs b/crates/ai-memory-cli/src/commands/install_mcp.rs
index 34d12b54..2b80ba3b 100644
--- a/crates/ai-memory-cli/src/commands/install_mcp.rs
+++ b/crates/ai-memory-cli/src/commands/install_mcp.rs
@@ -99,7 +99,7 @@ pub(crate) fn run_with_opencode_dialect(
effective_mcp_server_url(config, &args)
} else {
inferred.as_ref().map_or_else(
- || DEFAULT_MCP_URL.to_string(),
+ || effective_mcp_server_url(config, &args),
|entry| entry.mcp_url.clone(),
)
};
@@ -393,7 +393,10 @@ fn effective_mcp_server_url(config: &Config, args: &InstallMcpArgs) -> String {
// so callers who already pass the endpoint are unchanged.
return mcp_server_url_from_base(url);
}
- if config.server_url_configured() {
+ // A URL derived from `bind` counts here too: with nothing configured and no
+ // existing entry to infer from, the client should reach the server this
+ // config starts (#1269).
+ if config.server_url_configured() || config.server_url_derived_from_bind {
return mcp_server_url_from_base(&config.server_url);
}
DEFAULT_MCP_URL.to_string()
@@ -4210,6 +4213,27 @@ mod tests {
);
}
+ /// #1269: with only a custom `bind`, the MCP entry points at the server
+ /// that bind starts rather than the compiled default; a plain default
+ /// config is unchanged.
+ #[test]
+ fn mcp_server_url_uses_a_bind_derived_url() {
+ let config = Config {
+ server_url: "http://127.0.0.1:49921".into(),
+ server_url_derived_from_bind: true,
+ ..Config::default()
+ };
+ let args = args_for(McpClient::OpenCode);
+ assert_eq!(
+ effective_mcp_server_url(&config, &args),
+ "http://127.0.0.1:49921/mcp"
+ );
+ assert_eq!(
+ effective_mcp_server_url(&Config::default(), &args),
+ DEFAULT_MCP_URL
+ );
+ }
+
/// Specific shape checks — each client has a distinguishing key
/// in its JSON snippet. This catches accidental cross-pollination
/// between renderers (e.g. Gemini's `httpUrl` showing up under
diff --git a/crates/ai-memory-cli/src/config.rs b/crates/ai-memory-cli/src/config.rs
index 7008259e..32795dc6 100644
--- a/crates/ai-memory-cli/src/config.rs
+++ b/crates/ai-memory-cli/src/config.rs
@@ -2091,9 +2091,7 @@ impl FtsSettings {
/// (#1269).
///
/// Only runs when neither the config file nor `AI_MEMORY_SERVER_URL` set
-/// `server_url`; `file_set_server_url` carries the file half, and an env value
-/// would already have replaced `config.server_url` with something other than
-/// the default, which this checks again.
+/// `server_url` (`explicit_server_url`), even to the default URL itself.
///
/// Rules, matching the reporter's proposal:
/// - an unspecified address becomes loopback, because it is not itself a
@@ -2106,8 +2104,8 @@ impl FtsSettings {
///
/// The default `bind` derives exactly [`DEFAULT_SERVER_URL`], so an install
/// that never touched either key is byte-identical to before.
-fn derive_server_url_from_bind(config: &mut Config, file_set_server_url: bool) {
- if file_set_server_url || config.server_url != DEFAULT_SERVER_URL {
+fn derive_server_url_from_bind(config: &mut Config, explicit_server_url: bool) {
+ if explicit_server_url || config.server_url != DEFAULT_SERVER_URL {
return;
}
// A bind that differs from the default is the case this exists for; the
@@ -2135,7 +2133,9 @@ fn server_url_for_bind(bind: &str) -> Option<String> {
// colons of its own, keeps its host in one piece.
let (host, port) = bind.rsplit_once(':')?;
let port: u16 = port.parse().ok()?;
- if host.is_empty() {
+ // Port 0 asks the OS for any free port, so no client can know it in
+ // advance; keep the default URL.
+ if host.is_empty() || port == 0 {
return None;
}
let host = if host == "0.0.0.0" {
@@ -2262,11 +2262,16 @@ impl Config {
if let Some(token) = runtime_env.auth_token.clone() {
config.auth.bearer_token = Some(token);
}
- // Whether the CONFIG FILE named a server URL, sampled before the env
- // override below so the two sources stay distinguishable. `server_url`
+ // Whether the file or the environment named a server URL. `server_url`
// is the one key #1269 derives rather than only defaulting, and the
- // derivation must not fire when either source set it explicitly.
- let file_set_server_url = config.server_url != DEFAULT_SERVER_URL;
+ // derivation must not fire when either source set it, even to the
+ // default URL itself, so the file is asked for the key rather than
+ // the loaded value being compared against the default.
+ let explicit_server_url = runtime_env.server_url.is_some()
+ || (resolved_config_path.exists()
+ && Figment::from(Toml::file(&resolved_config_path))
+ .find_value("server_url")
+ .is_ok());
if let Some(server_url) = runtime_env.server_url.clone() {
config.server_url = server_url;
}
@@ -2321,7 +2326,7 @@ impl Config {
// (nothing on the default port) or, worse, operated on whatever
// server WAS on the default port -- which is how the reporter wrote
// test pages into their real store.
- derive_server_url_from_bind(&mut config, file_set_server_url);
+ derive_server_url_from_bind(&mut config, explicit_server_url);
apply_bearer_token_file(
&mut config.auth,
&TokenFileContext {
@@ -3719,6 +3724,46 @@ mod tests {
assert_eq!(cfg.server_url, "https://from-env.example");
}
+ /// An explicit `server_url` that happens to name the default URL is still
+ /// explicit: neither the file nor the environment may be overridden by a
+ /// custom `bind` just because the value matches the default.
+ #[test]
+ fn an_explicit_default_server_url_is_not_rederived_from_bind() {
+ let cfg = load_with_toml(&format!(
+ "bind = \"127.0.0.1:49921\"\nserver_url = \"{DEFAULT_SERVER_URL}\"\n"
+ ))
+ .unwrap();
+ assert_eq!(cfg.server_url, DEFAULT_SERVER_URL);
+ assert!(!cfg.server_url_derived_from_bind);
+
+ let tmp = TempDir::new().unwrap();
+ let config_path = tmp.path().join("config.toml");
+ std::fs::write(&config_path, "bind = \"127.0.0.1:49921\"\n").unwrap();
+ let cfg = Config::load_with_runtime_env(
+ Some(&config_path),
+ Some(tmp.path().to_path_buf()),
+ RuntimeEnv {
+ server_url: Some(DEFAULT_SERVER_URL.into()),
+ ..RuntimeEnv::default()
+ },
+ )
+ .unwrap();
+ assert_eq!(cfg.server_url, DEFAULT_SERVER_URL);
+ assert!(!cfg.server_url_derived_from_bind);
+ assert!(cfg.server_url_configured());
+ }
+
+ /// Port 0 picks a free port at startup, so no client URL can be derived
+ /// from it; the default URL stays.
+ #[test]
+ fn a_port_zero_bind_keeps_the_default_server_url() {
+ for bind in ["127.0.0.1:0", "0.0.0.0:0", "[::1]:0"] {
+ let cfg = load_with_toml(&format!("bind = \"{bind}\"\n")).unwrap();
+ assert_eq!(cfg.server_url, DEFAULT_SERVER_URL, "{bind}");
+ assert!(!cfg.server_url_derived_from_bind, "{bind}");
+ }
+ }
+
/// #1269: deriving the URL must not make an operator look like they
/// configured it.
///