Skip to content

World's first open-source enterprise world model.

The tab strip starts where the rows do (#771)

WaylandYang03417131d151
diff --git a/crates/utopia-ingest/src/lib.rs b/crates/utopia-ingest/src/lib.rs
index 94a76e44..b340695c 100644
--- a/crates/utopia-ingest/src/lib.rs
+++ b/crates/utopia-ingest/src/lib.rs
@@ -173,10 +173,12 @@ mod table;
pub mod transcript;
pub use chunker::{chunk_segments, chunk_text, chunk_with_budget, ChunkPiece, BUDGET_TOKENS};
+// 抽取那一侧要按值找回它那一列的表头(#729):写出这个表格式的模块,也负责读回它
/// Decode fetched text with the same encoding detection as file ingestion.
pub use parsers::plain_text as decode_text;
pub use provenance::{Origin, Provenance, Segment};
pub use reading::Reading;
+pub use table::column_header;
/// 解析产物:纯文本 + 可选结构信息。
#[derive(Debug)]
diff --git a/crates/utopia-ingest/src/table.rs b/crates/utopia-ingest/src/table.rs
index dc03b3ab..e56a5c9d 100644
--- a/crates/utopia-ingest/src/table.rs
+++ b/crates/utopia-ingest/src/table.rs
@@ -603,10 +603,160 @@ fn collapse_spans(cols: Vec<usize>, headers: &[Row], data: &[(String, Row)]) ->
out
}
+/// 一行切成格:每格的文字,以及它在这一行里的字节偏移。行首行尾的空段不算格。
+/// **带着偏移**,因为表头里两列同名是常事(两个「Amount」),光按文字回头找会指错格
+fn cells(row: &str) -> Vec<(&str, usize)> {
+ let lead = row.len() - row.trim_start().len();
+ let trimmed = row.trim();
+ let (inner, open) = match trimmed.strip_prefix('|') {
+ Some(rest) => (rest, 1),
+ None => (trimmed, 0),
+ };
+ let inner = inner.strip_suffix('|').unwrap_or(inner);
+ let mut at = lead + open;
+ let mut out = Vec::new();
+ for piece in inner.split('|') {
+ let pad = piece.len() - piece.trim_start().len();
+ out.push((piece.trim(), at + pad));
+ at += piece.len() + 1;
+ }
+ out
+}
+
+/// 比较一格与一个值:空白、千分位逗号、货币符号不算数,别的一个字都不能差。
+/// **不做包含**:一格写着 `$1,234`,值是 `234` 的那一条不该认领它
+fn same_number(cell: &str, value: &str) -> bool {
+ let strip = |s: &str| {
+ s.chars()
+ .filter(|c| !c.is_whitespace() && !is_currency(*c) && *c != ',')
+ .collect::<String>()
+ };
+ let (a, b) = (strip(cell), strip(value));
+ !a.is_empty() && a == b
+}
+
+/// 这个值落在这一行的哪一列,以及那一列的表头是什么(#729:财务表格单元的期间列)。
+///
+/// 一张财务表的列头写着期间(「Three Months Ended July 26, 2026」),值写在行里。**人是
+/// 按列读表的,抽取却是按行读的**:一行五个数进了图,五条陈述除了值一模一样,没有任何
+/// 东西说得出哪个数属于哪个期间。这里把那一列的表头找回来,交给时间解析去判它是不是一个
+/// 期间——代码不认「Months Ended」这类字眼,只认位置。
+///
+/// 它宁可什么都不给,也不给错的:
+///
+/// * 这一行在这一块里出现不止一次——说不清是哪张表的哪一行;
+/// * 这一行里没有恰好一格等于这个值——两格相等就分不出是哪一列;
+/// * 这一行上面不是 `| --- |` 分隔线,或分隔线上面不是表头行;
+/// * 表头那一格是空的,或一个数字都没有(「Amount」不是期间,而任何期间都会写出年份、
+/// 日期或季度的数字)。
+///
+/// 返回表头那一格的文字和它在这一块里的字节偏移——一条提及要说得出自己在原文的哪个位置。
+pub fn column_header<'a>(chunk: &'a str, row: &str, value: &str) -> Option<(&'a str, usize)> {
+ let row = row.trim();
+ if !row.starts_with('|') {
+ return None;
+ }
+ let at = chunk.find(row)?;
+ if chunk[at + row.len()..].contains(row) {
+ return None;
+ }
+ let mut hit = cells(row)
+ .into_iter()
+ .enumerate()
+ .filter(|(_, (c, _))| same_number(c, value));
+ let (col, _) = hit.next()?;
+ if hit.next().is_some() {
+ return None;
+ }
+ // 紧挨着这一行往上:一条分隔线,再上去一行是表头。位置用 rfind 从原文里量,
+ // 不靠换行符占几个字节
+ let before = &chunk[..at];
+ let mut lines = before.lines().rev();
+ let sep = lines.next()?;
+ if !sep.trim_start().starts_with("| ---") {
+ return None;
+ }
+ let head = lines.next()?;
+ if !head.trim_start().starts_with('|') {
+ return None;
+ }
+ let sep_at = before.rfind(sep)?;
+ let head_at = before[..sep_at].rfind(head)?;
+ let (cell, in_line) = *cells(head).get(col)?;
+ if cell.is_empty() || !cell.chars().any(|c| c.is_ascii_digit()) {
+ return None;
+ }
+ let from = head_at + in_line;
+ Some((&chunk[from..from + cell.len()], from))
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ /// #729:一行五个数,每个数的期间在它那一列的表头上
+ const COSTS: &str = "(A) Acquisition-related and other costs are comprised of amortization.\n\n\
+| | Three Months Ended July 26, 2026 | Three Months Ended April 26, 2026 | Six Months Ended July 26, 2026 |\n\
+| --- | --- | --- | --- |\n\
+| Cost of revenue | $46 | $47 | $93 |\n\
+| Sales, general and administrative | $6 | $5 | $11 |\n";
+
+ #[test]
+ fn a_cell_takes_the_period_of_its_own_column() {
+ let row = "| Cost of revenue | $46 | $47 | $93 |";
+ let (head, at) = column_header(COSTS, row, "$46").expect("46 是第一列的值");
+ assert_eq!(head, "Three Months Ended July 26, 2026");
+ assert_eq!(&COSTS[at..at + head.len()], head, "偏移指回原文那一格");
+ assert_eq!(
+ column_header(COSTS, row, "$47").unwrap().0,
+ "Three Months Ended April 26, 2026",
+ "同一行的下一个数换了一列,期间也跟着换"
+ );
+ assert_eq!(
+ column_header(COSTS, row, "93").unwrap().0,
+ "Six Months Ended July 26, 2026",
+ "货币符号与千分位不算数"
+ );
+ }
+
+ #[test]
+ fn a_header_that_names_no_time_gives_nothing() {
+ let t = "| Item | Amount |\n| --- | --- |\n| Cost of revenue | $46 |\n";
+ assert_eq!(column_header(t, "| Cost of revenue | $46 |", "$46"), None);
+ }
+
+ #[test]
+ fn an_ambiguous_cell_gives_nothing() {
+ // 同一行两格相同:说不出是哪一列
+ let t = "| | Q1 2026 | Q2 2026 |\n| --- | --- | --- |\n| Cost | $46 | $46 |\n";
+ assert_eq!(column_header(t, "| Cost | $46 | $46 |", "$46"), None);
+ // 同一块里两张表有一模一样的行:说不出是哪张表
+ let twice = format!("{COSTS}\n{COSTS}");
+ assert_eq!(
+ column_header(&twice, "| Cost of revenue | $46 | $47 | $93 |", "$46"),
+ None
+ );
+ }
+
+ #[test]
+ fn a_row_with_no_table_above_it_gives_nothing() {
+ // 没有分隔线就没有表头
+ let t = "| Cost of revenue | $46 |\n";
+ assert_eq!(column_header(t, "| Cost of revenue | $46 |", "$46"), None);
+ // 引文不是一行表格
+ assert_eq!(
+ column_header(COSTS, "Acquisition-related and other costs", "$46"),
+ None
+ );
+ // 一格写着 $1,234,值是 234 的不该认领它
+ let t2 = "| | Q1 2026 |\n| --- | --- |\n| Cost | $1,234 |\n";
+ assert_eq!(column_header(t2, "| Cost | $1,234 |", "234"), None);
+ assert_eq!(
+ column_header(t2, "| Cost | $1,234 |", "1234").unwrap().0,
+ "Q1 2026"
+ );
+ }
+
fn render(html: &str) -> String {
let (_, tables) = lift_tables(html);
tables.join("\n=====\n")
diff --git a/crates/utopia-server/src/extraction_open.rs b/crates/utopia-server/src/extraction_open.rs
index b6d2da2d..1bb4e658 100644
--- a/crates/utopia-server/src/extraction_open.rs
+++ b/crates/utopia-server/src/extraction_open.rs
@@ -59,6 +59,20 @@ fn locate(hay: &str, needle: &str) -> Option<(i32, i32)> {
/// 时间词在块里的字符起点。**必须在这条陈述自己的那句引文里**:模型会把一个时间词挂到
/// 好几条陈述上(FDA 语料实测「week 4」挂到了「不应由过敏患者服用」上,六条带时间的陈述错了三条),
/// 整块里搜得到不等于这句说了它。引文里有、但引文本身没在块里定位到的,起点退回整块里的第一处
+/// 这条陈述的值有没有真的写在它的引文里(#729)。
+///
+/// 千分位逗号、空白和货币符号两边都不算数;别的按原样比。**故意放得松**:这是一条丢弃
+/// 规则,放过一条可疑的,好过丢掉一条对的
+fn shows_value(quote: &str, value: &str) -> bool {
+ let strip = |s: &str| {
+ s.chars()
+ .filter(|c| !c.is_whitespace() && *c != ',' && !matches!(c, '$' | '¥' | '€' | '£'))
+ .collect::<String>()
+ };
+ let v = strip(value);
+ v.is_empty() || strip(quote).contains(&v)
+}
+
fn locate_time(chunk: &str, quote: Option<(&str, Option<(i32, i32)>)>, words: &str) -> Option<i32> {
let (q, span) = quote?;
if let Some((inner, _)) = locate(q, words) {
@@ -376,6 +390,9 @@ pub(crate) async fn run_open(
.as_deref()
.map(str::trim)
.filter(|v| v.chars().any(char::is_alphanumeric));
+ // 模型自己写的那个值,在下面被「宾语没声明」顶替之前记下来:引文要核的是它,
+ // 不是一个实体的名字(#729)
+ let stated_value = value;
let object = match s.object.as_deref().map(str::trim).filter(|o| !o.is_empty()) {
Some(name) => match place(
pool,
@@ -478,6 +495,23 @@ pub(crate) async fn run_open(
)
.await;
}
+ // **值必须出现在它自己的引文里**(#729)。一行五列被压成五条只有值不同的
+ // 陈述、引文却是表上面那句导语时,五条里至多一条对,而谁也分不出是哪一条。
+ // 只查带数字的值:实体名当值写的那一类(object_undeclared)不在此列
+ if let (Some(v), Some((q, _))) = (stated_value, quote) {
+ if v.chars().any(|c| c.is_ascii_digit()) && !shows_value(q, v) {
+ drop_signal(
+ state,
+ kb_id,
+ document_id,
+ reason::VALUE_NOT_IN_QUOTE,
+ "a statement's value is not in its own quoted sentence",
+ Some(v),
+ )
+ .await;
+ continue;
+ }
+ }
// 开放陈述没有模型自报的置信度:它说的是「文档这么说了」。看图描述出来的块
// 照旧压上限(0040 决定 4)
let confidence = origin_ceiling(&chunk.origin, 1.0);
@@ -526,6 +560,20 @@ pub(crate) async fn run_open(
}
}
}
+ // **表格单元的期间在它那一列的表头上**(#729)。模型自己说了时间就不动它——
+ // 它看得见整块原文,说得出的比一根列头多。这一条不是模型报的,所以不走
+ // `time_not_in_quote`:它的出处是位置(这个值在这一行的第几格),
+ // 而那一格的表头上写着期间。它是不是一个期间,由时间解析去判(0045:
+ // 模型读、代码算),这里一个字眼都不认
+ if !time_words.iter().any(|(_, _, role)| *role == "when") {
+ if let (Some(v), Some((q, _))) = (stated_value, quote) {
+ if let Some((head, at)) = utopia_ingest::column_header(&chunk.text, q, v) {
+ if let Ok(at) = i32::try_from(at) {
+ time_words.push((head, at, "when"));
+ }
+ }
+ }
+ }
if await_nod {
// 记忆日志:一切原样进待确认表(0015),人点头时 `pending::confirm` 才把它
// 落成开放陈述——同样的短语、限定、时间词、引文偏移
@@ -769,6 +817,31 @@ pub(crate) async fn run_open(
mod tests {
use super::*;
+ /// #729:引文里没有这个数,这条陈述就没有依据。丢弃规则要宁松勿紧
+ #[test]
+ fn a_value_must_be_in_the_sentence_that_is_quoted() {
+ let row = "| Cost of revenue | $46 | $47 | $93 |";
+ assert!(shows_value(row, "$46"));
+ assert!(shows_value(row, "46"), "货币符号不算数");
+ assert!(
+ shows_value("| Accounts payable | 1,915 |", "1915"),
+ "千分位不算数"
+ );
+ assert!(shows_value("revenue was $ 1,234", "$1,234"), "空白不算数");
+ assert!(
+ !shows_value(
+ "(A) Acquisition-related costs are comprised of amortization.",
+ "$46"
+ ),
+ "导语里没有这个数——正是压平表格那一行的错法"
+ );
+ assert!(shows_value("anything", ""), "没有值就没什么可核的");
+ assert!(
+ shows_value("| a | 4,600 |", "46"),
+ "放得松:像是命中就放过,丢错一条比留下一条可疑的更糟"
+ );
+ }
+
#[test]
fn offsets_are_in_characters_not_bytes() {
let text = "北京的冬天很冷。去年冬天下了三场雪。";
diff --git a/crates/utopia-store/src/extraction_drops.rs b/crates/utopia-store/src/extraction_drops.rs
index e25d92b4..b68e613a 100644
--- a/crates/utopia-store/src/extraction_drops.rs
+++ b/crates/utopia-store/src/extraction_drops.rs
@@ -27,6 +27,12 @@ pub mod reason {
pub const MALFORMED_ITEM: &str = "malformed_item";
/// 模型输出被截断(撞上 max_tokens)→ 已完整的那些留下,尾巴丢掉
pub const TRUNCATED_REPLY: &str = "truncated_reply";
+ /// 这条陈述的值不在它自己的引文里(#729):证据没写着这个数。
+ ///
+ /// 表格是这个错的产地——模型把一行五列压成五条只有值不同的陈述,引文却指向表上面
+ /// 那句导语。五条里至多一条对,而图上没有任何东西分得出是哪一条。**引文是这条陈述
+ /// 的全部依据**:值不在里面,它就不是文档说的
+ pub const VALUE_NOT_IN_QUOTE: &str = "value_not_in_quote";
/// 模型报的别名(或它的引文)不在这一块原文里(0041 决定 2):不记这个名字。
/// 名字是召回的桥,一座凭空的桥会把两个不相干的实体接到一起
pub const NAME_NOT_IN_TEXT: &str = "name_not_in_text";
diff --git a/web/src/pages/Graph.tsx b/web/src/pages/Graph.tsx
index 73e16a46..b6d2fef9 100644
--- a/web/src/pages/Graph.tsx
+++ b/web/src/pages/Graph.tsx
@@ -2894,13 +2894,14 @@ function EntityPanel({
{/* 视图切换:Relations(一张表,过去的折在组尾)| History(记录轴)| Derived */}
- {/* **左边比头部多一档**(24 而不是 16)。两个盒子本来都从 px-4 起,可
- 分段控件自己还有一圈内距(p-1 加按钮的 px-2),于是「Relations」四个字
- 落在 29,而标题「OpenAI」落在 35——标题前面是色点加 gap,正好差 6px,
- 看着就是这一排比标题往左漏出去一截。加一档之后字落在 37,压回标题上。
- 这一条只给这里:本体页那条是 `fill` 的整条,与下面正文同宽,
- 它的左缘该跟正文对齐,不跟标题对齐 */}
- <div className="pl-6 pr-4 pt-3">
+ {/* **左缘与下面那一列折叠箭头同起**(16)。它们上下紧挨着,中间没有别的东西,
+ 于是这一段读起来是一条竖线;从前对的是标题里的字(pl-6,让「Relations」
+ 四个字落在 37、压住标题),可标题在另一个盒子里、还隔着一条分隔线,那条
+ 对齐看不见,看得见的是它比下面整整齐齐的一列往右缩了一档。
+ 16 = 滚动区的 px-2 加行自己的 px-2,也就是每一行底色开始的地方;
+ 12 的箭头在 16 的图标格里居中,所以字形还在里面 2px——那 2px 是这个
+ 产品里每一个图标都有的,不是这一处没对齐 */}
+ <div className="pl-4 pr-4 pt-3">
<Segmented
size="sm"
value={view}