ralph-workflow 0.7.18

PROMPT-driven multi-agent orchestrator for git repos
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
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
use super::super::common::TestFixture;
use crate::prompts::template_context::TemplateContext;
use crate::prompts::template_registry::TemplateRegistry;
use crate::prompts::PromptHistoryEntry;
use crate::reducer::boundary::MainEffectHandler;
use crate::reducer::event::{AgentEvent, PipelineEvent, PipelinePhase, PromptInputEvent};
use crate::reducer::state::{
    AgentChainState, CommitState, ContinuationState, MaterializedCommitInputs,
    MaterializedPromptInput, PipelineState, PromptInputKind, PromptInputRepresentation,
    PromptInputsState, PromptMaterializationReason, PromptMode, SameAgentRetryReason,
};
use crate::workspace::{MemoryWorkspace, Workspace};
use std::fs;
use std::panic::{catch_unwind, AssertUnwindSafe};
use tempfile::tempdir;

#[test]
fn test_prepare_commit_prompt_does_not_emit_generation_started() {
    let mut fixture = TestFixture::new();
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.iteration = 1;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    let result = handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "diff --git a/a b/a\n+change\n",
            crate::reducer::state::PromptMode::Normal,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    assert!(matches!(
        result.event,
        PipelineEvent::Commit(crate::reducer::event::CommitEvent::PromptPrepared { attempt: 1 })
    ));
    assert!(
        result.additional_events.iter().all(|event| !matches!(
            event,
            PipelineEvent::Commit(crate::reducer::event::CommitEvent::GenerationStarted)
        )),
        "prepare commit prompt should not emit commit_generation_started"
    );
}

#[test]
fn test_prepare_commit_prompt_emits_template_rendered_on_validation_failure() {
    let tempdir = tempdir().expect("create temp dir");
    let template_path = tempdir.path().join("commit_message_xml.txt");
    fs::write(&template_path, "Diff:\n{{DIFF}}\nMissing: {{MISSING}}\n")
        .expect("write commit template");

    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    fixture.template_context =
        TemplateContext::new(TemplateRegistry::new(Some(tempdir.path().to_path_buf())));
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.iteration = 1;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    let result = handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "diff --git a/a b/a\n+change\n",
            PromptMode::Normal,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    assert!(result.ui_events.iter().any(|ev| matches!(
        ev,
        crate::reducer::ui_event::UIEvent::PromptReplayHit { key, was_replayed: false }
            if key == "commit_message_attempt_iter1_1"
    )));

    match result.event {
        PipelineEvent::PromptInput(PromptInputEvent::TemplateRendered {
            phase,
            template_name,
            log,
        }) => {
            assert_eq!(phase, PipelinePhase::CommitMessage);
            assert_eq!(template_name, "commit_message_xml");
            assert!(log.unsubstituted.contains(&"MISSING".to_string()));
        }
        other => panic!("expected TemplateRendered event, got {other:?}"),
    }

    assert!(
        result.additional_events.iter().any(|event| matches!(
            event,
            PipelineEvent::Agent(AgentEvent::TemplateVariablesInvalid { missing_variables, .. })
                if missing_variables.contains(&"MISSING".to_string())
        )),
        "expected TemplateVariablesInvalid with missing variables"
    );
}

#[test]
fn test_prepare_commit_prompt_xsd_retry_uses_commit_xsd_retry_template() {
    // The XSD retry prompt now validates that required input files exist.
    // This test provides those files to verify the retry prompt generation works.
    let workspace = MemoryWorkspace::new_test()
        .with_dir(".agent/tmp")
        .with_file(
            ".agent/tmp/commit_message.xml",
            "<ralph-commit><ralph-subject>test: subject</ralph-subject></ralph-commit>",
        );
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.iteration = 1;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.continuation.last_xsd_error =
        Some("XSD validation failed: MISSING REQUIRED ELEMENT".to_string());

    handler
        .prepare_commit_prompt(&ctx, PromptMode::XsdRetry)
        .expect("prepare_commit_prompt should succeed");

    let prompt = fixture
        .workspace
        .read(std::path::Path::new(".agent/tmp/commit_prompt.txt"))
        .expect("commit_prompt.txt should be written");
    assert!(
        prompt.contains("XSD VALIDATION FAILED - FIX XML ONLY"),
        "Expected commit_xsd_retry prompt template, got: {prompt}"
    );
    assert!(
        prompt.contains("MISSING REQUIRED ELEMENT"),
        "Expected XSD error to be included in retry prompt, got: {prompt}"
    );
    assert!(
        !prompt.contains("diff --git"),
        "XSD retry prompt should not include diff content, got: {prompt}"
    );
}

