objectiveai-sdk 2.0.5

ObjectiveAI SDK, definitions, and utilities
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
mod alpha_scalar_branch_state;
mod alpha_scalar_leaf_state;
mod alpha_scalar_state;
mod alpha_vector_branch_state;
mod alpha_vector_leaf_state;
mod alpha_vector_state;
pub mod error;
pub(super) mod files;
#[cfg(feature = "http")]
mod http;

#[cfg(feature = "http")]
pub use http::*;
mod input_schema;
mod params;
mod params_state_or_remote;
mod readme;
pub mod response;

pub use input_schema::*;
pub use params_state_or_remote::*;
pub use alpha_scalar_branch_state::*;
pub use alpha_scalar_leaf_state::*;
pub use alpha_scalar_state::*;
pub use alpha_vector_branch_state::*;
pub use alpha_vector_leaf_state::*;
pub use alpha_vector_state::*;
pub use params::*;

use std::sync::{Arc, Mutex};
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;

/// Constructs a child name by appending the task index to the parent's path.
///
/// Splits `parent_name` by `-`, takes the last segment, and tries to decode
/// it as a base62 path. If successful, pushes `task_index` and re-encodes.
/// If not, appends a new `-` segment with just the index encoded.
///
/// Detection of "is this trailing segment a path?" requires BOTH:
///   1. `last.len() == super::path::PATH_SUFFIX_LEN`, AND
///   2. `b62_to_path(last)` succeeds.
///
/// The length check is what stops a user-chosen suffix like `-v`,
/// `-vii`, or `-final` from being mistakenly decoded as a path and
/// replaced. Both checks are required — see the path module's header
/// comment for the full invariant.
fn child_name(parent_name: &str, task_index: usize) -> String {
    if let Some((prefix, last)) = parent_name.rsplit_once('-') {
        // ADDITIONAL length check — the original code only checked
        // "does it parse?", which lets `-v` (decodes as 57 → path
        // [56]) sneak through. Real paths are always exactly
        // PATH_SUFFIX_LEN base62 chars.
        if last.len() == super::path::PATH_SUFFIX_LEN {
            if let Ok(mut path) = super::path::b62_to_path::<u64>(last) {
                path.push(task_index as u64);
                if let Ok(b62) = super::path::path_to_b62(&path) {
                    return format!("{}-{}", prefix, b62);
                }
            }
        }
    }
    // Couldn't parse existing path segment — start a new one.
    let path = [task_index as u64];
    let b62 = super::path::path_to_b62(&path).unwrap_or_else(|_| format!("{}", task_index));
    format!("{}-{}", parent_name, b62)
}

