alef 0.67.6

Opinionated polyglot binding generator for Rust libraries
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
//! Validates each fixture's effective `args` against the Rust function signature the
//! core IR declares for the call it resolves to.
//!
//! Every backend lowers a configured [`ArgMapping`] purely from its own `arg_type`
//! string and the fixture's `input` JSON — nothing checked the `name` on that mapping
//! against the actual Rust parameter it claims to fill, or that every required
//! parameter had an arg supplying it at all. A fixture naming a parameter the function
//! no longer takes, or omitting one it now requires, generates code that fails to
//! compile in every target language at once, with no diagnostic naming the fixture, the
//! arg, or the function responsible.
//!
//! This mirrors [`crate::e2e::validate_call_class`] and
//! [`crate::e2e::validate_call_result_type`] in shape, but resolves against
//! [`crate::e2e::codegen::call_ir::CallIr`] rather than re-deriving a lookup of its own —
//! that module already holds the "declared parameters for this call" answer every
//! backend's argument-type resolution consults (`TargetParams`), so this validator asks
//! the exact question codegen asks, not an approximation of it.
//!
//! ## Scope: what "the IR" licenses
//!
//! An unresolved call signature — no free function or IR-type method of that name, or
//! several same-named methods disagreeing (see `CallIr::signature`'s doc comment) —
//! licenses no claim at all, not "no function found." This is deliberate: a function
//! intentionally removed from the binding surface (`#[alef::skip]`, `#[alef::exclude]`,
//! a per-crate `exclude_functions` entry) is not necessarily removed from
//! `ApiSurface::functions` — see [`crate::core::ir::items::FunctionDef::binding_excluded`]
//! — but this validator does not attempt to independently re-derive every exclusion
//! surface itself (`[crates.exclude]`, per-language `exclude_functions`,
//! `[crates.skipped]`, per-crate `[workspace.crates."<name>"]` overrides — auditing which
//! of these actually reach `functions`/`type_defs` by the time they arrive here is
//! `alef-tasks#323`'s scope, not this one's). It scopes itself to two signals it *can*
//! confirm directly: an absent/ambiguous `CallIr::signature` (skip), and an explicit
//! `binding_excluded` flag on the resolved function/method (skip) — both licensing "say
//! nothing" rather than "say wrong."
//!
//! ## Severity
//!
//! Lands as [`Severity::Warning`]. See `crate::e2e::validate_call_module`'s "Severity"
//! section for the same rationale: promotion to `Error` is a decision for once a
//! consumer-fleet measurement shows the rule's finding rate is real bugs, not noise.

use super::validate::{Severity, ValidationError};
use crate::core::config::e2e::{ArgMapping, CallConfig, E2eConfig};
use crate::core::ir::{FunctionDef, ParamDef, TypeDef};
use crate::e2e::codegen::call_ir::CallIr;
use crate::e2e::fixture::Fixture;

/// Run [`validate_call_arg_signatures`] and log every diagnostic. See
/// `crate::e2e::validate_call_module::enforce_call_module_overrides`'s doc comment for
/// why this never bails today: every diagnostic here is [`Severity::Warning`].
pub fn enforce_call_arg_signatures(
    fixtures: &[Fixture],
    e2e_config: &E2eConfig,
    functions: &[FunctionDef],
    type_defs: &[TypeDef],
    languages: &[String],
) -> anyhow::Result<()> {
    let diagnostics = validate_call_arg_signatures(fixtures, e2e_config, functions, type_defs, languages);
    for diag in &diagnostics {
        tracing::warn!("{}: {}", diag.file, diag.message);
    }
    let errors: Vec<_> = diagnostics
        .iter()
        .filter(|diag| diag.severity == Severity::Error)
        .collect();
    if errors.is_empty() {
        return Ok(());
    }
    anyhow::bail!(
        "e2e call argument signature validation failed: {}",
        errors
            .iter()
            .map(|diag| format!("{}: {}", diag.file, diag.message))
            .collect::<Vec<_>>()
            .join("; ")
    );
}

