folderbase-core 0.7.2

Local-first Folderbase protocol and filesystem primitives for humans and 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
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
use std::collections::BTreeMap;
use std::fs;
use std::io::{Seek, SeekFrom, Write};

use folderbase_core::{
    FolderbaseError, FolderbaseKind, InitializationInventoryLimitKind, InitializationOptions,
    InitializationPlanDigest, TemplateAnswerValue, initialize_with_expected_plan_digest,
    load_builtin_template, plan_initialization, plan_template_initialization,
};

#[test]
fn unchanged_repeated_plans_have_the_same_digest_despite_volatile_identity() {
    let root = tempfile::tempdir().expect("ordinary folder");
    fs::write(root.path().join("notes.md"), "# Existing work\n").expect("existing file");

    let first =
        plan_initialization(root.path(), InitializationOptions::default()).expect("first plan");
    let second =
        plan_initialization(root.path(), InitializationOptions::default()).expect("second plan");

    assert_ne!(
        first.folderbase_id(),
        second.folderbase_id(),
        "the fixture must prove volatile initialization identity changed"
    );
    assert_eq!(first.plan_digest(), second.plan_digest());
    assert_eq!(first.plan_digest().algorithm(), "sha256");
    assert_eq!(first.plan_digest().digest().len(), 64);
}

#[test]
fn request_semantics_change_the_plan_digest() {
    let root = tempfile::tempdir().expect("ordinary folder");
    let baseline =
        plan_initialization(root.path(), InitializationOptions::default()).expect("baseline plan");

    let changed = plan_initialization(
        root.path(),
        InitializationOptions {
            name: Some("Different name".to_owned()),
            kind: FolderbaseKind::Organization,
            create_agent_adapters: false,
        },
    )
    .expect("changed plan");

    assert_ne!(baseline.plan_digest(), changed.plan_digest());
}

#[test]
fn visible_destination_membership_changes_but_preserved_file_contents_do_not_change_the_plan_digest()
 {
    let root = tempfile::tempdir().expect("ordinary folder");
    fs::write(root.path().join("notes.md"), "before\n").expect("existing file");
    let baseline =
        plan_initialization(root.path(), InitializationOptions::default()).expect("baseline plan");

    fs::write(root.path().join("late.md"), "arrived later\n").expect("late file");
    let with_late_file =
        plan_initialization(root.path(), InitializationOptions::default()).expect("late file plan");
    assert_ne!(baseline.plan_digest(), with_late_file.plan_digest());

    fs::write(root.path().join("notes.md"), "after\n").expect("changed file");
    let with_changed_target = plan_initialization(root.path(), InitializationOptions::default())
        .expect("changed target plan");
    assert_eq!(
        with_late_file.plan_digest(),
        with_changed_target.plan_digest(),
        "Core never writes a preserved ordinary file, so its content is not part of approval"
    );
}

#[test]
fn nested_folderbase_boundaries_change_the_plan_digest_without_hashing_their_contents() {
    let root = tempfile::tempdir().expect("ordinary folder");
    let nested = root.path().join("client");
    fs::create_dir(&nested).expect("nested directory");
    fs::write(nested.join("private.md"), "private context\n").expect("nested content");
    let before = plan_initialization(root.path(), InitializationOptions::default())
        .expect("before boundary");

    fs::write(nested.join("FOLDERBASE.md"), "# Client\n").expect("nested entry");
    fs::create_dir(nested.join(".folderbase")).expect("nested state");
    fs::write(nested.join(".folderbase/manifest.json"), "{}\n").expect("nested marker");
    let bounded =
        plan_initialization(root.path(), InitializationOptions::default()).expect("bounded plan");
    assert_ne!(before.plan_digest(), bounded.plan_digest());

    fs::write(nested.join("private.md"), "changed behind boundary\n").expect("nested edit");
    let changed_behind_boundary =
        plan_initialization(root.path(), InitializationOptions::default()).expect("bounded replan");
    assert_eq!(bounded.plan_digest(), changed_behind_boundary.plan_digest());
}

