mana-core 0.3.2

Core library for mana — task tracker for AI coding agents
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
use std::fmt;

use crate::index::{ArchiveIndex, Index, IndexEntry};
use crate::unit::Status;

// ---------------------------------------------------------------------------
// Scope thresholds
// ---------------------------------------------------------------------------

/// Maximum number of `produces` artifacts before a unit is considered oversized.
pub const MAX_PRODUCES: usize = 3;

/// Maximum number of `paths` before a unit is considered oversized.
pub const MAX_PATHS: usize = 5;

// ---------------------------------------------------------------------------
// BlockReason
// ---------------------------------------------------------------------------

/// Why a unit cannot be dispatched right now.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockReason {
    /// One or more dependency units are not yet closed.
    WaitingOn(Vec<String>),
}

/// Soft scope warnings — displayed but don't block dispatch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScopeWarning {
    /// Scope is large: `produces > MAX_PRODUCES` or `paths > MAX_PATHS`.
    Oversized,
}

impl fmt::Display for BlockReason {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            BlockReason::WaitingOn(ids) => {
                write!(f, "waiting on {}", ids.join(", "))
            }
        }
    }
}

impl fmt::Display for ScopeWarning {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ScopeWarning::Oversized => write!(f, "oversized"),
        }
    }
}

// ---------------------------------------------------------------------------
// Unified blocking check
// ---------------------------------------------------------------------------

/// Check whether `entry` is blocked, returning the reason if so.
///
/// Checks in priority order:
/// 1. **Explicit dependencies** — any dep that isn't closed (or doesn't exist).
/// 2. **Requires/produces** — sibling units that produce a required artifact
///    but aren't closed yet.
///
/// Note: This overload does not check archived units. If a dependency was closed
/// and archived, it will appear unsatisfied. Use [`check_blocked_with_archive`]
/// when archive awareness is needed (e.g., `mana run`).
pub fn check_blocked(entry: &IndexEntry, index: &Index) -> Option<BlockReason> {
    check_blocked_with_archive(entry, index, None)
}

/// Like [`check_blocked`], but also checks the archive index.
/// Archived units are treated as closed (satisfied).
pub fn check_blocked_with_archive(
    entry: &IndexEntry,
    index: &Index,
    archive: Option<&ArchiveIndex>,
) -> Option<BlockReason> {
    let mut waiting_on = Vec::new();

    // Explicit dependencies
    for dep_id in &entry.dependencies {
        match index.units.iter().find(|e| e.id == *dep_id) {
            Some(dep) if dep.status == Status::Closed => {}
            Some(_) => waiting_on.push(dep_id.clone()), // active but not closed
            None => {
                // Not in active index — check archive (archived = closed)
                let in_archive = archive
                    .map(|a| a.units.iter().any(|e| e.id == *dep_id))
                    .unwrap_or(false);
                if !in_archive {
                    waiting_on.push(dep_id.clone());
                }
            }
        }
    }

    // Smart dependencies: requires → sibling produces
    for required in &entry.requires {
        if let Some(producer) = index
            .units
            .iter()
            .find(|e| e.id != entry.id && e.parent == entry.parent && e.produces.contains(required))
        {
            if producer.status != Status::Closed && !waiting_on.contains(&producer.id) {
                waiting_on.push(producer.id.clone());
            }
        }
        // If no active producer found, check archive — archived producers are satisfied
    }

    if !waiting_on.is_empty() {
        return Some(BlockReason::WaitingOn(waiting_on));
    }

    None
}

