elicitation 0.10.0

Conversational elicitation of strongly-typed Rust values via MCP
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
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
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! Code recovery — emit verified workflows as Rust source.
//!
//! [`EmitCode`] is the reverse of the MCP transport layer: where the MCP layer
//! serializes Rust calls into JSON tool calls for agents, `EmitCode` recovers
//! the original Rust source from a concrete parameterized invocation.
//!
//! # The Core Idea
//!
//! Every MCP tool call is a thin wrapper over real Rust. An agent composing
//! `parse_and_focus → validate_object → pointer_update` has authored a verified
//! Rust program without knowing it. `EmitCode` materializes that program.
//!
//! The emitted source:
//! - Calls our library's verified APIs directly (not reimplementing logic)
//! - Preserves the full typestate ceremony: proof tokens, `Established<P>`, etc.
//! - Compiles against our pinned workspace crates as dependencies
//! - Inherits formal verification by virtue of calling verified action trait impls
//!
//! # Usage
//!
//! ```rust,ignore
//! use elicitation::emit_code::{BinaryScaffold, CrateDep, EmitCode};
//!
//! // A workflow params struct implements EmitCode
//! let step = ParseFocusParams { json: r#"{"name":"Alice"}"#.into(), pointer: "/name".into() };
//!
//! let scaffold = BinaryScaffold::new(vec![Box::new(step)], true);
//! let source = scaffold.to_source(); // formatted Rust source as String
//! ```
//!
//! # Primitive impls
//!
//! All numeric primitives, `bool`, `char`, and `String` are covered by an
//! internal macro that delegates to `quote::ToTokens`. Compound types
//! (`Vec<T>`, `Option<T>`, `PathBuf`, `Duration`, tuples) have explicit impls.

use proc_macro2::TokenStream;

// ── Global emit registry ──────────────────────────────────────────────────────

/// An inventory entry connecting a tool name to an [`EmitCode`] constructor.
///
/// Register via `inventory::submit!(EmitEntry { ... })` in each crate that
/// exposes tools with code-recovery support.  The global [`dispatch_emit`]
/// function collects all registered entries at runtime.
///
/// # Example
///
/// ```rust,ignore
/// # use elicitation::emit_code::{EmitEntry, EmitCode};
/// fn make_my_emit(v: serde_json::Value) -> Result<Box<dyn EmitCode>, String> {
///     serde_json::from_value::<MyParams>(v)
///         .map(|p| Box::new(p) as Box<dyn EmitCode>)
///         .map_err(|e| e.to_string())
/// }
///
/// inventory::submit! {
///     EmitEntry { tool: "my_tool", constructor: make_my_emit }
/// }
/// ```
pub struct EmitEntry {
    /// Bare tool name (no namespace prefix), e.g. `"parse_url"`.
    pub tool: &'static str,
    /// Crate that registered this entry, e.g. `"elicit_chrono"`.
    pub crate_name: &'static str,
    /// Deserialize params from JSON and box as [`EmitCode`].
    pub constructor: fn(serde_json::Value) -> Result<Box<dyn EmitCode>, String>,
}

inventory::collect!(EmitEntry);

/// Look up a tool name in the global emit registry and deserialize its params.
///
/// Returns a boxed [`EmitCode`] ready to pass to [`BinaryScaffold`], or an
/// error string if the tool is not registered or params fail to deserialize.
///
/// This replaces the per-crate `dispatch_*_emit` chain that was previously
/// required in [`EmitBinaryPlugin`](crate::plugin::ElicitPlugin).
pub fn dispatch_emit(tool: &str, params: serde_json::Value) -> Result<Box<dyn EmitCode>, String> {
    inventory::iter::<EmitEntry>()
        .find(|e| e.tool == tool)
        .ok_or_else(|| format!("unknown emit tool: '{tool}'"))
        .and_then(|e| (e.constructor)(params))
}

/// Look up a tool registered by a specific crate and deserialize its params.
///
/// Use this when multiple crates define a tool with the same name
/// (e.g. `"assert_future"` in `elicit_chrono`, `elicit_jiff`, `elicit_time`).
pub fn dispatch_emit_from(
    tool: &str,
    crate_name: &str,
    params: serde_json::Value,
) -> Result<Box<dyn EmitCode>, String> {
    inventory::iter::<EmitEntry>()
        .find(|e| e.tool == tool && e.crate_name == crate_name)
        .ok_or_else(|| format!("unknown emit tool: '{crate_name}::{tool}'"))
        .and_then(|e| (e.constructor)(params))
}

