eidetic-engine 0.15.2

Durable, local-first, explainable memory for coding agents.
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
//! Posture summary and structured suggested action model (EE-041).
//!
//! Provides domain types for workspace posture assessment and
//! agent-friendly action suggestions. These types are designed
//! for deterministic JSON output and machine consumption.

/// Overall posture of the workspace.
///
/// Represents the operational readiness state as a tri-state:
/// ready for normal operation, degraded but usable, or needs attention.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Posture {
    /// All systems operational, ready for normal use.
    Ready,
    /// Some features degraded but core functionality works.
    Degraded,
    /// Workspace requires initialization or repair.
    NeedsAttention,
}

impl Posture {
    /// Stable string representation for JSON output.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Ready => "ready",
            Self::Degraded => "degraded",
            Self::NeedsAttention => "needs_attention",
        }
    }

    /// Returns true if the workspace is usable (ready or degraded).
    #[must_use]
    pub const fn is_usable(self) -> bool {
        matches!(self, Self::Ready | Self::Degraded)
    }

    /// All posture variants in severity order.
    #[must_use]
    pub const fn all() -> [Self; 3] {
        [Self::Ready, Self::Degraded, Self::NeedsAttention]
    }
}

impl std::fmt::Display for Posture {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Category of a suggested action.
///
/// Groups actions by their purpose to help agents prioritize.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ActionCategory {
    /// Workspace initialization or setup.
    Setup,
    /// Diagnostic or health check.
    Diagnostic,
    /// Repair or recovery action.
    Repair,
    /// Index rebuild or maintenance.
    Maintenance,
    /// Configuration change.
    Configuration,
}

impl ActionCategory {
    /// Stable string representation for JSON output.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Setup => "setup",
            Self::Diagnostic => "diagnostic",
            Self::Repair => "repair",
            Self::Maintenance => "maintenance",
            Self::Configuration => "configuration",
        }
    }

    /// All action categories.
    #[must_use]
    pub const fn all() -> [Self; 5] {
        [
            Self::Setup,
            Self::Diagnostic,
            Self::Repair,
            Self::Maintenance,
            Self::Configuration,
        ]
    }
}

impl std::fmt::Display for ActionCategory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A structured suggested action for agents.
///
/// Contains all information needed for an agent to understand,
/// prioritize, and execute a remediation step.
#[derive(Clone, Debug)]
pub struct SuggestedAction {
    /// Priority level (1 = highest, lower is more urgent).
    pub priority: u8,
    /// Category of this action.
    pub category: ActionCategory,
    /// The ee command to run.
    pub command: &'static str,
    /// Human-readable reason for this action.
    pub reason: &'static str,
    /// Whether this action is safe to run automatically.
    pub safe_for_automation: bool,
    /// Whether this action is idempotent (safe to retry).
    pub idempotent: bool,
}

impl SuggestedAction {
    /// Create a new suggested action with common defaults.
    #[must_use]
    pub const fn new(
        priority: u8,
        category: ActionCategory,
        command: &'static str,
        reason: &'static str,
    ) -> Self {
        Self {
            priority,
            category,
            command,
            reason,
            safe_for_automation: true,
            idempotent: true,
        }
    }

    /// Mark this action as not safe for automation.
    #[must_use]
    pub const fn requires_confirmation(mut self) -> Self {
        self.safe_for_automation = false;
        self
    }

    /// Mark this action as not idempotent.
    #[must_use]
    pub const fn not_idempotent(mut self) -> Self {
        self.idempotent = false;
        self
    }
}

/// Summary of workspace posture for quick assessment.
///
/// Provides a snapshot of workspace state with actionable suggestions.
#[derive(Clone, Debug)]
pub struct PostureSummary {
    /// Overall posture state.
    pub posture: Posture,
    /// Number of ready subsystems.
    pub ready_count: u8,
    /// Number of degraded subsystems.
    pub degraded_count: u8,
    /// Number of unavailable subsystems.
    pub unavailable_count: u8,
    /// Ordered list of suggested actions.
    pub suggested_actions: Vec<SuggestedAction>,
}

