raf-source 0.0.0

Workspace source index: a Tree file seam with disk and memory backends under canonical path containment.
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
// SPDX-FileCopyrightText: 2026 Blackcat InformaticsĀ® Inc. <paudley@blackcatinformatics.ca>
// SPDX-License-Identifier: MIT OR Apache-2.0 OR MulanPSL-2.0

//! Actual delivery ownership, native spill admission and visibility merging.

use super::*;
use crate::{Budget, LoadErrorKind, LoadOptions, MemoryTree, RelPath, Workspace};

fn retained_rows_tree() -> MemoryTree {
    let source = "pub mod a { pub struct X; pub struct Y; }
        pub mod b { pub struct X; pub struct Y; }
        pub mod hidden { pub struct X; pub struct Y; }
        pub mod hub { pub use crate::a::*; pub use crate::b::*;
            use crate::hidden::*; pub use crate::relay::*; }
        pub mod relay { pub use crate::hub::*; }";
    [("Cargo.toml", shapes::MANIFEST), ("src/lib.rs", source)]
        .into_iter()
        .collect()
}

#[test]
fn recorded_name_rows_survive_real_reconstruction_with_new_equal_interned_names() {
    let ws = Workspace::load(&retained_rows_tree(), &["."]).expect("returning names");
    let options = LoadOptions::default();
    let meter = Meter::new(&options);
    let decl = &ws.index().uses[ws.index().glob_imports[0]];
    meter
        .globs(ws.index().globs.charged, &decl.file, Some(decl.line))
        .expect("resident cloned tables admitted");
    let mut index = ws.index().clone();
    index.lookups.built(false);
    let retained = index.globs.drops.clone();
    assert!(!retained.is_empty());
    index
        .replay_globs_for_measurement(&meter)
        .expect("actual clearing and from-definitions reconstruction");
    assert_eq!(
        index.globs.drops, retained,
        "the authoritative rows survive unchanged"
    );
    let mut names_checked = 0;
    for (&(glob, target), names) in &retained {
        let read = index
            .reads_of(index.uses[glob].module)
            .iter()
            .find(|read| read.glob == glob && read.target == target)
            .expect("actual reconstructed read");
        for (name, spaces) in names {
            let current = index
                .globs
                .strings
                .get(name.as_str())
                .expect("new canonical name");
            assert_eq!(current, name);
            assert_ne!(
                current.as_ptr(),
                name.as_ptr(),
                "clearing replaced the interner allocation"
            );
            let independent = SharedStr::from(name.as_str());
            assert_ne!(independent.as_ptr(), name.as_ptr());
            assert_ne!(independent.as_ptr(), current.as_ptr());
            for (at, held) in spaces.iter().enumerate() {
                assert_eq!(index.replacements(read, independent.as_str(), at), held);
                assert!(
                    held.windows(2).all(|pair| pair[0].0 < pair[1].0),
                    "identities remain sorted"
                );
                if at < 2 {
                    assert_eq!(
                        held.len(),
                        2,
                        "two independent identities in Type and Value"
                    );
                    assert!(held.iter().all(|(_, level)| *level == Level::PUBLIC));
                } else {
                    assert!(held.is_empty(), "no Macro selection invented");
                }
            }
            names_checked += 1;
        }
    }
    assert_eq!(
        names_checked, 2,
        "both retained identifier rows were queried"
    );
}

#[test]
fn recorded_name_hash_storage_loads_at_exact_item_and_glob_grants_and_refuses_one_less() {
    let tree = retained_rows_tree();
    let ws = Workspace::load(&tree, &["."]).expect("measure actual original grants");
    let options = LoadOptions {
        max_item_bytes: ws.usage().item_bytes,
        max_glob_bytes: ws.usage().glob_bytes,
        ..LoadOptions::default()
    };
    Workspace::load_with(&tree, &["."], &options).expect("exact overlapping actual grants");
    for (short, budget, limit) in [
        (
            LoadOptions {
                max_item_bytes: options.max_item_bytes - 1,
                ..options.clone()
            },
            Budget::ItemBytes,
            options.max_item_bytes - 1,
        ),
        (
            LoadOptions {
                max_glob_bytes: options.max_glob_bytes - 1,
                ..options.clone()
            },
            Budget::GlobBytes,
            options.max_glob_bytes - 1,
        ),
    ] {
        let error = Workspace::load_with(&tree, &["."], &short).expect_err("one less refuses");
        assert!(
            matches!(error.kind, LoadErrorKind::Budget { budget: actual, limit: applied } if actual == budget && applied == limit),
            "{error:?}"
        );
        assert_eq!(
            error.file,
            RelPath::new("src/lib.rs").expect("fixture source")
        );
        assert!(
            error.line.is_some(),
            "responsible import retains source context"
        );
    }
}

