govctl 0.9.0

Project governance CLI for RFC, ADR, and Work Item management
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
use super::output::print_loop;
use super::state::{
    ensure_loop_not_terminal, ensure_unique_work_item_ids, loop_dependencies, loop_item_state,
};
use crate::cmd::work_lookup::load_work_item_by_id;
use crate::config::Config;
use crate::diagnostic::{Diagnostic, DiagnosticCode, DiagnosticResult, Diagnostics};
use crate::loop_planner::{propagate_blocked_outcomes, topological_order_for_state};
use crate::loop_state::{
    LoopLifecycleState, LoopNextAction, LoopRoundRecord, LoopRoundStatus, LoopState,
    LoopWorkItemStatus, load_loop_round_record, load_loop_state, loop_round_path,
    write_loop_round_record, write_loop_state_with_op,
};
use crate::model::WorkItemStatus;
use crate::write::WriteOp;
use std::collections::BTreeSet;

pub fn run(
    config: &Config,
    loop_id: &str,
    target_work_ids: &[String],
    max_rounds: u32,
    op: WriteOp,
) -> DiagnosticResult<Diagnostics> {
    if max_rounds == 0 {
        return Err(Diagnostic::new(
            DiagnosticCode::E1211LoopInvalidMaxRounds,
            "Loop max rounds must be at least 1",
            "loop",
        ));
    }

    let mut state = state_for_run(config, loop_id, target_work_ids)?;
    ensure_loop_not_terminal(&state, "run")?;

    println!("Running loop {}", state.loop_meta.id);
    println!("Max rounds: {max_rounds}");
    if !target_work_ids.is_empty() {
        println!("Targets: {}", target_work_ids.join(", "));
    }

    enter_active_state(&mut state)?;
    match current_round_record(config, &state)? {
        Some(record) if record.round_meta.status != LoopRoundStatus::Closed => {
            close_round(config, &mut state, record, target_work_ids, op)
        }
        _ => open_round(config, &mut state, target_work_ids, max_rounds, op),
    }
}

fn state_for_run(
    config: &Config,
    loop_id: &str,
    target_work_ids: &[String],
) -> DiagnosticResult<LoopState> {
    let state = load_loop_state(config, loop_id)?;
    validate_target_work_ids(&state, target_work_ids)?;
    Ok(state)
}

fn validate_target_work_ids(state: &LoopState, target_work_ids: &[String]) -> DiagnosticResult<()> {
    let loop_work_ids = state
        .loop_meta
        .resolved
        .iter()
        .map(String::as_str)
        .collect::<BTreeSet<_>>();
    ensure_unique_work_item_ids(
        target_work_ids,
        "Loop run target",
        "loop run target work item",
        &state.loop_meta.id,
    )?;
    for work_id in target_work_ids {
        if !loop_work_ids.contains(work_id.as_str()) {
            return Err(Diagnostic::new(
                DiagnosticCode::E1201LoopStateInvalid,
                format!(
                    "Loop run target '{work_id}' is not part of loop '{}'",
                    state.loop_meta.id
                ),
                state.loop_meta.id.clone(),
            ));
        }
    }
    Ok(())
}

fn enter_active_state(state: &mut LoopState) -> DiagnosticResult<()> {
    match state.loop_meta.state {
        LoopLifecycleState::Pending | LoopLifecycleState::Paused => {
            state.transition_to(LoopLifecycleState::Active)
        }
        LoopLifecycleState::Active => Ok(()),
        LoopLifecycleState::Completed | LoopLifecycleState::Failed => {
            ensure_loop_not_terminal(state, "run")
        }
    }
}

fn current_round_record(
    config: &Config,
    state: &LoopState,
) -> DiagnosticResult<Option<LoopRoundRecord>> {
    if state.loop_meta.current_round == 0 {
        return Ok(None);
    }
    match load_loop_round_record(config, &state.loop_meta.id, state.loop_meta.current_round) {
        Ok(record) => Ok(Some(record)),
        Err(err) if err.code == DiagnosticCode::E1202LoopStateNotFound => {
            let path = loop_round_path(config, &state.loop_meta.id, state.loop_meta.current_round)?;
            Err(Diagnostic::new(
                DiagnosticCode::E1201LoopStateInvalid,
                format!(
                    "Loop state points to missing round artifact: {}",
                    config.display_path(&path).display()
                ),
                state.loop_meta.id.clone(),
            ))
        }
        Err(err) => Err(err),
    }
}

