Skip to content

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

feat(task_sources): add document store for task sources

Steven Enamakel77cf5e4e784e
diff --git a/crates/openhuman-core/src/integrations/task_sources/store.rs b/crates/openhuman-core/src/integrations/task_sources/store.rs
index f8131f31ad..866040a52c 100644
--- a/crates/openhuman-core/src/integrations/task_sources/store.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/store.rs
@@ -89,8 +89,6 @@ pub fn add_source(
let now = Utc::now();
let filter_json = serde_json::to_string(&filter).context("serialize task source filter")?;
let target_json = serde_json::to_string(&target).context("serialize task source target")?;
- let interval_i64 = i64::try_from(interval_secs)
- .context("task source interval_secs exceeds SQLite INTEGER range")?;
if let Some(docs) = super::store_documents::current()? {
return docs.add_source(&TaskSource {
@@ -109,6 +107,9 @@ pub fn add_source(
});
}
+ // SQLite-only bound: the document store holds the full `u64`.
+ let interval_i64 = i64::try_from(interval_secs)
+ .context("task source interval_secs exceeds SQLite INTEGER range")?;
with_connection(config, |conn| {
conn.execute(
"INSERT INTO task_sources (
diff --git a/crates/openhuman-core/src/integrations/task_sources/store_documents.rs b/crates/openhuman-core/src/integrations/task_sources/store_documents.rs
index 57c966b5c2..2a3f92eaec 100644
--- a/crates/openhuman-core/src/integrations/task_sources/store_documents.rs
+++ b/crates/openhuman-core/src/integrations/task_sources/store_documents.rs
@@ -28,7 +28,7 @@ use super::types::{
};
use crate::integrations::composio::providers::NormalizedTask;
use crate::storage::documents::{compare_and_swap, text, Repo};
-use crate::storage::DocumentStoreExt;
+use crate::storage::{DocumentStoreExt, StorageError};
const SOURCES: &str = "task_sources";
const INGESTED: &str = "ingested_tasks";
@@ -193,10 +193,12 @@ impl Docs {
pub(super) fn remove_source(&self, id: &str) -> Result<()> {
let key = id.to_string();
let removed = self.0.run(|docs| async move {
- let removed = docs.delete(SOURCES, &key, Precondition::None).await?;
- docs.delete_where(INGESTED, &Filter::eq("source_id", key))
+ // Ledger first: if the source delete then fails, the source is
+ // still there (and merely re-ingests), instead of a ledger with no
+ // owner that a later source with the same id would inherit.
+ docs.delete_where(INGESTED, &Filter::eq("source_id", key.clone()))
.await?;
- Ok(removed)
+ docs.delete(SOURCES, &key, Precondition::None).await
})?;
if removed {
Ok(())
@@ -249,6 +251,9 @@ impl Docs {
}
pub(super) fn mark_ingested(&self, source_id: &str, task: &NormalizedTask) -> Result<()> {
+ // The SQL ledger has a foreign key to the source; keep that parent
+ // check so a fetch racing a removal does not leave an orphan entry.
+ self.get_source(source_id)?;
let key = ingested_id(source_id, &task.external_id);
let now = Utc::now();
let doc = json!({
@@ -305,8 +310,21 @@ impl Docs {
Ok(page
.items
.iter()
- .filter_map(|stored| serde_json::from_str(text(&stored.doc, "payload")?).ok())
- .collect())
+ .map(|stored| {
+ let raw = text(&stored.doc, "payload").ok_or_else(|| {
+ StorageError::invalid(format!(
+ "ingested task {} has no payload",
+ stored.id
+ ))
+ })?;
+ serde_json::from_str(raw).map_err(|error| {
+ StorageError::invalid(format!(
+ "ingested task {} payload is not valid: {error}",
+ stored.id
+ ))
+ })
+ })
+ .collect::<Result<Vec<NormalizedTask>, StorageError>>()?)
})
}