/// Fixes a task name after reindexing (e.g. after a delete).
///
/// Tries to parse the last `-` segment as a base62 path. If successful,
/// pops the last element and pushes `new_index`. If parsing fails, leaves
/// the name unchanged.
///
/// Same length+parse detection rule as [`child_name`] — see its docs.
fn reindex_name(name: &mut String, new_index: usize) {
    if let Some((prefix, last)) = name.rsplit_once('-') {
        // ADDITIONAL length check — see child_name.
        if last.len() == super::path::PATH_SUFFIX_LEN {
            if let Ok(mut path) = super::path::b62_to_path::<u64>(last) {
                if !path.is_empty() {
                    path.pop();
                    path.push(new_index as u64);
                    if let Ok(b62) = super::path::path_to_b62(&path) {
                        *name = format!("{}-{}", prefix, b62);
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod child_name_tests {
    use super::*;

    #[test]
    fn user_suffix_v_is_preserved() {
        // Regression: `unsettlingness-ranker-v` had its `-v` decoded
        // as base62 → integer → path and replaced with the child's
        // path. Detection now requires the trailing segment to be
        // exactly PATH_SUFFIX_LEN chars, so `"v"` (1 char) doesn't
        // qualify and a fresh path segment is appended.
        let child = child_name("unsettlingness-ranker-v", 0);
        assert!(
            child.starts_with("unsettlingness-ranker-v-"),
            "user suffix `-v` was dropped: {child:?}",
        );
    }

    #[test]
    fn user_suffix_vi_is_preserved() {
        let child = child_name("unsettlingness-ranker-vi", 0);
        assert!(
            child.starts_with("unsettlingness-ranker-vi-"),
            "user suffix `-vi` was dropped: {child:?}",
        );
    }

    #[test]
    fn user_suffix_vii_is_preserved() {
        let child = child_name("unsettlingness-ranker-vii", 0);
        assert!(
            child.starts_with("unsettlingness-ranker-vii-"),
            "user suffix `-vii` was dropped: {child:?}",
        );
    }

    #[test]
    fn other_short_user_suffixes_are_preserved() {
        for parent in ["myfn-final", "scorer-x", "cluster-abc", "thing-abcde"] {
            let child = child_name(parent, 0);
            assert!(
                child.starts_with(&format!("{parent}-")),
                "{parent:?} → {child:?} dropped the user suffix",
            );
        }
    }

    #[test]
    fn real_path_suffix_is_extended() {
        // Parents whose trailing segment IS exactly PATH_SUFFIX_LEN
        // base62 chars (and decodes as a path) get extended in place.
        // Construct a parent with such a suffix synthetically.
        let mut path = vec![3u64];
        let b62 = super::super::path::path_to_b62(&path).unwrap();
        // Pad the encoded value with arbitrary base62 chars so the
        // suffix lands at exactly PATH_SUFFIX_LEN, then re-decode to
        // discover what path that string represents — that's the
        // path child_name will see and extend.
        let padded = format!(
            "{:0>width$}",
            b62,
            width = super::super::path::PATH_SUFFIX_LEN
        );
        let parent = format!("myfn-{padded}");
        let decoded: Vec<u64> = super::super::path::b62_to_path(&padded).unwrap();
        path = decoded;
        let child = child_name(&parent, 7);
        let suffix = child.strip_prefix("myfn-").expect("user half survives");
        let extended: Vec<u64> = super::super::path::b62_to_path(suffix).unwrap();
        let mut expected = path.clone();
        expected.push(7);
        assert_eq!(extended, expected);
    }

    #[test]
    fn reindex_leaves_user_suffix_alone() {
        for original in ["scorer-v", "ranker-vi", "thing-final"] {
            let mut name = String::from(original);
            reindex_name(&mut name, 9);
            assert_eq!(name, original);
        }
    }
}

/// Abstracts over the 4 routed state variants for invention step orchestration.
pub trait InventionState: Clone + Send + 'static {
    fn params(this: &Arc<Mutex<Self>>) -> Params;
    fn is_scalar() -> bool;
    fn prompt_type() -> super::prompts::StepPromptType;
    fn object() -> super::response::streaming::Object;
    fn into_state(self) -> State;

    fn set_tasks_length(this: &Arc<Mutex<Self>>, len: u64);
    fn input_schema_json(this: &Arc<Mutex<Self>>) -> Option<String>;

    fn essay_tools(this: &Arc<Mutex<Self>>) -> Vec<super::InventionTool>;
    fn essay_tool_names(this: &Arc<Mutex<Self>>) -> Vec<String> {
        Self::essay_tools(this)
            .into_iter()
            .map(|t| t.name)
            .collect()
    }
    fn validate_essay(this: &Arc<Mutex<Self>>) -> Result<(), String>;

    fn input_schema_tools(this: &Arc<Mutex<Self>>) -> Vec<super::InventionTool>;
    fn input_schema_tool_names(this: &Arc<Mutex<Self>>) -> Vec<String> {
        Self::input_schema_tools(this)
            .into_iter()
            .map(|t| t.name)
            .collect()
    }
    fn validate_input_schema(this: &Arc<Mutex<Self>>) -> Result<(), String>;

    fn essay_tasks_tools(this: &Arc<Mutex<Self>>) -> Vec<super::InventionTool>;
    fn essay_tasks_tool_names(this: &Arc<Mutex<Self>>) -> Vec<String> {
        Self::essay_tasks_tools(this)
            .into_iter()
            .map(|t| t.name)
            .collect()
    }
    fn validate_essay_tasks(this: &Arc<Mutex<Self>>) -> Result<(), String>;

    fn tasks_tools(this: &Arc<Mutex<Self>>) -> Vec<super::InventionTool>;
    fn tasks_tool_names(this: &Arc<Mutex<Self>>) -> Vec<String> {
        Self::tasks_tools(this)
            .into_iter()
            .map(|t| t.name)
            .collect()
    }
    fn validate_function(this: &Arc<Mutex<Self>>) -> Result<(), String>;
    fn build_function(this: &Arc<Mutex<Self>>) -> Option<crate::functions::FullRemoteFunction>;

    fn description_tools(this: &Arc<Mutex<Self>>) -> Vec<super::InventionTool>;
    fn description_tool_names(this: &Arc<Mutex<Self>>) -> Vec<String> {
        Self::description_tools(this)
            .into_iter()
            .map(|t| t.name)
            .collect()
    }
    fn validate_description(this: &Arc<Mutex<Self>>) -> Result<(), String>;

    fn write_readme(this: &Arc<Mutex<Self>>);

    fn replace_placeholders(
        this: &Arc<Mutex<Self>>,
        paths: &[crate::RemotePath],
    );
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, arbitrary::Arbitrary)]
#[serde(tag = "type")]
#[schemars(rename = "functions.inventions.state.State")]
pub enum State {
    #[schemars(title = "AlphaScalarBranch")]
    #[serde(rename = "alpha.scalar.branch.function")]
    AlphaScalarBranch(AlphaScalarBranchState),
    #[schemars(title = "AlphaScalarLeaf")]
    #[serde(rename = "alpha.scalar.leaf.function")]
    AlphaScalarLeaf(AlphaScalarLeafState),
    #[schemars(title = "AlphaVectorBranch")]
    #[serde(rename = "alpha.vector.branch.function")]
    AlphaVectorBranch(AlphaVectorBranchState),
    #[schemars(title = "AlphaVectorLeaf")]
    #[serde(rename = "alpha.vector.leaf.function")]
    AlphaVectorLeaf(AlphaVectorLeafState),
}

impl State {
    /// Validates the initial state's input schema and tasks.
    ///
    /// Returns an error if the input schema is invalid or if the tasks
    /// are not valid for the provided input schema.
    pub fn validate_initial_state(
        &self,
        children: Option<&std::collections::HashMap<String, crate::functions::FullRemoteFunction>>,
    ) -> Result<(), String> {
        match self {
            State::AlphaScalarBranch(s) => s.validate_initial_state(children),
            State::AlphaScalarLeaf(s) => s.validate_initial_state(),
            State::AlphaVectorBranch(s) => s.validate_initial_state(children),
            State::AlphaVectorLeaf(s) => s.validate_initial_state(),
        }
    }

    /// Sets the checker seed on the inner state variant.
    pub fn set_checker_seed(&mut self, seed: Option<i64>) {
        match self {
            State::AlphaScalarBranch(s) => s.checker_seed = seed,
            State::AlphaScalarLeaf(s) => s.checker_seed = seed,
            State::AlphaVectorBranch(s) => s.checker_seed = seed,
            State::AlphaVectorLeaf(s) => s.checker_seed = seed,
        }
    }

    /// Returns the predicted tasks length, if set.
    pub fn tasks_length(&self) -> Option<u64> {
        match self {
            State::AlphaScalarBranch(s) => s.tasks_length,
            State::AlphaScalarLeaf(s) => s.tasks_length,
            State::AlphaVectorBranch(s) => s.tasks_length,
            State::AlphaVectorLeaf(s) => s.tasks_length,
        }
    }

    /// Returns a reference to the params.
    pub fn params(&self) -> &Params {
        match self {
            State::AlphaScalarBranch(s) => &s.params,
            State::AlphaScalarLeaf(s) => &s.params,
            State::AlphaVectorBranch(s) => &s.params,
            State::AlphaVectorLeaf(s) => &s.params,
        }
    }

    /// Returns the prompt type for this state variant.
    pub fn prompt_type(&self) -> super::prompts::StepPromptType {
        match self {
            State::AlphaScalarBranch(_) => super::prompts::StepPromptType::AlphaScalarBranchFunction,
            State::AlphaScalarLeaf(_) => super::prompts::StepPromptType::AlphaScalarLeafFunction,
            State::AlphaVectorBranch(_) => super::prompts::StepPromptType::AlphaVectorBranchFunction,
            State::AlphaVectorLeaf(_) => super::prompts::StepPromptType::AlphaVectorLeafFunction,
        }
    }

    /// Returns a reference to the params name.
    pub fn name(&self) -> &str {
        match self {
            State::AlphaScalarBranch(s) => &s.params.name,
            State::AlphaScalarLeaf(s) => &s.params.name,
            State::AlphaVectorBranch(s) => &s.params.name,
            State::AlphaVectorLeaf(s) => &s.params.name,
        }
    }

    /// Replaces placeholder tasks with real function tasks using the given paths.
    /// Matches by `repository == name`. No-op for leaf states.
    pub fn replace_placeholders(
        &mut self,
        paths: &[crate::RemotePath],
    ) {
        match self {
            State::AlphaScalarBranch(s) => s.replace_placeholders(paths),
            State::AlphaScalarLeaf(s) => s.replace_placeholders(paths),
            State::AlphaVectorBranch(s) => s.replace_placeholders(paths),
            State::AlphaVectorLeaf(s) => s.replace_placeholders(paths),
        }
    }

    /// Builds the `FullRemoteFunction` from the current state.
    /// Returns `None` if required fields are missing.
    pub fn build_function(&self) -> Option<crate::functions::FullRemoteFunction> {
        match self {
            State::AlphaScalarBranch(s) => s.build_function(),
            State::AlphaScalarLeaf(s) => s.build_function(),
            State::AlphaVectorBranch(s) => s.build_function(),
            State::AlphaVectorLeaf(s) => s.build_function(),
        }
    }

    /// Regenerates the README from the current state (description + sub-function URLs).
    pub fn write_readme(&mut self) {
        match self {
            State::AlphaScalarBranch(s) => s.write_readme(),
            State::AlphaScalarLeaf(s) => s.write_readme(),
            State::AlphaVectorBranch(s) => s.write_readme(),
            State::AlphaVectorLeaf(s) => s.write_readme(),
        }
    }

    /// Extracts child `ParamsState` values from placeholder tasks in branch states.
    /// Returns an empty vec for leaf states.
    pub fn placeholder_children(&self) -> Vec<ParamsState> {
        match self {
            State::AlphaScalarLeaf(_) | State::AlphaVectorLeaf(_) => vec![],
            State::AlphaScalarBranch(s) => {
                let tasks = match &s.tasks {
                    Some(tasks) => tasks,
                    None => return vec![],
                };
                tasks.iter().filter_map(|task| match task {
                    crate::functions::alpha_scalar::BranchTaskExpression::PlaceholderScalarFunction(t) => {
                        Some(ParamsState::AlphaScalar(AlphaScalarState {
                            params: t.params.clone(),
                            input_schema: Some(t.input_schema.clone()),
                        }))
                    }
                    _ => None,
                }).collect()
            }
            State::AlphaVectorBranch(s) => {
                let tasks = match &s.tasks {
                    Some(tasks) => tasks,
                    None => return vec![],
                };
                tasks.iter().filter_map(|task| match task {
                    crate::functions::alpha_vector::BranchTaskExpression::PlaceholderScalarFunction(t) => {
                        Some(ParamsState::AlphaScalar(AlphaScalarState {
                            params: t.params.clone(),
                            input_schema: Some(t.input_schema.clone()),
                        }))
                    }
                    crate::functions::alpha_vector::BranchTaskExpression::PlaceholderVectorFunction(t) => {
                        Some(ParamsState::AlphaVector(AlphaVectorState {
                            params: t.params.clone(),
                            input_schema: Some(t.input_schema.clone()),
                        }))
                    }
                    _ => None,
                }).collect()
            }
        }
    }

    pub fn serialize_into_files(&self) -> std::collections::HashMap<&'static str, String> {
        let files = match self {
            State::AlphaScalarBranch(s) => s.serialize_into_files(),
            State::AlphaScalarLeaf(s) => s.serialize_into_files(),
            State::AlphaVectorBranch(s) => s.serialize_into_files(),
            State::AlphaVectorLeaf(s) => s.serialize_into_files(),
        };
        files.into_hashmap()
    }

}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type")]
#[schemars(rename = "functions.inventions.state.ParamsState")]
pub enum ParamsState {
    #[schemars(title = "AlphaScalarBranch")]
    #[serde(rename = "alpha.scalar.branch.function")]
    AlphaScalarBranch(AlphaScalarBranchState),
    #[schemars(title = "AlphaScalarLeaf")]
    #[serde(rename = "alpha.scalar.leaf.function")]
    AlphaScalarLeaf(AlphaScalarLeafState),
    #[schemars(title = "AlphaVectorBranch")]
    #[serde(rename = "alpha.vector.branch.function")]
    AlphaVectorBranch(AlphaVectorBranchState),
    #[schemars(title = "AlphaVectorLeaf")]
    #[serde(rename = "alpha.vector.leaf.function")]
    AlphaVectorLeaf(AlphaVectorLeafState),
    #[schemars(title = "AlphaScalar")]
    #[serde(
        rename = "alpha.scalar.function",
        alias = "placeholder.alpha.scalar.function"
    )]
    AlphaScalar(AlphaScalarState),
    #[schemars(title = "AlphaVector")]
    #[serde(
        rename = "alpha.vector.function",
        alias = "placeholder.alpha.vector.function"
    )]
    AlphaVector(AlphaVectorState),
}

