ironclad-api 0.9.8

HTTP routes, WebSocket, auth, rate limiting, and dashboard for the Ironclad agent runtime
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
//! Unified pipeline configuration and entry-point presets.
//!
//! Replaces the ad-hoc divergence across the four entry points (`handlers.rs`,
//! `streaming.rs`, `channel_message.rs`, `scheduled_tasks.rs`) with a single
//! [`PipelineConfig`] that declaratively specifies which pipeline stages are
//! active. This makes it **impossible** to forget a stage because every path
//! constructs its config from one of the four preset constructors.
//!
//! ## Pipeline stages (in execution order)
//!
//! 1. **Injection defense** — block/sanitize via `ironclad_agent::injection`
//! 2. **Dedup tracking** — in-flight request deduplication via `DedupTracker`
//! 3. **Session resolution** — find or create the session for this turn
//! 4. **Decomposition gate** — evaluate whether to delegate subtasks
//! 5. **Delegated execution** — run `orchestrate-subagents` before inference
//! 6. **Specialist controls** — handle specialist creation control flows
//! 7. **Shortcut dispatch** — try execution shortcuts before LLM inference
//! 8. **Cache check** — look up semantic cache before LLM call
//! 9. **Inference** — Standard (ReAct tool loop) or Streaming (SSE)
//! 10. **Guard chain** — post-inference truth/integrity guards
//! 11. **Post-turn ingest** — background memory ingestion
//! 12. **Nickname refinement** — background LLM-driven session naming
//!
//! ## Security fixes integrated
//!
//! - **Cron injection defense**: `PipelineConfig::cron()` sets
//!   `injection_defense: true` — was completely absent from
//!   `scheduled_tasks.rs`.
//! - **Cache guard parity**: `cache_guard_set` uses `Cached` preset which
//!   includes `SubagentClaim` and `LiteraryQuoteRetry` (were missing).
//!
//! ## Feature matrix (authoritative)
//!
//! | Feature               | API | Stream | Channel | Cron  |
//! |-----------------------|-----|--------|---------|-------|
//! | injection_defense     |  ✓  |   ✓    |    ✓    |  ✓*   |
//! | dedup_tracking        |  ✓  |   ✓    |    ✓    |  ✗    |
//! | decomposition_gate    |  ✓  |   ✗    |    ✓    |  ✓    |
//! | delegated_execution   |  ✓  |   ✗    |    ✓    |  ✗    |
//! | shortcuts_enabled     |  ✓  |   ✗    |    ✓    |  ✓    |
//! | specialist_controls   |  ✗  |   ✗    |    ✓    |  ✗    |
//! | inference: Standard   |  ✓  |   ✗    |    ✓    |  ✓    |
//! | inference: Streaming  |  ✗  |   ✓    |    ✗    |  ✗    |
//! | guard_set: Full       |  ✓  |   ✗    |    ✓    |  ✓    |
//! | guard_set: Streaming  |  ✗  |   ✓    |    ✗    |  ✗    |
//! | cache_guard: Cached   |  ✓  |   ✗    |    ✓    |  ✓    |
//! | cache_enabled         |  ✓  |   ✓    |    ✓    |  ✓    |
//! | authority: ApiClaim   |  ✓  |   ✗    |    ✗    |  ✗    |
//! | authority: Channel    |  ✗  |   ✗    |    ✓    |  ✗    |
//! | authority: SelfGen    |  ✗  |   ✗    |    ✗    |  ✓    |
//! | authority: AuditOnly  |  ✗  |   ✓    |    ✗    |  ✗    |
//! | post_turn_ingest      |  ✓  |   ✓    |    ✓    |  ✓    |
//! | nickname_refinement   |  ✓  |   ✗    |    ✗    |  ✗    |
//! | inject_diagnostics    |  ✓  |   ✓    |    ✗    |  ✗    |
//!
//! `*` = security fix — was missing, now enabled.

use super::AppState;
use super::core;
use super::decomposition::DelegationProvenance;
#[cfg(test)]
use super::guard_registry::{GuardChain, guard_sets};

// ── Guard set presets ─────────────────────────────────────────────────────

