-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.ts
More file actions
618 lines (546 loc) · 24.6 KB
/
Copy pathanalysis.ts
File metadata and controls
618 lines (546 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import { parse } from 'yaml'
import rulesText from './rules.yaml?raw'
import { detectPilotChannel, parsePilotExport } from './connectors'
import type { ConnectorMessage, MessageRole, PilotChannel } from './connectors'
export type SourceKind = 'scope' | 'messages' | 'unknown'
export type SourceFormat = 'txt' | 'md' | 'eml' | 'json' | 'pdf' | 'docx' | 'unknown'
export type ScopeMatch = 'included' | 'excluded' | 'ambiguous' | 'none'
export type FindingCategory = 'scope_drift' | 'commercial_risk'
export type CommercialRiskKind = 'cancellation' | 'payment_risk' | 'availability_risk' | 'customer_delay'
export type SourceDocument = {
id: string
name: string
kind: SourceKind
format: SourceFormat
content: string
channel?: PilotChannel
}
export type Finding = {
id: string
category: FindingCategory
riskKind?: CommercialRiskKind
type: string
title: string
excerpt: string
source: string
scope: string
scopeMatch: ScopeMatch
hours: string
confidence: number
severity: 'high' | 'medium' | 'low'
reviewed: boolean
decision: 'pending' | 'change_request' | 'in_scope'
}
export type AnalysisResult = {
findings: Finding[]
scopeItemsCount: number
messagesCompared: number
messagesWithScopeBasis: number
scopeCoverage: number
hoursAtRisk: string
commercialRiskCount: number
unsupportedSources: string[]
}
export type SourceValidation = {
errors: string[]
warnings: string[]
}
type ScopeItem = {
id: string
section: string
text: string
excluded: boolean
}
type MessageRecord = {
id: string
text: string
source: string
author?: string
role?: MessageRole
}
type RuleConfig = {
id: string
category: FindingCategory
riskKind?: CommercialRiskKind
type: string
pattern: string
titleStyle: 'subject_not_in_scope' | 'changes_expectation' | 'extra_revision' | 'unpriced_commitment' | 'commercial_risk'
severity: Finding['severity']
confidence: number
minHours: number
maxHours: number
scopeTerms: string[]
}
type Rule = Omit<RuleConfig, 'pattern'> & { pattern: RegExp }
export const MAX_SOURCE_BYTES = 10 * 1024 * 1024
const rules = loadRules(rulesText)
export const demoSources: SourceDocument[] = [
{
id: 'demo-sow',
name: 'Acme_SOW_v3.txt',
kind: 'scope',
format: 'txt',
content: `# Acme launch site
## SOW §2.1 — Public marketing site
- Public marketing site
- Responsive desktop and mobile layouts
## SOW §3.2 — Daily status sync
- Daily status sync
## SOW §4.4 — Two revision rounds
- Two revision rounds
## SOW §2.2 — Excluded work
- No partner dashboard
- No analytics implementation or funnel tracking`,
},
{
id: 'demo-messages',
name: 'acme-slack-export.json',
kind: 'messages',
format: 'json',
channel: 'slack',
content: JSON.stringify([
{ channel: '#acme-launch', user: 'Alex', date: '14 May, 10:42', text: 'Can we also add a lightweight dashboard for partners before launch?' },
{ channel: 'Email', user: 'Alex', date: '13 May, 16:08', text: 'The status should update instantly without refreshing the page.' },
{ channel: '#acme-launch', user: 'Alex', date: '12 May, 09:17', text: 'One last pass on the headline and hero direction, then we are done.' },
{ channel: 'Email', user: 'Studio', date: '09 May, 11:24', text: 'We will make sure the new funnel is tracked end to end.' },
]),
},
]
export async function parseSourceFile(file: File): Promise<SourceDocument> {
const format = getFormat(file.name)
if (file.size > MAX_SOURCE_BYTES) {
throw new Error(`${file.name}: file is larger than 10 MB. Export a smaller date range for the pilot.`)
}
if (format === 'unknown') {
throw new Error(`${file.name}: unsupported file type. Use TXT, MD, EML or JSON for the pilot.`)
}
if (format === 'pdf' || format === 'docx') {
throw new Error(`${file.name}: PDF/DOCX extraction is not enabled yet; use TXT, MD, EML or JSON for now.`)
}
const content = await file.text()
return {
id: `${file.name}-${stableHash(content)}`,
name: file.name,
kind: inferKind(file.name, content),
format,
content,
channel: detectPilotChannel(file.name, content),
}
}
export function validateSources(sources: SourceDocument[]): SourceValidation {
const errors: string[] = []
const warnings: string[] = []
const scopeSources = sources.filter((source) => source.kind === 'scope')
const messageSources = sources.filter((source) => source.kind === 'messages')
const unknownSources = sources.filter((source) => source.kind === 'unknown')
if (!scopeSources.length) errors.push('Add at least one scope source (SOW, contract, brief or initial order email). If the agreed scope is in an email, classify that source as Scope document in the source list.')
if (!messageSources.length) errors.push('Add at least one communication export from Slack, email or WhatsApp.')
if (unknownSources.length) warnings.push(`Unclassified sources will not be analysed: ${unknownSources.map((source) => source.name).join(', ')}.`)
if (scopeSources.length && !scopeSources.some((source) => extractScopeItems(source.id, source.content).length)) {
errors.push('The scope source has no readable deliverables. Use bullet points or a clear initial order email with the requested work.')
}
if (messageSources.length && !messageSources.some((source) => extractMessages(source).length)) {
errors.push('The communication export has no readable messages. Check the export format or reclassify the source.')
}
return { errors, warnings }
}
export function analyzeSources(sources: SourceDocument[]): AnalysisResult {
const scopeSources = sources.filter((source) => source.kind === 'scope')
const messageSources = sources.filter((source) => source.kind === 'messages')
const scopeItems = scopeSources.flatMap((source) => extractScopeItems(source.id, source.content))
const messages = messageSources.flatMap((source) => extractMessages(source))
const findings = messages.flatMap((message) => createFindings(message, scopeItems))
const scopeFindings = findings.filter((finding) => finding.category === 'scope_drift')
const totalMin = scopeFindings.reduce((sum, finding) => sum + parseHours(finding.hours)[0], 0)
const totalMax = scopeFindings.reduce((sum, finding) => sum + parseHours(finding.hours)[1], 0)
const scopeRules = rules.filter((rule) => rule.category === 'scope_drift')
const messagesWithScopeBasis = messages.filter((message) => scopeRules.some((rule) => rule.pattern.test(message.text) && findScopeBasis(rule, message.text, scopeItems).status !== 'none')).length
const coverage = messages.length === 0 ? 0 : Math.round((messagesWithScopeBasis / messages.length) * 100)
return {
findings,
scopeItemsCount: scopeItems.length,
messagesCompared: messages.length,
messagesWithScopeBasis,
scopeCoverage: coverage,
hoursAtRisk: scopeFindings.length ? `${totalMin}–${totalMax}h` : '0h',
commercialRiskCount: findings.filter((finding) => finding.category === 'commercial_risk').length,
unsupportedSources: sources.filter((source) => source.kind === 'unknown').map((source) => source.name),
}
}
function inferKind(name: string, content: string): SourceKind {
const lowerName = name.toLowerCase().replace(/\s+/g, '-')
const normalizedContent = normalizeDocumentText(content)
const replyOrCancellationNameSignal = /(^|[-_.])(re|reply|fwd|forward|thread|cancel|cancellation)([-_.]|$)/.test(lowerName)
const scopeNameSignal = !replyOrCancellationNameSignal
&& /(^|[-_.])(sow|scope|contract|agreement|brief|proposal|order|request|intake|kickoff|requirements?)([-_.]|$)/.test(lowerName)
const scopeContentSignal = !replyOrCancellationNameSignal && (
/(^|\n)\s*#{1,6}\s*(included|excluded|scope|deliverables|assumptions|requirements)/im.test(normalizedContent)
|| /\b(new order|order number|shipping cost|total cost|deliverables?)\b/i.test(normalizedContent)
)
const messageNameSignal = /slack|email|message|messenger|telegram|whatsapp|facebook|meta|thread|chat|linear|jira/.test(lowerName)
const messageContentSignal = looksLikeMessageExport(content)
|| /(^|\n)\s*(from|subject|date):/im.test(content)
|| /^\s*(?:\[)?\d{4}[-/.]\d{1,2}[-/.]\d{1,2}[^|]*\|\s*[^|]+\|\s*.+$/m.test(content)
if (scopeNameSignal || scopeContentSignal) return 'scope'
if (messageNameSignal || messageContentSignal || detectPilotChannel(name, content)) return 'messages'
return 'unknown'
}
function looksLikeMessageExport(content: string): boolean {
try {
const parsed: unknown = JSON.parse(content)
const entries = Array.isArray(parsed) ? parsed : isRecord(parsed) && Array.isArray(parsed.messages) ? parsed.messages : []
return entries.some((entry) => isRecord(entry) && ['text', 'message', 'body', 'content'].some((key) => key in entry))
} catch {
return false
}
}
function getFormat(name: string): SourceFormat {
const extension = name.toLowerCase().split('.').pop()
if (extension === 'txt') return 'txt'
if (extension === 'md') return 'md'
if (extension === 'eml') return 'eml'
if (extension === 'json') return 'json'
if (extension === 'pdf') return 'pdf'
if (extension === 'docx') return 'docx'
return 'unknown'
}
function extractScopeItems(sourceId: string, content: string): ScopeItem[] {
const normalizedContent = normalizeDocumentText(content)
const richText = isRichTextDocument(content)
let currentSection = 'Scope'
const occurrences = new Map<string, number>()
const bulletItems = normalizedContent.split(/\r?\n/).flatMap((line) => {
const trimmed = line.trim()
if (!trimmed) return []
const heading = trimmed.match(/^#{1,6}\s*(.+)$/)
if (heading) {
currentSection = heading[1].trim()
return []
}
const bullet = trimmed.match(/^(?:[-*•]|\d+[.)])\s+(.+)$/)
if (!bullet || bullet[1].length < 5) return []
const text = bullet[1].trim()
const key = normalizeForMatching(text)
const occurrence = occurrences.get(key) ?? 0
occurrences.set(key, occurrence + 1)
return [{
id: `${sourceId}-scope-${stableHash(`${key}|${occurrence}`)}`,
section: currentSection,
text,
excluded: /excluded|no |not included|out of scope/i.test(currentSection) || /^(no |not included|excluded)/i.test(text),
}]
})
if (bulletItems.length) return bulletItems
const proseLines = richText
? normalizedContent.split(/\r?\n/)
: normalizedContent.replace(/\r\n/g, '\n').split(/\n\s*\n/).flatMap((block) => block.split(/(?<=[.!?])\s+(?=[A-Z0-9])/))
return proseLines
.map((line) => line.replace(/\s+/g, ' ').trim())
.filter((line) => line.length >= (richText ? 5 : 16))
.filter((line) => !/^(title|quantity|weight|cost)$/i.test(line))
.filter((line) => !/^(from|to|cc|bcc|subject|date|reply-to|mime-version|content-type|content-transfer-encoding|message-id):/i.test(line))
.filter((line) => !/^[-=]{3,}$/.test(line))
.filter((line) => !/^>/.test(line))
.filter((line) => !/^(hi|hello|dear|thanks|thank you|best|regards)[,!]?$/i.test(line))
.filter((line) => !/@/.test(line))
.map((text, index) => {
const key = normalizeForMatching(text)
const occurrence = occurrences.get(key) ?? 0
occurrences.set(key, occurrence + 1)
return {
id: `${sourceId}-scope-${stableHash(`${key}|${occurrence}|prose`)}`,
section: 'Initial order',
text,
excluded: /excluded|no |not included|out of scope/i.test(text),
}
})
}
function isRichTextDocument(content: string): boolean {
return /^\s*\{\\rtf/i.test(content)
}
function normalizeDocumentText(content: string): string {
if (!isRichTextDocument(content)) return content
const destinationWords = new Set([
'fonttbl', 'colortbl', 'stylesheet', 'info', 'generator', 'pict', 'object', 'filetbl',
'header', 'footer', 'listtable', 'listoverridetable', 'themedata', 'xmlnstbl', 'datastore',
])
const output: string[] = []
const stack: boolean[] = []
let skipGroup = false
let unicodeFallback = 1
let skipFallback = 0
for (let index = 0; index < content.length; index += 1) {
const character = content[index]
if (character === '{') {
stack.push(skipGroup)
continue
}
if (character === '}') {
skipGroup = stack.pop() ?? false
continue
}
if (character !== '\\') {
if (skipFallback > 0) {
skipFallback -= 1
} else if (!skipGroup) {
output.push(character)
}
continue
}
const next = content[index + 1]
if (next === '\\' || next === '{' || next === '}' || next === '~' || next === '-' || next === '_') {
if (!skipGroup && skipFallback === 0) output.push(next === '~' ? ' ' : next)
index += 1
continue
}
if (next === "'") {
const hex = content.slice(index + 2, index + 4)
if (/^[0-9a-f]{2}$/i.test(hex)) {
if (!skipGroup && skipFallback === 0) output.push(String.fromCharCode(Number.parseInt(hex, 16)))
index += 3
continue
}
}
const control = content.slice(index + 1).match(/^([a-z]+)(-?\d+)? ?/i)
if (!control) {
if (next === '*') skipGroup = true
index += 1
continue
}
const word = control[1].toLowerCase()
const parameter = control[2] ? Number(control[2]) : undefined
index += control[0].length
if (destinationWords.has(word)) skipGroup = true
if (word === 'uc' && parameter !== undefined) unicodeFallback = Math.max(0, parameter)
if (word === 'u' && parameter !== undefined) {
const codePoint = parameter < 0 ? parameter + 65536 : parameter
if (!skipGroup) output.push(String.fromCharCode(codePoint))
skipFallback = unicodeFallback
}
if (!skipGroup && ['par', 'line', 'cell', 'row'].includes(word)) output.push('\n')
if (!skipGroup && word === 'tab') output.push('\t')
}
return output.join('').replace(/\u00a0/g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim()
}
function extractMessages(source: SourceDocument): MessageRecord[] {
const channel = source.channel ?? (source.format === 'json' ? 'slack' : source.format === 'eml' ? 'gmail' : undefined)
if (channel) return parsePilotExport(channel, source.name, source.content).map((message, index) => normalizeMessage(source, message, index))
return source.content.split(/\n\s*\n|\r?\n/).map((text) => text.trim()).filter(Boolean).map((text, index) => ({
id: `${source.id}-${stableHash(`${normalizeForMatching(text)}|${index}`)}`,
text,
source: `${formatSourceName(source.name)} · message ${index + 1}`,
}))
}
function normalizeMessage(source: SourceDocument, message: ConnectorMessage, index: number): MessageRecord {
const text = message.text.trim()
return {
id: `${source.id}-${message.id ?? stableHash(`${normalizeForMatching(text)}|${index}`)}`,
text,
source: message.source || `${formatSourceName(source.name)} · message ${index + 1}`,
author: message.author,
role: message.role,
}
}
function createFindings(message: MessageRecord, scopeItems: ScopeItem[]): Finding[] {
return rules.flatMap((rule) => {
if (!rule.pattern.test(message.text)) return []
if (rule.category === 'scope_drift') {
if (rule.id === 'unpriced_commitment' && message.role === 'client') return []
if (isNegatedSignal(rule, message.text)) return []
} else if (isNegatedCommercialSignal(rule, message.text)) {
return []
}
if (rule.category === 'commercial_risk') return [createCommercialFinding(rule, message)]
const scopeMatch = findScopeBasis(rule, message.text, scopeItems)
if (rule.id === 'new_deliverable' && scopeMatch.status === 'included') return []
const confidence = adjustedConfidence(rule, scopeMatch.status)
const severity = adjustedSeverity(rule, scopeMatch.status)
return [{
id: `SG-${stableHash(`${message.id}|${rule.id}|${normalizeForMatching(message.text)}`)}`,
category: 'scope_drift',
type: rule.type,
title: titleFromRule(rule.titleStyle, message.text),
excerpt: findingExcerpt(rule, message.text),
source: message.author ? `${message.source} · ${message.author}` : message.source,
scope: scopeMatch.label,
scopeMatch: scopeMatch.status,
hours: `${rule.minHours}–${rule.maxHours}h`,
confidence,
severity,
reviewed: false,
decision: 'pending',
}]
})
}
function createCommercialFinding(rule: Rule, message: MessageRecord): Finding {
const riskKind = rule.riskKind ?? 'customer_delay'
return {
id: `SG-${stableHash(`${message.id}|${rule.id}|${normalizeForMatching(message.text)}`)}`,
category: 'commercial_risk',
riskKind,
type: rule.type,
title: commercialRiskTitle(riskKind),
excerpt: findingExcerpt(rule, message.text),
source: message.author ? `${message.source} · ${message.author}` : message.source,
scope: 'Commercial signal · Not a scope clause',
scopeMatch: 'none',
hours: '—',
confidence: rule.confidence,
severity: rule.severity,
reviewed: false,
decision: 'pending',
}
}
function commercialRiskTitle(kind: CommercialRiskKind): string {
if (kind === 'cancellation') return 'Order appears cancelled or paused'
if (kind === 'payment_risk') return 'Payment status may block the order'
if (kind === 'availability_risk') return 'Product availability may block delivery'
return 'Customer may be delaying the order'
}
function findingExcerpt(rule: Rule, text: string): string {
const match = rule.pattern.exec(text)
const matchIndex = match?.index ?? 0
const radiusBefore = rule.category === 'commercial_risk' ? 60 : 120
const radiusAfter = rule.category === 'commercial_risk' ? 140 : 220
const start = Math.max(0, matchIndex - radiusBefore)
const end = Math.min(text.length, matchIndex + (match?.[0].length ?? 0) + radiusAfter)
const prefix = start > 0 ? '…' : ''
const suffix = end < text.length ? '…' : ''
const excerpt = sanitizeEvidence(text.slice(start, end))
return `“${prefix}${excerpt}${suffix}”`
}
function sanitizeEvidence(value: string): string {
return value
.replace(/https?:\/\/\S+/gi, '[link]')
.replace(/[\w.+-]+@[\w.-]+\.[a-z]{2,}/gi, '[email]')
.replace(/(?:\+?\d[\d\s().-]{7,}\d)/g, '[phone]')
.replace(/\s+/g, ' ')
.trim()
.slice(0, 360)
}
function isNegatedCommercialSignal(rule: Rule, text: string): boolean {
if (rule.riskKind !== 'cancellation') return false
return /\b(?:not|never|no)\s+cancell?ed?\b/i.test(text)
}
function findScopeBasis(rule: Rule, text: string, scopeItems: ScopeItem[]): { status: ScopeMatch; label: string } {
const normalizedText = normalizeForMatching(text)
const textTokens = new Set(tokensFrom(normalizedText))
const candidates = scopeItems.map((item) => {
const itemText = normalizeForMatching(`${item.section} ${item.text}`)
const itemTokens = new Set(tokensFrom(itemText))
let score = 0
for (const term of rule.scopeTerms) {
const variants = expandTerm(term)
const textMatches = variants.some((variant) => hasTerm(normalizedText, variant))
const itemMatches = variants.some((variant) => hasTerm(itemText, variant))
if (textMatches && itemMatches) score += 5
}
for (const token of textTokens) {
if (token.length > 4 && itemTokens.has(token)) score += 1
}
if (score > 0 && item.excluded) score += 0.25
return { item, score }
}).filter((candidate) => candidate.score > 0)
if (!candidates.length) return { status: 'none', label: 'No matching clause found' }
candidates.sort((left, right) => right.score - left.score || Number(right.item.excluded) - Number(left.item.excluded))
const best = candidates[0]
const tied = candidates.filter((candidate) => Math.abs(candidate.score - best.score) < 0.01)
const status: ScopeMatch = tied.length > 1 ? 'ambiguous' : best.item.excluded ? 'excluded' : 'included'
const prefix = status === 'excluded' ? 'Excluded' : status === 'included' ? 'Included' : 'Ambiguous'
return { status, label: `${prefix} · ${best.item.section} · ${best.item.text}` }
}
function adjustedConfidence(rule: Rule, scopeMatch: ScopeMatch): number {
const adjustment = scopeMatch === 'none' ? -15 : scopeMatch === 'ambiguous' ? -20 : scopeMatch === 'included' ? -8 : 0
return Math.max(35, Math.min(99, rule.confidence + adjustment))
}
function adjustedSeverity(rule: Rule, scopeMatch: ScopeMatch): Finding['severity'] {
if (rule.id === 'unpriced_commitment' && scopeMatch === 'included') return 'low'
if (scopeMatch === 'ambiguous' && rule.severity === 'high') return 'medium'
return rule.severity
}
function isNegatedSignal(rule: Rule, text: string): boolean {
const match = rule.pattern.exec(text)
if (!match || match.index === undefined) return false
const context = text.slice(Math.max(0, match.index - 60), match.index + match[0].length + 80)
return /\b(?:not|no|never|won't|wouldn't|isn't|aren't|don't|do not|out of scope|not going to|will not)\b/i.test(context)
}
function extractSubject(text: string): string {
const subject = text.match(/(?:add|for|about)\s+(?:a\s+|an\s+|the\s+)?([^?.!]+?)(?:\s+before|\s+without|\s+for\s+partners|$)/i)?.[1]
const cleaned = subject ? subject.trim().replace(/^lightweight\s+/i, '').replace(/\s+$/, '') : 'This request'
return cleaned.charAt(0).toUpperCase() + cleaned.slice(1)
}
function titleFromRule(style: RuleConfig['titleStyle'], text: string): string {
if (style === 'subject_not_in_scope') return `${extractSubject(text)} is not in the agreed scope`
if (style === 'changes_expectation') return `${extractSubject(text)} changes the delivery expectation`
if (style === 'extra_revision') return 'An additional revision round was requested'
if (style === 'commercial_risk') return 'A commercial risk signal was detected'
return 'A delivery commitment appears without a matching price'
}
function loadRules(text: string): Rule[] {
try {
const parsed: unknown = parse(text)
if (!isRecord(parsed) || !Array.isArray(parsed.rules)) throw new Error('rules must contain a rules list')
return parsed.rules.map((value, index) => {
if (!isRecord(value) || !isRuleConfig(value)) throw new Error(`rule ${index + 1} is invalid`)
return { ...value, pattern: new RegExp(value.pattern, 'i') }
})
} catch (error) {
const message = error instanceof Error ? error.message : 'unknown YAML error'
throw new Error(`ScopeGuard rules could not be loaded: ${message}`)
}
}
function isRuleConfig(value: Record<string, unknown>): value is Record<string, unknown> & RuleConfig {
return typeof value.id === 'string'
&& ['scope_drift', 'commercial_risk'].includes(String(value.category))
&& typeof value.type === 'string'
&& typeof value.pattern === 'string'
&& typeof value.titleStyle === 'string'
&& (value.category !== 'commercial_risk' || ['cancellation', 'payment_risk', 'availability_risk', 'customer_delay'].includes(String(value.riskKind)))
&& ['high', 'medium', 'low'].includes(String(value.severity))
&& typeof value.confidence === 'number'
&& typeof value.minHours === 'number'
&& typeof value.maxHours === 'number'
&& Array.isArray(value.scopeTerms)
&& value.scopeTerms.every((term) => typeof term === 'string')
}
function expandTerm(term: string): string[] {
const normalized = normalizeForMatching(term)
const aliases: Record<string, string[]> = {
analytics: ['analytics', 'track', 'tracked', 'tracking'],
tracking: ['analytics', 'track', 'tracked', 'tracking'],
revision: ['revision', 'revisions', 'pass', 'round', 'rounds'],
round: ['revision', 'revisions', 'pass', 'round', 'rounds'],
}
return aliases[normalized] ?? [normalized]
}
function hasTerm(text: string, term: string): boolean {
const normalized = normalizeForMatching(term)
return normalized ? new RegExp(`(?:^|\\s)${escapeRegExp(normalized)}(?:$|\\s)`).test(text) : false
}
function normalizeForMatching(value: string): string {
return value.toLowerCase().replace(/[^a-z0-9]+/g, ' ').replace(/\s+/g, ' ').trim()
}
function tokensFrom(value: string): string[] {
return value.split(' ').filter((token) => token.length > 2 && !['the', 'and', 'for', 'with', 'this', 'that', 'from', 'into', 'will', 'can'].includes(token))
}
function escapeRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
function formatSourceName(name: string): string {
return name.replace(/\.[^.]+$/, '')
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
function parseHours(value: string): [number, number] {
const matches = value.match(/(\d+)–(\d+)/)
return matches ? [Number(matches[1]), Number(matches[2])] : [0, 0]
}
function stableHash(value: string): string {
let hash = 2166136261
for (let index = 0; index < value.length; index += 1) {
hash ^= value.charCodeAt(index)
hash = Math.imul(hash, 16777619)
}
return (hash >>> 0).toString(16).padStart(8, '0')
}