aivcs-core 0.3.0

Core library for AIVCS domain logic and orchestration
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
//! Planning and long-horizon autonomy primitives.

use std::collections::{BTreeMap, BTreeSet, VecDeque};

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// A high-level goal containing epics and tasks.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GoalPlan {
    pub id: String,
    pub objective: String,
    pub epics: Vec<EpicPlan>,
}

/// Epic decomposition unit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EpicPlan {
    pub id: String,
    pub title: String,
    pub tasks: Vec<TaskPlan>,
}

/// Task decomposition unit.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TaskPlan {
    pub id: String,
    pub title: String,
    pub depends_on: Vec<String>,
    pub estimate_hours: u32,
}

/// Runtime task status.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", rename_all = "snake_case")]
pub enum PlanTaskStatus {
    Pending,
    InProgress,
    Done,
    Blocked { reason: String },
    Failed { reason: String },
}

/// Executable task node in the DAG.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PlanTask {
    pub id: String,
    pub title: String,
    pub depends_on: Vec<String>,
    pub estimate_hours: u32,
    pub status: PlanTaskStatus,
    pub confidence: f32,
    pub updated_at: DateTime<Utc>,
}

impl PlanTask {
    pub fn pending(id: &str, depends_on: Vec<String>, updated_at: DateTime<Utc>) -> Self {
        Self {
            id: id.to_string(),
            title: id.to_string(),
            depends_on,
            estimate_hours: 1,
            status: PlanTaskStatus::Pending,
            confidence: 1.0,
            updated_at,
        }
    }
}

/// Executable DAG form of a goal plan.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ExecutionDag {
    pub goal_id: String,
    pub objective: String,
    pub tasks: BTreeMap<String, PlanTask>,
}

impl ExecutionDag {
    pub fn validate(&self) -> Result<(), PlanningError> {
        for (task_id, task) in &self.tasks {
            for dep in &task.depends_on {
                if !self.tasks.contains_key(dep) {
                    return Err(PlanningError::MissingDependency {
                        task_id: task_id.clone(),
                        missing_dependency: dep.clone(),
                    });
                }
            }
        }

        let mut indegree: BTreeMap<String, usize> =
            self.tasks.keys().map(|k| (k.clone(), 0usize)).collect();
        let mut edges: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for (task_id, task) in &self.tasks {
            for dep in &task.depends_on {
                edges.entry(dep.clone()).or_default().push(task_id.clone());
                *indegree.get_mut(task_id).expect("task in indegree") += 1;
            }
        }

        let mut queue: VecDeque<String> = indegree
            .iter()
            .filter(|(_, d)| **d == 0)
            .map(|(k, _)| k.clone())
            .collect();

        let mut visited = 0usize;
        while let Some(node) = queue.pop_front() {
            visited += 1;
            if let Some(neighbors) = edges.get(&node) {
                for n in neighbors {
                    let entry = indegree.get_mut(n).expect("neighbor in indegree");
                    *entry -= 1;
                    if *entry == 0 {
                        queue.push_back(n.clone());
                    }
                }
            }
        }

        if visited != self.tasks.len() {
            return Err(PlanningError::CycleDetected);
        }
        Ok(())
    }
}

/// Scheduling controls.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SchedulerConstraints {
    pub max_parallel: usize,
    pub blocked_tasks: BTreeSet<String>,
}

/// Progress report over task execution state.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProgressReport {
    pub total_tasks: usize,
    pub done_tasks: usize,
    pub in_progress_tasks: usize,
    pub blocked_tasks: usize,
    pub failed_tasks: usize,
    pub pending_tasks: usize,
    pub completion_ratio: f32,
    pub confidence: f32,
    pub blockers: Vec<String>,
}

/// Replan trigger policy.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReplanPolicy {
    pub min_confidence: f32,
    pub max_blocked_ratio: f32,
    pub trigger_on_failure: bool,
    pub max_stale_hours: i64,
}

/// Replan reason taxonomy.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "reason", rename_all = "snake_case")]
pub enum ReplanReason {
    LowConfidence {
        observed: f32,
        threshold: f32,
    },
    BlockedRatio {
        observed: f32,
        threshold: f32,
    },
    FailedTasks {
        count: usize,
    },
    StaleProgress {
        stale_hours: i64,
        threshold_hours: i64,
    },
}