fn open_round(
    config: &Config,
    state: &mut LoopState,
    target_work_ids: &[String],
    max_rounds: u32,
    op: WriteOp,
) -> DiagnosticResult<Diagnostics> {
    reflect_terminal_work_statuses(config, state)?;
    propagate_blocked_outcomes(state)?;

    let ready_work = ready_work_for_round(state, target_work_ids, max_rounds)?;
    if ready_work.failures.is_empty() && !ready_work.selected.is_empty() {
        let round_number = next_round_number(state)?;
        state.loop_meta.current_round = round_number;
        state.loop_meta.next_action = LoopNextAction::WriteSummary;
        for work_id in &ready_work.selected {
            state.set_item_status(work_id, LoopWorkItemStatus::Active)?;
            state.record_item_round(work_id, round_number)?;
        }

        let record = LoopRoundRecord::open(
            state.loop_meta.id.clone(),
            round_number,
            max_rounds,
            ready_work.selected,
        );
        write_loop_round_record(config, &record, op)?;
        write_loop_state_with_op(config, state, op)?;
        print_opened_round(config, &record)?;
        print_loop("Loop", state)?;
        return Ok(vec![]);
    }

    finalize_run_state(state)?;
    state.loop_meta.next_action = next_action_for_state(state);
    write_loop_state_with_op(config, state, op)?;
    print_final_state(state)?;
    if state.loop_meta.state == LoopLifecycleState::Failed {
        return Err(Diagnostic::new(
            DiagnosticCode::E1210LoopExecutionFailed,
            loop_failure_message(state, &ready_work.failures),
            state.loop_meta.id.clone(),
        ));
    }
    Ok(vec![])
}

fn close_round(
    config: &Config,
    state: &mut LoopState,
    mut record: LoopRoundRecord,
    target_work_ids: &[String],
    op: WriteOp,
) -> DiagnosticResult<Diagnostics> {
    validate_open_round_target_selector(state, &record, target_work_ids)?;
    if !record.has_required_summary_evidence() {
        let path = loop_round_path(
            config,
            &record.round_meta.loop_id,
            record.round_meta.round_number,
        )?;
        return Err(Diagnostic::new(
            DiagnosticCode::E1210LoopExecutionFailed,
            format!(
                "Loop round summary is incomplete; update {} before rerunning loop run",
                config.display_path(&path).display()
            ),
            record.round_meta.loop_id.clone(),
        ));
    }

    reflect_terminal_work_statuses(config, state)?;
    if record.summary.has_blockers() {
        state.transition_to(LoopLifecycleState::Paused)?;
        state.loop_meta.next_action = LoopNextAction::ResolveBlocker;
    } else {
        finalize_run_state(state)?;
        state.loop_meta.next_action = next_action_for_state(state);
    }
    record.round_meta.status = LoopRoundStatus::Closed;
    write_loop_round_record(config, &record, op)?;
    write_loop_state_with_op(config, state, op)?;
    print_final_state(state)?;
    if state.loop_meta.state == LoopLifecycleState::Failed {
        return Err(Diagnostic::new(
            DiagnosticCode::E1210LoopExecutionFailed,
            loop_failure_message(state, &[]),
            state.loop_meta.id.clone(),
        ));
    }
    Ok(vec![])
}

fn validate_open_round_target_selector(
    state: &LoopState,
    record: &LoopRoundRecord,
    target_work_ids: &[String],
) -> DiagnosticResult<()> {
    if target_work_ids.is_empty() {
        return Ok(());
    }
    let selected_work_ids = selected_execution_set(state, target_work_ids)?;
    for work_id in &record.round_meta.work {
        if !selected_work_ids.contains(work_id) {
            return Err(Diagnostic::new(
                DiagnosticCode::E1201LoopStateInvalid,
                format!(
                    "Loop run target selector does not include open round work item: {work_id}"
                ),
                state.loop_meta.id.clone(),
            ));
        }
    }
    Ok(())
}

struct ReadyWork {
    selected: Vec<String>,
    failures: Vec<String>,
}

fn ready_work_for_round(
    state: &mut LoopState,
    target_work_ids: &[String],
    max_rounds: u32,
) -> DiagnosticResult<ReadyWork> {
    let selected_work_ids = selected_execution_set(state, target_work_ids)?;
    let mut selected = Vec::new();
    let mut failures = Vec::new();

    for work_id in topological_order_for_state(state)? {
        propagate_blocked_outcomes(state)?;
        if !selected_work_ids.is_empty() && !selected_work_ids.contains(work_id.as_str()) {
            continue;
        }
        if is_terminal_item(state, &work_id) {
            continue;
        }
        match dependency_readiness(state, &work_id)? {
            DependencyReadiness::Ready => {}
            DependencyReadiness::Waiting => continue,
            DependencyReadiness::Blocked => {
                state.set_item_status(&work_id, LoopWorkItemStatus::Blocked)?;
                continue;
            }
        }

        let current_rounds = loop_item_state(state, &work_id)?.round_count;
        if current_rounds >= max_rounds {
            state.set_item_status(&work_id, LoopWorkItemStatus::Failed)?;
            failures.push(format!("{work_id}: maximum rounds reached ({max_rounds})"));
            continue;
        }
        selected.push(work_id);
    }

    propagate_blocked_outcomes(state)?;
    Ok(ReadyWork { selected, failures })
}

fn selected_execution_set(
    state: &LoopState,
    target_work_ids: &[String],
) -> DiagnosticResult<BTreeSet<String>> {
    let mut selected = BTreeSet::new();
    for work_id in target_work_ids {
        collect_target_with_dependencies(state, work_id, &mut selected)?;
    }
    Ok(selected)
}

