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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3052732
 
 
4671695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
from __future__ import annotations

import io
import json
import tempfile
import unittest
from contextlib import redirect_stderr, redirect_stdout
from pathlib import Path
from unittest.mock import patch

from lumi_eggcracker.cli import main


def doctor_response(**overrides: object) -> dict[str, object]:
    value: dict[str, object] = {
        "autonomous_discovery": True,
        "backend": "root-supervisor",
        "cgroup_v2": True,
        "catalogue": {"secret": "catalogue-canary"},
        "discovery": {
            "healthy": True,
            "receipt_persistence_healthy": True,
            "path": "/private/discovery-canary",
        },
        "execution_boundary": {"supported": True, "argv": ["argv-canary"]},
        "incidents": {"healthy": True, "lockdown": False, "secret": "incident-canary"},
        "installation": {"state": "HEALTHY", "path": "/private/install-canary"},
        "network": {
            "cleanup_healthy": True,
            "mode": "offline",
            "primitives": {"supported": True, "uid": "uid-canary"},
        },
        "pidfd": True,
        "result": "PASS",
        "version": "1.0.10",
        "workload_identity": {"healthy": True, "uid": 4242},
        "workload_uid": 4242,
    }
    value.update(overrides)
    return value


def wait_status(*, state: str = "RUNNING", run_id: str = "a" * 24) -> dict[str, object]:
    return {
        "exec_policy_digest": "0" * 64,
        "exec_policy_id": "legacy",
        "incident_lockdown": False,
        "name": "demo",
        "network_mode": "offline",
        "run_id": run_id,
        "state": state,
        "unit": f"lumi-eggcracker-workload-{run_id}.service",
        "workload_uid": 4242,
    }


