diff --git a/docs/adr_unified_loop_backend.md b/docs/adr_unified_loop_backend.md index d1827e6..8a0b2c4 100644 --- a/docs/adr_unified_loop_backend.md +++ b/docs/adr_unified_loop_backend.md @@ -119,12 +119,17 @@ Production characterization records 2,720 regular physical faces plus a band. D2 is therefore a closed-proof scope, while D2b determines whether and how the primary workload crosses the eventual generic production seam. -The legacy 11-control setup predicate currently admits faces whose three -corner valences are 5/5/5. The matrix construction describes one valence-5 -corner and two valence-6 corners (5/6/6). The predicate also declares `d4`, -`d7`, and `d8` before branches intended to identify the extraordinary corner. -This is a confirmed topology/implementation mismatch. D5 governs changing -it; WP0.1 only records it. +The legacy 11-control setup predicate admits faces whose three corner valences +are 5/5/5. The matrix construction describes one valence-5 corner and two +valence-6 corners (5/6/6); that retained matrix mismatch remains a confirmed +defect witness owned by D5. The classifier repair is owned separately by +WP1.1a. The inventory records whether the checked-out +`src/mesh/Mesh_setup_geometry.cpp` has total sentinel initialization for `d4`, +`d7`, and `d8` and whether rejection and its throw precede publication to +`face.adjacentVertices` and `face.oneRingVertices`; it does not treat an +unmerged branch object as current-tree state. The classifier repair record +neither quarantines the accepted all-Valence-5 fixture nor changes the matrix; +D5 still governs those decisions. ### OpenSubdiv provider policy diff --git a/scripts/inventory_unified_loop_baseline.py b/scripts/inventory_unified_loop_baseline.py index adeb751..90aebef 100644 --- a/scripts/inventory_unified_loop_baseline.py +++ b/scripts/inventory_unified_loop_baseline.py @@ -60,6 +60,25 @@ EXPECTED_VALENCE5_FACE_SOURCE_MAPPING_SHA256 = ( "9f5fe4e76a9815a806970164d4a5e02771c4350a6c1047ceb7ce3e86cd2acd1a") +# Reviewed active-token shape of the WP1.1a classifier: each count includes +# the one direct sentinel declaration plus every later use in the function. +# Exact cardinality is intentional and fail-closed: any additional active +# occurrence of one of these identifiers invalidates the observation without +# trying to recognize the full C++ declaration grammar. +REVIEWED_CLASSIFIER_SENTINEL_IDENTIFIER_COUNTS = { + "d4": 6, + "d7": 7, + "d8": 7, +} +# SHA256 of the normalized active contents of Mesh_setup_geometry.cpp in the +# reviewed WP1.1a source. This is a content contract, not a commit identity. +# It includes whitespace-normalized non-literal code, ordered active directive +# logical lines, active include operands, ordered exact active literal tokens, +# and their canonical placement among non-literal segments after the narrowly +# permitted literal-#if-0 removal performed below. +REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256 = ( + "ad05d22b1d0fcadb1d1f4a80e7f4d49cbcc38dd66b9914a340229d88ef391e98") + EXPECTED_FACES = { "valence3_tetrahedron": [ [0, 2, 1], [0, 1, 3], [0, 3, 2], [1, 2, 3] @@ -476,79 +495,172 @@ def _all_present(text: str, anchors: list[str]) -> bool: return all(anchor in text for anchor in anchors) -def _cpp_code(text: str) -> str: - """Mask C++ comments and ordinary/raw literals, preserving positions.""" - text = re.sub(r"\\\r?\n", "", text) - masked = list(text) +def _cpp_lexical_surfaces( + text: str, build_phase_three: bool = True + ) -> tuple[str, str, list[tuple[int, int, str]], bool, str]: + """Return phase-3 source, stable code, literals, status, and phase-3 code.""" + phase_one = text.replace("\r\n", "\n").replace("\r", "\n") + spliced = phase_one.replace("\\\n", "") + masked = list(spliced) + literals: list[tuple[int, int, str]] = [] + comment_spans: list[tuple[int, int]] = [] + complete = True + + def mask(start: int, end: int) -> None: + for cursor in range(start, end): + if spliced[cursor] not in "\r\n": + masked[cursor] = " " + + def identifier_suffix_end(start: int) -> int: + if (start >= len(spliced) or + not (spliced[start].isalpha() or spliced[start] == "_")): + return start + cursor = start + 1 + while (cursor < len(spliced) and + (spliced[cursor].isalnum() or spliced[cursor] == "_")): + cursor += 1 + return cursor + + def prefix_boundary(start: int) -> bool: + return (start == 0 or + not (spliced[start - 1].isalnum() or + spliced[start - 1] == "_")) + index = 0 - state = "code" - raw_start = re.compile( - r"(?:u8|[uUL])?R\"([^\s()\\]{0,16})\(") - while index < len(text): - current = text[index] - following = text[index + 1] if index + 1 < len(text) else "" - if state == "code": - raw = raw_start.match(text, index) - if raw and (index == 0 or not (text[index - 1].isalnum() or - text[index - 1] == "_")): - closing = ")" + raw.group(1) + '"' - end = text.find(closing, raw.end()) - end = len(text) if end < 0 else end + len(closing) - for cursor in range(index, end): - if text[cursor] != "\n": - masked[cursor] = " " - index = end - continue - if current == "/" and following == "/": - masked[index] = masked[index + 1] = " " - index += 2 - state = "line_comment" - continue - if current == "/" and following == "*": - masked[index] = masked[index + 1] = " " - index += 2 - state = "block_comment" - continue - if current in ('"', "'"): - masked[index] = " " - state = "string" if current == '"' else "character" - index += 1 - continue - elif state == "line_comment": - if current == "\n": - state = "code" - else: - masked[index] = " " - index += 1 + raw_prefixes = ("u8R\"", "uR\"", "UR\"", "LR\"", "R\"") + ordinary_prefixes = ("u8", "u", "U", "L", "") + while index < len(spliced): + following = spliced[index + 1] if index + 1 < len(spliced) else "" + if spliced[index] == "/" and following == "/": + end = spliced.find("\n", index + 2) + end = len(spliced) if end < 0 else end + comment_spans.append((index, end)) + mask(index, end) + index = end continue - elif state == "block_comment": - if current == "*" and following == "/": - masked[index] = masked[index + 1] = " " - index += 2 - state = "code" - continue - if current != "\n": - masked[index] = " " - index += 1 + if spliced[index] == "/" and following == "*": + closing = spliced.find("*/", index + 2) + if closing < 0: + complete = False + end = len(spliced) + else: + end = closing + 2 + comment_spans.append((index, end)) + mask(index, end) + index = end continue - else: - if current != "\n": - masked[index] = " " - if current == "\\" and following: - if following != "\n": - masked[index + 1] = " " - index += 2 + + raw_prefix = next( + (prefix for prefix in raw_prefixes + if spliced.startswith(prefix, index) and prefix_boundary(index)), + None) + if raw_prefix is not None: + delimiter_start = index + len(raw_prefix) + opening = spliced.find( + "(", delimiter_start, delimiter_start + 17) + delimiter = ( + spliced[delimiter_start:opening] if opening >= 0 else "") + delimiter_is_valid = ( + opening >= 0 + and len(delimiter) <= 16 + and not any(character.isspace() or character in "()\\" + for character in delimiter)) + if delimiter_is_valid: + closing_token = ")" + delimiter + '"' + closing = spliced.find(closing_token, opening + 1) + if closing < 0: + complete = False + literal_end = len(spliced) + else: + literal_end = closing + len(closing_token) + token_end = identifier_suffix_end(literal_end) + literals.append( + (index, token_end, spliced[index:token_end])) + mask(index, literal_end) + index = token_end continue - if ((state == "string" and current == '"') or - (state == "character" and current == "'")): - state = "code" - index += 1 + + ordinary = None + for prefix in ordinary_prefixes: + quote_index = index + len(prefix) + if (quote_index < len(spliced) + and (not prefix or spliced.startswith(prefix, index)) + and spliced[quote_index] in {'"', "'"} + and (not prefix or prefix_boundary(index)) + and not (not prefix and spliced[quote_index] == "'" + and quote_index > 0 + and quote_index + 1 < len(spliced) + and spliced[quote_index - 1].isalnum() + and spliced[quote_index + 1].isalnum())): + ordinary = (prefix, quote_index, spliced[quote_index]) + break + if ordinary is not None: + _, quote_index, quote = ordinary + cursor = quote_index + 1 + closed = False + while cursor < len(spliced): + if spliced[cursor] == "\\": + cursor += 2 + continue + if spliced[cursor] == quote: + cursor += 1 + closed = True + break + if spliced[cursor] in "\r\n": + complete = False + break + cursor += 1 + if not closed: + complete = False + literal_end = min(cursor, len(spliced)) + token_end = identifier_suffix_end(literal_end) + literals.append((index, token_end, spliced[index:token_end])) + # Preserve ordinary encoding prefixes in structural code, matching + # the historical masker; their exact spelling is also in the token. + mask(quote_index, literal_end) + index = max(token_end, index + 1) continue index += 1 - return "".join(masked) + stable_code = "".join(masked) + if not build_phase_three: + return (spliced, stable_code, literals, complete, stable_code) + + def phase_three_comments(source: str) -> str: + pieces: list[str] = [] + cursor = 0 + for start, end in comment_spans: + pieces.extend((source[cursor:start], " ")) + cursor = end + pieces.append(source[cursor:]) + return "".join(pieces) + + def phase_three_offset(position: int) -> int: + removed = 0 + for start, end in comment_spans: + if end > position: + break + removed += end - start - 1 + return position - removed + + phase_three_literals = [ + (phase_three_offset(start), phase_three_offset(end), token) + for start, end, token in literals + ] + return ( + phase_three_comments(spliced), + stable_code, + phase_three_literals, + complete, + phase_three_comments(stable_code), + ) + + +def _cpp_code(text: str) -> str: + """Mask C++ comments and ordinary/raw literals, preserving positions.""" + return _cpp_lexical_surfaces(text, build_phase_three=False)[1] -_CPP_DIRECTIVE_PREFIX = r"(?:#|%:|\?\?=)" +_CPP_DIRECTIVE_PREFIX = r"(?:#|%:)" _INCLUDE_GUARD_NAME = re.compile(r"[A-Z][A-Z0-9_]*_(?:H|HPP)") _REVIEWED_MESH_HEADER_INCLUDES = ( "", "", "", "", "", @@ -647,6 +759,39 @@ def _has_preprocessor_directive(code: str) -> bool: code, re.MULTILINE)) +def _has_active_define_or_undef_directive(code: str) -> bool: + """Report macro state unless it is inside a provable ``#if 0`` arm.""" + inactive_frames: list[tuple[bool, bool]] = [] + directive = re.compile( + rf"^\s*{_CPP_DIRECTIVE_PREFIX}\s*([A-Za-z_]\w*)\b") + for line in code.splitlines(): + match = directive.match(line) + if not match: + continue + name = match.group(1) + if name in {"if", "ifdef", "ifndef"}: + parent_inactive = ( + inactive_frames[-1][1] if inactive_frames else False) + expression = line[match.end():].strip() + definitely_zero = ( + name == "if" + and bool(re.fullmatch(r"(?:0|\(\s*0\s*\))", expression))) + inactive_frames.append( + (parent_inactive, parent_inactive or definitely_zero)) + elif name in {"elif", "else"} and inactive_frames: + parent_inactive, _ = inactive_frames[-1] + # Anything except the initial literal-#if-0 arm is potentially + # active; fail closed instead of evaluating preprocessor state. + inactive_frames[-1] = (parent_inactive, parent_inactive) + elif name == "endif": + if inactive_frames: + inactive_frames.pop() + elif (name in {"define", "undef"} + and not (inactive_frames and inactive_frames[-1][1])): + return True + return False + + def _has_nested_source_inclusion(code: str) -> bool: """Reject fragment expansion inside any scanned brace scope.""" directive = re.compile( @@ -681,6 +826,169 @@ def _mask_cpp_conditionals(code: str) -> str: return "".join(masked) +def _reviewed_active_source_contract( + text: str) -> tuple[str, str, bool]: + """Normalize the reviewed C++ source and report contract ambiguity. + + Only balanced outer ``#if 0``/``#if (0)`` blocks without a depth-one + alternative are ignored. All other conditionals and active macro state + are ambiguous. Directive logical lines, include operands, and ordered + exact literal tokens and their canonical placement among non-literal code + segments are retained because the general code normalization deliberately + erases line boundaries and masks literals. + """ + spliced, _, literal_tokens, lexically_complete, lexical = ( + _cpp_lexical_surfaces(text)) + def split_lf_lines(source: str) -> list[str]: + pieces = source.split("\n") + return ([piece + "\n" for piece in pieces[:-1]] + + ([pieces[-1]] if pieces[-1] else [])) + + raw_lines = split_lf_lines(spliced) + code_lines = split_lf_lines(lexical) + if len(raw_lines) != len(code_lines): + return ("", hashlib.sha256(b"").hexdigest(), False) + + horizontal_whitespace = r"[ \t\v\f]" + directive = re.compile( + rf"^{horizontal_whitespace}*{_CPP_DIRECTIVE_PREFIX}" + rf"{horizontal_whitespace}*([A-Za-z_]\w*)\b") + directive_start = re.compile( + rf"^{horizontal_whitespace}*{_CPP_DIRECTIVE_PREFIX}") + include_operand = re.compile( + rf'{horizontal_whitespace}*' + r'("(?:\\.|[^"\\])*"|<[^>\r\n]*>|[A-Za-z_]\w*)') + inactive_depth = 0 + inactive_has_depth_one_alternative = False + unambiguous = lexically_complete + active_lines: list[str] = [] + active_inclusions: list[tuple[str, str]] = [] + active_directives: list[str] = [] + inactive_spans: list[tuple[int, int]] = [] + line_offset = 0 + + for raw_line, code_line in zip(raw_lines, code_lines): + code_match = directive.match(code_line) + raw_match = directive.match(raw_line) + directive_matches_agree = ( + code_match is not None + and raw_match is not None + and code_match.span() == raw_match.span() + and code_match.group(1) == raw_match.group(1)) + if ((code_match is not None or raw_match is not None) + and not directive_matches_agree): + unambiguous = False + match = code_match if directive_matches_agree else None + name = match.group(1) if match else None + if inactive_depth: + if name in {"if", "ifdef", "ifndef"}: + inactive_depth += 1 + elif name in {"elif", "else"} and inactive_depth == 1: + inactive_has_depth_one_alternative = True + elif name == "endif": + inactive_depth -= 1 + if inactive_depth == 0: + unambiguous = ( + unambiguous + and not inactive_has_depth_one_alternative) + inactive_has_depth_one_alternative = False + active_lines.append("".join( + "\n" if character == "\n" else " " + for character in code_line)) + inactive_spans.append( + (line_offset, line_offset + len(code_line))) + line_offset += len(code_line) + continue + + if match and name == "if": + expression = raw_line[match.end():].rstrip("\n").strip( + " \t\v\f") + if re.fullmatch( + r"(?:0|\([ \t\v\f]*0[ \t\v\f]*\))", + expression): + inactive_depth = 1 + active_lines.append("".join( + "\n" if character == "\n" else " " + for character in code_line)) + inactive_spans.append( + (line_offset, line_offset + len(code_line))) + line_offset += len(code_line) + continue + if name in {"if", "ifdef", "ifndef", "elif", "else", "endif", + "define", "undef"}: + unambiguous = False + code_directive_start = directive_start.match(code_line) + raw_directive_start = directive_start.match(raw_line) + directive_starts_agree = ( + code_directive_start is not None + and raw_directive_start is not None + and code_directive_start.span() == raw_directive_start.span()) + if ((code_directive_start is not None or + raw_directive_start is not None) + and not directive_starts_agree): + unambiguous = False + if directive_starts_agree: + directive_code = code_line.rstrip("\r\n") + active_directives.append( + re.sub(r"[ \t\f\v]+", " ", directive_code).strip( + " \t\f\v")) + if match and name in {"include", "include_next", "import"}: + operand = include_operand.match(raw_line[match.end():]) + if operand is None: + unambiguous = False + else: + active_inclusions.append((name, operand.group(1))) + active_lines.append(code_line) + line_offset += len(code_line) + + if inactive_depth: + unambiguous = False + active = "".join(active_lines) + active_literal_spans = [ + (start, end, token) + for start, end, token in literal_tokens + if not any(span_start <= start < span_end + for span_start, span_end in inactive_spans) + ] + active_literals = [token for _, _, token in active_literal_spans] + literal_placement_surface: list[list[str]] = [] + literal_cursor = 0 + for start, end, token in active_literal_spans: + if (start < literal_cursor or end < start or end > len(active) or + spliced[start:end] != token): + unambiguous = False + continue + literal_placement_surface.append([ + "code", + re.sub(r"[ \t\v\f\r\n]+", " ", + active[literal_cursor:start]), + ]) + literal_placement_surface.append(["literal", token]) + literal_cursor = end + literal_placement_surface.append([ + "code", + re.sub(r"[ \t\v\f\r\n]+", " ", active[literal_cursor:]), + ]) + # Preserve empty versus one normalized whitespace character at every + # literal boundary. Only whitespace outside the complete source surface is + # irrelevant to the contract. + literal_placement_surface[0][1] = ( + literal_placement_surface[0][1].lstrip(" \t\v\f\r\n")) + literal_placement_surface[-1][1] = ( + literal_placement_surface[-1][1].rstrip(" \t\v\f\r\n")) + normalized = json.dumps({ + "code_with_normalized_whitespace": re.sub( + r"[ \t\v\f\r\n]+", " ", active).strip( + " \t\v\f\r\n"), + "active_inclusions": active_inclusions, + "active_preprocessor_directives": active_directives, + "active_literal_tokens": active_literals, + "active_literal_placement_surface": literal_placement_surface, + }, sort_keys=True, separators=(",", ":")) + digest = hashlib.sha256(normalized.encode("utf-8")).hexdigest() + return (active, digest, unambiguous) + + def _direct_access_label(code: str, position: int): depth = 0 access = None @@ -695,7 +1003,7 @@ def _direct_access_label(code: str, position: int): return access, depth -def _unique_braced_scope(code: str, signature_pattern: str): +def _unique_braced_scope_span(code: str, signature_pattern: str): matches = list(re.finditer( signature_pattern, code, re.MULTILINE | re.DOTALL)) if len(matches) != 1: @@ -714,7 +1022,13 @@ def _unique_braced_scope(code: str, signature_pattern: str): cursor += 1 if depth: return None - return signature.start(), code[opening + 1:cursor - 1] + return (signature.start(), opening + 1, cursor - 1, + code[opening + 1:cursor - 1]) + + +def _unique_braced_scope(code: str, signature_pattern: str): + scope = _unique_braced_scope_span(code, signature_pattern) + return None if scope is None else (scope[0], scope[3]) def _direct_scope_matches(code: str, pattern: re.Pattern[str]): @@ -727,6 +1041,112 @@ def _direct_scope_matches(code: str, pattern: re.Pattern[str]): return direct +def _legacy_classifier_repair_observations(text: str) -> tuple[bool, bool]: + """Observe the active, structurally scoped WP1.1a classifier repair.""" + lexical = _cpp_code(text) + active, active_source_sha256, source_contract_is_unambiguous = ( + _reviewed_active_source_contract(text)) + active_source_contract_matches = ( + source_contract_is_unambiguous + and active_source_sha256 == + REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256) + macro_state_is_unambiguous = not ( + _has_active_define_or_undef_directive(lexical)) + classifier = _unique_braced_scope_span( + active, + r"\bLegacyOneRingClassification\s+Mesh::classify_legacy_one_ring\s*" + r"\(\s*const\s+Face\s*&\s*face\s*\)\s*const\s*\{") + sentinel_patterns = [ + re.compile(rf"\bint\s+{name}\s*=\s*-\s*1\s*;") + for name in ("d4", "d7", "d8") + ] + sentinel_initialization_observed = False + if classifier is not None: + classifier_body = classifier[3] + sentinel_initialization_observed = ( + active_source_contract_matches + and macro_state_is_unambiguous + and all( + len(pattern.findall(active)) == 1 + and len(_direct_scope_matches(classifier_body, pattern)) == 1 + and len(re.findall(rf"\b{re.escape(name)}\b", + classifier_body)) == + REVIEWED_CLASSIFIER_SENTINEL_IDENTIFIER_COUNTS[name] + for name, pattern in zip( + ("d4", "d7", "d8"), sentinel_patterns)) + ) + + publication = _unique_braced_scope_span( + active, + r"\bvoid\s+Mesh::set_one_ring_vertices_sorted\s*\(\s*\)\s*\{") + rejection_precedes_publication_observed = False + if publication is not None: + publication_body = publication[3] + preflight = _unique_braced_scope_span( + publication_body, + r"\bfor\s*\(\s*const\s+Face\s*&\s*face\s*:\s*faces\s*\)\s*\{") + publication_loop = _unique_braced_scope_span( + publication_body, + r"\bfor\s*\(\s*std::size_t\s+faceIndex\s*=\s*0\s*;\s*" + r"faceIndex\s*<\s*faces\s*\.\s*size\s*\(\s*\)\s*;\s*" + r"\+\+\s*faceIndex\s*\)\s*\{") + rejection_pattern = re.compile( + r"\bif\s*\(\s*is_legacy_one_ring_rejection\s*\(\s*" + r"classification\s*\.\s*reasonCode\s*\)\s*\)\s*\{") + throw_pattern = re.compile( + r"\bthrow\s+std::runtime_error\s*\([^;]+\)\s*;") + write_patterns = [ + re.compile( + r"\bfaces\s*\[\s*faceIndex\s*\]\s*\.\s*" + r"adjacentVertices\s*\.\s*swap\s*\("), + re.compile( + r"\bfaces\s*\[\s*faceIndex\s*\]\s*\.\s*" + r"oneRingVertices\s*\.\s*swap\s*\("), + ] + publication_field_patterns = [ + re.compile(r"\badjacentVertices\b"), + re.compile(r"\boneRingVertices\b"), + ] + if preflight is not None and publication_loop is not None: + preflight_is_direct = ( + publication_body[:preflight[0]].count("{") == + publication_body[:preflight[0]].count("}")) + publication_loop_is_direct = ( + publication_body[:publication_loop[0]].count("{") == + publication_body[:publication_loop[0]].count("}")) + rejection = _unique_braced_scope_span( + preflight[3], rejection_pattern.pattern) + rejection_is_direct = ( + rejection is not None and + preflight[3][:rejection[0]].count("{") == + preflight[3][:rejection[0]].count("}")) + direct_throws = ([] if rejection is None else + _direct_scope_matches(rejection[3], throw_pattern)) + direct_writes = [ + _direct_scope_matches(publication_loop[3], pattern) + for pattern in write_patterns + ] + rejection_precedes_publication_observed = ( + active_source_contract_matches + and macro_state_is_unambiguous + and preflight_is_direct + and publication_loop_is_direct + and rejection_is_direct + and len(rejection_pattern.findall(publication_body)) == 1 + and len(throw_pattern.findall(publication_body)) == 1 + and len(direct_throws) == 1 + and all(len(pattern.findall(active)) == 1 + for pattern in write_patterns) + and all(len(pattern.findall(publication_body)) == 1 + for pattern in publication_field_patterns) + and all(len(matches) == 1 for matches in direct_writes) + and preflight[2] < publication_loop[0] + and direct_writes[0][0].start() < direct_writes[1][0].start() + ) + return (sentinel_initialization_observed, + rejection_precedes_publication_observed) + + def _scope_begins_with(code: str, pattern: re.Pattern[str]) -> bool: """Require the named direct statement to be the scope's first code.""" match = pattern.search(code) @@ -1095,6 +1515,12 @@ def collect_inventory() -> dict[str, Any]: geometry = _text("src/mesh/Mesh.cpp") legacy_topology = _text("src/mesh/Mesh_setup_geometry.cpp") legacy_matrix = _text("src/mesh/Gauss_quadrature.cpp") + (sentinel_initialization_observed, + rejection_precedes_publication_observed) = ( + _legacy_classifier_repair_observations(legacy_topology)) + classifier_repair_confirmed = ( + sentinel_initialization_observed + and rejection_precedes_publication_observed) source_keyed_hpp = _text("include/energy_force/Source_keyed_kernel_call.hpp") source_keyed_cpp = _text("src/energy_force/Source_keyed_kernel_call.cpp") output = _text("src/io/output.cpp") @@ -1517,15 +1943,43 @@ def collect_inventory() -> dict[str, Any]: "legacy_11_control_predicate": { "admitted_corner_valences": [5, 5, 5], "matrix_intended_corner_valences": [5, 6, 6], - "defect_confirmed": _all_present(legacy_topology, [ - "vertices[node0].adjacentVertices.size() == 5", - "vertices[node1].adjacentVertices.size() == 5", - "vertices[node2].adjacentVertices.size() == 5", - "int d4, d7, d8;", - ]) and _all_present(legacy_matrix, [ - "const int N = 6;", "const int N1 = 5;", - "std::vector> SM4(11", - ]), + "legacy_11_control_matrix_defect_assertion": { + "owner": "D5", + "lifecycle": "retained_defect_witness", + "source_path": "src/mesh/Gauss_quadrature.cpp", + "required_witness_literals": [ + "const int N = 6;", + "const int N1 = 5;", + "std::vector> SM4(11", + ], + "defect_confirmed": _all_present(legacy_matrix, [ + "const int N = 6;", "const int N1 = 5;", + "std::vector> SM4(11", + ]), + }, + "wp1_1a_classifier_repair_record": { + "owner": "WP1.1a", + "lifecycle": "current_tree_observation", + "source_path": "src/mesh/Mesh_setup_geometry.cpp", + "required_sentinel_initializers": { + "d4": -1, + "d7": -1, + "d8": -1, + }, + "required_active_classifier_identifier_occurrences": + REVIEWED_CLASSIFIER_SENTINEL_IDENTIFIER_COUNTS, + "required_active_source_contract_sha256": + REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256, + "required_rejection_precedes_writes_to": [ + "face.adjacentVertices", + "face.oneRingVertices", + ], + "sentinel_initialization_observed": + sentinel_initialization_observed, + "rejection_precedes_publication_observed": + rejection_precedes_publication_observed, + "repair_confirmed": classifier_repair_confirmed, + }, }, } @@ -1946,12 +2400,75 @@ def require(condition: bool, message: str) -> None: d["valence5_icosahedron"]["faces"], d["valence5_icosahedron"]["valence"]) == (12, 20, 5), "valence5 topology summary drift") - require(d["legacy_11_control_predicate"]["admitted_corner_valences"] == [5, 5, 5], + legacy_11_control = d["legacy_11_control_predicate"] + require(set(legacy_11_control) == { + "admitted_corner_valences", + "matrix_intended_corner_valences", + "legacy_11_control_matrix_defect_assertion", + "wp1_1a_classifier_repair_record", + }, "legacy 11-control owner/lifecycle split schema drift") + require(legacy_11_control["admitted_corner_valences"] == [5, 5, 5], "legacy predicate classification drift") - require(d["legacy_11_control_predicate"]["matrix_intended_corner_valences"] == [5, 6, 6], + require(legacy_11_control["matrix_intended_corner_valences"] == [5, 6, 6], "legacy matrix classification drift") - require(d["legacy_11_control_predicate"]["defect_confirmed"], - "legacy 11-control defect anchor missing") + matrix_defect = legacy_11_control[ + "legacy_11_control_matrix_defect_assertion"] + require(matrix_defect == { + "owner": "D5", + "lifecycle": "retained_defect_witness", + "source_path": "src/mesh/Gauss_quadrature.cpp", + "required_witness_literals": [ + "const int N = 6;", + "const int N1 = 5;", + "std::vector> SM4(11", + ], + "defect_confirmed": True, + }, "legacy 11-control matrix defect witness drift") + classifier_repair = legacy_11_control[ + "wp1_1a_classifier_repair_record"] + require(set(classifier_repair) == { + "owner", + "lifecycle", + "source_path", + "required_sentinel_initializers", + "required_active_classifier_identifier_occurrences", + "required_active_source_contract_sha256", + "required_rejection_precedes_writes_to", + "sentinel_initialization_observed", + "rejection_precedes_publication_observed", + "repair_confirmed", + }, "WP1.1a legacy classifier repair schema drift") + require(classifier_repair["owner"] == "WP1.1a" and + classifier_repair["lifecycle"] == "current_tree_observation" and + classifier_repair["source_path"] == + "src/mesh/Mesh_setup_geometry.cpp" and + classifier_repair["required_sentinel_initializers"] == { + "d4": -1, "d7": -1, "d8": -1} and + classifier_repair[ + "required_active_classifier_identifier_occurrences"] == + REVIEWED_CLASSIFIER_SENTINEL_IDENTIFIER_COUNTS and + classifier_repair["required_active_source_contract_sha256"] == + REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256 and + classifier_repair[ + "required_rejection_precedes_writes_to"] == [ + "face.adjacentVertices", + "face.oneRingVertices", + ], "WP1.1a legacy classifier repair contract drift") + classifier_observations = [ + classifier_repair["sentinel_initialization_observed"], + classifier_repair["rejection_precedes_publication_observed"], + classifier_repair["repair_confirmed"], + ] + classifier_observations_are_boolean = all( + type(value) is bool for value in classifier_observations) + require(classifier_observations_are_boolean, + "WP1.1a legacy classifier observation is not boolean") + if classifier_observations_are_boolean: + require(classifier_repair["repair_confirmed"] == ( + classifier_repair["sentinel_initialization_observed"] and + classifier_repair[ + "rejection_precedes_publication_observed"]), + "WP1.1a legacy classifier repair state is inconsistent") require(d["valence5_icosahedron"]["face_source_mapping_sha256"] == EXPECTED_VALENCE5_FACE_SOURCE_MAPPING_SHA256, "valence5 exact face-source mapping drift") diff --git a/tests/test_unified_loop_baseline_inventory.py b/tests/test_unified_loop_baseline_inventory.py index 62c6491..cc027b3 100644 --- a/tests/test_unified_loop_baseline_inventory.py +++ b/tests/test_unified_loop_baseline_inventory.py @@ -5,6 +5,7 @@ import copy import importlib.util import math +import re import unittest from pathlib import Path from unittest import mock @@ -273,6 +274,903 @@ def test_D_topology_face_order_count_valence_and_one_ring(self) -> None: self.assert_mutation_rejected( lambda r: r["D_topology_guards"]["legacy_11_control_predicate"] .update({"admitted_corner_valences": [5, 6, 6]})) + self.assert_mutation_rejected( + lambda r: r["D_topology_guards"]["legacy_11_control_predicate"] + ["legacy_11_control_matrix_defect_assertion"] + .update({"defect_confirmed": False})) + self.assert_mutation_rejected( + lambda r: r["D_topology_guards"]["legacy_11_control_predicate"] + ["wp1_1a_classifier_repair_record"] + .update({"defect_confirmed": True})) + self.assert_mutation_rejected( + lambda r: r["D_topology_guards"] + ["legacy_11_control_predicate"] + ["wp1_1a_classifier_repair_record"] + .update({"required_active_source_contract_sha256": "0" * 64})) + + current_repair = self.baseline["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (current_repair["sentinel_initialization_observed"], + current_repair["rejection_precedes_publication_observed"], + current_repair["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse( + INVENTORY.validate_inventory(self.baseline, check_adr=False)) + + original_text = INVENTORY._text + classifier_source = """ +LegacyOneRingClassification Mesh::classify_legacy_one_ring( + const Face &face) const +{ + int d4 = -1; + int d7 = -1; + int d8 = -1; + if (regular) + { + d4 = face.adjacentVertices[0]; + d7 = face.adjacentVertices[1]; + d8 = face.adjacentVertices[2]; + } + else if (candidate) + { + d4 = face.adjacentVertices[0]; + d7 = face.adjacentVertices[1]; + d8 = face.adjacentVertices[2]; + } + else + { + d4 = face.adjacentVertices[0]; + d7 = face.adjacentVertices[1]; + d8 = face.adjacentVertices[2]; + } + result.orientedFaceVertices = {d4, d7, d8}; + std::swap(d7, d8); + staged[3] = d4; + staged[6] = d7; + staged[7] = d8; + const char marker = 'R'; + const char *rawReason = + R"reason(INVALID_CORNER_VERTEX_INDEX)reason"; +} +""" + preflight_loop = """ + for (const Face &face : faces) + { + if (is_legacy_one_ring_rejection(classification.reasonCode)) + { + const std::string message = + "Legacy one-ring setup rejected face "; + const char *reasonCodeName = "INVALID_CORNER_VERTEX_INDEX"; + throw std::runtime_error(message + reasonCodeName); + } + } +""" + publication_loop = """ + for (std::size_t faceIndex = 0; + faceIndex < faces.size(); ++faceIndex) + { + faces[faceIndex].adjacentVertices.swap(orientedFaceVertices); + faces[faceIndex].oneRingVertices.swap(assembledOneRing); + } +""" + repaired_source = ( + '#include "mesh/Mesh.hpp"\n\n' + 'namespace repaired_fixture\n{\n}\n' + 'const char *reviewed_ready_state()\n' + '{\n' + ' return "READY_REGULAR";\n' + '}\n' + classifier_source + """ +void Mesh::set_one_ring_vertices_sorted() +{ +""" + preflight_loop + publication_loop + """ +} +""" + ) + _, repaired_contract_sha256, repaired_contract_is_unambiguous = ( + INVENTORY._reviewed_active_source_contract(repaired_source)) + self.assertTrue(repaired_contract_is_unambiguous) + self.assertEqual( + INVENTORY._reviewed_active_source_contract( + repaired_source.replace( + "int d4 = -1;", "int d4 = -1;", 1))[1], + repaired_contract_sha256, + "whitespace-only formatting changed the source contract", + ) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + repaired_source.replace( + "int d4 = -1;", "intd4 = -1;", 1))[1], + repaired_contract_sha256, + "distinct C++ tokenization collapsed to the same source contract", + ) + for equivalent_directive_source in ( + repaired_source.replace( + '#include "mesh/Mesh.hpp"', + '#include "mesh/Mesh.hpp"', 1), + repaired_source.replace( + '#include "mesh/Mesh.hpp"', + '#include "mesh/Mesh.hpp" // trailing comment', 1), + repaired_source.replace( + '#include "mesh/Mesh.hpp"', + '#include \\\n "mesh/Mesh.hpp"', 1)): + self.assertEqual( + INVENTORY._reviewed_active_source_contract( + equivalent_directive_source)[1], + repaired_contract_sha256, + "semantically equivalent directive formatting changed digest", + ) + literal_fixture = r''' +auto ordinary = u8"slash\\quote\""; +auto character = U'\x5a'; +auto raw = LR"tag(raw // /* " bytes)tag"_suffix; +// u8"comment literal" +/* R"(block comment literal)" */ +''' + _, _, literal_tokens, literal_fixture_is_complete, _ = ( + INVENTORY._cpp_lexical_surfaces(literal_fixture)) + self.assertTrue(literal_fixture_is_complete) + self.assertEqual( + [token for _, _, token in literal_tokens], + [r'u8"slash\\quote\""', r"U'\x5a'", + r'LR"tag(raw // /* " bytes)tag"_suffix'], + ) + + def collect_with_topology(source): + def source_text(path): + if path == "src/mesh/Mesh_setup_geometry.cpp": + return source + return original_text(path) + + with mock.patch.object( + INVENTORY, "_text", side_effect=source_text), \ + mock.patch.object( + INVENTORY, "_topology_invalidation_seam_errors", + return_value=[]), \ + mock.patch.object( + INVENTORY, + "REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256", + repaired_contract_sha256): + return INVENTORY.collect_inventory() + + def validate_topology(report): + with mock.patch.object( + INVENTORY, + "REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256", + repaired_contract_sha256): + return INVENTORY.validate_inventory(report, check_adr=False) + + def observe_topology(source): + with mock.patch.object( + INVENTORY, + "REVIEWED_MESH_SETUP_GEOMETRY_ACTIVE_SOURCE_SHA256", + repaired_contract_sha256): + return INVENTORY._legacy_classifier_repair_observations(source) + + repaired_report = collect_with_topology(repaired_source) + repaired_record = repaired_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (repaired_record["sentinel_initialization_observed"], + repaired_record["rejection_precedes_publication_observed"], + repaired_record["repair_confirmed"]), + (True, True, True), + ) + self.assertFalse(validate_topology(repaired_report)) + self.assertEqual( + repaired_record[ + "required_active_classifier_identifier_occurrences"], + {"d4": 6, "d7": 7, "d8": 7}, + ) + self.assertEqual( + repaired_record["required_active_source_contract_sha256"], + repaired_contract_sha256, + ) + tampered_contract = copy.deepcopy(repaired_report) + tampered_contract["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] \ + ["required_active_source_contract_sha256"] = "0" * 64 + self.assertTrue(validate_topology(tampered_contract)) + include_drift_report = collect_with_topology( + repaired_source.replace( + '"mesh/Mesh.hpp"', '"mesh/Other.hpp"', 1)) + include_drift_record = include_drift_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (include_drift_record["sentinel_initialization_observed"], + include_drift_record[ + "rejection_precedes_publication_observed"], + include_drift_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(include_drift_report)) + self.assert_mutation_rejected( + lambda r: r["D_topology_guards"] + ["legacy_11_control_predicate"] + ["wp1_1a_classifier_repair_record"] + ["required_active_classifier_identifier_occurrences"] + .update({"d4": 7})) + + rejection_line = ( + " if (is_legacy_one_ring_rejection(" + "classification.reasonCode))") + publication_mutations = [ + "faces[0].{field}.clear();", + "faces[0].{field}.push_back(0);", + "faces[0].{field} = {{}};", + "faces[0].{field}[0] = 0;", + ] + for field in ("adjacentVertices", "oneRingVertices"): + for mutation in publication_mutations: + extra_write_source = repaired_source.replace( + rejection_line, + " " + mutation.format(field=field) + "\n" + + rejection_line, + 1) + extra_write_report = collect_with_topology(extra_write_source) + extra_write_record = extra_write_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (extra_write_record["sentinel_initialization_observed"], + extra_write_record[ + "rejection_precedes_publication_observed"], + extra_write_record["repair_confirmed"]), + (False, False, False), + f"active preflight mutation escaped: {field} {mutation}", + ) + self.assertFalse(validate_topology(extra_write_report)) + + masked_mutations = [ + "#if 0\n" + " faces[0].adjacentVertices.clear();\n" + "#endif\n", + "#if (0)\n" + " faces[0].adjacentVertices.clear();\n" + "#endif\n", + "#if 0 /* reviewed inactive comment */\n" + " faces[0].adjacentVertices.clear();\n" + "#endif\n", + "#if ( /* reviewed inactive comment */ 0 )\n" + " faces[0].adjacentVertices.clear();\n" + "#endif\n", + " # if ( 0 )\n" + " faces[0].adjacentVertices.clear();\n" + " # endif\n", + "#if \\\n" + " 0\n" + " faces[0].adjacentVertices.clear();\n" + "#endif\n", + "#if 0\n" + " const auto hidden = R\"tag(inactive)tag\";\n" + "#endif\n", + "%:if 0\n" + " faces[0].adjacentVertices.clear();\n" + "%:endif\n", + " // faces[0].oneRingVertices.clear(); " + "\"ignored literal\" 'x' R\"(ignored raw)\"\n", + ] + for mutation in masked_mutations: + masked_write_report = collect_with_topology( + repaired_source.replace( + rejection_line, mutation + rejection_line, 1)) + masked_write_record = masked_write_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (masked_write_record["sentinel_initialization_observed"], + masked_write_record[ + "rejection_precedes_publication_observed"], + masked_write_record["repair_confirmed"]), + (True, True, True), + ) + self.assertFalse(validate_topology(masked_write_report)) + + duplicate_sentinel_report = collect_with_topology( + repaired_source.replace( + "int d4 = -1;", "int d4 = -1;\n int d4 = -1;", 1)) + duplicate_sentinel_record = duplicate_sentinel_report[ + "D_topology_guards"]["legacy_11_control_predicate"][ + "wp1_1a_classifier_repair_record"] + self.assertEqual( + (duplicate_sentinel_record["sentinel_initialization_observed"], + duplicate_sentinel_record[ + "rejection_precedes_publication_observed"], + duplicate_sentinel_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(duplicate_sentinel_report)) + + for name in ("d4", "d7", "d8"): + nested_declaration_source = repaired_source.replace( + f" int {name} = -1;", + f" int {name} = -1;\n" + f" if (nested) {{ int spare, {name}; }}", + 1) + nested_declaration_report = collect_with_topology( + nested_declaration_source) + nested_declaration_record = nested_declaration_report[ + "D_topology_guards"]["legacy_11_control_predicate"][ + "wp1_1a_classifier_repair_record"] + self.assertEqual( + (nested_declaration_record[ + "sentinel_initialization_observed"], + nested_declaration_record[ + "rejection_precedes_publication_observed"], + nested_declaration_record["repair_confirmed"]), + (False, False, False), + f"nested declaration escaped: {name}", + ) + self.assertFalse(validate_topology(nested_declaration_report)) + + for declaration in ( + f"int {name}(0);", + f"int {name}{{0}};", + f"decltype(0) {name};", + f"auto [{name}, spare] = pair;", + f"using {name} = int;"): + extra_identifier_source = repaired_source.replace( + f" int {name} = -1;", + f" int {name} = -1;\n" + f" if (nested) {{ {declaration} }}", + 1) + self.assertEqual( + observe_topology(extra_identifier_source), + (False, False), + f"active extra identifier escaped: {declaration}", + ) + + masked_identifier_sources = ( + repaired_source.replace( + f" int {name} = -1;", + f" int {name} = -1;\n" + f" // int {name}(0);", + 1), + repaired_source.replace( + f" int {name} = -1;", + f" int {name} = -1;\n" + f"#if 0\n int {name}(0);\n#endif", + 1), + ) + for masked_identifier_source in masked_identifier_sources: + self.assertEqual( + observe_topology(masked_identifier_source), + (True, True), + f"masked identifier affected observation: {name}", + ) + + macro_alias_source = ( + "#define ADJACENT_FIELD adjacentVertices\n" + "#define ONE_RING_FIELD oneRingVertices\n" + + repaired_source.replace( + rejection_line, + " faces[0].ADJACENT_FIELD.clear();\n" + " faces[0].ONE_RING_FIELD.clear();\n" + + rejection_line, + 1)) + macro_alias_report = collect_with_topology(macro_alias_source) + macro_alias_record = macro_alias_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (macro_alias_record["sentinel_initialization_observed"], + macro_alias_record[ + "rejection_precedes_publication_observed"], + macro_alias_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(macro_alias_report)) + + for masked_macro_prefix in ( + "// #define ADJACENT_FIELD adjacentVertices\n", + "#if 0\n#define ADJACENT_FIELD adjacentVertices\n#endif\n"): + self.assertEqual( + observe_topology(masked_macro_prefix + repaired_source), + (True, True), + "masked macro directive affected repair observation", + ) + + def assert_repair_state(source, expected, message): + report = collect_with_topology(source) + record = report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (record["sentinel_initialization_observed"], + record["rejection_precedes_publication_observed"], + record["repair_confirmed"]), + expected, + message, + ) + self.assertFalse(validate_topology(report)) + + for invalid_if_literal in ( + '"b0d_invalid_pp_expression"', + "'x'", + 'R"tag(b0d_invalid_pp_expression)tag"'): + masked_if_expression_source = ( + f"#if 0 {invalid_if_literal}\n" + "int b0d_hidden = does_not_compile;\n" + "#endif\n" + repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + masked_if_expression_source)[1], + repaired_contract_sha256, + "literal-bearing #if expression escaped digest", + ) + assert_repair_state( + masked_if_expression_source, + (False, False, False), + "literal-bearing #if expression escaped repair state", + ) + + for invalid_if_prefix_literal in ( + '"prefix"', + "'x'", + 'R"tag(prefix)tag"'): + fabricated_if_source = ( + f"{invalid_if_prefix_literal} #if 0\n" + "int b0d_active_breakage = does_not_compile;\n" + "#endif\n" + repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + fabricated_if_source)[1], + repaired_contract_sha256, + "prefix literal fabricated an inactive directive in digest", + ) + assert_repair_state( + fabricated_if_source, + (False, False, False), + "prefix literal fabricated an inactive directive", + ) + + literal_mutations = ( + ('"Legacy one-ring setup rejected face "', + '"Legacy one-ring setup rejected edge "'), + ('"INVALID_CORNER_VERTEX_INDEX"', + '"INVALID_CORNER_VERTEX_ID"'), + ("'R'", "'S'"), + ('R"reason(INVALID_CORNER_VERTEX_INDEX)reason"', + 'R"reason(INVALID_CORNER_VERTEX_ID)reason"'), + ) + for old_literal, new_literal in literal_mutations: + literal_mutation_source = repaired_source.replace( + old_literal, new_literal, 1) + self.assertNotEqual(literal_mutation_source, repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + literal_mutation_source)[1], + repaired_contract_sha256, + f"active literal mutation escaped digest: {old_literal}", + ) + assert_repair_state( + literal_mutation_source, + (False, False, False), + f"active literal mutation escaped repair state: {old_literal}", + ) + + literal_relocations = ( + ('return "READY_REGULAR";', + '"READY_REGULAR" return ;', + "ordinary string"), + ("const char marker = 'R';", + "'R' const char marker = ;", + "character"), + ('const char *rawReason =\n' + ' R"reason(INVALID_CORNER_VERTEX_INDEX)reason";', + 'R"reason(INVALID_CORNER_VERTEX_INDEX)reason" ' + 'const char *rawReason =\n' + ' ;', + "raw string"), + ) + for reviewed_literal_statement, relocated_statement, label in ( + literal_relocations): + relocated_source = repaired_source.replace( + reviewed_literal_statement, relocated_statement, 1) + self.assertNotEqual(relocated_source, repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + relocated_source)[1], + repaired_contract_sha256, + f"{label} relocation escaped literal placement digest", + ) + assert_repair_state( + relocated_source, + (False, False, False), + f"{label} relocation escaped repair state", + ) + + for equivalent_literal_spacing_source in ( + repaired_source.replace( + 'return "READY_REGULAR";', + 'return "READY_REGULAR";', 1), + repaired_source.replace( + 'return "READY_REGULAR";', + 'return/* comment\n\nspacing */"READY_REGULAR";', 1)): + self.assertEqual( + INVENTORY._reviewed_active_source_contract( + equivalent_literal_spacing_source)[1], + repaired_contract_sha256, + "equivalent literal spacing changed placement digest", + ) + self.assertEqual( + observe_topology(equivalent_literal_spacing_source), + (True, True), + "equivalent literal spacing changed repair observations", + ) + + literal_attachment_fixture = ( + 'auto encoding = u8"ready";\n' + 'auto raw = LR"tag(raw bytes)tag"_suffix;\n' + 'auto adjacent = "left""right";\n') + attachment_contract_sha256 = ( + INVENTORY._reviewed_active_source_contract( + literal_attachment_fixture)[1]) + for attachment_mutation in ( + literal_attachment_fixture.replace( + 'u8"ready"', 'u"ready"', 1), + literal_attachment_fixture.replace( + 'tag"_suffix', 'tag" _suffix', 1), + literal_attachment_fixture.replace( + '"left""right"', '"left" "right"', 1)): + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + attachment_mutation)[1], + attachment_contract_sha256, + "literal prefix/suffix/adjacency mutation escaped digest", + ) + + joined_directive_source = repaired_source.replace( + '#include "mesh/Mesh.hpp"\n\nnamespace', + '#include "mesh/Mesh.hpp" namespace', + 1) + self.assertNotEqual(joined_directive_source, repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + joined_directive_source)[1], + repaired_contract_sha256, + "directive/code line-boundary mutation escaped digest", + ) + assert_repair_state( + joined_directive_source, + (False, False, False), + "directive/code line-boundary mutation escaped repair state", + ) + + block_comment_join_source = repaired_source.replace( + '#include "mesh/Mesh.hpp"\n\nnamespace', + '#include "mesh/Mesh.hpp" /*\n*/ namespace', + 1) + self.assertNotEqual(block_comment_join_source, repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + block_comment_join_source)[1], + repaired_contract_sha256, + "multiline block comment preserved a directive boundary", + ) + assert_repair_state( + block_comment_join_source, + (False, False, False), + "multiline block-comment join escaped repair state", + ) + + block_comment_join_variants = ( + '#include "mesh/Mesh.hpp" /*\n\n\n*/ namespace', + '#include "mesh/Mesh.hpp" /*\\\n\n*/ namespace', + ) + for joined_include in block_comment_join_variants: + joined_source = repaired_source.replace( + '#include "mesh/Mesh.hpp"\n\nnamespace', + joined_include, + 1) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract(joined_source)[1], + repaired_contract_sha256, + "block-comment newline family escaped phase-3 digest", + ) + self.assertEqual( + observe_topology(joined_source), + (False, False), + "block-comment newline family escaped observations", + ) + + safe_block_comment_sources = ( + repaired_source.replace( + "int d4 = -1;", + "int/* first comment line\n\nthird comment line */d4 = -1;", + 1), + "/* standalone comment\n\nwith multiple new-lines */\n" + + repaired_source, + repaired_source + + "\n/* trailing standalone\n\nblock comment */", + repaired_source.replace( + "int d4 = -1;", + "int/* multiline\nblock comment */ // real line boundary\n" + "d4 = -1;", + 1), + ) + for safe_block_comment_source in safe_block_comment_sources: + self.assertEqual( + INVENTORY._reviewed_active_source_contract( + safe_block_comment_source)[1], + repaired_contract_sha256, + "safe multiline block comment changed phase-3 digest", + ) + assert_repair_state( + safe_block_comment_source, + (True, True, True), + "safe multiline block comment changed repair state", + ) + + pragma_control = ( + "int b0d_before_pragma = 0;\n#pragma b0d_probe\n" + + repaired_source) + pragma_join = ( + "int b0d_before_pragma = 0; /*\n*/ #pragma b0d_probe\n" + + repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract(pragma_join)[1], + INVENTORY._reviewed_active_source_contract(pragma_control)[1], + "ordinary-code/#pragma phase-3 join preserved digest", + ) + self.assertEqual( + observe_topology(pragma_join), + (False, False), + "ordinary-code/#pragma phase-3 join escaped observations", + ) + + for horizontal_line_character in ("\v", "\f"): + horizontal_join_source = repaired_source.replace( + '#include "mesh/Mesh.hpp"\n\nnamespace', + '#include "mesh/Mesh.hpp"' + + horizontal_line_character + "namespace", + 1) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + horizontal_join_source)[1], + repaired_contract_sha256, + "VT/FF incorrectly split a directive logical line", + ) + assert_repair_state( + horizontal_join_source, + (False, False, False), + "VT/FF directive-boundary mutation escaped repair state", + ) + + unicode_whitespace_source = repaired_source.replace( + "int d4 = -1;", "int\u00a0d4 = -1;", 1) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + unicode_whitespace_source)[1], + repaired_contract_sha256, + "Unicode whitespace collapsed with ASCII C++ whitespace", + ) + assert_repair_state( + unicode_whitespace_source, + (False, False, False), + "Unicode whitespace mutation escaped repair state", + ) + + unicode_boundary_sources = ( + "\u00a0" + repaired_source, + repaired_source + "\u00a0", + ) + for unicode_boundary_source in unicode_boundary_sources: + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + unicode_boundary_source)[1], + repaired_contract_sha256, + "leading/trailing Unicode whitespace escaped digest", + ) + assert_repair_state( + unicode_boundary_source, + (False, False, False), + "leading/trailing Unicode whitespace escaped repair state", + ) + + unicode_directive_sources = ( + "#if\u00a00\nint hidden_by_nbsp = does_not_compile;\n" + "#endif\n" + repaired_source, + "#\u00a0if 0\nint hidden_by_nbsp = does_not_compile;\n" + "#endif\n" + repaired_source, + ) + for unicode_directive_source in unicode_directive_sources: + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract( + unicode_directive_source)[1], + repaired_contract_sha256, + "Unicode directive whitespace escaped digest", + ) + assert_repair_state( + unicode_directive_source, + (False, False, False), + "Unicode directive whitespace escaped repair state", + ) + + for newline in ("\r\n", "\r"): + equivalent_newlines_source = repaired_source.replace("\n", newline) + self.assertEqual( + INVENTORY._reviewed_active_source_contract( + equivalent_newlines_source)[1], + repaired_contract_sha256, + "equivalent C++ new-line spelling changed digest", + ) + assert_repair_state( + equivalent_newlines_source, + (True, True, True), + "equivalent C++ new-line spelling changed repair state", + ) + + publication_signature = ( + "void Mesh::set_one_ring_vertices_sorted()\n{\n") + out_of_scope_helper_source = """ +void mutate_publication_fields_before_preflight(std::vector &faces) +{ + faces[0].adjacentVertices.clear(); + faces[0].oneRingVertices.clear(); +} +""" + repaired_source.replace( + publication_signature, + publication_signature + + " mutate_publication_fields_before_preflight(faces);\n", + 1) + assert_repair_state( + out_of_scope_helper_source, + (False, False, False), + "out-of-scope publication helper escaped the source contract", + ) + + count_preserving_source = repaired_source.replace( + " staged[3] = d4;", + " { int d4; staged[3] = 0; }", + 1) + count_preserving_active = ( + INVENTORY._reviewed_active_source_contract( + count_preserving_source)[0]) + count_preserving_classifier = INVENTORY._unique_braced_scope_span( + count_preserving_active, + r"\bLegacyOneRingClassification\s+" + r"Mesh::classify_legacy_one_ring\s*" + r"\(\s*const\s+Face\s*&\s*face\s*\)\s*const\s*\{") + self.assertIsNotNone(count_preserving_classifier) + self.assertEqual( + len(re.findall( + r"\bd4\b", count_preserving_classifier[3])), + 6, + "adversary did not preserve the reviewed d4 token count", + ) + assert_repair_state( + count_preserving_source, + (False, False, False), + "count-preserving nested d4 declaration escaped the contract", + ) + + ambiguous_conditionals = ( + "#if 1\nint conditionally_active_helper = 0;\n#endif\n", + "#if 0\nint inactive_helper = 0;\n" + "#else\nint potentially_active_helper = 0;\n#endif\n", + "#endif\n", + ) + for conditional_prefix in ambiguous_conditionals: + conditional_source = conditional_prefix + repaired_source + self.assertFalse( + INVENTORY._reviewed_active_source_contract( + conditional_source)[2], + "potentially active conditional was not ambiguous", + ) + assert_repair_state( + conditional_source, + (False, False, False), + "potentially active conditional escaped the contract", + ) + + trigraph_source = ( + "??=if 0\n" + "int b0d_trigraph_hidden_but_cpp17_active = does_not_compile;\n" + "??=endif\n" + repaired_source) + self.assertNotEqual( + INVENTORY._reviewed_active_source_contract(trigraph_source)[1], + repaired_contract_sha256, + "C++17-active trigraph spelling escaped the source digest", + ) + assert_repair_state( + trigraph_source, + (False, False, False), + "C++17-active trigraph spelling escaped repair state", + ) + + misordered_source = classifier_source + """ +void Mesh::set_one_ring_vertices_sorted() +{ +""" + publication_loop + preflight_loop + """ +} +""" + misordered_report = collect_with_topology(misordered_source) + misordered_record = misordered_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (misordered_record["sentinel_initialization_observed"], + misordered_record["rejection_precedes_publication_observed"], + misordered_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(misordered_report)) + + inactive_fake_report = collect_with_topology( + "#if 0\n" + repaired_source + "#endif\n" + + original_text("src/mesh/Mesh_setup_geometry.cpp")) + inactive_fake_record = inactive_fake_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (inactive_fake_record["sentinel_initialization_observed"], + inactive_fake_record[ + "rejection_precedes_publication_observed"], + inactive_fake_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(inactive_fake_report)) + + detached_preflight = preflight_loop.replace( + " {\n" + " throw std::runtime_error(message);\n" + " }", + " {\n" + " }\n" + " throw std::runtime_error(message);", 1) + detached_throw_source = classifier_source + """ +void Mesh::set_one_ring_vertices_sorted() +{ +""" + detached_preflight + publication_loop + """ +} +""" + detached_throw_report = collect_with_topology(detached_throw_source) + detached_throw_record = detached_throw_report["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] + self.assertEqual( + (detached_throw_record["sentinel_initialization_observed"], + detached_throw_record[ + "rejection_precedes_publication_observed"], + detached_throw_record["repair_confirmed"]), + (False, False, False), + ) + self.assertFalse(validate_topology(detached_throw_report)) + + inconsistent_current = copy.deepcopy(self.baseline) + inconsistent_current["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] \ + ["repair_confirmed"] = True + self.assertTrue(INVENTORY.validate_inventory( + inconsistent_current, check_adr=False)) + inconsistent_repaired = copy.deepcopy(repaired_report) + inconsistent_repaired["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] \ + ["repair_confirmed"] = False + self.assertTrue(validate_topology(inconsistent_repaired)) + non_boolean = copy.deepcopy(self.baseline) + non_boolean["D_topology_guards"] \ + ["legacy_11_control_predicate"] \ + ["wp1_1a_classifier_repair_record"] \ + ["sentinel_initialization_observed"] = "false" + self.assertTrue(INVENTORY.validate_inventory( + non_boolean, check_adr=False)) + + def collapse_legacy_11_control_split(report) -> None: + legacy = report["D_topology_guards"]["legacy_11_control_predicate"] + legacy.pop("legacy_11_control_matrix_defect_assertion") + legacy.pop("wp1_1a_classifier_repair_record") + legacy["defect_confirmed"] = True + + self.assert_mutation_rejected(collapse_legacy_11_control_split) def test_E_scheme_boundary_and_version_policy(self) -> None: self.assert_mutation_rejected(