World's first open-source enterprise world model.
Rust agent-memorybitemporalgraphragknowledge-baseknowledge-graphllmontologypgvectorpostgresqlragrustself-hostedsemantic-searchtemporal-knowledge-graphworld-model
The inference scheduler queues a base again only when it has no job waiting (#1304)
Quintin Botes4b492a12526f
diff --git a/crates/utopia-server/src/main.rs b/crates/utopia-server/src/main.rsindex 0e30b669..6617ea07 100644--- a/crates/utopia-server/src/main.rs+++ b/crates/utopia-server/src/main.rs@@ -260,21 +260,8 @@ async fn main() -> anyhow::Result<()> {tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);loop {tick.tick().await;- match utopia_store::reasoning::due_for_inference(&infer_state.pool).await {- Ok(due) => {- for kb_id in due {- if let Err(e) = utopia_store::jobs::enqueue(- &infer_state.pool,- "materialize_inferences",- serde_json::json!({ "kb_id": kb_id }),- )- .await- {- tracing::warn!(kb_id = %kb_id, error = %e, "推理任务入队失败");- }- }- }- Err(e) => tracing::warn!(error = %e, "扫描到期推理失败"),+ if let Err(e) = utopia_store::reasoning::queue_due_inferences(&infer_state.pool).await {+ tracing::warn!(error = %e, "扫描到期推理失败");}}});diff --git a/crates/utopia-store/src/reasoning.rs b/crates/utopia-store/src/reasoning.rsindex 48374d4f..607b3f5b 100644--- a/crates/utopia-store/src/reasoning.rs+++ b/crates/utopia-store/src/reasoning.rs@@ -2783,6 +2783,26 @@ pub async fn due_for_inference(pool: &PgPool) -> AppResult<Vec<Uuid>> {.await?)}+/// 定时扫描的一拍:到期的库各排一份推理任务,已经排着一份的不再排。+///+/// 只跳过排着的就够(#1304):处理器先 `mark_inference_ran` 再推,任务一开始跑这个库就+/// 不再到期;还到期的只有任务排着、`last_inference_at` 还没动的那段,而那段正是这里挡住的。+/// 一个库排不进去不拦着别的库+pub async fn queue_due_inferences(pool: &PgPool) -> AppResult<()> {+ for kb_id in due_for_inference(pool).await? {+ if let Err(e) = crate::jobs::enqueue_unless_queued(+ pool,+ "materialize_inferences",+ json!({ "kb_id": kb_id }),+ )+ .await+ {+ tracing::warn!(kb_id = %kb_id, error = %e, "推理任务入队失败");+ }+ }+ Ok(())+}+/// 记下这一轮推完的时间。////// **推完就记,哪怕什么都没变**:这一列答的是「上次看过没有」,不是「上次改过diff --git a/crates/utopia-store/tests/a_due_base_is_queued_once.rs b/crates/utopia-store/tests/a_due_base_is_queued_once.rsnew file mode 100644index 00000000..ac220f37--- /dev/null+++ b/crates/utopia-store/tests/a_due_base_is_queued_once.rs@@ -0,0 +1,52 @@+//! #1304:到期的库在它的推理任务还排着时只排一份。+//!+//! `queue_due_inferences` 扫的是全库到期的库,同时跑的测试刚建的库也会被它排上任务,所以+//! 这个测试自成一个二进制(为什么见 `store/main.rs`),收尾删掉它排下的**每一份**+//! `materialize_inferences`,不只是自己那个库的。++use sqlx::PgPool;+use utopia_store::reasoning::queue_due_inferences;+use uuid::Uuid;++#[tokio::test]+async fn a_due_base_is_queued_once_while_its_job_waits() -> anyhow::Result<()> {+ let Some(url) = utopia_store::test_db::url() else {+ return Ok(());+ };+ let pool = PgPool::connect(&url).await?;+ let (org, ws, kb) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());+ sqlx::raw_sql(&format!(+ "INSERT INTO organizations(id,name) VALUES ('{org}','inference-schedule');+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','inference-schedule');+ INSERT INTO knowledge_bases(id,workspace_id,name,materialize_inferences)+ VALUES ('{kb}','{ws}','inference-schedule',true);"+ ))+ .execute(&pool)+ .await?;+ let before: i64 = sqlx::query_scalar("SELECT coalesce(max(id), 0) FROM jobs")+ .fetch_one(&pool)+ .await?;++ // 两拍之间没人认领:从没推过的库两拍都算到期+ queue_due_inferences(&pool).await?;+ queue_due_inferences(&pool).await?;+ let queued: i64 = sqlx::query_scalar(+ "SELECT count(*) FROM jobs+ WHERE kind = 'materialize_inferences' AND status = 'queued' AND payload->>'kb_id' = $1",+ )+ .bind(kb.to_string())+ .fetch_one(&pool)+ .await?;++ sqlx::query("DELETE FROM jobs WHERE kind = 'materialize_inferences' AND id > $1")+ .bind(before)+ .execute(&pool)+ .await?;+ sqlx::query("DELETE FROM organizations WHERE id = $1")+ .bind(org)+ .execute(&pool)+ .await?;+ pool.close().await;+ assert_eq!(queued, 1);+ Ok(())+}diff --git a/crates/utopia-store/tests/store/main.rs b/crates/utopia-store/tests/store/main.rsindex 45cfd7e1..7bf02f19 100644--- a/crates/utopia-store/tests/store/main.rs+++ b/crates/utopia-store/tests/store/main.rs@@ -22,6 +22,9 @@//! 过滤——修正行、重复行都按全库找,会改到别的测试正在物化、改写的行。//! - `a_row_a_rule_implied_follows_its_statements`:把迁移 0101 原样跑一遍,同样不按 kb//! 过滤——它按来源重算全库类型化行的见证,会改到别的测试正在物化的行。+//! - `a_due_base_is_queued_once`:`queue_due_inferences` 扫全库到期的库。0050 起新建的库开着+//! 推理又从没推过,别的测试刚建的库全算到期,会被它排上推理任务——数自己库任务的测试+//! (比如 lib 单元测试里的 `phrase_bindings_delivery_tests`)就会多数出一个。//!//! 新加测试默认放这个目录;只有碰上面那类全局状态时才放顶层,并在这里补一行为什么。mod a_batch_decides_like_a_person;