nb-api 0.4.2

Typed Rust interface to the nb note-taking CLI (subprocess wrapper: escaping, validation, output parsing)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
use std::collections::HashSet;
use std::fs::OpenOptions;
use std::path::{Path, PathBuf};

use crate::error::NbError;
use crate::gate;
use crate::git;
use crate::types::{CommitOutcome, OpOutcome};

use super::apply::{OpMeta, PlanEffects, validate_and_apply_virtual};
use super::index::{
    PendingIndexEdit, acquire_index_lock, apply_index_edits, index_id_on_disk,
    index_rel_for_folder, is_index_rel, numeric_selector,
    reap_stale_index_lock, split_folder_basename,
};
use super::plan::annotate_plan_index;
use super::virtual_tree::{
    VirtualNode, VirtualTree, normalize_rel, parent_path,
    refuse_symlink_ancestors,
};

use super::plan::Transaction;

impl Transaction {
    /// Validate-all, apply-all, at most one Git checkpoint.
    pub async fn commit(self) -> Result<CommitOutcome, NbError> {
        // Resolve root without invoking `nb` when possible (avoids nb
        // auto-checkpoint clearing dirty state before the baseline check).
        let notebook_root = self
            .client
            .show_notebook_path_unguarded(Some(&self.notebook))
            .await?;
        let gate_key = gate::git_common_dir_realpath(&notebook_root)?;
        let _hold =
            gate::acquire_notebook(gate_key, self.gate_timeout, false).await?;

        // A crashed writer may leave its `.nb-api-index.lock` behind; an
        // untracked lock file reads as dirty, so reap a stale one first.
        reap_stale_index_lock(&notebook_root, self.gate_timeout);

        if git::notebook_is_dirty(&notebook_root)? {
            return Err(NbError::DirtyBaseline {
                guidance: "commit or clean the notebook worktree/index before Transaction::commit"
                    .into(),
            });
        }

        let pre_revision = git::notebook_head(&notebook_root)?;
        if self.plan.is_empty() {
            return Ok(CommitOutcome {
                commit_created: false,
                revision_id: None,
                pre_revision,
                ops: Vec::new(),
            });
        }

        let ignored_existing: HashSet<String> =
            git::list_ignored_paths(&notebook_root)?
                .into_iter()
                .map(|p| normalize_rel(&p))
                .collect();
        let mut tree = VirtualTree::from_disk(&notebook_root)?;
        let mut op_meta: Vec<OpMeta> = Vec::with_capacity(self.plan.len());
        let mut effects = PlanEffects::default();
        // Disk keys at snapshot time: materialize refuses a planned create
        // that appears on disk afterwards (external same-path race).
        let snapshot_keys: HashSet<String> =
            tree.nodes.keys().cloned().collect();

        for (index, op) in self.plan.iter().enumerate() {
            match validate_and_apply_virtual(
                &self.notebook,
                &notebook_root,
                &mut tree,
                &ignored_existing,
                op,
                index,
                &mut effects,
            ) {
                Ok(meta) => op_meta.push(meta),
                Err(err) => {
                    return Err(annotate_plan_index(err, index as u32));
                },
            }
        }

        #[cfg(feature = "testing")]
        if std::env::var_os("NB_API_SIMULATE_EXTERNAL_WRITER").is_some() {
            // Deterministic stand-in for an external `nb add` racing our
            // snapshot: create AND commit a note this transaction knows
            // nothing about. Our commit must preserve it (file + index line).
            simulate_external_writer(&notebook_root)?;
        }

        // Disk baseline before materialize (for rollback of new owned outputs).
        // Virtual tree already includes planned ops; disk is still pre-apply.
        let baseline_on_disk: HashSet<String> = {
            let mut s: HashSet<String> =
                git::list_notebook_paths(&notebook_root)?
                    .into_iter()
                    .map(|p| normalize_rel(&p))
                    .collect();
            s.extend(ignored_existing.iter().cloned());
            s
        };

        // Force-stage every final file path so ignore rules cannot drop
        // transaction-owned outputs (new ignored names, `.gitkeep`, etc.).
        // `.index` files are applied post-materialize (fresh read under the
        // index lock) but staged by the same checkpoint, so list them too.
        let mut force_paths: Vec<String> = tree
            .nodes
            .iter()
            .filter_map(|(p, n)| match n {
                VirtualNode::File(_) => Some(p.clone()),
                VirtualNode::Folder => None,
            })
            .collect();
        for meta in &op_meta {
            for edit in &meta.index_edits {
                if !force_paths.iter().any(|p| p == &edit.file_rel) {
                    force_paths.push(edit.file_rel.clone());
                }
            }
        }

        // Directories that already exist before materialize must never be
        // pruned during rollback (including empty ignored parents).
        let baseline_dirs = existing_dirs_under(&notebook_root);

        // Materialize non-index files, apply `.index` edits from fresh
        // on-disk content under `.nb-api-index.lock` (released before git
        // staging — never held across `git add`/`commit`), then checkpoint.
        // The lock serializes nb-api writers only; external `nb` writers are
        // covered by O_APPEND plus post-write selector derivation.
        let apply_result = async {
            materialize_tree(&notebook_root, &tree, &effects, &snapshot_keys)?;
            let mut pending: Vec<(u32, PendingIndexEdit)> = Vec::new();
            for (i, meta) in op_meta.iter().enumerate() {
                for edit in &meta.index_edits {
                    pending.push((i as u32, edit.clone()));
                }
            }
            let placements = if pending.is_empty() {
                Vec::new()
            } else {
                let _lock =
                    acquire_index_lock(&notebook_root, self.gate_timeout)
                        .await?;
                let placements = apply_index_edits(
                    &notebook_root,
                    &pending,
                    self.gate_timeout,
                )?;
                drop(_lock);
                placements
            };
            let created = git::notebook_commit_all(
                &notebook_root,
                &format!("nb-api transaction ({} ops)", self.plan.len()),
                &force_paths,
            )?;
            Ok::<_, NbError>((created, placements))
        }
        .await;

        match apply_result {
            Ok((created, placements)) => {
                let revision_id = if created {
                    match git::notebook_head(&notebook_root) {
                        Ok(head) => Some(head),
                        Err(_) => {
                            return Err(NbError::IndeterminateCommit {
                                pre_revision,
                                post_revision_observed: None,
                                guidance: "git commit may have succeeded but HEAD could not be re-read; do not retry blindly".into(),
                            });
                        },
                    }
                } else {
                    None
                };
                let ops = op_meta
                    .into_iter()
                    .enumerate()
                    .map(|(i, meta)| {
                        // Creates/moves/deletes carry post-write placements:
                        // selector becomes the numeric `<folder>/<id>` form.
                        // A cross-folder move records both a blank (source)
                        // and an append (destination); the destination
                        // placement is last, so take the last match.
                        // All other ops keep the echoed target selector and
                        // gain a numeric id when the path is indexed.
                        let (selector, numeric_id) = match placements
                            .iter()
                            .rev()
                            .find(|p| p.op_index == i as u32)
                        {
                            Some(p) => (
                                Some(numeric_selector(
                                    &self.notebook,
                                    &p.folder,
                                    p.id,
                                )),
                                Some(p.id),
                            ),
                            None => (
                                meta.selector,
                                meta.path.as_deref().and_then(|p| {
                                    index_id_on_disk(&notebook_root, p)
                                }),
                            ),
                        };
                        OpOutcome {
                            index: i as u32,
                            path: meta.path,
                            selector,
                            numeric_id,
                            noop: meta.noop,
                            fingerprint: meta.fingerprint,
                        }
                    })
                    .collect();
                Ok(CommitOutcome {
                    commit_created: created,
                    revision_id,
                    pre_revision,
                    ops,
                })
            },
            Err(err) => {
                let post = git::notebook_head(&notebook_root).ok();
                if post.as_deref().is_some_and(|h| h != pre_revision.as_str())
                {
                    return Err(NbError::IndeterminateCommit {
                        pre_revision,
                        post_revision_observed: post,
                        guidance: "checkpoint may have completed; re-read HEAD/status and do not retry the same plan blindly".into(),
                    });
                }
                // Owned outputs that did not exist at baseline must be removed
                // even when ignored (git clean -fd keeps ignored files).
                // Baseline directories are never owned outputs: pruning one
                // would `remove_dir_all` a pre-existing folder.
                let owned_new: Vec<String> = force_paths
                    .into_iter()
                    .filter(|p| !baseline_on_disk.contains(p))
                    .filter(|p| !baseline_dirs.contains(p))
                    .collect();
                match try_restore(
                    &notebook_root,
                    &pre_revision,
                    &owned_new,
                    &baseline_dirs,
                ) {
                    Ok(()) => Err(err),
                    Err(recovery) => Err(recovery),
                }
            },
        }
    }
}