/// Validate every fixture's effective `args` (its own `args`, or its resolved call's
/// `args` when the fixture declares none — see [`Fixture::resolved_args`]) against the
/// Rust parameter list the core IR declares for the call it resolves to.
///
/// Skipped entirely when both `functions` and `type_defs` are empty, mirroring
/// `validate_call_class_overrides`'s "absent IR licenses no claim" rule: several
/// legitimate callers pass an empty IR, and validating fixture args against a
/// deliberately incomplete registry would manufacture false positives rather than catch
/// a real drift.
pub fn validate_call_arg_signatures(
    fixtures: &[Fixture],
    e2e_config: &E2eConfig,
    functions: &[FunctionDef],
    type_defs: &[TypeDef],
    languages: &[String],
) -> Vec<ValidationError> {
    if functions.is_empty() && type_defs.is_empty() {
        return Vec::new();
    }
    let ir = CallIr { functions, type_defs };

    let mut errors = Vec::new();
    for fixture in fixtures {
        let call_config = e2e_config.resolve_call_for_fixture(
            fixture.call.as_deref(),
            &fixture.id,
            &fixture.resolved_category(),
            &fixture.tags,
            &fixture.input,
        );
        let Some(lookup_name) = canonical_lookup_name(call_config, languages) else {
            continue;
        };
        let Some(signature) = ir.signature(&lookup_name) else {
            continue;
        };
        if binding_excluded(&lookup_name, functions, type_defs) {
            continue;
        }
        let args = fixture.resolved_args(call_config);
        check_unknown_args(fixture, &lookup_name, args, signature.params, &mut errors);
        check_missing_required_params(fixture, &lookup_name, args, signature.params, &mut errors);
    }
    errors
}

/// The language-neutral Rust identity a call's `args` are declared against:
/// `CallConfig::function` when set, otherwise the first configured language's resolved
/// name — see `CallConfig::core_lookup_name`'s doc comment for why a per-language
/// override still resolves to one Rust-side identity. Returns `None` when no language
/// names a function at all (a call that exists only to carry per-fixture assertions
/// against another call's result, for instance).
fn canonical_lookup_name<'a>(call: &'a CallConfig, languages: &[String]) -> Option<std::borrow::Cow<'a, str>> {
    languages.iter().find_map(|language| call.core_lookup_name(language))
}

/// Whether the resolved free function or IR-type method named `name` is explicitly
/// marked excluded from the binding surface. See the module doc comment's "Scope"
/// section for why this is the one exclusion signal this validator confirms directly
/// rather than re-deriving the full exclusion-surface audit `alef-tasks#323` owns.
fn binding_excluded(name: &str, functions: &[FunctionDef], type_defs: &[TypeDef]) -> bool {
    if let Some(function) = functions.iter().find(|function| function.name == name) {
        return function.binding_excluded;
    }
    type_defs
        .iter()
        .flat_map(|type_def| type_def.methods.iter())
        .filter(|method| method.name == name)
        .all(|method| method.binding_excluded)
}

fn check_unknown_args(
    fixture: &Fixture,
    lookup_name: &str,
    args: &[ArgMapping],
    params: &[ParamDef],
    errors: &mut Vec<ValidationError>,
) {
    for arg in args {
        if params.iter().any(|param| param.name == arg.name) {
            continue;
        }
        errors.push(ValidationError {
            file: fixture.source.clone(),
            message: format!(
                "fixture '{}' arg '{}' names a parameter '{}' does not declare (call resolves to Rust \
                 function/method '{lookup_name}')",
                fixture.id, arg.name, lookup_name
            ),
            severity: Severity::Warning,
        });
    }
}

