dsh-data-domain-knowledge 0.1.1-alpha.7 → 0.1.1-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import { buildTrialSystem } from '../extract-contract.js'
5
5
  import { estimateTokens, requestBudget as modelRequestBudget } from './budget.js'
6
6
  import { parseExtractionReply } from '../extraction-reply.js'
7
7
  import { runWikiTrial, headingPathAt, locateQuote, type AxisPromptBlock } from '../wiki-trial.js'
8
+ import { auditCoverage, COVERAGE_SUPPLEMENT_NOTE, type CoverageAudit } from './coverage.js'
8
9
  import type { TrialReport, TrialTokenUsage } from '../wiki-trial-types.js'
9
10
  import type { PipelineStore } from './store.js'
10
11
  import { EXTRACTION_RECOVERY_LIMITS, type PipelineActivity, type PipelineActivityUpdate } from './types.js'
@@ -173,6 +174,46 @@ export function formatUsage(usage: TrialTokenUsage | undefined): string {
173
174
  return `(${parts.join(' · ')})`
174
175
  }
175
176
 
177
+ /**
178
+ * 把补抽结果并进已经合并的负载。
179
+ *
180
+ * 补抽是**同一篇文档的第二次观察**,所以三件事必须做:
181
+ * 知识按正文去重(同一句话补两遍只留一条);Event 按 external_id + 标题去重;
182
+ * 坐标 mention 去重。ID 加前缀,避免两次回复里的本地 id 撞车。
183
+ */
184
+ export function mergeCoverageSupplement(base: Row, extra: Row, prefix: string): void {
185
+ const knowledge: Row[] = base.knowledge ??= []
186
+ const seenKnowledge = new Set(knowledge.map(row => String(row.text ?? '').trim()))
187
+ for (const row of extra.knowledge ?? []) {
188
+ const key = String(row.text ?? '').trim()
189
+ if (key === '' || seenKnowledge.has(key)) continue
190
+ seenKnowledge.add(key)
191
+ knowledge.push({ ...row, id: `${prefix}_${String(row.id ?? knowledge.length + 1)}` })
192
+ }
193
+ const events: Row[] = base.events ??= []
194
+ const seenEvents = new Set(events.map(row => `${String(row.external_id ?? '')}\u0000${String(row.title ?? '')}`))
195
+ for (const row of extra.events ?? []) {
196
+ const key = `${String(row.external_id ?? '')}\u0000${String(row.title ?? '')}`
197
+ if (seenEvents.has(key)) continue
198
+ seenEvents.add(key)
199
+ const timeline = Array.isArray(row.timeline)
200
+ ? row.timeline.map((step: Row, index: number) => ({ ...step, id: `${prefix}_${String(step.id ?? index + 1)}` }))
201
+ : row.timeline
202
+ events.push({ ...row, ...(Array.isArray(row.timeline) ? { timeline } : {}), id: `${prefix}_${String(row.id ?? events.length + 1)}` })
203
+ }
204
+ for (const key of ['objects', 'actions', 'scenes'] as const) {
205
+ const target: Row[] = base[key] ??= []
206
+ const seen = new Set(target.map(row => String(row.mention ?? '').trim().toLowerCase()))
207
+ for (const row of extra[key] ?? []) {
208
+ const mention = String(row.mention ?? '').trim().toLowerCase()
209
+ if (mention === '' || seen.has(mention)) continue
210
+ seen.add(mention)
211
+ target.push(row)
212
+ }
213
+ }
214
+ for (const row of extra.dropped ?? []) (base.dropped ??= []).push(row)
215
+ }
216
+
176
217
  /** Merge already validated records; IDs are local to each response, quotes still point into the full source. */
177
218
  export function mergeExtractionPayloads(values: Row[], chunked: boolean): Row {
178
219
  const merged: Row = { subject: values[0]?.subject ?? '', knowledge: [], events: [], objects: [], actions: [], scenes: [], dropped: [] }
@@ -395,8 +436,100 @@ export async function runResumableExtraction(input: ResumableExtractionInput): P
395
436
  })