/// Which guard set to apply to inference output.
///
/// Resolved to a concrete `GuardChain` via `GuardSetPreset::resolve()` at
/// pipeline execution time. Using an enum rather than a direct `GuardChain`
/// allows `PipelineConfig` to be `Clone + Debug` without requiring `Guard`
/// implementors to be cloneable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum GuardSetPreset {
    /// All 10 guards — used for fresh inference on standard paths.
    Full,
    /// All 10 guards — used for cached responses. Fixes previously-missing
    /// `SubagentClaim` and `LiteraryQuoteRetry`.
    Cached,
    /// 6 guards — reduced subset for streaming where retries are impractical.
    Streaming,
    /// No guards applied. Used for paths where guards are inapplicable
    /// (e.g., cache guard set on the streaming path which doesn't use cache).
    None,
}

impl GuardSetPreset {
    /// Materialize the preset into a concrete guard chain.
    #[cfg(test)]
    pub fn resolve(self) -> GuardChain {
        match self {
            Self::Full => guard_sets::full(),
            Self::Cached => guard_sets::cached(),
            Self::Streaming => guard_sets::streaming(),
            Self::None => GuardChain::empty(),
        }
    }
}

// ── Session resolution modes ──────────────────────────────────────────────

/// How the session is resolved for this pipeline execution.
///
/// Each entry point has different session semantics:
/// - API: optional `session_id` in request body, or create from web scope
/// - Channel: scope derived from `platform:chat_id`
/// - Cron: dedicated agent-scoped session
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum SessionResolutionMode {
    /// API/Streaming: session_id provided in request body, or create from
    /// web scope if not provided.
    FromBody,
    /// Channel: scope derived from platform + chat_id.
    FromChannel {
        /// The chat platform identifier (e.g., "telegram", "discord").
        platform: String,
    },
    /// Cron/scheduled: find_or_create with agent-scoped session.
    Dedicated,
    /// Pre-resolved: session already created by caller (useful for
    /// testing or specialized workflows).
    #[cfg(test)]
    Provided { session_id: String },
}

// ── Authority modes ───────────────────────────────────────────────────────

/// How authority (RBAC) is determined for this pipeline execution.
///
/// Authority controls which tools the agent is permitted to execute.
/// Different entry points have different trust models.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum AuthorityMode {
    /// API: resolve via `resolve_api_claim()`. Supports reduced authority
    /// from injection caution-level detection.
    ApiClaim,
    /// Channel: resolve via `resolve_channel_claim()` with sender context
    /// (allow-list membership, trusted sender IDs, threat score).
    ChannelClaim,
    /// Cron: hardcoded `InputAuthority::SelfGenerated`. Internal system
    /// caller with no external user input.
    SelfGenerated,
    /// Streaming: authority is logged for audit trail but NOT enforced
    /// because the streaming path does not execute tools.
    AuditOnly,
}

// ── Inference modes ───────────────────────────────────────────────────────

/// How inference is executed.
///
/// The two modes have fundamentally different execution models:
/// - Standard: full ReAct tool loop, shortcut dispatch, guard chain
/// - Streaming: direct provider SSE stream, no ReAct, minimal post-processing
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum InferenceMode {
    /// Full inference with ReAct tool loop, shortcut dispatch, and guard chain.
    Standard,
    /// SSE streaming — direct provider call with chunk-by-chunk delivery.
    /// No ReAct loop, no shortcut dispatch. Reduced guard set applied
    /// post-accumulation.
    Streaming,
}

// ── Pipeline configuration ────────────────────────────────────────────────

/// Declarative configuration for the unified pipeline.
///
/// Each entry point constructs a `PipelineConfig` via one of the four preset
/// constructors ([`api`], [`streaming`], [`channel`], [`cron`]), making it
/// impossible to forget a pipeline stage.
///
/// Every boolean flag corresponds to a pipeline stage. If the flag is `false`,
/// the stage is completely skipped — no branching inside the stage itself.
///
/// [`api`]: PipelineConfig::api
/// [`streaming`]: PipelineConfig::streaming
/// [`channel`]: PipelineConfig::channel
/// [`cron`]: PipelineConfig::cron
#[derive(Debug, Clone)]
#[allow(dead_code)] // fields set by constructors; some consumed only by tests until pipeline phases are wired
pub(super) struct PipelineConfig {
    // ── Input defense ─────────────────────────────────────────────
    /// Run injection detection: block (>0.7), sanitize (0.3-0.7), pass (<0.3).
    pub injection_defense: bool,
    /// Track in-flight duplicates and reject concurrent identical requests.
    pub dedup_tracking: bool,

    // ── Session ──────────────────────────────────────────────────
    /// How to resolve or create the session for this turn.
    pub session_resolution: SessionResolutionMode,