/// All directory paths under the notebook root before materialize (relative,
/// `/`-separated). Used so rollback never deletes pre-existing dirs.
fn existing_dirs_under(notebook_root: &Path) -> HashSet<String> {
    let mut dirs = HashSet::new();
    fn walk(base: &Path, rel: &Path, out: &mut HashSet<String>) {
        let entries = match std::fs::read_dir(base) {
            Ok(e) => e,
            Err(_) => return,
        };
        for entry in entries.flatten() {
            let name = entry.file_name();
            if name == ".git" {
                continue;
            }
            let Ok(ft) = entry.file_type() else {
                continue;
            };
            // Do not follow symlinks.
            if ft.is_symlink() || !ft.is_dir() {
                continue;
            }
            let child_rel = if rel.as_os_str().is_empty() {
                PathBuf::from(&name)
            } else {
                rel.join(&name)
            };
            let key = child_rel.to_string_lossy().replace('\\', "/");
            out.insert(key);
            walk(&entry.path(), &child_rel, out);
        }
    }
    walk(notebook_root, Path::new(""), &mut dirs);
    dirs
}

fn try_restore(
    notebook_root: &Path,
    pre_revision: &str,
    remove_new_owned: &[String],
    baseline_dirs: &HashSet<String>,
) -> Result<(), NbError> {
    if let Err(e) = git::notebook_reset_clean(notebook_root, pre_revision) {
        let post = git::notebook_head(notebook_root).ok();
        let status = git::notebook_status_porcelain(notebook_root).ok();
        return Err(NbError::RecoveryRequired {
            pre_revision: pre_revision.to_string(),
            post_revision_observed: post,
            status_observed: status,
            preserved_paths: Some(remove_new_owned.to_vec()),
            guidance: format!(
                "cleanup after failed commit could not be verified; inspect HEAD/status before retry. underlying: {e}"
            ),
        });
    }
    // `git clean -fd` can remove empty untracked dirs (including ignored
    // parents) once staged children are cleared by reset --hard. Restore any
    // baseline directory that disappeared.
    for d in baseline_dirs {
        let abs = notebook_root.join(d);
        if !abs.exists()
            && let Err(e) = std::fs::create_dir_all(&abs)
        {
            return Err(NbError::RecoveryRequired {
                pre_revision: pre_revision.to_string(),
                post_revision_observed: git::notebook_head(notebook_root).ok(),
                status_observed: git::notebook_status_porcelain(notebook_root)
                    .ok(),
                preserved_paths: Some(vec![d.clone()]),
                guidance: format!(
                    "failed to restore baseline directory `{d}` after cleanup; do not retry blindly. underlying: {e}"
                ),
            });
        }
    }
    // Explicitly delete transaction-owned outputs that reset/clean leave behind
    // when they are Git-ignored untracked files.
    let mut remaining = Vec::new();
    for rel in remove_new_owned {
        let abs = notebook_root.join(rel);
        match std::fs::symlink_metadata(&abs) {
            Ok(meta) if meta.file_type().is_symlink() || meta.is_file() => {
                if let Err(e) = std::fs::remove_file(&abs) {
                    remaining.push(format!("{rel} ({e})"));
                }
            },
            Ok(meta) if meta.is_dir() => {
                if let Err(e) = std::fs::remove_dir_all(&abs) {
                    remaining.push(format!("{rel} ({e})"));
                }
            },
            Ok(_) => {},
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {},
            Err(e) => remaining.push(format!("{rel} ({e})")),
        }
        // Prune only empty parents that the transaction created — never a
        // directory that existed at baseline (e.g. pre-existing ignored parent).
        if let Some(parent) = parent_path(rel) {
            let mut cur = parent;
            loop {
                if baseline_dirs.contains(&cur) {
                    break;
                }
                let p = notebook_root.join(&cur);
                match std::fs::remove_dir(&p) {
                    Ok(()) => {},
                    Err(_) => break,
                }
                match parent_path(&cur) {
                    Some(next) => cur = next,
                    None => break,
                }
            }
        }
    }
    if !remaining.is_empty() {
        return Err(NbError::RecoveryRequired {
            pre_revision: pre_revision.to_string(),
            post_revision_observed: git::notebook_head(notebook_root).ok(),
            status_observed: git::notebook_status_porcelain(notebook_root).ok(),
            preserved_paths: Some(remaining),
            guidance: "failed to remove transaction-owned outputs after aborted commit; do not retry blindly".into(),
        });
    }
    // Verify none of the new owned paths remain (including ignored).
    let mut still_present = Vec::new();
    for rel in remove_new_owned {
        if notebook_root.join(rel).exists() {
            still_present.push(rel.clone());
        }
    }
    let head = match restore_verify_head(notebook_root) {
        Ok(h) => h,
        Err(e) => {
            return Err(NbError::RecoveryRequired {
                pre_revision: pre_revision.to_string(),
                post_revision_observed: None,
                status_observed: git::notebook_status_porcelain(notebook_root)
                    .ok(),
                preserved_paths: if still_present.is_empty() {
                    None
                } else {
                    Some(still_present)
                },
                guidance: format!(
                    "cleanup ran but HEAD re-read failed during verification; do not retry blindly. underlying: {e}"
                ),
            });
        },
    };
    let dirty = match restore_verify_dirty(notebook_root) {
        Ok(d) => d,
        Err(e) => {
            return Err(NbError::RecoveryRequired {
                pre_revision: pre_revision.to_string(),
                post_revision_observed: Some(head),
                status_observed: git::notebook_status_porcelain(notebook_root)
                    .ok(),
                preserved_paths: if still_present.is_empty() {
                    None
                } else {
                    Some(still_present)
                },
                guidance: format!(
                    "cleanup ran but dirty-status check failed during verification; do not retry blindly. underlying: {e}"
                ),
            });
        },
    };
    if head != pre_revision || dirty || !still_present.is_empty() {
        return Err(NbError::RecoveryRequired {
            pre_revision: pre_revision.to_string(),
            post_revision_observed: Some(head),
            status_observed: git::notebook_status_porcelain(notebook_root).ok(),
            preserved_paths: if still_present.is_empty() {
                None
            } else {
                Some(still_present)
            },
            guidance:
                "cleanup ran but HEAD/status/owned-path verification failed; do not retry blindly"
                    .into(),
        });
    }
    Ok(())
}

