Save a provenance graph to the repository. Serializes the graph to disk, registers it in the pristine database with `node_type::PROVENANCE`, and records dependencies on the changes this graph explains. # Returns The content hash of the saved provenance graph.
(
&self,
graph: &atomic_core::change::ProvenanceGraph,
)
| 1436 | /// |
| 1437 | /// The content hash of the saved provenance graph. |
| 1438 | pub fn save_provenance_graph( |
| 1439 | &self, |
| 1440 | graph: &atomic_core::change::ProvenanceGraph, |
| 1441 | ) -> Result<Hash, RepositoryError> { |
| 1442 | self.ensure_project_identity(atomic_core::kg::project_id::MintReason::Upgrade)?; |
| 1443 | use atomic_core::pristine::MutTxnT; |
| 1444 | |
| 1445 | // Save to disk |
| 1446 | let hash = self |
| 1447 | .change_store |
| 1448 | .save_provenance_graph(graph) |
| 1449 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1450 | |
| 1451 | // Register in the graph |
| 1452 | let mut txn = self |
| 1453 | .pristine |
| 1454 | .write_txn() |
| 1455 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1456 | |
| 1457 | let prov_id = txn |
| 1458 | .register_provenance(&hash) |
| 1459 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1460 | |
| 1461 | // Register dependencies: provenance → explained changes. Sidecars can |
| 1462 | // arrive with `--download-only`, before their changes are inserted into |
| 1463 | // a view. Registering the content hash now gives the relationship a |
| 1464 | // stable internal ID; insertion reuses that registration later. |
| 1465 | for change_hash in &graph.changes_explained { |
| 1466 | let change_id = txn |
| 1467 | .register_change(change_hash) |
| 1468 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1469 | txn.put_dep(prov_id, change_id) |
| 1470 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1471 | } |
| 1472 | |
| 1473 | // If chained, register dependency on previous provenance graph too |
| 1474 | if let Some(ref prev_hash) = graph.previous { |
| 1475 | if let Ok(Some(prev_id)) = txn.get_internal(prev_hash) { |
| 1476 | txn.put_dep(prov_id, prev_id) |
| 1477 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | // Populate session tables from the provenance graph, for any agent |
| 1482 | // (Sherpa, Claude Code, OpenCode, generic atomic-agent, ...). The |
| 1483 | // write transaction is still open, so this is atomic with the |
| 1484 | // provenance registration above. |
| 1485 | txn.populate_session_tables(prov_id.get(), graph) |
| 1486 | .map_err(|e| RepositoryError::Database(e.to_string()))?; |
| 1487 | |
| 1488 | // Maintain the Atomic-native session ledger alongside the provenance |
| 1489 | // registration. The JSON file is a mutable runtime cache; this index |
| 1490 | // connects the external session identity to immutable turn objects. |
| 1491 | let json_path = self |
| 1492 | .dot_dir |
| 1493 | .join("sessions") |
| 1494 | .join(format!("{}.json", graph.session_id)) |
| 1495 | .to_string_lossy() |