khive-runtime 0.8.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
//! Runtime error types.

use std::fmt;
use std::time::Duration;

use thiserror::Error;
use uuid::Uuid;

/// Convenience alias for `Result<T, RuntimeError>`.
pub type RuntimeResult<T> = Result<T, RuntimeError>;

/// Stable ADR-135 F6 stage and wire code for a finite-wait pooled writer
/// checkout that expires before SQLite executes.
pub const WRITER_POOL_CHECKOUT_TIMEOUT_STAGE: &str = "writer_pool_checkout_timeout";

/// Stable wire code/stage for a bounded write-queue enqueue that never
/// accepted the request within its configured deadline (#1382, #1643).
pub const WRITER_QUEUE_SATURATED_STAGE: &str = "writer_queue_saturated";

/// Stable wire code/stage for SQLite write-lock contention after the writer
/// queue accepted a request but before its operation closure ran.
pub const WRITER_TASK_BEGIN_BUSY_STAGE: &str = "writer_task_begin_busy";

/// Stable wire code/stage for a bounded storage-admission wait (a
/// reader/writer handle slot or a pooled reader checkout) that elapsed
/// before anything was acquired. The operation never started, so the
/// failure is safe to retry.
pub const STORAGE_ADMISSION_TIMEOUT_STAGE: &str = "storage_admission_timeout";

/// Stable wire code/stage for a cached read-only transaction proactively
/// rolled back after pinning a WAL snapshot past the configured
/// `read_tx_max_age` bound (#1846). Always safe to retry: the eviction only
/// ever fires on a read-only handle, so no side effect can exist for a
/// retry to duplicate. Shared by both
/// [`khive_storage::StorageError::ReadTransactionAgeEvicted`] (clean
/// rollback) and
/// [`khive_storage::StorageError::ReadTransactionAgeEvictionCleanupFailed`]
/// (rollback denied or failed outright) — the rendered `message` field
/// distinguishes the two; retry behavior is identical for both.
pub const READ_TX_AGE_EVICTED_STAGE: &str = "read_tx_age_evicted";

/// Stable ADR-131:251 `scope` discriminator carried on a
/// [`WRITER_QUEUE_SATURATED_STAGE`] failure — distinguishes write-queue
/// admission saturation from other `unavailable` failure kinds that share
/// the same `retryable: true` shape but are not bounded by the admission
/// deadline (e.g. [`WRITER_POOL_CHECKOUT_TIMEOUT_STAGE`], which has no
/// ADR-131-defined scope and carries `None`).
pub const WRITER_ADMISSION_SCOPE: &str = "writer_admission";

/// Structured context for a pre-execution admission failure: a finite-wait
/// pooled writer checkout timeout, a bounded write-queue enqueue timeout, or
/// a bounded storage-admission wait (reader/writer handle slot or pooled
/// reader checkout). All happen before SQLite executes the request, so all
/// are safe to classify as retryable — the request was never accepted, let
/// alone started.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdmissionFailureContext {
    /// Stable wire stage/code: one of [`WRITER_POOL_CHECKOUT_TIMEOUT_STAGE`],
    /// [`WRITER_QUEUE_SATURATED_STAGE`], or
    /// [`STORAGE_ADMISSION_TIMEOUT_STAGE`].
    pub stage: &'static str,
    /// The configured deadline that elapsed.
    pub timeout: Duration,
    /// Storage capability the request was scoped to, when known. The write
    /// queue is process-local and unscoped by capability, so this is `None`
    /// for [`WRITER_QUEUE_SATURATED_STAGE`].
    pub capability: Option<khive_storage::StorageCapability>,
    /// Storage operation name, when known.
    pub operation: Option<String>,
    /// ADR-131:251 `scope` discriminator. `Some(`[`WRITER_ADMISSION_SCOPE`]`)`
    /// for [`WRITER_QUEUE_SATURATED_STAGE`]; `None` for
    /// [`WRITER_POOL_CHECKOUT_TIMEOUT_STAGE`], which ADR-131 does not define a
    /// scope for.
    pub scope: Option<&'static str>,
    /// ADR-131:251 `retry_after_ms` hint — set equal to the admission
    /// deadline actually applied to the rejected operation, so a retrying
    /// caller waits at least one full admission window before retrying.
    /// `Some(timeout_ms)` for [`WRITER_QUEUE_SATURATED_STAGE`]; `None`
    /// otherwise.
    pub retry_after_ms: Option<u64>,
}