#[test]
fn test_prepare_commit_prompt_xsd_retry_does_not_replay_stale_prompt_when_diff_content_id_changes()
{
    use crate::reducer::prompt_inputs::sha256_hex_str;

    let workspace = MemoryWorkspace::new_test()
        .with_dir(".agent/tmp")
        .with_file(
            ".agent/tmp/commit_message.xml",
            "<ralph-commit><ralph-subject>test: subject</ralph-subject></ralph-commit>",
        );
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.iteration = 1;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.continuation.xsd_retry_count = 1;
    handler.state.continuation.last_xsd_error = Some("XSD validation failed".to_string());
    handler.state.commit_diff_content_id_sha256 = Some("new_hash".to_string());

    let expected_prompt_content_id = sha256_hex_str(&format!(
        "commit_xsd_retry|diff:{}|xsd_error:{}|consumer:{}",
        handler
            .state
            .commit_diff_content_id_sha256
            .as_deref()
            .expect("diff content id must be set for test"),
        handler
            .state
            .continuation
            .last_xsd_error
            .as_deref()
            .expect("xsd error must be set for test"),
        handler.state.agent_chain.consumer_signature_sha256(),
    ));

    // Arrange: store a stale prompt for the same key but with a different content-id.
    handler.state.prompt_history.insert(
        "commit_message_attempt_iter1_1_xsd_retry_1".to_string(),
        PromptHistoryEntry::new("OLD PROMPT".to_string(), Some("old_hash".to_string())),
    );

    let result = handler
        .prepare_commit_prompt(&ctx, PromptMode::XsdRetry)
        .expect("prepare_commit_prompt should succeed");

    assert!(result.ui_events.iter().any(|ev| matches!(
        ev,
        crate::reducer::ui_event::UIEvent::PromptReplayHit { key, was_replayed: false }
            if key == "commit_message_attempt_iter1_1_xsd_retry_1"
    )));
    assert!(result.additional_events.iter().any(|ev| matches!(
        ev,
        PipelineEvent::PromptInput(PromptInputEvent::PromptCaptured { key, content_id: Some(id), .. })
            if key == "commit_message_attempt_iter1_1_xsd_retry_1" && id == &expected_prompt_content_id
    )));

    let prompt = fixture
        .workspace
        .read(std::path::Path::new(".agent/tmp/commit_prompt.txt"))
        .expect("commit_prompt.txt should be written");
    assert_ne!(prompt, "OLD PROMPT");
    assert!(prompt.contains("XSD VALIDATION FAILED - FIX XML ONLY"));
}

