hedos-kernel 1.4.1

The hedos kernel: model records, registry, discovery, install planning, and resolution.
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
//! Integration tests for the `registry` store: CRUD, change detection,
//! case-insensitive listing, state updates, transform-based updates, persistence
//! across reopen, and corruption recovery. Public API only.

mod support;

use std::fs;

use kernel::records::{
    Capability, JsonValue, Modality, ModelRecord, ModelSource, ModelState, ParamSpec, ParamType,
    SourceKind, stable_id,
};
use kernel::registry::{Registry, RegistryError};
use support::TempDir;

fn record(name: &str, path: &str) -> ModelRecord {
    ModelRecord::new(
        name,
        Modality::text(),
        vec![Capability::chat()],
        ModelSource::new(SourceKind::file(), path),
    )
}

#[test]
fn open_empty_directory_is_empty() {
    let dir = TempDir::new();
    let registry = Registry::open(dir.path()).unwrap();
    assert!(registry.is_empty());
    assert_eq!(registry.len(), 0);
    assert!(registry.list().is_empty());
}

#[test]
fn register_persists_and_survives_reopen() {
    let dir = TempDir::new();
    let rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    {
        let mut registry = Registry::open(dir.path()).unwrap();
        assert!(registry.register(rec).unwrap());
        assert!(registry.contains(&id));
    }
    let reopened = Registry::open(dir.path()).unwrap();
    assert_eq!(reopened.get(&id).unwrap().name, "Model");
    assert_eq!(reopened.len(), 1);
}

#[test]
fn registering_an_identical_record_is_a_noop() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let rec = record("Model", "/m.gguf");
    assert!(
        registry.register(rec.clone()).unwrap(),
        "first insert changes"
    );
    assert!(
        !registry.register(rec).unwrap(),
        "identical re-insert is a no-op"
    );
}

#[test]
fn register_all_counts_changes_and_is_idempotent() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let records = vec![record("A", "/a.gguf"), record("B", "/b.gguf")];
    assert_eq!(registry.register_all(records.clone()).unwrap(), 2);
    assert_eq!(registry.len(), 2);
    assert_eq!(
        registry.register_all(records).unwrap(),
        0,
        "no changes second time"
    );
}

#[test]
fn unregister_removes_and_persists() {
    let dir = TempDir::new();
    let rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec).unwrap();

    let removed = registry.unregister(&id).unwrap();
    assert_eq!(removed.unwrap().id, id);
    assert!(!registry.contains(&id));
    assert!(
        registry.unregister(&id).unwrap().is_none(),
        "removing again is None"
    );

    let reopened = Registry::open(dir.path()).unwrap();
    assert!(reopened.get(&id).is_none());
}

#[test]
fn list_is_sorted_case_insensitively() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(record("banana", "/1")).unwrap();
    registry.register(record("Apple", "/2")).unwrap();
    registry.register(record("Cherry", "/3")).unwrap();
    let names: Vec<&str> = registry.list().iter().map(|r| r.name.as_str()).collect();
    assert_eq!(names, ["Apple", "banana", "Cherry"]);
}

#[test]
fn set_state_updates_present_and_ignores_absent() {
    let dir = TempDir::new();
    let rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec).unwrap();

    assert!(
        registry
            .set_state_if_present(&id, ModelState::Ready)
            .unwrap()
    );
    assert_eq!(registry.get(&id).unwrap().state, ModelState::Ready);
    assert!(
        !registry
            .set_state_if_present("nonexistent", ModelState::Missing)
            .unwrap()
    );

    let reopened = Registry::open(dir.path()).unwrap();
    assert_eq!(reopened.get(&id).unwrap().state, ModelState::Ready);
}