fn declaration() -> UseDecl {
    UseDecl {
        module: 0,
        file: RelPath::new("src/lib.rs").expect("source path"),
        line: 1,
        leading_colon: false,
        vis: Vis::Public,
        test: false,
        doc_hidden: false,
        configuration: crate::facts::ConfigurationDependence::Unconditional,
        kind: crate::index::UseKind::Glob {
            segs: vec!["source".to_owned()],
        },
    }
}

fn delta<'m>(meter: &'m Meter, id: usize, payload: bool, decl: &UseDecl) -> Grown<'m> {
    let memory = Transient::new(meter);
    let mut ids = Vec::new();
    if payload {
        reserve_next(
            &mut ids,
            Steps::LoadAt {
                memory: &memory,
                file: &decl.file,
                line: decl.line,
            },
        )
        .expect("delta payload admission");
        ids.push((ItemId(id), Level::PUBLIC));
    }
    Grown::new(
        id,
        SharedStr::from("X"),
        0,
        Added {
            ids,
            _memory: memory,
        },
    )
}

fn assert_released(meter: &Meter) {
    Transient::new(meter)
        .charge(meter.options().max_glob_bytes, &RelPath::root(), None)
        .expect("the entire live-byte grant is available after delivery dies");
}

#[test]
fn native_delivery_spill_is_lifo_and_failed_push_releases_only_the_incoming_delta() {
    let decl = declaration();
    let meter = Meter::new(&LoadOptions::default());
    let mut queue = Queue::new(&meter);
    queue
        .push(delta(&meter, 0, true, &decl), &decl)
        .expect("inline delta");
    assert_eq!(queue.work.heap_capacity(), 0);
    let payload = meter.usage().glob_bytes;
    assert!(payload > 0);
    queue
        .push(delta(&meter, 1, true, &decl), &decl)
        .expect("first spill");
    let spill = queue.work.heap_capacity() * size_of::<Grown<'_>>();
    assert!(spill > 0);
    assert_eq!(queue.work.pop().expect("spill entry").view, 1);
    assert_eq!(queue.work.pop().expect("inline entry").view, 0);
    assert!(queue.work.is_empty());
    assert!(
        queue.work.heap_capacity() > 0,
        "pop retains the real spill allocation"
    );
    drop(queue);
    assert_released(&meter);

    for short in [false, true] {
        let options = LoadOptions {
            max_glob_bytes: 2 * payload + spill - usize::from(short),
            ..LoadOptions::default()
        };
        let meter = Meter::new(&options);
        let mut queue = Queue::new(&meter);
        queue
            .push(delta(&meter, 0, true, &decl), &decl)
            .expect("inline owner");
        let result = queue.push(delta(&meter, 1, true, &decl), &decl);
        if short {
            assert!(matches!(
                result,
                Err(LoadError {
                    kind: LoadErrorKind::Budget {
                        budget: Budget::GlobBytes,
                        ..
                    },
                    ..
                })
            ));
            assert_eq!(queue.work.len(), 1);
            assert_eq!(queue.work.heap_capacity(), 0);
            // Failed publication dropped the incoming Added owner, while
            // the original inline owner's actual payload is still held.
            Transient::new(&meter)
                .charge(
                    options.max_glob_bytes - payload,
                    &decl.file,
                    Some(decl.line),
                )
                .expect("only the first payload remains live");
        } else {
            result.expect("the exact first-spill peak is admitted");
            assert_eq!(queue.work.len(), 2);
        }
        drop(queue);
        assert_released(&meter);
    }
}