/// Replan decision output.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReplanDecision {
    pub should_replan: bool,
    pub reasons: Vec<ReplanReason>,
}

/// Long-run controls that bound autonomous replanning behavior.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RecoveryControls {
    /// Minimum time between successful replan actions.
    pub replan_cooldown_hours: i64,
    /// Hard cap on replans allowed in the active execution window.
    pub max_replans_per_window: u32,
    /// Safety circuit: stop autonomous replans after repeated failed attempts.
    pub max_consecutive_failed_replans: u32,
}

impl Default for RecoveryControls {
    fn default() -> Self {
        Self {
            replan_cooldown_hours: 2,
            max_replans_per_window: 4,
            max_consecutive_failed_replans: 2,
        }
    }
}

/// Runtime state used to enforce [`RecoveryControls`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
pub struct PlannerRuntimeState {
    pub last_replan_at: Option<DateTime<Utc>>,
    pub replans_in_window: u32,
    pub consecutive_failed_replans: u32,
}

/// Why a replan candidate was suppressed by recovery controls.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "reason", rename_all = "snake_case")]
pub enum ReplanSuppressionReason {
    CooldownActive {
        hours_since_last: i64,
        required_hours: i64,
    },
    WindowLimitReached {
        observed: u32,
        limit: u32,
    },
    ConsecutiveFailuresExceeded {
        observed: u32,
        limit: u32,
    },
}

/// Final replan decision after applying trigger policy and recovery controls.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ControlledReplanDecision {
    pub should_replan: bool,
    pub reasons: Vec<ReplanReason>,
    pub suppressed_by: Option<ReplanSuppressionReason>,
}

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum PlanningError {
    #[error("task '{task_id}' has missing dependency '{missing_dependency}'")]
    MissingDependency {
        task_id: String,
        missing_dependency: String,
    },
    #[error("dependency cycle detected in execution DAG")]
    CycleDetected,
}

/// Decompose a goal into executable DAG tasks.
pub fn decompose_goal_to_dag(goal: &GoalPlan) -> Result<ExecutionDag, PlanningError> {
    let mut tasks = BTreeMap::new();
    let now = Utc::now();
    for epic in &goal.epics {
        for t in &epic.tasks {
            tasks.insert(
                t.id.clone(),
                PlanTask {
                    id: t.id.clone(),
                    title: t.title.clone(),
                    depends_on: t.depends_on.clone(),
                    estimate_hours: t.estimate_hours,
                    status: PlanTaskStatus::Pending,
                    confidence: 1.0,
                    updated_at: now,
                },
            );
        }
    }

    let dag = ExecutionDag {
        goal_id: goal.id.clone(),
        objective: goal.objective.clone(),
        tasks,
    };
    dag.validate()?;
    Ok(dag)
}

/// Return dependency-ready tasks respecting constraints.
pub fn schedule_next_ready_tasks(
    dag: &ExecutionDag,
    constraints: &SchedulerConstraints,
) -> Result<Vec<String>, PlanningError> {
    dag.validate()?;
    if constraints.max_parallel == 0 {
        return Ok(Vec::new());
    }

    let mut ready: Vec<String> = dag
        .tasks
        .iter()
        .filter_map(|(id, task)| match task.status {
            PlanTaskStatus::Pending => Some((id, task)),
            _ => None,
        })
        .filter(|(id, _)| !constraints.blocked_tasks.contains(*id))
        .filter(|(_, task)| {
            task.depends_on.iter().all(|dep| {
                matches!(
                    dag.tasks.get(dep).map(|t| &t.status),
                    Some(PlanTaskStatus::Done)
                )
            })
        })
        .map(|(id, _)| id.clone())
        .collect();

    ready.sort();
    ready.truncate(constraints.max_parallel);
    Ok(ready)
}

