hirn-core 0.1.0

Core types for the hirn cognitive memory database
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
use std::fmt;

use serde::{Deserialize, Serialize};
use ulid::Ulid;

use crate::config::ConflictResolutionPolicy;
use crate::error::HirnError;
use crate::id::MemoryId;
use crate::resource::ResourceId;
use crate::revision::LogicalMemoryId;
use crate::timestamp::Timestamp;
use crate::types::{AgentId, Namespace};

/// Unique identifier for an offline cognitive job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OfflineJobId(Ulid);

impl OfflineJobId {
    #[must_use]
    pub fn new() -> Self {
        Self(Ulid::new())
    }

    pub fn parse(value: &str) -> Result<Self, HirnError> {
        Ulid::from_string(value)
            .map(Self)
            .map_err(|error| HirnError::InvalidInput(format!("invalid offline job id: {error}")))
    }
}

impl Default for OfflineJobId {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Display for OfflineJobId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Scheduler priority for queued offline jobs.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord, Default,
)]
#[serde(rename_all = "snake_case")]
pub enum OfflineJobPriority {
    Low,
    #[default]
    Normal,
    High,
    Critical,
}

/// Offline cognitive operators backed by the scheduler runtime.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CognitiveJobKind {
    Dream,
    Reconcile,
    Plan,
    Reflect,
    Summarize,
    Evaluate,
    /// A-MEM backward evolution: enrich top-k related historical memories with
    /// a context note derived from the triggering new memory's perspective.
    /// The job target's `memory_ids[0]` is the newly written memory.
    Evolve,
    /// FadeMem offline decay sweep: apply adaptive importance decay to all
    /// memories that have not been accessed within `decay_sweep_window_secs`.
    ///
    /// FadeMem computes decay at query time for accessed memories, but never
    /// decrements importance for memories that are *not* accessed. This job
    /// closes that gap: it batch-scans memories with stale `last_accessed_ms`
    /// and applies `importance *= decay_factor` via `update_where`, then emits
    /// a log message. Scheduled by `decay_interval_secs` in `HirnConfig`.
    Decay,
}

/// Time-bounded target scope for an offline job.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct TemporalWindow {
    pub start: Timestamp,
    pub end: Timestamp,
}

impl TemporalWindow {
    #[must_use]
    pub const fn new(start: Timestamp, end: Timestamp) -> Self {
        Self { start, end }
    }

    pub fn validate(&self, field: &str) -> Result<(), HirnError> {
        if self.end < self.start {
            return Err(HirnError::InvalidConfig {
                field: field.to_string(),
                value: format!("{}..{}", self.start.timestamp_ms(), self.end.timestamp_ms()),
                reason: "end must be greater than or equal to start".to_string(),
            });
        }

        Ok(())
    }
}

/// Explicit job target selectors for offline cognition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct OfflineJobTarget {
    pub realm: Option<String>,
    pub namespace: Option<Namespace>,
    pub goal: Option<String>,
    pub topic: Option<String>,
    pub event_segment: Option<String>,
    pub temporal_window: Option<TemporalWindow>,
    pub memory_ids: Vec<MemoryId>,
    pub logical_memory_ids: Vec<LogicalMemoryId>,
}

