klieo-macros 3.3.0

Procedural macros for the klieo agent framework: #[tool] derives a Tool impl from an async fn.
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
//! Parse the `#[tool(...)]` attribute + the decorated function.

use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{Error, Expr, ExprLit, FnArg, Ident, ItemFn, Lit, LitStr, Meta, ReturnType, Token, Type};

/// Parsed `#[tool(...)]` attribute. `description`/`rename` are optional
/// name-value keys; `effectful`/`redacts_audit` are optional bare flags that
/// generate the matching `Tool` trait overrides. Codegen falls back to the
/// function's first doc-comment line if `description` is absent, and emits a
/// compile error when neither it nor a doc line is present.
#[derive(Default)]
pub(crate) struct ToolAttr {
    pub description: Option<LitStr>,
    pub rename: Option<LitStr>,
    pub effectful: bool,
    pub redacts_audit: bool,
}

// Manual Debug so test `unwrap_err()` compiles. LitStr doesn't derive Debug,
// but we only need enough for the error path.
impl std::fmt::Debug for ToolAttr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolAttr")
            .field("description", &self.description.as_ref().map(|s| s.value()))
            .field("rename", &self.rename.as_ref().map(|s| s.value()))
            .field("effectful", &self.effectful)
            .field("redacts_audit", &self.redacts_audit)
            .finish()
    }
}

impl Parse for ToolAttr {
    fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
        let metas: Punctuated<Meta, Token![,]> = Punctuated::parse_terminated(input)?;
        let mut description: Option<LitStr> = None;
        let mut rename: Option<LitStr> = None;
        let mut effectful = false;
        let mut redacts_audit = false;
        for m in metas {
            match m {
                Meta::Path(path) => {
                    let key = path
                        .get_ident()
                        .ok_or_else(|| Error::new_spanned(&path, "expected identifier flag"))?;
                    if key == "effectful" {
                        effectful = true;
                    } else if key == "redacts_audit" {
                        redacts_audit = true;
                    } else {
                        return Err(Error::new(
                            key.span(),
                            format!("unknown #[tool] flag {key}"),
                        ));
                    }
                }
                Meta::NameValue(nv) => {
                    let key = nv
                        .path
                        .get_ident()
                        .ok_or_else(|| Error::new_spanned(&nv.path, "expected identifier key"))?
                        .clone();
                    let value_lit = match nv.value {
                        Expr::Lit(ExprLit {
                            lit: Lit::Str(s), ..
                        }) => s,
                        _ => {
                            return Err(Error::new(
                                key.span(),
                                format!("`{key}` must be a string literal"),
                            ));
                        }
                    };
                    if key == "description" {
                        description = Some(value_lit);
                    } else if key == "rename" {
                        rename = Some(value_lit);
                    } else {
                        return Err(Error::new(
                            key.span(),
                            format!("unknown #[tool] attribute key {key}"),
                        ));
                    }
                }
                Meta::List(list) => {
                    return Err(Error::new_spanned(
                        &list,
                        "list-form #[tool] attributes are not supported",
                    ));
                }
            }
        }
        Ok(ToolAttr {
            description,
            rename,
            effectful,
            redacts_audit,
        })
    }
}

/// Extract the first non-empty trimmed line from `#[doc = "..."]`
/// attributes on the function. `///` comments are canonicalised to this
/// form by rustc before macros see them.
pub(crate) fn first_doc_line(attrs: &[syn::Attribute]) -> Option<String> {
    for a in attrs {
        if !a.path().is_ident("doc") {
            continue;
        }
        let nv: syn::MetaNameValue = match a.meta.require_name_value() {
            Ok(nv) => nv.clone(),
            Err(_) => continue,
        };
        if let syn::Expr::Lit(syn::ExprLit {
            lit: Lit::Str(s), ..
        }) = nv.value
        {
            let trimmed = s.value().trim().to_string();
            if !trimmed.is_empty() {
                return Some(trimmed);
            }
        }
    }
    None
}

/// One non-ctx argument extracted from the function signature.
// Fields consumed by Task 3 codegen.
#[allow(dead_code)]
pub(crate) struct ToolArg {
    pub ident: Ident,
    pub ty: Type,
}

/// Parsed function signature.
// Fields consumed by Task 3 codegen.
#[allow(dead_code)]
pub(crate) struct ToolFn {
    pub item_fn: ItemFn,
    pub fn_name: Ident,
    pub args: Vec<ToolArg>,
    pub output_ty: Type,
}

