Skip to main content

zag_agent/
builder.rs

1//! High-level builder API for driving agents programmatically.
2//!
3//! Instead of shelling out to the `agent` CLI binary, Rust programs can
4//! use `AgentBuilder` to configure and execute agent sessions directly.
5//!
6//! # Examples
7//!
8//! ```no_run
9//! use zag_agent::builder::AgentBuilder;
10//!
11//! # async fn example() -> anyhow::Result<()> {
12//! // Non-interactive exec — returns structured output
13//! let output = AgentBuilder::new()
14//!     .provider("claude")
15//!     .model("sonnet")
16//!     .auto_approve(true)
17//!     .exec("write a hello world program")
18//!     .await?;
19//!
20//! println!("{}", output.result.unwrap_or_default());
21//!
22//! // Interactive session
23//! AgentBuilder::new()
24//!     .provider("claude")
25//!     .run(Some("initial prompt"))
26//!     .await?;
27//! # Ok(())
28//! # }
29//! ```
30
31use crate::agent::Agent;
32use crate::attachment::{self, Attachment};
33use crate::config::Config;
34use crate::factory::AgentFactory;
35use crate::json_validation;
36use crate::listen::{self, ListenFormat};
37use crate::output::AgentOutput;
38use crate::process_registration::{self, ProcessRegistration, RegisterOptionsOwned};
39use crate::progress::{ProgressHandler, SilentProgress};
40use crate::providers::claude::Claude;
41use crate::providers::ollama::Ollama;
42use crate::sandbox::SandboxConfig;
43use crate::session::{SessionEntry, SessionStore};
44use crate::session_log::{
45    AgentLogEvent, LiveLogContext, LogEventCallback, SessionLogCoordinator, SessionLogMetadata,
46    live_adapter_for_provider, logs_dir,
47};
48use crate::streaming::StreamingSession;
49use crate::worktree;
50use anyhow::{Result, bail};
51use log::{debug, warn};
52use std::sync::Arc;
53use std::time::Duration;
54
55/// Format a Duration as a human-readable string (e.g., "5m", "1h30m").
56fn format_duration(d: Duration) -> String {
57    let total_secs = d.as_secs();
58    let h = total_secs / 3600;
59    let m = (total_secs % 3600) / 60;
60    let s = total_secs % 60;
61    let mut parts = Vec::new();
62    if h > 0 {
63        parts.push(format!("{h}h"));
64    }
65    if m > 0 {
66        parts.push(format!("{m}m"));
67    }
68    if s > 0 || parts.is_empty() {
69        parts.push(format!("{s}s"));
70    }
71    parts.join("")
72}
73
74/// Session discovery metadata — mirrors the `--name`, `--description`, and
75/// `--tag` flags on the `run`/`exec`/`spawn` CLI commands. Attached to a
76/// builder via [`AgentBuilder::name`], [`AgentBuilder::description`], and
77/// [`AgentBuilder::tag`].
78#[derive(Debug, Clone, Default)]
79pub struct SessionMetadata {
80    pub name: Option<String>,
81    pub description: Option<String>,
82    pub tags: Vec<String>,
83}
84
85/// Private guard returned by `AgentBuilder::start_session_log` — owns
86/// the coordinator (when `Auto`) or defers ownership to the caller (when
87/// `External`). Dropping the guard implicitly finalises the owned
88/// coordinator via its own `Drop` impl.
89struct SessionLogGuard {
90    /// Set when the builder started its own coordinator (`Auto`). Dropped
91    /// at the end of the terminal method, which finalises the log.
92    coordinator: Option<SessionLogCoordinator>,
93    wrapper_session_id: String,
94    log_path: Option<std::path::PathBuf>,
95    /// When the caller supplied an `External` coordinator, we keep a
96    /// writer clone so that `clear_event_callback` still works on exit.
97    external_writer: Option<crate::session_log::SessionLogWriter>,
98    /// Holds the externally-owned coordinator until the guard drops so
99    /// callers who pass `SessionLogMode::External` don't have to keep
100    /// their own handle alive. (They can; this is just a convenience.)
101    _owned_external: Option<SessionLogCoordinator>,
102}
103
104impl SessionLogGuard {
105    fn log_path_string(&self) -> Option<String> {
106        self.log_path
107            .as_ref()
108            .map(|p| p.to_string_lossy().to_string())
109    }
110
111    /// Flush the coordinator (emit `SessionEnded`, tear down the heartbeat /
112    /// live-adapter task). For `External` mode the caller retains ownership
113    /// — we merely detach our event callback so it stops firing after the
114    /// terminal method returns.
115    async fn finish(mut self, success: bool, error: Option<String>) {
116        // Run the finalization *before* detaching the callback so that the
117        // closing `SessionEnded` event still fires through the user's hook.
118        if let Some(coord) = self.coordinator.take() {
119            if let Err(e) = coord.finish(success, error).await {
120                warn!("Failed to finalize session log: {e}");
121            }
122        }
123        if let Some(w) = self.external_writer.take() {
124            let _ = w.clear_event_callback();
125        }
126    }
127}
128
129impl Drop for SessionLogGuard {
130    fn drop(&mut self) {
131        // Drop-path fallback: if a terminal method panicked or returned
132        // early without calling `finish`, still detach callbacks so user
133        // code stops receiving events. The owned coordinator's own `Drop`
134        // will kill the background task even without an explicit finish.
135        if let Some(ref w) = self.external_writer {
136            let _ = w.clear_event_callback();
137        }
138        if let Some(ref c) = self.coordinator {
139            let _ = c.writer().clear_event_callback();
140        }
141    }
142}
143
144/// Controls whether the builder manages a [`SessionLogCoordinator`] for the
145/// session it launches.
146///
147/// Default for [`AgentBuilder`] is [`SessionLogMode::Disabled`] so that
148/// existing Rust library callers see no side effects. The CLI and any
149/// caller that wants live event streaming should select
150/// [`SessionLogMode::Auto`].
151///
152/// [`SessionLogMode::External`] lets an advanced caller (e.g. the CLI's
153/// plan/review handlers, `zag-serve`) start and bookkeep its own
154/// coordinator — the builder will write through it without double-starting.
155#[derive(Default)]
156pub enum SessionLogMode {
157    /// No session log is started by the builder. No on-disk JSONL and no
158    /// live event callbacks.
159    #[default]
160    Disabled,
161    /// The builder starts its own [`SessionLogCoordinator`], tears it down
162    /// when the terminal method returns, and populates
163    /// [`AgentOutput::log_path`].
164    Auto,
165    /// The caller provides a pre-started [`SessionLogCoordinator`]; the
166    /// builder uses it verbatim and does not stop it at exit.
167    External(SessionLogCoordinator),
168}
169
170/// Builder for configuring and running agent sessions.
171///
172/// Use the builder pattern to set options, then call a terminal method
173/// (`exec`, `run`, `resume`, `continue_last`) to execute.
174pub struct AgentBuilder {
175    provider: Option<String>,
176    /// Set to true when the caller explicitly pinned a provider via
177    /// `.provider()`. When false (default), the fallback tier list is
178    /// allowed to downgrade to the next provider on binary/probe failure.
179    provider_explicit: bool,
180    model: Option<String>,
181    system_prompt: Option<String>,
182    root: Option<String>,
183    auto_approve: bool,
184    add_dirs: Vec<String>,
185    files: Vec<String>,
186    env_vars: Vec<(String, String)>,
187    worktree: Option<Option<String>>,
188    sandbox: Option<Option<String>>,
189    size: Option<String>,
190    json_mode: bool,
191    json_schema: Option<serde_json::Value>,
192    session_id: Option<String>,
193    metadata: SessionMetadata,
194    output_format: Option<String>,
195    input_format: Option<String>,
196    replay_user_messages: bool,
197    include_partial_messages: bool,
198    verbose: bool,
199    quiet: bool,
200    show_usage: bool,
201    max_turns: Option<u32>,
202    timeout: Option<std::time::Duration>,
203    mcp_config: Option<String>,
204    progress: Box<dyn ProgressHandler>,
205    session_log_mode: SessionLogMode,
206    /// Registered via [`AgentBuilder::on_log_event`] — fired for each
207    /// `AgentLogEvent` written to the session log while the terminal method
208    /// runs. Requires `session_log_mode != Disabled`.
209    log_event_callback: Option<LogEventCallback>,
210    /// Set via [`AgentBuilder::stream_events_to_stderr`] — overrides
211    /// `log_event_callback` to format and print each event to `stderr`.
212    stream_events_format: Option<ListenFormat>,
213    /// Whether the built-in stderr streamer should include reasoning events
214    /// (set via [`AgentBuilder::stream_show_thinking`]).
215    stream_show_thinking: bool,
216    /// Registered via [`AgentBuilder::on_spawn`] — invoked once with the
217    /// OS pid of the spawned agent subprocess right after spawn, before
218    /// the terminal wait. Useful for registering the child pid with an
219    /// external process store.
220    on_spawn_hook: Option<crate::agent::OnSpawnHook>,
221    /// Set via [`AgentBuilder::register_process`] — when present, the
222    /// terminal method registers a `ProcessEntry` in zag's `ProcessStore`,
223    /// injects `ZAG_PROCESS_ID` / `ZAG_SESSION_ID` etc. into the agent
224    /// subprocess's env, retargets the entry's pid to the agent child via
225    /// an internal `on_spawn` hook, and finalises the entry's status when
226    /// the terminal method returns.
227    register_process_opts: Option<RegisterOptionsOwned>,
228}
229
230impl Default for AgentBuilder {
231    fn default() -> Self {
232        Self::new()
233    }
234}
235
236impl AgentBuilder {
237    /// Create a new builder with default settings.
238    pub fn new() -> Self {
239        Self {
240            provider: None,
241            provider_explicit: false,
242            model: None,
243            system_prompt: None,
244            root: None,
245            auto_approve: false,
246            add_dirs: Vec::new(),
247            files: Vec::new(),
248            env_vars: Vec::new(),
249            worktree: None,
250            sandbox: None,
251            size: None,
252            json_mode: false,
253            json_schema: None,
254            session_id: None,
255            metadata: SessionMetadata::default(),
256            output_format: None,
257            input_format: None,
258            replay_user_messages: false,
259            include_partial_messages: false,
260            verbose: false,
261            quiet: false,
262            show_usage: false,
263            max_turns: None,
264            timeout: None,
265            mcp_config: None,
266            progress: Box::new(SilentProgress),
267            session_log_mode: SessionLogMode::Disabled,
268            log_event_callback: None,
269            stream_events_format: None,
270            stream_show_thinking: false,
271            on_spawn_hook: None,
272            register_process_opts: None,
273        }
274    }
275
276    /// Set the provider (e.g., "claude", "codex", "gemini", "copilot", "ollama").
277    ///
278    /// Calling this method pins the provider — it will NOT be downgraded to
279    /// another provider in the tier list if its binary is missing or the
280    /// startup probe fails. Omit this call (or set `provider` via the config
281    /// file) to allow automatic downgrading.
282    pub fn provider(mut self, provider: &str) -> Self {
283        self.provider = Some(provider.to_string());
284        self.provider_explicit = true;
285        self
286    }
287
288    /// Set the model (e.g., "sonnet", "opus", "small", "large").
289    pub fn model(mut self, model: &str) -> Self {
290        self.model = Some(model.to_string());
291        self
292    }
293
294    /// Set a system prompt to configure agent behavior.
295    pub fn system_prompt(mut self, prompt: &str) -> Self {
296        self.system_prompt = Some(prompt.to_string());
297        self
298    }
299
300    /// Set the root directory for the agent to operate in.
301    pub fn root(mut self, root: &str) -> Self {
302        self.root = Some(root.to_string());
303        self
304    }
305
306    /// Enable auto-approve mode (skip permission prompts).
307    pub fn auto_approve(mut self, approve: bool) -> Self {
308        self.auto_approve = approve;
309        self
310    }
311
312    /// Add an additional directory for the agent to include.
313    pub fn add_dir(mut self, dir: &str) -> Self {
314        self.add_dirs.push(dir.to_string());
315        self
316    }
317
318    /// Attach a file to the prompt (text files ≤50 KB inlined, others referenced).
319    pub fn file(mut self, path: &str) -> Self {
320        self.files.push(path.to_string());
321        self
322    }
323
324    /// Add an environment variable for the agent subprocess.
325    pub fn env(mut self, key: &str, value: &str) -> Self {
326        self.env_vars.push((key.to_string(), value.to_string()));
327        self
328    }
329
330    /// Enable worktree mode with an optional name.
331    pub fn worktree(mut self, name: Option<&str>) -> Self {
332        self.worktree = Some(name.map(String::from));
333        self
334    }
335
336    /// Enable sandbox mode with an optional name.
337    pub fn sandbox(mut self, name: Option<&str>) -> Self {
338        self.sandbox = Some(name.map(String::from));
339        self
340    }
341
342    /// Set the Ollama parameter size (e.g., "2b", "9b", "35b").
343    pub fn size(mut self, size: &str) -> Self {
344        self.size = Some(size.to_string());
345        self
346    }
347
348    /// Request JSON output from the agent.
349    pub fn json(mut self) -> Self {
350        self.json_mode = true;
351        self
352    }
353
354    /// Set a JSON schema for structured output validation.
355    /// Implies `json()`.
356    pub fn json_schema(mut self, schema: serde_json::Value) -> Self {
357        self.json_schema = Some(schema);
358        self.json_mode = true;
359        self
360    }
361
362    /// Set a specific session ID (UUID).
363    pub fn session_id(mut self, id: &str) -> Self {
364        self.session_id = Some(id.to_string());
365        self
366    }
367
368    /// Set a human-readable session name (mirrors the CLI's `--name`).
369    ///
370    /// Names are used by `zag input --name <n>`, `zag session list --name
371    /// <n>`, and for session discovery across the store. When the session
372    /// has a generated wrapper ID, the builder will persist this name to
373    /// the session store so CLI tools can find it later.
374    pub fn name(mut self, name: &str) -> Self {
375        self.metadata.name = Some(name.to_string());
376        self
377    }
378
379    /// Set a short description for the session (mirrors the CLI's
380    /// `--description`).
381    pub fn description(mut self, description: &str) -> Self {
382        self.metadata.description = Some(description.to_string());
383        self
384    }
385
386    /// Add a discovery tag for the session (mirrors the CLI's `--tag`,
387    /// repeatable).
388    pub fn tag(mut self, tag: &str) -> Self {
389        self.metadata.tags.push(tag.to_string());
390        self
391    }
392
393    /// Replace the full session metadata in one call.
394    pub fn metadata(mut self, metadata: SessionMetadata) -> Self {
395        self.metadata = metadata;
396        self
397    }
398
399    /// Set the output format (e.g., "text", "json", "json-pretty", "stream-json").
400    pub fn output_format(mut self, format: &str) -> Self {
401        self.output_format = Some(format.to_string());
402        self
403    }
404
405    /// Set the input format (Claude only, e.g., "text", "stream-json").
406    ///
407    /// No-op for Codex, Gemini, Copilot, and Ollama. See `docs/providers.md`
408    /// for the full per-provider support matrix.
409    pub fn input_format(mut self, format: &str) -> Self {
410        self.input_format = Some(format.to_string());
411        self
412    }
413
414    /// Re-emit user messages from stdin on stdout (Claude only).
415    ///
416    /// Only works with `--input-format stream-json` and `--output-format stream-json`.
417    /// [`exec_streaming`](Self::exec_streaming) auto-enables this flag, so most
418    /// callers never need to set it manually. No-op for non-Claude providers.
419    pub fn replay_user_messages(mut self, replay: bool) -> Self {
420        self.replay_user_messages = replay;
421        self
422    }
423
424    /// Include partial message chunks in streaming output (Claude only).
425    ///
426    /// Only works with `--output-format stream-json`. Defaults to `false`.
427    ///
428    /// When `false` (the default), streaming surfaces one `assistant_message`
429    /// event per complete assistant turn. When `true`, the agent instead emits
430    /// a stream of token-level partial `assistant_message` chunks as the model
431    /// generates them — use this for responsive, token-by-token UIs over
432    /// [`exec_streaming`](Self::exec_streaming). No-op for non-Claude providers.
433    pub fn include_partial_messages(mut self, include: bool) -> Self {
434        self.include_partial_messages = include;
435        self
436    }
437
438    /// Enable verbose output.
439    pub fn verbose(mut self, v: bool) -> Self {
440        self.verbose = v;
441        self
442    }
443
444    /// Enable quiet mode (suppress all non-essential output).
445    pub fn quiet(mut self, q: bool) -> Self {
446        self.quiet = q;
447        self
448    }
449
450    /// Show token usage statistics.
451    pub fn show_usage(mut self, show: bool) -> Self {
452        self.show_usage = show;
453        self
454    }
455
456    /// Set the maximum number of agentic turns.
457    pub fn max_turns(mut self, turns: u32) -> Self {
458        self.max_turns = Some(turns);
459        self
460    }
461
462    /// Set a timeout for exec. If the agent doesn't complete within this
463    /// duration, it will be killed and an error returned.
464    pub fn timeout(mut self, duration: std::time::Duration) -> Self {
465        self.timeout = Some(duration);
466        self
467    }
468
469    /// Set MCP server config for this invocation (Claude only).
470    ///
471    /// Accepts either a JSON string (`{"mcpServers": {...}}`) or a path to a JSON file.
472    /// No-op for Codex, Gemini, Copilot, and Ollama — those providers manage
473    /// MCP configuration through their own CLIs or do not support it. See
474    /// `docs/providers.md` for the full per-provider support matrix.
475    pub fn mcp_config(mut self, config: &str) -> Self {
476        self.mcp_config = Some(config.to_string());
477        self
478    }
479
480    /// Set a custom progress handler for status reporting.
481    pub fn on_progress(mut self, handler: Box<dyn ProgressHandler>) -> Self {
482        self.progress = handler;
483        self
484    }
485
486    /// Select how the builder manages the session log. See [`SessionLogMode`].
487    pub fn session_log(mut self, mode: SessionLogMode) -> Self {
488        self.session_log_mode = mode;
489        self
490    }
491
492    /// Shortcut for `.session_log(SessionLogMode::Auto)` when `true`, or
493    /// `.session_log(SessionLogMode::Disabled)` when `false`.
494    pub fn enable_session_log(mut self, enable: bool) -> Self {
495        self.session_log_mode = if enable {
496            SessionLogMode::Auto
497        } else {
498            SessionLogMode::Disabled
499        };
500        self
501    }
502
503    /// Register a callback fired for each `AgentLogEvent` written to the
504    /// session log during the terminal method. Implicitly switches
505    /// `session_log_mode` to [`SessionLogMode::Auto`] if it is currently
506    /// [`SessionLogMode::Disabled`].
507    pub fn on_log_event<F>(mut self, f: F) -> Self
508    where
509        F: Fn(&AgentLogEvent) + Send + Sync + 'static,
510    {
511        self.log_event_callback = Some(Arc::new(f));
512        if matches!(self.session_log_mode, SessionLogMode::Disabled) {
513            self.session_log_mode = SessionLogMode::Auto;
514        }
515        self
516    }
517
518    /// Convenience: tail the session log to stderr during the terminal
519    /// method, using the same formatters as the `zag listen` command.
520    ///
521    /// This is the drop-in replacement for the live stderr tail that a
522    /// previous shell-out-to-`zag` wrapper produced. Implicitly enables
523    /// session logging.
524    pub fn stream_events_to_stderr(mut self, format: ListenFormat) -> Self {
525        self.stream_events_format = Some(format);
526        if matches!(self.session_log_mode, SessionLogMode::Disabled) {
527            self.session_log_mode = SessionLogMode::Auto;
528        }
529        self
530    }
531
532    /// Include `Reasoning` events in the stderr stream when
533    /// [`stream_events_to_stderr`] is active. Off by default.
534    pub fn stream_show_thinking(mut self, show: bool) -> Self {
535        self.stream_show_thinking = show;
536        self
537    }
538
539    /// Register a callback invoked once with the OS pid of the spawned
540    /// agent subprocess, right after spawn and before the terminal
541    /// wait. Useful for registering the child pid with an external
542    /// process store so `zag ps kill self` (or equivalent) can SIGTERM
543    /// the agent child rather than the parent zag process.
544    ///
545    /// The callback fires once per spawn — on retries or resumes it
546    /// fires again for each new child. See [`crate::agent::OnSpawnHook`]
547    /// for the full semantics.
548    pub fn on_spawn<F>(mut self, f: F) -> Self
549    where
550        F: Fn(u32) + Send + Sync + 'static,
551    {
552        self.on_spawn_hook = Some(Arc::new(f));
553        self
554    }
555
556    /// Register the about-to-spawn agent in zag's `ProcessStore` and inject
557    /// `ZAG_PROCESS_ID` / `ZAG_SESSION_ID` / `ZAG_PROVIDER` / `ZAG_MODEL`
558    /// into its env so commands like `zag ps kill self` and `zig self
559    /// terminate` can resolve the running agent from inside its own
560    /// subshell.
561    ///
562    /// The terminal method takes care of:
563    ///
564    /// 1. Calling [`crate::process_registration::register`] before spawn.
565    /// 2. Appending the resulting env vars onto the builder.
566    /// 3. Composing the registration's `on_spawn` hook with any caller-set
567    ///    [`AgentBuilder::on_spawn`] so both fire.
568    /// 4. Calling [`ProcessRegistration::update_status`] with `"exited"` /
569    ///    `"killed"` once the agent finishes.
570    ///
571    /// Without this call, the builder leaves the registry untouched (the
572    /// pre-existing default behavior). Callers that already manage their
573    /// own `ProcessEntry` should not opt in.
574    pub fn register_process(mut self, opts: RegisterOptionsOwned) -> Self {
575        self.register_process_opts = Some(opts);
576        self
577    }
578}
579
580/// Apply a [`ProcessRegistration`] to the builder's `env_vars` and
581/// `on_spawn_hook` fields so the agent subprocess inherits the env vars and
582/// the registry entry's pid is retargeted on spawn. Composes with any
583/// caller-set `on_spawn` hook — both fire.
584fn apply_registration(builder: &mut AgentBuilder, reg: &ProcessRegistration) {
585    for (k, v) in reg.env_vars() {
586        builder.env_vars.push((k.clone(), v.clone()));
587    }
588    let reg_hook = reg.on_spawn_hook();
589    let prev_hook = builder.on_spawn_hook.take();
590    builder.on_spawn_hook = Some(Arc::new(move |pid: u32| {
591        reg_hook(pid);
592        if let Some(ref h) = prev_hook {
593            h(pid);
594        }
595    }));
596}
597
598/// Map a terminal-method `Result<T>` to the `(status, exit_code)` pair
599/// stored on the `ProcessEntry`. Mirrors what `zag-cli/src/commands/
600/// agent_action.rs` records: `"killed"` + the agent's reported exit code on
601/// failure, `"exited"` + 0 on success.
602fn status_for_result<T>(result: &Result<T>) -> (&'static str, Option<i32>) {
603    match result {
604        Ok(_) => ("exited", Some(0)),
605        Err(err) => {
606            let exit_code = err
607                .downcast_ref::<crate::process::ProcessError>()
608                .and_then(|pe| pe.exit_code)
609                .unwrap_or(1);
610            ("killed", Some(exit_code))
611        }
612    }
613}
614
615impl AgentBuilder {
616    /// Persist a `SessionEntry` to the session store so `zag session list`
617    /// and `zag input --name <n>` can discover this builder-spawned session.
618    ///
619    /// No-op when no metadata is set — callers who don't name their sessions
620    /// still get the old behavior of not leaving a trail in the session store.
621    ///
622    /// Returns the session ID that was persisted (either the caller-provided
623    /// one or a freshly generated UUID), so downstream logging can reference
624    /// the same ID.
625    fn persist_session_metadata_with_id(
626        &self,
627        provider: &str,
628        model: &str,
629        effective_root: Option<&str>,
630        explicit_session_id: Option<&str>,
631    ) -> Option<String> {
632        let has_metadata = self.metadata.name.is_some()
633            || self.metadata.description.is_some()
634            || !self.metadata.tags.is_empty();
635        if !has_metadata {
636            return None;
637        }
638
639        let session_id = explicit_session_id
640            .map(String::from)
641            .or_else(|| self.session_id.clone())
642            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
643        let workspace_path = effective_root
644            .map(String::from)
645            .or_else(|| self.root.clone())
646            .unwrap_or_else(|| {
647                std::env::current_dir()
648                    .map(|p| p.to_string_lossy().to_string())
649                    .unwrap_or_default()
650            });
651
652        let entry = SessionEntry {
653            session_id: session_id.clone(),
654            provider: provider.to_string(),
655            model: model.to_string(),
656            worktree_path: workspace_path,
657            worktree_name: String::new(),
658            created_at: chrono::Utc::now().to_rfc3339(),
659            provider_session_id: None,
660            sandbox_name: None,
661            is_worktree: self.worktree.is_some(),
662            discovered: false,
663            discovery_source: None,
664            log_path: None,
665            log_completeness: "partial".to_string(),
666            name: self.metadata.name.clone(),
667            description: self.metadata.description.clone(),
668            tags: self.metadata.tags.clone(),
669            dependencies: Vec::new(),
670            retried_from: None,
671            interactive: false,
672        };
673
674        let mut store = SessionStore::load(self.root.as_deref()).unwrap_or_default();
675        store.add(entry);
676        if let Err(e) = store.save(self.root.as_deref()) {
677            warn!("Failed to persist session metadata: {e}");
678        }
679
680        Some(session_id)
681    }
682
683    /// Resolve file attachments and prepend them to a prompt.
684    fn prepend_files(&self, prompt: &str) -> Result<String> {
685        if self.files.is_empty() {
686            return Ok(prompt.to_string());
687        }
688        let attachments: Vec<Attachment> = self
689            .files
690            .iter()
691            .map(|f| Attachment::from_path(std::path::Path::new(f)))
692            .collect::<Result<Vec<_>>>()?;
693        let prefix = attachment::format_attachments_prefix(&attachments);
694        Ok(format!("{prefix}{prompt}"))
695    }
696
697    /// Resolve the effective provider name.
698    fn resolve_provider(&self) -> Result<String> {
699        if let Some(ref p) = self.provider {
700            let p = p.to_lowercase();
701            if !Config::VALID_PROVIDERS.contains(&p.as_str()) {
702                bail!(
703                    "Invalid provider '{}'. Available: {}",
704                    p,
705                    Config::VALID_PROVIDERS.join(", ")
706                );
707            }
708            return Ok(p);
709        }
710        let config = Config::load(self.root.as_deref()).unwrap_or_default();
711        if let Some(p) = config.provider() {
712            return Ok(p.to_string());
713        }
714        Ok("claude".to_string())
715    }
716
717    /// Create and configure the agent.
718    ///
719    /// Returns the constructed agent along with the provider name that
720    /// actually succeeded. When `provider_explicit` is false, the factory
721    /// may downgrade to another provider in the tier list, so the returned
722    /// provider can differ from the one passed in.
723    async fn create_agent(&self, provider: &str) -> Result<(Box<dyn Agent + Send + Sync>, String)> {
724        // Apply system_prompt config fallback
725        let base_system_prompt = self.system_prompt.clone().or_else(|| {
726            Config::load(self.root.as_deref())
727                .unwrap_or_default()
728                .system_prompt()
729                .map(String::from)
730        });
731
732        // Augment system prompt with JSON instructions for non-Claude agents
733        let system_prompt = if self.json_mode && provider != "claude" {
734            let mut prompt = base_system_prompt.unwrap_or_default();
735            if let Some(ref schema) = self.json_schema {
736                let schema_str = serde_json::to_string_pretty(schema).unwrap_or_default();
737                prompt.push_str(&format!(
738                    "\n\nYou MUST respond with valid JSON only. No markdown fences, no explanations. \
739                     Your response must conform to this JSON schema:\n{schema_str}"
740                ));
741            } else {
742                prompt.push_str(
743                    "\n\nYou MUST respond with valid JSON only. No markdown fences, no explanations.",
744                );
745            }
746            Some(prompt)
747        } else {
748            base_system_prompt
749        };
750
751        self.progress
752            .on_spinner_start(&format!("Initializing {provider} agent"));
753
754        let progress = &*self.progress;
755        let mut on_downgrade = |from: &str, to: &str, reason: &str| {
756            progress.on_warning(&format!("Downgrading provider: {from} → {to} ({reason})"));
757        };
758        let (mut agent, effective_provider) = AgentFactory::create_with_fallback(
759            provider,
760            self.provider_explicit,
761            system_prompt,
762            self.model.clone(),
763            self.root.clone(),
764            self.auto_approve,
765            self.add_dirs.clone(),
766            &mut on_downgrade,
767        )
768        .await?;
769        let provider = effective_provider.as_str();
770
771        // Apply max_turns: explicit > config > none
772        let effective_max_turns = self.max_turns.or_else(|| {
773            Config::load(self.root.as_deref())
774                .unwrap_or_default()
775                .max_turns()
776        });
777        if let Some(turns) = effective_max_turns {
778            agent.set_max_turns(turns);
779        }
780
781        // Set output format
782        let mut output_format = self.output_format.clone();
783        if self.json_mode && output_format.is_none() {
784            output_format = Some("json".to_string());
785            if provider != "claude" {
786                agent.set_capture_output(true);
787            }
788        }
789        agent.set_output_format(output_format);
790
791        // Configure Claude-specific options
792        if provider == "claude"
793            && let Some(claude_agent) = agent.as_any_mut().downcast_mut::<Claude>()
794        {
795            claude_agent.set_verbose(self.verbose);
796            if let Some(ref session_id) = self.session_id {
797                claude_agent.set_session_id(session_id.clone());
798            }
799            if let Some(ref input_fmt) = self.input_format {
800                claude_agent.set_input_format(Some(input_fmt.clone()));
801            }
802            if self.replay_user_messages {
803                claude_agent.set_replay_user_messages(true);
804            }
805            if self.include_partial_messages {
806                claude_agent.set_include_partial_messages(true);
807            }
808            if self.json_mode
809                && let Some(ref schema) = self.json_schema
810            {
811                let schema_str = serde_json::to_string(schema).unwrap_or_default();
812                claude_agent.set_json_schema(Some(schema_str));
813            }
814            if self.mcp_config.is_some() {
815                claude_agent.set_mcp_config(self.mcp_config.clone());
816            }
817        }
818
819        // Configure Ollama-specific options
820        if provider == "ollama"
821            && let Some(ollama_agent) = agent.as_any_mut().downcast_mut::<Ollama>()
822        {
823            let config = Config::load(self.root.as_deref()).unwrap_or_default();
824            if let Some(ref size) = self.size {
825                let resolved = config.ollama_size_for(size);
826                ollama_agent.set_size(resolved.to_string());
827            }
828        }
829
830        // Configure sandbox
831        if let Some(ref sandbox_opt) = self.sandbox {
832            let sandbox_name = sandbox_opt
833                .as_deref()
834                .map(String::from)
835                .unwrap_or_else(crate::sandbox::generate_name);
836            let template = crate::sandbox::template_for_provider(provider);
837            let workspace = self.root.clone().unwrap_or_else(|| ".".to_string());
838            agent.set_sandbox(SandboxConfig {
839                name: sandbox_name,
840                template: template.to_string(),
841                workspace,
842            });
843        }
844
845        if !self.env_vars.is_empty() {
846            agent.set_env_vars(self.env_vars.clone());
847        }
848
849        if let Some(ref hook) = self.on_spawn_hook {
850            agent.set_on_spawn_hook(hook.clone());
851        }
852
853        self.progress.on_spinner_finish();
854        self.progress.on_success(&format!(
855            "{} initialized with model {}",
856            provider,
857            agent.get_model()
858        ));
859
860        Ok((agent, effective_provider))
861    }
862
863    /// Start (or adopt) a [`SessionLogCoordinator`] for the session about
864    /// to run, honouring the builder's [`SessionLogMode`] and wiring up any
865    /// registered `on_log_event` / `stream_events_to_stderr` callback.
866    ///
867    /// Returns a guard that owns the coordinator (where applicable) and the
868    /// resolved `wrapper_session_id` + log path, or `None` when logging is
869    /// disabled.
870    fn start_session_log(
871        &mut self,
872        command: &str,
873        resumed: bool,
874        provider: &str,
875        model: &str,
876    ) -> Option<SessionLogGuard> {
877        let mode = std::mem::replace(&mut self.session_log_mode, SessionLogMode::Disabled);
878        match mode {
879            SessionLogMode::Disabled => None,
880            SessionLogMode::External(c) => {
881                let wrapper_session_id = c
882                    .writer()
883                    .log_path()
884                    .ok()
885                    .and_then(|p| p.file_stem().map(|s| s.to_string_lossy().to_string()))
886                    .unwrap_or_default();
887                let log_path = c.writer().log_path().ok();
888                self.apply_event_callback(c.writer());
889                Some(SessionLogGuard {
890                    coordinator: None, // externally owned
891                    wrapper_session_id,
892                    log_path,
893                    external_writer: Some(c.writer().clone()),
894                    _owned_external: Some(c),
895                })
896            }
897            SessionLogMode::Auto => {
898                let wrapper_session_id = self
899                    .session_id
900                    .clone()
901                    .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
902                let metadata = SessionLogMetadata {
903                    provider: provider.to_string(),
904                    wrapper_session_id: wrapper_session_id.clone(),
905                    provider_session_id: None,
906                    workspace_path: self.root.clone().or_else(|| {
907                        std::env::current_dir()
908                            .ok()
909                            .map(|p| p.to_string_lossy().to_string())
910                    }),
911                    command: command.to_string(),
912                    model: Some(model.to_string()),
913                    resumed,
914                    backfilled: false,
915                };
916                let live_ctx = LiveLogContext {
917                    root: self.root.clone(),
918                    provider_session_id: metadata.provider_session_id.clone(),
919                    workspace_path: metadata.workspace_path.clone(),
920                    started_at: chrono::Utc::now(),
921                    is_worktree: self.worktree.is_some(),
922                };
923                let adapter = live_adapter_for_provider(provider, live_ctx, true);
924                let callback = self.build_event_callback();
925                match SessionLogCoordinator::start_with_callback(
926                    &logs_dir(self.root.as_deref()),
927                    metadata,
928                    adapter,
929                    callback,
930                ) {
931                    Ok(c) => {
932                        let _ = c.writer().set_global_index_dir(Config::global_base_dir());
933                        let log_path = c.writer().log_path().ok();
934                        Some(SessionLogGuard {
935                            coordinator: Some(c),
936                            wrapper_session_id,
937                            log_path,
938                            external_writer: None,
939                            _owned_external: None,
940                        })
941                    }
942                    Err(e) => {
943                        warn!("Failed to start session log coordinator: {e}");
944                        None
945                    }
946                }
947            }
948        }
949    }
950
951    /// Build a combined event callback from any registered `on_log_event`
952    /// and `stream_events_to_stderr` setters. Returns `None` when neither
953    /// is set so the writer doesn't pay any per-event cost.
954    fn build_event_callback(&self) -> Option<LogEventCallback> {
955        let user_cb = self.log_event_callback.clone();
956        let stream_fmt = self.stream_events_format;
957        let show_thinking = self.stream_show_thinking;
958
959        if user_cb.is_none() && stream_fmt.is_none() {
960            return None;
961        }
962
963        Some(Arc::new(move |event: &AgentLogEvent| {
964            if let Some(ref user) = user_cb {
965                user(event);
966            }
967            if let Some(fmt) = stream_fmt
968                && let Some(text) = listen::format_event(event, fmt, show_thinking)
969            {
970                eprintln!("{text}");
971            }
972        }))
973    }
974
975    /// Register the builder's callback on an externally-owned writer (used
976    /// by [`SessionLogMode::External`] — the coordinator is already running
977    /// so we can't register the callback before `SessionStarted`, but any
978    /// post-adoption event will still fire).
979    fn apply_event_callback(&self, writer: &crate::session_log::SessionLogWriter) {
980        if let Some(cb) = self.build_event_callback() {
981            if let Err(e) = writer.set_event_callback(cb) {
982                warn!("Failed to register session log event callback: {e}");
983            }
984        }
985    }
986
987    /// Run the agent non-interactively and return structured output.
988    ///
989    /// This is the primary entry point for programmatic use.
990    pub async fn exec(mut self, prompt: &str) -> Result<AgentOutput> {
991        let registration = self
992            .register_process_opts
993            .as_ref()
994            .map(|opts| process_registration::register(opts.as_borrowed()));
995        if let Some(ref reg) = registration {
996            apply_registration(&mut self, reg);
997        }
998        let result = self.exec_inner(prompt).await;
999        if let Some(reg) = registration {
1000            let (status, code) = status_for_result(&result);
1001            reg.update_status(status, code);
1002        }
1003        result
1004    }
1005
1006    async fn exec_inner(self, prompt: &str) -> Result<AgentOutput> {
1007        let provider = self.resolve_provider()?;
1008        debug!("exec: provider={provider}");
1009
1010        // Set up worktree if requested
1011        let effective_root = if let Some(ref wt_opt) = self.worktree {
1012            let wt_name = wt_opt
1013                .as_deref()
1014                .map(String::from)
1015                .unwrap_or_else(worktree::generate_name);
1016            let repo_root = worktree::git_repo_root(self.root.as_deref())?;
1017            let wt_path = worktree::create_worktree(&repo_root, &wt_name)?;
1018            self.progress
1019                .on_success(&format!("Worktree created at {}", wt_path.display()));
1020            Some(wt_path.to_string_lossy().to_string())
1021        } else {
1022            self.root.clone()
1023        };
1024
1025        let mut builder = self;
1026        if effective_root.is_some() {
1027            builder.root = effective_root;
1028        }
1029
1030        let (agent, provider) = builder.create_agent(&provider).await?;
1031
1032        // Start (or adopt) the session log coordinator. Held for the whole
1033        // terminal method; dropped after cleanup so the log file is
1034        // finalised exactly once.
1035        let log_guard = builder.start_session_log("exec", false, &provider, agent.get_model());
1036
1037        // Persist the session entry so discovery (session list --name, input
1038        // --name) works for builder-spawned sessions. No-op when no metadata
1039        // is set. When session logging is active, share its wrapper_session_id
1040        // so the store entry and the JSONL log agree.
1041        let _ = builder.persist_session_metadata_with_id(
1042            &provider,
1043            agent.get_model(),
1044            builder.root.as_deref(),
1045            log_guard.as_ref().map(|g| g.wrapper_session_id.as_str()),
1046        );
1047
1048        // Prepend file attachments
1049        let prompt_with_files = builder.prepend_files(prompt)?;
1050
1051        // Handle JSON mode with prompt wrapping for non-Claude agents
1052        let effective_prompt = if builder.json_mode && provider != "claude" {
1053            format!(
1054                "IMPORTANT: You MUST respond with valid JSON only. No markdown, no explanation.\n\n{prompt_with_files}"
1055            )
1056        } else {
1057            prompt_with_files
1058        };
1059
1060        let result = if let Some(timeout_dur) = builder.timeout {
1061            match tokio::time::timeout(timeout_dur, agent.run(Some(&effective_prompt))).await {
1062                Ok(r) => r?,
1063                Err(_) => {
1064                    agent.cleanup().await.ok();
1065                    bail!("Agent timed out after {}", format_duration(timeout_dur));
1066                }
1067            }
1068        } else {
1069            agent.run(Some(&effective_prompt)).await?
1070        };
1071
1072        // Clean up
1073        agent.cleanup().await?;
1074
1075        let log_path_string = log_guard.as_ref().and_then(|g| g.log_path_string());
1076
1077        if let Some(mut output) = result {
1078            // Validate JSON output if schema is provided
1079            if let Some(ref schema) = builder.json_schema {
1080                if !builder.json_mode {
1081                    warn!(
1082                        "json_schema is set but json_mode is false — \
1083                         schema will not be sent to the agent, only used for output validation"
1084                    );
1085                }
1086                if let Some(ref result_text) = output.result {
1087                    debug!(
1088                        "exec: validating result ({} bytes): {:.300}",
1089                        result_text.len(),
1090                        result_text
1091                    );
1092                    if let Err(errors) = json_validation::validate_json_schema(result_text, schema)
1093                    {
1094                        let preview = if result_text.len() > 500 {
1095                            &result_text[..500]
1096                        } else {
1097                            result_text.as_str()
1098                        };
1099                        bail!(
1100                            "JSON schema validation failed: {}\nRaw agent output ({} bytes):\n{}",
1101                            errors.join("; "),
1102                            result_text.len(),
1103                            preview
1104                        );
1105                    }
1106                }
1107            }
1108            output.log_path = log_path_string;
1109            let success = !output.is_error;
1110            let err_msg = output.error_message.clone();
1111            if let Some(g) = log_guard {
1112                g.finish(success, err_msg).await;
1113            }
1114            Ok(output)
1115        } else {
1116            // Agent returned no structured output — create a minimal one
1117            let mut output = AgentOutput::from_text(&provider, "");
1118            output.log_path = log_path_string;
1119            if let Some(g) = log_guard {
1120                g.finish(true, None).await;
1121            }
1122            Ok(output)
1123        }
1124    }
1125
1126    /// Run the agent with streaming input and output (Claude only).
1127    ///
1128    /// Returns a [`StreamingSession`] that allows sending NDJSON messages to
1129    /// the agent's stdin and reading events from stdout. Automatically
1130    /// configures `--input-format stream-json`, `--output-format stream-json`,
1131    /// and `--replay-user-messages`.
1132    ///
1133    /// # Default emission granularity
1134    ///
1135    /// By default `assistant_message` events are emitted **once per complete
1136    /// assistant turn** — you get one event when the model finishes speaking,
1137    /// not a stream of token chunks. For responsive, token-level UIs call
1138    /// [`include_partial_messages(true)`](Self::include_partial_messages)
1139    /// on the builder before `exec_streaming`; the session will then emit
1140    /// partial `assistant_message` chunks as the model generates them.
1141    ///
1142    /// The default is kept `false` so existing callers that render whole-turn
1143    /// bubbles are not broken. See `docs/providers.md` for the full
1144    /// per-provider flag support matrix.
1145    ///
1146    /// # Event lifecycle
1147    ///
1148    /// The session emits a unified
1149    /// [`Event::Result`](crate::output::Event::Result) at the **end of every
1150    /// agent turn** — not only at final session end. Use that event as the
1151    /// authoritative turn-boundary signal. After a `Result`, the session
1152    /// remains open and accepts another
1153    /// [`send_user_message`](StreamingSession::send_user_message) for the next
1154    /// turn. Call
1155    /// [`close_input`](StreamingSession::close_input) followed by
1156    /// [`wait`](StreamingSession::wait) to terminate the session cleanly.
1157    ///
1158    /// Do not depend on replayed `user_message` events to detect turn
1159    /// boundaries; those only appear while `--replay-user-messages` is set.
1160    ///
1161    /// # Examples
1162    ///
1163    /// ```no_run
1164    /// use zag_agent::builder::AgentBuilder;
1165    /// use zag_agent::output::Event;
1166    ///
1167    /// # async fn example() -> anyhow::Result<()> {
1168    /// let mut session = AgentBuilder::new()
1169    ///     .provider("claude")
1170    ///     .exec_streaming("initial prompt")
1171    ///     .await?;
1172    ///
1173    /// // Drain the first turn until Result.
1174    /// while let Some(event) = session.next_event().await? {
1175    ///     println!("{:?}", event);
1176    ///     if matches!(event, Event::Result { .. }) {
1177    ///         break;
1178    ///     }
1179    /// }
1180    ///
1181    /// // Follow-up turn.
1182    /// session.send_user_message("do something else").await?;
1183    /// while let Some(event) = session.next_event().await? {
1184    ///     if matches!(event, Event::Result { .. }) {
1185    ///         break;
1186    ///     }
1187    /// }
1188    ///
1189    /// session.close_input();
1190    /// session.wait().await?;
1191    /// # Ok(())
1192    /// # }
1193    /// ```
1194    pub async fn exec_streaming(self, prompt: &str) -> Result<StreamingSession> {
1195        let provider = self.resolve_provider()?;
1196        debug!("exec_streaming: provider={provider}");
1197
1198        if provider != "claude" {
1199            bail!("Streaming input is only supported by the Claude provider");
1200        }
1201
1202        // Prepend file attachments
1203        let prompt_with_files = self.prepend_files(prompt)?;
1204
1205        // Streaming only works on Claude — do not allow the fallback loop
1206        // to downgrade to a provider that can't stream.
1207        let mut builder = self;
1208        builder.provider_explicit = true;
1209        let (agent, _provider) = builder.create_agent(&provider).await?;
1210
1211        // Downcast to Claude to call execute_streaming
1212        let claude_agent = agent
1213            .as_any_ref()
1214            .downcast_ref::<Claude>()
1215            .ok_or_else(|| anyhow::anyhow!("Failed to downcast agent to Claude"))?;
1216
1217        claude_agent.execute_streaming(Some(&prompt_with_files))
1218    }
1219
1220    /// Start an interactive agent session.
1221    ///
1222    /// This takes over stdin/stdout for the duration of the session.
1223    pub async fn run(mut self, prompt: Option<&str>) -> Result<()> {
1224        let registration = self
1225            .register_process_opts
1226            .as_ref()
1227            .map(|opts| process_registration::register(opts.as_borrowed()));
1228        if let Some(ref reg) = registration {
1229            apply_registration(&mut self, reg);
1230        }
1231        let result = self.run_inner(prompt).await;
1232        if let Some(reg) = registration {
1233            let (status, code) = status_for_result(&result);
1234            reg.update_status(status, code);
1235        }
1236        result
1237    }
1238
1239    async fn run_inner(self, prompt: Option<&str>) -> Result<()> {
1240        let provider = self.resolve_provider()?;
1241        debug!("run: provider={provider}");
1242
1243        // Prepend file attachments
1244        let prompt_with_files = match prompt {
1245            Some(p) => Some(self.prepend_files(p)?),
1246            None if !self.files.is_empty() => {
1247                let attachments: Vec<Attachment> = self
1248                    .files
1249                    .iter()
1250                    .map(|f| Attachment::from_path(std::path::Path::new(f)))
1251                    .collect::<Result<Vec<_>>>()?;
1252                Some(attachment::format_attachments_prefix(&attachments))
1253            }
1254            None => None,
1255        };
1256
1257        let mut builder = self;
1258        let (agent, effective_provider) = builder.create_agent(&provider).await?;
1259        let log_guard =
1260            builder.start_session_log("run", false, &effective_provider, agent.get_model());
1261        let _ = builder.persist_session_metadata_with_id(
1262            &effective_provider,
1263            agent.get_model(),
1264            builder.root.as_deref(),
1265            log_guard.as_ref().map(|g| g.wrapper_session_id.as_str()),
1266        );
1267        agent.run_interactive(prompt_with_files.as_deref()).await?;
1268        agent.cleanup().await?;
1269        if let Some(g) = log_guard {
1270            g.finish(true, None).await;
1271        }
1272        Ok(())
1273    }
1274
1275    /// Resume a previous session by ID.
1276    pub async fn resume(mut self, session_id: &str) -> Result<()> {
1277        let registration = self
1278            .register_process_opts
1279            .as_ref()
1280            .map(|opts| process_registration::register(opts.as_borrowed()));
1281        if let Some(ref reg) = registration {
1282            apply_registration(&mut self, reg);
1283        }
1284        let result = self.resume_inner(session_id).await;
1285        if let Some(reg) = registration {
1286            let (status, code) = status_for_result(&result);
1287            reg.update_status(status, code);
1288        }
1289        result
1290    }
1291
1292    async fn resume_inner(self, session_id: &str) -> Result<()> {
1293        let provider = self.resolve_provider()?;
1294        debug!("resume: provider={provider}, session={session_id}");
1295
1296        // Resuming must stick with the recorded provider — no downgrade.
1297        let mut builder = self;
1298        builder.provider_explicit = true;
1299        let (agent, effective_provider) = builder.create_agent(&provider).await?;
1300        let log_guard =
1301            builder.start_session_log("resume", true, &effective_provider, agent.get_model());
1302        agent.run_resume(Some(session_id), false).await?;
1303        agent.cleanup().await?;
1304        if let Some(g) = log_guard {
1305            g.finish(true, None).await;
1306        }
1307        Ok(())
1308    }
1309
1310    /// Resume the most recent session.
1311    pub async fn continue_last(mut self) -> Result<()> {
1312        let registration = self
1313            .register_process_opts
1314            .as_ref()
1315            .map(|opts| process_registration::register(opts.as_borrowed()));
1316        if let Some(ref reg) = registration {
1317            apply_registration(&mut self, reg);
1318        }
1319        let result = self.continue_last_inner().await;
1320        if let Some(reg) = registration {
1321            let (status, code) = status_for_result(&result);
1322            reg.update_status(status, code);
1323        }
1324        result
1325    }
1326
1327    async fn continue_last_inner(self) -> Result<()> {
1328        let provider = self.resolve_provider()?;
1329        debug!("continue_last: provider={provider}");
1330
1331        // Resuming must stick with the recorded provider — no downgrade.
1332        let mut builder = self;
1333        builder.provider_explicit = true;
1334        let (agent, effective_provider) = builder.create_agent(&provider).await?;
1335        let log_guard =
1336            builder.start_session_log("resume", true, &effective_provider, agent.get_model());
1337        agent.run_resume(None, true).await?;
1338        agent.cleanup().await?;
1339        if let Some(g) = log_guard {
1340            g.finish(true, None).await;
1341        }
1342        Ok(())
1343    }
1344}
1345
1346#[cfg(test)]
1347#[path = "builder_tests.rs"]
1348mod tests;