class CliTests(unittest.TestCase):
    def test_version(self) -> None:
        output = io.StringIO()
        with redirect_stdout(output):
            self.assertEqual(0, main(["version"]))
        self.assertEqual("1.0.10", output.getvalue().strip())

    def test_public_help_has_only_supported_commands(self) -> None:
        from lumi_eggcracker.cli import _parser
        help_text = _parser().format_help()
        for command in (
            "start",
            "check-start",
            "kill",
            "status",
            "wait",
            "list",
            "history",
            "approve",
            "revoke",
            "approvals",
            "detections",
            "doctor",
            "support-bundle",
            "validate-support-bundle",
            "compare-detection-receipts",
            "version",
        ):
            self.assertIn(command, help_text)
        self.assertIn("exec-policy", help_text)
        self.assertNotIn("_supervisor", help_text)
        self.assertNotIn("network" + "-deny", help_text)
        self.assertIn(
            "retained terminal records in recorded monotonic order",
            " ".join(help_text.split()),
        )
        self.assertNotIn("recent", help_text)
        self.assertNotIn("chronological", help_text)
        doctor_help = io.StringIO()
        with redirect_stdout(doctor_help), self.assertRaises(SystemExit) as raised:
            main(["doctor", "--help"])
        self.assertEqual(0, raised.exception.code)
        self.assertIn("--recovery-plan", doctor_help.getvalue())

    def test_history_defaults_to_twenty_and_forwards_explicit_limit(self) -> None:
        with patch("lumi_eggcracker.cli.request", return_value={"runs": []}) as request:
            output = io.StringIO()
            with redirect_stdout(output):
                self.assertEqual(0, main(["history"]))
            request.assert_called_once_with("history", limit=20)
            output.seek(0)
            self.assertEqual({"runs": []}, json.loads(output.read()))

        with patch("lumi_eggcracker.cli.request", return_value={"runs": []}) as request:
            with redirect_stdout(io.StringIO()):
                self.assertEqual(0, main(["history", "--limit", "7"]))
            request.assert_called_once_with("history", limit=7)

    def test_history_forwards_each_supported_state(self) -> None:
        for state in (
            "COMPLETED_ALLOWED",
            "TERMINATED",
            "CONTAINMENT_FAILED",
            "CONTAINED_RECEIPT_FAILED",
        ):
            with self.subTest(state=state), patch(
                "lumi_eggcracker.cli.request", return_value={"runs": []}
            ) as request, redirect_stdout(io.StringIO()):
                self.assertEqual(0, main(["history", "--limit", "7", "--state", state]))
            request.assert_called_once_with("history", limit=7, state=state)

    def test_history_limit_rejects_invalid_duplicate_and_extra_arguments(self) -> None:
        cases = (
            ["history", "--limit", "0"],
            ["history", "--limit", "21"],
            ["history", "--limit", "true"],
            ["history", "--limit", "1.0"],
            ["history", "--limit", "1", "--limit", "2"],
            ["history", "--limit", "1", "unexpected"],
            ["history", "--state", "RUNNING"],
            ["history", "--state", "terminated"],
            ["history", "--state"],
            ["history", "--state", "TERMINATED", "--state", "CONTAINMENT_FAILED"],
            ["history", "--state", "TERMINATED", "unexpected"],
        )
        for values in cases:
            with self.subTest(values=values), patch(
                "lumi_eggcracker.cli.request"
            ) as request, redirect_stderr(io.StringIO()):
                with self.assertRaises(SystemExit) as raised:
                    main(values)
                self.assertEqual(2, raised.exception.code)
                request.assert_not_called()

    def test_doctor_summary_ready_is_fixed_and_nonidentifying(self) -> None:
        response = doctor_response()
        output = io.StringIO()
        error = io.StringIO()
        with (
            patch("lumi_eggcracker.cli.request", return_value=response) as request,
            redirect_stdout(output),
            redirect_stderr(error),
        ):
            self.assertEqual(0, main(["doctor", "--summary"]))
        self.assertEqual(
            {
                "failed_checks": [],
                "next_action": "NONE",
                "result": "READY",
                "schema": "lumi-eggcracker.doctor-summary.v1",
            },
            json.loads(output.getvalue()),
        )
        self.assertEqual("", error.getvalue())
        self.assertNotIn("canary", output.getvalue())
        request.assert_called_once_with("doctor")

    def test_doctor_summary_mixed_failures_are_sorted_and_deduplicated(self) -> None:
        response = doctor_response(
            autonomous_discovery=False,
            cgroup_v2=False,
            execution_boundary={"supported": False},
            discovery={"healthy": False, "receipt_persistence_healthy": False},
            incidents={"healthy": False, "lockdown": True},
            installation={"state": "DRIFT"},
            network={
                "cleanup_healthy": False,
                "mode": "unsupported",
                "primitives": {"supported": False},
            },
            pidfd=False,
            result="UNSUPPORTED",
            workload_identity={"healthy": False},
        )
        output = io.StringIO()
        with patch("lumi_eggcracker.cli.request", return_value=response), redirect_stdout(output):
            self.assertEqual(6, main(["doctor", "--summary"]))
        summary = json.loads(output.getvalue())
        self.assertEqual(
            [
                "cgroup_v2",
                "discovery",
                "execution_boundary",
                "incidents",
                "installation",
                "network_cleanup",
                "network_mode",
                "network_primitives",
                "pidfd",
                "receipt_persistence",
                "workload_identity",
            ],
            summary["failed_checks"],
        )
        self.assertEqual(len(summary["failed_checks"]), len(set(summary["failed_checks"])))
        self.assertEqual("NOT_READY", summary["result"])
        self.assertEqual("REVIEW_SUPERVISOR_READINESS", summary["next_action"])
        self.assertEqual(
            {"failed_checks", "next_action", "result", "schema"},
            set(summary),
        )
        self.assertNotIn("canary", output.getvalue())

    def test_doctor_summary_unsupported_without_visible_failure_is_honest(self) -> None:
        response = doctor_response(autonomous_discovery=False, result="UNSUPPORTED")
        output = io.StringIO()
        with patch("lumi_eggcracker.cli.request", return_value=response), redirect_stdout(output):
            self.assertEqual(6, main(["doctor", "--summary"]))
        self.assertEqual(
            {
                "failed_checks": ["supervisor_readiness"],
                "next_action": "REVIEW_SUPERVISOR_READINESS",
                "result": "NOT_READY",
                "schema": "lumi-eggcracker.doctor-summary.v1",
            },
            json.loads(output.getvalue()),
        )

    def test_doctor_summary_malformed_or_contradictory_response_is_constant_error(self) -> None:
        malformed = [
            {},
            doctor_response(result=True),
            doctor_response(cgroup_v2="true"),
            doctor_response(network={"mode": "secret-path-canary"}),
            doctor_response(autonomous_discovery=False),
            doctor_response(autonomous_discovery=True, result="UNSUPPORTED"),
            doctor_response(incidents={"healthy": False, "lockdown": False}),
            doctor_response(installation={"state": "PRIVATE-CANARY"}),
        ]
        for response in malformed:
            with self.subTest(response=response):
                output = io.StringIO()
                error = io.StringIO()
                with (
                    patch("lumi_eggcracker.cli.request", return_value=response),
                    redirect_stdout(output),
                    redirect_stderr(error),
                ):
                    self.assertEqual(6, main(["doctor", "--summary"]))
                self.assertEqual("", output.getvalue())
                self.assertEqual("eggcracker: doctor summary unavailable\n", error.getvalue())
                self.assertNotIn("canary", error.getvalue())

        output = io.StringIO()
        error = io.StringIO()
        with patch("lumi_eggcracker.cli.request", side_effect=OSError("/private/CANARY")), redirect_stdout(output), redirect_stderr(error):
            self.assertEqual(6, main(["doctor", "--summary"]))
        self.assertEqual("", output.getvalue())
        self.assertEqual("eggcracker: doctor summary unavailable\n", error.getvalue())
        self.assertNotIn("CANARY", error.getvalue())

    def test_default_doctor_output_and_exit_remain_unchanged(self) -> None:
        for result, expected_exit in (("PASS", 0), ("UNSUPPORTED", 6)):
            with self.subTest(result=result):
                response = doctor_response(
                    autonomous_discovery=result == "PASS", result=result
                )
                output = io.StringIO()
                error = io.StringIO()
                with patch("lumi_eggcracker.cli.request", return_value=response), redirect_stdout(output), redirect_stderr(error):
                    self.assertEqual(expected_exit, main(["doctor"]))
                self.assertEqual(json.dumps(response, sort_keys=True) + "\n", output.getvalue())
                self.assertEqual("", error.getvalue())

    def test_doctor_recovery_plan_ready_is_closed_and_empty(self) -> None:
        response = doctor_response()
        output = io.StringIO()
        error = io.StringIO()
        with (
            patch("lumi_eggcracker.cli.request", return_value=response) as request,
            redirect_stdout(output),
            redirect_stderr(error),
        ):
            self.assertEqual(0, main(["doctor", "--recovery-plan"]))
        self.assertEqual(
            {
                "actions": [],
                "failed_checks": [],
                "installation_state": "HEALTHY",
                "result": "READY",
                "schema": "lumi-eggcracker.doctor-recovery-plan.v1",
            },
            json.loads(output.getvalue()),
        )
        self.assertEqual("", error.getvalue())
        self.assertNotIn("canary", output.getvalue())
        request.assert_called_once_with("doctor")

    def test_doctor_recovery_plan_distinguishes_installation_states(self) -> None:
        expected_actions = {
            "NOT_INSTALLED": ["MANUAL_INSTALLATION_REQUIRED"],
            "DRIFT": ["MANUAL_INSTALLATION_REPAIR"],
            "RECOVERY_REQUIRED": ["MANUAL_INSTALLATION_RECOVERY"],
        }
        for state, actions in expected_actions.items():
            with self.subTest(state=state):
                output = io.StringIO()
                response = doctor_response(
                    autonomous_discovery=False,
                    installation={"state": state, "path": "/private/install-canary"},
                    result="UNSUPPORTED",
                )
                with (
                    patch("lumi_eggcracker.cli.request", return_value=response),
                    redirect_stdout(output),
                ):
                    self.assertEqual(6, main(["doctor", "--recovery-plan"]))
                plan = json.loads(output.getvalue())
                self.assertEqual(state, plan["installation_state"])
                self.assertEqual(["installation"], plan["failed_checks"])
                self.assertEqual(actions, plan["actions"])
                self.assertNotIn("canary", output.getvalue())

    def test_doctor_recovery_plan_hidden_readiness_is_manual_supervisor_review(self) -> None:
        response = doctor_response(autonomous_discovery=False, result="UNSUPPORTED")
        output = io.StringIO()
        with (
            patch("lumi_eggcracker.cli.request", return_value=response),
            redirect_stdout(output),
        ):
            self.assertEqual(6, main(["doctor", "--recovery-plan"]))
        self.assertEqual(
            {
                "actions": ["MANUAL_SUPERVISOR_REVIEW"],
                "failed_checks": ["supervisor_readiness"],
                "installation_state": "HEALTHY",
                "result": "NOT_READY",
                "schema": "lumi-eggcracker.doctor-recovery-plan.v1",
            },
            json.loads(output.getvalue()),
        )

    def test_doctor_recovery_plan_failures_are_sorted_and_deduplicated(self) -> None:
        response = doctor_response(
            autonomous_discovery=False,
            cgroup_v2=False,
            execution_boundary={"supported": False},
            discovery={"healthy": False, "receipt_persistence_healthy": False},
            incidents={"healthy": False, "lockdown": True},
            installation={"state": "DRIFT"},
            network={
                "cleanup_healthy": False,
                "mode": "unsupported",
                "primitives": {"supported": False},
            },
            pidfd=False,
            result="UNSUPPORTED",
            workload_identity={"healthy": False},
        )
        output = io.StringIO()
        with (
            patch("lumi_eggcracker.cli.request", return_value=response),
            redirect_stdout(output),
        ):
            self.assertEqual(6, main(["doctor", "--recovery-plan"]))
        plan = json.loads(output.getvalue())
        self.assertEqual(
            [
                "cgroup_v2",
                "discovery",
                "execution_boundary",
                "incidents",
                "installation",
                "network_cleanup",
                "network_mode",
                "network_primitives",
                "pidfd",
                "receipt_persistence",
                "workload_identity",
            ],
            plan["failed_checks"],
        )
        self.assertEqual(
            [
                "MANUAL_HOST_REVIEW",
                "MANUAL_IDENTITY_REVIEW",
                "MANUAL_INCIDENT_REVIEW",
                "MANUAL_INSTALLATION_REPAIR",
                "MANUAL_POLICY_REVIEW",
                "MANUAL_SUPERVISOR_REVIEW",
            ],
            plan["actions"],
        )
        self.assertEqual(len(plan["failed_checks"]), len(set(plan["failed_checks"])))
        self.assertEqual(len(plan["actions"]), len(set(plan["actions"])))

    def test_doctor_recovery_plan_malformed_or_transport_response_is_constant_error(self) -> None:
        malformed = [
            {},
            doctor_response(result=True),
            doctor_response(installation={"state": "PRIVATE-CANARY"}),
            doctor_response(autonomous_discovery=True, result="UNSUPPORTED"),
        ]
        for response in malformed:
            with self.subTest(response=response):
                output = io.StringIO()
                error = io.StringIO()
                with (
                    patch("lumi_eggcracker.cli.request", return_value=response),
                    redirect_stdout(output),
                    redirect_stderr(error),
                ):
                    self.assertEqual(6, main(["doctor", "--recovery-plan"]))
                self.assertEqual("", output.getvalue())
                self.assertEqual(
                    "eggcracker: doctor recovery plan unavailable\n", error.getvalue()
                )
                self.assertNotIn("canary", error.getvalue())

        output = io.StringIO()
        error = io.StringIO()
        with (
            patch("lumi_eggcracker.cli.request", side_effect=OSError("/private/CANARY")),
            redirect_stdout(output),
            redirect_stderr(error),
        ):
            self.assertEqual(6, main(["doctor", "--recovery-plan"]))
        self.assertEqual("", output.getvalue())
        self.assertEqual(
            "eggcracker: doctor recovery plan unavailable\n", error.getvalue()
        )
        self.assertNotIn("CANARY", error.getvalue())

    def test_doctor_projections_cannot_be_combined_before_transport(self) -> None:
        with (
            patch("lumi_eggcracker.cli.request") as request,
            redirect_stderr(io.StringIO()),
            self.assertRaises(SystemExit) as raised,
        ):
            main(["doctor", "--summary", "--recovery-plan"])
        self.assertEqual(2, raised.exception.code)
        request.assert_not_called()

    def test_compare_detection_receipts_help_uses_installed_command(self) -> None:
        output = io.StringIO()
        with redirect_stdout(output), self.assertRaises(SystemExit) as raised:
            main(["compare-detection-receipts", "--help"])
        self.assertEqual(0, raised.exception.code)
        self.assertIn("usage: eggcracker compare-detection-receipts", output.getvalue())
        self.assertIn("--before FILE", output.getvalue())
        self.assertIn("--after FILE", output.getvalue())

    def test_compare_detection_receipts_forwards_arguments_before_connected_commands(
        self,
    ) -> None:
        forwarded = [
            "--before",
            "before-export.json",
            "--after",
            "after-export.json",
            "--output",
            "comparison.json",
        ]
        with (
            patch(
                "lumi_eggcracker.cli.compare_detection_receipts_main", return_value=0
            ) as compare,
            patch("lumi_eggcracker.cli.request") as request,
            patch("lumi_eggcracker.cli.supervisor_main") as supervisor,
            patch("lumi_eggcracker.cli.gate_main") as gate,
            patch("lumi_eggcracker.cli.watchdog_main") as watchdog,
        ):
            self.assertEqual(0, main(["compare-detection-receipts", *forwarded]))
        compare.assert_called_once_with(
            forwarded, prog="eggcracker compare-detection-receipts"
        )
        request.assert_not_called()
        supervisor.assert_not_called()
        gate.assert_not_called()
        watchdog.assert_not_called()

    def test_compare_detection_receipts_success_and_existing_output_refusal(self) -> None:
        def synthetic_export(event_id: str) -> dict[str, object]:
            return {
                "export_schema": "lumi-eggcracker.redacted-detection-receipt-export.v1",
                "source_sha256": "f" * 64,
                "authentication": "NOT_AUTHENTICATED",
                "live_verification": "NOT_PERFORMED",
                "receipt": {
                    "schema_version": "lumi-eggcracker.detection-receipt.v2",
                    "event_id": event_id,
                    "source_commit": "a" * 40,
                    "version": "1.0.10",
                    "catalogue_sha256": "b" * 64,
                    "receipt_written_utc": "2026-10-02T00:00:00Z",
                    "detector": {
                        "profile": "content.gguf-llama",
                        "detection_path": "CONTENT",
                        "catalogue_schema": "lumi-eggcracker.detectors.v3",
                    },
                    "trigger": {"kind": "UNAPPROVED_AI_MATCH"},
                    "recorded_result": "CONTAINMENT_FAILED",
                },
            }

        with tempfile.TemporaryDirectory(prefix="cli-receipt-comparison-") as directory:
            root = Path(directory)
            before = root / "before.json"
            after = root / "after.json"
            before.write_text(json.dumps(synthetic_export("0" * 24)), encoding="utf-8")
            after.write_text(json.dumps(synthetic_export("1" * 24)), encoding="utf-8")
            output = root / "existing-comparison.json"
            original_output = b"preserve this synthetic report\n"
            output.write_bytes(original_output)

            stdout = io.StringIO()
            stderr = io.StringIO()
            with (
                patch("lumi_eggcracker.cli.request") as request,
                patch("lumi_eggcracker.cli.supervisor_main") as supervisor,
                redirect_stdout(stdout),
                redirect_stderr(stderr),
            ):
                self.assertEqual(
                    0,
                    main(
                        [
                            "compare-detection-receipts",
                            "--before",
                            str(before),
                            "--after",
                            str(after),
                        ]
                    ),
                )
                comparison = json.loads(stdout.getvalue())
                self.assertEqual("RECORDED", comparison["comparison_status"])
                self.assertEqual("", stderr.getvalue())
                stdout.seek(0)
                stdout.truncate(0)
                stderr.seek(0)
                stderr.truncate(0)
                self.assertEqual(
                    1,
                    main(
                        [
                            "compare-detection-receipts",
                            "--before",
                            str(before),
                            "--after",
                            str(after),
                            "--output",
                            str(output),
                        ]
                    ),
                )
                self.assertEqual("", stdout.getvalue())
                self.assertIn("OUTPUT_ALREADY_EXISTS", stderr.getvalue())
                request.assert_not_called()
                supervisor.assert_not_called()
            self.assertEqual(original_output, output.read_bytes())

    def test_validate_support_bundle_dispatches_before_connected_commands(self) -> None:
        with (
            patch("lumi_eggcracker.cli.validate_support_bundle_main", return_value=0) as validate,
            patch("lumi_eggcracker.cli.request") as request,
            patch("lumi_eggcracker.cli.supervisor_main") as supervisor,
            patch("lumi_eggcracker.cli.gate_main") as gate,
            patch("lumi_eggcracker.cli.watchdog_main") as watchdog,
        ):
            self.assertEqual(
                0,
                main(["validate-support-bundle", "private-name-must-not-be-echoed.json"]),
            )
        validate.assert_called_once_with(["private-name-must-not-be-echoed.json"])
        request.assert_not_called()
        supervisor.assert_not_called()
        gate.assert_not_called()
        watchdog.assert_not_called()

    def test_internal_supervisor_dispatch_remains_available(self) -> None:
        with patch("lumi_eggcracker.cli.supervisor_main", return_value=7) as supervisor:
            self.assertEqual(main(["_supervisor", "--policy", "/tmp/policy.json"]), 7)
        supervisor.assert_called_once_with(["--policy", "/tmp/policy.json"])

    def test_approve_forwards_only_exact_command_arguments(self) -> None:
        with patch("lumi_eggcracker.cli.os.geteuid", return_value=0, create=True), patch("lumi_eggcracker.cli.request", return_value={"result": "APPROVED"}) as request:
            self.assertEqual(0, main(["approve", "--name", "qwen", "--uid", "1001", "--", "/opt/llama-cli", "-m", "/models/qwen.gguf"]))
        request.assert_called_once_with(
            "approve",
            name="qwen",
            uid=1001,
            max_pids=64,
            max_memory_mib=2048,
            cpu_quota_percent=400,
            allow_interface_discovery=False,
            argv=["/opt/llama-cli", "-m", "/models/qwen.gguf"],
        )

    def test_approve_can_grant_interface_discovery(self) -> None:
        with patch("lumi_eggcracker.cli.os.geteuid", return_value=0, create=True), patch("lumi_eggcracker.cli.request", return_value={"result": "APPROVED"}) as request:
            self.assertEqual(0, main(["approve", "--name", "qwen", "--uid", "1001", "--allow-interface-discovery", "--", "/opt/llama-cli", "-m", "/models/qwen.gguf"]))
        self.assertTrue(request.call_args.kwargs["allow_interface_discovery"])

    def test_start_includes_resource_limits(self) -> None:
        with patch("lumi_eggcracker.cli.request", return_value={"result": "STARTED"}) as request:
            self.assertEqual(0, main(["start", "--name", "demo", "--max-pids", "8", "--", "/bin/sleep", "1"]))
        request.assert_called_once_with(
            "start", name="demo", max_pids=8, max_memory_mib=2048,
            cpu_quota_percent=400, argv=["/bin/sleep", "1"],
        )

    def test_start_forwards_selected_execution_policy(self) -> None:
        with patch("lumi_eggcracker.cli.request", return_value={"result": "STARTED"}) as request:
            self.assertEqual(0, main(["start", "--name", "demo", "--exec-policy", "a" * 24, "--max-pids", "8", "--", "/bin/sleep", "1"]))
        request.assert_called_once_with(
            "start", name="demo", max_pids=8, max_memory_mib=2048,
            cpu_quota_percent=400, argv=["/bin/sleep", "1"], exec_policy="a" * 24,
        )

    def test_start_can_require_exact_approval_at_admission_time(self) -> None:
        with patch("lumi_eggcracker.cli.request", return_value={"result": "STARTED"}) as request:
            self.assertEqual(
                0,
                main(
                    [
                        "start",
                        "--name",
                        "demo",
                        "--max-pids",
                        "8",
                        "--require-approval",
                        "--",
                        "/bin/sleep",
                        "1",
                    ]
                ),
            )
        request.assert_called_once_with(
            "start",
            name="demo",
            max_pids=8,
            max_memory_mib=2048,
            cpu_quota_percent=400,
            argv=["/bin/sleep", "1"],
            require_approval=True,
        )

    def test_check_start_forwards_exact_tuple_and_emits_fixed_projection(self) -> None:
        response = {
            "next_action": (
                "run start --require-approval immediately; admission is revalidated"
            ),
            "reason_code": "EXACT_APPROVAL_MATCHED",
            "result": "READY_TO_ATTEMPT",
            "schema": "lumi-eggcracker.check-start.v1",
        }
        output = io.StringIO()
        with patch("lumi_eggcracker.cli.request", return_value=response) as request, redirect_stdout(output):
            self.assertEqual(
                0,
                main(
                    [
                        "check-start",
                        "--name",
                        "demo",
                        "--max-pids",
                        "8",
                        "--max-memory-mib",
                        "4096",
                        "--cpu-quota-percent",
                        "700",
                        "--exec-policy",
                        "a" * 24,
                        "--",
                        "/bin/sleep",
                        "1",
                    ]
                ),
            )
        request.assert_called_once_with(
            "check_start",
            name="demo",
            max_pids=8,
            max_memory_mib=4096,
            cpu_quota_percent=700,
            argv=["/bin/sleep", "1"],
            exec_policy="a" * 24,
        )
        self.assertEqual(response, json.loads(output.getvalue()))

    def test_check_start_malformed_or_transport_response_is_constant_safe_error(self) -> None:
        cases = (
            {"result": "READY_TO_ATTEMPT", "name": "/private/name"},
            {
                "next_action": "raw /private/path",
                "reason_code": "EXACT_APPROVAL_MATCHED",
                "result": "READY_TO_ATTEMPT",
                "schema": "lumi-eggcracker.check-start.v1",
            },
        )
        for response in (*cases, OSError("/private/socket")):
            with self.subTest(response=response):
                output = io.StringIO()
                error = io.StringIO()
                with patch(
                    "lumi_eggcracker.cli.request",
                    side_effect=response if isinstance(response, BaseException) else None,
                    return_value=None if isinstance(response, BaseException) else response,
                ) as request, redirect_stdout(output), redirect_stderr(error):
                    self.assertEqual(
                        4,
                        main(
                            [
                                "check-start",
                                "--name",
                                "demo",
                                "--max-pids",
                                "8",
                                "--",
                                "/private/executable",
                            ]
                        ),
                    )
                self.assertEqual("", output.getvalue())
                self.assertEqual(
                    "eggcracker: check-start response unavailable\n",
                    error.getvalue(),
                )
                self.assertNotIn("/private", error.getvalue())
                request.assert_called_once()

    def test_check_start_parse_failure_is_constant_and_excludes_canary(self) -> None:
        output = io.StringIO()
        error = io.StringIO()
        with patch("lumi_eggcracker.cli.request") as request, redirect_stdout(output), redirect_stderr(error):
            self.assertEqual(
                4,
                main(
                    [
                        "check-start",
                        "--name",
                        "demo",
                        "--max-pids",
                        "/private/parse-canary",
                        "--",
                        "/private/executable-canary",
                    ]
                ),
            )
        self.assertEqual("", output.getvalue())
        self.assertEqual(
            "eggcracker: check-start response unavailable\n",
            error.getvalue(),
        )
        self.assertNotIn("/private", error.getvalue())
        request.assert_not_called()

    def test_check_start_not_ready_is_a_nonzero_snapshot_result(self) -> None:
        response = {
            "next_action": (
                "create or restore the exact root approval for this tuple, then check again"
            ),
            "reason_code": "APPROVAL_MISSING",
            "result": "NOT_READY",
            "schema": "lumi-eggcracker.check-start.v1",
        }
        output = io.StringIO()
        with patch("lumi_eggcracker.cli.request", return_value=response), redirect_stdout(output):
            self.assertEqual(
                6,
                main(
                    [
                        "check-start",
                        "--name",
                        "demo",
                        "--max-pids",
                        "8",
                        "--",
                        "/bin/sleep",
                        "1",
                    ]
                ),
            )
        self.assertEqual(response, json.loads(output.getvalue()))

    def test_wait_immediate_terminal_emits_fixed_marker_and_validated_state(self) -> None:
        response = wait_status(state="TERMINATED")
        output = io.StringIO()
        error = io.StringIO()
        with patch("lumi_eggcracker.cli.time.monotonic", return_value=10.0), patch(
            "lumi_eggcracker.cli.request_before", return_value=response
        ) as request_before, patch("lumi_eggcracker.cli.time.sleep") as sleep, redirect_stdout(
            output
        ), redirect_stderr(error):
            self.assertEqual(0, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        self.assertEqual("TERMINAL\n", output.getvalue().splitlines()[0] + "\n")
        self.assertEqual(response, json.loads(output.getvalue().splitlines()[1]))
        self.assertEqual("", error.getvalue())
        request_before.assert_called_once_with(
            "status", deadline_monotonic=11.0, name="demo"
        )
        sleep.assert_not_called()

    def test_wait_active_to_terminal_pins_run_id_and_uses_bounded_sleep(self) -> None:
        active = wait_status()
        terminal = wait_status(state="CONTAINMENT_FAILED")
        output = io.StringIO()
        with patch(
            "lumi_eggcracker.cli.time.monotonic",
            side_effect=[10.0, 10.0, 10.1, 10.1, 10.2, 10.2],
        ), patch(
            "lumi_eggcracker.cli.request_before", side_effect=[active, terminal]
        ) as request_before, patch("lumi_eggcracker.cli.time.sleep") as sleep, redirect_stdout(
            output
        ):
            self.assertEqual(0, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        self.assertEqual("TERMINAL", output.getvalue().splitlines()[0])
        self.assertEqual(terminal, json.loads(output.getvalue().splitlines()[1]))
        self.assertEqual(2, request_before.call_count)
        self.assertEqual([((0.1,), {})], sleep.call_args_list)
        for call in request_before.call_args_list:
            self.assertEqual("status", call.args[0])
            self.assertEqual(11.0, call.kwargs["deadline_monotonic"])
            self.assertEqual("demo", call.kwargs["name"])

    def test_wait_timeout_emits_last_validated_state_without_postdeadline_request(self) -> None:
        active = wait_status()
        output = io.StringIO()
        with patch(
            "lumi_eggcracker.cli.time.monotonic",
            side_effect=[0.0, 0.0, 0.1, 0.1, 1.0],
        ), patch("lumi_eggcracker.cli.request_before", return_value=active) as request_before, patch(
            "lumi_eggcracker.cli.time.sleep"
        ) as sleep, redirect_stdout(output):
            self.assertEqual(6, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        self.assertEqual("TIMED_OUT", output.getvalue().splitlines()[0])
        self.assertEqual(active, json.loads(output.getvalue().splitlines()[1]))
        request_before.assert_called_once()
        sleep.assert_called_once_with(0.1)

    def test_wait_inflight_deadline_is_timeout_without_inventing_state(self) -> None:
        output = io.StringIO()
        with patch("lumi_eggcracker.cli.time.monotonic", return_value=10.0), patch(
            "lumi_eggcracker.cli.request_before", side_effect=TimeoutError
        ) as request_before, redirect_stdout(output), redirect_stderr(io.StringIO()):
            self.assertEqual(6, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        self.assertEqual("TIMED_OUT\n", output.getvalue())
        request_before.assert_called_once()

    def test_wait_fails_closed_for_drift_malformed_unknown_and_nonmonotonic_results(self) -> None:
        cases = (
            ({**wait_status(), "extra": True}, "extra field"),
            ({**wait_status(), "state": "UNKNOWN"}, "unknown state"),
            ({**wait_status(), "name": "other"}, "name drift"),
        )
        for response, label in cases:
            with self.subTest(label=label):
                output = io.StringIO()
                error = io.StringIO()
                with patch("lumi_eggcracker.cli.time.monotonic", return_value=10.0), patch(
                    "lumi_eggcracker.cli.request_before", return_value=response
                ), redirect_stdout(output), redirect_stderr(error):
                    self.assertEqual(
                        4, main(["wait", "--name", "demo", "--timeout-seconds", "1"])
                    )
                self.assertEqual("", output.getvalue())
                self.assertEqual("ERROR\n", error.getvalue())

        first = wait_status()
        second = wait_status(state="TERMINATED", run_id="b" * 24)
        output = io.StringIO()
        error = io.StringIO()
        with patch(
            "lumi_eggcracker.cli.time.monotonic",
            side_effect=[10.0, 10.0, 10.0, 10.0, 10.1, 10.1],
        ), patch(
            "lumi_eggcracker.cli.request_before", side_effect=[first, second]
        ) as request_before, patch("lumi_eggcracker.cli.time.sleep"), redirect_stdout(
            output
        ), redirect_stderr(error):
            self.assertEqual(4, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        self.assertEqual(2, request_before.call_count)
        self.assertEqual("", output.getvalue())
        self.assertEqual("ERROR\n", error.getvalue())

        output = io.StringIO()
        error = io.StringIO()
        with patch(
            "lumi_eggcracker.cli.time.monotonic", side_effect=[10.0, 9.0]
        ), patch("lumi_eggcracker.cli.request_before") as request_before, redirect_stdout(
            output
        ), redirect_stderr(error):
            self.assertEqual(4, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
        request_before.assert_not_called()
        self.assertEqual("", output.getvalue())
        self.assertEqual("ERROR\n", error.getvalue())

    def test_wait_timeout_argument_is_bounded_and_does_not_request(self) -> None:
        for timeout in ("0", "301", "1.0", "true"):
            with self.subTest(timeout=timeout), patch(
                "lumi_eggcracker.cli.request_before"
            ) as request_before, redirect_stderr(io.StringIO()):
                with self.assertRaises(SystemExit) as raised:
                    main(["wait", "--name", "demo", "--timeout-seconds", timeout])
                self.assertEqual(2, raised.exception.code)
                request_before.assert_not_called()
        with patch("lumi_eggcracker.cli.request_before") as request_before, redirect_stderr(
            io.StringIO()
        ):
            with self.assertRaises(SystemExit) as raised:
                main(
                    [
                        "wait",
                        "--name",
                        "demo",
                        "--timeout-seconds",
                        "1",
                        "--timeout-seconds",
                        "2",
                    ]
                )
            self.assertEqual(2, raised.exception.code)
            request_before.assert_not_called()

    def test_wait_uses_only_status_action_and_existing_status_still_uses_request(self) -> None:
        terminal = wait_status(state="COMPLETED_ALLOWED")
        with patch("lumi_eggcracker.cli.time.monotonic", return_value=10.0), patch(
            "lumi_eggcracker.cli.request_before", return_value=terminal
        ) as request_before, patch(
            "lumi_eggcracker.cli.request", return_value={"state": "RUNNING"}
        ) as request:
            with redirect_stdout(io.StringIO()):
                self.assertEqual(0, main(["wait", "--name", "demo", "--timeout-seconds", "1"]))
            with redirect_stdout(io.StringIO()):
                self.assertEqual(0, main(["status", "--name", "demo"]))
        request_before.assert_called_once()
        self.assertEqual("status", request_before.call_args.args[0])
        request.assert_called_once_with("status", name="demo")

    def test_execution_policy_create_requires_root(self) -> None:
        with patch("lumi_eggcracker.cli.os.geteuid", return_value=1001, create=True):
            self.assertEqual(4, main(["exec-policy", "create", "--name", "demo", "--", "/bin/sh"]))

    def test_duplicate_identifier_options_are_rejected_before_request(self) -> None:
        policy_a = "a" * 24
        policy_b = "b" * 24
        cases = (
            ["start", "--name", "first", "--name", "second", "--max-pids", "8", "--", "/bin/true"],
            ["start", "--name", "demo", "--exec-policy", policy_a, "--exec-policy", policy_b, "--max-pids", "8", "--", "/bin/true"],
            ["kill", "--name", "first", "--name", "second", "--receipt", "/tmp/receipt.json"],
            ["status", "--name", "first", "--name", "second"],
            ["approve", "--name", "first", "--name", "second", "--uid", "1001", "--", "/bin/true"],
            ["revoke", "--name", "first", "--name", "second"],
            ["exec-policy", "create", "--name", "first", "--name", "second", "--", "/bin/true"],
            ["exec-policy", "revoke", "--policy-id", policy_a, "--policy-id", policy_b],
        )
        for values in cases:
            with self.subTest(values=values), patch(
                "lumi_eggcracker.cli.request"
            ) as request, redirect_stderr(io.StringIO()):
                with self.assertRaises(SystemExit) as raised:
                    main(values)
                self.assertEqual(2, raised.exception.code)
                request.assert_not_called()