#[test]
fn test_prepare_commit_prompt_does_not_panic_when_materialized_attempt_mismatch() {
    let workspace = MemoryWorkspace::new_test()
        .with_dir(".agent/tmp")
        .with_file(".agent/tmp/commit_diff.model_safe.txt", "diff");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 2,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["qwen".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    // Materialized for attempt 1, but current attempt is 2 (mismatch).
    handler.state.prompt_inputs.commit = Some(MaterializedCommitInputs {
        attempt: 1,
        diff: MaterializedPromptInput {
            kind: PromptInputKind::Diff,
            content_id_sha256: "hash".to_string(),
            consumer_signature_sha256: handler.state.agent_chain.consumer_signature_sha256(),
            original_bytes: 1,
            final_bytes: 1,
            model_budget_bytes: Some(100_000),
            inline_budget_bytes: Some(100_000),
            representation: PromptInputRepresentation::Inline,
            reason: PromptMaterializationReason::WithinBudgets,
        },
    });

    let result = catch_unwind(AssertUnwindSafe(|| {
        handler.prepare_commit_prompt(&ctx, PromptMode::Normal)
    }));
    assert!(
        result.is_ok(),
        "prepare_commit_prompt should not panic when commit inputs are missing for the current attempt"
    );
}

#[test]
fn test_prepare_commit_prompt_same_agent_retry_uses_previous_prepared_prompt() {
    let marker = "<<<PREVIOUS_COMMIT_PROMPT_MARKER>>>";
    let workspace = MemoryWorkspace::new_test()
        .with_dir(".agent/tmp")
        .with_file(".agent/tmp/commit_prompt.txt", marker);
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState {
        continuation: ContinuationState {
            same_agent_retry_count: 1,
            same_agent_retry_reason: Some(SameAgentRetryReason::Timeout),
            ..ContinuationState::new()
        },
        ..PipelineState::initial(1, 0)
    });
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "diff --git a/a b/a\n+change\n",
            PromptMode::SameAgentRetry,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .read(std::path::Path::new(".agent/tmp/commit_prompt.txt"))
        .expect("commit_prompt.txt should be written");

    assert!(
        prompt.contains(marker),
        "Same-agent retry should reuse the previously prepared prompt; got: {prompt}"
    );
    assert!(
        prompt.contains("## Retry Note (attempt 1)"),
        "Same-agent retry should prepend retry note; got: {prompt}"
    );
}

#[test]
fn test_prepare_commit_prompt_same_agent_retry_does_not_stack_retry_notes() {
    let marker = "<<<PREVIOUS_COMMIT_PROMPT_MARKER>>>";
    let workspace = MemoryWorkspace::new_test()
        .with_dir(".agent/tmp")
        .with_file(".agent/tmp/commit_prompt.txt", marker);
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState {
        continuation: ContinuationState {
            same_agent_retry_count: 1,
            same_agent_retry_reason: Some(SameAgentRetryReason::Timeout),
            ..ContinuationState::new()
        },
        ..PipelineState::initial(1, 0)
    });
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "diff --git a/a b/a\n+change\n",
            PromptMode::SameAgentRetry,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    handler.state.continuation.same_agent_retry_count = 2;
    handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "diff --git a/a b/a\n+change\n",
            PromptMode::SameAgentRetry,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .read(std::path::Path::new(".agent/tmp/commit_prompt.txt"))
        .expect("commit_prompt.txt should be written");

    assert!(
        prompt.contains(marker),
        "Same-agent retry should keep the base prompt content; got: {prompt}"
    );
    assert_eq!(
        prompt.matches("## Retry Note").count(),
        1,
        "Expected exactly one retry note block, got: {prompt}"
    );
    assert!(
        prompt.contains("## Retry Note (attempt 2)"),
        "Expected retry note attempt 2 after second retry, got: {prompt}"
    );
    assert!(
        !prompt.contains("## Retry Note (attempt 1)"),
        "Expected previous retry note to be replaced, got: {prompt}"
    );
}

#[test]
fn test_prepare_commit_prompt_same_agent_retry_replays_from_prompt_history_when_available() {
    use crate::reducer::prompt_inputs::sha256_hex_str;
    use crate::reducer::ui_event::UIEvent;

    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState {
        continuation: ContinuationState {
            same_agent_retry_count: 1,
            same_agent_retry_reason: Some(SameAgentRetryReason::Timeout),
            ..ContinuationState::new()
        },
        ..PipelineState::initial(1, 0)
    });
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    let scope_key = crate::prompts::PromptScopeKey::for_commit(
        handler.state.iteration,
        1,
        crate::prompts::RetryMode::SameAgent { count: 1 },
        handler.state.recovery_epoch,
    );
    let key = scope_key.to_string();
    let diff_content_id = sha256_hex_str("DIFF");
    let consumer_sig = handler.state.agent_chain.consumer_signature_sha256();
    let prompt_content_id = sha256_hex_str(&format!(
        "commit_prompt|diff:{diff_content_id}|consumer:{consumer_sig}"
    ));
    handler.state.prompt_history.insert(
        key.clone(),
        PromptHistoryEntry::new("STORED-PROMPT".to_string(), Some(prompt_content_id)),
    );

    let result = handler
        .prepare_commit_prompt_with_diff_and_mode(&ctx, "DIFF", PromptMode::SameAgentRetry)
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .read(std::path::Path::new(".agent/tmp/commit_prompt.txt"))
        .expect("commit_prompt.txt should be written");
    assert_eq!(prompt, "STORED-PROMPT");

    assert!(
        result.ui_events.iter().any(|e| matches!(
            e,
            UIEvent::PromptReplayHit {
                key: k,
                was_replayed: true
            } if k == &key
        )),
        "Expected PromptReplayHit(was_replayed=true) for {key}; got: {:?}",
        result.ui_events
    );
    assert!(
        !result.additional_events.iter().any(|e| matches!(
            e,
            PipelineEvent::PromptInput(PromptInputEvent::PromptCaptured { key: k, .. })
                if k == &key
        )),
        "Prompt replay should not emit PromptCaptured for {key}; got: {:?}",
        result.additional_events
    );
}

