loopflow 0.12.32

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
use std::num::NonZeroU32;

use anyhow::Context;
use clap::Subcommand;

use crate::exec::{Exec, ExecCursor, ExecFilter, ExecOutcomeFilter, ExecWorkFilter};
use crate::repository::CanonicalRepo;
use crate::store::{open_store, storage_config_from_env};

#[derive(Debug, Subcommand)]
pub enum MonitorCommand {
    /// List a bounded page of recorded commands, newest first
    List {
        #[arg(long)]
        json: bool,
        /// Include all repositories
        #[arg(long)]
        all: bool,
        #[arg(long, default_value = "100")]
        limit: NonZeroU32,
        /// Continue with the previous page's next object, encoded as JSON
        #[arg(long, value_parser = parse_cursor)]
        after: Option<ExecCursor>,
        /// Direct children of an exact or unambiguous parent Exec
        #[arg(long)]
        parent: Option<String>,
        /// Commands issued by this AgentSession
        #[arg(long)]
        caller: Option<String>,
        /// Literal command text, ignoring ASCII case
        #[arg(long)]
        search: Option<String>,
        #[arg(long, value_parser = ["succeeded", "failed", "interrupted", "unknown"])]
        outcome: Option<String>,
        /// Recorded work for a Task, including completed Tasks
        #[arg(long, conflicts_with = "wave")]
        task: Option<String>,
        /// Recorded work for a Wave
        #[arg(long)]
        wave: Option<String>,
    },
    /// Inspect an Exec or Session by identity
    Show {
        id: String,
        #[arg(long, conflicts_with_all = ["events", "final_answer"])]
        json: bool,
        /// Print a Session's retained raw input events
        #[arg(long, conflicts_with = "final_answer")]
        events: bool,
        /// Print a Session's last recorded provider conclusion
        #[arg(long = "final")]
        final_answer: bool,
        /// Inspect an exact retained input belonging to this Session
        #[arg(long)]
        input: Option<String>,
        /// Print only what the step's submitted input was made of, by source
        #[arg(long, conflicts_with_all = ["events", "final_answer"])]
        context: bool,
    },
    /// Observe active conversations and missing process evidence
    Active {
        #[arg(long)]
        json: bool,
        /// Stream NDJSON until stdin closes
        #[arg(long, requires = "json")]
        watch: bool,
        #[arg(long)]
        task: Option<String>,
    },
    /// Show direct provider-authored usage from recorded Session inputs
    Usage {
        /// Emit Session usage evidence as JSON
        #[arg(long)]
        json: bool,
        /// Observation window, in days (zero means all time)
        #[arg(long, default_value_t = 30)]
        days: u32,
        /// Report context cost and turn time by week since 2026-09-30
        #[arg(long, conflicts_with_all = ["days", "parent"])]
        weekly: bool,
        /// Compare Task and Wave usage under prospective and post-hoc bind attribution
        #[arg(long, conflicts_with_all = ["days", "parent", "weekly", "context", "wave", "project", "task"])]
        binds: bool,
        /// Inputs issued by this Session or retained capture
        #[arg(long, conflicts_with_all = ["wave", "project", "task"])]
        parent: Option<String>,
        /// Limit to Session inputs attributed to one Wave
        #[arg(long)]
        wave: Option<String>,
        /// Limit to Session inputs attributed to one Project
        #[arg(long)]
        project: Option<String>,
        /// Limit to Session inputs attributed to one Task
        #[arg(long)]
        task: Option<String>,
        /// Break each step's submitted input down by source, flagged against budgets
        #[arg(long)]
        context: bool,
    },
    /// Print one parseable snapshot of live Loopflow call trees
    Ps {
        /// Emit the versioned activity snapshot as JSON
        #[arg(long)]
        json: bool,
    },
    /// Refresh live Loopflow call trees on a terminal; print once when redirected
    Top {
        /// Emit one versioned activity snapshot as JSON
        #[arg(long)]
        json: bool,
    },
    /// Reap registered orphan providers and remove dead process receipts
    Prune {
        /// Show exact targets without changing process or receipt state
        #[arg(long)]
        dry_run: bool,
        /// Emit the versioned prune report as JSON
        #[arg(long)]
        json: bool,
    },
    /// Show one ordered record of durable Work, Session, PR, and Steer facts
    Activity {
        /// Relative window (7d, 24h, 30m) or RFC3339 start
        #[arg(long, default_value = "7d")]
        since: String,
        /// Maximum rows after Work filters (1-200)
        #[arg(long, default_value_t = 50)]
        limit: usize,
        /// Scope to one Wave by name
        #[arg(long)]
        wave: Option<String>,
        /// Scope to one Project by slug
        #[arg(long)]
        project: Option<String>,
        /// Scope to one Task by Linear identifier
        #[arg(long)]
        task: Option<String>,
        /// Emit the typed activity snapshot as JSON
        #[arg(long)]
        json: bool,
    },
}