/// HEAD observation used only in post-cleanup verification.
fn restore_verify_head(notebook_root: &Path) -> Result<String, NbError> {
    #[cfg(feature = "testing")]
    if std::env::var_os("NB_API_FAIL_RESTORE_HEAD").is_some() {
        return Err(NbError::CommandFailed {
            command: "nb-api://fail-restore-head".into(),
            stderr: "injected HEAD verify failure for rollback tests".into(),
            exit_code: Some(1),
        });
    }
    git::notebook_head(notebook_root)
}

/// Dirty-status observation used only in post-cleanup verification.
fn restore_verify_dirty(notebook_root: &Path) -> Result<bool, NbError> {
    #[cfg(feature = "testing")]
    if std::env::var_os("NB_API_FAIL_RESTORE_DIRTY").is_some() {
        return Err(NbError::CommandFailed {
            command: "nb-api://fail-restore-dirty".into(),
            stderr: "injected dirty-status verify failure for rollback tests"
                .into(),
            exit_code: Some(1),
        });
    }
    git::notebook_is_dirty(notebook_root)
}

/// Deterministic stand-in for an external `nb add` racing our snapshot
/// (testing only, behind `NB_API_SIMULATE_EXTERNAL_WRITER`): create AND
/// commit a note plus its `.index` line that this transaction knows nothing
/// about. The commit must preserve both.
///
/// `NB_API_SIMULATE_EXTERNAL_SAME_PATH=<rel>` makes the external writer take
/// the given path instead (to test the same-path race: our commit must fail
/// `PathCollision`, never overwrite).
#[cfg(feature = "testing")]
fn simulate_external_writer(notebook_root: &Path) -> Result<(), NbError> {
    use std::io::Write as _;
    let (folder, name) =
        match std::env::var("NB_API_SIMULATE_EXTERNAL_SAME_PATH") {
            Ok(rel) => {
                let (folder, basename) =
                    split_folder_basename(&normalize_rel(&rel));
                (folder, basename)
            },
            Err(_) => {
                (String::new(), format!("external-{}.md", std::process::id()))
            },
        };
    let rel = if folder.is_empty() {
        name.clone()
    } else {
        format!("{folder}/{name}")
    };
    if !folder.is_empty() {
        std::fs::create_dir_all(notebook_root.join(&folder)).map_err(|e| {
            NbError::Io {
                path: notebook_root.join(&folder),
                source: e.into(),
            }
        })?;
    }
    std::fs::write(notebook_root.join(&rel), b"# External\n\nexternal body\n")
        .map_err(|e| NbError::Io {
            path: notebook_root.join(&rel),
            source: e.into(),
        })?;
    let index_abs = notebook_root.join(index_rel_for_folder(&folder));
    let mut f = OpenOptions::new()
        .create(true)
        .append(true)
        .open(&index_abs)
        .map_err(|e| NbError::Io {
            path: index_abs.clone(),
            source: e.into(),
        })?;
    writeln!(f, "{name}").map_err(|e| NbError::Io {
        path: index_abs.clone(),
        source: e.into(),
    })?;
    drop(f);
    git::notebook_commit_all(
        notebook_root,
        "external writer",
        &[rel, index_rel_for_folder(&folder)],
    )?;
    Ok(())
}