#[test]
fn initialization_distinguishes_inert_context_from_case_folded_marker_aliases() {
    let inert = tempfile::tempdir().expect("inert folder");
    fs::create_dir_all(inert.path().join("client/.folderbase/questions")).expect("inert context");
    fs::write(
        inert.path().join("client/.folderbase/summary.md"),
        "ordinary context\n",
    )
    .expect("inert summary");
    plan_initialization(inert.path(), InitializationOptions::default())
        .expect("markerless context remains inert");

    let alias = tempfile::tempdir().expect("alias folder");
    fs::create_dir_all(alias.path().join("client/.FOLDERBASE")).expect("alias state");
    fs::write(
        alias.path().join("client/.FOLDERBASE/MANIFEST.JSON"),
        "opaque\n",
    )
    .expect("alias manifest");
    assert!(matches!(
        plan_initialization(alias.path(), InitializationOptions::default()),
        Err(FolderbaseError::UnsafePath(_))
    ));
}

#[cfg(unix)]
#[test]
fn initialization_refuses_a_symlink_shaped_nested_marker_without_following_it() {
    let root = tempfile::tempdir().expect("ordinary folder");
    fs::create_dir(root.path().join("client")).expect("client");
    std::os::unix::fs::symlink(
        root.path().join("missing-state"),
        root.path().join("client/.folderbase"),
    )
    .expect("state symlink");

    assert!(matches!(
        plan_initialization(root.path(), InitializationOptions::default()),
        Err(FolderbaseError::UnsafePath(_))
    ));
}

#[test]
fn every_canonical_reconstructable_tree_is_collapsed_for_initialization_approval() {
    for name in [
        "node_modules",
        ".next",
        ".nuxt",
        ".sites",
        ".svelte-kit",
        ".wrangler",
        "dist",
        "build",
        "coverage",
        ".build",
        ".swiftpm",
        ".venv",
        "__pycache__",
        ".dart_tool",
        "Pods",
        "DerivedData",
        "target",
    ] {
        let root = tempfile::tempdir().expect("ordinary folder");
        let tree = root.path().join(name);
        fs::create_dir(&tree).expect("reconstructable directory");
        fs::write(tree.join("cache"), "one\n").expect("cache");
        let before =
            plan_initialization(root.path(), InitializationOptions::default()).expect("before");

        fs::write(tree.join("cache"), "two\n").expect("cache update");
        fs::write(tree.join("late"), "late\n").expect("late cache");
        let after =
            plan_initialization(root.path(), InitializationOptions::default()).expect("after");

        assert_eq!(
            before.plan_digest(),
            after.plan_digest(),
            "{name} must use the same reconstructable policy as inspection and workspace listing"
        );
    }
}

#[test]
fn a_large_sparse_preserved_file_is_metadata_only_and_can_change_before_apply() {
    const MOVIE_BYTES: u64 = 10 * 1024 * 1024 * 1024;
    const SAMPLE_OFFSET: u64 = 5 * 1024 * 1024 * 1024;
    let root = tempfile::tempdir().expect("ordinary folder");
    let movie_path = root.path().join("feature-film.mov");
    let mut movie = fs::File::create(&movie_path).expect("sparse movie");
    movie.set_len(MOVIE_BYTES).expect("sparse length");
    movie.seek(SeekFrom::Start(SAMPLE_OFFSET)).expect("seek");
    movie.write_all(b"before").expect("sample bytes");
    movie.sync_all().expect("movie synced");

    let plan =
        plan_initialization(root.path(), InitializationOptions::default()).expect("approved plan");
    let approved = plan.plan_digest().clone();

    movie.seek(SeekFrom::Start(SAMPLE_OFFSET)).expect("seek");
    movie.write_all(b"after!").expect("same-length user edit");
    movie.sync_all().expect("movie synced");

    let result = initialize_with_expected_plan_digest(&plan, &approved)
        .expect("preserved movie contents are outside the write set");
    assert_eq!(result.applied_plan_digest, approved);
    assert_eq!(
        fs::metadata(movie_path).expect("preserved movie").len(),
        MOVIE_BYTES
    );
}

#[test]
fn traversal_depth_is_bounded_with_a_typed_no_write_refusal() {
    let root = tempfile::tempdir().expect("ordinary folder");
    let mut deepest = root.path().to_path_buf();
    for index in 0..66 {
        deepest.push(format!("d{index}"));
        fs::create_dir(&deepest).expect("nested directory");
    }

    let error = plan_initialization(root.path(), InitializationOptions::default())
        .expect_err("over-deep inventory must be refused");
    assert!(matches!(
        error,
        FolderbaseError::InitializationInventoryLimitExceeded {
            limit: InitializationInventoryLimitKind::Depth,
            ..
        }
    ));
    assert_no_protocol_writes(root.path());
}

