nusy-kanban 0.15.2

Arrow-native kanban engine — SHACL shapes, graph-native PRs, dual boards, NATS multi-agent collaboration
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
//! Persistence — load and save KanbanStore from/to Parquet files.
//!
//! Delegates to `nusy-arrow-git::save_named_batches()` for crash-safe
//! atomic Parquet persistence (WAL + atomic tmp+rename).

use crate::comments::CommentsStore;
use crate::crud::KanbanStore;
use crate::relations::RelationsStore;
use arrow::array::{RecordBatch, new_null_array};
use nusy_arrow_git::save::{restore_named_batches, save_named_batches};
#[cfg(feature = "pr")]
use nusy_graph_review::{CiResultStore, CommentStore, ProposalStore};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Errors from persistence operations.
#[derive(Debug, thiserror::Error)]
pub enum PersistError {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("Parquet error: {0}")]
    Parquet(#[from] parquet::errors::ParquetError),

    #[error("Arrow error: {0}")]
    Arrow(#[from] arrow::error::ArrowError),

    #[error("Save error: {0}")]
    Save(#[from] nusy_arrow_git::save::SaveError),
}

pub type Result<T> = std::result::Result<T, PersistError>;

/// The data directory for Arrow-kanban state.
const DATA_DIR: &str = ".nusy-kanban";

/// Normalize a RecordBatch to match the target schema.
///
/// If the batch has fewer columns than the target, null columns are appended.
/// This handles schema evolution: old Parquet files with fewer columns can be
/// loaded by new code with additional columns.
fn normalize_batch(
    batch: &RecordBatch,
    target_schema: &arrow::datatypes::Schema,
) -> Result<RecordBatch> {
    let batch_cols = batch.num_columns();
    let target_cols = target_schema.fields().len();

    if batch_cols >= target_cols {
        return Ok(batch.clone());
    }

    let num_rows = batch.num_rows();
    let mut columns: Vec<Arc<dyn arrow::array::Array>> = Vec::with_capacity(target_cols);

    for i in 0..batch_cols {
        columns.push(batch.column(i).clone());
    }

    for i in batch_cols..target_cols {
        let field = target_schema.field(i);
        columns.push(new_null_array(field.data_type(), num_rows));
    }

    Ok(RecordBatch::try_new(
        Arc::new(target_schema.clone()),
        columns,
    )?)
}

fn normalize_batches(
    batches: Vec<RecordBatch>,
    target_schema: &arrow::datatypes::Schema,
) -> Result<Vec<RecordBatch>> {
    batches
        .iter()
        .map(|b| normalize_batch(b, target_schema))
        .collect()
}

/// Get the data directory path, creating it if necessary.
pub fn data_dir(root: &Path) -> Result<PathBuf> {
    let dir = root.join(DATA_DIR);
    if !dir.exists() {
        fs::create_dir_all(&dir)?;
    }
    Ok(dir)
}

/// Load a KanbanStore from Parquet files in the data directory.
///
/// Auto-normalizes old Parquet files with fewer columns to the current schema.
pub fn load_store(root: &Path) -> Result<KanbanStore> {
    let dir = data_dir(root)?;
    let mut store = KanbanStore::new();

    let results = restore_named_batches(&dir, &["items", "runs", "item_comments"])?;
    for (name, batches) in results {
        match name.as_str() {
            "items" => {
                let normalized = normalize_batches(batches, store.items_schema())?;
                store.load_items(normalized);
            }
            "runs" => {
                let normalized = normalize_batches(batches, store.runs_schema())?;
                store.load_runs(normalized);
            }
            "item_comments" => {
                let normalized = normalize_batches(batches, store.comments_schema())?;
                store.load_comments(normalized);
            }
            _ => {}
        }
    }

    // Migrate legacy comments from runs table (only if no comments loaded yet)
    if store.comments_batches().is_empty() {
        let mut migrator = CommentsStore::new();
        migrator.migrate_from_runs(store.runs_batches());
        if !migrator.is_empty() {
            store.load_comments(migrator.batches().to_vec());
        }
    }

    Ok(store)
}

/// Load a RelationsStore from a Parquet file in the data directory.
pub fn load_relations(root: &Path) -> Result<RelationsStore> {
    let dir = data_dir(root)?;
    let mut store = RelationsStore::new();

    let results = restore_named_batches(&dir, &["relations"])?;
    for (name, batches) in results {
        if name == "relations" {
            let normalized = normalize_batches(batches, store.schema())?;
            store.load(normalized);
        }
    }

    Ok(store)
}

/// Save a KanbanStore to Parquet files in the data directory.
///
/// Uses `nusy-arrow-git::save_named_batches()` for crash-safe atomic writes.
pub fn save_store(root: &Path, store: &KanbanStore) -> Result<()> {
    let dir = data_dir(root)?;
    save_named_batches(
        &[
            ("items", store.items_batches(), store.items_schema()),
            ("runs", store.runs_batches(), store.runs_schema()),
            (
                "item_comments",
                store.comments_batches(),
                store.comments_schema(),
            ),
        ],
        &dir,
    )?;
    Ok(())
}

/// Save a RelationsStore to a Parquet file in the data directory.
pub fn save_relations(root: &Path, store: &RelationsStore) -> Result<()> {
    let dir = data_dir(root)?;
    save_named_batches(&[("relations", store.batches(), store.schema())], &dir)?;
    Ok(())
}

/// Save all kanban data atomically (items + runs + relations).
///
/// Single WAL covers all three datasets — better crash safety than
/// calling `save_store` + `save_relations` separately.
pub fn save_all(root: &Path, store: &KanbanStore, relations: &RelationsStore) -> Result<()> {
    let dir = data_dir(root)?;
    save_named_batches(
        &[
            ("items", store.items_batches(), store.items_schema()),
            ("runs", store.runs_batches(), store.runs_schema()),
            (
                "item_comments",
                store.comments_batches(),
                store.comments_schema(),
            ),
            ("relations", relations.batches(), relations.schema()),
        ],
        &dir,
    )?;
    Ok(())
}

/// Load all kanban data (items + runs + relations).
///
/// Auto-normalizes old Parquet files with fewer columns to the current schema.
pub fn load_all(root: &Path) -> Result<(KanbanStore, RelationsStore)> {
    let dir = data_dir(root)?;
    let mut store = KanbanStore::new();
    let mut relations = RelationsStore::new();

    let results = restore_named_batches(&dir, &["items", "runs", "item_comments", "relations"])?;
    for (name, batches) in results {
        match name.as_str() {
            "items" => {
                let normalized = normalize_batches(batches, store.items_schema())?;
                store.load_items(normalized);
            }
            "runs" => {
                let normalized = normalize_batches(batches, store.runs_schema())?;
                store.load_runs(normalized);
            }
            "item_comments" => {
                let normalized = normalize_batches(batches, store.comments_schema())?;
                store.load_comments(normalized);
            }
            "relations" => {
                let normalized = normalize_batches(batches, relations.schema())?;
                relations.load(normalized);
            }
            _ => {}
        }
    }

    // Migrate legacy comments from runs table (only if no comments loaded yet)
    if store.comments_batches().is_empty() {
        let mut migrator = CommentsStore::new();
        migrator.migrate_from_runs(store.runs_batches());
        if !migrator.is_empty() {
            store.load_comments(migrator.batches().to_vec());
        }
    }

    Ok((store, relations))
}

#[cfg(feature = "pr")]
/// Save proposals, comments, and CI results to Parquet files in the data directory.
pub fn save_proposals(
    root: &Path,
    proposals: &ProposalStore,
    comments: &CommentStore,
    ci_results: &CiResultStore,
) -> Result<()> {
    let dir = data_dir(root)?;
    let proposals_batches = proposals.proposals_batches();
    let comments_batches = comments.comments_batches();
    let ci_batches = ci_results.ci_batches();
    save_named_batches(
        &[
            ("proposals", proposals_batches, proposals.proposals_schema()),
            ("comments", comments_batches, comments.comments_schema()),
            ("ci_results", ci_batches, ci_results.ci_schema()),
        ],
        &dir,
    )?;
    Ok(())
}

#[cfg(feature = "pr")]
/// Load proposals, comments, and CI results from Parquet files in the data directory.
///
/// Auto-normalizes old Parquet files with fewer columns to the current schema.
pub fn load_proposals(root: &Path) -> Result<(ProposalStore, CommentStore, CiResultStore)> {
    let dir = data_dir(root)?;
    let mut proposals = ProposalStore::new();
    let mut comments = CommentStore::new();
    let mut ci_results = CiResultStore::new();

    let results = restore_named_batches(&dir, &["proposals", "comments", "ci_results"])?;
    for (name, batches) in results {
        match name.as_str() {
            "proposals" => {
                let normalized = normalize_batches(batches, proposals.proposals_schema())?;
                proposals.load_proposals(normalized);
            }
            "comments" => {
                let normalized = normalize_batches(batches, comments.comments_schema())?;
                comments.load_comments(normalized);
            }
            "ci_results" => {
                let normalized = normalize_batches(batches, ci_results.ci_schema())?;
                ci_results.load_results(normalized);
            }
            _ => {}
        }
    }

    Ok((proposals, comments, ci_results))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::crud::CreateItemInput;
    use crate::item_type::ItemType;

    #[test]
    fn test_save_and_load_roundtrip() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = dir.path();

        // Create store with items
        let mut store = KanbanStore::new();
        let id = store
            .create_item(&CreateItemInput {
                title: "Test Expedition".to_string(),
                item_type: ItemType::Expedition,
                priority: Some("high".to_string()),
                assignee: Some("M5".to_string()),
                tags: vec!["v14".to_string()],
                related: vec![],
                depends_on: vec![],
                body: None,
            })
            .expect("create item");

        store
            .update_status(&id, "in_progress", Some("M5"), false, None)
            .expect("update status");

        // Save
        save_store(root, &store).expect("save store");

        // Load
        let loaded = load_store(root).expect("load store");

        assert_eq!(loaded.active_item_count(), 1);
        let item = loaded.get_item(&id).expect("get item");
        assert_eq!(item.num_rows(), 1);
    }

    #[test]
    fn test_load_empty_dir() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let store = load_store(dir.path()).expect("load empty");
        assert_eq!(store.active_item_count(), 0);
    }

    #[test]
    fn test_data_dir_created() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let ddir = data_dir(dir.path()).expect("data dir");
        assert!(ddir.exists());
        assert!(ddir.ends_with(".nusy-kanban"));
    }

    #[test]
    fn test_save_all_and_load_all() {
        let dir = tempfile::tempdir().expect("create tempdir");
        let root = dir.path();

        let mut store = KanbanStore::new();
        store
            .create_item(&CreateItemInput {
                title: "Test".to_string(),
                item_type: ItemType::Expedition,
                priority: None,
                assignee: None,
                tags: vec![],
                related: vec![],
                depends_on: vec![],
                body: None,
            })
            .expect("create item");

        let relations = RelationsStore::new();
        save_all(root, &store, &relations).expect("save all");

        let (loaded_store, _loaded_rels) = load_all(root).expect("load all");
        assert_eq!(loaded_store.active_item_count(), 1);
    }
}

// ── Experiment Runs Persistence ─────────────────────────────────────────

/// Load experiment runs from Parquet.
pub fn load_experiment_runs(root: &Path) -> crate::experiment_runs::ExperimentRunStore {
    let dir = match data_dir(root) {
        Ok(d) => d,
        Err(_) => return crate::experiment_runs::ExperimentRunStore::new(),
    };

    match restore_named_batches(&dir, &["experiment_runs"]) {
        Ok(results) => {
            for (name, batches) in results {
                if name == "experiment_runs" {
                    return crate::experiment_runs::ExperimentRunStore::from_batches(batches);
                }
            }
            crate::experiment_runs::ExperimentRunStore::new()
        }
        Err(_) => crate::experiment_runs::ExperimentRunStore::new(),
    }
}

/// Save experiment runs to Parquet.
pub fn save_experiment_runs(
    root: &Path,
    store: &crate::experiment_runs::ExperimentRunStore,
) -> Result<()> {
    let dir = data_dir(root)?;
    let batches = store.batches();

    if batches.is_empty() {
        return Ok(());
    }

    let schema = crate::schema::experiment_runs_schema();
    save_named_batches(&[("experiment_runs", batches, &schema)], &dir)?;
    Ok(())
}