#[test]
fn native_delivery_growth_admits_old_and_new_spill_buffers_together() {
    let decl = declaration();
    let meter = Meter::new(&LoadOptions::default());
    let mut queue = Queue::new(&meter);
    queue
        .push(delta(&meter, 0, false, &decl), &decl)
        .expect("inline");
    queue
        .push(delta(&meter, 1, false, &decl), &decl)
        .expect("spill");
    let capacity = queue.work.heap_capacity();
    let next = purrdf_lex::allocation::growth_capacity::<Grown<'_>>(capacity, capacity + 1)
        .expect("native next capacity");
    drop(queue);
    assert_released(&meter);
    for short in [false, true] {
        let options = LoadOptions {
            max_glob_bytes: (capacity + next) * size_of::<Grown<'_>>() - usize::from(short),
            ..LoadOptions::default()
        };
        let meter = Meter::new(&options);
        let mut queue = Queue::new(&meter);
        for id in 0..=capacity {
            queue
                .push(delta(&meter, id, false, &decl), &decl)
                .expect("fill inline and old spill");
        }
        let result = queue.push(delta(&meter, capacity + 1, false, &decl), &decl);
        if short {
            assert!(matches!(
                result,
                Err(LoadError {
                    kind: LoadErrorKind::Budget {
                        budget: Budget::GlobBytes,
                        ..
                    },
                    ..
                })
            ));
            assert_eq!(queue.work.heap_capacity(), capacity);
            assert_eq!(queue.work.len(), capacity + 1);
        } else {
            result.expect("exact coexisting physical buffers are admitted");
            assert_eq!(queue.work.heap_capacity(), next);
            assert_eq!(queue.work.pop().expect("new entry").view, capacity + 1);
        }
        for expected in (0..=capacity).rev() {
            assert_eq!(queue.work.pop().expect("original entry").view, expected);
        }
        drop(queue);
        assert_released(&meter);
    }
}

#[test]
fn widest_routes_merge_firm_evidence_in_place_and_refuse_before_mutation() {
    let tree: MemoryTree = [
        (
            "Cargo.toml",
            "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n",
        ),
        (
            "src/lib.rs",
            "mod source { pub struct X; } pub use source::*;",
        ),
    ]
    .into_iter()
    .collect();
    let ws = Workspace::load(&tree, &["."]).expect("indexed glob");
    let index = ws.index();
    let site = index.glob_imports[0];
    let input = vec![
        (ItemId(2), Level::within(0).companion()),
        (ItemId(0), Level::PUBLIC.companion()),
        (ItemId(1), Level::PUBLIC.companion()),
        (ItemId(0), Level::within(0)),
        (ItemId(2), Level::PUBLIC),
        (ItemId(1), Level::within(0).companion()),
    ];
    let mut routes = input.clone();
    let allocation = (routes.as_ptr(), routes.capacity());
    index
        .widest(&mut routes, site, &Meter::new(&LoadOptions::default()))
        .expect("merge routes");
    assert_eq!(
        routes,
        [
            (ItemId(0), Level::PUBLIC),
            (ItemId(1), Level::PUBLIC.companion()),
            (ItemId(2), Level::PUBLIC)
        ]
    );
    assert_eq!((routes.as_ptr(), routes.capacity()), allocation);
    let options = LoadOptions {
        max_work: 0,
        ..LoadOptions::default()
    };
    let mut refused = input.clone();
    assert!(matches!(
        index.widest(&mut refused, site, &Meter::new(&options)),
        Err(LoadError {
            kind: LoadErrorKind::Budget {
                budget: Budget::Work,
                ..
            },
            ..
        })
    ));
    assert_eq!(refused, input);
}

