Skip to content

World's first open-source enterprise world model.

A document that states no date is not asked for one (#998)

WaylandYangb036f456ec85
diff --git a/crates/utopia-server/src/time_context_tests.rs b/crates/utopia-server/src/time_context_tests.rs
index d5a5765d..5dfa4b55 100644
--- a/crates/utopia-server/src/time_context_tests.rs
+++ b/crates/utopia-server/src/time_context_tests.rs
@@ -237,3 +237,115 @@ async fn last_week_is_counted_from_the_date_of_its_own_section() -> anyhow::Resu
.await?;
run
}
+
+/// 文档没说自己是哪天的:抽取报了「没有」,就是没有。不再另问一次开头;相对的时间词
+/// 锚不到,等着(C 级),陈述没有见证。
+#[tokio::test]
+async fn a_document_that_states_no_date_is_not_asked_for_one() -> anyhow::Result<()> {
+ let Some(url) = utopia_store::test_db::url() else {
+ return Ok(());
+ };
+ let pool = sqlx::PgPool::connect(&url).await?;
+ utopia_store::db::migrate(&pool).await?;
+ let ids: Vec<Uuid> = (0..5).map(|_| Uuid::now_v7()).collect();
+ let (org, ws, kb, doc, chunk) = (ids[0], ids[1], ids[2], ids[3], ids[4]);
+ sqlx::raw_sql(&format!(
+ "INSERT INTO organizations(id,name) VALUES ('{org}','time-context-undated');
+ INSERT INTO workspaces(id,org_id,name) VALUES ('{ws}','{org}','time-context-undated');
+ INSERT INTO knowledge_bases(id,workspace_id,name) VALUES ('{kb}','{ws}','time-context-undated');
+ INSERT INTO documents(id,kb_id,filename,sha256) VALUES ('{doc}','{kb}','背景.md','x');
+ INSERT INTO chunks(id,kb_id,document_id,seq,text) VALUES
+ ('{chunk}','{kb}','{doc}',0,'# 项目背景\n\n去年,码表一代进入北美市场。');"
+ ))
+ .execute(&pool)
+ .await?;
+ let model = Model {
+ replies: Arc::new(Mutex::new(vec![
+ json!({
+ "e": [["码表一代", "产品", 1], ["北美市场", "市场", 1]],
+ "s": [["去年,码表一代进入北美市场。", "码表一代", "进入", "北美市场", null, null, "去年", null]],
+ "n": [],
+ "t": []
+ })
+ .to_string(),
+ json!({ "m": [[0, "point", {
+ "kind": "anchored", "anchor": {"kind": "document"},
+ "offset": {"count": 1, "unit": "year", "direction": "before"}
+ }, "year"]] })
+ .to_string(),
+ ])),
+ requests: Arc::new(Mutex::new(Vec::new())),
+ };
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
+ let endpoint = format!("http://{}", listener.local_addr()?);
+ let router = Router::new()
+ .route("/chat/completions", post(reply))
+ .with_state(model.clone());
+ let server = tokio::spawn(async move { axum::serve(listener, router).await.unwrap() });
+ utopia_store::settings::upsert(
+ &pool,
+ ws,
+ Some(&endpoint),
+ None,
+ Some("scripted"),
+ None,
+ None,
+ None,
+ None,
+ )
+ .await?;
+ let dir = tempfile::tempdir()?;
+ let cfg = utopia_core::config::AppConfig {
+ data_dir: dir.path().to_string_lossy().into_owned(),
+ ..Default::default()
+ };
+ let search = Arc::new(utopia_search::SearchIndex::open(
+ &dir.path().join("search"),
+ )?);
+ let state = AppState::new(pool.clone(), &cfg, search, "test-only".into());
+
+ let run = async {
+ crate::extraction::extract_document(&state, doc, utopia_core::models::Proposer::default())
+ .await?;
+ resolve_document(&state, doc).await?;
+ let requests = model.requests.lock().unwrap().clone();
+ assert_eq!(
+ requests.len(),
+ 2,
+ "one extraction call and one interpretation call; the opening is not read for a date"
+ );
+ for r in &requests {
+ let system = r["messages"][0]["content"].as_str().unwrap_or("");
+ assert!(
+ !system.starts_with("You read the opening of a document"),
+ "the dating prompt is not sent"
+ );
+ }
+ let row: (Option<DateTime<Utc>>, Option<String>, Option<DateTime<Utc>>) = sqlx::query_as(
+ "SELECT valid_from, valid_from_grade, attested_from FROM facts
+ WHERE kb_id = $1 AND layer = 'open'",
+ )
+ .bind(kb)
+ .fetch_one(&pool)
+ .await?;
+ assert_eq!(
+ row,
+ (None, Some("C".to_string()), None),
+ "no anchor: the time word waits, and the statement has no attestation"
+ );
+ Ok::<(), anyhow::Error>(())
+ }
+ .await;
+
+ server.abort();
+ sqlx::query("DELETE FROM jobs WHERE payload->>'document_id'=$1 OR payload->>'kb_id'=$2")
+ .bind(doc.to_string())
+ .bind(kb.to_string())
+ .execute(&pool)
+ .await?;
+ sqlx::query("DELETE FROM organizations WHERE id=$1")
+ .bind(org)
+ .execute(&pool)
+ .await?;
+ run
+}
diff --git a/crates/utopia-server/src/time_resolution.rs b/crates/utopia-server/src/time_resolution.rs
index 283f485e..4d66383a 100644
--- a/crates/utopia-server/src/time_resolution.rs
+++ b/crates/utopia-server/src/time_resolution.rs
@@ -2,7 +2,7 @@
//!
//! 抽取只记时间**词**(`time_mentions`:原样的字、块、偏移、起/止)。这里是它后面的任务:
//!
-//! 1. **文档时间上下文**(决定 3)。一篇文档只做一次:把开头交给模型,它转写文档自己的日期
+//! 1. **文档时间上下文**(决定 3;0064 起由抽取时各块报上来,这里不再问模型)。从前:把开头交给模型,它转写文档自己的日期
//! (落款、备案日、公报的报告年)和文档命名的期间(「fiscal 2027」及其起止);日期词要在
//! 开头里核对得到才算数。结果存在 `documents.time_context`;文档自己的日期写进 `doc_time`
//! (来源 `content`)。上传时间永远不进来(#714)。
@@ -14,24 +14,21 @@
//! B 级,锚不到的是 C 级——C 级什么也不写,等锚点(决定 4、6)。
//! 4. 结果落到开放陈述的 `valid_from` / `valid_to`(决定 5 的第一根轴),时间轴上才有它。
-use crate::extraction::{chat_retrying_rate_limits_at, span_in_quote};
+use crate::extraction::chat_retrying_rate_limits_at;
use crate::llm_util;
use crate::state::AppState;
use chrono::{DateTime, Duration, Months, NaiveDate, NaiveTime, TimeZone, Utc};
use std::collections::HashMap;
use utopia_extract::time::{
- build_dating_messages, build_interpretation_messages, headings_at, now_in_force,
- parse_dating_response, parse_interpretation_response, Anchor, DateParts, Direction,
- DocumentDating, Granularity, Interpretation, MentionInput, NamedPeriod, Offset, Reference,
- Shape, TimeContext, Unit,
+ build_interpretation_messages, headings_at, now_in_force, parse_interpretation_response,
+ Anchor, DateParts, Direction, DocumentDating, Granularity, Interpretation, MentionInput,
+ NamedPeriod, Offset, Reference, Shape, TimeContext, Unit,
};
use utopia_store::graph::{truncate_to, ENDED_UNKNOWN, WORLD_PRECISIONS};
use uuid::Uuid;
/// 一批解释问多少个提及。
const BATCH: usize = 40;
-/// 文档开头给模型看多少字:落款、报告年、财年定义都在前面
-const OPENING_CHARS: usize = 3000;
/// 一次解析的结果:世界轴上的位置与等级。
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -382,7 +379,7 @@ pub async fn resolve_document(state: &AppState, document_id: Uuid) -> anyhow::Re
let client = llm_util::chat_client(&settings)
.ok_or_else(|| anyhow::anyhow!("Chat model not configured; cannot resolve time"))?;
- // 1. 文档时间上下文:一篇只问一次。抽取已经报上来的(0064)不再问
+ // 1. 文档时间上下文:抽取时各块报上来的(0064)。这里不问模型
let context: DocumentDating = match doc.time_context.clone() {
Some(json) => {
let context: DocumentDating = serde_json::from_value(json)?;
@@ -394,40 +391,14 @@ pub async fn resolve_document(state: &AppState, document_id: Uuid) -> anyhow::Re
}
context
}
- None => {
- let opening = utopia_store::documents::opening_chunk(pool, document_id)
- .await?
- .map(|(_, text)| text)
- .unwrap_or_default();
- let opening: String = opening.chars().take(OPENING_CHARS).collect();
- let messages = build_dating_messages(&doc.filename, &opening);
- let reply =
- chat_retrying_rate_limits_at(state, &settings, &client, &messages, Some(0.0))
- .await?;
- let mut dating = parse_dating_response(&reply.text)?;
- // 日期词要在开头里核对得到(与名字、引文同一条规矩);核不到的日期不算
- match dating.date_words.as_deref() {
- Some(words) if span_in_quote(words, &opening) => {}
- _ => dating.date = None,
- }
- utopia_store::documents::set_time_context(
- pool,
- document_id,
- &serde_json::to_value(&dating)?,
- )
- .await?;
- // 文档自己的日期(决定 3):只在它还没有内容或来源给的日期时写
- if crate::extraction_open::dated_at(&doc).is_none() {
- if let Some((t, _)) = dating.date.as_ref().and_then(parts_to_time) {
- utopia_store::documents::set_content_date(pool, document_id, t).await?;
- }
- }
- dating
- }
+ // 抽取一条日期都没报:文档没说自己是哪天的。不再另问一次开头(0064 决定 1)——
+ // 那一次调用在抽取报了「没有」的文档上问的是同一个问题,测量里二十篇有十六篇这样
+ // 白问;文档里写了日期的,抽取都报上来了
+ None => DocumentDating::default(),
};
- // 老路读开头定出来的日期(抽取一条都没报的时候):它就是整篇的起算点,和抽取报上来的
- // 一样进条目里,下面给陈述作证、给相对的时间词当基准都从条目里找
+ // 这一刀之前读开头定出来、存在文档上的日期(只有日期、没有条目):它就是整篇的起算点,
+ // 和抽取报上来的一样进条目里,下面给陈述作证、给相对的时间词当基准都从条目里找
let mut context = context;
if context.entries.is_empty() {
if let (Some(date), Some(words)) = (context.date.clone(), context.date_words.clone()) {
diff --git a/docs/decisions/0064-extraction-reads-time-as-it-goes.md b/docs/decisions/0064-extraction-reads-time-as-it-goes.md
index 12b6d947..271bdabb 100644
--- a/docs/decisions/0064-extraction-reads-time-as-it-goes.md
+++ b/docs/decisions/0064-extraction-reads-time-as-it-goes.md
@@ -34,7 +34,7 @@ Three things are wrong and they are one thing.
- **periods and calendars**: as 0045 decision 3 has them;
- **anchors the narrative sets**: an event the text has dated ("上市" is 2024-03), so a later "上市半年后" or "验厂后两个月" has something to be measured from.
-The separate dating call over the opening goes away when extraction reported any entry, and stays as the fallback when it reported none. The context is stored with the document as today.
+The separate dating call over the opening goes away. Cut 2 kept it as a fallback for documents where extraction reported no entry; measured on twenty Re-DocRED documents, sixteen took that fallback, each asking the question extraction had just answered with "none", about 940 tokens a document, and on the time bench it never found a date extraction had missed. It was removed (#998). The context is stored with the document as today.
*Revised with cut 2 (2026-09-28).* As first written this decision had the context travel from chunk to chunk in the prompt, the way the list of recorded things does. That ties time to the order chunks are read in, and #588 is moving the model calls of one document to run at once. What an entry governs is decided by structure instead: the chunker opens every chunk with the headings it lives under, so the chunk's own text says which section a date was stated in and which section a time word sits in. The scope of an entry is the heading path at its position; the reference point in force for a mention is the `now` entry with the deepest scope that is a prefix of the mention's path. Nothing is passed between calls, and the result is the same whether chunks are read one after another or together. Narrative anchors (the fourth kind) stay with the mentions of 0045: a dated mention is already an anchor for a later one in the same document.