dsh-compaction-weave 0.0.0-stage → 0.3.3
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.
- package/CHANGELOG.md +97 -0
- package/LICENSE +21 -0
- package/README.en.md +136 -0
- package/README.md +139 -2
- package/cordis.patch.yml +19 -0
- package/docs/architecture.md +84 -0
- package/docs/benchmarks/published-0.3.2-rc.1-tests.json +576 -0
- package/docs/benchmarks/rc2-documentation.json +41 -0
- package/docs/benchmarks/rc2-linux-tests.json +590 -0
- package/docs/benchmarks/rc2-live-and-cost.json +746 -0
- package/docs/benchmarks/rc3-host-compatibility.json +2314 -0
- package/docs/benchmarks/rc4-tests.json +617 -0
- package/docs/benchmarks/review35-compression.json +117 -0
- package/docs/benchmarks/v0.3.2-live-four-way.json +3495 -0
- package/docs/benchmarks/v0.3.2-tests.json +590 -0
- package/docs/configuration.md +131 -0
- package/docs/development.md +88 -0
- package/docs/en/architecture.md +75 -0
- package/docs/en/configuration.md +131 -0
- package/docs/en/development.md +86 -0
- package/docs/en/feedback.md +47 -0
- package/docs/en/principles.md +235 -0
- package/docs/en/testing.md +368 -0
- package/docs/en/troubleshooting.md +59 -0
- package/docs/feedback.md +47 -0
- package/docs/images/principles-context.en.svg +77 -0
- package/docs/images/principles-context.zh.svg +77 -0
- package/docs/images/principles-pipeline.en.svg +93 -0
- package/docs/images/principles-pipeline.zh.svg +93 -0
- package/docs/images/principles-recovery.en.svg +46 -0
- package/docs/images/principles-recovery.zh.svg +46 -0
- package/docs/principles.md +235 -0
- package/docs/test-results.json +634 -0
- package/docs/testing.md +358 -0
- package/docs/troubleshooting.md +59 -0
- package/lib/abort.js +13 -0
- package/lib/action-state.js +60 -0
- package/lib/atomic-replace.js +18 -0
- package/lib/attachments.js +76 -0
- package/lib/audit-reject.js +161 -0
- package/lib/audit.js +939 -0
- package/lib/cache-policy.js +71 -0
- package/lib/client.js +696 -0
- package/lib/command-compact.js +38 -0
- package/lib/command-heartbeat.js +61 -0
- package/lib/config.js +596 -0
- package/lib/constraint-scope.js +37 -0
- package/lib/continuation-budget.js +69 -0
- package/lib/continuation-pending.js +57 -0
- package/lib/continuation-progress.js +46 -0
- package/lib/continuation.js +287 -0
- package/lib/engine-entry.js +103 -0
- package/lib/engine.js +2485 -0
- package/lib/failure-diagnostics.js +220 -0
- package/lib/history-source.js +74 -0
- package/lib/history.js +712 -0
- package/lib/idle-compaction.js +64 -0
- package/lib/index.js +611 -0
- package/lib/instruction-source.js +163 -0
- package/lib/journal.js +164 -0
- package/lib/manual-error.js +179 -0
- package/lib/memory-policy.js +383 -0
- package/lib/meter.js +387 -0
- package/lib/observation-mask.js +390 -0
- package/lib/proactive.js +108 -0
- package/lib/progress-facts.js +347 -0
- package/lib/progress-state.js +30 -0
- package/lib/prompt.js +194 -0
- package/lib/reconstruct.js +72 -0
- package/lib/reference-input.js +107 -0
- package/lib/region.js +972 -0
- package/lib/source-provenance.js +58 -0
- package/lib/source-recovery.js +22 -0
- package/lib/source-refs.js +137 -0
- package/lib/state.js +46 -0
- package/lib/storage-path.js +42 -0
- package/lib/summary-budget.js +187 -0
- package/lib/summary-config.js +308 -0
- package/lib/summary-progress.js +56 -0
- package/lib/summary-retry.js +27 -0
- package/lib/summary-timeout.js +68 -0
- package/lib/summary.js +476 -0
- package/lib/tools.js +341 -0
- package/lib/transaction-host.js +252 -0
- package/lib/transaction.js +285 -0
- package/lib/validate.js +503 -0
- package/lib/version.js +3 -0
- package/lib/window.js +120 -0
- package/lib/work-report.js +55 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +71 -4
package/lib/audit.js
ADDED
|
@@ -0,0 +1,939 @@
|
|
|
1
|
+
// Domain-agnostic constraint, polarity, numeric/condition, and provenance auditor.
|
|
2
|
+
// Zero LLM calls; operates on summaryProse() so verbatim Section 6 cannot mask prose reversals.
|
|
3
|
+
|
|
4
|
+
import { summaryProse } from './validate.js';
|
|
5
|
+
import { actionState, sameAction } from './action-state.js';
|
|
6
|
+
import { isReferenceContextKind } from './source-provenance.js';
|
|
7
|
+
import { instructionText, referenceTexts } from './instruction-source.js';
|
|
8
|
+
|
|
9
|
+
const CN_MODAL = /不允许|不应(?:当|该)?|不(?=修改|删除|运行|调用|安装|部署|上传|下载|发送|联网|覆盖|发布|写入|写库|重启|终止|降低|改)|必须|不要(?!求)|不能|不得|禁止|记得|务必|只能|一定要|(?<![分区识个特级差类性鉴告离甄辨判派])别(?!的|人)|需要|请|注意|不可|无需|不准|严禁|勿|只有|仅当|如果|除非/;
|
|
10
|
+
const EN_MODAL = /\b(?:must|never|always|only|ensure|remember|required|forbidden|avoid|should|please|unless|if|when)\b|\bdo not\b|\bdon't\b/;
|
|
11
|
+
const COMMAND = /^(?:you\s+)?(?:use|run|keep|retain|preserve|delete|remove|restart|deploy|copy|move|sync|transfer|output|format)\b|^(?:保留|删除|运行|使用|发布|重启|复制|输出|跑\s)/;
|
|
12
|
+
const LABEL = /^(?:output(?:\s+format)?|format|port|timeout|retention|limit|输出格式|输出|端口|超时|保留时间|限制)\s*[::]/;
|
|
13
|
+
|
|
14
|
+
const NEGATION =
|
|
15
|
+
/不允许|不应(?:当|该)?|不要(?!求)|不能|不得|不必|不用|禁止|(?<![分区识个特级差类性鉴告离甄辨判派])别(?!的|人)|无需|不可|不准|严禁|勿|没有|尚未|并未|未曾|不(?:实际|直接|擅自)|不(?![同仅但过论管断少足安便光止久妨料然如至]|得不)(?=[\u4e00-\u9fff])|未(?=执行|运行|调用|修改|删除|写入|安装|重启|强停|改|降|提|kill|restart|append)|\b(?:must not|do not|don't|never|forbidden|avoid|no|not)\b/g;
|
|
16
|
+
|
|
17
|
+
const CJK_RUN = /[\u4e00-\u9fff\u3400-\u4dbf]{2,}/g;
|
|
18
|
+
const CONDITIONAL_PREFIX_RE =
|
|
19
|
+
/^(?:>\s*)*(?:(?:说明|注意|另外|后续|以后|其实|因此|当前)[^,,。;;\n]{0,12}?)?(?:(only\s+)?(if|when|unless)\s+(.+?)|(只有当|只有|仅当|如果|倘若|假如|除非|若|当)(.+?))[,,]\s*(.+)$/i;
|
|
20
|
+
const CONDITIONAL_SUFFIX_RE =
|
|
21
|
+
/^(?:>\s*)*(?:only\s+)?(.+?)\s+(?:only\s+)?(if|when|unless)\s+([^.,;!?\n]+)[.!?;]*$/i;
|
|
22
|
+
|
|
23
|
+
const CODE_SPLIT_MASK = (text, literals) =>
|
|
24
|
+
String(text ?? '').replace(
|
|
25
|
+
/`+[^`\n]*`+|(?:[a-z]:[/\\]|https?:\/\/)[^\s`,。;()]*[^\s`,。;().!?]|(?<![\w.])\d+(?:[.,]\d+)+(?![\w.])|\b[a-z_]\w*(?:\.[a-z0-9_-]+)+\b/gi,
|
|
26
|
+
(part) => {
|
|
27
|
+
literals.push(part);
|
|
28
|
+
return `\uE000${literals.length - 1}\uE001`;
|
|
29
|
+
},
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
function unmask(text, literals) {
|
|
33
|
+
return text.replace(/\uE000(\d+)\uE001/g, (_, idx) => literals[Number(idx)] ?? '');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function proseOnly(text) {
|
|
37
|
+
return String(text ?? '')
|
|
38
|
+
.replace(/`+[^`\n]*`+/g, (value) => {
|
|
39
|
+
const inner = value.replace(/^`+|`+$/g, '');
|
|
40
|
+
return /^(?:do not |must |never |always |不要(?!求)|必须|不能)/i.test(inner) ? inner : ' ';
|
|
41
|
+
})
|
|
42
|
+
.replace(/--[a-z][\w-]*|\bnot\s+null\b/gi, ' ');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function normalize(text) {
|
|
46
|
+
return String(text ?? '')
|
|
47
|
+
.normalize('NFC')
|
|
48
|
+
.replace(/\u00a0/g, ' ')
|
|
49
|
+
.replace(/\s+/g, ' ')
|
|
50
|
+
.trim()
|
|
51
|
+
.toLowerCase();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function negationsOf(line) {
|
|
55
|
+
const text = proseOnly(line).toLowerCase();
|
|
56
|
+
return [...new Set([...text.matchAll(NEGATION)].map((m) => m[0]))];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const DOUBLE_NEGATION_RE =
|
|
60
|
+
/(?:不要|不能|不得|不可|切勿|严禁|(?<![分区识个特级差类性鉴告离甄辨判派])别)\s*(?:不(?![同仅但过论管断少足安便光止久妨料然如至])|没(?:有)?|未)|不得不|\b(?:do\s+not|don't|must\s+not|never|cannot|can't)\s+(?:avoid\s+\w+ing\b|not\b|fail\s+to\b)/i;
|
|
61
|
+
|
|
62
|
+
function ruleNegative(line) {
|
|
63
|
+
const text = proseOnly(line).toLowerCase();
|
|
64
|
+
if (DOUBLE_NEGATION_RE.test(text)) return false;
|
|
65
|
+
return negationsOf(line).length > 0;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const UNIT_NORM = new Map([
|
|
69
|
+
['s', 's'], ['sec', 's'], ['second', 's'], ['seconds', 's'], ['秒', 's'],
|
|
70
|
+
['ms', 'ms'], ['毫秒', 'ms'],
|
|
71
|
+
['m', 'min'], ['min', 'min'], ['minute', 'min'], ['minutes', 'min'], ['分', 'min'], ['分钟', 'min'],
|
|
72
|
+
['h', 'h'], ['hour', 'h'], ['hours', 'h'], ['小时', 'h'],
|
|
73
|
+
['d', 'd'], ['day', 'd'], ['days', 'd'], ['天', 'd'],
|
|
74
|
+
['b', 'b'], ['byte', 'b'], ['bytes', 'b'], ['字节', 'b'],
|
|
75
|
+
['kb', 'kb'], ['kib', 'kib'], ['mb', 'mb'], ['mib', 'mib'], ['g', 'gb'], ['gb', 'gb'], ['gib', 'gib'], ['tb', 'tb'],
|
|
76
|
+
['%', '%'],
|
|
77
|
+
]);
|
|
78
|
+
|
|
79
|
+
const SIZE_CONTEXT_RE =
|
|
80
|
+
/(?:内存|显存|硬盘|磁盘|存储|空间|容量|体积|大小|上限|下限|配额|限额|限制|缓存|备份|文件|日志|带宽|流量|最大|最小|至少|不超过|小于|大于|低于|高于|\b(?:ram|vram|mem(?:ory)?|disk|storage|space|size|cap(?:acity)?|limit|quota|backup|cache|buffer|heap|max|min|at\s+least|at\s+most|under|over|below|above)\b)/i;
|
|
81
|
+
|
|
82
|
+
function isNonQuantityTechTerm(numText, space, rawUnit, contextText) {
|
|
83
|
+
if (space || !rawUnit) return false;
|
|
84
|
+
// Compact uppercase single-letter duration symbols (e.g. 3D, 2D, 3M, 6S, 2H) are tech terms, not durations.
|
|
85
|
+
if (/^[DSMH]$/.test(rawUnit)) return true;
|
|
86
|
+
// Bare "5G" without storage/memory/quota context is typically the telecom/WiFi term rather than 5GB.
|
|
87
|
+
if (rawUnit === 'G' && numText === '5' && !SIZE_CONTEXT_RE.test(contextText)) return true;
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function unitFamily(rawUnit) {
|
|
92
|
+
const u = UNIT_NORM.get(String(rawUnit ?? '').toLowerCase()) ?? String(rawUnit ?? '').toLowerCase();
|
|
93
|
+
if (['ms', 's', 'min', 'h', 'd'].includes(u)) return { family: 'duration', unit: u };
|
|
94
|
+
if (['b', 'kb', 'kib', 'mb', 'mib', 'gb', 'gib', 'tb'].includes(u)) return { family: 'size', unit: u };
|
|
95
|
+
return { family: u ? 'unit' : 'scalar', unit: u };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Parse simple `<subject> [= | 改为 | must be | ...] <number><unit?>` scalar assignments. */
|
|
99
|
+
export function scalarRequirement(text) {
|
|
100
|
+
const raw = String(text ?? '').trim();
|
|
101
|
+
if (!raw || raw.length > 512) return null;
|
|
102
|
+
let value = raw
|
|
103
|
+
.replace(/^(?:当前|目前|现行(?:有效)?|仍生效的?)(?:\s*[::]\s*)?/, '')
|
|
104
|
+
.replace(/[。.!!??;;,,]+$/, '');
|
|
105
|
+
if (!value || value.length > 256) return null;
|
|
106
|
+
if (/[`"'“”‘’「」『』<>{}[\]/\\]|如果|除非|只有|仅当|否则|或者|要么|(?:^|[,,])\s*(?:若|当)/.test(value)) return null;
|
|
107
|
+
value = value.replace(/[,,]\s*(?:替代|取代)(?:先前|原先|之前|此前|旧的)?\s*-?\d+(?:\.\d+)?[^\n。;;]*$/, '').trim();
|
|
108
|
+
const match =
|
|
109
|
+
/^(.{1,80}?)\s*(必须为|设为|设置为|保持(?:为)?|改为|改成|是|为|[:=:]|\bmust\s+be|\bis|\bset\s+to)?\s*(-?\d+(?:\.\d+)?)(\s*)(ms|s|sec|seconds?|m|min|minutes?|h|hours?|d|days?|gb|gib|mb|mib|kb|kib|tb|bytes?|b|g|%|毫秒|秒|分钟?|小时|天|字节|个|次|条)?$/i.exec(
|
|
110
|
+
value,
|
|
111
|
+
);
|
|
112
|
+
if (!match) return null;
|
|
113
|
+
const rawSubject = match[1];
|
|
114
|
+
const explicitOp = Boolean(match[2]);
|
|
115
|
+
const rawUnit = match[5] ?? '';
|
|
116
|
+
if (isNonQuantityTechTerm(match[3], match[4], rawUnit, value)) return null;
|
|
117
|
+
if (!explicitOp && !rawUnit) return null;
|
|
118
|
+
const hasLeadingSpace = !explicitOp && (/\s$/.test(rawSubject) || /\s(?=-?\d)/.test(value.slice(rawSubject.length)));
|
|
119
|
+
if (!explicitOp && !hasLeadingSpace && /[a-z0-9_.-]$/i.test(rawSubject)) return null;
|
|
120
|
+
const subject = normalize(rawSubject).replace(/\s+/g, ' ');
|
|
121
|
+
const num = Number(match[3]);
|
|
122
|
+
if (!subject || !Number.isFinite(num)) return null;
|
|
123
|
+
const normalizedNum = /^-?\d+$/.test(match[3]) && !Number.isSafeInteger(num) ? match[3] : num;
|
|
124
|
+
const { family, unit } = unitFamily(rawUnit);
|
|
125
|
+
return { subject, family, value: normalizedNum, unit };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function tokens(text) {
|
|
129
|
+
return normalize(text)
|
|
130
|
+
.split(/[\s,.;:!?()[\]{}"'`/\\|<>+=*&^%$#@~,。、;:!?()【】《》「」『』“”]+/)
|
|
131
|
+
.filter((token) => token.length > 0);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Tokenize for constraint coverage checks: keep `tokens()` intact for
|
|
136
|
+
* descriptor matching while splitting unpunctuated CJK runs (>= 3 chars)
|
|
137
|
+
* into overlapping 2-char bigrams.
|
|
138
|
+
*/
|
|
139
|
+
const coverageTokenCache = new Map();
|
|
140
|
+
const ASCII_TOKEN_RE = /^[a-zA-Z0-9_]+$/;
|
|
141
|
+
const ASCII_WORD_RUN_RE = /[a-zA-Z0-9_]+/g;
|
|
142
|
+
const asciiWordCache = new Map();
|
|
143
|
+
|
|
144
|
+
function coverageTokens(text) {
|
|
145
|
+
const key = String(text ?? '');
|
|
146
|
+
const cached = coverageTokenCache.get(key);
|
|
147
|
+
if (cached) return cached;
|
|
148
|
+
const base = tokens(key);
|
|
149
|
+
const out = [];
|
|
150
|
+
for (const token of base) {
|
|
151
|
+
let last = 0;
|
|
152
|
+
let expanded = false;
|
|
153
|
+
for (const match of token.matchAll(CJK_RUN)) {
|
|
154
|
+
const run = match[0];
|
|
155
|
+
if (run.length < 3) continue;
|
|
156
|
+
expanded = true;
|
|
157
|
+
if (match.index > last) out.push(token.slice(last, match.index));
|
|
158
|
+
for (let i = 0; i < run.length - 1; i += 1) {
|
|
159
|
+
out.push(run.slice(i, i + 2));
|
|
160
|
+
}
|
|
161
|
+
last = match.index + run.length;
|
|
162
|
+
}
|
|
163
|
+
if (!expanded) out.push(token);
|
|
164
|
+
else if (last < token.length) out.push(token.slice(last));
|
|
165
|
+
}
|
|
166
|
+
if (coverageTokenCache.size >= 1024) coverageTokenCache.clear();
|
|
167
|
+
const frozen = Object.freeze(out);
|
|
168
|
+
coverageTokenCache.set(key, frozen);
|
|
169
|
+
return frozen;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function asciiWordsOf(text) {
|
|
173
|
+
let words = asciiWordCache.get(text);
|
|
174
|
+
if (!words) {
|
|
175
|
+
if (asciiWordCache.size >= 512) asciiWordCache.clear();
|
|
176
|
+
const matches = text.match(ASCII_WORD_RUN_RE);
|
|
177
|
+
words = matches ? new Set(matches.map((w) => w.toLowerCase())) : new Set();
|
|
178
|
+
asciiWordCache.set(text, words);
|
|
179
|
+
}
|
|
180
|
+
return words;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function coverageOf(line, summaryNormalized) {
|
|
184
|
+
const haystack = String(summaryNormalized ?? '');
|
|
185
|
+
const parts = coverageTokens(line);
|
|
186
|
+
if (parts.length === 0) return 1;
|
|
187
|
+
const seen = new Map();
|
|
188
|
+
let asciiWords = null;
|
|
189
|
+
let hit = 0;
|
|
190
|
+
for (const token of parts) {
|
|
191
|
+
let matched = seen.get(token);
|
|
192
|
+
if (matched === undefined) {
|
|
193
|
+
if (ASCII_TOKEN_RE.test(token)) {
|
|
194
|
+
asciiWords ??= asciiWordsOf(haystack);
|
|
195
|
+
matched = asciiWords.has(token.toLowerCase());
|
|
196
|
+
} else {
|
|
197
|
+
matched = haystack.includes(token);
|
|
198
|
+
}
|
|
199
|
+
seen.set(token, matched);
|
|
200
|
+
}
|
|
201
|
+
if (matched) hit += 1;
|
|
202
|
+
}
|
|
203
|
+
return hit / parts.length;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function sourceEvidenceSentences(text) {
|
|
207
|
+
const literals = [];
|
|
208
|
+
const masked = CODE_SPLIT_MASK(text, literals);
|
|
209
|
+
const out = [];
|
|
210
|
+
for (const line of masked.split(/\r?\n/)) {
|
|
211
|
+
const strippedLine = line.replace(/^\s*(?:\d+[.)](?=\s)|\(\d+\)\s+|[-*•+]\s+)/, '');
|
|
212
|
+
if (!strippedLine.trim()) continue;
|
|
213
|
+
const parts = strippedLine.match(/[^.!?。!?;;]+[.!?。!?;;]*|[^.!?。!?;;]+$/g) ?? [strippedLine];
|
|
214
|
+
for (const part of parts) {
|
|
215
|
+
const restored = unmask(part, literals);
|
|
216
|
+
if (restored.trim()) out.push(restored);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function isInstructionLine(line) {
|
|
223
|
+
const raw = String(line ?? '').trim();
|
|
224
|
+
const lower = normalize(raw);
|
|
225
|
+
if (!lower) return false;
|
|
226
|
+
if (/^\s*#{1,6}\s/.test(lower)) return false;
|
|
227
|
+
if (/^\s*why\b[^.!\n]*[??]\s*$/i.test(raw)) return false;
|
|
228
|
+
const whyMatch = /(?:为什么|为何)[^。!!\n]*[??]\s*$/.exec(raw);
|
|
229
|
+
if (whyMatch) {
|
|
230
|
+
const preWhy = normalize(raw.slice(0, whyMatch.index).replace(/[,,;;::\s]+$/, ''));
|
|
231
|
+
if (
|
|
232
|
+
!preWhy ||
|
|
233
|
+
(!CN_MODAL.test(preWhy) &&
|
|
234
|
+
!EN_MODAL.test(preWhy) &&
|
|
235
|
+
!COMMAND.test(preWhy) &&
|
|
236
|
+
!LABEL.test(preWhy) &&
|
|
237
|
+
scalarRequirement(preWhy) === null)
|
|
238
|
+
) {
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return CN_MODAL.test(lower) || EN_MODAL.test(lower) || COMMAND.test(lower) || LABEL.test(lower) || scalarRequirement(line) !== null;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const SEQ_CITATION_RE =
|
|
246
|
+
/(?:\[\s*seqs?\s+\d+(?:\s*[-–,]\s*\d+)*\s*\]|[((]\s*seqs?\s+\d+(?:\s*[-–,]\s*\d+)*\s*[))]|\bseqs?\s*#?\s*\d+(?:\s*[-–,]\s*\d+)*\b|\bseqs?\s*[=:]\s*\d+\b|\bphase\s*#?\d+\b|>\s*user source\s*\(seq\s+\d+\),\s*verbatim:)/gi;
|
|
247
|
+
|
|
248
|
+
const stripSeqCitations = (text) => String(text ?? '').replace(SEQ_CITATION_RE, ' ');
|
|
249
|
+
|
|
250
|
+
function numericFacts(text) {
|
|
251
|
+
const out = [];
|
|
252
|
+
const cleaned = stripSeqCitations(text);
|
|
253
|
+
const value = cleaned.replace(/(?<!\d)\d{1,3}(?:,\d{3})+(?!\d)/g, (m) => m.replaceAll(',', ''));
|
|
254
|
+
const re =
|
|
255
|
+
/(?<![a-z0-9_-]|[a-z0-9]\.)(-?\d+(?:\.\d+)?)(\s*)(ms|s|sec|seconds?|m|min|minutes?|h|hours?|d|days?|gb|gib|mb|mib|kb|kib|tb|bytes?|b|g|%|毫秒|秒|分钟?|小时|天|字节)?(?![a-z0-9_-]|\.[a-z0-9])/gi;
|
|
256
|
+
for (const m of value.matchAll(re)) {
|
|
257
|
+
const rawUnit = m[3] ?? '';
|
|
258
|
+
if (isNonQuantityTechTerm(m[1], m[2], rawUnit, value)) continue;
|
|
259
|
+
const num = Number(m[1]);
|
|
260
|
+
if (!Number.isFinite(num)) continue;
|
|
261
|
+
const { unit } = unitFamily(rawUnit);
|
|
262
|
+
out.push(`${num}${unit}`);
|
|
263
|
+
}
|
|
264
|
+
for (const m of cleaned.matchAll(/(?<![a-z0-9_./-])([a-z0-9_./-]{1,120}\.[a-z]{1,6}):(\d+(?:-\d+)?)/gi)) {
|
|
265
|
+
out.push(`loc:${m[1].toLowerCase()}:${m[2]}`);
|
|
266
|
+
}
|
|
267
|
+
return out;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export function extractConstraintLines(text) {
|
|
271
|
+
if (typeof text !== 'string' || text.trim().length === 0) return [];
|
|
272
|
+
const out = [];
|
|
273
|
+
const seen = new Set();
|
|
274
|
+
for (const raw of sourceEvidenceSentences(text)) {
|
|
275
|
+
const sentence = raw.trim();
|
|
276
|
+
if (!isInstructionLine(sentence)) continue;
|
|
277
|
+
if (seen.has(sentence)) continue;
|
|
278
|
+
seen.add(sentence);
|
|
279
|
+
out.push(sentence);
|
|
280
|
+
}
|
|
281
|
+
return out;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
export function sourceConstraintEvidence(text) {
|
|
285
|
+
return String(text ?? '')
|
|
286
|
+
.split(/\r?\n/)
|
|
287
|
+
.filter((line) => extractConstraintLines(line).length > 0)
|
|
288
|
+
.flatMap((line) => sourceEvidenceSentences(line))
|
|
289
|
+
.filter((raw) => {
|
|
290
|
+
const sentence = raw.trim();
|
|
291
|
+
return isInstructionLine(sentence) || numericFacts(sentence).length > 0;
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function splitContrastClauses(text) {
|
|
296
|
+
const out = [];
|
|
297
|
+
for (const sentence of sourceEvidenceSentences(text)) {
|
|
298
|
+
if (/^\s*#{1,6}\s/.test(sentence)) continue;
|
|
299
|
+
if (conditionParts(sentence)) {
|
|
300
|
+
out.push(sentence);
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
const literals = [];
|
|
304
|
+
const masked = CODE_SPLIT_MASK(sentence, literals);
|
|
305
|
+
const parts = masked
|
|
306
|
+
.split(/(?<=[」』])\s*(?:(?:与|和|及|以及|、|\band\b)\s*)?(?=[「『])|(?:(?<!\d),|,(?!\d)|,)\s*(?:(?:但是?|不过|然而|but)\s+)?/i);
|
|
307
|
+
for (const part of parts) {
|
|
308
|
+
const clean = unmask(part, literals).trim();
|
|
309
|
+
if (clean) out.push(clean);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
return out;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const LOWER_BOUND_RE = /\b(?:at\s+least|minimum|no\s+fewer\s+than|no\s+less\s+than)\b|>=|至少|最少|不少于|不低于/i;
|
|
316
|
+
const UPPER_BOUND_RE = /\b(?:at\s+most|maximum|no\s+more\s+than|up\s+to)\b|<=|最多|至多|不超过|不高于/i;
|
|
317
|
+
|
|
318
|
+
const TRANSFER_VERB_RE = /\b(?:copy(?:ing)?|sync(?:ing)?|mov(?:e|ing)|migrat(?:e|ing)|transfer(?:ring)?|replicat(?:e|ing))\b/i;
|
|
319
|
+
|
|
320
|
+
function allTransferPairs(text) {
|
|
321
|
+
const value = String(text ?? '');
|
|
322
|
+
const found = [];
|
|
323
|
+
if (TRANSFER_VERB_RE.test(value)) {
|
|
324
|
+
for (const m of value.matchAll(
|
|
325
|
+
/\bfrom\s+([a-z0-9_]+(?:[.-][a-z0-9_]+)*)\s+(?:to|into)\s+([a-z0-9_]+(?:[.-][a-z0-9_]+)*)/gi,
|
|
326
|
+
)) {
|
|
327
|
+
found.push({ idx: m.index, pair: [m[1].toLowerCase(), m[2].toLowerCase()] });
|
|
328
|
+
}
|
|
329
|
+
for (const m of value.matchAll(
|
|
330
|
+
/\b(?:copy(?:ing)?|sync(?:ing)?|mov(?:e|ing)|migrat(?:e|ing)|transfer(?:ring)?|replicat(?:e|ing))\s+(?!from\b)([a-z0-9_]+(?:[.-][a-z0-9_]+)*)\s+(?:to|into)\s+([a-z0-9_]+(?:[.-][a-z0-9_]+)*)/gi,
|
|
331
|
+
)) {
|
|
332
|
+
found.push({ idx: m.index, pair: [m[1].toLowerCase(), m[2].toLowerCase()] });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
for (const m of value.matchAll(
|
|
336
|
+
/(?:把|将)\s*([^\s,。;,.;]{1,24}?)\s*(?:复制|同步|迁移|移动|拷贝|导入)(?:到|至|进)\s*([^\s,。;,.;。!!??]{1,24})/gu,
|
|
337
|
+
)) {
|
|
338
|
+
found.push({ idx: m.index, pair: [m[1].trim().toLowerCase(), m[2].trim().toLowerCase()] });
|
|
339
|
+
}
|
|
340
|
+
for (const m of value.matchAll(
|
|
341
|
+
/从\s*([^\s,。;,.;]{1,24}?)\s*(?:复制|同步|迁移|移动|拷贝)(?:到|至)\s*([^\s,。;,.;。!!??]{1,24})/gu,
|
|
342
|
+
)) {
|
|
343
|
+
found.push({ idx: m.index, pair: [m[1].trim().toLowerCase(), m[2].trim().toLowerCase()] });
|
|
344
|
+
}
|
|
345
|
+
found.sort((a, b) => a.idx - b.idx);
|
|
346
|
+
return found.map((item) => item.pair);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
const EXCEPTION_RE =
|
|
350
|
+
/\b(?:except(?:\s+for)?|excluding|other\s+than|aside\s+from|but\s+not|not\s+including)\s+([^.,;!?\n]{1,80})|除(?:了)?\s*([^,。;!!??\s][^,。;!!??\n]{0,80}?)\s*(?:以外|之外|外)|(?:不含|不包括|不包含)\s*([^,。;!!??\s][^,。;!!??\n]{0,80})/i;
|
|
351
|
+
|
|
352
|
+
function exceptionTarget(text) {
|
|
353
|
+
const m = EXCEPTION_RE.exec(text);
|
|
354
|
+
return m ? normalize(m[1] ?? m[2] ?? m[3]) : null;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const stripException = (text) => String(text ?? '').replace(EXCEPTION_RE, '').trim() || text;
|
|
358
|
+
|
|
359
|
+
function scopesCompatible(a, b) {
|
|
360
|
+
const scopeSet = (s) =>
|
|
361
|
+
new Set([
|
|
362
|
+
...[...String(s).matchAll(/\b(?:production|staging|development|test)\b|生产|测试|预发布/gi)].map((m) => m[0].toLowerCase()),
|
|
363
|
+
...[...String(s).matchAll(/(?:服务器|第|阶段|轮|批|步)\s*\d+/g)].map((m) => m[0].replace(/\s+/g, '')),
|
|
364
|
+
...[...String(s).matchAll(/\b(?:phase|stage|round|cycle|batch|step|turn|iteration)\s*#?\d+\b/gi)].map((m) =>
|
|
365
|
+
m[0].toLowerCase().replace(/[\s#]+/g, ''),
|
|
366
|
+
),
|
|
367
|
+
]);
|
|
368
|
+
const sa = scopeSet(a);
|
|
369
|
+
const sb = scopeSet(b);
|
|
370
|
+
for (const k of sa) {
|
|
371
|
+
if (sb.size > 0 && !sb.has(k)) return false;
|
|
372
|
+
}
|
|
373
|
+
for (const k of sb) {
|
|
374
|
+
if (sa.size > 0 && !sa.has(k)) return false;
|
|
375
|
+
}
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const NEGATED_PRIVATIVE = /(?:不要|不能|不得|不可|禁止|切勿|严禁|免于|避免|\bdo\s+not\b|\bdon't\b|\bmust\s+not\b|\bnever\b|\bavoid\b)[^。!?;;\n]{0,80}?(?:漏掉|遗漏|漏记|丢失|丢掉|丢弃|省略|略过|忽略|跳过|忘掉|删掉|删除|\bomit\b|\bdrop\b|\bskip\b|\bmiss\b|\bignore\b|\bforget\b|\bdelete\b|\bdiscard\b)/iu;
|
|
380
|
+
const BARE_PRIVATIVE = /(?:漏掉|遗漏|漏记|丢失|丢掉|丢弃|省略|略过|跳过|删掉|删除|\bomit\b|\bdrop\b|\bskip\b|\bmiss\b|\bforget\b|\bdelete\b|\bignore\b|\bdiscard\b)/iu;
|
|
381
|
+
const AFFIRMATIVE_KEEP = /(?:保留|留存|保存|存留|完整记录|包含|涵盖|纳入|覆盖|执行|\bkeep\b|\bretain\b|\bpreserve\b|\binclude\b|\benforce\b)/iu;
|
|
382
|
+
const NEGATED_DEFECT = /(?:不得|不能|不要|禁止|避免|\bno\b|\bnever\b|\bmust\s+not\b|\bwithout\b)[^。!?;;\n]{0,80}?(?:报错|错误|异常|漏洞|失败|缺陷|\berrors?\b|\bfailures?\b|\bbugs?\b|\bdefects?\b|\bexceptions?\b)/iu;
|
|
383
|
+
const DEFECT_FREE = /(?:零错误|零报错|零缺陷|无报错|无错误|无异常|zero\s+(?:errors?|defects?|bugs?)|error[-\s]?free|bug[-\s]?free|defect[-\s]?free)/iu;
|
|
384
|
+
const ANTONYM_BOILERPLATE =
|
|
385
|
+
/(?:不要|不能|不得|不可|禁止|切勿|严禁|免于|避免|必须|务必|应当|需要|应该|保证|确保|完整|全面|始终|永远|任何|所有|全部|一点|一丝|出现|漏掉|遗漏|漏记|丢失|丢掉|丢弃|省略|略过|忽略|跳过|忘掉|删掉|删除|保留|留存|保存|存留|包含|涵盖|纳入|覆盖|执行|\bdo\s+not\b|\bdon't\b|\bmust\s+not\b|\bnever\b|\bavoid\b|\bmust\b|\balways\b|\bensure\b|\ball\b|\bany\b|\bomit\b|\bdrop\b|\bskip\b|\bmiss\b|\bignore\b|\bforget\b|\bdelete\b|\bdiscard\b|\bkeep\b|\bretain\b|\bpreserve\b|\binclude\b|\benforce\b)/giu;
|
|
386
|
+
|
|
387
|
+
function oneWayAntonymMatch(negNorm, posNorm) {
|
|
388
|
+
if (NEGATED_PRIVATIVE.test(negNorm) && AFFIRMATIVE_KEEP.test(posNorm) && !BARE_PRIVATIVE.test(posNorm)) return true;
|
|
389
|
+
if (NEGATED_DEFECT.test(negNorm) && DEFECT_FREE.test(posNorm)) return true;
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function equivalentAntonymPolarity(source, candidate, sourceNegative, candNegative, limit = 0.6) {
|
|
394
|
+
if (sourceNegative === candNegative) return false;
|
|
395
|
+
const sNorm = normalize(source);
|
|
396
|
+
const cNorm = normalize(candidate);
|
|
397
|
+
const matched = sourceNegative ? oneWayAntonymMatch(sNorm, cNorm) : oneWayAntonymMatch(cNorm, sNorm);
|
|
398
|
+
if (!matched) return false;
|
|
399
|
+
const core = sNorm.replace(ANTONYM_BOILERPLATE, ' ').replace(/\s+/g, ' ').trim();
|
|
400
|
+
if (!core) return false;
|
|
401
|
+
return coverageOf(core, cNorm) >= limit;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
function conditionParts(text) {
|
|
405
|
+
const stripTemporal = (p, kw = '') => {
|
|
406
|
+
let out = p.replace(/(?:的话|的时候)$/, '').trim();
|
|
407
|
+
if (kw === '当') out = out.replace(/(?<![超实及暂随同定临长短])时$/, '').trim();
|
|
408
|
+
return out || p;
|
|
409
|
+
};
|
|
410
|
+
const trimmed = String(text ?? '').trim();
|
|
411
|
+
if (!trimmed || trimmed.length > 1024) return null;
|
|
412
|
+
const m = CONDITIONAL_PREFIX_RE.exec(trimmed);
|
|
413
|
+
if (m) {
|
|
414
|
+
const predicate = stripTemporal((m[3] ?? m[5] ?? '').trim(), m[4] ?? '');
|
|
415
|
+
const consequence = (m[6] ?? '').trim();
|
|
416
|
+
if (predicate && consequence) return { predicate, consequence };
|
|
417
|
+
}
|
|
418
|
+
const s = CONDITIONAL_SUFFIX_RE.exec(trimmed);
|
|
419
|
+
if (s) {
|
|
420
|
+
const consequence = s[1].trim();
|
|
421
|
+
const predicate = stripTemporal(s[3].trim());
|
|
422
|
+
if (predicate && consequence) return { predicate, consequence };
|
|
423
|
+
}
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const MODAL_STRIP = /(?:必须|务必|应当|应该|需要|可以|允许|请|先|\bmust\b|\bshould\b|\bcan\b|\bmay\b|\bplease\b)/gi;
|
|
428
|
+
const stripNegAndModal = (t) =>
|
|
429
|
+
proseOnly(normalize(t))
|
|
430
|
+
.replace(NEGATION, ' ')
|
|
431
|
+
.replace(MODAL_STRIP, ' ')
|
|
432
|
+
.replace(/[「」『』。.!!??;;,,]+/g, ' ')
|
|
433
|
+
.replace(/\s+/g, ' ')
|
|
434
|
+
.trim();
|
|
435
|
+
|
|
436
|
+
function clauseMatchesSource(source, candidate, limit) {
|
|
437
|
+
if (!scopesCompatible(source, candidate)) return false;
|
|
438
|
+
const sa = actionState(source);
|
|
439
|
+
const ca = actionState(candidate);
|
|
440
|
+
if (sa && ca && sameAction(sa, ca)) return true;
|
|
441
|
+
const stripLeadingBullet = (t) => stripSeqCitations(t).replace(/^\s*(?:[-*•+]|\d+[.)])\s+/, '').trim();
|
|
442
|
+
const sScalar = scalarRequirement(stripLeadingBullet(source));
|
|
443
|
+
const cScalar = scalarRequirement(stripLeadingBullet(candidate));
|
|
444
|
+
if (sScalar && cScalar && sScalar.subject === cScalar.subject) return true;
|
|
445
|
+
if (scalarSubjectVerdict(source, candidate) !== null) return true;
|
|
446
|
+
const sNorm = normalize(source);
|
|
447
|
+
const cNorm = normalize(candidate);
|
|
448
|
+
if (coverageOf(source, cNorm) >= limit) return true;
|
|
449
|
+
const subClauses = splitContrastClauses(source);
|
|
450
|
+
if (
|
|
451
|
+
subClauses.length > 1 &&
|
|
452
|
+
subClauses.some((sub) => isInstructionLine(sub) && coverageOf(sub, cNorm) >= limit)
|
|
453
|
+
) {
|
|
454
|
+
return true;
|
|
455
|
+
}
|
|
456
|
+
if (
|
|
457
|
+
coverageTokens(candidate).length >= 2 &&
|
|
458
|
+
coverageOf(candidate, sNorm) >= limit &&
|
|
459
|
+
coverageOf(source, cNorm) >= Math.min(limit, 0.35)
|
|
460
|
+
) {
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
const stripNeg = (t) =>
|
|
464
|
+
t.replace(NEGATION, ' ').replace(MODAL_STRIP, ' ').replace(/\*\*|__/g, '').replace(/[。.!!??;;,,]+/g, ' ').replace(/\s+/g, ' ').trim();
|
|
465
|
+
const sCore = stripNeg(proseOnly(sNorm));
|
|
466
|
+
const cCore = stripNeg(proseOnly(cNorm));
|
|
467
|
+
if (
|
|
468
|
+
sCore &&
|
|
469
|
+
cCore &&
|
|
470
|
+
(sCore === cCore ||
|
|
471
|
+
(coverageOf(sCore, cNorm) >= Math.max(limit, 0.75) &&
|
|
472
|
+
coverageOf(cCore, sNorm) >= 0.4))
|
|
473
|
+
) {
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
const cond = conditionParts(source);
|
|
477
|
+
if (cond) {
|
|
478
|
+
const ca2 = actionState(cond.consequence);
|
|
479
|
+
const consCore = stripNegAndModal(cond.consequence);
|
|
480
|
+
if (
|
|
481
|
+
(ca2 && ca && sameAction(ca2, ca)) ||
|
|
482
|
+
coverageOf(cond.consequence, cNorm) >= limit ||
|
|
483
|
+
(coverageOf(cond.predicate, cNorm) >= 0.5 && consCore.length >= 2 && coverageOf(consCore, cNorm) >= limit)
|
|
484
|
+
) {
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
return false;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function identifierImpliedUnits(text) {
|
|
492
|
+
const units = new Set();
|
|
493
|
+
const str = String(text ?? '');
|
|
494
|
+
for (const _ of str.matchAll(/(?:_|-)ms\b|(?<=[a-z0-9])Ms\b/g)) {
|
|
495
|
+
units.add('ms');
|
|
496
|
+
}
|
|
497
|
+
for (const m of str.matchAll(/(?:_|-|(?<=[a-z0-9]))(milliseconds?|millis|seconds?|secs?|minutes?|mins?|hours?|hrs?|days?|bytes?|kb|mb|gb|tb|pct|percent)\b/gi)) {
|
|
498
|
+
const raw = m[1].toLowerCase();
|
|
499
|
+
if (raw === 'millis' || raw === 'millisecond' || raw === 'milliseconds') units.add('ms');
|
|
500
|
+
else if (raw === 'pct' || raw === 'percent') units.add('%');
|
|
501
|
+
else if (raw === 'hrs' || raw === 'hr') units.add('h');
|
|
502
|
+
else if (raw === 'mins') units.add('min');
|
|
503
|
+
else if (raw === 'secs') units.add('s');
|
|
504
|
+
else units.add(unitFamily(raw).unit);
|
|
505
|
+
}
|
|
506
|
+
return units;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
function numericFactsEquivalent(source, candidate, sNums, cNums) {
|
|
510
|
+
if (JSON.stringify(sNums) === JSON.stringify(cNums)) return true;
|
|
511
|
+
if (sNums.length !== cNums.length) return false;
|
|
512
|
+
const splitNum = (token) => {
|
|
513
|
+
const m = /^(-?\d+(?:\.\d+)?)([a-z%]*)$/.exec(token);
|
|
514
|
+
return m ? { val: m[1], unit: m[2] } : null;
|
|
515
|
+
};
|
|
516
|
+
const sImplied = identifierImpliedUnits(source);
|
|
517
|
+
const cImplied = identifierImpliedUnits(candidate);
|
|
518
|
+
for (let i = 0; i < sNums.length; i += 1) {
|
|
519
|
+
if (sNums[i] === cNums[i]) continue;
|
|
520
|
+
const sp = splitNum(sNums[i]);
|
|
521
|
+
const cp = splitNum(cNums[i]);
|
|
522
|
+
if (!sp || !cp || sp.val !== cp.val) return false;
|
|
523
|
+
if (!sp.unit && cp.unit && sImplied.has(cp.unit)) continue;
|
|
524
|
+
if (!cp.unit && sp.unit && cImplied.has(sp.unit)) continue;
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
return true;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function stripInlineMarkdownFormatting(text) {
|
|
531
|
+
return String(text ?? '')
|
|
532
|
+
.replace(/(`+)([^\n]{1,256}?)\1/g, (_, _ticks, inner) => ` ${inner} `)
|
|
533
|
+
.replace(/(?<![a-z0-9_])(\*\*|__)([^\n]{1,160}?)\1(?![a-z0-9_])/gi, (full, marker, inner) =>
|
|
534
|
+
/^\s|\s$/.test(inner) || inner.includes(marker) ? full : ` ${inner} `,
|
|
535
|
+
);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
function scalarSubjectVerdict(activeSource, candidate) {
|
|
539
|
+
const strippedSrc = stripSeqCitations(activeSource).replace(/^\s*(?:[-*•+]|\d+[.)])\s+/, '').trim();
|
|
540
|
+
const sScalar = scalarRequirement(strippedSrc);
|
|
541
|
+
if (!sScalar || sScalar.subject.length < 2) return null;
|
|
542
|
+
const esc = sScalar.subject.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\s+/g, '\\s+');
|
|
543
|
+
const candClean = normalize(stripInlineMarkdownFormatting(stripSeqCitations(candidate)));
|
|
544
|
+
const re = new RegExp(
|
|
545
|
+
`(?<![a-z0-9_])${esc}(?:\\s*[:=:]?\\s*-?\\d+(?:\\.\\d+)?\\s*(?:->|→|=>)\\s*|\\s*(?:[:=:]|为|是|设为|设置为|保持(?:为)?|改为|改成|\\bmust\\s+be|\\bis|\\bset\\s+to)\\s*|\\s+)(-?\\d+(?:\\.\\d+)?)\\s*(ms|s|sec|seconds?|m|min|minutes?|h|hours?|d|days?|gb|gib|mb|mib|kb|kib|tb|bytes?|b|g|%|毫秒|秒|分钟?|小时|天|字节)?(?![a-z0-9_]|\\.[a-z0-9]|\\s*(?:->|→|=>))`,
|
|
546
|
+
'gi',
|
|
547
|
+
);
|
|
548
|
+
const matches = [...candClean.matchAll(re)];
|
|
549
|
+
if (matches.length === 0) return null;
|
|
550
|
+
const sToken = `${sScalar.value}${sScalar.unit}`;
|
|
551
|
+
for (const m of matches) {
|
|
552
|
+
const after = candClean.slice(m.index + m[0].length);
|
|
553
|
+
if (
|
|
554
|
+
/^\s*(?:[*+÷×^/]|[-–](?=\S)|[-–]\s*[-\d(]|~|\.\.|\bto\s+[-\d(])/i.test(after) ||
|
|
555
|
+
(m[2] !== '%' && /^\s*%/.test(after)) ||
|
|
556
|
+
(m[2] === '%' && /^\s*[-+\d(]/.test(after) && !/^\s*\([a-z\u4e00-\u9fff]/i.test(after))
|
|
557
|
+
) {
|
|
558
|
+
return 'mismatch';
|
|
559
|
+
}
|
|
560
|
+
const num = Number(m[1]);
|
|
561
|
+
if (!Number.isFinite(num)) return null;
|
|
562
|
+
const { unit } = unitFamily(m[2] ?? '');
|
|
563
|
+
const cToken = `${num}${unit}`;
|
|
564
|
+
if (!numericFactsEquivalent(activeSource, candidate, [sToken], [cToken])) {
|
|
565
|
+
return 'mismatch';
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return 'match';
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
function conflictForPair(source, candidate, limit) {
|
|
572
|
+
let activeSource = source;
|
|
573
|
+
const candNorm = normalize(candidate);
|
|
574
|
+
if (!conditionParts(source) && coverageOf(source, candNorm) < 1) {
|
|
575
|
+
const subClauses = splitContrastClauses(source);
|
|
576
|
+
if (subClauses.length > 1) {
|
|
577
|
+
const matchingSubs = subClauses.filter((sub) => clauseMatchesSource(sub, candidate, limit));
|
|
578
|
+
if (matchingSubs.length > 0) {
|
|
579
|
+
const scoreSub = (sub) => coverageOf(sub, candNorm) + coverageOf(candidate, normalize(sub));
|
|
580
|
+
matchingSubs.sort((a, b) => scoreSub(b) - scoreSub(a));
|
|
581
|
+
activeSource = matchingSubs[0];
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
const sEx = exceptionTarget(activeSource);
|
|
586
|
+
const cEx = exceptionTarget(candidate);
|
|
587
|
+
const cond = conditionParts(activeSource);
|
|
588
|
+
const candCond = conditionParts(candidate);
|
|
589
|
+
const normativeSource = cond ? cond.consequence : activeSource;
|
|
590
|
+
const candNormative = candCond ? candCond.consequence : candidate;
|
|
591
|
+
const srcNeg = ruleNegative(sEx ? stripException(normativeSource) : normativeSource);
|
|
592
|
+
const candNeg = ruleNegative(cEx ? stripException(candNormative) : candNormative);
|
|
593
|
+
|
|
594
|
+
if (isInstructionLine(normativeSource) && candNeg !== srcNeg && !equivalentAntonymPolarity(normativeSource, candNormative, srcNeg, candNeg, limit)) {
|
|
595
|
+
return { reason: 'polarity', candidate };
|
|
596
|
+
}
|
|
597
|
+
if (cond && !candCond && coverageOf(cond.predicate, candNorm) < 0.5) {
|
|
598
|
+
return { reason: 'condition', candidate };
|
|
599
|
+
}
|
|
600
|
+
if (cond && candCond) {
|
|
601
|
+
const srcUnless = /\bunless\b|除非/i.test(activeSource);
|
|
602
|
+
const candUnless = /\bunless\b|除非/i.test(candidate);
|
|
603
|
+
const srcPredNeg = ruleNegative(cond.predicate);
|
|
604
|
+
const candPredNeg = ruleNegative(candCond.predicate);
|
|
605
|
+
const hasAnd = (s) => /\band\b|且|并且/i.test(s);
|
|
606
|
+
const hasOr = (s) => /\bor\b|或者?|要么/i.test(s);
|
|
607
|
+
const connectorMismatch =
|
|
608
|
+
(hasAnd(cond.predicate) && !hasAnd(candCond.predicate) && hasOr(candCond.predicate)) ||
|
|
609
|
+
(hasOr(cond.predicate) && !hasOr(candCond.predicate) && hasAnd(candCond.predicate));
|
|
610
|
+
const splitCondTerms = (p) =>
|
|
611
|
+
String(p ?? '')
|
|
612
|
+
.split(/\s*(?:\b(?:and|or)\b|[且与]|并且|或者?|要么|、)\s*/i)
|
|
613
|
+
.map((s) => s.trim())
|
|
614
|
+
.filter(Boolean);
|
|
615
|
+
const srcTerms = splitCondTerms(cond.predicate);
|
|
616
|
+
const candTerms = splitCondTerms(candCond.predicate);
|
|
617
|
+
const droppedTerm =
|
|
618
|
+
srcTerms.length > candTerms.length &&
|
|
619
|
+
srcTerms.some((term) => Math.max(...candTerms.map((ct) => coverageOf(term, normalize(ct)))) < 0.6);
|
|
620
|
+
if (
|
|
621
|
+
srcUnless !== candUnless ||
|
|
622
|
+
connectorMismatch ||
|
|
623
|
+
droppedTerm ||
|
|
624
|
+
(srcPredNeg !== candPredNeg && !equivalentAntonymPolarity(cond.predicate, candCond.predicate, srcPredNeg, candPredNeg, limit)) ||
|
|
625
|
+
coverageOf(cond.predicate, normalize(candCond.predicate)) < limit
|
|
626
|
+
) {
|
|
627
|
+
return { reason: 'condition', candidate };
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (
|
|
631
|
+
(LOWER_BOUND_RE.test(activeSource) && UPPER_BOUND_RE.test(candidate) && !LOWER_BOUND_RE.test(candidate)) ||
|
|
632
|
+
(UPPER_BOUND_RE.test(activeSource) && LOWER_BOUND_RE.test(candidate) && !UPPER_BOUND_RE.test(candidate))
|
|
633
|
+
) {
|
|
634
|
+
return { reason: 'range', candidate };
|
|
635
|
+
}
|
|
636
|
+
const sDirs = allTransferPairs(activeSource);
|
|
637
|
+
const cDirs = allTransferPairs(candidate);
|
|
638
|
+
if (sDirs.length > 0 && cDirs.length > 0 && JSON.stringify(sDirs) !== JSON.stringify(cDirs)) {
|
|
639
|
+
return { reason: 'relation', candidate };
|
|
640
|
+
}
|
|
641
|
+
if (sEx && (!cEx || sEx !== cEx)) {
|
|
642
|
+
return { reason: 'exception', candidate };
|
|
643
|
+
}
|
|
644
|
+
const subjVerdict = scalarSubjectVerdict(activeSource, candidate);
|
|
645
|
+
if (subjVerdict === 'mismatch') {
|
|
646
|
+
return { reason: 'value', candidate };
|
|
647
|
+
}
|
|
648
|
+
if (subjVerdict === 'match') {
|
|
649
|
+
return null;
|
|
650
|
+
}
|
|
651
|
+
const sNums = numericFacts(activeSource);
|
|
652
|
+
const cNums = numericFacts(candidate);
|
|
653
|
+
if (sNums.length > 0) {
|
|
654
|
+
if (cNums.length > 0 && !numericFactsEquivalent(activeSource, candidate, sNums, cNums)) {
|
|
655
|
+
return { reason: 'value', candidate };
|
|
656
|
+
}
|
|
657
|
+
if (cNums.length === 0 && isInstructionLine(activeSource) && !/[零一二两三四五六七八九十百千万亿]/.test(candidate)) {
|
|
658
|
+
const nonNumSource = normalize(activeSource).replace(/-?\d+(?:\.\d+)?/g, ' ').replace(/\s+/g, ' ').trim();
|
|
659
|
+
if (coverageTokens(nonNumSource).length >= 2 && coverageOf(nonNumSource, candNorm) >= Math.max(limit, 0.75)) {
|
|
660
|
+
return { reason: 'value', candidate };
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
return null;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
const NORMATIVE_OR_CURRENT_CLAIM_RE =
|
|
668
|
+
/(?:仍(?:然|旧)?|继续|现行|当前|目前|依然|有效|生效|批准|允许|保持|维持|遵循|采用|\b(?:still|current(?:ly)?|now|active|valid|effective|approved|authorized|permitted|enforced|kept|retained|maintain(?:ed)?|use[ds]?|apply|applies)\b)/i;
|
|
669
|
+
|
|
670
|
+
const UNIT_OPT_RE = '(?:ms|s|sec|seconds?|m|min|minutes?|h|hours?|d|days?|gb|gib|mb|mib|kb|kib|tb|bytes?|b|g|%|毫秒|秒|分钟?|小时|天|字节)?';
|
|
671
|
+
const HIST_TAG_ONLY_RE =
|
|
672
|
+
'(?:previously|formerly|originally|initially|superseded|deprecated|当时|原值|旧值|已修订|已取代)';
|
|
673
|
+
|
|
674
|
+
function isBoundHistoricalValue(sourceLine, candidate) {
|
|
675
|
+
if (new Set(numericFacts(candidate)).size > 1) return false;
|
|
676
|
+
const strippedSrc = stripSeqCitations(sourceLine).replace(/^\s*(?:[-*•+]|\d+[.)])\s+/, '').trim();
|
|
677
|
+
const sScalar = scalarRequirement(strippedSrc);
|
|
678
|
+
if (!sScalar || sScalar.subject.length < 2) return false;
|
|
679
|
+
const esc = sScalar.subject.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').replace(/\s+/g, '\\s+');
|
|
680
|
+
const candClean = normalize(stripSeqCitations(candidate)).replace(/^\s*(?:[-*•+]|\d+[.)])\s+/, '').trim();
|
|
681
|
+
const histLeadPrefix = '(?:(?:轮已输出|历史记录|历史快照)\\s*[::]\\s*)?';
|
|
682
|
+
const histTrailSuffix = `(?:\\s*[((]\\s*${HIST_TAG_ONLY_RE}\\s*[))]|\\s+\\b(?:was|is)\\s+(?:superseded|deprecated)\\b)?\\s*[。.!!;;]?$`;
|
|
683
|
+
return (
|
|
684
|
+
new RegExp(
|
|
685
|
+
`^${histLeadPrefix}${esc}\\s*(?:当时(?:报|为|是|输出)|曾(?:经)?(?:为|是|报|用|设为)|原(?:先|来)?(?:为|是|设(?:为)?)|(?:之前|此前|早先|先前)(?:为|是|报)|\\b(?:was|were)\\s+(?:previously|formerly|originally|initially)\\b|\\b(?:previously|formerly|originally|initially)\\s+(?:set\\s+to|reported\\s+as|was|were)\\b)\\s*(?:[:=:]\\s*)?(?:null|-?\\d+(?:\\.\\d+)?\\s*${UNIT_OPT_RE})${histTrailSuffix}`,
|
|
686
|
+
'i',
|
|
687
|
+
).test(candClean) ||
|
|
688
|
+
new RegExp(
|
|
689
|
+
`^${histLeadPrefix}(?:当时|之前|此前|早先|先前|原先)\\s+${esc}\\s*(?:[:=:]|为|是|报)\\s*(?:null|-?\\d+(?:\\.\\d+)?\\s*${UNIT_OPT_RE})${histTrailSuffix}`,
|
|
690
|
+
'i',
|
|
691
|
+
).test(candClean) ||
|
|
692
|
+
new RegExp(
|
|
693
|
+
`^${histLeadPrefix}${esc}\\s*[:=:]?\\s*(?:null|-?\\d+(?:\\.\\d+)?\\s*${UNIT_OPT_RE})\\s*(?:[((]\\s*${HIST_TAG_ONLY_RE}\\s*[))]|\\b(?:was|is)\\s+(?:superseded|deprecated)\\b)\\s*[。.!!;;]?$`,
|
|
694
|
+
'i',
|
|
695
|
+
).test(candClean)
|
|
696
|
+
);
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
export function auditCompaction({ userMessages, summaryText, checkpointText, threshold, includeAllConflicts = false, referenceMessages = [] }) {
|
|
700
|
+
const originalMessages = userMessages ?? [];
|
|
701
|
+
const trustedMessages = originalMessages
|
|
702
|
+
.filter((message) => !isReferenceContextKind(message.sourceKind))
|
|
703
|
+
.map((message) => ({ ...message, text: instructionText(message) }));
|
|
704
|
+
|
|
705
|
+
const limit = typeof threshold === 'number' && Number.isFinite(threshold) ? threshold : 0.6;
|
|
706
|
+
const haystack = normalize(String(checkpointText ?? summaryText).replace(/\*\*|__/g, ''));
|
|
707
|
+
const summaryClauses = splitContrastClauses(summaryText ? summaryProse(summaryText) : '');
|
|
708
|
+
const messageLines = trustedMessages.map((message) => ({
|
|
709
|
+
message,
|
|
710
|
+
lines: sourceEvidenceSentences(message.text).filter(
|
|
711
|
+
(line) => isInstructionLine(line) || numericFacts(line).length > 0,
|
|
712
|
+
),
|
|
713
|
+
}));
|
|
714
|
+
const allTrustedLines = messageLines.flatMap((entry) => entry.lines);
|
|
715
|
+
|
|
716
|
+
const lost = [];
|
|
717
|
+
let checked = 0;
|
|
718
|
+
let worst = 1;
|
|
719
|
+
let polarityFlips = 0;
|
|
720
|
+
|
|
721
|
+
for (const { message, lines } of messageLines) {
|
|
722
|
+
for (const line of lines) {
|
|
723
|
+
checked += 1;
|
|
724
|
+
let coverage = coverageOf(line, haystack);
|
|
725
|
+
const srcNeg = ruleNegative(line);
|
|
726
|
+
|
|
727
|
+
const matchedCandidates = summaryClauses.filter((c) => clauseMatchesSource(line, c, limit));
|
|
728
|
+
const lineIsScalar =
|
|
729
|
+
scalarRequirement(stripSeqCitations(line).replace(/^\s*(?:[-*•+]|\d+[.)])\s+/, '').trim()) !== null;
|
|
730
|
+
const hasExactActiveMatch = matchedCandidates.some(
|
|
731
|
+
(c) =>
|
|
732
|
+
coverageOf(line, normalize(c)) >= Math.max(limit, 0.8) &&
|
|
733
|
+
conflictForPair(line, c, limit) === null &&
|
|
734
|
+
(!lineIsScalar || scalarSubjectVerdict(line, c) === 'match'),
|
|
735
|
+
);
|
|
736
|
+
const conflicts = [];
|
|
737
|
+
for (const candidate of matchedCandidates) {
|
|
738
|
+
const conflict = conflictForPair(line, candidate, limit);
|
|
739
|
+
if (!conflict) continue;
|
|
740
|
+
const candNormForHist = normalize(candidate);
|
|
741
|
+
const hasNormativeOrCurrentClaim =
|
|
742
|
+
CN_MODAL.test(candNormForHist) ||
|
|
743
|
+
EN_MODAL.test(candNormForHist) ||
|
|
744
|
+
COMMAND.test(candNormForHist) ||
|
|
745
|
+
NORMATIVE_OR_CURRENT_CLAIM_RE.test(candNormForHist);
|
|
746
|
+
if (
|
|
747
|
+
hasExactActiveMatch &&
|
|
748
|
+
conflict.reason === 'value' &&
|
|
749
|
+
!hasNormativeOrCurrentClaim &&
|
|
750
|
+
isBoundHistoricalValue(line, candidate)
|
|
751
|
+
) {
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
if (scalarSubjectVerdict(line, candidate) === 'mismatch') {
|
|
755
|
+
conflicts.push(conflict);
|
|
756
|
+
continue;
|
|
757
|
+
}
|
|
758
|
+
const cNorm = normalize(candidate);
|
|
759
|
+
const lineCov = coverageOf(line, cNorm);
|
|
760
|
+
const candInLine = coverageOf(candidate, normalize(line));
|
|
761
|
+
const ownedByPeer = allTrustedLines.some((peer) => {
|
|
762
|
+
if (peer === line || !scopesCompatible(peer, candidate)) return false;
|
|
763
|
+
const pNorm = normalize(peer);
|
|
764
|
+
const forwardBetter = coverageOf(peer, cNorm) >= Math.max(0.85, lineCov + 0.05);
|
|
765
|
+
const reverseBetter = coverageOf(candidate, pNorm) >= Math.max(0.85, candInLine + 0.05);
|
|
766
|
+
return (forwardBetter || reverseBetter) && conflictForPair(peer, candidate, limit) === null;
|
|
767
|
+
});
|
|
768
|
+
if (!ownedByPeer) conflicts.push(conflict);
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
if (coverage < limit) {
|
|
772
|
+
const antonymMatch = summaryClauses.find(
|
|
773
|
+
(c) =>
|
|
774
|
+
scopesCompatible(line, c) &&
|
|
775
|
+
equivalentAntonymPolarity(line, c, srcNeg, ruleNegative(c), limit) &&
|
|
776
|
+
numericFacts(line).join(',') === numericFacts(c).join(','),
|
|
777
|
+
);
|
|
778
|
+
if (antonymMatch) coverage = 1;
|
|
779
|
+
}
|
|
780
|
+
worst = Math.min(worst, coverage);
|
|
781
|
+
|
|
782
|
+
if (conflicts.length > 0) {
|
|
783
|
+
const primary = conflicts[0];
|
|
784
|
+
if (primary.reason === 'polarity') polarityFlips += 1;
|
|
785
|
+
lost.push({
|
|
786
|
+
seq: message.seq,
|
|
787
|
+
line,
|
|
788
|
+
candidate: primary.candidate,
|
|
789
|
+
coverage: Number(coverage.toFixed(3)),
|
|
790
|
+
reason: primary.reason,
|
|
791
|
+
...(includeAllConflicts ? { conflicts } : {}),
|
|
792
|
+
});
|
|
793
|
+
} else if (coverage < limit) {
|
|
794
|
+
lost.push({
|
|
795
|
+
seq: message.seq,
|
|
796
|
+
line,
|
|
797
|
+
coverage: Number(coverage.toFixed(3)),
|
|
798
|
+
reason: 'coverage',
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
const provenance = auditInstructionProvenance({ userMessages: originalMessages, referenceMessages, summaryText });
|
|
805
|
+
return {
|
|
806
|
+
checked,
|
|
807
|
+
lost: [...lost, ...provenance],
|
|
808
|
+
polarityFlips,
|
|
809
|
+
worstCoverage: checked === 0 ? 1 : Number(worst.toFixed(3)),
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
const APPROVAL_CLAIM_RE =
|
|
814
|
+
/(?:用户\s*(?:现在|已经|已|明确|现已)?\s*(?:批准|授权|同意|允许)|(?:已|已经)(?:获(?:得)?|得到)用户(?:授权|批准|同意)|\b(?:the\s+)?user\s+(?:has\s+)?(?:already\s+|explicitly\s+|now\s+)?(?:approved|authorized|authorised|permitted|allowed|consented|granted\s+permission)|\b(?:approved|authorized|authorised|permitted)\s+by\s+(?:the\s+)?user\b)/i;
|
|
815
|
+
const DIRECT_USER_APPROVAL_RE =
|
|
816
|
+
/(?:^\s*(?:我\s*(?:现在|已经|已|明确|现已)?\s*(?:批准|授权|同意|允许)|i\s+(?:hereby\s+)?(?:authorize|authorise|approve|allow|permit|consent\s+to)\b))/im;
|
|
817
|
+
const NEGATED_OR_PENDING_APPROVAL_RE =
|
|
818
|
+
/(?:未|不|没有|尚无|尚未|并非|等待|等候|待|如(?:果)?|只有|仅当)[^。!?;;\n]{0,30}(?:授权|批准|允许|同意)|\b(?:if|unless|pending|awaiting|not|no|without)\b[^.!?\n;]{0,50}\b(?:approval|permission|consent|approved|authorized|authorised)\b/i;
|
|
819
|
+
|
|
820
|
+
const APPROVAL_BOILERPLATE_RE =
|
|
821
|
+
/(?:(?:我|用户)\s*(?:现在|已经|已|明确|现已|在此)?\s*(?:批准|授权|同意|允许)|(?:已|已经)?(?:获(?:得)?|得到)用户(?:授权|批准|同意)|\b(?:the\s+)?user\s+(?:has\s+)?(?:already\s+|explicitly\s+|now\s+)?(?:approved|authorized|authorised|permitted|allowed|consented|granted\s+permission(?:\s+(?:to|for))?)|\bi\s+(?:hereby\s+)?(?:authorize|authorise|approve|allow|permit|consent\s+to)|\b(?:approved|authorized|authorised|permitted)\s+by\s+(?:the\s+)?user|\bto\b|\bfor\b|\bthe\b)/gi;
|
|
822
|
+
|
|
823
|
+
const REVOCATION_APPROVAL_RE =
|
|
824
|
+
/\b(?:revoke|withdraw|cancel|rescind|reject|deny|forbid)\s+(?:my\s+|the\s+|this\s+)?(?:prior\s+|previous\s+)?(?:approval|authorization|authorisation|permission|consent)\b|\b(?:do\s+not|don't|no\s+longer|not)\s+(?:approve|authorize|authorise|permit|allow|consent)\b|(?:撤销|取消|收回|撤回|作废|不再|不|不要|禁止|拒绝)(?:之前|刚才|上述|先前的?)?(?:的)?(?:批准|授权|同意|允许)/i;
|
|
825
|
+
|
|
826
|
+
const REVOCATION_BOILERPLATE_RE =
|
|
827
|
+
/(?:(?:(?:我|用户)\s*)?(?:现在|已经|已|明确|现已|在此)?\s*(?:撤销|取消|收回|撤回|作废|不再|禁止|拒绝|不要|不)\s*(?:之前|刚才|上述|先前的?)?(?:对)?(?:的)?(?:批准|授权|同意|允许)?|(?:之前|刚才|上述|先前的?)?(?:的)?(?:批准|授权|同意|允许)|\b(?:(?:i|the\s+user)\s+)?(?:hereby\s+)?(?:revoke|withdraw|cancel|rescind|reject|deny|forbid|do\s+not|don't|no\s+longer|not)\s+(?:my\s+|the\s+|this\s+)?(?:prior\s+|previous\s+)?(?:approval|authorization|authorisation|permission|consent|approve|authorize|authorise|permit|allow)\b|\b(?:of|for|on|to|the|my|this)\b)/gi;
|
|
828
|
+
|
|
829
|
+
const stripApprovalBoilerplate = (text) =>
|
|
830
|
+
normalize(text)
|
|
831
|
+
.replace(APPROVAL_BOILERPLATE_RE, ' ')
|
|
832
|
+
.replace(/[。.!!??;;,,]+/g, ' ')
|
|
833
|
+
.replace(/\s+/g, ' ')
|
|
834
|
+
.trim();
|
|
835
|
+
|
|
836
|
+
const stripRevocationBoilerplate = (text) =>
|
|
837
|
+
normalize(text)
|
|
838
|
+
.replace(REVOCATION_BOILERPLATE_RE, ' ')
|
|
839
|
+
.replace(/[。.!!??;;,,]+/g, ' ')
|
|
840
|
+
.replace(/\s+/g, ' ')
|
|
841
|
+
.trim();
|
|
842
|
+
|
|
843
|
+
function approvalTargetsMatch(srcCore, srcClause, claimCore, claimSentence, { isRevocation = false } = {}) {
|
|
844
|
+
if (!srcCore || !claimCore) return false;
|
|
845
|
+
const sDirs = allTransferPairs(srcCore);
|
|
846
|
+
const cDirs = allTransferPairs(claimCore);
|
|
847
|
+
const srcPairs = sDirs.length ? sDirs : allTransferPairs(srcClause);
|
|
848
|
+
const claimPairs = cDirs.length ? cDirs : allTransferPairs(claimSentence);
|
|
849
|
+
if ((srcPairs.length > 0 || claimPairs.length > 0) && JSON.stringify(srcPairs) !== JSON.stringify(claimPairs)) {
|
|
850
|
+
return false;
|
|
851
|
+
}
|
|
852
|
+
const sEx = exceptionTarget(srcCore) ?? exceptionTarget(srcClause);
|
|
853
|
+
const cEx = exceptionTarget(claimCore) ?? exceptionTarget(claimSentence);
|
|
854
|
+
if (isRevocation) {
|
|
855
|
+
if (cEx && sEx !== cEx) return false;
|
|
856
|
+
} else if ((sEx || cEx) && sEx !== cEx) {
|
|
857
|
+
return false;
|
|
858
|
+
}
|
|
859
|
+
const sNums = numericFacts(srcCore);
|
|
860
|
+
const cNums = numericFacts(claimCore);
|
|
861
|
+
if (isRevocation ? cNums.length > 0 && JSON.stringify(sNums) !== JSON.stringify(cNums) : JSON.stringify(sNums) !== JSON.stringify(cNums)) {
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
const sa = actionState(srcCore);
|
|
865
|
+
const ca = actionState(claimCore);
|
|
866
|
+
if (isRevocation && coverageOf(sEx ? stripException(srcCore) : srcCore, normalize(claimCore)) >= 0.7) return true;
|
|
867
|
+
if (sa && ca) return sa.negative === ca.negative && sameAction(sa, ca);
|
|
868
|
+
return (
|
|
869
|
+
Math.min(
|
|
870
|
+
coverageOf(claimCore, normalize(srcClause)),
|
|
871
|
+
coverageOf(srcCore, normalize(claimSentence)),
|
|
872
|
+
) >= 0.7
|
|
873
|
+
);
|
|
874
|
+
}
|
|
875
|
+
|
|
876
|
+
/**
|
|
877
|
+
* Channel-based compaction laundering defense:
|
|
878
|
+
* rejects summary claims of user approval/authorization unless a matching,
|
|
879
|
+
* unrevoked verified user channel approval backs that specific action/scope.
|
|
880
|
+
*/
|
|
881
|
+
export function auditInstructionProvenance({ userMessages = [], referenceMessages = [], summaryText }) {
|
|
882
|
+
const verifiedMessages = userMessages
|
|
883
|
+
.filter((m) => !isReferenceContextKind(m.sourceKind))
|
|
884
|
+
.slice()
|
|
885
|
+
.sort((a, b) => (a.seq ?? 0) - (b.seq ?? 0));
|
|
886
|
+
|
|
887
|
+
let activeGrants = [];
|
|
888
|
+
for (const m of verifiedMessages) {
|
|
889
|
+
for (const clause of splitContrastClauses(instructionText(m))) {
|
|
890
|
+
const isRevoke =
|
|
891
|
+
REVOCATION_APPROVAL_RE.test(clause) ||
|
|
892
|
+
((APPROVAL_CLAIM_RE.test(clause) || DIRECT_USER_APPROVAL_RE.test(clause)) &&
|
|
893
|
+
NEGATED_OR_PENDING_APPROVAL_RE.test(clause));
|
|
894
|
+
if (isRevoke) {
|
|
895
|
+
const revCore = stripRevocationBoilerplate(clause);
|
|
896
|
+
activeGrants = activeGrants.filter((g) => {
|
|
897
|
+
if (!scopesCompatible(clause, g.clause)) return true;
|
|
898
|
+
if (!revCore) return false;
|
|
899
|
+
return !approvalTargetsMatch(g.core, g.clause, revCore, clause, { isRevocation: true });
|
|
900
|
+
});
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
if (APPROVAL_CLAIM_RE.test(clause) || DIRECT_USER_APPROVAL_RE.test(clause)) {
|
|
904
|
+
activeGrants.push({ clause, core: stripApprovalBoilerplate(clause) });
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
const untrustedRefs = [
|
|
910
|
+
...userMessages.flatMap((m) =>
|
|
911
|
+
(isReferenceContextKind(m.sourceKind) ? [m.text] : referenceTexts(m)).map((text) => ({ seq: m.seq, text })),
|
|
912
|
+
),
|
|
913
|
+
...referenceMessages.map((m) => ({ seq: m.seq ?? -1, text: m.text })),
|
|
914
|
+
];
|
|
915
|
+
const refSeq = untrustedRefs.find((r) => APPROVAL_CLAIM_RE.test(r.text))?.seq ?? untrustedRefs[0]?.seq ?? -1;
|
|
916
|
+
|
|
917
|
+
const lost = [];
|
|
918
|
+
for (const sentence of splitContrastClauses(summaryText ? summaryProse(summaryText) : '')) {
|
|
919
|
+
if (!APPROVAL_CLAIM_RE.test(sentence)) continue;
|
|
920
|
+
if (NEGATED_OR_PENDING_APPROVAL_RE.test(sentence)) continue;
|
|
921
|
+
|
|
922
|
+
const claimCore = stripApprovalBoilerplate(sentence);
|
|
923
|
+
const backed = activeGrants.some((g) => {
|
|
924
|
+
if (!scopesCompatible(g.clause, sentence)) return false;
|
|
925
|
+
if (!claimCore) return true;
|
|
926
|
+
return approvalTargetsMatch(g.core, g.clause, claimCore, sentence);
|
|
927
|
+
});
|
|
928
|
+
if (backed) continue;
|
|
929
|
+
|
|
930
|
+
lost.push({
|
|
931
|
+
seq: refSeq,
|
|
932
|
+
line: sentence,
|
|
933
|
+
candidate: sentence,
|
|
934
|
+
coverage: 0,
|
|
935
|
+
reason: 'provenance',
|
|
936
|
+
});
|
|
937
|
+
}
|
|
938
|
+
return lost;
|
|
939
|
+
}
|