Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Fix completion guard section attribution for long contract sections

rcourtmanb05d25557680
diff --git a/scripts/release_control/canonical_completion_guard.py b/scripts/release_control/canonical_completion_guard.py
index 126a7d22b4..aa18034a80 100644
--- a/scripts/release_control/canonical_completion_guard.py
+++ b/scripts/release_control/canonical_completion_guard.py
@@ -9,6 +9,7 @@ staged in the same commit.
from __future__ import annotations
import argparse
+import difflib
import fnmatch
import json
import os
@@ -416,45 +417,69 @@ def staged_verification_files_for_requirement(rule: dict, requirement: dict, sta
return sorted(set(matches))
-def staged_contract_patch(path: str) -> str:
+def git_blob_text(spec: str) -> str:
result = subprocess.run(
- ["git", "diff", "--cached", "--unified=1000", "--no-color", "--", path],
+ ["git", "show", spec],
cwd=REPO_ROOT,
- check=True,
+ check=False,
capture_output=True,
text=True,
)
+ if result.returncode != 0:
+ return ""
return result.stdout
-def contract_patch_has_substantive_change(patch_text: str) -> bool:
- current_section = ""
- for line in patch_text.splitlines():
- if line.startswith(("diff --git ", "index ", "--- ", "+++ ", "@@ ")):
- continue
- if not line:
- continue
- prefix = line[0]
- if prefix not in {" ", "+", "-"}:
- continue
- stripped = line[1:].strip()
+def contract_section_map(lines: Sequence[str]) -> List[str]:
+ """Map each line to the `## ` section it belongs to, from the full file."""
+ sections: List[str] = []
+ current = ""
+ for line in lines:
+ stripped = line.strip()
if stripped.startswith("## "):
- current_section = stripped
- continue
- if prefix == " ":
+ current = stripped
+ sections.append(current)
+ return sections
+
+
+def _is_substantive_contract_line(stripped: str, section: str) -> bool:
+ if section not in SUBSTANTIVE_CONTRACT_SECTIONS:
+ return False
+ if not stripped:
+ return False
+ if stripped.startswith("## "):
+ return False
+ if stripped in {"```", "```json"}:
+ return False
+ return True
+
+
+def contract_texts_have_substantive_change(base_text: str, staged_text: str) -> bool:
+ """Compare full contract texts so section membership never depends on how
+ close a change sits to its `## ` header (diff-context attribution broke on
+ sections longer than the context window)."""
+ base_lines = base_text.splitlines()
+ staged_lines = staged_text.splitlines()
+ base_sections = contract_section_map(base_lines)
+ staged_sections = contract_section_map(staged_lines)
+ matcher = difflib.SequenceMatcher(a=base_lines, b=staged_lines, autojunk=False)
+ for tag, base_start, base_end, staged_start, staged_end in matcher.get_opcodes():
+ if tag == "equal":
continue
- if current_section not in SUBSTANTIVE_CONTRACT_SECTIONS:
- continue
- if not stripped:
- continue
- if stripped in {"```", "```json"}:
- continue
- return True
+ for idx in range(base_start, base_end):
+ if _is_substantive_contract_line(base_lines[idx].strip(), base_sections[idx]):
+ return True
+ for idx in range(staged_start, staged_end):
+ if _is_substantive_contract_line(staged_lines[idx].strip(), staged_sections[idx]):
+ return True
return False
def staged_contract_has_substantive_change(path: str) -> bool:
- return contract_patch_has_substantive_change(staged_contract_patch(path))
+ return contract_texts_have_substantive_change(
+ git_blob_text(f"HEAD:{path}"),
+ git_blob_text(f":0:{path}"),
+ )
def format_missing_requirements(
diff --git a/scripts/release_control/canonical_completion_guard_test.py b/scripts/release_control/canonical_completion_guard_test.py
index 4886de2c5a..3ac56396ec 100644
--- a/scripts/release_control/canonical_completion_guard_test.py
+++ b/scripts/release_control/canonical_completion_guard_test.py
@@ -11,7 +11,7 @@ from canonical_completion_guard import (
SUBSYSTEM_REGISTRY,
build_verification_requirements,
check_staged_contracts,
- contract_patch_has_substantive_change,
+ contract_texts_have_substantive_change,
infer_impacted_subsystems,
is_ignored_runtime_file,
is_test_or_fixture,
@@ -1953,52 +1953,111 @@ class CanonicalCompletionGuardTest(unittest.TestCase):
},
)
- def test_contract_patch_metadata_only_is_not_substantive(self):
- patch = """diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
-index 1111111..2222222 100644
---- a/docs/release-control/v6/internal/subsystems/monitoring.md
-+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
-@@ -1,12 +1,12 @@
- # Monitoring Contract
+ def test_contract_metadata_only_change_is_not_substantive(self):
+ base = """# Monitoring Contract
- ## Contract Metadata
+## Contract Metadata
- ```json
- {
-- "dependency_subsystem_ids": []
-+ "dependency_subsystem_ids": ["unified-resources"]
- }
- ```
+```json
+{
+ "dependency_subsystem_ids": []
+}
+```
- ## Purpose
+## Purpose
+
+Watch things.
+"""
+ staged = base.replace(
+ '"dependency_subsystem_ids": []',
+ '"dependency_subsystem_ids": ["unified-resources"]',
+ )
+ self.assertFalse(contract_texts_have_substantive_change(base, staged))
+
+ def test_contract_current_state_change_is_substantive(self):
+ base = """# Monitoring Contract
+
+## Purpose
+
+Watch things.
+
+## Current State
+
+Read-state migration remains partial.
"""
- self.assertFalse(contract_patch_has_substantive_change(patch))
-
- def test_contract_patch_current_state_change_is_substantive(self):
- patch = """diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
-index 1111111..2222222 100644
---- a/docs/release-control/v6/internal/subsystems/monitoring.md
-+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
-@@ -20,8 +20,8 @@
- ## Current State
-
--Read-state migration remains partial.
-+Read-state migration is now complete for storage-backed workload assembly.
+ staged = base.replace(
+ "Read-state migration remains partial.",
+ "Read-state migration is now complete for storage-backed workload assembly.",
+ )
+ self.assertTrue(contract_texts_have_substantive_change(base, staged))
+
+ def test_contract_shared_boundaries_change_is_substantive(self):
+ base = """# API Contracts Contract
+
+## Shared Boundaries
+
+1. `internal/api/resources.go` shared with `unified-resources`: old shared rationale.
+"""
+ staged = base.replace("old shared rationale.", "new shared rationale.")
+ self.assertTrue(contract_texts_have_substantive_change(base, staged))
+
+ def test_contract_current_state_edit_far_below_section_header_is_substantive(self):
+ # Regression: the old implementation attributed sections from
+ # `git diff --unified=1000` context, so an edit more than 1000 lines
+ # below its `## ` header lost its section and was wrongly reported
+ # as not substantive (unified-resources.md's Current State spans
+ # thousands of lines).
+ filler = "\n".join(f"- historical note {i} about the registry." for i in range(1500))
+ base = f"""# Unified Resources Contract
+
+## Contract Metadata
+
+```json
+{{
+ "dependency_subsystem_ids": []
+}}
+```
+
+## Current State
+
+{filler}
+
+Registry rebuilds still emit no-op relationship_change events.
+
+## Extension Points
+
+None yet.
"""
- self.assertTrue(contract_patch_has_substantive_change(patch))
-
- def test_contract_patch_shared_boundaries_change_is_substantive(self):
- patch = """diff --git a/docs/release-control/v6/internal/subsystems/api-contracts.md b/docs/release-control/v6/internal/subsystems/api-contracts.md
-index 1111111..2222222 100644
---- a/docs/release-control/v6/internal/subsystems/api-contracts.md
-+++ b/docs/release-control/v6/internal/subsystems/api-contracts.md
-@@ -18,7 +18,7 @@
- ## Shared Boundaries
-
--1. `internal/api/resources.go` shared with `unified-resources`: old shared rationale.
-+1. `internal/api/resources.go` shared with `unified-resources`: new shared rationale.
+ staged = base.replace(
+ "Registry rebuilds still emit no-op relationship_change events.",
+ "Registry rebuilds now skip no-op relationship_change events.",
+ )
+ self.assertTrue(contract_texts_have_substantive_change(base, staged))
+
+ def test_contract_metadata_edit_in_file_with_long_current_state_stays_non_substantive(self):
+ # Counterpart to the deep-edit regression test: full-file attribution
+ # must not over-report either — a metadata-only tweak in a file whose
+ # Current State is huge is still not substantive.
+ filler = "\n".join(f"- historical note {i} about the registry." for i in range(1500))
+ base = f"""# Unified Resources Contract
+
+## Contract Metadata
+
+```json
+{{
+ "dependency_subsystem_ids": []
+}}
+```
+
+## Current State
+
+{filler}
"""
- self.assertTrue(contract_patch_has_substantive_change(patch))
+ staged = base.replace(
+ '"dependency_subsystem_ids": []',
+ '"dependency_subsystem_ids": ["monitoring"]',
+ )
+ self.assertFalse(contract_texts_have_substantive_change(base, staged))
def test_alerts_owned_runtime_has_no_default_fallback(self):
alerts_rule = next(rule for rule in load_subsystem_rules() if rule["id"] == "alerts")