apcore 0.19.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
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
// APCore Protocol — Built-in execution pipeline steps
// Spec reference: design-execution-pipeline.md (Section 3)

use std::collections::HashMap;
use std::time::Duration;

use async_trait::async_trait;

use crate::context::Identity;
use crate::errors::{ErrorCode, ModuleError};
use crate::executor::{has_schema, redact_sensitive, validate_against_schema};
use crate::pipeline::{ExecutionStrategy, PipelineContext, Step, StepResult};

// Macro for step metadata — execute is implemented manually per step.
macro_rules! step_meta {
    ($name:ident, $step_name:expr, $desc:expr, removable=$rm:expr, replaceable=$rp:expr, pure=$pure:expr) => {
        pub struct $name;

        impl $name {
            fn _name(&self) -> &str {
                $step_name
            }
            fn _description(&self) -> &str {
                $desc
            }
            fn _removable(&self) -> bool {
                $rm
            }
            fn _replaceable(&self) -> bool {
                $rp
            }
            fn _pure(&self) -> bool {
                $pure
            }
        }
    };
}

// Shared helper macro to implement the non-execute Step trait methods.
macro_rules! impl_step_meta {
    ($name:ident) => {
        fn name(&self) -> &str {
            self._name()
        }
        fn description(&self) -> &str {
            self._description()
        }
        fn removable(&self) -> bool {
            self._removable()
        }
        fn replaceable(&self) -> bool {
            self._replaceable()
        }
        fn pure(&self) -> bool {
            self._pure()
        }
    };
}

step_meta!(
    BuiltinContextCreation,
    "context_creation",
    "Create or inherit execution context",
    removable = false,
    replaceable = false,
    pure = true
);
step_meta!(
    BuiltinCallChainGuard,
    "call_chain_guard",
    "Validate call depth and module repeat limits",
    removable = true,
    replaceable = true,
    pure = true
);
step_meta!(
    BuiltinModuleLookup,
    "module_lookup",
    "Resolve module from registry by ID",
    removable = false,
    replaceable = false,
    pure = true
);
step_meta!(
    BuiltinACLCheck,
    "acl_check",
    "Enforce access control rules",
    removable = true,
    replaceable = true,
    pure = true
);
step_meta!(
    BuiltinApprovalGate,
    "approval_gate",
    "Handle human or AI approval flow",
    removable = true,
    replaceable = true,
    pure = false
);
step_meta!(
    BuiltinInputValidation,
    "input_validation",
    "Validate inputs against schema",
    removable = true,
    replaceable = true,
    pure = true
);
step_meta!(
    BuiltinMiddlewareBefore,
    "middleware_before",
    "Execute before-middleware chain",
    removable = true,
    replaceable = false,
    pure = false
);
step_meta!(
    BuiltinExecute,
    "execute",
    "Invoke the module with timeout",
    removable = false,
    replaceable = true,
    pure = false
);
step_meta!(
    BuiltinOutputValidation,
    "output_validation",
    "Validate outputs against schema",
    removable = true,
    replaceable = true,
    pure = true
);
step_meta!(
    BuiltinMiddlewareAfter,
    "middleware_after",
    "Execute after-middleware chain",
    removable = true,
    replaceable = false,
    pure = false
);
step_meta!(
    BuiltinReturnResult,
    "return_result",
    "Finalize and return output",
    removable = false,
    replaceable = false,
    pure = true
);

// ---------------------------------------------------------------------------
// Step implementations
// ---------------------------------------------------------------------------

