Skip to main content

locode_exec/
run.rs

1//! Session assembly + drive (plan §3.2): resolve inputs, build host/pack/
2//! provider through the facade, run the engine, emit per `--output-format`.
3
4use std::io::Read;
5use std::process::ExitCode;
6use std::sync::Arc;
7
8use locode_core::{
9    CacheHint, EngineConfig, EventSink, FnSink, Host, HostConfig, InstructionsConfig, PackContext,
10    PathPolicy, ProviderInit, ProviderRegistry, SamplingArgs, Session, SkillsConfig,
11};
12
13use crate::cli::{Cli, OutputFormat};
14use crate::output;
15
16/// A pre-run failure (config/setup — no report exists yet): stderr + exit 1.
17pub struct PreRunError(pub String);
18
19impl<E: std::fmt::Display> From<E> for PreRunError {
20    fn from(e: E) -> Self {
21        PreRunError(e.to_string())
22    }
23}
24
25/// Build and drive one session; returns the process exit code.
26///
27/// Every terminal state of a *started* run yields a report (the engine's
28/// `run()` is infallible) — only pre-run setup can fail here.
29///
30/// # Errors
31/// [`PreRunError`] on config/setup failures before a run exists (bad `--cwd`,
32/// unknown/misconfigured provider, empty prompt): stderr + exit 1, nothing on
33/// stdout.
34pub async fn run(cli: Cli, providers: &ProviderRegistry) -> Result<ExitCode, PreRunError> {
35    // ---- 0. SIGTERM handler (ADR-0018, Task 21): installed before any
36    //         pre-run work so a pre-run SIGTERM exits 1 cleanly; armed with
37    //         the session's cancel handle once one exists. ----
38    #[cfg(unix)]
39    let cancel_slot = crate::signal::install_sigterm();
40
41    // ---- 1. Prompt: positional, or stdin when absent / `-`. ----
42    let prompt = resolve_prompt(cli.prompt.as_deref())?;
43
44    // ---- 2. Workspace root: canonicalize FIRST, then hand the SAME canonical
45    //         path to the host (jail root), the engine (cwd), and the pack
46    //         (prompt context) — they must agree (STATUS concern #7). ----
47    let cwd = match &cli.cwd {
48        Some(dir) => dir.clone(),
49        None => std::env::current_dir()?,
50    };
51    let cwd = std::fs::canonicalize(&cwd)
52        .map_err(|e| PreRunError(format!("--cwd {}: {e}", cwd.display())))?;
53
54    let mut host_config = HostConfig::new(&cwd);
55    // Unrestricted is the default (ADR-0008 amendment 2026-07-24): the jail and the
56    // approval seam both ship, but the permission *rules* behind them do not, so the
57    // restricted path cannot remember an answer. `--restricted` opts back in.
58    if !cli.restricted {
59        host_config.path_policy = PathPolicy::Unrestricted;
60    }
61    let host = Arc::new(Host::new(host_config)?);
62    output::warning_line(if cli.restricted {
63        output::RESTRICTED_MODE_NOTICE
64    } else {
65        output::UNRESTRICTED_MODE_NOTICE
66    });
67
68    // ---- 2b. Settings (ADR-0024): the durable defaults *under* the flags —
69    //          an explicit flag/env always wins. Layer degradations surface as
70    //          stderr warnings, never hard errors. ----
71    let settings_load = locode_core::load_settings(&cwd, cli.settings.as_deref());
72    for warning in &settings_load.warnings {
73        output::warning_line(warning);
74    }
75    let settings = settings_load.settings;
76    // The resolved `extends` dotfolders travel with the settings — instruction and
77    // (later) skill discovery are consumers of this value, which is what keeps the
78    // load order an invariant (ADR-0025 §6.1).
79    let extends_dirs = settings_load.extends_dirs;
80
81    // ---- 2c. Resume target (`-c`/`-r`, ADR-0024 §2.5): recover the transcript
82    //          and the run identity (pack/wire/model/id) from the rollout header;
83    //          an explicit conflicting flag errors rather than silently swapping
84    //          a session's pack or wire mid-transcript. ----
85    let identity = resolve_identity(&cli, &cwd, &settings)?;
86
87    // ---- 3. Provider: registry-resolved (ADR-0015); unknown names and factory
88    //         failures (missing env, …) fail BEFORE driving the loop. Built first
89    //         so the pack env block can name the model (D9). ----
90    let pack = locode_core::resolve(&identity.harness)?;
91    let registry = pack.build_registry(&host);
92    let session_id = identity.session_id.clone();
93    let built = providers
94        .build(
95            &identity.api_schema,
96            &ProviderInit {
97                session_id: session_id.clone(),
98                model: identity.model_override.clone(),
99            },
100        )
101        .map_err(|e| PreRunError(e.to_string()))?;
102    let (provider, model) = (built.provider, built.model);
103
104    // ---- 3b. Wire requirement: a pack whose tools only round-trip on a specific
105    //          wire (codex's freeform `apply_patch` → openai-responses, D5) rejects
106    //          a mismatched `--api-schema` here, before the loop. ----
107    enforce_wire_requirement(pack, provider.api_schema())?;
108
109    // ---- 4. Pack: preamble (system prompt + env + first-turn reminder). ----
110    let pack_ctx = PackContext {
111        cwd: cwd.clone(),
112        os: std::env::consts::OS.to_string(),
113        shell: std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_string()),
114        date: chrono::Local::now().format("%Y-%m-%d").to_string(),
115        headless: true,
116        is_git_repo: detect_git_repo(&cwd),
117        model: Some(model.clone()),
118        os_version: os_version(),
119        timezone: timezone(),
120        strip_identity: cli.strip_identity,
121    };
122    // A resumed session's preamble IS the recovered history (the pack preamble
123    // is already inside it — it was traced); Init then carries the full
124    // transcript, keeping the stream self-sufficient with zero engine changes.
125    let preamble = match &identity.resumed {
126        Some(resumed) => resumed.history.clone(),
127        None => pack.preamble(&pack_ctx),
128    };
129
130    // Pack-specific user-prompt shaping (grok wraps in <user_query>; claude sends
131    // it verbatim). The pack owns the shape — the exec layer stays harness-agnostic.
132    let user_prompt = pack.shape_user_prompt(&prompt);
133
134    // ---- 5. Engine config + event sink per output mode. ----
135    let config = EngineConfig {
136        session_id,
137        harness: pack.name().to_string(),
138        api_schema: provider.api_schema().to_string(),
139        model,
140        cwd: cwd.clone(),
141        workspace_root: cwd,
142        max_turns: cli.max_turns,
143        sampling_args: SamplingArgs::default(),
144        cache_hint: CacheHint::Standard,
145        // Opt-in headless streaming (`--stream`) — required for unbounded output
146        // (Anthropic rejects non-streaming past ~10 min). Off by default keeps
147        // `-p` byte-for-byte as it was (ADR-0021).
148        streaming: cli.stream,
149        // Project-instruction loading (`AGENTS.md`, ADR-0023) — on by default,
150        // `--no-project-instructions` opts out. `root_stop_pattern` threads
151        // through from settings (ADR-0024 §1.4; matching activates in S2).
152        instructions: InstructionsConfig {
153            enabled: !cli.no_project_instructions,
154            root_stop_pattern: settings.root_stop_pattern.clone(),
155            extends_dirs: extends_dirs.clone(),
156            ..InstructionsConfig::default()
157        },
158        // Skills (ADR-0025): the same resolved settings feed discovery, which is what
159        // keeps "settings before discovery" an invariant rather than a convention.
160        skills: SkillsConfig {
161            extends_dirs,
162            extra: settings.skills_extra.clone(),
163            ..SkillsConfig::enabled()
164        },
165        ..EngineConfig::default()
166    };
167    // ---- 5b. Session trace (ADR-0024 §2): every run appends a rollout under
168    //          `<locode home>/sessions/<encoded-cwd>/`. See `build_trace_writer`.
169    let mut trace = build_trace_writer(&cli, &identity, &config.cwd);
170
171    let sink = make_sink(cli.output_format, trace.take());
172
173    // ---- 6. Drive to a terminal state (infallible) and emit the artifact. ----
174    let mut session = Session::new(provider, registry, preamble, config, sink);
175    #[cfg(unix)]
176    crate::signal::arm(&cancel_slot, session.cancel_handle());
177    let report = session.run_text(user_prompt).await;
178
179    match cli.output_format {
180        OutputFormat::Json => output::write_json_line(&report),
181        OutputFormat::Text => output::write_text(report.final_message.as_deref().unwrap_or("")),
182        OutputFormat::StreamJson => {} // the result event already streamed
183    }
184    Ok(output::exit_code(report.status))
185}
186
187/// A recovered session (`-c`/`-r`): its rollout path + replayable history.
188struct ResumedSession {
189    path: std::path::PathBuf,
190    history: Vec<locode_core::Message>,
191}
192
193/// The run identity: pack/wire/model/session-id, from flags, settings, and (for
194/// `-c`/`-r`) the rollout header — which wins over settings but loses to an
195/// explicit flag only by *erroring* (no silent pack/wire swap mid-transcript).
196struct RunIdentity {
197    harness: String,
198    api_schema: String,
199    model_override: Option<String>,
200    session_id: String,
201    resumed: Option<ResumedSession>,
202}
203
204fn resolve_identity(
205    cli: &Cli,
206    cwd: &std::path::Path,
207    settings: &locode_core::Settings,
208) -> Result<RunIdentity, PreRunError> {
209    // Locate + read the rollout when resuming.
210    let recovered = if cli.continue_session || cli.resume.is_some() {
211        let home = locode_core::locode_home().map_err(PreRunError)?;
212        let root = home.join("sessions");
213        let path = if let Some(id) = &cli.resume {
214            locode_core::find_rollout_by_id(&root, cwd, id)
215                .ok_or_else(|| PreRunError(format!("--resume: no session `{id}` found")))?
216        } else {
217            locode_core::find_latest_rollout(&root, cwd).ok_or_else(|| {
218                PreRunError(format!(
219                    "--continue: no session found for {}",
220                    cwd.display()
221                ))
222            })?
223        };
224        let contents = locode_core::read_rollout(&path).map_err(PreRunError)?;
225        Some((path, contents))
226    } else {
227        None
228    };
229
230    if let Some((path, contents)) = recovered {
231        let meta = contents.meta;
232        // Explicit flags may confirm the recorded identity, never change it.
233        if let Some(flag) = cli.harness
234            && flag.as_str() != meta.harness
235        {
236            return Err(PreRunError(format!(
237                "--harness {} conflicts with the resumed session's harness `{}`",
238                flag.as_str(),
239                meta.harness
240            )));
241        }
242        if let Some(flag) = &cli.api_schema
243            && flag != &meta.api_schema
244        {
245            return Err(PreRunError(format!(
246                "--api-schema {flag} conflicts with the resumed session's wire `{}` \
247                 (a session never crosses wires)",
248                meta.api_schema
249            )));
250        }
251        return Ok(RunIdentity {
252            harness: meta.harness.clone(),
253            api_schema: meta.api_schema.clone(),
254            // The model is deliberately NOT recovered from the header (user
255            // decision 2026-07-24): resume resolves it exactly like a fresh
256            // run — flag > settings > the wire's default. Pack/wire stay
257            // header-bound because they affect transcript validity; the model
258            // doesn't, and yesterday's model should not leak into today.
259            model_override: cli.model.clone().or_else(|| settings.model.clone()),
260            session_id: meta.session_id.clone(),
261            resumed: Some(ResumedSession {
262                path,
263                history: contents.history,
264            }),
265        });
266    }
267
268    Ok(RunIdentity {
269        harness: match cli.harness {
270            Some(harness) => harness.as_str().to_string(),
271            None => settings
272                .harness
273                .clone()
274                .unwrap_or_else(|| "claude".to_string()),
275        },
276        api_schema: cli
277            .api_schema
278            .clone()
279            .or_else(|| settings.api_schema.clone())
280            .unwrap_or_else(|| "anthropic".to_string()),
281        model_override: cli.model.clone().or_else(|| settings.model.clone()),
282        session_id: new_session_id(),
283        resumed: None,
284    })
285}
286
287/// The session-trace writer (ADR-0024 §2): decoration over the event sink, so
288/// tracing needs zero engine changes. A resumed run reopens the same rollout for
289/// appending; a fresh run creates a new one. `None` — no rollout written — when
290/// there is no `~/.locode` home, `--no-session-persistence` is set, or a resume
291/// reopen fails (warned, never fatal).
292fn build_trace_writer(
293    cli: &Cli,
294    identity: &RunIdentity,
295    cwd: &std::path::Path,
296) -> Option<locode_core::TraceWriter> {
297    let root = locode_core::locode_home()
298        .ok()
299        .filter(|_| !cli.no_session_persistence)?
300        .join("sessions");
301    match &identity.resumed {
302        // Reopen the same rollout for appending (id and file continue).
303        Some(resumed) => locode_core::TraceWriter::resume(resumed.path.clone(), root)
304            .map_err(|e| output::warning_line(&format!("trace: {e}; tracing disabled")))
305            .ok(),
306        None => Some(locode_core::TraceWriter::new(
307            root,
308            locode_core::TraceExtras {
309                cli_version: env!("CARGO_PKG_VERSION").to_string(),
310                git: git_meta(cwd),
311                ..Default::default()
312            },
313        )),
314    }
315}
316
317/// The event sink for `output_format`, decorated with the session-trace writer
318/// (ADR-0024 §2): every event feeds the trace first; a trace failure warns once
319/// and disables tracing, never the run. `stream-json` additionally writes the
320/// whole-message JSONL to stdout; `json`/`text` emit nothing per-event.
321fn make_sink(
322    output_format: OutputFormat,
323    mut trace: Option<locode_core::TraceWriter>,
324) -> Box<dyn EventSink> {
325    let stream = matches!(output_format, OutputFormat::StreamJson);
326    Box::new(FnSink(move |event| {
327        if let Some(writer) = trace.as_mut() {
328            writer.on_event(&event);
329            if let Some(e) = writer.take_error() {
330                output::warning_line(&format!("trace: {e}; tracing disabled"));
331            }
332        }
333        if stream && in_whole_message_trace(&event) {
334            output::write_json_line(&event);
335        }
336    }))
337}
338
339/// Reject a `--api-schema` a pack's tools can't round-trip on (codex's freeform
340/// `apply_patch` requires the OpenAI Responses wire, D5). `mock` (keyless CI) is
341/// the universal escape hatch and is always allowed, independent of the pack list.
342fn enforce_wire_requirement(pack: &dyn locode_core::Pack, schema: &str) -> Result<(), PreRunError> {
343    if schema != "mock"
344        && let Some(required) = pack.required_api_schemas()
345        && !required.contains(&schema)
346    {
347        return Err(PreRunError(format!(
348            "harness `{}` requires one of these wires: {}; got `--api-schema {}`",
349            pack.name(),
350            required.join(", "),
351            schema,
352        )));
353    }
354    Ok(())
355}
356
357/// Positional prompt, or stdin when absent / `-` (Codex's convention;
358/// positional XOR stdin per the plan addendum). Empty → usage error.
359fn resolve_prompt(arg: Option<&str>) -> Result<String, PreRunError> {
360    let prompt = match arg {
361        Some("-") | None => {
362            let mut buf = String::new();
363            std::io::stdin().read_to_string(&mut buf)?;
364            buf
365        }
366        Some(text) => text.to_string(),
367    };
368    let prompt = prompt.trim().to_string();
369    if prompt.is_empty() {
370        return Err(PreRunError(
371            "no prompt: pass it as the positional argument or on stdin".to_string(),
372        ));
373    }
374    Ok(prompt)
375}
376
377/// Best-effort git provenance for the trace header (ADR-0024 §2.3) — one
378/// `git rev-parse` + one `remote get-url`, all fields optional; `None` when not
379/// in a repo at all.
380fn git_meta(cwd: &std::path::Path) -> Option<locode_core::GitMeta> {
381    if !detect_git_repo(cwd) {
382        return None;
383    }
384    let run = |args: &[&str]| -> Option<String> {
385        let out = std::process::Command::new("git")
386            .arg("-C")
387            .arg(cwd)
388            .args(args)
389            .output()
390            .ok()?;
391        if !out.status.success() {
392            return None;
393        }
394        let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
395        (!s.is_empty()).then_some(s)
396    };
397    Some(locode_core::GitMeta {
398        root: run(&["rev-parse", "--show-toplevel"]).map(std::path::PathBuf::from),
399        branch: run(&["rev-parse", "--abbrev-ref", "HEAD"]),
400        head: run(&["rev-parse", "HEAD"]),
401        remote: run(&["remote", "get-url", "origin"]),
402    })
403}
404
405/// Whether `cwd` is inside a git repository — walk up looking for a `.git` entry
406/// (a cheap probe for the Claude pack's env `Is a git repository:` line, D9; no
407/// host handle needed in `preamble()`).
408fn detect_git_repo(cwd: &std::path::Path) -> bool {
409    cwd.ancestors().any(|dir| dir.join(".git").exists())
410}
411
412/// `uname -s -r` for the Claude pack's env `OS Version:` line; `None` off Unix or
413/// if the probe fails.
414fn os_version() -> Option<String> {
415    #[cfg(unix)]
416    {
417        let out = std::process::Command::new("uname")
418            .args(["-s", "-r"])
419            .output()
420            .ok()?;
421        if !out.status.success() {
422            return None;
423        }
424        let s = String::from_utf8_lossy(&out.stdout).trim().to_string();
425        (!s.is_empty()).then_some(s)
426    }
427    #[cfg(not(unix))]
428    {
429        None
430    }
431}
432
433/// Best-effort IANA timezone name for the codex pack's `<environment_context>`
434/// (`America/Los_Angeles`), dependency-free: `$TZ` if set, else the `/etc/localtime`
435/// symlink target after `zoneinfo/`. `None` when neither resolves (the pack then
436/// omits the `<timezone>` line — codex's field is optional).
437fn timezone() -> Option<String> {
438    if let Ok(tz) = std::env::var("TZ") {
439        let tz = tz.trim();
440        if !tz.is_empty() {
441            return Some(tz.to_string());
442        }
443    }
444    #[cfg(unix)]
445    {
446        let target = std::fs::read_link("/etc/localtime").ok()?;
447        let s = target.to_string_lossy();
448        s.split_once("zoneinfo/")
449            .map(|(_, name)| name.to_string())
450            .filter(|name| !name.is_empty())
451    }
452    #[cfg(not(unix))]
453    {
454        None
455    }
456}
457
458/// A unique-enough session id for a headless run (no uuid dep in v0).
459fn new_session_id() -> String {
460    let now = std::time::SystemTime::now()
461        .duration_since(std::time::UNIX_EPOCH)
462        .map_or(0, |d| d.as_millis());
463    format!("sess-{now}-{}", std::process::id())
464}
465
466/// Whether an event belongs in the whole-message `stream-json` trace (ADR-0021
467/// Q1): everything except live token deltas, which are a TUI-only concern so the
468/// trace stays replayable/whole-message even under `--stream`.
469fn in_whole_message_trace(event: &locode_core::Event) -> bool {
470    !matches!(event, locode_core::Event::MessageDelta { .. })
471}
472
473#[cfg(test)]
474mod tests {
475    use super::{enforce_wire_requirement, in_whole_message_trace};
476    use locode_core::{Event, Message, Role};
477
478    #[test]
479    fn codex_rejects_a_non_responses_wire() {
480        let codex = locode_core::resolve("codex").unwrap();
481        // A real, mismatched wire is rejected pre-run with an actionable message.
482        let err = enforce_wire_requirement(codex, "anthropic").expect_err("mismatch");
483        assert!(err.0.contains("codex"), "{}", err.0);
484        assert!(err.0.contains("openai-responses"), "{}", err.0);
485        assert!(err.0.contains("anthropic"), "{}", err.0);
486        // The required wire and the keyless-CI escape hatch both pass.
487        assert!(enforce_wire_requirement(codex, "openai-responses").is_ok());
488        assert!(enforce_wire_requirement(codex, "mock").is_ok());
489    }
490
491    #[test]
492    fn wire_agnostic_packs_accept_any_wire() {
493        let grok = locode_core::resolve("grok").unwrap();
494        assert!(enforce_wire_requirement(grok, "anthropic").is_ok());
495        assert!(enforce_wire_requirement(grok, "openai-responses").is_ok());
496    }
497
498    #[test]
499    fn stream_json_trace_drops_message_deltas_keeps_whole_messages() {
500        // Token deltas are dropped from the trace...
501        assert!(!in_whole_message_trace(&Event::MessageDelta {
502            text: "tok".into()
503        }));
504        // ...but whole messages and every other event stay.
505        assert!(in_whole_message_trace(&Event::Message {
506            message: Message {
507                role: Role::Assistant,
508                content: vec![],
509            },
510        }));
511        assert!(in_whole_message_trace(&Event::Error {
512            message: "e".into()
513        }));
514    }
515}