You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maka.apache.org by wy...@apache.org on 2026/10/17 17:44:06 UTC
(maka) branch rust-preview updated: perf(event-log): reuse authenticated checkpoint prefixes
This is an automated email from the ASF dual-hosted git repository.
M4n5ter pushed a commit to branch rust-preview
in repository https://gitbox.apache.org/repos/asf/maka.git
The following commit(s) were added to refs/heads/rust-preview by this push:
new c3b40b27cd perf(event-log): reuse authenticated checkpoint prefixes
c3b40b27cd is described below
commit c3b40b27cda95c0a45c7e2571ded51eb9fd20c74
Author: Wang <m4...@gmail.com>
AuthorDate: Sun Oct 18 01:43:59 2026 +0800
perf(event-log): reuse authenticated checkpoint prefixes
Reuse verified checkpoint prefixes within proven SQLite snapshots, with full validation when hints are unavailable. Preserve archive, replay, and external writer invalidation boundaries.
Five paired production-release runs reduce 100-cycle compaction wall time by 80.79% and 100k-turn cold-owner continuation by 12.29%, with approximately 2% high-concurrency short-turn overhead.
Generated-by: Codex
---
Cargo.lock | 1 +
platform/event-log/Cargo.toml | 2 +-
platform/event-log/README.md | 2 +
platform/event-log/README.zh-CN.md | 2 +
platform/event-log/src/append.rs | 78 +-
platform/event-log/src/append/history.rs | 15 +-
platform/event-log/src/archive.rs | 2 +-
platform/event-log/src/archive/accepted.rs | 8 +-
platform/event-log/src/archive/candidates.rs | 26 +-
platform/event-log/src/archive/effective.rs | 39 +-
platform/event-log/src/bundle/validation.rs | 8 +-
platform/event-log/src/connection.rs | 66 +-
platform/event-log/src/context.rs | 1 +
platform/event-log/src/context/cache.rs | 434 +++++++
platform/event-log/src/context/cache/tests.rs | 1213 ++++++++++++++++++++
platform/event-log/src/context/evidence.rs | 20 +-
platform/event-log/src/context/evidence/scan.rs | 63 +-
platform/event-log/src/context/read.rs | 80 +-
platform/event-log/src/context/request.rs | 22 +-
platform/event-log/src/context/selection.rs | 2 +-
platform/event-log/src/context/validate.rs | 37 +-
platform/event-log/src/lib.rs | 7 +-
.../event-log/tests/archive/lineage_checkpoint.rs | 117 ++
products/maka/cli/notices.json | 4 +-
scripts/rust/README.md | 2 +
scripts/rust/README.zh-CN.md | 2 +
26 files changed, 2179 insertions(+), 74 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
index 336e4f7e38..a8a64cb329 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8018,6 +8018,7 @@ version = "0.37.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1f111c8c41e7c61a49cd34e44c7619462967221a6443b0ec299e0ac30cfb9b1"
dependencies = [
+ "bindgen 0.72.1",
"cc",
"pkg-config",
"vcpkg",
diff --git a/platform/event-log/Cargo.toml b/platform/event-log/Cargo.toml
index e09e59596b..20e7aa6965 100644
--- a/platform/event-log/Cargo.toml
+++ b/platform/event-log/Cargo.toml
@@ -36,7 +36,7 @@ schemars.workspace = true
uuid.workspace = true
maka-presentation.workspace = true
rusqlite = { workspace = true, features = ["functions"] }
-sqlx.workspace = true
+sqlx = { workspace = true, features = ["sqlite-preupdate-hook"] }
futures-util.workspace = true
serde_json.workspace = true
regex = "1.13.1"
diff --git a/platform/event-log/README.md b/platform/event-log/README.md
index 37ccef4771..18ffd25a38 100644
--- a/platform/event-log/README.md
+++ b/platform/event-log/README.md
@@ -27,4 +27,6 @@ Original operation contracts are stored once by digest. Operation requests and r
Checkpoint reads validate every raw source commitment, including predecessors hidden by the latest summary. Prefix digests bind the scope, ordered sequence/byte-length/raw-byte records, then the high-water suffix; identical selections can share a streamed scan across multiple cuts. Lineage selections and archive request fences remain independently checked. This internal encoding has no legacy compatibility path.
+An open store can reuse a verified checkpoint chain and extend its authenticated prefix within the same selection. Fresh writer confirmations bind that evidence to the reader's pinned snapshot or the writer's locked transaction; protected history mutations, external commits, schema changes and failed writes revoke stale evidence. Selection, admission, settlement and archive validation remain fresh. Opening-only reads use full validation without warming a certificate. These disposable cer [...]
+
[Source](src/lib.rs) · [Cargo manifest](Cargo.toml)
diff --git a/platform/event-log/README.zh-CN.md b/platform/event-log/README.zh-CN.md
index fb7313f60b..578cb042ec 100644
--- a/platform/event-log/README.zh-CN.md
+++ b/platform/event-log/README.zh-CN.md
@@ -27,4 +27,6 @@
读取 checkpoint 时会校验每个原始来源承诺,包括被最新摘要隐藏的前驱。前缀摘要依次绑定作用域、有序的序号/字节长度/原始字节记录,最后绑定 high-water;相同选择范围的多个边界可共用一次流式扫描。继承链选择和归档请求边界仍分别校验。该内部编码不保留旧格式兼容路径。
+存储打开期间,同一选择范围可复用已验证的 checkpoint 链,并延伸已认证的前缀。写连接的实时确认将证明绑定到读连接的固定快照或已取得写锁的事务;受保护历史的变更、外部提交、schema 变化及写入失败会撤销失效证明。选择范围、准入、结算和归档仍实时校验。仅含 opening 的读取直接完整校验,不预热证明。这些可丢弃的证明不持久化,也不授予权限。
+
[源码](src/lib.rs) · [Cargo 配置](Cargo.toml)
diff --git a/platform/event-log/src/append.rs b/platform/event-log/src/append.rs
index b3f1d2e8d9..2ccbfea0d4 100644
--- a/platform/event-log/src/append.rs
+++ b/platform/event-log/src/append.rs
@@ -70,10 +70,55 @@ impl EventLog {
self.validate_root()?;
let events = events.to_vec();
let commits = self.commits.clone();
+ let cache = self.context_cache.clone();
+ let ingress = self.connection.writer();
self.connection
.run(move |connection| {
Box::pin(async move {
+ let context = if let Some(consumer) =
+ events.iter().find(|write| match &write.event().fact {
+ Fact::ContextCheckpointRecorded { .. } => true,
+ Fact::ModelRequested {
+ purpose,
+ checkpoint_event_id,
+ source_scope,
+ ..
+ } => {
+ checkpoint_event_id.is_some()
+ || *purpose == maka_agent_facts::context::ModelPurpose::Summary
+ || (matches!(
+ source_scope,
+ maka_agent_facts::event::LogScope::Lineage { .. }
+ ) && cache.has_raw(&write.event().invocation.session_id))
+ }
+ _ => false,
+ }) {
+ let origin = cache.stamp(connection, ingress.failures()).await?;
+ let certificate =
+ cache.get(&consumer.event().invocation.session_id, &origin)?;
+ Some((origin, certificate))
+ } else {
+ None
+ };
let mut transaction = connection.begin_with("BEGIN IMMEDIATE").await?;
+ let (origin, mut verification) = if let Some((origin, certificate)) = context {
+ let certificate = if cache
+ .confirm_append(&mut transaction, &origin, ingress.failures())
+ .await?
+ {
+ certificate
+ } else {
+ None
+ };
+ let verification = crate::context::cache::Verification::new(
+ cache.clone(),
+ origin.clone(),
+ certificate,
+ );
+ (Some(origin), verification)
+ } else {
+ (None, crate::context::cache::Verification::default())
+ };
let mut sequences = Vec::with_capacity(events.len());
let mut last_inserted = None;
for event in &events {
@@ -88,7 +133,9 @@ impl EventLog {
{
return Err(StoreError::PermissionRevoked);
}
- let result = Self::append_in_transaction(&mut transaction, event).await?;
+ let result =
+ Self::append_verified(&mut transaction, event, &mut verification)
+ .await?;
let sequence = match result {
AppendResult::Existing(sequence) => sequence,
AppendResult::Inserted(sequence) => {
@@ -105,6 +152,20 @@ impl EventLog {
.await
.map_err(StoreError::CommitUnknown)?;
commits.send_replace(sequence);
+ // Optional cache work cannot change a confirmed commit's outcome.
+ if let Some(origin) = origin
+ && let Some(candidate) = verification.candidate()
+ {
+ match cache.stamp(connection, ingress.failures()).await {
+ Ok(stamp) if stamp == origin => {
+ if cache.publish(candidate).is_err() {
+ cache.invalidate();
+ }
+ }
+ Ok(_) => {}
+ Err(_) => cache.invalidate(),
+ }
+ }
} else {
transaction.rollback().await?;
}
@@ -118,6 +179,19 @@ impl EventLog {
pub(crate) async fn append_in_transaction(
transaction: &mut SqliteConnection,
write: &EventWrite,
+ ) -> Result<AppendResult, StoreError> {
+ Self::append_verified(
+ transaction,
+ write,
+ &mut crate::context::cache::Verification::default(),
+ )
+ .await
+ }
+
+ async fn append_verified(
+ transaction: &mut SqliteConnection,
+ write: &EventWrite,
+ verification: &mut crate::context::cache::Verification,
) -> Result<AppendResult, StoreError> {
let event = write.event();
crate::recovery::require_local(transaction, &event.invocation.invocation_id).await?;
@@ -154,7 +228,7 @@ impl EventLog {
return Err(StoreError::SessionRetired);
}
let id = &event.invocation.invocation_id;
- history::validate(transaction, event).await?;
+ history::validate_cached(transaction, event, verification).await?;
if matches!(event.fact, Fact::InvocationOpened { .. }) {
crate::sessions::retain(transaction, &event.invocation.session_id).await?;
let archived: bool = sqlx::query_scalar(
diff --git a/platform/event-log/src/append/history.rs b/platform/event-log/src/append/history.rs
index 0706a94707..665775fe8b 100644
--- a/platform/event-log/src/append/history.rs
+++ b/platform/event-log/src/append/history.rs
@@ -32,6 +32,19 @@ use sqlx::SqliteConnection;
pub(crate) async fn validate(
connection: &mut SqliteConnection,
event: &RuntimeEvent,
+) -> Result<(), StoreError> {
+ validate_cached(
+ connection,
+ event,
+ &mut crate::context::cache::Verification::default(),
+ )
+ .await
+}
+
+pub(crate) async fn validate_cached(
+ connection: &mut SqliteConnection,
+ event: &RuntimeEvent,
+ verification: &mut crate::context::cache::Verification,
) -> Result<(), StoreError> {
let model_failure = match &event.fact {
Fact::ModelInterrupted { model_failure, .. }
@@ -52,7 +65,7 @@ pub(crate) async fn validate(
crate::continuation::validate(connection, event).await?;
crate::handoff::validate_history(connection, event).await?;
crate::steering::validate_append(connection, event).await?;
- crate::context::validate_append(connection, event).await?;
+ crate::context::validate_append(connection, event, verification).await?;
crate::archive::validate_append(connection, event).await?;
outcomes(connection, event).await
}
diff --git a/platform/event-log/src/archive.rs b/platform/event-log/src/archive.rs
index 45b3aad772..1417319c94 100644
--- a/platform/event-log/src/archive.rs
+++ b/platform/event-log/src/archive.rs
@@ -25,7 +25,7 @@ pub(crate) use accepted::archived;
pub(crate) use accepted::validate_append;
pub(crate) use accepted::verify_replay;
-pub(crate) use effective::{digest_selected, validate_summary};
+pub(crate) use effective::{digest_selected, validate_selected, validate_summary};
use maka_agent_facts::tool_output::DurableToolProjection;
diff --git a/platform/event-log/src/archive/accepted.rs b/platform/event-log/src/archive/accepted.rs
index dc6c9f4270..8cbf7cdd8c 100644
--- a/platform/event-log/src/archive/accepted.rs
+++ b/platform/event-log/src/archive/accepted.rs
@@ -95,7 +95,13 @@ pub(crate) async fn validate_append(
}
let selection = Selection::for_invocation(connection, &event.invocation).await?;
validate_selection(connection, &selection, &target.event_id).await?;
- let baseline = read::latest_selected(connection, &selection, i64::MAX as u64).await?;
+ let baseline = read::latest_selected(
+ connection,
+ &selection,
+ i64::MAX as u64,
+ &mut crate::context::cache::Verification::default(),
+ )
+ .await?;
if baseline
.as_ref()
.is_some_and(|b| target.sequence <= b.checkpoint.covered_through)
diff --git a/platform/event-log/src/archive/candidates.rs b/platform/event-log/src/archive/candidates.rs
index d118a6f180..3312e3cfd7 100644
--- a/platform/event-log/src/archive/candidates.rs
+++ b/platform/event-log/src/archive/candidates.rs
@@ -20,7 +20,7 @@
use super::{PruneCandidate, PruneCandidates, PruneCursor, target};
use crate::{
EventLog, StoreError,
- context::{read, safety, selection::Selection},
+ context::{cache, read, safety, selection::Selection},
};
use maka_agent_facts::event::RuntimeEvent;
use sqlx::{Connection, SqliteConnection};
@@ -41,7 +41,12 @@ impl EventLog {
}
let (session, current) = (session.to_owned(), current.map(str::to_owned));
let cursor = cursor.cloned();
+ let cache = self.context_cache.clone();
+ let writer = self.connection.writer();
self.connection.read_interruptible(move |connection| Box::pin(async move {
+ let observation = if max_candidates > 0 && cache.has_anchor(&session) {
+ Some(cache::observe(cache.clone(), &writer, session.clone()).await?)
+ } else { None };
let mut tx = connection.begin().await?;
if let Some(id) = ¤t {
let live: bool = sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM runtime_events o WHERE invocation_id=? AND kind='invocation_opened'
@@ -53,7 +58,7 @@ impl EventLog {
safety::require_safe(&mut tx, &session, current.as_deref()).await?;
let mut output = PruneCandidates { candidates: Vec::new(), next: None };
// The admission preflight checks safety without materializing history.
- if max_candidates > 0 {
+ let promotion = if max_candidates > 0 {
let selection = if let Some(id) = ¤t {
let json: String = sqlx::query_scalar("SELECT event_json FROM runtime_events WHERE invocation_id=? AND kind='invocation_opened'")
.bind(id).fetch_one(&mut *tx).await?;
@@ -62,17 +67,30 @@ impl EventLog {
} else {
Selection::session(&session)
};
- let baseline = read::latest_selected(&mut tx, &selection, i64::MAX as u64).await?;
+ let (origin, mut verification) = if let Some((origin, certificate)) = observation {
+ let (confirmed, _) = cache::observe(cache.clone(), &writer, session.clone()).await?;
+ let certificate = certificate.filter(|_| confirmed == origin);
+ let verification = cache::Verification::new(cache.clone(), origin.clone(), certificate);
+ (Some(origin), verification)
+ } else { (None, cache::Verification::default()) };
+ let baseline = read::latest_selected(&mut tx, &selection, i64::MAX as u64, &mut verification).await?;
let covered = baseline.as_ref().map_or(0, |b| b.checkpoint.covered_through);
let through = selection.high_water(&mut tx, i64::MAX as u64).await?;
+ if baseline.is_some() {
+ super::validate_selected(&mut tx, &selection, through, i64::MAX as u64).await?;
+ }
let mut cursor = cursor.unwrap_or(PruneCursor { through, after_sequence: covered });
if cursor.after_sequence > cursor.through || cursor.through > through {
return Err(StoreError::InvalidTransition("invalid prune scan fence".into()));
}
cursor.after_sequence = cursor.after_sequence.max(covered);
scan(&mut tx, &selection, cursor, max_candidates, max_bytes, &mut output).await?;
- }
+ origin.map(|origin| (origin, verification.candidate()))
+ } else { None };
tx.commit().await?;
+ if let Some((origin, candidate)) = promotion {
+ cache::promote(cache, &writer, origin, candidate).await?;
+ }
Ok(output)
})).await
}
diff --git a/platform/event-log/src/archive/effective.rs b/platform/event-log/src/archive/effective.rs
index 5d75d99619..b502799112 100644
--- a/platform/event-log/src/archive/effective.rs
+++ b/platform/event-log/src/archive/effective.rs
@@ -32,6 +32,36 @@ pub(crate) async fn digest_selected(
source: &SourceEvidence,
before: u64,
) -> Result<String, StoreError> {
+ let mut hash = Sha256::new();
+ hash.update(b"maka.effective-context.v1\0");
+ hash.update(source.digest.as_bytes());
+ fold_selected(connection, selection, source.high_water, before, &mut hash).await?;
+ Ok(format!("sha256:{}", hex::encode(hash.finalize())))
+}
+
+pub(crate) async fn validate_selected(
+ connection: &mut SqliteConnection,
+ selection: &Selection,
+ high_water: u64,
+ before: u64,
+) -> Result<(), StoreError> {
+ fold_selected(
+ connection,
+ selection,
+ high_water,
+ before,
+ &mut Sha256::new(),
+ )
+ .await
+}
+
+async fn fold_selected(
+ connection: &mut SqliteConnection,
+ selection: &Selection,
+ high_water: u64,
+ before: u64,
+ hash: &mut Sha256,
+) -> Result<(), StoreError> {
let session = selection.session.as_deref().ok_or(ArchiveError::Corrupt)?;
let archive_filter = Selection::predicate("a", "?4");
let target_filter = Selection::predicate("t", "?5");
@@ -49,7 +79,7 @@ pub(crate) async fn digest_selected(
AND (t.kind!='tool_settled' OR a.kind IS NOT 'tool_result_archived' OR a.sequence<=t.sequence
OR json_extract(a.event_json,'$.fact.placeholder.identity.runtime_event_id') IS NOT t.event_id))"
))).bind(before as i64).bind(session).bind(session).bind(&selection.lineage)
- .bind(source.high_water as i64).fetch_one(&mut *connection).await?;
+ .bind(high_water as i64).fetch_one(&mut *connection).await?;
if broken {
return Err(ArchiveError::Corrupt.into());
}
@@ -66,9 +96,6 @@ pub(crate) async fn digest_selected(
"{} ORDER BY sequence LIMIT 129",
branches.join(" UNION ALL ")
);
- let mut hash = Sha256::new();
- hash.update(b"maka.effective-context.v1\0");
- hash.update(source.digest.as_bytes());
let mut after = 0_i64;
loop {
let rows: Vec<(i64, String, String, i64)> =
@@ -76,7 +103,7 @@ pub(crate) async fn digest_selected(
.bind(before as i64)
.bind(session)
.bind(after)
- .bind(source.high_water as i64)
+ .bind(high_water as i64)
.bind(&selection.lineage)
.fetch_all(&mut *connection)
.await?;
@@ -110,7 +137,7 @@ pub(crate) async fn digest_selected(
after = sequence;
}
}
- Ok(format!("sha256:{}", hex::encode(hash.finalize())))
+ Ok(())
}
pub(crate) async fn affected_after(
diff --git a/platform/event-log/src/bundle/validation.rs b/platform/event-log/src/bundle/validation.rs
index 2a99795a87..e961f48164 100644
--- a/platform/event-log/src/bundle/validation.rs
+++ b/platform/event-log/src/bundle/validation.rs
@@ -226,7 +226,13 @@ async fn request(
if source.digest != *source_digest {
return Err(invalid("request original source digest mismatch"));
}
- let baseline = context::read::latest_selected(db, &selection, *source_high_water).await?;
+ let baseline = context::read::latest_selected(
+ db,
+ &selection,
+ *source_high_water,
+ &mut context::cache::Verification::default(),
+ )
+ .await?;
if baseline.as_ref().map(|b| &b.event_id) != checkpoint_event_id.as_ref() {
return Err(invalid("request original baseline mismatch"));
}
diff --git a/platform/event-log/src/connection.rs b/platform/event-log/src/connection.rs
index 4e33b26c15..a592c04f14 100644
--- a/platform/event-log/src/connection.rs
+++ b/platform/event-log/src/connection.rs
@@ -23,7 +23,7 @@ use std::{
pin::Pin,
sync::{
Arc,
- atomic::{AtomicBool, Ordering},
+ atomic::{AtomicBool, AtomicU64, Ordering},
},
};
@@ -57,6 +57,7 @@ enum Command {
/// Ingress is bounded; accepted work and the database lease belong to a dedicated
/// thread, independent of the caller's executor. Dropping ingress drains work.
pub struct OwnedConnection {
+ failures: Arc<AtomicU64>,
jobs: mpsc::Sender<Command>,
read_jobs: mpsc::Sender<ReadJob>,
read_stop: CancellationToken,
@@ -65,7 +66,54 @@ pub struct OwnedConnection {
changes: watch::Receiver<()>,
}
+/// Short writer observations can be sent from a reader without retaining a
+/// second database owner or its reader/shutdown authority.
+#[derive(Clone)]
+pub(crate) struct WriterIngress {
+ jobs: mpsc::Sender<Command>,
+ failures: Arc<AtomicU64>,
+}
+
+impl WriterIngress {
+ pub(crate) fn failures(&self) -> u64 {
+ self.failures.load(Ordering::Acquire)
+ }
+
+ pub(crate) async fn run<T: Send + 'static>(
+ &self,
+ operation: impl for<'c> FnOnce(&'c mut SqliteConnection) -> BoxFuture<'c, Result<T, StoreError>>
+ + Send
+ + 'static,
+ ) -> Result<T, StoreError> {
+ let (reply, result) = oneshot::channel();
+ let failures = self.failures.clone();
+ self.jobs
+ .send(Command::Run(Box::new(move |connection, valid| {
+ Box::pin(async move {
+ let result = match valid {
+ Ok(()) => operation(connection).await,
+ Err(error) => Err(error),
+ };
+ if result.is_err() {
+ failures.fetch_add(1, Ordering::AcqRel);
+ }
+ let _ = reply.send(result);
+ })
+ })))
+ .await
+ .map_err(|_| StoreError::ConnectionClosed)?;
+ result.await.map_err(|_| StoreError::OperationUnknown)?
+ }
+}
+
impl OwnedConnection {
+ pub(crate) fn writer(&self) -> WriterIngress {
+ WriterIngress {
+ jobs: self.jobs.clone(),
+ failures: self.failures.clone(),
+ }
+ }
+
/// Disposable write hints. Re-read canonical state after each wakeup;
/// rolled-back work may also notify. Only the original worker owns ingress.
pub fn subscribe_changes(&self) -> watch::Receiver<()> {
@@ -203,6 +251,7 @@ impl OwnedConnection {
})?;
match startup.await {
Ok(Ok(())) => Ok(Self {
+ failures: Arc::new(AtomicU64::new(0)),
jobs,
read_jobs,
read_stop,
@@ -262,20 +311,7 @@ impl OwnedConnection {
+ Send
+ 'static,
) -> Result<T, StoreError> {
- let (reply, result) = oneshot::channel();
- self.jobs
- .send(Command::Run(Box::new(move |connection, valid| {
- Box::pin(async move {
- let result = match valid {
- Ok(()) => operation(connection).await,
- Err(error) => Err(error),
- };
- let _ = reply.send(result);
- })
- })))
- .await
- .map_err(|_| StoreError::ConnectionClosed)?;
- result.await.map_err(|_| StoreError::OperationUnknown)?
+ self.writer().run(operation).await
}
pub async fn shutdown(&self) -> Result<(), StoreError> {
diff --git a/platform/event-log/src/context.rs b/platform/event-log/src/context.rs
index 69fa57b5c5..d40d45363a 100644
--- a/platform/event-log/src/context.rs
+++ b/platform/event-log/src/context.rs
@@ -18,6 +18,7 @@
*/
mod boundary;
+pub(crate) mod cache;
pub(crate) mod evidence;
pub(crate) mod frozen;
pub(crate) mod history;
diff --git a/platform/event-log/src/context/cache.rs b/platform/event-log/src/context/cache.rs
new file mode 100644
index 0000000000..bdd16a1000
--- /dev/null
+++ b/platform/event-log/src/context/cache.rs
@@ -0,0 +1,434 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+//! Owner-local certificates for exact committed history. No permissions or
+//! rendered data are retained here.
+
+use super::selection::Selection;
+use crate::{StoreError, connection::WriterIngress};
+use sqlx::{Decode, Sqlite, SqliteConnection, sqlite::SqliteOperation};
+use std::collections::HashMap;
+use std::sync::{
+ Arc, Mutex,
+ atomic::{AtomicI64, AtomicU64, Ordering},
+};
+
+#[derive(Default)]
+pub(crate) struct Cache {
+ #[cfg(test)]
+ anchor_hits: std::sync::atomic::AtomicUsize,
+ #[cfg(test)]
+ validated_links: std::sync::atomic::AtomicUsize,
+ #[cfg(test)]
+ stamps: std::sync::atomic::AtomicUsize,
+ #[cfg(test)]
+ raw_reuses: std::sync::atomic::AtomicUsize,
+ generation: AtomicU64,
+ frontier: AtomicI64,
+ certificates: Mutex<HashMap<String, Certificate>>,
+}
+
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub(crate) struct Stamp {
+ version: i64,
+ main_schema: i64,
+ temp_schema: i64,
+ failures: u64,
+ generation: u64,
+}
+
+#[derive(Clone)]
+pub(crate) struct Certificate {
+ pub selection: Selection,
+ pub stamp: Stamp,
+ pub raw: Option<Raw>,
+ pub checkpoint: Option<Checkpoint>,
+}
+
+#[derive(Clone, serde::Serialize, serde::Deserialize)]
+pub(crate) struct Raw {
+ pub through: u64,
+ pub state: Vec<u8>,
+}
+
+#[derive(Clone)]
+pub(crate) struct Checkpoint {
+ pub id: String,
+ pub sequence: u64,
+ pub covered_through: u64,
+}
+
+impl Cache {
+ /// Presence only selects an optimization attempt; fresh stamps and the
+ /// transaction's real Selection still authenticate every consumed proof.
+ pub(crate) fn has_proof(&self, session: &str) -> Option<bool> {
+ self.certificates.lock().ok().map(|entries| {
+ entries
+ .get(session)
+ .is_some_and(|entry| entry.raw.is_some() || entry.checkpoint.is_some())
+ })
+ }
+
+ pub(crate) fn has_anchor(&self, session: &str) -> bool {
+ self.certificates.lock().ok().is_some_and(|entries| {
+ entries
+ .get(session)
+ .is_some_and(|entry| entry.checkpoint.is_some())
+ })
+ }
+
+ pub(crate) fn has_raw(&self, session: &str) -> bool {
+ self.certificates.lock().ok().is_some_and(|entries| {
+ entries
+ .get(session)
+ .is_some_and(|entry| entry.raw.is_some())
+ })
+ }
+
+ pub(crate) async fn install(
+ self: &Arc<Self>,
+ connection: &mut SqliteConnection,
+ ) -> Result<(), StoreError> {
+ let observed = self.clone();
+ let mut handle = connection.lock_handle().await?;
+ handle.set_preupdate_hook(move |change| {
+ if change.database != "main" {
+ return;
+ }
+ match change.table {
+ "session_history_members" => observed.invalidate(),
+ "event_log" => {
+ if change.operation != SqliteOperation::Insert {
+ observed.invalidate();
+ return;
+ }
+ let Ok(row) = change.get_new_row_id() else {
+ observed.invalidate();
+ return;
+ };
+ // Advance on every observed INSERT, even without a certificate.
+ // An out-of-order insertion can change an already pinned cut.
+ if row <= observed.frontier.fetch_max(row, Ordering::AcqRel) {
+ observed.invalidate();
+ }
+ let kind = change
+ .get_new_column_value(3)
+ .ok()
+ .and_then(|value| <&str as Decode<Sqlite>>::decode(value).ok());
+ if kind.is_none_or(|kind| kind == "tool_result_archived") {
+ observed.invalidate();
+ }
+ }
+ _ => {}
+ }
+ });
+ Ok(())
+ }
+
+ pub(crate) fn invalidate(&self) {
+ self.generation.fetch_add(1, Ordering::AcqRel);
+ }
+
+ pub(crate) async fn stamp(
+ &self,
+ connection: &mut SqliteConnection,
+ failures: u64,
+ ) -> Result<Stamp, StoreError> {
+ #[cfg(test)]
+ self.stamps.fetch_add(1, Ordering::Relaxed);
+ // SAFETY: the live SQLx handle remains locked during this synchronous
+ // SQLite transaction-state observation.
+ let autocommit = unsafe {
+ rusqlite::ffi::sqlite3_get_autocommit(
+ connection.lock_handle().await?.as_raw_handle().as_ptr(),
+ )
+ };
+ if autocommit == 0 {
+ self.invalidate();
+ return Err(StoreError::OperationUnknown);
+ }
+ let stamp = self.current_stamp(connection, failures).await?;
+ let maximum: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(sequence),0) FROM event_log")
+ .fetch_one(connection)
+ .await?;
+ // Only fresh writer boundaries rebase after rollback/external commits.
+ self.frontier.store(maximum, Ordering::Release);
+ Ok(stamp)
+ }
+
+ async fn current_stamp(
+ &self,
+ connection: &mut SqliteConnection,
+ failures: u64,
+ ) -> Result<Stamp, StoreError> {
+ let version = sqlx::query_scalar("PRAGMA main.data_version")
+ .fetch_one(&mut *connection)
+ .await?;
+ let main_schema = sqlx::query_scalar("PRAGMA main.schema_version")
+ .fetch_one(&mut *connection)
+ .await?;
+ let temp_schema = sqlx::query_scalar("PRAGMA temp.schema_version")
+ .fetch_one(connection)
+ .await?;
+ Ok(Stamp {
+ version,
+ main_schema,
+ temp_schema,
+ failures,
+ generation: self.generation.load(Ordering::Acquire),
+ })
+ }
+
+ /// BEGIN IMMEDIATE has pinned and locked the append snapshot. Confirm the
+ /// original autocommit observation; never re-label its certificate here.
+ pub(crate) async fn confirm_append(
+ &self,
+ connection: &mut SqliteConnection,
+ origin: &Stamp,
+ failures: u64,
+ ) -> Result<bool, StoreError> {
+ Ok(origin == &self.current_stamp(connection, failures).await?)
+ }
+
+ pub(crate) fn get(
+ &self,
+ session: &str,
+ stamp: &Stamp,
+ ) -> Result<Option<Certificate>, StoreError> {
+ let mut certificates = self
+ .certificates
+ .lock()
+ .map_err(|_| StoreError::OperationUnknown)?;
+ if certificates
+ .get(session)
+ .is_some_and(|certificate| &certificate.stamp != stamp)
+ {
+ certificates.remove(session);
+ }
+ Ok(certificates.get(session).cloned())
+ }
+
+ pub(crate) fn publish(&self, candidate: Certificate) -> Result<(), StoreError> {
+ let Some(session) = candidate.selection.session.clone() else {
+ return Ok(());
+ };
+ let mut certificates = self
+ .certificates
+ .lock()
+ .map_err(|_| StoreError::OperationUnknown)?;
+ if let Some(current) = certificates.get(&session)
+ && current.stamp == candidate.stamp
+ && current.selection == candidate.selection
+ && (current
+ .checkpoint
+ .as_ref()
+ .map_or(0, |value| value.sequence)
+ > candidate
+ .checkpoint
+ .as_ref()
+ .map_or(0, |value| value.sequence)
+ || current.raw.as_ref().map_or(0, |value| value.through)
+ > candidate.raw.as_ref().map_or(0, |value| value.through))
+ {
+ return Ok(());
+ }
+ certificates.insert(session, candidate);
+ Ok(())
+ }
+}
+
+pub(crate) async fn observe(
+ cache: Arc<Cache>,
+ writer: &WriterIngress,
+ session: String,
+) -> Result<(Stamp, Option<Certificate>), StoreError> {
+ let ingress = writer.clone();
+ writer
+ .run(move |connection| {
+ Box::pin(async move {
+ let stamp = cache.stamp(connection, ingress.failures()).await?;
+ let certificate = cache.get(&session, &stamp)?;
+ Ok((stamp, certificate))
+ })
+ })
+ .await
+}
+
+/// Transaction-local scratch. Only an authenticated reader handshake or the
+/// audited append entry constructs this; ordinary run validators use Default.
+#[derive(Default)]
+pub(crate) struct Verification {
+ cache: Option<Arc<Cache>>,
+ origin: Option<Stamp>,
+ candidate: Option<Certificate>,
+}
+
+impl Verification {
+ pub(crate) fn new(cache: Arc<Cache>, origin: Stamp, certificate: Option<Certificate>) -> Self {
+ Self {
+ cache: Some(cache),
+ origin: Some(origin),
+ candidate: certificate,
+ }
+ }
+
+ fn check(&mut self) {
+ if let (Some(cache), Some(origin), Some(candidate)) =
+ (&self.cache, &self.origin, &mut self.candidate)
+ && cache.generation.load(Ordering::Acquire) != origin.generation
+ {
+ candidate.raw = None;
+ candidate.checkpoint = None;
+ }
+ }
+
+ pub(crate) fn select(&mut self, selection: &Selection) {
+ self.check();
+ if let Some(origin) = &self.origin
+ && self
+ .candidate
+ .as_ref()
+ .is_none_or(|candidate| candidate.selection != *selection)
+ {
+ self.candidate = Some(Certificate {
+ selection: selection.clone(),
+ stamp: origin.clone(),
+ raw: None,
+ checkpoint: None,
+ });
+ }
+ }
+
+ pub(crate) fn anchor(&mut self, id: &str, sequence: u64, covered_through: u64) -> bool {
+ self.check();
+ let matched = self
+ .candidate
+ .as_ref()
+ .and_then(|candidate| candidate.checkpoint.as_ref())
+ .is_some_and(|anchor| {
+ anchor.id == id
+ && anchor.sequence == sequence
+ && anchor.covered_through == covered_through
+ });
+ #[cfg(test)]
+ if matched && let Some(cache) = &self.cache {
+ cache.anchor_hits.fetch_add(1, Ordering::Relaxed);
+ }
+ matched
+ }
+
+ #[cfg(test)]
+ pub(super) fn validated_link(&self) {
+ if let Some(cache) = &self.cache {
+ cache.validated_links.fetch_add(1, Ordering::Relaxed);
+ }
+ }
+
+ pub(crate) fn adopt(&mut self, event: &maka_agent_facts::event::RuntimeEvent, sequence: u64) {
+ self.check();
+ if let Some(candidate) = &mut self.candidate
+ && let maka_agent_facts::event::Fact::ContextCheckpointRecorded { checkpoint } =
+ &event.fact
+ {
+ candidate.checkpoint = Some(Checkpoint {
+ id: event.id.clone(),
+ sequence,
+ covered_through: checkpoint.covered_through,
+ });
+ }
+ }
+
+ pub(crate) async fn digests(
+ &mut self,
+ connection: &mut SqliteConnection,
+ selection: &Selection,
+ throughs: &[u64],
+ ) -> Result<Vec<String>, StoreError> {
+ self.check();
+ let candidate = self
+ .candidate
+ .as_mut()
+ .filter(|candidate| candidate.selection == *selection);
+ let raw = candidate
+ .as_ref()
+ .and_then(|candidate| candidate.raw.as_ref());
+ #[cfg(test)]
+ if raw.is_some_and(|raw| throughs.first().is_some_and(|cut| *cut >= raw.through))
+ && let Some(cache) = &self.cache
+ {
+ cache.raw_reuses.fetch_add(1, Ordering::Relaxed);
+ }
+ let (digests, raw) = super::evidence::scan(connection, selection, throughs, raw).await?;
+ if let Some(candidate) = candidate
+ && raw.as_ref().is_some_and(|raw| {
+ candidate
+ .raw
+ .as_ref()
+ .is_none_or(|old| raw.through >= old.through)
+ })
+ {
+ candidate.raw = raw;
+ }
+ Ok(digests)
+ }
+
+ pub(crate) fn candidate(self) -> Option<Certificate> {
+ self.candidate
+ .filter(|candidate| candidate.raw.is_some() || candidate.checkpoint.is_some())
+ }
+}
+
+/// A disposable autocommit hint, never evidence about the work snapshot.
+pub(crate) async fn history_hint(connection: &mut SqliteConnection, session: &str) -> bool {
+ sqlx::query_scalar(
+ "SELECT EXISTS(SELECT 1 FROM runtime_events e
+ WHERE json_extract(e.event_json, '$.invocation.session_id')=?1
+ AND e.kind <> 'invocation_opened')
+ OR EXISTS(SELECT 1 FROM session_history_members WHERE session_id=?1)",
+ )
+ .bind(session)
+ .fetch_one(connection)
+ .await
+ .unwrap_or(false)
+}
+
+pub(crate) async fn promote(
+ cache: Arc<Cache>,
+ writer: &WriterIngress,
+ origin: Stamp,
+ candidate: Option<Certificate>,
+) -> Result<(), StoreError> {
+ let Some(candidate) = candidate else {
+ return Ok(());
+ };
+ let ingress = writer.clone();
+ writer
+ .run(move |connection| {
+ Box::pin(async move {
+ if cache.stamp(connection, ingress.failures()).await? == origin {
+ cache.publish(candidate)?;
+ }
+ Ok(())
+ })
+ })
+ .await
+}
+
+#[cfg(test)]
+mod tests;
diff --git a/platform/event-log/src/context/cache/tests.rs b/platform/event-log/src/context/cache/tests.rs
new file mode 100644
index 0000000000..14b6e5549c
--- /dev/null
+++ b/platform/event-log/src/context/cache/tests.rs
@@ -0,0 +1,1213 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use super::*;
+use crate::{EventLog, context};
+use maka_agent_facts::{
+ context::{CheckpointMode, CompactOutcome, ContextCheckpoint, ModelPurpose, TextSummary},
+ event::{EventWrite, Fact, Invocation, InvocationInput, InvocationOutcome, RuntimeEvent},
+ model::ModelStep,
+};
+use serde_json::json;
+use sqlx::Connection;
+
+fn event(id: &str, fact: Fact) -> EventWrite {
+ EventWrite::plain(RuntimeEvent::new(
+ Invocation {
+ session_id: "session".into(),
+ turn_id: id.into(),
+ run_id: id.into(),
+ invocation_id: id.into(),
+ },
+ fact,
+ ))
+ .unwrap()
+}
+
+async fn closed(log: &EventLog, id: &str) -> String {
+ let opening = event(
+ id,
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::Message {
+ source_messages: vec![],
+ content: "literal original question".into(),
+ request_fingerprint: None,
+ },
+ },
+ );
+ let original = opening.event().id.clone();
+ log.append_batch(&[
+ opening,
+ event(
+ id,
+ Fact::InvocationEnded {
+ outcome: InvocationOutcome::Completed,
+ },
+ ),
+ ])
+ .await
+ .unwrap();
+ original
+}
+
+async fn prepared_checkpoint(log: &EventLog, id: &str) -> Vec<EventWrite> {
+ log.append(&event(
+ id,
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::ContextCompact {
+ request_fingerprint: id.into(),
+ },
+ },
+ ))
+ .await
+ .unwrap();
+ let source = log
+ .prepare_context_compaction("session", Some(id), &CheckpointMode::Standalone)
+ .await
+ .unwrap();
+ let previous = source
+ .baseline
+ .as_ref()
+ .map(|baseline| baseline.event_id.clone());
+ let output: ModelStep = serde_json::from_value(json!({
+ "parts":[{"kind":"text","text_kind":"text","text":"literal retained summary"}],
+ "finish_reason":"stop","usage":{}}))
+ .unwrap();
+ log.append(&event(
+ id,
+ Fact::ModelRequested {
+ context_breakdown: None,
+ item_acceptance: false,
+ effective_source_digest: Some(source.effective_source_digest),
+ purpose: ModelPurpose::Summary,
+ context: None,
+ step_id: id.into(),
+ model_id: "test".into(),
+ source_scope: source.source_evidence.scope,
+ source_high_water: source.source_evidence.high_water,
+ source_digest: source.source_evidence.digest.clone(),
+ input_digest: "input".into(),
+ route_identity: format!("sha256:{}", "a".repeat(64)),
+ checkpoint_event_id: previous.clone(),
+ },
+ ))
+ .await
+ .unwrap();
+ log.append(&event(
+ id,
+ Fact::ModelCompleted {
+ step_id: id.into(),
+ output: output.clone(),
+ },
+ ))
+ .await
+ .unwrap();
+ let checkpoint = event(
+ id,
+ Fact::ContextCheckpointRecorded {
+ checkpoint: ContextCheckpoint {
+ mode: CheckpointMode::Standalone,
+ covered_through: source.source_evidence.high_water,
+ source_digest: source.source_evidence.digest,
+ previous_checkpoint_id: previous,
+ summary: TextSummary::from_model_step(&output).unwrap(),
+ summary_step_id: id.into(),
+ },
+ },
+ );
+ let checkpoint_id = checkpoint.event().id.clone();
+ vec![
+ checkpoint,
+ event(
+ id,
+ Fact::InvocationEnded {
+ outcome: InvocationOutcome::ContextCompactFinished {
+ outcome: CompactOutcome::Compacted {
+ checkpoint_id: checkpoint_id.clone(),
+ },
+ },
+ },
+ ),
+ ]
+}
+
+async fn checkpoint(log: &EventLog, id: &str) -> String {
+ let pair = prepared_checkpoint(log, id).await;
+ let checkpoint_id = pair[0].event().id.clone();
+ log.append_batch(&pair).await.unwrap();
+ checkpoint_id
+}
+
+async fn certificate(log: &EventLog) -> (Stamp, Certificate) {
+ let (stamp, certificate) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ (
+ stamp,
+ certificate.expect("actual completed verification published a certificate"),
+ )
+}
+
+async fn setup(path: &std::path::Path) -> (EventLog, String, String) {
+ let log = EventLog::open(path).await.unwrap();
+ log.create_session("session", "created", &json!({}), 1)
+ .await
+ .unwrap();
+ let opening = closed(&log, "main").await;
+ let checkpoint = checkpoint(&log, "summary").await;
+ let source = log.read_model_context("session", None).await.unwrap();
+ assert_eq!(
+ source.baseline.unwrap().checkpoint.summary.text,
+ "literal retained summary"
+ );
+ (log, opening, checkpoint)
+}
+
+fn tamper(path: &std::path::Path, event: &str) {
+ rusqlite::Connection::open(path)
+ .unwrap()
+ .execute(
+ "UPDATE event_log SET event_json=' ' || event_json WHERE event_id=?",
+ [event],
+ )
+ .unwrap();
+}
+
+#[tokio::test]
+async fn opening_only_then_same_invocation_progress_and_cold_reads_keep_exact_history() {
+ use maka_agent_facts::{tool_call::ToolCallIdentity, tool_output::ToolSuccess};
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("opt-in.sqlite");
+ let log = EventLog::open(&path).await.unwrap();
+ log.create_session("session", "create", &json!({}), 1)
+ .await
+ .unwrap();
+ let opening = event(
+ "active",
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::Message {
+ content: "literal tool-loop question".into(),
+ source_messages: vec![],
+ request_fingerprint: None,
+ },
+ },
+ );
+ log.append(&opening).await.unwrap();
+ let empty = log
+ .prepare_prune_candidates("session", Some("active"), 128, 1024 * 1024, None)
+ .await
+ .unwrap();
+ assert!(empty.candidates.is_empty());
+ assert!(empty.next.is_none());
+ assert!(log.context_cache.certificates.lock().unwrap().is_empty());
+ let first = log
+ .read_model_context("session", Some("active"))
+ .await
+ .unwrap();
+ assert!(first.baseline.is_none());
+ assert!(first.anchor.is_none());
+ assert_eq!(first.tail.len(), 1);
+ let context::ContextEvent::Canonical(actual) = &first.tail[0] else {
+ panic!("original opening")
+ };
+ assert_eq!(actual.event.id, opening.event().id);
+ let Fact::InvocationOpened {
+ input: InvocationInput::Message { content, .. },
+ ..
+ } = &actual.event.fact
+ else {
+ panic!("original message")
+ };
+ assert_eq!(content.text, "literal tool-loop question");
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ assert_eq!(first.source_evidence.digest, full.digest);
+ assert_eq!(log.context_cache.stamps.load(Ordering::Relaxed), 0);
+ assert_eq!(log.context_cache.has_proof("session"), Some(false));
+ let mut source = first;
+ let mut results = Vec::new();
+ for step in ["first-tool", "second-tool"] {
+ let before = log.context_cache.stamps.load(Ordering::Relaxed);
+ log.append(&event(
+ "active",
+ Fact::ModelRequested {
+ context_breakdown: None,
+ item_acceptance: false,
+ purpose: ModelPurpose::Main,
+ context: None,
+ step_id: step.into(),
+ model_id: "test".into(),
+ source_scope: source.source_evidence.scope.clone(),
+ source_high_water: source.source_evidence.high_water,
+ source_digest: source.source_evidence.digest.clone(),
+ input_digest: "input".into(),
+ route_identity: format!("sha256:{}", "a".repeat(64)),
+ checkpoint_event_id: None,
+ effective_source_digest: Some(source.effective_source_digest.clone()),
+ },
+ ))
+ .await
+ .unwrap();
+ assert_eq!(
+ log.context_cache.stamps.load(Ordering::Relaxed),
+ before,
+ "ordinary Main with no checkpoint consumes no certificate"
+ );
+ let output = serde_json::from_value(json!({"parts":[{"kind":"tool_call","call":{
+ "id":step,"name":"Read","input":{"kind":"json","data":{"path":"file"}},"provider_executed":false
+ }}],"finish_reason":"tool-calls","usage":{}})).unwrap();
+ log.append(&event(
+ "active",
+ Fact::ModelCompleted {
+ step_id: step.into(),
+ output,
+ },
+ ))
+ .await
+ .unwrap();
+ let operation = format!("{step}:{step}");
+ log.append(&event(
+ "active",
+ Fact::ToolDispatched {
+ presentation: None,
+ title: None,
+ operation_id: operation.clone(),
+ call: ToolCallIdentity::provider(step.into(), step.into()),
+ name: "Read".into(),
+ input: json!({"path":"file"}),
+ },
+ ))
+ .await
+ .unwrap();
+ let (result, _) = EventWrite::tool_success(
+ format!("result-{step}"),
+ std::time::SystemTime::now(),
+ opening.event().invocation.clone(),
+ operation,
+ ToolSuccess::from(json!(format!("literal {step} output"))),
+ )
+ .unwrap();
+ log.append(&result).await.unwrap();
+ results.push(result.event().id.clone());
+ source = log
+ .read_model_context("session", Some("active"))
+ .await
+ .unwrap();
+ assert!(source.baseline.is_none());
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ assert_eq!(source.source_evidence.digest, full.digest);
+ let proof = log.context_cache.certificates.lock().unwrap()["session"].clone();
+ assert_eq!(
+ proof.raw.unwrap().through,
+ source.source_evidence.high_water
+ );
+ assert!(
+ source.tail.iter().any(
+ |entry| matches!(entry, context::ContextEvent::Canonical(stored)
+ if stored.event.id == result.event().id)
+ ),
+ "real tool result remains visible"
+ );
+ }
+ assert!(
+ log.context_cache.raw_reuses.load(Ordering::Relaxed) > 0,
+ "same-invocation progress must extend the authenticated raw prefix"
+ );
+ let before = log.context_cache.stamps.load(Ordering::Relaxed);
+ let page = log
+ .prepare_prune_candidates("session", Some("active"), 1, 1024 * 1024, None)
+ .await
+ .unwrap();
+ assert_eq!(page.candidates.len(), 1);
+ assert_eq!(page.candidates[0].event_id, results[0]);
+ assert_eq!(page.candidates[0].tool_call_id, "first-tool");
+ assert_eq!(page.candidates[0].tool_name, "Read");
+ assert_eq!(
+ page.candidates[0].projection,
+ maka_agent_facts::tool_output::DurableToolProjection::Text {
+ text: "literal first-tool output".into(),
+ }
+ );
+ let cursor = page.next.unwrap();
+ let page = log
+ .prepare_prune_candidates("session", Some("active"), 1, 1024 * 1024, Some(&cursor))
+ .await
+ .unwrap();
+ assert_eq!(page.candidates.len(), 1);
+ assert_eq!(page.candidates[0].event_id, results[1]);
+ assert_eq!(page.candidates[0].tool_call_id, "second-tool");
+ assert_eq!(
+ page.candidates[0].projection,
+ maka_agent_facts::tool_output::DurableToolProjection::Text {
+ text: "literal second-tool output".into(),
+ }
+ );
+ assert!(page.next.is_none());
+ assert_eq!(
+ log.context_cache.stamps.load(Ordering::Relaxed),
+ before,
+ "raw-only hint must not turn prune into certificate work or skip its scan"
+ );
+ log.append(&event(
+ "active",
+ Fact::InvocationEnded {
+ outcome: InvocationOutcome::Completed,
+ },
+ ))
+ .await
+ .unwrap();
+ log.close().await.unwrap();
+ let log = EventLog::open(&path).await.unwrap();
+ assert_eq!(log.context_cache.has_proof("session"), Some(false));
+ let cold = log.read_model_context("session", None).await.unwrap();
+ assert!(cold.baseline.is_none());
+ assert!(log.context_cache.has_raw("session"));
+ assert_eq!(log.context_cache.raw_reuses.load(Ordering::Relaxed), 0);
+ let warm = log.read_model_context("session", None).await.unwrap();
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ assert_eq!(cold.source_evidence.digest, full.digest);
+ assert_eq!(warm.source_evidence.digest, full.digest);
+ assert_eq!(warm.tail.len(), cold.tail.len());
+ assert_eq!(
+ log.context_cache.raw_reuses.load(Ordering::Relaxed),
+ 1,
+ "cold pure reads of non-checkpoint history must warm and reuse Raw"
+ );
+ log.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn metadata_writes_preserve_real_certificate_and_new_checkpoint_stops_at_committed_anchor() {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, first) = setup(&directory.path().join("events.sqlite")).await;
+ let (before, _) = certificate(&log).await;
+ log.connection.run(|connection| Box::pin(async move {
+ sqlx::query("INSERT INTO message_queue_state VALUES ('session',1) ON CONFLICT(session_id) DO UPDATE SET revision=revision+1").execute(&mut *connection).await?;
+ sqlx::query("INSERT INTO application_ui_checkpoints VALUES ('deployment','{}','key',1,'format','{}','{}',x'0102')").execute(connection).await?;
+ Ok(())
+ })).await.unwrap();
+ assert_eq!(
+ log.close_run_interactions(
+ &Invocation {
+ session_id: "session".into(),
+ turn_id: "main".into(),
+ run_id: "main".into(),
+ invocation_id: "main".into(),
+ },
+ maka_agent_facts::interaction::ClosureReason::TurnTerminal,
+ 2
+ )
+ .await
+ .unwrap(),
+ 0
+ );
+ let (after, retained) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert_eq!(
+ before, after,
+ "queue, WITHOUT ROWID UI data and empty interaction closure do not change history evidence"
+ );
+ assert!(retained.is_some());
+ let links = log.context_cache.validated_links.load(Ordering::Relaxed);
+ let hits = log.context_cache.anchor_hits.load(Ordering::Relaxed);
+ assert_eq!(
+ log.read_model_context("session", None)
+ .await
+ .unwrap()
+ .baseline
+ .unwrap()
+ .event_id,
+ first
+ );
+ assert_eq!(
+ log.context_cache.validated_links.load(Ordering::Relaxed),
+ links
+ );
+ assert!(log.context_cache.anchor_hits.load(Ordering::Relaxed) > hits);
+ closed(&log, "next-main").await;
+ let before_compact = log.read_model_context("session", None).await.unwrap();
+ let opening = log
+ .append(&event(
+ "prune-probe",
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::ContextCompact {
+ request_fingerprint: "prune-probe".into(),
+ },
+ },
+ ))
+ .await
+ .unwrap();
+ let hits_before_prune = log.context_cache.anchor_hits.load(Ordering::Relaxed);
+ let candidates = log
+ .prepare_prune_candidates("session", Some("prune-probe"), 128, 1024 * 1024, None)
+ .await
+ .unwrap();
+ assert!(candidates.candidates.is_empty());
+ assert!(candidates.next.is_none());
+ assert!(
+ log.context_cache.anchor_hits.load(Ordering::Relaxed) > hits_before_prune,
+ "the real prune path must reuse its verified committed checkpoint"
+ );
+ let (_, after_prune) = certificate(&log).await;
+ assert_eq!(
+ after_prune.raw.unwrap().through,
+ before_compact.source_evidence.high_water,
+ "prune must not advance the sole raw frontier to the compact opening"
+ );
+ let compact = log
+ .prepare_context_compaction("session", Some("prune-probe"), &CheckpointMode::Standalone)
+ .await
+ .unwrap();
+ assert_eq!(compact.baseline.unwrap().event_id, first);
+ assert!(compact.source_evidence.high_water < opening);
+ assert_eq!(
+ compact.source_evidence.high_water,
+ before_compact.source_evidence.high_water
+ );
+ assert_eq!(
+ compact.source_evidence.digest,
+ before_compact.source_evidence.digest
+ );
+ assert_eq!(
+ compact.effective_source_digest,
+ before_compact.effective_source_digest
+ );
+ assert_eq!(
+ certificate(&log).await.1.raw.unwrap().through,
+ before_compact.source_evidence.high_water
+ );
+ log.append(&event(
+ "prune-probe",
+ Fact::InvocationEnded {
+ outcome: InvocationOutcome::Cancelled {
+ source: "fixture_probe_complete".into(),
+ },
+ },
+ ))
+ .await
+ .unwrap();
+ let second = checkpoint(&log, "next-summary").await;
+ let source = log.read_model_context("session", None).await.unwrap();
+ assert_eq!(source.baseline.unwrap().event_id, second);
+ let (_, actual) = certificate(&log).await;
+ assert_eq!(actual.checkpoint.unwrap().id, second);
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ assert_eq!(source.source_evidence.digest, full.digest);
+ assert!(
+ log.context_cache.validated_links.load(Ordering::Relaxed) - links <= 2,
+ "only the new checkpoint link is proved, never the old chain"
+ );
+ log.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn external_changes_in_both_confirmation_gaps_use_the_pinned_full_validator() {
+ for before_pin in [true, false] {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("events.sqlite");
+ let (log, original, _) = setup(&path).await;
+ let (origin, candidate) = certificate(&log).await;
+ let cache = log.context_cache.clone();
+ let writer = log.connection.writer();
+ let original_path = path.clone();
+ let result = log
+ .connection
+ .read_interruptible(move |connection| {
+ Box::pin(async move {
+ if before_pin {
+ tamper(&original_path, &original);
+ }
+ let mut tx = connection.begin().await?;
+ sqlx::query_scalar::<_, i64>("SELECT COALESCE(MAX(sequence),0) FROM event_log")
+ .fetch_one(&mut *tx)
+ .await?;
+ if !before_pin {
+ tamper(&original_path, &original);
+ }
+ let (confirmed, _) = observe(cache.clone(), &writer, "session".into()).await?;
+ assert_ne!(origin, confirmed);
+ let mut verification = Verification::new(cache.clone(), origin.clone(), None);
+ let high = candidate
+ .selection
+ .high_water(&mut tx, i64::MAX as u64)
+ .await?;
+ let result = context::read::latest_with_evidence(
+ &mut tx,
+ &candidate.selection,
+ high,
+ &mut verification,
+ )
+ .await;
+ tx.commit().await?;
+ if result.is_ok() {
+ promote(cache, &writer, origin, verification.candidate()).await?;
+ }
+ result.map(|(baseline, _)| baseline.unwrap().checkpoint.summary.text)
+ })
+ })
+ .await;
+ if before_pin {
+ assert!(matches!(result, Err(StoreError::InvalidTransition(_))));
+ } else {
+ assert_eq!(result.unwrap(), "literal retained summary");
+ }
+ assert!(log.read_model_context("session", None).await.is_err());
+ log.close().await.unwrap();
+ }
+}
+
+#[tokio::test]
+async fn cold_promotion_and_writer_begin_gap_never_relabel_an_old_snapshot() {
+ for writer_gap in [false, true] {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("events.sqlite");
+ let (log, original, _) = setup(&path).await;
+ let (origin, old) = certificate(&log).await;
+ if writer_gap {
+ // Exact ordered gate: C1 completed, external commit, then the
+ // audited writer obtains its BEGIN IMMEDIATE lock.
+ tamper(&path, &original);
+ let cache = log.context_cache.clone();
+ let writer = log.connection.writer();
+ let failures = writer.failures();
+ let result = writer
+ .run(move |connection| {
+ Box::pin(async move {
+ let mut tx = connection.begin_with("BEGIN IMMEDIATE").await?;
+ assert!(!cache.confirm_append(&mut tx, &origin, failures).await?);
+ let mut verification = Verification::new(cache, origin, None);
+ let high = old.selection.high_water(&mut tx, i64::MAX as u64).await?;
+ context::read::latest_with_evidence(
+ &mut tx,
+ &old.selection,
+ high,
+ &mut verification,
+ )
+ .await?;
+ tx.rollback().await?;
+ Ok(())
+ })
+ })
+ .await;
+ assert!(matches!(result, Err(StoreError::InvalidTransition(_))));
+ } else {
+ log.context_cache.certificates.lock().unwrap().clear();
+ let (origin, none) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert!(none.is_none());
+ let cache = log.context_cache.clone();
+ let cold_origin = origin.clone();
+ let candidate = log
+ .connection
+ .read_interruptible(move |connection| {
+ Box::pin(async move {
+ let mut tx = connection.begin().await?;
+ sqlx::query_scalar::<_, i64>(
+ "SELECT COALESCE(MAX(sequence),0) FROM event_log",
+ )
+ .fetch_one(&mut *tx)
+ .await?;
+ let selection = Selection::session("session");
+ let high = selection.high_water(&mut tx, i64::MAX as u64).await?;
+ let mut verification = Verification::new(cache, cold_origin, None);
+ let (baseline, _) = context::read::latest_with_evidence(
+ &mut tx,
+ &selection,
+ high,
+ &mut verification,
+ )
+ .await?;
+ assert_eq!(
+ baseline.unwrap().checkpoint.summary.text,
+ "literal retained summary"
+ );
+ tx.commit().await?;
+ Ok(verification.candidate())
+ })
+ })
+ .await
+ .unwrap();
+ // Verification completed under A. B commits before promotion.
+ tamper(&path, &original);
+ promote(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ origin,
+ candidate,
+ )
+ .await
+ .unwrap();
+ let (_, published) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert!(published.is_none());
+ }
+ assert!(log.read_model_context("session", None).await.is_err());
+ log.close().await.unwrap();
+ }
+}
+
+#[tokio::test]
+async fn every_insert_advances_frontier_even_between_cold_observation_and_promotion() {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, _) = setup(&directory.path().join("events.sqlite")).await;
+ let (origin, old) = certificate(&log).await;
+ let selection = old.selection.clone();
+ log.context_cache.certificates.lock().unwrap().clear();
+ let later = event(
+ "main",
+ Fact::ModelObserved {
+ step_id: "late-step".into(),
+ event: maka_agent_facts::model::ModelEvent::PartDelta {
+ id: "text".into(),
+ text: "selected later suffix".into(),
+ provider_options: None,
+ },
+ },
+ );
+ let earlier = event(
+ "main",
+ Fact::ModelObserved {
+ step_id: "late-step".into(),
+ event: maka_agent_facts::model::ModelEvent::PartDelta {
+ id: "text".into(),
+ text: "selected inserted gap".into(),
+ provider_options: None,
+ },
+ },
+ );
+ log.connection.run(move |connection| Box::pin(async move {
+ sqlx::query("INSERT INTO event_log(sequence,event_id,invocation_id,kind,operation_id,event_json) VALUES(1000,?,'main','model_observed',NULL,?)")
+ .bind(&later.event().id).bind(serde_json::to_string(later.event())?).execute(connection).await?;
+ Ok(())
+ })).await.unwrap();
+ let (same, none) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert!(none.is_none());
+ assert_eq!(
+ origin, same,
+ "strict suffix preserves previously committed evidence"
+ );
+ let cache = log.context_cache.clone();
+ let pinned = origin.clone();
+ let writer = log.connection.writer();
+ let (candidate, pinned_digest) = log.connection.read_interruptible(move |connection| Box::pin(async move {
+ let mut tx = connection.begin().await?;
+ sqlx::query_scalar::<_, i64>("SELECT COALESCE(MAX(sequence),0) FROM event_log").fetch_one(&mut *tx).await?;
+ let mut verification = Verification::new(cache, pinned, None);
+ let high = selection.high_water(&mut tx, i64::MAX as u64).await?;
+ assert_eq!(high, 1000, "the new row is actual selected runtime history");
+ let (_, source) = context::read::latest_with_evidence(&mut tx, &selection, high, &mut verification).await?;
+ writer.run(move |connection| Box::pin(async move {
+ sqlx::query("INSERT INTO event_log(sequence,event_id,invocation_id,kind,operation_id,event_json) VALUES(600,?,'main','model_observed',NULL,?)")
+ .bind(&earlier.event().id).bind(serde_json::to_string(earlier.event())?).execute(connection).await?;
+ Ok(())
+ })).await?;
+ tx.commit().await?;
+ let candidate = verification.candidate().expect("full cold verification completed");
+ assert_eq!(candidate.raw.as_ref().unwrap().through, 1000);
+ Ok((Some(candidate), source.digest))
+ })).await.unwrap();
+ promote(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ origin.clone(),
+ candidate,
+ )
+ .await
+ .unwrap();
+ let (changed, published) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert_ne!(origin, changed);
+ assert!(
+ published.is_none(),
+ "the pinned accumulator omitted the subsequently inserted row"
+ );
+ let source = log.read_model_context("session", None).await.unwrap();
+ assert_eq!(
+ source.baseline.unwrap().checkpoint.summary.text,
+ "literal retained summary"
+ );
+ assert_eq!(source.source_evidence.high_water, 1000);
+ assert_ne!(
+ source.source_evidence.digest, pinned_digest,
+ "fresh history includes sequence 600 that was absent in the pinned snapshot"
+ );
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ assert_eq!(source.source_evidence.digest, full.digest);
+ log.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn main_and_temp_trigger_replace_delete_the_original_prefix_and_invalidate_before_reuse() {
+ for temporary in [false, true] {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, original, _) = setup(&directory.path().join("events.sqlite")).await;
+ log.connection.run(move |connection| Box::pin(async move {
+ sqlx::query("INSERT INTO message_queue_state VALUES ('session',1)").execute(&mut *connection).await?;
+ let trigger = format!("CREATE {} TRIGGER rewrite_history AFTER UPDATE ON message_queue_state BEGIN
+ INSERT OR REPLACE INTO event_log(event_id,invocation_id,kind,operation_id,event_json)
+ SELECT event_id,invocation_id,kind,operation_id,event_json FROM event_log WHERE event_id='{}'; END",
+ if temporary { "TEMP" } else { "" }, original);
+ sqlx::query(sqlx::AssertSqlSafe(trigger)).execute(connection).await?;
+ Ok(())
+ })).await.unwrap();
+ // A schema change causes a cold read; the trigger remains installed
+ // while this genuine committed proof is authenticated again.
+ log.read_model_context("session", None).await.unwrap();
+ let (origin, prior) = certificate(&log).await;
+ let cache = log.context_cache.clone();
+ log.connection.run(move |connection| Box::pin(async move {
+ let mut tx = connection.begin_with("BEGIN IMMEDIATE").await?;
+ let mut verification = Verification::new(cache.clone(), origin.clone(), Some(prior));
+ sqlx::query("UPDATE message_queue_state SET revision=revision+1 WHERE session_id='session'").execute(&mut *tx).await?;
+ assert_ne!(origin.generation, cache.generation.load(Ordering::Acquire), "implicit REPLACE deletion is observed inside the transaction");
+ let selection = Selection::session("session");
+ let high = selection.high_water(&mut tx, i64::MAX as u64).await?;
+ assert!(context::read::latest_with_evidence(&mut tx, &selection, high, &mut verification).await.is_err(),
+ "a cached consumer observes the trigger's invalidation before commit");
+ assert!(context::read::latest_selected(&mut tx, &selection, i64::MAX as u64, &mut Verification::default()).await.is_err(),
+ "unknown transactions retain the full validator");
+ tx.commit().await?;
+ Ok(())
+ })).await.unwrap();
+ assert!(log.read_model_context("session", None).await.is_err());
+ log.close().await.unwrap();
+ }
+}
+
+#[tokio::test]
+async fn truncate_members_and_failed_transactions_revoke_combined_certificates() {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, _) = setup(&directory.path().join("events.sqlite")).await;
+ let (origin, _) = certificate(&log).await;
+ let result = log
+ .connection
+ .run(|connection| {
+ Box::pin(async move {
+ let mut tx = connection.begin_with("BEGIN IMMEDIATE").await?;
+ sqlx::query("UPDATE event_log SET event_json=' ' || event_json WHERE sequence=1")
+ .execute(&mut *tx)
+ .await?;
+ tx.rollback().await?;
+ Err::<(), _>(StoreError::EventConflict)
+ })
+ })
+ .await;
+ assert!(matches!(result, Err(StoreError::EventConflict)));
+ let (changed, none) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert_ne!(origin, changed);
+ assert!(none.is_none());
+ assert_eq!(
+ log.read_model_context("session", None)
+ .await
+ .unwrap()
+ .baseline
+ .unwrap()
+ .checkpoint
+ .summary
+ .text,
+ "literal retained summary"
+ );
+ // Real copied membership makes DELETE-all nonempty; no WHERE and no
+ // application row loop means SQLite's truncate optimization is exercised.
+ let revision = log
+ .get_session::<serde_json::Value>("session")
+ .await
+ .unwrap()
+ .unwrap()
+ .revision;
+ assert!(matches!(
+ log.copy_session(
+ crate::sessions::SessionCopy {
+ source_session_id: "session".into(),
+ target_session_id: "other".into(),
+ expected_source_revision: revision,
+ purpose: maka_agent_facts::session::CopyPurpose::Branch {
+ turn_id: None,
+ side_conversation: false
+ },
+ },
+ &json!({}),
+ 2
+ )
+ .await
+ .unwrap(),
+ crate::sessions::SessionCopyResult::Committed(_)
+ ));
+ log.read_model_context("session", None).await.unwrap();
+ let (origin, _) = certificate(&log).await;
+ let cache = log.context_cache.clone();
+ log.connection
+ .run(move |connection| {
+ Box::pin(async move {
+ sqlx::query("DELETE FROM session_history_members")
+ .execute(connection)
+ .await?;
+ assert_ne!(origin.generation, cache.generation.load(Ordering::Acquire));
+ Ok(())
+ })
+ })
+ .await
+ .unwrap();
+ let (_, none) = observe(
+ log.context_cache.clone(),
+ &log.connection.writer(),
+ "session".into(),
+ )
+ .await
+ .unwrap();
+ assert!(none.is_none());
+ log.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn optional_publication_failure_cannot_change_a_known_successful_commit() {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, _) = setup(&directory.path().join("events.sqlite")).await;
+ log.append(&event(
+ "committed",
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::Message {
+ content: "known committed input".into(),
+ source_messages: vec![],
+ request_fingerprint: None,
+ },
+ },
+ ))
+ .await
+ .unwrap();
+ let source = log
+ .read_model_context("session", Some("committed"))
+ .await
+ .unwrap();
+ let requested = event(
+ "committed",
+ Fact::ModelRequested {
+ context_breakdown: None,
+ item_acceptance: false,
+ purpose: ModelPurpose::Main,
+ context: None,
+ step_id: "committed-main".into(),
+ model_id: "test".into(),
+ source_scope: source.source_evidence.scope,
+ source_high_water: source.source_evidence.high_water,
+ source_digest: source.source_evidence.digest,
+ input_digest: "input".into(),
+ route_identity: format!("sha256:{}", "a".repeat(64)),
+ checkpoint_event_id: Some(source.baseline.unwrap().event_id),
+ effective_source_digest: Some(source.effective_source_digest),
+ },
+ );
+ let generation = log.context_cache.generation.load(Ordering::Acquire);
+ let cache = log.context_cache.clone();
+ log.connection
+ .run(move |connection| {
+ Box::pin(async move {
+ connection.lock_handle().await?.set_commit_hook(move || {
+ // Real SQLite commit succeeds; only optional cache publication
+ // loses its lock. No database or lifecycle failure is fabricated.
+ let _ = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
+ let _locked = cache.certificates.lock().unwrap();
+ panic!("deliberately poison the optional certificate lock");
+ }));
+ true
+ });
+ Ok(())
+ })
+ })
+ .await
+ .unwrap();
+ let id = requested.event().id.clone();
+ let sequence = log
+ .append(&requested)
+ .await
+ .expect("the original commit was confirmed");
+ let recorded: i64 = log
+ .connection
+ .run(move |connection| {
+ Box::pin(async move {
+ Ok(
+ sqlx::query_scalar("SELECT sequence FROM event_log WHERE event_id=?")
+ .bind(id)
+ .fetch_one(connection)
+ .await?,
+ )
+ })
+ })
+ .await
+ .unwrap();
+ assert_eq!(sequence, recorded as u64);
+ assert!(log.context_cache.certificates.is_poisoned());
+ assert_ne!(
+ generation,
+ log.context_cache.generation.load(Ordering::Acquire),
+ "the consumer actually reached the failed optional publication handler"
+ );
+ let full = log
+ .scoped_prefix(Selection::session("session").scope, 100, 1024 * 1024)
+ .await
+ .unwrap();
+ let stamps = log.context_cache.stamps.load(Ordering::Relaxed);
+ let source = log
+ .read_model_context("session", Some("committed"))
+ .await
+ .expect("an unavailable optional hint must fall back to full history validation");
+ assert_eq!(source.source_evidence.high_water, sequence);
+ assert_eq!(source.source_evidence.digest, full.digest);
+ assert_eq!(
+ source.baseline.unwrap().checkpoint.summary.text,
+ "literal retained summary"
+ );
+ assert_eq!(
+ log.context_cache.stamps.load(Ordering::Relaxed),
+ stamps,
+ "an unavailable map must not be opted back in by a positive SQL hint"
+ );
+ log.close().await.unwrap();
+}
+
+#[tokio::test]
+async fn shutdown_rejects_reader_confirmation_then_drains_its_real_rollback() {
+ use futures_util::FutureExt;
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, _) = setup(&directory.path().join("events.sqlite")).await;
+ let log = Arc::new(log);
+ let (entered, pinned) = tokio::sync::oneshot::channel();
+ let (release, released) = tokio::sync::oneshot::channel();
+ let reader = log.clone();
+ let task = tokio::spawn(async move {
+ let cache = reader.context_cache.clone();
+ let writer = reader.connection.writer();
+ reader
+ .connection
+ .read_interruptible(move |connection| {
+ Box::pin(async move {
+ observe(cache.clone(), &writer, "session".into()).await?;
+ let mut tx = connection.begin().await?;
+ sqlx::query_scalar::<_, i64>("SELECT MAX(sequence) FROM event_log")
+ .fetch_one(&mut *tx)
+ .await?;
+ entered.send(()).unwrap();
+ released.await.unwrap();
+ observe(cache, &writer, "session".into()).await?;
+ tx.commit().await?;
+ Ok(())
+ })
+ })
+ .await
+ });
+ pinned.await.unwrap();
+ let mut shutdown = Box::pin(log.shutdown());
+ assert!(shutdown.as_mut().now_or_never().is_none());
+ // A queued barrier either drains behind Shutdown or is already refused.
+ // Both outcomes prove ingress is closed before the reader is released.
+ let barrier = log
+ .connection
+ .writer()
+ .run(|_| Box::pin(async { Ok(()) }))
+ .await;
+ assert!(barrier.is_ok() || matches!(barrier, Err(StoreError::ConnectionClosed)));
+ release.send(()).unwrap();
+ assert!(matches!(
+ task.await.unwrap(),
+ Err(StoreError::ConnectionClosed)
+ ));
+ shutdown.await.unwrap();
+ assert!(matches!(
+ log.read_model_context("session", None).await,
+ Err(StoreError::ConnectionClosed)
+ ));
+}
+
+#[tokio::test]
+async fn an_unsealed_checkpoint_cannot_certify_a_later_fact_in_its_own_batch() {
+ let directory = tempfile::tempdir().unwrap();
+ let (log, _, _) = setup(&directory.path().join("events.sqlite")).await;
+ closed(&log, "second-main").await;
+ let pair = prepared_checkpoint(&log, "batch-summary").await;
+ log.connection.run(|connection| Box::pin(async move {
+ // The plain sequence is refused by active-Session admission. These
+ // actual metadata triggers exercise the supported trigger boundary:
+ // hide that execution origin until the new Main has been appended.
+ sqlx::query("CREATE TRIGGER hide_pending_compact AFTER INSERT ON event_log
+ WHEN NEW.kind='context_checkpoint_recorded' AND NEW.invocation_id='batch-summary'
+ BEGIN INSERT INTO imported_invocations(invocation_id,bundle_digest)
+ VALUES('batch-summary','sha256:0000000000000000000000000000000000000000000000000000000000000000'); END")
+ .execute(&mut *connection).await?;
+ sqlx::query("CREATE TRIGGER restore_pending_compact AFTER INSERT ON event_log
+ WHEN NEW.kind='model_requested' AND NEW.invocation_id='batch-main'
+ BEGIN DELETE FROM imported_invocations WHERE invocation_id='batch-summary'; END")
+ .execute(connection).await?;
+ Ok(())
+ })).await.unwrap();
+ let (batch, full) = log
+ .connection
+ .run(move |connection| {
+ Box::pin(async move {
+ let mut tx = connection.begin_with("BEGIN IMMEDIATE").await?;
+ let crate::append::AppendResult::Inserted(sequence) =
+ EventLog::append_in_transaction(&mut tx, &pair[0]).await?
+ else {
+ panic!("the prepared checkpoint must be new")
+ };
+ // Obtain the real exact prefix after CP INSERT in this snapshot. The
+ // following rollback restores both rowids and trigger side effects.
+ let source =
+ context::evidence::selected(&mut tx, &Selection::session("session"), sequence)
+ .await?;
+ let opening = event(
+ "batch-main",
+ Fact::InvocationOpened {
+ configuration: None,
+ input: InvocationInput::Message {
+ content: "next input".into(),
+ source_messages: vec![],
+ request_fingerprint: None,
+ },
+ },
+ );
+ let main = event(
+ "batch-main",
+ Fact::ModelRequested {
+ context_breakdown: None,
+ item_acceptance: false,
+ purpose: ModelPurpose::Main,
+ context: None,
+ step_id: "batch-main-step".into(),
+ model_id: "test".into(),
+ source_scope: source.scope,
+ source_high_water: source.high_water,
+ source_digest: source.digest,
+ input_digest: "input".into(),
+ route_identity: format!("sha256:{}", "a".repeat(64)),
+ checkpoint_event_id: Some(pair[0].event().id.clone()),
+ effective_source_digest: None,
+ },
+ );
+ let batch = vec![pair[0].clone(), opening, main, pair[1].clone()];
+ let mut full = Ok(());
+ for write in &batch[1..] {
+ if let Err(error) = EventLog::append_in_transaction(&mut tx, write).await {
+ full = Err(error.to_string());
+ break;
+ }
+ }
+ if full.is_ok() {
+ full = context::validate_batch(&mut tx, &batch)
+ .await
+ .map_err(|error| error.to_string());
+ }
+ tx.rollback().await?;
+ Ok((batch, full))
+ })
+ })
+ .await
+ .unwrap();
+ assert!(
+ full.as_ref()
+ .is_err_and(|reason| reason.contains("missing proof event")),
+ "causal replay must require the Compact terminal before Main: {full:?}"
+ );
+ let ids: Vec<_> = batch.iter().map(|write| write.event().id.clone()).collect();
+ let cached = log
+ .append_batch(&batch)
+ .await
+ .map(|_| ())
+ .map_err(|error| match error {
+ maka_agent_facts::event::CommitError::Rejected(reason) => reason,
+ other => other.to_string(),
+ });
+ let recorded: i64 = log
+ .connection
+ .run(move |connection| {
+ Box::pin(async move {
+ Ok(sqlx::query_scalar(
+ "SELECT COUNT(*) FROM event_log WHERE event_id IN (?, ?, ?, ?)",
+ )
+ .bind(&ids[0])
+ .bind(&ids[1])
+ .bind(&ids[2])
+ .bind(&ids[3])
+ .fetch_one(connection)
+ .await?)
+ })
+ })
+ .await
+ .unwrap();
+ assert_eq!(
+ cached, full,
+ "public cached append persisted {recorded} facts that causal full replay rejects"
+ );
+ assert_eq!(recorded, 0);
+ log.close().await.unwrap();
+}
diff --git a/platform/event-log/src/context/evidence.rs b/platform/event-log/src/context/evidence.rs
index b3abdb3802..a45d9c311a 100644
--- a/platform/event-log/src/context/evidence.rs
+++ b/platform/event-log/src/context/evidence.rs
@@ -59,22 +59,34 @@ pub(super) async fn digests(
selection: &Selection,
throughs: &[u64],
) -> Result<Vec<String>, StoreError> {
+ Ok(scan(connection, selection, throughs, None).await?.0)
+}
+
+pub(super) async fn scan(
+ connection: &mut SqliteConnection,
+ selection: &Selection,
+ throughs: &[u64],
+ raw: Option<&super::cache::Raw>,
+) -> Result<(Vec<String>, Option<super::cache::Raw>), StoreError> {
if throughs.is_empty() {
- return Ok(Vec::new());
+ return Ok((Vec::new(), None));
}
let session = selection
.session
.as_deref()
.ok_or_else(|| invalid("context requires a Session"))?;
- let result: String = sqlx::query_scalar("SELECT context_source_digests(?, ?, ?, ?)")
+ let result: String = sqlx::query_scalar("SELECT context_source_digests(?, ?, ?, ?, ?)")
.bind(session)
.bind(&selection.lineage)
.bind(serde_json::to_vec(&selection.scope)?)
.bind(serde_json::to_string(throughs)?)
+ .bind(raw.map(serde_json::to_string).transpose()?)
.fetch_one(connection)
.await?;
- serde_json::from_str::<Result<Vec<String>, String>>(&result)?
- .map_err(StoreError::InvalidTransition)
+ let (digests, raw) =
+ serde_json::from_str::<Result<(Vec<String>, super::cache::Raw), String>>(&result)?
+ .map_err(StoreError::InvalidTransition)?;
+ Ok((digests, Some(raw)))
}
pub(super) async fn high_water(
diff --git a/platform/event-log/src/context/evidence/scan.rs b/platform/event-log/src/context/evidence/scan.rs
index 94f5cee355..bcbddb100a 100644
--- a/platform/event-log/src/context/evidence/scan.rs
+++ b/platform/event-log/src/context/evidence/scan.rs
@@ -18,17 +18,21 @@
*/
//! Reduce exact source bytes on SQLite's worker, returning only digest cuts.
-//! No authenticated state survives this function or the calling transaction.
+//! Serialized state is temporary, owner-local evidence, never a wire fact.
+use crate::context::cache::Raw;
use crate::context::{invalid, selection::Selection};
use crate::{StoreError, sequence_number};
use rusqlite::{Connection, functions::FunctionFlags, params, types::ValueRef};
-use sha2::{Digest, Sha256};
+use sha2::{
+ Digest, Sha256,
+ digest::common::hazmat::{SerializableState, SerializedState},
+};
pub(crate) fn register(connection: &Connection) -> Result<(), StoreError> {
connection.create_scalar_function(
"context_source_digests",
- 4,
+ 5,
FunctionFlags::SQLITE_UTF8 | FunctionFlags::SQLITE_DIRECTONLY,
|context| {
let session: String = context.get(0)?;
@@ -37,11 +41,24 @@ pub(crate) fn register(connection: &Connection) -> Result<(), StoreError> {
let cuts: String = context.get(3)?;
let cuts: Vec<u64> = serde_json::from_str(&cuts)
.map_err(|error| rusqlite::Error::UserFunctionError(Box::new(error)))?;
+ let raw: Option<String> = context.get(4)?;
+ let raw: Option<Raw> = raw
+ .as_deref()
+ .map(serde_json::from_str)
+ .transpose()
+ .map_err(|error| rusqlite::Error::UserFunctionError(Box::new(error)))?;
// SAFETY: synchronous read-only use on SQLite's current worker and
// transaction. No handle, row, or reference escapes the callback;
// it never awaits, changes threads, or recursively invokes this UDF.
let connection = unsafe { context.get_connection() }?;
- let result = match digests(&connection, &session, lineage.as_deref(), &scope, &cuts) {
+ let result = match digests(
+ &connection,
+ &session,
+ lineage.as_deref(),
+ &scope,
+ &cuts,
+ raw.as_ref(),
+ ) {
Ok(digests) => Ok(digests),
// Keep domain failures typed across the SQLx/UDF boundary.
Err(StoreError::InvalidTransition(message)) => Err(message),
@@ -61,14 +78,24 @@ fn digests(
lineage: Option<&str>,
scope: &[u8],
throughs: &[u64],
-) -> Result<Vec<String>, StoreError> {
- let Some(through) = throughs.last().copied() else {
- return Ok(Vec::new());
+ raw: Option<&Raw>,
+) -> Result<(Vec<String>, Raw), StoreError> {
+ let through = *throughs
+ .last()
+ .ok_or_else(|| invalid("missing source cut"))?;
+ let raw = raw.filter(|raw| throughs[0] >= raw.through);
+ let after = raw.map_or(0, |raw| raw.through);
+ let mut digest = if let Some(raw) = raw {
+ let state = SerializedState::<Sha256>::try_from(raw.state.as_slice())
+ .map_err(|_| invalid("invalid internal SHA state"))?;
+ Sha256::deserialize(&state).map_err(|_| invalid("invalid internal SHA state"))?
+ } else {
+ let mut digest = Sha256::new();
+ digest.update(b"maka.log-prefix.v1\0");
+ digest.update((scope.len() as u64).to_be_bytes());
+ digest.update(scope);
+ digest
};
- let mut digest = Sha256::new();
- digest.update(b"maka.log-prefix.v1\0");
- digest.update((scope.len() as u64).to_be_bytes());
- digest.update(scope);
let mut boundaries = throughs.iter().copied().peekable();
let mut result = Vec::new();
let mut finish = |digest: &Sha256, sequence: u64| -> Result<(), StoreError> {
@@ -85,7 +112,7 @@ fn digests(
}
Ok(())
};
- finish(&digest, 0)?;
+ finish(&digest, after)?;
let filter = Selection::predicate("e", "?4");
let payload = "length(CAST(e.event_json AS BLOB)),
CASE WHEN length(CAST(e.event_json AS BLOB)) <= 65536 THEN CAST(e.event_json AS BLOB) END";
@@ -102,8 +129,8 @@ fn digests(
let mut slices = connection.prepare(
"SELECT substr(CAST(event_json AS BLOB), ?, 65536) FROM runtime_events WHERE sequence = ?",
)?;
- let mut previous = 0i64;
- let mut rows = statement.query(params![0i64, through as i64, session, lineage])?;
+ let mut previous = after as i64;
+ let mut rows = statement.query(params![after as i64, through as i64, session, lineage])?;
while let Some(row) = rows.next()? {
let sequence: i64 = row.get(0)?;
let bytes: i64 = row.get(1)?;
@@ -143,5 +170,11 @@ fn digests(
if boundaries.peek().is_some() {
return Err(invalid("coverage is not a selected source high-water"));
}
- Ok(result)
+ Ok((
+ result,
+ Raw {
+ through,
+ state: digest.serialize().to_vec(),
+ },
+ ))
}
diff --git a/platform/event-log/src/context/read.rs b/platform/event-log/src/context/read.rs
index 5e6cba12a3..4d856d6e61 100644
--- a/platform/event-log/src/context/read.rs
+++ b/platform/event-log/src/context/read.rs
@@ -19,8 +19,7 @@
use super::selection::Selection;
use super::{
- ContextBaseline, ModelContextSource, boundary, evidence, invalid, latest_main, proof, safety,
- validate,
+ ContextBaseline, ModelContextSource, boundary, invalid, latest_main, proof, safety, validate,
};
use crate::{EventLog, StoreError};
use maka_agent_facts::context::CheckpointMode;
@@ -86,9 +85,21 @@ impl EventLog {
self.validate_root()?;
crate::sessions::validate_id(session)?;
let (session, current) = (session.to_owned(), current.map(str::to_owned));
+ let cache = self.context_cache.clone();
+ let writer = self.connection.writer();
self.connection
.read_interruptible(move |connection| {
Box::pin(async move {
+ let attempt = match cache.has_proof(&session) {
+ Some(true) => true,
+ Some(false) => super::cache::history_hint(connection, &session).await,
+ None => false,
+ };
+ let observation = if attempt {
+ Some(super::cache::observe(cache.clone(), &writer, session.clone()).await?)
+ } else {
+ None
+ };
let mut tx = connection.begin().await?;
let opening =
safety::current_opening(&mut tx, &session, current.as_deref()).await?;
@@ -116,6 +127,21 @@ impl EventLog {
Some((_, event)) => Selection::for_opening(&mut tx, event).await?,
None => Selection::session(&session),
};
+ let (origin, mut verification) = if let Some((origin, certificate)) =
+ observation
+ {
+ let (confirmed, _) =
+ super::cache::observe(cache.clone(), &writer, session.clone()).await?;
+ let certificate = certificate.filter(|_| confirmed == origin);
+ let verification = super::cache::Verification::new(
+ cache.clone(),
+ origin.clone(),
+ certificate,
+ );
+ (Some(origin), verification)
+ } else {
+ (None, super::cache::Verification::default())
+ };
let high_water = if let Some(mode) = &mode {
if let Some((_, event)) = &opening {
boundary::admit_summary(
@@ -146,17 +172,21 @@ impl EventLog {
let before = i64::MAX as u64;
let latest_main =
latest_main::read_selected(&mut tx, &selection, high_water).await?;
- let source = materialize_selected(
+ let source = materialize_cached(
&mut tx,
&selection,
high_water,
before,
usize::MAX,
usize::MAX,
- latest_main,
+ (latest_main, &mut verification),
)
.await?;
tx.commit().await?;
+ if let Some(origin) = origin {
+ super::cache::promote(cache, &writer, origin, verification.candidate())
+ .await?;
+ }
Ok(source)
})
})
@@ -194,9 +224,30 @@ pub(super) async fn materialize_selected(
max_events: usize,
max_bytes: usize,
latest_main: super::LatestMainContext,
+) -> Result<ModelContextSource, StoreError> {
+ materialize_cached(
+ connection,
+ selection,
+ high_water,
+ before,
+ max_events,
+ max_bytes,
+ (latest_main, &mut super::cache::Verification::default()),
+ )
+ .await
+}
+
+async fn materialize_cached(
+ connection: &mut SqliteConnection,
+ selection: &Selection,
+ high_water: u64,
+ before: u64,
+ max_events: usize,
+ max_bytes: usize,
+ (latest_main, verification): (super::LatestMainContext, &mut super::cache::Verification),
) -> Result<ModelContextSource, StoreError> {
let (baseline, source_evidence) =
- latest_with_evidence(connection, selection, high_water).await?;
+ latest_with_evidence(connection, selection, high_water, verification).await?;
let anchor = if let Some(ContextBaseline { checkpoint, .. }) = &baseline {
if let CheckpointMode::MidTurn { anchor_event_id } = &checkpoint.mode {
let (sequence, event) = proof::by_id(connection, anchor_event_id).await?;
@@ -247,13 +298,15 @@ pub(crate) async fn latest_selected(
connection: &mut SqliteConnection,
selection: &Selection,
through: u64,
+ verification: &mut super::cache::Verification,
) -> Result<Option<ContextBaseline>, StoreError> {
+ verification.select(selection);
let Some(StoredEvent { sequence, event }) =
latest_record(connection, selection, through).await?
else {
return Ok(None);
};
- validate::chain(connection, &event, sequence, true, None).await?;
+ validate::chain_cached(connection, &event, sequence, true, None, verification).await?;
let Fact::ContextCheckpointRecorded { checkpoint } = event.fact else {
return Err(invalid("checkpoint kind mismatch"));
};
@@ -269,21 +322,32 @@ pub(super) async fn latest_with_evidence(
connection: &mut SqliteConnection,
selection: &Selection,
through: u64,
+ verification: &mut super::cache::Verification,
) -> Result<(Option<ContextBaseline>, super::SourceEvidence), StoreError> {
+ verification.select(selection);
let Some(StoredEvent { sequence, event }) =
latest_record(connection, selection, through).await?
else {
return Ok((
None,
- evidence::selected(connection, selection, through).await?,
+ super::SourceEvidence {
+ scope: selection.scope.clone(),
+ high_water: through,
+ digest: verification
+ .digests(connection, selection, &[through])
+ .await?
+ .pop()
+ .expect("one requested prefix"),
+ },
));
};
- let digest = validate::chain(
+ let digest = validate::chain_cached(
connection,
&event,
sequence,
true,
Some((selection, through)),
+ verification,
)
.await?
.expect("requested current source digest");
diff --git a/platform/event-log/src/context/request.rs b/platform/event-log/src/context/request.rs
index ca2a36a21f..40ad68c943 100644
--- a/platform/event-log/src/context/request.rs
+++ b/platform/event-log/src/context/request.rs
@@ -30,6 +30,7 @@ use sqlx::SqliteConnection;
pub(super) async fn validate(
connection: &mut SqliteConnection,
event: &RuntimeEvent,
+ verification: &mut super::cache::Verification,
) -> Result<(), StoreError> {
let Fact::ModelRequested {
purpose,
@@ -70,13 +71,26 @@ pub(super) async fn validate(
if source_scope != &selection.scope {
return Err(invalid("request checkpoint belongs to another scope"));
}
- let (baseline, source) =
- super::read::latest_with_evidence(connection, &selection, *source_high_water)
- .await?;
+ let (baseline, source) = super::read::latest_with_evidence(
+ connection,
+ &selection,
+ *source_high_water,
+ verification,
+ )
+ .await?;
let baseline = baseline.ok_or_else(|| invalid("missing request baseline"))?;
if baseline.event_id != *id || source.digest != *source_digest {
return Err(invalid("request baseline evidence changed"));
}
+ // A certified checkpoint still needs fresh archive-writer lineage:
+ // a later Message can make a previously unique Run ambiguous.
+ crate::archive::validate_selected(
+ connection,
+ &selection,
+ source.high_water,
+ i64::MAX as u64,
+ )
+ .await?;
}
return Ok(());
}
@@ -122,7 +136,7 @@ pub(super) async fn validate(
return Err(invalid("model request source changed"));
}
let (baseline, source) =
- super::read::latest_with_evidence(connection, &selection, high).await?;
+ super::read::latest_with_evidence(connection, &selection, high, verification).await?;
if source.digest != *source_digest {
return Err(invalid("model request source changed"));
}
diff --git a/platform/event-log/src/context/selection.rs b/platform/event-log/src/context/selection.rs
index ed72838683..4b52fd7000 100644
--- a/platform/event-log/src/context/selection.rs
+++ b/platform/event-log/src/context/selection.rs
@@ -26,7 +26,7 @@ use maka_agent_facts::event::LogScope;
use maka_agent_facts::event::RuntimeEvent;
use sqlx::SqliteConnection;
-#[derive(PartialEq, Eq)]
+#[derive(Clone, PartialEq, Eq)]
pub(crate) struct Selection {
pub scope: LogScope,
pub session: Option<String>,
diff --git a/platform/event-log/src/context/validate.rs b/platform/event-log/src/context/validate.rs
index b3d6b2515c..b35ade75a3 100644
--- a/platform/event-log/src/context/validate.rs
+++ b/platform/event-log/src/context/validate.rs
@@ -17,7 +17,7 @@
* under the License.
*/
-use super::{evidence, invalid, proof, read, safety, selection::Selection};
+use super::{invalid, proof, read, safety, selection::Selection};
use crate::StoreError;
use maka_agent_facts::context::CheckpointMode;
use maka_agent_facts::context::CompactOutcome;
@@ -30,8 +30,9 @@ use sqlx::SqliteConnection;
pub(crate) async fn validate_append(
connection: &mut SqliteConnection,
event: &RuntimeEvent,
+ verification: &mut super::cache::Verification,
) -> Result<(), StoreError> {
- super::request::validate(connection, event).await?;
+ super::request::validate(connection, event, verification).await?;
match &event.fact {
Fact::ContextCheckpointRecorded { checkpoint } => {
safety::current_opening(
@@ -62,7 +63,16 @@ pub(crate) async fn validate_append(
if current != checkpoint.previous_checkpoint_id {
return Err(invalid("checkpoint predecessor changed before adoption"));
}
- chain(connection, event, i64::MAX as u64, false, None).await?;
+ verification.select(&selection);
+ chain_cached(
+ connection,
+ event,
+ i64::MAX as u64,
+ false,
+ None,
+ verification,
+ )
+ .await?;
}
Fact::InvocationEnded { outcome } => {
let compact: bool = sqlx::query_scalar(
@@ -107,12 +117,13 @@ pub(crate) async fn validate_append(
/// Strictly decreasing coverage and record sequence make cycles impossible.
/// An optional current source cut must be at or after the first record.
-pub(super) async fn chain(
+pub(super) async fn chain_cached(
connection: &mut SqliteConnection,
first: &RuntimeEvent,
first_sequence: u64,
committed: bool,
source: Option<(&Selection, u64)>,
+ verification: &mut super::cache::Verification,
) -> Result<Option<String>, StoreError> {
let mut event = first.clone();
let mut sequence = first_sequence;
@@ -122,6 +133,11 @@ pub(super) async fn chain(
let Fact::ContextCheckpointRecorded { checkpoint } = &event.fact else {
return Err(invalid("predecessor is not a checkpoint"));
};
+ if verification.anchor(&event.id, sequence, checkpoint.covered_through) {
+ break;
+ }
+ #[cfg(test)]
+ verification.validated_link();
let selection = proof::validate(connection, &event, sequence, checkpoint).await?;
if needs_terminal && matches!(checkpoint.mode, CheckpointMode::Standalone) {
require_terminal(connection, &event).await?;
@@ -176,7 +192,9 @@ pub(super) async fn chain(
if let Some((_, through)) = current {
throughs.push(through);
}
- let mut actual = evidence::digests(connection, &selection, &throughs).await?;
+ let mut actual = verification
+ .digests(connection, &selection, &throughs)
+ .await?;
if current.is_some() {
// Current coverage follows the first checkpoint record, so its
// digest is after every checkpoint source boundary in the stream.
@@ -194,11 +212,16 @@ pub(super) async fn chain(
&& source_digest.is_none()
{
source_digest = Some(
- evidence::selected(connection, selection, through)
+ verification
+ .digests(connection, selection, &[through])
.await?
- .digest,
+ .pop()
+ .expect("one requested prefix"),
);
}
+ if committed {
+ verification.adopt(first, first_sequence);
+ }
Ok(source_digest)
}
diff --git a/platform/event-log/src/lib.rs b/platform/event-log/src/lib.rs
index b7532d1683..8035a8c482 100644
--- a/platform/event-log/src/lib.rs
+++ b/platform/event-log/src/lib.rs
@@ -160,6 +160,7 @@ pub enum StoreError {
}
pub struct EventLog {
+ context_cache: Arc<context::cache::Cache>,
// The independent owner drains SQLx and closes it before releasing its lease.
connection: OwnedConnection,
root_owner: Option<Arc<root::RootOwner>>,
@@ -253,9 +254,12 @@ impl EventLog {
|connection| Box::pin(schema::initialize_connection(connection)),
)
.await?;
+ let context_cache = Arc::new(context::cache::Cache::default());
+ let install = context_cache.clone();
let high_water = connection
- .run(|connection| {
+ .run(move |connection| {
Box::pin(async move {
+ install.install(connection).await?;
sequence_number(
sqlx::query_scalar::<_, i64>(
"SELECT COALESCE(MAX(sequence), 0) FROM event_log",
@@ -280,6 +284,7 @@ impl EventLog {
}
};
Ok(Self {
+ context_cache,
connection,
root_owner,
commits: tokio::sync::watch::channel(high_water).0,
diff --git a/platform/event-log/tests/archive/lineage_checkpoint.rs b/platform/event-log/tests/archive/lineage_checkpoint.rs
index ebcaf8fe8d..5a9c46603f 100644
--- a/platform/event-log/tests/archive/lineage_checkpoint.rs
+++ b/platform/event-log/tests/archive/lineage_checkpoint.rs
@@ -128,3 +128,120 @@ async fn narrower_checkpoint_does_not_corrupt_later_whole_session_archives() {
log.append(&archived).await.unwrap();
log.close().await.unwrap();
}
+
+#[tokio::test]
+async fn session_checkpoint_cache_preserves_lineage_archive_run_identity_rejection() {
+ let mut outcomes = Vec::new();
+ for cold in [false, true] {
+ let directory = tempfile::tempdir().unwrap();
+ let path = directory.path().join("archive-run-identity.sqlite");
+ let log = EventLog::open(&path).await.unwrap();
+ open(&log, "prior", false).await;
+ let target = tool(
+ &log,
+ "prior",
+ "prior-tool",
+ "retained base tool".repeat(800),
+ )
+ .await;
+ end(&log, "prior").await;
+ let source = continuation::opening("source", None);
+ continuation::append(&log, &source).await;
+ continuation::close(&log, &source).await;
+ let base = log
+ .context_before_run(&source.invocation, 100, 65536)
+ .await
+ .unwrap()
+ .source_evidence;
+ let claim = continuation::claim(
+ &log,
+ "archive-claim",
+ &source,
+ SessionBase {
+ high_water: base.high_water,
+ digest: base.digest,
+ },
+ )
+ .await;
+ let child = continuation::opening("archive-writer", Some(claim));
+ continuation::append(&log, &child).await;
+ log.append(&archive("archive-writer", &target))
+ .await
+ .unwrap();
+ continuation::close(&log, &child).await;
+ open(&log, "whole-session-summary", true).await;
+ let source = log
+ .prepare_context_compaction(
+ "session",
+ Some("whole-session-summary"),
+ &CheckpointMode::Standalone,
+ )
+ .await
+ .unwrap();
+ let pair = summary_pair(&log, "whole-session-summary", &source).await;
+ log.append_batch(&pair).await.unwrap();
+ let original = log.read_model_context("session", None).await.unwrap();
+ assert_eq!(
+ original.baseline.as_ref().unwrap().event_id,
+ pair[0].event().id
+ );
+ assert!(matches!(
+ original.source_evidence.scope,
+ LogScope::Session { .. }
+ ));
+
+ // A fresh Message may reuse a Run name. It must not erase the old
+ // archive's lineage identity requirement from the adopted checkpoint.
+ let mut reused = continuation::opening("new-message", None);
+ reused.invocation.run_id = "archive-writer".into();
+ continuation::append(&log, &reused).await;
+ let mut main = request("new-message", "next-main", Some(&original))
+ .event()
+ .clone();
+ main.invocation = reused.invocation;
+ let Fact::ModelRequested { purpose, .. } = &mut main.fact else {
+ panic!("fixture must be an actual model request")
+ };
+ *purpose = ModelPurpose::Main;
+ let log = if cold {
+ log.close().await.unwrap();
+ EventLog::open(&path).await.unwrap()
+ } else {
+ log
+ };
+ let safety = log
+ .prepare_prune_candidates("session", Some("new-message"), 0, 0, None)
+ .await
+ .unwrap();
+ assert!(safety.candidates.is_empty());
+ assert!(safety.next.is_none());
+ let prune = log
+ .prepare_prune_candidates("session", Some("new-message"), 128, 65536, None)
+ .await
+ .expect_err("prune must freshly validate the archived writer's lineage")
+ .to_string();
+ assert!(
+ prune.contains("Run names multiple invocations"),
+ "unexpected prune rejection: {prune}"
+ );
+ let outcome = log
+ .append(&EventWrite::plain(main).unwrap())
+ .await
+ .map(|_| ())
+ .map_err(|error| error.to_string());
+ outcomes.push((outcome, prune));
+ log.close().await.unwrap();
+ }
+ assert!(
+ outcomes[1]
+ .0
+ .as_ref()
+ .is_err_and(|message| message.contains("Run names multiple invocations")),
+ "full validation must retain the original archive writer identity: {:?}",
+ outcomes[1].0
+ );
+ assert_eq!(
+ outcomes[0], outcomes[1],
+ "cached and full validation must preserve the same archive writer identity rejection"
+ );
+}
diff --git a/products/maka/cli/notices.json b/products/maka/cli/notices.json
index b97fe9bd3b..067ebb8a46 100644
--- a/products/maka/cli/notices.json
+++ b/products/maka/cli/notices.json
@@ -1,6 +1,6 @@
{
"inputs": {
- "Cargo.lock": "cc8eb758caeae9c4e69b57afc5edbc421b8013b9c051a92f5f16a1d013fdf769",
+ "Cargo.lock": "812c6b26f81f98e9b1cacca52957dd2c9e35d110350887feb6590d4f0b57d872",
"Cargo.toml": "84dad9aaa98e01f9d194633a830d2c886c1e81de823436efc8057ad10a44f894",
"package-lock.json": "a5af15aca21fabe0e1bdd48784167df4dc81cee17777d54861dcbd641168a4eb",
"package.json": "38baf3beeb5876849ca16e31868d59bfaee769f260c7555b4a8a1847b92e28a1",
@@ -15,7 +15,7 @@
"platform/capabilities/Cargo.toml": "b6a0d3d90f3289fcc2f263fcad661df3d76ac2f5c953c07940f2b5c9c8afa5c8",
"platform/computer-use/Cargo.toml": "fc8a60fbb29acf1c9391a95dbca312203bf893fa28e873264ccf26226c22241f",
"platform/computer-use/THIRD_PARTY_NOTICES": "709951ed2ee5c4b9ed933f375a37c5b15ec0efa2dea340dc0f289906d72a7bde",
- "platform/event-log/Cargo.toml": "785b29ae887f80ac0b1ac7f64afa126580846cab06ae6a5d49455774c47e7216",
+ "platform/event-log/Cargo.toml": "e78dc980b90d9eb1934736ba856dd77e28e5c102c832c8e6793d8a5f1688be76",
"platform/host-js/Cargo.toml": "ad83d0133b55b69298aeb65db408c6de4170bf1d55afb47f93b121992335a3b5",
"platform/host-runtime/Cargo.toml": "6ac21566be141265f87876bd25bf30de562db4f5407b8cb77080b38984de967f",
"platform/js-runtime/Cargo.toml": "fff266ae188ff641e0dc13a7088fb023624d6446470fe2445de26c53947c824c",
diff --git a/scripts/rust/README.md b/scripts/rust/README.md
index b4bc9d1a6c..1bf34248c5 100644
--- a/scripts/rust/README.md
+++ b/scripts/rust/README.md
@@ -25,6 +25,8 @@ These scripts build source-bound native packages and their npm launcher. The pub
Native macOS packaging requires Xcode 26 or newer. Linux needs GLib, X11/XCB, xkbcommon, Wayland, Fontconfig and FreeType development files, plus cargo-zigbuild and Zig. The [native CI workflow](../../.github/workflows/npm-publication.yml) records the build environment.
+Rust builds require libclang to generate SQLite preupdate-hook bindings, including when a cached V8 bundle is available.
+
Release packages use pinned V8 builds with optional LGPL glibc math disabled. Completed libraries, bindings and notices are cached in `target/v8`, or `$CARGO_TARGET_DIR/v8`. On a miss, the builder downloads the bundle named by [v8-prebuilts.json](v8-prebuilts.json) and checks its digest. A corrupt cache or broken pin fails explicitly. An absent pin triggers a source build, which also needs Python, Git, Clang/libclang 19 or newer, a C++ toolchain, substantial disk space and time.
A cache key identifies the V8 source, target, features and producer code. For a new key, use the exact archives emitted by a completed native workflow. Publish them as versioned dependency prerelease assets, then record their URLs, hashes and validation evidence in the manifest. Preserve existing assets and key meanings. Native link and smoke checks still run for each package.
diff --git a/scripts/rust/README.zh-CN.md b/scripts/rust/README.zh-CN.md
index 5d33d56d2c..e571e0a208 100644
--- a/scripts/rust/README.zh-CN.md
+++ b/scripts/rust/README.zh-CN.md
@@ -25,6 +25,8 @@
macOS 原生打包需要 Xcode 26 或更新版本。Linux 需要 GLib、X11/XCB、xkbcommon、Wayland、Fontconfig 和 FreeType 开发文件,以及 cargo-zigbuild 与 Zig。[原生 CI 工作流](../../.github/workflows/npm-publication.yml)记录构建环境。
+Rust 构建需要 libclang 来生成 SQLite preupdate-hook 绑定,即使已有 V8 缓存包也需要。
+
发布包使用关闭可选 LGPL glibc 数学代码的固定 V8 构建。完成的库、绑定和 notices 缓存在 `target/v8` 或 `$CARGO_TARGET_DIR/v8`。缓存未命中时,构建器下载 [v8-prebuilts.json](v8-prebuilts.json)指定的包并校验摘要。缓存损坏或固定来源失效会直接报错;没有对应固定来源时才从源码构建。源码构建还需要 Python、Git、Clang/libclang 19 或更新版本、C++ 工具链,以及较多磁盘空间和时间。
缓存键标识 V8 源码、目标、特性和生成代码。新增键时,使用已完成原生工作流输出的精确归档,将其发布为带版本的依赖预发布资产,再把 URL、哈希和验证依据写入 manifest。保留已有资产及键的含义。每个包仍执行原生链接和 smoke 检查。
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org