Skip to content

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

Rust

test(store): pin the interrupted-session recovery rules

John Costa4ba5acd3e261
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8d386ed..283488d6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,8 +17,8 @@ version in [`docs/changelog/`](docs/changelog/):
On a store without planner statistics, the unfinished-session recovery
lookup walked every observation in the project and rescanned the project for
each one, so the request never returned and left a server thread spinning.
- It now walks the project's sessions and pins each per-session probe to
- `idx_observations_session` (#1879).
+ It now checks each of the project's sessions once through
+ `idx_observations_session` and reads the project's handoffs once (#1879).
- Project search links now use the project's current stored name after
resolving a canonical or legacy key. Results, breadcrumbs, and the scope
selector stay on that project without renaming it (#1875).
diff --git a/crates/ai-memory-store/tests/suite/multi_session.rs b/crates/ai-memory-store/tests/suite/multi_session.rs
index 5441e060..38e0441e 100644
--- a/crates/ai-memory-store/tests/suite/multi_session.rs
+++ b/crates/ai-memory-store/tests/suite/multi_session.rs
@@ -5032,3 +5032,115 @@ async fn interrupted_recovery_refuses_foreign_scope_owner_and_receiver_with_cont
.unwrap();
assert_eq!(foreign_control.session_id, sessions[2]);
}
+
+/// One session id can carry work and handoffs in several scopes. Neither may
+/// decide recovery in another scope: foreign work must not reopen an ended
+/// session here, and a foreign baton must not hide unfinished work here.
+#[tokio::test]
+async fn interrupted_recovery_ignores_foreign_scope_work_and_batons_with_controls() {
+ use ai_memory_core::{NewObservation, ObservationKind, Sanitized, Sanitizer};
+
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let (workspace, project) = scope(&store).await;
+ let sibling = store
+ .writer
+ .get_or_create_project(workspace, "sibling", None)
+ .await
+ .unwrap();
+ // Prompts land before or after the wall-clock end and baton timestamps.
+ let (before, after) = (1_790_812_800_000_i64, 4_102_444_800_000_000_i64);
+ let prompt = |session: SessionId, proj: ProjectId, occurred: i64| {
+ Sanitized::new(
+ NewObservation {
+ occurred_at: Some(occurred),
+ session_id: session,
+ workspace_id: workspace,
+ project_id: proj,
+ kind: ObservationKind::UserPrompt,
+ extension: None,
+ source_event: None,
+ title: "unfinished task".into(),
+ body: "private evidence".into(),
+ importance: 5,
+ },
+ &Sanitizer::builtin(),
+ )
+ };
+ let baton = |session: SessionId, proj: ProjectId| NewHandoff {
+ workspace_id: workspace,
+ project_id: proj,
+ from_agent: AgentKind::Codex,
+ to_agent: None,
+ from_session_id: Some(session),
+ summary: "baton".into(),
+ next_steps: Vec::new(),
+ open_questions: Vec::new(),
+ files_touched: Vec::new(),
+ cwd: None,
+ owner_user: None,
+ };
+ let candidate = || async {
+ store
+ .reader
+ .latest_interrupted_session_candidate(workspace, project, OwnerFilter::Any, None)
+ .await
+ .unwrap()
+ .map(|candidate| candidate.session_id)
+ };
+
+ let unfinished = open_session(&store, workspace, project, AgentKind::Codex).await;
+ store
+ .writer
+ .insert_observation(prompt(unfinished, project, before))
+ .await
+ .unwrap();
+ store
+ .writer
+ .insert_handoff(baton(unfinished, sibling))
+ .await
+ .unwrap();
+ assert_eq!(
+ candidate().await,
+ Some(unfinished),
+ "a baton from another scope must not hide unfinished work here"
+ );
+ store
+ .writer
+ .insert_handoff(baton(unfinished, project))
+ .await
+ .unwrap();
+ assert_eq!(
+ candidate().await,
+ None,
+ "control: a baton from this scope covers the work"
+ );
+
+ let ended = open_session(&store, workspace, project, AgentKind::Codex).await;
+ store
+ .writer
+ .insert_observation(prompt(ended, project, before))
+ .await
+ .unwrap();
+ store.writer.end_session(ended, None).await.unwrap();
+ store
+ .writer
+ .insert_observation(prompt(ended, sibling, after))
+ .await
+ .unwrap();
+ assert_eq!(
+ candidate().await,
+ None,
+ "work in another scope must not reopen a session ended here"
+ );
+ store
+ .writer
+ .insert_observation(prompt(ended, project, after))
+ .await
+ .unwrap();
+ assert_eq!(
+ candidate().await,
+ Some(ended),
+ "control: work resumed in this scope reopens it"
+ );
+}
diff --git a/crates/ai-memory-store/tests/suite/session_activity.rs b/crates/ai-memory-store/tests/suite/session_activity.rs
index f6920f11..a9df43bb 100644
--- a/crates/ai-memory-store/tests/suite/session_activity.rs
+++ b/crates/ai-memory-store/tests/suite/session_activity.rs
@@ -4,7 +4,9 @@
//! (a quota limit, a crash) without ending. Such a session never writes a
//! fresh start or a clean end, so the ordering must come from its captured
//! work, a session resumed after an earlier end must read as not ended, and
-//! the owner filter must keep one operator's session ids from another.
+//! the owner filter must keep one operator's session ids from another. The
+//! same rules pick the session `latest_interrupted_session_candidate` offers
+//! as a startup recovery snapshot.
use ai_memory_core::{ActorContext, IdentityKey, OwnerFilter, ProjectId, WorkspaceId};
use ai_memory_store::Store;
@@ -384,3 +386,98 @@ async fn a_later_persisted_end_closes_the_resumed_read_view() {
assert_eq!(listed, vec![32, 31, 30]);
assert!(completed.iter().all(|s| s.ended_at.is_some()));
}
+
+fn observe(conn: &Connection, n: u8, session: u8, project: u8, kind: &str, at: i64) {
+ conn.execute(
+ "INSERT INTO observations \
+ (id, session_id, workspace_id, project_id, kind, title, body, importance, created_at) \
+ VALUES (?1, ?2, ?3, ?4, ?5, 't', 'b', 5, ?6)",
+ params![
+ &id(n)[..],
+ &id(session)[..],
+ &id(1)[..],
+ &id(project)[..],
+ kind,
+ at
+ ],
+ )
+ .unwrap();
+}
+
+/// The recovery candidate in `proj-a`: its session byte and last activity.
+async fn interrupted(store: &Store, filter: OwnerFilter) -> Option<(u8, String)> {
+ store
+ .reader
+ .latest_interrupted_session_candidate(ws(), proj_a(), filter, None)
+ .await
+ .unwrap()
+ .map(|candidate| {
+ (
+ candidate.session_id.as_bytes()[15],
+ candidate.last_activity_at,
+ )
+ })
+}
+
+/// Recovery offers the newest session whose substantive work nothing covers.
+/// Activity counts every event, eligibility only prompts and tool calls: the
+/// open 35 holds the newest events but no work, 31 ended after its work, 32
+/// never worked, 33 is bob's and 34 works in another project.
+#[tokio::test]
+async fn interrupted_candidate_is_the_newest_uncovered_substantive_work() {
+ let (_tmp, store) = open_seeded();
+ let conn = Connection::open(store.db_path()).unwrap();
+ conn.execute(
+ "INSERT INTO sessions (id, workspace_id, project_id, agent_kind, cwd, started_at) \
+ VALUES (?1, ?2, ?3, 'claude-code', '/w/a', ?4)",
+ params![&id(35)[..], &id(1)[..], &id(2)[..], NOW + 700],
+ )
+ .unwrap();
+ observe(&conn, 80, 35, 2, "session-start", NOW + 700);
+ observe(&conn, 81, 35, 2, "stop", NOW + 710);
+ observe(&conn, 82, 30, 2, "stop", NOW + 520);
+ assert_eq!(
+ interrupted(&store, OwnerFilter::Unattributed).await,
+ Some((30, ts(NOW + 520).to_string()))
+ );
+ for filter in [filter_for("bob"), OwnerFilter::Any] {
+ assert_eq!(interrupted(&store, filter).await.map(|c| c.0), Some(33));
+ }
+
+ // An end after 30's newest work covers it; the stop that follows is not work.
+ conn.execute(
+ "UPDATE sessions SET ended_at = ?1 WHERE id = ?2",
+ params![NOW + 510, &id(30)[..]],
+ )
+ .unwrap();
+ assert_eq!(interrupted(&store, OwnerFilter::Unattributed).await, None);
+}
+
+/// A handoff from the session written at or after its newest work covers it,
+/// even when lifecycle events follow; one written before does not.
+#[tokio::test]
+async fn a_handoff_at_the_newest_work_covers_the_interrupted_candidate() {
+ let (_tmp, store) = open_seeded();
+ let conn = Connection::open(store.db_path()).unwrap();
+ observe(&conn, 82, 30, 2, "stop", NOW + 520);
+ let baton = |n: u8, at: i64| {
+ conn.execute(
+ "INSERT INTO handoffs \
+ (id, workspace_id, project_id, from_session_id, from_agent, summary, \
+ open_questions, next_steps, files_touched, state, created_at) \
+ VALUES (?1, ?2, ?3, ?4, 'other', 's', '[]', '[]', '[]', 'accepted', ?5)",
+ params![&id(n)[..], &id(1)[..], &id(2)[..], &id(30)[..], at],
+ )
+ .unwrap();
+ };
+ baton(60, NOW + 499);
+ assert_eq!(
+ interrupted(&store, OwnerFilter::Unattributed)
+ .await
+ .map(|c| c.0),
+ Some(30),
+ "a baton before the newest work leaves it uncovered"
+ );
+ baton(61, NOW + 500);
+ assert_eq!(interrupted(&store, OwnerFilter::Unattributed).await, None);
+}
diff --git a/docs/security-boundaries.md b/docs/security-boundaries.md
index 6e86a21a..f2b695d9 100644
--- a/docs/security-boundaries.md
+++ b/docs/security-boundaries.md
@@ -167,7 +167,7 @@ the legitimate restricted-project control and the failed-query cases.
| 27b | LLM workload telemetry cannot turn prompts, outputs, scopes, projects, or caller text into status data or unbounded metric labels | `ai-memory-llm/src/types.rs` `LlmWorkload` is a closed enum carried beside, but absent from the display/wire form of, `LlmOperationId`; `health.rs` `LlmWorkloadHealth` uses one fixed array and fixed latency buckets and records only counts/times/coarse estimates/reported usage/redacted error class and status; `ProviderHealth::snapshot` omits the rows when LLM support is disabled; `/admin/status` remains behind the common admin gate | `ai-memory-llm` `workload_snapshot_is_bounded_redacted_and_percentiled` (private prompt, output and upstream body absent; fixed row count, recovery-preserved last error, and percentile controls), `llm_wrapper_forwards_the_callers_operation_id` (label recorded while the same UUID-only id reaches the provider), `types::tests::workload_labels_are_stable_and_do_not_change_the_wire_operation_id`; `ai-memory-mcp` `status_reports_bounded_content_free_llm_workload_telemetry` (private prompt absent from serialized status, one fixed row per label), `status_reports_provider_health_block` (disabled LLM emits no rows) | STRONG for content/cardinality and the existing root-only admin surface |
| 27c | A maintenance compaction cannot replace a newer user page or partly apply a stale batch | `ai-memory-consolidate/src/sweep.rs` carries each selected source `PageId` into `Wiki::apply_batch_if`; `ai-memory-wiki/src/wiki.rs` checks the preconditions after locking every page, installs files with rollback snapshots, and delegates the decisive check to `WriterHandle::upsert_pages_batch_if`; `ai-memory-store/src/ops.rs` validates every latest id and writes every replacement in one SQLite transaction, returning a mismatch before any write | store `conditional_page_batch_is_all_or_nothing`; wiki `conditional_batch_rejects_every_page_when_one_version_changed` (file and DB controls); consolidate `compaction_does_not_supersede_a_concurrent_fresh_write` (an admission barrier lands a semantic pinned correction after A2 reads the old body, then proves the correction remains latest and the selected batch stays uncommitted) | STRONG; all-or-nothing intentionally retries unchanged peers with the next sweep instead of weakening invariant #2 into per-page writes |
| 28 | An Antigravity hook reads, and uploads as prompts, only the named conversation's own transcript, within bounds | `ai-memory-hooks/src/antigravity.rs` `antigravity_transcript_path` accepts the payload's `transcriptPath` only when it is absolute, has no `..`, names `transcript.jsonl` or `transcript_full.jsonl`, and its canonical directory equals `canonical(<home>/.gemini/<app>/brain)/<conversationId>/.system_generated/logs` for `<app>` `antigravity`, `antigravity-cli` or `antigravity-ide`, with `conversationId` a plain id (`antigravity_conversation_id`); `read_transcript_prompts` opens only a regular, non-symlink file that is still the same file once open (`open_regular_file`, opened with `O_NOFOLLOW | O_NONBLOCK` so a symlink or FIFO swapped in after the check is refused without blocking, and on Linux only when the opened descriptor's `/proc/self/fd` path is the checked one, so a directory swapped for a symlink after the containment check is refused too), reads at most `MAX_TRANSCRIPT_SCAN_BYTES` per hook and parses lines up to `MAX_TRANSCRIPT_LINE_BYTES`, and returns only `USER_INPUT` steps whose `source` is absent or `USER_EXPLICIT`; `ai-memory-cli/src/commands/hook.rs` `antigravity_transcript_prompt_events` runs inside the spool block, so after the capture allowlist, routing, server-profile and external-capture gates, with the home `run_with_payload` resolved once; its path checks, bounded cursor load and transcript read run behind `ANTIGRAVITY_TRANSCRIPT_READ_BUDGET`, and a timeout advances no cursor so a later hook retries; cursor replacement uses the canonical atomic writer; each prompt is capped and scrubbed client-side, keyed by conversation and step, and sanitized again by the server (row 7) | `antigravity.rs` `transcript_path_refuses_anything_outside_the_conversations_own_logs` (another conversation's transcript, the same layout outside the home, another file in the logs directory, an unknown app directory, `..`, a traversal or empty id, a relative path, a missing or non-string path; the conversation's own transcript is the control), `transcript_path_refuses_symlinked_directories_that_leave_the_conversation` (a symlinked `.system_generated` and `brain/<id>` refused; a symlinked `~/.gemini` accepted as the control), `transcript_reader_refuses_symlinks_and_special_files` (a symlinked transcript and a directory refused; the same bytes as a regular file are read), `skips_injected_malformed_empty_and_non_prompt_steps`, `a_line_longer_than_the_scan_budget_is_skipped_across_reads`, `one_read_returns_a_bounded_number_of_prompts`; `hook.rs` `antigravity_transcript_reads_are_confined_to_the_conversations_own_logs` (end to end through the native hook: a forged path outside the home, another conversation's transcript and an arbitrary JSONL file under the home spool no prompt; the conversation's own transcript spools one), `antigravity_transcript_prompts_follow_the_capture_allowlist` (a repository the allowlist leaves out is not read), `antigravity_transcript_read_deadline_does_not_wait_for_blocked_work`, and `antigravity_transcript_cursor_store_is_atomic_and_bounded` (oversized cursor refused) | STRONG on Linux, including the opened descriptor path. Stable symlink escapes and special files are refused on every platform; without Linux `/proc/self/fd`, a same-user process able to replace an ancestor in the narrow interval between canonicalization and open could redirect the read, which still remains size- and time-bounded. The directory-swap bite test is Linux-only; the other symlink cases are `cfg(unix)` |
-| 29 | Interrupted-session recovery stays scope-, owner-, and receiver-bound | `ReaderPool::latest_interrupted_session_candidate` filters before selection; hooks render only the admitted candidate and exclude the receiving session | `multi_session.rs` `interrupted_recovery_refuses_foreign_scope_owner_and_receiver_with_controls`; hooks `unfinished_session_recovery_is_owner_scoped_and_excludes_receiver` | STRONG |
+| 29 | Interrupted-session recovery stays scope-, owner-, and receiver-bound | `ReaderPool::latest_interrupted_session_candidate` (`latest_interrupted_session_candidate_sql`) filters scope, owner and receiver before selection, and judges a session's work, end and handoff coverage only from rows in the same scope; hooks render only the admitted candidate and exclude the receiving session | `multi_session.rs` `interrupted_recovery_refuses_foreign_scope_owner_and_receiver_with_controls`, `interrupted_recovery_ignores_foreign_scope_work_and_batons_with_controls` (foreign work does not reopen an ended session and a foreign baton does not hide unfinished work; home-scope work and batons are the controls); hooks `unfinished_session_recovery_is_owner_scoped_and_excludes_receiver` | STRONG |
| 30 | Per-response usage remains project/session/owner scoped and replay-monotonic | `ReaderPool::token_usage_report_page` applies scope, owner, search, and signal filters before pagination; `ops::upsert_token_calls` keys native response ids by session and merges counters with maxima in one writer transaction | `live_token_usage.rs` scope, owner, pagination, replay, and mutation controls | STRONG |
| 30a | Session drilldowns are owner-scoped for a user and complete for root | `ai-memory-web/src/routes/api.rs` `session_owner_filter_for`: `AuthLevel::Root` → `OwnerFilter::Any`, every other caller → `owner_filter_for` (own plus shared). Used by `routes/agents.rs`, `routes/project.rs` (sessions, agent and token-usage tabs), `routes/session.rs`, and the `/api/v1` `sessions_handler` and `session_observations_handler`. Handoff reads keep `owner_filter_for` and root's explicit `all_owners` | `ai-memory-web` `operations.rs` `operations_root_drilldowns_reach_every_users_sessions_and_users_stay_owned`: a user's agent page, Sessions tab and session list omit another user's session and its session page and observations answer 404; root's list it and answer 200 (control: the user's own and the shared session stay visible) | STRONG. Bite checks: with root mapped to `owner_filter_for`, root's agent page misses the other user's session; with every caller mapped to `Any`, the user's agent page lists it |
| 31 | Browser inbox operations hide restricted projects and preserve both-coordinate write authorization | Inbox and notification routes derive choices and counts from viewer-filtered projects; sends and edits require write access to the recipient and sender coordinates, sanitization, admission, CSRF, and fixed capabilities | `operations.rs` restricted-project, foreign-workspace, authorization, and notification tests; dashboard browser mutation cases | STRONG |