Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
Merge feat/abrupt-switch-continuity into release/3.2
AkitaOnRailsef0fca99dc35
diff --cc crates/ai-memory-hooks/src/router.rsindex 0a315e56,e56585a6..be3f3326--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@@ -5371,52 -5473,195 +5485,241 @@@ pub(crate) mod tests);}+ /// A resumed session that had already ended may not take a baton+ /// (#840), but the refused claim must cost only the claim. Before, the+ /// refusal failed the whole fetch, so `claude --resume` of a session that+ /// had exited cleanly started with no on-start context at all (no profile+ /// digest, brief or inbox notice) whenever any baton was open.+ #[tokio::test]+ async fn a_resumed_ended_session_still_gets_its_session_start_context() {+ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ let resumed = "claude-ended-then-resumed";+ for event in ["session-start", "user-prompt", "session-end"] {+ process(+ &state,+ HookEnvelope::from_query_and_body(+ HookQuery {+ event: event.into(),+ agent: Some("claude-code".into()),+ ..Default::default()+ },+ serde_json::json!({ "session_id": resumed, "prompt": "pair the controller" }),+ ),+ None,+ Vec::new(),+ )+ .await+ .unwrap();+ }+ assert!(open_handoff_exists(&state).await, "the end left a baton");+ seed_inbox(&state, 1).await;++ let mut query = session_start_query(&tmp.path().to_string_lossy());+ query.session_id = Some(resumed.into());+ let body = fetch_and_accept_handoff(&state, query, None, Vec::new(), None)+ .await+ .unwrap()+ .unwrap_or_default();+ assert!(+ body.contains("1 cross-project message waiting"),+ "the rest of the on-start context must still arrive: {body:?}"+ );+ assert!(+ open_handoff_exists(&state).await,+ "the refused claim must leave the baton open"+ );+ }++ /// One lifecycle event from `agent`'s session `sid`, in the fixture's+ /// default project.+ fn lifecycle_event(agent: &str, event: &str, sid: &str, prompt: &str) -> HookEnvelope {+ HookEnvelope::from_query_and_body(+ HookQuery {+ event: event.into(),+ agent: Some(agent.into()),+ ..Default::default()+ },+ serde_json::json!({+ "session_id": sid,+ "prompt": prompt,+ "tool_name": "Bash",+ "tool_input": { "command": "ip -6 route" },+ "tool_response": { "stdout": "default via fe80::1" },+ }),+ )+ }++ /// An OpenCode session that prompted and ran a tool, then stopped with no+ /// session end: the shape a quota limit leaves.+ async fn cut_off_opencode_session(state: &HookState, sid: &str, actor: Option<IdentityKey>) {+ for event in ["session-start", "user-prompt", "post-tool-use"] {+ process(+ state,+ lifecycle_event("opencode", event, sid, "check the router IPv6"),+ actor.clone(),+ Vec::new(),+ )+ .await+ .unwrap();+ }+ }++ async fn session_start_body(+ state: &HookState,+ cwd: &str,+ receiver: &str,+ actor: Option<IdentityKey>,+ ) -> String {+ let mut query = session_start_query(cwd);+ query.session_id = Some(receiver.into());+ fetch_and_accept_handoff(state, query, actor, Vec::new(), None)+ .await+ .unwrap()+ .unwrap_or_default()+ }++ /// The next harness's session start names a session cut off mid-work:+ /// its agent and id (the v5 mapping of the native id), never its text.+ #[tokio::test]+ async fn session_start_names_a_session_cut_off_without_ending() {+ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ let cut_off = "ses_cut_off_by_quota";+ cut_off_opencode_session(&state, cut_off, None).await;++ let body = session_start_body(+ &state,+ &tmp.path().to_string_lossy(),+ "claude-new-session",+ None,+ )+ .await;+ let id = SessionId::from_native(cut_off).to_string();+ assert!(body.contains("🧭"), "the notice must be present: {body}");+ assert!(+ body.contains(&format!("`open-code` session `{id}`")),+ "{body}"+ );+ assert!(body.contains("memory_read_session_observations"), "{body}");+ assert!(+ body.contains(r#"workspace "default" and project "scratch""#),+ "{body}"+ );+ for captured in ["router IPv6", "ip -6 route", "fe80::1"] {+ assert!(+ !body.contains(captured),+ "the notice must not carry captured text ({captured:?}): {body}"+ );+ }+ }++ /// No notice when there is nothing a handoff misses: the other session+ /// ended cleanly, the receiver is that same session, or the receiver kept+ /// working after the cut-off.+ #[tokio::test]+ async fn session_start_names_no_session_when_the_latest_work_is_covered() {+ let cwd_of = |tmp: &TempDir| tmp.path().to_string_lossy().into_owned();++ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ cut_off_opencode_session(&state, "ses_clean", None).await;+ process(+ &state,+ lifecycle_event("opencode", "session-end", "ses_clean", ""),+ None,+ Vec::new(),+ )+ .await+ .unwrap();+ let body = session_start_body(&state, &cwd_of(&tmp), "claude-next", None).await;+ assert!(!body.contains("🧭"), "an ended session: {body}");++ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ cut_off_opencode_session(&state, "ses_resumed", None).await;+ let body = session_start_body(&state, &cwd_of(&tmp), "ses_resumed", None).await;+ assert!(!body.contains("🧭"), "the receiver's own session: {body}");++ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ cut_off_opencode_session(&state, "ses_earlier", None).await;+ process(+ &state,+ lifecycle_event("claude-code", "user-prompt", "claude-resumed", "later work"),+ None,+ Vec::new(),+ )+ .await+ .unwrap();+ let body = session_start_body(&state, &cwd_of(&tmp), "claude-resumed", None).await;+ assert!(!body.contains("🧭"), "the receiver worked after it: {body}");+ // Control: a fresh receiver in the same store still gets the pointer+ // to whichever other session is newest, here the resumed one.+ let body = session_start_body(&state, &cwd_of(&tmp), "codex-fresh", None).await;+ let resumed = SessionId::from_native("claude-resumed").to_string();+ assert!(body.contains(&resumed), "{body}");+ }++ /// The reported shape: the user resumes a Claude Code session that ended+ /// days ago (its own baton is still open, and an ended session may not+ /// claim it, #840) after another harness was cut off. The resumed session+ /// is pointed at the cut-off one, and its own baton stays open.+ #[tokio::test]+ async fn a_resumed_ended_session_is_pointed_at_a_later_cut_off_session() {+ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ let resumed = "claude-ended-then-resumed";+ for event in ["session-start", "user-prompt", "session-end"] {+ process(+ &state,+ lifecycle_event("claude-code", event, resumed, "pair the controller"),+ None,+ Vec::new(),+ )+ .await+ .unwrap();+ }+ assert!(open_handoff_exists(&state).await, "the end left a baton");+ cut_off_opencode_session(&state, "ses_after_the_end", None).await;++ let body = session_start_body(&state, &tmp.path().to_string_lossy(), resumed, None).await;+ assert!(+ body.contains(&SessionId::from_native("ses_after_the_end").to_string()),+ "the rest of the context must still arrive: {body}"+ );+ assert!(+ open_handoff_exists(&state).await,+ "the refused claim must leave the baton open"+ );+ }++ /// Adversarial: on a shared server one operator's cut-off session is+ /// never named to another operator, who would otherwise learn its id.+ #[tokio::test]+ async fn session_start_never_names_another_operators_cut_off_session() {+ let tmp = TempDir::new().unwrap();+ let state = make_state(&tmp).await;+ // Registered users make the server tell operators apart; without+ // them every session is shared by design (single-operator mode).+ for name in ["alice", "bob"] {+ user_holding(&state, name, state.project_id, None).await;+ }+ let bob = Some(IdentityKey::User("bob".into()));+ let alice = Some(IdentityKey::User("alice".into()));+ cut_off_opencode_session(&state, "ses_bob", bob.clone()).await;+ let cwd = tmp.path().to_string_lossy().into_owned();++ let body = session_start_body(&state, &cwd, "alice-session", alice).await;+ assert!(!body.contains("🧭"), "another operator's session: {body}");+ // Control: bob's own next harness is pointed at it.+ let body = session_start_body(&state, &cwd, "bob-next", bob).await;+ assert!(+ body.contains(&SessionId::from_native("ses_bob").to_string()),+ "{body}"+ );+ }+/// With an empty inbox, the on-start block emits no inbox notice at all —/// `render_inbox_notice(0)` returns `None`, so nothing is appended.#[tokio::test]