import type { RunEvidenceLedger } from '../types.js'; export interface FinalClaimValidation { verified: boolean; warnings: string[]; } /** For explicit memory operations only. A queue, a read or a review flag is * not a receipt for an actual save, correction or deletion. */ export function checkMemoryPublication(text: string, evidence: RunEvidenceLedger): { body: string; warning?: string } { const prefix = String.raw`(^|[\s,。!?,;:;:.!?])`; const endOrNoun = String.raw`(?=(?:(?:这条|该|你的|您的|长期|本机|我的)\s*)*(?:记忆|偏好)|[ \t]*(?:[,。!,;;.!]|$))`; const enMemory = String.raw`(?= (?:your|this|that|the) (?:preference|memory|decision)\b| (?:that|it)(?: to (?:long-term )?memory\b|[.!]|$)|[.!]|$)`; const grouped = String.raw`(?:(?:(?:这)?(?:两|多|几)条(?:记忆)?)?(?:都|均|全部))?`; const rule = (zh: string, en: string) => new RegExp(prefix + '(?:' + grouped + '(?:' + zh + ')|I(?:[\'’]ve| have) (?:' + en + '))', 'gi'); const claims = [ [rule('我?(?:已经|已|帮你|为你)(?:记住|记下)了?|我?(?:记住|记下)了|我?(?:已经|已)保存(?:了|到)?' + endOrNoun, String.raw`remembered\b|saved` + enMemory), ['saved','updated'], '保存'], [rule('我?(?:已经|已)(?:更新|纠正|改好|修改)了?' + endOrNoun, 'updated' + enMemory), ['updated'], '更新'], [rule('我?(?:已经|已)(?:忘记|遗忘)了?|我?(?:已经|已)删除了?' + endOrNoun, String.raw`forgotten\b|deleted` + enMemory), ['forgotten'], '遗忘'], ] as const; const missing = new Set(), partial = new Set(); // Replace only the unsupported verb phrase. Never split/rejoin the answer // into sentences: doing so used to flatten lists, code, paths and URLs. let fence: string | undefined; const body = text.split(/(?<=\n)/).map(line => { const boundary = /^\s*(`{3,}|~{3,})/.exec(line)?.[1]; if (fence) { if (boundary && boundary[0] === fence[0] && boundary.length >= fence.length) fence = undefined; return line; } if (boundary) { fence = boundary; return line; } if (/^\s*>/.test(line)) return line; return line.split(/(`[^`\r\n]*`|“[^”\r\n]*”|「[^」\r\n]*」|"[^"\r\n]*")/g).map((part, index) => { if (index % 2) return part; for (const [pattern, permitted, action] of claims) { part = part.replace(pattern, (match, prefix: string) => { const receipts = evidence.entries.filter(e => permitted.some(p => p === e.memoryMutation)); const successes = receipts.filter(e => e.ok); if (!successes.length) missing.add(action); else if (/(?:都|均|全部)/.test(match) && (new Set(successes.map(e => e.callHash)).size < 2 || receipts.some(e => !e.ok))) partial.add(action); else return match; return prefix + `【记忆${action}未确认】`; }); } return part; }).join(''); }).join(''); if (!missing.size && !partial.size) return { body: text }; const warnings: string[] = []; if (missing.size) warnings.push(`本轮没有长期记忆工具的${[...missing].join('、')}成功回执。后台学习即使已排队,也不代表已经落库。`); if (partial.size) warnings.push(`本轮只有部分${[...partial].join('、')}回执,不能确认全部成功。`); return { body, warning: warnings.join('') }; } export function missingMemoryReceipt(text: string, evidence: RunEvidenceLedger): string | undefined { return checkMemoryPublication(text, evidence).warning; } /** Conservative deterministic check: only strong positive verification claims are inspected. */ export function validateFinalClaims( assistantText: string, evidence: RunEvidenceLedger, ): FinalClaimValidation { const claimsVerification = /(?:我)?已(?:运行|执行|完成).{0,20}(?:测试|typecheck|构建)|(?:测试|typecheck|构建).{0,12}(?:已全部通过|全部通过|成功完成)/i.test( assistantText, ); const hasVerification = evidence.entries.some((entry) => entry.kind === 'verification' && entry.ok); const warnings = claimsVerification && !hasVerification ? ['最终回答包含已验证声明,但本轮证据台账没有成功的验证工具记录;该声明按未验证处理。'] : []; return { verified: warnings.length === 0, warnings }; }