/// Compute progress and confidence.
pub fn compute_progress(dag: &ExecutionDag) -> ProgressReport {
    let total = dag.tasks.len();
    let mut done = 0usize;
    let mut in_progress = 0usize;
    let mut blocked = 0usize;
    let mut failed = 0usize;
    let mut pending = 0usize;
    let mut blockers = Vec::new();
    let mut confidence_sum = 0.0f32;

    for task in dag.tasks.values() {
        confidence_sum += task.confidence;
        match &task.status {
            PlanTaskStatus::Done => done += 1,
            PlanTaskStatus::InProgress => in_progress += 1,
            PlanTaskStatus::Blocked { reason } => {
                blocked += 1;
                blockers.push(reason.clone());
            }
            PlanTaskStatus::Failed { .. } => failed += 1,
            PlanTaskStatus::Pending => pending += 1,
        }
    }

    let completion_ratio = if total == 0 {
        0.0
    } else {
        done as f32 / total as f32
    };
    let confidence = if total == 0 {
        0.0
    } else {
        confidence_sum / total as f32
    };

    ProgressReport {
        total_tasks: total,
        done_tasks: done,
        in_progress_tasks: in_progress,
        blocked_tasks: blocked,
        failed_tasks: failed,
        pending_tasks: pending,
        completion_ratio,
        confidence,
        blockers,
    }
}

/// Evaluate replan triggers from execution drift/failure state.
pub fn evaluate_replan(
    dag: &ExecutionDag,
    policy: &ReplanPolicy,
    now: DateTime<Utc>,
) -> ReplanDecision {
    let report = compute_progress(dag);
    let mut reasons = Vec::new();

    if report.confidence < policy.min_confidence {
        reasons.push(ReplanReason::LowConfidence {
            observed: report.confidence,
            threshold: policy.min_confidence,
        });
    }

    let blocked_ratio = if report.total_tasks == 0 {
        0.0
    } else {
        report.blocked_tasks as f32 / report.total_tasks as f32
    };
    if blocked_ratio > policy.max_blocked_ratio {
        reasons.push(ReplanReason::BlockedRatio {
            observed: blocked_ratio,
            threshold: policy.max_blocked_ratio,
        });
    }

    if policy.trigger_on_failure && report.failed_tasks > 0 {
        reasons.push(ReplanReason::FailedTasks {
            count: report.failed_tasks,
        });
    }

    if let Some(oldest) = dag.tasks.values().map(|t| t.updated_at).min() {
        let stale_hours = (now - oldest).num_hours();
        if stale_hours > policy.max_stale_hours {
            reasons.push(ReplanReason::StaleProgress {
                stale_hours,
                threshold_hours: policy.max_stale_hours,
            });
        }
    }

    ReplanDecision {
        should_replan: !reasons.is_empty(),
        reasons,
    }
}

/// Evaluate replan triggers and then enforce bounded recovery controls.
pub fn evaluate_replan_with_controls(
    dag: &ExecutionDag,
    policy: &ReplanPolicy,
    controls: &RecoveryControls,
    runtime: &PlannerRuntimeState,
    now: DateTime<Utc>,
) -> ControlledReplanDecision {
    let base = evaluate_replan(dag, policy, now);
    if !base.should_replan {
        return ControlledReplanDecision {
            should_replan: false,
            reasons: base.reasons,
            suppressed_by: None,
        };
    }

    if runtime.replans_in_window >= controls.max_replans_per_window {
        return ControlledReplanDecision {
            should_replan: false,
            reasons: base.reasons,
            suppressed_by: Some(ReplanSuppressionReason::WindowLimitReached {
                observed: runtime.replans_in_window,
                limit: controls.max_replans_per_window,
            }),
        };
    }

    if runtime.consecutive_failed_replans >= controls.max_consecutive_failed_replans {
        return ControlledReplanDecision {
            should_replan: false,
            reasons: base.reasons,
            suppressed_by: Some(ReplanSuppressionReason::ConsecutiveFailuresExceeded {
                observed: runtime.consecutive_failed_replans,
                limit: controls.max_consecutive_failed_replans,
            }),
        };
    }

    if let Some(last) = runtime.last_replan_at {
        let hours_since = (now - last).num_hours();
        if hours_since < controls.replan_cooldown_hours {
            return ControlledReplanDecision {
                should_replan: false,
                reasons: base.reasons,
                suppressed_by: Some(ReplanSuppressionReason::CooldownActive {
                    hours_since_last: hours_since,
                    required_hours: controls.replan_cooldown_hours,
                }),
            };
        }
    }

    ControlledReplanDecision {
        should_replan: true,
        reasons: base.reasons,
        suppressed_by: None,
    }
}