#[async_trait]
impl Step for BuiltinContextCreation {
    impl_step_meta!(BuiltinContextCreation);
    fn provides(&self) -> &[&str] {
        &["context"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // If the context has no caller_id, default to @external.
        if ctx.context.caller_id.is_none() {
            ctx.context = crate::context::Context::new(Identity::new(
                "@external".to_string(),
                "external".to_string(),
                vec![],
                HashMap::new(),
            ));
        }
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinCallChainGuard {
    impl_step_meta!(BuiltinCallChainGuard);
    fn requires(&self) -> &[&str] {
        &["context"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // INVARIANT: Executor::inject_resources sets config before any step runs.
        let config = ctx
            .config
            .as_ref()
            .expect("config must be injected into PipelineContext");
        crate::utils::guard_call_chain_with_repeat(
            &ctx.context,
            &ctx.module_id,
            config.executor.max_call_depth,
            config.executor.max_module_repeat as usize,
        )?;
        // Create child context (adds module_id to call_chain).
        ctx.context = ctx.context.child(&ctx.module_id);
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinModuleLookup {
    impl_step_meta!(BuiltinModuleLookup);
    fn provides(&self) -> &[&str] {
        &["module"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // INVARIANT: Executor::inject_resources sets registry before any step runs.
        let registry = ctx
            .registry
            .as_ref()
            .expect("registry must be injected into PipelineContext");
        let module = registry.get(&ctx.module_id).ok_or_else(|| {
            ModuleError::new(
                ErrorCode::ModuleNotFound,
                format!("Module '{}' not found in registry", ctx.module_id),
            )
        })?;
        // Check if the module is disabled before proceeding.
        if let Some(false) = registry.is_enabled(&ctx.module_id) {
            return Err(ModuleError::new(
                ErrorCode::ModuleDisabled,
                format!("Module '{}' is disabled", ctx.module_id),
            ));
        }
        ctx.module = Some(module.clone());

        // Early input redaction: set context.redacted_inputs BEFORE
        // middleware runs (step 6), so logging sees redacted data.
        let input_schema = module.input_schema().clone();
        if has_schema(&input_schema) {
            let redacted = redact_sensitive(&ctx.inputs, &input_schema);
            ctx.context.redacted_inputs = Some(
                redacted
                    .as_object()
                    .cloned()
                    .unwrap_or_default()
                    .into_iter()
                    .collect(),
            );
        } else {
            ctx.context.redacted_inputs = Some(
                ctx.inputs
                    .as_object()
                    .cloned()
                    .unwrap_or_default()
                    .into_iter()
                    .collect(),
            );
        }

        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinACLCheck {
    impl_step_meta!(BuiltinACLCheck);
    fn requires(&self) -> &[&str] {
        &["context", "module"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        if let Some(ref acl) = ctx.acl {
            let caller_id = ctx.context.caller_id.as_deref();
            let allowed = acl.check(caller_id, &ctx.module_id, Some(&ctx.context));
            if !allowed {
                return Err(ModuleError::new(
                    ErrorCode::ACLDenied,
                    format!(
                        "Access denied: caller '{:?}' cannot access module '{}'",
                        caller_id, ctx.module_id
                    ),
                ));
            }
        }
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinApprovalGate {
    impl_step_meta!(BuiltinApprovalGate);
    fn requires(&self) -> &[&str] {
        &["context", "module"]
    }

    #[allow(clippy::too_many_lines)] // approval gate logic is inherently multi-step; splitting would obscure the protocol flow
    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        let handler = match ctx.approval_handler {
            Some(ref h) => h.clone(),
            None => return Ok(StepResult::continue_step()),
        };

        // INVARIANT: Executor::inject_resources sets registry before any step runs.
        let registry = ctx
            .registry
            .as_ref()
            .expect("registry must be injected into PipelineContext");

        let desc = match registry.get_definition(&ctx.module_id) {
            Some(d) if d.annotations.as_ref().is_some_and(|a| a.requires_approval) => d,
            _ => return Ok(StepResult::continue_step()),
        };
        let _ = desc; // used only for the requires_approval check above

        // Phase B: check for _approval_token in inputs.
        let approval_result = if let Some(token) = ctx
            .inputs
            .as_object()
            .and_then(|obj| obj.get("_approval_token"))
        {
            let token_str = match token.as_str() {
                Some(s) => s.to_string(),
                None => {
                    return Err(ModuleError::new(
                        ErrorCode::GeneralInvalidInput,
                        format!(
                            "_approval_token must be a string, got {}",
                            match token {
                                serde_json::Value::Number(_) => "number",
                                serde_json::Value::Bool(_) => "boolean",
                                serde_json::Value::Array(_) => "array",
                                serde_json::Value::Object(_) => "object",
                                serde_json::Value::Null => "null",
                                serde_json::Value::String(_) => "string", // covered by as_str() above
                            }
                        ),
                    ));
                }
            };
            // Strip _approval_token from inputs.
            if let Some(obj) = ctx.inputs.as_object_mut() {
                obj.remove("_approval_token");
            }
            handler.check_approval(&token_str).await?
        } else {
            let request = crate::approval::ApprovalRequest {
                module_id: ctx.module_id.clone(),
                arguments: ctx.inputs.clone(),
                context: Some(ctx.context.clone()),
                annotations: crate::module::ModuleAnnotations::default(),
                description: None,
                tags: vec![],
            };
            handler.request_approval(&request).await?
        };

        match approval_result.status.as_str() {
            "approved" => {}
            "rejected" => {
                return Err(ModuleError::new(
                    ErrorCode::ApprovalDenied,
                    format!(
                        "Approval denied for module '{}': {}",
                        ctx.module_id,
                        approval_result
                            .reason
                            .unwrap_or_else(|| "no reason given".to_string())
                    ),
                ));
            }
            "timeout" => {
                return Err(ModuleError::new(
                    ErrorCode::ApprovalTimeout,
                    format!(
                        "Approval timed out for module '{}': {}",
                        ctx.module_id,
                        approval_result
                            .reason
                            .unwrap_or_else(|| "no reason given".to_string())
                    ),
                ));
            }
            "pending" => {
                return Err(ModuleError::new(
                    ErrorCode::ApprovalPending,
                    format!(
                        "Approval pending for module '{}': {}",
                        ctx.module_id,
                        approval_result
                            .reason
                            .unwrap_or_else(|| "no reason given".to_string())
                    ),
                ));
            }
            _ => {
                tracing::warn!(
                    module_id = %ctx.module_id,
                    status = %approval_result.status,
                    "Unknown approval status, treating as denied"
                );
                return Err(ModuleError::new(
                    ErrorCode::ApprovalDenied,
                    format!(
                        "Approval denied for module '{}': unknown status '{}'",
                        ctx.module_id, approval_result.status
                    ),
                ));
            }
        }

        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinInputValidation {
    impl_step_meta!(BuiltinInputValidation);
    fn requires(&self) -> &[&str] {
        &["module"]
    }
    fn provides(&self) -> &[&str] {
        &["validated_inputs"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // INVARIANT: BuiltinModuleLookup runs before this step and sets ctx.module.
        let module = ctx
            .module
            .as_ref()
            .expect("module must be resolved before input_validation");
        let input_schema = module.input_schema();
        validate_against_schema(&ctx.inputs, &input_schema, "Input")?;

        // Store redacted inputs on context.
        if has_schema(&input_schema) {
            let redacted = redact_sensitive(&ctx.inputs, &input_schema);
            ctx.context.redacted_inputs = Some(
                redacted
                    .as_object()
                    .cloned()
                    .unwrap_or_default()
                    .into_iter()
                    .collect(),
            );
        }
        ctx.validated_inputs = Some(ctx.inputs.clone());
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinMiddlewareBefore {
    impl_step_meta!(BuiltinMiddlewareBefore);

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        let middleware_manager = match ctx.middleware_manager {
            Some(ref mm) => mm.clone(),
            None => return Ok(StepResult::continue_step()),
        };

        let (modified_inputs, executed) = match middleware_manager
            .execute_before(&ctx.module_id, ctx.inputs.clone(), &ctx.context)
            .await
        {
            Ok((inputs, executed)) => (inputs, executed),
            Err(e) => {
                // On middleware before error, run on_error for recovery.
                let recovery = middleware_manager
                    .execute_on_error(&ctx.module_id, ctx.inputs.clone(), &e, &ctx.context, &[])
                    .await;
                if let Some(recovery_value) = recovery {
                    ctx.output = Some(recovery_value);
                    return Ok(StepResult::skip_to("return_result"));
                }
                return Err(e);
            }
        };
        ctx.inputs = modified_inputs;
        ctx.executed_middlewares = executed;
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinExecute {
    impl_step_meta!(BuiltinExecute);
    fn requires(&self) -> &[&str] {
        &["module"]
    }
    fn provides(&self) -> &[&str] {
        &["output"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // INVARIANT: BuiltinModuleLookup runs before this step and sets ctx.module.
        let module = ctx
            .module
            .as_ref()
            .expect("module must be resolved before execute")
            .clone();
        // INVARIANT: Executor::inject_resources sets config before any step runs.
        let config = ctx
            .config
            .as_ref()
            .expect("config must be injected into PipelineContext");

        // Compute effective timeout: clamp to remaining global deadline (dual-timeout model).
        let mut timeout_ms = config.executor.default_timeout;
        if let Some(deadline) = ctx.context.global_deadline {
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs_f64();
            #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
            // intentional: remaining_ms is non-negative and fits in u64 for any reasonable deadline
            let remaining_ms = ((deadline - now) * 1000.0) as u64;
            if remaining_ms == 0 {
                return Err(ModuleError::new(
                    ErrorCode::ModuleTimeout,
                    format!(
                        "Module '{}' execution aborted: global deadline already exceeded",
                        ctx.module_id
                    ),
                ));
            }
            if timeout_ms == 0 || remaining_ms < timeout_ms {
                timeout_ms = remaining_ms;
            }
        }

        // Note: Streaming is handled exclusively by `Executor::stream()`, which
        // bypasses this step entirely. `ctx.stream` is intentionally ignored
        // here — `BuiltinExecute` always calls `module.execute()` for the
        // unary path. See `Executor::stream` for the streaming pipeline.

        let execute_result = if timeout_ms > 0 {
            match tokio::time::timeout(
                Duration::from_millis(timeout_ms),
                module.execute(ctx.inputs.clone(), &ctx.context),
            )
            .await
            {
                Ok(result) => result,
                Err(_elapsed) => Err(ModuleError::new(
                    ErrorCode::ModuleTimeout,
                    format!(
                        "Module '{}' execution timed out after {}ms",
                        ctx.module_id, timeout_ms
                    ),
                )),
            }
        } else {
            module.execute(ctx.inputs.clone(), &ctx.context).await
        };

        match execute_result {
            Ok(output) => {
                ctx.output = Some(output);
                Ok(StepResult::continue_step())
            }
            Err(e) => {
                // On execution error, attempt middleware recovery.
                if let Some(ref mm) = ctx.middleware_manager {
                    let recovery = mm
                        .execute_on_error(
                            &ctx.module_id,
                            ctx.inputs.clone(),
                            &e,
                            &ctx.context,
                            &ctx.executed_middlewares,
                        )
                        .await;
                    if let Some(recovery_value) = recovery {
                        ctx.output = Some(recovery_value);
                        return Ok(StepResult::skip_to("return_result"));
                    }
                }
                Err(e)
            }
        }
    }
}

#[async_trait]
impl Step for BuiltinOutputValidation {
    impl_step_meta!(BuiltinOutputValidation);
    fn requires(&self) -> &[&str] {
        &["module", "output"]
    }
    fn provides(&self) -> &[&str] {
        &["validated_output"]
    }

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // In dry_run mode, execute step is skipped so output may be absent.
        let Some(output) = ctx.output.as_ref() else {
            return Ok(StepResult::continue_step());
        };
        // INVARIANT: BuiltinModuleLookup runs before this step and sets ctx.module.
        let module = ctx
            .module
            .as_ref()
            .expect("module must be resolved before output_validation");
        let output_schema = module.output_schema();
        validate_against_schema(output, &output_schema, "Output")?;

        // Store redacted output as first-class Context field (symmetric with
        // redacted_inputs). Previously stored under data["_apcore.executor.
        // redacted_output"] which was filtered out by serialize().
        if has_schema(&output_schema) {
            let redacted = redact_sensitive(output, &output_schema);
            ctx.context.redacted_output = Some(
                redacted
                    .as_object()
                    .cloned()
                    .unwrap_or_default()
                    .into_iter()
                    .collect(),
            );
        }
        ctx.validated_output.clone_from(&ctx.output);
        Ok(StepResult::continue_step())
    }
}

#[async_trait]
impl Step for BuiltinMiddlewareAfter {
    impl_step_meta!(BuiltinMiddlewareAfter);

    async fn execute(&self, ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        let middleware_manager = match ctx.middleware_manager {
            Some(ref mm) => mm.clone(),
            None => return Ok(StepResult::continue_step()),
        };

        // INVARIANT: BuiltinExecute runs before this step and sets ctx.output.
        let output = ctx
            .output
            .take()
            .expect("output must be set before middleware_after");

        match middleware_manager
            .execute_after(&ctx.module_id, ctx.inputs.clone(), output, &ctx.context)
            .await
        {
            Ok(modified_output) => {
                ctx.output = Some(modified_output);
                Ok(StepResult::continue_step())
            }
            Err(e) => {
                // On middleware after error, run on_error for recovery.
                let recovery = middleware_manager
                    .execute_on_error(
                        &ctx.module_id,
                        ctx.inputs.clone(),
                        &e,
                        &ctx.context,
                        &ctx.executed_middlewares,
                    )
                    .await;
                if let Some(recovery_value) = recovery {
                    ctx.output = Some(recovery_value);
                    return Ok(StepResult::skip_to("return_result"));
                }
                Err(e)
            }
        }
    }
}

#[async_trait]
impl Step for BuiltinReturnResult {
    impl_step_meta!(BuiltinReturnResult);
    fn requires(&self) -> &[&str] {
        &["output"]
    }

    async fn execute(&self, _ctx: &mut PipelineContext) -> Result<StepResult, ModuleError> {
        // Output is already stored in ctx.output; PipelineEngine returns it.
        Ok(StepResult::continue_step())
    }
}

// ---------------------------------------------------------------------------
// Preset strategies
// ---------------------------------------------------------------------------

/// Build the standard 11-step execution strategy.
#[must_use]
pub fn build_standard_strategy() -> ExecutionStrategy {
    // INVARIANT: all step names are unique literals, so `new()` cannot fail.
    ExecutionStrategy::new(
        "standard",
        vec![
            Box::new(BuiltinContextCreation),
            Box::new(BuiltinCallChainGuard),
            Box::new(BuiltinModuleLookup),
            Box::new(BuiltinACLCheck),
            Box::new(BuiltinApprovalGate),
            Box::new(BuiltinMiddlewareBefore),
            Box::new(BuiltinInputValidation),
            Box::new(BuiltinExecute),
            Box::new(BuiltinOutputValidation),
            Box::new(BuiltinMiddlewareAfter),
            Box::new(BuiltinReturnResult),
        ],
    )
    // INVARIANT: the builtin step list above uses distinct types — duplicates would be a compile-time error.
    .expect("standard strategy should have unique step names")
}

/// Build the internal strategy (standard minus `acl_check` and `approval_gate`).
#[must_use]
pub fn build_internal_strategy() -> ExecutionStrategy {
    let mut s = build_standard_strategy();
    s.set_name("internal");
    s.remove("acl_check").ok();
    s.remove("approval_gate").ok();
    s
}

/// Build the testing strategy (standard minus `acl_check`, `approval_gate`, and `call_chain_guard`).
#[must_use]
pub fn build_testing_strategy() -> ExecutionStrategy {
    let mut s = build_standard_strategy();
    s.set_name("testing");
    s.remove("acl_check").ok();
    s.remove("approval_gate").ok();
    s.remove("call_chain_guard").ok();
    s
}

/// Build the performance strategy (standard minus `middleware_before` and `middleware_after`).
#[must_use]
pub fn build_performance_strategy() -> ExecutionStrategy {
    let mut s = build_standard_strategy();
    s.set_name("performance");
    s.remove("middleware_before").ok();
    s.remove("middleware_after").ok();
    s
}

/// Build a minimal strategy: context → lookup → execute → return only.
///
/// No safety checks, no ACL, no approval, no validation, no middleware.
/// Suitable for pre-validated internal hot paths. Use with caution.
#[must_use]
pub fn build_minimal_strategy() -> ExecutionStrategy {
    let mut s = build_standard_strategy();
    s.set_name("minimal");
    s.remove("call_chain_guard").ok();
    s.remove("acl_check").ok();
    s.remove("approval_gate").ok();
    s.remove("middleware_before").ok();
    s.remove("input_validation").ok();
    s.remove("output_validation").ok();
    s.remove("middleware_after").ok();
    s
}