Prove legacy Gitea state from observed inspect orders

This commit is contained in:
Codex
2026-09-29 16:44:22 +03:00
parent ae5adfbd63
commit d87a0c48b4
3 changed files with 201 additions and 33 deletions
+102 -6
View File
@@ -54,7 +54,7 @@ class ActionsRecoveryTests(unittest.TestCase):
self.assertFalse(RUNNER.gitea_actions_prove_legacy_order(changed, fingerprint))
self.assertNotEqual(RUNNER.gitea_actions_fingerprint_v2(old), RUNNER.gitea_actions_fingerprint_v2(changed))
def test_duplicate_malformed_and_unbounded_runtime_fields_rejected(self):
def test_duplicate_and_malformed_runtime_fields_rejected(self):
duplicate = self.with_limits()
duplicate["Mounts"].append(copy.deepcopy(duplicate["Mounts"][0]))
with self.assertRaisesRegex(RUNNER.DeployError, "duplicate"):
@@ -63,15 +63,58 @@ class ActionsRecoveryTests(unittest.TestCase):
malformed["HostConfig"] = None
with self.assertRaisesRegex(RUNNER.DeployError, "malformed"):
RUNNER.gitea_actions_fingerprint_v2(malformed)
many = copy.deepcopy(self.info)
many["Mounts"] = [{"Destination": "/mount" + str(i)} for i in range(8)]
with self.assertRaisesRegex(RUNNER.DeployError, "bounded"):
RUNNER.gitea_actions_prove_legacy_order(many, "0" * 64)
for value in (None, []):
info = copy.deepcopy(self.info)
info["HostConfig"]["Ulimits"] = value
self.assertTrue(RUNNER.gitea_actions_prove_legacy_order(info, RUNNER.gitea_actions_fingerprint(info)))
def many_fields(self):
value = copy.deepcopy(self.info)
value["Mounts"].extend({"Destination": "/mount" + str(i), "Source": "/data" + str(i), "RW": False} for i in range(6))
value["HostConfig"]["Ulimits"] = [{"Name": "limit%02d" % i, "Soft": 1024, "Hard": 4096} for i in range(16)]
return value
def cross_sample_orders(self):
target = self.many_fields()
for values in (target["Mounts"], target["HostConfig"]["Ulimits"]):
values[:] = values[::2] + values[1::2]
a, b = copy.deepcopy(target), copy.deepcopy(target)
a["HostConfig"]["Ulimits"].sort(key=lambda x: x["Name"])
b["Mounts"].sort(key=lambda x: x["Destination"])
return target, a, b
def test_large_lists_direct_match_does_not_require_factorial_search(self):
info = self.many_fields()
evidence = {}
self.assertTrue(RUNNER.gitea_actions_prove_legacy_order(info, RUNNER.gitea_actions_fingerprint(info), evidence))
self.assertEqual(evidence["mountCount"], 8)
self.assertEqual(evidence["ulimitCount"], 16)
self.assertEqual(len(evidence.get("tested", set())), 0)
def test_cross_sample_proof_recovers_exact_hash_with_large_lists(self):
target, a, b = self.cross_sample_orders()
expected = RUNNER.gitea_actions_fingerprint(target)
evidence = {}
self.assertNotEqual(RUNNER.gitea_actions_fingerprint(a), expected)
self.assertNotEqual(RUNNER.gitea_actions_fingerprint(b), expected)
self.assertFalse(RUNNER.gitea_actions_prove_legacy_order(a, expected, evidence))
self.assertTrue(RUNNER.gitea_actions_prove_legacy_order(b, expected, evidence))
self.assertLessEqual(len(evidence["tested"]), 65536)
# A cached proof is not transferable to changed values.
b["HostConfig"]["Ulimits"][0]["Hard"] += 1
with self.assertRaisesRegex(RUNNER.DeployError, "runtime changed"):
RUNNER.gitea_actions_prove_legacy_order(b, expected, evidence)
def test_repeated_observations_do_not_repeat_hash_search(self):
info = self.many_fields()
evidence = {}
self.assertFalse(RUNNER.gitea_actions_prove_legacy_order(info, "0" * 64, evidence))
attempts = len(evidence["tested"])
for _ in range(64):
self.assertFalse(RUNNER.gitea_actions_prove_legacy_order(info, "0" * 64, evidence))
self.assertEqual(len(evidence["tested"]), attempts)
self.assertLessEqual(attempts, 65536)
def test_v1_archive_unchanged_and_v2_has_one_data_descriptor(self):
self.assertEqual(hashlib.sha256(build_actions_artifact.bundle(RUNNER.GITEA_ACTIONS_FAILED_ID)).hexdigest(),
"2ae874cfc6015378246e0bb7b24d4594a749e4136fbd82dbe9db0b57356d2162")
@@ -150,11 +193,64 @@ class ActionsRecoveryTests(unittest.TestCase):
def test_readonly_reconciliation_rejects_drift_without_mutation(self):
self.recovery_fixture()
self.reordered["HostConfig"]["Memory"] = 123
with redirect_stdout(io.StringIO()), self.assertRaisesRegex(RUNNER.DeployError, "cannot prove order-only"):
with redirect_stdout(io.StringIO()), self.assertRaisesRegex(RUNNER.DeployError, "runtime changed"):
RUNNER.preflight_gitea_actions_recovery()
self.mutation.assert_not_called()
self.restart.assert_not_called()
def test_readonly_reconciliation_handles_cross_sample_proof_and_two_confirmations(self):
self.recovery_fixture()
target, a, b = self.cross_sample_orders()
receipt_path = RUNNER.BACKUPS_DIR / RUNNER.GITEA_ACTIONS_FAILED_BACKUP / "gitea-actions-before.json"
receipt = json.loads(receipt_path.read_text())
receipt["fingerprint"] = RUNNER.gitea_actions_fingerprint(target)
receipt_path.write_text(json.dumps(receipt))
self.inspect.side_effect = [a,b,a,b]
with redirect_stdout(io.StringIO()) as output:
before = RUNNER.preflight_gitea_actions_recovery()
self.assertEqual(self.inspect.call_count, 4)
self.assertFalse(before["mutationStarted"])
report = json.loads(output.getvalue().split("gitea_actions_readonly_audit=")[1])
self.assertTrue(report["exactLegacyProof"])
self.assertEqual(report["sampleCount"], 4)
self.assertEqual(report["ulimitCount"], 16)
self.assertTrue(all(s["matchesSavedStateByOrderOnly"] for s in report["samples"]))
self.mutation.assert_not_called()
self.restart.assert_not_called()
def test_unproven_stable_drift_exhausts_readonly_samples_without_mutation(self):
self.recovery_fixture()
self.reordered["HostConfig"]["Memory"] = 123
self.inspect.side_effect = None
self.inspect.return_value = self.reordered
with redirect_stdout(io.StringIO()) as output, self.assertRaisesRegex(RUNNER.DeployError, "cannot prove order-only"):
RUNNER.preflight_gitea_actions_recovery()
self.assertEqual(self.inspect.call_count, 64)
report = json.loads(output.getvalue().split("gitea_actions_readonly_audit=")[1])
self.assertFalse(report["exactLegacyProof"])
self.assertFalse(report["mutation"])
self.assertEqual(report["sampleCount"], 64)
self.assertNotIn("fixture-keep-private", output.getvalue())
self.mutation.assert_not_called()
self.restart.assert_not_called()
def test_proof_on_last_search_sample_still_requires_two_fresh_confirmations(self):
self.recovery_fixture()
target, a, b = self.cross_sample_orders()
receipt_path = RUNNER.BACKUPS_DIR / RUNNER.GITEA_ACTIONS_FAILED_BACKUP / "gitea-actions-before.json"
receipt = json.loads(receipt_path.read_text())
receipt["fingerprint"] = RUNNER.gitea_actions_fingerprint(target)
receipt_path.write_text(json.dumps(receipt))
self.inspect.side_effect = [a] * 63 + [b,a,b]
with redirect_stdout(io.StringIO()) as output:
RUNNER.preflight_gitea_actions_recovery()
self.assertEqual(self.inspect.call_count, 66)
report = json.loads(output.getvalue().split("gitea_actions_readonly_audit=")[1])
self.assertTrue(report["exactLegacyProof"])
self.assertEqual(report["sampleCount"], 66)
self.mutation.assert_not_called()
self.restart.assert_not_called()
def test_reconciliation_rejects_wrong_ledger_before_inspection(self):
ledger, row = self.recovery_fixture()
row["rollback_status"] = "ok:gitea-actions-config"