fn parse_cursor(value: &str) -> Result<ExecCursor, String> {
    serde_json::from_str(value).map_err(|error| error.to_string())
}

pub fn run(command: &MonitorCommand) -> anyhow::Result<()> {
    match command {
        MonitorCommand::Active { json, watch, task } => {
            super::runs::list_active(*json, *watch, task.as_deref())
        }
        MonitorCommand::Ps { json } => super::top::run_ps(*json),
        MonitorCommand::Top { json } => super::top::run_top(*json),
        MonitorCommand::Prune { dry_run, json } => super::top::run_prune(*json, *dry_run),
        MonitorCommand::Usage {
            json, binds: true, ..
        } => super::bind_attribution::run(*json),
        MonitorCommand::Usage {
            json,
            weekly: true,
            wave,
            project,
            task,
            ..
        } => super::context_cost::run(
            *json,
            super::WorkFilter {
                wave: wave.as_deref(),
                project: project.as_deref(),
                task: task.as_deref(),
            },
        ),
        MonitorCommand::Usage {
            json,
            days,
            parent,
            wave,
            project,
            task,
            weekly: false,
            binds: false,
            context,
        } => super::usage::run(
            *context,
            *json,
            *days,
            wave.as_deref(),
            project.as_deref(),
            task.as_deref(),
            parent.as_deref(),
        ),
        MonitorCommand::Activity {
            since,
            limit,
            wave,
            project,
            task,
            json,
        } => super::activity::run(
            since,
            *limit,
            wave.as_deref(),
            project.as_deref(),
            task.as_deref(),
            *json,
        ),
        MonitorCommand::Show {
            id,
            json,
            events,
            final_answer,
            input,
            context,
        } => show(
            id,
            *json,
            *events,
            *final_answer,
            input.as_deref(),
            *context,
        ),
        MonitorCommand::List {
            json,
            all,
            limit,
            after,
            parent,
            caller,
            search,
            outcome,
            task,
            wave,
        } => tokio::runtime::Runtime::new()?.block_on(async {
            let store = open_store(&storage_config_from_env()?).await?;
            let repo = if *all {
                None
            } else {
                CanonicalRepo::current()?.map(|repo| repo.to_string())
            };
            let performed_work = match (task, wave) {
                (Some(task), _) => Some(ExecWorkFilter::Task(
                    store
                        .sqlite
                        .resolve_task_id(task, repo.as_deref())?
                        .context("Task was not found")?,
                )),
                (_, Some(wave)) => Some(ExecWorkFilter::Wave(
                    store
                        .sqlite
                        .resolve_wave_id(wave, repo.as_deref())?
                        .context("Wave was not found")?,
                )),
                _ => None,
            };
            let filter = ExecFilter {
                repo,
                parent_exec_id: match parent {
                    Some(parent) => Some(
                        store
                            .resolve_exec(parent)
                            .await?
                            .context("Parent Exec was not found")?
                            .id,
                    ),
                    None => None,
                },
                caller_session_id: caller.clone(),
                command_contains: search.clone(),
                outcome: outcome.as_deref().map(|value| match value {
                    "succeeded" => ExecOutcomeFilter::Succeeded,
                    "failed" => ExecOutcomeFilter::Failed,
                    "interrupted" => ExecOutcomeFilter::Interrupted,
                    _ => ExecOutcomeFilter::Unknown,
                }),
                performed_work,
                ..Default::default()
            };
            let page = store.execs(&filter, after.as_ref(), *limit).await?;
            if *json {
                println!("{}", serde_json::to_string_pretty(&page)?);
            } else {
                for exec in &page.entries {
                    print_exec(exec);
                }
                if let Some(next) = page.next {
                    println!(
                        "Continue with --after '{}' and the same filters",
                        serde_json::to_string(&next)?
                    );
                }
            }
            Ok(())
        }),
    }
}

