From faca3ceb8e77ae2eb42b8de72eb79dd240e666af Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Sun, 25 Oct 2026 19:01:15 +0100 Subject: [PATCH 1/2] fix(kg): render second and millisecond turn timestamps without upgrades Accept second and millisecond epochs at the session-date formatting boundary. Normalize the old projector's expanded-year created_at strings only in returned session-ledger KG read models, including read-only queries. Keep KG_GENERATION at 2. Do not require a migration, reindex, or writable open; leave timestamp producers, ordering, RPC payloads, provenance bytes, and session-manifest heads unchanged. Add regressions using DDSB7T6MPKZB's 1792920379589 timestamp. Verify correct dates and millisecond precision, unchanged valid dates and unrelated nodes, and unchanged cached KG bytes and generation after read-only queries. Validation: 3683 core library tests and all 9 KG-projection integration tests pass. --- atomic-core/src/pristine/txn/kg_identity.rs | 2 +- atomic-core/src/pristine/txn/read.rs | 7 +- .../src/pristine/txn/write/session_kg.rs | 2 +- atomic-core/src/pristine/txn/write/triples.rs | 7 +- atomic-core/src/pristine/vault.rs | 99 ++++++++++++++++ atomic-repository/tests/kg_projection.rs | 106 ++++++++++++++++++ 6 files changed, 213 insertions(+), 10 deletions(-) diff --git a/atomic-core/src/pristine/txn/kg_identity.rs b/atomic-core/src/pristine/txn/kg_identity.rs index bffc48c0..d882ca1a 100644 --- a/atomic-core/src/pristine/txn/kg_identity.rs +++ b/atomic-core/src/pristine/txn/kg_identity.rs @@ -24,7 +24,7 @@ macro_rules! reads { }; table .iter()? - .map(|row| serde_json::from_slice(row?.1.value()).map_err(serialization)) + .map(|row| crate::KgNode::from_json(row?.1.value()).map_err(serialization)) .collect() } pub fn all_kg_edges(&self) -> PristineResult> { diff --git a/atomic-core/src/pristine/txn/read.rs b/atomic-core/src/pristine/txn/read.rs index 01d12b1f..e07b35e4 100644 --- a/atomic-core/src/pristine/txn/read.rs +++ b/atomic-core/src/pristine/txn/read.rs @@ -1379,11 +1379,10 @@ impl KgTxnT for ReadTxn { }; match table.get(id)? { Some(value) => { - let node: KgNode = serde_json::from_slice(value.value()).map_err(|e| { - PristineError::Serialization { + let node = + KgNode::from_json(value.value()).map_err(|e| PristineError::Serialization { message: e.to_string(), - } - })?; + })?; Ok(Some(node)) } None => Ok(None), diff --git a/atomic-core/src/pristine/txn/write/session_kg.rs b/atomic-core/src/pristine/txn/write/session_kg.rs index 33725241..16ee3650 100644 --- a/atomic-core/src/pristine/txn/write/session_kg.rs +++ b/atomic-core/src/pristine/txn/write/session_kg.rs @@ -249,7 +249,7 @@ impl WriteTxn<'_> { } } self.kg_clear_finding("UnresolvedReference", &id)?; - let created_at = chrono::DateTime::from_timestamp(turn.timestamp, 0) + let created_at = crate::pristine::vault::session_timestamp_datetime(turn.timestamp) .ok_or_else(|| PristineError::Serialization { message: "session turn timestamp is outside the RDF dateTime range".into(), })? diff --git a/atomic-core/src/pristine/txn/write/triples.rs b/atomic-core/src/pristine/txn/write/triples.rs index b9bac524..f65167b3 100644 --- a/atomic-core/src/pristine/txn/write/triples.rs +++ b/atomic-core/src/pristine/txn/write/triples.rs @@ -164,10 +164,9 @@ impl<'a> KgTxnT for WriteTxn<'a> { let result = match table.get(id)? { Some(value) => { let bytes: &[u8] = value.value(); - let node: KgNode = - serde_json::from_slice(bytes).map_err(|e| PristineError::Serialization { - message: e.to_string(), - })?; + let node = KgNode::from_json(bytes).map_err(|e| PristineError::Serialization { + message: e.to_string(), + })?; Some(node) } None => None, diff --git a/atomic-core/src/pristine/vault.rs b/atomic-core/src/pristine/vault.rs index 5b50e03e..01cc71dd 100644 --- a/atomic-core/src/pristine/vault.rs +++ b/atomic-core/src/pristine/vault.rs @@ -479,6 +479,18 @@ pub fn parse_intent_reference( // ── Knowledge Graph ───────────────────────────────────────────── +// Legacy session epochs are untagged. Keep small values as seconds (including +// old fixtures near 1970); contemporary millisecond epochs exceed this bound. +const SESSION_MILLISECOND_THRESHOLD: u64 = 100_000_000_000; + +pub(crate) fn session_timestamp_datetime(timestamp: i64) -> Option> { + if timestamp.unsigned_abs() >= SESSION_MILLISECOND_THRESHOLD { + chrono::DateTime::from_timestamp_millis(timestamp) + } else { + chrono::DateTime::from_timestamp(timestamp, 0) + } +} + /// A node in the knowledge graph. /// /// Nodes represent repository artifacts: changes, files, entities, views, @@ -503,6 +515,37 @@ pub struct KgNode { } impl KgNode { + /// Normalize the legacy session projector's seconds/milliseconds mix-up in + /// the returned read model only. Stored KG and provenance bytes stay intact. + pub(crate) fn from_json(bytes: &[u8]) -> Result { + let mut node: Self = serde_json::from_slice(bytes)?; + if node.source == "session_ledger" && node.has_type(crate::kg::ontology::Class::SessionTurn) + { + if let Some(created_at) = node.metadata.as_mut().and_then(|m| m.get_mut("created_at")) { + if let Some(legacy) = created_at + .as_str() + .and_then(|value| { + chrono::DateTime::parse_from_rfc3339(value) + // Chrono emits expanded years for the erroneous + // date, but its strict RFC-3339 parser rejects them. + .or_else(|_| { + chrono::DateTime::parse_from_str(value, "%Y-%m-%dT%H:%M:%S%.f%:z") + }) + .ok() + }) + .filter(|value| { + value.timestamp().unsigned_abs() >= SESSION_MILLISECOND_THRESHOLD + }) + { + if let Some(corrected) = session_timestamp_datetime(legacy.timestamp()) { + *created_at = serde_json::json!(corrected.to_rfc3339()); + } + } + } + } + Ok(node) + } + pub fn has_type(&self, class: crate::kg::ontology::Class) -> bool { self.types.iter().any(|iri| iri == class.iri()) } @@ -656,6 +699,62 @@ pub struct SearchResult { mod tests { use super::*; + #[test] + fn session_epoch_formatting_accepts_seconds_and_milliseconds() { + for (raw, expected) in [ + (1_792_920_379, "2026-10-25T09:26:19+00:00"), + (1_792_920_379_589, "2026-10-25T09:26:19.589+00:00"), + (0, "1970-01-01T00:00:00+00:00"), + ] { + assert_eq!( + session_timestamp_datetime(raw).unwrap().to_rfc3339(), + expected + ); + } + assert!(session_timestamp_datetime(i64::MAX).is_none()); + assert!(session_timestamp_datetime(i64::MIN).is_none()); + } + + #[test] + fn cached_session_date_is_normalized_only_in_the_read_model() { + use crate::kg::ontology::Class; + let original = KgNode::new("turn", Class::SessionTurn, "continue", "session_ledger") + .with_metadata(serde_json::json!({"created_at": "+58785-05-21T06:46:29+00:00"})); + let bytes = serde_json::to_vec(&original).unwrap(); + let decoded = KgNode::from_json(&bytes).unwrap(); + assert_eq!( + decoded.metadata.unwrap()["created_at"], + "2026-10-25T09:26:19.589+00:00" + ); + assert_eq!(serde_json::to_vec(&original).unwrap(), bytes); + } + + #[test] + fn date_normalization_leaves_valid_dates_and_unrelated_nodes_unchanged() { + use crate::kg::ontology::Class; + for (class, source, date) in [ + ( + Class::SessionTurn, + "session_ledger", + "2026-10-25T09:26:19.589Z", + ), + (Class::SessionTurn, "session_ledger", "not a date"), + (Class::SessionTurn, "other", "+58785-05-21T06:46:29+00:00"), + ( + Class::Memory, + "session_ledger", + "+58785-05-21T06:46:29+00:00", + ), + ] { + let original = KgNode::new("node", class, "label", source) + .with_metadata(serde_json::json!({"created_at": date})); + assert_eq!( + KgNode::from_json(&serde_json::to_vec(&original).unwrap()).unwrap(), + original + ); + } + } + #[test] fn compose_human_key_uppercases_project_and_keeps_author() { assert_eq!( diff --git a/atomic-repository/tests/kg_projection.rs b/atomic-repository/tests/kg_projection.rs index 5e869867..3ca47ebf 100644 --- a/atomic-repository/tests/kg_projection.rs +++ b/atomic-repository/tests/kg_projection.rs @@ -11,6 +11,112 @@ use atomic_repository::{Repository, UnrecordOptions, ViewManifest}; const UID: &str = "01ARZ3NDEKTSV4RRFFQ69G5FAV"; +#[test] +fn session_dates_accept_both_epoch_units_without_a_kg_upgrade() { + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + for (session, timestamp, expected) in [ + ("seconds", 1_792_920_379, "2026-10-25T09:26:19+00:00"), + ( + "milliseconds", + 1_792_920_379_589, + "2026-10-25T09:26:19.589+00:00", + ), + ] { + let graph = ProvenanceGraph::builder(session, "opencode") + .timestamp(timestamp) + .build(); + let bytes = graph.serialize().unwrap(); + let hash = repo.save_provenance_graph(&graph).unwrap(); + let id = iri::mint_turn(&repo.project_id().unwrap(), session, 0); + assert_eq!( + repo.vault_kg_node(&id).unwrap().unwrap().metadata.unwrap()["created_at"], + expected + ); + assert_eq!( + repo.load_provenance_graph(&hash) + .unwrap() + .serialize() + .unwrap(), + bytes + ); + } + assert_eq!( + repo.pristine().read_txn().unwrap().kg_generation().unwrap(), + 2 + ); +} + +#[test] +fn readonly_queries_correct_cached_turn_dates_without_reindexing_or_writes() { + use atomic_core::pristine::tables::{KG_INDEX_META, KG_NODES}; + use redb::ReadableDatabase; + + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + let graph = ProvenanceGraph::builder("legacy-timestamp", "opencode") + .timestamp(1_792_920_379_589) + .build(); + let hash = repo.save_provenance_graph(&graph).unwrap(); + let manifest_head = repo.get_session_head("legacy-timestamp").unwrap(); + let id = iri::mint_turn(&repo.project_id().unwrap(), "legacy-timestamp", 0); + let mut cached = repo.vault_kg_node(&id).unwrap().unwrap(); + cached.metadata.as_mut().unwrap()["created_at"] = + serde_json::json!("+58785-05-21T06:46:29+00:00"); + let cached_bytes = serde_json::to_vec(&cached).unwrap(); + let db_path = repo.dot_dir().join("atomic.redb"); + drop(repo); + { + let db = redb::Database::open(&db_path).unwrap(); + let txn = db.begin_write().unwrap(); + txn.open_table(KG_NODES) + .unwrap() + .insert(id.as_str(), cached_bytes.as_slice()) + .unwrap(); + txn.commit().unwrap(); + } + + let repo = Repository::open_readonly(directory.path()).unwrap(); + let expected = "2026-10-25T09:26:19.589+00:00"; + assert_eq!( + repo.vault_kg_node(&id).unwrap().unwrap().metadata.unwrap()["created_at"], + expected + ); + let snapshot = repo.workflow_snapshot().unwrap(); + let turn = snapshot.nodes.iter().find(|node| node.id == id).unwrap(); + assert_eq!(turn.metadata.as_ref().unwrap()["created_at"], expected); + assert_eq!(snapshot.generation, 2); + assert_eq!( + repo.get_session_head("legacy-timestamp").unwrap(), + manifest_head + ); + assert_eq!(repo.load_provenance_graph(&hash).unwrap(), graph); + drop(repo); + + // Both the old cached date and the generation marker remain byte-for-byte + // untouched: correcting the returned date must not require writable open. + let db = redb::Database::open(&db_path).unwrap(); + let txn = db.begin_read().unwrap(); + assert_eq!( + txn.open_table(KG_NODES) + .unwrap() + .get(id.as_str()) + .unwrap() + .unwrap() + .value(), + cached_bytes + ); + assert_eq!( + txn.open_table(KG_INDEX_META) + .unwrap() + .get("kg_generation") + .unwrap() + .unwrap() + .value(), + 2 + ); +} + #[test] fn references_never_take_ownership_of_another_intents_children() { for reference_first in [false, true] { From 1f3720c2eb8a957a6954831f76a05cf33673450c Mon Sep 17 00:00:00 2001 From: Bradley Hilton Date: Mon, 26 Oct 2026 11:29:55 +0100 Subject: [PATCH 2/2] fix(agent): preserve millisecond provenance and session timestamps Use millisecond epochs for new goal, reasoning, response, graph, request-metadata, turn-envelope, and session lifecycle writes. Preserve subsecond turn durations and keep explicit timestamp_millis graph construction separate from raw historical imports. Share compatibility decoding and chronological comparisons across graph/session readers, trace export, triage, and CLI listings. Convert to protobuf seconds plus nanoseconds without losing milliseconds. Historical payloads remain byte-for-byte intact; causal links still take precedence over clocks. No KG generation bump, migration, or reindex is required. Cover same-second actions through journal replay, graph preparation, session storage and KG projection; legacy seconds, mixed-unit ordering, clock skew, raw-byte preservation, fallback callbacks, envelope durations and RPC precision. Core, agent, RPC, KG-projection, triage and targeted CLI suites pass; final accumulator and consistency regressions also pass. --- atomic-agent/src/export.rs | 23 +++- .../src/provenance/accumulator/append.rs | 2 + .../src/provenance/accumulator/convert.rs | 2 +- .../src/provenance/accumulator/tests.rs | 27 ++++ atomic-agent/src/record/provenance.rs | 9 +- atomic-agent/src/record/tests.rs | 27 +++- .../src/turn/orchestrator/provenance.rs | 4 +- .../src/turn/orchestrator/session_end.rs | 2 +- atomic-agent/src/turn/orchestrator/tests.rs | 29 ++++ atomic-agent/src/turn/orchestrator/turn.rs | 2 +- .../tests/provenance_timestamp_consistency.rs | 130 ++++++++++++++++++ atomic-cli/src/commands/change/types.rs | 4 +- atomic-cli/src/commands/provenance/command.rs | 4 +- atomic-cli/src/commands/session.rs | 3 +- atomic-core/src/change/envelope.rs | 18 +-- atomic-core/src/change/provenance/builder.rs | 3 +- atomic-core/src/change/provenance/mod.rs | 7 +- .../src/change/provenance_graph/builder.rs | 11 +- .../src/change/provenance_graph/mod.rs | 10 +- .../src/change/provenance_graph/tests.rs | 22 ++- atomic-core/src/change/session.rs | 65 ++++++++- atomic-core/src/pristine/txn/write/mod.rs | 24 +++- .../src/pristine/txn/write/session_kg.rs | 3 +- atomic-core/src/pristine/vault.rs | 27 ++-- atomic-core/src/types/mod.rs | 1 + atomic-core/src/types/timestamp.rs | 60 ++++++++ atomic-repository/src/repository/changes.rs | 20 +-- atomic-repository/src/triage/report.rs | 4 +- docs/knowledge-graph-phase1-upgrade.md | 18 +++ libatomic/src/daemon/sandbox_wire.rs | 36 ++++- libatomic/src/daemon/services_agent.rs | 21 +-- libatomic/src/daemon/services_query.rs | 36 ++++- 32 files changed, 566 insertions(+), 88 deletions(-) create mode 100644 atomic-agent/tests/provenance_timestamp_consistency.rs create mode 100644 atomic-core/src/types/timestamp.rs diff --git a/atomic-agent/src/export.rs b/atomic-agent/src/export.rs index 4e30cc08..38b27db1 100644 --- a/atomic-agent/src/export.rs +++ b/atomic-agent/src/export.rs @@ -44,7 +44,7 @@ use std::path::Path; use atomic_core::change::provenance_graph::{ProvenanceGraph, ProvenanceNodeKind}; use atomic_core::types::Base32; -use chrono::{DateTime, Utc}; +use chrono::Utc; use serde::{Deserialize, Serialize}; use serde_json::Value; @@ -182,7 +182,8 @@ pub struct RelatedRef { /// /// This function never fails; any missing data is silently omitted. pub fn to_agent_trace_record(graph: &ProvenanceGraph, root: &Path) -> TraceRecord { - let timestamp = DateTime::::from_timestamp(graph.timestamp, 0) + let timestamp = graph + .timestamp_datetime() .unwrap_or_else(Utc::now) .to_rfc3339(); @@ -603,6 +604,24 @@ mod tests { .build() } + #[test] + fn trace_timestamp_preserves_milliseconds_and_accepts_legacy_seconds() { + let directory = tempfile::tempdir().unwrap(); + for (raw, expected) in [ + (1_792_920_379, "2026-10-25T09:26:19+00:00"), + (1_792_920_379_589, "2026-10-25T09:26:19.589+00:00"), + ] { + let graph = ProvenanceGraph::builder("timestamp-compat", "agent") + .timestamp(raw) + .build(); + assert_eq!( + to_agent_trace_record(&graph, directory.path()).timestamp, + expected + ); + assert_eq!(graph.timestamp, raw); + } + } + #[test] fn test_record_has_required_fields() { let graph = sherpa_graph(); diff --git a/atomic-agent/src/provenance/accumulator/append.rs b/atomic-agent/src/provenance/accumulator/append.rs index b98ca218..a2aa407c 100644 --- a/atomic-agent/src/provenance/accumulator/append.rs +++ b/atomic-agent/src/provenance/accumulator/append.rs @@ -10,6 +10,8 @@ impl ProvenanceAccumulator { /// Append a goal node (human prompt). /// + /// `timestamp` is Unix epoch milliseconds, as on every accumulator node. + /// /// Returns the new node's ID. pub fn append_goal(&mut self, prompt: &str, timestamp: i64) -> String { let summary = truncate_prompt(prompt, MAX_GOAL_PROMPT_LEN); diff --git a/atomic-agent/src/provenance/accumulator/convert.rs b/atomic-agent/src/provenance/accumulator/convert.rs index 0767c4dc..afd22589 100644 --- a/atomic-agent/src/provenance/accumulator/convert.rs +++ b/atomic-agent/src/provenance/accumulator/convert.rs @@ -125,7 +125,7 @@ impl ProvenanceAccumulator { .nodes(nodes) .edges(edges) .changes_explained(changes_explained.to_vec()) - .timestamp(timestamp); + .timestamp_millis(timestamp); if let Some(previous) = previous { builder = builder.previous(previous); } diff --git a/atomic-agent/src/provenance/accumulator/tests.rs b/atomic-agent/src/provenance/accumulator/tests.rs index ee05e598..8005499b 100644 --- a/atomic-agent/src/provenance/accumulator/tests.rs +++ b/atomic-agent/src/provenance/accumulator/tests.rs @@ -587,6 +587,33 @@ fn test_from_serialized_rebuilds_commitments_since_patch() { // ---- to_provenance_graph conversion ---- +#[test] +fn graph_header_preserves_explicit_milliseconds_without_unit_guessing() { + for timestamp_ms in [-1001, 0, 1000, 1_792_920_379_589] { + let mut accumulator = ProvenanceAccumulator::new("timestamp-millis"); + accumulator.append_goal("Preserve event milliseconds verbatim", timestamp_ms); + let graph = accumulator + .prepare_provenance_graph("agent", "Agent", "vendor", &[]) + .graph; + assert_eq!(graph.timestamp, timestamp_ms); + assert_eq!(graph.nodes[0].timestamp, timestamp_ms); + } +} + +#[test] +fn graph_header_uses_only_the_new_turns_millisecond_timestamps() { + let mut accumulator = ProvenanceAccumulator::new("timestamp-delta"); + accumulator.append_goal("first", 1_792_920_379_589); + accumulator.to_provenance_graph("agent", "Agent", "vendor", &[]); + // A skewed clock must not reuse the previous turn's later timestamp. + accumulator.append_goal("second", 1_792_920_379_100); + let graph = accumulator + .prepare_provenance_graph("agent", "Agent", "vendor", &[]) + .graph; + assert_eq!(graph.timestamp, 1_792_920_379_100); + assert_eq!(graph.nodes.len(), 1); +} + #[test] fn test_to_provenance_graph_basic() { use atomic_core::change::provenance_graph as pg; diff --git a/atomic-agent/src/record/provenance.rs b/atomic-agent/src/record/provenance.rs index ae9f8588..acdfc8c3 100644 --- a/atomic-agent/src/record/provenance.rs +++ b/atomic-agent/src/record/provenance.rs @@ -43,7 +43,7 @@ pub(crate) fn build_turn_provenance(options: &TurnRecordOptions<'_>) -> Provenan _ => PromptContent::None, }; - let timestamp = options.event.timestamp.timestamp(); + let timestamp = options.event.timestamp.timestamp_millis(); // Extract enriched metadata from the raw JSON payload sent by the plugin. // The plugin accumulates data across all events within a turn and sends @@ -218,9 +218,10 @@ pub(crate) fn build_turn_envelope( let files_in_turn: Vec = recorded_files.to_vec(); - let session_started_at = options.session.started_at.timestamp(); - let turn_ended_at = options.event.timestamp.timestamp(); - let turn_started_at = turn_ended_at - (options.turn_duration_ms as i64 / 1000); + let session_started_at = options.session.started_at.timestamp_millis(); + let turn_ended_at = options.event.timestamp.timestamp_millis(); + let turn_started_at = + turn_ended_at.saturating_sub(i64::try_from(options.turn_duration_ms).unwrap_or(i64::MAX)); let mut builder = SessionEnvelope::builder(&options.session.session_id, &options.session.agent_name) diff --git a/atomic-agent/src/record/tests.rs b/atomic-agent/src/record/tests.rs index 4ad70d2a..030880da 100644 --- a/atomic-agent/src/record/tests.rs +++ b/atomic-agent/src/record/tests.rs @@ -720,11 +720,12 @@ fn test_provenance_model_fallback_unknown() { #[test] fn test_provenance_has_timestamp() { let session = make_session(); - let event = make_event(); + let mut event = make_event(); + event.timestamp = chrono::DateTime::from_timestamp_millis(1_792_920_379_589).unwrap(); let options = make_options(&session, &event); let prov = build_turn_provenance(&options); - assert!(prov.timestamp.is_some()); + assert_eq!(prov.timestamp, Some(1_792_920_379_589)); } // build_turn_envelope tests @@ -839,7 +840,27 @@ fn test_envelope_session_started_at() { let options = make_options(&session, &event); let env = build_turn_envelope(&options, &[]); - assert_eq!(env.session_started_at, session.started_at.timestamp()); + assert_eq!( + env.session_started_at, + session.started_at.timestamp_millis() + ); +} + +#[test] +fn envelope_timing_preserves_subsecond_duration() { + let session = make_session(); + let mut event = make_event(); + event.timestamp = chrono::DateTime::from_timestamp_millis(1_792_920_379_589).unwrap(); + let mut options = make_options(&session, &event); + options.turn_duration_ms = 489; + let envelope = build_turn_envelope(&options, &[]); + assert_eq!(envelope.turn_ended_at, 1_792_920_379_589); + assert_eq!(envelope.turn_started_at, 1_792_920_379_100); + assert_eq!(envelope.turn_duration_ms, 489); + assert_eq!( + SessionEnvelope::decode(&envelope.encode().unwrap()).unwrap(), + envelope + ); } #[test] diff --git a/atomic-agent/src/turn/orchestrator/provenance.rs b/atomic-agent/src/turn/orchestrator/provenance.rs index d22967aa..d899d37e 100644 --- a/atomic-agent/src/turn/orchestrator/provenance.rs +++ b/atomic-agent/src/turn/orchestrator/provenance.rs @@ -484,7 +484,7 @@ impl TurnOrchestrator { None => return, }; - let timestamp = event.timestamp.timestamp(); + let timestamp = event.timestamp.timestamp_millis(); self.with_accumulator(session_id, |acc| { let mut block_count = 0; @@ -584,7 +584,7 @@ impl TurnOrchestrator { return; }; - let timestamp = event.timestamp.timestamp(); + let timestamp = event.timestamp.timestamp_millis(); self.with_accumulator(session_id, |acc| { acc.append_llm_response(&response, timestamp); true diff --git a/atomic-agent/src/turn/orchestrator/session_end.rs b/atomic-agent/src/turn/orchestrator/session_end.rs index 336221fa..20c559c1 100644 --- a/atomic-agent/src/turn/orchestrator/session_end.rs +++ b/atomic-agent/src/turn/orchestrator/session_end.rs @@ -288,7 +288,7 @@ impl TurnOrchestrator { // Reconcile the Atomic session ledger: the session is now ended. // Best-effort — the JSON file remains the runtime fallback. - let ended_at = session.ended_at.map(|t| t.timestamp()); + let ended_at = session.ended_at.map(|t| t.timestamp_millis()); self.sync_session_lifecycle(&session, ended_at); log::info!( diff --git a/atomic-agent/src/turn/orchestrator/tests.rs b/atomic-agent/src/turn/orchestrator/tests.rs index 108b25de..02a09a33 100644 --- a/atomic-agent/src/turn/orchestrator/tests.rs +++ b/atomic-agent/src/turn/orchestrator/tests.rs @@ -555,6 +555,35 @@ async fn test_sandbox_turn_end_leaves_canonical_current_view_untouched() { // TurnStart tests +#[tokio::test] +async fn fallback_goal_reasoning_and_response_use_millisecond_epochs() { + let dir = TempDir::new().unwrap(); + let mut orch = make_orchestrator(&dir); + orch.dispatch(session_start_event("timestamp-fallback")) + .await + .unwrap(); + let mut start = turn_start_event("timestamp-fallback", "Preserve subsecond actions"); + start.timestamp = chrono::DateTime::from_timestamp_millis(1_792_920_379_100).unwrap(); + orch.dispatch(start).await.unwrap(); + let session = orch + .session_store + .load("timestamp-fallback") + .unwrap() + .unwrap(); + let mut end = turn_end_event("timestamp-fallback"); + end.timestamp = chrono::DateTime::from_timestamp_millis(1_792_920_379_589).unwrap(); + end.raw_json = Some(serde_json::json!({ + "reasoning_text": "Check the timestamp unit", + "response": "Done" + })); + orch.inject_reasoning_nodes("timestamp-fallback", &end); + orch.inject_response_node("timestamp-fallback", &session, &end); + let accumulator = orch.load_accumulator("timestamp-fallback").unwrap(); + assert_eq!(accumulator.nodes()[0].timestamp, 1_792_920_379_100); + assert_eq!(accumulator.nodes()[1].timestamp, 1_792_920_379_589); + assert_eq!(accumulator.nodes()[2].timestamp, 1_792_920_379_589); +} + #[tokio::test] async fn test_turn_start_transitions_to_active() { let dir = TempDir::new().unwrap(); diff --git a/atomic-agent/src/turn/orchestrator/turn.rs b/atomic-agent/src/turn/orchestrator/turn.rs index aeca2c98..34d45d27 100644 --- a/atomic-agent/src/turn/orchestrator/turn.rs +++ b/atomic-agent/src/turn/orchestrator/turn.rs @@ -186,7 +186,7 @@ impl TurnOrchestrator { if let Some(ref prompt) = event.prompt { if !prompt.is_empty() && self.journal_sink.is_none() { if let Some(mut acc) = self.load_accumulator(session_id) { - acc.append_goal(prompt, event.timestamp.timestamp()); + acc.append_goal(prompt, event.timestamp.timestamp_millis()); self.save_accumulator(session_id, &acc); } } diff --git a/atomic-agent/tests/provenance_timestamp_consistency.rs b/atomic-agent/tests/provenance_timestamp_consistency.rs new file mode 100644 index 00000000..fc6925df --- /dev/null +++ b/atomic-agent/tests/provenance_timestamp_consistency.rs @@ -0,0 +1,130 @@ +use atomic_agent::{ + event::{ProvenanceJournalEnvelope, ProvenanceJournalEvent}, + provenance::accumulator::ProvenanceAccumulator, +}; +use atomic_core::{change::ProvenanceGraph, kg::iri}; +use atomic_repository::Repository; + +#[test] +fn journal_actions_in_one_second_preserve_milliseconds_through_the_ledger_and_kg() { + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + let goal = ProvenanceJournalEnvelope::new( + "goal", + "subsecond", + 1, + 1, + 1_792_920_379_100, + ProvenanceJournalEvent::Goal { + prompt: "Preserve precision".into(), + }, + ); + let response = ProvenanceJournalEnvelope::new( + "response", + "subsecond", + 1, + 1, + 1_792_920_379_589, + ProvenanceJournalEvent::Response { + text: "Done".into(), + }, + ) + .with_causal_parents(["goal"]); + let accumulator = + ProvenanceAccumulator::replay_provenance_journal(vec![response, goal]).unwrap(); + let prepared = accumulator.prepare_provenance_graph("opencode", "OpenCode", "openrouter", &[]); + assert_eq!(prepared.graph.timestamp, 1_792_920_379_589); + assert_eq!(prepared.graph.nodes[0].timestamp, 1_792_920_379_100); + assert_eq!(prepared.graph.nodes[1].timestamp, 1_792_920_379_589); + assert_eq!( + prepared.graph.serialize().unwrap(), + accumulator + .prepare_provenance_graph("opencode", "OpenCode", "openrouter", &[]) + .graph + .serialize() + .unwrap() + ); + let hash = repo.save_provenance_graph(&prepared.graph).unwrap(); + let (_, turns) = repo.get_session_ledger("subsecond").unwrap().unwrap(); + assert_eq!(turns[0].timestamp, 1_792_920_379_589); + assert_eq!(turns[0].provenance_hash, hash); + let id = iri::mint_turn(&repo.project_id().unwrap(), "subsecond", 0); + let turn = repo.vault_kg_node(&id).unwrap().unwrap(); + assert_eq!( + turn.metadata.unwrap()["created_at"], + "2026-10-25T09:26:19.589+00:00" + ); + assert_eq!( + repo.pristine().read_txn().unwrap().kg_generation().unwrap(), + 2 + ); +} + +#[test] +fn mixed_unit_histories_keep_order_start_time_and_original_payloads() { + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + let earlier = ProvenanceGraph::builder("mixed", "agent") + .timestamp_millis(1_792_920_379_589) + .build(); + let earlier_bytes = earlier.serialize().unwrap(); + let earlier_hash = repo.save_provenance_graph(&earlier).unwrap(); + // Import a legacy seconds record whose instant is later despite its + // smaller raw integer. Causal edges must still take priority over clocks. + let later = ProvenanceGraph::builder("mixed", "agent") + .timestamp(1_792_920_380) + .previous(earlier_hash) + .build(); + let later_bytes = later.serialize().unwrap(); + let later_hash = repo.save_provenance_graph(&later).unwrap(); + let (record, turns) = repo.get_session_ledger("mixed").unwrap().unwrap(); + assert_eq!(record.started_at, earlier.timestamp); + assert_eq!(turns[0].timestamp, earlier.timestamp); + assert_eq!(turns[1].timestamp, later.timestamp); + let graphs = repo.find_provenance_for_session("mixed").unwrap(); + assert_eq!(graphs[0].0, earlier_hash); + assert_eq!(graphs[1].0, later_hash); + assert_eq!( + repo.load_provenance_graph(&earlier_hash) + .unwrap() + .serialize() + .unwrap(), + earlier_bytes + ); + assert_eq!( + repo.load_provenance_graph(&later_hash) + .unwrap() + .serialize() + .unwrap(), + later_bytes + ); + // Session-list sorting must compare instants, not integer magnitudes. + repo.save_provenance_graph( + &ProvenanceGraph::builder("newest", "agent") + .timestamp(1_792_920_381) + .build(), + ) + .unwrap(); + assert_eq!( + repo.list_session_ledgers(2).unwrap()[0].0.session_id, + "newest" + ); +} + +#[test] +fn empty_session_lifecycle_starts_in_milliseconds() { + let directory = tempfile::tempdir().unwrap(); + let repo = Repository::init(directory.path()).unwrap(); + let before = chrono::Utc::now().timestamp_millis(); + repo.upsert_session_lifecycle("empty", None, None, Some(1_792_920_379_589)) + .unwrap(); + let after = chrono::Utc::now().timestamp_millis(); + let (record, turns) = repo.get_session_ledger("empty").unwrap().unwrap(); + assert!(turns.is_empty()); + assert!((before..=after).contains(&record.started_at)); + assert_eq!(record.ended_at, Some(1_792_920_379_589)); + assert_eq!( + record.activity_datetime(&turns).unwrap().to_rfc3339(), + "2026-10-25T09:26:19.589+00:00" + ); +} diff --git a/atomic-cli/src/commands/change/types.rs b/atomic-cli/src/commands/change/types.rs index 1c7b069d..3ac2b142 100644 --- a/atomic-cli/src/commands/change/types.rs +++ b/atomic-cli/src/commands/change/types.rs @@ -331,7 +331,7 @@ pub struct JsonChangeLedger { /// AI vendor (e.g. "anthropic", "openai"). #[serde(skip_serializing_if = "String::is_empty")] pub agent_vendor: String, - /// Graph creation timestamp (Unix epoch seconds). + /// Graph creation timestamp (Unix epoch milliseconds; historical values may be seconds). pub timestamp: i64, /// Schema profile identifier (e.g. "sherpa-trace/1.0.0"), when present. #[serde(skip_serializing_if = "Option::is_none")] @@ -487,7 +487,7 @@ pub struct JsonProvenance { /// Temperature setting. #[serde(skip_serializing_if = "Option::is_none")] pub temperature: Option, - /// Request timestamp. + /// Request timestamp (Unix epoch milliseconds; historical values may be seconds). #[serde(skip_serializing_if = "Option::is_none")] pub timestamp: Option, /// Request ID from provider. diff --git a/atomic-cli/src/commands/provenance/command.rs b/atomic-cli/src/commands/provenance/command.rs index 7a5a2c7a..a89fe223 100644 --- a/atomic-cli/src/commands/provenance/command.rs +++ b/atomic-cli/src/commands/provenance/command.rs @@ -216,7 +216,9 @@ pub(crate) fn load_graphs( } // Most-recent first (a change explained by >1 graph shows all; the newest // leads). Both loaders sort/return unspecified order, so sort here. - graphs.sort_by_key(|(_, g)| std::cmp::Reverse(g.timestamp)); + graphs.sort_by_key(|(_, g)| { + std::cmp::Reverse(atomic_core::types::timestamp::epoch_millis(g.timestamp)) + }); Ok(graphs) } diff --git a/atomic-cli/src/commands/session.rs b/atomic-cli/src/commands/session.rs index 033fe290..9dda3e9c 100644 --- a/atomic-cli/src/commands/session.rs +++ b/atomic-cli/src/commands/session.rs @@ -203,7 +203,8 @@ pub(crate) fn render_recent_sessions( .map(|turn| turn.timestamp) .or(record.ended_at) .unwrap_or(record.started_at); - let activity = chrono::DateTime::from_timestamp(activity, 0) + let activity = record + .activity_datetime(&turns) .map(|time| time.format("%Y-%m-%d %H:%M").to_string()) .unwrap_or_else(|| activity.to_string()); let intents_display = if intent_count > 0 { diff --git a/atomic-core/src/change/envelope.rs b/atomic-core/src/change/envelope.rs index 430cb0a5..50da5ac1 100644 --- a/atomic-core/src/change/envelope.rs +++ b/atomic-core/src/change/envelope.rs @@ -176,13 +176,13 @@ pub struct SessionEnvelope { pub total_turns: Option, // Timing - /// When the session started (Unix epoch seconds). + /// When the session started (Unix epoch milliseconds; legacy values may be seconds). pub session_started_at: i64, - /// When this turn started (Unix epoch seconds). + /// When this turn started (Unix epoch milliseconds; legacy values may be seconds). pub turn_started_at: i64, - /// When this turn ended (Unix epoch seconds). + /// When this turn ended (Unix epoch milliseconds; legacy values may be seconds). pub turn_ended_at: i64, /// Wall clock duration of this turn in milliseconds. @@ -423,9 +423,9 @@ impl std::fmt::Display for SessionEnvelope { /// let envelope = SessionEnvelope::builder("session-abc", "claude-code") /// .agent_display_name("Claude Code") /// .turn_number(1) -/// .session_started_at(1737000000) -/// .turn_started_at(1737000100) -/// .turn_ended_at(1737000112) +/// .session_started_at(1_737_000_000_000) +/// .turn_started_at(1_737_000_100_000) +/// .turn_ended_at(1_737_000_112_000) /// .turn_duration_ms(12000) /// .prompt_summary("Fix the bug") /// .files_in_turn(vec!["src/main.rs".to_string()]) @@ -495,21 +495,21 @@ impl SessionEnvelopeBuilder { self } - /// Set when the session started (Unix epoch seconds). + /// Set when the session started (Unix epoch milliseconds). #[must_use] pub fn session_started_at(mut self, ts: i64) -> Self { self.session_started_at = ts; self } - /// Set when this turn started (Unix epoch seconds). + /// Set when this turn started (Unix epoch milliseconds). #[must_use] pub fn turn_started_at(mut self, ts: i64) -> Self { self.turn_started_at = ts; self } - /// Set when this turn ended (Unix epoch seconds). + /// Set when this turn ended (Unix epoch milliseconds). #[must_use] pub fn turn_ended_at(mut self, ts: i64) -> Self { self.turn_ended_at = ts; diff --git a/atomic-core/src/change/provenance/builder.rs b/atomic-core/src/change/provenance/builder.rs index a25409ef..eeb07f1c 100644 --- a/atomic-core/src/change/provenance/builder.rs +++ b/atomic-core/src/change/provenance/builder.rs @@ -133,7 +133,8 @@ impl ProvenanceBuilder { self } - /// Set timestamp (Unix epoch seconds). + /// Set timestamp (Unix epoch milliseconds). Legacy second values are + /// preserved verbatim when importing historical provenance. pub fn timestamp(mut self, ts: i64) -> Self { self.timestamp = Some(ts); self diff --git a/atomic-core/src/change/provenance/mod.rs b/atomic-core/src/change/provenance/mod.rs index 5e6e5651..992f073f 100644 --- a/atomic-core/src/change/provenance/mod.rs +++ b/atomic-core/src/change/provenance/mod.rs @@ -112,7 +112,8 @@ pub struct Provenance { #[serde(default)] pub temperature: Option, - /// Request timestamp (Unix epoch seconds) + /// Request timestamp (Unix epoch milliseconds). Historical records may + /// contain seconds; interpret with [`crate::types::timestamp::epoch_datetime`]. #[serde(default)] pub timestamp: Option, @@ -508,7 +509,9 @@ impl Provenance { if let Some(session_id) = &parts.session_id { builder = builder.session_id(session_id.clone()); } - builder.timestamp(chrono::Utc::now().timestamp()).build() + builder + .timestamp(chrono::Utc::now().timestamp_millis()) + .build() } /// Set the prompt from text (hashed for privacy). diff --git a/atomic-core/src/change/provenance_graph/builder.rs b/atomic-core/src/change/provenance_graph/builder.rs index 5e562f49..52bbb41f 100644 --- a/atomic-core/src/change/provenance_graph/builder.rs +++ b/atomic-core/src/change/provenance_graph/builder.rs @@ -158,12 +158,19 @@ impl ProvenanceGraphBuilder { self } - /// Set the timestamp (Unix epoch seconds). Defaults to now. + /// Set a raw epoch timestamp, preserved verbatim for historical imports. + /// New writers should use [`Self::timestamp_millis`]; legacy seconds remain + /// readable without rewriting their payloads. pub fn timestamp(mut self, ts: i64) -> Self { self.timestamp = Some(ts); self } + /// Set the timestamp in Unix epoch milliseconds. Defaults to now. + pub fn timestamp_millis(self, ts: i64) -> Self { + self.timestamp(ts) + } + /// Set the vault work-item/intent governing this turn. pub fn plan_id(mut self, plan_id: impl Into) -> Self { self.plan_id = Some(plan_id.into()); @@ -181,7 +188,7 @@ impl ProvenanceGraphBuilder { let timestamp = self.timestamp.unwrap_or_else(|| { std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_secs() as i64) + .map(|d| d.as_millis() as i64) .unwrap_or(0) }); diff --git a/atomic-core/src/change/provenance_graph/mod.rs b/atomic-core/src/change/provenance_graph/mod.rs index 938ff62d..5f8eea89 100644 --- a/atomic-core/src/change/provenance_graph/mod.rs +++ b/atomic-core/src/change/provenance_graph/mod.rs @@ -174,7 +174,9 @@ pub struct ProvenanceGraph { /// Schema version for forward compatibility. pub version: u8, - /// Timestamp when this graph was created (Unix epoch seconds). + /// Timestamp when this graph was created (Unix epoch milliseconds). + /// Historical graphs may contain seconds; use [`Self::timestamp_datetime`] + /// when interpreting the value without changing its content hash. pub timestamp: i64, /// Session identifier linking provenance graphs within the same session. @@ -240,6 +242,12 @@ pub struct ProvenanceGraph { } impl ProvenanceGraph { + /// Decode millisecond timestamps and historical second timestamps using the + /// compatibility convention in [`crate::types::timestamp::epoch_datetime`]. + pub fn timestamp_datetime(&self) -> Option> { + crate::types::timestamp::epoch_datetime(self.timestamp) + } + /// Create a builder for constructing a provenance graph. pub fn builder( session_id: impl Into, diff --git a/atomic-core/src/change/provenance_graph/tests.rs b/atomic-core/src/change/provenance_graph/tests.rs index 000dac41..7b9c768d 100644 --- a/atomic-core/src/change/provenance_graph/tests.rs +++ b/atomic-core/src/change/provenance_graph/tests.rs @@ -265,19 +265,37 @@ fn test_builder_timestamp_defaults_to_now() { let before = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() - .as_secs() as i64; + .as_millis() as i64; let graph = ProvenanceGraph::builder("sess-1", "agent").build(); let after = std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) .unwrap() - .as_secs() as i64; + .as_millis() as i64; assert!(graph.timestamp >= before); assert!(graph.timestamp <= after); } +#[test] +fn timestamp_readers_preserve_milliseconds_and_legacy_hashed_bytes() { + for (raw, expected) in [ + (1_792_920_379, "2026-10-25T09:26:19+00:00"), + (1_792_920_379_589, "2026-10-25T09:26:19.589+00:00"), + ] { + let graph = ProvenanceGraph::builder("timestamp-compat", "agent") + .timestamp(raw) + .build(); + let bytes = graph.serialize().unwrap(); + let (loaded, hash) = ProvenanceGraph::deserialize(&bytes).unwrap(); + assert_eq!(loaded.timestamp_datetime().unwrap().to_rfc3339(), expected); + assert_eq!(loaded.timestamp, raw); + assert_eq!(loaded.serialize().unwrap(), bytes); + assert_eq!(hash, Hash::of(&bytes)); + } +} + // ---- Serialization ---- #[test] diff --git a/atomic-core/src/change/session.rs b/atomic-core/src/change/session.rs index 6df27856..604e05fb 100644 --- a/atomic-core/src/change/session.rs +++ b/atomic-core/src/change/session.rs @@ -302,7 +302,9 @@ pub struct SessionRecord { pub first_provenance: Option, pub latest_provenance: Option, pub turn_count: u32, + /// Unix epoch milliseconds; historical records may contain seconds. pub started_at: i64, + /// Unix epoch milliseconds; historical records may contain seconds. pub ended_at: Option, pub status: SessionStatus, } @@ -422,6 +424,7 @@ pub struct SessionTurn { pub provenance_hash: Hash, pub change_hashes: Vec, pub previous_provenance: Option, + /// Unix epoch milliseconds; historical turns may contain seconds. pub timestamp: i64, /// Stable vault work-item/intent ID governing this turn, when known. #[serde(default)] @@ -465,7 +468,13 @@ pub fn canonicalize_session_turns(turns: Vec) -> Vec { let sort_keys: Vec = turns .iter() .enumerate() - .map(|(index, turn)| (turn.timestamp, *turn.provenance_hash.as_bytes(), index)) + .map(|(index, turn)| { + ( + crate::types::timestamp::epoch_millis(turn.timestamp), + *turn.provenance_hash.as_bytes(), + index, + ) + }) .collect(); let mut children = vec![Vec::new(); count]; let mut blocked = vec![false; count]; @@ -647,6 +656,20 @@ pub struct SessionManifest { } impl SessionRecord { + /// Latest turn time, or the session end/start time if it has no turns. + /// Handles current milliseconds and historical seconds without changing them. + pub fn activity_datetime( + &self, + turns: &[SessionTurn], + ) -> Option> { + let timestamp = turns + .last() + .map(|turn| turn.timestamp) + .or(self.ended_at) + .unwrap_or(self.started_at); + crate::types::timestamp::epoch_datetime(timestamp) + } + pub fn to_bytes(&self) -> Vec { postcard::to_allocvec(self).expect("SessionRecord serialization") } @@ -665,6 +688,12 @@ impl SessionRecord { } impl SessionTurn { + /// Decode current milliseconds and historical seconds without changing the + /// serialized turn or the content-addressed manifest that contains it. + pub fn timestamp_datetime(&self) -> Option> { + crate::types::timestamp::epoch_datetime(self.timestamp) + } + pub fn to_bytes(&self) -> Vec { postcard::to_allocvec(self).expect("SessionTurn serialization") } @@ -1460,6 +1489,40 @@ mod tests { assert_eq!(decode_session_turn_key(&later), (namespace, 2)); } + #[test] + fn turn_order_preserves_subseconds_and_compares_legacy_seconds() { + let make_turn = |timestamp: i64| SessionTurn { + session_id: "timestamp-order".into(), + turn_number: 99, + goal: None, + provenance_hash: Hash::of(×tamp.to_le_bytes()), + change_hashes: Vec::new(), + previous_provenance: None, + timestamp, + plan_id: None, + todos: Vec::new(), + boundary_start: None, + boundary_end: None, + outcome: None, + }; + for (earlier, later) in [ + (1_792_920_379_100, 1_792_920_379_589), + (1_792_920_379_589, 1_792_920_380), + ] { + let ordered = canonicalize_session_turns(vec![make_turn(later), make_turn(earlier)]); + assert_eq!(ordered[0].timestamp, earlier); + assert_eq!(ordered[1].timestamp, later); + assert_eq!(ordered[0].turn_number, 0); + assert_eq!(ordered[1].turn_number, 1); + } + let parent = make_turn(1_792_920_380); + let mut child = make_turn(1_792_920_379_589); + child.previous_provenance = Some(parent.provenance_hash); + let ordered = canonicalize_session_turns(vec![child.clone(), parent.clone()]); + assert_eq!(ordered[0].provenance_hash, parent.provenance_hash); + assert_eq!(ordered[1].provenance_hash, child.provenance_hash); + } + #[test] fn test_session_turn_order_is_independent_of_ingestion_order() { let parent_hash = Hash::of(b"parent"); diff --git a/atomic-core/src/pristine/txn/write/mod.rs b/atomic-core/src/pristine/txn/write/mod.rs index de59c823..1f88c9dd 100644 --- a/atomic-core/src/pristine/txn/write/mod.rs +++ b/atomic-core/src/pristine/txn/write/mod.rs @@ -445,7 +445,11 @@ impl<'a> WriteTxn<'a> { } record.latest_provenance = Some(turn.provenance_hash); record.turn_count = turn.turn_number.saturating_add(1); - record.started_at = record.started_at.min(turn.timestamp); + if crate::types::timestamp::epoch_millis(turn.timestamp) + < crate::types::timestamp::epoch_millis(record.started_at) + { + record.started_at = turn.timestamp; + } { let mut sessions = self.txn.open_table(SESSIONS)?; let bytes = record.to_bytes(); @@ -553,7 +557,11 @@ impl<'a> WriteTxn<'a> { record.latest_provenance = turns.last().map(|turn| turn.provenance_hash); record.turn_count = turns.len() as u32; if let Some(first) = turns.first() { - record.started_at = record.started_at.min(first.timestamp); + if crate::types::timestamp::epoch_millis(first.timestamp) + < crate::types::timestamp::epoch_millis(record.started_at) + { + record.started_at = first.timestamp; + } } { let mut sessions = self.txn.open_table(SESSIONS)?; @@ -833,7 +841,7 @@ impl<'a> WriteTxn<'a> { first_provenance: None, latest_provenance: None, turn_count: 0, - started_at: chrono::Utc::now().timestamp(), + started_at: chrono::Utc::now().timestamp_millis(), ended_at: None, status: crate::change::session::SessionStatus::Active, }; @@ -849,6 +857,8 @@ impl<'a> WriteTxn<'a> { /// session has no ledger entries yet; otherwise updates only the fields /// supplied, preserving ledger data (provenance links, turn count). /// `ended_at: None` on a re-entered (resumed) session clears a prior end. + /// New `ended_at` values use Unix epoch milliseconds; legacy seconds are + /// still accepted without changing historical records. pub fn upsert_session_lifecycle( &mut self, session_id: &str, @@ -874,7 +884,7 @@ impl<'a> WriteTxn<'a> { first_provenance: None, latest_provenance: None, turn_count: 0, - started_at: chrono::Utc::now().timestamp(), + started_at: chrono::Utc::now().timestamp_millis(), ended_at: None, status: crate::change::session::SessionStatus::Active, }, @@ -944,7 +954,7 @@ impl<'a> WriteTxn<'a> { first_provenance: None, latest_provenance: None, turn_count: 0, - started_at: chrono::Utc::now().timestamp(), + started_at: chrono::Utc::now().timestamp_millis(), ended_at: None, status: SessionStatus::Active, }, @@ -1048,9 +1058,9 @@ impl<'a> WriteTxn<'a> { } } -/// Format a Unix epoch milliseconds timestamp to RFC-3339 string. +/// Format an event timestamp, including legacy seconds, to RFC-3339. fn format_timestamp_ms(epoch_ms: i64) -> String { - chrono::DateTime::from_timestamp_millis(epoch_ms) + crate::types::timestamp::epoch_datetime(epoch_ms) .map(|dt| dt.to_rfc3339()) .unwrap_or_else(|| format!("{}", epoch_ms)) } diff --git a/atomic-core/src/pristine/txn/write/session_kg.rs b/atomic-core/src/pristine/txn/write/session_kg.rs index 16ee3650..acf0ff26 100644 --- a/atomic-core/src/pristine/txn/write/session_kg.rs +++ b/atomic-core/src/pristine/txn/write/session_kg.rs @@ -249,7 +249,8 @@ impl WriteTxn<'_> { } } self.kg_clear_finding("UnresolvedReference", &id)?; - let created_at = crate::pristine::vault::session_timestamp_datetime(turn.timestamp) + let created_at = turn + .timestamp_datetime() .ok_or_else(|| PristineError::Serialization { message: "session turn timestamp is outside the RDF dateTime range".into(), })? diff --git a/atomic-core/src/pristine/vault.rs b/atomic-core/src/pristine/vault.rs index 01cc71dd..4b08f7e5 100644 --- a/atomic-core/src/pristine/vault.rs +++ b/atomic-core/src/pristine/vault.rs @@ -479,18 +479,6 @@ pub fn parse_intent_reference( // ── Knowledge Graph ───────────────────────────────────────────── -// Legacy session epochs are untagged. Keep small values as seconds (including -// old fixtures near 1970); contemporary millisecond epochs exceed this bound. -const SESSION_MILLISECOND_THRESHOLD: u64 = 100_000_000_000; - -pub(crate) fn session_timestamp_datetime(timestamp: i64) -> Option> { - if timestamp.unsigned_abs() >= SESSION_MILLISECOND_THRESHOLD { - chrono::DateTime::from_timestamp_millis(timestamp) - } else { - chrono::DateTime::from_timestamp(timestamp, 0) - } -} - /// A node in the knowledge graph. /// /// Nodes represent repository artifacts: changes, files, entities, views, @@ -534,10 +522,13 @@ impl KgNode { .ok() }) .filter(|value| { - value.timestamp().unsigned_abs() >= SESSION_MILLISECOND_THRESHOLD + value.timestamp().unsigned_abs() + >= crate::types::timestamp::MILLISECOND_EPOCH_THRESHOLD }) { - if let Some(corrected) = session_timestamp_datetime(legacy.timestamp()) { + if let Some(corrected) = + crate::types::timestamp::epoch_datetime(legacy.timestamp()) + { *created_at = serde_json::json!(corrected.to_rfc3339()); } } @@ -707,12 +698,14 @@ mod tests { (0, "1970-01-01T00:00:00+00:00"), ] { assert_eq!( - session_timestamp_datetime(raw).unwrap().to_rfc3339(), + crate::types::timestamp::epoch_datetime(raw) + .unwrap() + .to_rfc3339(), expected ); } - assert!(session_timestamp_datetime(i64::MAX).is_none()); - assert!(session_timestamp_datetime(i64::MIN).is_none()); + assert!(crate::types::timestamp::epoch_datetime(i64::MAX).is_none()); + assert!(crate::types::timestamp::epoch_datetime(i64::MIN).is_none()); } #[test] diff --git a/atomic-core/src/types/mod.rs b/atomic-core/src/types/mod.rs index 457507e8..71a27eb2 100644 --- a/atomic-core/src/types/mod.rs +++ b/atomic-core/src/types/mod.rs @@ -28,6 +28,7 @@ mod node_id; mod operation_id; mod position; mod set_id; +pub mod timestamp; mod working_copy_id; pub use edge_kind::{Edge, EdgeKind, ForwardEdge, ParentEdge, ParentEdgeKind}; diff --git a/atomic-core/src/types/timestamp.rs b/atomic-core/src/types/timestamp.rs new file mode 100644 index 00000000..6e64e51c --- /dev/null +++ b/atomic-core/src/types/timestamp.rs @@ -0,0 +1,60 @@ +//! Compatibility for untagged agent timestamps: new writes use milliseconds, +//! while historical provenance and session records may contain seconds. + +use chrono::{DateTime, Utc}; + +/// Values below this magnitude retain the historical seconds interpretation. +/// Untagged timestamps near the epoch are inherently ambiguous; this convention +/// supports existing records without rewriting content-addressed payloads. +pub const MILLISECOND_EPOCH_THRESHOLD: u64 = 100_000_000_000; + +/// Interpret an agent timestamp as milliseconds, accepting legacy seconds. +/// +/// Suitable for comparisons across old and new records. The multiplication is +/// bounded by the threshold, so even `i64::MIN` and `i64::MAX` cannot overflow. +pub fn epoch_millis(timestamp: i64) -> i64 { + if timestamp.unsigned_abs() < MILLISECOND_EPOCH_THRESHOLD { + timestamp * 1000 + } else { + timestamp + } +} + +/// Decode current millisecond and historical second agent epochs without +/// modifying their serialized values. Values outside Chrono's range return None. +pub fn epoch_datetime(timestamp: i64) -> Option> { + DateTime::from_timestamp_millis(epoch_millis(timestamp)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn seconds_and_milliseconds_preserve_their_precision() { + for (raw, expected) in [ + (1_792_920_379, "2026-10-25T09:26:19+00:00"), + (1_792_920_379_589, "2026-10-25T09:26:19.589+00:00"), + (0, "1970-01-01T00:00:00+00:00"), + ] { + assert_eq!(epoch_datetime(raw).unwrap().to_rfc3339(), expected); + } + } + + #[test] + fn comparisons_handle_mixed_units_and_subsecond_actions() { + assert!(epoch_millis(1_792_920_379_589) < epoch_millis(1_792_920_380)); + assert!(epoch_millis(1_792_920_379_100) < epoch_millis(1_792_920_379_589)); + assert_eq!(epoch_millis(1_792_920_379), 1_792_920_379_000); + } + + #[test] + fn negative_and_extreme_values_do_not_overflow() { + assert_eq!(epoch_millis(-1), -1000); + assert_eq!(epoch_millis(-1_792_920_379_589), -1_792_920_379_589); + assert_eq!(epoch_millis(i64::MIN), i64::MIN); + assert_eq!(epoch_millis(i64::MAX), i64::MAX); + assert!(epoch_datetime(i64::MIN).is_none()); + assert!(epoch_datetime(i64::MAX).is_none()); + } +} diff --git a/atomic-repository/src/repository/changes.rs b/atomic-repository/src/repository/changes.rs index 8e224c96..2814753e 100644 --- a/atomic-repository/src/repository/changes.rs +++ b/atomic-repository/src/repository/changes.rs @@ -601,8 +601,8 @@ impl Repository { for (session_id, mut session_graphs) in by_session { session_graphs.sort_by(|(a_hash, a), (b_hash, b)| { - a.timestamp - .cmp(&b.timestamp) + atomic_core::types::timestamp::epoch_millis(a.timestamp) + .cmp(&atomic_core::types::timestamp::epoch_millis(b.timestamp)) .then_with(|| a_hash.as_bytes().cmp(b_hash.as_bytes())) }); @@ -1506,7 +1506,7 @@ impl Repository { } // Sort by timestamp (oldest first) for stable ordering. - graphs.sort_by_key(|(_, g)| g.timestamp); + graphs.sort_by_key(|(_, g)| atomic_core::types::timestamp::epoch_millis(g.timestamp)); Ok(graphs) } @@ -1543,7 +1543,7 @@ impl Repository { } // Sort by timestamp (oldest first) for chain reconstruction - graphs.sort_by_key(|(_, g)| g.timestamp); + graphs.sort_by_key(|(_, g)| atomic_core::types::timestamp::epoch_millis(g.timestamp)); Ok(graphs) } @@ -1612,11 +1612,13 @@ impl Repository { let activity = |record: &atomic_core::change::session::SessionRecord, turns: &[atomic_core::change::session::SessionTurn]| { - turns - .last() - .map(|turn| turn.timestamp) - .or(record.ended_at) - .unwrap_or(record.started_at) + atomic_core::types::timestamp::epoch_millis( + turns + .last() + .map(|turn| turn.timestamp) + .or(record.ended_at) + .unwrap_or(record.started_at), + ) }; activity(b_record, b_turns) .cmp(&activity(a_record, a_turns)) diff --git a/atomic-repository/src/triage/report.rs b/atomic-repository/src/triage/report.rs index 2e130576..8c0a180a 100644 --- a/atomic-repository/src/triage/report.rs +++ b/atomic-repository/src/triage/report.rs @@ -1336,7 +1336,9 @@ pub(crate) fn load_graphs( } // Most-recent first (a change explained by >1 graph shows all; the newest // leads). Both loaders sort/return unspecified order, so sort here. - graphs.sort_by_key(|(_, g)| std::cmp::Reverse(g.timestamp)); + graphs.sort_by_key(|(_, g)| { + std::cmp::Reverse(atomic_core::types::timestamp::epoch_millis(g.timestamp)) + }); Ok(graphs) } diff --git a/docs/knowledge-graph-phase1-upgrade.md b/docs/knowledge-graph-phase1-upgrade.md index aab2337e..a17d906a 100644 --- a/docs/knowledge-graph-phase1-upgrade.md +++ b/docs/knowledge-graph-phase1-upgrade.md @@ -68,3 +68,21 @@ Run the Phase 1 acceptance harness with a freshly built CLI: cargo build -p atomic-cli --bin atomic ATOMIC_BIN="$PWD/target/debug/atomic" bash tests/harness/46_knowledge_graph_closure.sh ``` + +## Agent timestamp precision (no KG upgrade) + +New agent activity timestamps use Unix epoch **milliseconds**: provenance nodes +and graph headers, session turns and lifecycle times, AI request metadata, and +turn-envelope timings. RPC timestamps retain that precision in their standard +seconds-plus-nanoseconds representation. Causal links remain authoritative for +ordering; clocks and milliseconds alone do not establish causality. + +Readers and chronological comparisons also accept historical second epochs. +Untagged values below 100 billion in absolute magnitude retain their historical +seconds interpretation; larger values are interpreted as milliseconds. Ambiguous +values near 1970 cannot be distinguished without a unit tag. New graph writers +use the explicit `timestamp_millis` setter; raw historical values remain intact. + +Existing incorrect cached session-turn dates are corrected in query results, +including read-only queries. This correction requires no generation change, +migration, or reindex and does not rewrite provenance bytes or historical hashes. diff --git a/libatomic/src/daemon/sandbox_wire.rs b/libatomic/src/daemon/sandbox_wire.rs index 5a5b6546..80357677 100644 --- a/libatomic/src/daemon/sandbox_wire.rs +++ b/libatomic/src/daemon/sandbox_wire.rs @@ -347,12 +347,12 @@ pub fn session_turn_from(bytes: Option<&pb::VersionedBytes>) -> Result Result { + let published_at = publication.turn.timestamp_datetime().ok_or_else(|| { + "session publication timestamp is outside the supported range".to_string() + })?; Ok(pb::ProvenancePublished { provenance_hash: Some(hash_proto(&publication.turn.provenance_hash)), - published_at: Some(prost_types::Timestamp { - seconds: publication.turn.timestamp, - nanos: 0, - }), + published_at: Some(super::convert::timestamp_proto(published_at)), manifest_hash: Some(hash_proto(&publication.manifest_hash)), publication: Some(versioned( PUBLICATION_SCHEMA, @@ -372,6 +372,34 @@ pub fn publication_from( mod tests { use super::*; + #[test] + fn publication_timestamp_preserves_milliseconds_and_original_turn_payload() { + for (timestamp, nanos) in [(1_792_920_379, 0), (1_792_920_379_589, 589_000_000)] { + let publication = SessionCheckpointPublication { + turn: SessionTurn { + session_id: "timestamp-wire".into(), + turn_number: 0, + goal: None, + provenance_hash: Hash::of(b"provenance"), + change_hashes: Vec::new(), + previous_provenance: None, + timestamp, + plan_id: None, + todos: Vec::new(), + boundary_start: None, + boundary_end: None, + outcome: None, + }, + manifest_hash: Hash::of(b"manifest"), + }; + let wire = publication_proto(&publication).unwrap(); + let published = wire.published_at.as_ref().unwrap(); + assert_eq!(published.seconds, 1_792_920_379); + assert_eq!(published.nanos, nanos); + assert_eq!(publication_from(&wire).unwrap(), publication); + } + } + #[test] fn every_rejection_survives_the_wire() { for rejection in [ diff --git a/libatomic/src/daemon/services_agent.rs b/libatomic/src/daemon/services_agent.rs index 4a7b357f..1ec4275f 100644 --- a/libatomic/src/daemon/services_agent.rs +++ b/libatomic/src/daemon/services_agent.rs @@ -4247,18 +4247,19 @@ impl provenance_service_server::ProvenanceService for ProvenanceImpl { .unwrap_or(0) }; intent_counts.push(intent_count as u32); + let started_at = + atomic_core::types::timestamp::epoch_datetime(record.started_at) + .map(super::convert::timestamp_proto); + let ended_at = record + .ended_at + .and_then(atomic_core::types::timestamp::epoch_datetime) + .map(super::convert::timestamp_proto); SessionSummary { session_id: record.session_id, view: record.view_name, turn_count: record.turn_count.max(turns.len() as u32), - started_at: Some(prost_types::Timestamp { - seconds: record.started_at, - nanos: 0, - }), - ended_at: record.ended_at.map(|ended| prost_types::Timestamp { - seconds: ended, - nanos: 0, - }), + started_at, + ended_at, } }) .collect::>(); @@ -4790,7 +4791,9 @@ async fn export_provenance_impl( ), )); } - graphs.sort_by_key(|(_, graph)| std::cmp::Reverse(graph.timestamp)); + graphs.sort_by_key(|(_, graph)| { + std::cmp::Reverse(atomic_core::types::timestamp::epoch_millis(graph.timestamp)) + }); let mut bundles = Vec::with_capacity(graphs.len()); for (graph_hash, graph) in &graphs { let (input, previous) = provenance_map_input(&repo, graph, &change_hash, &person_did); diff --git a/libatomic/src/daemon/services_query.rs b/libatomic/src/daemon/services_query.rs index ebb29094..a5502305 100644 --- a/libatomic/src/daemon/services_query.rs +++ b/libatomic/src/daemon/services_query.rs @@ -692,6 +692,9 @@ fn vault_turn_intent(repo: &Repository, session_id: &str, turn_number: u32) -> O /// Map a stored session turn to the wire shape (the fields the ledger owns: /// turn number, provenance hash, and the turn timestamp). fn stored_turn_proto(turn: atomic_core::change::session::SessionTurn) -> StoredProvenanceTurn { + let created_at = turn + .timestamp_datetime() + .map(super::convert::timestamp_proto); StoredProvenanceTurn { schema_version: 1, provenance_id: 0, @@ -700,10 +703,7 @@ fn stored_turn_proto(turn: atomic_core::change::session::SessionTurn) -> StoredP state: ProvenanceTurnState::Unspecified as i32, generation: 0, next_event_seq: 0, - created_at: Some(prost_types::Timestamp { - seconds: turn.timestamp, - nanos: 0, - }), + created_at, updated_at: None, completed_at: None, final_hash: Some(hash_proto(&turn.provenance_hash)), @@ -712,6 +712,34 @@ fn stored_turn_proto(turn: atomic_core::change::session::SessionTurn) -> StoredP } } +#[cfg(test)] +mod timestamp_tests { + use super::*; + + #[test] + fn stored_turn_timestamp_preserves_milliseconds_and_accepts_legacy_seconds() { + for (timestamp, nanos) in [(1_792_920_379, 0), (1_792_920_379_589, 589_000_000)] { + let turn = atomic_core::change::session::SessionTurn { + session_id: "timestamp-wire".into(), + turn_number: 0, + goal: None, + provenance_hash: atomic_core::types::Hash::of(b"provenance"), + change_hashes: Vec::new(), + previous_provenance: None, + timestamp, + plan_id: None, + todos: Vec::new(), + boundary_start: None, + boundary_end: None, + outcome: None, + }; + let created = stored_turn_proto(turn).created_at.unwrap(); + assert_eq!(created.seconds, 1_792_920_379); + assert_eq!(created.nanos, nanos); + } + } +} + // --------------------------------------------------------------------------- // ListViews — `atomic view list` // ---------------------------------------------------------------------------