impl OfflineJobTarget {
    #[must_use]
    pub fn realm(realm: impl Into<String>) -> Self {
        Self {
            realm: Some(realm.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn namespace(namespace: Namespace) -> Self {
        Self {
            namespace: Some(namespace),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn goal(goal: impl Into<String>) -> Self {
        Self {
            goal: Some(goal.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn topic(topic: impl Into<String>) -> Self {
        Self {
            topic: Some(topic.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn event_segment(segment: impl Into<String>) -> Self {
        Self {
            event_segment: Some(segment.into()),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn temporal_window(window: TemporalWindow) -> Self {
        Self {
            temporal_window: Some(window),
            ..Self::default()
        }
    }

    #[must_use]
    pub fn memory_subset(memory_ids: Vec<MemoryId>) -> Self {
        Self {
            memory_ids,
            ..Self::default()
        }
    }

    #[must_use]
    pub fn logical_subset(logical_memory_ids: Vec<LogicalMemoryId>) -> Self {
        Self {
            logical_memory_ids,
            ..Self::default()
        }
    }

    #[must_use]
    pub fn is_explicit(&self) -> bool {
        self.realm.is_some()
            || self.namespace.is_some()
            || self.goal.is_some()
            || self.topic.is_some()
            || self.event_segment.is_some()
            || self.temporal_window.is_some()
            || !self.memory_ids.is_empty()
            || !self.logical_memory_ids.is_empty()
    }

    pub fn validate(&self, field: &str) -> Result<(), HirnError> {
        if !self.is_explicit() {
            return Err(HirnError::InvalidConfig {
                field: field.to_string(),
                value: "<empty>".to_string(),
                reason: "at least one target selector must be set".to_string(),
            });
        }

        if let Some(realm) = self.realm.as_ref() {
            if realm.trim().is_empty() {
                return Err(HirnError::InvalidConfig {
                    field: format!("{field}.realm"),
                    value: realm.clone(),
                    reason: "realm must be non-empty when provided".to_string(),
                });
            }
        }
        if let Some(goal) = self.goal.as_ref() {
            if goal.trim().is_empty() {
                return Err(HirnError::InvalidConfig {
                    field: format!("{field}.goal"),
                    value: goal.clone(),
                    reason: "goal must be non-empty when provided".to_string(),
                });
            }
        }
        if let Some(topic) = self.topic.as_ref() {
            if topic.trim().is_empty() {
                return Err(HirnError::InvalidConfig {
                    field: format!("{field}.topic"),
                    value: topic.clone(),
                    reason: "topic must be non-empty when provided".to_string(),
                });
            }
        }
        if let Some(segment) = self.event_segment.as_ref() {
            if segment.trim().is_empty() {
                return Err(HirnError::InvalidConfig {
                    field: format!("{field}.event_segment"),
                    value: segment.clone(),
                    reason: "event_segment must be non-empty when provided".to_string(),
                });
            }
        }
        if let Some(window) = self.temporal_window.as_ref() {
            window.validate(&format!("{field}.temporal_window"))?;
        }

        Ok(())
    }
}

/// Deterministic reconcile outcomes proposed by offline cognition.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconcileProposalAction {
    RetainBoth,
    Supersede,
    Retract,
    Quarantine,
    EscalateForReview,
}

impl ReconcileProposalAction {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::RetainBoth => "retain_both",
            Self::Supersede => "supersede",
            Self::Retract => "retract",
            Self::Quarantine => "quarantine",
            Self::EscalateForReview => "escalate_for_review",
        }
    }
}

/// Arbitration status captured when a reconcile proposal was generated.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReconcileArbitrationStatus {
    Unresolved,
    Resolved,
    Quarantined,
    Superseded,
}

/// Frozen member reference captured by an offline reconcile proposal.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReconcileProposalMember {
    pub memory_id: MemoryId,
    pub logical_memory_id: LogicalMemoryId,
}

/// Serializable snapshot of the conflict-resolution policy used to build a proposal.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct ConflictResolutionPolicySnapshot {
    pub recency_weight: f32,
    pub source_reliability_weight: f32,
    pub supporting_evidence_weight: f32,
    pub human_override_weight: f32,
    pub prefer_human_override: bool,
}

impl ConflictResolutionPolicySnapshot {
    #[must_use]
    pub const fn from_policy(policy: ConflictResolutionPolicy) -> Self {
        Self {
            recency_weight: policy.recency_weight,
            source_reliability_weight: policy.source_reliability_weight,
            supporting_evidence_weight: policy.supporting_evidence_weight,
            human_override_weight: policy.human_override_weight,
            prefer_human_override: policy.prefer_human_override,
        }
    }
}

/// JSON payload embedded in quarantined semantic reconcile proposals.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReconcileProposal {
    pub action: ReconcileProposalAction,
    pub conflict_id: String,
    pub arbitration_status: ReconcileArbitrationStatus,
    pub preferred_memory_id: Option<MemoryId>,
    pub authoritative_memory_id: Option<MemoryId>,
    pub members: Vec<ReconcileProposalMember>,
    pub rationale: String,
    pub policy: ConflictResolutionPolicySnapshot,
}

impl ReconcileProposal {
    pub fn to_json(&self) -> Result<String, HirnError> {
        serde_json::to_string(self).map_err(|error| {
            HirnError::InvalidInput(format!("failed to serialize reconcile proposal: {error}"))
        })
    }

    pub fn from_json(value: &str) -> Result<Self, HirnError> {
        serde_json::from_str(value).map_err(|error| {
            HirnError::InvalidInput(format!("failed to parse reconcile proposal: {error}"))
        })
    }
}

/// Memory-class support included in a goal-conditioned planning agenda.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PlanningSupportKind {
    Semantic,
    Procedural,
}

impl PlanningSupportKind {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Semantic => "semantic",
            Self::Procedural => "procedural",
        }
    }
}

/// Revision-aware memory reference carried by a planning agenda.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlanningMemoryRef {
    pub kind: PlanningSupportKind,
    pub memory_id: MemoryId,
    pub logical_memory_id: LogicalMemoryId,
    pub title: String,
    pub evidence_resource_ids: Vec<ResourceId>,
}

/// One ordered subgoal inside a planning agenda proposal.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlanningSubgoal {
    pub order: u32,
    pub title: String,
    pub rationale: String,
    pub supporting_memories: Vec<PlanningMemoryRef>,
    pub evidence_resource_ids: Vec<ResourceId>,
    pub unresolved_gaps: Vec<String>,
    pub confidence: f32,
}

/// JSON payload embedded in quarantined semantic planning agendas.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlanningAgenda {
    pub goal: String,
    pub summary: String,
    pub ordered_subgoals: Vec<PlanningSubgoal>,
    pub supporting_memories: Vec<PlanningMemoryRef>,
    pub unresolved_gaps: Vec<String>,
    pub evidence_resource_ids: Vec<ResourceId>,
    pub quality_score: f32,
}