impl PostureSummary {
    /// Create a new posture summary.
    #[must_use]
    pub fn new(
        posture: Posture,
        ready_count: u8,
        degraded_count: u8,
        unavailable_count: u8,
    ) -> Self {
        Self {
            posture,
            ready_count,
            degraded_count,
            unavailable_count,
            suggested_actions: Vec::new(),
        }
    }

    /// Add a suggested action.
    pub fn add_action(&mut self, action: SuggestedAction) {
        self.suggested_actions.push(action);
    }

    /// Sort actions by priority (lowest number first).
    pub fn sort_actions(&mut self) {
        self.suggested_actions.sort_by_key(|a| a.priority);
    }

    /// Get the most urgent action, if any.
    #[must_use]
    pub fn most_urgent_action(&self) -> Option<&SuggestedAction> {
        self.suggested_actions.iter().min_by_key(|a| a.priority)
    }

    /// Get actions that are safe for automation.
    #[must_use]
    pub fn automatable_actions(&self) -> Vec<&SuggestedAction> {
        self.suggested_actions
            .iter()
            .filter(|a| a.safe_for_automation)
            .collect()
    }
}

/// E6 workspace/subsystem posture status.
///
/// This is intentionally separate from the older `Posture` enum above:
/// `Posture` powers the coarse `ee check` summary, while this enum is the
/// stable machine-facing status for per-subsystem diagnostics.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum SubsystemPostureStatus {
    Ok,
    DegradedRecoverable,
    DegradedRequired,
    Blocked,
    Unimplemented,
    Initializing,
}

impl SubsystemPostureStatus {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Ok => "ok",
            Self::DegradedRecoverable => "degraded_recoverable",
            Self::DegradedRequired => "degraded_required",
            Self::Blocked => "blocked",
            Self::Unimplemented => "unimplemented",
            Self::Initializing => "initializing",
        }
    }

    /// Aggregate subsystem statuses into the workspace-wide status.
    ///
    /// Deterministic precedence:
    /// blocked > degraded_required > degraded_recoverable/unimplemented >
    /// initializing > ok. An all-initializing workspace stays initializing.
    #[must_use]
    pub fn aggregate(statuses: &[Self]) -> Self {
        if statuses.is_empty() {
            return Self::Ok;
        }
        if statuses.iter().all(|status| *status == Self::Initializing) {
            return Self::Initializing;
        }
        if statuses.contains(&Self::Blocked) {
            return Self::Blocked;
        }
        if statuses.contains(&Self::DegradedRequired) {
            return Self::DegradedRequired;
        }
        if statuses
            .iter()
            .any(|status| matches!(status, Self::DegradedRecoverable | Self::Unimplemented))
        {
            return Self::DegradedRecoverable;
        }
        if statuses.contains(&Self::Initializing) {
            return Self::Initializing;
        }
        Self::Ok
    }
}

/// Per-subsystem posture row for workspace diagnostics.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SubsystemPostureReport {
    pub id: &'static str,
    pub status: SubsystemPostureStatus,
    pub reason: Option<&'static str>,
    pub fallback: Option<&'static str>,
    pub checks_passed: u32,
}

impl SubsystemPostureReport {
    #[must_use]
    pub const fn new(id: &'static str, status: SubsystemPostureStatus) -> Self {
        Self {
            id,
            status,
            reason: None,
            fallback: None,
            checks_passed: 0,
        }
    }

    #[must_use]
    pub const fn with_reason(mut self, reason: &'static str) -> Self {
        self.reason = Some(reason);
        self
    }

    #[must_use]
    pub const fn with_fallback(mut self, fallback: &'static str) -> Self {
        self.fallback = Some(fallback);
        self
    }

    #[must_use]
    pub const fn with_checks_passed(mut self, checks_passed: u32) -> Self {
        self.checks_passed = checks_passed;
        self
    }
}

/// Posture for the command currently being rendered.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct OperationPostureReport {
    pub status: SubsystemPostureStatus,
    pub subsystems_used: Vec<&'static str>,
    pub subsystems_skipped: Vec<&'static str>,
    pub degradations_applied: Vec<&'static str>,
}

impl OperationPostureReport {
    #[must_use]
    pub fn ok(subsystems_used: impl IntoIterator<Item = &'static str>) -> Self {
        Self {
            status: SubsystemPostureStatus::Ok,
            subsystems_used: subsystems_used.into_iter().collect(),
            subsystems_skipped: Vec::new(),
            degradations_applied: Vec::new(),
        }
    }
}