/// Test that commit prompt keys are unique per iteration, preventing cross-cycle prompt replay.
///
/// Root cause of the stale-commit-diff bug: commit prompt keys use only the attempt number
/// (e.g. `commit_message_attempt_1`). Since attempt numbers reset to 1 on each new commit cycle
/// and `prompt_history` is run-scoped, cycle 2 with attempt=1 looks up the same key as cycle 1
/// and replays the stale cycle-1 prompt (which embeds cycle-1's diff content).
///
/// This test proves the bug: it pre-populates `prompt_history` with the cycle-1 key and asserts
/// that a cycle-2 handler generates a fresh prompt (not replayed from history). It FAILS on code
/// where the key is `commit_message_attempt_{attempt}` and PASSES when iteration is included.
#[test]
fn test_commit_prompt_key_is_unique_per_cycle_prevents_stale_replay() {
    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    // Simulate cycle 2: iteration=2, attempt resets to 1.
    // Pre-populate state.prompt_history with the cycle-1 key as if cycle 1 already ran.
    // On buggy code (key = "commit_message_attempt_1"), cycle 2 will find and replay this.
    // Since handlers now read from self.state.prompt_history, insert into the handler state.
    let mut handler = MainEffectHandler::new(PipelineState::initial(2, 0));
    handler.state.prompt_history.insert(
        "commit_message_attempt_1".to_string(),
        PromptHistoryEntry::from_string(
            "STALE-CYCLE-1-PROMPT: old diff content from iteration 1".to_string(),
        ),
    );
    handler.state.iteration = 2;
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    handler
        .prepare_commit_prompt_with_diff_and_mode(
            &ctx,
            "FRESH-CYCLE-2-DIFF: unique cycle 2 changes",
            PromptMode::Normal,
        )
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .get_file(".agent/tmp/commit_prompt.txt")
        .expect("commit_prompt.txt should be written");

    assert!(
        !prompt.contains("STALE-CYCLE-1-PROMPT"),
        "Cycle-2 commit prompt must NOT contain stale cycle-1 content — \
         prompt key collision caused cross-cycle replay; got: {prompt}"
    );
    assert!(
        prompt.contains("FRESH-CYCLE-2-DIFF"),
        "Cycle-2 commit prompt must contain fresh cycle-2 diff content; got: {prompt}"
    );
}

