Files
AgentLab/frontend/src/canvas/reportStamp.ts
T
ThearchyHelios d071bf119f 可点击证据第三期:表名字段名和引文也点得开、可疑实体与结论句、受管门禁 G1–G5、证据审计导出
一、表结构、实体与引文

- 数据源工具查库时顺带存一份表结构快照(schema_snapshot),和查询快照一起进台账、进封存;
  台账里的查询记下 SQL 用到的表
- 报告里的表名、字段名可以点:反引号里的名字一律核对,正文里的裸名字只在明显是标识符时才自动链接,
  常见英文单词不链接;点开看到它出现在哪次查询、类型、所属的表,快照被截断时写明
- 找不到的名字标成可疑实体:在快照、SQL、结果列里都没有的记「可能是编造的名字」并给出最接近的 3 个;
  快照被截断、核对不了的记「核对不了」,不冤枉成编造
- 检索引文 [[q:K1|原文]]:原文在检索快照的命中片段里逐字出现(空白归一化后)才算有出处,点开高亮原文位置
- 自动链接的名字不算句子的引用,只有写作者写的引用才算

二、结论句与出具

- 报告节点新增 claims:off(默认,保持原样)或 require_citation(没有引用的结论句计入缺口)
- 受管级别的正式运行里,可疑实体和没有引用的结论句按「有缺口」降档,不拦截;探索运行和已发布级别只标注
- 契约的 claims 和报告节点的设置取更严的那个;受管正式运行里写 ignore 也按降档处理,并给出警告
- 出具横幅除了数字,也写出可疑实体和没有引用的结论句

三、受管门禁 G1–G5(只看图和配置)

- G1 带契约的出口,成果字段的文字只能来自报告撰写节点
- G2 模型写的文字不绕过报告撰写节点直接流进出口
- G3 报告撰写节点写明 numbers: strict、on_violation: fail、claims: require_citation
- G4 口径卡的输入不来自计算角色的沙箱代码
- G5 给口径卡供数的 agent 配 output_schema 并开 cite_fields
- 受管级别是错误、挡住发布;已发布级别只给警告;已经发布的版本照常运行,下次发布时按新规则检查
- 每条规则都接进发布前自动修复(一键修复 / 给选项 / 交给 Copilot);禁止的改动补上关 cite_fields、
  删 output_schema、放宽契约 claims;Copilot 兜底不许自己把代码节点标成 source、不许放宽报告节点
- 发布弹窗里选「交给 Copilot」时明确写「正在交给 Copilot」

四、旧运行与审计

- 没有契约的旧答案:数字和已封存的查询单元格、口径卡值按数值比对,每个数最多 3 个候选,
  默认折叠,写明「猜测的来源,不能当证据」
- 有契约的旧运行:候选只从已封存的口径卡里找,人为插进工件表的行不当证据
- 证据审计:GET /api/runs/{id}/evidence/audit 按「无证据 / 可疑实体 / 有出处 / 旧运行猜测」分组,
  可导出 JSON 和 CSV(带封存状态);记录页和画布上的报告节点显示「有出处 N · 无证据 M」

五、SQL 守卫修正(安全)

- 去注释改成单遍扫描,认得字符串和各方言的引号(单引号转义、MySQL 反斜杠与 #、PostgreSQL 的 $$ 与嵌套注释、
  Oracle 的 q'[…]'、SQLite 的方括号);以前字符串里的 -- 会被当成注释,SELECT '--'; DROP … 这类写法能骗过多语句拦截
- 看不到服务器设置时每种读法都扫一遍,读法之间切分不一致就拒绝,任何一种读法是写操作就按写操作处理
- 执行查询和工具危险判定都按数据源的方言检查

六、其他

- 修掉检查脚本里画布检查的一处竞态,并行估时按实测更新
2026-09-29 05:26:34 +08:00

74 lines
3.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect } from 'react'
import { stampCounts } from '../lib/evidence'
import { useEvidence } from '../store/evidence'
import { useStudio } from '../store/studio'
import type { EvidenceStats, RunEvent } from '../types'
/**
* 报告节点卡上的章:「引用 N · 无证据 M」,数据取这张图最近一次运行的 report.checked。
*
* - 画布上挂着一次运行(刚跑的、从记录页回放的):就用它的事件,跟着 report.checked 实时落下;回放时
* 游标还没走到那一刻不画
* - 没挂运行:取这张工作流最近一次运行的证据图(只认封存范围内的 report.checked)
* - 两边都没有这个节点的核对(没跑过、跑到一半、老后端):不画章
*/
export interface ReportStamp {
cited: number
none: number
stats: EvidenceStats
claims?: string | null
/** report.checked 落下的时刻(毫秒,和航迹同一条时间轴);取自证据图的没有 */
at: number | null
runId: string | null
}
interface Checked { stats: EvidenceStats; claims?: string | null; at: number | null; event: RunEvent }
const checkedCache = new WeakMap<RunEvent[], Map<string, Checked>>()
/** 同一条事件给出同一个对象:选择器返回稳定引用,别的事件进来时报告卡不跟着重渲染 */
const byEvent = new WeakMap<RunEvent, Checked>()
/** 这次运行里每个报告节点最后一次核对,按事件数组缓存(每条事件都会把所有卡片的选择器跑一遍) */
function checkedOf(events: RunEvent[]): Map<string, Checked> {
const hit = checkedCache.get(events)
if (hit) return hit
const out = new Map<string, Checked>()
for (const e of events) {
if (e.type !== 'report.checked' || !e.node_id || !e.data?.stats || typeof e.data.stats !== 'object') continue
let c = byEvent.get(e)
if (!c) {
c = { stats: e.data.stats, claims: e.data.claims, at: typeof e.ts === 'number' ? e.ts * 1000 : null, event: e }
byEvent.set(e, c)
}
out.set(e.node_id, c)
}
checkedCache.set(events, out)
return out
}
export function useReportStamp(nodeId: string, enabled: boolean): ReportStamp | null {
const workflowId = useStudio((s) => s.workflow?.id ?? null)
const runId = useStudio((s) => s.run?.id ?? null)
const live = useStudio((s) => (enabled && s.run ? checkedOf(s.events).get(nodeId) ?? null : null))
const latest = useEvidence((s) => (enabled && workflowId ? s.latest[workflowId] : undefined))
const loadLatest = useEvidence((s) => s.loadLatest)
const dropLatest = useEvidence((s) => s.dropLatest)
// 没挂运行时才去取最近一次运行;挂着运行时它就是「最近一次」,之前取的那份随之作废——
// 「清除」把运行摘下来(或者换一张图再回来)时重新取,不拿这次运行之前的旧统计冒充「最近一次」
useEffect(() => {
if (!enabled || !workflowId) return
if (runId) dropLatest(workflowId)
else void loadLatest(workflowId)
}, [enabled, workflowId, runId, loadLatest, dropLatest])
if (!enabled) return null
if (runId) {
if (!live) return null
const counts = stampCounts(live.stats, live.claims)
return counts ? { ...counts, stats: live.stats, claims: live.claims, at: live.at, runId } : null
}
const data = latest?.status === 'ok' ? latest.data : undefined
const report = data?.reports[nodeId]
if (!report?.stats) return null
const counts = stampCounts(report.stats, report.claims)
return counts ? { ...counts, stats: report.stats, claims: report.claims, at: null, runId: data?.runId ?? null } : null
}