/// Subsystem ids that constitute the CORE memory store/retrieve loop.
///
/// Only these drive the top-line `overall` posture (ADR 0081, bd-1et0v.12):
/// they answer "can ee store and retrieve memory right now?". Every other
/// subsystem (`graph_compute`, `rch_worker_pressure`, `shard_fanout`,
/// `flight_recorder`, `singleflight`, `maintenance`, `agent_detection`,
/// `curate`, `feedback`, …) is ADVISORY — it stays visible as its own row but
/// never degrades the top-line unless it actually breaks the memory loop. Keep
/// this list in sync with ADR 0081 and the doctor CORE checks
/// (src/core/doctor.rs `gather_with_workspace`).
pub const CORE_SUBSYSTEM_IDS: &[&str] = &["runtime", "storage", "search", "memory", "pack"];

/// Workspace posture with aggregate, operation, and fixed subsystem rows.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WorkspacePostureReport {
    pub overall: SubsystemPostureStatus,
    pub this_operation: OperationPostureReport,
    pub subsystems: Vec<SubsystemPostureReport>,
}

impl WorkspacePostureReport {
    /// Build a report whose top-line `overall` aggregates **every** subsystem
    /// row. Retained for callers/tests that want the unfiltered aggregate;
    /// the live `ee status` surface uses [`Self::new_core_overall`].
    #[must_use]
    pub fn new(
        subsystems: Vec<SubsystemPostureReport>,
        this_operation: OperationPostureReport,
    ) -> Self {
        let statuses = subsystems
            .iter()
            .map(|subsystem| subsystem.status)
            .collect::<Vec<_>>();
        Self {
            overall: SubsystemPostureStatus::aggregate(&statuses),
            this_operation,
            subsystems,
        }
    }