// ── Registration helper macro ─────────────────────────────────────────────────

/// Register a params type with the global emit registry under a tool name.
///
/// Generates a named constructor function (to satisfy `inventory`'s requirement
/// for `'static` function pointers) and submits an [`EmitEntry`].
///
/// Only active when the `emit` feature is enabled.
///
/// # Example
///
/// ```rust,ignore
/// // In workflow.rs, under #[cfg(feature = "emit")]:
/// register_emit!("parse_url", ParseUrlParams);
/// register_emit!("assert_https", AssertHttpsParams);
/// ```
#[macro_export]
macro_rules! register_emit {
    ($tool:literal, $T:ty) => {
        const _: () = {
            fn __emit_constructor(
                v: elicitation::serde_json::Value,
            ) -> ::std::result::Result<
                ::std::boxed::Box<dyn elicitation::emit_code::EmitCode>,
                ::std::string::String,
            > {
                elicitation::serde_json::from_value::<$T>(v)
                    .map(|p| {
                        ::std::boxed::Box::new(p)
                            as ::std::boxed::Box<dyn elicitation::emit_code::EmitCode>
                    })
                    .map_err(|e| e.to_string())
            }
            elicitation::inventory::submit! {
                elicitation::emit_code::EmitEntry {
                    tool: $tool,
                    crate_name: env!("CARGO_PKG_NAME"),
                    constructor: __emit_constructor,
                }
            }
        };
    };
}

/// Convert a value to a Rust source expression that constructs it.
///
/// Unlike [`EmitCode`] which for workflow step types emits a full statement
/// sequence, `ToCodeLiteral` emits a single *expression* that reproduces this
/// value. Used by `#[elicit_tool]`-generated `impl EmitCode` blocks to bind
/// field values.
pub trait ToCodeLiteral {
    /// Return a `TokenStream` containing a single Rust expression whose
    /// evaluation produces a value equal to `self`.
    fn to_code_literal(&self) -> TokenStream;

    /// Token stream for the concrete type name (used to annotate `None::<T>`).
    ///
    /// The default returns `_`, which works when context provides enough
    /// inference — but for `Option<T>` `None` cases, a concrete type avoids
    /// "type annotations needed" errors.
    fn type_tokens() -> TokenStream
    where
        Self: Sized,
    {
        quote::quote! { _ }
    }
}

/// Trait-based escape hatch for handlers the rewriter cannot auto-derive.
///
/// Implement this on a zero-sized type and annotate the handler with
/// `#[elicit_tool(emit = MyType)]`. The macro generates an [`EmitCode`] impl
/// that delegates `emit_code` to `MyType` and derives `crate_deps` automatically
/// from the crate's `Cargo.toml`.
///
/// # Example
///
/// ```rust,ignore
/// struct FetchJsonEmit;
/// impl CustomEmit<FetchJsonParams> for FetchJsonEmit {
///     fn emit_code(params: &FetchJsonParams) -> proc_macro2::TokenStream {
///         let url = params.url.to_code_literal();
///         quote::quote! { /* ... */ }
///     }
/// }
/// ```
pub trait CustomEmit<P> {
    /// Emit the Rust token stream for this step, given concrete params.
    fn emit_code(params: &P) -> TokenStream;
}

/// A type that knows how to recover itself as Rust source code.
///
/// Two roles:
///
/// - **Value emission** (primitives, std types): emit the literal that produces
///   this value. `42i32` emits `42i32`. `"hello"` emits `"hello".to_string()`.
/// - **Step emission** (workflow params): emit the full typestate sequence this
///   params struct drives. `ParseFocusParams` emits `RawJson::new → .parse() →
///   .focus() → .extract()`.
///
/// Implement this trait for any type whose construction should be recoverable
/// as part of an emitted binary.
pub trait EmitCode {
    /// Emit the Rust token stream for this item.
    ///
    /// The emitted code runs in an `async` context with `?` available.
    /// For value emission, emit a single expression. For step emission,
    /// emit a statement sequence.
    fn emit_code(&self) -> TokenStream;

    /// Crate dependencies required by the emitted code.
    ///
    /// The default impl returns an empty vec (for primitive/std types that
    /// need no external deps). Override for types that emit calls into
    /// workspace crates.
    fn crate_deps(&self) -> Vec<CrateDep> {
        vec![]
    }