// Manual Debug so test `unwrap_err()` compiles without requiring all
// syn types to be Debug. We only surface the fn name.
impl std::fmt::Debug for ToolFn {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ToolFn")
            .field("fn_name", &self.fn_name.to_string())
            .finish_non_exhaustive()
    }
}

impl ToolFn {
    pub fn parse(item_fn: ItemFn) -> syn::Result<Self> {
        let sig = &item_fn.sig;

        if sig.asyncness.is_none() {
            return Err(Error::new_spanned(
                sig.fn_token,
                "#[tool] requires an async fn",
            ));
        }

        let fn_name = sig.ident.clone();

        // Guard: empty / leading-underscore names produce surprising
        // PascalCase output (e.g. `_hidden` → `Hidden`, dropping the
        // underscore intent). Reject explicitly.
        let name_str = fn_name.to_string();
        if name_str.is_empty() || name_str.starts_with('_') {
            return Err(Error::new_spanned(
                &fn_name,
                "#[tool] does not support empty or leading-underscore function names; use a public-style identifier (e.g. `greet` not `_greet`)",
            ));
        }

        // First arg must be a typed pattern (the ctx). We skip it
        // positionally and don't enforce the exact type — the
        // generated trampoline uses whatever type the user wrote.
        let inputs = &sig.inputs;
        if inputs.is_empty() {
            return Err(Error::new_spanned(
                sig,
                "#[tool] requires at least one arg (ctx: &ToolCtx)",
            ));
        }

        let mut iter = inputs.iter();
        match iter.next().unwrap() {
            FnArg::Receiver(_) => {
                return Err(Error::new_spanned(
                    sig,
                    "#[tool] cannot decorate methods (use a free fn)",
                ));
            }
            FnArg::Typed(_) => {}
        }

        let mut args = Vec::new();
        for fn_arg in iter {
            match fn_arg {
                FnArg::Receiver(_) => {
                    return Err(Error::new_spanned(
                        fn_arg,
                        "#[tool] cannot decorate methods (use a free fn)",
                    ));
                }
                FnArg::Typed(pat_ty) => {
                    let ident = match &*pat_ty.pat {
                        syn::Pat::Ident(pat_ident) => pat_ident.ident.clone(),
                        _ => {
                            return Err(Error::new_spanned(
                                &pat_ty.pat,
                                "#[tool] requires plain `name: Type` arg patterns",
                            ));
                        }
                    };
                    args.push(ToolArg {
                        ident,
                        ty: (*pat_ty.ty).clone(),
                    });
                }
            }
        }

        let output_ty = match &sig.output {
            ReturnType::Default => {
                return Err(Error::new_spanned(
                    &sig.output,
                    "#[tool] requires a return type Result<T, ToolError>",
                ));
            }
            ReturnType::Type(_, ty) => (**ty).clone(),
        };

        Ok(ToolFn {
            item_fn,
            fn_name,
            args,
            output_ty,
        })
    }
}

/// Combined parsed input.
// Fields consumed by Task 3 codegen.
#[allow(dead_code)]
pub(crate) struct ToolDecl {
    pub attr: ToolAttr,
    pub func: ToolFn,
}

#[cfg(test)]
mod tests {
    use super::*;
    use quote::quote;
    use syn::parse2;

    #[test]
    fn parses_simple_function() {
        let f: ItemFn = parse2(quote! {
            async fn greet(ctx: &ToolCtx, name: String) -> Result<String, ToolError> {
                Ok(format!("hi {name}"))
            }
        })
        .unwrap();
        let parsed = ToolFn::parse(f).unwrap();
        assert_eq!(parsed.fn_name.to_string(), "greet");
        assert_eq!(parsed.args.len(), 1);
        assert_eq!(parsed.args[0].ident.to_string(), "name");
    }

    #[test]
    fn rejects_non_async() {
        let f: ItemFn = parse2(quote! {
            fn greet(ctx: &ToolCtx, name: String) -> Result<String, ToolError> { unimplemented!() }
        })
        .unwrap();
        let err = ToolFn::parse(f).unwrap_err();
        assert!(err.to_string().contains("async fn"));
    }

    #[test]
    fn rejects_no_return_type() {
        let f: ItemFn = parse2(quote! {
            async fn greet(ctx: &ToolCtx, name: String) {}
        })
        .unwrap();
        let err = ToolFn::parse(f).unwrap_err();
        assert!(err.to_string().contains("return type"));
    }

