Skip to main content

organism_planning/
lib.rs

1//! Planning layer.
2//!
3//! Multi-model collaborative planning. A huddle runs several reasoning
4//! systems in parallel, then the debate loop refines candidate plans
5//! before they're handed to the simulation swarm.
6//!
7//! Plans flow through converge's PromotionGate like any other proposal.
8//! No special bypass; standard convergence pipeline applies.
9
10pub mod charter_derivation;
11pub mod collaboration;
12pub mod dd;
13pub mod debate;
14pub mod fuzzy_reasoner;
15pub mod fuzzy_suggestor;
16pub mod huddle;
17pub mod huddle_invocation;
18pub mod kb;
19pub mod ml_prediction_reasoner;
20pub mod provenance;
21pub mod shape_hypothesis;
22pub mod suggestor;
23pub mod topology_transition;
24
25pub use fuzzy_reasoner::FuzzyReasoner;
26pub use fuzzy_suggestor::{
27    FuzzyInferenceSuggestor, FuzzyInferenceTrace, FuzzyRuleActivationTrace, FuzzySuggestorError,
28};
29pub use ml_prediction_reasoner::{
30    ML_PREDICTION_REASONER_META, MlPredictionMode, MlPredictionReasoner,
31    MlPredictionReasonerDescriptor,
32};
33pub use prism::fuzzy::{
34    FuzzyConsequent, FuzzyExpression, FuzzyRule, FuzzySet, LinguisticVariable, MembershipFunction,
35};
36
37use converge_pack::FactId;
38use organism_intent::IntentPacket;
39use serde::{Deserialize, Serialize};
40use uuid::Uuid;
41
42pub use collaboration::{
43    CollaborationCharter, CollaborationDiscipline, CollaborationMember, CollaborationRole,
44    CollaborationTopology, CollaborationValidationError, ConsensusRule, TeamFormation,
45    TeamFormationMode, TurnCadence,
46};
47pub use huddle_invocation::{HuddleInvocation, HuddleInvocationKind, HuddleUrgency};
48
49// ── Plan ───────────────────────────────────────────────────────────
50
51/// A candidate plan produced by reasoning. Plans are *proposals*, not
52/// commitments — authority is recomputed at the commit boundary.
53#[derive(Debug, Clone, Serialize, Deserialize)]
54pub struct Plan {
55    pub id: Uuid,
56    pub intent: Uuid,
57    pub steps: Vec<PlanStep>,
58    pub rationale: String,
59    pub annotation: PlanAnnotation,
60    pub contributor: ReasoningSystem,
61}
62
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct PlanStep {
65    pub action: String,
66    pub expected_effect: String,
67}
68
69impl Plan {
70    pub fn new(intent: &IntentPacket, rationale: impl Into<String>) -> Self {
71        Self {
72            id: Uuid::new_v4(),
73            intent: intent.id,
74            steps: Vec::new(),
75            rationale: rationale.into(),
76            annotation: PlanAnnotation::default(),
77            contributor: ReasoningSystem::LlmReasoning,
78        }
79    }
80}
81
82// ── Plan Annotation ────────────────────────────────────────────────
83
84#[derive(Debug, Clone, Default, Serialize, Deserialize)]
85pub struct PlanAnnotation {
86    pub impacts: Vec<Impact>,
87    pub costs: Vec<CostEstimate>,
88    pub risks: Vec<Risk>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct Impact {
93    pub description: String,
94    pub confidence: f64,
95}
96
97#[derive(Debug, Clone, Serialize, Deserialize)]
98pub struct CostEstimate {
99    pub description: String,
100    pub compute_cost: f64,
101    pub time_cost: f64,
102    pub unit: String,
103}
104
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct Risk {
107    pub description: String,
108    pub likelihood: Likelihood,
109    pub impact: RiskImpact,
110    pub mitigation: Option<String>,
111}
112
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
114#[serde(rename_all = "snake_case")]
115pub enum Likelihood {
116    Low,
117    Medium,
118    High,
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
122#[serde(rename_all = "snake_case")]
123pub enum RiskImpact {
124    Low,
125    Medium,
126    High,
127    Critical,
128}
129
130// ── Reasoning Systems ──────────────────────────────────────────────
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
133#[serde(rename_all = "snake_case")]
134pub enum ReasoningSystem {
135    LlmReasoning,
136    ConstraintSolver,
137    MlPrediction,
138    CausalAnalysis,
139    CostEstimation,
140    DomainModel,
141    /// Graded reasoning over linguistic variables via `prism::fuzzy`.
142    /// Holds membership-of-X-set semantics through to synthesis instead of
143    /// collapsing to binary outcomes.
144    FuzzyReasoning,
145}
146
147// ── Huddle ─────────────────────────────────────────────────────────
148
149#[derive(Debug, Clone, Serialize, Deserialize)]
150pub struct PlanContribution {
151    pub system: ReasoningSystem,
152    pub suggestions: Vec<String>,
153    pub constraints: Vec<String>,
154    pub risks: Vec<Risk>,
155}
156
157/// A reasoning capability participating in a huddle.
158#[async_trait::async_trait]
159pub trait Reasoner: Send + Sync {
160    fn name(&self) -> &str;
161    fn system_type(&self) -> ReasoningSystem;
162    async fn propose(&self, intent: &IntentPacket) -> anyhow::Result<Plan>;
163    fn contribute(&self, context: &serde_json::Value) -> PlanContribution;
164}
165
166// ── Plan Bundle (debate output) ────────────────────────────────────
167
168#[derive(Debug, Clone, Serialize, Deserialize)]
169pub struct PlanBundle {
170    pub plans: Vec<Plan>,
171    pub debate_rounds: u32,
172}
173
174// ── Hypothesis Tracking ───────────────────────────────────────────
175
176/// Lifecycle state of a tracked hypothesis.
177#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
178#[serde(rename_all = "snake_case", tag = "outcome")]
179pub enum HypothesisOutcome {
180    Open,
181    Confirmed,
182    Falsified { contradiction_id: FactId },
183    Superseded,
184    Unresolved,
185}
186
187/// A hypothesis tracked across convergence cycles.
188///
189/// Created from a `ContextKey::Hypotheses` fact. The tracker records
190/// when it was first seen, its confidence trajectory, and its final outcome.
191#[derive(Debug, Clone, Serialize, Deserialize)]
192pub struct TrackedHypothesis {
193    pub fact_id: FactId,
194    pub domain: String,
195    pub claim: String,
196    pub confidence: f64,
197    pub formed_cycle: u32,
198    pub resolved_cycle: Option<u32>,
199    pub outcome: HypothesisOutcome,
200}