Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(handoff): scope ended-session reopen to session-id-reusing agents (#840)
AkitaOnRails0ba0e38df660
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c1740f3c..130c99c2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -46,9 +46,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0open for `memory_handoff_accept`. The note is clipped to 10,000 characters(Grok's own cap). Because Grok reuses one session id across aSessionEnd→restart, `memory_handoff_accept` now reopens an already-ended- receiver session (clears `ended_at`) instead of rejecting it — but only after- the exactly-once claim guard, so a session that already took a baton still- cannot take another (the multi-session claim-once invariant is preserved).+ receiver session (clears `ended_at`) instead of rejecting it — but only for+ agents that reuse their session id across a restart (Grok); for every other+ agent an ended session stays final, so a lifecycle-only receiver still cannot+ reclaim after it released and ended. The reopen also only happens after the+ exactly-once claim guard, so a session that already took a baton still cannot+ take another (the multi-session claim-once invariant is preserved).Delivery of this PostToolUse handoff is exempt from `AI_MEMORY_CAPTURE_OWNER`capture suppression, like the other context-delivery events. (#840)diff --git a/crates/ai-memory-core/src/ids.rs b/crates/ai-memory-core/src/ids.rsindex 5d3463d3..e5f449b8 100644--- a/crates/ai-memory-core/src/ids.rs+++ b/crates/ai-memory-core/src/ids.rs@@ -441,6 +441,21 @@ impl AgentKind {pub fn post_tool_injects_handoff(self) -> bool {matches!(self, Self::Grok)}++ /// Whether this agent reuses one session id across a `SessionEnd` and a+ /// later restart of the same conversation.+ ///+ /// Grok does: the same session id comes back after an end, so an already-+ /// ended receiver row is a live restart, not a corpse, and+ /// `accept_handoff` reopens it. For every other agent an ended session is+ /// final — a late/out-of-order startup fetch must not rebind it to a new+ /// handoff, so accepting into an ended session stays an error (this is what+ /// keeps a lifecycle-only receiver from reclaiming after it released and+ /// ended).+ #[must_use]+ pub fn reuses_session_id_after_end(self) -> bool {+ matches!(self, Self::Grok)+ }}#[cfg(test)]diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rsindex 324fc125..4307c582 100644--- a/crates/ai-memory-store/src/ops.rs+++ b/crates/ai-memory-store/src/ops.rs@@ -2960,13 +2960,23 @@ pub(crate) fn accept_handoff_in_transaction(return Ok(false);}if !open {- // Grok reuses the session id after SessionEnd when the same- // conversation restarts. The row is the receiver, not a corpse,- // as long as it has not already taken a baton.- tx.execute(- "UPDATE sessions SET ended_at = NULL WHERE id = ?1",- params![accepting_session.as_bytes()],- )?;+ if accepting_agent.reuses_session_id_after_end() {+ // Grok reuses the session id after SessionEnd when the same+ // conversation restarts. The row is the receiver, not a corpse,+ // as long as it has not already taken a baton (guarded above).+ tx.execute(+ "UPDATE sessions SET ended_at = NULL WHERE id = ?1",+ params![accepting_session.as_bytes()],+ )?;+ } else {+ // For every other agent an ended session is final: a late or+ // out-of-order startup fetch must not rebind it to a new+ // handoff (keeps a lifecycle-only receiver from reclaiming+ // after it released and ended).+ return Err(StoreError::InvalidState(+ "an ended session cannot accept a handoff".into(),+ ));+ }}}let metadata = tx