396
437
  await input.checkpoint()
397
438
  progress('validating', '分片已保存,正在合并并校验整份文档')
398
- return runWikiTrial({ title: input.title, text: input.text, docId: input.sourceId, axes: input.axes, note: input.note,
439
+ const merged = mergeExtractionPayloads(values, parts.length > 1 || values.length > 1)
440
+ /**
441
+ * 合并之后先对一次账:原文里的每个二级章节、每一行表格,至少要被一条**已核验**的引文
442
+ * 覆盖。逐字核验只能证明写下来的没抄错,只有这一层能发现「整张表没抽」。
443
+ */
444
+ const coverage = auditCoverage(input.text, merged)
445
+ const supplementWarnings: string[] = []
446
+ let supplemented = false
447
+ if (coverage.gaps.length > 0) {
448
+ progress('validating', `${coverage.detail};正在补抽未被覆盖的部分`)
449
+ supplemented = await supplementCoverage(coverage, merged, {
450
+ input, budget: budget.bodyBudget, deadline, now,
451
+ nextRequest: () => {
452
+ // 补抽是附加动作:到顶就停,不把任务标成 failed(主体结果已经保存)。
453
+ if (now() >= deadline || requests >= requestBudget) {
454
+ const timedOut = now() >= deadline
455
+ throw Object.assign(new Error(`补抽已到${timedOut ? '时间' : '请求'}上限,后续缺口不再补`), { code: timedOut ? 'EXTRACTION_TIME_BUDGET' : 'EXTRACTION_REQUEST_BUDGET' })
456
+ }
457
+ requests += 1
458
+ },
459
+ progress: (text, phase) => progress(phase ?? 'waiting-model', text),
460
+ }).then(warnings => { supplementWarnings.push(...warnings); return true })
461
+ const after = auditCoverage(input.text, merged)
462
+ progress('validating', after.gaps.length === 0 ? `补抽完成:${after.detail}` : `${after.detail};仍有缺口,已记入报告`)
463
+ }
464
+ const report = await runWikiTrial({ title: input.title, text: input.text, docId: input.sourceId, axes: input.axes, note: input.note,
399
465
  modelRequest: input.modelRequest, sessionId: input.sessionId, outputFormat: input.outputFormat,
400
- call: async () => JSON.stringify(mergeExtractionPayloads(values, parts.length > 1 || values.length > 1)),
466
+ call: async () => JSON.stringify(merged),
401
467
  })
468
+ /**
469
+ * 最终覆盖度按**报告里真正留下来的知识**算。
470
+ *
471
+ * 补抽前用的是合并负载(还没过最后的归一/校验)。那一步可能再丢掉几条,所以对外
472
+ * 公布的读数必须以最终报告为准,否则「补过就齐了」会是一句假话。
473
+ */
474
+ const final = auditCoverage(input.text, { knowledge: report.knowledge, events: report.events })
475
+ report.coverage = {
476
+ sections: final.sections,
477
+ tables: final.tables,
478
+ blocks: final.blocks,
479
+ supplemented,
480
+ }
481
+ for (const gap of final.gaps) report.exceptions.push({ step: '覆盖自检', message: gap.detail })
482
+ for (const warning of supplementWarnings) report.exceptions.push({ step: '补抽', message: warning })
483
+ return report
484
+ }
485
+
486
+ /** 补抽一轮:把未覆盖的章节/表行发回去,只合并能核验的记录。失败只记下来,不推翻整次抽取。 */
487
+ async function supplementCoverage(
488
+ coverage: CoverageAudit,
489
+ merged: Row,
490
+ context: {
491
+ input: ResumableExtractionInput
492
+ budget: number
493
+ deadline: number
494
+ now: () => number
495
+ nextRequest: () => void
496
+ progress: (text: string, phase?: PipelineActivity['phase']) => void
497
+ },
498
+ ): Promise<string[]> {
499
+ const { input } = context
500
+ const warnings: string[] = []
501
+ const text = coverage.gaps.map(gap => gap.text.trim()).filter(Boolean).join('\n\n')
502
+ if (text === '') return warnings
503
+ const parts = planExtractionChunks(text, context.budget)
504
+ for (let index = 0; index < parts.length; index++) {
505
+ input.signal.throwIfAborted()
506
+ try {
507
+ context.nextRequest()
508
+ context.progress(`补抽未被覆盖的部分 ${index + 1}/${parts.length}`)
509
+ const timeout = AbortSignal.timeout(Math.max(1, Math.min(input.timeoutMs ?? EXTRACTION_RECOVERY_LIMITS.requestTimeoutMs, context.deadline - context.now())))
510
+ const signal = AbortSignal.any([input.signal, timeout])
511
+ const report = await runWikiTrial({
512
+ title: input.title, text: parts[index]!, docId: input.sourceId, axes: input.axes,
513
+ note: `${input.note ?? ''}\n${COVERAGE_SUPPLEMENT_NOTE}`,
514
+ modelRequest: input.modelRequest, sessionId: input.sessionId, outputFormat: input.outputFormat,
515
+ call: async request => input.call({ ...request, signal, retryAttempts: 1 }),
516
+ onDelta: () => {},
517
+ })
518
+ if (report.model.parsed !== true) throw new Error(report.model.why ?? '补抽没有按契约返回')
519
+ const raw = report.model.response
520
+ if (raw === undefined || raw.trim() === '') throw new Error('补抽没有返回内容')
521
+ const pruned = pruneUnverifiedRecords(payload(raw), report)
522
+ if (pruned.dropped > 0) context.progress(`补抽里有 ${pruned.dropped} 条引文核验未过,已丢弃`)
523
+ mergeCoverageSupplement(merged, pruned.value, `supp${index + 1}`)
524
+ context.progress(`补抽已合并;当前 ${Array.isArray(merged.knowledge) ? merged.knowledge.length : 0} 条知识`, 'saving')
525
+ } catch (error) {
526
+ input.signal.throwIfAborted()
527
+ const code = (error as { code?: string })?.code
528
+ const message = error instanceof Error ? error.message : String(error)
529
+ warnings.push(`第 ${index + 1} 段补抽未完成:${message}`)
530
+ // 预算或时间到顶时后面的缺口也不会成功,停下来把已有结果交出去。
531
+ if (code === 'EXTRACTION_TIME_BUDGET' || code === 'EXTRACTION_REQUEST_BUDGET') break
532
+ }
533
+ }
534
+ return warnings
402
535
  }
@@ -319,6 +319,21 @@ export interface TrialModelCallTrace {
319
319
  usage?: TrialTokenUsage
320
320
  }
321
321
 
322
+ /**
323
+ * 覆盖度自检的结果。
324
+ *
325
+ * 它回答「原文里有多少章节/表行被引用到了」,与 `checks` 的语义不同:
326
+ * `checks` 判的是写下来的对不对,这里判的是该写的有没有写。
327
+ */
328
+ export interface TrialCoverageSummary {
329
+ sections: { total: number; covered: number; missing: string[] }
330
+ tables: { total: number; rows: number; coveredRows: number; missing: string[] }
331
+ /** 段落 / 列表项 / 代码块:要么被引用,要么在 dropped 里写明原因。 */
332
+ blocks: { total: number; covered: number; missing: number }
333
+ /** 这次是否触发过补抽。false = 一次就覆盖齐了。 */
334
+ supplemented: boolean
335
+ }
336
+
322
337
  export interface TrialReport {
323
338
  title: string
324
339
  subject: string
@@ -335,6 +350,8 @@ export interface TrialReport {
335
350
  checks: TrialCheck[]
336
351
  steps: TrialStep[]
337
352
  exceptions: Array<{ step: string; message: string }>
353
+ /** 覆盖度自检(2026-11-03):二级章节与表格行是否都有已核验的引文。 */
354
+ coverage?: TrialCoverageSummary
338
355
  /**
339
356
  * 模型接缝的状态。`ok: false` 时 `why` 说明原因(界面上要显示,不能静默)。
340
357
  *