Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 18 additions & 31 deletions knowledge-runtime/src/runtime/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ impl KnowledgeRuntime {
})
}

/// Wire a bitemporal record source so the temporal route answers as-of
/// version selection from `bitemporal-runtime` (empty = existing behavior).
/// Attach process-local reference records for [`Self::reference_bitemporal_as_of`].
/// These records have no scope or search-source identity and are never
/// used by scoped or ranked search.
pub fn with_bitemporal_records(
mut self,
records: Vec<bitemporal_runtime::BitemporalRecord<String>>,
Expand All @@ -204,6 +205,20 @@ impl KnowledgeRuntime {
self
}

/// Select a snapshot of the caller-supplied reference records. This is not
/// a scoped search, a fact lookup, or a read from the persisted owner.
pub fn reference_bitemporal_as_of(
&self,
valid_at: chrono::DateTime<chrono::Utc>,
recorded_at_or_before: chrono::DateTime<chrono::Utc>,
) -> Vec<bitemporal_runtime::BitemporalRecord<String>> {
crate::temporal::bitemporal_bridge::as_of(
&self.bitemporal_records,
valid_at,
recorded_at_or_before,
)
}

pub(crate) fn adapter_ref(&self) -> &SemanticMemoryAdapter {
&self.adapter
}
Expand Down Expand Up @@ -459,35 +474,7 @@ impl KnowledgeRuntime {
trace.recorded_as_of = Some(recorded_at_or_before.to_string());
trace.temporal_mode = Some(temporal_mode.to_string());

let mut results: Vec<SearchResult> = merged.results.into_iter().map(|m| m.result).collect();
if !self.bitemporal_records.is_empty() {
if let (Ok(v), Ok(r)) = (
chrono::DateTime::parse_from_rfc3339(valid_at),
chrono::DateTime::parse_from_rfc3339(recorded_at_or_before),
) {
let asof = crate::temporal::bitemporal_bridge::as_of(
&self.bitemporal_records,
v.with_timezone(&chrono::Utc),
r.with_timezone(&chrono::Utc),
);
if !asof.is_empty() {
results = asof
.into_iter()
.map(|rec| SearchResult {
content: rec.value,
source: semantic_memory::SearchSource::Fact {
fact_id: rec.id,
namespace: scope.namespace.clone(),
},
score: 1.0,
bm25_rank: None,
vector_rank: None,
cosine_similarity: None,
})
.collect();
}
}
}
let results = merged.results.into_iter().map(|m| m.result).collect();
Ok((results, trace))
}

Expand Down
76 changes: 43 additions & 33 deletions knowledge-runtime/tests/bitemporal_route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,45 +39,55 @@ fn runtime(dir: &std::path::Path, records: Vec<BitemporalRecord<String>>) -> Kno
.with_bitemporal_records(records)
}

#[tokio::test]
async fn temporal_route_consumes_bitemporal_records() {
#[test]
fn explicit_reference_snapshot_respects_both_time_coordinates() {
let dir = tempfile::tempdir().unwrap();
let rt = runtime(
dir.path(),
vec![rec("x", 100, 100, "old"), rec("x", 200, 200, "new")],
);
let cutoff = |seconds| chrono::Utc.timestamp_opt(seconds, 0).unwrap();
for (valid, recorded, expected) in [(150, 250, "old"), (250, 150, "old"), (250, 250, "new")] {
let hits = rt.reference_bitemporal_as_of(cutoff(valid), cutoff(recorded));
assert_eq!(hits.len(), 1);
assert_eq!(hits[0].id, "x");
assert_eq!(hits[0].value, expected);
}
}

// As of valid=150, recorded=250 -> only "old" was valid.
let (early, trace) = rt
.query_temporal_with_trace(
"what changed",
None,
None,
"1970-01-01T00:02:30Z", // 150s
"1970-01-01T00:04:10Z", // 250s
)
.await
.expect("temporal query should succeed");
assert!(!early.is_empty(), "expected a bitemporal result");
assert_eq!(
early[0].content, "old",
"as-of must select the older version"
#[tokio::test]
async fn reference_snapshot_does_not_fabricate_search_hits_across_scopes() {
let dir = tempfile::tempdir().unwrap();
let rt = runtime(
dir.path(),
vec![
rec("amber", 100, 100, "amber deployment status"),
rec("indigo", 100, 100, "indigo inventory status"),
rec("amber", 90, 200, "amber retroactive correction"),
],
);
assert!(trace.temporal_mode.as_deref().is_some());
let default = Scope::new("ns");
let other = Scope::new("unrelated-namespace");

// As of valid=250 -> "new".
let (late, _) = rt
.query_temporal_with_trace(
"what changed",
None,
None,
"1970-01-01T00:04:10Z",
"1970-01-01T00:04:10Z",
)
.await
.expect("temporal query should succeed");
assert_eq!(
late[0].content, "new",
"as-of must select the newer version"
);
// The adapter has no imports or facts in either scope. A reference vector
// has no scope, search ranking or dereferenceable fact IDs.
for (scope, query, recorded) in [
(&default, "amber deployment", "1970-01-01T00:02:30Z"),
(&other, "indigo inventory", "1970-01-01T00:04:10Z"),
] {
let (hits, trace) = rt
.query_temporal_with_trace(query, Some(scope), None, "1970-01-01T00:02:30Z", recorded)
.await
.expect("permissive route explicitly degrades when imports are absent");
assert_eq!(
hits.len(),
trace.merged_result_count,
"trace must describe the results actually returned"
);
assert!(
hits.is_empty(),
"unscoped reference records are not search hits"
);
assert_eq!(trace.temporal_mode.as_deref(), Some("downgraded"));
}
}
Loading
Loading