#[test]
fn update_applies_transform_and_skips_none() {
    let dir = TempDir::new();
    let rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec).unwrap();

    let changed = registry
        .update(std::slice::from_ref(&id), |current| {
            let mut next = current.clone();
            next.alias = Some("Nick".into());
            Some(next)
        })
        .unwrap();
    assert_eq!(changed.len(), 1);
    assert_eq!(registry.get(&id).unwrap().alias.as_deref(), Some("Nick"));

    let unchanged = registry
        .update(std::slice::from_ref(&id), |_| None)
        .unwrap();
    assert!(unchanged.is_empty(), "returning None changes nothing");

    let same = registry
        .update(&[id], |current| Some(current.clone()))
        .unwrap();
    assert!(
        same.is_empty(),
        "an identical transform result changes nothing"
    );

    let absent = registry
        .update(&["missing".to_owned()], |c| Some(c.clone()))
        .unwrap();
    assert!(absent.is_empty());
}

#[test]
fn corrupt_store_is_quarantined_and_reported() {
    let dir = TempDir::new();
    fs::write(dir.join("models.json"), b"{ not valid json").unwrap();

    match Registry::open(dir.path()) {
        Err(RegistryError::CorruptStore(_)) => {}
        other => panic!("expected CorruptStore, got {other:?}"),
    }
    assert!(
        !dir.join("models.json").exists(),
        "corrupt store should be quarantined"
    );
    let quarantined = fs::read_dir(dir.path())
        .unwrap()
        .filter_map(Result::ok)
        .any(|e| {
            e.file_name()
                .to_string_lossy()
                .starts_with("models.json.corrupt-")
        });
    assert!(quarantined);
}

#[test]
fn update_that_changes_id_migrates_the_key() {
    let dir = TempDir::new();
    let rec = record("Model", "/a.gguf");
    let old_id = rec.id.clone();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec).unwrap();

    let changed = registry
        .update(std::slice::from_ref(&old_id), |current| {
            let mut next = current.clone();
            next.source = ModelSource::new(SourceKind::file(), "/b.gguf");
            next.id = stable_id(&next.source);
            Some(next)
        })
        .unwrap();
    let new_id = changed[0].id.clone();
    assert_ne!(new_id, old_id);
    assert!(registry.get(&old_id).is_none(), "old key must be gone");
    assert_eq!(registry.get(&new_id).unwrap().name, "Model");
    assert_eq!(registry.len(), 1);

    let reopened = Registry::open(dir.path()).unwrap();
    assert!(reopened.get(&old_id).is_none());
    assert_eq!(reopened.get(&new_id).unwrap().id, new_id);
}

#[test]
fn register_replaces_existing_content_and_persists() {
    let dir = TempDir::new();
    let mut rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec.clone()).unwrap();

    rec.alias = Some("Renamed".into());
    assert!(
        registry.register(rec).unwrap(),
        "changed content is a change"
    );
    assert_eq!(registry.get(&id).unwrap().alias.as_deref(), Some("Renamed"));

    let reopened = Registry::open(dir.path()).unwrap();
    assert_eq!(reopened.get(&id).unwrap().alias.as_deref(), Some("Renamed"));
}

#[test]
fn a_shelf_written_in_mebibytes_keeps_its_sizes_after_the_upgrade() {
    let dir = TempDir::new();
    // A store from before sizes were exact: the size is whole mebibytes under
    // the old key, and there is no byte figure at all.
    std::fs::write(
        dir.path().join("models.json"),
        r#"{"schema_version":1,"models":[{
            "id":"old","name":"Model","modality":"text","capabilities":["chat"],
            "source":{"kind":"file","path":"/m.gguf"},
            "footprint_mb":4096,"registered_at":1
        }]}"#,
    )
    .unwrap();

    let mut registry = Registry::open(dir.path()).expect("open the old store");
    let record = registry.get("old").expect("the record survives").clone();
    assert_eq!(record.footprint_bytes, Some(4096 * (1 << 20)));

    // And once anything about the record is written, the old key is gone with
    // it, so the fold has nothing left to do.
    let mut renamed = record;
    renamed.alias = Some("nick".to_owned());
    registry.register(renamed).unwrap();
    let written = std::fs::read_to_string(dir.path().join("models.json")).unwrap();
    assert!(!written.contains("footprint_mb"), "{written}");
    assert!(written.contains("footprint_bytes"));
}

