|
| 1 | +//! Real checkpoint metadata and persisted namespace checks; no model, device or shader is opened. |
| 2 | +const std = @import("std"); |
| 3 | +const disk = @import("src/native/disk_identity.zig"); |
| 4 | +const glm = @import("src/families/glm/load_plan.zig"); |
| 5 | +const pack = @import("src/families/flashnext/pack_io.zig"); |
| 6 | +const Imprint = @import("engine_api").prompt_imprint.Imprint; |
| 7 | +const binary = @import("sys").file_identity; |
| 8 | +const t = std.testing; |
| 9 | +const io = t.io; |
| 10 | +const Kind = enum { glm, flashnext }; |
| 11 | +const config = "{}"; |
| 12 | +const index = "{\"weight_map\":{\"unprobed.weight\":\"model.safetensors\"}}"; |
| 13 | +const header = "{\"unprobed.weight\":{\"dtype\":\"F32\",\"shape\":[1],\"data_offsets\":[0,4]}}"; |
| 14 | + |
| 15 | +fn legacy(a: std.mem.Allocator, dir: []const u8, kind: Kind) !u64 { |
| 16 | + if (kind == .glm) return glm.modelHash(a, dir, config); |
| 17 | + const text = try pack.sourceIdentity(a, io, dir); |
| 18 | + defer a.free(text); |
| 19 | + return std.hash.Wyhash.hash(0x6964, text); |
| 20 | +} |
| 21 | + |
| 22 | +fn allocatingScope(a: std.mem.Allocator, dir: []const u8) !void { |
| 23 | + _ = try disk.scoped(a, io, dir, 7); |
| 24 | +} |
| 25 | + |
| 26 | +fn changedWeightMiss(kind: Kind) !void { |
| 27 | + const a = t.allocator; |
| 28 | + var tmp = t.tmpDir(.{}); |
| 29 | + defer tmp.cleanup(); |
| 30 | + const dir = try tmp.dir.realPathFileAlloc(io, ".", a); |
| 31 | + defer a.free(dir); |
| 32 | + const root = try std.fs.path.join(a, &.{ dir, "states" }); |
| 33 | + defer a.free(root); |
| 34 | + try tmp.dir.writeFile(io, .{ .sub_path = "config.json", .data = config }); |
| 35 | + try tmp.dir.writeFile(io, .{ .sub_path = "model.safetensors.index.json", .data = index }); |
| 36 | + var shard: [8 + header.len + 4]u8 = @splat(0); |
| 37 | + std.mem.writeInt(u64, shard[0..8], header.len, .little); |
| 38 | + @memcpy(shard[8..][0..header.len], header); |
| 39 | + try tmp.dir.writeFile(io, .{ .sub_path = "model.safetensors", .data = &shard }); |
| 40 | + const old = try legacy(a, dir, kind); |
| 41 | + if (kind == .glm) try t.checkAllAllocationFailures(a, allocatingScope, .{dir}); |
| 42 | + const before = try disk.scoped(a, io, dir, old); |
| 43 | + try t.expectEqual(before, try disk.scoped(a, io, dir, old)); |
| 44 | + const tokens = [_]u32{ 7, 11, 13 }; |
| 45 | + const key = Imprint.keyOf(&tokens); |
| 46 | + { |
| 47 | + var state = try Imprint.open(a, root, before, 1 << 20); |
| 48 | + defer state.deinit(); |
| 49 | + try state.add(key, 2, &tokens, &.{}, 4); |
| 50 | + try t.expect(state.best(&tokens, &.{}, false, 0) != null); |
| 51 | + } |
| 52 | + // An unvisited tensor can change without changing a fixed probe, config, index or shard header. |
| 53 | + try std.Io.sleep(io, .fromMilliseconds(5), .awake); |
| 54 | + shard[shard.len - 1] = 1; |
| 55 | + try tmp.dir.writeFile(io, .{ .sub_path = "model.safetensors", .data = &shard }); |
| 56 | + try t.expectEqual(old, try legacy(a, dir, kind)); |
| 57 | + const after = try disk.scoped(a, io, dir, old); |
| 58 | + var fresh = try Imprint.open(a, root, after, 1 << 20); |
| 59 | + defer fresh.deinit(); |
| 60 | + try t.expectEqual(@as(usize, 0), fresh.metas.items.len); |
| 61 | + try t.expect(fresh.best(&tokens, &.{}, false, 0) == null); |
| 62 | + var preserved = try Imprint.open(a, root, before, 1 << 20); |
| 63 | + defer preserved.deinit(); |
| 64 | + try t.expect(preserved.best(&tokens, &.{}, false, 0) != null); |
| 65 | +} |
| 66 | + |
| 67 | +test "persistence identity: GLM same-header weight rewrite misses the old serving namespace" { |
| 68 | + try changedWeightMiss(.glm); |
| 69 | +} |
| 70 | + |
| 71 | +test "persistence identity: Flash Next same-header weight rewrite misses the old serving namespace" { |
| 72 | + try changedWeightMiss(.flashnext); |
| 73 | +} |
| 74 | + |
| 75 | +test { |
| 76 | + _ = @import("src/native/variant_identity.zig"); |
| 77 | +} |
| 78 | + |
| 79 | +test "persistence identity: changed serving binary bytes move an otherwise identical namespace" { |
| 80 | + const a = std.testing.allocator; |
| 81 | + var tmp = std.testing.tmpDir(.{}); |
| 82 | + defer tmp.cleanup(); |
| 83 | + const dir = try tmp.dir.realPathFileAlloc(io, ".", a); |
| 84 | + defer a.free(dir); |
| 85 | + const path = try std.fs.path.join(a, &.{ dir, "server" }); |
| 86 | + defer a.free(path); |
| 87 | + try tmp.dir.writeFile(io, .{ .sub_path = "server", .data = "compiled code one" }); |
| 88 | + const first = try binary.hash(io, path, 1024); |
| 89 | + try std.testing.expectEqual(first, try binary.hash(io, path, 1024)); |
| 90 | + try std.testing.expectError(error.IdentityFileTooLarge, binary.hash(io, path, 1)); |
| 91 | + var before = std.hash.Wyhash.init(17); |
| 92 | + binary.bind(&before, first); |
| 93 | + const root = try std.fs.path.join(a, &.{ dir, "kept" }); |
| 94 | + defer a.free(root); |
| 95 | + const ids = [_]u32{ 5, 7, 11 }; |
| 96 | + var kept = try Imprint.open(a, root, before.final(), 1 << 20); |
| 97 | + defer kept.deinit(); |
| 98 | + try kept.add(Imprint.keyOf(&ids), 2, &ids, &.{}, 4); |
| 99 | + try tmp.dir.writeFile(io, .{ .sub_path = "server", .data = "compiled code two" }); |
| 100 | + var after = std.hash.Wyhash.init(17); |
| 101 | + binary.bind(&after, try binary.hash(io, path, 1024)); |
| 102 | + try std.testing.expect(before.final() != after.final()); |
| 103 | + var next = try Imprint.open(a, root, after.final(), 1 << 20); |
| 104 | + defer next.deinit(); |
| 105 | + try t.expect(next.best(&ids, &.{}, false, 0) == null); |
| 106 | + try t.expect(kept.best(&ids, &.{}, false, 0) != null); |
| 107 | +} |
| 108 | + |
| 109 | +test "persistence identity: file fingerprints cover every block and agree with the whole-file SHA256" { |
| 110 | + const a = t.allocator; |
| 111 | + var tmp = t.tmpDir(.{}); |
| 112 | + defer tmp.cleanup(); |
| 113 | + const dir = try tmp.dir.realPathFileAlloc(io, ".", a); |
| 114 | + defer a.free(dir); |
| 115 | + const path = try std.fs.path.join(a, &.{ dir, "code" }); |
| 116 | + defer a.free(path); |
| 117 | + const data = try a.alloc(u8, 2 * 32768 + 17); |
| 118 | + defer a.free(data); |
| 119 | + for (data, 0..) |*value, i| value.* = @truncate(i *% 131); |
| 120 | + try tmp.dir.writeFile(io, .{ .sub_path = "code", .data = data }); |
| 121 | + var expected: [32]u8 = undefined; |
| 122 | + std.crypto.hash.sha2.Sha256.hash(data, &expected, .{}); |
| 123 | + try t.expectEqual(expected, try binary.hash(io, path, data.len)); |
| 124 | + data[32768 + 100] ^= 1; |
| 125 | + try tmp.dir.writeFile(io, .{ .sub_path = "code", .data = data }); |
| 126 | + try t.expect(!std.mem.eql(u8, &expected, &(try binary.hash(io, path, data.len)))); |
| 127 | +} |
| 128 | + |
| 129 | +test "persistence identity: tokenizer and template edits reuse only identical canonical token prefixes" { |
| 130 | + const a = t.allocator; |
| 131 | + var tmp = t.tmpDir(.{}); |
| 132 | + defer tmp.cleanup(); |
| 133 | + const dir = try tmp.dir.realPathFileAlloc(io, ".", a); |
| 134 | + defer a.free(dir); |
| 135 | + try tmp.dir.writeFile(io, .{ .sub_path = "tokenizer.json", .data = "first tokenizer" }); |
| 136 | + try tmp.dir.writeFile(io, .{ .sub_path = "chat_template.jinja", .data = "first template" }); |
| 137 | + const identity = try disk.scoped(a, io, dir, 1); |
| 138 | + const root = try std.fs.path.join(a, &.{ dir, "states" }); |
| 139 | + defer a.free(root); |
| 140 | + var kept = try Imprint.open(a, root, identity, 1 << 20); |
| 141 | + defer kept.deinit(); |
| 142 | + const ids = [_]u32{ 5, 7, 11 }; |
| 143 | + try kept.add(Imprint.keyOf(&ids), 2, &ids, &.{1}, 4); |
| 144 | + try tmp.dir.writeFile(io, .{ .sub_path = "tokenizer.json", .data = "other tokenizer" }); |
| 145 | + try tmp.dir.writeFile(io, .{ .sub_path = "chat_template.jinja", .data = "other template" }); |
| 146 | + try t.expectEqual(identity, try disk.scoped(a, io, dir, 1)); |
| 147 | + try t.expect(kept.best(&ids, &.{ 1, 2 }, true, 0) != null); |
| 148 | + try t.expect(kept.best(&.{ 5, 9, 11 }, &.{ 1, 2 }, true, 0) == null); |
| 149 | + try t.expect(kept.best(&.{ 5, 7, 13 }, &.{ 1, 2 }, true, 0) == null); |
| 150 | + try t.expect(kept.best(&ids, &.{ 0, 2 }, true, 0) == null); |
| 151 | +} |
0 commit comments