/// Structured context for a failure that is proven safe to retry.
///
/// This includes the two pre-admission failures above and writer-task BEGIN
/// contention after queue acceptance but before operation execution. Callers
/// must inspect `scope`: only `writer_admission` means the queue never
/// accepted the operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RetryableFailureContext {
    /// Stable wire stage/code.
    pub stage: &'static str,
    /// Configured wait that ended before execution.
    pub timeout: Duration,
    /// Storage capability when the failure is capability-scoped.
    pub capability: Option<khive_storage::StorageCapability>,
    /// Storage operation when known.
    pub operation: Option<String>,
    /// Admission scope only when the queue never accepted the operation.
    pub scope: Option<&'static str>,
    /// Server backoff hint when a governing contract defines one.
    pub retry_after_ms: Option<u64>,
}

impl From<AdmissionFailureContext> for RetryableFailureContext {
    fn from(context: AdmissionFailureContext) -> Self {
        Self {
            stage: context.stage,
            timeout: context.timeout,
            capability: context.capability,
            operation: context.operation,
            scope: context.scope,
            retry_after_ms: context.retry_after_ms,
        }
    }
}

/// Typed disposition for a channel message whose `comm.ingest` write failed.
///
/// Every bucket carries the stable [`RuntimeError`] variant name that selected
/// it. Consumers can therefore make retry/quarantine decisions and report a
/// reason without inspecting the error's rendered text.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelIngestFailureClass {
    /// A pre-execution admission failure that is safe to retry indefinitely.
    Retryable { reason: &'static str },
    /// A deterministic policy refusal that should quarantine immediately.
    Permanent { reason: &'static str },
    /// An error with no explicit retry or permanent policy classification.
    Unknown { reason: &'static str },
}

impl ChannelIngestFailureClass {
    /// Stable lowercase bucket name persisted on quarantine notifications.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Retryable { .. } => "retryable",
            Self::Permanent { .. } => "permanent",
            Self::Unknown { .. } => "unknown",
        }
    }

    /// Stable typed error-variant name; never derived from `Display` output.
    pub const fn reason(self) -> &'static str {
        match self {
            Self::Retryable { reason } | Self::Permanent { reason } | Self::Unknown { reason } => {
                reason
            }
        }
    }
}

/// Structured context recovered from either a direct SQLite runtime error or
/// a typed SQLite source preserved inside a storage-driver wrapper.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WriterPoolCheckoutTimeoutContext {
    /// Finite checkout deadline that elapsed before the pooled mutex was
    /// acquired.
    pub timeout: Duration,
    /// Storage capability that wrapped the SQLite source, if one did.
    pub capability: Option<khive_storage::StorageCapability>,
    /// Storage operation that wrapped the SQLite source, if one did.
    pub operation: Option<String>,
}

/// A guarded edge write (`link`/`link_many`) was refused because one or both
/// endpoints no longer existed at write time. Names the exact endpoint(s)
/// missing instead of a generic "source or target" message, and, for a batch
/// write, which entry in the batch failed first.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GuardedWriteFailure {
    /// Index of the failing entry within a `link_many` batch. `None` for the
    /// singleton `link` path, which has no batch to index into.
    pub entry_index: Option<usize>,
    /// The source endpoint id, present only when it is the (or one of the)
    /// missing endpoint(s).
    pub missing_source: Option<Uuid>,
    /// The target endpoint id, present only when it is the (or one of the)
    /// missing endpoint(s).
    pub missing_target: Option<Uuid>,
}

impl fmt::Display for GuardedWriteFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut missing = Vec::new();
        if let Some(source) = self.missing_source {
            missing.push(format!("source {source}"));
        }
        if let Some(target) = self.missing_target {
            missing.push(format!("target {target}"));
        }
        let missing = if missing.is_empty() {
            "endpoint(s)".to_string()
        } else {
            missing.join(" and ")
        };
        match self.entry_index {
            Some(index) => write!(
                f,
                "batch entry {index}: {missing} no longer exist at write time"
            ),
            None => write!(f, "{missing} no longer exist at write time"),
        }
    }
}