fn print_exec(exec: &Exec) {
    let command = exec.command.as_deref().unwrap_or("unknown command");
    let display = serde_json::from_str::<Vec<String>>(command)
        .map(|argv| argv.join(" "))
        .unwrap_or_else(|_| command.to_string());
    println!(
        "{}  {}  {}  {}",
        exec.id,
        exec.started_at,
        exec.outcome.as_deref().unwrap_or("unknown"),
        display
    );
}

fn show(
    id: &str,
    json: bool,
    events: bool,
    final_answer: bool,
    input: Option<&str>,
    context: bool,
) -> anyhow::Result<()> {
    let exec = tokio::runtime::Runtime::new()?.block_on(async {
        let store = open_store(&storage_config_from_env()?).await?;
        let exec = store.resolve_exec(id).await?;
        let session = store.session(id).await?;
        anyhow::ensure!(
            exec.is_none() || session.is_none(),
            "Identity matches both an Exec and a Session; use an exact identity"
        );
        anyhow::ensure!(
            exec.is_some() || session.is_some(),
            "Exec or Session {id} was not found"
        );
        if let Some(input) = input {
            anyhow::ensure!(session.is_some(), "--input requires a Session identity");
            let captured = store.sqlite.input_history(input)?;
            anyhow::ensure!(
                captured.session_id == id,
                "Input does not belong to Session {id}"
            );
        }
        Ok::<_, anyhow::Error>(exec)
    })?;
    match exec {
        Some(exec) => {
            anyhow::ensure!(
                !events && !final_answer && !context,
                "--events, --final and --context inspect Session evidence; Execs record command outcomes"
            );
            if json {
                println!("{}", serde_json::to_string_pretty(&exec)?);
            } else {
                print_exec(&exec);
                println!("{}", serde_json::to_string_pretty(&exec)?);
            }
            Ok(())
        }
        None => super::runs::inspect(input.unwrap_or(id), events, final_answer, context, json),
    }
}

#[derive(Debug, serde::Serialize)]
struct MonitorItem {
    kind: &'static str,
    id: String,
    title: String,
    state: &'static str,
    reason: String,
    next_action: String,
}

#[derive(Debug, serde::Serialize)]
struct MonitorOverview {
    observed_at: i64,
    items: Vec<MonitorItem>,
    active: crate::session_record::active::ActiveSessionsSnapshot,
    recent_commands: crate::exec::ExecPage,
    sessions_next: Option<String>,
    flows_next: Option<String>,
    gaps: Vec<String>,
}

