Skip to content

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

Rust

fix(consolidate): run the cross-scope link guard on dream-pass merges

Rudy Mizrahi Celeklia6380b637241
diff --git a/crates/ai-memory-consolidate/src/dream.rs b/crates/ai-memory-consolidate/src/dream.rs
index bb668386..30531250 100644
--- a/crates/ai-memory-consolidate/src/dream.rs
+++ b/crates/ai-memory-consolidate/src/dream.rs
@@ -530,7 +530,7 @@ pub async fn run_dream_pass(
report.cancelled = true;
break;
}
- match merge_one_cluster(wiki, llm, workspace_id, project_id, &plan).await {
+ match merge_one_cluster(wiki, reader, llm, workspace_id, project_id, &plan).await {
MergeOutcome::Applied {
new_survivor_id,
superseded,
@@ -571,6 +571,7 @@ enum MergeOutcome {
/// skips the cluster without touching a source.
async fn merge_one_cluster(
wiki: &Wiki,
+ reader: &ReaderPool,
llm: &(dyn LlmProvider + 'static),
workspace_id: WorkspaceId,
project_id: ProjectId,
@@ -676,11 +677,44 @@ async fn merge_one_cluster(
source_id: format!("b2_dream:{}", loser.id),
});
}
+ // #1218: a cross-scope link the model invented must not be written. The
+ // member bodies are injected into the merge prompt verbatim, so a
+ // `[[project:path]]` from a source page — or one the model simply made up —
+ // reaches the survivor. Resolve each against the store and de-link what
+ // does not exist, as the consolidator and bootstrap writes already do;
+ // otherwise the merge mints a dangling cross-project edge that
+ // `dangling_cross_project_links` reports on every later lint run and
+ // nothing in the pipeline can clear.
+ let mut survivor_fm = serde_json::Value::Object(survivor_fm);
+ match crate::link_guard::unresolved_generated_links(
+ reader,
+ workspace_id,
+ project_id,
+ &[(&survivor.path, survivor_body.as_str(), &survivor_fm)],
+ )
+ .await
+ {
+ Ok(unresolved) => {
+ survivor_body =
+ crate::link_guard::delink_body(&survivor_body, &survivor.path, &unresolved.body)
+ .into_owned();
+ crate::link_guard::prune_relations(&mut survivor_fm, &unresolved.relations);
+ }
+ Err(error) => {
+ tracing::warn!(
+ path = %survivor.path.as_str(),
+ %error,
+ "dream pass: cross-scope link resolution failed; skipping cluster (retried next run)"
+ );
+ return MergeOutcome::Skipped;
+ }
+ }
+
requests.push(WritePageRequest {
workspace_id,
project_id,
path: survivor.path.clone(),
- frontmatter: serde_json::Value::Object(survivor_fm),
+ frontmatter: survivor_fm,
body: survivor_body,
tier: Tier::Episodic,
pinned: false,
diff --git a/crates/ai-memory-consolidate/tests/suite/dream_pass.rs b/crates/ai-memory-consolidate/tests/suite/dream_pass.rs
index fe4aa6e8..3c511af7 100644
--- a/crates/ai-memory-consolidate/tests/suite/dream_pass.rs
+++ b/crates/ai-memory-consolidate/tests/suite/dream_pass.rs
@@ -573,3 +573,109 @@ async fn cancel_stops_before_next_cluster() {
"only the first cluster was written (survivor + 1 loser); the rest is untouched"
);
}
+
+/// Fake provider whose merged body carries a cross-scope wikilink into a
+/// project that does not exist. The merge prompt forbids inventing one, but a
+/// prompt is not a guarantee (#1218), and the member bodies are injected
+/// verbatim, so a `[[project:path]]` reaches the survivor either way.
+struct FakeGhostLinkLlm;
+
+impl LlmProvider for FakeGhostLinkLlm {
+ fn name(&self) -> &'static str {
+ "fake-ghost"
+ }
+ fn model(&self) -> &str {
+ "dream-merge"
+ }
+ fn complete<'life0, 'async_trait>(
+ &'life0 self,
+ _request: ChatRequest,
+ ) -> Pin<Box<dyn Future<Output = LlmResult<ChatResponse>> + Send + 'async_trait>>
+ where
+ 'life0: 'async_trait,
+ Self: 'async_trait,
+ {
+ Box::pin(async move {
+ Ok(ChatResponse {
+ text: "unused".into(),
+ usage: None,
+ model: "dream-merge".into(),
+ })
+ })
+ }
+ fn complete_structured_raw<'life0, 'async_trait>(
+ &'life0 self,
+ _request: ChatRequest,
+ _schema: serde_json::Value,
+ ) -> Pin<Box<dyn Future<Output = LlmResult<serde_json::Value>> + Send + 'async_trait>>
+ where
+ 'life0: 'async_trait,
+ Self: 'async_trait,
+ {
+ Box::pin(async move {
+ Ok(serde_json::json!({
+ "title": "Dream Merge",
+ "body_markdown": "Dream-merged coherent page covering \
+ crates/ai-memory-store/src/ops.rs and \
+ crates/ai-memory-store/src/reader.rs. \
+ See [[ghost:notes/missing.md]] for the rest.",
+ }))
+ })
+ }
+}
+
+/// #1218: a cross-scope link in the merged body must be de-linked before the
+/// survivor is written, exactly as the consolidator and bootstrap writes
+/// already do. Otherwise the dream pass mints a dangling cross-project edge
+/// that `dangling_cross_project_links` reports on every later lint run and
+/// nothing in the pipeline can clear.
+#[tokio::test]
+async fn dream_merge_delinks_an_unresolvable_cross_scope_link() {
+ let fx = seed_fixture(true).await;
+ write(&fx, "sessions/a.md", DUP_A).await;
+ write(&fx, "sessions/b.md", DUP_B).await;
+ for p in ["sessions/a.md", "sessions/b.md"] {
+ backdate_latest(&fx, p, COLD_DAYS);
+ }
+
+ let report = run_dream_pass(
+ &fx.store.reader,
+ &fx.wiki,
+ Some(&FakeGhostLinkLlm),
+ fx.ws,
+ fx.proj,
+ &DecayParams::default(),
+ 0.0,
+ &dream_on(),
+ &DreamCancel::new(),
+ false,
+ )
+ .await
+ .unwrap();
+
+ assert_eq!(report.clusters_merged, 1, "the merge applied: {report:?}");
+ assert_eq!(report.skipped, 0, "the guard de-links, it does not skip");
+
+ // The label survives as prose; the brackets — and the edge — do not.
+ let survivor_body = latest_body(&fx, &report.merges[0].survivor);
+ assert!(
+ survivor_body.contains("ghost:notes/missing.md"),
+ "the label stays readable: {survivor_body}"
+ );
+ assert!(
+ !survivor_body.contains("[["),
+ "the unresolvable link is de-linked: {survivor_body}"
+ );
+
+ // And the lint pass has nothing to report.
+ let dangling = fx
+ .store
+ .reader
+ .dangling_cross_project_links(fx.ws, fx.proj)
+ .await
+ .unwrap();
+ assert!(
+ dangling.is_empty(),
+ "no dangling cross-project edge was minted: {dangling:?}"
+ );
+}