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/22 20:12:22 UTC
(maka) branch rust-preview updated: perf(event-log): batch history cache fills and cache selection metadata
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 28f980ea57 perf(event-log): batch history cache fills and cache selection metadata
28f980ea57 is described below
commit 28f980ea571d36e511d9d66715585b96814aee60
Author: Wang <m4...@gmail.com>
AuthorDate: Fri Oct 23 04:09:33 2026 +0800
perf(event-log): batch history cache fills and cache selection metadata
History reads repeatedly prepared selection metadata statements and dispatched
three cache queries for every emitted sibling. Reuse the bounded SQLx cache for
selection metadata and validate/fill/verify same-source bodies in bounded batches.
Keep original tool ancestry, pending NULL errors, source identity and full payload
checks, transaction rollback, retention policy, and exact page budgets.
Add late-sibling rollback and pending/fence/rebinding coverage. Linux: 354 tests
passed, 5 existing skips; macOS: 353 passed, 5 existing skips. Strict all-target
Clippy, formatting, notices, current CLI build and controlled PTY startup/exit pass.
Five alternating original product pairs preserve all 1,010 provider requests and
canonical reads, with all 20 Hosts cleaned up. First-history median is 203.07 to
187.09 ms; reopened read is 205.90 to 186.79 ms. Workload RSS growth above each
idle baseline is unchanged; no memory reduction or whole-release/scale claim.
---
platform/event-log/src/transcript/bodycache.rs | 89 +++++++--
.../event-log/src/transcript/bodycache_tests.rs | 129 ++++++++++++++
platform/event-log/src/transcript/evidence.rs | 198 +++++++++++++++------
3 files changed, 344 insertions(+), 72 deletions(-)
diff --git a/platform/event-log/src/transcript/bodycache.rs b/platform/event-log/src/transcript/bodycache.rs
index cd6b47007f..62982214de 100644
--- a/platform/event-log/src/transcript/bodycache.rs
+++ b/platform/event-log/src/transcript/bodycache.rs
@@ -22,7 +22,7 @@ use super::read::sql_position;
use crate::StoreError;
use maka_agent_facts::history::RowPosition;
use sha2::{Digest, Sha256};
-use sqlx::{Connection, SqliteConnection};
+use sqlx::{Connection, QueryBuilder, Sqlite, SqliteConnection};
// The ordinary retained database is at most 32 MiB. One active producer may
// exceed that target so tiny fragment/sibling reads do not repeatedly emit it.
@@ -166,23 +166,80 @@ pub(super) async fn ensure(
if !rows.iter().any(|row| row.position == position) {
return Err(StoreError::TranscriptConflict);
}
- for row in rows {
- let (source, item) = sql_position(row.position)?;
- let identity: Option<(String,String)> = sqlx::query_as("SELECT turn_id,message_id FROM transcript_directory WHERE session_id=?1 AND source_sequence=?2 AND item_index=?3")
- .bind(session).bind(source).bind(item).fetch_optional(&mut *db).await?;
- if row.position.source_sequence != position.source_sequence
- || !matches!(identity,Some((turn,message)) if turn==row.message.turn_id && message==row.message.id)
- {
+ // Bound statement size and extra encoded bytes, not producer size. One
+ // oversized row still works; no further siblings join its batch. Every
+ // batch stays inside the caller's original read transaction.
+ let mut rows = rows.iter().peekable();
+ while rows.peek().is_some() {
+ let mut encoded = Vec::with_capacity(32);
+ let mut encoded_bytes = 0;
+ for row in rows.by_ref().take(32) {
+ if row.position.source_sequence != position.source_sequence {
+ return Err(StoreError::TranscriptConflict);
+ }
+ let (_, item) = sql_position(row.position)?;
+ let payload = serde_json::to_vec(&row.message)?;
+ let bytes = i64::try_from(payload.len()).map_err(|_| StoreError::PrefixTooLarge)?;
+ let digest = format!("sha256:{}", hex::encode(Sha256::digest(&payload)));
+ encoded_bytes += payload.len();
+ encoded.push((item, &row.message, payload, digest, bytes));
+ if encoded_bytes >= 64 * 1024 {
+ break;
+ }
+ }
+ let mut identities =
+ QueryBuilder::<Sqlite>::new("WITH wanted(item_index,turn_id,message_id) AS (");
+ identities.push_values(&encoded, |mut values, (item, message, ..)| {
+ values
+ .push_bind(item)
+ .push_bind(&message.turn_id)
+ .push_bind(&message.id);
+ });
+ identities.push(") SELECT count(*) FROM wanted JOIN transcript_directory d ON d.item_index=wanted.item_index AND d.turn_id=wanted.turn_id AND d.message_id=wanted.message_id WHERE d.session_id=")
+ .push_bind(session).push(" AND d.source_sequence=").push_bind(source);
+ let matched: i64 = identities.build_query_scalar().fetch_one(&mut *db).await?;
+ if matched != encoded.len() as i64 {
return Err(StoreError::TranscriptConflict);
}
- let payload = serde_json::to_vec(&row.message)?;
- let bytes = i64::try_from(payload.len()).map_err(|_| StoreError::PrefixTooLarge)?;
- let digest = format!("sha256:{}", hex::encode(Sha256::digest(&payload)));
- sqlx::query("INSERT OR IGNORE INTO transcript_cache.rows (session_id,source_sequence,item_index,turn_id,message_id,payload,digest,total_bytes) VALUES (?1,?2,?3,?4,?5,?6,?7,?8)")
- .bind(session).bind(source).bind(item).bind(&row.message.turn_id).bind(&row.message.id).bind(&payload).bind(&digest).bind(bytes).execute(&mut *db).await?;
- let matches: bool=sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM transcript_cache.rows WHERE session_id=?1 AND source_sequence=?2 AND item_index=?3 AND turn_id=?4 AND message_id=?5 AND payload=?6 AND digest=?7 AND total_bytes=?8)")
- .bind(session).bind(source).bind(item).bind(&row.message.turn_id).bind(&row.message.id).bind(payload).bind(digest).bind(bytes).fetch_one(&mut *db).await?;
- if !matches {
+ let mut insert = QueryBuilder::<Sqlite>::new(
+ "INSERT OR IGNORE INTO transcript_cache.rows (session_id,source_sequence,item_index,turn_id,message_id,payload,digest,total_bytes) ",
+ );
+ insert.push_values(
+ &encoded,
+ |mut values, (item, message, payload, digest, bytes)| {
+ values
+ .push_bind(session)
+ .push_bind(source)
+ .push_bind(item)
+ .push_bind(&message.turn_id)
+ .push_bind(&message.id)
+ .push_bind(payload)
+ .push_bind(digest)
+ .push_bind(bytes);
+ },
+ );
+ insert.build().execute(&mut *db).await?;
+ // A conflict must preserve an existing body and fail the entire read.
+ // Compare every field even when INSERT OR IGNORE changed no rows.
+ let mut verify = QueryBuilder::<Sqlite>::new(
+ "WITH wanted(item_index,turn_id,message_id,payload,digest,total_bytes) AS (",
+ );
+ verify.push_values(
+ &encoded,
+ |mut values, (item, message, payload, digest, bytes)| {
+ values
+ .push_bind(item)
+ .push_bind(&message.turn_id)
+ .push_bind(&message.id)
+ .push_bind(payload)
+ .push_bind(digest)
+ .push_bind(bytes);
+ },
+ );
+ verify.push(") SELECT count(*) FROM wanted JOIN transcript_cache.rows c ON c.item_index=wanted.item_index AND c.turn_id=wanted.turn_id AND c.message_id=wanted.message_id AND c.payload=wanted.payload AND c.digest=wanted.digest AND c.total_bytes=wanted.total_bytes WHERE c.session_id=")
+ .push_bind(session).push(" AND c.source_sequence=").push_bind(source);
+ let matched: i64 = verify.build_query_scalar().fetch_one(&mut *db).await?;
+ if matched != encoded.len() as i64 {
return Err(StoreError::TranscriptConflict);
}
}
diff --git a/platform/event-log/src/transcript/bodycache_tests.rs b/platform/event-log/src/transcript/bodycache_tests.rs
index f8eda7ef56..f9818d3a25 100644
--- a/platform/event-log/src/transcript/bodycache_tests.rs
+++ b/platform/event-log/src/transcript/bodycache_tests.rs
@@ -381,3 +381,132 @@ async fn oversized_source_reuses_fragments_then_reclaims_backing_file_on_replace
);
log.close().await.unwrap();
}
+
+#[tokio::test]
+async fn late_sibling_conflicts_roll_back_all_earlier_cache_fills() {
+ use maka_agent_facts::{
+ input::DeliveredMessage,
+ message::{MessageDisposition, Placement, RootSourceMessage, aggregate},
+ };
+ for corruption in ["identity", "payload", "digest", "bytes", "unique_message"] {
+ let directory = tempfile::tempdir().unwrap();
+ let log = EventLog::open(&directory.path().join("conflict.sqlite"))
+ .await
+ .unwrap();
+ log.create_session("cache", "create", &serde_json::json!({}), 1)
+ .await
+ .unwrap();
+ let sources: Vec<_> = (0..35)
+ .map(|index| {
+ let content: maka_agent_facts::input::MessageInput =
+ format!("sibling {index}").into();
+ let digest = content.content_digest().unwrap();
+ RootSourceMessage {
+ message: DeliveredMessage {
+ origin: Some(maka_agent_facts::message::MessageOrigin::WorkhubTask {
+ task_id: format!("task-{index}"),
+ }),
+ message_id: format!("message-{index}"),
+ content: content.clone(),
+ submitted_content_digest: digest,
+ },
+ unprepared_content: content,
+ submitted_placement: Placement::CurrentTurn,
+ disposition: MessageDisposition::TurnStarted,
+ submitted_intent: None,
+ }
+ })
+ .collect();
+ let through = user_input(
+ &log,
+ "siblings",
+ InvocationInput::Message {
+ content: aggregate(sources.iter().map(|source| &source.message.content)),
+ source_messages: sources,
+ request_fingerprint: None,
+ },
+ )
+ .await;
+ read(&log, through, Position::Start).await;
+ let before = inspect(&log, move |db| {
+ db.execute(
+ "DELETE FROM transcript_cache.rows WHERE item_index != 34",
+ [],
+ )
+ .unwrap();
+ assert_eq!(
+ db.query_row("SELECT count(*) FROM transcript_cache.rows", [], |row| row
+ .get::<_, i64>(
+ 0
+ ))
+ .unwrap(),
+ 1
+ );
+ match corruption {
+ "identity" => db.execute(
+ "UPDATE transcript_directory SET turn_id='wrong' WHERE item_index=34",
+ [],
+ ),
+ "payload" => db.execute("UPDATE transcript_cache.rows SET payload=x'00'", []),
+ "digest" => db.execute("UPDATE transcript_cache.rows SET digest='wrong'", []),
+ "bytes" => db.execute(
+ "UPDATE transcript_cache.rows SET total_bytes=total_bytes+1",
+ [],
+ ),
+ "unique_message" => {
+ db.execute("UPDATE transcript_cache.rows SET item_index=999", [])
+ }
+ _ => unreachable!(),
+ }
+ .unwrap();
+ db.query_row(
+ "SELECT item_index,payload,digest,total_bytes FROM transcript_cache.rows",
+ [],
+ |row| {
+ Ok((
+ row.get::<_, i64>(0)?,
+ row.get::<_, Vec<u8>>(1)?,
+ row.get::<_, String>(2)?,
+ row.get::<_, i64>(3)?,
+ ))
+ },
+ )
+ .unwrap()
+ })
+ .await;
+ let result = log
+ .history_text("cache", through, Position::Start, false, 8192, 1, 32768)
+ .await;
+ assert!(
+ matches!(result, Err(crate::StoreError::TranscriptConflict)),
+ "{corruption}: {result:?}"
+ );
+ inspect(&log, move |db| {
+ assert_eq!(
+ db.query_row("SELECT count(*) FROM transcript_cache.rows", [], |row| row
+ .get::<_, i64>(
+ 0
+ ))
+ .unwrap(),
+ 1
+ );
+ let after = db
+ .query_row(
+ "SELECT item_index,payload,digest,total_bytes FROM transcript_cache.rows",
+ [],
+ |row| {
+ Ok((
+ row.get::<_, i64>(0)?,
+ row.get::<_, Vec<u8>>(1)?,
+ row.get::<_, String>(2)?,
+ row.get::<_, i64>(3)?,
+ ))
+ },
+ )
+ .unwrap();
+ assert_eq!(after, before, "{corruption}");
+ })
+ .await;
+ log.close().await.unwrap();
+ }
+}
diff --git a/platform/event-log/src/transcript/evidence.rs b/platform/event-log/src/transcript/evidence.rs
index 6c53e2f2a5..ab1ec8b1ce 100644
--- a/platform/event-log/src/transcript/evidence.rs
+++ b/platform/event-log/src/transcript/evidence.rs
@@ -24,8 +24,6 @@ use rusqlite::{Connection as RawConnection, OptionalExtension, params};
use sqlx::SqliteConnection;
const HIGH: &str = r#"SELECT COALESCE(MAX(sequence), 0) FROM event_log"#;
-const KIND: &str = r#"SELECT kind FROM runtime_events WHERE sequence = ? AND invocation_id = ?"#;
-const EXTERNAL: &str = r#"SELECT EXISTS(SELECT 1 FROM runtime_events WHERE invocation_id = ?1 AND sequence <= ?2 AND kind = 'executor_started')"#;
const EXEC_BOUNDARY: &str = r#"SELECT sequence FROM runtime_events
WHERE invocation_id = ?1 AND sequence <= ?2 AND kind = 'invocation_opened'
UNION
@@ -85,9 +83,29 @@ const PENDING: &str = r#"SELECT operation_id FROM runtime_events AS request
AND boundary.sequence <= ?2)
ORDER BY sequence DESC LIMIT 1"#;
-/// Live readers retain their original transaction and reducer boundaries.
-/// Selection policy stays in one worker-local core; its final query uses the
-/// existing bounded SQLx statement cache across callbacks.
+// Tool boundaries keep their original ancestry path and do not inspect
+// external execution or unresolved model requests.
+const PLAN: &str = r#"WITH boundary(kind) AS (
+ SELECT CASE WHEN ?3 THEN (
+ SELECT kind FROM runtime_events WHERE sequence=?2 AND invocation_id=?1
+ ) END
+), classified AS (
+ SELECT kind, COALESCE(kind IN ('tool_dispatched','tool_rejected','tool_settled'),0) AS tool
+ FROM boundary
+)
+SELECT kind, tool, CASE WHEN tool THEN 0 ELSE EXISTS(
+ SELECT 1 FROM runtime_events WHERE invocation_id=?1 AND sequence<=?2 AND kind='executor_started'
+) END AS external FROM classified"#;
+
+#[derive(sqlx::FromRow)]
+struct Metadata {
+ kind: Option<String>,
+ tool: bool,
+ external: bool,
+}
+
+/// Live and preparation readers use the same metadata and selection policy.
+/// Both queries remain inside the reader's original transaction.
pub(super) async fn selected(
tx: &mut SqliteConnection,
invocation: &str,
@@ -95,17 +113,44 @@ pub(super) async fn selected(
through: i64,
boundary: bool,
) -> Result<Vec<i64>, StoreError> {
- let invocation = invocation.to_owned();
- let step = step.map(str::to_owned);
- let selection = tx
- .run_on_worker(move |handle| {
- // SAFETY: the worker exclusively borrows its handle for this callback.
- // This wrapper and its statements are destroyed before returning the plan.
- let db = unsafe { RawConnection::from_handle(handle.as_ptr()) }?;
- db.set_prepared_statement_cache_capacity(100);
- plan(&db, &invocation, step.as_deref(), through, boundary)
- })
- .await??;
+ let metadata = sqlx::query_as::<_, Metadata>(PLAN)
+ .bind(invocation)
+ .bind(through)
+ .bind(boundary)
+ .fetch_one(&mut *tx)
+ .await?;
+ let mut selection =
+ if let Some(selection) = plan(metadata, invocation, step, through, boundary)? {
+ selection
+ } else {
+ let invocation = invocation.to_owned();
+ tx.run_on_worker(move |handle| {
+ // SAFETY: no borrowed handle or statement escapes this callback.
+ let db = unsafe { RawConnection::from_handle(handle.as_ptr()) }?;
+ db.set_prepared_statement_cache_capacity(100);
+ super::tools::selection(&db, &invocation, through)
+ })
+ .await??
+ };
+ if let Some(slot) = selection.pending_step_slot() {
+ *slot = match sqlx::query_scalar::<_, Option<String>>(PENDING)
+ .bind(invocation)
+ .bind(through)
+ .fetch_optional(&mut *tx)
+ .await?
+ {
+ Some(Some(step)) => Some(step),
+ None => None,
+ Some(None) => {
+ return Err(rusqlite::Error::InvalidColumnType(
+ 0,
+ "operation_id".into(),
+ rusqlite::types::Type::Null,
+ )
+ .into());
+ }
+ };
+ }
selection.read(tx).await
}
@@ -191,65 +236,69 @@ pub(super) fn selected_on_handle(
through: i64,
boundary: bool,
) -> Result<Vec<i64>, StoreError> {
- plan(db, invocation, step, through, boundary)?.read_on_handle(db)
+ let metadata =
+ db.prepare_cached(PLAN)?
+ .query_row(params![invocation, through, boundary], |row| {
+ Ok(Metadata {
+ kind: row.get(0)?,
+ tool: row.get(1)?,
+ external: row.get(2)?,
+ })
+ })?;
+ let mut selection = match plan(metadata, invocation, step, through, boundary)? {
+ Some(selection) => selection,
+ None => super::tools::selection(db, invocation, through)?,
+ };
+ if let Some(slot) = selection.pending_step_slot() {
+ *slot = pending_step_on_handle(db, invocation, through)?;
+ }
+ selection.read_on_handle(db)
}
+/// None requests the existing tool ancestry planner; all other branches own
+/// their complete query inputs without retaining a connection or evidence.
fn plan(
- db: &RawConnection,
+ metadata: Metadata,
invocation: &str,
step: Option<&str>,
through: i64,
boundary: bool,
-) -> Result<Selection, StoreError> {
- let mut executor_boundary = false;
- if boundary {
- let kind: String = db
- .prepare_cached(KIND)?
- .query_row(params![through, invocation], |row| row.get(0))?;
- executor_boundary = kind == "executor_observed";
- if matches!(
- kind.as_str(),
- "tool_dispatched" | "tool_rejected" | "tool_settled"
- ) {
- return super::tools::selection(db, invocation, through);
- }
+) -> Result<Option<Selection>, StoreError> {
+ if boundary && metadata.kind.is_none() {
+ return Err(rusqlite::Error::QueryReturnedNoRows.into());
}
- let external: bool = db
- .prepare_cached(EXTERNAL)?
- .query_row(params![invocation, through], |row| row.get(0))?;
- if external {
- return Ok(if executor_boundary {
- Selection::ExecutorBoundary {
- invocation: invocation.to_owned(),
- through,
- boundary,
- }
- } else {
- Selection::ExecutorRest {
- invocation: invocation.to_owned(),
- through,
- boundary,
- }
- });
+ if metadata.tool {
+ return Ok(None);
+ }
+ if metadata.external {
+ return Ok(Some(
+ if metadata.kind.as_deref() == Some("executor_observed") {
+ Selection::ExecutorBoundary {
+ invocation: invocation.to_owned(),
+ through,
+ boundary,
+ }
+ } else {
+ Selection::ExecutorRest {
+ invocation: invocation.to_owned(),
+ through,
+ boundary,
+ }
+ },
+ ));
}
- let unresolved: Option<String> = if step.is_none() {
- pending_step_on_handle(db, invocation, through)?
- } else {
- None
- };
- let step = step.or(unresolved.as_deref());
let boundaries = serde_json::to_string(if boundary {
std::slice::from_ref(&through)
} else {
&[]
})?;
- Ok(Selection::Step {
+ Ok(Some(Selection::Step {
invocation: invocation.to_owned(),
step: step.map(str::to_owned),
through,
boundaries,
completed: false,
- })
+ }))
}
/// Owned query inputs selected under the current transaction. No evidence
@@ -275,6 +324,19 @@ pub(super) enum Selection {
}
impl Selection {
+ // A standalone tool deliberately has no model step. Only ordinary step
+ // selection may resolve a pending request, retaining NULL/error semantics.
+ fn pending_step_slot(&mut self) -> Option<&mut Option<String>> {
+ match self {
+ Self::Step {
+ step,
+ completed: false,
+ ..
+ } if step.is_none() => Some(step),
+ _ => None,
+ }
+ }
+
fn sql(&self) -> &'static str {
match self {
Self::Step { .. } => STEP,
@@ -384,6 +446,10 @@ mod tests {
for (step, through, boundary, expected) in [
(Some("42"), 5, false, vec![1, 2, 3]),
(None, 5, false, vec![1]),
+ (None, 3, false, vec![1, 2, 3]),
+ (None, 3, true, vec![1, 2, 3]),
+ (Some("42"), 7, false, vec![1, 2, 3, 6]),
+ (None, 7, false, vec![1]),
(Some("42"), 5, true, vec![1, 2, 3, 5]),
(Some("42"), 3, false, vec![1, 2, 3]),
(Some("missing"), 5, false, vec![1]),
@@ -397,6 +463,26 @@ mod tests {
}).await??;
assert_eq!(synchronous, expected);
}
+ assert!(selected(&mut tx, "invocation", None, 7, true).await.is_err());
+ let missing_boundary = tx.run_on_worker(move |handle| {
+ // SAFETY: the raw handle and all statements stay within this callback.
+ let raw = unsafe { RawConnection::from_handle(handle.as_ptr()) }?;
+ selected_on_handle(&raw, "invocation", None, 7, true)
+ }).await?;
+ assert!(missing_boundary.is_err());
+ // A malformed pending request with NULL identity must remain an
+ // error, rather than becoming indistinguishable from no request.
+ sqlx::query("INSERT INTO event_log(sequence,event_id,invocation_id,kind,event_json) VALUES(8,'event-8','invocation','model_requested',?)")
+ .bind(json!({"invocation":{"session_id":"session"},"fact":{"purpose":"main"}}).to_string())
+ .execute(&mut *tx).await?;
+ assert!(selected(&mut tx, "invocation", None, 8, false).await.is_err());
+ let malformed_pending = tx.run_on_worker(move |handle| {
+ // SAFETY: the handle and statements stay within this callback.
+ let raw = unsafe { RawConnection::from_handle(handle.as_ptr()) }?;
+ selected_on_handle(&raw, "invocation", None, 8, false)
+ }).await?;
+ assert!(malformed_pending.is_err());
+ assert_eq!(selected(&mut tx, "invocation", Some("42"), 8, false).await?, vec![1, 2, 3, 6]);
tx.rollback().await?;
Ok(())
})).await.unwrap();
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@maka.apache.org
For additional commands, e-mail: commits-help@maka.apache.org