loopflow 0.10.0

Run steps and flows with coding agents
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
//! One merge-queue reconcile pass, verb-shaped: `lf op queue reconcile`.
//!
//! Runs the same pass the daemon's 60s poller runs (stack-status inference,
//! draft/ready flips, lazy head rebase, queue-block attention writes),
//! sourced from lfdb + gh, then exits. The doorman's poll loop and PR-merged
//! webhook will exec this verb once the collapse lands (wave 3).
//!
//! EventHub delta: the daemon path emits attention created/updated/resolved
//! events when a queue block appears or clears. This verb has no hub — the
//! journal/store write is the record of the fact. The doorman's
//! store-poll→event push bridge (collapse call #4) owns turning those writes
//! back into live events.

use std::sync::Arc;

use anyhow::{anyhow, Context, Result};
use secrecy::SecretString;

use crate::lfd::config::GitHubConfig;
use crate::lfd::id::LfdId;
use crate::lfd::queue::{
    acquire_reconcile_lock, reconcile_wave_queue_with_ops, QueueOps, RealQueueOps,
};
use crate::lfd::types::Wave;
use crate::lfdb::{open_store, SharedStore};

#[derive(Debug)]
pub struct WaveQueueOutcome {
    pub wave: String,
    pub wave_id: LfdId,
    pub result: Result<(), String>,
}

/// CLI entry: open the local registry, reconcile one wave (by name) or every
/// wave with queue state, print one line per wave.
pub fn reconcile_queue_cmd(wave: Option<&str>) -> Result<()> {
    let rt = tokio::runtime::Runtime::new().context("failed to create async runtime")?;
    rt.block_on(async {
        let cfg = crate::lfd::storage_config_from_env()
            .context("failed to resolve local run registry")?;
        let store: SharedStore = Arc::new(
            open_store(&cfg)
                .await
                .map_err(|err| anyhow!("failed to open local run registry: {err}"))?,
        );
        let github = github_config_for_verb(&store).await;
        let outcomes = reconcile_wave_queues(&store, &github, wave)
            .await
            .map_err(|err| anyhow!(err))?;

        if outcomes.is_empty() {
            println!("no waves with queue state");
            return Ok(());
        }

        let mut failures = 0;
        for outcome in &outcomes {
            match &outcome.result {
                Ok(()) => {
                    let block_note = store
                        .list_queue_blocks(&outcome.wave_id)
                        .await
                        .ok()
                        .filter(|blocks| !blocks.is_empty())
                        .map(|blocks| {
                            blocks
                                .iter()
                                .map(|block| block.reason.as_str())
                                .collect::<Vec<_>>()
                                .join(", ")
                        });
                    match block_note {
                        Some(reasons) => println!("{}: blocked ({reasons})", outcome.wave),
                        None => println!("{}: reconciled", outcome.wave),
                    }
                }
                Err(err) => {
                    failures += 1;
                    eprintln!("{}: {err}", outcome.wave);
                }
            }
        }
        if failures > 0 {
            return Err(anyhow!("{failures} wave(s) failed to reconcile"));
        }
        Ok(())
    })
}

/// One reconcile pass. `wave` narrows to a single wave by name; `None` covers
/// every wave that has stack runs (queue state).
pub async fn reconcile_wave_queues(
    store: &SharedStore,
    github: &GitHubConfig,
    wave: Option<&str>,
) -> Result<Vec<WaveQueueOutcome>, String> {
    reconcile_wave_queues_with_ops(store, github, wave, &RealQueueOps).await
}

async fn reconcile_wave_queues_with_ops(
    store: &SharedStore,
    github: &GitHubConfig,
    wave: Option<&str>,
    ops: &dyn QueueOps,
) -> Result<Vec<WaveQueueOutcome>, String> {
    let waves: Vec<Wave> = match wave {
        Some(name) => {
            let wave = store
                .get_wave_by_name(name)
                .await
                .map_err(|err| format!("get_wave_by_name failed: {err}"))?
                .ok_or_else(|| format!("wave not found: {name}"))?;
            vec![wave]
        }
        None => store
            .list_waves(None)
            .await
            .map_err(|err| format!("list_waves failed: {err}"))?,
    };

    let mut outcomes = Vec::new();
    for wave_row in waves {
        let runs = store
            .list_stack_runs(wave_row.id())
            .await
            .map_err(|err| format!("list_stack_runs failed: {err}"))?;
        if runs.is_empty() && wave.is_none() {
            continue;
        }
        let _guard = acquire_reconcile_lock(wave_row.id()).await;
        let result = reconcile_wave_queue_with_ops(
            store,
            github,
            wave_row.id(),
            ops,
            None, // no EventHub in the verb; see module docs
        )
        .await;
        outcomes.push(WaveQueueOutcome {
            wave: wave_row.name().clone(),
            wave_id: wave_row.id().clone(),
            result,
        });
    }
    Ok(outcomes)
}

