Skip to content

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

Rust

fix(setup): let a volume GUID data dir own its logon entry

Gabriel Scharb4e85cd31e023
diff --git a/crates/ai-memory-cli/src/commands/user_service.rs b/crates/ai-memory-cli/src/commands/user_service.rs
index b8ae8b58..a5d88616 100644
--- a/crates/ai-memory-cli/src/commands/user_service.rs
+++ b/crates/ai-memory-cli/src/commands/user_service.rs
@@ -444,7 +444,18 @@ fn same_windows_dir(left: &str, right: &str) -> bool {
&& bytes[1] == b':'
&& bytes[2] == b'\\';
let unc = path.starts_with(r"\\") && !path.starts_with(r"\\?\");
- (drive_absolute || unc).then(|| path.trim_end_matches('\\').to_owned())
+ // `\\?\Volume{GUID}\…` names a volume by its GUID; it keeps the
+ // verbatim prefix and is as absolute as a drive path.
+ const VOLUME: &str = r"\\?\Volume{";
+ let volume = path
+ .get(..VOLUME.len())
+ .is_some_and(|head| head.eq_ignore_ascii_case(VOLUME))
+ && path[VOLUME.len()..]
+ .split_once('}')
+ .is_some_and(|(guid, rest)| {
+ !guid.is_empty() && (rest.is_empty() || rest.starts_with('\\'))
+ });
+ (drive_absolute || unc || volume).then(|| path.trim_end_matches('\\').to_owned())
};
match (plain(left), plain(right)) {
(Some(left), Some(right)) => left.eq_ignore_ascii_case(&right),
@@ -1459,6 +1470,41 @@ mod tests {
Removal::NotOurs(path.clone())
);
assert_eq!(drive_relative.calls.borrow().len(), 1, "only the query ran");
+
+ // A volume GUID data dir removes its own entry and leaves another
+ // volume's alone.
+ let volume_entry = " ai-memory REG_SZ \"C:\\x\\ai-memory.exe\" --data-dir \
+ \"\\\\?\\Volume{01234567-89ab-cdef-0123-456789abcdef}\\ai-memory\" \
+ --config \"C:\\c.toml\" service-host\r\n";
+ let other_volume = FakeRunner {
+ stdout: vec![("reg query", volume_entry)],
+ ..FakeRunner::default()
+ };
+ assert_eq!(
+ remove(
+ ServiceManager::WindowsRun,
+ &path,
+ Path::new(r"\\?\Volume{fedcba98-7654-3210-fedc-ba9876543210}\ai-memory"),
+ &other_volume
+ )
+ .unwrap(),
+ Removal::NotOurs(path.clone())
+ );
+ assert_eq!(other_volume.calls.borrow().len(), 1, "only the query ran");
+ let own_volume = FakeRunner {
+ stdout: vec![("reg query", volume_entry)],
+ ..FakeRunner::default()
+ };
+ assert_eq!(
+ remove(
+ ServiceManager::WindowsRun,
+ &path,
+ Path::new(r"\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\ai-memory"),
+ &own_volume
+ )
+ .unwrap(),
+ Removal::Removed(path.clone())
+ );
assert!(
ours.calls
.borrow()
@@ -1558,6 +1604,40 @@ mod windows_entry_tests {
assert!(!same_windows_dir("C:", "C:"));
assert!(!same_windows_dir(r"ai-memory", r"ai-memory"));
assert!(same_windows_dir(r"C:\", r"\\?\C:\"));
+ // A volume GUID path is absolute and matches itself, not another
+ // volume; a malformed one matches nothing.
+ let volume = r"\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\ai-memory";
+ assert!(same_windows_dir(
+ volume,
+ &format!(r"{}\", volume.to_uppercase())
+ ));
+ assert!(!same_windows_dir(
+ volume,
+ r"\\?\Volume{fedcba98-7654-3210-fedc-ba9876543210}\ai-memory"
+ ));
+ assert!(!same_windows_dir(
+ r"\\?\Volume{}\ai-memory",
+ r"\\?\Volume{}\ai-memory"
+ ));
+ assert!(!same_windows_dir(r"\\?\Volume{x", r"\\?\Volume{x"));
+ }
+
+ /// The entry `setup` renders for a volume GUID data dir is that
+ /// install's, and not another volume's.
+ #[test]
+ fn a_volume_guid_data_dir_owns_its_entry() {
+ let dir = r"\\?\Volume{01234567-89ab-cdef-0123-456789abcdef}\ai-memory";
+ let entry = format!(
+ "{} --data-dir {} --config {} {SERVICE_HOST}",
+ windows_arg("binary", r"C:\bin\ai-memory.exe").unwrap(),
+ windows_arg("data dir", dir).unwrap(),
+ windows_arg("config", r"C:\c.toml").unwrap(),
+ );
+ assert!(is_setup_run_entry_for(&entry, Path::new(dir)), "{entry}");
+ assert!(!is_setup_run_entry_for(
+ &entry,
+ Path::new(r"\\?\Volume{fedcba98-7654-3210-fedc-ba9876543210}\ai-memory")
+ ));
}
/// A drive-relative `--data-dir "C:"` names the current directory on C,
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index f21a42b1..c7c48612 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -110,7 +110,7 @@ means the boundary is not built yet.
| 15 | Agent-archive restore path confinement, resource bounds, and consistency | `ai-memory-cli/src/commands/restore_agents.rs` stages only bounded regular UTF-8 entries; requires a one-to-one manifest/body/checksum/target mapping; allowlists destinations by scope; rejects symlinked descendants; writes each file atomically and rolls back earlier writes after a later failure. `backup_agents.rs` skips direct and directory-root source symlinks, bounds individual assets, and atomically replaces a private archive only after gzip completion | `backup_agents.rs` `restore_agents_rejects_path_traversal`, `restore_agents_rejects_unauthorized_destination_path`, `restore_agents_verifies_sha256_integrity`, `restore_agents_requires_a_complete_one_to_one_manifest`, `restore_agents_rejects_oversized_entries_from_the_header`, `restore_agents_rejects_a_symlinked_destination_parent`, `backup_agents_does_not_follow_config_or_asset_root_symlinks`, and `failed_backup_leaves_the_previous_archive_intact`; `backup_and_restore_agents_round_trip` is the valid-path control; `restore_agents::tests::rollback_removes_new_files_and_restores_overwrites` covers recovery | STRONG |
| 16 | Relay queue identity, migration, and private filesystem state | `companions/ai-memory-relay/src/queue.rs` `Queue::open_inner` performs read-only identity preflight, rechecks under `IMMEDIATE`, commits schema and metadata together; `configure` checks the presence of user objects and rechecks `identity`/`schema_version` metadata on the same connection before every bounded BUSY/LOCKED retry; `fsguard.rs` checks existing paths and creates owner-only files before SQLite opens them | `tests/relay.rs` `a_foreign_database_is_refused_byte_for_byte`, `unknown_schemas_refuse_without_mutation`, `a_foreign_database_with_a_writer_is_refused_before_write_coordination`, `a_foreign_identity_after_preflight_is_refused_without_mutation`, `a_foreign_identity_during_journal_retry_is_refused_without_mutation`, `journal_retry_refuses_a_changed_version_or_empty_database_without_mutation`; controls: `journal_transition_retries_sqlite_lock_without_losing_v1_values`, `fresh_queue_init_and_v1_reopens_work_across_processes`; recovery: `interrupted_and_failed_migrations_roll_back_and_recover`, `journal_reclassification_busy_preserves_committed_migration_and_retries_same_queue`; diagnostics: `classification_read_failures_display_the_sqlite_cause_once_and_preserve_foreign_bytes`; Unix privacy: `the_queue_database_and_its_sidecars_are_owner_only` and the contended migration control | Guard-removal proof covers preflight, locked recheck, and journal retry identity/version checks; full table structure is outside this classification. Windows path rejection is preserved; Windows ACL hardening remains operator-owned |
| 17 | `ai-memory setup --web` local wizard: only the page this process printed can read the plan or apply a setup | `ai-memory-cli/src/commands/setup_web.rs` `guard` (binds `127.0.0.1` on an OS-picked port; every request needs a `Host` of exactly `127.0.0.1:<port>` or `localhost:<port>` and the one-time 256-bit path secret, compared with `subtle`; a POST also needs this page's `Origin` and the CSRF token the page reads from its own JSON; no CORS grant; `no-store`, `no-referrer`, `DENY` framing, and a CSP with a per-run nonce and no outside sources on the page) and `apply` (agent names must be `--agent` values, de-duplicated, at least one; the child is always `ai-memory setup --yes --agent ...` with the wizard's own flags, never a caller-chosen argument) | `setup_web.rs` `the_page_is_served_only_to_our_host_and_secret` (a rebinding `Host`, a bare `127.0.0.1`, another port, a wrong secret and a missing one refused; controls: `127.0.0.1` and `localhost` with the secret), `apply_refuses_cross_site_and_unknown_input_and_starts_nothing` (no `Origin`, a foreign `Origin`, no CSRF token and a wrong one each refused; `--force`, a shell-like name and an empty choice refused; no child starts), `apply_runs_setup_with_the_picked_agents_and_streams_its_output` (legitimate control: exact argument list); `tests/suite/setup_e2e.rs` `setup_web_serves_the_plan_and_applies_the_picked_agent` (the same through the binary); bite-checked by removing the `Host` check, the `Origin` check, the CSRF check and the agent allowlist one at a time | STRONG |
-| 18 | `uninstall` removes only a service `setup` wrote, including the Windows logon entry | `ai-memory-cli/src/commands/user_service.rs` `planned_removal` / `is_rendered_by_setup` (a unit file whose identity digest still matches) and `planned_removal_for` / `is_setup_run_entry_for` (a `REG_SZ` `HKCU\...\Run\ai-memory` value that is a quoted `ai-memory.exe` ending in `service-host`, with no `&` or `|` chaining, whose `--data-dir`, the first argument after the quoted executable and a whole quoted argument ending at its closing quote, is the data dir being uninstalled, both absolute (`X:\…` or UNC; a drive-relative `C:` matches nothing), compared without the verbatim `\\?\` / `\\?\UNC\` prefix, case or trailing separators) before `remove` stops anything or deletes it | `user_service.rs` `uninstall_removes_only_setups_logon_entry` (another install's entry, naming a different `--data-dir`, is left with only the query run; another program's value under the same name is left; absent value is nothing to do; control: this data dir's entry is deleted), `only_the_entry_for_this_data_dir_is_ours` (a temporary data dir does not claim the operator's entry, #1280), `a_drive_relative_data_dir_is_not_the_drive_root` (`--data-dir "C:"` does not claim `C:\`'s entry; control: the rendered drive-root entry does; `uninstall_removes_only_setups_logon_entry` drives it through `remove` and asserts nothing is stopped or deleted), `a_data_dir_that_runs_past_its_quote_is_not_ours` (`"C:\d"\other` and an escaped closing quote, which Windows reads as other directories, do not claim `C:\d`'s entry; control: the rendered entry does; `uninstall_removes_only_setups_logon_entry` drives the same case through `remove`), `the_data_dir_is_read_after_the_executable` and, on Windows, `a_rendered_entry_belongs_to_its_own_data_dir` (a ` --data-dir ` inside the executable's path neither hides nor fakes the option), `data_dirs_compare_without_prefix_case_or_trailing_separator` (verbatim UNC matches its plain spelling; another share does not), Windows-only `only_a_plain_string_value_is_read` (a `REG_EXPAND_SZ` value under the name is not read as setup's; control: the same data as `REG_SZ` is), `only_setups_entry_shape_counts_as_ours` (unquoted, other program, `serve`, `&`/`|` chaining all refused), and the unit-file identity tests in the same module | STRONG |
+| 18 | `uninstall` removes only a service `setup` wrote, including the Windows logon entry | `ai-memory-cli/src/commands/user_service.rs` `planned_removal` / `is_rendered_by_setup` (a unit file whose identity digest still matches) and `planned_removal_for` / `is_setup_run_entry_for` (a `REG_SZ` `HKCU\...\Run\ai-memory` value that is a quoted `ai-memory.exe` ending in `service-host`, with no `&` or `|` chaining, whose `--data-dir`, the first argument after the quoted executable and a whole quoted argument ending at its closing quote, is the data dir being uninstalled, both absolute (`X:\…`, UNC or `\\?\Volume{GUID}\…`; a drive-relative `C:` matches nothing), compared without the verbatim `\\?\` / `\\?\UNC\` prefix, case or trailing separators) before `remove` stops anything or deletes it | `user_service.rs` `uninstall_removes_only_setups_logon_entry` (another install's entry, naming a different `--data-dir`, is left with only the query run; another program's value under the same name is left; absent value is nothing to do; control: this data dir's entry is deleted), `only_the_entry_for_this_data_dir_is_ours` (a temporary data dir does not claim the operator's entry, #1280), `a_volume_guid_data_dir_owns_its_entry` (a volume GUID data dir owns its rendered entry and not another volume's; `uninstall_removes_only_setups_logon_entry` drives both through `remove`), `a_drive_relative_data_dir_is_not_the_drive_root` (`--data-dir "C:"` does not claim `C:\`'s entry; control: the rendered drive-root entry does; `uninstall_removes_only_setups_logon_entry` drives it through `remove` and asserts nothing is stopped or deleted), `a_data_dir_that_runs_past_its_quote_is_not_ours` (`"C:\d"\other` and an escaped closing quote, which Windows reads as other directories, do not claim `C:\d`'s entry; control: the rendered entry does; `uninstall_removes_only_setups_logon_entry` drives the same case through `remove`), `the_data_dir_is_read_after_the_executable` and, on Windows, `a_rendered_entry_belongs_to_its_own_data_dir` (a ` --data-dir ` inside the executable's path neither hides nor fakes the option), `data_dirs_compare_without_prefix_case_or_trailing_separator` (verbatim UNC matches its plain spelling; another share does not), Windows-only `only_a_plain_string_value_is_read` (a `REG_EXPAND_SZ` value under the name is not read as setup's; control: the same data as `REG_SZ` is), `only_setups_entry_shape_counts_as_ours` (unquoted, other program, `serve`, `&`/`|` chaining all refused), and the unit-file identity tests in the same module | STRONG |
## Keeping this current (the standing protocol)