#[test]
fn a_single_huge_directory_is_bounded_before_unlimited_name_retention() {
    let root = tempfile::tempdir().expect("ordinary folder");
    for index in 0..=50_000 {
        fs::File::create(root.path().join(format!("entry-{index:05}")))
            .expect("bounded directory entry");
    }

    let error = plan_initialization(root.path(), InitializationOptions::default())
        .expect_err("over-wide inventory must be refused");
    assert!(matches!(
        error,
        FolderbaseError::InitializationInventoryLimitExceeded {
            limit: InitializationInventoryLimitKind::Entries,
            maximum: 50_000,
        }
    ));
    assert_no_protocol_writes(root.path());
}

#[cfg(unix)]
#[test]
fn a_preserved_file_replaced_by_an_outside_symlink_is_refused_without_following_it() {
    use std::os::unix::fs::symlink;

    let root = tempfile::tempdir().expect("ordinary folder");
    let outside = tempfile::tempdir().expect("outside folder");
    let outside_file = outside.path().join("private.md");
    fs::write(&outside_file, "outside bytes\n").expect("outside file");
    let preserved = root.path().join("notes.md");
    fs::write(&preserved, "inside bytes\n").expect("inside file");

    let plan =
        plan_initialization(root.path(), InitializationOptions::default()).expect("approved plan");
    let approved = plan.plan_digest().clone();
    fs::remove_file(&preserved).expect("replace preserved file");
    symlink(&outside_file, &preserved).expect("outside symlink");

    let error = initialize_with_expected_plan_digest(&plan, &approved)
        .expect_err("file-to-symlink swap must be refused");
    assert!(matches!(
        error,
        FolderbaseError::InitializationDestinationChanged(_)
    ));
    assert_no_protocol_writes(root.path());
    assert_eq!(
        fs::read_to_string(outside_file).expect("outside bytes preserved"),
        "outside bytes\n"
    );
}

#[test]
fn template_identity_and_answers_change_the_plan_digest() {
    let root = tempfile::tempdir().expect("ordinary folder");
    let package = load_builtin_template("folderbase.project", "0.2.2").expect("project template");
    let baseline_answers = project_answers("Ship the approval binding.");
    let baseline = plan_template_initialization(
        root.path(),
        InitializationOptions::default(),
        &package,
        &baseline_answers,
    )
    .expect("baseline template plan");

    let changed_answers = project_answers("Ship a different decision.");
    let changed = plan_template_initialization(
        root.path(),
        InitializationOptions::default(),
        &package,
        &changed_answers,
    )
    .expect("changed template plan");

    assert_ne!(baseline.plan_digest(), changed.plan_digest());

    let prior_package =
        load_builtin_template("folderbase.project", "0.2.1").expect("prior project template");
    let prior_package_plan = plan_template_initialization(
        root.path(),
        InitializationOptions::default(),
        &prior_package,
        &baseline_answers,
    )
    .expect("prior package plan");
    assert_ne!(baseline.plan_digest(), prior_package_plan.plan_digest());
}

#[test]
fn exact_approved_digest_applies_the_single_core_plan_and_returns_the_digest() {
    let root = tempfile::tempdir().expect("ordinary folder");
    fs::write(root.path().join("notes.md"), "preserve me\n").expect("existing file");
    let plan =
        plan_initialization(root.path(), InitializationOptions::default()).expect("approved plan");
    let approved = plan.plan_digest().clone();

    let result = initialize_with_expected_plan_digest(&plan, &approved).expect("approved apply");

    assert_eq!(result.applied_plan_digest, approved);
    assert!(root.path().join(".folderbase/manifest.json").is_file());
    assert_eq!(
        fs::read_to_string(root.path().join("notes.md")).expect("preserved file"),
        "preserve me\n"
    );
}