/// Test that stored commit prompts are gated on the current commit diff content-id.
///
/// RFC-007 introduced optional content-id validation for prompt replay: if a stored
/// prompt's content-id does not match the current materialized inputs, the stored
/// entry must be treated as a cache miss and a fresh prompt must be generated.
///
/// For commit prompts, the relevant materialized input is the commit diff; this
/// test ensures that when the diff content-id changes, we do not replay a stale
/// stored commit prompt under the same prompt key.
#[test]
fn test_commit_prompt_replay_is_gated_on_commit_diff_content_id() {
    use crate::reducer::prompt_inputs::sha256_hex_str;

    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(2, 0));
    handler.state.iteration = 2;
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.commit_diff_content_id_sha256 = Some("new_hash".to_string());
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    let consumer_sig = handler.state.agent_chain.consumer_signature_sha256();
    let expected_prompt_content_id = sha256_hex_str(&format!(
        "commit_prompt|diff:new_hash|consumer:{consumer_sig}"
    ));

    let scope_key = crate::prompts::PromptScopeKey::for_commit(
        2,
        1,
        crate::prompts::RetryMode::Normal,
        handler.state.recovery_epoch,
    );
    let prompt_key = scope_key.to_string();

    // Pre-populate prompt history with a stale prompt entry for the same key.
    handler.state.prompt_history.insert(
        prompt_key.clone(),
        PromptHistoryEntry::new("STALE-PROMPT".to_string(), Some("old_hash".to_string())),
    );

    let result = handler
        .prepare_commit_prompt_with_diff_and_mode(&ctx, "FRESH-DIFF", PromptMode::Normal)
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .get_file(".agent/tmp/commit_prompt.txt")
        .expect("commit_prompt.txt should be written");

    assert!(
        !prompt.contains("STALE-PROMPT"),
        "Commit prompt must not replay stale stored prompt when diff content-id changes; got: {prompt}"
    );
    assert!(
        prompt.contains("FRESH-DIFF"),
        "Commit prompt must be freshly generated using the current diff; got: {prompt}"
    );

    // Replay observability must report a cache miss (fresh generation).
    assert!(
        result.ui_events.iter().any(|e| matches!(
            e,
            crate::reducer::ui_event::UIEvent::PromptReplayHit {
                key,
                was_replayed: false
            } if key == &prompt_key
        )),
        "Expected PromptReplayHit with was_replayed=false for key {prompt_key}; got: {:?}",
        result.ui_events
    );

    // Fresh generation must emit PromptCaptured so reducer-owned history can be updated.
    assert!(
        result.additional_events.iter().any(|e| matches!(
            e,
            PipelineEvent::PromptInput(PromptInputEvent::PromptCaptured {
                key,
                content_id: Some(content_id),
                ..
            }) if key == &prompt_key && content_id == &expected_prompt_content_id
        )),
        "Expected PromptCaptured with computed prompt content id for key {prompt_key}; got: {:?}",
        result.additional_events
    );
}

/// Commit prompt replay must also be gated on the agent-chain consumer signature.
///
/// If the consumer signature changes (e.g., agent selection changes), replaying a stored
/// prompt based only on diff id can return a prompt rendered for a different consumer.
#[test]
fn test_commit_prompt_replay_is_gated_on_consumer_signature() {
    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(2, 0));
    handler.state.iteration = 2;
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.commit_diff_content_id_sha256 = Some("diff_id".to_string());

    // Current consumer signature (new chain)
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["codex".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    // Stored prompt was captured under a different consumer signature (old chain), but legacy
    // checkpoints may only store a diff content-id in PromptHistoryEntry.content_id.
    let scope_key = crate::prompts::PromptScopeKey::for_commit(
        2,
        1,
        crate::prompts::RetryMode::Normal,
        handler.state.recovery_epoch,
    );
    let prompt_key = scope_key.to_string();
    handler.state.prompt_history.insert(
        prompt_key.clone(),
        PromptHistoryEntry::new(
            "PROMPT-FOR-OLD-CONSUMER".to_string(),
            Some("diff_id".to_string()),
        ),
    );

    let result = handler
        .prepare_commit_prompt_with_diff_and_mode(&ctx, "FRESH-DIFF", PromptMode::Normal)
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .get_file(".agent/tmp/commit_prompt.txt")
        .expect("commit_prompt.txt should be written");

    assert!(
        !prompt.contains("PROMPT-FOR-OLD-CONSUMER"),
        "Commit prompt must not replay a prompt captured for a different consumer signature; got: {prompt}"
    );

    assert!(result.ui_events.iter().any(|e| matches!(
        e,
        crate::reducer::ui_event::UIEvent::PromptReplayHit { key, was_replayed: false }
            if key == &prompt_key
    )));
}