fn check_missing_required_params(
    fixture: &Fixture,
    lookup_name: &str,
    args: &[ArgMapping],
    params: &[ParamDef],
    errors: &mut Vec<ValidationError>,
) {
    for param in params {
        if param.optional || param.default.is_some() {
            continue;
        }
        if args.iter().any(|arg| arg.name == param.name) {
            continue;
        }
        errors.push(ValidationError {
            file: fixture.source.clone(),
            message: format!(
                "fixture '{}' does not supply required parameter '{}' of '{lookup_name}' (no arg, and the \
                 parameter has no default)",
                fixture.id, param.name
            ),
            severity: Severity::Warning,
        });
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::ir::{MethodDef, TypeRef};

    fn param(name: &str) -> ParamDef {
        ParamDef {
            name: name.to_string(),
            ty: TypeRef::String,
            ..ParamDef::default()
        }
    }

    fn optional_param(name: &str) -> ParamDef {
        ParamDef {
            optional: true,
            ..param(name)
        }
    }

    fn function(name: &str, params: Vec<ParamDef>) -> FunctionDef {
        FunctionDef {
            name: name.to_string(),
            params,
            return_type: TypeRef::String,
            ..FunctionDef::default()
        }
    }

    fn arg(name: &str, optional: bool) -> ArgMapping {
        ArgMapping {
            name: name.to_string(),
            field: format!("input.{name}"),
            arg_type: "string".to_string(),
            optional,
            owned: false,
            element_type: None,
            go_type: None,
            vec_inner_is_ref: false,
            trait_name: None,
        }
    }

    fn call_named(function: &str, args: Vec<ArgMapping>) -> CallConfig {
        CallConfig {
            function: function.to_string(),
            args,
            ..CallConfig::default()
        }
    }

    fn fixture_with_call(id: &str, call: Option<&str>) -> Fixture {
        Fixture {
            id: id.to_string(),
            call: call.map(str::to_string),
            source: format!("{id}.json"),
            ..Fixture::default()
        }
    }

    #[test]
    fn matching_args_pass() {
        let functions = vec![function("complete", vec![param("prompt"), optional_param("model")])];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false), arg("model", true)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 0, "expected no errors, got: {errors:?}");
    }

    #[test]
    fn an_arg_naming_a_removed_parameter_is_flagged() {
        let functions = vec![function("complete", vec![param("prompt")])];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false), arg("concurrency", true)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 1, "expected exactly one error, got: {errors:?}");
        assert_eq!(errors[0].severity, Severity::Warning);
        assert!(
            errors[0].message.contains("fixture 'basic' arg 'concurrency'"),
            "got: {}",
            errors[0].message
        );
        assert!(errors[0].message.contains("'complete'"), "got: {}", errors[0].message);
    }

    #[test]
    fn a_missing_required_parameter_is_flagged() {
        let functions = vec![function("complete", vec![param("prompt"), param("config")])];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 1, "expected exactly one error, got: {errors:?}");
        assert!(
            errors[0]
                .message
                .contains("fixture 'basic' does not supply required parameter 'config'"),
            "got: {}",
            errors[0].message
        );
    }

    #[test]
    fn a_missing_optional_parameter_is_not_flagged() {
        let functions = vec![function("complete", vec![param("prompt"), optional_param("model")])];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 0, "expected no errors, got: {errors:?}");
    }

    #[test]
    fn a_missing_parameter_with_a_declared_default_is_not_flagged() {
        let functions = vec![function(
            "complete",
            vec![
                param("prompt"),
                ParamDef {
                    default: Some("Config::default()".to_string()),
                    ..param("config")
                },
            ],
        )];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 0, "expected no errors, got: {errors:?}");
    }

    #[test]
    fn an_unresolvable_call_licenses_no_claim() {
        let functions = vec![function("complete", vec![param("prompt")])];
        let e2e_config = E2eConfig {
            call: call_named("mystery", vec![arg("wrong_name", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(
            errors.len(),
            0,
            "an unresolved call must not be claimed wrong: {errors:?}"
        );
    }

    #[test]
    fn an_empty_ir_skips_validation_entirely() {
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("wrong_name", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &[], &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 0, "an absent IR must license no claim: {errors:?}");
    }

    #[test]
    fn a_binding_excluded_function_licenses_no_claim() {
        let functions = vec![FunctionDef {
            binding_excluded: true,
            ..function("complete", vec![param("prompt")])
        }];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("wrong_name", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(
            errors.len(),
            0,
            "an excluded function must not be claimed wrong: {errors:?}"
        );
    }

    #[test]
    fn a_fixture_level_args_override_replaces_the_call_args_for_validation() {
        let functions = vec![function("complete", vec![param("prompt"), param("visitor")])];
        let e2e_config = E2eConfig {
            call: call_named("complete", vec![arg("prompt", false)]),
            ..E2eConfig::default()
        };
        let mut fixture = fixture_with_call("with_visitor", None);
        fixture.args = vec![arg("prompt", false), arg("visitor", false)];
        let fixtures = vec![fixture];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 0, "fixture-level args must replace call args: {errors:?}");
    }

    #[test]
    fn a_method_declared_on_an_ir_type_resolves_too() {
        let type_defs = vec![TypeDef {
            name: "Client".to_string(),
            methods: vec![MethodDef {
                name: "chat".to_string(),
                params: vec![param("request")],
                return_type: TypeRef::String,
                ..MethodDef::default()
            }],
            ..TypeDef::default()
        }];
        let e2e_config = E2eConfig {
            call: call_named("chat", vec![arg("wrong_name", false), arg("request", false)]),
            ..E2eConfig::default()
        };
        let fixtures = vec![fixture_with_call("basic", None)];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &[], &type_defs, &["rust".to_string()]);

        assert_eq!(errors.len(), 1, "expected exactly one error, got: {errors:?}");
        assert!(errors[0].message.contains("'chat'"), "got: {}", errors[0].message);
    }

    #[test]
    fn a_named_call_resolves_through_the_fixtures_call_field() {
        let functions = vec![function("embed", vec![param("text")])];
        let mut e2e_config = E2eConfig::default();
        e2e_config.calls.insert(
            "embed".to_string(),
            call_named("embed", vec![arg("wrong_name", false), arg("text", false)]),
        );
        let fixtures = vec![fixture_with_call("embed_basic", Some("embed"))];

        let errors = validate_call_arg_signatures(&fixtures, &e2e_config, &functions, &[], &["rust".to_string()]);

        assert_eq!(errors.len(), 1, "expected exactly one error, got: {errors:?}");
        assert!(
            errors[0].message.contains("fixture 'embed_basic'"),
            "got: {}",
            errors[0].message
        );
    }
}