    // ── Pre-inference ────────────────────────────────────────────
    /// Evaluate the decomposition gate for potential delegation.
    pub decomposition_gate: bool,
    /// Execute delegated subtasks via `orchestrate-subagents` before inference.
    pub delegated_execution: bool,
    /// Handle specialist creation control flows (channel-only feature).
    pub specialist_controls: bool,
    /// Try execution shortcuts (acknowledgement, model identity, etc.) before LLM.
    pub shortcuts_enabled: bool,

    // ── Inference ────────────────────────────────────────────────
    /// Standard (ReAct loop) or Streaming (SSE, no ReAct).
    pub inference_mode: InferenceMode,
    /// Guard set applied to fresh inference output.
    pub guard_set: GuardSetPreset,
    /// Guard set applied to cached responses.
    pub cache_guard_set: GuardSetPreset,
    /// Whether semantic cache is checked before inference.
    pub cache_enabled: bool,

    // ── Authority ────────────────────────────────────────────────
    /// How authority (RBAC) is resolved for tool execution.
    pub authority_mode: AuthorityMode,

    // ── Post-inference ──────────────────────────────────────────
    /// Run background memory ingestion after the turn completes.
    pub post_turn_ingest: bool,
    /// Run background nickname refinement after 4+ messages.
    pub nickname_refinement: bool,

    // ── Output control ──────────────────────────────────────────
    /// Inject diagnostics metadata into system prompt.
    pub inject_diagnostics: bool,

    // ── Channel label ────────────────────────────────────────────
    /// Human-readable label for logging, cost tracking, and event bus.
    pub channel_label: String,
}

// ── Preset constructors ───────────────────────────────────────────────────

impl PipelineConfig {
    /// API endpoint (`/agent/message`): all features enabled.
    ///
    /// Injection defense, dedup, decomposition, delegated execution,
    /// shortcuts, full guard chain, cache, ReAct, diagnostics, nickname
    /// refinement. Authority via `resolve_api_claim()`.
    pub fn api() -> Self {
        Self {
            injection_defense: true,
            dedup_tracking: true,
            session_resolution: SessionResolutionMode::FromBody,
            decomposition_gate: true,
            delegated_execution: true,
            specialist_controls: false,
            shortcuts_enabled: true,
            inference_mode: InferenceMode::Standard,
            guard_set: GuardSetPreset::Full,
            cache_guard_set: GuardSetPreset::Cached,
            cache_enabled: true,
            authority_mode: AuthorityMode::ApiClaim,
            post_turn_ingest: true,
            nickname_refinement: true,
            inject_diagnostics: true,
            channel_label: "api".into(),
        }
    }

    /// SSE streaming endpoint (`/agent/message/stream`).
    ///
    /// Injection defense and dedup are active. No ReAct loop, no shortcuts,
    /// no decomposition, no delegated execution. Authority is audit-only
    /// (no tool execution on this path). Streaming guard set applied
    /// post-accumulation.
    pub fn streaming() -> Self {
        Self {
            injection_defense: true,
            dedup_tracking: true,
            session_resolution: SessionResolutionMode::FromBody,
            decomposition_gate: false,
            delegated_execution: false,
            specialist_controls: false,
            shortcuts_enabled: false,
            inference_mode: InferenceMode::Streaming,
            guard_set: GuardSetPreset::Streaming,
            cache_guard_set: GuardSetPreset::None,
            cache_enabled: true, // streaming writes to cache post-stream
            authority_mode: AuthorityMode::AuditOnly,
            post_turn_ingest: true,
            nickname_refinement: false,
            inject_diagnostics: true,
            channel_label: "api-stream".into(),
        }
    }

    /// Channel message (Telegram, Discord, Signal, Email, etc.).
    ///
    /// Full pipeline with channel-specific authority resolution.
    /// All core features enabled. Channel-specific behaviors that are
    /// NOT part of the pipeline config (handled by channel handler wrapper):
    /// - Addressability filter
    /// - Multimodal enrichment
    /// - Typing/thinking indicators
    /// - Correction turn detection
    /// - Skill-first fulfillment
    /// - Bot command handling
    /// - Reply formatting (telegram normalize, etc.)
    pub fn channel(platform: &str) -> Self {
        Self {
            injection_defense: true,
            dedup_tracking: true,
            session_resolution: SessionResolutionMode::FromChannel {
                platform: platform.to_string(),
            },
            decomposition_gate: true,
            delegated_execution: true,
            specialist_controls: true,
            shortcuts_enabled: true,
            inference_mode: InferenceMode::Standard,
            guard_set: GuardSetPreset::Full,
            cache_guard_set: GuardSetPreset::Cached,
            cache_enabled: true,
            authority_mode: AuthorityMode::ChannelClaim,
            post_turn_ingest: true,
            nickname_refinement: false,
            inject_diagnostics: false,
            channel_label: platform.to_string(),
        }
    }

