Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
test(store): prove a moved session's ingest keys and profile candidates follow it
AkitaOnRails0d47b6566129
diff --git a/crates/ai-memory-store/tests/suite/mod.rs b/crates/ai-memory-store/tests/suite/mod.rsindex 6d38b101..ac9f3d9f 100644--- a/crates/ai-memory-store/tests/suite/mod.rs+++ b/crates/ai-memory-store/tests/suite/mod.rs@@ -26,6 +26,7 @@ mod retrieval_observation_stream;mod retrieval_superseded;mod retrieval_tuning_streams;mod session_ids_touching_scope;+mod session_move;mod session_observations;mod session_scope_from_observations;mod sessions_by_agent;diff --git a/crates/ai-memory-store/tests/suite/session_move.rs b/crates/ai-memory-store/tests/suite/session_move.rsnew file mode 100644index 00000000..dbfe7b02--- /dev/null+++ b/crates/ai-memory-store/tests/suite/session_move.rs@@ -0,0 +1,279 @@+//! What follows a session when `move_session` re-stamps it into another+//! project: the ingest keys that make a spool redelivery idempotent, and the+//! profile evidence harvested from its prompts, which must answer to the+//! target project's profile governance rather than the source's.++use ai_memory_core::profile::{ProfileCandidateSource, ProfileGenerality};+use ai_memory_core::{+ AgentKind, NewObservation, NewSession, ObservationKind, OwnerFilter, ProjectId, Sanitized,+ Sanitizer, SessionId, WorkspaceId,+};+use ai_memory_store::{+ AccessMode, HookSessionAdmission, IngestObservationOutcome, NewProfileCandidate, PagesMode,+ ProfileHarvestMark, ProjectProfileFlags, Store,+};+use rusqlite::{Connection, params};++async fn project(store: &Store, ws: WorkspaceId, name: &str) -> ProjectId {+ store+ .writer+ .get_or_create_project(ws, name.to_owned(), None)+ .await+ .unwrap()+}++fn hook_session(id: SessionId, ws: WorkspaceId, proj: ProjectId) -> NewSession {+ NewSession {+ occurred_at: None,+ id,+ workspace_id: ws,+ project_id: proj,+ agent_kind: AgentKind::Codex,+ cwd: Some("/repo/app".into()),+ actor_user: None,+ }+}++fn observation(session: &NewSession, kind: ObservationKind, body: &str) -> NewObservation {+ NewObservation {+ occurred_at: Some(1_700_000_000_000_000),+ session_id: session.id,+ workspace_id: session.workspace_id,+ project_id: session.project_id,+ kind,+ extension: None,+ source_event: None,+ title: kind.as_str().into(),+ body: body.into(),+ importance: 5,+ }+}++fn sanitized(obs: NewObservation) -> Sanitized<NewObservation> {+ Sanitized::new(obs, &Sanitizer::builtin())+}++fn session_end_count(store: &Store, sid: SessionId) -> i64 {+ let conn = Connection::open(store.db_path()).unwrap();+ conn.query_row(+ "SELECT COUNT(*) FROM observations WHERE session_id = ?1 AND kind = ?2",+ params![sid.as_bytes(), ObservationKind::SessionEnd.as_str()],+ |r| r.get(0),+ )+ .unwrap()+}++/// A keyed recovery SessionEnd that already completed must stay complete+/// after its session moves. The spool redelivers the same envelope (still+/// naming the source project); admission rescopes it to the session row's new+/// project and looks the key up there. Before #1265 the key stayed behind in+/// the source project, the lookup missed, and the replay appended a second+/// SessionEnd to the moved session.+#[tokio::test]+async fn a_replayed_recovery_session_end_stays_complete_after_its_session_moves() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default".to_owned())+ .await+ .unwrap();+ let source = project(&store, ws, "source").await;+ let target = project(&store, ws, "target").await;++ let sid = SessionId::new();+ let session = hook_session(sid, ws, source);+ store.writer.begin_session(session.clone()).await.unwrap();+ store+ .writer+ .insert_observation(sanitized(observation(+ &session,+ ObservationKind::UserPrompt,+ "fix the parser",+ )))+ .await+ .unwrap();++ let key = format!("recovery_{}", "b".repeat(55));+ let end = observation(&session, ObservationKind::SessionEnd, "session ended");+ let admitted = match store+ .writer+ .admit_hook_session_event(+ session.clone(),+ sanitized(end.clone()),+ OwnerFilter::Any,+ Some(key.clone()),+ None,+ )+ .await+ .unwrap()+ {+ HookSessionAdmission::EndOpen {+ session,+ ingest: IngestObservationOutcome::Inserted(_),+ } => session,+ other => panic!("the first recovery SessionEnd must be admitted: {other:?}"),+ };+ store+ .writer+ .end_admitted_session(admitted, None, None)+ .await+ .unwrap();+ store+ .writer+ .complete_observation_ingest(source, key.clone())+ .await+ .unwrap();+ // A late delivery after the end, so a redelivered SessionEnd is not+ // short-circuited as `AlreadyEnded` and must be judged by its key.+ store+ .writer+ .admit_hook_session_event(+ session.clone(),+ sanitized(observation(+ &session,+ ObservationKind::PostToolUse,+ "late tool event",+ )),+ OwnerFilter::Any,+ None,+ None,+ )+ .await+ .unwrap();++ store+ .writer+ .move_session(sid, ws, target, PagesMode::Move, None, true)+ .await+ .unwrap();++ let replay = store+ .writer+ .admit_hook_session_event(session, sanitized(end), OwnerFilter::Any, Some(key), None)+ .await+ .unwrap();+ assert!(+ matches!(+ replay,+ HookSessionAdmission::ReEnd {+ ingest: IngestObservationOutcome::AlreadyComplete,+ ..+ }+ ),+ "the replayed recovery SessionEnd must be recognised as complete: {replay:?}"+ );+ assert_eq!(+ session_end_count(&store, sid),+ 1,+ "a replay must not append a second SessionEnd"+ );+}++/// Adversarial (security-boundaries row 1h): profile evidence harvested from+/// a session follows the session, so moving it into a project that refuses to+/// contribute takes the candidate out of convergence, and moving it into a+/// restricted project marks it restricted so a team profile drops it. Control:+/// the same move into an open, contributing project keeps it counted, now+/// attributed to the target. Before #1265 the candidate stayed stamped with+/// the open source project and kept counting from there.+#[tokio::test]+async fn a_moved_sessions_profile_candidate_follows_the_target_projects_governance() {+ let tmp = tempfile::tempdir().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default".to_owned())+ .await+ .unwrap();+ let source = project(&store, ws, "open-source").await;+ let opted_out = project(&store, ws, "client-work").await;+ store+ .writer+ .set_project_profile_flags(+ opted_out,+ ProjectProfileFlags {+ contribute: false,+ consume: true,+ },+ )+ .await+ .unwrap();+ let restricted = project(&store, ws, "private-repo").await;+ store+ .writer+ .set_access_mode(restricted, AccessMode::Restricted)+ .await+ .unwrap();+ let open = project(&store, ws, "open-target").await;++ let mut sessions = Vec::new();+ for statement in ["opted-out habit", "restricted habit", "open habit"] {+ let sid = SessionId::new();+ store+ .writer+ .begin_session(hook_session(sid, ws, source))+ .await+ .unwrap();+ store+ .writer+ .record_profile_harvest(+ ws,+ source,+ vec![NewProfileCandidate {+ source: ProfileCandidateSource::Prompt,+ source_ref: format!("session:{sid}"),+ topic_key: statement.into(),+ category: "workflow".into(),+ statement: statement.into(),+ quote: statement.into(),+ applies_to: Vec::new(),+ observed_at: 1,+ contributor: None,+ generality: ProfileGenerality::General,+ confidence: 0.9,+ }],+ ProfileHarvestMark::default(),+ )+ .await+ .unwrap();+ sessions.push((sid, statement));+ }+ let counted = |rows: &[ai_memory_store::ProfileCandidateRow], statement: &str| {+ rows.iter()+ .find(|row| row.candidate.statement == statement)+ .map(|row| (row.project.clone(), row.restricted))+ };+ let before = store.reader.profile_candidates(100).await.unwrap();+ for (_, statement) in &sessions {+ assert_eq!(+ counted(&before, statement),+ Some(("open-source".to_owned(), false)),+ "every candidate counts from the open source project before the move"+ );+ }++ for ((sid, _), dest) in sessions.iter().zip([opted_out, restricted, open]) {+ store+ .writer+ .move_session(*sid, ws, dest, PagesMode::Move, None, true)+ .await+ .unwrap();+ }+ let after = store.reader.profile_candidates(100).await.unwrap();+ assert_eq!(+ counted(&after, "opted-out habit"),+ None,+ "a candidate moved into a non-contributing project must not count"+ );+ assert_eq!(+ counted(&after, "restricted habit"),+ Some(("private-repo".to_owned(), true)),+ "a candidate moved into a restricted project must carry its restriction"+ );+ assert_eq!(+ counted(&after, "open habit"),+ Some(("open-target".to_owned(), false)),+ "control: a candidate moved into an open project still counts, from there"+ );+}diff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex f577bcbf..ba1d0a8f 100644--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@ -29,7 +29,7 @@ means the boundary is not built yet.| 1e | The shared profile scopes are read-open, write-gated like `_global` | `ai-memory-store/src/project_authz.rs` `is_reserved_global` (a workspace `_profile` joins `default/_global`: on a server that distinguishes operators a write needs root or a `write` grant, creating it grants nothing); `ops.rs` `initial_access_mode` keeps `_profile` open | store `integration::profile` `shared_profiles_are_write_gated_for_database_users` (ungranted user refused for both shares; root and a write grant admitted; reads open); `server.rs` `a_workspace_profile_is_write_gated_and_read_open`; both fail with `_profile` dropped from the gate | STRONG || 1f | Event capture never lands in a profile project, and the sweep keeps them | `ai-memory-hooks/src/router.rs` project resolution refuses `ai_memory_core::profile::is_reserved_scope_project` (`_global`, `_profile`, `_profile.*`) from a directory or a marker override, falling back to the server default; `ops.rs` `sweep_hollow_projects` exempts `_profile` and `_profile.%` | `router.rs` `capture_is_never_attributed_to_a_profile_project` (override and directory spellings refused; ordinary `my_profile` control resolves), fails with the check narrowed to `_global`; store `integration::profile` `the_hollow_project_sweep_keeps_empty_profile_projects` | STRONG || 1g | The profile reaches only the sessions it is meant for: off by default on a multi-user server, suppressed by `[profile] consume = false`, and an opt-out is sticky | `ai_memory_core::profile::ProfileSettings::effective_share` (`enabled = auto` is off once operators are distinguished); `router.rs` `persist_profile_flags` (flags are tri-state: an absent forwarded value keeps the stored one, so a client that sends nothing never undoes an opt-out; every front door sends explicit `0`/`1` once a marker resolved) and `render_requested_profile_digest` (a team profile is headed "team defaults"); `server.rs` `memory_query` drops `profile/` hits for a project whose stored `profile_consume` is off; `admin_profile.rs` `handle_apply` (`ai-memory profile apply`) returns no lines for such a project and sits behind the root-only `/admin/*` gate on a multi-user server | `router.rs` `a_multi_user_server_has_no_profile_digest_by_default`, `profile_digest_honours_consume_once_per_session_and_the_server_switch`, `session_start_records_the_marker_profile_flags` (a session start with no flags after an opt-out keeps the project out of the harvest and the digest; fails when an absent flag defaults to on); `server.rs` `the_single_user_profile_lives_in_global_and_honours_consume` (fails with the consume filter removed); `admin_profile_routes.rs` `apply_lists_the_lines_for_a_project_and_honours_consume`, `apply_is_root_only_on_a_multi_user_server` (database user refused, root control admitted); core `profile::tests::effective_share_matrix` | STRONG |-| 1h | The profile learns only from what it may: the user's own prompts and curated pages, from contributing projects, never another operator's words in a private profile and never a restricted project in a shared one; stored text stays data | `ai-memory-store/src/profile.rs` `profile_prompts_since` (only `user-prompt` observations), `auto_improve_decided_by` (the decider agent a page's `auto_improve_proposal_id` resolves to), `profile_harvest_projects` / `profile_candidates` (`profile_contribute = 0` excluded, including candidates recorded before the opt-out); `ai-memory-consolidate/src/profile.rs` `user_written` (a harness turn or Codex's injected instructions stored as a prompt is skipped whole, and ai-memory's routing and untrusted-history blocks, fenced by markers on their own lines, are cut out before any sentence is read), the auto-approved-proposal skip in `harvest_project` (a page the auto-improve scheduler auto-approved is model output nobody reviewed and never becomes a candidate; a human-approved page, or one whose proposal id has no row, stays curated, #1211), `convergence_targets` (a `user` target keeps only candidates whose `contributor` is that operator, from projects the per-project choke point lets them read; a `global`/`workspace` target on an operator-distinguishing server drops restricted projects), `min_contributors` / `converge` (a team profile admits a statement only on evidence from at least two distinct operators, so one operator cannot plant a default in everyone's session start) (never rewrites a page without its `generated_by` stamp or whose body changed; `profile_entry_ledger` keeps a forgotten entry out), `classify_batch` / `merge_entry` (sanitized, JSON-encoded input; the evidence quote is always the user's sentence) | consolidate `integration::profile_harvest` `tool_output_is_never_a_candidate`, `an_opted_out_project_is_never_harvested`, `a_private_profile_never_harvests_another_operator`, `a_team_profile_never_harvests_a_restricted_project`, `a_team_profile_needs_more_than_one_operator` (one operator in two open projects not admitted; a second operator admits it; fails with the threshold at one), `a_personal_profile_admits_one_operators_habit` (control), `a_hand_edited_entry_is_never_clobbered`, `a_forgotten_entry_is_not_recreated_until_said_again`, `injected_instructions_stay_data`, `injected_instructions_and_ai_memory_blocks_are_never_candidates`, `page_metadata_fields_are_never_candidates`, `auto_approved_auto_improve_pages_are_never_candidates` (an auto-approved page refused; human-approved and hand-written controls admitted; fails with the decider check removed), consolidate `profile::tests::a_stored_page_metadata_field_is_not_a_waiting_habit` (each with a legitimate control; each fails with its guard removed); mcp `integration::admin_profile_routes` `review_and_rebuild_are_root_only_on_a_multi_user_server` | STRONG |+| 1h | The profile learns only from what it may: the user's own prompts and curated pages, from contributing projects, never another operator's words in a private profile and never a restricted project in a shared one; stored text stays data | `ai-memory-store/src/profile.rs` `profile_prompts_since` (only `user-prompt` observations), `auto_improve_decided_by` (the decider agent a page's `auto_improve_proposal_id` resolves to), `profile_harvest_projects` / `profile_candidates` (`profile_contribute = 0` excluded, including candidates recorded before the opt-out); `ai-memory-store/src/ops.rs` `move_session` (a moved session's `session:<id>` prompt candidates are re-stamped into the target project in the same transaction, so they answer to its contribute flag and access mode, #1265); `ai-memory-consolidate/src/profile.rs` `user_written` (a harness turn or Codex's injected instructions stored as a prompt is skipped whole, and ai-memory's routing and untrusted-history blocks, fenced by markers on their own lines, are cut out before any sentence is read), the auto-approved-proposal skip in `harvest_project` (a page the auto-improve scheduler auto-approved is model output nobody reviewed and never becomes a candidate; a human-approved page, or one whose proposal id has no row, stays curated, #1211), `convergence_targets` (a `user` target keeps only candidates whose `contributor` is that operator, from projects the per-project choke point lets them read; a `global`/`workspace` target on an operator-distinguishing server drops restricted projects), `min_contributors` / `converge` (a team profile admits a statement only on evidence from at least two distinct operators, so one operator cannot plant a default in everyone's session start) (never rewrites a page without its `generated_by` stamp or whose body changed; `profile_entry_ledger` keeps a forgotten entry out), `classify_batch` / `merge_entry` (sanitized, JSON-encoded input; the evidence quote is always the user's sentence) | consolidate `integration::profile_harvest` `tool_output_is_never_a_candidate`, `an_opted_out_project_is_never_harvested`, `a_private_profile_never_harvests_another_operator`, `a_team_profile_never_harvests_a_restricted_project`, `a_team_profile_needs_more_than_one_operator` (one operator in two open projects not admitted; a second operator admits it; fails with the threshold at one), `a_personal_profile_admits_one_operators_habit` (control), `a_hand_edited_entry_is_never_clobbered`, `a_forgotten_entry_is_not_recreated_until_said_again`, `injected_instructions_stay_data`, `injected_instructions_and_ai_memory_blocks_are_never_candidates`, `page_metadata_fields_are_never_candidates`, `auto_approved_auto_improve_pages_are_never_candidates` (an auto-approved page refused; human-approved and hand-written controls admitted; fails with the decider check removed), consolidate `profile::tests::a_stored_page_metadata_field_is_not_a_waiting_habit` (each with a legitimate control; each fails with its guard removed); mcp `integration::admin_profile_routes` `review_and_rebuild_are_root_only_on_a_multi_user_server`; store `integration::session_move` `a_moved_sessions_profile_candidate_follows_the_target_projects_governance` (a session moved into an opted-out project stops counting and one moved into a restricted project is marked restricted; an open-target control still counts, from the target; fails with the `move_session` re-stamp removed) | STRONG || 2 | Workspace isolation | same as #1; same-named project → distinct ids per workspace | `scope.rs` cross-workspace resolution rows; `multi_scope` dedup/validate; `ai-memory-web` `history_replay.rs` (another workspace's page path, version id and session id answer 404, same tests and bites as row 1) | STRONG || 2b | Managed-workstream discovery stays checkout-local, including pagination | `ai-memory-workstream/src/repository.rs` `inspect_repository_fingerprints` shares the launch identity derivation without checkpoint scans; `ai-memory-cli/src/commands/resume.rs` `candidates_for_checkout` queries only the cwd, never registry links; the explicit `--all` walk (`candidates_across_checkouts`) lists each linked checkout through its own fingerprint-scoped request and applies `--workspace` before any request; `ai-memory-hooks/src/workstream.rs` `list_recent_workstreams` resolves an authorized explicit scope on every page; `ai-memory-store/src/workstream.rs` `list_recent` applies workspace/project/repository/worktree predicates before `LIMIT`/`OFFSET` | `resume.rs` `resume_lists_all_current_checkout_rows_not_registered_foreign_checkouts` (same scope/remote foreign checkout refused, 125 local rows visible), `empty_current_checkout_does_not_fall_back_to_the_registry`, `resume_all_walks_every_linked_checkout_and_honours_the_workspace_filter` (a foreign workspace filter sends no request; the default picker still ignores the registry); `workstream.rs` `recent_workstream_pages_reach_older_rows_without_crossing_checkout_or_scope` (foreign repo, worktree, project and workspace excluded on every page; 105 legitimate rows reach the client, huge offsets return nothing); store `recent_workstreams_are_checkout_local_and_include_linked_harnesses` (page two keeps native harness links); `repository.rs` `reports_origin_push_url_and_linked_worktree` verifies lightweight/full fingerprint parity and distinct worktree keys; CLI `resume_yolo_displays_picker_without_scanning_git_status` verifies listing skips status scans | STRONG || 3 | Multi-user auth ladder | `ai-memory-core/src/actor.rs` `AuthLevel::authorize`; `ai-memory-mcp/src/auth.rs` middleware; `admin.rs` `require_root_for_multiuser_admin` / `require_root`; `identity.rs` authorizes `NormalRead` behind the same machine bearer middleware as MCP; `serve.rs` mounts API-only and web+API protected routers behind `require_dual_auth` | `admin.rs` `multiuser_admin_routes_reject_db_user_tier` / `…reject_anonymous` / `create_user_as_user_tier_returns_403`; `auth.rs` unknown-bearer 401; `actor.rs` `skip_admission_chain_rejects_db_users`; `identity.rs` `identity_real_auth_isolates_users_root_and_revoked_keys`, `identity_anonymous_ignores_forged_headers_and_proxy_qualifies_oidc` (distinct users and root, invalid/revoked keys, forged headers, anonymous control); `serve.rs` `api_only_mode_keeps_data_authenticated_and_web_absent` (anonymous API read refused, authenticated control admitted, browser route absent; #986) | STRONG |