/// Test that `prepare_commit_prompt` reads from materialized model-safe diff file.
///
/// Once commit inputs are materialized, the `prepare_commit_prompt` effect should
/// read from .`agent/tmp/commit_diff.model_safe.txt`, ensuring the prompt uses
/// the already-truncated content instead of re-truncating.
#[test]
fn test_prepare_commit_prompt_uses_materialized_diff() {
    // Original large diff (will be truncated)
    let large_diff = format!("diff --git a/a b/a\n+{}\n", "x".repeat(150_000));
    // Simulated truncated diff from materialization
    let model_safe_diff = "diff --git a/a b/a\n+truncated_content [truncated...]\n";

    let workspace = MemoryWorkspace::new_test()
        .with_file(".agent/tmp/commit_diff.txt", &large_diff)
        .with_file(".agent/tmp/commit_diff.model_safe.txt", model_safe_diff)
        .with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["qwen".to_string()], // qwen has 100KB budget
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    // Set up pre-materialized inputs
    let consumer_sig = handler.state.agent_chain.consumer_signature_sha256();
    handler.state.prompt_inputs = PromptInputsState {
        commit: Some(MaterializedCommitInputs {
            attempt: 1,
            diff: MaterializedPromptInput {
                kind: PromptInputKind::Diff,
                content_id_sha256: "hash".to_string(),
                consumer_signature_sha256: consumer_sig,
                original_bytes: large_diff.len() as u64,
                final_bytes: model_safe_diff.len() as u64,
                model_budget_bytes: Some(100_000),
                inline_budget_bytes: Some(100_000),
                representation: PromptInputRepresentation::Inline,
                reason: PromptMaterializationReason::ModelBudgetExceeded,
            },
        }),
        ..Default::default()
    };

    let result = handler
        .prepare_commit_prompt(&ctx, PromptMode::Normal)
        .expect("prepare_commit_prompt should succeed");

    // Should succeed with a prompt containing the truncated diff, not the original
    assert!(
        matches!(
            result.event,
            PipelineEvent::Commit(crate::reducer::event::CommitEvent::PromptPrepared { .. })
        ),
        "expected PromptPrepared event"
    );

    // The generated prompt file should contain the truncated diff content
    let prompt_content = fixture
        .workspace
        .get_file(".agent/tmp/commit_prompt.txt")
        .unwrap();
    assert!(
        prompt_content.contains("truncated_content"),
        "prompt should contain materialized (truncated) diff content"
    );
    assert!(
        !prompt_content.contains(&"x".repeat(1000)),
        "prompt should NOT contain original large diff content"
    );
}

#[test]
fn test_commit_prompt_residual_files_are_accounted_for_not_forced_into_commit() {
    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    handler.state.commit_residual_files = vec!["src/leftover.rs".to_string()];

    handler
        .prepare_commit_prompt_with_diff_and_mode(&ctx, "DIFF", PromptMode::Normal)
        .expect("prepare_commit_prompt_with_diff_and_mode should succeed");

    let prompt = fixture
        .workspace
        .get_file(".agent/tmp/commit_prompt.txt")
        .expect("commit_prompt.txt should be written");

    assert!(
        prompt.contains("must be accounted for"),
        "Residual file guidance must require accounting, not forced inclusion; got: {prompt}"
    );
    assert!(
        prompt.contains("ralph-excluded-files"),
        "Residual file guidance must mention the exclusion metadata section; got: {prompt}"
    );
    assert!(
        prompt.contains("- src/leftover.rs"),
        "Residual file list must be included; got: {prompt}"
    );
}