    /// Cron/scheduled task execution: internal system caller.
    ///
    /// **Security fix**: injection defense is now enabled — was completely
    /// absent from `scheduled_tasks.rs`.
    ///
    /// No dedup (cron tasks are guaranteed unique by scheduler). Authority
    /// is `SelfGenerated` (internal system caller). No delegated execution
    /// (cron tasks go through standard inference with tool access). No
    /// nickname refinement.
    pub fn cron() -> Self {
        Self {
            injection_defense: true, // SECURITY FIX: was missing!
            dedup_tracking: false,
            session_resolution: SessionResolutionMode::Dedicated,
            decomposition_gate: true,
            delegated_execution: false, // cron doesn't pre-execute delegation
            specialist_controls: false,
            shortcuts_enabled: true,
            inference_mode: InferenceMode::Standard,
            guard_set: GuardSetPreset::Full,
            cache_guard_set: GuardSetPreset::Cached,
            cache_enabled: true,
            authority_mode: AuthorityMode::SelfGenerated,
            post_turn_ingest: true,
            nickname_refinement: false,
            inject_diagnostics: false,
            channel_label: "cron".into(),
        }
    }
}

// ── Stage predicates ──────────────────────────────────────────────────────
//
// Convenience methods for querying pipeline capabilities. These are used by
// `execute_unified_pipeline()` (Phase 5) to branch on stage availability.

#[cfg(test)]
impl PipelineConfig {
    /// Whether this pipeline uses the standard ReAct inference path.
    pub fn is_standard_inference(&self) -> bool {
        self.inference_mode == InferenceMode::Standard
    }

    /// Whether this pipeline uses the streaming inference path.
    pub fn is_streaming_inference(&self) -> bool {
        self.inference_mode == InferenceMode::Streaming
    }

    /// Whether authority needs to be enforced for tool execution.
    /// Returns `false` for `AuditOnly` (streaming) and `SelfGenerated` (cron).
    pub fn enforces_authority(&self) -> bool {
        matches!(
            self.authority_mode,
            AuthorityMode::ApiClaim | AuthorityMode::ChannelClaim
        )
    }

    /// Whether this pipeline can execute tools (ReAct loop).
    /// Streaming never executes tools.
    pub fn can_execute_tools(&self) -> bool {
        self.inference_mode == InferenceMode::Standard
    }

    /// Whether this pipeline resolves the session from the request body.
    /// True for API and streaming paths.
    pub fn resolves_session_from_body(&self) -> bool {
        matches!(self.session_resolution, SessionResolutionMode::FromBody)
    }

    /// Whether this is a channel pipeline.
    pub fn is_channel(&self) -> bool {
        matches!(
            self.session_resolution,
            SessionResolutionMode::FromChannel { .. }
        )
    }

    /// Whether this is a cron pipeline.
    pub fn is_cron(&self) -> bool {
        matches!(self.session_resolution, SessionResolutionMode::Dedicated)
            && matches!(self.authority_mode, AuthorityMode::SelfGenerated)
    }
}

// ── Unified pipeline input ──────────────────────────────────────────────

/// Input to `execute_unified_pipeline()`.
///
/// Callers are responsible for:
/// 1. Injection defense (before constructing this struct)
/// 2. Dedup tracking (before constructing this struct, ideally via `DedupGuard`)
/// 3. Session resolution (providing the resolved `session_id`)
/// 4. Storing the user message
/// 5. Creating the turn record
///
/// The pipeline handles everything from `prepare_inference()` through
/// `execute_inference_pipeline()`, driven by `PipelineConfig` feature flags.
pub(super) struct UnifiedPipelineInput<'a> {
    pub state: &'a AppState,
    pub config: &'a PipelineConfig,
    pub session_id: &'a str,
    pub user_content: &'a str,
    pub turn_id: &'a str,
    pub is_correction_turn: bool,
    /// Delegation workflow note from `apply_decomposition_decision()`.
    pub delegation_workflow_note: Option<String>,
    /// Gate system note from `build_gate_system_note()`.
    pub gate_system_note: Option<String>,
    /// Note from delegated `orchestrate-subagents` execution.
    pub delegated_execution_note: Option<String>,
    /// Pre-computed delegation provenance (from delegation step).
    pub delegation_provenance: DelegationProvenance,
}

