porch 0.2.1

Local git gate: push to porch, review and certify before origin
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
use std::env;
use std::io::{self, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::Arc;

use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
use porch_gate::{
    EjectOptions, InitOptions, admit_push, eject, ensure_daemon, get_run, git_dir_from_env,
    health_check, init, install_service, list_runs, notify_push, porch_home, repo_id_for,
    run_daemon, service_status, start_service, stop_daemon, uninstall_service,
};
use porch_run::{
    AgentResponse, AgentRunOpts, PipelineExecutor, agent_respond, agent_run, agent_status,
    agent_sync, rerun,
};

mod doctor;
mod setup;
mod tui;

#[derive(Parser)]
#[command(name = "porch", version, about = "Local git gate")]
struct Cli {
    #[command(subcommand)]
    command: Option<Command>,
}

#[derive(Subcommand)]
enum Command {
    /// Install the porch remote, bare repo, and hooks in this working tree.
    Init {
        /// Run setup non-interactively when review is missing, then init.
        #[arg(long)]
        yes: bool,
        /// Skip first-run review setup entirely.
        #[arg(long)]
        skip_setup: bool,
        /// Optional intent reminder printed in next-steps (E17: empty skips).
        /// Authoritative intent is set on push via `PORCH_INTENT` or `porch agent run --intent`.
        #[arg(long)]
        intent: Option<String>,
    },
    /// Detect review engine, write `$PORCH_HOME/config.yaml` + wrapper.
    Setup {
        /// Detect, write, verify; print JSON (non-interactive).
        #[arg(long)]
        yes: bool,
        /// Re-check wrapper/config without rewriting.
        #[arg(long)]
        verify: bool,
        /// Force engine (`agent`, `ocr`, or `generic`).
        #[arg(long)]
        engine: Option<String>,
        /// Rewrite wrapper from current config.yaml.
        #[arg(long)]
        apply: bool,
        /// Also install the daemon as a login service (default: off / detached).
        #[arg(long)]
        install_daemon: bool,
    },
    /// Check PATH / home / daemon prerequisites for a push.
    Doctor,
    /// List recent runs for this repo (JSON array).
    Runs {
        /// Max rows (default 20).
        #[arg(long, default_value_t = 20)]
        limit: usize,
    },
    /// Daemon health + latest run summary for this repo.
    Status {
        /// Emit JSON instead of text.
        #[arg(long)]
        json: bool,
    },
    /// Attach the park TUI (or print a snapshot when not a TTY).
    Attach {
        #[arg(long)]
        run_id: Option<String>,
    },
    /// Remove the `porch` remote (and neutralize bare hooks).
    Eject {
        /// Also delete this repo's bare, worktrees, run artifacts, and DB row.
        /// Leaves other repos under `$PORCH_HOME` and global config untouched.
        #[arg(long)]
        purge: bool,
    },
    /// Enqueue a new run from a prior run's recorded tip (fresh worktree).
    Rerun {
        /// Prior run id. Default: latest run for the current branch.
        #[arg(long)]
        run_id: Option<String>,
    },
    /// Daemon process (hooks call these subcommands).
    Daemon {
        #[command(subcommand)]
        command: DaemonCommand,
    },
    /// Headless agent interface (JSON on stdout).
    Agent {
        #[command(subcommand)]
        command: AgentCommand,
    },
}

#[derive(Subcommand)]
enum DaemonCommand {
    /// Long-lived process: flock, socket, sqlite, run executor.
    Run,
    /// pre-receive: currently always allow.
    AdmitPush,
    /// post-receive: record a pending run and ask the daemon to start it.
    NotifyPush {
        /// Authoritative intent for enqueued runs (preferred over `PORCH_INTENT`).
        /// Empty skips intent (E17); does not fail.
        #[arg(long)]
        intent: Option<String>,
    },
    /// Write the OS service definition (launchd/systemd).
    Install,
    /// Stop if possible and remove the service definition.
    Uninstall,
    /// Start via OS manager, or detached `ensure_daemon` fallback.
    Start,
    /// Stop the daemon process (definition stays unless uninstall).
    Stop {
        /// Allow stop even with pending/running/parked runs.
        #[arg(long)]
        force: bool,
    },
    /// Print daemon / service status.
    Status {
        #[arg(long)]
        json: bool,
    },
}

#[derive(Subcommand)]
enum AgentCommand {
    /// Ensure daemon; attach or push; optional wait until park/terminal (JSON/JSONL).
    Run {
        /// Attach to this run (no push). Defaults: active branch run, else `git push porch`.
        #[arg(long)]
        run_id: Option<String>,
        /// Stream JSONL until parked, completed, failed, or cancelled.
        #[arg(long)]
        wait: bool,
        /// Max seconds to wait (only with `--wait`). Omit for no limit.
        #[arg(long)]
        timeout: Option<u64>,
        /// Authoritative intent when pushing (E17: empty skips). Not with `--run-id`.
        #[arg(long)]
        intent: Option<String>,
    },
    /// Print run status JSON (default: latest parked for this repo).
    Status {
        /// Run id (ULID). Defaults to latest parked run for the cwd repo.
        #[arg(long)]
        run_id: Option<String>,
    },
    /// Respond to a parked review: approve | skip | abort | fix.
    Respond {
        /// `approve`, `skip`, `abort`, or `fix`.
        response: String,
        /// Run id (ULID). Defaults to latest parked run for the cwd repo.
        #[arg(long)]
        run_id: Option<String>,
        /// Comma-separated finding ids (only with `fix`).
        #[arg(long)]
        findings: Option<String>,
        /// After one fix round, approve remaining findings (only with `fix`).
        #[arg(long)]
        yes: bool,
    },
    /// Custody / sync JSON: author branch vs pipeline HEAD (`--recover` optional).
    Sync {
        #[arg(long)]
        run_id: Option<String>,
        /// Fast-forward local branch from a recorded recovery tip when safe.
        #[arg(long)]
        recover: bool,
    },
}

fn main() -> ExitCode {
    match main_inner() {
        Ok(code) => code,
        Err(e) => {
            eprintln!("porch: {e:#}");
            ExitCode::from(1)
        }
    }
}

fn main_inner() -> Result<ExitCode> {
    let argv: Vec<String> = env::args().collect();
    // Fast path: do not attach a stderr logger that would steal daemon logs.
    if argv.len() == 3 && argv[1] == "daemon" && argv[2] == "run" {
        init_file_tracing();
        let home = porch_home();
        let executor: Arc<dyn porch_gate::RunExecutor> = Arc::new(PipelineExecutor);
        run_daemon(&home, &executor).context("daemon run")?;
        return Ok(ExitCode::SUCCESS);
    }

    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
        )
        .with_writer(io::stderr)
        .init();

    match Cli::parse().command {
        None => run_bare(),
        Some(Command::Init {
            yes,
            skip_setup,
            intent,
        }) => run_init(yes, skip_setup, intent.as_deref()),
        Some(Command::Setup {
            yes,
            verify,
            engine,
            apply,
            install_daemon,
        }) => {
            let home = porch_home();
            setup::run(
                &home,
                &setup::SetupArgs {
                    yes,
                    verify,
                    apply,
                    engine,
                    install_daemon,
                },
            )
        }
        Some(Command::Doctor) => Ok(doctor::run()?),
        Some(Command::Runs { limit }) => run_runs(limit),
        Some(Command::Status { json }) => run_status(json),
        Some(Command::Attach { run_id }) => run_attach_cmd(run_id.as_deref()),
        Some(Command::Eject { purge }) => run_eject(purge),
        Some(Command::Rerun { run_id }) => run_rerun(run_id.as_deref()),
        Some(Command::Daemon { command }) => run_daemon_command(&command),
        Some(Command::Agent {
            command:
                AgentCommand::Run {
                    run_id,
                    wait,
                    timeout,
                    intent,
                },
        }) => Ok(run_agent_run(
            run_id.as_deref(),
            wait,
            timeout,
            intent.as_deref(),
        )?),
        Some(Command::Agent {
            command: AgentCommand::Status { run_id },
        }) => {
            let home = porch_home();
            let work = env::current_dir()?;
            let result = agent_status(&home, run_id.as_deref(), &work);
            Ok(emit_agent(&result))
        }
        Some(Command::Agent {
            command:
                AgentCommand::Respond {
                    response,
                    run_id,
                    findings,
                    yes,
                },
        }) => Ok(run_agent_respond(
            &response,
            run_id.as_deref(),
            findings.as_deref(),
            yes,
        )?),
        Some(Command::Agent {
            command: AgentCommand::Sync { run_id, recover },
        }) => {
            let home = porch_home();
            let work = env::current_dir()?;
            Ok(emit_agent(&agent_sync(
                &home,
                &work,
                run_id.as_deref(),
                recover,
            )))
        }
    }
}

