Skip to content

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

Rust

fix(backfill): keep dry runs from marking an import attempt

Samir Hanna Verza89f6de210016
diff --git a/CHANGELOG.md b/CHANGELOG.md
index aedb30ba..8e79a88d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
+- `backfill --dry-run` recorded a completed attempt and suppressed the next
+ automatic import. Planning now leaves the backfill sentinel untouched, even
+ for populated projects or an opted-out automatic invocation (#785).
- The generated OpenCode and OpenCode 2 plugins now forward a subagent session's
`parentID` as the `agent_id` marker, so `[capture] drop_subagent_captures` can
recognize and drop OpenCode subagent sessions. Previously both plugins emitted
diff --git a/crates/ai-memory-cli/src/commands/backfill.rs b/crates/ai-memory-cli/src/commands/backfill.rs
index 2529c094..4bacb8d1 100644
--- a/crates/ai-memory-cli/src/commands/backfill.rs
+++ b/crates/ai-memory-cli/src/commands/backfill.rs
@@ -157,7 +157,9 @@ pub async fn run(config: &Config, args: crate::cli::BackfillArgs) -> Result<()>
// that it has been attempted for this checkout, so it runs at most once per
// machine regardless of outcome. Manual runs ignore both.
if args.auto && !config.backfill_on_start {
- write_sentinel(&config.data_dir, &cwd);
+ if !args.dry_run {
+ write_sentinel(&config.data_dir, &cwd);
+ }
return Ok(());
}
@@ -180,7 +182,9 @@ pub async fn run(config: &Config, args: crate::cli::BackfillArgs) -> Result<()>
if !empty {
report.skipped_non_empty = true;
// Mark attempted so the auto-trigger stops probing this checkout.
- write_sentinel(&config.data_dir, &cwd);
+ if !args.dry_run {
+ write_sentinel(&config.data_dir, &cwd);
+ }
return finish(&args, &report);
}
}
@@ -191,7 +195,6 @@ pub async fn run(config: &Config, args: crate::cli::BackfillArgs) -> Result<()>
report.selected = selected.len();
if args.dry_run {
- write_sentinel(&config.data_dir, &cwd);
return finish(&args, &report);
}
diff --git a/crates/ai-memory-cli/tests/suite/backfill_dry_run.rs b/crates/ai-memory-cli/tests/suite/backfill_dry_run.rs
new file mode 100644
index 00000000..000f87bc
--- /dev/null
+++ b/crates/ai-memory-cli/tests/suite/backfill_dry_run.rs
@@ -0,0 +1,69 @@
+//! A planning-only backfill must not suppress the next automatic import.
+use axum::{Json, Router, routing::get};
+use serde_json::json;
+
+#[tokio::test]
+async fn dry_run_preserves_sentinel_for_empty_populated_and_opted_out_projects() {
+ for (sessions, auto) in [(0, false), (1, false), (0, true)] {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let addr = listener.local_addr().unwrap();
+ let app = Router::new().route(
+ "/admin/sessions/by-agent",
+ get(move || async move {
+ Json(json!({"by_agent": [{"agent": "claude-code", "sessions": sessions}]}))
+ }),
+ );
+ let server = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
+ let data = tempfile::tempdir().unwrap();
+ let home = tempfile::tempdir().unwrap();
+ let cwd = tempfile::tempdir().unwrap();
+ let state = data.path().join("backfill-state");
+ // Exercise both an absent marker and a marker from an earlier attempt.
+ for existing in [false, true] {
+ if existing {
+ std::fs::create_dir_all(&state).unwrap();
+ use sha2::{Digest, Sha256};
+ let canonical = std::fs::canonicalize(cwd.path()).unwrap();
+ let hash = Sha256::digest(canonical.as_os_str().as_encoded_bytes());
+ std::fs::write(state.join(format!("{hash:x}")), b"keep-existing").unwrap();
+ }
+ let mut cmd = crate::e2e_support::hermetic(env!("CARGO_BIN_EXE_ai-memory"));
+ cmd.current_dir(cwd.path())
+ .env("AI_MEMORY_HOME", home.path())
+ .env("AI_MEMORY_DATA_DIR", data.path())
+ .env("AI_MEMORY_SERVER_URL", format!("http://{addr}"))
+ .env("AI_MEMORY_BACKFILL_ON_START", "false")
+ .args([
+ "backfill",
+ "--workspace",
+ "review",
+ "--project",
+ "fixture",
+ "--dry-run",
+ ]);
+ if auto {
+ cmd.arg("--auto");
+ }
+ let output = tokio::process::Command::from(cmd).output().await.unwrap();
+ assert!(
+ output.status.success(),
+ "{}",
+ String::from_utf8_lossy(&output.stderr)
+ );
+ if existing {
+ let paths: Vec<_> = std::fs::read_dir(&state).unwrap().collect();
+ assert_eq!(paths.len(), 1);
+ assert_eq!(
+ std::fs::read(paths[0].as_ref().unwrap().path()).unwrap(),
+ b"keep-existing"
+ );
+ } else {
+ assert!(
+ !state.exists(),
+ "dry-run created backfill-state: sessions={sessions}, auto={auto}"
+ );
+ }
+ }
+ server.abort();
+ }
+}
diff --git a/crates/ai-memory-cli/tests/suite/main.rs b/crates/ai-memory-cli/tests/suite/main.rs
index 15a6e194..d6ee0e94 100644
--- a/crates/ai-memory-cli/tests/suite/main.rs
+++ b/crates/ai-memory-cli/tests/suite/main.rs
@@ -6,6 +6,7 @@
//! so a new file must be declared here (`scripts/check-test-suites.*` enforces it).
mod autoscope_env;
+mod backfill_dry_run;
mod backfill_e2e;
mod completions;
mod doctor_e2e;
diff --git a/docs/design-boot-backfill.md b/docs/design-boot-backfill.md
index 1daa335f..80fd3f9a 100644
--- a/docs/design-boot-backfill.md
+++ b/docs/design-boot-backfill.md
@@ -23,6 +23,9 @@ behavior and touches the trust boundary (retroactive untrusted text enters the s
re-spawning. Live hook capture (install-time forward) and backfill (before-install
history) do not overlap, so there is no double-capture to reconcile.
+`backfill --dry-run` only plans the import; it neither creates nor overwrites
+the local sentinel, so it does not suppress a later automatic attempt.
+
The automatic path runs detached and silent (like Claude Code's own auto-memory); the
`📼 imported N session(s)` summary is shown on a **manual** `ai-memory backfill` only.
Surfacing the count in the next session's on-start context is a possible follow-up