impl ParamsState {
    /// Returns the prompt type for this state variant.
    /// For unrouted `AlphaScalar`/`AlphaVector`, routes based on depth.
    pub fn prompt_type(&self) -> super::prompts::StepPromptType {
        match self {
            ParamsState::AlphaScalarBranch(_) => super::prompts::StepPromptType::AlphaScalarBranchFunction,
            ParamsState::AlphaScalarLeaf(_) => super::prompts::StepPromptType::AlphaScalarLeafFunction,
            ParamsState::AlphaVectorBranch(_) => super::prompts::StepPromptType::AlphaVectorBranchFunction,
            ParamsState::AlphaVectorLeaf(_) => super::prompts::StepPromptType::AlphaVectorLeafFunction,
            ParamsState::AlphaScalar(s) => {
                if s.params.depth == 0 {
                    super::prompts::StepPromptType::AlphaScalarLeafFunction
                } else {
                    super::prompts::StepPromptType::AlphaScalarBranchFunction
                }
            }
            ParamsState::AlphaVector(s) => {
                if s.params.depth == 0 {
                    super::prompts::StepPromptType::AlphaVectorLeafFunction
                } else {
                    super::prompts::StepPromptType::AlphaVectorBranchFunction
                }
            }
        }
    }

    pub fn route(self) -> State {
        match self {
            ParamsState::AlphaScalarBranch(s) => State::AlphaScalarBranch(s),
            ParamsState::AlphaScalarLeaf(s) => State::AlphaScalarLeaf(s),
            ParamsState::AlphaVectorBranch(s) => State::AlphaVectorBranch(s),
            ParamsState::AlphaVectorLeaf(s) => State::AlphaVectorLeaf(s),
            ParamsState::AlphaScalar(s) => {
                if s.params.depth == 0 {
                    State::AlphaScalarLeaf(AlphaScalarLeafState {
                        params: s.params,
                        essay: None,
                        input_schema: s.input_schema,
                        essay_tasks: None,
                        tasks: None,
                        tasks_length: None,
                        description: None,
                        readme: None,
                        checker_seed: None,
                    })
                } else {
                    State::AlphaScalarBranch(AlphaScalarBranchState {
                        params: s.params,
                        essay: None,
                        input_schema: s.input_schema,
                        essay_tasks: None,
                        tasks: None,
                        tasks_length: None,
                        description: None,
                        readme: None,
                        checker_seed: None,
                    })
                }
            }
            ParamsState::AlphaVector(s) => {
                if s.params.depth == 0 {
                    State::AlphaVectorLeaf(AlphaVectorLeafState {
                        params: s.params,
                        essay: None,
                        input_schema: s.input_schema,
                        essay_tasks: None,
                        tasks: None,
                        tasks_length: None,
                        description: None,
                        readme: None,
                        checker_seed: None,
                    })
                } else {
                    State::AlphaVectorBranch(AlphaVectorBranchState {
                        params: s.params,
                        essay: None,
                        input_schema: s.input_schema,
                        essay_tasks: None,
                        tasks: None,
                        tasks_length: None,
                        description: None,
                        readme: None,
                        checker_seed: None,
                    })
                }
            }
        }
    }

