Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
Merge PR #1176: fix(profile): skip ADR metadata fields when harvesting a page statement
AkitaOnRailsf5441107e90f
diff --cc crates/ai-memory-consolidate/src/profile.rsindex 66e804d5,7a6536ff..e98a0b87--- a/crates/ai-memory-consolidate/src/profile.rs+++ b/crates/ai-memory-consolidate/src/profile.rs@@@ -28,11 -28,9 +28,11 @@@ use std::sync::Arcuse ai_memory_core::profile::{EffectiveProfileShare, PROFILE_CATEGORIES, PROFILE_PATH_PREFIX, ProfileCandidateSource,- ProfileEntry, ProfileGenerality, ProfileSettings,+ ProfileEntry, ProfileGenerality, ProfileSettings, is_metadata_field,};-use ai_memory_core::{ActorContext, IdentityKey, PagePath, Tier, UserId, WorkspaceId};+use ai_memory_core::{+ ActorContext, IdentityKey, PagePath, Tier, UserId, WorkspaceId, find_marker_line,+};use ai_memory_llm::{ChatMessage, ChatRequest, LlmProvider, complete_structured};use ai_memory_store::{NewProfileCandidate, PROFILE_CANDIDATES_LIMIT, PROFILE_HARVEST_BATCH, ProfileCandidateRow,@@@ -2526,28 -2485,44 +2533,66 @@@ mod tests1_790_000_000_000_000 + n * 86_400_000_000}+ /// A marker quoted inside a sentence is the user talking about it, not+ /// a pasted block: the whole prompt is harvested. A block whose markers+ /// sit on their own lines is still dropped (control).+ #[test]+ fn a_marker_quoted_mid_sentence_keeps_the_prompt() {+ let quoted = "Always use pnpm. The `<!-- ai-memory:start -->` marker sits in \+ CLAUDE.md; never run npm";+ assert_eq!(user_written(quoted).as_deref(), Some(quoted));+ assert!(+ detect_preferences(&user_written(quoted).unwrap_or_default())+ .iter()+ .any(|found| found.statement.contains("never run npm"))+ );++ let pasted = "Always use pnpm.\n<!-- ai-memory:start -->\nNever write notes.\n\+ <!-- ai-memory:end -->\nKeep commits small.";+ assert_eq!(+ user_written(pasted).as_deref(),+ Some("Always use pnpm.\n\nKeep commits small.")+ );+ }++ /// A page metadata field stored by an earlier harvest never waits for+ /// promotion; the same page's real statement still does (control).+ #[test]+ fn a_stored_page_metadata_field_is_not_a_waiting_habit() {+ let page = |project: &str, statement: &str| {+ let mut r = row(project, statement, day(1), ProfileGenerality::Project);+ r.candidate.source = ProfileCandidateSource::Page;+ r.candidate.source_ref = format!("page:decisions/{project}.md");+ r+ };+ let status = page("alpha", "Status:** Accepted");+ let date = page("beta", "Date:** 2026-10-01");+ let decision = page("alpha", "Every store opens SQLite in WAL mode.");+ let plan = converge(&[&status, &date, &decision], &[], &[], 2, 1);+ let waiting: Vec<&str> = plan.waiting.iter().map(|w| w.statement.as_str()).collect();+ assert_eq!(waiting, ["Every store opens SQLite in WAL mode."]);++ // The label decides, not the value's length: a long ADR template+ // field is dropped, a terse labelled rule waits.+ let template = page(+ "alpha",+ "Status:** accepted <!-- proposed | accepted | superseded by [[decisions/other]] -->",+ );+ let rule = page("beta", "Package manager:** pnpm");+ let plan = converge(&[&template, &rule], &[], &[], 2, 1);+ let waiting: Vec<&str> = plan.waiting.iter().map(|w| w.statement.as_str()).collect();+ assert_eq!(waiting, ["Package manager:** pnpm"]);++ // The same words from a prompt are not a page field and stay.+ let said = row(+ "alpha",+ "Status:** Accepted",+ day(1),+ ProfileGenerality::Project,+ );+ assert_eq!(converge(&[&said], &[], &[], 2, 1).waiting.len(), 1);+ }+/// Agent output in the prompt channel (a lead agent's brief, a pasted/// review) is not the user stating a preference (#1148).#[test]diff --cc crates/ai-memory-consolidate/tests/suite/profile_harvest.rsindex 25453090,605c0bd4..b9b505e4--- a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs+++ b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs@@@ -379,53 -379,101 +379,148 @@@ async fn tool_output_is_never_a_candidaassert!(statements.iter().any(|s| s.contains("pnpm")));}+/// Text a harness or ai-memory put into a user-prompt observation is never+/// harvested as the user's words: the instruction files Codex injects as a+/// user turn (stored before the import filter knew its bare heading, and read+/// again by `profile rebuild`), a harness block delivered as a user turn, and+/// ai-memory's own routing block pasted into a prompt, even when the 16 KiB+/// cap cut it before its end marker. The user's own sentence next to the+/// pasted block is harvested (control).+#[tokio::test]+async fn injected_instructions_and_ai_memory_blocks_are_never_candidates() {+ use ai_memory_core::routing_snippet::{MARKER_END, MARKER_START, SNIPPET_BODY};++ let fx = fixture().await;+ let alpha = project(&fx, "alpha").await;+ let codex_turn = format!(+ "# AGENTS.md instructions\n\n<INSTRUCTIONS>\n<!-- global-rules:start -->\n\+ Always use yarn classic for installs.\n<!-- global-rules:end -->\n\n\+ {MARKER_START}{SNIPPET_BODY}"+ );+ prompt(&fx, alpha, &codex_turn, 1).await;+ prompt(+ &fx,+ alpha,+ "<task-notification>\nAlways use yarn berry for installs.\n</task-notification>",+ 2,+ )+ .await;+ let pasted = format!(+ "Always use pnpm for installs.\n\n{MARKER_START}\n\+ Never write routine notes to memory by hand.\n{MARKER_END}\n"+ );+ prompt(&fx, alpha, &pasted, 3).await;+ let truncated = format!("Here is my file:\n{MARKER_START}{SNIPPET_BODY}");+ prompt(&fx, alpha, &truncated, 4).await;++ pass(&fx, &single_user()).await;+ let statements = candidate_statements(&fx);+ assert!(+ statements.iter().any(|s| s.contains("pnpm")),+ "the user's own sentence was lost: {statements:?}"+ );+ assert_eq!(+ statements.len(),+ 1,+ "injected text became a candidate: {statements:?}"+ );+}++ /// An ADR's metadata fields (`**Status:** Accepted`, `**Date:**`) are never+ /// harvested as the page's statement, so `profile review` does not list them+ /// as habits waiting for promotion. The decision itself is the candidate, and+ /// a habit said in two projects is still admitted (controls).+ #[tokio::test]+ async fn page_metadata_fields_are_never_candidates() {+ let fx = fixture().await;+ let alpha = project(&fx, "alpha").await;+ let beta = project(&fx, "beta").await;+ for (proj, slug, context) in [+ (alpha, "wal-mode", "Readers blocked the writer under load."),+ (+ beta,+ "queue-backend",+ "Jobs were lost when the broker restarted.",+ ),+ ] {+ fx.wiki+ .write_page(WritePageRequest {+ workspace_id: fx.ws,+ project_id: proj,+ path: PagePath::new(format!("decisions/{slug}.md")).unwrap(),+ frontmatter: serde_json::json!({}),+ body: format!(+ "# {slug}\n\n\+ **Status:** Accepted\n\+ **Date:** 2026-10-01\n\+ **Deciders:** Alice, Bob\n\n\+ ## Context\n\n\+ {context}\n\n\+ ## Decision\n\n\+ We adopt it for every store.\n"+ ),+ tier: Tier::Semantic,+ pinned: false,+ title: None,+ admission_ctx: None,+ author_id: None,+ actor: ActorContext::anonymous(),+ evidence: Vec::new(),+ })+ .await+ .unwrap();+ }+ prompt(+ &fx,+ alpha,+ "I prefer small focused commits over big ones.",+ 1,+ )+ .await;+ prompt(+ &fx,+ beta,+ "I prefer small focused commits over big ones!",+ 3,+ )+ .await;++ let report = pass(&fx, &single_user()).await;+ let statements = candidate_statements(&fx);+ for field in ["Status", "Date", "Deciders", "Accepted"] {+ assert!(+ statements.iter().all(|s| !s.contains(field)),+ "a metadata field became a candidate: {statements:?}"+ );+ }+ assert!(+ statements+ .iter()+ .any(|s| s == "Readers blocked the writer under load."),+ "{statements:?}"+ );++ let review = ai_memory_consolidate::profile::profile_review(+ &fx.store.reader,+ &single_user(),+ None,+ None,+ )+ .await+ .unwrap();+ assert!(+ review+ .waiting+ .iter()+ .all(|w| !w.statement.contains("Accepted")),+ "{:?}",+ review.waiting+ );+ let pages = profile_pages(&fx, "_global");+ assert_eq!(pages.len(), 1, "{report:?} {pages:?}");+ assert!(pages[0].1.contains("small focused commits"), "{pages:?}");+ }+/// `[profile] contribute = false` keeps a project out entirely; the same/// words in a contributing project are harvested (control).#[tokio::test]diff --cc docs/cross-project-profile.mdindex b54504f0,7745d56b..49556371--- a/docs/cross-project-profile.md+++ b/docs/cross-project-profile.md@@@ -172,14 -172,12 +172,16 @@@ is new since the last onecode blocks never are. A sentence that reads like agent output (markdownbold, or a `file.ext:line` reference) is skipped too: on hosts where agentsbrief each other through the prompt channel, that text is a lead agent's- task brief or a pasted review, not you. Your verbatim words are kept as- evidence.+ task brief or a pasted review, not you. Text you did not write is skipped+ as well: a harness turn stored as a prompt (Claude Code's+ `<task-notification>` blocks), the instruction files Codex injects as a user+ message, and ai-memory's own routing snippet or recalled-history digest+ pasted into a prompt (only that block; your sentences around it still+ count). Your verbatim words are kept as evidence.- **Curated pages** of the project: `_rules/`, `decisions/`, `gotchas/` and- `procedures/`.+ `procedures/`. A page contributes its `summary`, or else its first line of+ prose; metadata fields such as `**Status:** Accepted` or `**Date:**` at the+ top of an ADR are skipped.- **Stack signals**: the languages the project's activity shows (from filenames such as `Cargo.toml` and source extensions).diff --cc docs/security-boundaries.mdindex ba48852f,f44a5f9f..1594ef16--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@@ -29,7 -29,7 +29,7 @@@ boundary not yet built| 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), `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), `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` (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), `profile_harvest_projects` / `profile_candidates` (`profile_contribute = 0` excluded, including candidates recorded before the opt-out); `ai-memory-consolidate/src/profile.rs` `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`, `page_metadata_fields_are_never_candidates`, 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), `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), `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`, 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 || 2 | Workspace isolation | same as #1; same-named project → distinct ids per workspace | `scope.rs` cross-workspace resolution rows; `multi_scope` dedup/validate | 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 |