forked from hyphen-2025/cyber-pilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ralphex_delegation.py
More file actions
1572 lines (1365 loc) · 69.4 KB
/
test_ralphex_delegation.py
File metadata and controls
1572 lines (1365 loc) · 69.4 KB
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Integration tests for the ralphex delegation feature.
Covers all 14 acceptance criteria from the feature spec (section 7):
AC-01: cypilot-ralphex skill discovers ralphex on PATH and persists resolved path
AC-02: Previously persisted path is reused without re-discovery
AC-03: Missing ralphex produces diagnostic output with install guidance, not hard error
AC-04: ralphex --init can be invoked for project-local bootstrap on user request
AC-05: Plan outputs exported into ralphex-compatible Markdown with Validation Commands and Task sections
AC-06: Export target directory resolved from ralphex config precedence
AC-07: Exported plans contain only bounded SDLC slices, not the entire kit
AC-08: One Cypilot phase maps to one Task block
AC-09: Delegation invokes ralphex with correct mode flags
AC-10: --worktree only appended for full mode and tasks-only, not review-only
AC-11: Review-only delegation verifies committed changes before invoking ralphex --review
AC-12: Post-run handoff reports status, output refs, and re-runs validation commands
AC-13: Integration is fully optional — projects without ralphex have zero behavioral change
AC-14: No Cypilot SDLC assets duplicated into .ralphex/
"""
import builtins
import os
import subprocess
import sys
import textwrap
import pytest
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest.mock import patch, MagicMock
sys.path.insert(0, str(Path(__file__).parent.parent / "skills" / "cypilot" / "scripts"))
from cypilot.ralphex_discover import discover, validate, persist_path, INSTALL_GUIDANCE
from cypilot.ralphex_export import (
compile_delegation_plan,
generate_review_artifacts,
map_phase_to_task,
resolve_plans_dir,
build_delegation_command,
check_review_precondition,
read_handoff_status,
check_completed_plans,
run_validation_commands,
report_handoff,
DelegationLifecycle,
check_bootstrap_needed,
run_delegation,
REVIEW_PROMPT_RELATIVES,
)
# -- Fixtures ----------------------------------------------------------------
MINIMAL_PLAN_TOML = textwrap.dedent("""\
[plan]
task = "Implement widget-feature"
type = "implement"
target = "FEATURE"
[[phases]]
number = 1
title = "Widget Factory"
slug = "widget-factory"
file = "phase-01.md"
status = "pending"
kind = "delivery"
depends_on = []
input_files = ["architecture/features/widget.md"]
output_files = ["src/widget.py", "tests/test_widget.py"]
[[phases]]
number = 2
title = "Widget Validator"
slug = "widget-validator"
file = "phase-02.md"
status = "pending"
kind = "delivery"
depends_on = [1]
input_files = []
output_files = ["src/validator.py"]
""")
PHASE_01_CONTENT = textwrap.dedent("""\
```toml
[phase]
plan = "widget-feature"
number = 1
total = 2
type = "implement"
title = "Widget Factory"
depends_on = []
input_files = ["architecture/features/widget.md"]
output_files = ["src/widget.py", "tests/test_widget.py"]
outputs = []
inputs = []
```
## What
Build the widget factory module.
## Task
1. Read design spec.
2. Implement WidgetFactory class.
## Acceptance Criteria
- [ ] WidgetFactory class exists
- [ ] Unit tests pass
""")
PHASE_02_CONTENT = textwrap.dedent("""\
```toml
[phase]
plan = "widget-feature"
number = 2
total = 2
type = "implement"
title = "Widget Validator"
depends_on = [1]
input_files = []
output_files = ["src/validator.py"]
outputs = []
inputs = []
```
## What
Add validation to widgets.
## Rules
### Engineering
- **TDD**: Write failing test first
### Quality
- **Readability**: Clear naming
### MUST NOT
- No hardcoded secrets
## Task
1. Write failing tests.
2. Implement validate() method.
## Acceptance Criteria
- [ ] validate() rejects invalid widgets
""")
def _make_plan_dir(tmp: str) -> str:
"""Create a minimal plan directory for integration tests."""
plan_dir = Path(tmp) / "test-plan"
plan_dir.mkdir()
(plan_dir / "plan.toml").write_text(MINIMAL_PLAN_TOML, encoding="utf-8")
(plan_dir / "phase-01.md").write_text(PHASE_01_CONTENT, encoding="utf-8")
(plan_dir / "phase-02.md").write_text(PHASE_02_CONTENT, encoding="utf-8")
return str(plan_dir)
def _make_plan_dir_with_lifecycle_action(
tmp: str,
lifecycle: str,
*,
lifecycle_action: str | None = None,
phase_numbers: tuple[int, int] = (1, 2),
) -> str:
"""Create a minimal plan directory with lifecycle metadata and optional action."""
plan_dir = Path(tmp) / f"test-plan-{lifecycle}-{lifecycle_action or 'none'}"
plan_dir.mkdir()
plan_toml = MINIMAL_PLAN_TOML.replace(
'type = "implement"\n',
(
'type = "implement"\n'
f'lifecycle = "{lifecycle}"\n'
'lifecycle_status = "pending"\n'
f'plan_dir = "{plan_dir.as_posix()}"\n'
f'active_plan_dir = "{plan_dir.as_posix()}"\n'
),
1,
)
# Replace higher number first to avoid collision when phase_numbers[0]
# equals the original value of the second phase (e.g. (2, 4) would turn
# the first 'number = 1' into 'number = 2', shadowing the real second phase).
plan_toml = plan_toml.replace('number = 2\n', f'number = {phase_numbers[1]}\n', 1)
plan_toml = plan_toml.replace('number = 1\n', f'number = {phase_numbers[0]}\n', 1)
if lifecycle_action is not None:
plan_toml += f'\n[decisions]\nlifecycle_action = "{lifecycle_action}"\n'
(plan_dir / "plan.toml").write_text(plan_toml, encoding="utf-8")
(plan_dir / "phase-01.md").write_text(PHASE_01_CONTENT, encoding="utf-8")
(plan_dir / "phase-02.md").write_text(PHASE_02_CONTENT, encoding="utf-8")
return str(plan_dir)
def _make_plan_dir_with_lifecycle(tmp: str, lifecycle: str) -> str:
"""Create a minimal plan directory with lifecycle metadata for export tests."""
plan_dir = Path(tmp) / f"test-plan-{lifecycle}"
plan_dir.mkdir()
plan_toml = MINIMAL_PLAN_TOML.replace(
'type = "implement"\n',
(
'type = "implement"\n'
f'lifecycle = "{lifecycle}"\n'
'lifecycle_status = "pending"\n'
f'plan_dir = "{plan_dir.as_posix()}"\n'
f'active_plan_dir = "{plan_dir.as_posix()}"\n'
),
1,
)
(plan_dir / "plan.toml").write_text(plan_toml, encoding="utf-8")
(plan_dir / "phase-01.md").write_text(PHASE_01_CONTENT, encoding="utf-8")
(plan_dir / "phase-02.md").write_text(PHASE_02_CONTENT, encoding="utf-8")
return str(plan_dir)
# -- AC-01: Discovery persists resolved path ----------------------------------
class TestAC01DiscoveryAndPersistence:
"""AC-01: cypilot-ralphex discovers ralphex on PATH and persists resolved path."""
def test_discover_finds_and_persist_writes(self):
"""Full flow: discover on PATH, then persist to core.toml."""
config = {"integrations": {"ralphex": {"executable_path": ""}}}
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/local/bin/ralphex"):
path = discover(config)
assert path == "/usr/local/bin/ralphex"
# Persist the discovered path
with TemporaryDirectory() as tmp:
config_path = Path(tmp) / "core.toml"
config_path.write_text(
'[integrations.ralphex]\nexecutable_path = ""\n',
encoding="utf-8",
)
persist_path(config_path, path)
import tomllib
with open(config_path, "rb") as f:
data = tomllib.load(f)
assert data["integrations"]["ralphex"]["executable_path"] == "/usr/local/bin/ralphex"
# -- AC-02: Persisted path reused without re-discovery -------------------------
class TestAC02PersistedPathReuse:
"""AC-02: Previously persisted path is reused on subsequent invocations."""
def test_reuses_persisted_path_when_path_lookup_misses(self):
"""discover() returns persisted path without re-scanning PATH."""
with TemporaryDirectory() as tmp:
fake_bin = Path(tmp) / "ralphex"
fake_bin.write_text("#!/bin/sh\n", encoding="utf-8")
fake_bin.chmod(0o755)
config = {"integrations": {"ralphex": {"executable_path": str(fake_bin)}}}
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
result = discover(config)
assert result == str(fake_bin)
# -- AC-03: Missing ralphex → diagnostic, not hard error ----------------------
class TestAC03MissingRalphexDiagnostic:
"""AC-03: Missing ralphex produces diagnostic with install guidance, not hard error."""
def test_missing_returns_none_not_exception(self):
"""discover() returns None, does not raise."""
config = {}
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
result = discover(config)
assert result is None
def test_validate_none_gives_install_guidance(self):
"""validate(None) returns unavailable with install instructions."""
result = validate(None)
assert result["status"] == "unavailable"
assert "brew" in result["message"].lower() or "homebrew" in result["message"].lower()
assert "go install" in result["message"].lower()
def test_doctor_check_warns_not_fails(self):
"""Doctor inst-check-ralphex returns WARN, not FAIL, when missing."""
from cypilot.commands.doctor import _check_ralphex
with TemporaryDirectory() as tmp:
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
result = _check_ralphex(Path(tmp))
assert result["level"] == "WARN"
assert result["name"] == "inst-check-ralphex"
def test_doctor_reads_cypilot_adapter_config(self):
"""Doctor discovers ralphex via cypilot/config/core.toml (not just .bootstrap)."""
from cypilot.commands._core_config import load_core_config as _load_core_config
with TemporaryDirectory() as tmp:
project_root = Path(tmp)
# Only create cypilot adapter config (no .bootstrap)
config_dir = project_root / "cypilot" / "config"
config_dir.mkdir(parents=True)
(config_dir / "core.toml").write_text(
'[ralphex]\npath = "/usr/local/bin/ralphex"\n',
encoding="utf-8",
)
config = _load_core_config(project_root)
assert config.get("ralphex", {}).get("path") == "/usr/local/bin/ralphex"
def test_doctor_prefers_bootstrap_over_cypilot_config(self):
"""Doctor checks .bootstrap before cypilot adapter config."""
from cypilot.commands._core_config import load_core_config as _load_core_config
with TemporaryDirectory() as tmp:
project_root = Path(tmp)
# Create both adapter configs with different paths
for adapter, path_val in [(".bootstrap", "/opt/bootstrap/ralphex"), ("cypilot", "/opt/cypilot/ralphex")]:
config_dir = project_root / adapter / "config"
config_dir.mkdir(parents=True)
(config_dir / "core.toml").write_text(
f'[ralphex]\npath = "{path_val}"\n',
encoding="utf-8",
)
config = _load_core_config(project_root)
assert config["ralphex"]["path"] == "/opt/bootstrap/ralphex"
def test_doctor_check_finds_ralphex_from_cypilot_config(self):
"""Doctor _check_ralphex finds ralphex when path is only in cypilot/config/core.toml."""
from cypilot.commands.doctor import _check_ralphex
with TemporaryDirectory() as tmp:
project_root = Path(tmp)
config_dir = project_root / "cypilot" / "config"
config_dir.mkdir(parents=True)
# Write a config with the correct key structure for discover()
fake_bin = project_root / "fake-ralphex"
fake_bin.write_text("#!/bin/sh\necho ralphex", encoding="utf-8")
fake_bin.chmod(0o755)
(config_dir / "core.toml").write_text(
f'[integrations.ralphex]\nexecutable_path = "{fake_bin}"\n',
encoding="utf-8",
)
# Patch shutil.which to return None (not on PATH) so discover
# falls through to the persisted config path
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
with patch("cypilot.ralphex_discover.validate", return_value={"status": "available", "version": "0.1.0"}):
result = _check_ralphex(project_root)
assert result["level"] == "PASS"
# -- AC-04: ralphex --init bootstrap on user request ---------------------------
class TestAC04BootstrapInit:
"""AC-04: ralphex --init can be invoked for project-local bootstrap on user request."""
def test_bootstrap_needed_when_no_config(self):
"""check_bootstrap_needed detects missing .ralphex/config."""
with TemporaryDirectory() as tmp:
result = check_bootstrap_needed(tmp)
assert result["needed"] is True
def test_bootstrap_not_needed_when_config_exists(self):
"""check_bootstrap_needed returns not needed when config present."""
with TemporaryDirectory() as tmp:
ralphex_dir = Path(tmp) / ".ralphex"
ralphex_dir.mkdir()
(ralphex_dir / "config").write_text('plans_dir = "docs/plans"\n', encoding="utf-8")
result = check_bootstrap_needed(tmp)
assert result["needed"] is False
def test_never_runs_init_automatically(self):
"""check_bootstrap_needed never executes ralphex --init itself."""
with TemporaryDirectory() as tmp:
with patch("cypilot.ralphex_export.subprocess.run") as mock_run:
check_bootstrap_needed(tmp)
mock_run.assert_not_called()
def test_message_mentions_user_approval(self):
"""Bootstrap message explicitly requires user approval."""
with TemporaryDirectory() as tmp:
result = check_bootstrap_needed(tmp)
msg = result["message"].lower()
assert "approval" in msg or "approve" in msg or "explicit" in msg
# -- AC-05: Export produces ralphex-compatible Markdown ------------------------
class TestAC05ExportFormat:
"""AC-05: Exported plans have ## Validation Commands and ### Task N: sections."""
def test_exported_plan_has_validation_commands(self):
"""Compiled plan contains ## Validation Commands section."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
assert "## Validation Commands" in result
def test_exported_plan_has_task_sections(self):
"""Compiled plan contains ### Task N: blocks."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
assert "### Task 1:" in result
assert "### Task 2:" in result
assert "**Original Phase File:**" in result
assert "**Execution Prompt:**" in result
assert "`test-plan/phase-01.md`" in result
assert "`test-plan/phase-02.md`" in result
def test_sections_in_ralphex_grammar_order(self):
"""Title, Validation Commands, Tasks appear in correct order."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
title_pos = result.index("# ")
validation_pos = result.index("## Validation Commands")
task1_pos = result.index("### Task 1:")
assert title_pos < validation_pos < task1_pos
def test_archive_lifecycle_adds_final_task(self):
"""Archive lifecycle exports an additional final lifecycle task."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir_with_lifecycle(tmp, "archive")
result = compile_delegation_plan(plan_dir)
assert "### Task 3: Plan lifecycle — archive plan files" in result
assert "Move the active plan directory" in result
assert "`.plans/.archive/`" not in result
assert "lifecycle_status = `done`" not in result
assert "`lifecycle_status` is `done`." in result
assert 'Set `lifecycle_status = "ready"` before moving the completed plan directory.' in result
def test_manual_delete_lifecycle_action_adds_final_task(self):
"""Manual lifecycle with chosen delete action exports a final delete task."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir_with_lifecycle_action(
tmp,
"manual",
lifecycle_action="delete",
)
result = compile_delegation_plan(plan_dir)
assert "### Task 3: Plan lifecycle — delete plan files" in result
assert "Delete the active plan directory" in result
assert "Do not delete project files outside the plan directory." in result
def test_manual_without_lifecycle_action_adds_no_final_task(self):
"""Manual lifecycle without a resolved action does not export a synthetic lifecycle task."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir_with_lifecycle_action(tmp, "manual")
result = compile_delegation_plan(plan_dir)
assert "Plan lifecycle —" not in result
def test_unrecognized_lifecycle_raises_error(self):
"""Unrecognized lifecycle value (e.g. legacy 'delete') raises ValueError."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir_with_lifecycle(tmp, "delete")
with pytest.raises(ValueError, match="Unrecognized plan.lifecycle value 'delete'"):
compile_delegation_plan(plan_dir)
def test_lifecycle_task_uses_next_phase_number(self):
"""Synthetic lifecycle task uses max phase number plus one."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir_with_lifecycle_action(
tmp,
"archive",
phase_numbers=(2, 4),
)
result = compile_delegation_plan(plan_dir)
assert "### Task 2: Widget Factory" in result
assert "### Task 4: Widget Validator" in result
assert "### Task 5: Plan lifecycle — archive plan files" in result
assert "Run after Tasks 2, 4 complete successfully." in result
# -- AC-06: Export target from ralphex config precedence -----------------------
class TestAC06ExportTargetPrecedence:
"""AC-06: Export target directory resolved from ralphex config precedence."""
def test_local_config_overrides_default(self):
"""Local .ralphex/config plans_dir takes precedence."""
with TemporaryDirectory() as tmp:
ralphex_dir = Path(tmp) / ".ralphex"
ralphex_dir.mkdir()
(ralphex_dir / "config").write_text(
'plans_dir = "custom/plans"\n', encoding="utf-8"
)
result = resolve_plans_dir(tmp)
assert result == os.path.join(tmp, "custom/plans")
def test_default_is_docs_plans(self):
"""Without any config, falls back to docs/plans/."""
with TemporaryDirectory() as tmp:
with patch.dict(os.environ, {"XDG_CONFIG_HOME": str(Path(tmp) / "no_config")}):
result = resolve_plans_dir(tmp)
assert result == os.path.join(tmp, "docs/plans")
def test_not_hardcoded_or_cypilot_owned(self):
"""Resolved path does not contain .bootstrap or cypilot-specific directories."""
with TemporaryDirectory() as tmp:
with patch.dict(os.environ, {"XDG_CONFIG_HOME": str(Path(tmp) / "no_config")}):
result = resolve_plans_dir(tmp)
assert ".bootstrap" not in result
assert "cypilot" not in result.lower() or "cypilot" in tmp.lower()
# -- AC-07: Exported plans contain only bounded SDLC slices -------------------
class TestAC07BoundedSlices:
"""AC-07: Exported plans contain only bounded SDLC slices, not entire kit."""
def test_guidance_only_from_engineering_and_quality(self):
"""Only Engineering and Quality rules subsections appear in guidance."""
result = map_phase_to_task(PHASE_02_CONTENT, 2)
# Should include Engineering/Quality guidance
assert "TDD" in result or "test first" in result.lower()
assert "Readability" in result or "naming" in result.lower()
# Should NOT include MUST NOT section items
assert "hardcoded secrets" not in result.lower()
def test_excluded_sections_not_in_output(self):
"""Output Format, Preamble, etc. are excluded from task blocks."""
phase_with_excluded = PHASE_01_CONTENT + "\n## Output Format\n\nThis should not appear.\n"
result = map_phase_to_task(phase_with_excluded, 1)
assert "This should not appear" not in result
assert "Prioritize the phase frontmatter plus `What`, `Rules`, `Input`, `Task`, `Acceptance Criteria`, and `Output Format`." in result
assert "**Ignore:**" in result
# -- AC-08: One phase maps to one Task block -----------------------------------
class TestAC08PhaseToTaskMapping:
"""AC-08: One Cypilot phase maps to one ### Task N: block."""
def test_each_phase_is_one_task_block(self):
"""compile_delegation_plan produces exactly one Task block per phase."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
# Count ### Task N: blocks
import re
task_blocks = re.findall(r"### Task \d+:", result)
assert len(task_blocks) == 2 # matches 2 phases in fixture
def test_phase_number_matches_task_number(self):
"""Phase 1 → Task 1, Phase 2 → Task 2."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
assert "### Task 1: Widget Factory" in result
assert "### Task 2: Widget Validator" in result
assert "test-plan/phase-01.md" in result
assert "test-plan/phase-02.md" in result
# -- AC-09: Delegation uses correct mode flags ---------------------------------
class TestAC09DelegationModeFlags:
"""AC-09: Delegation invokes ralphex with correct mode flags."""
def test_execute_mode(self):
"""Execute mode: ralphex <plan.md>."""
cmd = build_delegation_command("/bin/ralphex", "plan.md", "execute")
assert cmd == ["/bin/ralphex", "plan.md"]
def test_tasks_only_mode(self):
"""Tasks-only mode: --tasks-only flag appended."""
cmd = build_delegation_command("/bin/ralphex", "plan.md", "tasks-only")
assert "--tasks-only" in cmd
def test_review_mode(self):
"""Review mode: --review flag."""
cmd = build_delegation_command("/bin/ralphex", None, "review")
assert "--review" in cmd
def test_serve_flag(self):
"""Dashboard mode: --serve flag appended."""
cmd = build_delegation_command("/bin/ralphex", "plan.md", "execute", serve=True)
assert "--serve" in cmd
def test_worktree_flag(self):
"""Worktree mode: --worktree flag appended."""
cmd = build_delegation_command("/bin/ralphex", "plan.md", "execute", worktree=True)
assert "--worktree" in cmd
# -- AC-10: --worktree constraint enforcement ----------------------------------
class TestAC10WorktreeConstraint:
"""AC-10: --worktree only for full mode and tasks-only, not review-only."""
def test_worktree_allowed_for_execute(self):
cmd = build_delegation_command("/bin/ralphex", "p.md", "execute", worktree=True)
assert "--worktree" in cmd
def test_worktree_allowed_for_tasks_only(self):
cmd = build_delegation_command("/bin/ralphex", "p.md", "tasks-only", worktree=True)
assert "--worktree" in cmd
def test_worktree_not_appended_for_review(self):
"""--worktree is silently dropped for review mode."""
cmd = build_delegation_command("/bin/ralphex", None, "review", worktree=True)
assert "--worktree" not in cmd
# -- AC-11: Review-only verifies committed changes -----------------------------
class TestAC11ReviewPrecondition:
"""AC-11: Review-only verifies committed changes before invoking ralphex --review."""
def test_passes_when_commits_ahead(self):
"""Precondition passes with commits ahead of default branch."""
proc = MagicMock(returncode=0, stdout="abc123\ndef456\n", stderr="")
with patch("cypilot.ralphex_export.subprocess.run", return_value=proc):
result = check_review_precondition("main")
assert result["ok"] is True
assert result["commit_count"] == 2
def test_fails_when_no_commits_ahead(self):
"""Precondition fails when no commits ahead."""
proc = MagicMock(returncode=0, stdout="", stderr="")
with patch("cypilot.ralphex_export.subprocess.run", return_value=proc):
result = check_review_precondition("main")
assert result["ok"] is False
assert "no committed changes" in result["message"].lower()
def test_fails_on_git_error(self):
"""Precondition fails gracefully on git error."""
proc = MagicMock(returncode=128, stdout="", stderr="fatal: bad revision")
with patch("cypilot.ralphex_export.subprocess.run", return_value=proc):
result = check_review_precondition("main")
assert result["ok"] is False
# -- AC-12: Post-run handoff reports status and re-runs validation -------------
class TestAC12PostRunHandoff:
"""AC-12: Post-run handoff reports status, output refs, and re-runs validation."""
def test_handoff_success_flow(self):
"""Full handoff: read status → check completed → run validation → report."""
# Step 1: Read status
status = read_handoff_status(exit_code=0, output_refs=["out/report.md"])
assert status["status"] == "success"
# Step 2: Check completed plans
with TemporaryDirectory() as tmp:
completed = Path(tmp) / "completed"
completed.mkdir()
(completed / "task.md").write_text("# Done\n", encoding="utf-8")
plans = check_completed_plans(tmp, "task")
assert plans["found"] is True
# Step 3: Run validation commands
proc = MagicMock(returncode=0, stdout="ok\n", stderr="")
with patch("cypilot.ralphex_export.subprocess.run", return_value=proc):
validation = run_validation_commands(["python -m pytest tests/"])
assert validation["passed"] is True
# Step 4: Report handoff
report = report_handoff(
plan_file="docs/plans/task.md",
mode="execute",
exit_code=0,
output_refs=["out/report.md"],
completed_plan_path=str(completed / "task.md"),
validation_passed=True,
)
assert report["status"] == "success"
assert report["validation_passed"] is True
assert report["output_refs"] == ["out/report.md"]
def test_handoff_failure_flow(self):
"""Handoff reports failure when ralphex exits non-zero."""
status = read_handoff_status(exit_code=1, output_refs=[])
assert status["status"] == "failed"
report = report_handoff(
plan_file="p.md",
mode="execute",
exit_code=1,
output_refs=[],
completed_plan_path=None,
validation_passed=False,
)
assert report["status"] == "failed"
def test_lifecycle_tracks_full_flow(self):
"""DelegationLifecycle tracks export → delegate → complete."""
lc = DelegationLifecycle()
lc.export()
lc.delegate()
lc.complete()
assert lc.state == "completed"
assert len(lc.history) == 3
# -- AC-13: Fully optional integration ----------------------------------------
class TestAC13OptionalIntegration:
"""AC-13: Projects without ralphex use normal Cypilot workflows with zero change."""
def test_discover_returns_none_no_side_effects(self):
"""When ralphex is absent, discover returns None without modifying anything."""
config = {}
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
result = discover(config)
assert result is None
# Config unchanged
assert config == {}
def test_validate_none_is_informational_only(self):
"""validate(None) provides guidance without raising or failing."""
result = validate(None)
assert result["status"] == "unavailable"
# Not a hard error — caller decides how to handle
def test_doctor_check_does_not_block_on_missing(self):
"""Doctor returns WARN (not FAIL) when ralphex is missing."""
from cypilot.commands.doctor import _check_ralphex
with TemporaryDirectory() as tmp:
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
result = _check_ralphex(Path(tmp))
assert result["level"] == "WARN"
def test_doctor_exit_code_zero_when_only_warns(self):
"""Doctor returns exit 0 even with WARN-level checks."""
from cypilot.commands.doctor import cmd_doctor
with patch("cypilot.ralphex_discover.shutil.which", return_value=None):
exit_code = cmd_doctor(["--root", "/tmp"])
assert exit_code == 0
# -- AC-14: No SDLC asset duplication into .ralphex/ ---------------------------
class TestAC14NoSDLCDuplication:
"""AC-14: No Cypilot SDLC assets duplicated into .ralphex/."""
def test_export_does_not_write_to_ralphex_dir(self):
"""compile_delegation_plan output contains no .ralphex/ file writes."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
# Output is a string, not written to disk by compile_delegation_plan
assert ".ralphex/" not in result
def test_no_kit_content_in_exported_plan(self):
"""Exported plan does not contain kit file references (rules.md, checklist.md)."""
with TemporaryDirectory() as tmp:
plan_dir = _make_plan_dir(tmp)
result = compile_delegation_plan(plan_dir)
# Should not contain references to kit workflow files
assert "rules.md" not in result.lower()
assert "checklist.md" not in result.lower()
assert "kit workflows" not in result.lower()
def test_bootstrap_check_does_not_copy_sdlc(self):
"""check_bootstrap_needed only reports — never copies SDLC assets."""
with TemporaryDirectory() as tmp:
result = check_bootstrap_needed(tmp)
# The .ralphex directory should not have been created
assert not (Path(tmp) / ".ralphex").exists()
# -- Review artifact generation integration -----------------------------------
class TestReviewArtifactGeneration:
"""Review artifact generation produces deterministic .ralphex/ overrides."""
def _make_project_with_sources(self, tmp):
"""Create a project with canonical Cypilot sources for review artifact generation."""
repo_root = Path(tmp) / "repo"
repo_root.mkdir()
(repo_root / ".git").mkdir()
(repo_root / ".ralphex" / "config").parent.mkdir(parents=True, exist_ok=True)
(repo_root / ".ralphex" / "config").write_text(
'plans_dir = "docs/plans"\n', encoding="utf-8"
)
prompts = repo_root / ".ralphex" / "prompts"
prompts.mkdir(parents=True)
(prompts / "review_first.txt").write_text(
"# first review prompt\n\nOriginal first review body.\n",
encoding="utf-8",
)
(prompts / "review_second.txt").write_text(
"# second review prompt\n\nOriginal second review body.\n",
encoding="utf-8",
)
core = repo_root / ".bootstrap" / ".core"
workflows = core / "workflows"
workflows.mkdir(parents=True)
(workflows / "analyze.md").write_text("# Analyze\n", encoding="utf-8")
reqs = core / "requirements"
reqs.mkdir(parents=True)
for name in ("bug-finding.md", "prompt-engineering.md",
"prompt-bug-finding.md", "code-checklist.md"):
(reqs / name).write_text(f"# {name}\n", encoding="utf-8")
plan_dir = _make_plan_dir(tmp)
return str(repo_root), plan_dir
def test_review_artifacts_generated_at_deterministic_path(self):
"""generate_review_artifacts creates override at .ralphex/prompts/."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_project_with_sources(tmp)
result = generate_review_artifacts(plan_dir, repo_root)
assert len(result["artifacts"]) == 2
assert Path(result["artifacts"][0]).exists()
assert result["relative_paths"] == list(REVIEW_PROMPT_RELATIVES)
def test_review_artifacts_do_not_alter_compiled_plan(self):
"""Generating review artifacts does not change compile_delegation_plan output."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_project_with_sources(tmp)
plan_before = compile_delegation_plan(plan_dir)
generate_review_artifacts(plan_dir, repo_root)
plan_after = compile_delegation_plan(plan_dir)
assert plan_before == plan_after
def test_review_override_not_in_compiled_plan(self):
"""The .ralphex/ review override is not referenced in the compiled plan."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_project_with_sources(tmp)
generate_review_artifacts(plan_dir, repo_root)
plan = compile_delegation_plan(plan_dir)
assert ".ralphex/prompts" not in plan
def test_non_review_delegation_unaffected(self):
"""Non-review delegation (execute mode) continues to work identically."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_project_with_sources(tmp)
config = {}
mock_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/bin/ralphex"), \
patch("cypilot.ralphex_discover.subprocess.run", return_value=mock_proc):
result = run_delegation(
config=config,
plan_dir=plan_dir,
repo_root=repo_root,
mode="execute",
dry_run=True,
)
assert result["status"] == "ready"
assert "### Task 1:" in Path(result["plan_file"]).read_text(encoding="utf-8")
# -- Orchestration entrypoint tests ------------------------------------------
class TestRunDelegation:
"""Tests for the canonical run_delegation() orchestration entrypoint."""
def _make_delegatable_project(self, tmp):
"""Create a project directory with plan, ralphex config, and mock ralphex."""
repo_root = Path(tmp) / "repo"
repo_root.mkdir()
# ralphex config so bootstrap gate passes
ralphex_dir = repo_root / ".ralphex"
ralphex_dir.mkdir()
(ralphex_dir / "config").write_text(
'plans_dir = "docs/plans"\n', encoding="utf-8"
)
# Plan directory
plan_dir = _make_plan_dir(tmp)
return str(repo_root), plan_dir
def test_dry_run_returns_ready_with_command(self):
"""dry_run=True assembles command without invoking, returns 'ready'."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_delegatable_project(tmp)
config = {}
mock_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/bin/ralphex"), \
patch("cypilot.ralphex_discover.subprocess.run", return_value=mock_proc):
result = run_delegation(
config=config,
plan_dir=plan_dir,
repo_root=repo_root,
mode="execute",
dry_run=True,
)
assert result["status"] == "ready"
assert result["ralphex_path"] == "/usr/bin/ralphex"
assert result["plan_file"] is not None
assert Path(result["plan_file"]).exists()
assert len(result["command"]) > 0
assert result["command"][0] == "/usr/bin/ralphex"
assert result["lifecycle_state"] == "exported"
assert result["error"] is None
def test_full_delegation_returns_delegated(self):
"""Non-dry-run returns 'delegated' with lifecycle in completed state."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_delegatable_project(tmp)
config = {}
discover_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
invoke_proc = MagicMock(returncode=0)
invoke_proc.communicate.return_value = ("Done\n", "")
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/bin/ralphex"), \
patch("cypilot.ralphex_discover.subprocess.run", return_value=discover_proc), \
patch("cypilot.ralphex_export.subprocess.Popen", return_value=invoke_proc):
result = run_delegation(
config=config,
plan_dir=plan_dir,
repo_root=repo_root,
mode="execute",
dry_run=False,
)
assert result["status"] == "delegated"
assert result["lifecycle_state"] == "completed"
assert result["returncode"] == 0
def test_full_delegation_streams_output_when_requested(self):
"""stream_output=True invokes ralphex with inherited stdio."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_delegatable_project(tmp)
config = {}
discover_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
invoke_proc = MagicMock(returncode=0)
invoke_kwargs = {}
def _capture_popen(*args, **kwargs):
invoke_kwargs.update(kwargs)
return invoke_proc
invoke_proc.communicate.return_value = (None, None)
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/bin/ralphex"), \
patch("cypilot.ralphex_discover.subprocess.run", return_value=discover_proc), \
patch("cypilot.ralphex_export.subprocess.Popen", side_effect=_capture_popen):
result = run_delegation(
config=config,
plan_dir=plan_dir,
repo_root=repo_root,
mode="execute",
dry_run=False,
stream_output=True,
)
assert result["status"] == "delegated"
assert result["lifecycle_state"] == "completed"
assert result["stdout"] is None
assert result["stderr"] is None
assert result["returncode"] == 0
assert "capture_output" not in invoke_kwargs
assert "text" not in invoke_kwargs
def test_nonzero_exit_sets_failed_lifecycle(self):
"""Regression: non-zero subprocess return code transitions lifecycle to failed."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_delegatable_project(tmp)
config = {}
discover_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
invoke_proc = MagicMock(returncode=1)
invoke_proc.communicate.return_value = ("", "task failed")
with patch("cypilot.ralphex_discover.shutil.which", return_value="/usr/bin/ralphex"), \
patch("cypilot.ralphex_discover.subprocess.run", return_value=discover_proc), \
patch("cypilot.ralphex_export.subprocess.Popen", return_value=invoke_proc):
result = run_delegation(
config=config,
plan_dir=plan_dir,
repo_root=repo_root,
mode="execute",
dry_run=False,
)
assert result["status"] == "error"
assert result["lifecycle_state"] == "failed"
assert result["returncode"] == 1
def _mock_tty_open(self, answer):
"""Return a context manager that mocks /dev/tty open to return *answer*.
Falls through to the real ``open`` for any other path so file I/O in
the test helpers is unaffected.
"""
real_open = builtins.open
mock_tty = MagicMock()
mock_tty.readline.return_value = answer + "\n"
def _side_effect(path, *args, **kwargs):
if str(path) == "/dev/tty":
return mock_tty
return real_open(path, *args, **kwargs)
return patch("builtins.open", side_effect=_side_effect)
def test_timeout_prompt_enter_continues_waiting(self):
"""Timeout prompt continues waiting on Enter and preserves successful completion."""
with TemporaryDirectory() as tmp:
repo_root, plan_dir = self._make_delegatable_project(tmp)
config = {}
discover_proc = MagicMock(returncode=0, stdout="ralphex v1.0.0\n", stderr="")
invoke_proc = MagicMock(returncode=0)
invoke_proc.communicate.side_effect = [
subprocess.TimeoutExpired(["/usr/bin/ralphex"], 3600),
("Done\n", ""),