    #[test]
    fn rejects_no_args() {
        let f: ItemFn = parse2(quote! {
            async fn greet() -> Result<String, ToolError> { unimplemented!() }
        })
        .unwrap();
        let err = ToolFn::parse(f).unwrap_err();
        assert!(err.to_string().contains("at least one arg"));
    }

    #[test]
    fn parses_attr_description() {
        let attr_tokens: proc_macro2::TokenStream = quote! { description = "Greet a person" };
        let attr: ToolAttr = parse2(attr_tokens).unwrap();
        assert_eq!(attr.description.as_ref().unwrap().value(), "Greet a person");
        assert!(attr.rename.is_none());
    }

    #[test]
    fn rejects_unknown_key() {
        let attr_tokens: proc_macro2::TokenStream = quote! { unknown = "x" };
        let err: syn::Error = parse2::<ToolAttr>(attr_tokens).unwrap_err();
        assert!(err.to_string().contains("unknown #[tool] attribute key"));
    }

    #[test]
    fn parses_effectful_and_redacts_audit_flags() {
        let attr_tokens: proc_macro2::TokenStream =
            quote! { effectful, redacts_audit, description = "pays out" };
        let attr: ToolAttr = parse2(attr_tokens).unwrap();
        assert!(attr.effectful);
        assert!(attr.redacts_audit);
        assert_eq!(attr.description.as_ref().unwrap().value(), "pays out");
    }

    #[test]
    fn flags_default_false_when_absent() {
        let attr: ToolAttr = parse2(quote! { description = "read only" }).unwrap();
        assert!(!attr.effectful);
        assert!(!attr.redacts_audit);
    }

    #[test]
    fn rejects_unknown_flag() {
        let err = parse2::<ToolAttr>(quote! { bogus_flag }).unwrap_err();
        assert!(err.to_string().contains("unknown #[tool] flag"));
    }

    #[test]
    fn parses_attr_with_rename_and_no_description() {
        let attr_tokens: proc_macro2::TokenStream = quote! { rename = "explicitName" };
        let attr: ToolAttr = parse2(attr_tokens).unwrap();
        assert!(attr.description.is_none());
        assert_eq!(attr.rename.as_ref().unwrap().value(), "explicitName");
    }

    #[test]
    fn parses_attr_with_no_args() {
        // `#[tool]` alone — both description and rename absent. Parser
        // accepts this; codegen will require a doc comment to fall back on.
        let attr_tokens: proc_macro2::TokenStream = quote! {};
        let attr: ToolAttr = parse2(attr_tokens).unwrap();
        assert!(attr.description.is_none());
        assert!(attr.rename.is_none());
    }

    #[test]
    fn rejects_method_receiver() {
        let f: syn::Result<ItemFn> = parse2(quote! {
            async fn greet(self, name: String) -> Result<String, ToolError> {
                Ok(name)
            }
        });
        // Method-receiver fns might fail at syn parse OR at ToolFn::parse.
        // Both are acceptable — we just need to confirm rejection.
        match f {
            Ok(item_fn) => {
                let err = ToolFn::parse(item_fn).unwrap_err();
                assert!(err.to_string().contains("decorate methods"));
            }
            Err(_) => {
                // syn rejected at parse time; that's also a valid rejection path.
            }
        }
    }

    #[test]
    fn rejects_pattern_arg() {
        let f: ItemFn = parse2(quote! {
            async fn greet(ctx: &ToolCtx, (a, b): (u32, u32)) -> Result<String, ToolError> {
                Ok(String::new())
            }
        })
        .unwrap();
        let err = ToolFn::parse(f).unwrap_err();
        assert!(err.to_string().contains("plain"));
    }

    #[test]
    fn rejects_attr_with_non_string_description() {
        let attr_tokens: proc_macro2::TokenStream = quote! { description = 42 };
        let err = parse2::<ToolAttr>(attr_tokens).unwrap_err();
        assert!(err.to_string().contains("string literal"));
    }

    #[test]
    fn rejects_leading_underscore_fn_name() {
        let f: ItemFn = parse2(quote! {
            async fn _hidden(ctx: &ToolCtx, name: String) -> Result<String, ToolError> {
                Ok(name)
            }
        })
        .unwrap();
        let err = ToolFn::parse(f).unwrap_err();
        assert!(err.to_string().contains("leading-underscore"));
    }
}