Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(store): finish writer drop with a full queue
Samir Hanna Verza1db726902de0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e65cba04..69612e16 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -13,6 +13,10 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed+- Dropping the final store writer handle now finishes when its command queue+ is full. Accepted writes still drain before the writer thread exits, including+ when callers have cancelled their waits (#PR).+- Refused empty and whitespace-only `--workspace` and `--project` flags at theshared CLI scope-resolution choke point (`resolve_scope`,`resolve_scope_noting_marker_project`, `resolve_scope_with_marker`, anddiff --git a/crates/ai-memory-store/src/writer.rs b/crates/ai-memory-store/src/writer.rsindex 0558d76a..dbea4ae3 100644--- a/crates/ai-memory-store/src/writer.rs+++ b/crates/ai-memory-store/src/writer.rs@@ -859,8 +859,10 @@ pub struct WriterHandle {}struct WriterInner {- tx: mpsc::Sender<WriteCmd>,+ tx: Option<mpsc::Sender<WriteCmd>>,join: Mutex<Option<JoinHandle<()>>>,+ #[cfg(test)]+ before_join: Option<Box<dyn FnOnce() + Send + Sync>>,}impl WriterHandle {@@ -874,8 +876,10 @@ impl WriterHandle {Self {inner: Arc::new(WriterInner {- tx,+ tx: Some(tx),join: Mutex::new(Some(handle)),+ #[cfg(test)]+ before_join: None,}),}}@@ -2730,8 +2734,11 @@ impl WriterHandle {/// wedged-writer signal `status` surfaces (2.0 item 7).#[must_use]pub fn queue_depth(&self) -> (usize, usize) {- let max = self.inner.tx.max_capacity();- (max.saturating_sub(self.inner.tx.capacity()), max)+ let Some(tx) = self.inner.tx.as_ref() else {+ return (0, 0);+ };+ let max = tx.max_capacity();+ (max.saturating_sub(tx.capacity()), max)}/// Record a completed cross-session ("experience") pass for a scope.@@ -3667,6 +3674,8 @@ impl WriterHandle {async fn send(&self, cmd: WriteCmd) -> StoreResult<()> {self.inner.tx+ .as_ref()+ .ok_or(StoreError::WriterClosed)?.send(cmd).await.map_err(|_| StoreError::WriterClosed)@@ -3675,7 +3684,17 @@ impl WriterHandle {impl Drop for WriterInner {fn drop(&mut self) {- let _ = self.tx.try_send(WriteCmd::Shutdown);+ // A full mailbox cannot accept Shutdown. Drop the last sender before+ // joining so the worker still exits after draining accepted commands.+ // Keeping the sender alive here would leave it waiting for more work+ // forever while this thread waits for it to finish.+ if let Some(tx) = self.tx.take() {+ let _ = tx.try_send(WriteCmd::Shutdown);+ }+ #[cfg(test)]+ if let Some(before_join) = self.before_join.take() {+ before_join();+ }if let Some(handle) = self.join.lock().expect("writer join mutex poisoned").take() {let _ = handle.join();}@@ -5125,6 +5144,8 @@ mod tests {writer.inner.tx+ .as_ref()+ .unwrap().send(WriteCmd::GetOrCreateWorkspace {name: name.to_owned(),reply: tx,@@ -5288,6 +5309,103 @@ mod tests {}}+ #[test]+ fn drop_with_a_full_queue_completes_pending_commands_and_joins() {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let mut writer = store.writer.clone();+ let db_path = store.db_path().to_owned();+ drop(store);++ let (entered_tx, entered_rx) = std::sync::mpsc::channel();+ let (resume_tx, resume_rx) = std::sync::mpsc::channel();+ let (reply, blocker) = oneshot::channel();+ let blocked =+ writer+ .inner+ .tx+ .as_ref()+ .unwrap()+ .try_send(WriteCmd::EndIdleSessionsPreStamp {+ candidates: vec![IdleEndCandidate {+ session_id: SessionId::new(),+ workspace_id: WorkspaceId::new(),+ project_id: ProjectId::new(),+ agent_kind: AgentKind::Codex,+ action: ops::IdleEndAction::LifecycleOnly {+ now_us: 0,+ last_activity_us: 0,+ },+ }],+ enqueue_consolidation: false,+ pre_stamp: Box::new(move |_| {+ entered_tx.send(()).unwrap();+ resume_rx.recv().unwrap();+ }),+ reply,+ });+ assert!(blocked.is_ok());+ entered_rx.recv_timeout(Duration::from_secs(5)).unwrap();++ let mut pending = Vec::new();+ let capacity = writer.queue_depth().1;+ for i in 0..capacity {+ let (reply, rx) = oneshot::channel();+ assert!(+ writer+ .inner+ .tx+ .as_ref()+ .unwrap()+ .try_send(WriteCmd::GetOrCreateWorkspace {+ name: format!("full-drain-{i}"),+ reply,+ })+ .is_ok()+ );+ pending.push(rx);+ }+ assert_eq!(writer.queue_depth(), (capacity, capacity));+ let rescue = writer.inner.tx.as_ref().unwrap().downgrade();+ let (joining_tx, joining_rx) = std::sync::mpsc::channel();+ Arc::get_mut(&mut writer.inner).unwrap().before_join = Some(Box::new(move || {+ joining_tx.send(()).unwrap();+ }));+ let (done_tx, done_rx) = std::sync::mpsc::channel();+ let dropper = thread::spawn(move || {+ drop(writer);+ let _ = done_tx.send(());+ });+ // The writer cannot consume a queued command until Drop has attempted+ // shutdown against the full queue. No scheduling sleeps are needed.+ joining_rx.recv_timeout(Duration::from_secs(5)).unwrap();+ resume_tx.send(()).unwrap();+ let completed = done_rx.recv_timeout(Duration::from_secs(5)).is_ok();+ if !completed {+ // Rescue the old deadlock before failing, so a regression never+ // leaves the test suite waiting on a stuck writer thread.+ if let Some(sender) = rescue.upgrade() {+ let _ = sender.try_send(WriteCmd::Shutdown);+ }+ assert!(done_rx.recv_timeout(Duration::from_secs(5)).is_ok());+ }+ dropper.join().unwrap();+ blocker.blocking_recv().unwrap().unwrap();+ for rx in pending {+ rx.blocking_recv().unwrap().unwrap();+ }+ let conn = Connection::open(db_path).unwrap();+ let committed: usize = conn+ .query_row(+ "SELECT COUNT(*) FROM workspaces WHERE name LIKE 'full-drain-%'",+ [],+ |row| row.get(0),+ )+ .unwrap();+ assert_eq!(committed, capacity, "accepted commands must all commit");+ assert!(completed, "dropping a full writer queue must finish");+ }+/// The dry run and the real move both go through the actor; only the/// latter changes the session row a reader sees.#[tokio::test]@@ -5371,6 +5489,8 @@ mod tests {writer.inner.tx+ .as_ref()+ .unwrap().send(WriteCmd::Shutdown).await.expect("writer channel closed before Shutdown");@@ -5399,6 +5519,8 @@ mod tests {writer.inner.tx+ .as_ref()+ .unwrap().send(WriteCmd::Shutdown).await.expect("writer channel closed before Shutdown");diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.mdindex 05437ef1..a2ba204a 100644--- a/docs/ARCHITECTURE.md+++ b/docs/ARCHITECTURE.md@@ -1032,7 +1032,9 @@ that touch the relevant area.code that must read process or harness environment takes an injectablelookup so tests can stub it. (agentmemory #456 / #469.)2. **Single-writer SQLite actor.** All writes go through one `mpsc`- channel to one dedicated OS thread. (cognee #2717.)+ channel to one dedicated OS thread. Dropping the final writer handle+ drains accepted commands and joins that thread, even when the bounded+ queue is full or callers have cancelled their reply waits. (cognee #2717.)3. **Indexes commit in the same transaction as the data.** Nobackground-task-indexing-after-return. (basic-memory #763 / #578.)4. **Typed 3-tuple identity** (`workspace_id`, `project_id`, path)