/// Write the plan's effects to disk.
///
/// ONLY paths recorded in `effects` are touched: files created after the
/// snapshot by an external writer are never deleted, and files deleted
/// externally are never resurrected. `.index` rels are skipped here (applied
/// separately from fresh on-disk content under `.nb-api-index.lock`).
///
/// `snapshot_keys` are the tree keys at snapshot time. A `created` path that
/// exists on disk now but was absent from the snapshot means an external
/// writer created the same path after our collision validation — refuse with
/// `PathCollision` instead of overwriting it (which would also leave
/// ambiguous duplicate `.index` entries).
fn materialize_tree(
    root: &Path,
    tree: &VirtualTree,
    effects: &PlanEffects,
    snapshot_keys: &HashSet<String>,
) -> Result<(), NbError> {
    // Preflight: no path may traverse an existing symlink ancestor (including
    // ignored directory symlinks excluded from the virtual snapshot).
    for rel in tree.nodes.keys() {
        refuse_symlink_ancestors(root, rel)?;
    }

    // Remove ONLY plan delete/move sources that exist on disk as files.
    let mut removed: Vec<&String> = effects.removed.iter().collect();
    removed.sort();
    removed.dedup();
    for rel in removed {
        let key = normalize_rel(rel);
        let abs = root.join(&key);
        let meta = match std::fs::symlink_metadata(&abs) {
            Ok(m) => m,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
            Err(e) => {
                return Err(NbError::Io {
                    path: abs,
                    source: e.into(),
                });
            },
        };
        if meta.file_type().is_symlink() {
            return Err(NbError::UnsupportedStructure {
                reason: format!(
                    "refusing to materialize through symlink `{key}`"
                ),
            });
        }
        if meta.is_file()
            && !matches!(tree.nodes.get(&key), Some(VirtualNode::File(_)))
        {
            std::fs::remove_file(&abs).map_err(|e| NbError::Io {
                path: abs,
                source: e.into(),
            })?;
        }
    }
    // Create ONLY plan folders.
    let mut mkdirs: Vec<&String> = effects.mkdirs.iter().collect();
    mkdirs.sort();
    mkdirs.dedup();
    for rel in mkdirs {
        let key = normalize_rel(rel);
        let abs = root.join(&key);
        refuse_symlink_ancestors(root, &key)?;
        std::fs::create_dir_all(&abs).map_err(|e| NbError::Io {
            path: abs.clone(),
            source: e.into(),
        })?;
    }
    // Write ONLY plan-created/modified files (never `.index`).
    let mut written: Vec<&String> = effects
        .written
        .iter()
        .filter(|rel| !is_index_rel(rel))
        .collect();
    written.sort();
    written.dedup();
    let created: HashSet<&String> = effects.created.iter().collect();
    for rel in written {
        let key = normalize_rel(rel);
        let created_contains = created.contains(rel);
        let Some(VirtualNode::File(bytes)) = tree.nodes.get(&key) else {
            return Err(NbError::ValidationError {
                reason: format!("plan effect `{key}` has no file content"),
                location: None,
            });
        };
        let abs = root.join(&key);
        // Never write through an existing symlink leaf or ancestor.
        refuse_symlink_ancestors(root, &key)?;
        if let Ok(meta) = std::fs::symlink_metadata(&abs) {
            if meta.file_type().is_symlink() {
                return Err(NbError::UnsupportedStructure {
                    reason: format!(
                        "refusing to write through symlink `{key}`"
                    ),
                });
            }
            // A planned create racing an external same-path create (file or
            // directory appearing after the snapshot): refuse instead of
            // overwriting the external note or failing obscurely on a dir.
            if created_contains && !snapshot_keys.contains(&key) {
                return Err(NbError::PathCollision {
                    path: key,
                    plan_index: None,
                });
            }
        }
        if let Some(parent) = abs.parent() {
            std::fs::create_dir_all(parent).map_err(|e| NbError::Io {
                path: parent.to_path_buf(),
                source: e.into(),
            })?;
        }
        std::fs::write(&abs, bytes).map_err(|e| NbError::Io {
            path: abs,
            source: e.into(),
        })?;
    }
    Ok(())
}