Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(memory): drop the host's cached chunk-store handle when the module quarantines\n\nSeen live while testing #5820: the module quarantined and rebuilt a corrupt\n`chunks.db`, the sync reported the truth and the notice fired — but every\nin-process read (`sources::status`, which still runs on this process's own\nengine copy) kept failing with `database disk image is malformed`, because\nthe host's connection cache still pointed at the old inode. The module and\nthe host each hold their own cache.\n\n`RuntimeCallbacks::publish_event` now resets the in-process chunk store on\n`StoreCorruptQuarantined` before the event is mapped, via the engine's own\nrecovery entry point (init lock, drop cached handle, `quick_check` on the\nrebuilt file, reopen) — best-effort and logged. Unit test pins that it never\nquarantines a healthy or absent store.\n

Shanu43223737cffe
diff --git a/src/openhuman/memory/host_impls.rs b/src/openhuman/memory/host_impls.rs
index c65a561a1d..32f92f401b 100644
--- a/src/openhuman/memory/host_impls.rs
+++ b/src/openhuman/memory/host_impls.rs
@@ -471,6 +471,40 @@ pub fn install_memory_host_seams(config: Arc<Config>) {
/// overflows the 2 MiB test-thread stack. Building it on a thread with a stack
/// of its own keeps the cost off the caller entirely, and `Once` means it
/// happens exactly one time per test binary.
+/// Drop this process's cached handle to the chunk store and reopen it.
+///
+/// The loaded TinyMemory module and this process each embed their own copy of
+/// the engine, with their own connection cache. When the *module* quarantines a
+/// corrupt `chunks.db` it renames the file and rebuilds an empty one — but this
+/// process's cached connection still points at the old inode (now the
+/// `.corrupt-<ts>` copy), so every in-process read (`sources::status`, the
+/// session builder's recall) keeps failing with `database disk image is
+/// malformed` until restart. Seen live on openhuman#5820's fix: the module
+/// recovered, the host's status rows stayed red.
+///
+/// Delegates to the engine's own recovery entry point, which takes the
+/// per-path init lock, drops the cached connection, re-runs `quick_check` on
+/// what is on disk now (the rebuilt file, so no second quarantine), and
+/// reopens. Best-effort: a failure is logged, never propagated — the module's
+/// quarantine already happened and the notice is already on its way.
+pub(crate) fn reset_in_process_chunk_store(config: &Config) {
+ let engine = tinymemory_core::engine::memory_config_from(config, config.workspace_dir.clone());
+ match tinymemory_core::engine::backend::chunks::recover_corrupt_db(&engine) {
+ Ok(false) => log::info!(
+ "[memory:host] dropped the in-process chunk-store handle and reopened the \
+ rebuilt file after the module's quarantine"
+ ),
+ Ok(true) => log::warn!(
+ "[memory:host] the in-process chunk-store reset found the file corrupt as \
+ well and quarantined it"
+ ),
+ Err(error) => log::warn!(
+ "[memory:host] could not reset the in-process chunk-store handle after the \
+ module's quarantine; in-process reads may keep failing until restart: {error:#}"
+ ),
+ }
+}
+
#[cfg(test)]
pub(crate) fn install_for_tests() {
use std::sync::Once;
@@ -525,3 +559,30 @@ mod boot_seam_tests {
);
}
}
+
+#[cfg(test)]
+mod chunk_store_reset_tests {
+ use super::*;
+
+ /// The reset must be safe to run against a healthy (or absent) store: it
+ /// drops the cached handle and reopens without quarantining anything.
+ #[test]
+ fn reset_reopens_a_healthy_or_absent_store_without_quarantining() {
+ let tmp = tempfile::tempdir().unwrap();
+ let mut config = Config::default();
+ config.workspace_dir = tmp.path().join("workspace");
+ std::fs::create_dir_all(config.workspace_dir.join("memory_tree")).unwrap();
+
+ reset_in_process_chunk_store(&config);
+
+ let entries: Vec<String> = std::fs::read_dir(config.workspace_dir.join("memory_tree"))
+ .unwrap()
+ .filter_map(|e| e.ok())
+ .map(|e| e.file_name().to_string_lossy().into_owned())
+ .collect();
+ assert!(
+ !entries.iter().any(|name| name.contains(".corrupt-")),
+ "a healthy or absent store must never be quarantined by the reset: {entries:?}"
+ );
+ }
+}
diff --git a/src/openhuman/modules/memory_host.rs b/src/openhuman/modules/memory_host.rs
index f16e3c2257..8b460ae43c 100644
--- a/src/openhuman/modules/memory_host.rs
+++ b/src/openhuman/modules/memory_host.rs
@@ -313,6 +313,17 @@ struct RuntimeCallbacks(Arc<Config>);
#[tinybus::interface(name = "ai.tinyhumans.tinymemory.RuntimeHost")]
impl RuntimeCallbacks {
async fn publish_event(&self, event: MemoryEvent) -> tinybus::Result<()> {
+ // openhuman#5820: the module just renamed and rebuilt `chunks.db`;
+ // this process's own engine copy still holds a handle to the old
+ // inode, so drop it before anything in-process reads the store again.
+ if matches!(event, MemoryEvent::StoreCorruptQuarantined { .. }) {
+ let config = Arc::clone(&self.0);
+ tokio::task::spawn_blocking(move || {
+ crate::openhuman::memory::host_impls::reset_in_process_chunk_store(&config);
+ })
+ .await
+ .ok();
+ }
if let Some(event) = into_domain_event(event) {
BUS.publish(event);
}