    /// Build a report whose top-line `overall` aggregates **CORE subsystems
    /// only** ([`CORE_SUBSYSTEM_IDS`], ADR 0081 / bd-1et0v.12).
    ///
    /// Advisory subsystem rows remain present in `subsystems` with their own
    /// statuses but do not degrade `overall`, so a working memory store reports
    /// `overall: ok` even when an optional subsystem (NUMA pin, RCH worker
    /// pressure, CASS, …) is degraded. This mirrors the doctor surface, where
    /// `Posture::from_checks` skips advisory checks; the two surfaces therefore
    /// agree on a clean-with-advisories workspace.
    #[must_use]
    pub fn new_core_overall(
        subsystems: Vec<SubsystemPostureReport>,
        this_operation: OperationPostureReport,
    ) -> Self {
        let core_statuses = subsystems
            .iter()
            .filter(|subsystem| CORE_SUBSYSTEM_IDS.contains(&subsystem.id))
            .map(|subsystem| subsystem.status)
            .collect::<Vec<_>>();
        Self {
            overall: SubsystemPostureStatus::aggregate(&core_statuses),
            this_operation,
            subsystems,
        }
    }
}

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

    type TestResult = Result<(), String>;

    fn ensure<T: PartialEq + std::fmt::Debug>(actual: T, expected: T, ctx: &str) -> TestResult {
        if actual == expected {
            Ok(())
        } else {
            Err(format!("{ctx}: expected {expected:?}, got {actual:?}"))
        }
    }

    #[test]
    fn posture_as_str_is_stable() -> TestResult {
        ensure(Posture::Ready.as_str(), "ready", "ready")?;
        ensure(Posture::Degraded.as_str(), "degraded", "degraded")?;
        ensure(
            Posture::NeedsAttention.as_str(),
            "needs_attention",
            "needs_attention",
        )
    }

    #[test]
    fn posture_is_usable() -> TestResult {
        ensure(Posture::Ready.is_usable(), true, "ready is usable")?;
        ensure(Posture::Degraded.is_usable(), true, "degraded is usable")?;
        ensure(
            Posture::NeedsAttention.is_usable(),
            false,
            "needs_attention is not usable",
        )
    }

    #[test]
    fn action_category_as_str_is_stable() -> TestResult {
        ensure(ActionCategory::Setup.as_str(), "setup", "setup")?;
        ensure(
            ActionCategory::Diagnostic.as_str(),
            "diagnostic",
            "diagnostic",
        )?;
        ensure(ActionCategory::Repair.as_str(), "repair", "repair")?;
        ensure(
            ActionCategory::Maintenance.as_str(),
            "maintenance",
            "maintenance",
        )?;
        ensure(
            ActionCategory::Configuration.as_str(),
            "configuration",
            "configuration",
        )
    }

    #[test]
    fn suggested_action_builder_defaults() -> TestResult {
        let action =
            SuggestedAction::new(1, ActionCategory::Setup, "ee init", "Initialize workspace");
        ensure(action.priority, 1, "priority")?;
        ensure(action.safe_for_automation, true, "safe_for_automation")?;
        ensure(action.idempotent, true, "idempotent")
    }

    #[test]
    fn suggested_action_requires_confirmation() -> TestResult {
        let action = SuggestedAction::new(1, ActionCategory::Repair, "ee reset", "Reset workspace")
            .requires_confirmation();
        ensure(action.safe_for_automation, false, "not safe_for_automation")
    }

    #[test]
    fn posture_summary_most_urgent_action() -> TestResult {
        let mut summary = PostureSummary::new(Posture::NeedsAttention, 0, 0, 3);
        summary.add_action(SuggestedAction::new(
            3,
            ActionCategory::Maintenance,
            "ee index rebuild",
            "Rebuild index",
        ));
        summary.add_action(SuggestedAction::new(
            1,
            ActionCategory::Setup,
            "ee init",
            "Initialize",
        ));
        summary.add_action(SuggestedAction::new(
            2,
            ActionCategory::Diagnostic,
            "ee doctor",
            "Run doctor",
        ));

        let urgent = summary
            .most_urgent_action()
            .ok_or_else(|| "expected an urgent action".to_string())?;
        ensure(urgent.priority, 1, "most urgent is priority 1")
    }

    #[test]
    fn posture_summary_automatable_actions() -> TestResult {
        let mut summary = PostureSummary::new(Posture::Degraded, 1, 1, 1);
        summary.add_action(SuggestedAction::new(
            1,
            ActionCategory::Setup,
            "ee init",
            "Initialize",
        ));
        summary.add_action(
            SuggestedAction::new(2, ActionCategory::Repair, "ee reset", "Reset")
                .requires_confirmation(),
        );

        let automatable = summary.automatable_actions();
        ensure(automatable.len(), 1, "one automatable action")?;
        let first = automatable
            .first()
            .ok_or_else(|| "expected an automatable action".to_string())?;
        ensure(first.command, "ee init", "automatable command")
    }

    #[test]
    fn posture_all_returns_all_variants() -> TestResult {
        ensure(Posture::all().len(), 3, "three posture variants")
    }

    #[test]
    fn action_category_all_returns_all_variants() -> TestResult {
        ensure(ActionCategory::all().len(), 5, "five action categories")
    }

    #[test]
    fn subsystem_posture_status_wire_strings_are_stable() -> TestResult {
        ensure(SubsystemPostureStatus::Ok.as_str(), "ok", "ok")?;
        ensure(
            SubsystemPostureStatus::DegradedRecoverable.as_str(),
            "degraded_recoverable",
            "recoverable",
        )?;
        ensure(
            SubsystemPostureStatus::DegradedRequired.as_str(),
            "degraded_required",
            "required",
        )?;
        ensure(
            SubsystemPostureStatus::Blocked.as_str(),
            "blocked",
            "blocked",
        )?;
        ensure(
            SubsystemPostureStatus::Unimplemented.as_str(),
            "unimplemented",
            "unimplemented",
        )?;
        ensure(
            SubsystemPostureStatus::Initializing.as_str(),
            "initializing",
            "initializing",
        )
    }

    #[test]
    fn subsystem_posture_aggregation_precedence() -> TestResult {
        use SubsystemPostureStatus as S;
        ensure(S::aggregate(&[S::Ok, S::Ok]), S::Ok, "all ok")?;
        ensure(
            S::aggregate(&[S::Initializing, S::Initializing]),
            S::Initializing,
            "all initializing",
        )?;
        ensure(
            S::aggregate(&[S::Ok, S::Blocked, S::DegradedRequired]),
            S::Blocked,
            "blocked wins",
        )?;
        ensure(
            S::aggregate(&[S::Ok, S::DegradedRequired, S::DegradedRecoverable]),
            S::DegradedRequired,
            "required beats recoverable",
        )?;
        ensure(
            S::aggregate(&[S::Ok, S::DegradedRecoverable]),
            S::DegradedRecoverable,
            "recoverable beats ok",
        )?;
        ensure(
            S::aggregate(&[S::Ok, S::Unimplemented]),
            S::DegradedRecoverable,
            "unimplemented rolls up recoverable",
        )?;
        ensure(
            S::aggregate(&[S::Ok, S::Initializing]),
            S::Initializing,
            "mixed initializing remains initializing",
        )
    }

    #[test]
    fn workspace_posture_report_aggregates_subsystems() -> TestResult {
        let report = WorkspacePostureReport::new(
            vec![
                SubsystemPostureReport::new("runtime", SubsystemPostureStatus::Ok),
                SubsystemPostureReport::new("search", SubsystemPostureStatus::DegradedRecoverable)
                    .with_reason("index_missing")
                    .with_fallback("lexical_fallback"),
            ],
            OperationPostureReport::ok(["runtime", "search"]),
        );

        ensure(
            report.overall,
            SubsystemPostureStatus::DegradedRecoverable,
            "overall",
        )?;
        ensure(
            report.this_operation.status,
            SubsystemPostureStatus::Ok,
            "operation",
        )?;
        ensure(report.subsystems.len(), 2, "subsystem count")
    }

    #[test]
    fn core_overall_ignores_advisory_subsystem_degradation() -> TestResult {
        // ADR 0081 / bd-1et0v.12: advisory subsystem rows stay visible but
        // never degrade the top-line `overall`.
        use SubsystemPostureStatus as S;
        let subsystems = vec![
            SubsystemPostureReport::new("runtime", S::Ok),
            SubsystemPostureReport::new("storage", S::Ok),
            SubsystemPostureReport::new("search", S::Ok),
            SubsystemPostureReport::new("memory", S::Ok),
            SubsystemPostureReport::new("pack", S::Ok),
            // Advisory subsystems are degraded but must not flip the top-line.
            SubsystemPostureReport::new("graph_compute", S::Unimplemented),
            SubsystemPostureReport::new("rch_worker_pressure", S::DegradedRecoverable),
        ];
        let operation = OperationPostureReport::ok(["runtime"]);
        let core = WorkspacePostureReport::new_core_overall(subsystems.clone(), operation.clone());
        ensure(
            core.overall,
            S::Ok,
            "advisory degradation must not flip overall",
        )?;
        ensure(
            core.subsystems.len(),
            7,
            "advisory rows remain visible in subsystems",
        )?;
        // Sanity: the unfiltered constructor WOULD degrade on the same rows.
        let unfiltered = WorkspacePostureReport::new(subsystems, operation);
        ensure(
            unfiltered.overall,
            S::DegradedRecoverable,
            "unfiltered aggregate still includes advisory rows",
        )
    }

    #[test]
    fn core_overall_still_degrades_on_core_subsystem() -> TestResult {
        // A genuine CORE subsystem failure must still drive the top-line.
        use SubsystemPostureStatus as S;
        let subsystems = vec![
            SubsystemPostureReport::new("runtime", S::Ok),
            SubsystemPostureReport::new("storage", S::DegradedRecoverable),
            SubsystemPostureReport::new("graph_compute", S::Ok),
        ];
        let report = WorkspacePostureReport::new_core_overall(
            subsystems,
            OperationPostureReport::ok(["storage"]),
        );
        ensure(
            report.overall,
            S::DegradedRecoverable,
            "core subsystem degradation drives overall",
        )
    }

    #[test]
    fn core_subsystem_ids_are_the_memory_loop() -> TestResult {
        ensure(
            CORE_SUBSYSTEM_IDS,
            &["runtime", "storage", "search", "memory", "pack"][..],
            "core subsystem set is the store/retrieve memory loop",
        )
    }
}