noqt commited on
Commit
566f672
·
1 Parent(s): 44553a9

test: wait for autonomous lockdown persistence

Browse files
scripts/run_autonomous_matrix.py CHANGED
@@ -55,18 +55,57 @@ def incident_ids() -> set[str]:
55
  }
56
 
57
 
58
- def clear_new_incidents(previous: set[str]) -> int:
59
- cleared = 0
60
- for item in root_call(["incidents"]).get("incidents", []):
61
- if (
62
- isinstance(item, dict)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  and item.get("state") == "ACTIVE"
64
  and isinstance(item.get("incident_id"), str)
65
  and item["incident_id"] not in previous
66
- ):
67
- root_call(["incident", "clear", item["incident_id"]])
68
- cleared += 1
69
- return cleared
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
  def wait_for_armed_doctor(operator: str, *, timeout: float = 45) -> dict[str, Any]:
@@ -182,6 +221,7 @@ def main() -> int:
182
  try:
183
  wait_for_armed_doctor(operator)
184
  before_incidents = incident_ids()
 
185
  for index in range(args.discoveries):
186
  canary = subprocess.Popen(["/bin/sleep", "30"], start_new_session=True)
187
  process: subprocess.Popen[bytes] | None = None
@@ -196,6 +236,7 @@ def main() -> int:
196
  starts.append((receipt["containment"]["first_stop_monotonic_ns"] - started) / 1_000_000)
197
  empties.append(float(receipt["containment"]["trigger_to_empty_ms"]))
198
  results["discoveries"].append(receipt["event_id"])
 
199
  results["canary_survival"] += 1
200
  finally:
201
  if process is not None:
@@ -205,7 +246,10 @@ def main() -> int:
205
  # incident. Root-clear only this run's incident before the
206
  # independent approved-survival phase; protected relaunch blocking
207
  # remains covered by the dedicated incident/lockdown tests.
208
- clear_new_incidents(before_incidents)
 
 
 
209
  for index in range(args.approved):
210
  approval_name = f"allow-{secrets.token_hex(6)}"
211
  run_name = f"approved-{secrets.token_hex(6)}"
 
55
  }
56
 
57
 
58
+ def clear_new_incidents(
59
+ previous: set[str],
60
+ *,
61
+ expected_event_id: str,
62
+ timeout: float = 8.0,
63
+ settle_seconds: float = 1.0,
64
+ ) -> int:
65
+ """Wait for post-receipt response persistence, then clear this phase.
66
+
67
+ The containment receipt is intentionally durable before local active
68
+ response. Polling once immediately after the final receipt can therefore
69
+ miss the incident and let its lockdown race the following approved phase.
70
+ """
71
+ if not expected_event_id or expected_event_id in previous:
72
+ raise ValueError("expected event identity is invalid")
73
+ deadline = time.monotonic() + timeout
74
+ observed_response = False
75
+ quiet_since: float | None = None
76
+ cleared: set[str] = set()
77
+ while time.monotonic() < deadline:
78
+ incidents = root_call(["incidents"]).get("incidents", [])
79
+ if not isinstance(incidents, list):
80
+ raise TypeError("invalid Eggcracker incident response")
81
+ current_ids = {
82
+ item["incident_id"]
83
+ for item in incidents
84
+ if isinstance(item, dict) and isinstance(item.get("incident_id"), str)
85
+ }
86
+ new_active = {
87
+ item["incident_id"]
88
+ for item in incidents
89
+ if isinstance(item, dict)
90
  and item.get("state") == "ACTIVE"
91
  and isinstance(item.get("incident_id"), str)
92
  and item["incident_id"] not in previous
93
+ }
94
+ observed_response = observed_response or expected_event_id in current_ids or bool(
95
+ new_active
96
+ )
97
+ if new_active:
98
+ for incident_id in sorted(new_active):
99
+ root_call(["incident", "clear", incident_id])
100
+ cleared.add(incident_id)
101
+ quiet_since = None
102
+ elif observed_response:
103
+ now = time.monotonic()
104
+ quiet_since = now if quiet_since is None else quiet_since
105
+ if now - quiet_since >= settle_seconds:
106
+ return len(cleared)
107
+ time.sleep(0.01)
108
+ raise RuntimeError("autonomous incident response did not settle before approval")
109
 
110
 
111
  def wait_for_armed_doctor(operator: str, *, timeout: float = 45) -> dict[str, Any]:
 
221
  try:
222
  wait_for_armed_doctor(operator)
223
  before_incidents = incident_ids()
224
+ last_event_id = ""
225
  for index in range(args.discoveries):
226
  canary = subprocess.Popen(["/bin/sleep", "30"], start_new_session=True)
227
  process: subprocess.Popen[bytes] | None = None
 
236
  starts.append((receipt["containment"]["first_stop_monotonic_ns"] - started) / 1_000_000)
237
  empties.append(float(receipt["containment"]["trigger_to_empty_ms"]))
238
  results["discoveries"].append(receipt["event_id"])
239
+ last_event_id = str(receipt["event_id"])
240
  results["canary_survival"] += 1
241
  finally:
242
  if process is not None:
 
246
  # incident. Root-clear only this run's incident before the
247
  # independent approved-survival phase; protected relaunch blocking
248
  # remains covered by the dedicated incident/lockdown tests.
249
+ clear_new_incidents(
250
+ before_incidents,
251
+ expected_event_id=last_event_id,
252
+ )
253
  for index in range(args.approved):
254
  approval_name = f"allow-{secrets.token_hex(6)}"
255
  run_name = f"approved-{secrets.token_hex(6)}"
tests/test_ai_smoke.py CHANGED
@@ -73,6 +73,35 @@ class AiSmokeTests(unittest.TestCase):
73
  ):
74
  autonomous_matrix.stop_selected("operator", "approved-test")
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  def test_manifest_assets_must_match_their_recorded_digests(self) -> None:
77
  with tempfile.TemporaryDirectory() as raw:
78
  root = Path(raw)
 
73
  ):
74
  autonomous_matrix.stop_selected("operator", "approved-test")
75
 
76
+ def test_autonomous_incident_cleanup_waits_for_post_receipt_response(self) -> None:
77
+ empty = {"incidents": []}
78
+ active = {
79
+ "incidents": [
80
+ {"incident_id": "event-late", "state": "ACTIVE"},
81
+ ]
82
+ }
83
+ cleared = {
84
+ "incidents": [
85
+ {"incident_id": "event-late", "state": "CLEARED"},
86
+ ]
87
+ }
88
+ with (
89
+ mock.patch.object(
90
+ autonomous_matrix,
91
+ "root_call",
92
+ side_effect=[empty, active, {}, cleared],
93
+ ) as control,
94
+ mock.patch.object(autonomous_matrix.time, "sleep"),
95
+ ):
96
+ count = autonomous_matrix.clear_new_incidents(
97
+ set(),
98
+ expected_event_id="event-late",
99
+ timeout=10,
100
+ settle_seconds=0,
101
+ )
102
+ self.assertEqual(1, count)
103
+ control.assert_any_call(["incident", "clear", "event-late"])
104
+
105
  def test_manifest_assets_must_match_their_recorded_digests(self) -> None:
106
  with tempfile.TemporaryDirectory() as raw:
107
  root = Path(raw)