fn run_eject(purge: bool) -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        bail!("not a git work tree");
    }
    let home = porch_home();
    let result = eject(EjectOptions {
        work_tree: &work,
        porch_home: &home,
        purge,
    })?;
    println!(
        "ejected repo {} (bare was {})",
        result.repo_id,
        result.bare_path.display()
    );
    if result.purged {
        println!(
            "purged this repo's bare, worktrees, run artifacts, and DB row under {}",
            home.display()
        );
        println!("other repos under PORCH_HOME were not touched");
    } else {
        println!("PORCH_HOME left intact (use --purge to remove this repo's gate state)");
    }
    Ok(ExitCode::SUCCESS)
}

fn run_rerun(run_id: Option<&str>) -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        bail!("not a git work tree");
    }
    let home = porch_home();
    ensure_daemon_for_cwd(&home)?;
    let new_id = rerun(&home, &work, run_id).map_err(|e| anyhow::anyhow!(e))?;
    println!("rerun started: {new_id}");
    Ok(ExitCode::SUCCESS)
}

fn run_daemon_command(command: &DaemonCommand) -> Result<ExitCode> {
    match command {
        DaemonCommand::Run => {
            let home = porch_home();
            let executor: Arc<dyn porch_gate::RunExecutor> = Arc::new(PipelineExecutor);
            run_daemon(&home, &executor)?;
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::AdmitPush => {
            admit_push(io::stdin())?;
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::NotifyPush { intent } => {
            let home = porch_home();
            let git_dir = git_dir_from_env()?;
            let ids = notify_push(&home, &git_dir, io::stdin(), intent.as_deref())?;
            for id in ids {
                eprintln!("porch: recorded run {id}");
            }
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::Install => {
            let home = porch_home();
            let user_home = user_home_dir()?;
            let bin = env::current_exe().context("current_exe")?;
            let paths = install_service(&bin, &home, &user_home)?;
            println!("wrote {}", paths.definition_path.display());
            println!("label: {}", paths.label);
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::Uninstall => {
            let home = porch_home();
            let user_home = user_home_dir()?;
            let paths = uninstall_service(&home, &user_home)?;
            println!("removed {}", paths.definition_path.display());
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::Start => {
            let home = porch_home();
            let user_home = user_home_dir()?;
            let bin = env::current_exe().context("current_exe")?;
            let msg = start_service(&bin, &home, &user_home)?;
            println!("{msg}");
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::Stop { force } => {
            let home = porch_home();
            stop_daemon(&home, *force)?;
            println!("daemon stopped");
            Ok(ExitCode::SUCCESS)
        }
        DaemonCommand::Status { json } => {
            let home = porch_home();
            let user_home = user_home_dir()?;
            let st = service_status(&home, &user_home)?;
            if *json {
                println!("{}", serde_json::to_string_pretty(&st)?);
            } else {
                println!(
                    "running={} pid={:?} socket_healthy={} service={} exists={}",
                    st.running,
                    st.pid,
                    st.socket_healthy,
                    st.service_file.display(),
                    st.service_file_exists
                );
            }
            Ok(ExitCode::SUCCESS)
        }
    }
}

fn user_home_dir() -> Result<PathBuf> {
    env::var_os("HOME")
        .map(PathBuf::from)
        .context("HOME is unset")
}

fn is_git_work_tree(work: &Path) -> bool {
    std::process::Command::new("git")
        .args(["rev-parse", "--is-inside-work-tree"])
        .current_dir(work)
        .output()
        .ok()
        .is_some_and(|o| o.status.success() && String::from_utf8_lossy(&o.stdout).trim() == "true")
}

fn ensure_daemon_for_cwd(home: &Path) -> Result<()> {
    let bin = env::current_exe().context("current_exe")?;
    ensure_daemon(&bin, home).context("ensure daemon (try: porch daemon start)")
}

fn run_init(yes: bool, skip_setup: bool, intent: Option<&str>) -> Result<ExitCode> {
    let work = env::current_dir()?;
    let home = porch_home();
    if !skip_setup {
        let review_missing = setup::setup_incomplete(&home);
        if review_missing {
            if yes {
                let result = porch_review::setup_yes(&home, None)?;
                if !result.ok {
                    eprintln!(
                        "porch: setup failed: {}",
                        result.error.as_deref().unwrap_or("unknown")
                    );
                    println!("{}", serde_json::to_string_pretty(&result)?);
                    return Ok(ExitCode::from(1));
                }
            } else if io::stdin().is_terminal() {
                eprintln!(
                    "porch: review setup incomplete — run `porch setup` (or `porch init --yes`)"
                );
            } else {
                eprintln!(
                    "porch: review setup incomplete — run `porch setup --yes` or `porch init --skip-setup`"
                );
            }
        }
    }
    let bin = env::current_exe().context("current_exe")?;
    let result = init(InitOptions {
        work_tree: &work,
        porch_home: &home,
        porch_bin: &bin,
        start_daemon: true,
    })?;
    print_init_next_steps(&result, &work, &home, intent);
    Ok(ExitCode::SUCCESS)
}

fn run_bare() -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        eprintln!("porch: not a git work tree; run from a git repo after `porch init`");
        return Ok(ExitCode::from(1));
    }
    let home = porch_home();
    ensure_daemon_for_cwd(&home)?;

    let repo_id = repo_id_for(&work);
    let branch = doctor::current_branch(&work);
    let runs = list_runs(&home, Some(&repo_id), Some(20)).unwrap_or_default();
    let active = runs.iter().find(|r| {
        let status = r.get("status").and_then(|v| v.as_str()).unwrap_or("");
        let b = r.get("branch").and_then(|v| v.as_str()).unwrap_or("");
        b == branch && matches!(status, "pending" | "running" | "parked")
    });

    if io::stdin().is_terminal() {
        if let Some(run) = active {
            let run_id = run.get("id").and_then(|v| v.as_str()).unwrap_or("");
            if !run_id.is_empty() {
                tui::run_attach(&home, &work, run_id)?;
                return Ok(ExitCode::SUCCESS);
            }
        }
        if setup::setup_incomplete(&home) {
            return setup::run(
                &home,
                &setup::SetupArgs {
                    yes: false,
                    verify: false,
                    apply: false,
                    engine: None,
                    install_daemon: false,
                },
            );
        }
    }

    print_runs_summary(&runs);
    if setup::setup_incomplete(&home) {
        println!("hint: review setup incomplete — run `porch setup --yes`");
    }
    Ok(ExitCode::SUCCESS)
}

fn print_runs_summary(runs: &[serde_json::Value]) {
    if runs.is_empty() {
        println!("no runs yet");
    } else {
        for r in runs.iter().take(10) {
            let id = r.get("id").and_then(|v| v.as_str()).unwrap_or("?");
            let branch = r.get("branch").and_then(|v| v.as_str()).unwrap_or("?");
            let status = r.get("status").and_then(|v| v.as_str()).unwrap_or("?");
            let sha = r.get("sha").and_then(|v| v.as_str()).unwrap_or("");
            let sha_pfx: String = sha.chars().take(8).collect();
            println!("{id}  {branch}  {status}  {sha_pfx}");
        }
    }
    println!("hint: git push porch   or   porch attach --run-id <id>");
}

fn run_runs(limit: usize) -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        bail!("not a git work tree");
    }
    let home = porch_home();
    ensure_daemon_for_cwd(&home)?;
    let repo_id = repo_id_for(&work);
    let runs = list_runs(&home, Some(&repo_id), Some(limit))?;
    println!("{}", serde_json::to_string_pretty(&runs)?);
    Ok(ExitCode::SUCCESS)
}

fn run_status(json: bool) -> Result<ExitCode> {
    let work = env::current_dir()?;
    let home = porch_home();
    let healthy = health_check(&home).unwrap_or(false);
    let mut latest: Option<serde_json::Value> = None;
    if is_git_work_tree(&work) {
        let _ = ensure_daemon_for_cwd(&home);
        let repo_id = repo_id_for(&work);
        if let Ok(runs) = list_runs(&home, Some(&repo_id), Some(1)) {
            latest = runs.into_iter().next();
        }
    }
    if json {
        println!(
            "{}",
            serde_json::json!({
                "daemon_healthy": healthy,
                "porch_home": home,
                "latest_run": latest,
            })
        );
    } else {
        println!("daemon_healthy={healthy}");
        println!("PORCH_HOME={}", home.display());
        match latest {
            Some(r) => {
                println!(
                    "latest: {} {} {}",
                    r.get("id").and_then(|v| v.as_str()).unwrap_or("?"),
                    r.get("branch").and_then(|v| v.as_str()).unwrap_or("?"),
                    r.get("status").and_then(|v| v.as_str()).unwrap_or("?"),
                );
            }
            None => println!("latest: (none)"),
        }
    }
    Ok(ExitCode::SUCCESS)
}

fn run_attach_cmd(run_id: Option<&str>) -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        bail!("not a git work tree");
    }
    let home = porch_home();
    ensure_daemon_for_cwd(&home)?;
    let repo_id = repo_id_for(&work);
    let id = if let Some(id) = run_id {
        id.to_string()
    } else {
        let runs = list_runs(&home, Some(&repo_id), Some(20))?;
        let branch = doctor::current_branch(&work);
        runs.iter()
            .find(|r| {
                let status = r.get("status").and_then(|v| v.as_str()).unwrap_or("");
                let b = r.get("branch").and_then(|v| v.as_str()).unwrap_or("");
                b == branch && matches!(status, "pending" | "running" | "parked")
            })
            .or_else(|| runs.first())
            .and_then(|r| r.get("id").and_then(|v| v.as_str()).map(str::to_string))
            .ok_or_else(|| anyhow::anyhow!("no active run"))?
    };

    if !io::stdin().is_terminal() {
        if let Ok(snap) = get_run(&home, &id) {
            println!(
                "run {}  branch {}  status {}",
                snap.run_id, snap.branch, snap.status
            );
            println!("{}", serde_json::to_string_pretty(&snap)?);
        } else {
            println!("no active run");
            let runs = list_runs(&home, Some(&repo_id), Some(5)).unwrap_or_default();
            print_runs_summary(&runs);
        }
        return Ok(ExitCode::SUCCESS);
    }

    tui::run_attach(&home, &work, &id)?;
    Ok(ExitCode::SUCCESS)
}

