mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-25 07:52:25 +00:00
可点击证据第一期:报告里的每个数字都点得开出处
「引用即渲染」:写报告的模型只写引用标记([[m:指标id]]、[[i:输入字段]]、[[see:…]]),数值由系统按口径卡
填进去,每个数字都挂着一条能复算的证据链。本期是数字层闭环;实体、结论句的概率性证据放在后续几期。
内核(engine/evidence.py,口径卡、出具契约、表达式等)
- 口径卡记录每个指标的来源:原式、代入式(每个输入写成「值 ← 路径」)、复算结果、口径与版本
- 出具契约的数值匹配改为按位置标注;同一个值有几个出处时记为「出处不唯一」并列出候选,不再随便挑一个
- 证据 id 由内容派生,事后对不上就是被改过;零值、结构性数字(列表序号、代码块标签)单独处理
- 口径卡新增 on_missing:缺输入时默认仍是整个节点失败,可以改成记为空值、交给契约判档
报告撰写节点(report)与引用模式的出具契约
- 流式生成时 token 里就是真实数字,不会漏出原始标记;裸数字、不存在的引用当场判违规,交回去重写
(max_repairs,默认 1 次);仍不过时按 on_violation 失败(正式运行默认)或把违规标在报告里(探索运行默认)
- report.checked 事件记下核对结果,落在封存范围内;fail 模式下仍不过时带 failed,运行记录里画成失败
- 出具契约写了 report_from 就按引用核对:取回文档时复验哈希,独立重算一遍,全部带引用才判完整出具
- 成果字段逐字等于报告正文时标注 _evidence;复核只能加说明,不能改写带证据的答案(前后端各拦一道)
证据接口
- GET /api/runs/{id}/evidence 返回证据图;GET …/evidence/segments/{id} 返回一个片段的证据链
- 只认封存范围内的事件能追到的工件,不读 artifacts 表;封存被改过时不把任何一项标成已封存
- 没有报告的旧运行降级为 none 或 legacy_contract
界面
- EvidenceDoc 按块、句、片段渲染报告,片段是和正文一样的按钮;整份报告只占一个 Tab 位,
←/→ 在片段间移动、↑/↓ 按句、n/N 跳到下一处「无证据」、Enter 打开面板、Esc 关闭并把焦点还回去
- EvidencePanel:宽屏侧边、窄屏底部抽屉、360px 画布右栏里栏内展开;显示指标步骤、代入式、复算结果、
封存状态。正文不是封存的那份报告时明确说出来,不拿别的报告的封存画绿
- 出具横幅多一行「N/N 数字有出处 · 无证据 M」和「定位下一处」;没有 _evidence 的旧回答渲染一字不差
- 画布认得报告撰写节点;Copilot 手册补上口径卡、报告节点、出具契约的写法;新增模板「⑨ 可追溯周报」
验证(沙箱):tsc 干净,生产构建通过,pytest 1601 passed,check-all 16 项检查全绿(新增 check-evidence)。
This commit is contained in:
@@ -0,0 +1,428 @@
|
||||
/**
|
||||
* 可点击证据:片段状态的外观,和几件证据相关的纯函数。
|
||||
*
|
||||
* 和 lib/status 一个路子,每种状态同时有四条通道:线型、字形、颜色、文字。去掉颜色
|
||||
* 还能靠线型和字形分(灰度打印、色弱),去掉线型还能读字(读屏的 aria-label 就是文字
|
||||
* 那一条)。颜色只用 --st-* 令牌:概率性的「有依据」借 --st-running(强调色),
|
||||
* 「未裁判」借 --st-cancelled(暗字色),「猜测」借 --st-idle(最淡的字色)——
|
||||
* 绝不用确定性的绿,模型的判断不能长得像系统核对过的事实。
|
||||
*
|
||||
* 本期(数字层)实际只会出现两种:确定性(引用解析成功)和无证据(裸数字、引用
|
||||
* 不存在)。其余几种先把外观定下,后续期接上时不用再各处补。
|
||||
*/
|
||||
|
||||
import { EVIDENCE_STATE_LABEL, EVIDENCE_TEXT, evidenceTally } from './terms'
|
||||
import type {
|
||||
EvidenceBlock, EvidenceDocData, EvidenceFieldRef, EvidenceGraph, EvidenceSeal, EvidenceSegment,
|
||||
EvidenceSegmentDetail, EvidenceStep, EvidenceUnit, EvidenceViolation, ReviewResult,
|
||||
} from '../types'
|
||||
import type { MarkSpec } from '../run/Markdown'
|
||||
|
||||
export type EvidenceStateCode = keyof typeof EVIDENCE_STATE_LABEL
|
||||
|
||||
/**
|
||||
* 正文里的线型。solid / dotted 画在片段下面;badge 是句末的小徽标——结论句的状态
|
||||
* 不画整句下划线,否则一份报告满屏都是线;none 不装饰
|
||||
*/
|
||||
export type EvidenceLine = 'solid' | 'dotted' | 'badge' | 'none'
|
||||
|
||||
export interface EvidenceStateMeta {
|
||||
code: EvidenceStateCode
|
||||
/** 文字:aria-label、面板标题、图例 */
|
||||
label: string
|
||||
line: EvidenceLine
|
||||
/** 字形。'' 表示不画——确定性的数字就是正文,不另加记号 */
|
||||
glyph: string
|
||||
/** 前景色(字形、面板标题),只用 --st-* */
|
||||
color: string
|
||||
/** 下划线颜色 */
|
||||
decoration: string
|
||||
/** 淡底:悬停、打开面板时那一段的底色 */
|
||||
soft: string
|
||||
/** 异常态:正常态安静,只有它们该醒目、该进 n / N 的跳转 */
|
||||
alert: boolean
|
||||
/** 从哪一期开始会出现 */
|
||||
phase: 1 | 3 | 4
|
||||
/** 一句话说明,放 title 和面板 */
|
||||
hint: string
|
||||
}
|
||||
|
||||
const mix = (token: string, pct: number) => `color-mix(in srgb, var(${token}) ${pct}%, transparent)`
|
||||
|
||||
export const EVIDENCE_STATE: Record<EvidenceStateCode, EvidenceStateMeta> = {
|
||||
deterministic: {
|
||||
code: 'deterministic', label: EVIDENCE_STATE_LABEL.deterministic, line: 'solid', glyph: '',
|
||||
color: 'var(--st-done)', decoration: mix('--st-done', 60), soft: 'var(--st-done-soft)',
|
||||
alert: false, phase: 1,
|
||||
hint: '数字由系统从口径卡取值、按口径卡的格式渲染,不是模型写的',
|
||||
},
|
||||
supported: {
|
||||
code: 'supported', label: EVIDENCE_STATE_LABEL.supported, line: 'badge', glyph: '◆',
|
||||
color: 'var(--st-running)', decoration: 'transparent', soft: 'var(--st-running-soft)',
|
||||
alert: false, phase: 4,
|
||||
hint: '裁判模型认为引用的证据支持这句话。这是模型的判断,不是系统核对',
|
||||
},
|
||||
partial: {
|
||||
code: 'partial', label: EVIDENCE_STATE_LABEL.partial, line: 'badge', glyph: '◇',
|
||||
color: 'var(--st-waiting)', decoration: 'transparent', soft: 'var(--st-waiting-soft)',
|
||||
alert: true, phase: 4,
|
||||
hint: '裁判模型认为证据只支持这句话的一部分',
|
||||
},
|
||||
unsupported: {
|
||||
code: 'unsupported', label: EVIDENCE_STATE_LABEL.unsupported, line: 'badge', glyph: '!',
|
||||
color: 'var(--st-failed)', decoration: 'transparent', soft: 'var(--st-failed-soft)',
|
||||
alert: true, phase: 4,
|
||||
hint: '裁判模型认为引用的证据不支持这句话',
|
||||
},
|
||||
unjudged: {
|
||||
code: 'unjudged', label: EVIDENCE_STATE_LABEL.unjudged, line: 'badge', glyph: '?',
|
||||
color: 'var(--st-cancelled)', decoration: 'transparent', soft: 'var(--st-cancelled-soft)',
|
||||
alert: false, phase: 4,
|
||||
hint: '这句话带了引用,还没请模型判断引用支不支持它',
|
||||
},
|
||||
none: {
|
||||
code: 'none', label: EVIDENCE_STATE_LABEL.none, line: 'dotted', glyph: '?',
|
||||
color: 'var(--st-waiting)', decoration: 'var(--st-waiting)', soft: 'var(--st-waiting-soft)',
|
||||
alert: true, phase: 1,
|
||||
hint: '没写成引用标记的数字,或者引用解析不了:系统核对不到它从哪来',
|
||||
},
|
||||
connective: {
|
||||
code: 'connective', label: EVIDENCE_STATE_LABEL.connective, line: 'none', glyph: '',
|
||||
color: 'var(--st-idle)', decoration: 'transparent', soft: 'var(--st-idle-soft)',
|
||||
alert: false, phase: 4,
|
||||
hint: '过渡、组织结构的话,不陈述数据事实,所以不需要证据',
|
||||
},
|
||||
candidate: {
|
||||
code: 'candidate', label: EVIDENCE_STATE_LABEL.candidate, line: 'dotted', glyph: '~',
|
||||
color: 'var(--st-idle)', decoration: mix('--st-idle', 70), soft: 'var(--st-idle-soft)',
|
||||
alert: false, phase: 3,
|
||||
hint: '旧运行按数值猜的可能来源,不能当证据',
|
||||
},
|
||||
}
|
||||
|
||||
/** 图例、检查脚本按这个顺序列 */
|
||||
export const EVIDENCE_STATES = Object.keys(EVIDENCE_STATE) as EvidenceStateCode[]
|
||||
|
||||
/**
|
||||
* 片段在正文里的状态。文字、结构片段(行首符号、表格竖线)是 null:不画线、不进键盘顺序。
|
||||
* 后端的 probabilistic 在裁判给出结论之前按「未裁判」画
|
||||
*/
|
||||
export function segmentState(seg: Pick<EvidenceSegment, 'kind' | 'state'>): EvidenceStateCode | null {
|
||||
if (seg.kind === 'structural') return null
|
||||
switch (seg.state) {
|
||||
case 'deterministic': return 'deterministic'
|
||||
case 'none': return 'none'
|
||||
case 'probabilistic': return 'unjudged'
|
||||
case 'candidate': return 'candidate'
|
||||
default: return null
|
||||
}
|
||||
}
|
||||
|
||||
/** 一个句子(单元格、列表项)的正文:结构片段不算 */
|
||||
export function unitText(unit: Pick<EvidenceUnit, 'segments'>): string {
|
||||
return (unit.segments ?? []).filter((s) => s.kind !== 'structural').map((s) => s.text).join('')
|
||||
}
|
||||
|
||||
/** 片段的来源说一句:「口径卡指标 环比增幅」「运行输入 week」 */
|
||||
export function sourceOf(seg: EvidenceSegment, doc?: Pick<EvidenceDocData, 'catalog'> | null): string {
|
||||
const cite = seg.cite
|
||||
if (!cite) return ''
|
||||
const entry = cite.alias ? doc?.catalog?.[cite.alias] : undefined
|
||||
if (cite.kind === 'metric' || entry?.kind === 'metric') {
|
||||
const name = entry?.name ?? cite.locator?.metric ?? cite.ref ?? ''
|
||||
return name ? `口径卡指标 ${name}` : '口径卡指标'
|
||||
}
|
||||
if (cite.kind === 'input' || entry?.kind === 'input') {
|
||||
return `运行输入 ${entry?.locator?.field ?? cite.locator?.field ?? cite.ref ?? ''}`.trim()
|
||||
}
|
||||
return cite.alias ?? cite.ref ?? ''
|
||||
}
|
||||
|
||||
/** 无证据的原因:裸数字,还是引用解析不了(带后端给的人话原因) */
|
||||
export function reasonOf(seg: EvidenceSegment): string {
|
||||
if (seg.issue === 'uncited_number' || (!seg.cite && seg.kind === 'number')) return EVIDENCE_TEXT.uncited
|
||||
return seg.cite?.reason ?? (seg.ref ? `引用 ${seg.ref} 解析不了` : '')
|
||||
}
|
||||
|
||||
/**
|
||||
* 片段的 aria-label:字、状态、出处都写全。「8.7%,有出处:口径卡指标 环比增幅」
|
||||
* 「12,无证据:这个数字没有写成引用标记…」。读屏用户靠它,不靠颜色和线型
|
||||
*/
|
||||
export function segmentLabel(seg: EvidenceSegment, doc?: Pick<EvidenceDocData, 'catalog'> | null): string {
|
||||
const state = segmentState(seg)
|
||||
if (!state) return seg.text
|
||||
const why = state === 'none' ? reasonOf(seg) : sourceOf(seg, doc)
|
||||
return `${seg.text},${EVIDENCE_STATE[state].label}${why ? `:${why}` : ''}`
|
||||
}
|
||||
|
||||
/** 在正文里画得出线的违规:指向的片段存在,而且不是结构片段 */
|
||||
export function locatable(doc: EvidenceDocData, v: EvidenceViolation): EvidenceSegment | null {
|
||||
if (!v.segment) return null
|
||||
for (const block of doc.blocks ?? []) {
|
||||
for (const unit of block.units ?? []) {
|
||||
for (const seg of unit.segments ?? []) {
|
||||
if (seg.id === v.segment) return seg.kind === 'structural' ? null : seg
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export interface EvidenceTally {
|
||||
/** 数字总数:带引用的数字片段 + 裸数字(含列表序号、代码块标签里的) */
|
||||
total: number
|
||||
cited: number
|
||||
/** 没有出处的数字:总数 − 有出处(裸数字、解析不了的数字引用)。横幅上「无证据 M」的 M */
|
||||
none: number
|
||||
/** 不是数字、解析不了的引用:[[v:]] 值、句末 [[see:]] 依据。另起一句说,不混进 none */
|
||||
other: number
|
||||
/** 画不了线、只能在清单里找的:structural + noSegment */
|
||||
hidden: number
|
||||
/** 其中在结构片段里的(列表序号、代码块标签里的数字):违规指着片段,片段是 Markdown 语法 */
|
||||
structural: number
|
||||
/** 其中在正文里没有对应字的(句末依据里写错的引用):违规不指着任何片段 */
|
||||
noSegment: number
|
||||
}
|
||||
|
||||
/** 缺陷类违规:完整性问题(render_mismatch 这些)不在这里数,它们说的是文档本身靠不住 */
|
||||
const GAP_CODES = new Set(['uncited_number', 'unresolved_ref'])
|
||||
|
||||
const num = (v: unknown): number | undefined => (typeof v === 'number' && Number.isFinite(v) ? v : undefined)
|
||||
|
||||
/**
|
||||
* 文档的计数。stats 是后端 verify_doc 算的,有就用它;老文档、别的版本缺字段时
|
||||
* 从片段自己数,数出来的和 stats 是同一个口径。
|
||||
*
|
||||
* 数字那半句要算得平:none = total − cited。不是数字的解析不了的引用(other)按违规
|
||||
* 清单逐条认(指着的片段不是数字,或者根本不指片段);没有清单时用 stats 反推
|
||||
* (uncited + unresolved − none),再没有就从片段的状态数
|
||||
*/
|
||||
export function docTally(doc: EvidenceDocData): EvidenceTally {
|
||||
const segs = segmentsOf(doc.blocks ?? [])
|
||||
const byId = new Map(segs.map((s) => [s.id, s]))
|
||||
const all = doc.violations ?? []
|
||||
const gaps = all.filter((v) => GAP_CODES.has(v.code))
|
||||
const hidden = gaps.filter((v) => !locatable(doc, v))
|
||||
const structural = hidden.filter((v) => !!v.segment).length
|
||||
const st = doc.stats ?? {}
|
||||
const cited = num(st.numbers_cited) ?? segs.filter((s) => s.kind === 'number' && s.state === 'deterministic').length
|
||||
const total = num(st.numbers)
|
||||
?? segs.filter((s) => s.kind === 'number').length + hidden.filter((v) => v.code === 'uncited_number').length
|
||||
const none = Math.max(0, total - cited)
|
||||
const uncited = num(st.uncited_numbers)
|
||||
const unresolved = num(st.unresolved)
|
||||
const other = all.length
|
||||
? gaps.filter((v) => v.code === 'unresolved_ref' && byId.get(v.segment ?? '')?.kind !== 'number').length
|
||||
: uncited != null && unresolved != null
|
||||
? Math.max(0, uncited + unresolved - none)
|
||||
: segs.filter((s) => segmentState(s) === 'none' && s.kind !== 'number').length
|
||||
return { total, cited, none, other, hidden: hidden.length, structural, noSegment: hidden.length - structural }
|
||||
}
|
||||
|
||||
/**
|
||||
* 只有 stats 的时候(report.checked 事件的载荷,没有文档):同一种算法。other 用
|
||||
* uncited + unresolved − none 反推——数字引用渲染对不上(render_mismatch,文档本身
|
||||
* 靠不住的那类)时会少数几处,这类文档另有完整性违规兜着
|
||||
*/
|
||||
export function statsTally(stats: EvidenceDocData['stats'] | null | undefined):
|
||||
Pick<EvidenceTally, 'total' | 'cited' | 'none' | 'other'> | null {
|
||||
const total = num(stats?.numbers)
|
||||
const cited = num(stats?.numbers_cited)
|
||||
if (total == null || cited == null) return null
|
||||
const none = Math.max(0, total - cited)
|
||||
const other = Math.max(0, (num(stats?.uncited_numbers) ?? 0) + (num(stats?.unresolved) ?? 0) - none)
|
||||
return { total, cited, none, other }
|
||||
}
|
||||
|
||||
/** 读屏摘要:计数、画不了线的两种各在哪、按键说明 */
|
||||
export function tallySummary(t: EvidenceTally, keys: boolean): string {
|
||||
return [
|
||||
evidenceTally(t.cited, t.total, t.other),
|
||||
t.structural ? EVIDENCE_TEXT.structuralCount(t.structural) : '',
|
||||
t.noSegment ? EVIDENCE_TEXT.noSegmentCount(t.noSegment) : '',
|
||||
keys ? EVIDENCE_TEXT.keysHint : '',
|
||||
].filter(Boolean).join('。')
|
||||
}
|
||||
|
||||
/**
|
||||
* 证据接口答的是不是正文这份报告。
|
||||
*
|
||||
* 正文是按成果上 _evidence.doc_artifact 取的——成果是可以改写的一列,工件表可以事后插行,
|
||||
* 都不可信;片段接口只从封存范围内的 report.checked 找文档。两边不是同一份时,接口给的
|
||||
* 封存状态和证据链说的是另一份报告,挂到正文上就成了「已封存 · 核对一致」地证明一段
|
||||
* 别的字。对得上的条件:接口报的文档工件 id、报告节点、片段 id、这个位置的字都和正文一致
|
||||
* (接口没给的那一项不比)。
|
||||
*/
|
||||
export function docForeign(
|
||||
detail: EvidenceSegmentDetail | undefined,
|
||||
mine: { artifact?: string; node?: string; seg: Pick<EvidenceSegment, 'id' | 'text'> },
|
||||
): { sealedText?: string } | null {
|
||||
if (!detail) return null
|
||||
const theirs = detail.report?.doc_artifact
|
||||
const node = detail.report?.node_id
|
||||
const text = detail.segment?.text
|
||||
const id = detail.segment?.id
|
||||
const differs = (a: unknown, b: unknown) => typeof a === 'string' && !!a && typeof b === 'string' && !!b && a !== b
|
||||
const textOff = typeof text === 'string' && text !== mine.seg.text
|
||||
if (differs(theirs, mine.artifact) || differs(node, mine.node) || differs(id, mine.seg.id) || textOff) {
|
||||
return textOff ? { sealedText: text } : {}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 证据图(片段接口取不到时的兜底)里有没有正文这份报告。有 reports 列表、却找不到这个
|
||||
* 工件:正文不是封存的那一份。找到了但哈希对不上:文档被改过。不知道正文的工件 id、
|
||||
* 证据图没有列表时不下结论
|
||||
*/
|
||||
export function graphDoc(
|
||||
graph: EvidenceGraph | undefined, artifact: string | undefined,
|
||||
): 'ok' | 'foreign' | 'tampered' | null {
|
||||
if (!graph || !artifact || !Array.isArray(graph.reports)) return null
|
||||
const hit = graph.reports.find((r) => r?.doc_artifact === artifact)
|
||||
if (!hit) return graph.reports.length ? 'foreign' : null
|
||||
return hit.hash_ok === false ? 'tampered' : 'ok'
|
||||
}
|
||||
|
||||
export type SealStatus = 'idle' | 'waiting' | 'failed' | 'done'
|
||||
|
||||
/**
|
||||
* 封存那一行说什么。顺序有讲究:后端的 covered = 封存核对通过 && 每步都在台账里,
|
||||
* 所以核对失败(sealed 且 ok=false)的时候 covered 一定也是 false——先看 covered 的话,
|
||||
* 被改过的封存永远显示成琥珀色的「不在封存范围内」,红色的「核对不一致」一次都画不出来。
|
||||
*
|
||||
* - foreign:正文不是封存的那份报告,封存链再完好也证明不了屏幕上的字 → 失败
|
||||
* - 没拿到封存状态 → 灰(正在取 / 没有运行 / 拿不到)
|
||||
* - 没封存 → 灰「尚未封存」
|
||||
* - 封存核对没通过 → 红「核对不一致」
|
||||
* - 这件证据不在封存范围内 → 琥珀
|
||||
* - 都过了 → 绿;没有证据的片段落到文档上(docOnly),链没取到的落到「只核对了文档」(viaGraph)
|
||||
*/
|
||||
export function sealVerdict(seal: EvidenceSeal | undefined, opts: {
|
||||
foreign?: boolean; pending?: boolean; noRun?: boolean; docOnly?: boolean; viaGraph?: boolean
|
||||
} = {}): { status: SealStatus; label: string } {
|
||||
if (opts.foreign) return { status: 'failed', label: EVIDENCE_TEXT.sealForeign }
|
||||
if (!seal) {
|
||||
return { status: 'idle', label: opts.pending ? EVIDENCE_TEXT.chainPending : opts.noRun ? EVIDENCE_TEXT.noRun
|
||||
: EVIDENCE_TEXT.sealUnknown }
|
||||
}
|
||||
if (!seal.sealed) return { status: 'idle', label: EVIDENCE_TEXT.sealOpen }
|
||||
if (seal.ok === false) return { status: 'failed', label: EVIDENCE_TEXT.sealBad }
|
||||
if (seal.covered === false) return { status: 'waiting', label: EVIDENCE_TEXT.sealOutside }
|
||||
return { status: 'done', label: opts.docOnly ? EVIDENCE_TEXT.sealDocOk
|
||||
: opts.viaGraph ? EVIDENCE_TEXT.sealDocChain : EVIDENCE_TEXT.sealOk }
|
||||
}
|
||||
|
||||
/**
|
||||
* 证据链一步的逐项复核里,哪几项明确没过。「在不在封存范围内」只在封存完好时单独说:
|
||||
* 没封存、封存核对失败时后端把每一步的 sealed 都记成 false,那是封存那一行已经说清的
|
||||
* 状态,再报一条「事后补进来的」就是冤枉它
|
||||
*/
|
||||
export function integrityFailures(
|
||||
step: Pick<EvidenceStep, 'eid_ok' | 'hash_ok' | 'render_ok' | 'sealed'> | undefined,
|
||||
seal: EvidenceSeal | undefined,
|
||||
): ('eid' | 'hash' | 'render' | 'sealed')[] {
|
||||
if (!step) return []
|
||||
const sealTrusted = !!seal?.sealed && seal.ok !== false
|
||||
return ([
|
||||
['eid', step.eid_ok], ['hash', step.hash_ok], ['render', step.render_ok],
|
||||
...(sealTrusted ? [['sealed', step.sealed] as const] : []),
|
||||
] as const).filter(([, ok]) => ok === false).map(([k]) => k)
|
||||
}
|
||||
|
||||
/**
|
||||
* 成果上的证据标注:字段名 → 它逐字等于的那份报告。最外层是契约 report_from 指着的那份,
|
||||
* others 里是别的报告。缺了取文档要的 doc_artifact、没列字段的条目当没有
|
||||
*/
|
||||
export function evidenceFields(
|
||||
output: Record<string, any> | null | undefined,
|
||||
): Map<string, { report?: string; artifact: string }> {
|
||||
const out = new Map<string, { report?: string; artifact: string }>()
|
||||
const ev = output?._evidence
|
||||
if (!ev || typeof ev !== 'object') return out
|
||||
for (const r of [ev, ...(Array.isArray(ev.others) ? ev.others : [])] as EvidenceFieldRef[]) {
|
||||
if (!r || typeof r.doc_artifact !== 'string' || !r.doc_artifact || !Array.isArray(r.fields)) continue
|
||||
for (const f of r.fields) {
|
||||
if (!out.has(String(f))) out.set(String(f), { report: r.report_node, artifact: r.doc_artifact })
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
/** 成果带不带逐段证据 */
|
||||
export const hasEvidence = (output: Record<string, any> | null | undefined): boolean =>
|
||||
evidenceFields(output).size > 0
|
||||
|
||||
/**
|
||||
* 成果带逐段证据时,复核只能加说明、不能改写。改写会把整个成果换成 {answer: 改写后的
|
||||
* 文本},报告文档的每个片段就都对不上了——而证据正是这个答案最值钱的部分。后端
|
||||
* review.py 也拦,这里再拦一道:老后端、别的入口回来的改写都不能落到界面上
|
||||
*/
|
||||
export function guardReview(
|
||||
result: ReviewResult | null, output: Record<string, any> | null | undefined,
|
||||
): ReviewResult | null {
|
||||
if (!result?.answer || !hasEvidence(output)) return result
|
||||
return { ...result, answer: null, original: null, verdict: result.verdict === 'rewritten' ? 'annotated' : result.verdict }
|
||||
}
|
||||
|
||||
/**
|
||||
* 旧契约(按数值回指)里一条 matched 的出处。出处不唯一的(metric 为 null、带
|
||||
* candidates)照实写「出处不唯一:候选 a、b」,不能再写成「来自口径卡指标「」」
|
||||
*/
|
||||
export function matchedSource(m: any): { text: string; ambiguous: boolean } {
|
||||
const candidates: string[] = Array.isArray(m?.candidates) ? m.candidates.map(String).filter(Boolean) : []
|
||||
if (m?.ambiguous || (m?.metric == null && candidates.length)) {
|
||||
return { text: EVIDENCE_TEXT.ambiguous(candidates.length ? candidates : ['—']), ambiguous: true }
|
||||
}
|
||||
return { text: String(m?.metric ?? '').trim(), ambiguous: false }
|
||||
}
|
||||
|
||||
/**
|
||||
* 旧契约(按数值回指)在正文上画的标记:回指不上的、回指上的、出处不唯一的。
|
||||
* 带 start / end 的只标那一处(Markdown 按位置核对,对不上再退回按字符串标)
|
||||
*/
|
||||
export function issuanceMarks(issuance: any): MarkSpec[] | undefined {
|
||||
const unmatched: any[] = Array.isArray(issuance?.unmatched_numbers) ? issuance.unmatched_numbers : []
|
||||
const matched: any[] = Array.isArray(issuance?.matched) ? issuance.matched : []
|
||||
const where = (x: any) => (typeof x?.start === 'number' && typeof x?.end === 'number'
|
||||
? { start: x.start, end: x.end } : {})
|
||||
const marks: MarkSpec[] = [
|
||||
...unmatched.map((u) => ({
|
||||
token: String(u?.token ?? u),
|
||||
tone: 'warn' as const,
|
||||
title: `这个数字在口径卡里找不到来源${u?.context ? `:「${u.context}」` : ''}`,
|
||||
...where(u),
|
||||
})),
|
||||
...matched.map((m) => {
|
||||
const src = matchedSource(m)
|
||||
return {
|
||||
token: String(m?.token ?? ''),
|
||||
tone: src.ambiguous ? 'ambiguous' as const : 'ok' as const,
|
||||
title: src.ambiguous ? src.text : `来自口径卡指标「${src.text}」${m?.caliber ? ` · ${m.caliber}` : ''}`,
|
||||
...where(m),
|
||||
}
|
||||
}),
|
||||
].filter((m) => m.token)
|
||||
return marks.length ? marks : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* 码点偏移 → UTF-16 下标。后端的偏移是 Python 的 str 下标(按码点),JS 的字符串下标
|
||||
* 按 UTF-16:遇到 BMP 之外的字符(emoji)两边就差一位。没有这类字符时原样返回
|
||||
*/
|
||||
export function codePointIndex(text: string): (cp: number) => number {
|
||||
if (!/[\uD800-\uDBFF]/.test(text)) return (cp) => cp
|
||||
const at: number[] = []
|
||||
let i = 0
|
||||
for (const ch of text) {
|
||||
at.push(i)
|
||||
i += ch.length
|
||||
}
|
||||
at.push(i)
|
||||
return (cp) => (cp >= 0 && cp < at.length ? at[cp] : -1)
|
||||
}
|
||||
|
||||
/** 文档里的全部片段,按正文顺序 */
|
||||
export function segmentsOf(blocks: EvidenceBlock[]): EvidenceSegment[] {
|
||||
return blocks.flatMap((b) => (b.units ?? []).flatMap((u) => u.segments ?? []))
|
||||
}
|
||||
@@ -18,6 +18,7 @@
|
||||
*/
|
||||
|
||||
import type { NodeType } from '../types'
|
||||
import { formatNumber } from './format'
|
||||
|
||||
export const TERMS = {
|
||||
workflow: '工作流',
|
||||
@@ -108,6 +109,7 @@ export const NODE_TYPE_LABEL: Record<NodeType, string> = {
|
||||
human: '人工审批',
|
||||
validate: '结构校验',
|
||||
metrics: '口径卡',
|
||||
report: '报告撰写',
|
||||
}
|
||||
|
||||
/** 与 decode.ts 里旧名字同名的出口,第二波改成从这里导入时不用改调用处 */
|
||||
@@ -125,3 +127,107 @@ export const APPROVAL_POLICY_LABEL: Record<'dangerous' | 'always' | 'never', str
|
||||
always: '每次调用都审批',
|
||||
never: '全部自动放行',
|
||||
}
|
||||
|
||||
/**
|
||||
* 可点击证据:片段状态的叫法。正文里片段的 aria-label、面板标题、图例都用这一份。
|
||||
* 外观(线型、字形、颜色)在 lib/evidence.ts,和这里一一对应。
|
||||
*
|
||||
* 本期(数字层)实际只会出现「有出处」和「无证据」;其余几种是后续期的,先把叫法定下,
|
||||
* 免得到时候各处各起一个名字。
|
||||
*/
|
||||
export const EVIDENCE_STATE_LABEL = {
|
||||
deterministic: '有出处',
|
||||
supported: '模型判断:有依据',
|
||||
partial: '模型判断:部分有依据',
|
||||
unsupported: '模型判断:证据不支持',
|
||||
unjudged: '有引用,未裁判',
|
||||
none: '无证据',
|
||||
connective: '连接性文字',
|
||||
candidate: '猜测的来源',
|
||||
} as const
|
||||
|
||||
/**
|
||||
* 「7/12 数字有出处 · 无证据 5 · 另有 1 处引用解析不了」:出具横幅、报告核对那一行、证据条、
|
||||
* 读屏摘要共用一种说法。
|
||||
*
|
||||
* 前半句只数数字,要让人一眼算得平:无证据 = 总数 − 有出处(12 − 7 = 5)。不是数字的
|
||||
* 引用([[v:]] 值、句末 [[see:]] 依据)解析不了的另起一句——以前把它们也算进「无证据」,
|
||||
* 12 − 7 却写成 6,读的人以为算错了。一个数字都没有、也没有别的问题时返回空串,调用方
|
||||
* 自己决定说什么
|
||||
*/
|
||||
export function evidenceTally(cited: number, total: number, other = 0): string {
|
||||
const gaps = Math.max(0, total - cited)
|
||||
const parts: string[] = []
|
||||
if (total > 0) {
|
||||
parts.push(gaps
|
||||
? `${formatNumber(cited)}/${formatNumber(total)} 数字有出处 · 无证据 ${formatNumber(gaps)}`
|
||||
: `${formatNumber(total)} 个数字都有出处`)
|
||||
}
|
||||
if (other > 0) parts.push(`${total > 0 ? '另有 ' : ''}${formatNumber(other)} 处引用解析不了`)
|
||||
return parts.join(' · ')
|
||||
}
|
||||
|
||||
/** 证据面板和证据条的文案 */
|
||||
export const EVIDENCE_TEXT = {
|
||||
allCited: (n: number) => `${formatNumber(n)} 个数字都有出处`,
|
||||
locateNext: '定位下一处',
|
||||
/** 读屏摘要末尾的操作说明 */
|
||||
keysHint: '用左右方向键逐个查看,上下键按句子走,n 跳到下一处无证据,回车打开证据,Esc 关闭',
|
||||
panelTitle: '证据',
|
||||
close: '关闭证据',
|
||||
back: '回到正文',
|
||||
sentence: '所在的句子',
|
||||
metric: '口径卡指标',
|
||||
expression: '原式',
|
||||
substituted: '代入式',
|
||||
inputs: '输入',
|
||||
recompute: '复算',
|
||||
recomputeOk: '复算一致',
|
||||
recomputeBad: '复算不一致:代入式算出来和记录的值对不上',
|
||||
recomputeNone: '没法复算',
|
||||
input: '运行输入',
|
||||
seal: '封存',
|
||||
sealOk: '已封存 · 核对一致',
|
||||
sealDocOk: '报告文档已封存 · 核对一致(这个数字本身没有证据可封存)',
|
||||
/** 片段接口取不到、封存状态是从整次运行的证据图查的:只核对了文档在封存范围内,这个数的链没逐项核对 */
|
||||
sealDocChain: '报告文档已封存 · 核对一致(这个数字的证据链这次没取到,没有逐项核对)',
|
||||
sealBad: '已封存 · 核对不一致',
|
||||
sealOpen: '尚未封存',
|
||||
sealOutside: '不在封存范围内',
|
||||
/** 正文这份报告不是封存的那一份:封存链再完好,也证明不了屏幕上这些字 */
|
||||
sealForeign: '正文不是封存的那份报告,封存证明不了这里的字',
|
||||
sealUnknown: '封存状态拿不到',
|
||||
violations: '违规清单',
|
||||
violationsHint: '报告撰写节点核对出来的全部问题。列表序号、代码块标签里的数字在正文里画不了线,只能在这里看',
|
||||
structural: '这个数字在列表序号、代码块标签这类 Markdown 语法里,正文里画不了线',
|
||||
noSegment: '这一条在正文里没有对应的字,比如句末依据里写错的引用',
|
||||
/** 读屏摘要:画不了线的两种,分开说在哪(和违规清单里每条的 structural / noSegment 同一个意思) */
|
||||
structuralCount: (n: number) => `其中 ${formatNumber(n)} 个数字在列表序号、代码块标签这类 Markdown 语法里,正文里画不了线,只在违规清单里列出`,
|
||||
noSegmentCount: (n: number) => `${formatNumber(n)} 处在正文里没有对应的字(比如句末依据里写错的引用),只在违规清单里列出`,
|
||||
/** 出具横幅:回指不上的数字。有报告文档时点状线只画得出一部分(列表序号里的画不了),不能说「已用虚线标出」 */
|
||||
unmatchedInDoc: '无法回指的数字(逐条见报告的违规清单):',
|
||||
unmatchedPlain: '无法回指的数字:',
|
||||
uncited: '这个数字没有写成引用标记,系统核对不到它从哪来',
|
||||
missingValue: '缺输入:口径卡这次没有算出这个指标的值',
|
||||
unshowable: (value: string) => `有值(${value}),但按口径卡的格式显示不出来`,
|
||||
chainPending: '正在取算式和输入…',
|
||||
/** 证据接口逐项复核出来的问题。正常时不说,出问题才醒目 */
|
||||
integrity: {
|
||||
eid: '证据标识对不上:目录里记的和按工件重算的不一样',
|
||||
hash: '口径卡工件取回时哈希对不上,或者卡里找不到这个指标',
|
||||
render: '按口径卡重新渲染出来的字和报告上的不一样',
|
||||
sealed: '这件证据不在封存范围内:事后补进来的,不能当证据',
|
||||
/** 证据接口答的是封存范围内那份报告,和正文这份不是同一份(工件 id 或这个位置的字对不上) */
|
||||
doc: '正文这份报告不是封存范围内的那一份:下面的出处是正文自己记的,没有经过封存核对',
|
||||
/** 证据图里正文这份报告的哈希对不上 */
|
||||
docHash: '正文这份报告和它的哈希对不上,疑似被改过',
|
||||
sealedText: (text: string) => `封存的那一份在这个位置写的是「${text}」`,
|
||||
},
|
||||
chainMissing: '算式、代入式和输入要从证据接口取,这一次没取到',
|
||||
chainForeign: '证据接口给的是封存的那份报告的算式,和正文这份对不上,不在这里展示',
|
||||
noRun: '这份报告不属于某次运行,只能看文档里记下的出处',
|
||||
docMissing: '证据文档取不到,按普通文本显示',
|
||||
docMismatch: '成果字段和报告文档对不上,按普通文本显示',
|
||||
docOtherRun: '证据文档不是这次运行写的,按普通文本显示',
|
||||
ambiguous: (candidates: string[]) => `出处不唯一:候选 ${candidates.join('、')}`,
|
||||
} as const
|
||||
|
||||
Reference in New Issue
Block a user