#[test]
fn an_exact_size_is_not_overwritten_by_a_leftover_mebibyte_figure() {
    let dir = TempDir::new();
    std::fs::write(
        dir.path().join("models.json"),
        r#"{"schema_version":1,"models":[{
            "id":"both","name":"Model","modality":"text","capabilities":["chat"],
            "source":{"kind":"file","path":"/m.gguf"},
            "footprint_mb":4096,"footprint_bytes":491400032,"registered_at":1
        }]}"#,
    )
    .unwrap();

    let registry = Registry::open(dir.path()).expect("open the store");
    assert_eq!(
        registry.get("both").expect("the record").footprint_bytes,
        Some(491_400_032)
    );
}

#[test]
fn reopen_preserves_full_record_fidelity() {
    let dir = TempDir::new();
    let mut rec = record("Model", "/m.gguf");
    rec.alias = Some("Nick".into());
    rec.system_prompt = Some("be terse".into());
    rec.footprint_bytes = Some(4096 * (1 << 20));
    rec.content_fingerprint = Some("abcd".into());
    rec.params.push(ParamSpec {
        key: "temperature".into(),
        param_type: ParamType::Float,
        default_value: Some(JsonValue::Double(0.8)),
        range: Some(vec![JsonValue::Double(0.0), JsonValue::Double(2.0)]),
        values: None,
    });
    rec.param_values
        .insert("temperature".into(), JsonValue::Double(0.5));
    let id = rec.id.clone();

    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(rec.clone()).unwrap();
    let reopened = Registry::open(dir.path()).unwrap();
    assert_eq!(reopened.get(&id), Some(&rec));
}

#[test]
fn future_schema_is_rejected_not_downgraded() {
    let dir = TempDir::new();
    let file = dir.join("models.json");
    fs::write(&file, br#"{"schema_version":999,"models":[]}"#).unwrap();

    match Registry::open(dir.path()) {
        Err(RegistryError::FutureSchema {
            found: 999,
            supported: 1,
        }) => {}
        other => panic!("expected FutureSchema, got {other:?}"),
    }
    assert!(
        file.exists(),
        "a future-schema store must be left in place, not touched"
    );
}

#[test]
fn duplicate_ids_in_file_keep_the_last() {
    let dir = TempDir::new();
    fs::write(
        dir.join("models.json"),
        br#"{"schema_version":1,"models":[
            {"id":"dup","name":"First","modality":"text","capabilities":["chat"],
             "source":{"kind":"file","path":"/a"},"registered_at":1},
            {"id":"dup","name":"Second","modality":"text","capabilities":["chat"],
             "source":{"kind":"file","path":"/b"},"registered_at":2}
        ]}"#,
    )
    .unwrap();

    let registry = Registry::open(dir.path()).unwrap();
    assert_eq!(registry.len(), 1);
    assert_eq!(registry.get("dup").unwrap().name, "Second");
}

#[test]
fn register_all_with_internal_duplicate_keeps_last() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let mut first = record("First", "/x.gguf");
    let mut second = first.clone();
    second.name = "Second".into();
    assert_eq!(second.id, first.id, "same source means same id");
    first.name = "First".into();

    registry.register_all(vec![first, second]).unwrap();
    assert_eq!(registry.len(), 1);
    assert_eq!(registry.list()[0].name, "Second");
}

#[test]
fn register_all_counts_only_new_or_changed() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let a = record("A", "/a.gguf");
    let b = record("B", "/b.gguf");
    registry.register(a.clone()).unwrap();

    let c = record("C", "/c.gguf");
    assert_eq!(
        registry.register_all(vec![a, b, c]).unwrap(),
        2,
        "a is unchanged"
    );
    assert_eq!(registry.len(), 3);
}

