apcore 0.22.0

Schema-driven module standard for AI-perceivable interfaces
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
// APCore Protocol — Pipeline YAML configuration: step type registry and strategy builder.
// Spec reference: design-execution-pipeline.md (Section 8)

use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::OnceLock;

use serde_json::Value;

use crate::builtin_steps::build_standard_strategy;
use crate::errors::{ErrorCode, ModuleError};
use crate::pipeline::{ExecutionStrategy, Step};

// ---------------------------------------------------------------------------
// Step factory type
// ---------------------------------------------------------------------------

/// Factory function that creates a `Step` from a config dict.
pub(crate) type StepFactory =
    Box<dyn Fn(&Value) -> Result<Box<dyn Step>, ModuleError> + Send + Sync>;

// ---------------------------------------------------------------------------
// Global step type registry (OnceLock + RwLock pattern)
// ---------------------------------------------------------------------------

fn global_step_factories() -> &'static RwLock<HashMap<String, StepFactory>> {
    static FACTORIES: OnceLock<RwLock<HashMap<String, StepFactory>>> = OnceLock::new();
    FACTORIES.get_or_init(|| RwLock::new(HashMap::new()))
}

/// Register a step type for YAML pipeline configuration.
///
/// The `name` is the type name referenced in YAML `type` fields.
/// The `factory` is a callable that receives `&Value` config and returns a `Box<dyn Step>`.
///
/// Returns an error if the name is empty, contains whitespace, or is already registered.
pub fn register_step_type(name: &str, factory: StepFactory) -> Result<(), ModuleError> {
    if name.is_empty() || name.contains(' ') {
        return Err(ModuleError::new(
            ErrorCode::GeneralInvalidInput,
            format!("Invalid step type name: '{name}'"),
        ));
    }
    let mut map = global_step_factories().write();
    if map.contains_key(name) {
        return Err(ModuleError::new(
            ErrorCode::GeneralInvalidInput,
            format!("Step type '{name}' is already registered"),
        ));
    }
    map.insert(name.to_string(), factory);
    Ok(())
}

/// Remove a registered step type. Returns `true` if found and removed.
#[must_use]
pub fn unregister_step_type(name: &str) -> bool {
    let mut map = global_step_factories().write();
    map.remove(name).is_some()
}

/// Return a list of all registered step type names.
#[must_use]
pub fn registered_step_types() -> Vec<String> {
    let map = global_step_factories().read();
    map.keys().cloned().collect()
}

/// Clear the step type registry (for testing only).
#[cfg(test)]
pub(crate) fn reset_step_registry() {
    let mut map = global_step_factories().write();
    map.clear();
}

// ---------------------------------------------------------------------------
// Step resolution from config dict (DECLARATIVE_CONFIG_SPEC.md §4)
// ---------------------------------------------------------------------------

/// Wrapper that overlays YAML metadata fields onto a factory-created step.
struct ConfiguredStep {
    inner: Box<dyn Step>,
    name_override: Option<String>,
    match_modules: Option<Vec<String>>,
    ignore_errors: bool,
    pure: bool,
    timeout_ms: u64,
}

#[async_trait::async_trait]
impl Step for ConfiguredStep {
    fn name(&self) -> &str {
        self.name_override
            .as_deref()
            .unwrap_or_else(|| self.inner.name())
    }
    fn description(&self) -> &str {
        self.inner.description()
    }
    fn removable(&self) -> bool {
        self.inner.removable()
    }
    fn replaceable(&self) -> bool {
        self.inner.replaceable()
    }
    fn match_modules(&self) -> Option<&[String]> {
        self.match_modules
            .as_deref()
            .or_else(|| self.inner.match_modules())
    }
    fn ignore_errors(&self) -> bool {
        self.ignore_errors
    }
    fn pure(&self) -> bool {
        self.pure
    }
    fn timeout_ms(&self) -> u64 {
        self.timeout_ms
    }
    async fn execute(
        &self,
        ctx: &mut crate::pipeline::PipelineContext,
    ) -> Result<crate::pipeline::StepResult, ModuleError> {
        self.inner.execute(ctx).await
    }
}

