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
//! `AwaitingDevFix` event reduction.
//!
//! Handles events during the failure remediation phase.
use crate::reducer::event::{AwaitingDevFixEvent, PipelinePhase};
use crate::reducer::state::PipelineState;
/// Reduce `AwaitingDevFix` events.
///
/// This phase handles pipeline failure remediation by tracking the dev-fix
/// flow state and transitioning to Interrupted after completion marker emission.
pub(super) fn reduce_awaiting_dev_fix_event(
state: PipelineState,
event: AwaitingDevFixEvent,
) -> PipelineState {
match event {
AwaitingDevFixEvent::DevFixTriggered { .. } => {
// Record that dev-fix was triggered, stay in AwaitingDevFix phase
PipelineState {
dev_fix_triggered: true,
..state
}
}
AwaitingDevFixEvent::DevFixSkipped { .. } => {
// Dev-fix was skipped (disabled/unavailable feature).
// Treat this as a completed recovery attempt so unattended orchestration
// can advance into the recovery loop instead of re-triggering dev-fix
// indefinitely.
let new_attempt_count = state.dev_fix_attempt_count + 1;
let new_level = match new_attempt_count {
1..=3 => 1,
4..=6 => 2,
7..=9 => 3,
_ => 4,
};
PipelineState {
dev_fix_triggered: true,
dev_fix_attempt_count: new_attempt_count,
recovery_escalation_level: new_level,
..state
}
}
AwaitingDevFixEvent::DevFixCompleted {
success: _,
summary: _,
} => {
// Dev-fix attempt completed. Decide whether to:
// 1. Attempt recovery at current level
// 2. Escalate to next recovery level
let new_attempt_count = state.dev_fix_attempt_count + 1;
// Determine recovery escalation level based on attempt count
// Level 1 (attempts 1-3): Retry same operation
// Level 2 (attempts 4-6): Reset to phase start
// Level 3 (attempts 7-9): Reset iteration counter
// Level 4 (attempts 10+): Reset to iteration 0
let new_level = match new_attempt_count {
1..=3 => 1,
4..=6 => 2,
7..=9 => 3,
_ => 4,
};
// Prepare for recovery attempt at the determined level.
//
// IMPORTANT: Do not transition to Interrupted directly here.
// Internal failures are handled via recovery attempts; termination is reserved
// for explicit external/catastrophic conditions and must go through the single
// completion-marker path: Effect::EmitCompletionMarkerAndTerminate ->
// CompletionMarkerEmitted.
PipelineState {
dev_fix_attempt_count: new_attempt_count,
recovery_escalation_level: new_level,
// Stay in AwaitingDevFix until recovery is attempted
..state
}
}
AwaitingDevFixEvent::DevFixAgentUnavailable { .. } => {
// Dev-fix agent unavailable (quota/usage limit). Stay in AwaitingDevFix so
// orchestration can keep the unattended recovery loop running.
state
}
AwaitingDevFixEvent::CompletionMarkerEmitted { is_failure } => {
// Completion marker emitted, transition to Interrupted
PipelineState {
phase: PipelinePhase::Interrupted,
previous_phase: Some(state.phase),
completion_marker_pending: false,
completion_marker_is_failure: is_failure,
completion_marker_reason: None,
..state
}
}
AwaitingDevFixEvent::CompletionMarkerWriteFailed { is_failure, error } => {
// Marker write failed; stay in AwaitingDevFix but set an explicit retry flag so
// orchestration deterministically re-derives EmitCompletionMarkerAndTerminate.
PipelineState {
completion_marker_pending: true,
completion_marker_is_failure: is_failure,
completion_marker_reason: Some(error),
..state
}
}
AwaitingDevFixEvent::RecoveryAttempted {
level,
attempt_count: _,
target_phase,
} => {
// Recovery state transitions documented for clarity:
//
// Level 1: Retry same operation (attempts 1-3)
// - No state reset, just transition back to failed phase
// - Orchestration will derive the same effect that failed
// - Example: If InvokeAgent failed, retry InvokeAgent
//
// Level 2: Reset to phase start (attempts 4-6)
// - Clear all phase-specific progress flags
// - Orchestration starts the phase from scratch
// - Preserves: iteration counter, reviewer_pass, other phases
// - Example: Clear development_agent_invoked_iteration, restart from PrepareDevelopmentContext
//
// Level 3: Reset iteration (attempts 7-9)
// - Decrement iteration counter (floor at 0)
// - Clear Planning/Development/Commit flags
// - Transition to Planning phase to redo iteration
// - Preserves: reviewer_pass, total_iterations
//
// Level 4: Complete reset (attempts 10+)
// - Reset iteration to 0
// - Clear Planning/Development/Commit flags
// - Transition to Planning phase for full restart
// - Preserves: reviewer_pass, total_iterations
// Base state with phase transition
let new_state = PipelineState {
phase: target_phase,
previous_phase: Some(PipelinePhase::AwaitingDevFix),
// Keep recovery tracking fields so we can escalate if this fails
..state.clone()
};
// Apply state reset based on escalation level (functional style)
let new_state = match level {
1 => {
// Level 1: Simple retry - just transition back, no state reset
new_state
}
2 => {
// Level 2: Reset to phase start - clear phase-specific progress flags
let reset = new_state.clear_phase_flags(target_phase);
// IMPORTANT: Level 2 is a true "phase start" restart.
// Clear continuation/retry flags that have higher orchestration
// priority than normal phase sequencing (same-agent retry, XSD retry,
// continuation pending, context write/cleanup pending).
let reset = PipelineState {
continuation: reset.continuation.clone().reset(),
..reset
};
// Clear phase-scoped materialized prompt inputs so prompt preparation
// reruns from scratch for the restarted phase.
let reset = PipelineState {
prompt_inputs: match target_phase {
PipelinePhase::Planning => reset
.prompt_inputs
.clone()
.with_planning_cleared()
.with_xsd_retry_cleared(),
PipelinePhase::Development => reset
.prompt_inputs
.clone()
.with_development_cleared()
.with_xsd_retry_cleared(),
PipelinePhase::Review => reset
.prompt_inputs
.clone()
.with_review_cleared()
.with_xsd_retry_cleared(),
PipelinePhase::CommitMessage => reset
.prompt_inputs
.clone()
.with_commit_cleared()
.with_xsd_retry_cleared(),
_ => reset.prompt_inputs.clone().with_xsd_retry_cleared(),
},
..reset
};
// Planning phase has global prerequisites at the true phase start.
// If we are resetting to Planning phase start, we must re-run these
// prerequisite effects; otherwise orchestration will skip them and the
// "phase start" reset won't actually restart from the beginning.
if matches!(target_phase, PipelinePhase::Planning) {
PipelineState {
context_cleaned: false,
gitignore_entries_ensured: false,
..reset
}
} else {
reset
}
}
3 => {
// Level 3: Reset iteration counter - decrement iteration and restart from Planning.
// Advance recovery_epoch so PromptScopeKey replay identity changes with scope.
// Clear prompt_history atomically so stale prompts are not replayed after scope rotation.
let s = new_state.reset_iteration();
PipelineState {
recovery_epoch: s.recovery_epoch + 1,
prompt_history: std::collections::HashMap::new(),
..s
}
}
_ => {
// Level 4+: Complete reset - reset to iteration 0, restart from Planning.
// Advance recovery_epoch so PromptScopeKey replay identity changes with scope.
// Clear prompt_history atomically so stale prompts are not replayed after scope rotation.
let s = new_state.reset_to_iteration_zero();
PipelineState {
recovery_epoch: s.recovery_epoch + 1,
prompt_history: std::collections::HashMap::new(),
..s
}
}
};
// Recovery must also reset agent-chain state.
//
// If the original failure was agent-chain exhaustion, leaving the chain exhausted
// would cause immediate re-failure on the next orchestration cycle.
//
// Semantics:
// - Always reset for Level 2+ recovery (phase/iteration resets imply fresh work).
// - Also reset for Level 1 if the chain is already exhausted.
if level >= 2 || new_state.agent_chain.is_exhausted() {
let drain = new_state.agent_chain.current_drain;
PipelineState {
agent_chain: new_state.agent_chain.reset_for_drain(drain),
..new_state
}
} else {
new_state
}
}
AwaitingDevFixEvent::RecoveryEscalated {
from_level: _,
to_level,
reason: _,
} => {
// Recovery escalated - update level, stay in AwaitingDevFix
PipelineState {
recovery_escalation_level: to_level,
..state
}
}
AwaitingDevFixEvent::RecoverySucceeded {
level: _,
total_attempts: _,
} => {
// Recovery succeeded - clear recovery state and resume normal operation
PipelineState {
dev_fix_attempt_count: 0,
recovery_escalation_level: 0,
failed_phase_for_recovery: None,
// Stay in current phase (which should be the recovered phase)
..state
}
}
}
}
#[cfg(test)]
#[path = "awaiting_dev_fix/tests.rs"]
mod tests;