loopflow 0.12.3

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
//! Place Work on durable Homes and route execution to those Homes.
//!
//! Placement is the only execution-location authority. `lf start` groups Waves
//! by Home and asks each Home's one resident to serve them. Remote durable
//! lifecycle commands use that Home's observed SSH route without forwarding
//! the origin machine's provider, GitHub, PM, or secret authority.

use std::collections::HashMap;
use std::path::Path;

use anyhow::anyhow;

use crate::engine::wave_context::{resolve_managed_wave_name_sync, resolve_run_wave_name};
use crate::engine::wave_home::{
    resolve_home_relative_repo, HomeActionDto, HomeRoute, HomeRuntimeDto, HomeState,
    HOME_ROUTED_ENV,
};
use crate::lf::{Commands, HomeCommand};
use crate::provider_account::lease::AccountSelection;

/// `lf home <id|observe|probe>` — inspect durable Home identity and reachability.
pub fn run(cmd: &HomeCommand, repo: &Path) -> anyhow::Result<()> {
    match cmd {
        HomeCommand::Id { json } => id_cmd(*json),
        HomeCommand::Observe {
            home_id,
            route,
            json,
        } => observe_cmd(home_id, route, *json),
        HomeCommand::Probe { wave, json } => probe_cmd(wave.as_deref(), *json, repo),
    }
}

fn id_cmd(json: bool) -> anyhow::Result<()> {
    let runtime = tokio::runtime::Runtime::new()?;
    let home = runtime.block_on(async {
        crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("lf home id needs an initialized local store"))?
            .local_home()
            .await
            .map_err(anyhow::Error::from)
    })?;
    if json {
        println!("{}", serde_json::to_string(&home)?);
    } else {
        println!("{}", home.id);
    }
    Ok(())
}

fn observe_cmd(home_id: &crate::durable::HomeId, route: &str, json: bool) -> anyhow::Result<()> {
    let parsed = crate::engine::wave_home::HomeRoute::parse(route)
        .ok_or_else(|| anyhow!("invalid Home route: {route:?}"))?;
    if !parsed.is_remote() {
        return Err(anyhow!(
            "the local Home route is managed by `lf home id`; observe only remote SSH routes"
        ));
    }
    let route = parsed.to_string();
    let runtime = tokio::runtime::Runtime::new()?;
    let home = runtime.block_on(async {
        crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("lf home observe needs an initialized local store"))?
            .observe_home(home_id, &route)
            .await
            .map_err(anyhow::Error::from)
    })?;
    if json {
        println!("{}", serde_json::to_string(&home)?);
    } else {
        println!("{}  {}", home.id, home.route);
    }
    Ok(())
}

fn probe_cmd(wave: Option<&str>, json: bool, _repo: &Path) -> anyhow::Result<()> {
    let name = resolve_managed_wave_name_sync(wave).map_err(|err| anyhow!("{err}"))?;
    let rt = tokio::runtime::Runtime::new()?;
    let runtime = rt.block_on(async {
        let store = crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("lf home probe needs an initialized local store"))?;
        let wave = store
            .get_wave_by_name(&name)
            .await?
            .ok_or_else(|| anyhow!("Wave '{name}' was not found"))?;
        let placement = store
            .placement(&crate::durable::WorkRef::Wave(wave.id().clone()))
            .await?;
        let home = store
            .home_by_id(&placement.home_id)
            .await?
            .ok_or_else(|| anyhow!("Home {} was not found", placement.home_id))?;
        Ok::<_, anyhow::Error>(
            crate::ops::home::probe_home(&name, &home, Path::new(wave.repo())).await,
        )
    })?;
    if json {
        println!("{}", serde_json::to_string(&runtime)?);
    } else {
        print_runtime(&name, &runtime);
    }
    Ok(())
}

pub fn start(waves: &[String], wave_ids: &[String], json: bool, repo: &Path) -> anyhow::Result<()> {
    let runtime = tokio::runtime::Runtime::new()?;
    let responses = runtime.block_on(start_inner(waves, wave_ids, repo))?;
    if json {
        println!("{}", serde_json::to_string(&responses)?);
        return Ok(());
    }
    for wave in responses {
        let state = if wave.live { "running" } else { "starting" };
        println!("{}  {state} on {}", wave.name, wave.home.id);
    }
    Ok(())
}

pub fn stop(name: &str, repo: &Path) -> anyhow::Result<()> {
    let name = crate::ops::util::normalize_wave_name(name)
        .ok_or_else(|| anyhow!("invalid wave name: '{name}'"))?;
    let runtime = tokio::runtime::Runtime::new()?;
    let target = runtime.block_on(resolve_stop_target(&name, repo))?;
    match target {
        StopTarget::Local => crate::wave::stop(&name),
        StopTarget::Remote { home_id, repo } => {
            let cmd = vec!["lf".to_string(), "stop".to_string(), name];
            crate::lf::commands::ssh::capture_remote_native(&home_id, &repo, &cmd)
                .map(|stdout| print!("{stdout}"))
                .map_err(|error| anyhow!("remote Home {home_id} stop failed: {error}"))
        }
    }
}

