loopflow 0.12.31

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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! A finite repository check. Task and landing owners retain all execution authority.
use std::fs::OpenOptions;
use std::path::Path;
use std::time::Duration;

use fs2::FileExt;
use serde::{Deserialize, Serialize};

use crate::durable::{WorkRef, WorkStatus};
use crate::id::ExecId;
use crate::journal::{exec_process_evidence, ProcessIdentityEvidence};
use crate::store::SharedStore;
use crate::work::task::Task;

use super::error::{OpsError, OpsResult};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskAutomation {
    pub task_id: String,
    pub issue: String,
    pub enabled: Option<bool>,
    pub exec_id: Option<String>,
    pub retry_key: Option<String>,
    pub retries: u32,
    pub checked_at: Option<i64>,
    pub detail: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AutomationCheck {
    pub checked_at: i64,
    pub tasks: Vec<TaskAutomation>,
    pub errors: Vec<String>,
    pub deferred: usize,
}

fn error(error: impl std::fmt::Display) -> OpsError {
    OpsError::Message(error.to_string())
}

pub fn select(issue: &str, enabled: bool) -> OpsResult<()> {
    let runtime = tokio::runtime::Runtime::new()?;
    runtime.block_on(async {
        let store = super::pr_landing::landing_store().await?;
        let task = store
            .get_task_by_issue(issue)
            .await
            .map_err(error)?
            .ok_or_else(|| error(format!("no Task exists for {issue:?}")))?;
        if enabled {
            let (_, flow) = super::task_execution::task_execution_and_flow(&store, &task.id)
                .await
                .map_err(error)?;
            if matches!(flow, super::task_flow::TaskFlowRecord::None) {
                let project = store
                    .get_project(&task.project_id)
                    .await
                    .map_err(error)?
                    .ok_or_else(|| error("Task Project is missing"))?;
                crate::controller::task::ensure_flow_position(
                    &store,
                    &task.id,
                    Some(&project.plan.flow),
                )
                .await
                .map_err(error)?;
            }
        }
        let _lock = store.sqlite.lock_checkout(&task.worktree).map_err(error)?;
        store
            .sqlite
            .set_task_automation(&task.id, enabled, false)
            .map_err(error)
    })
}

/// No pending review, unknown provider, or unrelated live Exec is exempted.
pub(crate) fn admission_blocker(
    store: &crate::store::sqlite::SqliteStore,
    task: &crate::durable::TaskId,
    recovery: bool,
    repair_session: Option<&str>,
) -> OpsResult<Option<String>> {
    let work = store.task_work(task).map_err(error)?;
    let caller = crate::journal::current_exec_id();
    for session in &work.sessions {
        if Some(session.id.as_str()) == repair_session {
            continue;
        }
        if session.completed_at.is_none()
            && session.kind != crate::session::SessionKind::Conversation
        {
            return Ok(Some(format!("Session {} awaits completion", session.id)));
        }
        if let Some(input) = store.session(&session.id).map_err(error)? {
            if !input.interactive && !input.input_published {
                let dead_managed_reservation = if recovery && session.managed {
                    input
                        .flow_session_id
                        .as_deref()
                        .map(|flow| store.pending_flow_step_exec(flow))
                        .transpose()
                        .map_err(error)?
                        .flatten()
                        .is_some_and(|exec| {
                            exec_process_evidence(store, &exec) == ProcessIdentityEvidence::Dead
                        })
                } else {
                    false
                };
                if !dead_managed_reservation {
                    return Ok(Some(format!("Session {} has a reserved input", session.id)));
                }
            }
        }
        if store.session_has_pending_turn(&session.id).map_err(error)?
            && (!recovery || !session.managed || session_engine_unresolved(store, &session.id)?)
        {
            return Ok(Some(format!(
                "Session {} has an unresolved provider turn",
                session.id
            )));
        }
    }
    for exec in &work.execs {
        if caller.as_ref() == Some(&exec.id) {
            continue;
        }
        if exec_process_evidence(store, &exec.id) != ProcessIdentityEvidence::Dead {
            return Ok(Some(format!("Exec {} is live or unresolved", exec.id)));
        }
    }
    for flow in &work.flows {
        if let Some(flow) = store.flow(&flow.summary.id).map_err(error)? {
            if flow.is_human() && !flow.finished {
                return Ok(Some("Flow awaits review".into()));
            }
            if let Some(claim) = flow.claim {
                if Some(&claim.owner.exec_id) != caller.as_ref()
                    && crate::journal::task_worker_owner_evidence(&claim.owner)
                        != ProcessIdentityEvidence::Dead
                {
                    return Ok(Some(format!(
                        "Flow {} has a live or unresolved worker",
                        flow.invocation.id
                    )));
                }
            }
        }
    }
    Ok(None)
}

pub(crate) fn session_engine_unresolved(
    store: &crate::store::sqlite::SqliteStore,
    session: &str,
) -> OpsResult<bool> {
    if let Some((pid, started)) = store.session_provider_process(session).map_err(error)? {
        return Ok(match crate::journal::process_started_at(pid) {
            Ok(Some(actual)) => (actual - started).abs() <= 3,
            Ok(None) => false,
            Err(_) => true,
        });
    }
    Ok(store
        .session_driver(session)
        .map_err(error)?
        .is_none_or(|driver| {
            exec_process_evidence(store, &driver.provider_exec_id) != ProcessIdentityEvidence::Dead
        }))
}

async fn repository_tasks(
    store: &SharedStore,
    repo: &crate::repository::RepoId,
) -> OpsResult<Vec<Task>> {
    let mut tasks = Vec::new();
    for task in store.list_tasks(None).await.map_err(error)? {
        let Some(wave) = store.get_wave(&task.wave_id).await.map_err(error)? else {
            continue;
        };
        if crate::repository::RepoId::discover(Path::new(wave.repo()))
            .ok()
            .as_ref()
            == Some(repo)
        {
            tasks.push(task);
        }
    }
    Ok(tasks)
}

pub fn reconcile(repo: &Path) -> OpsResult<AutomationCheck> {
    let root = crate::engine::worktrees::main_repo_root(repo).map_err(error)?;
    let lock = OpenOptions::new()
        .create(true)
        .truncate(false)
        .read(true)
        .write(true)
        .open(crate::engine::git::absolute_git_dir(&root)?.join("lf-task-reconcile.lock"))?;
    let mut report = AutomationCheck {
        checked_at: time::OffsetDateTime::now_utc().unix_timestamp(),
        tasks: vec![],
        errors: vec![],
        deferred: 0,
    };
    if let Err(cause) = FileExt::try_lock_exclusive(&lock) {
        if cause.kind() != std::io::ErrorKind::WouldBlock {
            return Err(cause.into());
        }
        // The running check already covers this repository; overlap is not a failure.
        eprintln!("another repository check is running");
        return Ok(report);
    }
    let runtime = tokio::runtime::Runtime::new()?;
    let result = runtime.block_on(async {
        let store = super::pr_landing::landing_store().await?;
        let deadline = tokio::time::Instant::now() + Duration::from_secs(45);
        super::pr_landing::reconcile_repository_async(&root, &store, deadline, &mut report.errors)
            .await?;
        let repo_id = crate::repository::RepoId::discover(&root).map_err(error)?;
        let mut tasks = Vec::new();
        for task in repository_tasks(&store, &repo_id).await? {
            let state = store.sqlite.task_automation(&task.id).map_err(error)?;
            if state.enabled.is_some() {
                tasks.push((task, state));
            }
        }
        tasks.sort_by_key(|(_, state)| state.checked_at.unwrap_or(0));
        for (mut task, mut state) in tasks {
            if tokio::time::Instant::now() >= deadline {
                report.deferred += 1;
                report.tasks.push(state);
                continue;
            }
            let outcome = tokio::time::timeout_at(
                deadline.min(tokio::time::Instant::now() + Duration::from_secs(10)),
                reconcile_task(&store, &mut task, &mut state),
            )
            .await;
            let detail = match outcome {
                Ok(Ok(detail)) => detail,
                Ok(Err(cause)) => {
                    report
                        .errors
                        .push(format!("{}: {cause}", task.plan.identifier));
                    cause.to_string()
                }
                Err(_) => {
                    report
                        .errors
                        .push(format!("{}: check deadline exceeded", task.plan.identifier));
                    "check deadline exceeded; next check will inspect the reservation".into()
                }
            };
            state.checked_at = Some(time::OffsetDateTime::now_utc().unix_timestamp());
            state.detail = Some(detail);
            store.sqlite.record_automation(&state).map_err(error)?;
            report.tasks.push(state);
        }
        Ok(report)
    });
    // Timed-out network observations retain their effect locks until process exit.
    runtime.shutdown_background();
    result
}

async fn reconcile_task(
    store: &SharedStore,
    task: &mut Task,
    state: &mut TaskAutomation,
) -> OpsResult<String> {
    if state.enabled != Some(true) {
        return Ok("held".into());
    }
    let local = store.local_home().await.map_err(error)?;
    let placement = store
        .placement(&WorkRef::Task(task.id.clone()))
        .await
        .map_err(error)?;
    if placement.home_id != local.id {
        return Ok(format!("placed on Home {}", placement.home_id));
    }
    if super::task::task_work_status(store, task).await? != WorkStatus::Ready
        || task.abandon_intent.is_some()
    {
        return Ok("terminal Task; no new work".into());
    }
    let lock = store.sqlite.lock_checkout(&task.worktree).map_err(error)?;
    // Selection is re-read after taking the reservation boundary.
    if store
        .sqlite
        .task_automation(&task.id)
        .map_err(error)?
        .enabled
        != Some(true)
    {
        return Ok("held".into());
    }
    if let Some(exec) = state
        .exec_id
        .as_deref()
        .map(ExecId::parse)
        .transpose()
        .map_err(error)?
    {
        if Some(&exec) != crate::journal::current_exec_id().as_ref()
            && exec_process_evidence(&store.sqlite, &exec) != ProcessIdentityEvidence::Dead
        {
            return Ok(format!("admission Exec {exec} is live or unresolved"));
        }
    }
    if let Some(reason) = admission_blocker(&store.sqlite, &task.id, true, None)? {
        return Ok(reason);
    }
    let Some(flow) = store.task_flow(&task.id).await.map_err(error)? else {
        return Ok("no unfinished captured Flow; select a Flow with lf task run".into());
    };
    if let Some(failure) = &flow.failure {
        return Ok(format!(
            "Flow failed: {}; inspect and explicitly retry",
            failure.reason
        ));
    }
    if flow.finished {
        return Ok("Flow finished".into());
    }
    if flow.is_human() || flow.pending_session_id.is_some() {
        return Ok("waiting for review".into());
    }
    if let Some(landing) = store.sqlite.operation_landing(flow.id()).map_err(error)? {
        if landing.state != crate::pr_landing::PrLandingState::Merged {
            return Ok(format!(
                "waiting for PR #{}: {}",
                landing.pr_number,
                landing
                    .blocked_reason
                    .as_deref()
                    .unwrap_or(landing.state.as_str())
            ));
        }
    }
    let key = format!(
        "{}:{}",
        flow.id(),
        serde_json::to_string(&flow.cursor).map_err(error)?
    );
    if state.retry_key.as_ref() != Some(&key) {
        state.retry_key = Some(key);
        state.retries = 0;
        state.exec_id = None;
    }
    let completed = flow
        .current_attempt
        .as_ref()
        .is_some_and(|attempt| attempt.completed())
        || store
            .sqlite
            .flow_operation_completed(flow.id())
            .map_err(error)?;
    let retry = !completed
        && (state.exec_id.is_some() || flow.current_attempt.is_some() || flow.claim.is_some());
    let config = crate::engine::config::load_config_or_default(Some(&task.worktree));
    if retry && state.retries >= config.automation.retries {
        return Ok(
            "unchanged failure exhausted automatic retries; inspect and explicitly retry".into(),
        );
    }
    state.exec_id = crate::journal::current_exec_id().map(|id| id.to_string());
    if state.exec_id.is_none() {
        return Err(error("automatic admission requires a recorded Exec"));
    }
    if retry {
        state.retries += 1;
    }
    store.sqlite.record_automation(state).map_err(error)?;
    drop(lock);
    if retry {
        crate::lf::commands::flow::prepare_native_retry(store, flow)
            .await
            .map_err(error)?;
    }
    super::task::exec_task_process(store, task, None).await?;
    Ok("Flow admitted through its saved claim".into())
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AutomationStatus {
    pub enabled: bool,
    pub cadence_seconds: u32,
    pub coverage: String,
    pub last_success_at: Option<i64>,
    pub last_failure_at: Option<i64>,
    pub last_error: Option<String>,
    pub tasks: Vec<TaskAutomation>,
    pub deliveries: Vec<DeliveryStatus>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeliveryStatus {
    pub pr_number: u32,
    pub task_id: Option<String>,
    pub state: String,
    pub detail: Option<String>,
}

pub fn status(repo: &Path) -> OpsResult<AutomationStatus> {
    let root = crate::engine::worktrees::main_repo_root(repo).map_err(error)?;
    let runtime = tokio::runtime::Runtime::new()?;
    runtime.block_on(async {
        let store = super::pr_landing::landing_store().await?;
        let local = store.local_home().await.map_err(error)?;
        let key = super::cron::repository_cron_key(&root, &local.id);
        let jobs = super::cron::list_crons(
            &super::cron::default_launch_agents_dir()?,
            &super::cron::SystemLaunchctl,
        )?;
        let job = jobs.iter().find(|job| {
            job.target_kind == super::cron::CronTargetKind::Repository && job.flow == key
        });
        let enabled = job.is_some_and(|job| job.loaded);
        let receipts = super::cron::list_cron_receipts(
            &super::cron::receipt_root(&crate::store::lf_home_dir()),
            "",
            Some(&key),
            35,
        )?;
        let success = receipts
            .iter()
            .find(|receipt| receipt.outcome == super::cron::CronOutcome::Succeeded);
        let failure = receipts
            .iter()
            .find(|receipt| receipt.outcome == super::cron::CronOutcome::Failed);
        let now = time::OffsetDateTime::now_utc().unix_timestamp();
        let coverage = if !enabled {
            "disabled"
        } else if receipts
            .first()
            .is_some_and(|receipt| receipt.outcome == super::cron::CronOutcome::Failed)
        {
            "failed"
        } else if success.is_none() {
            "unknown"
        } else if success.is_some_and(|receipt| now - receipt.started_at > 180) {
            "overdue"
        } else {
            "checked"
        };
        let repo_id = crate::repository::RepoId::discover(&root).map_err(error)?;
        let mut tasks = Vec::new();
        for task in repository_tasks(&store, &repo_id).await? {
            // Finished Tasks get no new work; unsettled deliveries are listed below.
            if super::task::task_work_status(&store, &task).await? == WorkStatus::Ready {
                tasks.push(store.sqlite.task_automation(&task.id).map_err(error)?);
            }
        }
        let deliveries = store
            .pending_pr_landings(repo_id.as_str())
            .await
            .map_err(error)?
            .into_iter()
            .map(|landing| DeliveryStatus {
                pr_number: landing.pr_number,
                task_id: landing.task_id.map(|id| id.to_string()),
                state: landing.state.as_str().into(),
                detail: landing.blocked_reason,
            })
            .collect();
        Ok(AutomationStatus {
            enabled,
            cadence_seconds: 60,
            coverage: coverage.into(),
            last_success_at: success.map(|receipt| receipt.started_at),
            last_failure_at: failure.map(|receipt| receipt.started_at),
            last_error: failure.and_then(|receipt| receipt.error.clone()),
            tasks,
            deliveries,
        })
    })
}