Populate the session tables from a provenance graph. Called during `save_provenance_graph` for provenance produced by any** agent — Sherpa, Claude Code, OpenCode, the generic `atomic-agent`, etc. Sherpa graphs (`profile == "sherpa-trace/1.0.0"`) carry richer structured `detail` JSON (intent/todo/phase/verification), so they populate the intent and phase tables more fully; graphs from other agents
(
&mut self,
provenance_id: u64,
graph: &crate::change::ProvenanceGraph,
)
| 166 | /// |
| 167 | /// Best-effort: parse errors on individual nodes are logged and skipped. |
| 168 | pub fn populate_session_tables( |
| 169 | &mut self, |
| 170 | provenance_id: u64, |
| 171 | graph: &crate::change::ProvenanceGraph, |
| 172 | ) -> PristineResult<()> { |
| 173 | use crate::change::provenance_graph::ProvenanceNodeKind; |
| 174 | use crate::change::session::*; |
| 175 | |
| 176 | // No profile gate: provenance from any agent is indexed into the |
| 177 | // session tables. The per-node extraction below is profile-agnostic |
| 178 | // and falls back to node summary / kind labels when the richer |
| 179 | // Sherpa-specific `detail` keys are absent. |
| 180 | |
| 181 | // Open all four tables. |
| 182 | let mut events_table = self.txn.open_table(SESSION_EVENTS)?; |
| 183 | let mut todos_table = self.txn.open_table(SESSION_TODOS)?; |
| 184 | let mut phases_table = self.txn.open_table(SESSION_PHASES)?; |
| 185 | let mut intents_table = self.txn.open_table(SESSION_INTENTS)?; |
| 186 | |
| 187 | for (seq, node) in graph.nodes.iter().enumerate() { |
| 188 | let seq = seq as u64; |
| 189 | let detail: Option<serde_json::Value> = if let Some(s) = node.detail.as_deref() { |
| 190 | match serde_json::from_str(s) { |
| 191 | Ok(v) => Some(v), |
| 192 | Err(e) => { |
| 193 | log::warn!( |
| 194 | "Warning: failed to parse provenance node detail JSON (node id={}, kind={}): {}", |
| 195 | node.id, |
| 196 | node.kind.label(), |
| 197 | e |
| 198 | ); |
| 199 | None |
| 200 | } |
| 201 | } |
| 202 | } else { |
| 203 | None |
| 204 | }; |
| 205 | |
| 206 | // Write every node as a SessionEvent regardless of kind. |
| 207 | // |
| 208 | // `event_kind` and `record_type` are populated from the original |
| 209 | // Sherpa/agent-trace `record_type` field stored in the detail JSON |
| 210 | // (e.g. "intent", "todo_status", "phase_transition"). We fall back |
| 211 | // to `node.kind.label()` only when no such field is present so that |
| 212 | // non-Sherpa nodes still get a meaningful discriminant. |
| 213 | let sherpa_record_type = detail |
| 214 | .as_ref() |
| 215 | .and_then(|d| d["record_type"].as_str()) |
| 216 | .map(|s| s.to_string()); |
| 217 | let event_kind = sherpa_record_type |
| 218 | .clone() |
| 219 | .unwrap_or_else(|| node.kind.label().to_string()); |
| 220 | let event = SessionEvent { |
| 221 | seq, |
| 222 | timestamp: format_timestamp_ms(node.timestamp), |
| 223 | event_kind, |
| 224 | place: None, |
| 225 | transition: None, |