// ── Unified pipeline execution ──────────────────────────────────────────

/// Execute the unified inference pipeline for Standard inference paths.
///
/// This is the single entry point for **all non-streaming inference**,
/// replacing the duplicated ceremony in `handlers.rs`, `channel_message.rs`,
/// and `scheduled_tasks.rs`.
///
/// ## Stages handled
///
/// 1. Read agent config (name, ID, primary model, tier config)
/// 2. Read personality (OS text, firmware text)
/// 3. Build `InferenceInput` from `PipelineConfig` + caller-provided context
/// 4. `prepare_inference()` — model selection, embedding, RAG, history assembly
/// 5. `execute_inference_pipeline()` — cache check → inference + ReAct → store
///    assistant → record cost → post-turn ingest → cache store
///
/// ## Stages NOT handled (caller responsibility)
///
/// - Injection defense (`check_injection` / `sanitize`)
/// - Dedup tracking (`DedupGuard` RAII)
/// - Session resolution (various per entry point)
/// - User message storage (`append_message`)
/// - Turn pre-creation (`create_turn_with_id`)
/// - Decomposition gate evaluation (`evaluate_decomposition_gate`)
/// - Delegated execution (`orchestrate-subagents`)
/// - Output formatting (JSON, channel reply, etc.)
/// - Nickname refinement (API-only post-step)
pub(super) async fn execute_unified_pipeline(
    input: UnifiedPipelineInput<'_>,
) -> Result<core::PipelineResult, String> {
    let config = input.state.config.read().await;
    let agent_name = config.agent.name.clone();
    let agent_id = config.agent.id.clone();
    let primary_model = config.models.primary.clone();
    let tier_adapt = config.tier_adapt.clone();
    drop(config);

    let personality = input.state.personality.read().await;
    let os_text = personality.os_text.clone();
    let firmware_text = personality.firmware_text.clone();
    drop(personality);

    let inference_input = core::InferenceInput {
        state: input.state,
        session_id: input.session_id,
        user_content: input.user_content,
        turn_id: input.turn_id,
        channel_label: &input.config.channel_label,
        agent_name,
        agent_id,
        os_text,
        firmware_text,
        primary_model,
        tier_adapt,
        delegation_workflow_note: input.delegation_workflow_note,
        inject_diagnostics: input.config.inject_diagnostics,
        gate_system_note: input.gate_system_note,
        delegated_execution_note: input.delegated_execution_note,
        is_correction_turn: input.is_correction_turn,
    };

    let prepared = core::prepare_inference(&inference_input).await?;

    // Resolve authority from PipelineConfig.
    let authority = match input.config.authority_mode {
        AuthorityMode::SelfGenerated => ironclad_core::InputAuthority::SelfGenerated,
        AuthorityMode::AuditOnly => ironclad_core::InputAuthority::SelfGenerated,
        // ApiClaim and ChannelClaim must be resolved by the caller and passed
        // as SelfGenerated only when the caller has verified the claim. For the
        // unified pipeline, callers that need claim-based authority should
        // resolve it before calling and set authority_mode to SelfGenerated with
        // the resolved authority. This case should not be reached in practice.
        AuthorityMode::ApiClaim | AuthorityMode::ChannelClaim => {
            tracing::warn!(
                mode = ?input.config.authority_mode,
                "execute_unified_pipeline called with claim-based authority — \
                 caller should resolve authority before calling"
            );
            ironclad_core::InputAuthority::SelfGenerated
        }
    };

    let mut provenance = input.delegation_provenance;
    core::execute_inference_pipeline(
        input.state,
        &prepared,
        input.session_id,
        input.user_content,
        input.turn_id,
        authority,
        Some(&input.config.channel_label),
        &mut provenance,
    )
    .await
}