#[test]
fn stale_or_wrong_approved_digest_refuses_before_any_write() {
    let stale_root = tempfile::tempdir().expect("stale folder");
    let stale_plan = plan_initialization(stale_root.path(), InitializationOptions::default())
        .expect("approved plan");
    let approved = stale_plan.plan_digest().clone();
    fs::write(stale_root.path().join("late.md"), "late\n").expect("late file");

    let error = initialize_with_expected_plan_digest(&stale_plan, &approved)
        .expect_err("destination changed after approval");
    assert!(matches!(
        error,
        FolderbaseError::InitializationDestinationChanged(_)
    ));
    assert_no_protocol_writes(stale_root.path());

    let wrong_root = tempfile::tempdir().expect("wrong digest folder");
    let wrong_plan = plan_initialization(wrong_root.path(), InitializationOptions::default())
        .expect("current plan");
    let wrong = InitializationPlanDigest::parse_sha256("0".repeat(64)).expect("valid wrong digest");
    let error = initialize_with_expected_plan_digest(&wrong_plan, &wrong)
        .expect_err("wrong digest must not apply");
    assert!(matches!(
        error,
        FolderbaseError::InitializationPlanChanged { .. }
    ));
    assert_no_protocol_writes(wrong_root.path());
}

#[test]
fn nested_boundary_and_template_request_changes_refuse_approved_apply() {
    let boundary_root = tempfile::tempdir().expect("boundary folder");
    let nested = boundary_root.path().join("client");
    fs::create_dir(&nested).expect("nested directory");
    let boundary_plan = plan_initialization(boundary_root.path(), InitializationOptions::default())
        .expect("approved boundary plan");
    let approved = boundary_plan.plan_digest().clone();
    fs::write(nested.join("FOLDERBASE.md"), "# Client\n").expect("nested entry");
    fs::create_dir(nested.join(".folderbase")).expect("nested state");
    fs::write(nested.join(".folderbase/manifest.json"), "{}\n").expect("nested marker");
    assert!(matches!(
        initialize_with_expected_plan_digest(&boundary_plan, &approved),
        Err(FolderbaseError::InitializationDestinationChanged(_))
    ));
    assert_no_protocol_writes(boundary_root.path());

    let template_root = tempfile::tempdir().expect("template folder");
    let package = load_builtin_template("folderbase.project", "0.2.2").expect("project template");
    let reviewed = plan_template_initialization(
        template_root.path(),
        InitializationOptions::default(),
        &package,
        &project_answers("Reviewed action."),
    )
    .expect("reviewed template plan");
    let changed_request = plan_template_initialization(
        template_root.path(),
        InitializationOptions::default(),
        &package,
        &project_answers("Unreviewed action."),
    )
    .expect("changed template plan");
    assert!(matches!(
        initialize_with_expected_plan_digest(&changed_request, reviewed.plan_digest()),
        Err(FolderbaseError::InitializationPlanChanged { .. })
    ));
    assert_no_protocol_writes(template_root.path());
}

#[test]
fn malformed_digest_is_typed_and_cannot_write() {
    let root = tempfile::tempdir().expect("ordinary folder");

    let error = InitializationPlanDigest::parse_sha256("NOT-A-SHA256")
        .expect_err("malformed digest must be refused");

    assert!(matches!(
        error,
        FolderbaseError::InvalidInitializationPlanDigest
    ));
    assert_no_protocol_writes(root.path());

    let plan =
        plan_initialization(root.path(), InitializationOptions::default()).expect("current plan");
    let deserialized: InitializationPlanDigest = serde_json::from_value(serde_json::json!({
        "algorithm": "md5",
        "digest": "0".repeat(64)
    }))
    .expect("public digest JSON shape");
    let error = initialize_with_expected_plan_digest(&plan, &deserialized)
        .expect_err("deserialized digest must be validated by Core");
    assert!(matches!(
        error,
        FolderbaseError::InvalidInitializationPlanDigest
    ));
    assert_no_protocol_writes(root.path());
}

fn project_answers(next_action: &str) -> BTreeMap<String, TemplateAnswerValue> {
    BTreeMap::from([
        (
            "purpose".to_owned(),
            TemplateAnswerValue::Text("Prove approved initialization plans.".to_owned()),
        ),
        (
            "current_state".to_owned(),
            TemplateAnswerValue::Text("The Core owns the decision.".to_owned()),
        ),
        (
            "next_action".to_owned(),
            TemplateAnswerValue::Text(next_action.to_owned()),
        ),
    ])
}

fn assert_no_protocol_writes(root: &std::path::Path) {
    for path in [
        ".folderbase",
        "FOLDERBASE.md",
        ".folderbaseignore",
        "AGENTS.md",
        "CLAUDE.md",
    ] {
        assert!(
            !root.join(path).exists(),
            "{path} must not exist after refusal"
        );
    }
}