impl PlanningAgenda {
    pub fn to_json(&self) -> Result<String, HirnError> {
        serde_json::to_string(self).map_err(|error| {
            HirnError::InvalidInput(format!("failed to serialize planning agenda: {error}"))
        })
    }

    pub fn from_json(value: &str) -> Result<Self, HirnError> {
        serde_json::from_str(value).map_err(|error| {
            HirnError::InvalidInput(format!("failed to parse planning agenda: {error}"))
        })
    }
}

/// Generated cognition classes that flow through quarantine review.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GeneratedCognitionKind {
    DreamHypothesis,
    ReconcileProposal,
    PlanningAgenda,
}

/// Promotion decision for generated cognition captured in durable review metadata.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GeneratedCognitionDecision {
    PendingReview,
    RejectedByQualityGate,
    Approved,
    Rejected,
    RolledBack,
}

/// Whether a generated output requires explicit human review before promotion.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum GeneratedReviewRequirement {
    NotRequired,
    HumanReviewRequired,
}

/// Rollback receipt recorded once a generated output has been promoted.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GeneratedCognitionRollbackReceipt {
    pub applied_memory_ids: Vec<MemoryId>,
    pub previous_active_memory_ids: Vec<MemoryId>,
}

/// Durable quality and rollback metadata for generated cognition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GeneratedCognitionReview {
    pub kind: GeneratedCognitionKind,
    pub quality_score: f32,
    pub promotion_threshold: f32,
    pub decision: GeneratedCognitionDecision,
    pub review_requirement: GeneratedReviewRequirement,
    pub reasons: Vec<String>,
    pub rollback_receipt: Option<GeneratedCognitionRollbackReceipt>,
    pub rollback_reason: Option<String>,
    pub rolled_back_by: Option<AgentId>,
    pub rolled_back_at: Option<Timestamp>,
}

impl GeneratedCognitionReview {
    #[must_use]
    pub fn new(
        kind: GeneratedCognitionKind,
        quality_score: f32,
        promotion_threshold: f32,
        review_requirement: GeneratedReviewRequirement,
        reasons: Vec<String>,
    ) -> Self {
        let decision = if quality_score >= promotion_threshold {
            GeneratedCognitionDecision::PendingReview
        } else {
            GeneratedCognitionDecision::RejectedByQualityGate
        };

        Self {
            kind,
            quality_score: quality_score.clamp(0.0, 1.0),
            promotion_threshold: promotion_threshold.clamp(0.0, 1.0),
            decision,
            review_requirement,
            reasons,
            rollback_receipt: None,
            rollback_reason: None,
            rolled_back_by: None,
            rolled_back_at: None,
        }
    }

    #[must_use]
    pub const fn allows_promotion(&self) -> bool {
        matches!(self.decision, GeneratedCognitionDecision::PendingReview)
    }

    pub fn mark_approved(&mut self) {
        self.decision = GeneratedCognitionDecision::Approved;
    }

    pub fn mark_rejected(&mut self, reason: impl Into<String>) {
        self.decision = GeneratedCognitionDecision::Rejected;
        self.reasons.push(reason.into());
    }

    pub fn attach_rollback_receipt(&mut self, receipt: GeneratedCognitionRollbackReceipt) {
        self.rollback_receipt = Some(receipt);
    }