impl std::error::Error for GuardedWriteFailure {}

/// A single missing pack dependency.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingPackDependency {
    pub from: String,
    pub requires: String,
}

impl fmt::Display for MissingPackDependency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "pack '{}' requires '{}', but '{}' is not in the loaded pack set",
            self.from, self.requires, self.requires
        )
    }
}

impl std::error::Error for MissingPackDependency {}

/// Multiple missing pack dependencies collected into one error.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MissingPackDependencies {
    pub missing: Vec<MissingPackDependency>,
}

impl fmt::Display for MissingPackDependencies {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let parts: Vec<String> = self.missing.iter().map(ToString::to_string).collect();
        write!(f, "{}", parts.join("; "))
    }
}

impl std::error::Error for MissingPackDependencies {}

/// Circular pack dependency detected during topological sort.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CircularPackDependency {
    pub cycle: Vec<String>,
}

impl fmt::Display for CircularPackDependency {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "circular dependency detected among packs: {}",
            self.cycle.join(" -> ")
        )
    }
}

impl std::error::Error for CircularPackDependency {}

/// All errors produced by the khive-runtime layer.
///
/// Variants cover storage, query, validation, namespace isolation, and permission failures.
/// Callers should match on `InvalidInput` for bad arguments, `NotFound` for missing records,
/// and `NamespaceMismatch` (reported as not-found) for cross-namespace access attempts.
#[derive(Debug, Error)]
pub enum RuntimeError {
    #[error("storage: {0}")]
    Storage(#[from] khive_storage::StorageError),

    #[error("sqlite: {0}")]
    Sqlite(#[from] khive_db::SqliteError),

    #[error("query: {0}")]
    Query(#[from] khive_query::QueryError),

    #[error("not found: {0}")]
    NotFound(String),

    #[error("invalid input: {0}")]
    InvalidInput(String),

    #[error("invalid input: {0}")]
    UnknownVerb(String),

    #[error("unconfigured: {0} is not set")]
    Unconfigured(String),

    #[error("unknown embedding model: {0}")]
    UnknownModel(String),

    #[error("embedding: {0}")]
    Embedding(#[from] lattice_embed::EmbedError),

    #[error("ambiguous: {0}")]
    Ambiguous(String),

    #[error("fusion: {0}")]
    Fusion(#[from] khive_fusion::FuseError),

    /// `FusionStrategy::Custom { name, .. }` named a strategy no pack has
    /// registered via `KhiveRuntime::register_fusion_strategy` (ADR-012).
    /// Fails closed — never falls back to RRF or any other default.
    #[error("unknown fusion strategy: {0}")]
    UnknownFusionStrategy(String),

    #[error("internal: {0}")]
    Internal(String),

    /// An `EventStore` was configured via `with_event_store` but does not
    /// implement ADR-133's `preflight_event`/`append_events_idempotent`
    /// pair (`EventStore::supports_idempotent_audit_batch` reports
    /// `false`). Raised at `build()` time rather than left to fail every
    /// audited dispatch silently.
    #[error("audit batch incompatible event store: {0}")]
    IncompatibleEventStore(String),

    #[error("guarded edge write refused: {0}")]
    GuardedWriteFailed(GuardedWriteFailure),

    #[error("missing pack dependency: {0}")]
    MissingPackDependency(MissingPackDependency),

    #[error("missing pack dependencies: {0}")]
    MissingPackDependencies(MissingPackDependencies),

    #[error("{0}")]
    CircularPackDependency(CircularPackDependency),

    #[error("pack '{name}' registered twice (indices {first_idx} and {second_idx})")]
    PackRedeclared {
        name: String,
        first_idx: usize,
        second_idx: usize,
    },

    /// Two packs declared the same `Visibility::Verb` handler name.
    /// `Visibility::Subhandler` entries are pack-prefixed and do not
    /// participate in cross-pack collision checks.
    #[error(
        "verb collision: verb {verb:?} declared by both pack {first_pack:?} and pack \
         {second_pack:?}; rename one handler or use Visibility::Subhandler for internal verbs"
    )]
    VerbCollision {
        verb: String,
        first_pack: String,
        second_pack: String,
    },

    /// A handler advertised a parameter that request parsing owns at the envelope level.
    #[error(
        "pack {pack:?} handler {verb:?} declares request-envelope parameter {param:?}; rename the verb argument"
    )]
    ReservedEnvelopeParam {
        pack: String,
        verb: String,
        param: String,
    },

    /// Gate denied this verb invocation.
    ///
    /// Returned by `VerbRegistry::dispatch` when the configured `Gate` returns
    /// `GateDecision::Deny`. The pack is never invoked. The `reason` field
    /// carries the deny message produced by the gate implementation.
    #[error("permission denied for verb {verb:?}: {reason}")]
    PermissionDenied { verb: String, reason: String },

    /// The configured gate could not produce an authorization decision.
    ///
    /// This is distinct from [`Self::PermissionDenied`]: the pack or
    /// intercepted operation is never invoked, but the gate did not answer
    /// with an explicit denial.
    #[error("gate unavailable for verb {verb:?}: {reason}")]
    GateUnavailable { verb: String, reason: String },

    /// A structured [`khive_types::KhiveError`] converted into the runtime
    /// layer. The full structured error is preserved so callers can inspect
    /// `kind`, `code`, `details`, and `retry_hint` without information loss.
    #[error("{0}")]
    Khive(khive_types::KhiveError),

    /// Record exists but belongs to a different namespace than the provided token.
    ///
    /// Externally reported as "not found in this namespace" to avoid leaking
    /// cross-namespace existence information (timing-oracle mitigation).
    #[error("not found in this namespace")]
    NamespaceMismatch { id: uuid::Uuid },

    /// A short-prefix lookup matched more than one record.
    ///
    /// `prefix` is the 8+ hex-char prefix supplied by the caller.
    /// `matches` holds the full UUIDs of all matching records (at most 2 are
    /// reported to bound the scan — callers must supply the full UUID to disambiguate).
    #[error("ambiguous prefix {prefix:?}: matches {}", format_uuid_list(matches))]
    AmbiguousPrefix {
        prefix: String,
        matches: Vec<uuid::Uuid>,
    },

    /// Cross-backend `merge_entity` is unsupported in v1.
    ///
    /// Both entities must reside on the same backend. To merge entities on different
    /// backends, manually export `from_id`, delete it, and re-import on `into_id`'s backend.
    #[error(
        "cross-backend merge is not supported: \
         into_id {into_id} is on backend '{into_backend}', \
         from_id {from_id} is on backend '{from_backend}'. \
         Both entities must be on the same backend to merge."
    )]
    CrossBackendMergeUnsupported {
        into_id: uuid::Uuid,
        from_id: uuid::Uuid,
        into_backend: String,
        from_backend: String,
    },

    // ── Remote Resolution and Content-Hash Verification ──────────────────────
    /// A `kg://` ref names a remote not declared in `schema.yaml`.
    #[error("unknown remote: {name:?}")]
    UnknownRemote { name: String },

    /// A remote cache entry is absent and `--fetch` was not requested.
    #[error("remote cache missing for remote={remote:?} namespace={namespace:?}")]
    RemoteCacheMissing { remote: String, namespace: String },

    /// A short ID matches multiple entities in the same namespace or remote cache.
    #[error("ambiguous id {id:?}: matched {count} records")]
    AmbiguousId { id: String, count: usize },

    /// A write operation targeted a remote namespace, which is read-only.
    #[error("cross-namespace write denied: cannot write to remote namespace {namespace:?}")]
    CrossNamespaceWrite { namespace: String },

    /// A remote fetch failed (network error, authentication failure, etc.).
    #[error("remote fetch error for remote={remote:?}: {message}")]
    RemoteFetchError { remote: String, message: String },

    /// A caller-supplied write budget was exceeded during a Compound apply.
    ///
    /// `max_new_entries` is the limit passed by the caller. `attempted_new_entries`
    /// is `consumed + 1`, i.e. the create that would have exceeded the cap.
    /// `None` budget never produces this error (unlimited path).
    #[error(
        "write budget exceeded: max_new_entries={max_new_entries}, \
         attempted_new_entries={attempted_new_entries}"
    )]
    WriteBudgetExceeded {
        max_new_entries: u64,
        attempted_new_entries: u64,
    },

    /// Write blocked: content matches a secret pattern.
    ///
    /// The `SecretMatch` carries the detector name and a masked excerpt
    /// (`first6...Nchars`). The full candidate is never stored in the error.
    /// Store a pointer (env-var name, keychain item) rather than the raw value.
    #[error("write blocked: {0}")]
    SecretDetected(crate::secret_gate::SecretMatch),

    /// A bounded per-operation deadline elapsed before the operation
    /// completed (#889). The operation may still be running in the
    /// background (this is a client-observable timeout, not a cancellation
    /// signal to the underlying work) — callers should treat this as "no
    /// answer within budget", not "the operation failed or was rolled back".
    ///
    /// Distinct from `#836`'s narrower `ann_ready_timeout_ms`, which bounds
    /// only a single cold-miss ANN-build wait inside the recall vector leg
    /// and degrades to an in-band FTS-only result. This variant bounds the
    /// *entire* operation end-to-end and is surfaced as a typed error so a
    /// caller under sustained contention gets a fast, clear answer instead
    /// of hanging until an upstream client-side ceiling (observed at 300s in
    /// production, #889) fires instead.
    #[error("{operation} exceeded its {budget_ms}ms deadline (elapsed {elapsed_ms}ms)")]
    DeadlineExceeded {
        operation: String,
        budget_ms: u64,
        elapsed_ms: u64,
    },
}

