reqtable 0.1.0__py3-none-any.whl

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.
reqtable/findings.py ADDED
@@ -0,0 +1,605 @@
1
+ """Trace findings computed in Python, kept in step with the report's JavaScript.
2
+
3
+ Each function mirrors one function of TRACE_MODEL_JS, VALUE_MODEL_JS or GLOSSARY_MODEL_JS
4
+ so the CLI can report the same findings the report shows. tests/test_findings_parity.py
5
+ runs both implementations against the same tables.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import re
11
+ from collections.abc import Callable, Container, Iterable
12
+ from dataclasses import dataclass, field
13
+ from typing import Any
14
+
15
+ from .baselines import select_scope
16
+
17
+ # String.prototype.trim strips this set, which differs from str.strip() (\x1c-\x1f, \x85, \ufeff).
18
+ _JS_WHITESPACE = (
19
+ "\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a"
20
+ "\u2028\u2029\u202f\u205f\u3000\ufeff"
21
+ )
22
+
23
+ TRACE_FIELDS: dict[str, tuple[tuple[str, ...], tuple[str, ...], bool]] = {
24
+ "Part of": (("System",), ("System",), True),
25
+ "Allocated to": (("Requirement",), ("System",), False),
26
+ "Owner": (("Requirement",), ("System",), True),
27
+ "Derived from": (("Requirement",), ("Requirement",), False),
28
+ "Source refs": (("Requirement", "Need", "Term"), ("Need", "Source"), False),
29
+ "Verified by": (("Requirement",), ("Test",), False),
30
+ "Test plan": (("Test",), ("Test plan",), True),
31
+ "Affects": (
32
+ ("Change request",),
33
+ (
34
+ "Requirement",
35
+ "System",
36
+ "Need",
37
+ "Source",
38
+ "Term",
39
+ "Value",
40
+ "Field",
41
+ "Test",
42
+ "Test plan",
43
+ ),
44
+ False,
45
+ ),
46
+ }
47
+ RECORD_TYPES = (
48
+ "System",
49
+ "Requirement",
50
+ "Need",
51
+ "Source",
52
+ "Term",
53
+ "Value",
54
+ "Field",
55
+ "Test",
56
+ "Test plan",
57
+ "Change request",
58
+ )
59
+ VERIFICATION_STATES = {
60
+ "Compliance": ("Requirement", "requirements"),
61
+ "Result": ("Test", "tests"),
62
+ "Change status": ("Change request", "change requests"),
63
+ }
64
+ APPROVED_CHANGE_STATES = ("Approved", "Implemented")
65
+ _YES_OR_NO = re.compile(r"(?:yes|no)", re.IGNORECASE | re.ASCII)
66
+
67
+
68
+ @dataclass(frozen=True)
69
+ class Finding:
70
+ id: str | None
71
+ field: str
72
+ message: str
73
+
74
+
75
+ @dataclass(eq=False)
76
+ class Record:
77
+ id: str
78
+ row: dict[str, str]
79
+ type: str
80
+ outgoing: dict[str, list[Record]] = field(default_factory=dict)
81
+
82
+
83
+ @dataclass(eq=False)
84
+ class ValueRule:
85
+ multiple: bool
86
+ values: dict[str, Record] = field(default_factory=dict)
87
+ aliases: dict[str, Record] = field(default_factory=dict)
88
+
89
+
90
+ Issue = Callable[[str | None, str, str], None]
91
+ Controls = dict[str, Any]
92
+
93
+
94
+ def _trim(value: str) -> str:
95
+ return value.strip(_JS_WHITESPACE)
96
+
97
+
98
+ def _cell(row: dict[str, str], column: str) -> str:
99
+ value = row.get(column)
100
+ return "" if value is None else str(value)
101
+
102
+
103
+ def trace_type(row: dict[str, str]) -> str:
104
+ return _trim(_cell(row, "Type")) or "Requirement"
105
+
106
+
107
+ def trace_ids(value: str | None) -> list[str]:
108
+ return [
109
+ part
110
+ for part in (
111
+ _trim(piece) for piece in ("" if value is None else value).split(";")
112
+ )
113
+ if part
114
+ ]
115
+
116
+
117
+ def trace_target_types(field_name: str, record_type: str) -> tuple[str, ...]:
118
+ if field_name == "Source refs" and record_type != "Requirement":
119
+ return ("Source",)
120
+ return TRACE_FIELDS[field_name][1]
121
+
122
+
123
+ def defined_values(rule: ValueRule, cell: str | None) -> list[str]:
124
+ text = _trim("" if cell is None else cell)
125
+ parts = trace_ids(text) if rule.multiple else [text] if text else []
126
+ names = [
127
+ part
128
+ if part in rule.values
129
+ else _trim(_cell(rule.aliases[part].row, "Name"))
130
+ if part in rule.aliases
131
+ else part
132
+ for part in parts
133
+ ]
134
+ return list(dict.fromkeys(names))
135
+
136
+
137
+ def verification_state(
138
+ values: dict[str, ValueRule], column: str, cell: str | None
139
+ ) -> str:
140
+ text = _trim("" if cell is None else cell)
141
+ rule = values.get(column)
142
+ if not rule:
143
+ return text
144
+ found = defined_values(rule, text)
145
+ return found[0] if found else ""
146
+
147
+
148
+ def check_verification(
149
+ records: dict[str, Record], values: dict[str, ValueRule], issue: Issue
150
+ ) -> None:
151
+ def sentence(parts: list[str]) -> str:
152
+ return (
153
+ f"{', '.join(parts[:-1])} and {parts[-1]}" if len(parts) > 1 else parts[0]
154
+ )
155
+
156
+ for record in records.values():
157
+ for column, (applies_to, plural) in VERIFICATION_STATES.items():
158
+ if record.type != applies_to and _trim(_cell(record.row, column)):
159
+ issue(record.id, column, f"{column} applies to {plural}.")
160
+ if (
161
+ record.type != "Requirement"
162
+ or verification_state(values, "Compliance", record.row.get("Compliance"))
163
+ != "Compliant"
164
+ ):
165
+ continue
166
+ results = [
167
+ (test.id, verification_state(values, "Result", test.row.get("Result")))
168
+ for test in record.outgoing.get("Verified by", [])
169
+ ]
170
+ unmet = [(test_id, result) for test_id, result in results if result != "Passed"]
171
+ if unmet:
172
+ described = [
173
+ f"{test_id} is “{result}”" if result else f"{test_id} has no result"
174
+ for test_id, result in unmet
175
+ ]
176
+ issue(record.id, "Compliance", f"Compliant, but {sentence(described)}.")
177
+
178
+
179
+ def check_test_coverage(
180
+ records: dict[str, Record], values: dict[str, ValueRule], issue: Issue
181
+ ) -> None:
182
+ def quote(names: Iterable[str]) -> str:
183
+ return ", ".join(f"“{name}”" for name in names)
184
+
185
+ for record in records.values():
186
+ tests = record.outgoing.get("Verified by", [])
187
+ if not tests:
188
+ continue
189
+ for field_name, rule in values.items():
190
+ planned = defined_values(rule, record.row.get(field_name))
191
+ covered = list(
192
+ dict.fromkeys(
193
+ name
194
+ for test in tests
195
+ for name in defined_values(rule, test.row.get(field_name))
196
+ )
197
+ )
198
+ missing = [name for name in planned if name not in covered]
199
+ if covered and missing:
200
+ issue(
201
+ record.id,
202
+ field_name,
203
+ f"No test in Verified by covers {quote(missing)}. Its tests have {quote(covered)}.",
204
+ )
205
+
206
+
207
+ def build_glossary(records: Iterable[Record], issue: Issue) -> None:
208
+ names: dict[str, list[Record]] = {}
209
+ spellings: dict[str, dict[str, tuple[Record, str]]] = {}
210
+ for record in records:
211
+ if record.type != "Term":
212
+ continue
213
+ name, definition = (
214
+ _trim(_cell(record.row, "Name")),
215
+ _trim(_cell(record.row, "Definition")),
216
+ )
217
+ if not name:
218
+ issue(record.id, "Name", "Enter a glossary term name.")
219
+ if not definition:
220
+ issue(record.id, "Definition", "Enter a glossary definition.")
221
+ if name:
222
+ names.setdefault(name.lower(), []).append(record)
223
+ for spelling, field_name in [
224
+ (name, "Name"),
225
+ *((alias, "Aliases") for alias in trace_ids(record.row.get("Aliases"))),
226
+ ]:
227
+ if spelling:
228
+ spellings.setdefault(spelling, {}).setdefault(
229
+ record.id, (record, field_name)
230
+ )
231
+ ambiguous_names = set()
232
+ for group in names.values():
233
+ if len(group) < 2:
234
+ continue
235
+ for record in group:
236
+ issue(record.id, "Name", "Another glossary record uses this term name.")
237
+ ambiguous_names.add(_trim(_cell(record.row, "Name")))
238
+ for spelling, entries in spellings.items():
239
+ if len(entries) < 2:
240
+ continue
241
+ for record, field_name in entries.values():
242
+ if field_name != "Name" or spelling not in ambiguous_names:
243
+ issue(
244
+ record.id,
245
+ field_name,
246
+ f"Glossary spelling “{spelling}” is shared by multiple terms.",
247
+ )
248
+
249
+
250
+ def value_column_problem(
251
+ name: str, headers: set[str], reserved: set[str]
252
+ ) -> str | None:
253
+ if not name:
254
+ return None
255
+ if name in reserved:
256
+ return f"“{name}” is a reserved column and cannot have defined values."
257
+ if name not in headers:
258
+ return f"There is no “{name}” column."
259
+ return None
260
+
261
+
262
+ def value_groups(
263
+ records: Iterable[Record], key: Callable[[Record], str]
264
+ ) -> dict[str, list[Record]]:
265
+ groups: dict[str, list[Record]] = {}
266
+ for record in records:
267
+ name = key(record)
268
+ if name:
269
+ groups.setdefault(name, []).append(record)
270
+ return groups
271
+
272
+
273
+ def build_value_rules(
274
+ records: Iterable[Record], headers: list[str], id_column: str, issue: Issue
275
+ ) -> dict[str, ValueRule]:
276
+ header_set, reserved = (
277
+ set(headers),
278
+ {*TRACE_FIELDS, "Type", "Field", "Multiple", id_column},
279
+ )
280
+ rules: dict[str, ValueRule] = {}
281
+ every = list(records)
282
+ field_records = [record for record in every if record.type == "Field"]
283
+
284
+ def column(record: Record) -> str:
285
+ return _trim(_cell(record.row, "Field"))
286
+
287
+ def target(record: Record) -> str:
288
+ return _trim(_cell(record.row, "Name"))
289
+
290
+ for name, group in value_groups(field_records, target).items():
291
+ problem = value_column_problem(name, header_set, reserved)
292
+ if problem:
293
+ for record in group:
294
+ issue(record.id, "Name", problem)
295
+ continue
296
+ if len(group) > 1:
297
+ for record in group:
298
+ issue(record.id, "Name", f"Another Field record describes {name}.")
299
+ rules[name] = ValueRule(
300
+ multiple=_trim(_cell(group[0].row, "Multiple")).lower() == "yes"
301
+ )
302
+ for record in field_records:
303
+ multiple = _trim(_cell(record.row, "Multiple"))
304
+ if not target(record):
305
+ issue(record.id, "Name", "Enter the column this record describes.")
306
+ if not _trim(_cell(record.row, "Definition")):
307
+ issue(record.id, "Definition", "Describe this column.")
308
+ if multiple and not _YES_OR_NO.fullmatch(multiple):
309
+ issue(record.id, "Multiple", "Enter Yes or No.")
310
+ value_records = [record for record in every if record.type == "Value"]
311
+ for record in value_records:
312
+ name, field_name = target(record), column(record)
313
+ if not field_name:
314
+ issue(record.id, "Field", "Enter the column this value belongs to.")
315
+ else:
316
+ problem = value_column_problem(field_name, header_set, reserved)
317
+ if problem:
318
+ issue(record.id, "Field", problem)
319
+ if not name:
320
+ issue(record.id, "Name", "Enter the allowed value.")
321
+ if not _trim(_cell(record.row, "Definition")):
322
+ issue(record.id, "Definition", "Explain what this value means.")
323
+ for field_name, group in value_groups(value_records, column).items():
324
+ if value_column_problem(field_name, header_set, reserved):
325
+ continue
326
+ rule = rules.setdefault(field_name, ValueRule(multiple=False))
327
+ claims: dict[str, Record] = {}
328
+ for record in group:
329
+ name = target(record)
330
+ if not name:
331
+ continue
332
+ owner = claims.get(name.lower())
333
+ if owner and owner is not record:
334
+ issue(
335
+ record.id, "Name", f"Another {field_name} value is named “{name}”."
336
+ )
337
+ continue
338
+ claims[name.lower()] = record
339
+ rule.values[name] = record
340
+ for record in group:
341
+ if not target(record):
342
+ continue
343
+ for alias in trace_ids(record.row.get("Aliases")):
344
+ owner = claims.get(alias.lower())
345
+ if owner and owner is not record:
346
+ issue(
347
+ record.id,
348
+ "Aliases",
349
+ f"“{alias}” is already a {field_name} value name or alias.",
350
+ )
351
+ continue
352
+ if not owner:
353
+ claims[alias.lower()] = record
354
+ rule.aliases[alias] = record
355
+ rules = {name: rule for name, rule in rules.items() if rule.values}
356
+ for record in every:
357
+ if record.type in ("Value", "Field"):
358
+ continue
359
+ for field_name, rule in rules.items():
360
+ cell = _trim(_cell(record.row, field_name))
361
+ if not cell:
362
+ continue
363
+ seen = set()
364
+ for part in trace_ids(cell) if rule.multiple else [cell]:
365
+ key = part.lower()
366
+ if key in seen:
367
+ issue(record.id, field_name, f"“{part}” is listed more than once.")
368
+ seen.add(key)
369
+ if part in rule.values:
370
+ continue
371
+ replaced = rule.aliases.get(part) or next(
372
+ (
373
+ owner
374
+ for alias, owner in rule.aliases.items()
375
+ if alias.lower() == key
376
+ ),
377
+ None,
378
+ )
379
+ canonical = replaced or next(
380
+ (
381
+ value
382
+ for value in rule.values.values()
383
+ if target(value).lower() == key
384
+ ),
385
+ None,
386
+ )
387
+ if canonical:
388
+ spelling = (
389
+ "a replaced spelling"
390
+ if replaced
391
+ else "spelled differently from the defined value"
392
+ )
393
+ issue(
394
+ record.id,
395
+ field_name,
396
+ f"“{part}” is {spelling}; use “{target(canonical)}”.",
397
+ )
398
+ else:
399
+ one = (
400
+ f" {field_name} allows one value."
401
+ if not rule.multiple and ";" in part
402
+ else ""
403
+ )
404
+ issue(
405
+ record.id,
406
+ field_name,
407
+ f"“{part}” is not a defined {field_name} value.{one}",
408
+ )
409
+ return rules
410
+
411
+
412
+ def link_records(
413
+ records: dict[str, Record], issue: Issue, controlled: Container[str] = frozenset()
414
+ ) -> list[Record]:
415
+ systems = []
416
+ for record in records.values():
417
+ record_id, row, record_type = record.id, record.row, record.type
418
+ if ";" in record_id:
419
+ issue(record_id, "ID", "A traceable ID cannot contain a semicolon.")
420
+ if record_type == "System":
421
+ systems.append(record)
422
+ if record_type not in RECORD_TYPES:
423
+ issue(record_id, "Type", f"Unknown record type “{record_type}”.")
424
+ if (
425
+ record_type == "Requirement"
426
+ and _trim(_cell(row, "Origin")) == "Self-derived"
427
+ and not _trim(_cell(row, "Rationale"))
428
+ ):
429
+ issue(
430
+ record_id,
431
+ "Rationale",
432
+ "Explain why this self-derived requirement is needed.",
433
+ )
434
+ for field_name, (sources, _, single) in TRACE_FIELDS.items():
435
+ ids = trace_ids(row.get(field_name))
436
+ if not ids:
437
+ continue
438
+ if record_type not in sources:
439
+ issue(
440
+ record_id,
441
+ field_name,
442
+ f"{field_name} does not apply to {record_type} records.",
443
+ )
444
+ continue
445
+ if single and len(ids) > 1:
446
+ issue(record_id, field_name, "Select at most one target.")
447
+ seen, targets = set(), []
448
+ for target_id in ids:
449
+ if target_id in seen:
450
+ issue(record_id, field_name, f"Duplicate reference to {target_id}.")
451
+ continue
452
+ seen.add(target_id)
453
+ target = records.get(target_id)
454
+ expected = trace_target_types(field_name, record_type)
455
+ if target_id == record_id:
456
+ issue(record_id, field_name, "A record cannot link to itself.")
457
+ elif not target:
458
+ if field_name != "Affects" or target_id not in controlled:
459
+ issue(record_id, field_name, f"Target {target_id} is missing.")
460
+ elif target.type not in expected:
461
+ issue(
462
+ record_id,
463
+ field_name,
464
+ f"Target {target_id} has type {target.type}. Expected {' or '.join(expected)}.",
465
+ )
466
+ else:
467
+ targets.append(target)
468
+ record.outgoing[field_name] = targets
469
+ return systems
470
+
471
+
472
+ def find_cycles(records: dict[str, Record], field_name: str, issue: Issue) -> None:
473
+ done: set[str] = set()
474
+ active: set[str] = set()
475
+ for start in records.values():
476
+ if start.id in done:
477
+ continue
478
+ stack = [(start, iter(start.outgoing.get(field_name, [])))]
479
+ active.add(start.id)
480
+ while stack:
481
+ record, targets = stack[-1]
482
+ target = next(targets, None)
483
+ if target is None:
484
+ active.discard(record.id)
485
+ done.add(record.id)
486
+ stack.pop()
487
+ elif target.id in active:
488
+ cycle = [
489
+ item.id
490
+ for item, _ in stack[
491
+ next(i for i, (item, _) in enumerate(stack) if item is target) :
492
+ ]
493
+ ]
494
+ issue(
495
+ record.id, field_name, f"Cycle: {' → '.join([*cycle, target.id])}."
496
+ )
497
+ elif target.id not in done:
498
+ active.add(target.id)
499
+ stack.append((target, iter(target.outgoing.get(field_name, []))))
500
+
501
+
502
+ def check_changes(
503
+ rows: list[dict[str, str]],
504
+ id_column: str,
505
+ records: dict[str, Record],
506
+ values: dict[str, ValueRule],
507
+ headers: list[str],
508
+ controls: Controls,
509
+ issue: Issue,
510
+ ) -> None:
511
+ baselines, controlled = controls["baselines"], controls["records"]
512
+ authorised = {
513
+ target
514
+ for record in records.values()
515
+ if record.type == "Change request"
516
+ and verification_state(values, "Change status", record.row.get("Change status"))
517
+ in APPROVED_CHANGE_STATES
518
+ for target in trace_ids(record.row.get("Affects"))
519
+ }
520
+ scopes = [
521
+ {
522
+ _trim(_cell(row, id_column))
523
+ for row in select_scope(
524
+ rows, id_column, baseline["system"], baseline["includes"]
525
+ )
526
+ }
527
+ for baseline in baselines
528
+ ]
529
+ for row in rows:
530
+ record_id = _trim(_cell(row, id_column))
531
+ if record_id in authorised:
532
+ continue
533
+ if record_id in controlled:
534
+ name, old = controlled[record_id]["baseline"], controlled[record_id]["row"]
535
+ baseline = next(item for item in baselines if item["name"] == name)
536
+ ignored = () if baseline["capturesStatus"] else baseline["statusColumns"]
537
+ changed = [
538
+ column
539
+ for column in dict.fromkeys([*headers, *old, *row])
540
+ if column not in ignored and _cell(old, column) != _cell(row, column)
541
+ ]
542
+ if changed:
543
+ columns = ", ".join(changed)
544
+ issue(
545
+ record_id,
546
+ changed[0],
547
+ f"Changed since {name} ({columns}) without an approved change request.",
548
+ )
549
+ continue
550
+ latest = None
551
+ for baseline, scope in zip(baselines, scopes):
552
+ if record_id in scope and (
553
+ latest is None or baseline["created"] > latest["created"]
554
+ ):
555
+ latest = baseline
556
+ if latest:
557
+ issue(
558
+ record_id,
559
+ id_column,
560
+ f"Added since {latest['name']} without an approved change request.",
561
+ )
562
+ present = {_trim(_cell(row, id_column)) for row in rows}
563
+ for record_id, entry in controlled.items():
564
+ if record_id not in present and record_id not in authorised:
565
+ issue(
566
+ record_id,
567
+ id_column,
568
+ f"Removed since {entry['baseline']} without an approved change request.",
569
+ )
570
+
571
+
572
+ def trace_findings(
573
+ rows: list[dict[str, str]],
574
+ headers: list[str],
575
+ id_column: str,
576
+ controls: Controls | None = None,
577
+ ) -> list[Finding]:
578
+ """Return the findings buildTraceIndex reports for these rows, in the same order.
579
+
580
+ With controls, the change-control findings follow the others (tests/fixtures/change_control.json).
581
+ """
582
+ findings: list[Finding] = []
583
+
584
+ def issue(record_id: str | None, field_name: str, message: str) -> None:
585
+ findings.append(Finding(record_id, field_name, message))
586
+
587
+ records: dict[str, Record] = {}
588
+ for row in rows:
589
+ record_id = _trim(_cell(row, id_column))
590
+ records[record_id] = Record(record_id, row, trace_type(row))
591
+ systems = link_records(
592
+ records, issue, controls["records"] if controls else frozenset()
593
+ )
594
+ roots = [system for system in systems if not trace_ids(system.row.get("Part of"))]
595
+ if len(roots) != 1:
596
+ issue(None, "Part of", f"Expected one top-level system. Found {len(roots)}.")
597
+ for field_name in ("Part of", "Derived from"):
598
+ find_cycles(records, field_name, issue)
599
+ build_glossary(records.values(), issue)
600
+ values = build_value_rules(records.values(), headers, id_column, issue)
601
+ check_test_coverage(records, values, issue)
602
+ check_verification(records, values, issue)
603
+ if controls is not None:
604
+ check_changes(rows, id_column, records, values, headers, controls, issue)
605
+ return findings
@@ -0,0 +1,84 @@
1
+ """Gate readiness check evaluator shared by the report and its Node tests."""
2
+
3
+ GATE_CHECKS_JS = r"""
4
+ const gateFindingIds=new WeakMap();
5
+ const gateConditions={
6
+ filled:{columns:spec=>[spec],passes:(row,spec,context)=>context.cell(row,spec)!==""},
7
+ in:{columns:spec=>Object.keys(spec),passes:(row,spec,context)=>Object.entries(spec).every(([column,allowed])=>{
8
+ const parts=context.parts(context.cell(row,column));
9
+ return parts.length>0&&parts.every(part=>allowed.includes(part));
10
+ })},
11
+ linked:{columns:spec=>[spec],passes:(row,spec,context,check)=>context.parts(context.cell(row,spec)).some(target=>
12
+ check.leaf?context.leaves.has(target):context.records.has(target))},
13
+ no_findings:{columns:()=>[],passes:(row,spec,context)=>!context.findingIds.has(context.id(row))},
14
+ unchanged_since:{columns:()=>[]},
15
+ };
16
+ const changeFindingMessage=/^(?:Changed|Added|Removed) since .+ without an approved change request\.$/;
17
+ function gateCondition(check){
18
+ const name=Object.keys(gateConditions).find(key=>key in check);
19
+ return name?{name,spec:check[name]}:null;
20
+ }
21
+ function gateAppliesTo(check,context){
22
+ const spec=check.applies_to??(check.no_findings||check.unchanged_since?{}:{type:"Requirement"});
23
+ const accepted=value=>[].concat(value);
24
+ return {
25
+ columns:Object.keys(spec).filter(key=>key!=="type"),
26
+ matches:row=>Object.entries(spec).every(([key,value])=>
27
+ accepted(value).includes(key==="type"?context.cell(row,"Type")||"Requirement":context.cell(row,key))),
28
+ };
29
+ }
30
+ function evaluateGateCheck(check,rows,idColumn,headers,system,findings,controls=null){
31
+ const cell=(row,column)=>String(row[column]??"").trim(),parts=text=>text.split(";").map(part=>part.trim()).filter(Boolean);
32
+ const id=row=>cell(row,idColumn);
33
+ const base={id:check.id,description:check.description,enforcement:check.enforcement??"advisory"};
34
+ const condition=gateCondition(check);
35
+ if(!condition)return {...base,error:"The check has no condition."};
36
+ const context={cell,parts,id};
37
+ const applies=gateAppliesTo(check,context);
38
+ const missing=[...applies.columns,...gateConditions[condition.name].columns(condition.spec)].find(column=>!headers.includes(column));
39
+ if(missing!==undefined)return {...base,error:`The table has no “${missing}” column.`};
40
+ const systems=rows.filter(row=>(cell(row,"Type")||"Requirement")==="System");
41
+ const parents=new Set(systems.flatMap(row=>parts(cell(row,"Part of"))));
42
+ context.records=new Set(rows.map(id));
43
+ context.leaves=new Set(systems.map(id).filter(systemId=>!parents.has(systemId)));
44
+ if(!gateFindingIds.has(findings))gateFindingIds.set(findings,new Set(findings.filter(finding=>finding.id!=null).map(finding=>finding.id)));
45
+ context.findingIds=gateFindingIds.get(findings);
46
+ let scoped=rows;
47
+ if(system){
48
+ const types=[...new Set(rows.map(row=>cell(row,"Type")||"Requirement"))],scope=baselineScopeIds(rows,idColumn,system,types);
49
+ scoped=rows.filter(row=>scope.has(id(row)));
50
+ }
51
+ const applicable=scoped.filter(applies.matches),{passes}=gateConditions[condition.name];
52
+ if(condition.name==="unchanged_since"){
53
+ let removed=Object.keys(controls?.records??{}).filter(removedId=>!context.records.has(removedId));
54
+ if(system&&removed.length){
55
+ const all=[...rows,...removed.map(removedId=>controls.records[removedId].row)];
56
+ const scope=baselineScopeIds(all,idColumn,system,[...new Set(all.map(row=>cell(row,"Type")||"Requirement"))]);
57
+ removed=removed.filter(removedId=>scope.has(removedId));
58
+ }
59
+ const counted=new Set([...applicable.map(id),...removed]);
60
+ const changed=[...new Set(findings.filter(finding=>changeFindingMessage.test(finding.message)).map(finding=>finding.id))];
61
+ return {...base,total:applicable.length+removed.length,failing:changed.filter(changedId=>counted.has(changedId)),global:[],error:null};
62
+ }
63
+ const failing=applicable.filter(row=>!passes(row,condition.spec,context,check)).map(id),messages=[];
64
+ if(condition.name==="no_findings")messages.push(...findings.filter(finding=>finding.id==null).map(finding=>finding.message));
65
+ return {...base,total:applicable.length+(messages.length?1:0),failing:messages.length?[...failing,""]:failing,global:messages,error:null};
66
+ }
67
+ function gateCheckFailed(result){
68
+ return !!result.error||result.failing.length>0;
69
+ }
70
+ function gateCheckBlocks(result){
71
+ return result.enforcement==="strict"&&gateCheckFailed(result);
72
+ }
73
+ function gateCheckLabel(result){
74
+ const level=result.enforcement==="strict"?"blocking":"advisory";
75
+ if(result.error)return {tone:"gate-error",mark:result.enforcement==="strict"?"! Error · blocking":"! Error"};
76
+ if(!gateCheckFailed(result))return {tone:"gate-pass",mark:"✓ Pass"};
77
+ return {tone:level==="blocking"?"gate-fail":"gate-advisory",mark:`✗ Fail · ${level}`};
78
+ }
79
+ function gateBadgeLabel(results){
80
+ if(results.some(gateCheckBlocks))return {tone:"gate-fail",mark:"✗ Blocked"};
81
+ if(results.some(gateCheckFailed))return {tone:"gate-advisory",mark:"✗ Not ready"};
82
+ return {tone:"gate-pass",mark:"✓ Ready"};
83
+ }
84
+ """