    /// Whether this step's emitted code shares the outer function scope with
    /// adjacent steps.
    ///
    /// When `false` (default), `BinaryScaffold` wraps the step in `{ }` so
    /// local variables and type-inference contexts stay isolated.
    ///
    /// When `true`, the step is emitted directly into the function body — its
    /// bindings (e.g. `let pool = ...`) are visible to subsequent steps.
    /// Use this for workflow steps that intentionally pass state through
    /// variable names (e.g. sqlx `connect` → `execute` → `begin` → `commit`).
    fn shared_scope(&self) -> bool {
        false
    }
}

/// A pre-rendered token stream fragment received across an MCP boundary.
///
/// Emit tools return source fragments as plain strings.  When an agent passes
/// those strings to an [`AssembleParams`](crate::emit_code) step (or nests
/// one fragment inside another tool's parameters), this wrapper parses the
/// string back into a live [`TokenStream`] so it can participate in
/// [`BinaryScaffold`] assembly.
///
/// # Example
///
/// ```rust
/// use elicitation::emit_code::{EmitCode, RawFragment};
///
/// let fragment = RawFragment("format!(\"x = {}\", value)".into());
/// let ts = fragment.emit_code();
/// assert!(!ts.is_empty());
/// ```
#[derive(Debug, Clone)]
pub struct RawFragment(pub String);

impl EmitCode for RawFragment {
    fn emit_code(&self) -> TokenStream {
        self.0
            .parse()
            .unwrap_or_else(|_| quote::quote!(/* fragment parse error */))
    }

    fn crate_deps(&self) -> Vec<CrateDep> {
        vec![]
    }
}

/// A Cargo dependency descriptor with pinned version.
///
/// Each `EmitCode` impl that calls into a workspace crate returns `CrateDep`
/// entries so the scaffold can generate a correct `Cargo.toml`.
///
/// Versions are pinned by the impl author (co-located with the `EmitCode`
/// impl) — we know the correct versions because they are our workspace.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CrateDep {
    /// Crate name as it appears in `Cargo.toml` (e.g. `"elicit_serde_json"`).
    pub name: &'static str,
    /// Semver version string (e.g. `"0.8"`).
    pub version: &'static str,
    /// Optional feature flags (e.g. `&["full"]`).
    pub features: &'static [&'static str],
}

impl CrateDep {
    /// Construct a dependency with no extra features.
    pub const fn new(name: &'static str, version: &'static str) -> Self {
        Self {
            name,
            version,
            features: &[],
        }
    }