    pub fn mark_rolled_back(
        &mut self,
        rolled_back_by: AgentId,
        rolled_back_at: Timestamp,
        reason: impl Into<String>,
    ) {
        self.decision = GeneratedCognitionDecision::RolledBack;
        self.rollback_reason = Some(reason.into());
        self.rolled_back_by = Some(rolled_back_by);
        self.rolled_back_at = Some(rolled_back_at);
    }

    pub fn to_json(&self) -> Result<String, HirnError> {
        serde_json::to_string(self).map_err(|error| {
            HirnError::InvalidInput(format!(
                "failed to serialize generated cognition review: {error}"
            ))
        })
    }

    pub fn from_json(value: &str) -> Result<Self, HirnError> {
        serde_json::from_str(value).map_err(|error| {
            HirnError::InvalidInput(format!(
                "failed to parse generated cognition review: {error}"
            ))
        })
    }
}

/// Budget controls applied to an offline operator.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct OperatorBudget {
    pub wall_clock_limit_ms: u64,
    pub token_limit: u32,
    pub provider_spend_limit_usd: f32,
    pub max_result_volume: u32,
}

impl Default for OperatorBudget {
    fn default() -> Self {
        Self {
            wall_clock_limit_ms: 300_000,
            token_limit: 10_000,
            provider_spend_limit_usd: 1.0,
            max_result_volume: 1_000,
        }
    }
}

impl OperatorBudget {
    pub fn validate(&self, field: &str) -> Result<(), HirnError> {
        if self.wall_clock_limit_ms == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.wall_clock_limit_ms"),
                value: self.wall_clock_limit_ms.to_string(),
                reason: "wall-clock budget must be greater than zero".to_string(),
            });
        }
        if self.token_limit == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.token_limit"),
                value: self.token_limit.to_string(),
                reason: "token budget must be greater than zero".to_string(),
            });
        }
        if self.provider_spend_limit_usd < 0.0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.provider_spend_limit_usd"),
                value: self.provider_spend_limit_usd.to_string(),
                reason: "provider spend budget must be non-negative".to_string(),
            });
        }
        if self.max_result_volume == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.max_result_volume"),
                value: self.max_result_volume.to_string(),
                reason: "maximum result volume must be greater than zero".to_string(),
            });
        }

        Ok(())
    }
}

/// Policy applied when a job attempts to exceed its configured budget.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum BudgetExceededPolicy {
    #[default]
    Abort,
    Downgrade,
}

/// Durable summary of an offline job run.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
#[serde(default)]
pub struct OfflineJobOutcome {
    pub tokens_consumed: u32,
    pub provider_spend_usd: f32,
    pub result_count: u32,
    pub affected_memory_ids: Vec<MemoryId>,
    pub input_summary: Option<String>,
    pub output_summary: Option<String>,
    pub generated_review: Option<GeneratedCognitionReview>,
    pub change_summary: Option<String>,
}

impl OfflineJobOutcome {
    #[must_use]
    pub fn exceeds_budget(&self, budget: &OperatorBudget) -> bool {
        self.tokens_consumed > budget.token_limit
            || self.provider_spend_usd > budget.provider_spend_limit_usd
            || self.result_count > budget.max_result_volume
    }

    #[must_use]
    pub fn clamp_to_budget(&self, budget: &OperatorBudget) -> Self {
        let mut affected_memory_ids = self.affected_memory_ids.clone();
        affected_memory_ids.truncate(budget.max_result_volume as usize);
        Self {
            tokens_consumed: self.tokens_consumed.min(budget.token_limit),
            provider_spend_usd: self.provider_spend_usd.min(budget.provider_spend_limit_usd),
            result_count: self.result_count.min(budget.max_result_volume),
            affected_memory_ids,
            input_summary: self.input_summary.clone(),
            output_summary: self.output_summary.clone(),
            generated_review: self.generated_review.clone(),
            change_summary: self.change_summary.clone(),
        }
    }
}

/// Snapshot of a queued or completed offline job.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CognitiveJob {
    pub id: OfflineJobId,
    pub kind: CognitiveJobKind,
    pub priority: OfflineJobPriority,
    pub target: OfflineJobTarget,
    pub budget: OperatorBudget,
    pub budget_exceeded_policy: BudgetExceededPolicy,
    pub scheduled_by: Option<AgentId>,
    pub rationale: Option<String>,
}

impl CognitiveJob {
    #[must_use]
    pub fn new(kind: CognitiveJobKind, target: OfflineJobTarget) -> Self {
        Self {
            id: OfflineJobId::new(),
            kind,
            priority: OfflineJobPriority::default(),
            target,
            budget: OperatorBudget::default(),
            budget_exceeded_policy: BudgetExceededPolicy::default(),
            scheduled_by: None,
            rationale: None,
        }
    }