impl RuntimeError {
    /// Classify a failed inbound channel write without inspecting rendered
    /// error text.
    ///
    /// Existing typed safe-retry failures remain retryable. Secret detection is
    /// the first deterministic policy refusal and is permanent. `None` from
    /// [`Self::retryable_failure_context`] is deliberately not interpreted as
    /// permanent: every other variant starts in the bounded `Unknown` bucket.
    pub fn channel_ingest_failure_class(&self) -> ChannelIngestFailureClass {
        let reason = self.variant_name();
        if self.retryable_failure_context().is_some() {
            ChannelIngestFailureClass::Retryable { reason }
        } else if matches!(self, Self::SecretDetected(_)) {
            ChannelIngestFailureClass::Permanent { reason }
        } else {
            ChannelIngestFailureClass::Unknown { reason }
        }
    }

    /// Stable top-level variant name used by typed policy classifiers.
    const fn variant_name(&self) -> &'static str {
        match self {
            Self::Storage(_) => "Storage",
            Self::Sqlite(_) => "Sqlite",
            Self::Query(_) => "Query",
            Self::NotFound(_) => "NotFound",
            Self::InvalidInput(_) => "InvalidInput",
            Self::UnknownVerb(_) => "UnknownVerb",
            Self::Unconfigured(_) => "Unconfigured",
            Self::UnknownModel(_) => "UnknownModel",
            Self::Embedding(_) => "Embedding",
            Self::Ambiguous(_) => "Ambiguous",
            Self::Fusion(_) => "Fusion",
            Self::UnknownFusionStrategy(_) => "UnknownFusionStrategy",
            Self::Internal(_) => "Internal",
            Self::GuardedWriteFailed(_) => "GuardedWriteFailed",
            Self::MissingPackDependency(_) => "MissingPackDependency",
            Self::MissingPackDependencies(_) => "MissingPackDependencies",
            Self::CircularPackDependency(_) => "CircularPackDependency",
            Self::PackRedeclared { .. } => "PackRedeclared",
            Self::VerbCollision { .. } => "VerbCollision",
            Self::ReservedEnvelopeParam { .. } => "ReservedEnvelopeParam",
            Self::PermissionDenied { .. } => "PermissionDenied",
            Self::GateUnavailable { .. } => "GateUnavailable",
            Self::Khive(_) => "Khive",
            Self::NamespaceMismatch { .. } => "NamespaceMismatch",
            Self::AmbiguousPrefix { .. } => "AmbiguousPrefix",
            Self::CrossBackendMergeUnsupported { .. } => "CrossBackendMergeUnsupported",
            Self::UnknownRemote { .. } => "UnknownRemote",
            Self::RemoteCacheMissing { .. } => "RemoteCacheMissing",
            Self::AmbiguousId { .. } => "AmbiguousId",
            Self::CrossNamespaceWrite { .. } => "CrossNamespaceWrite",
            Self::RemoteFetchError { .. } => "RemoteFetchError",
            Self::WriteBudgetExceeded { .. } => "WriteBudgetExceeded",
            Self::SecretDetected(_) => "SecretDetected",
            Self::DeadlineExceeded { .. } => "DeadlineExceeded",
            Self::IncompatibleEventStore(_) => "IncompatibleEventStore",
        }
    }

    /// Recover a finite-wait pool-checkout timeout without inspecting rendered
    /// error text.
    ///
    /// Store implementations retain [`khive_db::SqliteError`] as the typed
    /// source of `StorageError::Driver`; this method carries that structure
    /// through the runtime wrapper for the MCP wire serializer. A direct
    /// `RuntimeError::Sqlite` follows the same classification path.
    pub fn writer_pool_checkout_timeout_context(&self) -> Option<WriterPoolCheckoutTimeoutContext> {
        let (sqlite_error, capability, operation) = match self {
            Self::Sqlite(error) => (error, None, None),
            Self::Storage(khive_storage::StorageError::Driver {
                capability,
                operation,
                source,
            }) => (
                source.downcast_ref::<khive_db::SqliteError>()?,
                Some(*capability),
                Some(operation.to_string()),
            ),
            _ => return None,
        };

        let khive_db::SqliteError::WriterPoolCheckoutTimeout { timeout } = sqlite_error else {
            return None;
        };
        Some(WriterPoolCheckoutTimeoutContext {
            timeout: *timeout,
            capability,
            operation,
        })
    }

    /// Recover either pre-execution write-admission failure this process can
    /// produce, by typed variant rather than rendered message text (#1643).
    /// Both are safe to classify as retryable: the request was never
    /// accepted, so no partial side effect can exist to roll back.
    pub fn admission_failure_context(&self) -> Option<AdmissionFailureContext> {
        if let Some(context) = self.writer_pool_checkout_timeout_context() {
            return Some(AdmissionFailureContext {
                stage: WRITER_POOL_CHECKOUT_TIMEOUT_STAGE,
                timeout: context.timeout,
                capability: context.capability,
                operation: context.operation,
                scope: None,
                retry_after_ms: None,
            });
        }
        if let Self::Storage(khive_storage::StorageError::WriteQueueFull { timeout_ms }) = self {
            return Some(AdmissionFailureContext {
                stage: WRITER_QUEUE_SATURATED_STAGE,
                timeout: Duration::from_millis(*timeout_ms),
                capability: None,
                operation: None,
                scope: Some(WRITER_ADMISSION_SCOPE),
                retry_after_ms: Some(*timeout_ms),
            });
        }
        if let Self::Storage(khive_storage::StorageError::AdmissionTimeout {
            operation,
            timeout_ms,
        }) = self
        {
            return Some(AdmissionFailureContext {
                stage: STORAGE_ADMISSION_TIMEOUT_STAGE,
                timeout: Duration::from_millis(*timeout_ms),
                capability: None,
                operation: Some(operation.to_string()),
                scope: None,
                retry_after_ms: None,
            });
        }
        None
    }

    /// Recover every typed failure for which this process can prove that
    /// retrying the one failed operation cannot duplicate a side effect.
    pub fn retryable_failure_context(&self) -> Option<RetryableFailureContext> {
        if let Some(context) = self.admission_failure_context() {
            return Some(context.into());
        }
        if let Self::Storage(khive_storage::StorageError::WriterTaskBusy { timeout_ms }) = self {
            return Some(RetryableFailureContext {
                stage: WRITER_TASK_BEGIN_BUSY_STAGE,
                timeout: Duration::from_millis(*timeout_ms),
                capability: None,
                operation: Some("writer_task_begin".to_string()),
                scope: None,
                retry_after_ms: None,
            });
        }
        if let Self::Storage(khive_storage::StorageError::ReadTransactionAgeEvicted {
            operation,
            max_age_secs,
        }) = self
        {
            return Some(RetryableFailureContext {
                stage: READ_TX_AGE_EVICTED_STAGE,
                timeout: Duration::from_secs(*max_age_secs),
                capability: Some(khive_storage::StorageCapability::Sql),
                operation: Some(operation.to_string()),
                scope: None,
                retry_after_ms: None,
            });
        }
        if let Self::Storage(
            khive_storage::StorageError::ReadTransactionAgeEvictionCleanupFailed {
                operation,
                max_age_secs,
                ..
            },
        ) = self
        {
            return Some(RetryableFailureContext {
                stage: READ_TX_AGE_EVICTED_STAGE,
                timeout: Duration::from_secs(*max_age_secs),
                capability: Some(khive_storage::StorageCapability::Sql),
                operation: Some(operation.to_string()),
                scope: None,
                retry_after_ms: None,
            });
        }
        None
    }
}