#[test]
fn maximal_public_replacements_avoid_repeat_proofs_while_narrower_ones_are_widened() {
    let source = shapes::ambiguous_hub(8);
    let tree: MemoryTree = [
        ("Cargo.toml", shapes::MANIFEST),
        ("src/lib.rs", source.as_str()),
    ]
    .into_iter()
    .collect();
    let ws = Workspace::load(&tree, &["."]).expect("replacement-positive hub");
    let mut index = ws.index().clone();
    assert!(index.drop_count() > 0);
    let mut selected = None;
    for (&(glob, target), names) in &index.globs.drops {
        for (name, spaces) in names {
            for (at, entries) in spaces.iter().enumerate() {
                for &(id, level) in entries {
                    assert_eq!(
                        level,
                        Level::PUBLIC,
                        "the control contains only maximal records"
                    );
                    selected.get_or_insert((glob, target, name.clone(), at, id));
                }
            }
        }
    }
    let meter = Meter::new(&LoadOptions::default());
    let (recorded, counts) = crate::work::measure(usize::MAX, || {
        index.record_drops(&meter).expect("maximal replay")
    });
    assert_eq!(recorded, 0);
    // Any remaining negative proofs are the control population. Widening
    // one real unsaturated selection must require additional routed proof.
    let negative_proof_steps = counts.get(Work::SupplySteps);
    assert_eq!(
        negative_proof_steps, 0,
        "maximal selections and competitors defeated by firm evidence need no graph proof"
    );
    let (glob, target, name, at, id) = selected.expect("a replacement exists");
    let reader = index.uses[glob].module;
    let entries = &mut index
        .globs
        .drops
        .get_mut(&(glob, target))
        .expect("recorded carrier")
        .get_mut(&name)
        .expect("recorded name")[at];
    let place = entries
        .binary_search_by_key(&id, |entry| entry.0)
        .expect("recorded candidate");
    entries[place].1 = Level::within(reader);
    let meter = Meter::new(&LoadOptions::default());
    let (recorded, counts) = crate::work::measure(usize::MAX, || {
        index
            .record_drops(&meter)
            .expect("wider support is still discovered")
    });
    assert_eq!(recorded, 1);
    assert!(
        counts.get(Work::SupplySteps) > negative_proof_steps,
        "widening one unsaturated member requires additional actual route proof"
    );
    assert_eq!(
        index.globs.drops[&(glob, target)][&name][at][place].1,
        Level::PUBLIC
    );
}

#[test]
fn one_returning_carrier_retains_multiple_independently_selected_identities() {
    let source = "pub mod a { pub struct X; }
        pub mod b { pub struct X; }
        pub mod hidden { pub struct X; }
        pub mod hub {
            pub use crate::a::*;
            pub use crate::b::*;
            use crate::hidden::*;
            pub use crate::relay::*;
        }
        pub mod relay { pub use crate::hub::*; }";
    let tree: MemoryTree = [("Cargo.toml", shapes::MANIFEST), ("src/lib.rs", source)]
        .into_iter()
        .collect();
    let ws = Workspace::load(&tree, &["."]).expect("multiple independent selections");
    let index = ws.index();
    let mut both = 0;
    for names in index.globs.drops.values() {
        let Some(spaces) = names.get("X") else {
            continue;
        };
        for entries in spaces {
            let paths: Vec<_> = entries
                .iter()
                .map(|(id, level)| {
                    assert_eq!(*level, Level::PUBLIC);
                    index.items[id.0].path.as_str()
                })
                .collect();
            if paths == ["demo::a::X", "demo::b::X"] {
                both += 1;
            }
        }
    }
    assert_eq!(
        both, 2,
        "one carrier selects both identities in both namespaces"
    );
    let options = LoadOptions {
        max_glob_bytes: ws.usage().glob_bytes,
        ..LoadOptions::default()
    };
    Workspace::load_with(&tree, &["."], &options).expect("exact overlapping scratch grant");
    let refused = Workspace::load_with(
        &tree,
        &["."],
        &LoadOptions {
            max_glob_bytes: options.max_glob_bytes - 1,
            ..options
        },
    )
    .expect_err("one byte below the actual peak is refused");
    assert!(matches!(
        refused.kind,
        LoadErrorKind::Budget {
            budget: Budget::GlobBytes,
            ..
        }
    ));
}