enum StopTarget {
    Local,
    Remote {
        home_id: crate::durable::HomeId,
        repo: String,
    },
}

async fn resolve_stop_target(name: &str, repo: &Path) -> anyhow::Result<StopTarget> {
    let repo =
        crate::engine::worktrees::main_repo_root(repo).unwrap_or_else(|_| repo.to_path_buf());
    let store = crate::store::open_existing_store()
        .await
        .ok_or_else(|| anyhow!("lf stop needs an initialized local store"))?;
    let local = store.local_home().await?;
    validate_expected_home(&local.id)?;
    let addressed_home = std::env::var_os(crate::lf::commands::ssh::EXPECTED_HOME_ID_ENV).is_some();
    let wave = store
        .get_wave_by_name(name)
        .await?
        .ok_or_else(|| anyhow!("Wave '{name}' was not found"))?;
    let placement = store
        .placement(&crate::durable::WorkRef::Wave(wave.id().clone()))
        .await?;
    if addressed_home && placement.home_id != local.id {
        return Err(anyhow!(
            "refusing remote stop for Wave '{name}': this Home places it on {}",
            placement.home_id
        ));
    }
    if placement.home_id == local.id {
        return Ok(StopTarget::Local);
    }
    Ok(StopTarget::Remote {
        home_id: placement.home_id,
        repo: resolve_home_relative_repo(&repo).map_err(anyhow::Error::msg)?,
    })
}

async fn start_inner(
    names: &[String],
    raw_wave_ids: &[String],
    repo: &Path,
) -> anyhow::Result<Vec<crate::lf::commands::waves::WaveSnapshot>> {
    let store = std::sync::Arc::new(
        crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("lf start needs an initialized local store"))?,
    );
    let local = store.local_home().await?;
    validate_expected_home(&local.id)?;
    let addressed_home = std::env::var_os(crate::lf::commands::ssh::EXPECTED_HOME_ID_ENV).is_some();
    let repo =
        crate::engine::worktrees::main_repo_root(repo).unwrap_or_else(|_| repo.to_path_buf());
    let wave_ids = parse_wave_ids(raw_wave_ids)?;
    let selected = if names.is_empty() {
        if !wave_ids.is_empty() {
            return Err(anyhow!("--wave-id requires explicit Wave names"));
        }
        store.list_waves(Some(&repo.display().to_string())).await?
    } else {
        let mut selected = Vec::with_capacity(names.len());
        for raw in names {
            let name = crate::ops::util::normalize_wave_name(raw)
                .ok_or_else(|| anyhow!("invalid wave name: '{raw}'"))?;
            let wave = match wave_ids.get(&name) {
                Some(id) => {
                    crate::wave::registry::ensure_wave_row_with_id(&store, &repo, &name, id).await?
                }
                None => crate::wave::registry::ensure_wave_row(&store, &repo, &name).await?,
            };
            selected.push(wave);
        }
        for name in wave_ids.keys() {
            if !selected.iter().any(|wave| wave.name() == name) {
                return Err(anyhow!("--wave-id names unselected Wave '{name}'"));
            }
        }
        selected
    };
    if selected.is_empty() {
        return Err(anyhow!(
            "no Waves found in {}; create one with `lf wave create <name>`",
            repo.display()
        ));
    }

    let mut groups: HashMap<crate::durable::HomeId, Vec<(crate::id::WaveId, String)>> =
        HashMap::new();
    for wave in selected {
        let placement = store
            .placement(&crate::durable::WorkRef::Wave(wave.id().clone()))
            .await?;
        if addressed_home && placement.home_id != local.id {
            return Err(anyhow!(
                "refusing remote start for Wave '{}': this Home places it on {}",
                wave.name(),
                placement.home_id
            ));
        }
        groups
            .entry(placement.home_id)
            .or_default()
            .push((wave.id().clone(), wave.name().to_string()));
    }
    let mut responses = Vec::with_capacity(groups.len());
    let mut groups = groups.into_iter().collect::<Vec<_>>();
    groups.sort_by(|(left, _), (right, _)| left.as_str().cmp(right.as_str()));
    for (home_id, waves) in groups {
        if home_id == local.id {
            let wave_ids = waves.iter().map(|(id, _)| id.clone()).collect();
            crate::home_resident::ensure(&home_id, &repo).await?;
            crate::home_resident::start_waves(&home_id, wave_ids).await?;
            for (_, name) in waves {
                let wave = store
                    .get_wave_by_name(&name)
                    .await?
                    .ok_or_else(|| anyhow!("Wave '{name}' disappeared after start"))?;
                responses.push(crate::lf::commands::waves::snapshot_wave(&store, &wave).await?);
            }
            continue;
        }
        let remote_repo = resolve_home_relative_repo(&repo).map_err(anyhow::Error::msg)?;
        let mut cmd = vec!["lf".to_string(), "start".to_string()];
        for (id, name) in waves {
            cmd.push(name.clone());
            cmd.push("--wave-id".to_string());
            cmd.push(format!("{name}={id}"));
        }
        cmd.push("--json".to_string());
        let remote_home = home_id.clone();
        let stdout = tokio::task::spawn_blocking(move || {
            crate::lf::commands::ssh::capture_remote_native(&remote_home, &remote_repo, &cmd)
        })
        .await
        .map_err(|error| anyhow!("remote Home start task failed: {error}"))?
        .map_err(|error| anyhow!("remote Home {home_id} start failed: {error}"))?;
        responses.extend(
            serde_json::from_str::<Vec<crate::lf::commands::waves::WaveSnapshot>>(stdout.trim())
                .map_err(|error| {
                    anyhow!("remote Home {home_id} returned invalid Wave status JSON: {error}")
                })?,
        );
    }
    Ok(responses)
}