#[test]
fn list_tie_breaks_by_id_when_names_match_case_insensitively() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let lower = record("apple", "/one");
    let upper = record("Apple", "/two");
    let mut ids = [lower.id.clone(), upper.id.clone()];
    ids.sort();
    registry.register(lower).unwrap();
    registry.register(upper).unwrap();

    let listed: Vec<&str> = registry.list().iter().map(|r| r.id.as_str()).collect();
    assert_eq!(listed, [ids[0].as_str(), ids[1].as_str()]);
}

#[test]
fn update_over_multiple_ids_changes_only_the_relevant_ones() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    let a = record("A", "/a");
    let b = record("B", "/b");
    let c = record("C", "/c");
    let (ida, idb, idc) = (a.id.clone(), b.id.clone(), c.id.clone());
    registry.register_all(vec![a, b, c]).unwrap();

    let changed = registry
        .update(&[ida.clone(), idb.clone(), idc.clone()], |current| {
            if current.id == idb {
                let mut next = current.clone();
                next.alias = Some("only-b".into());
                Some(next)
            } else if current.id == idc {
                None
            } else {
                Some(current.clone())
            }
        })
        .unwrap();
    assert_eq!(changed.len(), 1);
    assert_eq!(changed[0].id, idb);
    assert_eq!(registry.get(&idb).unwrap().alias.as_deref(), Some("only-b"));
    assert!(registry.get(&ida).unwrap().alias.is_none());
}

#[test]
fn unregister_absent_id_returns_none() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    assert!(registry.unregister("never").unwrap().is_none());
}

#[test]
fn concurrent_writers_do_not_lose_each_others_updates() {
    let dir = TempDir::new();
    let mut a = Registry::open(dir.path()).unwrap();
    let mut b = Registry::open(dir.path()).unwrap();

    let x = record("X", "/x.gguf");
    let y = record("Y", "/y.gguf");
    let (id_x, id_y) = (x.id.clone(), y.id.clone());

    assert!(a.register(x).unwrap(), "a registers X");
    assert!(
        b.register(y).unwrap(),
        "b registers Y, reloading a's write first"
    );

    let c = Registry::open(dir.path()).unwrap();
    assert!(c.contains(&id_x), "X must survive both writers");
    assert!(c.contains(&id_y), "Y must survive both writers");
    assert_eq!(c.len(), 2);
}

#[test]
fn registering_the_same_id_from_two_instances_converges() {
    let dir = TempDir::new();
    let mut a = Registry::open(dir.path()).unwrap();
    let mut b = Registry::open(dir.path()).unwrap();

    let mut rec = record("Model", "/m.gguf");
    let id = rec.id.clone();
    assert!(a.register(rec.clone()).unwrap());

    rec.alias = Some("FromB".into());
    assert!(
        b.register(rec).unwrap(),
        "b's differing content is a change"
    );

    let c = Registry::open(dir.path()).unwrap();
    assert_eq!(c.len(), 1);
    assert_eq!(c.get(&id).unwrap().alias.as_deref(), Some("FromB"));
}

#[test]
fn a_refresh_picks_up_what_another_process_registered() {
    let dir = TempDir::new();
    let mut screen = Registry::open(dir.path()).unwrap();
    let generation = screen.generation();

    // A second handle on the same directory is another process: the pull worker
    // that registers what it fetched.
    let mut worker = Registry::open(dir.path()).unwrap();
    worker.register(record("pulled", "/models/pulled")).unwrap();

    // The in-memory view is only reloaded under a mutation, so until it is told
    // to look again the screen shows a shelf without the model.
    assert!(screen.is_empty());

    assert!(screen.refresh().unwrap());
    assert_eq!(screen.len(), 1);
    assert!(screen.list().iter().any(|held| held.name == "pulled"));
    // What was cached against the old generation was derived from records that
    // have just been replaced.
    assert!(screen.generation() > generation);
}

#[test]
fn a_refresh_that_finds_nothing_new_is_not_a_change() {
    let dir = TempDir::new();
    let mut registry = Registry::open(dir.path()).unwrap();
    registry.register(record("held", "/models/held")).unwrap();
    let generation = registry.generation();

    assert!(!registry.refresh().unwrap());
    assert_eq!(registry.generation(), generation);
}