/// Check for scope warnings (non-blocking).
///
/// Returns a warning if scope is large (`produces > MAX_PRODUCES` or `paths > MAX_PATHS`).
/// Units with no scope (no produces, no paths) are fine — not every unit needs explicit paths.
pub fn check_scope_warning(entry: &IndexEntry) -> Option<ScopeWarning> {
    if entry.produces.len() > MAX_PRODUCES || entry.paths.len() > MAX_PATHS {
        return Some(ScopeWarning::Oversized);
    }
    None
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_entry(id: &str) -> IndexEntry {
        IndexEntry {
            handle: None,
            id: id.to_string(),
            title: format!("Unit {}", id),
            status: Status::Open,
            priority: 2,
            parent: None,
            dependencies: vec![],
            labels: vec![],
            assignee: None,
            updated_at: Utc::now(),
            produces: vec![],
            requires: vec![],
            has_verify: true,
            verify: None,
            created_at: Utc::now(),
            claimed_by: None,
            attempts: 0,
            paths: vec![],
            kind: crate::unit::UnitType::Task,
            feature: false,
            has_decisions: false,
        }
    }

    fn make_index(entries: Vec<IndexEntry>) -> Index {
        Index { units: entries }
    }

    // -- WaitingOn: explicit deps --

    #[test]
    fn blocking_not_blocked_when_deps_closed() {
        let mut dep = make_entry("1");
        dep.status = Status::Closed;

        let mut entry = make_entry("2");
        entry.dependencies = vec!["1".into()];
        entry.produces = vec!["Foo".into()];
        entry.paths = vec!["src/foo.rs".into()];

        let index = make_index(vec![dep, entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
    }

    #[test]
    fn blocking_waiting_on_open_dep() {
        let dep = make_entry("1"); // open

        let mut entry = make_entry("2");
        entry.dependencies = vec!["1".into()];
        entry.produces = vec!["Foo".into()];
        entry.paths = vec!["src/foo.rs".into()];

        let index = make_index(vec![dep, entry.clone()]);
        assert_eq!(
            check_blocked(&entry, &index),
            Some(BlockReason::WaitingOn(vec!["1".into()]))
        );
    }

    #[test]
    fn blocking_waiting_on_missing_dep() {
        let mut entry = make_entry("2");
        entry.dependencies = vec!["999".into()]; // doesn't exist
        entry.produces = vec!["Foo".into()];
        entry.paths = vec!["src/foo.rs".into()];

        let index = make_index(vec![entry.clone()]);
        assert_eq!(
            check_blocked(&entry, &index),
            Some(BlockReason::WaitingOn(vec!["999".into()]))
        );
    }

    #[test]
    fn blocking_waiting_on_multiple_deps() {
        let dep_a = make_entry("1"); // open
        let dep_b = make_entry("3"); // open

        let mut entry = make_entry("2");
        entry.dependencies = vec!["1".into(), "3".into()];
        entry.produces = vec!["Foo".into()];
        entry.paths = vec!["src/foo.rs".into()];

        let index = make_index(vec![dep_a, entry.clone(), dep_b]);
        assert_eq!(
            check_blocked(&entry, &index),
            Some(BlockReason::WaitingOn(vec!["1".into(), "3".into()]))
        );
    }

    // -- WaitingOn: requires/produces --

    #[test]
    fn blocking_waiting_on_sibling_producer() {
        let mut producer = make_entry("5.1");
        producer.parent = Some("5".into());
        producer.produces = vec!["UserType".into()];

        let mut consumer = make_entry("5.2");
        consumer.parent = Some("5".into());
        consumer.requires = vec!["UserType".into()];
        consumer.produces = vec!["UserAPI".into()];
        consumer.paths = vec!["src/api.rs".into()];

        let index = make_index(vec![producer, consumer.clone()]);
        assert_eq!(
            check_blocked(&consumer, &index),
            Some(BlockReason::WaitingOn(vec!["5.1".into()]))
        );
    }

    #[test]
    fn blocking_not_blocked_when_producer_closed() {
        let mut producer = make_entry("5.1");
        producer.parent = Some("5".into());
        producer.produces = vec!["UserType".into()];
        producer.status = Status::Closed;

        let mut consumer = make_entry("5.2");
        consumer.parent = Some("5".into());
        consumer.requires = vec!["UserType".into()];
        consumer.produces = vec!["UserAPI".into()];
        consumer.paths = vec!["src/api.rs".into()];

        let index = make_index(vec![producer, consumer.clone()]);
        assert_eq!(check_blocked(&consumer, &index), None);
    }

    #[test]
    fn blocking_no_duplicate_when_dep_and_requires_overlap() {
        let mut producer = make_entry("5.1");
        producer.parent = Some("5".into());
        producer.produces = vec!["UserType".into()];

        let mut consumer = make_entry("5.2");
        consumer.parent = Some("5".into());
        consumer.dependencies = vec!["5.1".into()]; // explicit dep
        consumer.requires = vec!["UserType".into()]; // also requires from same unit
        consumer.produces = vec!["UserAPI".into()];
        consumer.paths = vec!["src/api.rs".into()];

        let index = make_index(vec![producer, consumer.clone()]);
        if let Some(BlockReason::WaitingOn(ids)) = check_blocked(&consumer, &index) {
            // 5.1 should appear only once even though it's both an explicit dep and a producer
            assert_eq!(ids, vec!["5.1".to_string()]);
        } else {
            panic!("Expected WaitingOn");
        }
    }

    // -- Scope warnings (non-blocking) --

    #[test]
    fn warning_oversized_too_many_produces() {
        let mut entry = make_entry("1");
        entry.produces = vec!["A".into(), "B".into(), "C".into(), "D".into()]; // 4 > MAX_PRODUCES
        entry.paths = vec!["src/a.rs".into()];

        // Not blocked — just a warning
        let index = make_index(vec![entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
        assert_eq!(check_scope_warning(&entry), Some(ScopeWarning::Oversized));
    }

    #[test]
    fn warning_oversized_too_many_paths() {
        let mut entry = make_entry("1");
        entry.produces = vec!["A".into()];
        entry.paths = vec![
            "a.rs".into(),
            "b.rs".into(),
            "c.rs".into(),
            "d.rs".into(),
            "e.rs".into(),
            "f.rs".into(),
        ]; // 6 > MAX_PATHS

        let index = make_index(vec![entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
        assert_eq!(check_scope_warning(&entry), Some(ScopeWarning::Oversized));
    }

    #[test]
    fn warning_not_oversized_at_threshold() {
        let mut entry = make_entry("1");
        entry.produces = vec!["A".into(), "B".into(), "C".into()]; // exactly MAX_PRODUCES
        entry.paths = vec![
            "a.rs".into(),
            "b.rs".into(),
            "c.rs".into(),
            "d.rs".into(),
            "e.rs".into(),
        ]; // exactly MAX_PATHS

        assert_eq!(check_scope_warning(&entry), None);
    }

    // -- Unscoped is NOT blocking --

    #[test]
    fn unscoped_unit_is_not_blocked() {
        let entry = make_entry("1"); // produces=[], paths=[]

        let index = make_index(vec![entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
    }

    #[test]
    fn not_blocked_with_produces_only() {
        let mut entry = make_entry("1");
        entry.produces = vec!["SomeType".into()];

        let index = make_index(vec![entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
    }

    #[test]
    fn not_blocked_with_paths_only() {
        let mut entry = make_entry("1");
        entry.paths = vec!["src/main.rs".into()];

        let index = make_index(vec![entry.clone()]);
        assert_eq!(check_blocked(&entry, &index), None);
    }

    // -- Display --

    #[test]
    fn blocking_display_waiting_on() {
        let reason = BlockReason::WaitingOn(vec!["3.1".into(), "3.2".into()]);
        assert_eq!(format!("{}", reason), "waiting on 3.1, 3.2");
    }

    #[test]
    fn warning_display_oversized() {
        assert_eq!(format!("{}", ScopeWarning::Oversized), "oversized");
    }

    // -- Priority: deps still checked --

    #[test]
    fn blocking_deps_still_block_oversized_units() {
        let dep = make_entry("1"); // open

        let mut entry = make_entry("2");
        entry.dependencies = vec!["1".into()];
        entry.produces = vec!["A".into(), "B".into(), "C".into(), "D".into()]; // oversized
        entry.paths = vec!["a.rs".into()];

        let index = make_index(vec![dep, entry.clone()]);
        assert!(matches!(
            check_blocked(&entry, &index),
            Some(BlockReason::WaitingOn(_))
        ));
    }

    #[test]
    fn blocking_deps_still_block_unscoped_units() {
        let dep = make_entry("1"); // open

        let mut entry = make_entry("2");
        entry.dependencies = vec!["1".into()];
        // produces=[], paths=[] → unscoped but deps block first

        let index = make_index(vec![dep, entry.clone()]);
        assert!(matches!(
            check_blocked(&entry, &index),
            Some(BlockReason::WaitingOn(_))
        ));
    }
}