fn parse_wave_ids(
    values: &[String],
) -> anyhow::Result<std::collections::BTreeMap<String, crate::id::WaveId>> {
    let mut bindings = std::collections::BTreeMap::new();
    for value in values {
        let (raw_name, raw_id) = value
            .split_once('=')
            .ok_or_else(|| anyhow!("invalid --wave-id {value:?}; expected NAME=ID"))?;
        let name = crate::ops::util::normalize_wave_name(raw_name)
            .ok_or_else(|| anyhow!("invalid Wave name in --wave-id {value:?}"))?;
        let id = crate::id::WaveId::parse(raw_id)
            .map_err(|error| anyhow!("invalid Wave id in --wave-id {value:?}: {error}"))?;
        if bindings.insert(name.clone(), id).is_some() {
            return Err(anyhow!("duplicate --wave-id binding for Wave '{name}'"));
        }
    }
    Ok(bindings)
}

fn validate_expected_home(local: &crate::durable::HomeId) -> anyhow::Result<()> {
    let Ok(raw) = std::env::var(crate::lf::commands::ssh::EXPECTED_HOME_ID_ENV) else {
        return Ok(());
    };
    let expected = crate::durable::HomeId::parse(&raw)
        .map_err(|error| anyhow!("invalid expected Home id: {error}"))?;
    if expected != *local {
        return Err(anyhow!(
            "refusing remote lifecycle command: expected Home {expected}, local Home is {local}"
        ));
    }
    Ok(())
}

fn print_runtime(name: &str, runtime: &HomeRuntimeDto) {
    let state = match runtime.state {
        HomeState::Unreachable => "unreachable",
        HomeState::Stopped => "stopped",
        HomeState::Running => "running",
        HomeState::Unknown => "unknown",
    };
    let action = match &runtime.action {
        HomeActionDto::Attach { endpoint } => format!("Attach ({endpoint})"),
        HomeActionDto::Start { home_id } => format!("Start on {home_id}"),
        HomeActionDto::Reason { message } => message.clone(),
    };
    println!(
        "{name}  {} ({})  [{state}]",
        runtime.home.id, runtime.home.route
    );
    println!("  reason  {}", runtime.reason);
    println!("  action  {action}");
}

/// Decide whether `command` runs on a remote home and, if so, run it there.
///
/// Returns `Some(result)` when the command was handled remotely (the caller
/// must not also run it locally), or `None` to fall through to local dispatch.
pub fn route(
    command: &Commands,
    wave: Option<&str>,
    account_selection: &AccountSelection,
    args: &[String],
) -> Option<anyhow::Result<()>> {
    if !is_routable(command) {
        return None;
    }
    // We are already on the home host after a forward: run locally, never loop.
    if std::env::var_os(HOME_ROUTED_ENV).is_some() {
        return None;
    }
    let name = wave.map(str::to_string).or_else(resolve_run_wave_name)?;
    let (home, wave_repo) = match resolve_home(&name) {
        Ok(resolved) => resolved,
        Err(error) => return Some(Err(error)),
    };
    if home.route == "local" {
        return None;
    }
    let remote_repo = match resolve_home_relative_repo(Path::new(&wave_repo)) {
        Ok(repo) => repo,
        Err(error) => return Some(Err(anyhow!(error))),
    };
    let route = match HomeRoute::parse(&home.route).filter(HomeRoute::is_remote) {
        Some(route) => route,
        None => {
            return Some(Err(anyhow!(
                "Home {} has invalid remote route {:?}",
                home.id,
                home.route
            )))
        }
    };
    let dest = route
        .ssh_destination()
        .expect("a remote Home route has an SSH destination");
    Some(crate::lf::commands::ssh::run_routed(
        &home.id,
        &dest,
        route.ssh_port(),
        Some(&remote_repo),
        account_selection,
        &remote_argv(args),
    ))
}

