loopflow 0.12.14

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
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
use std::path::{Path, PathBuf};

use crate::durable::{
    AdvanceReceipt, AgentInvocation, BoundaryState, Containment, InvocationRoute, ProjectId,
    RunAdvance, RunContext, RunTrigger, TaskId, WorkRef,
};
use crate::engine::process::{
    current_home_execution_context, current_process_group_id, pin_control_binary,
    start_lf_session_with_env,
};
use crate::id::WaveId;
use crate::store::SharedStore;

use super::{OpsError, OpsResult};

#[derive(Debug, Clone)]
pub struct WorkBinding {
    pub work: WorkRef,
    pub wave_id: WaveId,
    pub wave_name: String,
    pub cwd: PathBuf,
    pub context: String,
}

impl WorkBinding {
    pub fn matches_run(&self, context: &RunContext) -> bool {
        context.work == self.work
    }
}

#[derive(Debug)]
pub(crate) struct DirectRun {
    store: SharedStore,
    context: RunContext,
    invocation: AgentInvocation,
    finished: bool,
}

impl DirectRun {
    pub(crate) async fn start(
        store: SharedStore,
        binding: &WorkBinding,
        route: InvocationRoute,
        surface: &str,
    ) -> OpsResult<Self> {
        let (_, context) = store
            .reserve_run(&binding.work, RunTrigger::User)
            .await
            .map_err(run_error)?;
        let process_group = match current_process_group_id() {
            Some(process_group) => process_group,
            None => {
                let _ = store.stop_run_on_interrupt(&context);
                return Err(run_error("direct lf invocation has no process group"));
            }
        };
        if let Err(error) = store
            .advance_run(
                &context,
                RunAdvance::RunStarting {
                    containment: Containment::ProcessGroup {
                        id: i64::from(process_group),
                    },
                    cwd: binding.cwd.clone(),
                },
            )
            .await
        {
            let _ = store.stop_run_on_interrupt(&context);
            return Err(run_error(error));
        }
        let invocation = match store
            .advance_run(
                &context,
                RunAdvance::InvocationStarting {
                    route,
                    surface: surface.to_string(),
                    resume_token: None,
                    answer_ask_id: None,
                },
            )
            .await
        {
            Ok(AdvanceReceipt::Invocation(invocation)) => invocation,
            Ok(_) => unreachable!("InvocationStarting returns an Invocation receipt"),
            Err(error) => {
                let _ = store.stop_run_on_interrupt(&context);
                return Err(run_error(error));
            }
        };

        let cleanup_store = store.clone();
        let cleanup_context = context.clone();
        crate::engine::agent::register_interrupt_cleanup(move || {
            let _ = cleanup_store.stop_run_on_interrupt(&cleanup_context);
        });

        Ok(Self {
            store,
            context,
            invocation,
            finished: false,
        })
    }

    pub(crate) fn context(&self) -> &RunContext {
        &self.context
    }

    pub(crate) fn invocation(&self) -> &AgentInvocation {
        &self.invocation
    }

    pub(crate) async fn finish(&mut self, outcome: BoundaryState) -> OpsResult<()> {
        let invocation_result = self
            .store
            .advance_run(
                &self.context,
                RunAdvance::InvocationEnded {
                    invocation_id: self.invocation.id.clone(),
                    outcome,
                },
            )
            .await;
        let run_result = self.store.finish_run(&self.context, outcome).await;
        if run_result.is_ok() {
            self.finished = true;
        }
        invocation_result.map_err(run_error)?;
        run_result.map_err(run_error)
    }
}

impl Drop for DirectRun {
    fn drop(&mut self) {
        if !self.finished {
            let _ = self.store.stop_run_on_interrupt(&self.context);
        }
    }
}