/// Resolve an FTS text-leg search result, failing loud on parser syntax
/// errors instead of silently degrading to vector-only fusion.
///
/// A genuine backend outage (pool exhaustion, connection failure, etc.) is
/// NOT a bad query and is returned as-is via the fallthrough `Err(e)` arm;
/// `is_fts5_syntax_error` is the narrow gate that tells the two apart.
pub fn fts_text_leg_or_err<T>(
    result: Result<Vec<T>, RuntimeError>,
    context: &'static str,
    query: &str,
) -> RuntimeResult<Vec<T>> {
    match result {
        Ok(hits) => Ok(hits),
        Err(RuntimeError::Storage(se)) if se.is_fts5_syntax_error() => {
            tracing::warn!(
                error = %se,
                query = %query,
                context,
                "FTS text leg failed on a parser syntax error; failing loud (#569)"
            );
            Err(RuntimeError::InvalidInput(format!(
                "{context}: FTS query could not be parsed: {se}"
            )))
        }
        Err(e) => Err(e),
    }
}

fn format_uuid_list(uuids: &[uuid::Uuid]) -> String {
    uuids
        .iter()
        .map(uuid::Uuid::to_string)
        .collect::<Vec<_>>()
        .join(", ")
}

/// Maps the dependency-light `khive-types` entity-type resolution error onto
/// `RuntimeError::InvalidInput` at the pack boundary: `khive-types` cannot
/// depend on `khive-runtime`, so it cannot produce `RuntimeError` directly.
impl From<khive_types::EntityTypeError> for RuntimeError {
    fn from(e: khive_types::EntityTypeError) -> Self {
        Self::InvalidInput(e.to_string())
    }
}