#[test]
fn test_prepare_commit_prompt_invalidates_materialized_inputs_when_model_safe_diff_missing() {
    let workspace = MemoryWorkspace::new_test()
        .with_file(
            ".agent/tmp/commit_diff.txt",
            "diff --git a/a b/a\n+change\n",
        )
        .with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.commit_diff_prepared = true;
    handler.state.commit_diff_empty = false;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["qwen".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    let consumer_sig = handler.state.agent_chain.consumer_signature_sha256();
    handler.state.prompt_inputs = PromptInputsState {
        commit: Some(MaterializedCommitInputs {
            attempt: 1,
            diff: MaterializedPromptInput {
                kind: PromptInputKind::Diff,
                content_id_sha256: "hash".to_string(),
                consumer_signature_sha256: consumer_sig,
                original_bytes: 1,
                final_bytes: 1,
                model_budget_bytes: Some(100_000),
                inline_budget_bytes: Some(100_000),
                representation: PromptInputRepresentation::Inline,
                reason: PromptMaterializationReason::WithinBudgets,
            },
        }),
        ..Default::default()
    };

    let result = handler
        .prepare_commit_prompt(&ctx, PromptMode::Normal)
        .expect("prepare_commit_prompt should return an EffectResult");

    assert!(
        matches!(
            result.event,
            PipelineEvent::Commit(crate::reducer::event::CommitEvent::DiffInvalidated { .. })
        ),
        "Expected DiffInvalidated event to force diff recomputation when commit_diff.model_safe.txt is missing, got {:?}",
        result.event
    );
}

#[test]
fn test_prepare_commit_prompt_invalidates_materialized_inputs_when_diff_file_reference_missing() {
    let workspace = MemoryWorkspace::new_test()
        .with_file(
            ".agent/tmp/commit_diff.txt",
            "diff --git a/a b/a\n+change\n",
        )
        .with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.commit_diff_prepared = true;
    handler.state.commit_diff_empty = false;
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["qwen".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );
    let consumer_sig = handler.state.agent_chain.consumer_signature_sha256();
    handler.state.prompt_inputs = PromptInputsState {
        commit: Some(MaterializedCommitInputs {
            attempt: 1,
            diff: MaterializedPromptInput {
                kind: PromptInputKind::Diff,
                content_id_sha256: "hash".to_string(),
                consumer_signature_sha256: consumer_sig,
                original_bytes: 1,
                final_bytes: 1,
                model_budget_bytes: Some(100_000),
                inline_budget_bytes: Some(1),
                representation: PromptInputRepresentation::FileReference {
                    path: std::path::PathBuf::from(".agent/tmp/commit_diff.model_safe.txt"),
                },
                reason: PromptMaterializationReason::InlineBudgetExceeded,
            },
        }),
        ..Default::default()
    };

    // The file reference points at `.agent/tmp/commit_diff.model_safe.txt` but it doesn't exist.
    // The handler should invalidate diff-prepared state by emitting DiffInvalidated, forcing
    // CheckCommitDiff (and subsequent rematerialization) on the next orchestration loop.
    let result = handler
        .prepare_commit_prompt(&ctx, PromptMode::Normal)
        .expect("prepare_commit_prompt should return an EffectResult");

    assert!(
        matches!(
            result.event,
            PipelineEvent::Commit(crate::reducer::event::CommitEvent::DiffInvalidated { .. })
        ),
        "Expected DiffInvalidated event to force diff recomputation when a diff file reference is missing, got {:?}",
        result.event
    );
}

#[test]
#[cfg(debug_assertions)]
#[should_panic(expected = "Orchestrator must filter Continuation mode")]
fn test_prepare_commit_prompt_asserts_continuation_precondition_in_debug() {
    // GREEN test proving Phase-4 fix: boundary now documents the precondition via
    // debug_assert instead of implementing policy validation. Orchestrator ensures
    // Continuation is never passed (verified by orchestration tests).

    let workspace = MemoryWorkspace::new_test().with_dir(".agent/tmp");
    let mut fixture = TestFixture::with_workspace(workspace);
    let ctx = fixture.ctx();

    let mut handler = MainEffectHandler::new(PipelineState::initial(1, 0));
    handler.state.commit = CommitState::Generating {
        attempt: 1,
        max_attempts: 2,
    };
    handler.state.agent_chain = AgentChainState::initial().with_agents(
        vec!["claude".to_string()],
        vec![vec![]],
        crate::agents::AgentRole::Commit,
    );

    // In debug builds, this triggers the precondition assertion.
    // In release builds, behavior is undefined (orchestrator guarantees this never happens).
    let _ = handler.prepare_commit_prompt(&ctx, PromptMode::Continuation);
}