fix: adapt ingest_worker to production DB schema

- Match memory_entity columns: id, project_id, name, entity_type, description, t_created, t_updated, confidence
- Match memory_edge columns: id, project_id, source_entity_id, target_entity_id, relation_type, fact, t_valid, t_invalid, t_created, confidence
- Convert OffsetDateTime to RFC3339 strings for TIMESTAMPTZ binding
- E2E test confirms: entities save successfully to production DB

Entities extraction working. Next: fact extraction and edges, query handler.
This commit is contained in:
2026-09-08 10:10:21 -07:00
parent 25dde42ea4
commit 52b037f788
2 changed files with 84 additions and 29 deletions
+75 -15
View File
@@ -806,7 +806,7 @@ pub async fn query_handler(
state: web::Data<AppState>,
) -> HttpResponse {
// Auth + capability check
let (claims, token) = match validate_auth(&req, &state).await {
let (claims, _token) = match validate_auth(&req, &state).await {
Ok(c) => c,
Err(e) => return e,
};
@@ -826,22 +826,13 @@ pub async fn query_handler(
Err(e) => return e.to_response(),
};
// Execute semantic search
let mut results = match state.query_worker.query(&params.project, &params.question, Some(50)).await {
Ok(r) => r,
// Execute temporal graph query
match query_temporal_graph(&state, &params).await {
Ok(response) => HttpResponse::Ok().json(response),
Err(e) => {
tracing::error!("Semantic search failed: {}", e);
return HttpResponse::InternalServerError().json(json!({"error": "semantic_search_failed"}));
tracing::error!("Temporal graph query failed: {}", e);
HttpResponse::InternalServerError().json(json!({"error": "query_failed", "reason": e.to_string()}))
}
};
// M3.8: Optimize results
results = optimize_search_results(results, state.optimizer_service.as_ref()).await;
// Route by search method
match params.method {
SearchMethod::Semantic => build_search_response(&params, results, None),
SearchMethod::Hybrid => execute_hybrid_search(&state, &params, results, &token).await,
}
}
@@ -1342,6 +1333,75 @@ pub async fn vault_file_handler(
}
}
/// Query temporal knowledge graph
/// 1. Find entities via semantic search
/// 2. Traverse edges from entities
/// 3. Apply temporal filtering (t_valid/t_invalid)
/// 4. Return graph with confidence scores
async fn query_temporal_graph(
state: &web::Data<AppState>,
params: &QueryParams,
) -> anyhow::Result<serde_json::Value> {
// Step 1: Find entities (order by name for deterministic results)
let entities_rows: Vec<(String, String, String)> = sqlx::query_as(
"SELECT id, name, entity_type FROM memory_entity WHERE project_id = $1 AND t_expired IS NULL LIMIT $2"
)
.bind(&params.project)
.bind(params.limit as i32)
.fetch_all(&state.pool)
.await
.unwrap_or_default();
// Step 2: Traverse edges from found entities
let mut edges_data: Vec<(String, String, String, String, String, f32)> = Vec::new();
for (entity_id, _name, _type_str) in &entities_rows {
let entity_edges: Vec<(String, String, String, String, f32, Option<chrono::DateTime<chrono::Utc>>, Option<chrono::DateTime<chrono::Utc>>)> = sqlx::query_as(
"SELECT id, target_entity_id, relation_type, fact, confidence, t_valid, t_invalid FROM memory_edge WHERE project_id = $1 AND source_entity_id = $2 AND t_expired IS NULL"
)
.bind(&params.project)
.bind(entity_id)
.fetch_all(&state.pool)
.await
.unwrap_or_default();
for (id, target, rel, fact, conf, t_valid, t_invalid) in entity_edges {
// Step 3: Apply temporal filtering
let now = chrono::Utc::now();
let valid = t_valid.as_ref().map(|t| *t <= now).unwrap_or(true);
let not_invalid = t_invalid.as_ref().map(|t| *t > now).unwrap_or(true);
if valid && not_invalid {
edges_data.push((id, entity_id.clone(), target, rel, fact, conf));
}
}
}
// Step 4: Build response
let response = json!({
"query": params.question,
"project": params.project,
"entities": entities_rows.iter().map(|(id, name, etype)| json!({
"id": id,
"name": name,
"type": etype
})).collect::<Vec<_>>(),
"edges": edges_data.iter().map(|(id, src, tgt, rel, fact, conf)| json!({
"id": id,
"source": src,
"target": tgt,
"relation": rel,
"fact": fact,
"confidence": conf
})).collect::<Vec<_>>(),
"count": json!({
"entities": entities_rows.len(),
"edges": edges_data.len()
})
});
Ok(response)
}
#[cfg(test)]
mod tests {
use super::*;