impl From<khive_types::KhiveError> for RuntimeError {
    fn from(e: khive_types::KhiveError) -> Self {
        Self::Khive(e)
    }
}

#[cfg(test)]
mod channel_ingest_failure_class_tests {
    use super::{ChannelIngestFailureClass, RuntimeError};
    use crate::secret_gate::SecretMatch;
    use std::time::Duration;

    #[test]
    fn secret_detected_is_permanent_by_typed_variant_not_display_text() {
        let first = RuntimeError::SecretDetected(SecretMatch {
            detector: "fixture",
            masked: "first-rendering".to_string(),
        });
        let second = RuntimeError::SecretDetected(SecretMatch {
            detector: "fixture",
            masked: "completely-different-rendering".to_string(),
        });

        assert_ne!(first.to_string(), second.to_string());
        assert_eq!(
            first.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Permanent {
                reason: "SecretDetected"
            }
        );
        assert_eq!(
            second.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Permanent {
                reason: "SecretDetected"
            },
            "rendered error details must not participate in ingest classification"
        );
    }

    #[test]
    fn admission_failures_are_retryable_and_unclassified_errors_are_unknown() {
        let retryable =
            RuntimeError::Storage(khive_storage::StorageError::WriteQueueFull { timeout_ms: 25 });
        assert_eq!(
            retryable.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Retryable { reason: "Storage" }
        );

        let begin_busy =
            RuntimeError::Storage(khive_storage::StorageError::WriterTaskBusy { timeout_ms: 175 });
        let context = begin_busy
            .retryable_failure_context()
            .expect("contended BEGIN must remain typed and retryable");
        assert_eq!(context.stage, "writer_task_begin_busy");
        assert_eq!(context.timeout, Duration::from_millis(175));
        assert_eq!(context.operation.as_deref(), Some("writer_task_begin"));
        assert_eq!(context.scope, None);
        assert_eq!(context.retry_after_ms, None);
        assert_eq!(
            begin_busy.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Retryable { reason: "Storage" }
        );

        let unknown = RuntimeError::InvalidInput(
            "write blocked: SecretDetected text must not affect classification".to_string(),
        );
        assert_eq!(
            unknown.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Unknown {
                reason: "InvalidInput"
            },
            "a rendered message resembling SecretDetected must remain Unknown unless its typed variant is SecretDetected"
        );
    }