    /// Construct a dependency with feature flags.
    pub const fn with_features(
        name: &'static str,
        version: &'static str,
        features: &'static [&'static str],
    ) -> Self {
        Self {
            name,
            version,
            features,
        }
    }

    /// Render as a TOML dependency line.
    ///
    /// ```rust
    /// use elicitation::emit_code::CrateDep;
    /// let dep = CrateDep::new("elicit_serde_json", "0.8");
    /// assert_eq!(dep.to_toml_line(), r#"elicit_serde_json = "0.8""#);
    /// ```
    pub fn to_toml_line(&self) -> String {
        if self.features.is_empty() {
            format!(r#"{} = "{}""#, self.name, self.version)
        } else {
            let feats = self
                .features
                .iter()
                .map(|f| format!(r#""{}""#, f))
                .collect::<Vec<_>>()
                .join(", ");
            format!(
                r#"{} = {{ version = "{}", features = [{}] }}"#,
                self.name, self.version, feats
            )
        }
    }
}

// ── Blanket impl for ToTokens ─────────────────────────────────────────────────

/// Value-emission impl for primitive types via [`quote::ToTokens`].
///
/// We enumerate these explicitly (rather than a blanket `impl<T: ToTokens>`)
/// to avoid conflicts with our specific impls for `Vec`, `Option`, `PathBuf`, etc.
macro_rules! impl_emit_totokens {
    ($($T:ty),+ $(,)?) => {
        $(
            impl EmitCode for $T {
                fn emit_code(&self) -> TokenStream {
                    let mut ts = TokenStream::new();
                    quote::ToTokens::to_tokens(self, &mut ts);
                    ts
                }
            }
        )+
    };
}

impl_emit_totokens!(
    bool, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize, f32, f64, char, String,
);

// ── BinaryScaffold ────────────────────────────────────────────────────────────

/// A sequence of [`EmitCode`] steps wrapped in a `#[tokio::main]` binary scaffold.
///
/// Assembles multiple steps (each emitting async statement sequences) into a
/// complete, compilable Rust program with `main()`, optional tracing init, and
/// correct `Cargo.toml` dependencies.
///
/// # Example
///
/// ```rust,ignore
/// let scaffold = BinaryScaffold::new(vec![Box::new(step1), Box::new(step2)], true);
/// let source: String = scaffold.to_source(); // pretty-printed Rust
/// scaffold.emit_to_disk(std::path::Path::new("./output"))?;
/// ```
pub struct BinaryScaffold {
    steps: Vec<Box<dyn EmitCode>>,
    with_tracing: bool,
    /// When set, `elicit_*` / `elicitation` deps are emitted as path deps
    /// instead of crates.io version refs — enabling pre-publish dev/test.
    ///
    /// Falls back to the `ELICIT_WORKSPACE_ROOT` environment variable when
    /// `None`. Integrates cleanly with crates like `config` that manage env.
    workspace_root: Option<std::path::PathBuf>,
}

impl BinaryScaffold {
    /// Create a new scaffold from ordered steps.
    ///
    /// - `steps`: Ordered list of [`EmitCode`] items. Each step's emitted code
    ///   runs sequentially inside `main()`.
    /// - `with_tracing`: If true, inserts `tracing_subscriber::fmt::init();`
    ///   at the top of `main()`.
    pub fn new(steps: Vec<Box<dyn EmitCode>>, with_tracing: bool) -> Self {
        Self {
            steps,
            with_tracing,
            workspace_root: None,
        }
    }

    /// Override elicit workspace crates with local path deps instead of
    /// crates.io version strings.
    ///
    /// Use this during development / pre-publish testing so the emitted
    /// `Cargo.toml` resolves against your local checkout rather than the
    /// registry. Falls back to the `ELICIT_WORKSPACE_ROOT` env var when not
    /// set explicitly.
    pub fn with_workspace_root(mut self, root: impl Into<std::path::PathBuf>) -> Self {
        self.workspace_root = Some(root.into());
        self
    }

    /// Resolve the effective workspace root: explicit field → env var → None.
    fn resolved_workspace_root(&self) -> Option<std::path::PathBuf> {
        self.workspace_root
            .clone()
            .or_else(|| std::env::var("ELICIT_WORKSPACE_ROOT").ok().map(Into::into))
    }

    /// Collect all crate dependencies from all steps, deduplicated by name.
    ///
    /// When two steps declare the same crate name, the first declaration wins.
    /// Always includes `tokio` and `tracing-subscriber` (required by scaffold).
    pub fn all_deps(&self) -> Vec<CrateDep> {
        let mut seen = std::collections::HashSet::new();
        let mut deps = Vec::new();

        // Scaffold always needs these
        let scaffold_deps = [
            CrateDep::with_features("tokio", "1", &["full"]),
            CrateDep::new("tracing-subscriber", "0.3"),
            CrateDep::new("tracing", "0.1"),
        ];
        for dep in scaffold_deps {
            if seen.insert(dep.name) {
                deps.push(dep);
            }
        }

        for step in &self.steps {
            for dep in step.crate_deps() {
                if seen.insert(dep.name) {
                    deps.push(dep);
                }
            }
        }
        deps
    }

    /// Emit the raw token stream for the full `main.rs`.
    pub fn render(&self) -> TokenStream {
        let step_tokens: Vec<TokenStream> = self
            .steps
            .iter()
            .map(|s| {
                let code = s.emit_code();
                if s.shared_scope() {
                    // Steps that pass state (like `let pool`) to later steps
                    // must be emitted directly into the function body. The
                    // trailing `;` ensures a trailing expression is a statement.
                    quote::quote! { #code ; }
                } else {
                    // Wrap in a block for scope + type-inference isolation.
                    // This lets steps that end in `Ok(...)` work correctly.
                    quote::quote! { { #code } }
                }
            })
            .collect();

        let tracing_init = if self.with_tracing {
            quote::quote! { tracing_subscriber::fmt::init(); }
        } else {
            TokenStream::new()
        };

        // Wildcard imports for workflow crates so their types (e.g. UnvalidatedUrl,
        // both, Established) are in scope without fully-qualified paths.
        // `elicitation` uses a sub-module import to avoid shadowing workflow
        // crate prop structs that share names with elicitation verification types
        // (e.g. `elicit_reqwest::UrlValid` vs `elicitation::UrlValid`).
        let mut use_stmts: Vec<TokenStream> = self
            .all_deps()
            .into_iter()
            .filter(|d| d.name.starts_with("elicit"))
            .map(|d| {
                let krate: TokenStream = d.name.parse().expect("valid ident");
                if d.name == "elicitation" {
                    quote::quote! { use #krate::contracts::*; }
                } else {
                    quote::quote! { use #krate::*; }
                }
            })
            .collect();

        // When `reqwest` is a direct dep (e.g. from emit_ctx substitutions that
        // reference `reqwest::Client::new()`), bring `reqwest::header::HeaderMap`
        // into scope so handler bodies that call `HeaderMap::new()` compile cleanly
        // (the `elicit_reqwest::HeaderMap` newtype is a different type).
        if self.all_deps().iter().any(|d| d.name == "reqwest") {
            use_stmts.push(quote::quote! { use reqwest::header::HeaderMap; });
        }

        // `elicit_reqwest` handlers use `HashMap` for headers/query params.
        if self.all_deps().iter().any(|d| d.name == "elicit_reqwest") {
            use_stmts.push(quote::quote! { use std::collections::HashMap; });
        }

        quote::quote! {
            #( #use_stmts )*
            #[tokio::main]
            async fn main() -> Result<(), Box<dyn std::error::Error>> {
                #tracing_init
                #( #step_tokens )*
                Ok(())
            }
        }
    }

    /// Emit formatted Rust source for `main.rs`.
    ///
    /// Uses `prettyplease` to format the token stream into readable source.
    /// Returns an error if the token stream is not valid Rust syntax.
    pub fn to_source(&self) -> Result<String, syn::Error> {
        let tokens = self.render();
        let file: syn::File = syn::parse2(tokens)?;
        Ok(prettyplease::unparse(&file))
    }

    /// Emit the `Cargo.toml` content as a string.
    ///
    /// When a workspace root is available (via [`Self::with_workspace_root`]
    /// or the `ELICIT_WORKSPACE_ROOT` env var), any `elicit_*` / `elicitation*`
    /// dep is emitted as `{ path = "<root>/crates/<name>" }` instead of a
    /// crates.io version string. All other deps use their declared versions.
    pub fn to_cargo_toml(&self, package_name: &str) -> String {
        let ws_root = self.resolved_workspace_root();
        let deps = self.all_deps();
        let dep_lines: String = deps
            .iter()
            .map(|d| {
                let line = if let Some(ref root) = ws_root {
                    if d.name == "elicitation"
                        || d.name.starts_with("elicit_")
                        || d.name.starts_with("elicitation_")
                    {
                        let path = root.join("crates").join(d.name);
                        // Use forward slashes — TOML treats `\` as an escape
                        // character, so Windows paths would be invalid otherwise.
                        let path_str = path.to_string_lossy().replace('\\', "/");
                        format!(r#"{} = {{ path = "{}" }}"#, d.name, path_str)
                    } else {
                        d.to_toml_line()
                    }
                } else {
                    d.to_toml_line()
                };
                format!("{line}\n")
            })
            .collect();

        format!(
            r#"[package]
name = "{}"
version = "0.1.0"
edition = "2021"

# Prevent cargo from treating this as a member of any parent workspace.
[workspace]

[dependencies]
{}
"#,
            package_name, dep_lines
        )
    }

    /// Write `src/main.rs` and `Cargo.toml` to `output_dir`.
    ///
    /// Creates the directory structure if it does not exist.
    /// Returns the path to `src/main.rs` on success.
    pub fn emit_to_disk(
        &self,
        output_dir: &std::path::Path,
        package_name: &str,
    ) -> Result<std::path::PathBuf, EmitError> {
        let src_dir = output_dir.join("src");
        std::fs::create_dir_all(&src_dir)?;

        let source = self.to_source().map_err(EmitError::Syntax)?;
        let main_rs = src_dir.join("main.rs");
        std::fs::write(&main_rs, &source)?;

        let cargo_toml = output_dir.join("Cargo.toml");
        std::fs::write(&cargo_toml, self.to_cargo_toml(package_name))?;

        Ok(main_rs)
    }
}

// ── Artifact compilation ──────────────────────────────────────────────────────

/// Compile the generated project with `cargo build --release`.
///
/// Returns the path to the compiled binary on success, or a [`CompileError`]
/// containing stderr output on failure.
pub fn compile(project_dir: &std::path::Path) -> Result<std::path::PathBuf, CompileError> {
    let output = std::process::Command::new("cargo")
        .args(["build", "--release"])
        .current_dir(project_dir)
        .output()
        .map_err(|e| CompileError::Io(e.to_string()))?;

    if output.status.success() {
        // Conventional release binary location
        let binary = project_dir.join("target/release").join(
            project_dir
                .file_name()
                .unwrap_or(std::ffi::OsStr::new("generated_workflow")),
        );
        Ok(binary)
    } else {
        Err(CompileError::CargoFailed(
            String::from_utf8_lossy(&output.stderr).into_owned(),
        ))
    }
}

// ── Error types ───────────────────────────────────────────────────────────────

/// Error emitting source to disk.
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum EmitError {
    /// The emitted token stream was not valid Rust syntax.
    #[display("Syntax error in emitted code: {}", _0)]
    Syntax(#[error(not(source))] syn::Error),
    /// File system error writing source or Cargo.toml.
    #[display("IO error: {}", _0)]
    Io(#[error(not(source))] std::io::Error),
}

impl From<std::io::Error> for EmitError {
    fn from(e: std::io::Error) -> Self {
        EmitError::Io(e)
    }
}

/// Error compiling the generated project.
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum CompileError {
    /// `cargo build` exited with non-zero status. Contains stderr.
    #[display("Compilation failed:\n{}", _0)]
    CargoFailed(#[error(not(source))] String),
    /// Could not spawn the `cargo` process.
    #[display("Could not launch cargo: {}", _0)]
    Io(#[error(not(source))] String),
}

// ── Specific impls for std types not covered by blanket ───────────────────────

/// `Vec<T>` emits `vec![elem0, elem1, ...]`
impl<T: EmitCode> EmitCode for Vec<T> {
    fn emit_code(&self) -> TokenStream {
        let elems: Vec<TokenStream> = self.iter().map(|e| e.emit_code()).collect();
        quote::quote! { vec![ #( #elems ),* ] }
    }
}

/// `Option<T>` emits `Some(inner)` or `None`
impl<T: EmitCode> EmitCode for Option<T> {
    fn emit_code(&self) -> TokenStream {
        match self {
            Some(inner) => {
                let inner_ts = inner.emit_code();
                quote::quote! { Some(#inner_ts) }
            }
            None => quote::quote! { None },
        }
    }
}

/// `PathBuf` emits `std::path::PathBuf::from("...")`
impl EmitCode for std::path::PathBuf {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string_lossy();
        let s = s.as_ref();
        quote::quote! { std::path::PathBuf::from(#s) }
    }
}

/// `std::time::Duration` emits `std::time::Duration::from_nanos(n)`
impl EmitCode for std::time::Duration {
    fn emit_code(&self) -> TokenStream {
        let nanos = self.as_nanos() as u64;
        quote::quote! { std::time::Duration::from_nanos(#nanos) }
    }
}

// Tuples — macro to stamp out (A, B), (A, B, C), (A, B, C, D)
macro_rules! impl_emit_tuple {
    ( $( $T:ident ),+ ; $( $idx:tt ),+ ) => {
        impl< $( $T: EmitCode ),+ > EmitCode for ( $( $T, )+ ) {
            fn emit_code(&self) -> TokenStream {
                paste::paste! {
                    $( let [<$T:lower _val>] = self.$idx.emit_code(); )+
                    quote::quote! { ( $( #[<$T:lower _val>] ),+ ) }
                }
            }
        }
    };
}

impl_emit_tuple!(A, B; 0, 1);
impl_emit_tuple!(A, B, C; 0, 1, 2);
impl_emit_tuple!(A, B, C, D; 0, 1, 2, 3);

// ── Feature-gated impls ───────────────────────────────────────────────────────

/// `serde_json::Value` emits `serde_json::json!(...)` via the literal repr.
#[cfg(feature = "serde_json")]
impl EmitCode for serde_json::Value {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! {
            serde_json::from_str(#s).expect("valid json literal")
        }
    }
}

/// `url::Url` emits `url::Url::parse("...").unwrap()`
#[cfg(feature = "url")]
impl EmitCode for url::Url {
    fn emit_code(&self) -> TokenStream {
        let s = self.as_str();
        quote::quote! { url::Url::parse(#s).expect("valid URL") }
    }
}

/// `uuid::Uuid` emits `uuid::Uuid::parse_str("...").unwrap()`
#[cfg(feature = "uuid")]
impl EmitCode for uuid::Uuid {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! { uuid::Uuid::parse_str(#s).expect("valid UUID") }
    }
}

/// `std::net::IpAddr` emits `"...".parse::<std::net::IpAddr>().unwrap()`
impl EmitCode for std::net::IpAddr {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! { #s.parse::<std::net::IpAddr>().expect("valid IP") }
    }
}

/// `std::net::Ipv4Addr`
impl EmitCode for std::net::Ipv4Addr {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! { #s.parse::<std::net::Ipv4Addr>().expect("valid IPv4") }
    }
}

/// `std::net::Ipv6Addr`
impl EmitCode for std::net::Ipv6Addr {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! { #s.parse::<std::net::Ipv6Addr>().expect("valid IPv6") }
    }
}

/// `chrono::DateTime<Utc>` emits RFC 3339 parse
#[cfg(feature = "chrono")]
impl EmitCode for chrono::DateTime<chrono::Utc> {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_rfc3339();
        quote::quote! {
            chrono::DateTime::parse_from_rfc3339(#s)
                .expect("valid RFC3339 datetime")
                .with_timezone(&chrono::Utc)
        }
    }
}

/// `chrono::NaiveDateTime`
#[cfg(feature = "chrono")]
impl EmitCode for chrono::NaiveDateTime {
    fn emit_code(&self) -> TokenStream {
        let s = self.format("%Y-%m-%dT%H:%M:%S%.f").to_string();
        quote::quote! {
            chrono::NaiveDateTime::parse_from_str(#s, "%Y-%m-%dT%H:%M:%S%.f")
                .expect("valid NaiveDateTime")
        }
    }
}

/// `time::OffsetDateTime`
#[cfg(feature = "time")]
impl EmitCode for time::OffsetDateTime {
    fn emit_code(&self) -> TokenStream {
        let s = self
            .format(&time::format_description::well_known::Rfc3339)
            .unwrap_or_default();
        quote::quote! {
            time::OffsetDateTime::parse(#s, &time::format_description::well_known::Rfc3339)
                .expect("valid OffsetDateTime")
        }
    }
}

/// `jiff::Timestamp`
#[cfg(feature = "jiff")]
impl EmitCode for jiff::Timestamp {
    fn emit_code(&self) -> TokenStream {
        let s = self.to_string();
        quote::quote! {
            #s.parse::<jiff::Timestamp>().expect("valid Timestamp")
        }
    }
}

/// `reqwest::StatusCode`
#[cfg(feature = "reqwest")]
impl EmitCode for reqwest::StatusCode {
    fn emit_code(&self) -> TokenStream {
        let n = self.as_u16();
        quote::quote! {
            reqwest::StatusCode::from_u16(#n).expect("valid status code")
        }
    }
}

// ── ToCodeLiteral impls ───────────────────────────────────────────────────────

macro_rules! impl_to_code_literal_totokens {
    ($($T:ty),+ $(,)?) => {
        $(
            impl ToCodeLiteral for $T {
                fn to_code_literal(&self) -> TokenStream {
                    let mut ts = TokenStream::new();
                    quote::ToTokens::to_tokens(self, &mut ts);
                    ts
                }
                fn type_tokens() -> TokenStream {
                    quote::quote! { $T }
                }
            }
        )+
    };
}

impl_to_code_literal_totokens!(
    bool, i8, i16, i32, i64, i128, u8, u16, u32, u64, u128, usize, isize, f32, f64, char,
);

impl ToCodeLiteral for String {
    fn to_code_literal(&self) -> TokenStream {
        let s = self.as_str();
        quote::quote! { #s.to_string() }
    }
    fn type_tokens() -> TokenStream {
        quote::quote! { String }
    }
}

impl<T: ToCodeLiteral> ToCodeLiteral for Option<T> {
    fn to_code_literal(&self) -> TokenStream {
        match self {
            Some(v) => {
                let inner = v.to_code_literal();
                quote::quote! { ::std::option::Option::Some(#inner) }
            }
            None => {
                let t = <T as ToCodeLiteral>::type_tokens();
                quote::quote! { None::<#t> }
            }
        }
    }
}

impl<T: ToCodeLiteral> ToCodeLiteral for Vec<T> {
    fn type_tokens() -> TokenStream {
        let t = <T as ToCodeLiteral>::type_tokens();
        quote::quote! { ::std::vec::Vec<#t> }
    }

    fn to_code_literal(&self) -> TokenStream {
        let elems: Vec<_> = self.iter().map(|v| v.to_code_literal()).collect();
        quote::quote! { ::std::vec![#(#elems),*] }
    }
}

impl<T: ToCodeLiteral, const N: usize> ToCodeLiteral for [T; N] {
    fn type_tokens() -> TokenStream {
        let t = <T as ToCodeLiteral>::type_tokens();
        let n = proc_macro2::Literal::usize_suffixed(N);
        quote::quote! { [#t; #n] }
    }

    fn to_code_literal(&self) -> TokenStream {
        let elements: Vec<_> = self.iter().map(|e| e.to_code_literal()).collect();
        quote::quote! { [#(#elements),*] }
    }
}

impl<V: ToCodeLiteral> ToCodeLiteral for std::collections::HashMap<String, V> {
    fn type_tokens() -> TokenStream {
        let v = <V as ToCodeLiteral>::type_tokens();
        quote::quote! { ::std::collections::HashMap<::std::string::String, #v> }
    }

    fn to_code_literal(&self) -> TokenStream {
        let entries: Vec<_> = self
            .iter()
            .map(|(k, v)| {
                let v_ts = v.to_code_literal();
                quote::quote! { (#k.to_string(), #v_ts) }
            })
            .collect();
        quote::quote! {
            [#(#entries),*].into_iter().collect::<::std::collections::HashMap<_, _>>()
        }
    }
}

impl<T: ToCodeLiteral> ToCodeLiteral for Box<T> {
    fn type_tokens() -> TokenStream {
        let inner = <T as ToCodeLiteral>::type_tokens();
        quote::quote! { ::std::boxed::Box<#inner> }
    }

    fn to_code_literal(&self) -> TokenStream {
        let inner = (**self).to_code_literal();
        quote::quote! { ::std::boxed::Box::new(#inner) }
    }
}

impl<A: ToCodeLiteral, B: ToCodeLiteral> ToCodeLiteral for (A, B) {
    fn type_tokens() -> TokenStream {
        let a = <A as ToCodeLiteral>::type_tokens();
        let b = <B as ToCodeLiteral>::type_tokens();
        quote::quote! { (#a, #b) }
    }

    fn to_code_literal(&self) -> TokenStream {
        let a = self.0.to_code_literal();
        let b = self.1.to_code_literal();
        quote::quote! { (#a, #b) }
    }
}

// ToCodeLiteral for std types that already have EmitCode: delegate directly.

impl ToCodeLiteral for std::net::IpAddr {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

impl ToCodeLiteral for std::net::Ipv4Addr {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

impl ToCodeLiteral for std::net::Ipv6Addr {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

impl ToCodeLiteral for std::path::PathBuf {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

impl ToCodeLiteral for std::time::Duration {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "serde_json")]
impl ToCodeLiteral for serde_json::Value {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "url")]
impl ToCodeLiteral for url::Url {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "uuid")]
impl ToCodeLiteral for uuid::Uuid {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "chrono")]
impl ToCodeLiteral for chrono::DateTime<chrono::Utc> {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "chrono")]
impl ToCodeLiteral for chrono::NaiveDateTime {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "time")]
impl ToCodeLiteral for time::OffsetDateTime {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "jiff")]
impl ToCodeLiteral for jiff::Timestamp {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

#[cfg(feature = "reqwest")]
impl ToCodeLiteral for reqwest::StatusCode {
    fn to_code_literal(&self) -> TokenStream {
        EmitCode::emit_code(self)
    }
}

// ── Atomic types ─────────────────────────────────────────────────────────────

/// Generate `ToCodeLiteral` for an atomic type by loading the current value
/// and emitting `::std::sync::atomic::Atomic*::new(value)`.
macro_rules! impl_atomic_to_code_literal {
    ($($atomic:ident => $prim:ty),+ $(,)?) => {
        $(
            impl ToCodeLiteral for ::std::sync::atomic::$atomic {
                fn to_code_literal(&self) -> TokenStream {
                    use ::std::sync::atomic::Ordering;
                    let val = self.load(Ordering::SeqCst);
                    let val_lit = <$prim as ToCodeLiteral>::to_code_literal(&val);
                    let ty: TokenStream =
                        concat!("::std::sync::atomic::", stringify!($atomic))
                            .parse()
                            .expect("valid atomic type path");
                    quote::quote! { #ty::new(#val_lit) }
                }

                fn type_tokens() -> TokenStream {
                    concat!("::std::sync::atomic::", stringify!($atomic))
                        .parse()
                        .expect("valid atomic type path")
                }
            }
        )+
    };
}

impl_atomic_to_code_literal!(
    AtomicBool => bool,
    AtomicI8 => i8,
    AtomicI16 => i16,
    AtomicI32 => i32,
    AtomicI64 => i64,
    AtomicIsize => isize,
    AtomicU8 => u8,
    AtomicU16 => u16,
    AtomicU32 => u32,
    AtomicU64 => u64,
    AtomicUsize => usize,
);