fn collect_target_with_dependencies(
    state: &LoopState,
    work_id: &str,
    selected: &mut BTreeSet<String>,
) -> DiagnosticResult<()> {
    if !selected.insert(work_id.to_string()) {
        return Ok(());
    }
    let dependencies = loop_dependencies(state, work_id, "selected work item")?;
    for dependency in dependencies {
        collect_target_with_dependencies(state, dependency, selected)?;
    }
    Ok(())
}

fn reflect_terminal_work_statuses(config: &Config, state: &mut LoopState) -> DiagnosticResult<()> {
    for work_id in state.loop_meta.resolved.clone() {
        match load_work_item_by_id(config, &work_id)?.spec.govctl.status {
            WorkItemStatus::Done => state.set_item_status(&work_id, LoopWorkItemStatus::Done)?,
            WorkItemStatus::Cancelled => {
                state.set_item_status(&work_id, LoopWorkItemStatus::Cancelled)?
            }
            WorkItemStatus::Queue | WorkItemStatus::Active => {}
        }
    }
    Ok(())
}

fn finalize_run_state(state: &mut LoopState) -> DiagnosticResult<()> {
    propagate_blocked_outcomes(state)?;
    let has_failed = state.items.values().any(|item| {
        matches!(
            item.status,
            LoopWorkItemStatus::Failed | LoopWorkItemStatus::Blocked
        )
    });
    let all_terminal = state.items.values().all(|item| {
        matches!(
            item.status,
            LoopWorkItemStatus::Done
                | LoopWorkItemStatus::Failed
                | LoopWorkItemStatus::Blocked
                | LoopWorkItemStatus::Cancelled
        )
    });

    if has_failed {
        state.transition_to(LoopLifecycleState::Failed)
    } else if all_terminal {
        state.transition_to(LoopLifecycleState::Completed)
    } else {
        state.transition_to(LoopLifecycleState::Paused)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DependencyReadiness {
    Ready,
    Waiting,
    Blocked,
}

fn dependency_readiness(state: &LoopState, work_id: &str) -> DiagnosticResult<DependencyReadiness> {
    let dependencies = loop_dependencies(state, work_id, "work item")?;
    if dependencies.iter().any(|dependency| {
        matches!(
            state.items[dependency.as_str()].status,
            LoopWorkItemStatus::Failed
                | LoopWorkItemStatus::Blocked
                | LoopWorkItemStatus::Cancelled
        )
    }) {
        return Ok(DependencyReadiness::Blocked);
    }
    if dependencies
        .iter()
        .all(|dependency| state.items[dependency.as_str()].status == LoopWorkItemStatus::Done)
    {
        Ok(DependencyReadiness::Ready)
    } else {
        Ok(DependencyReadiness::Waiting)
    }
}

fn is_terminal_item(state: &LoopState, work_id: &str) -> bool {
    matches!(
        state.items[work_id].status,
        LoopWorkItemStatus::Done
            | LoopWorkItemStatus::Failed
            | LoopWorkItemStatus::Blocked
            | LoopWorkItemStatus::Cancelled
    )
}

fn next_round_number(state: &LoopState) -> DiagnosticResult<u32> {
    state.loop_meta.current_round.checked_add(1).ok_or_else(|| {
        Diagnostic::new(
            DiagnosticCode::E1201LoopStateInvalid,
            "Loop current_round overflow",
            state.loop_meta.id.clone(),
        )
    })
}

fn next_action_for_state(state: &LoopState) -> LoopNextAction {
    match state.loop_meta.state {
        LoopLifecycleState::Completed => LoopNextAction::Complete,
        LoopLifecycleState::Failed => LoopNextAction::ResolveBlocker,
        LoopLifecycleState::Pending | LoopLifecycleState::Active | LoopLifecycleState::Paused => {
            LoopNextAction::Continue
        }
    }
}

fn print_opened_round(config: &Config, record: &LoopRoundRecord) -> DiagnosticResult<()> {
    let path = loop_round_path(
        config,
        &record.round_meta.loop_id,
        record.round_meta.round_number,
    )?;
    println!(
        "Opened round {} for loop {}",
        record.round_meta.round_number, record.round_meta.loop_id
    );
    println!("Round: {}", config.display_path(&path).display());
    println!(
        "Next action: fill summary evidence, then rerun `govctl loop run {}`",
        record.round_meta.loop_id
    );
    Ok(())
}

fn print_final_state(state: &LoopState) -> DiagnosticResult<()> {
    match state.loop_meta.state {
        LoopLifecycleState::Completed => print_loop("Completed", state),
        LoopLifecycleState::Paused => print_loop("Paused", state),
        LoopLifecycleState::Failed => print_loop("Failed", state),
        LoopLifecycleState::Pending | LoopLifecycleState::Active => print_loop("Loop", state),
    }
}

fn loop_failure_message(state: &LoopState, failures: &[String]) -> String {
    if failures.is_empty() {
        format!("Loop '{}' failed", state.loop_meta.id)
    } else {
        format!(
            "Loop '{}' failed:\n{}",
            state.loop_meta.id,
            failures
                .iter()
                .map(|failure| format!("  - {failure}"))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}