    #[test]
    fn storage_admission_timeout_is_a_retryable_admission_failure() {
        let admission = RuntimeError::Storage(khive_storage::StorageError::AdmissionTimeout {
            operation: "sql_bridge.writer_handle".into(),
            timeout_ms: 30_000,
        });
        let context = admission
            .admission_failure_context()
            .expect("a storage admission timeout happens before the operation starts");
        assert_eq!(context.stage, super::STORAGE_ADMISSION_TIMEOUT_STAGE);
        assert_eq!(context.timeout, Duration::from_millis(30_000));
        assert_eq!(
            context.operation.as_deref(),
            Some("sql_bridge.writer_handle")
        );
        assert_eq!(context.scope, None);
        assert_eq!(context.retry_after_ms, None);
        assert_eq!(
            admission.channel_ingest_failure_class(),
            ChannelIngestFailureClass::Retryable { reason: "Storage" }
        );

        // The general mid-flight timeout stays unclassified: it proves
        // nothing about whether work was in flight when the deadline expired.
        let mid_flight = RuntimeError::Storage(khive_storage::StorageError::Timeout {
            operation: "sql_bridge.reader_open".into(),
        });
        assert!(mid_flight.admission_failure_context().is_none());
        assert!(mid_flight.retryable_failure_context().is_none());
    }