    pub const fn filenames() -> &'static [&'static str] {
        files::Files::filenames()
    }

    pub fn serialize_into_files(&self) -> std::collections::HashMap<&'static str, String> {
        let files = match self {
            ParamsState::AlphaScalarBranch(s) => s.serialize_into_files(),
            ParamsState::AlphaScalarLeaf(s) => s.serialize_into_files(),
            ParamsState::AlphaVectorBranch(s) => s.serialize_into_files(),
            ParamsState::AlphaVectorLeaf(s) => s.serialize_into_files(),
            ParamsState::AlphaScalar(s) => s.serialize_into_files(),
            ParamsState::AlphaVector(s) => s.serialize_into_files(),
        };
        files.into_hashmap()
    }

    pub fn deserialize_from_files(map: std::collections::HashMap<&'static str, String>) -> Result<Option<Self>, error::Error> {
        let files = files::Files::from_hashmap(map)?;

        // Deserialize function.json if present to determine the routed variant
        let function: Option<crate::functions::FullRemoteFunction> = files.function_json.as_ref()
            .map(|json| {
                let mut de = serde_json::Deserializer::from_str(json);
                serde_path_to_error::deserialize(&mut de)
                    .map_err(|e| error::Error::Deserialize {
                        file: files::Files::FUNCTION_JSON,
                        source: e,
                    })
            })
            .transpose()?;

        match function {
            Some(crate::functions::FullRemoteFunction::Alpha(
                crate::functions::AlphaRemoteFunction::Scalar(
                    scalar @ crate::functions::alpha_scalar::RemoteFunction::Leaf { .. }
                )
            )) => Ok(Some(ParamsState::AlphaScalarLeaf(AlphaScalarLeafState::deserialize_from_files(Some(scalar), &files)?))),
            Some(crate::functions::FullRemoteFunction::Alpha(
                crate::functions::AlphaRemoteFunction::Scalar(
                    scalar @ crate::functions::alpha_scalar::RemoteFunction::Branch { .. }
                )
            )) => Ok(Some(ParamsState::AlphaScalarBranch(AlphaScalarBranchState::deserialize_from_files(Some(scalar), &files)?))),
            Some(crate::functions::FullRemoteFunction::Alpha(
                crate::functions::AlphaRemoteFunction::Vector(
                    vector @ crate::functions::alpha_vector::RemoteFunction::Leaf { .. }
                )
            )) => Ok(Some(ParamsState::AlphaVectorLeaf(AlphaVectorLeafState::deserialize_from_files(Some(vector), &files)?))),
            Some(crate::functions::FullRemoteFunction::Alpha(
                crate::functions::AlphaRemoteFunction::Vector(
                    vector @ crate::functions::alpha_vector::RemoteFunction::Branch { .. }
                )
            )) => Ok(Some(ParamsState::AlphaVectorBranch(AlphaVectorBranchState::deserialize_from_files(Some(vector), &files)?))),
            Some(other) => Err(error::Error::UnrecognizedFunctionType(format!("{:?}", std::mem::discriminant(&other)))),
            // No function.json — composite AlphaScalar/AlphaVector from params + optional input_schema
            None => {
                let input_schema: Option<InputSchema> = files.input_schema_json.as_ref()
                    .map(|json| {
                        let mut de = serde_json::Deserializer::from_str(json);
                        serde_path_to_error::deserialize(&mut de)
                            .map_err(|e| error::Error::Deserialize {
                                file: files::Files::INPUT_SCHEMA_JSON,
                                source: e,
                            })
                    })
                    .transpose()?;

                match input_schema {
                    Some(InputSchema::Scalar { schema }) => {
                        Ok(Some(ParamsState::AlphaScalar(AlphaScalarState::deserialize_from_files(Some(schema), &files)?)))
                    }
                    Some(InputSchema::Vector { schema }) => {
                        Ok(Some(ParamsState::AlphaVector(AlphaVectorState::deserialize_from_files(Some(schema), &files)?)))
                    }
                    None => Ok(None),
                }
            }
        }
    }
}