/// Execute the unified pipeline with an explicit authority override.
///
/// Used by entry points that resolve authority externally (API via
/// `resolve_api_claim()`, channels via allow-list). Identical to
/// `execute_unified_pipeline()` except authority comes from the caller
/// instead of being derived from `PipelineConfig::authority_mode`.
pub(super) async fn execute_unified_pipeline_with_authority(
    input: UnifiedPipelineInput<'_>,
    authority: ironclad_core::InputAuthority,
) -> Result<core::PipelineResult, String> {
    let config = input.state.config.read().await;
    let agent_name = config.agent.name.clone();
    let agent_id = config.agent.id.clone();
    let primary_model = config.models.primary.clone();
    let tier_adapt = config.tier_adapt.clone();
    drop(config);

    let personality = input.state.personality.read().await;
    let os_text = personality.os_text.clone();
    let firmware_text = personality.firmware_text.clone();
    drop(personality);

    let inference_input = core::InferenceInput {
        state: input.state,
        session_id: input.session_id,
        user_content: input.user_content,
        turn_id: input.turn_id,
        channel_label: &input.config.channel_label,
        agent_name,
        agent_id,
        os_text,
        firmware_text,
        primary_model,
        tier_adapt,
        delegation_workflow_note: input.delegation_workflow_note,
        inject_diagnostics: input.config.inject_diagnostics,
        gate_system_note: input.gate_system_note,
        delegated_execution_note: input.delegated_execution_note,
        is_correction_turn: input.is_correction_turn,
    };

    let prepared = core::prepare_inference(&inference_input).await?;
    let mut provenance = input.delegation_provenance;
    core::execute_inference_pipeline(
        input.state,
        &prepared,
        input.session_id,
        input.user_content,
        input.turn_id,
        authority,
        Some(&input.config.channel_label),
        &mut provenance,
    )
    .await
}

/// Execute the unified pipeline returning both the result and the prepared
/// inference, allowing callers to inspect or modify prepared state (e.g.,
/// channel model-switch logic).
pub(super) async fn prepare_unified_pipeline(
    input: &UnifiedPipelineInput<'_>,
) -> Result<core::PreparedInference, String> {
    let config = input.state.config.read().await;
    let agent_name = config.agent.name.clone();
    let agent_id = config.agent.id.clone();
    let primary_model = config.models.primary.clone();
    let tier_adapt = config.tier_adapt.clone();
    drop(config);

    let personality = input.state.personality.read().await;
    let os_text = personality.os_text.clone();
    let firmware_text = personality.firmware_text.clone();
    drop(personality);

    let inference_input = core::InferenceInput {
        state: input.state,
        session_id: input.session_id,
        user_content: input.user_content,
        turn_id: input.turn_id,
        channel_label: &input.config.channel_label,
        agent_name,
        agent_id,
        os_text,
        firmware_text,
        primary_model,
        tier_adapt,
        delegation_workflow_note: input.delegation_workflow_note.clone(),
        inject_diagnostics: input.config.inject_diagnostics,
        gate_system_note: input.gate_system_note.clone(),
        delegated_execution_note: input.delegated_execution_note.clone(),
        is_correction_turn: input.is_correction_turn,
    };

    core::prepare_inference(&inference_input).await
}