/// Resolve a single step definition dict into a `Box<dyn Step>`.
///
/// Per `DECLARATIVE_CONFIG_SPEC.md` §4.3:
///   - `type:` → registry lookup (only supported mode in Rust)
///   - `handler:` → parse-time error (Rust cannot dynamically load modules)
///   - Metadata: `match_modules`, `ignore_errors`, `pure`, `timeout_ms` applied via wrapper.
fn resolve_step(step_def: &Value) -> Result<Box<dyn Step>, ModuleError> {
    let step_name = step_def.get("name").and_then(|v| v.as_str()).unwrap_or("");
    let type_name = step_def.get("type").and_then(|v| v.as_str());
    let handler_path = step_def.get("handler").and_then(|v| v.as_str());
    let config = step_def
        .get("config")
        .cloned()
        .unwrap_or(Value::Object(serde_json::Map::new()));

    // handler: is not supported in Rust (compiled language, no runtime import).
    if let Some(hp) = handler_path {
        return Err(ModuleError::new(
            ErrorCode::PipelineHandlerNotSupported,
            format!(
                "pipeline step '{step_name}' uses 'handler: {hp}' which is not supported in apcore-rust. \
                 Use 'type:' with register_step_type(). \
                 See DECLARATIVE_CONFIG_SPEC.md §4.4",
            ),
        ));
    }

    let type_name = type_name.ok_or_else(|| {
        ModuleError::new(
            ErrorCode::GeneralInvalidInput,
            format!("Step '{step_name}' has neither 'type' nor 'handler'"),
        )
    })?;

    let map = global_step_factories().read();
    let factory = map.get(type_name).ok_or_else(|| {
        ModuleError::new(
            ErrorCode::GeneralInvalidInput,
            format!(
                "Step type '{type_name}' not registered. \
                 Register with: register_step_type(\"{type_name}\", factory)"
            ),
        )
    })?;

    let inner = factory(&config)?;

    // Parse metadata from YAML
    let match_modules = step_def
        .get("match_modules")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(String::from))
                .collect()
        });
    let ignore_errors = step_def
        .get("ignore_errors")
        .and_then(serde_json::Value::as_bool)
        .unwrap_or(false);
    let pure = step_def
        .get("pure")
        .and_then(serde_json::Value::as_bool)
        .unwrap_or(false);
    let timeout_ms = step_def
        .get("timeout_ms")
        .and_then(serde_json::Value::as_u64)
        .unwrap_or(0);

    let name_override = step_def
        .get("name")
        .and_then(|v| v.as_str())
        .map(String::from);

    Ok(Box::new(ConfiguredStep {
        inner,
        name_override,
        match_modules,
        ignore_errors,
        pure,
        timeout_ms,
    }))
}

// ---------------------------------------------------------------------------
// Build strategy from YAML config
// ---------------------------------------------------------------------------

