Skip to content

Commit 21068a4

Browse files
feat(storage): add get_observations_with_empty_metadata query
NULL-safe query for observations with empty facts, concepts, and keywords. Used by backfill-metadata CLI command. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-opencode) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
1 parent 17b4574 commit 21068a4

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

crates/storage/src/pg_storage/observations.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,4 +402,25 @@ impl ObservationStore for PgStorage {
402402
}
403403
Ok(())
404404
}
405+
406+
async fn get_observations_with_empty_metadata(
407+
&self,
408+
limit: usize,
409+
) -> Result<Vec<Observation>, StorageError> {
410+
let rows = sqlx::query(&format!(
411+
"SELECT {} \
412+
FROM observations \
413+
WHERE (facts IS NULL OR jsonb_array_length(facts) = 0) \
414+
AND (concepts IS NULL OR jsonb_array_length(concepts) = 0) \
415+
AND (keywords IS NULL OR jsonb_array_length(keywords) = 0) \
416+
ORDER BY created_at DESC, id DESC LIMIT $1",
417+
super::OBSERVATION_COLUMNS
418+
))
419+
.bind(usize_to_i64(limit))
420+
.fetch_all(&self.pool)
421+
.await?;
422+
Ok(collect_skipping_corrupt(
423+
rows.iter().map(row_to_observation),
424+
))
425+
}
405426
}

crates/storage/src/traits/observation.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,11 @@ pub trait ObservationStore: Send + Sync {
7575
id: &str,
7676
metadata: &ObservationMetadata,
7777
) -> Result<(), StorageError>;
78+
79+
/// Get observations that have empty metadata (facts, concepts, keywords all empty).
80+
/// Used by the backfill-metadata CLI command to find observations needing enrichment.
81+
async fn get_observations_with_empty_metadata(
82+
&self,
83+
limit: usize,
84+
) -> Result<Vec<Observation>, StorageError>;
7885
}

0 commit comments

Comments
 (0)