    pub fn validate(&self) -> Result<(), HirnError> {
        self.target.validate("target")?;
        self.budget.validate("budget")?;
        if let Some(rationale) = self.rationale.as_ref() {
            if rationale.trim().is_empty() {
                return Err(HirnError::InvalidConfig {
                    field: "rationale".to_string(),
                    value: rationale.clone(),
                    reason: "rationale must be non-empty when provided".to_string(),
                });
            }
        }

        Ok(())
    }
}

/// State of an offline job in the scheduler.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum OfflineJobStatus {
    Queued {
        enqueued_at: Timestamp,
    },
    Running {
        enqueued_at: Timestamp,
        started_at: Timestamp,
    },
    Completed {
        enqueued_at: Timestamp,
        started_at: Timestamp,
        finished_at: Timestamp,
        outcome: Box<OfflineJobOutcome>,
        downgraded: bool,
    },
    Failed {
        enqueued_at: Timestamp,
        started_at: Option<Timestamp>,
        finished_at: Timestamp,
        reason: String,
    },
    Skipped {
        enqueued_at: Timestamp,
        finished_at: Timestamp,
        reason: String,
    },
}

/// Durable audit record for an offline job transition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OfflineJobRecord {
    pub job: CognitiveJob,
    pub realm: String,
    pub namespace: Namespace,
    pub status: OfflineJobStatus,
    pub attempt_number: u32,
    pub transition_sequence: u32,
}

/// Full durable inspection payload for an offline job, including history.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OfflineJobInspection {
    pub latest: OfflineJobRecord,
    pub history: Vec<OfflineJobRecord>,
}

/// Operator-visible scheduler counters.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct OfflineSchedulerMetrics {
    pub queued_jobs: u64,
    pub running_jobs: u64,
    pub completed_jobs: u64,
    pub failed_jobs: u64,
    pub skipped_jobs: u64,
}

/// Recovery policy applied to persisted queued/running jobs on startup.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum OfflineRecoveryPolicy {
    #[default]
    RequeueInterrupted,
    MarkInterruptedFailed,
}

/// Policy-controlled retry behavior for failed jobs.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct OfflineRetryPolicy {
    /// Maximum retry attempts after the initial failed run.
    pub max_retry_attempts: u32,
    /// Delay before enqueuing a retry attempt.
    pub backoff_ms: u64,
}

impl Default for OfflineRetryPolicy {
    fn default() -> Self {
        Self {
            max_retry_attempts: 3,
            backoff_ms: 500,
        }
    }
}

impl OfflineRetryPolicy {
    pub fn validate(&self, field: &str) -> Result<(), HirnError> {
        if self.max_retry_attempts > 0 && self.backoff_ms == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.backoff_ms"),
                value: self.backoff_ms.to_string(),
                reason: "backoff_ms must be greater than zero when retries are enabled".to_string(),
            });
        }

        Ok(())
    }
}

/// Scheduler runtime configuration for queued offline cognition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct OfflineSchedulerConfig {
    pub enabled: bool,
    pub max_concurrent_jobs: usize,
    pub max_queue_depth: usize,
    pub default_budget: OperatorBudget,
    pub recovery_policy: OfflineRecoveryPolicy,
    pub retry_policy: OfflineRetryPolicy,
}

impl Default for OfflineSchedulerConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            max_concurrent_jobs: 2,
            max_queue_depth: 256,
            default_budget: OperatorBudget::default(),
            recovery_policy: OfflineRecoveryPolicy::default(),
            retry_policy: OfflineRetryPolicy::default(),
        }
    }
}

impl OfflineSchedulerConfig {
    pub fn validate(&self, field: &str) -> Result<(), HirnError> {
        if self.max_concurrent_jobs == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.max_concurrent_jobs"),
                value: self.max_concurrent_jobs.to_string(),
                reason: "max_concurrent_jobs must be greater than zero".to_string(),
            });
        }
        if self.max_queue_depth == 0 {
            return Err(HirnError::InvalidConfig {
                field: format!("{field}.max_queue_depth"),
                value: self.max_queue_depth.to_string(),
                reason: "max_queue_depth must be greater than zero".to_string(),
            });
        }
        self.default_budget
            .validate(&format!("{field}.default_budget"))?;
        self.retry_policy
            .validate(&format!("{field}.retry_policy"))?;

        Ok(())
    }
}