Fix Device Core release health contract and prove r5 restoration

This commit is contained in:
Codex
2026-09-29 19:26:33 +03:00
parent d161d7fb91
commit ed500278a0
5 changed files with 437 additions and 10 deletions
+188 -10
View File
@@ -2960,6 +2960,11 @@ GITEA_SALVAGE_UNSUPPORTED_SCHEMA_TABLES = tuple(
)
COMPONENTS = {
"device-core-release-reconciliation": {
"payload_root": DEVICE_PLANE_ROOT,
"artifact_only": True,
"services": (),
},
"device-core-release": {
"payload_root": DEVICE_PLANE_ROOT,
"compose_root": DEVICE_PLANE_ROOT,
@@ -4963,6 +4968,10 @@ def denied_payload_path(component, rel):
def allowed_payload_path(component, rel):
validate_posix_path(rel)
if component == "device-core-release-reconciliation":
if rel in ("deployment", DEVICE_RELEASE_RECONCILIATION_ENTRY):
return True
die("Device Core reconciliation accepts only its exact descriptor")
if component == "device-core-release":
return device_release_payload_path(rel)
reason = denied_payload_path(component, rel)
@@ -11579,6 +11588,8 @@ def load_artifact(artifact, work_dir):
die(f"files.txt entry missing in payload: {rel}")
validate_payload_tree(manifest["component"], payload_dir, entries)
if manifest["component"] == "device-core-release-reconciliation":
validate_device_release_reconciliation_payload(payload_dir, entries, manifest["id"])
if manifest["component"] == "device-core-release":
validate_device_release_payload(payload_dir, entries, manifest["id"])
if manifest["component"] == "gitea-actions-config":
@@ -20567,6 +20578,7 @@ def validate_device_manager_control_plane_runtime(
require_edge_channel=None,
core_network_mode=None,
require_persistent_data=None,
read_only=False,
):
core_ids = device_plane_service_container_ids("device-control-core")
manager_ids = device_plane_service_container_ids("device-manager")
@@ -20790,11 +20802,14 @@ def validate_device_manager_control_plane_runtime(
if inspect_device_edge_channel_core_identity_state() != "valid-reuse-at-apply":
die("Device Edge channel Core identity is not active")
validate_device_edge_channel_public_export()
ensure_platform_runtime_secret(
PLATFORM_DEVICE_CORE_INTERNAL_TOKEN_FILE,
MAP_GATEWAY_SECRET_RE,
"Device Core Hub handoff",
)
if read_only:
validate_device_release_hub_secret()
else:
ensure_platform_runtime_secret(
PLATFORM_DEVICE_CORE_INTERNAL_TOKEN_FILE,
MAP_GATEWAY_SECRET_RE,
"Device Core Hub handoff",
)
return "exact"
@@ -22425,6 +22440,10 @@ def is_platform_provider_catalog_only(entries):
def component_services(component, entries=None):
if component == "device-core-release-reconciliation":
if tuple(entries or ()) != (DEVICE_RELEASE_RECONCILIATION_ENTRY,):
die("Device Core reconciliation requires the exact descriptor entry")
return ()
if component == "device-core-release":
if tuple(entries or ()) != DEVICE_RELEASE_ENTRIES:
die("Device Core release service selection requires exact entries")
@@ -32370,7 +32389,8 @@ def device_release_predecessor(candidate):
applied = load_state(STATE_FILE)
for row in load_state(FAILED_STATE_FILE):
if row.get("component") == "device-core-release" and row.get("started_apply") and not str(row.get("rollback_status", "")).startswith("ok:"):
die("Device Core release requires reconciliation of a failed rollback")
if not device_release_r5_reconciled(row):
die("Device Core release requires reconciliation of a failed rollback")
normal = [row for row in applied if row.get("component") == "device-core-release"]
installed = DEVICE_PLANE_ROOT / DEVICE_RELEASE_ENTRY
expected = {}
@@ -32463,6 +32483,146 @@ def device_release_other_compose_services():
return result
# One exact evidence-only reconciliation of the r5 health-contract incident.
DEVICE_RELEASE_R5_ID = "device-core-release-v1-r5-67b2ae586399eb1eb7d3a6c074a292e0dbb7b406"
DEVICE_RELEASE_R5_SHA = "b8dd1384f499d1173ce7d95dc1f2433a404290f443bd612816ad9136ccbf9347"
DEVICE_RELEASE_R5_BACKUP = "device-core-release-" + DEVICE_RELEASE_R5_ID + "-20260929-190019"
DEVICE_RELEASE_RECONCILIATION_ID = "device-core-release-r5-reconciliation-v1-20260929-001"
DEVICE_RELEASE_RECONCILIATION_ENTRY = "deployment/device-core-release-r5-reconciliation-v1.json"
DEVICE_RELEASE_R5_BACKUP_HASHES = {
"device-release-before.json": "0e30ebd1a1e511da5e8026afbba9b761c5edb2fcc151b23ecc79bbae8af0b739",
"source-before.tgz": "d597d1cd1dcb1eeaf79836c40523258ae1a9aa1b6e3333a94238f40f078accb5",
"manifest.env": "484c124d22d89daf9fb869c64c5748d6ec7cb4614b0c6155b9e68f952d111248",
"files.txt": "4d02728c991e4666425d968497d3540ad95802de055db4f899411ce34e526b5a",
"existing-files.txt": "f34f96aba57759c7302c24f03dc45281eea49032deed0a4f0d55685689d79e2b",
"missing-files.txt": "8d836605cbac79413813582fbce798c80196a4bbbd259f9b97559fab90f5d2e8",
}
def validate_device_release_hub_secret():
path = PLATFORM_DEVICE_CORE_INTERNAL_TOKEN_FILE
for target, directory, mode in ((path.parent, True, 0o710), (path, False, 0o640)):
try:
info = target.lstat()
except FileNotFoundError:
die("Device Core existing Hub secret is missing")
if (stat.S_ISLNK(info.st_mode) or (not stat.S_ISDIR(info.st_mode) if directory else not stat.S_ISREG(info.st_mode))
or info.st_uid != 0 or info.st_gid != MAP_GATEWAY_RUNTIME_GID or stat.S_IMODE(info.st_mode) != mode):
die("Device Core existing Hub secret boundary mismatch")
if not directory and info.st_size > 512:
die("Device Core existing Hub secret is oversized")
try:
value = path.read_text(encoding="ascii").strip()
except (OSError, UnicodeDecodeError):
die("Device Core existing Hub secret is unreadable")
if not MAP_GATEWAY_SECRET_RE.fullmatch(value):
die("Device Core existing Hub secret format mismatch")
def device_release_healthchecks():
# Matches the preserved Core v4 + Manager v13 foundation, not the older
# disabled foundation's generic device-plane fallback.
return (
{"url": "http://127.0.0.1:18120/healthz", "expected_json": {
"ok": True, "service": "nodedc-device-control-core", "database": "ready",
"discoveryIngest": "enabled", "managementApi": "enabled",
"commandTransport": "typed-service-ping-v1",
}},
{"url": "http://127.0.0.1:18080/healthz", "headers": {"Host": "device.nodedc.ru"},
"expected_json": {"ok": True, "service": "nodedc-device-manager",
"authRequired": True, "deviceCoreConfigured": True}},
)
def device_release_reconciliation_descriptor():
return {"schemaVersion": "nodedc.device-core.release-reconciliation.v1",
"releaseId": DEVICE_RELEASE_RECONCILIATION_ID,
"failedReleaseId": DEVICE_RELEASE_R5_ID, "failedSha256": DEVICE_RELEASE_R5_SHA,
"backupId": DEVICE_RELEASE_R5_BACKUP, "mode": "prove-restored-baseline-only"}
def validate_device_release_reconciliation_payload(payload, entries, patch_id):
if tuple(entries) != (DEVICE_RELEASE_RECONCILIATION_ENTRY,) or patch_id != DEVICE_RELEASE_RECONCILIATION_ID:
die("Device Core reconciliation identity/entry mismatch")
if read_strict_json(payload / DEVICE_RELEASE_RECONCILIATION_ENTRY, "Device Core reconciliation") != device_release_reconciliation_descriptor():
die("Device Core reconciliation descriptor mismatch")
def device_release_r5_failed_row():
return {"artifact": "nodedc-" + DEVICE_RELEASE_R5_ID + ".tgz.20260929-190019",
"backup_id": DEVICE_RELEASE_R5_BACKUP, "component": "device-core-release",
"failed_at": "2026-09-29T16:06:28+00:00", "id": DEVICE_RELEASE_R5_ID,
"message": "healthcheck failed for http://127.0.0.1:18120/healthz: health response contract mismatch",
"rollback_status": "failed:DeployError", "sha256": DEVICE_RELEASE_R5_SHA,
"started_apply": True, "status": "failed"}
def device_release_r5_evidence():
expected = device_release_r5_failed_row()
rows = [r for r in load_state(FAILED_STATE_FILE) if r.get("id") == DEVICE_RELEASE_R5_ID or r.get("sha256") == DEVICE_RELEASE_R5_SHA]
if rows != [expected]:
die("Device Core reconciliation failed journal mismatch")
if any(r.get("id") == DEVICE_RELEASE_R5_ID or r.get("sha256") == DEVICE_RELEASE_R5_SHA for r in load_state(STATE_FILE)):
die("Device Core failed release conflicts with applied journal")
failed = FAILED_DIR / expected["artifact"]
if failed.is_symlink() or not failed.is_file() or sha256_file(failed) != DEVICE_RELEASE_R5_SHA:
die("Device Core reconciliation failed archive mismatch")
backup = BACKUPS_DIR / DEVICE_RELEASE_R5_BACKUP
if backup.is_symlink() or not backup.is_dir():
die("Device Core reconciliation backup is missing or unsafe")
for name, digest in DEVICE_RELEASE_R5_BACKUP_HASHES.items():
path = backup / name
if path.is_symlink() or not path.is_file() or sha256_file(path) != digest:
die("Device Core reconciliation backup digest mismatch: " + name)
before = read_strict_json(backup / "device-release-before.json", "Device Core r5 runtime backup", max_bytes=256*1024)
existing = read_backup_path_list(backup / "existing-files.txt")
missing = read_backup_path_list(backup / "missing-files.txt")
validate_backup_partition(DEVICE_RELEASE_ENTRIES, existing, missing, "Device Core reconciliation")
if missing != [DEVICE_RELEASE_ENTRY]:
die("Device Core reconciliation backup partition mismatch")
with tempfile.TemporaryDirectory(prefix="device-r5-evidence-", dir=TMP_DIR) as tmp:
root = Path(tmp)
materialize_backup_tree(backup / "source-before.tgz", root, set(existing))
if collect_exact_files(root, existing, "Device Core r5 source backup") != before["installedFiles"]:
die("Device Core reconciliation source/runtime evidence conflict")
return before
def preflight_device_release_reconciliation(payload, entries, patch_id, *, marker_installed=False):
validate_device_release_reconciliation_payload(payload, entries, patch_id)
marker = DEVICE_PLANE_ROOT / DEVICE_RELEASE_RECONCILIATION_ENTRY
if not marker_installed and (marker.exists() or marker.is_symlink()):
die("Device Core reconciliation marker already exists")
before = device_release_r5_evidence()
# Source, current image IDs, configuration and every preserved dependency
# are checked without invoking any build, Compose mutation or secret ensure.
run_healthchecks("device-core-release", DEVICE_RELEASE_ENTRIES, DEVICE_RELEASE_SERVICES)
accept_device_release(before, rollback=True)
print("device_release_reconciliation=restored-baseline-proven;services=none;build=none;runtime-mutation=none")
return before
def device_release_r5_reconciled(row):
if row != device_release_r5_failed_row():
return False
marker = DEVICE_PLANE_ROOT / DEVICE_RELEASE_RECONCILIATION_ENTRY
if marker.is_symlink() or not marker.is_file():
return False
validate_device_release_reconciliation_payload(DEVICE_PLANE_ROOT, (DEVICE_RELEASE_RECONCILIATION_ENTRY,), DEVICE_RELEASE_RECONCILIATION_ID)
records = [r for r in load_state(STATE_FILE) if r.get("id") == DEVICE_RELEASE_RECONCILIATION_ID]
if len(records) != 1 or records[0].get("component") != "device-core-release-reconciliation":
die("Device Core reconciliation marker lacks its canonical applied receipt")
with tempfile.TemporaryDirectory(prefix="device-r5-marker-", dir=TMP_DIR) as tmp:
manifest, entries, payload = load_artifact(device_release_applied_archive(records[0]), Path(tmp))
if manifest.get("component") != "device-core-release-reconciliation" or manifest.get("id") != DEVICE_RELEASE_RECONCILIATION_ID:
die("Device Core reconciliation applied archive identity mismatch")
if sha256_file(payload / DEVICE_RELEASE_RECONCILIATION_ENTRY) != sha256_file(marker):
die("Device Core reconciliation installed marker drift")
# Keep original incident evidence immutable after acknowledgement.
device_release_r5_evidence()
return True
def preflight_device_release(payload, entries, patch_id):
d = validate_device_release_payload(payload, entries, patch_id)
predecessor = device_release_predecessor(d)
@@ -32471,7 +32631,8 @@ def preflight_device_release(payload, entries, patch_id):
immutable = (".dockerignore", "services/device-control-core/Dockerfile", "services/device-manager/Dockerfile", "services/device-control-core/migrations")
if collect_exact_files(payload, immutable, "Device Core candidate foundation") != collect_exact_files(DEVICE_PLANE_ROOT, immutable, "Device Core installed foundation"):
die("Device Core release changes the immutable database/build foundation")
validate_device_manager_control_plane_runtime(require_edge_channel=True, core_network_mode="private-egress", require_persistent_data=True)
validate_device_manager_control_plane_runtime(require_edge_channel=True, core_network_mode="private-egress", require_persistent_data=True, read_only=True)
run_healthchecks("device-core-release", entries, DEVICE_RELEASE_SERVICES)
before = device_release_runtime_snapshot()
compose = {str(p): sha256_file(p) for p in component_compose_files("device-plane")}
return {"descriptor": d, "predecessor": predecessor["releaseId"] if predecessor else "manager-v13/core-v4/edge-v4",
@@ -32504,7 +32665,7 @@ def accept_device_release(before, *, rollback=False):
die("Device Core release changed an unrelated Compose service")
if {str(p): sha256_file(p) for p in component_compose_files("device-plane")} != before["compose"]:
die("Device Core release changed Compose topology")
validate_device_manager_control_plane_runtime(require_edge_channel=True, core_network_mode="private-egress", require_persistent_data=True)
validate_device_manager_control_plane_runtime(require_edge_channel=True, core_network_mode="private-egress", require_persistent_data=True, read_only=True)
if rollback:
entries = DEVICE_RELEASE_ENTRIES if DEVICE_RELEASE_ENTRY in before["installedFiles"] else DEVICE_RELEASE_ENTRIES[:-1]
if collect_exact_files(DEVICE_PLANE_ROOT, entries, "Device Core rollback source") != before["installedFiles"]:
@@ -32581,6 +32742,8 @@ def plan_artifact(artifact):
gitea_salvage_preflight = None
with tempfile.TemporaryDirectory(prefix="plan-", dir=TMP_DIR) as tmp:
manifest, entries, payload_dir = load_artifact(artifact, Path(tmp))
if manifest["component"] == "device-core-release-reconciliation":
preflight_device_release_reconciliation(payload_dir, entries, manifest["id"])
if manifest["component"] == "device-core-release":
release_before = preflight_device_release(payload_dir, entries, manifest["id"])
print("device_release_source=" + release_before["descriptor"]["sourceCommit"])
@@ -38743,12 +38906,17 @@ process.stdout.write('engine-l2-closed-loop:0.7.0:cas+safe-profile+external-plan
def run_healthchecks(component, entries=None, services=None):
if component == "device-core-release-reconciliation":
if tuple(services or ()) != ():
die("Device Core reconciliation must not select services")
preflight_device_release_reconciliation(DEVICE_PLANE_ROOT, entries, DEVICE_RELEASE_RECONCILIATION_ID, marker_installed=True)
return
if component == "device-core-release":
if tuple(services or ()) != DEVICE_RELEASE_SERVICES or tuple(entries or ()) != DEVICE_RELEASE_ENTRIES:
die("Device Core release health scope mismatch")
for service in DEVICE_RELEASE_INVENTORY:
healthcheck_compose_service_with_grace("device-plane", service)
for check in component_healthchecks("device-plane", entries, services):
for check in device_release_healthchecks():
healthcheck_url(check)
return
if component == "gitea-actions-config":
@@ -39913,6 +40081,8 @@ def apply_artifact(artifact):
gitea_ci_before = preflight_gitea_ci(entries)
if component == "gitea-actions-config":
gitea_actions_before = preflight_gitea_actions(entries)
if component == "device-core-release-reconciliation":
preflight_device_release_reconciliation(payload_dir, entries, patch_id)
if component == "device-core-release":
device_release_before = preflight_device_release(payload_dir, entries, patch_id)
artifact_only = component_artifact_only(component)
@@ -40650,7 +40820,15 @@ def apply_artifact(artifact):
and backup_dir is not None
and root is not None
):
if component == "device-core-release":
if component == "device-core-release-reconciliation":
try:
restore_platform_overlay(root, backup_dir, entries, current_stamp)
if (root / DEVICE_RELEASE_RECONCILIATION_ENTRY).exists():
die("Device Core reconciliation rollback left a marker")
rollback_status = "ok:device-core-release-reconciliation-marker-only"
except Exception as rollback_exc:
rollback_status = "failed:" + type(rollback_exc).__name__
elif component == "device-core-release":
try:
rollback_device_release(root, backup_dir, entries, current_stamp, device_release_before, runtime_started)
rollback_status = "ok:device-core-release"