Skip to content

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

Rust

fix(workstream): retry renewable lease lapses

AkitaOnRails27d62d868c97
diff --git a/crates/ai-memory-store/src/error.rs b/crates/ai-memory-store/src/error.rs
index 5e6fa3e3..016bfe0a 100644
--- a/crates/ai-memory-store/src/error.rs
+++ b/crates/ai-memory-store/src/error.rs
@@ -182,6 +182,12 @@ pub enum StoreError {
#[error("ID-selected managed run boundary mismatch")]
ManagedRunBoundaryRefused,
+ /// An otherwise-valid ID-selected run is still active but its renewable
+ /// lease has lapsed. Unlike a terminal boundary refusal, a heartbeat may
+ /// make the same spooled event valid after a server outage.
+ #[error("ID-selected managed run lease lapsed")]
+ ManagedRunLeaseLapsed,
+
/// A workspace delete was refused because it still holds projects and the
/// caller did not pass `force`. Carries the project count so the admin
/// endpoint / CLI can report it before the operator retries with force.
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index 9e98fe6c..b0f1f719 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -6715,6 +6715,55 @@ mod tests {
);
}
+ #[tokio::test]
+ async fn a_lapsed_active_run_boundary_remains_retryable_after_heartbeat() {
+ let tmp = TempDir::new().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let (ws, proj) = open_managed_scope(&store, "managed-lapsed-boundary").await;
+ let mut input = managed_prepare_input(ws, proj, "test:lapsed-boundary");
+ input.scope_by_id = true;
+ let run = store.writer.prepare_workstream_run(input).await.unwrap();
+ let identity = ManagedRunHookIdentity {
+ run_id: run.run_id,
+ agent: AgentKind::Codex,
+ cwd: Some("/repo".into()),
+ native_session_id: "native-session".into(),
+ owner_user: None,
+ session_start: true,
+ };
+
+ set_managed_run_lease(store.db_path(), run.run_id, 1);
+ let err = store
+ .reader
+ .managed_run_pinned_scope(identity)
+ .await
+ .unwrap_err();
+ assert!(matches!(err, StoreError::ManagedRunLeaseLapsed));
+
+ assert!(
+ store
+ .writer
+ .heartbeat_managed_run(run.run_id)
+ .await
+ .unwrap()
+ );
+ assert_eq!(
+ store
+ .reader
+ .managed_run_pinned_scope(ManagedRunHookIdentity {
+ run_id: run.run_id,
+ agent: AgentKind::Codex,
+ cwd: Some("/repo".into()),
+ native_session_id: "native-session".into(),
+ owner_user: None,
+ session_start: true,
+ })
+ .await
+ .unwrap(),
+ Some((ws, proj))
+ );
+ }
+
#[tokio::test]
async fn finish_run_after_complete_is_an_idempotent_no_op() {
let tmp = TempDir::new().unwrap();
diff --git a/crates/ai-memory-store/src/workstream.rs b/crates/ai-memory-store/src/workstream.rs
index 166312af..d8144545 100644
--- a/crates/ai-memory-store/src/workstream.rs
+++ b/crates/ai-memory-store/src/workstream.rs
@@ -296,7 +296,6 @@ pub(crate) fn pinned_hook_scope(
let native = NativeSessionIdentity::parse(&input.native_session_id, &Sanitizer::builtin())
.map_err(|_| refused())?;
if run.state != "active"
- || expires <= Timestamp::now().as_microsecond()
|| run.agent != input.agent.as_str()
|| run.owner_user != input.owner_user
|| ((linked || delivered) && run.native_session.as_deref() != Some(native.as_str()))
@@ -304,6 +303,9 @@ pub(crate) fn pinned_hook_scope(
{
return Err(refused());
}
+ if expires <= Timestamp::now().as_microsecond() {
+ return Err(StoreError::ManagedRunLeaseLapsed);
+ }
Ok(Some((
WorkspaceId::from_slice(&run.workspace)?,
ProjectId::from_slice(&run.project)?,