a73x

255115ee

Refine instance upload planning contract

a73x   2026-04-08 18:50


diff --git a/src/renderer.zig b/src/renderer.zig
index 864b195..9b17ae6 100644
--- a/src/renderer.zig
+++ b/src/renderer.zig
@@ -319,19 +319,27 @@ const InstanceUploadRequest = struct {
    write_len: u32,
};

const InstanceUploadMode = enum {
    partial,
    full,
    invalid_range,
};

const InstanceUploadDecision = struct {
    needed_capacity: u32,
    needs_growth: bool,
    force_full_upload: bool,
    needed_capacity: ?u32,
    upload_mode: InstanceUploadMode,
};

fn planInstanceUpload(req: InstanceUploadRequest) InstanceUploadDecision {
    const needed_capacity = std.math.add(u32, req.offset_instances, req.write_len) catch std.math.maxInt(u32);
    const needs_growth = needed_capacity > req.current_capacity;
    const needed_capacity = std.math.add(u32, req.offset_instances, req.write_len) catch {
        return .{
            .needed_capacity = null,
            .upload_mode = .invalid_range,
        };
    };
    return .{
        .needed_capacity = needed_capacity,
        .needs_growth = needs_growth,
        .force_full_upload = needs_growth,
        .upload_mode = if (needed_capacity > req.current_capacity) .full else .partial,
    };
}

@@ -1375,8 +1383,8 @@ test "range upload falls back to full upload when capacity must grow" {
        .write_len = 4,
    });

    try std.testing.expect(decision.needs_growth);
    try std.testing.expect(decision.force_full_upload);
    try std.testing.expectEqual(@as(?u32, 10), decision.needed_capacity);
    try std.testing.expectEqual(InstanceUploadMode.full, decision.upload_mode);
}

test "range upload stays partial when capacity is sufficient" {
@@ -1386,6 +1394,39 @@ test "range upload stays partial when capacity is sufficient" {
        .write_len = 3,
    });

    try std.testing.expect(!decision.needs_growth);
    try std.testing.expect(!decision.force_full_upload);
    try std.testing.expectEqual(@as(?u32, 7), decision.needed_capacity);
    try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode);
}

test "range upload stays partial on an exact fit" {
    const decision = planInstanceUpload(.{
        .current_capacity = 7,
        .offset_instances = 4,
        .write_len = 3,
    });

    try std.testing.expectEqual(@as(?u32, 7), decision.needed_capacity);
    try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode);
}

test "range upload allows zero-length writes without forcing growth" {
    const decision = planInstanceUpload(.{
        .current_capacity = 8,
        .offset_instances = 8,
        .write_len = 0,
    });

    try std.testing.expectEqual(@as(?u32, 8), decision.needed_capacity);
    try std.testing.expectEqual(InstanceUploadMode.partial, decision.upload_mode);
}

test "range upload reports overflow explicitly" {
    const decision = planInstanceUpload(.{
        .current_capacity = std.math.maxInt(u32),
        .offset_instances = std.math.maxInt(u32),
        .write_len = 1,
    });

    try std.testing.expectEqual(@as(?u32, null), decision.needed_capacity);
    try std.testing.expectEqual(InstanceUploadMode.invalid_range, decision.upload_mode);
}