fn print_init_next_steps(
    result: &porch_gate::InitResult,
    work: &std::path::Path,
    home: &std::path::Path,
    intent: Option<&str>,
) {
    let branch = doctor::current_branch(work);
    println!("porch remote -> {}", result.bare_path.display());
    println!("repo id: {}", result.repo_id);
    println!("default branch: {}", result.default_branch);
    println!("PORCH_HOME: {}", home.display());
    for path in &result.skills.written {
        println!("skill: wrote {}", path.display());
    }
    for path in &result.skills.skipped_identical {
        println!("skill: unchanged {}", path.display());
    }
    for w in &result.skills.warnings {
        eprintln!("porch: {w}");
    }
    println!("next: git push porch HEAD:refs/heads/{branch}");
    println!("or:   porch agent run --wait");
    if let Some(text) = intent.map(str::trim).filter(|s| !s.is_empty()) {
        println!("intent tip: porch agent run --intent {text:?} --wait");
        println!("       or: PORCH_INTENT={text:?} git push porch");
    }

    // Agent engine has no `review` wrapper — use setup-aware check (same as doctor).
    let missing_review = !porch_review::review_setup_ok(home);
    let gh_bin = env::var("PORCH_GH_BIN").unwrap_or_else(|_| "gh".into());
    let missing_gh = !doctor::bin_on_path(&gh_bin);
    if missing_review || missing_gh {
        println!(
            "tip: run `porch setup` / `porch doctor` — review and/or gh look missing for a complete run"
        );
    }
}