pub fn overview(json: bool, all: bool) -> anyhow::Result<()> {
    let report = tokio::runtime::Runtime::new()?.block_on(async {
        let store = std::sync::Arc::new(open_store(&storage_config_from_env()?).await?);
        let repo = if all {
            None
        } else {
            CanonicalRepo::current()?.map(|repo| repo.to_string())
        };
        let home = crate::store::lf_home_dir();
        let active = crate::session_record::active::snapshot(&home, &store, None).await;
        let mut gaps = active.gaps.clone();
        let mut items = Vec::new();
        let filter = crate::session::SessionFilter {
            repo: repo.clone(),
            interactive: None,
            history: true,
            limit: 101,
            after: Some(String::new()),
            ..Default::default()
        };
        let mut sessions = match crate::ops::human_session::list(&store, &filter).await {
            Ok(sessions) => sessions,
            Err(error) => {
                gaps.push(format!("Session inventory unavailable: {error}"));
                Vec::new()
            }
        };
        let sessions_next = if sessions.len() > 100 {
            sessions.truncate(100);
            sessions.last().map(|session| session.id.clone())
        } else {
            None
        };
        for session in sessions {
            use crate::ops::human_session::SessionState;
            let live = active.sessions.iter().any(|live| live.id == session.id);
            let (state, reason, next) = match session.state {
                SessionState::Closed => (
                    "finished",
                    "Conversation completion is recorded".to_string(),
                    format!("lf session history {}", session.id),
                ),
                SessionState::Ready => (
                    "waiting",
                    session
                        .ready_summary
                        .clone()
                        .unwrap_or_else(|| "Review is ready".into()),
                    format!("lf session connect {}", session.id),
                ),
                _ if live => (
                    "active",
                    "Matching provider process observed".into(),
                    "lf monitor active --json".to_string(),
                ),
                SessionState::Waiting => (
                    "waiting",
                    session.detail.clone(),
                    format!("lf session connect {}", session.id),
                ),
                _ => (
                    "unknown",
                    "No current provider observation or completion receipt".into(),
                    format!("lf session history {}", session.id),
                ),
            };
            items.push(MonitorItem {
                kind: "session",
                id: session.id,
                title: session.title,
                state,
                reason,
                next_action: next,
            });
        }
        let flows = store
            .flow_inventory(
                &crate::durable::FlowFilter {
                    repo: repo.clone(),
                    ..Default::default()
                },
                None,
                NonZeroU32::new(100).expect("100 is nonzero"),
            )
            .await?;
        for entry in flows.entries {
            let summary = &entry.summary;
            let detail = match store.flow_detail(&summary.id).await {
                Ok(detail) => detail,
                Err(error) => {
                    gaps.push(format!(
                        "FlowSession {} detail unavailable: {error}",
                        summary.id
                    ));
                    None
                }
            };
            let (state, reason, next) =
                if let Some(failure) = detail.as_ref().and_then(|detail| detail.failure.as_ref()) {
                    (
                        "blocked",
                        failure.reason.clone(),
                        format!("lf flow show {} --sessions", summary.id),
                    )
                } else if summary.state != crate::session::FlowSummaryState::Current {
                    (
                        "finished",
                        format!("Recorded {:?}", summary.state),
                        format!("lf flow show {} --sessions", summary.id),
                    )
                } else if let Some(session) = &summary.pending_session {
                    (
                        "waiting",
                        "Flow waits for a review or decision".into(),
                        format!("lf session connect {session}"),
                    )
                } else {
                    (
                        "unknown",
                        "Saved progress does not establish a live driver".into(),
                        format!("lf flow show {} --sessions", summary.id),
                    )
                };
            items.push(MonitorItem {
                kind: "flow_session",
                id: summary.id.clone(),
                title: summary
                    .name
                    .clone()
                    .unwrap_or_else(|| "Unnamed Flow".into()),
                state,
                reason,
                next_action: next,
            });
        }
        let recent_commands = store
            .execs(
                &ExecFilter {
                    repo,
                    ..Default::default()
                },
                None,
                NonZeroU32::new(20).expect("20 is nonzero"),
            )
            .await?;
        Ok::<_, anyhow::Error>(MonitorOverview {
            observed_at: active.observed_at,
            items,
            active,
            recent_commands,
            sessions_next,
            flows_next: flows.next,
            gaps,
        })
    })?;
    if json {
        println!("{}", serde_json::to_string_pretty(&report)?);
    } else {
        for item in &report.items {
            println!(
                "{}  {}  {} ({})\n  {}\n  {}",
                item.state, item.id, item.title, item.kind, item.reason, item.next_action
            );
        }
        for command in &report.recent_commands.entries {
            print_exec(command);
        }
        for gap in &report.gaps {
            println!("Unavailable: {gap}");
        }
        if report.items.is_empty() {
            println!("No recorded conversations or Flows in this scope.");
        }
        if report.sessions_next.is_some() {
            println!(
                "More conversations: lf session list --interactive all --history --page --json"
            );
        }
        if report.flows_next.is_some() {
            println!("More Flows: lf flow list --sessions --json");
        }
        if report.recent_commands.next.is_some() {
            println!("More commands: lf monitor list");
        }
    }
    Ok(())
}