Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(importer): refuse resumes over edited destination bodies
Samir Hanna Verzab4a4d83fedbe
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 75ce823b..3d74163a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Fixed agentmemory imports resuming over destination body edits that retained+ the original attribution footer. A resume now requires the complete body to+ match a planned version before using its page id for the next conditional+ write; identical imports still resume without duplicate versions.- Restored affirmative profile preferences about tools, browsers and MCP.The capability-ban filter also discarded statements such as "Always usetools to verify facts"; it now requires negative or disabling wording.diff --git a/companions/ai-memory-importer/README.md b/companions/ai-memory-importer/README.mdindex ea8fe0d4..2e3eb91b 100644--- a/companions/ai-memory-importer/README.md+++ b/companions/ai-memory-importer/README.md@@ -204,8 +204,9 @@ The agentmemory routes keep the same dry-run default, `--apply` plus`--manifest-out`, auth and path rules, with these differences:- Writes use MCP `memory_write_page` only, each conditional, so the server- refuses a page that appears or changes during the import. A destination page this import did not write is never- replaced; the run stops and names it.+ refuses a page that appears or changes after the version read. A resume+ accepts an existing page only when its complete body matches a planned+ version; a different body stops the run and names the page.- `--dest-prefix` must be relative and portable. `..`, absolute paths, aWindows drive, and a first segment that is reserved (`sessions`,`session-logs`, `procedures`, `decisions`, `gotchas`, `profile`) or starts@@ -218,10 +219,15 @@ The agentmemory routes keep the same dry-run default, `--apply` plusthe manifest or any request exists. Record ids that look like credentialsare refused.- Each record has a stable ingest key, a UUIDv5 of its agentmemory id, written- on the page's last line. A rerun finds the key in the page the server- already holds and skips it, so an interrupted import resumes without- duplicates and a finished one writes nothing. The manifest records each- version as `written`, `present` (already there) or `failed`.+ on the page's last line. A rerun compares the entire sanitized planned body,+ including that line, with the page the server already holds before skipping+ it. Editing the body while keeping its attribution footer therefore cannot+ authorize a later imported version to replace the edit. Server-side+ sanitation that changes the body also stops automatic resumption. This+ comparison covers the body, not frontmatter-only edits; a later imported+ version still applies its planned metadata. An identical interrupted import+ resumes without duplicates and a finished one writes nothing. The manifest+ records each version as `written`, `present` (already there) or `failed`.- Without `--create-destination`, every destination must already exist; thecheck is a read, which never creates a scope.diff --git a/companions/ai-memory-importer/src/agentmemory/e2e.rs b/companions/ai-memory-importer/src/agentmemory/e2e.rsindex b1c5a9f2..8376c91d 100644--- a/companions/ai-memory-importer/src/agentmemory/e2e.rs+++ b/companions/ai-memory-importer/src/agentmemory/e2e.rs@@ -254,4 +254,36 @@ async fn e2e_export_import_rebuilds_chains_and_honours_ttl() {let second = manifest(&fixture.manifest);assert_eq!(second["written_versions"], 0, "a rerun writes 0");assert_eq!(second["present_versions"], 14);++ // The destination can evolve independently between imports. Keeping the+ // attribution footer does not mean the current body is still ours.+ let edited_body = format!(+ "Keep this destination correction.\n\n{}",+ latest["body"].as_str().unwrap()+ );+ let edited = tool(+ &server,+ "memory_write_page",+ json!({"workspace": "acme", "project": "acme-api",+ "path": "imported/agentmemory/memories/mem-q1.md",+ "body": edited_body, "expected_page_id": latest["page_id"]}),+ )+ .await;+ data["memories"].as_array_mut().unwrap().push(json!({+ "id": "mem_q4", "version": 4, "parentId": "mem_q3",+ "supersedes": ["mem_q3"], "project": "api",+ "title": "A new source version", "content": "Do not replace the destination edit."+ }));+ std::fs::write(&fixture.file, serde_json::to_vec(&data).unwrap()).unwrap();+ assert!(export::run(args()).await.is_err());+ let current = tool(+ &server,+ "memory_read_page",+ json!({"workspace": "acme", "project": "acme-api",+ "path": "imported/agentmemory/memories/mem-q1.md"}),+ )+ .await;+ assert_eq!(current["page_id"], edited["page_id"]);+ assert_eq!(current["body"], edited_body);+ assert_eq!(manifest(&fixture.manifest)["status"], "failed");}diff --git a/companions/ai-memory-importer/src/agentmemory/export/tests.rs b/companions/ai-memory-importer/src/agentmemory/export/tests.rsindex 25340a7a..d0a30880 100644--- a/companions/ai-memory-importer/src/agentmemory/export/tests.rs+++ b/companions/ai-memory-importer/src/agentmemory/export/tests.rs@@ -438,6 +438,54 @@ async fn resume_after_an_injected_failure_writes_no_duplicates() {}}+#[tokio::test]+async fn resume_refuses_an_edited_body_even_with_the_original_ingest_key() {+ let fixture = fixture_files(+ &json!({"memories": [+ {"id": "mem_q1", "project": "api", "version": 1,+ "title": "First policy", "content": "Initial source body."},+ {"id": "mem_q2", "project": "api", "version": 2, "parentId": "mem_q1",+ "title": "Second policy", "content": "New source body."}+ ]}),+ &fixture_mapping(),+ );+ let server = MockServer::start(&[]);+ server.state().failure = Some(Failure::Before(2));+ assert!(run(args(&fixture, &server.url)).await.is_err());++ // A normal destination edit preserves the import's attribution footer.+ // Its new page_id must not become the precondition for importing v2.+ let edited_body = {+ let mut state = server.state();+ state.failure = None;+ let chain = state+ .pages+ .get_mut(&("acme".into(), "acme-api".into(), CHAIN.into()))+ .unwrap();+ let mut edited = chain.last().unwrap().clone();+ edited.page_id = "destination-edit".into();+ edited+ .body+ .insert_str(0, "Keep this destination correction.\n\n");+ let body = edited.body.clone();+ chain.push(edited);+ body+ };++ let error = run(args(&fixture, &server.url)).await.unwrap_err();+ assert!(error.to_string().contains("did not write"), "{error:#}");+ let state = server.state();+ let chain = state.chain("acme", "acme-api", CHAIN);+ assert_eq!(chain.len(), 2, "no source version replaces the edit");+ assert_eq!(chain[1].page_id, "destination-edit");+ assert_eq!(chain[1].body, edited_body);+ assert_eq!(state.stored_versions(), 2, "the run stops at the conflict");+ let manifest = read_manifest(&fixture.manifest);+ assert_eq!(manifest["status"], "failed");+ assert_eq!(manifest["written_versions"], 0);+ assert_eq!(manifest["present_versions"], 0);+}+#[tokio::test]async fn observations_replay_as_ordered_external_import_sessions_when_asked() {let fixture = fixture_files(&fixture_export(), &fixture_mapping());diff --git a/companions/ai-memory-importer/src/agentmemory/mod.rs b/companions/ai-memory-importer/src/agentmemory/mod.rsindex 425c84f4..22fa2b06 100644--- a/companions/ai-memory-importer/src/agentmemory/mod.rs+++ b/companions/ai-memory-importer/src/agentmemory/mod.rs@@ -8,9 +8,10 @@//! `imported/agentmemory`).//!//! Each page version ends with a provenance line naming its stable ingest key-//! (a UUIDv5 of the agentmemory record id). A rerun finds the key in the page-//! the server already holds and skips what is there, so an interrupted import-//! resumes without duplicates and a finished one writes nothing.+//! (a UUIDv5 of the agentmemory record id). A rerun matches the complete body+//! against its planned versions before skipping what is there, so an+//! interrupted import resumes without duplicates and a finished one writes+//! nothing. A destination edit retaining the key is still a conflict.pub(crate) mod export;pub(crate) mod vault;@@ -66,8 +67,8 @@ pub(crate) fn ingest_key(record: &str, id: &str) -> String {Uuid::new_v5(&key_namespace(), format!("{record}:{id}").as_bytes()).to_string()}-/// The line that ends every imported page version. Its key is what a rerun-/// looks for in the page the server already holds.+/// The attribution line that ends every imported page version. A rerun+/// compares the complete body, including this line, before skipping a version.pub(crate) fn provenance_line(record: &str, id: &str, version: Option<u64>, key: &str) -> String {let version = version.map_or_else(String::new, |v| format!(", version {v}"));format!(@@ -516,8 +517,9 @@ async fn apply_page(index += 1;}Err(error) if error.downcast_ref::<PreconditionFailed>().is_some() => {- // The page exists or moved on. Resume only from a version- // this import wrote; anything else is somebody else's page.+ // The page exists or moved on. The attribution key can survive+ // a destination edit, so only an identical planned body can+ // supply the version id for the next conditional write.let current = client.mcp_read_page(&page.destination.workspace,@@ -529,7 +531,7 @@ async fn apply_page(let Some(found) = page.versions.iter()- .rposition(|v| current.body.contains(&key_marker(&v.key)))+ .rposition(|v| current.body == v.body).filter(|found| *found >= index)else {let entry = &mut manifest.pages[page_index].versions[index];diff --git a/docs/companion-crates.md b/docs/companion-crates.mdindex 3da6856c..c2a03697 100644--- a/docs/companion-crates.md+++ b/docs/companion-crates.md@@ -138,7 +138,9 @@ Initial source support is intentionally narrow:scope, every write conditional (`create_only`, then `expected_page_id` perlater version), refuse servers older than 3.0, and land under`imported/agentmemory/`, outside every reserved family. Each page version- carries a stable UUIDv5 ingest key, so a rerun skips what is already there.+ carries a stable UUIDv5 ingest key; a rerun skips an existing version only+ when its complete body matches the sanitized plan. A destination body edit+ retaining the key stops resumption before another version replaces it.Observations are dropped unless replayed as `external-import` sessionsthrough `/hook/batch`; records with no ai-memory counterpart are counted inthe manifest. Details are in the importer README.diff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex b02f3ae4..04c25956 100644--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@ -69,7 +69,7 @@ the legitimate restricted-project control and the failed-query cases.| 5b | Page supersession (loser stays reachable) | `ai-memory-store/src/ops.rs` `upsert_page_in_tx` — demote `is_latest=0` (never delete) + `supersedes` chain; read side: `reader.rs` `page_versions` lists every row at the path, with no `is_latest` filter, for `/web/w/…/h/*path` and `/api/v1/…/history/*path`. It does apply TTL expiry (`reader.rs` `history_visible`): a version whose own `expires_at` passed is neither listed nor readable by id, a path whose live version expired shows no history at all (the forget sweep hard-deletes every row there), and a `supersedes` link to a hidden version reads as `null`. Expiry is the only thing that hides a superseded version; a concurrent write never does | `multi_session.rs` `concurrent_writes_to_one_path_supersede_rather_than_destroy`; `retrieval_superseded.rs`; `multi_session.rs` `page_history_keeps_the_overwritten_version_and_stays_in_scope`; `ai-memory-web` `history_replay.rs` `history_keeps_both_sides_of_a_concurrent_divergent_write` (two attributed writers on one base: all three versions and both authors listed, one `is_latest`, the two writers' versions diff by id) and `history_lists_k_plus_one_versions_newest_first_and_diffs_the_latest`; bite: an `is_latest = 1` filter in `page_versions` fails both; `history_hides_expired_versions_and_expired_pages` (an expired superseded version and every version of a page whose live version expired answer 404 by path and by id on HTML and JSON, and the live successor's `supersedes` is `null`; control: a TTL in the future is listed and diffable; bites with the per-row predicate removed, with the live-version clause disabled, and with the `supersedes` hiding removed) | STRONG || 5c | Watcher reindex and Wiki writes share per-page serialization (invariant #16) | `ai-memory-wiki/src/wiki.rs` `reindex_page` takes `mutation_lock.read` before `reindex_page_locked` takes `lock_page`, covering disk read through the single-writer upsert; callers already holding the exclusive global guard do not reacquire it. `write_page` uses the same page mutex and `apply_batch` retains sorted, deduplicated locks; embedding runs after both guards are released | `wiki.rs` `reindex_page_lock_waits_before_reading_and_allows_another_path` (transient snapshot refused while locked, another path progresses, release indexes the committed bytes without rewriting disk), `reindex_page_lock_serializes_write_through_the_upsert` and `reindex_page_lock_and_reversed_duplicate_batches_do_not_deadlock` (real concurrent operations, final disk/SQL winner and supersession ancestry preserved); all three fail with the reindex page guard removed. `reindex_page_lock_releases_both_guards_before_embedding` checks both body and abstract embedding; `decay_cleanup_reindex_does_not_embed_under_the_exclusive_lock` covers the exclusive caller | STRONG for a shared Wiki handle and its clones. External editors and independently constructed Wiki handles do not participate in these locks || 5d | Wiki project-tree filesystem confinement | `ai-memory-wiki/src/confinement.rs` checks the wiki root, workspace/project roots, every existing ancestor, the final component, and the in-root ordinary `.git` directory with link-aware metadata; repository workdir/gitdir/commondir are containment-checked after opening, and write parents are created one component at a time and rechecked. `Wiki` checks before admission and again under mutation/page locks, while `GitAdapter` rechecks immediately before writes, appends, renames, and removals. Direct reindex, checkpoint restore, manifests, auto-improvement artifacts, ledger append, startup repair, purge/session moves, rollback, migrations, and git tree staging use the same boundary. Confinement is a distinct `WikiError` and therefore cannot trigger missing-file DB fallback | `wiki.rs` `page_reads_refuse_leaf_and_ancestor_symlinks_without_fallback_classification`, `ai-memory-mcp` `admin_read_page.rs` `read_page_does_not_db_fallback_through_a_symlink`, `page_mutations_refuse_ancestor_symlink_and_leave_outside_and_store_unchanged`, `confinement_refusal_happens_before_blocking_and_observer_admission`, `restore_and_reindex_refuse_ancestor_symlink_without_touching_canary_or_db`; `git.rs` `git_metadata_link_is_refused_without_touching_external_repository`, `gitfile_redirect_outside_is_refused_without_touching_target`, `git_metadata_redirect_files_are_refused_before_repository_open`, `configured_worktree_outside_the_wiki_root_is_refused`, `linked_git_metadata_never_reaches_commit_fallback`, `gitfile_metadata_never_reaches_commit_fallback`, `out_of_root_worktree_never_reaches_commit_fallback`, `fallback_preflight_refuses_invalid_git_metadata`, `windows_commit_fallback_accepts_established_eligible_errors`, `eligible_windows_open_error_dispatches_the_fallback_after_validation`, `windows_init_error_dispatches_fallback_after_root_preflight`, `invalid_root_never_reaches_init_fallback`, `checkpoint_and_revision_reads_refuse_a_replaced_git_directory`, and ordinary-repository/API controls `init_is_idempotent_and_creates_dotgit`; `linked_wiki_root_is_refused_during_construction`, `linked_or_dangling_scope_roots_are_refused_but_real_roots_work`, existing `decay_cleanup_refuses_to_reindex_a_symlinked_recreation` and `reindex_rejects_symlinked_scope_manifest`; `ai-memory-mcp` `admin_move.rs` `true_move_refuses_an_invalid_destination_namespace_without_db_changes` and `delete_workspace_refuses_invalid_namespace_before_db_or_notification`; `admin_backup.rs` `backup_refuses_redirected_git_metadata_before_returning_an_archive`, `backup_refuses_wiki_leaf_links_before_returning_an_archive`, `backup_refuses_linked_wiki_directories_before_returning_an_archive`, and ordinary archive controls; `watcher.rs` `direct_file_event_skips_symlink`, `walk_markdown_skips_linked_directories_and_their_ledgers`, and `shared_link_like_helper_accepts_regular_files_and_directories` (Windows directory-link coverage exercises reparse metadata when privilege is available); `backup.rs` `a_pre_migration_backup_refuses_a_linked_wiki_tree_without_an_archive`; `confinement.rs` metadata-helper controls run on every platform | STRONG for imported/static links and same-process mutations. Component checks do not claim descriptor-relative protection against a concurrently malicious same-user process; Windows reparse behavior requires the `windows` PR label/full CI |-| 5e | Conditional page mutation (#986, invariant #16) | `ai-memory-store/src/ops.rs` `upsert_page_if` reads the latest version and writes in one transaction, the authority; `ai-memory-wiki/src/wiki.rs` `write_page_if` pre-checks under the per-page lock before touching disk and restores the file when the store check fails; conditional deletes reuse `delete_page_if_latest` (exclusive mutation lock plus an in-transaction check). `ai-memory-mcp/src/server.rs` checks the precondition only after scope resolution and the write grant, against the resolved scope's own latest row, and returns `precondition_failed` with no write, checkpoint or webhook dispatch | `multi_session.rs` `conditional_writers_on_one_base_exactly_one_wins_and_nothing_is_destroyed` (two writers from one version: one wins, the base stays reachable, no loser row; fails with the store check removed); `server.rs` `conditional_page_writes::*` (stale id, `create_only` on an existing page, a version id from another project, and conditional deletes are refused and change nothing, with and without the wiki pre-check; the store check proven to bite with the reader pre-check off); `a_reader_may_read_everything_and_change_nothing` (a read-only user's conditional write or delete, with the right id or a wrong one, gets the authorization error and no version data; the page is unchanged) | STRONG |+| 5e | Conditional page mutation (#986, invariant #16) | `ai-memory-store/src/ops.rs` `upsert_page_if` reads the latest version and writes in one transaction, the authority; `ai-memory-wiki/src/wiki.rs` `write_page_if` pre-checks under the per-page lock before touching disk and restores the file when the store check fails; conditional deletes reuse `delete_page_if_latest` (exclusive mutation lock plus an in-transaction check). `ai-memory-mcp/src/server.rs` checks the precondition only after scope resolution and the write grant, against the resolved scope's own latest row, and returns `precondition_failed` with no write, checkpoint or webhook dispatch. The agentmemory importer (`agentmemory/mod.rs` `apply_page`) resumes from a conflicting destination only when its complete body equals a planned version; a retained attribution key alone cannot supply the next write's expected version | `multi_session.rs` `conditional_writers_on_one_base_exactly_one_wins_and_nothing_is_destroyed` (two writers from one version: one wins, the base stays reachable, no loser row; fails with the store check removed); `server.rs` `conditional_page_writes::*` (stale id, `create_only` on an existing page, a version id from another project, and conditional deletes are refused and change nothing, with and without the wiki pre-check; the store check proven to bite with the reader pre-check off); `a_reader_may_read_everything_and_change_nothing` (a read-only user's conditional write or delete, with the right id or a wrong one, gets the authorization error and no version data; the page is unchanged); importer `resume_refuses_an_edited_body_even_with_the_original_ingest_key` and `e2e_export_import_rebuilds_chains_and_honours_ttl` (an edited destination body keeps its attribution key but cannot be superseded by a resumed import; both fail with marker-only matching); `resume_after_an_injected_failure_writes_no_duplicates` and `a_foreign_page_at_the_destination_is_never_replaced` are the legitimate-resume and foreign-page controls | STRONG. Importer resumption compares bodies, not frontmatter-only edits; custom server sanitation that changes the planned body fails closed || 5f | Wiki-sync companion destination confinement | `companions/ai-memory-wikisync/src/paths.rs` `prepare_dest` compares canonical and lexical destination components without resolving the lexical path; Windows disk and UNC verbatim prefixes compare as equivalent spellings, while changed directory components are refused. `secure_join` keeps validated page paths below that root and refuses existing symlink components | `paths.rs` `prepare_dest_refuses_windows_junctions_without_touching_the_target` refuses a junction and a destination reached through it, leaves an outside canary unchanged, and accepts the real directory; `windows_destination_prefixes_do_not_hide_different_paths` refuses different drives, shares and directory components with ordinary/verbatim controls; `prepare_dest_refuses_symlinks_and_files` and `secure_join_stays_under_root_and_refuses_symlink_components` retain the Unix link controls | Static path checks; no descriptor-relative protection against a concurrently malicious same-user process. The dedicated `windows.yml` wiki sync companion job runs the prefix and junction tests on companion/workflow changes, `windows` or `full-ci` labels, nightly runs and dispatch || 6 | Active-project pointer (PerActor, no clobber) | `ai-memory-core/src/active_project.rs` `set_for`/`lookup_for` (fail-closed on `Mismatch`); `ai-memory-cli/src/commands/mcp_bridge.rs` `bridge_session_id`: the session-less bridge (`--no-session-id`, every non-Claude stdio client on a token-file install) never forwards an inherited `CLAUDE_CODE_SESSION_ID`, so a client launched from a Claude Code terminal cannot steer that session's per-session pointer; the session-aware bridge reads only the variable its `--agent` names (`CLAUDE_CODE_SESSION_ID`, or `MUSE_SESSION_ID` for Meta Muse Code), so neither harness's id stands in for the other's | `active_project.rs` `parallel_harnesses_of_one_user_keep_separate_pointers`, `two_operators_never_read_each_others_pointer`, `a_session_mismatch_fails_closed_once_anything_has_been_keyed`; `mcp_bridge.rs` `only_the_session_aware_mode_forwards_and_requires_a_session_id` (an inherited id is dropped in session-less mode; the session-aware control forwards it and still fails closed without one), `each_harness_forwards_only_its_own_session_id` (a Muse bridge never forwards an inherited Claude id and fails closed without its own, a Claude bridge never forwards an inherited Muse id; controls forward each harness's own id) and `a_session_less_upstream_config_carries_no_session_header`; `auth_default_e2e.rs` `a_session_less_bridge_serves_any_client_without_holding_the_token` (the built `mcp-bridge --agent muse` refuses to start with only `CLAUDE_CODE_SESSION_ID` set) | STRONG || 6b | Native `_meta` routing coordinate is routing-only (never grants identity) | `ai-memory-mcp/src/server.rs` `attach_native_session` reads only `_meta["ai.opencode/sessionID"]` into `ActorKey.session_id` (trimmed, non-empty-string-validated); `user` still comes from the authenticated `ActorContext`, so a forged `_meta` cannot cross a user/scope boundary — it only selects a session slot behind the existing `active_project` guard | `server.rs` `native_session_metadata_is_routing_only_and_preserves_precedence`, `native_session_metadata_validates_strings_without_granting_identity`; `autoscope_multiuser.rs` `native_metadata_routes_concurrent_reads_and_writes_to_hook_workspaces` (#864) | STRONG |