fn run_agent_run(
    run_id: Option<&str>,
    wait: bool,
    timeout: Option<u64>,
    intent: Option<&str>,
) -> Result<ExitCode> {
    let work = env::current_dir()?;
    if !is_git_work_tree(&work) {
        let _ = writeln!(
            io::stdout(),
            "{}",
            serde_json::json!({"error": "not a git work tree", "code": "usage"})
        );
        return Ok(ExitCode::from(2));
    }
    let home = porch_home();
    ensure_daemon_for_cwd(&home)?;
    let result = agent_run(AgentRunOpts {
        home: &home,
        work_tree: &work,
        run_id,
        wait,
        timeout_secs: timeout,
        intent,
    });
    Ok(emit_agent(&result))
}

fn run_agent_respond(
    response: &str,
    run_id: Option<&str>,
    findings: Option<&str>,
    yes: bool,
) -> Result<ExitCode> {
    if (findings.is_some() || yes) && response != "fix" {
        let _ = writeln!(
            io::stdout(),
            "{}",
            serde_json::json!({
                "error": "--findings and --yes are only valid with fix",
                "code": "usage"
            })
        );
        return Ok(ExitCode::from(2));
    }
    let parsed = match parse_agent_response(response, findings, yes) {
        Ok(r) => r,
        Err(msg) => {
            let _ = writeln!(
                io::stdout(),
                "{}",
                serde_json::json!({"error": msg, "code": "usage"})
            );
            return Ok(ExitCode::from(2));
        }
    };
    let home = porch_home();
    let work = env::current_dir()?;
    Ok(emit_agent(&agent_respond(&home, run_id, &work, parsed)))
}