pub fn validate_expected_home_process() -> anyhow::Result<()> {
    if std::env::var_os(crate::lf::commands::ssh::EXPECTED_HOME_ID_ENV).is_none() {
        return Ok(());
    }
    let runtime = tokio::runtime::Runtime::new()?;
    let local = runtime.block_on(async {
        crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("Home-addressed command needs an initialized local store"))?
            .local_home()
            .await
            .map_err(anyhow::Error::from)
    })?;
    validate_expected_home(&local.id)
}

/// The repo/PR/release/PM operations that must run where the Wave's work lives.
/// Deliberately minimal — everything else stays local until a concrete need
/// grows the set.
fn is_routable(command: &Commands) -> bool {
    matches!(
        command,
        Commands::Pr { .. }
            | Commands::Commit { .. }
            | Commands::Rebase { .. }
            | Commands::Release { .. }
            | Commands::Pm { .. }
    )
}

fn resolve_home(name: &str) -> anyhow::Result<(crate::durable::Home, String)> {
    let runtime = tokio::runtime::Runtime::new()?;
    runtime.block_on(async {
        let store = crate::store::open_existing_store()
            .await
            .ok_or_else(|| anyhow!("Home routing needs an initialized local store"))?;
        let wave = store
            .get_wave_by_name(name)
            .await?
            .ok_or_else(|| anyhow!("Wave '{name}' was not found"))?;
        let placement = store
            .placement(&crate::durable::WorkRef::Wave(wave.id().clone()))
            .await?;
        let home = store
            .home_by_id(&placement.home_id)
            .await?
            .ok_or_else(|| anyhow!("Home {} was not found", placement.home_id))?;
        Ok((home, wave.repo().to_string()))
    })
}

/// Rebuild the invocation as an `lf` command for the remote shell: the local
/// `argv[0]` is an absolute path to this machine's binary, so replace it with
/// bare `lf` (resolved against the remote PATH). Account-selection flags are
/// consumed at this origin boundary: the remote invocation inherits the fixed
/// lease handle and must not try to resolve the selectors again.
fn remote_argv(args: &[String]) -> Vec<String> {
    let mut cmd = Vec::with_capacity(args.len());
    cmd.push("lf".to_string());
    let mut args = args.iter().skip(1);
    while let Some(arg) = args.next() {
        if arg == "--" {
            cmd.push(arg.clone());
            cmd.extend(args.cloned());
            break;
        }
        if matches!(arg.as_str(), "--account" | "--only-account") {
            let _ = args.next();
            continue;
        }
        if arg.starts_with("--account=") || arg.starts_with("--only-account=") {
            continue;
        }
        cmd.push(arg.clone());
    }
    cmd
}

#[cfg(test)]
mod tests {
    use super::{is_routable, remote_argv};
    use crate::lf::Commands;

    #[test]
    fn remote_argv_swaps_the_binary_path_for_bare_lf() {
        let args = vec![
            "/Users/jack/src/loopflow/target/debug/lf".to_string(),
            "pr".to_string(),
            "open".to_string(),
        ];
        assert_eq!(remote_argv(&args), vec!["lf", "pr", "open"]);
    }

    #[test]
    fn remote_argv_consumes_account_selectors_at_the_origin_boundary() {
        let args = vec![
            "/local/lf",
            "--account",
            "claude=personal",
            "--only-account=codex=reserve",
            "commit",
            "-m",
            "ship it",
            "--",
            "--account",
            "literal",
        ]
        .into_iter()
        .map(str::to_string)
        .collect::<Vec<_>>();

        assert_eq!(
            remote_argv(&args),
            vec![
                "lf",
                "commit",
                "-m",
                "ship it",
                "--",
                "--account",
                "literal"
            ]
        );
    }

    #[test]
    fn routable_set_is_repo_and_release_ops_only() {
        assert!(is_routable(&Commands::Commit {
            message: None,
            push: false,
            no_add: false,
        }));
        assert!(is_routable(&Commands::Pr { cmd: None }));
        assert!(!is_routable(&Commands::Status {
            wave: None,
            json: false,
        }));
        assert!(!is_routable(&Commands::Stop {
            name: "infra".to_string(),
        }));
    }
}