Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(run): preserve Claude turns before clear
AkitaOnRails762d8b48f45c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 676e0415..792aeb89 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -31,6 +31,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Imported every Claude transcript left behind by `/clear`, oldest first,+ before finalizing the managed run on the newest session. Pre-clear turns now+ retain their native-session provenance instead of disappearing from the+ workstream ledger (#1797).- Kept purge-session tombstones out of automatic hollow-project cleanup, soan old otherwise-empty project cannot lose replay protection immediatelyafter its session is purged (GHSA-pphw-hm98-3wh4).diff --git a/crates/ai-memory-cli/src/commands/run.rs b/crates/ai-memory-cli/src/commands/run.rsindex ac6ee713..728b6f7d 100644--- a/crates/ai-memory-cli/src/commands/run.rs+++ b/crates/ai-memory-cli/src/commands/run.rs@@ -18,8 +18,8 @@ use ai_memory_workstream::{LaunchPlan, LaunchRoots, ManagedHarness, NativeSessionCandidate, ai_jail_support,allows_native_session_adoption, apply_claude_true_yolo, apply_yolo, build_ai_jail_invocation,build_launch_plan, build_launch_plan_with_env_lookup, claude_continued_session,- claude_live_background_attach_id, claude_session_ran_in_background, clean_path,- crush_global_config_path, discover_native_session, export_transcript,+ claude_continued_sessions, claude_live_background_attach_id, claude_session_ran_in_background,+ clean_path, crush_global_config_path, discover_native_session, export_transcript,has_native_session_selector, inside_ai_jail_here, inspect_repository,is_interactive_session_invocation, jail_checklist, jail_toggle, kiro_explicit_session_id,kiro_harness_from_source_cursor, kiro_selects_non_default_engine, kiro_selects_v2_engine,@@ -1343,7 +1343,55 @@ async fn run_once_with_wiring(} else {None};- let (transcript, exact_export_available) = if plan.mode == LaunchMode::Session+ let left_behind_session_ids = if let (Some(own), Some(followed)) =+ (own_session_id.as_deref(), native_session_id.as_deref())+ {+ match claude_left_behind_session_ids(+ harness,+ &home,+ &repository.cwd,+ plan.session_dir.as_deref(),+ own,+ followed,+ started_at,+ ) {+ Ok(ids) => ids,+ Err(error) => {+ eprintln!(+ "ai-memory: could not enumerate Claude sessions left behind by continuation ({error:#}); only the followed session will be imported"+ );+ Vec::new()+ }+ }+ } else {+ Vec::new()+ };+ let mut left_behind_transcripts = Vec::new();+ let mut left_behind_losses = Vec::new();+ for session_id in &left_behind_session_ids {+ let source_cursor = (prepared.native_session_id.as_deref() == Some(session_id.as_str()))+ .then(|| prepared.source_cursor.as_deref())+ .flatten();+ let mut exported = export_after_flush(+ harness,+ &home,+ &repository.cwd,+ plan.session_dir.as_deref(),+ Some(session_id),+ source_cursor,+ )+ .await;+ left_behind_losses.extend(exported.losses.drain(..).map(|loss| {+ format!(+ "Claude session {} was left behind before continuation: {loss}",+ display_session_id(session_id)+ )+ }));+ if !exported.events.is_empty() {+ left_behind_transcripts.push(exported);+ }+ }+ let (mut transcript, exact_export_available) = if plan.mode == LaunchMode::Session&& harness.transcript_capability() == ai_memory_workstream::TranscriptCapability::Export{let exported = export_after_flush(@@ -1369,6 +1417,7 @@ async fn run_once_with_wiring(false,)};+ transcript.losses.extend(left_behind_losses);let recovery_event_digests =exact_export_available.then(|| transcript_interval_digests(&transcript.events));let recovery_cursor = exact_export_available@@ -1377,17 +1426,42 @@ async fn run_once_with_wiring(let checkpoint = inspect_repository(&repository.cwd).map(|identity| identity.checkpoint).unwrap_or(repository.checkpoint);- let imported = match import_batches(- &endpoint,- &run_path,- transcript,- checkpoint,- Some(exit_code),- &privacy,- &TranscriptCapture::for_checkout(&config.data_dir, &repository.cwd),- )- .await- {+ let capture = TranscriptCapture::for_checkout(&config.data_dir, &repository.cwd);+ let mut preliminary_imported = 0;+ let mut preliminary_error = None;+ for abandoned in left_behind_transcripts {+ match import_incomplete_batches(+ &endpoint,+ &run_path,+ abandoned,+ checkpoint.clone(),+ &privacy,+ &capture,+ )+ .await+ {+ Ok(imported) => preliminary_imported += imported,+ Err(error) => {+ preliminary_error = Some(error);+ break;+ }+ }+ }+ let import_result = match preliminary_error {+ Some(error) => Err(error),+ None => import_batches(+ &endpoint,+ &run_path,+ transcript,+ checkpoint,+ Some(exit_code),+ &privacy,+ &capture,+ )+ .await+ .map(|imported| preliminary_imported + imported),+ };+ let imported = match import_result {Ok(imported) => imported,// The child has already exited; an unreachable server must not turn// its exit code into an error. The transcript is not recorded, so say@@ -1405,6 +1479,16 @@ async fn run_once_with_wiring(&error,));+ if !left_behind_session_ids.is_empty() {+ eprintln!(+ "ai-memory: offline recovery records only the followed session; verify the left-behind Claude session(s) {} after the server returns",+ left_behind_session_ids+ .iter()+ .map(|id| display_session_id(id))+ .collect::<Vec<_>>()+ .join(", ")+ );+ }journal_finish_failed_run(config,&endpoint,@@ -2854,6 +2938,37 @@ fn follow_claude_continued_session(}}+#[allow(clippy::too_many_arguments)]+fn claude_left_behind_session_ids(+ harness: ManagedHarness,+ home: &Path,+ cwd: &Path,+ session_dir: Option<&Path>,+ own_session_id: &str,+ followed_session_id: &str,+ started_at: SystemTime,+) -> Result<Vec<String>> {+ if harness != ManagedHarness::Claude || own_session_id == followed_session_id {+ return Ok(Vec::new());+ }+ let continuations =+ claude_continued_sessions(home, cwd, session_dir, own_session_id, started_at)?;+ let mut sessions = vec![own_session_id.to_owned()];+ for continuation in continuations {+ let id = continuation.session_id();+ if id == followed_session_id {+ return Ok(sessions);+ }+ if !sessions.iter().any(|known| known == id) {+ sessions.push(id.to_owned());+ }+ }+ Err(anyhow!(+ "followed Claude session {} was absent from its continuation chain",+ display_session_id(followed_session_id)+ ))+}+#[allow(clippy::too_many_arguments)]async fn resolve_native_session_after_run(plan: &LaunchPlan,@@ -4250,6 +4365,40 @@ pub(crate) async fn import_batches(Ok(imported)}+async fn import_incomplete_batches(+ endpoint: &ServerEndpoint,+ run_path: &str,+ transcript: ExportedTranscript,+ checkpoint: ai_memory_core::WorkstreamCheckpoint,+ sanitizer: &Sanitizer,+ capture: &TranscriptCapture,+) -> Result<usize> {+ let native_identity = NativeSessionIdentity::parse(&transcript.native_session_id, sanitizer)?;+ for event in &transcript.events {+ NativeSessionIdentity::parse(&event.native_session_id, sanitizer)?;+ }+ let events = capture.filter_ledger_events(transcript.events);+ if events.is_empty() {+ return Ok(0);+ }+ let mut imported = 0;+ for batch in event_batches(events) {+ let request = FinishManagedRunRequest {+ native_session_id: Some(native_identity.as_str().to_owned()),+ source_cursor: None,+ events: batch,+ complete: false,+ checkpoint: checkpoint.clone(),+ losses: Vec::new(),+ exit_code: None,+ };+ imported += finish_with_retry(endpoint, run_path, &request)+ .await?+ .imported_events;+ }+ Ok(imported)+}+fn event_batches(events: Vec<ai_memory_core::NewWorkstreamEvent>,) -> Vec<Vec<ai_memory_core::NewWorkstreamEvent>> {@@ -10385,6 +10534,71 @@ mod tests {server.abort();}+ #[tokio::test]+ async fn abandoned_session_batches_do_not_finalize_the_followed_run() {+ let requests = Arc::new(std::sync::Mutex::new(Vec::<FinishManagedRunRequest>::new()));+ let captured = requests.clone();+ let app = Router::new().route(+ "/workstream/runs/{id}/finish",+ post(+ move |axum::Json(request): axum::Json<FinishManagedRunRequest>| {+ let captured = captured.clone();+ async move {+ let imported_events = request.events.len();+ captured.lock().unwrap().push(request);+ axum::Json(FinishManagedRunResponse {+ imported_events,+ latest_sequence: 1,+ })+ }+ },+ ),+ );+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let address = listener.local_addr().unwrap();+ let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });+ let home = tempfile::tempdir().unwrap();+ let config = launch_config(home.path(), home.path(), address);+ let endpoint = ServerEndpoint::from_config_resolving_auth(&config).await;+ let transcript = ExportedTranscript {+ native_session_id: "abandoned".into(),+ source_cursor: Some("must-not-advance".into()),+ events: vec![ai_memory_core::NewWorkstreamEvent {+ event_id: "abandoned-event".into(),+ agent: AgentKind::ClaudeCode,+ native_session_id: "abandoned".into(),+ source_record_id: None,+ kind: ai_memory_core::WorkstreamEventKind::Message,+ role: Some("user".into()),+ content: "before clear".into(),+ occurred_at: None,+ metadata: serde_json::json!({}),+ }],+ losses: vec!["reported on the final transcript".into()],+ };++ assert_eq!(+ import_incomplete_batches(+ &endpoint,+ "/workstream/runs/test",+ transcript,+ Default::default(),+ &Sanitizer::builtin(),+ &TranscriptCapture::for_checkout(home.path(), home.path()),+ )+ .await+ .unwrap(),+ 1+ );+ let request = &requests.lock().unwrap()[0];+ assert_eq!(request.native_session_id.as_deref(), Some("abandoned"));+ assert!(!request.complete);+ assert!(request.source_cursor.is_none());+ assert!(request.losses.is_empty());+ assert!(request.exit_code.is_none());+ server.abort();+ }+/// GHSA-gcpc-88hm-3xqr: the ledger upload applies the checkout's/// `[capture] ignore_paths`. An excluded read's call and output never reach/// the finish request (and so never a raw segment); a public read in thediff --git a/crates/ai-memory-workstream/src/lib.rs b/crates/ai-memory-workstream/src/lib.rsindex 8478d398..bf3ccb24 100644--- a/crates/ai-memory-workstream/src/lib.rs+++ b/crates/ai-memory-workstream/src/lib.rs@@ -27,8 +27,8 @@ pub use repository::{pub use transcript::{AmbiguousNativeSession, ClaudeContinuation, ExportedTranscript, NativeSessionCandidate,OVERSIZED_RECORD_LOSS, RECORD_CWD_KEY, SKIPPED_RECORD_KEY, claude_continued_session,- claude_live_background_attach_id, claude_session_ran_in_background, discover_native_session,- export_transcript, export_transcript_delta, export_transcript_range,+ claude_continued_sessions, claude_live_background_attach_id, claude_session_ran_in_background,+ discover_native_session, export_transcript, export_transcript_delta, export_transcript_range,kiro_harness_from_source_cursor, kiro_v3_resume_uses_default_store, list_native_sessions,native_memory_dir, native_session_exists, native_session_in_checkout, native_store_root,transcript_baseline, transcript_interval_digests, wait_for_transcript_flush,diff --git a/crates/ai-memory-workstream/src/transcript.rs b/crates/ai-memory-workstream/src/transcript.rsindex 44a540e1..0cb45934 100644--- a/crates/ai-memory-workstream/src/transcript.rs+++ b/crates/ai-memory-workstream/src/transcript.rs@@ -837,8 +837,21 @@ pub fn claude_continued_session(native_session_id: &str,started_at: SystemTime,) -> Result<Option<ClaudeContinuation>> {+ Ok(claude_continued_sessions(home, cwd, session_dir, native_session_id, started_at)?.pop())+}++/// Find every session a Claude Code process continued in during this run,+/// oldest first. This lets callers preserve turns from each transcript in a+/// chain of `/clear` commands while still following the newest session.+pub fn claude_continued_sessions(+ home: &Path,+ cwd: &Path,+ session_dir: Option<&Path>,+ native_session_id: &str,+ started_at: SystemTime,+) -> Result<Vec<ClaudeContinuation>> {if !valid_native_session_id(native_session_id) {- return Ok(None);+ return Ok(Vec::new());}let mut candidates = collect_session_files(ManagedHarness::Claude, home, session_dir)?;candidates.sort_by(|left, right| {@@ -847,7 +860,8 @@ pub fn claude_continued_session(.then_with(|| left.cmp(right))});let attached_marker = format!("\"session_id\":\"{native_session_id}\"");- for path in candidates.into_iter().take(512) {+ let mut continuations = Vec::new();+ 'candidate: for path in candidates.into_iter().take(512) {if modified(&path).is_some_and(|time| time + Duration::from_secs(2) < started_at) {break;}@@ -884,7 +898,8 @@ pub fn claude_continued_session(}if value.get("sessionKind").and_then(Value::as_str) == Some("bg") {if recorded_cwd.is_some_and(|recorded| same_path(recorded, cwd)) {- return Ok(Some(ClaudeContinuation::Background(session.to_string())));+ continuations.push(ClaudeContinuation::Background(session.to_string()));+ continue 'candidate;}} else if session == stem&& recorded_cwd.is_some_and(|recorded| path_within(recorded, cwd))@@ -893,10 +908,11 @@ pub fn claude_continued_session(}}if cleared && cleared_in_checkout {- return Ok(Some(ClaudeContinuation::Cleared(stem.to_string())));+ continuations.push(ClaudeContinuation::Cleared(stem.to_string()));}}- Ok(None)+ continuations.reverse();+ Ok(continuations)}const CLAUDE_CLEAR_COMMAND: &str = "<command-name>/clear</command-name>";@@ -8558,6 +8574,13 @@ mod tests {find("launch"),Some(ClaudeContinuation::Cleared("cleared-again".into())));+ assert_eq!(+ claude_continued_sessions(temp.path(), &cwd, None, "launch", started_at).unwrap(),+ vec![+ ClaudeContinuation::Cleared("cleared".into()),+ ClaudeContinuation::Cleared("cleared-again".into()),+ ]+ );}#[test]diff --git a/docs/managed-workstreams.md b/docs/managed-workstreams.mdindex c3a5d27d..f6a0b1b5 100644--- a/docs/managed-workstreams.md+++ b/docs/managed-workstreams.md@@ -541,7 +541,15 @@ selected explicitly on the next managed run.result whose call was imported by an earlier run is dropped under apolicy. Messages are unaffected. Under allowlist capture mode a checkoutwithout a marker uploads no events at all, as the live hook emits none- there; the run still finishes. Each delivered+ there; the run still finishes.++ Claude process can write one run across several native transcripts when+ `/clear` starts a new conversation. The launcher enumerates those+ continuations, imports each abandoned transcript oldest first as a+ non-final ledger segment, and only then finalizes the run on the newest+ session. Thus pre-clear turns keep their original native-session identity,+ while the next managed launch still resumes the conversation after the last+ `/clear`. Each deliveredworkstream packet begins with a versioned origin marker. If Claude Codepersists that packet and its `Read` tool returns it, the Claude transcriptnormalizer excludes the marked result instead of feeding delivered history