fn parse_agent_response(
    response: &str,
    findings: Option<&str>,
    yes: bool,
) -> std::result::Result<AgentResponse, String> {
    match response {
        "approve" => Ok(AgentResponse::Approve),
        "skip" => Ok(AgentResponse::Skip),
        "abort" => Ok(AgentResponse::Abort),
        "fix" => {
            let finding_ids = findings.map(|s| {
                s.split(',')
                    .map(str::trim)
                    .filter(|p| !p.is_empty())
                    .map(str::to_string)
                    .collect::<Vec<_>>()
            });
            Ok(AgentResponse::Fix { finding_ids, yes })
        }
        other => Err(format!(
            "unknown response {other:?}; expected approve|skip|abort|fix"
        )),
    }
}

fn emit_agent(result: &porch_run::AgentCliResult) -> ExitCode {
    if !result.already_emitted {
        println!("{}", result.json);
    }
    ExitCode::from(u8::try_from(result.exit_code).unwrap_or(1))
}

fn init_file_tracing() {
    let home = porch_home();
    let logs = home.join("logs");
    let _ = std::fs::create_dir_all(&logs);
    let file = std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(logs.join("daemon.log"));
    match file {
        Ok(f) => {
            tracing_subscriber::fmt()
                .with_writer(std::sync::Mutex::new(f))
                .with_env_filter("info")
                .init();
        }
        Err(_) => {
            tracing_subscriber::fmt().with_writer(io::stderr).init();
        }
    }
}