/// Build an `ExecutionStrategy` from a YAML pipeline configuration section.
///
/// Starts with `build_standard_strategy()`, then applies:
///   1. `remove` -- remove named steps
///   2. `steps` -- resolve and insert custom steps (via `after` or `before` anchors)
///
/// # Example config shape (as JSON)
///
/// ```json
/// {
///   "remove": ["acl_check", "approval_gate"],
///   "steps": [
///     {
///       "name": "rate_limit",
///       "type": "rate_limiter",
///       "after": "call_chain_guard",
///       "config": { "max_rps": 100 }
///     }
///   ]
/// }
/// ```
#[allow(clippy::too_many_lines)] // declarative-config dispatcher; splitting hurts readability more than length helps
pub fn build_strategy_from_config(
    pipeline_config: &Value,
) -> Result<ExecutionStrategy, ModuleError> {
    let mut strategy = build_standard_strategy();

    // (1) Remove steps — Issue #33 §1.2: fail-fast when YAML refers to a
    // nonexistent step rather than emitting a tracing::warn! and proceeding.
    if let Some(remove_list) = pipeline_config.get("remove").and_then(|v| v.as_array()) {
        for entry in remove_list {
            if let Some(step_name) = entry.as_str() {
                strategy.remove(step_name).map_err(|e| {
                    // Sync alignment W-7: missing-step is a structural
                    // configuration error, distinct from a dependency-graph
                    // violation. ConfigurationError mirrors PY/TS.
                    ModuleError::new(
                        ErrorCode::ConfigurationError,
                        format!(
                            "pipeline.remove: cannot remove step '{step_name}': {}",
                            e.message
                        ),
                    )
                })?;
            }
        }
    }

    // (2) Configure existing step fields (DECLARATIVE_CONFIG_SPEC.md §4.2)
    if let Some(Value::Object(configure)) = pipeline_config.get("configure") {
        for (step_name, overrides) in configure {
            let step_name_str = step_name.as_str();
            if let Value::Object(fields) = overrides {
                let match_modules =
                    fields
                        .get("match_modules")
                        .and_then(|v| v.as_array())
                        .map(|arr| {
                            arr.iter()
                                .filter_map(|v| v.as_str().map(String::from))
                                .collect()
                        });
                let ignore_errors = fields
                    .get("ignore_errors")
                    .and_then(serde_json::Value::as_bool);
                let pure_val = fields.get("pure").and_then(serde_json::Value::as_bool);
                let timeout_ms = fields.get("timeout_ms").and_then(serde_json::Value::as_u64);

                // Warn on unknown keys (never silently drop).
                for key in fields.keys() {
                    if !["match_modules", "ignore_errors", "pure", "timeout_ms"]
                        .contains(&key.as_str())
                    {
                        tracing::warn!(
                            step = step_name_str,
                            field = key.as_str(),
                            "Unknown configurable field — ignored"
                        );
                    }
                }

                // Wrap the existing step with a ConfiguredStep overlay.
                // Issue #33 §1.2: configuring a nonexistent step is a hard
                // configuration error, not a warning.
                strategy
                    .replace_with(step_name_str, |inner| {
                        Box::new(ConfiguredStep {
                            name_override: None,
                            match_modules: match_modules.or_else(|| {
                                inner.match_modules().map(<[std::string::String]>::to_vec)
                            }),
                            ignore_errors: ignore_errors.unwrap_or_else(|| inner.ignore_errors()),
                            pure: pure_val.unwrap_or_else(|| inner.pure()),
                            timeout_ms: timeout_ms.unwrap_or_else(|| inner.timeout_ms()),
                            inner,
                        })
                    })
                    .map_err(|e| {
                        // Sync alignment W-7: configuring a non-existent step
                        // is a structural configuration error, not a
                        // dependency violation.
                        ModuleError::new(
                            ErrorCode::ConfigurationError,
                            format!(
                                "pipeline.configure: cannot configure step '{step_name_str}': {}",
                                e.message
                            ),
                        )
                    })?;
            }
        }
    }

    // (3) Resolve and insert custom steps
    if let Some(steps_list) = pipeline_config.get("steps").and_then(|v| v.as_array()) {
        for step_def in steps_list {
            let step = resolve_step(step_def)?;
            let after = step_def.get("after").and_then(|v| v.as_str());
            let before = step_def.get("before").and_then(|v| v.as_str());

            if let Some(anchor) = after {
                strategy.insert_after(anchor, step).map_err(|e| {
                    // Re-classify missing-anchor as a structural configuration
                    // error (sync alignment W-7). The underlying find_step_index
                    // returns GeneralInvalidInput, which is too broad for
                    // pipeline-config callers.
                    if matches!(e.code, ErrorCode::GeneralInvalidInput) {
                        ModuleError::new(
                            ErrorCode::ConfigurationError,
                            format!(
                                "pipeline.steps: 'after' anchor '{anchor}' not found: {}",
                                e.message
                            ),
                        )
                    } else {
                        e
                    }
                })?;
            } else if let Some(anchor) = before {
                strategy.insert_before(anchor, step).map_err(|e| {
                    if matches!(e.code, ErrorCode::GeneralInvalidInput) {
                        ModuleError::new(
                            ErrorCode::ConfigurationError,
                            format!(
                                "pipeline.steps: 'before' anchor '{anchor}' not found: {}",
                                e.message
                            ),
                        )
                    } else {
                        e
                    }
                })?;
            } else {
                // Issue #33 §1.2: fail-fast on YAML configuration errors.
                // Sync alignment W-7: structural configuration errors use
                // ConfigurationError rather than PipelineConfigInvalid.
                let name = step_def
                    .get("name")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown");
                return Err(ModuleError::new(
                    ErrorCode::ConfigurationError,
                    format!(
                        "pipeline.steps: step '{name}' has neither 'after' nor 'before' anchor"
                    ),
                ));
            }
        }
    }

    Ok(strategy)
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pipeline::{PipelineContext, StepResult};
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::Mutex;

    /// Minimal configurable step for testing.
    struct ConfigurableStep {
        name: String,
        description: String,
    }

    impl ConfigurableStep {
        fn new(name: &str) -> Self {
            Self {
                name: name.to_string(),
                description: format!("Configurable step: {name}"),
            }
        }
    }

    #[async_trait]
    impl Step for ConfigurableStep {
        fn name(&self) -> &str {
            &self.name
        }
        fn description(&self) -> &str {
            &self.description
        }
        fn removable(&self) -> bool {
            true
        }
        fn replaceable(&self) -> bool {
            true
        }
        fn pure(&self) -> bool {
            true
        }
        async fn execute(&self, _ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
            Ok(StepResult::continue_step())
        }
    }

    // Serialize tests that mutate the global registry to avoid races.
    static TEST_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    fn test_register_step_type_success() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        let result = register_step_type(
            "my_step",
            Box::new(|_config| Ok(Box::new(ConfigurableStep::new("my_step")))),
        );
        assert!(result.is_ok());
        assert!(registered_step_types().contains(&"my_step".to_string()));
    }

    #[test]
    fn test_register_step_type_empty_name_rejected() {
        let _guard = TEST_LOCK.lock().unwrap();
        let result = register_step_type("", Box::new(|_| Ok(Box::new(ConfigurableStep::new("x")))));
        assert!(result.is_err());
    }

    #[test]
    fn test_register_step_type_whitespace_name_rejected() {
        let _guard = TEST_LOCK.lock().unwrap();
        let result = register_step_type(
            "my step",
            Box::new(|_| Ok(Box::new(ConfigurableStep::new("x")))),
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_register_step_type_duplicate_rejected() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        register_step_type(
            "dup_step",
            Box::new(|_| Ok(Box::new(ConfigurableStep::new("dup_step")))),
        )
        .unwrap();
        let result = register_step_type(
            "dup_step",
            Box::new(|_| Ok(Box::new(ConfigurableStep::new("dup_step")))),
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_unregister_step_type_returns_true_if_found() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        register_step_type(
            "removable",
            Box::new(|_| Ok(Box::new(ConfigurableStep::new("removable")))),
        )
        .unwrap();
        assert!(unregister_step_type("removable"));
        assert!(!registered_step_types().contains(&"removable".to_string()));
    }

    #[test]
    fn test_unregister_step_type_returns_false_if_not_found() {
        let _guard = TEST_LOCK.lock().unwrap();
        assert!(!unregister_step_type("__nonexistent__"));
    }

    #[test]
    fn test_registered_step_types_after_reset() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        assert!(registered_step_types().is_empty());
    }

    #[test]
    fn test_build_strategy_from_config_remove_steps() {
        // Does not use the step type registry — no lock needed.
        let config = json!({
            "remove": ["acl_check", "approval_gate"]
        });
        let strategy = build_strategy_from_config(&config).unwrap();
        let names = strategy.step_names();
        assert!(!names.contains(&"acl_check".to_string()));
        assert!(!names.contains(&"approval_gate".to_string()));
        assert!(names.contains(&"module_lookup".to_string()));
        assert!(names.contains(&"execute".to_string()));
    }

    #[test]
    fn test_build_strategy_from_config_insert_custom_step() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        register_step_type(
            "rate_limiter",
            Box::new(|_config| Ok(Box::new(ConfigurableStep::new("rate_limit")))),
        )
        .unwrap();

        let config = json!({
            "steps": [{
                "name": "rate_limit",
                "type": "rate_limiter",
                "after": "call_chain_guard",
                "config": {}
            }]
        });
        let strategy = build_strategy_from_config(&config).unwrap();
        let names = strategy.step_names();
        let guard_idx = names.iter().position(|n| n == "call_chain_guard").unwrap();
        let rate_idx = names.iter().position(|n| n == "rate_limit").unwrap();
        assert_eq!(rate_idx, guard_idx + 1);
    }

    #[test]
    fn test_build_strategy_from_config_insert_before() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        register_step_type(
            "before_type",
            Box::new(|_config| Ok(Box::new(ConfigurableStep::new("custom_before")))),
        )
        .unwrap();

        let config = json!({
            "steps": [{
                "name": "custom_before",
                "type": "before_type",
                "before": "execute"
            }]
        });
        let strategy = build_strategy_from_config(&config).unwrap();
        let names = strategy.step_names();
        let custom_idx = names.iter().position(|n| n == "custom_before").unwrap();
        let exec_idx = names.iter().position(|n| n == "execute").unwrap();
        assert_eq!(custom_idx + 1, exec_idx);
    }

    #[test]
    fn test_build_strategy_from_config_unknown_type_returns_error() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        let config = json!({
            "steps": [{
                "name": "bad",
                "type": "__nonexistent_type__",
                "after": "execute"
            }]
        });
        let result = build_strategy_from_config(&config);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("__nonexistent_type__"));
    }

    #[test]
    fn test_build_strategy_from_config_empty_config() {
        let config = json!({});
        let strategy = build_strategy_from_config(&config).unwrap();
        assert_eq!(strategy.step_names().len(), 11);
    }

    #[test]
    fn test_resolve_step_missing_type_returns_error() {
        let _guard = TEST_LOCK.lock().unwrap();
        let step_def = json!({"name": "no_type"});
        let result = resolve_step(&step_def);
        assert!(result.is_err());
    }

    #[test]
    fn test_handler_rejected_with_pipeline_handler_not_supported() {
        let _guard = TEST_LOCK.lock().unwrap();
        let step_def = json!({
            "name": "dynamic",
            "handler": "my_app.steps:CustomStep",
            "after": "execute"
        });
        let result = resolve_step(&step_def);
        match result {
            Err(err) => {
                assert_eq!(err.code, ErrorCode::PipelineHandlerNotSupported);
                assert!(err.message.contains("handler"));
                assert!(err.message.contains("DECLARATIVE_CONFIG_SPEC.md §4.4"));
            }
            Ok(_) => panic!("expected PipelineHandlerNotSupportedError"),
        }
    }

    #[test]
    fn test_resolve_step_applies_metadata_overrides() {
        let _guard = TEST_LOCK.lock().unwrap();
        reset_step_registry();
        register_step_type(
            "meta_step",
            Box::new(|_config| Ok(Box::new(ConfigurableStep::new("meta")))),
        )
        .unwrap();

        let step_def = json!({
            "name": "configured",
            "type": "meta_step",
            "match_modules": ["api.*", "web.*"],
            "ignore_errors": true,
            "pure": true,
            "timeout_ms": 5000
        });
        let step = resolve_step(&step_def).unwrap();
        assert_eq!(step.name(), "configured");
        assert_eq!(step.match_modules().unwrap(), &["api.*", "web.*"]);
        assert!(step.ignore_errors());
        assert!(step.pure());
        assert_eq!(step.timeout_ms(), 5000);
    }

    #[test]
    fn test_configure_section_overrides_existing_step() {
        let config = json!({
            "configure": {
                "input_validation": {
                    "ignore_errors": true,
                    "timeout_ms": 3000
                }
            }
        });
        let strategy = build_strategy_from_config(&config).unwrap();
        // Find the configured step and verify overrides took effect.
        let step = strategy
            .steps()
            .iter()
            .find(|s| s.name() == "input_validation")
            .expect("input_validation step should exist");
        assert!(step.ignore_errors());
        assert_eq!(step.timeout_ms(), 3000);
    }
}