File size: 7,169 Bytes
c5cf11b
 
 
 
 
156e872
 
e6e90f2
156e872
c5cf11b
c612049
c5cf11b
 
c612049
 
c5cf11b
 
 
01274b0
dead914
 
c5cf11b
 
 
01274b0
 
 
63e3e38
cb3668d
156e872
9bec131
8c62083
e6e90f2
9bec131
01274b0
 
 
 
c5cf11b
d249bbb
 
c5cf11b
 
 
 
 
c612049
 
c5cf11b
 
 
 
 
eeb144a
 
c5cf11b
 
 
 
 
d249bbb
 
 
 
 
 
 
c5cf11b
 
 
 
 
 
eeb144a
c5cf11b
eeb144a
c5cf11b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c612049
 
 
 
 
 
eeb144a
c612049
 
 
 
 
 
 
 
 
 
 
 
 
c5cf11b
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
"""Pinned S1/S2 source migration; only unchanged historical debt may survive."""
import difflib
import hashlib
import json
from pathlib import Path
from scripts.backend_paths import (
    carry_catalog_coverage, carry_stream_override_coverage, carry_upstream_coverage, reviewed_source,
    require_audit_reader_assets_full_coverage, require_audit_seo_full_coverage, require_audit_upstream_full_coverage,
)

APP_SHA = '769155429400702a51524f3d2cf2d6dee19b26f67ea08a1f9db22aec70fa61ef'
STREAMS_SHA = '02c6df15f5058d322042f99b1e8cd454380b5059c7ac9815cddf49bbf3296d6a'
TRANSPORT_SHA = '9c46e4c4fc1803ab3ff976b1694c27510fb92e2ed162043bf7495d8900c87ad1'
CATALOG_PARENT_SHA = 'e0aeeb62753839ded9d749c88739f730a8657645ecbf81ea63574ddc92e9ccd0'
CATALOG_SHA = '114424d45ebf5c45b9c28a24f0d09bca3e72fc97349b81bef58d9e4d028dde41'


def transport_layout(root: Path) -> bool:
    from scripts.check_refactor_coverage import refactor_layout
    return (refactor_layout(root)
            or hashlib.sha256((root / 'backend/app.py').read_bytes()).hexdigest() == APP_SHA)


def transport_baseline(root: Path, baseline: dict) -> dict:
    from scripts.check_refactor_coverage import refactor_layout, migrate_refactor_baseline
    if refactor_layout(root):
        parent = root / 'scripts/coverage-anchors/refactor-parent-app.py.snapshot'
        previous = migrate_refactor_baseline(root, _transport_baseline(root, baseline, parent))
        previous = carry_catalog_coverage(root, previous)
        previous = carry_upstream_coverage(root, previous)
        previous = carry_stream_override_coverage(root, previous)
        previous = require_audit_upstream_full_coverage(root, previous)
        previous = require_audit_reader_assets_full_coverage(root, previous)
        return require_audit_seo_full_coverage(root, previous)
    return _transport_baseline(root, baseline, root / 'backend/app.py')