pub async fn resolve_work_binding(
    store: &SharedStore,
    repo: &Path,
    selector: &str,
) -> OpsResult<WorkBinding> {
    let (kind, value) = selector.split_once(':').ok_or_else(|| {
        run_error(format!(
            "invalid --as {selector:?}; use task:<selector>, project:<selector>, or wave:<selector>"
        ))
    })?;
    let value = value.trim();
    if value.is_empty() {
        return Err(run_error(format!("{kind} selector cannot be empty")));
    }

    match kind {
        "task" => {
            let task = if let Ok(id) = TaskId::parse(value) {
                store.get_task(&id).await.map_err(run_error)?
            } else {
                store.get_task_by_issue(value).await.map_err(run_error)?
            }
            .ok_or_else(|| run_error(format!("Task {value:?} is not registered")))?;
            let wave = store
                .get_wave(&task.wave_id)
                .await
                .map_err(run_error)?
                .ok_or_else(|| run_error(format!("Task {} has no owning Wave", task.id)))?;
            let project = store
                .get_project(&task.project_id)
                .await
                .map_err(run_error)?
                .ok_or_else(|| run_error(format!("Task {} has no owning Project", task.id)))?;
            Ok(WorkBinding {
                work: WorkRef::Task(task.id.clone()),
                wave_id: task.wave_id,
                wave_name: wave.name().to_string(),
                cwd: task.worktree,
                context: format!(
                    "Task {}: {}\n\n{}\n\nProject {}:\n{}",
                    task.plan.identifier,
                    task.plan.title,
                    task.plan.description,
                    project.plan.slug,
                    project.plan.prompt_context,
                ),
            })
        }
        "project" => {
            let project = if let Ok(id) = ProjectId::parse(value) {
                store.get_project(&id).await.map_err(run_error)?
            } else {
                let matches = store
                    .list_projects(None)
                    .await
                    .map_err(run_error)?
                    .into_iter()
                    .filter(|project| {
                        project.plan.id.as_str() == value || project.plan.slug == value
                    })
                    .collect::<Vec<_>>();
                match matches.as_slice() {
                    [project] => Some(project.clone()),
                    [] => None,
                    _ => {
                        return Err(run_error(format!(
                            "Project selector {value:?} is ambiguous; use its durable or planning-system id"
                        )));
                    }
                }
            }
            .ok_or_else(|| run_error(format!("Project {value:?} is not registered")))?;
            let wave = store
                .get_wave(&project.wave_id)
                .await
                .map_err(run_error)?
                .ok_or_else(|| run_error(format!("Project {} has no owning Wave", project.id)))?;
            let metric_context = crate::ops::metrics::metric_prompt_section(
                "project-owned-metrics",
                crate::ops::metrics::stored_project_metric_portfolio(
                    store,
                    &wave,
                    project.plan.id.as_str(),
                    time::OffsetDateTime::now_utc(),
                )
                .await,
            );
            Ok(WorkBinding {
                work: WorkRef::Project(project.id.clone()),
                wave_id: project.wave_id,
                wave_name: wave.name().to_string(),
                cwd: PathBuf::from(wave.repo()),
                context: format!(
                    "Project {}: {}\n\n{}\n\n{}\n\nOnly metrics owned by this Project appear above. Cross-owned evidence appears only when the Wave routes it through durable direction. Metrics inform KR judgment; they never check a KR automatically.",
                    project.plan.slug,
                    project.plan.name,
                    project.plan.prompt_context,
                    metric_context,
                ),
            })
        }
        "wave" => {
            let wave = if let Ok(id) = WaveId::parse(value) {
                store.get_wave(&id).await.map_err(run_error)?
            } else {
                let locator = crate::wave::WaveLocator::discover(repo, value).map_err(run_error)?;
                store.get_wave_at(&locator).await.map_err(run_error)?
            }
            .ok_or_else(|| run_error(format!("Wave {value:?} is not registered")))?;
            let metric_context = crate::ops::metrics::metric_prompt_section(
                "metric-portfolio",
                crate::ops::metrics::stored_wave_metric_portfolio(
                    store,
                    &wave,
                    time::OffsetDateTime::now_utc(),
                )
                .await,
            );
            Ok(WorkBinding {
                work: WorkRef::Wave(wave.id().clone()),
                wave_id: wave.id().clone(),
                wave_name: wave.name().to_string(),
                cwd: PathBuf::from(wave.repo()),
                context: format!(
                    "Wave {}\n\n{}\n\nAnswer the executive loop from the objective, Project portfolio, Work state, and evidence:\n1. What is most important?\n2. What signals are arriving?\n3. What works?\n4. What does not?\n5. What is the current strategy?\n6. How should strategy adjust?\n\nMetrics are evidence, never automatic KR completion or a composite Wave score.",
                    wave.name(),
                    metric_context,
                ),
            })
        }
        _ => Err(run_error(format!(
            "invalid --as kind {kind:?}; expected task, project, or wave"
        ))),
    }
}

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

#[derive(Debug)]
pub(crate) struct RunLaunch {
    pub work: WorkRef,
    pub wave_id: WaveId,
    pub cwd: PathBuf,
    pub tmux_name: String,
    pub agent: String,
    pub account_id: Option<crate::store::ProviderAccountId>,
    pub resume_token: Option<String>,
}

