The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(task-sources): validate full schema on cache hit + make init atomic
Maciej Myszkiewicz5fef63e68b29
diff --git a/src/openhuman/integrations/task_sources/store.rs b/src/openhuman/integrations/task_sources/store.rsindex 9b2e4db709..ab3514ddfd 100644--- a/src/openhuman/integrations/task_sources/store.rs+++ b/src/openhuman/integrations/task_sources/store.rs@@ -468,6 +468,13 @@ fn sql_conv<E: std::fmt::Display>(err: E) -> rusqlite::Error {/// after the first test to run in the process.static INITIALIZED_SCHEMAS: OnceLock<Mutex<HashSet<PathBuf>>> = OnceLock::new();+/// On-disk schema version stamped into `PRAGMA user_version` by [`init_schema`]+/// and checked by [`ensure_schema_initialized`] on a cache hit. **Bump this+/// whenever a table or `add_column_if_missing` migration is added to+/// [`init_schema`]** so a database replaced at runtime with an older/partial+/// schema is re-migrated rather than trusted.+const TASK_SOURCES_SCHEMA_VERSION: i64 = 1;+/// Runs the one-time schema DDL + migrations against `conn` unless `db_path`/// has already been initialized in this process (see [`INITIALIZED_SCHEMAS`])./// Only marks `db_path` as initialized *after* [`init_schema`] succeeds, so a@@ -485,37 +492,38 @@ static INITIALIZED_SCHEMAS: OnceLock<Mutex<HashSet<PathBuf>>> = OnceLock::new();/// self-healing rather than trusting a cache entry the filesystem may have/// invalidated.fn ensure_schema_initialized(conn: &Connection, db_path: &Path) -> Result<()> {- use rusqlite::OptionalExtension;-let initialized = INITIALIZED_SCHEMAS.get_or_init(|| Mutex::new(HashSet::new()));- {- let guard = initialized- .lock()- .unwrap_or_else(|poisoned| poisoned.into_inner());- if guard.contains(db_path) {- let schema_present: bool = conn- .query_row(- "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'task_sources'",- [],- |_| Ok(true),- )- .optional()- .context("Failed to probe task_sources schema presence")?- .unwrap_or(false);- if schema_present {- return Ok(());- }- tracing::warn!(- target: "task_sources",- db = %db_path.display(),- "[task_sources:store] schema cached as initialized but the database has no tables (deleted or replaced at runtime?) — re-running schema init"- );- }- }- init_schema(conn)?;+ // Hold the guard across the verify *and* `init_schema`, so two first callers+ // for the same path cannot both observe a miss and both run the DDL+ // (CodeRabbit: initialization must be atomic per path). On a cache hit the+ // critical section is a single `PRAGMA user_version` read; on a miss it also+ // covers the one-time init + insert.let mut guard = initialized.lock().unwrap_or_else(|poisoned| poisoned.into_inner());+ if guard.contains(db_path) {+ // Trust, but verify — against the *whole* schema, not just one table.+ // `user_version` is stamped only after a full `init_schema` succeeds, so+ // a database deleted (fresh file ⇒ version 0) or replaced at runtime+ // with an older/partial schema — missing `ingested_tasks` or a migrated+ // column ⇒ a stale version — fails this check and is re-migrated, rather+ // than being trusted and later failing on the incomplete schema+ // (CodeRabbit / Codex review on #5709).+ let version: i64 = conn+ .query_row("PRAGMA user_version", [], |row| row.get(0))+ .unwrap_or(0);+ if version == TASK_SOURCES_SCHEMA_VERSION {+ return Ok(());+ }+ tracing::warn!(+ target: "task_sources",+ db = %db_path.display(),+ cached_version = version,+ expected_version = TASK_SOURCES_SCHEMA_VERSION,+ "[task_sources:store] schema cached as initialized but the database's user_version does not match (deleted or replaced at runtime?) — re-running schema init"+ );+ }+ init_schema(conn)?;guard.insert(db_path.to_path_buf());Ok(())}@@ -563,6 +571,12 @@ fn init_schema(conn: &Connection) -> Result<()> {// G7: static executor routing on a source.add_column_if_missing(conn, "task_sources", "assigned_executor", "TEXT")?;+ // Stamp the schema version last, so [`ensure_schema_initialized`] only+ // trusts a cache hit whose on-disk schema is fully migrated. Bump+ // TASK_SOURCES_SCHEMA_VERSION whenever a table or migration is added above.+ conn.pragma_update(None, "user_version", TASK_SOURCES_SCHEMA_VERSION)+ .context("Failed to stamp task_sources schema version")?;+Ok(())}diff --git a/src/openhuman/integrations/task_sources/store_tests.rs b/src/openhuman/integrations/task_sources/store_tests.rsindex ae892a0e9f..b28b84aaf8 100644--- a/src/openhuman/integrations/task_sources/store_tests.rs+++ b/src/openhuman/integrations/task_sources/store_tests.rs@@ -448,3 +448,72 @@ fn schema_reinitializes_when_the_database_file_is_deleted_at_runtime() {assert_eq!(get_source(&config, &recreated.id).unwrap().id, recreated.id);assert_eq!(list_sources(&config).unwrap().len(), 1);}++/// Regression (CodeRabbit / Codex on #5709): a cache hit must be validated+/// against the *whole* schema, not just the presence of one table. A database+/// replaced at runtime with an older/partial schema — `task_sources` present but+/// a migrated column dropped — must be re-migrated, not trusted and then failed+/// on the incomplete schema. The `PRAGMA user_version` check is what catches it.+#[test]+fn older_on_disk_schema_under_a_cached_path_is_remigrated() {+ let tmp = TempDir::new().unwrap();+ let config = test_config(&tmp);++ // First use creates the full (versioned) schema and caches the path.+ let original = add_source(+ &config,+ ProviderSlug::Github,+ None,+ Some("v1".into()),+ github_filter(),+ 1800,+ SourceTarget::TodoOnly,+ 25,+ )+ .unwrap();++ // Simulate a workspace restore of an OLDER database swapped in under the+ // same (already-cached) path: drop a migrated column and clear the version+ // stamp, exactly as a pre-migration database would look on disk.+ let db_path = config.workspace_dir.join("task_sources").join("sources.db");+ {+ let raw = rusqlite::Connection::open(&db_path).unwrap();+ raw.execute_batch(+ "ALTER TABLE task_sources DROP COLUMN assigned_executor;+ PRAGMA user_version = 0;",+ )+ .unwrap();+ }++ // The path is still cached. With a single-table `sqlite_master` probe this+ // would be trusted and `list_sources` (which selects `assigned_executor`)+ // would fail with `no such column`. The version check detects the drift and+ // re-migrates instead.+ let listed = list_sources(&config)+ .expect("an older on-disk schema under a cached path must be re-migrated, not trusted");+ assert_eq!(+ listed.len(),+ 1,+ "the pre-existing row survives DROP COLUMN and the schema is repaired"+ );+ assert_eq!(listed[0].id, original.id);+ // The migrated column is back (reads as None for the pre-existing row).+ assert_eq!(+ get_source(&config, &original.id).unwrap().assigned_executor,+ None+ );++ // And the store is fully usable again.+ let src = add_source(+ &config,+ ProviderSlug::Github,+ None,+ Some("v2".into()),+ github_filter(),+ 1800,+ SourceTarget::TodoOnly,+ 25,+ )+ .unwrap();+ assert_eq!(get_source(&config, &src.id).unwrap().id, src.id);+}