def _transport_baseline(root: Path, baseline: dict, app_source: Path) -> dict:
    from scripts.check_backend_coverage import (LIFESPAN_APP_SHA, LIFESPAN_PARENT_DEBT_SHA,
                                               PROXY_SAFETY_NEW_STREAMS_SHA, migrate_lifespan_baseline,
                                               migrate_ci_baseline_to_lifespan_parent, coverage_debt_errors)
    anchors = root / 'scripts/coverage-anchors'
    expected = {
        'backend/app.py': APP_SHA,
        'backend/proxy_streams.py': STREAMS_SHA,
        'backend/search_transport.py': TRANSPORT_SHA,
        'backend/reader_v3_catalog.py': CATALOG_SHA,
        'scripts/coverage-anchors/catalog-parent.py.snapshot': CATALOG_PARENT_SHA,
        'scripts/coverage-anchors/transport-parent-app.py.snapshot': LIFESPAN_APP_SHA,
        'scripts/coverage-anchors/proxy-safety-new-streams.py.snapshot': PROXY_SAFETY_NEW_STREAMS_SHA,
        'scripts/coverage-anchors/lifespan-parent-backend-coverage-debt.json': LIFESPAN_PARENT_DEBT_SHA,
    }
    for name, digest in expected.items():
        data = app_source.read_bytes() if name == 'backend/app.py' else reviewed_source(root, name)
        if hashlib.sha256(data).hexdigest() != digest:
            raise ValueError(f'transport coverage requires exact reviewed source: {name}')
    pinned = json.loads((anchors / 'lifespan-parent-backend-coverage-debt.json').read_text())
    # The stored debt stays in its independently pinned historical coordinates.
    # No current report is ever used to create or expand missing coverage debt.
    if baseline != pinned:
        historical = migrate_ci_baseline_to_lifespan_parent(root, baseline)
        errors = coverage_debt_errors(pinned, historical)
        if errors:
            raise ValueError('\n'.join(errors))
        # The independently pinned parent includes later coverage improvements.
        # Adopt it only after proving it retains the entire translated CI ratchet.
        baseline = pinned
    result = migrate_lifespan_baseline(baseline, anchors / 'lifespan-parent-app.py.snapshot',
                                      anchors / 'transport-parent-app.py.snapshot')
    for name, old in (
        ('backend/app.py', anchors / 'transport-parent-app.py.snapshot'),
        ('backend/proxy_streams.py', anchors / 'proxy-safety-new-streams.py.snapshot'),
    ):
        data = app_source.read_bytes() if name == 'backend/app.py' else reviewed_source(root, name)
        operations = difflib.SequenceMatcher(None, old.read_text().splitlines(),
                                             data.decode().splitlines(), autojunk=False).get_opcodes()
        mapping = {i + 1: j + 1 + i - start for op, start, end, j, _ in operations
                   if op == 'equal' for i in range(start, end)}
        def line(n):
            return (-1 if n < 0 else 1) * mapping[abs(n)]
        entry = result[name]
        # Changed statements/arcs leave the old debt inventory and therefore
        # must all execute. Every old missing coordinate must map unchanged.
        missing_lines = [line(n) for n in entry['missing_lines']]
        missing_arcs = [[line(a), line(b)] for a, b in entry['missing_arcs']]
        lines = [line(n) for n in entry['lines'] if abs(n) in mapping]
        arcs = [[line(a), line(b)] for a, b in entry['arcs'] if abs(a) in mapping and abs(b) in mapping]
        if name == 'backend/proxy_streams.py':
            # False branches now enter the added cleanup-stage bookkeeping.
            redirects = {(48, 53): (48, 51), (133, 136): (133, 135), (238, 242): (238, 240)}
            arcs = [list(redirects.get(tuple(arc), tuple(arc))) for arc in arcs]
        result[name] = {'lines': sorted(lines), 'arcs': sorted(arcs),
                        'missing_lines': sorted(missing_lines), 'missing_arcs': sorted(missing_arcs),
                        'covered_lines': len(lines), 'covered_branches': len(arcs)}
    # Catalog debt belongs to the parent layout, not the current coordinates.
    # Only identical lines carry debt; changed statements and crossing arcs
    # remain outside the inventory and must therefore be fully exercised.
    name = 'backend/reader_v3_catalog.py'
    old = anchors / 'catalog-parent.py.snapshot'
    operations = difflib.SequenceMatcher(None, old.read_text().splitlines(),
                                         reviewed_source(root, name).decode().splitlines(), autojunk=False).get_opcodes()
    mapping = {i + 1: j + 1 + i - start for op, start, end, j, _ in operations
               if op == 'equal' for i in range(start, end)}
    def translate(n):
        return (-1 if n < 0 else 1) * mapping[abs(n)]
    entry = result[name]
    missing_lines = [translate(n) for n in entry['missing_lines']]
    missing_arcs = [[translate(a), translate(b)] for a, b in entry['missing_arcs']]
    lines = [translate(n) for n in entry['lines'] if abs(n) in mapping]
    arcs = [[translate(a), translate(b)] for a, b in entry['arcs']
            if abs(a) in mapping and abs(b) in mapping]
    result[name] = {'lines': sorted(lines), 'arcs': sorted(arcs),
                    'missing_lines': sorted(missing_lines), 'missing_arcs': sorted(missing_arcs),
                    'covered_lines': len(lines), 'covered_branches': len(arcs)}
    return result