pub(crate) async fn launch_in_run(
    store: &SharedStore,
    lease: &RunContext,
    request: RunLaunch,
) -> OpsResult<AgentInvocation> {
    let execution = current_home_execution_context()
        .map_err(|error| OpsError::Message(format!("cannot resolve current lf binary: {error}")))?;
    let (provider, model) = crate::engine::config::parse_agent(&request.agent);
    let tmux_name = request.tmux_name.clone();
    let run = store
        .advance_run(
            lease,
            RunAdvance::RunStarting {
                containment: Containment::Tmux {
                    name: tmux_name.clone(),
                },
                cwd: request.cwd.clone(),
            },
        )
        .await
        .map_err(|error| OpsError::Message(error.to_string()))?;
    let AdvanceReceipt::Run(_) = run else {
        unreachable!("RunStarting returns a Run receipt")
    };
    let receipt = store
        .advance_run(
            lease,
            RunAdvance::InvocationStarting {
                route: InvocationRoute {
                    provider,
                    model,
                    account_id: request
                        .account_id
                        .as_ref()
                        .map(|account_id| account_id.as_str().to_string()),
                },
                surface: "headless".to_string(),
                resume_token: request.resume_token,
                answer_ask_id: None,
            },
        )
        .await
        .map_err(|error| OpsError::Message(error.to_string()))?;
    let AdvanceReceipt::Invocation(invocation) = receipt else {
        unreachable!("InvocationStarting returns an Invocation receipt")
    };
    let control_bin = pin_control_binary(&execution.lf_bin)
        .to_string_lossy()
        .to_string();
    let argv = vec![
        control_bin.clone(),
        "__work".to_string(),
        request.work.kind().to_string(),
        request.work.id().to_string(),
    ];
    let run_context = lease.run_id.to_string();
    let invocation_id = invocation.id.as_str().to_string();
    let db_path = execution.db_path.to_string_lossy().to_string();
    let lf_home = execution.lf_home.to_string_lossy().to_string();
    let environment = [
        (
            crate::engine::wave_context::WAVE_ID_ENV,
            request.wave_id.as_str(),
        ),
        (crate::durable::RUN_CONTEXT_ENV, "agent"),
        (crate::durable::RUN_ID_ENV, run_context.as_str()),
        (crate::durable::AGENT_INVOCATION_ENV, invocation_id.as_str()),
        (crate::store::CONTROL_BIN_ENV, control_bin.as_str()),
        (crate::store::CONTROL_DB_PATH_ENV, db_path.as_str()),
        (crate::store::CONTROL_HOME_ENV, lf_home.as_str()),
    ];
    if let Err(error) =
        start_lf_session_with_env(&tmux_name, &request.cwd, &argv, &environment).await
    {
        let _ = store
            .advance_run(
                lease,
                RunAdvance::InvocationEnded {
                    invocation_id: invocation.id.clone(),
                    outcome: BoundaryState::Failed,
                },
            )
            .await;
        let _ = store
            .stop_run(
                lease,
                crate::durable::StopCause::Recovery,
                crate::durable::ContainmentObservation::Absent,
            )
            .await;
        return Err(OpsError::Message(format!(
            "failed to launch {} body: {error}",
            request.work.kind()
        )));
    }
    Ok(invocation)
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use time::OffsetDateTime;

    use super::*;
    use crate::planning::{LinearProjectId, ProjectPlan};
    use crate::pm::{PmKr, PmProject, PmSnapshot, ProjectFlowPlan};
    use crate::project::Project;
    use crate::store::{open_store, PmSnapshotRow, StorageConfig};
    use crate::wave::Wave;

    async fn test_store() -> (tempfile::TempDir, SharedStore) {
        let directory = tempfile::tempdir().unwrap();
        let store = open_store(&StorageConfig::sqlite(directory.path().join("registry.db")))
            .await
            .unwrap();
        (directory, Arc::new(store))
    }

    fn binding(wave: &Wave, cwd: &Path) -> WorkBinding {
        WorkBinding {
            work: WorkRef::Wave(wave.id().clone()),
            wave_id: wave.id().clone(),
            wave_name: wave.name().to_string(),
            cwd: cwd.to_path_buf(),
            context: format!("Wave {}", wave.name()),
        }
    }

    fn project(wave: &Wave, slug: &str, planning_id: &str) -> Project {
        let now = OffsetDateTime::now_utc();
        Project {
            id: ProjectId::new(),
            plan: ProjectPlan {
                id: LinearProjectId::new(planning_id).unwrap(),
                slug: slug.to_string(),
                name: slug.to_string(),
                prompt_context: "Ship the requested behavior.".to_string(),
                pm_snapshot_synced_at: now.unix_timestamp(),
            },
            wave_id: wave.id().clone(),
            iteration: 0,
            observation_cursor: 0,
            last_state_fingerprint: None,
            agent: "codex".to_string(),
            provider: "codex".to_string(),
            provider_session_id: None,
            abandon_intent: None,
            created_at: now,
            updated_at: now,
        }
    }

    #[tokio::test]
    async fn project_slug_selector_requires_one_exact_match() {
        let (directory, store) = test_store().await;
        let wave = Wave::new(
            WaveId::new(),
            "runtime".to_string(),
            directory.path().display().to_string(),
        );
        store.create_wave(&wave).await.unwrap();
        store
            .create_project(&project(&wave, "shared", "project-one"))
            .await
            .unwrap();
        store
            .create_project(&project(&wave, "shared", "project-two"))
            .await
            .unwrap();

        let error = resolve_work_binding(&store, directory.path(), "project:shared")
            .await
            .expect_err("ambiguous slug must not infer identity");

        assert!(error.to_string().contains("ambiguous"));
    }

    #[tokio::test]
    async fn direct_wave_and_project_bindings_carry_the_shared_metric_context() {
        let (directory, store) = test_store().await;
        let repo = directory.path().join("repo");
        std::fs::create_dir_all(repo.join("wave/runtime")).unwrap();
        let wave = Wave::new(
            WaveId::new(),
            "runtime".to_string(),
            repo.display().to_string(),
        );
        store.create_wave(&wave).await.unwrap();
        store
            .create_project(&project(&wave, "loopflow-api", "project-api"))
            .await
            .unwrap();
        let snapshot = PmSnapshot {
            projects: vec![PmProject {
                id: "project-api".to_string(),
                slug: "loopflow-api".to_string(),
                name: "Loopflow API".to_string(),
                summary: String::new(),
                definition: "Keep one product model.".to_string(),
                flows: Some(ProjectFlowPlan::empty()),
                krs: vec![PmKr {
                    text: "One model everywhere".to_string(),
                    holds: false,
                }],
                initiative_ids: vec!["initiative-1".to_string()],
                team_ids: vec!["team-1".to_string()],
            }],
            items: Vec::new(),
        };
        store
            .put_pm_snapshot(PmSnapshotRow {
                wave_id: wave.id().clone(),
                provider: "linear".to_string(),
                initiative: "initiative-1".to_string(),
                synced_at: OffsetDateTime::now_utc().unix_timestamp(),
                payload: serde_json::to_string(&snapshot).unwrap(),
            })
            .await
            .unwrap();

        let project_binding = resolve_work_binding(&store, &repo, "project:project-api")
            .await
            .unwrap();
        assert!(project_binding
            .context
            .contains("<lf:project-owned-metrics>"));
        assert!(project_binding.context.contains("\"metrics\":[]"));
        assert!(!project_binding.context.contains("<lf:metric-portfolio>"));

        let wave_binding =
            resolve_work_binding(&store, &repo, &format!("wave:{}", wave.id().as_str()))
                .await
                .unwrap();
        assert!(wave_binding.context.contains("<lf:metric-portfolio>"));
        assert!(wave_binding.context.contains("What signals are arriving?"));
        assert!(!wave_binding.context.contains("<lf:project-owned-metrics>"));
    }

    #[tokio::test]
    async fn work_binding_matches_only_its_run_identity() {
        let (directory, store) = test_store().await;
        let selected = Wave::new(
            WaveId::new(),
            "selected".to_string(),
            directory.path().display().to_string(),
        );
        let other = Wave::new(
            WaveId::new(),
            "other".to_string(),
            directory.path().display().to_string(),
        );
        store.create_wave(&selected).await.unwrap();
        store.create_wave(&other).await.unwrap();
        let selected_binding = binding(&selected, directory.path());
        let (_, selected_lease) = store
            .reserve_run(&selected_binding.work, RunTrigger::User)
            .await
            .unwrap();
        let (_, other_lease) = store
            .reserve_run(&WorkRef::Wave(other.id().clone()), RunTrigger::User)
            .await
            .unwrap();

        assert!(selected_binding.matches_run(&selected_lease));
        assert!(!selected_binding.matches_run(&other_lease));
    }

    #[tokio::test]
    async fn direct_run_fences_overlap_and_releases_work_after_finish() {
        let (directory, store) = test_store().await;
        let wave = Wave::new(
            WaveId::new(),
            "runtime".to_string(),
            directory.path().display().to_string(),
        );
        store.create_wave(&wave).await.unwrap();
        let binding = binding(&wave, directory.path());
        let route = InvocationRoute {
            provider: "codex".to_string(),
            model: None,
            account_id: None,
        };
        let mut direct = DirectRun::start(store.clone(), &binding, route.clone(), "headless")
            .await
            .unwrap();

        let active_run = store.current_run(&binding.work).await.unwrap().unwrap();
        DirectRun::start(store.clone(), &binding, route, "headless")
            .await
            .expect_err("active direct Run must fence overlap");
        assert_eq!(
            store.current_run(&binding.work).await.unwrap().unwrap().id,
            active_run.id
        );

        direct.finish(BoundaryState::Succeeded).await.unwrap();
        assert!(store.current_run(&binding.work).await.unwrap().is_none());
        let invocation = store
            .invocations_for_run(&direct.context().run_id)
            .await
            .unwrap()
            .pop()
            .expect("direct invocation");
        assert!(invocation.ended_at.is_some());
    }
}