    /// Binds both age-eviction outcomes a cached-reader rollback can produce
    /// (clean rollback vs. failed/denied cleanup) to the same retryable wire
    /// stage: a regression that reverted either branch back to generic
    /// `StorageError::Transaction` would drop this match arm entirely and
    /// fail here, since `Transaction` does have a `retryable_failure_context`
    /// mapping distinct from `super::READ_TX_AGE_EVICTED_STAGE`.
    #[test]
    fn both_read_tx_age_eviction_outcomes_map_to_the_same_retryable_stage() {
        let clean = RuntimeError::Storage(khive_storage::StorageError::ReadTransactionAgeEvicted {
            operation: "query_all".into(),
            max_age_secs: 120,
        });
        let clean_context = clean
            .retryable_failure_context()
            .expect("a clean age eviction must be typed-retryable");
        assert_eq!(clean_context.stage, super::READ_TX_AGE_EVICTED_STAGE);
        assert_eq!(clean_context.timeout, Duration::from_secs(120));
        assert_eq!(clean_context.operation.as_deref(), Some("query_all"));
        assert_eq!(
            clean_context.capability,
            Some(khive_storage::StorageCapability::Sql)
        );

        let cleanup_failed = RuntimeError::Storage(
            khive_storage::StorageError::ReadTransactionAgeEvictionCleanupFailed {
                operation: "query_all".into(),
                max_age_secs: 120,
                message: "rollback failed: disk I/O error".into(),
            },
        );
        let cleanup_failed_context = cleanup_failed
            .retryable_failure_context()
            .expect("a failed cleanup rollback must remain typed-retryable");
        assert_eq!(
            cleanup_failed_context.stage,
            super::READ_TX_AGE_EVICTED_STAGE
        );
        assert_eq!(cleanup_failed_context.timeout, Duration::from_secs(120));
        assert_eq!(
            cleanup_failed_context.operation.as_deref(),
            Some("query_all")
        );
        assert_eq!(
            cleanup_failed_context.capability,
            Some(khive_storage::StorageCapability::Sql)
        );

        assert_ne!(
            clean.to_string(),
            cleanup_failed.to_string(),
            "the rendered message must distinguish a clean eviction from a failed cleanup even \
             though both map to the same wire stage"
        );
        assert!(cleanup_failed.to_string().contains("rollback failed"));
    }
}