x0x 0.19.23

Agent-to-agent gossip network for AI systems — no winners, no losers, just cooperation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/usr/bin/env python3
"""Focused tests for the launch-readiness gate policy."""

from __future__ import annotations

import csv
import importlib.util
import sys
import tempfile
import unittest
from pathlib import Path


def load_launch_readiness():
    script = Path(__file__).with_name("launch_readiness.py")
    spec = importlib.util.spec_from_file_location("launch_readiness", script)
    assert spec is not None
    module = importlib.util.module_from_spec(spec)
    assert spec.loader is not None
    sys.modules[spec.name] = module
    spec.loader.exec_module(module)
    return module


class LaunchReadinessGateTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls) -> None:
        cls.lr = load_launch_readiness()

    def test_broad_launch_per_peer_timeout_gate_is_ratio_based(self) -> None:
        deltas = {
            "nyc": {
                "dispatcher_completed": 1000,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 160,
            }
        }
        posts = {"nyc": {"suppressed_peers_size": 0}}
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertTrue(passed)
        self.assertEqual([], violations)

    def test_broad_launch_rejects_high_per_peer_timeout_ratio(self) -> None:
        deltas = {
            "sydney": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 40,
            }
        }
        posts = {"sydney": {"suppressed_peers_size": 0}}
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertFalse(passed)
        self.assertIn("per_peer_timeout/dispatcher_completed ratio", violations[0])

    def test_broad_launch_suppressed_peers_gate_is_ratio_based(self) -> None:
        deltas = {
            "nuremberg": {
                "dispatcher_completed": 1000,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            }
        }
        posts = {
            "nuremberg": {
                "suppressed_peers_size": 134,
                "known_peer_topic_pairs": 1422,
            }
        }
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertTrue(passed)
        self.assertEqual([], violations)

    def test_broad_launch_accepts_warmed_soak_suppression_variance(self) -> None:
        deltas = {
            "nuremberg": {
                "dispatcher_completed": 1000,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            }
        }
        posts = {
            "nuremberg": {
                "suppressed_peers_size": 154,
                "known_peer_topic_pairs": 1359,
            }
        }
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertTrue(passed)
        self.assertEqual([], violations)

    def test_broad_launch_rejects_high_suppressed_peers_ratio(self) -> None:
        deltas = {
            "nuremberg": {
                "dispatcher_completed": 1000,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            }
        }
        posts = {
            "nuremberg": {
                "suppressed_peers_size": 134,
                "known_peer_topic_pairs": 1000,
            }
        }
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertFalse(passed)
        self.assertIn("suppressed_peers/known_peer_topic_pairs ratio", violations[0])

    def test_nonzero_suppressed_with_zero_known_pairs_fails_ratio_gate(self) -> None:
        ratio = self.lr.suppressed_peers_ratio(
            {"suppressed_peers_size": 1, "known_peer_topic_pairs": 0}
        )

        self.assertEqual(float("inf"), ratio)

    def test_limited_production_still_uses_absolute_timeout_cap(self) -> None:
        deltas = {
            "helsinki": {
                "dispatcher_completed": 10_000,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 201,
            }
        }
        posts = {"helsinki": {"suppressed_peers_size": 0}}
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)

        passed, violations = self.lr.evaluate_slos(
            "limited-production", deltas, posts, scenario
        )

        self.assertFalse(passed)
        self.assertIn("per_peer_timeout delta 201", violations[0])

    def test_nonzero_timeouts_with_zero_completed_fail_ratio_gate(self) -> None:
        ratio = self.lr.per_peer_timeout_ratio(
            {"dispatcher_completed": 0, "per_peer_timeout_count": 1}
        )

        self.assertEqual(float("inf"), ratio)

    def test_drop_ratio_uses_produced_delta(self) -> None:
        ratio = self.lr.dropped_full_ratio(
            {"recv_pump_dropped_full": 5, "recv_pump_produced_total": 100}
        )

        self.assertEqual(0.05, ratio)

    def test_diff_counters_clamps_monotonic_resets(self) -> None:
        delta = self.lr.diff_counters(
            {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 10,
                "recv_pump_dropped_full": 2,
                "per_peer_timeout_count": 50,
            },
            {},
        )

        self.assertEqual(0, delta["dispatcher_completed"])
        self.assertEqual(0, delta["dispatcher_timed_out"])
        self.assertEqual(0, delta["recv_pump_dropped_full"])
        self.assertEqual(0, delta["per_peer_timeout_count"])

    def test_redact_auth_tokens_masks_bearer_values(self) -> None:
        text = "curl -H 'Authorization: Bearer abc123SECRET' http://127.0.0.1"

        redacted = self.lr.redact_auth_tokens(text)

        self.assertNotIn("abc123SECRET", redacted)
        self.assertIn("Bearer [REDACTED]", redacted)

    def test_netem_commands_apply_cleanup_and_verify_named_qdisc(self) -> None:
        apply_cmd = self.lr.netem_apply_command("eth0", 1500, 200, "normal")
        cleanup_cmd = self.lr.netem_cleanup_command("eth0")
        verify_cmd = self.lr.netem_verify_clean_command("eth0")

        self.assertIn("tc qdisc add dev eth0 root netem delay 1500ms 200ms", apply_cmd)
        self.assertIn("distribution normal", apply_cmd)
        self.assertEqual("tc qdisc del dev eth0 root 2>/dev/null || true", cleanup_cmd)
        self.assertEqual("! tc qdisc show dev eth0 | grep -q netem", verify_cmd)

    def test_partition_iptables_commands_use_comment_and_cleanup_loop(self) -> None:
        apply_cmd = self.lr.iptables_apply_command("170.64.176.102", 5483)
        cleanup_cmd = self.lr.iptables_cleanup_command("170.64.176.102", 5483)
        verify_cmd = self.lr.iptables_verify_clean_command("170.64.176.102", 5483)

        self.assertIn("iptables -I INPUT 1", apply_cmd)
        self.assertIn("--sport 5483", apply_cmd)
        self.assertIn("--comment x0x-partition-recovery", apply_cmd)
        self.assertIn("while iptables -C INPUT", cleanup_cmd)
        self.assertIn("iptables -D INPUT", cleanup_cmd)
        self.assertTrue(verify_cmd.startswith("! iptables -C INPUT"))

    def test_partition_pair_rejects_anchor_and_unknown_nodes(self) -> None:
        nodes = {"nyc": ("1", "t"), "sfo": ("2", "t"), "sydney": ("3", "t")}

        self.assertEqual(
            ("sfo", "sydney"),
            self.lr.parse_partition_pair("sfo,sydney", "nyc", nodes),
        )
        with self.assertRaises(ValueError):
            self.lr.parse_partition_pair("nyc,sfo", "nyc", nodes)
        with self.assertRaises(ValueError):
            self.lr.parse_partition_pair("sfo,moon", "nyc", nodes)

    def test_dispatcher_timeout_exempt_nodes_are_scenario_scoped(self) -> None:
        deltas = {
            "sfo": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 1,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            },
            "nyc": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            },
        }
        posts = {
            "sfo": {"suppressed_peers_size": 0, "known_peer_topic_pairs": 100},
            "nyc": {"suppressed_peers_size": 0, "known_peer_topic_pairs": 100},
        }
        scenario = self.lr.ScenarioResult(
            name="partition_recovery",
            duration_secs=1.0,
            extra_metrics={"dispatcher_timeout_exempt_nodes": "sfo"},
        )

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertTrue(passed)
        self.assertEqual([], violations)

    def test_suppression_ratio_exempt_nodes_are_scenario_scoped(self) -> None:
        deltas = {
            "sydney": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            },
            "nyc": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 0,
                "per_peer_timeout_count": 0,
            },
        }
        posts = {
            "sydney": {"suppressed_peers_size": 50, "known_peer_topic_pairs": 100},
            "nyc": {"suppressed_peers_size": 5, "known_peer_topic_pairs": 100},
        }
        scenario = self.lr.ScenarioResult(
            name="high_rtt_peer",
            duration_secs=1.0,
            extra_metrics={"suppression_ratio_exempt_nodes": "sydney"},
        )

        passed, violations = self.lr.evaluate_slos(
            "broad-launch", deltas, posts, scenario
        )

        self.assertTrue(passed)
        self.assertEqual([], violations)

    def test_target_cooling_observed_uses_per_observer_deltas(self) -> None:
        start = {
            "observers": {
                "nyc": {
                    "suppressed_entries": 10,
                    "suppressed_topics": ["old"],
                    "cooling_events": 5.0,
                    "outbound_send_timeouts": 100.0,
                },
                "sfo": {
                    "suppressed_entries": 0,
                    "suppressed_topics": [],
                    "cooling_events": 2.0,
                    "outbound_send_timeouts": 20.0,
                },
                "sydney": {
                    "suppressed_entries": 0,
                    "suppressed_topics": [],
                    "cooling_events": 0.0,
                    "outbound_send_timeouts": 0.0,
                },
            }
        }
        mid = {
            "observers": {
                "nyc": {
                    "suppressed_entries": 4,
                    "suppressed_topics": ["old"],
                    "cooling_events": 5.0,
                    "outbound_send_timeouts": 140.0,
                },
                "sfo": {
                    "suppressed_entries": 1,
                    "suppressed_topics": ["new-topic"],
                    "cooling_events": 3.0,
                    "outbound_send_timeouts": 20.0,
                },
                "sydney": {
                    "suppressed_entries": 0,
                    "suppressed_topics": [],
                    "cooling_events": 0.0,
                    "outbound_send_timeouts": 0.0,
                },
            }
        }

        summary = self.lr.target_cooling_delta_summary(
            start,
            mid,
            exclude_node="sydney",
        )

        self.assertTrue(summary["cooling_observed"])
        self.assertEqual(["sfo"], summary["cooling_event_observers"])
        self.assertEqual(["nyc"], summary["outbound_send_timeout_observers"])
        self.assertEqual(["sfo"], summary["new_suppression_observers"])
        self.assertEqual(
            ["new-topic"],
            summary["observers"]["sfo"]["new_suppressed_topics"],
        )

    def test_report_outputs_append_new_csv_fields_and_markdown_ratios(self) -> None:
        scenario = self.lr.ScenarioResult(name="fanout_burst", duration_secs=1.0)
        deltas = {
            "nyc": {
                "dispatcher_completed": 100,
                "dispatcher_timed_out": 0,
                "recv_pump_dropped_full": 1,
                "recv_pump_produced_total": 200,
                "per_peer_timeout_count": 5,
                "_post": {
                    "recv_pump_latest_depth": 7,
                    "suppressed_peers_size": 3,
                    "known_peer_topic_pairs": 60,
                    "pubsub_workers": 2,
                },
            }
        }
        results = [(scenario, deltas, True, [])]

        with tempfile.TemporaryDirectory() as tmp:
            proof_dir = Path(tmp)
            self.lr.write_summary_md(proof_dir, "broad-launch", results)
            self.lr.write_summary_csv(proof_dir, results)

            md = (proof_dir / "summary.md").read_text(encoding="utf-8")
            self.assertIn("drop_ratio", md)
            self.assertIn("pp_to/completed", md)
            self.assertIn("depth_post", md)
            self.assertIn("suppressed/known", md)

            with (proof_dir / "summary.csv").open(newline="") as f:
                rows = list(csv.reader(f))
            self.assertEqual(
                [
                    "scenario",
                    "node",
                    "passed",
                    "fail_reason",
                    "dispatcher_timed_out_delta",
                    "recv_pump_dropped_full_delta",
                    "per_peer_timeout_delta",
                    "suppressed_peers_post",
                    "pubsub_workers_post",
                    "violations_count",
                ],
                rows[0][:10],
            )
            self.assertEqual(
                [
                    "per_peer_timeout_to_completed_ratio",
                    "recv_pump_drop_full_ratio",
                    "recv_pump_latest_depth_post",
                    "suppressed_peers_to_known_ratio",
                    "known_peer_topic_pairs_post",
                ],
                rows[0][10:],
            )
            self.assertEqual("0.050000", rows[1][10])
            self.assertEqual("0.005000", rows[1][11])
            self.assertEqual("7", rows[1][12])
            self.assertEqual("0.050000", rows[1][13])
            self.assertEqual("60", rows[1][14])


if __name__ == "__main__":
    unittest.main()