/// The daemon reads its GitHub token from config/`LFD_GITHUB_TOKEN`; the verb
/// honors the same env var, then falls back to the stored github provider
/// token so a plain shell works. With no token, live-PR lookups fall back to
/// the last synced state in the store.
async fn github_config_for_verb(store: &SharedStore) -> GitHubConfig {
    let env_token = std::env::var("LFD_GITHUB_TOKEN")
        .ok()
        .map(|value| value.trim().to_string())
        .filter(|value| !value.is_empty());
    let token = match env_token {
        Some(token) => Some(token),
        None => store
            .get_provider_token("github")
            .await
            .ok()
            .flatten()
            .map(|token| token.access_token),
    };
    GitHubConfig {
        webhook_secret: String::new(),
        token: token.map(SecretString::new),
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use std::path::Path;

    use time::OffsetDateTime;

    use crate::lfd::config::GitHubConfig;
    use crate::lfd::id::LfdId;
    use crate::lfd::queue::{QueueOps, QueueRebaseConflict};
    use crate::lfd::types::{
        LivePrState, LivePullRequestState, PullRequest, QueueBlockReason, RepoWork, Run,
        RunStackStatus, RunStatus, Wave, WaveStatus,
    };
    use crate::lfdb::SharedStore;

    use super::{reconcile_wave_queues_with_ops, WaveQueueOutcome};

    #[derive(Debug, Default)]
    struct MockOps {
        scratch_clean: bool,
    }

    impl QueueOps for MockOps {
        fn ensure_branch_checked_out(&self, _worktree: &Path, _branch: &str) -> Result<(), String> {
            Ok(())
        }

        fn mark_ready(&self, _worktree: &Path, _pr_number: u32) -> Result<(), String> {
            Ok(())
        }

        fn mark_draft(&self, _worktree: &Path, _pr_number: u32) -> Result<(), String> {
            Ok(())
        }

        fn rebase_onto_default(
            &self,
            _worktree: &Path,
            _default_branch: &str,
        ) -> Result<(), QueueRebaseConflict> {
            Ok(())
        }

        fn scratch_clean(&self, _worktree: &Path) -> Result<bool, String> {
            Ok(self.scratch_clean)
        }
    }

    async fn sqlite_store() -> SharedStore {
        let db_path = std::env::temp_dir().join(format!("lf-queue-verb-test-{}.db", LfdId::new()));
        let config = crate::lfdb::StorageConfig::sqlite(db_path);
        std::sync::Arc::new(
            crate::lfdb::open_store(&config)
                .await
                .expect("sqlite store should initialize"),
        )
    }

    fn make_wave(name: &str) -> Wave {
        Wave {
            id: LfdId::new(),
            name: name.to_string(),
            primary_flow: "ship-roadmap".to_string(),
            goal: "ship-roadmap".to_string(),
            metrics: Vec::new(),
            repos: vec![RepoWork {
                repo: ".".to_string(),
                worktree: String::new(),
                branch: String::new(),
                status: WaveStatus::Idle,
                iteration: 0,
                cycle_start_iteration: 0,
                position: 0,
            }],
            direction: Vec::new(),
            area: Vec::new(),
            paused: false,
            created_at: Some(OffsetDateTime::now_utc()),
            workers: 1,
            parent_wave_id: None,
        }
    }

    fn make_run(wave: &Wave, stack_position: u32, pr_number: u32) -> Run {
        Run {
            id: LfdId::new(),
            wave_id: wave.id().clone(),
            repo: wave.repo().to_string(),
            flow: wave.primary_flow().clone(),
            task: None,
            direction: wave.direction().clone(),
            area: wave.area().clone(),
            iteration: stack_position,
            step_index: 0,
            status: RunStatus::Completed,
            worktree: ".".to_string(),
            branch: format!("feature-{pr_number}"),
            started_at: Some(OffsetDateTime::now_utc()),
            ended_at: Some(OffsetDateTime::now_utc()),
            error: None,
            flow_parents: Vec::new(),
            execution_cursor: None,
            parent_run_id: None,
            parent_pr_number: None,
            stack_position,
            stack_group_id: wave.id().to_string(),
            stack_status: RunStackStatus::Active,
            lineage_inferred: false,
            target_branch: "main".to_string(),
            repair_of: None,
            pr: Some(PullRequest {
                url: format!("https://example.test/pr/{pr_number}"),
                number: Some(pr_number),
                state: Some("open".to_string()),
                title: Some(format!("run-{pr_number}")),
                branch: Some(format!("feature-{pr_number}")),
            }),
        }
    }

    async fn set_live_state(store: &SharedStore, run: &Run, state: LivePrState, is_draft: bool) {
        let pr_number = run.pr.as_ref().and_then(|pr| pr.number).expect("pr number");
        store
            .upsert_live_pr_state(&LivePullRequestState {
                repo_id: run.repo.clone(),
                pr_number,
                state,
                is_draft,
                head_ref: run.branch.clone(),
                head_sha: "abc123".to_string(),
                base_ref: "main".to_string(),
                updated_at: OffsetDateTime::now_utc(),
                merged_at: None,
                synced_at: OffsetDateTime::now_utc(),
            })
            .await
            .expect("live state");
    }

    fn assert_ok(outcomes: &[WaveQueueOutcome]) {
        for outcome in outcomes {
            assert!(
                outcome.result.is_ok(),
                "wave {} failed: {:?}",
                outcome.wave,
                outcome.result
            );
        }
    }

    #[tokio::test]
    async fn verb_promotes_head_and_infers_stack_status() {
        let store = sqlite_store().await;
        let wave = make_wave("queue-wave");
        store.create_wave(&wave).await.expect("wave");
        let merged = make_run(&wave, 0, 11);
        let head = make_run(&wave, 1, 12);
        store.create_run(&merged).await.expect("merged run");
        store.create_run(&head).await.expect("head run");
        set_live_state(&store, &merged, LivePrState::Merged, false).await;
        set_live_state(&store, &head, LivePrState::Open, true).await;

        let ops = MockOps {
            scratch_clean: true,
        };
        let outcomes = reconcile_wave_queues_with_ops(&store, &GitHubConfig::default(), None, &ops)
            .await
            .expect("reconcile");
        assert_eq!(outcomes.len(), 1);
        assert_ok(&outcomes);

        // Stack-status inference: the merged live PR flips the durable status.
        let runs = store.list_stack_runs(wave.id()).await.expect("runs");
        let statuses: HashMap<_, _> = runs
            .iter()
            .map(|run| (run.id.clone(), run.stack_status))
            .collect();
        assert_eq!(statuses.get(&merged.id), Some(&RunStackStatus::Merged));
        assert_eq!(statuses.get(&head.id), Some(&RunStackStatus::Active));

        // The head was promoted (ready), so its live state is no longer draft
        // and no queue block was written.
        let head_live = store
            .get_live_pr_state(&head.repo, 12)
            .await
            .expect("live state")
            .expect("head live state");
        assert!(!head_live.is_draft);
        assert!(store
            .list_queue_blocks(wave.id())
            .await
            .expect("blocks")
            .is_empty());
    }

    #[tokio::test]
    async fn verb_writes_queue_block_when_scratch_dirty() {
        let store = sqlite_store().await;
        let wave = make_wave("dirty-wave");
        store.create_wave(&wave).await.expect("wave");
        let run = make_run(&wave, 0, 31);
        store.create_run(&run).await.expect("run");
        set_live_state(&store, &run, LivePrState::Open, true).await;

        let ops = MockOps {
            scratch_clean: false,
        };
        let outcomes = reconcile_wave_queues_with_ops(&store, &GitHubConfig::default(), None, &ops)
            .await
            .expect("reconcile");
        assert_ok(&outcomes);

        let block = store
            .list_queue_blocks(wave.id())
            .await
            .expect("blocks")
            .into_iter()
            .next()
            .expect("block");
        assert_eq!(block.reason, QueueBlockReason::ScratchDirty);
        assert_eq!(block.run_id, run.id);
    }

    #[tokio::test]
    async fn verb_targets_named_wave_only() {
        let store = sqlite_store().await;
        let target = make_wave("target-wave");
        let other = make_wave("other-wave");
        store.create_wave(&target).await.expect("target wave");
        store.create_wave(&other).await.expect("other wave");
        let target_run = make_run(&target, 0, 41);
        let other_run = make_run(&other, 0, 42);
        store.create_run(&target_run).await.expect("target run");
        store.create_run(&other_run).await.expect("other run");
        set_live_state(&store, &target_run, LivePrState::Merged, false).await;
        set_live_state(&store, &other_run, LivePrState::Merged, false).await;

        let ops = MockOps {
            scratch_clean: true,
        };
        let outcomes = reconcile_wave_queues_with_ops(
            &store,
            &GitHubConfig::default(),
            Some("target-wave"),
            &ops,
        )
        .await
        .expect("reconcile");
        assert_eq!(outcomes.len(), 1);
        assert_eq!(outcomes[0].wave, "target-wave");

        let target_runs = store.list_stack_runs(target.id()).await.expect("runs");
        assert_eq!(target_runs[0].stack_status, RunStackStatus::Merged);
        // The other wave was not touched: its durable status is still Active.
        let other_runs = store.list_stack_runs(other.id()).await.expect("runs");
        assert_eq!(other_runs[0].stack_status, RunStackStatus::Active);
    }

    #[tokio::test]
    async fn verb_skips_waves_without_queue_state() {
        let store = sqlite_store().await;
        let wave = make_wave("idle-wave");
        store.create_wave(&wave).await.expect("wave");

        let ops = MockOps {
            scratch_clean: true,
        };
        let outcomes = reconcile_wave_queues_with_ops(&store, &GitHubConfig::default(), None, &ops)
            .await
            .expect("reconcile");
        assert!(outcomes.is_empty());
    }

    #[tokio::test]
    async fn verb_rejects_unknown_wave_name() {
        let store = sqlite_store().await;
        let ops = MockOps {
            scratch_clean: true,
        };
        let error =
            reconcile_wave_queues_with_ops(&store, &GitHubConfig::default(), Some("ghost"), &ops)
                .await
                .expect_err("unknown wave should fail");
        assert!(error.contains("wave not found"));
    }
}