Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(wikisync): classify imports with their write precondition
Samir Hanna Verza88f893ce9b0f
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 75ce823b..074dc58b 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Fixed `ai-memory-wikisync sync` overwriting a concurrent server edit when+ importing a repository edit. Edited files now use one full page read for+ classification and the write precondition; a second read after a 304 could+ previously accept the newer version without checking for a conflict (#986).- 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-wikisync/README.md b/companions/ai-memory-wikisync/README.mdindex 56e1d25c..65fce1f6 100644--- a/companions/ai-memory-wikisync/README.md+++ b/companions/ai-memory-wikisync/README.md@@ -173,7 +173,10 @@ Safety rules:import; the refusal names the keys. Such pages still export.- **Every write is conditional.** Each import carries the page version theplan classified (`expected_page_id`), or `create_only` for a new page, and- each delete carries the version too. A page that changes on the server+ each delete carries the version too. An edited repository file triggers a+ full page read, so classification and the write precondition use the same+ server version. Unchanged files still use ETag revalidation. A page that+ changes on the serverafter it was classified is refused by the server, reported as "changedduring this run", and left with its old state for the next run; the otherpages still sync, and the run exits non-zero. A server without thesediff --git a/companions/ai-memory-wikisync/src/bidi.rs b/companions/ai-memory-wikisync/src/bidi.rsindex ab28924b..decbea05 100644--- a/companions/ai-memory-wikisync/src/bidi.rs+++ b/companions/ai-memory-wikisync/src/bidi.rs@@ -465,9 +465,15 @@ async fn build_plan(let mut etag = None;let mut rendered = None;if on_server.contains(&path) {- let hint = disk- .as_ref()- .and(base.and_then(|base| base.etag.as_deref()));+ // An edited file needs metadata and a version id from the same+ // read we classify. Following a 304 with a full read could pick+ // up a concurrent edit and import over it without a conflict.+ let hint = base+ .filter(|base| {+ disk.as_ref()+ .is_some_and(|disk| state::sha256_hex(disk) == base.hash)+ })+ .and_then(|base| base.etag.as_deref());let read = api.read_page(&run_args.workspace, &run_args.project, &path, hint).await?;diff --git a/companions/ai-memory-wikisync/tests/api_server.rs b/companions/ai-memory-wikisync/tests/api_server.rsindex 669a3b36..df458eeb 100644--- a/companions/ai-memory-wikisync/tests/api_server.rs+++ b/companions/ai-memory-wikisync/tests/api_server.rs@@ -71,6 +71,9 @@ struct Fixture {/// Rewrite this page's body on its Nth read (1-based), to simulate an/// edit landing between a sync's classification and its write.edit_on_read: Option<(&'static str, usize)>,+ /// Edit after capturing the Nth read's snapshot, before the next request.+ /// The response still describes the old version, whether 200 or 304.+ edit_after_read: Option<(&'static str, usize)>,/// Save an editor's local change while the next page request is in flight.repository_edit_on_read: Mutex<Option<(PathBuf, Vec<u8>)>>,/// Text the fixture's write path redacts, like the server's sanitizer.@@ -112,6 +115,7 @@ impl Fixture {project_exists: true,unreachable: None,edit_on_read: None,+ edit_after_read: None,repository_edit_on_read: Mutex::new(None),redact: None,edit_before_mcp: None,@@ -299,6 +303,12 @@ async fn page_handler(// Like the server: the ETag covers the whole projection, so a change to// metadata alone also changes it.let etag = format!("\"{}\"", state::sha256_hex(document.to_string().as_bytes()));+ if let Some((target, nth)) = fixture.edit_after_read+ && target == path+ && nth == read_count+ {+ fixture.update(&path, |page| page.body.push_str("edited concurrently\n"));+ }if let Some(if_none_match) = headers.get(header::IF_NONE_MATCH).and_then(|v| v.to_str().ok())@@ -977,6 +987,12 @@ async fn sync_exports_metadata_and_round_trips_without_writes() {);sync(addr, &dest).await.expect("second sync");+ assert_eq!(fixture.page_reads_200.load(Ordering::SeqCst), 1);+ assert_eq!(+ fixture.page_reads_304.load(Ordering::SeqCst),+ 1,+ "an unchanged repository file still revalidates with its ETag"+ );assert_eq!(fixture.mcp_writes.load(Ordering::SeqCst),0,@@ -993,6 +1009,7 @@ async fn sync_imports_a_repo_edit_with_its_metadata() {let (addr, fixture) = serve(Fixture::with_pages(vec![decision()])).await;let (_tmp, dest) = temp_dest();sync(addr, &dest).await.expect("first sync");+ let base = state::load(&dest).unwrap().pages["decisions/db.md"].clone();let edited ="---\ntitle: \"DB\"\ntags: [\"db\", \"ops\"]\npinned: true\n---\n# DB\nUse Postgres 17.\n";@@ -1002,8 +1019,19 @@ async fn sync_imports_a_repo_edit_with_its_metadata() {.await.expect("dry run");assert_eq!(fixture.mcp_writes.load(Ordering::SeqCst), 0);+ assert_eq!(+ fixture.page_reads_200.load(Ordering::SeqCst),+ 2,+ "classifying an edited file needs one full read"+ );+ assert_eq!(fixture.page_reads_304.load(Ordering::SeqCst), 0);sync(addr, &dest).await.expect("import");+ assert_eq!(+ fixture.mcp_calls.lock().unwrap()[0]["arguments"]["expected_page_id"],+ base.page_id.unwrap(),+ "the write is conditional on the version that was classified"+ );let page = fixture.page("decisions/db.md").unwrap();assert_eq!(page.body, "# DB\nUse Postgres 17.\n");assert!(@@ -1180,6 +1208,42 @@ async fn sync_import_sends_the_classified_version_and_records_the_new_one() {}}+/// A 304 used to trigger a second, full read for an import's metadata. If+/// that read saw a concurrent edit, its id silently replaced the classified+/// version and the import overwrote it instead of failing its precondition.+#[tokio::test]+async fn sync_does_not_rebase_an_import_onto_a_concurrent_server_edit() {+ let (addr, fixture) = serve(Fixture {+ edit_after_read: Some(("decisions/db.md", 2)),+ ..Fixture::with_pages(vec![decision()])+ })+ .await;+ let (_tmp, dest) = temp_dest();+ sync(addr, &dest).await.expect("first sync");+ let base = state::load(&dest).unwrap().pages["decisions/db.md"].clone();+ let edited = DECISION_FILE.replace("Use Postgres.", "Repo edit.");+ fs::write(dest.join("decisions/db.md"), &edited).unwrap();++ let err = sync(addr, &dest)+ .await+ .expect_err("the concurrent server edit must be preserved");+ assert!(err.to_string().contains("changed during this run"), "{err}");+ let page = fixture.page("decisions/db.md").unwrap();+ assert!(page.body.contains("edited concurrently"), "{}", page.body);+ assert!(!page.body.contains("Repo edit"), "{}", page.body);+ assert_eq!(fixture.mcp_writes.load(Ordering::SeqCst), 0);+ assert_eq!(+ fixture.mcp_calls.lock().unwrap()[0]["arguments"]["expected_page_id"],+ base.page_id.as_ref().unwrap().as_str(),+ "the import must keep the version it classified"+ );+ assert_eq!(state::load(&dest).unwrap().pages["decisions/db.md"], base);+ assert_eq!(+ fs::read_to_string(dest.join("decisions/db.md")).unwrap(),+ edited+ );+}+/// The race slice 3 could only narrow: another writer lands between the/// classification read and the write. The server refuses the stale version,/// the run reports it and fails, and every other page still syncs. Withoutdiff --git a/docs/companion-crates.md b/docs/companion-crates.mdindex 3da6856c..c6efe19d 100644--- a/docs/companion-crates.md+++ b/docs/companion-crates.md@@ -345,9 +345,11 @@ directions are family-scoped and dry-run by default.and is refused for a server page carrying any metadata the write cannotcarry (`summary`, `sources`, `kind`, …), naming the keys.- Every import and delete is conditional on the page version the plan- classified, so the server refuses one that changed in between. That page- is reported as changed during the run and keeps its state; the other pages- still sync and the run exits non-zero.+ classified. An edited repository file uses one full page read to obtain+ both its comparison and its write precondition; unchanged files still use+ ETag revalidation. The server refuses a version that changed in between.+ That page is reported as changed during the run and keeps its state; the+ other pages still sync and the run exits non-zero.### Validationdiff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex b02f3ae4..90371bf0 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. `companions/ai-memory-wikisync/src/bidi.rs` `build_plan` reads the full page for an edited repository file, keeping classification and `expected_page_id` on the same server 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); wikisync `tests/api_server.rs` `sync_does_not_rebase_an_import_onto_a_concurrent_server_edit` (an edit after the read snapshot is refused by the original version precondition, preserving server, repository and baseline; fails without the conditional-read guard), `sync_imports_a_repo_edit_with_its_metadata` and `sync_exports_metadata_and_round_trips_without_writes` (ordinary import and unchanged-file ETag controls) | STRONG || 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 |