Skip to main content

phago_core/primitives/
apoptose.rs

1//! APOPTOSE — Programmed Cell Death
2//!
3//! Apoptosis is genetically programmed, orderly cell death. Unlike necrosis
4//! (accidental death causing inflammation), apoptosis is clean: the cell
5//! shrinks, packages its contents into membrane-bound bodies, and signals
6//! for recycling. No collateral damage.
7//!
8//! The decision to die is **intrinsic**. The cell evaluates its own state
9//! and initiates its own death. In Rust, `trigger_apoptosis` takes `self`
10//! by value — the agent is consumed by its own death. The `Drop` trait
11//! handles resource cleanup, providing deterministic death without GC.
12
13use crate::types::{CellHealth, DeathSignal};
14
15/// Self-assess integrity and gracefully self-terminate when compromised.
16///
17/// Every agent must be able to honestly evaluate whether it is still
18/// useful to the colony. Agents that are stuck, redundant, or compromised
19/// should die — releasing resources and final learnings.
20pub trait Apoptose {
21    /// Evaluate own health and usefulness.
22    ///
23    /// This is introspection: the agent examines its own state,
24    /// recent output quality, resource consumption, and whether
25    /// other agents already cover its function.
26    fn self_assess(&self) -> CellHealth;
27
28    /// Whether the agent should initiate death.
29    ///
30    /// Default implementation triggers death for Compromised,
31    /// Redundant, or Senescent states.
32    fn should_die(&self) -> bool {
33        self.self_assess().should_die()
34    }
35
36    /// Package final state into a death signal before destruction.
37    ///
38    /// This prepares the apoptotic bodies — the final learnings that
39    /// other agents can digest. The colony runtime calls this before
40    /// dropping the agent. Works with trait objects (`&dyn Apoptose`).
41    fn prepare_death_signal(&self) -> DeathSignal;
42
43    /// Initiate orderly self-destruction (for concrete types).
44    ///
45    /// This method takes `self` by value — the agent is consumed.
46    /// After this call, the agent no longer exists. Rust's ownership
47    /// system guarantees this — no dangling references, no zombie agents.
48    ///
49    /// Default implementation calls `prepare_death_signal` then drops self.
50    fn trigger_apoptosis(self) -> DeathSignal
51    where
52        Self: Sized,
53    {
54        self.prepare_death_signal()
55    }
56}