// ── Tests ─────────────────────────────────────────────────────────────────

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

    // ── Preset field verification ─────────────────────────────────────

    #[test]
    fn api_preset_enables_all_core_features() {
        let cfg = PipelineConfig::api();
        assert!(cfg.injection_defense);
        assert!(cfg.dedup_tracking);
        assert!(cfg.decomposition_gate);
        assert!(cfg.delegated_execution);
        assert!(cfg.shortcuts_enabled);
        assert!(cfg.cache_enabled);
        assert!(cfg.post_turn_ingest);
        assert!(cfg.nickname_refinement);
        assert!(cfg.inject_diagnostics);
        assert!(!cfg.specialist_controls); // API doesn't have specialist controls
        assert_eq!(cfg.inference_mode, InferenceMode::Standard);
        assert_eq!(cfg.guard_set, GuardSetPreset::Full);
        assert_eq!(cfg.cache_guard_set, GuardSetPreset::Cached);
        assert_eq!(cfg.authority_mode, AuthorityMode::ApiClaim);
        assert_eq!(cfg.channel_label, "api");
        assert_eq!(cfg.session_resolution, SessionResolutionMode::FromBody);
    }

    #[test]
    fn streaming_preset_disables_react_features() {
        let cfg = PipelineConfig::streaming();
        assert!(cfg.injection_defense);
        assert!(cfg.dedup_tracking);
        // Streaming disables all pre-inference stages
        assert!(!cfg.decomposition_gate);
        assert!(!cfg.delegated_execution);
        assert!(!cfg.shortcuts_enabled);
        assert!(!cfg.specialist_controls);
        // Streaming mode with reduced guards
        assert_eq!(cfg.inference_mode, InferenceMode::Streaming);
        assert_eq!(cfg.guard_set, GuardSetPreset::Streaming);
        assert_eq!(cfg.cache_guard_set, GuardSetPreset::None);
        // No nickname refinement on streaming
        assert!(!cfg.nickname_refinement);
        // But post-turn ingest and cache are on
        assert!(cfg.post_turn_ingest);
        assert!(cfg.cache_enabled);
        assert_eq!(cfg.authority_mode, AuthorityMode::AuditOnly);
        assert_eq!(cfg.channel_label, "api-stream");
    }

    #[test]
    fn channel_preset_enables_specialist_controls() {
        let cfg = PipelineConfig::channel("telegram");
        assert!(cfg.injection_defense);
        assert!(cfg.dedup_tracking);
        assert!(cfg.decomposition_gate);
        assert!(cfg.delegated_execution);
        assert!(cfg.shortcuts_enabled);
        assert!(cfg.specialist_controls); // only channel has this
        assert!(cfg.cache_enabled);
        assert!(cfg.post_turn_ingest);
        assert!(!cfg.nickname_refinement); // channels don't refine nicknames
        assert!(!cfg.inject_diagnostics); // channels don't inject diagnostics
        assert_eq!(cfg.inference_mode, InferenceMode::Standard);
        assert_eq!(cfg.guard_set, GuardSetPreset::Full);
        assert_eq!(cfg.cache_guard_set, GuardSetPreset::Cached);
        assert_eq!(cfg.authority_mode, AuthorityMode::ChannelClaim);
        assert_eq!(cfg.channel_label, "telegram");
        assert_eq!(
            cfg.session_resolution,
            SessionResolutionMode::FromChannel {
                platform: "telegram".into()
            }
        );
    }

    #[test]
    fn channel_preset_uses_platform_as_label() {
        let telegram = PipelineConfig::channel("telegram");
        assert_eq!(telegram.channel_label, "telegram");

        let discord = PipelineConfig::channel("discord");
        assert_eq!(discord.channel_label, "discord");

        let email = PipelineConfig::channel("email");
        assert_eq!(email.channel_label, "email");
    }

    #[test]
    fn cron_preset_has_injection_defense() {
        let cfg = PipelineConfig::cron();
        // SECURITY FIX: injection defense was missing from scheduled_tasks.rs
        assert!(cfg.injection_defense);
        // No dedup for cron (scheduler guarantees uniqueness)
        assert!(!cfg.dedup_tracking);
        assert!(cfg.decomposition_gate);
        assert!(!cfg.delegated_execution); // cron doesn't pre-execute delegation
        assert!(cfg.shortcuts_enabled);
        assert!(!cfg.specialist_controls);
        assert!(cfg.cache_enabled);
        assert!(cfg.post_turn_ingest);
        assert!(!cfg.nickname_refinement);
        assert!(!cfg.inject_diagnostics);
        assert_eq!(cfg.inference_mode, InferenceMode::Standard);
        assert_eq!(cfg.guard_set, GuardSetPreset::Full);
        assert_eq!(cfg.cache_guard_set, GuardSetPreset::Cached);
        assert_eq!(cfg.authority_mode, AuthorityMode::SelfGenerated);
        assert_eq!(cfg.channel_label, "cron");
        assert_eq!(cfg.session_resolution, SessionResolutionMode::Dedicated);
    }

    // ── Guard set resolution ──────────────────────────────────────────

    #[test]
    fn guard_set_presets_resolve_to_non_empty_chains() {
        let full = GuardSetPreset::Full.resolve();
        assert!(!full.is_empty());

        let cached = GuardSetPreset::Cached.resolve();
        assert!(!cached.is_empty());

        let streaming = GuardSetPreset::Streaming.resolve();
        assert!(!streaming.is_empty());
    }

    #[test]
    fn guard_set_none_resolves_to_empty_chain() {
        let none = GuardSetPreset::None.resolve();
        assert!(none.is_empty());
    }

    // ── Predicate methods ─────────────────────────────────────────────

    #[test]
    fn api_predicates() {
        let cfg = PipelineConfig::api();
        assert!(cfg.is_standard_inference());
        assert!(!cfg.is_streaming_inference());
        assert!(cfg.enforces_authority());
        assert!(cfg.can_execute_tools());
        assert!(cfg.resolves_session_from_body());
        assert!(!cfg.is_channel());
        assert!(!cfg.is_cron());
    }

    #[test]
    fn streaming_predicates() {
        let cfg = PipelineConfig::streaming();
        assert!(!cfg.is_standard_inference());
        assert!(cfg.is_streaming_inference());
        assert!(!cfg.enforces_authority());
        assert!(!cfg.can_execute_tools());
        assert!(cfg.resolves_session_from_body());
        assert!(!cfg.is_channel());
        assert!(!cfg.is_cron());
    }

    #[test]
    fn channel_predicates() {
        let cfg = PipelineConfig::channel("telegram");
        assert!(cfg.is_standard_inference());
        assert!(!cfg.is_streaming_inference());
        assert!(cfg.enforces_authority());
        assert!(cfg.can_execute_tools());
        assert!(!cfg.resolves_session_from_body());
        assert!(cfg.is_channel());
        assert!(!cfg.is_cron());
    }

    #[test]
    fn cron_predicates() {
        let cfg = PipelineConfig::cron();
        assert!(cfg.is_standard_inference());
        assert!(!cfg.is_streaming_inference());
        // Cron uses SelfGenerated — authority not enforced (trusted internal caller)
        assert!(!cfg.enforces_authority());
        assert!(cfg.can_execute_tools());
        assert!(!cfg.resolves_session_from_body());
        assert!(!cfg.is_channel());
        assert!(cfg.is_cron());
    }

    // ── Security invariants ───────────────────────────────────────────

    #[test]
    fn all_presets_have_injection_defense() {
        // Every entry point MUST have injection defense. This is a
        // security-critical invariant.
        assert!(PipelineConfig::api().injection_defense);
        assert!(PipelineConfig::streaming().injection_defense);
        assert!(PipelineConfig::channel("test").injection_defense);
        assert!(PipelineConfig::cron().injection_defense);
    }

    #[test]
    fn all_presets_have_post_turn_ingest() {
        // Memory ingestion should never be skipped — it's essential for
        // episodic memory continuity.
        assert!(PipelineConfig::api().post_turn_ingest);
        assert!(PipelineConfig::streaming().post_turn_ingest);
        assert!(PipelineConfig::channel("test").post_turn_ingest);
        assert!(PipelineConfig::cron().post_turn_ingest);
    }

    #[test]
    fn standard_inference_paths_have_full_guards() {
        // All standard inference paths must use the Full guard set.
        let api = PipelineConfig::api();
        let channel = PipelineConfig::channel("telegram");
        let cron = PipelineConfig::cron();

        for cfg in [&api, &channel, &cron] {
            assert_eq!(cfg.inference_mode, InferenceMode::Standard);
            assert_eq!(cfg.guard_set, GuardSetPreset::Full);
            assert_eq!(cfg.cache_guard_set, GuardSetPreset::Cached);
        }
    }

    #[test]
    fn only_api_has_nickname_refinement() {
        assert!(PipelineConfig::api().nickname_refinement);
        assert!(!PipelineConfig::streaming().nickname_refinement);
        assert!(!PipelineConfig::channel("test").nickname_refinement);
        assert!(!PipelineConfig::cron().nickname_refinement);
    }

    #[test]
    fn only_channel_has_specialist_controls() {
        assert!(!PipelineConfig::api().specialist_controls);
        assert!(!PipelineConfig::streaming().specialist_controls);
        assert!(PipelineConfig::channel("test").specialist_controls);
        assert!(!PipelineConfig::cron().specialist_controls);
    }

    #[test]
    fn streaming_never_executes_tools() {
        let cfg = PipelineConfig::streaming();
        assert!(!cfg.can_execute_tools());
        assert!(!cfg.shortcuts_enabled);
        assert!(!cfg.decomposition_gate);
        assert!(!cfg.delegated_execution);
    }

    // ── Session resolution mode ───────────────────────────────────────

    #[test]
    fn session_resolution_modes_are_correct() {
        assert_eq!(
            PipelineConfig::api().session_resolution,
            SessionResolutionMode::FromBody
        );
        assert_eq!(
            PipelineConfig::streaming().session_resolution,
            SessionResolutionMode::FromBody
        );
        assert_eq!(
            PipelineConfig::channel("discord").session_resolution,
            SessionResolutionMode::FromChannel {
                platform: "discord".into()
            }
        );
        assert_eq!(
            PipelineConfig::cron().session_resolution,
            SessionResolutionMode::Dedicated
        );
    }

    #[test]
    fn provided_session_resolution_stores_id() {
        let mode = SessionResolutionMode::Provided {
            session_id: "test-session-123".into(),
        };
        match mode {
            SessionResolutionMode::Provided { session_id } => {
                assert_eq!(session_id, "test-session-123");
            }
            _ => panic!("expected Provided variant"),
        }
    }
}