{
  "schemaVersion": 1,
  "title": "32 MiB 合成 memfile：逻辑镜像 → Header → 来源数据文件 → Mooncake KV",
  "evidenceType": "synthetic arithmetic example derived from pinned source, not an observed KAS run",
  "repo": "KASandbox",
  "commit": "54a49bc74e2d1dc7e393ce1d61fe4ea7cc592af7",
  "scope": "Illustrates one memfile only, not the complete six-artifact snapshot group or a real published template.",
  "units": {
    "MiB": 1048576,
    "mappingBlockBytes": 2097152,
    "chunkBytes": 4194304
  },
  "assumptions": {
    "providerBucketAlias": "templates",
    "buildAliases": {
      "B0": {
        "meaning": "base build alias, not a literal production UUID",
        "syntheticUUIDIfSerialized": "00000000-0000-4000-8000-000000000000"
      },
      "B1": {
        "meaning": "new build alias, not a literal production UUID",
        "syntheticUUIDIfSerialized": "00000000-0000-4000-8000-000000000001"
      },
      "Nil": {
        "meaning": "actual uuid.Nil zero-source sentinel",
        "UUID": "00000000-0000-0000-0000-000000000000"
      }
    },
    "displayKeys": "B0/B1 strings keep parity with KAS_MODEL. Real keys substitute actual Build UUIDs and configured bucket/prefix; templates is a chosen example bucket.",
    "baseMaterialization": "Assume B0 source file is a complete 32 MiB base. B1 nonzero dirty block indices are 2,3,10; empty block index is 6; each mapping block is 2 MiB.",
    "payloads": "Data chunk contents are symbolic, not generated/loaded real guest bytes."
  },
  "logicalImage": {
    "id": "LI-B1-memfile",
    "kind": "LogicalImage",
    "identity": {
      "currentBuildAlias": "B1",
      "fileKind": "memfile"
    },
    "logicalBytes": 33554432,
    "blockCount": 16,
    "header": "H-B1",
    "sourceDataFiles": [
      "SF-B0",
      "SF-B1"
    ],
    "zeroExtentMappingIds": [
      "M4"
    ],
    "note": "A composed read view presented by build.File + Header; not a separate 32 MiB B1 remote data file or a single giant KV."
  },
  "header": {
    "id": "H-B1",
    "kind": "Header",
    "storagePathAlias": "B1/memfile.header",
    "mainKV": "KV-B1-header",
    "metadata": {
      "Version": 3,
      "BlockSize": 2097152,
      "Size": 33554432,
      "Generation": 1,
      "BuildIdAlias": "B1",
      "BaseBuildIdAlias": "B0"
    },
    "ownSerializedValueBytes": 344,
    "ownSerializedValueDerivation": "64-byte Metadata + 7 × 40-byte BuildMap = 344 bytes; derived for exactly this synthetic record list using current binary.Write format",
    "mappingCount": 7,
    "mappings": [
      {
        "id": "M1",
        "Offset": 0,
        "Length": 4194304,
        "BuildIdAlias": "B0",
        "BuildId": "00000000-0000-4000-8000-000000000000",
        "BuildStorageOffset": 0,
        "logicalRangeMiB": [
          0,
          4
        ],
        "sourceRangeMiB": [
          0,
          4
        ],
        "targetSourceFile": "SF-B0",
        "meaning": "inherited base bytes"
      },
      {
        "id": "M2",
        "Offset": 4194304,
        "Length": 4194304,
        "BuildIdAlias": "B1",
        "BuildId": "00000000-0000-4000-8000-000000000001",
        "BuildStorageOffset": 0,
        "logicalRangeMiB": [
          4,
          8
        ],
        "sourceRangeMiB": [
          0,
          4
        ],
        "targetSourceFile": "SF-B1",
        "meaning": "new nonzero dirty bytes, packed contiguously"
      },
      {
        "id": "M3",
        "Offset": 8388608,
        "Length": 4194304,
        "BuildIdAlias": "B0",
        "BuildId": "00000000-0000-4000-8000-000000000000",
        "BuildStorageOffset": 8388608,
        "logicalRangeMiB": [
          8,
          12
        ],
        "sourceRangeMiB": [
          8,
          12
        ],
        "targetSourceFile": "SF-B0",
        "meaning": "inherited base bytes"
      },
      {
        "id": "M4",
        "Offset": 12582912,
        "Length": 2097152,
        "BuildIdAlias": "Nil",
        "BuildId": "00000000-0000-0000-0000-000000000000",
        "BuildStorageOffset": 0,
        "logicalRangeMiB": [
          12,
          14
        ],
        "sourceRangeMiB": null,
        "targetSourceFile": null,
        "meaning": "zero extent; BuildStorageOffset is ignored"
      },
      {
        "id": "M5",
        "Offset": 14680064,
        "Length": 6291456,
        "BuildIdAlias": "B0",
        "BuildId": "00000000-0000-4000-8000-000000000000",
        "BuildStorageOffset": 14680064,
        "logicalRangeMiB": [
          14,
          20
        ],
        "sourceRangeMiB": [
          14,
          20
        ],
        "targetSourceFile": "SF-B0",
        "meaning": "inherited base bytes"
      },
      {
        "id": "M6",
        "Offset": 20971520,
        "Length": 2097152,
        "BuildIdAlias": "B1",
        "BuildId": "00000000-0000-4000-8000-000000000001",
        "BuildStorageOffset": 4194304,
        "logicalRangeMiB": [
          20,
          22
        ],
        "sourceRangeMiB": [
          4,
          6
        ],
        "targetSourceFile": "SF-B1",
        "meaning": "new nonzero dirty bytes, packed contiguously"
      },
      {
        "id": "M7",
        "Offset": 23068672,
        "Length": 10485760,
        "BuildIdAlias": "B0",
        "BuildId": "00000000-0000-4000-8000-000000000000",
        "BuildStorageOffset": 23068672,
        "logicalRangeMiB": [
          22,
          32
        ],
        "sourceRangeMiB": [
          22,
          32
        ],
        "targetSourceFile": "SF-B0",
        "meaning": "inherited base bytes"
      }
    ],
    "note": "Metadata.Size describes the logical image length, not Header byte size and not packed source-data-file size. Final mappings already name B0/B1 data sources; normal reads do not need to recursively fetch B0 Header."
  },
  "sourceDataFiles": [
    {
      "id": "SF-B0",
      "kind": "sourceDataFile",
      "buildAlias": "B0",
      "fileName": "memfile",
      "storagePathAlias": "B0/memfile",
      "fullStoreKeyAlias": "templates/B0/memfile",
      "fileBytes": 33554432,
      "mainKV": "KV-B0-main",
      "chunkKVs": [
        "CK-B0-0",
        "CK-B0-4194304",
        "CK-B0-8388608",
        "CK-B0-12582912",
        "CK-B0-16777216",
        "CK-B0-20971520",
        "CK-B0-25165824",
        "CK-B0-29360128"
      ],
      "meaning": "assumed fully materialized base file; the B1 logical image references 24 MiB of it, while its own stored length remains 32 MiB",
      "referencedBytesByLogicalImage": 25165824
    },
    {
      "id": "SF-B1",
      "kind": "sourceDataFile",
      "buildAlias": "B1",
      "fileName": "memfile",
      "storagePathAlias": "B1/memfile",
      "fullStoreKeyAlias": "templates/B1/memfile",
      "fileBytes": 6291456,
      "mainKV": "KV-B1-main",
      "chunkKVs": [
        "CK-B1-0",
        "CK-B1-4194304"
      ],
      "meaning": "packed nonzero dirty data for this generation; not a complete 32 MiB image",
      "packedSegments": [
        {
          "sourceRangeMiB": [
            0,
            2
          ],
          "originLogicalRangeMiB": [
            4,
            6
          ],
          "dirtyBlockIndex": 2
        },
        {
          "sourceRangeMiB": [
            2,
            4
          ],
          "originLogicalRangeMiB": [
            6,
            8
          ],
          "dirtyBlockIndex": 3
        },
        {
          "sourceRangeMiB": [
            4,
            6
          ],
          "originLogicalRangeMiB": [
            20,
            22
          ],
          "dirtyBlockIndex": 10
        }
      ]
    }
  ],
  "mainKV": [
    {
      "id": "KV-B0-main",
      "kind": "mainKV",
      "key": "templates/B0/memfile",
      "valueKind": "chunk-layout JSON descriptor",
      "valueUTF8": "{\"size\":33554432,\"chunk_size\":4194304}",
      "valueBytes": 38,
      "descriptor": {
        "size": 33554432,
        "chunk_size": 4194304
      },
      "sourceDataFile": "SF-B0",
      "chunkKVs": [
        "CK-B0-0",
        "CK-B0-4194304",
        "CK-B0-8388608",
        "CK-B0-12582912",
        "CK-B0-16777216",
        "CK-B0-20971520",
        "CK-B0-25165824",
        "CK-B0-29360128"
      ],
      "note": "the key contains descriptor bytes, not the whole memfile; descriptor.size is this source data file length"
    },
    {
      "id": "KV-B1-main",
      "kind": "mainKV",
      "key": "templates/B1/memfile",
      "valueKind": "chunk-layout JSON descriptor",
      "valueUTF8": "{\"size\":6291456,\"chunk_size\":4194304}",
      "valueBytes": 37,
      "descriptor": {
        "size": 6291456,
        "chunk_size": 4194304
      },
      "sourceDataFile": "SF-B1",
      "chunkKVs": [
        "CK-B1-0",
        "CK-B1-4194304"
      ],
      "note": "the key contains descriptor bytes, not the whole memfile; descriptor.size is this source data file length"
    },
    {
      "id": "KV-B1-header",
      "kind": "mainKV",
      "key": "templates/B1/memfile.header",
      "valueKind": "raw serialized binary Header",
      "valueBytes": 344,
      "header": "H-B1",
      "chunkKVs": [],
      "payloadLayout": "little-endian Metadata (64 bytes) + 7 BuildMap records (40 bytes each)",
      "note": "stored via Blob.Put; 344 <= 4 MiB, so this example is raw at its main key, with no #c# children"
    }
  ],
  "chunkKV": [
    {
      "id": "CK-B0-0",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#0",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 0,
      "sourceRangeBytes": [
        0,
        4194304
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-4194304",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#4194304",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 4194304,
      "sourceRangeBytes": [
        4194304,
        8388608
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-8388608",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#8388608",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 8388608,
      "sourceRangeBytes": [
        8388608,
        12582912
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-12582912",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#12582912",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 12582912,
      "sourceRangeBytes": [
        12582912,
        16777216
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-16777216",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#16777216",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 16777216,
      "sourceRangeBytes": [
        16777216,
        20971520
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-20971520",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#20971520",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 20971520,
      "sourceRangeBytes": [
        20971520,
        25165824
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-25165824",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#25165824",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 25165824,
      "sourceRangeBytes": [
        25165824,
        29360128
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B0-29360128",
      "kind": "chunkKV",
      "key": "templates/B0/memfile#c#29360128",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B0",
      "sourceOffset": 29360128,
      "sourceRangeBytes": [
        29360128,
        33554432
      ],
      "mainKV": "KV-B0-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B1-0",
      "kind": "chunkKV",
      "key": "templates/B1/memfile#c#0",
      "valueKind": "raw data bytes",
      "valueBytes": 4194304,
      "sourceDataFile": "SF-B1",
      "sourceOffset": 0,
      "sourceRangeBytes": [
        0,
        4194304
      ],
      "mainKV": "KV-B1-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    },
    {
      "id": "CK-B1-4194304",
      "kind": "chunkKV",
      "key": "templates/B1/memfile#c#4194304",
      "valueKind": "raw data bytes",
      "valueBytes": 2097152,
      "sourceDataFile": "SF-B1",
      "sourceOffset": 4194304,
      "sourceRangeBytes": [
        4194304,
        6291456
      ],
      "mainKV": "KV-B1-main",
      "note": "suffix is decimal byte offset in source packed file, not a logical-image offset or chunk ordinal"
    }
  ],
  "relationships": [
    {
      "from": "LI-B1-memfile",
      "to": "H-B1",
      "relation": "uses logical-offset source mapping"
    },
    {
      "from": "H-B1",
      "to": "KV-B1-header",
      "relation": "serialized as raw main-key value"
    },
    {
      "from": "H-B1",
      "to": "SF-B0",
      "relation": "4 inherited extents totaling 24 MiB"
    },
    {
      "from": "H-B1",
      "to": "SF-B1",
      "relation": "2 new-data extents totaling 6 MiB"
    },
    {
      "from": "H-B1",
      "to": null,
      "relation": "one 2 MiB Nil/zero extent has no source data file or data KV"
    },
    {
      "from": "SF-B1",
      "to": "KV-B1-main",
      "relation": "main key stores size/chunk_size descriptor"
    },
    {
      "from": "KV-B1-main",
      "to": [
        "CK-B1-0",
        "CK-B1-4194304"
      ],
      "relation": "descriptor supplies length/chunk size; chunk keys are deterministically derived, not an explicit embedded key list"
    },
    {
      "from": "CK-B1-0",
      "to": "SF-B1",
      "relation": "raw source bytes [0, 4 MiB)"
    },
    {
      "from": "CK-B1-4194304",
      "to": "SF-B1",
      "relation": "raw source bytes [4, 6 MiB), short final chunk"
    }
  ],
  "readExamples": [
    {
      "logicalMiB": 6,
      "logicalOffset": 6291456,
      "requestedBytesForIllustration": 4096,
      "mappingId": "M2",
      "buildAlias": "B1",
      "sourceDataFile": "SF-B1",
      "sourceOffset": 2097152,
      "sourceOffsetMiB": 2,
      "mainKV": "KV-B1-main",
      "chunkKV": "CK-B1-0",
      "chunkKey": "templates/B1/memfile#c#0",
      "chunkStartInSourceBytes": 0,
      "offsetWithinChunk": 2097152,
      "storedChunkValueBytes": 4194304,
      "headerObject": "KV-B1-header",
      "zeroBytesSynthesizedByUpperLayer": false,
      "meaning": "resolved remote address only; local COW/cache or localDiff may satisfy the read before any remote data request"
    },
    {
      "logicalMiB": 20,
      "logicalOffset": 20971520,
      "requestedBytesForIllustration": 4096,
      "mappingId": "M6",
      "buildAlias": "B1",
      "sourceDataFile": "SF-B1",
      "sourceOffset": 4194304,
      "sourceOffsetMiB": 4,
      "mainKV": "KV-B1-main",
      "chunkKV": "CK-B1-4194304",
      "chunkKey": "templates/B1/memfile#c#4194304",
      "chunkStartInSourceBytes": 4194304,
      "offsetWithinChunk": 0,
      "storedChunkValueBytes": 2097152,
      "headerObject": "KV-B1-header",
      "zeroBytesSynthesizedByUpperLayer": false,
      "meaning": "resolved remote address only; local COW/cache or localDiff may satisfy the read before any remote data request"
    },
    {
      "logicalMiB": 12,
      "logicalOffset": 12582912,
      "requestedBytesForIllustration": 4096,
      "mappingId": "M4",
      "buildAlias": "Nil",
      "sourceDataFile": null,
      "sourceOffset": null,
      "mainKV": null,
      "chunkKV": null,
      "chunkKey": null,
      "offsetWithinChunk": null,
      "headerObject": "KV-B1-header",
      "zeroBytesSynthesizedByUpperLayer": true,
      "meaning": "no remote data key; Header must still exist/reach the reader; current build.File nil branch assumes initially zero destination"
    }
  ],
  "lengthsToKeepSeparate": [
    {
      "entity": "logical image / Header.Metadata.Size",
      "bytes": 33554432,
      "meaning": "addressable restored memfile range [0,32 MiB)"
    },
    {
      "entity": "B1 packed source data file / main descriptor.size",
      "bytes": 6291456,
      "meaning": "only this generation nonzero dirty payload"
    },
    {
      "entity": "B1 memfile main KV JSON value",
      "bytes": 37,
      "meaning": "descriptor text bytes, not data payload"
    },
    {
      "entity": "B1 memfile.header main KV raw value",
      "bytes": 344,
      "meaning": "serialized Header own size for this synthetic seven-record example"
    },
    {
      "entity": "B1 chunk #c#0 value",
      "bytes": 4194304,
      "meaning": "first source-data slice"
    },
    {
      "entity": "B1 chunk #c#4194304 value",
      "bytes": 2097152,
      "meaning": "short last source-data slice"
    }
  ],
  "readBoundaries": [
    "These three examples resolve addresses, not RPC logs, transfer bytes, timings or cache miss probabilities.",
    "Header/descriptor may already be cached; localDiff, mmap cache and NBD COW can avoid remote data reads. A zero extent avoids data KV reads but does not eliminate storage of the Header that describes the zero extent.",
    "Current mooncakeSeekable.ReadAt delegates to readAtSingleChunk; do not imply one call spans multiple chunks. The illustrative 4 KiB reads stay inside one mapping and one chunk.",
    "Header binary mappings, per-source JSON chunk descriptor and KAS metadata.json are distinct formats/objects.",
    "B1 data main key is a JSON descriptor; the small B1 memfile.header Blob main key is raw binary. A main key is not universally a descriptor."
  ],
  "validation": {
    "mappingCoverageBytes": 33554432,
    "inheritedBytes": 25165824,
    "newNonzeroDirtyBytes": 6291456,
    "zeroBytes": 2097152,
    "syntheticHeaderValueBytes": 344,
    "threeRequestedOffsetsMatchKASModel": true,
    "allSixteen2MiBMarkersMatchKASModel": true,
    "verification": "Arithmetic/serialization-size derivation only; product source read-only. No KAS workload, native storage write or runtime recovery test executed. Existing report mappingAt extracted without DOM/UI and compared at all sixteen 2 MiB markers.",
    "actualReportModelExecuted": true
  },
  "sourceEvidence": [
    {
      "path": "packages/shared/pkg/storage/header/mapping.go",
      "start": 14,
      "end": 20,
      "symbol": "BuildMap",
      "excerpt": "14: type BuildMap struct {\n15: \t// Offset defines which block of the current layer this mapping starts at\n16: \tOffset             uint64\n17: \tLength             uint64\n18: \tBuildId            uuid.UUID\n19: \tBuildStorageOffset uint64\n20: }"
    },
    {
      "path": "packages/shared/pkg/storage/header/mapping.go",
      "start": 31,
      "end": 75,
      "symbol": "CreateMapping",
      "excerpt": "31: func CreateMapping(\n32: \tbuildId *uuid.UUID,\n33: \tdirty *bitset.BitSet,\n34: \tblockSize int64,\n35: ) []*BuildMap {\n36: \tvar mappings []*BuildMap\n37: \n38: \tvar startBlock uint\n39: \tvar blockLength uint\n40: \tvar buildStorageOffset uint64\n41: \n42: \tfor blockIdx, e := dirty.NextSet(0); e; blockIdx, e = dirty.NextSet(blockIdx + 1) {\n43: \t\tif startBlock+blockLength == blockIdx {\n44: \t\t\tblockLength++\n45: \n46: \t\t\tcontinue\n47: \t\t}\n48: \n49: \t\tif blockLength > 0 {\n50: \t\t\tm := &BuildMap{\n51: \t\t\t\tOffset:             uint64(startBlock) * uint64(blockSize),\n52: \t\t\t\tBuildId:            *buildId,\n53: \t\t\t\tLength:             uint64(blockLength) * uint64(blockSize),\n54: \t\t\t\tBuildStorageOffset: buildStorageOffset,\n55: \t\t\t}\n56: \n57: \t\t\tmappings = append(mappings, m)\n58: \n59: \t\t\tbuildStorageOffset += m.Length\n60: \t\t}\n61: \n62: \t\tstartBlock = blockIdx\n63: \t\tblockLength = 1\n64: \t}\n65: \n66: \tif blockLength > 0 {\n67: \t\tmappings = append(mappings, &BuildMap{\n68: \t\t\tOffset:             uint64(startBlock) * uint64(blockSize),\n69: \t\t\tBuildId:            *buildId,\n70: \t\t\tLength:             uint64(blockLength) * uint64(blockSize),\n71: \t\t\tBuildStorageOffset: buildStorageOffset,\n72: \t\t})\n73: \t}\n74: \n75: \treturn mappings"
    },
    {
      "path": "packages/shared/pkg/storage/header/header.go",
      "start": 64,
      "end": 72,
      "symbol": "Header.GetShiftedMapping",
      "excerpt": "64: func (t *Header) GetShiftedMapping(ctx context.Context, offset int64) (mappedOffset int64, mappedLength int64, buildID *uuid.UUID, err error) {\n65: \tmapping, shift, err := t.getMapping(ctx, offset)\n66: \tif err != nil {\n67: \t\treturn 0, 0, nil, err\n68: \t}\n69: \n70: \tmappedOffset = int64(mapping.BuildStorageOffset) + shift\n71: \tmappedLength = int64(mapping.Length) - shift\n72: \tbuildID = &mapping.BuildId"
    },
    {
      "path": "packages/shared/pkg/storage/header/serialization.go",
      "start": 17,
      "end": 27,
      "symbol": "Metadata",
      "excerpt": "17: \n18: type Metadata struct {\n19: \tVersion    uint64\n20: \tBlockSize  uint64\n21: \tSize       uint64\n22: \tGeneration uint64\n23: \tBuildId    uuid.UUID\n24: \t// TODO: Use the base build id when setting up the snapshot rootfs\n25: \tBaseBuildId uuid.UUID\n26: }\n27: "
    },
    {
      "path": "packages/shared/pkg/storage/header/serialization.go",
      "start": 53,
      "end": 69,
      "symbol": "Serialize",
      "excerpt": "53: \terr := binary.Write(&buf, binary.LittleEndian, metadata)\n54: \tif err != nil {\n55: \t\treturn nil, fmt.Errorf(\"failed to write metadata: %w\", err)\n56: \t}\n57: \n58: \tfor _, mapping := range mappings {\n59: \t\terr := binary.Write(&buf, binary.LittleEndian, mapping)\n60: \t\tif err != nil {\n61: \t\t\treturn nil, fmt.Errorf(\"failed to write block mapping: %w\", err)\n62: \t\t}\n63: \t}\n64: \n65: \treturn buf.Bytes(), nil\n66: }\n67: \n68: func Deserialize(ctx context.Context, in storage.Blob) (*Header, error) {\n69: \tdata, err := storage.GetBlob(ctx, in)"
    },
    {
      "path": "packages/shared/pkg/storage/template.go",
      "start": 7,
      "end": 16,
      "symbol": "memfile / HeaderSuffix",
      "excerpt": "7: const (\n8: \tGuestEnvdPath = \"/usr/bin/envd\"\n9: \n10: \tMemfileName  = \"memfile\"\n11: \tRootfsName   = \"rootfs.ext4\"\n12: \tSnapfileName = \"snapfile\"\n13: \tMetadataName = \"metadata.json\"\n14: \n15: \tHeaderSuffix = \".header\"\n16: )"
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 1014,
      "end": 1019,
      "symbol": "mooncakeStorage.key",
      "excerpt": "1014: func (s *mooncakeStorage) key(path string) string {\n1015: \tif s.bucketName == \"\" {\n1016: \t\treturn path\n1017: \t}\n1018: \treturn s.bucketName + \"/\" + path\n1019: }"
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 1091,
      "end": 1095,
      "symbol": "Blob.Put small raw value",
      "excerpt": "1091: func (o *mooncakeBlob) Put(ctx context.Context, data []byte) error {\n1092: \tinvalidateCachedMetadata(o.path)\n1093: \tif len(data) <= MemoryChunkSize {\n1094: \t\treturn o.store.Put(o.path, data, nil)\n1095: \t}"
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 1389,
      "end": 1393,
      "symbol": "mooncakeObjectMeta",
      "excerpt": "1389: type mooncakeObjectMeta struct {\n1390: \tSize      int64 `json:\"size\"`\n1391: \tChunkSize int64 `json:\"chunk_size\"`\n1392: }\n1393: "
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 1520,
      "end": 1553,
      "symbol": "ReadAt / readAtSingleChunk",
      "excerpt": "1520: func (o *mooncakeSeekable) ReadAt(ctx context.Context, buff []byte, off int64) (int, error) {\n1521: \tmeta, err := o.getMeta(ctx)\n1522: \tif err != nil {\n1523: \t\treturn 0, err\n1524: \t}\n1525: \tif off >= meta.Size {\n1526: \t\treturn 0, io.EOF\n1527: \t}\n1528: \tif off < 0 {\n1529: \t\treturn 0, errors.New(\"negative offset\")\n1530: \t}\n1531: \n1532: \tremaining := min(int64(len(buff)), meta.Size-off)\n1533: \n1534: \treturn o.readAtSingleChunk(ctx, buff, off, remaining, meta)\n1535: }\n1536: \n1537: func (o *mooncakeSeekable) readAtSingleChunk(ctx context.Context, buff []byte, off int64, remaining int64, meta *mooncakeObjectMeta) (int, error) {\n1538: \tchunkOffset := (off / meta.ChunkSize) * meta.ChunkSize\n1539: \tchunkEnd := min(chunkOffset+meta.ChunkSize, meta.Size)\n1540: \tchunkSize := chunkEnd - chunkOffset\n1541: \n1542: \tstartInChunk := off - chunkOffset\n1543: \tdata, err := o.readChunk(ctx, chunkOffset, chunkSize, buff, startInChunk)\n1544: \tif err != nil {\n1545: \t\treturn 0, fmt.Errorf(\"failed to read chunk at %d: %w\", chunkOffset, err)\n1546: \t}\n1547: \n1548: \tcopied := min(len(data), int(remaining))\n1549: \n1550: \tif copied < len(buff) && off+int64(copied) >= meta.Size {\n1551: \t\treturn copied, io.EOF\n1552: \t}\n1553: \treturn copied, nil"
    },
    {
      "path": "packages/orchestrator/internal/sandbox/build/build.go",
      "start": 70,
      "end": 77,
      "symbol": "File.ReadAt Nil source skip",
      "excerpt": "70: \t\t// Skip reading when the uuid is nil.\n71: \t\t// We will use this to handle base builds that are already diffs.\n72: \t\t// The passed slice p must start as empty, otherwise we would need to copy the empty values there.\n73: \t\tif *buildID == uuid.Nil {\n74: \t\t\tn += int(readLength)\n75: \n76: \t\t\tcontinue\n77: \t\t}"
    },
    {
      "path": "packages/orchestrator/internal/sandbox/template_build.go",
      "start": 42,
      "end": 73,
      "symbol": "uploadMemfileHeader / uploadMemfile",
      "excerpt": "42: func (t *TemplateBuild) uploadMemfileHeader(ctx context.Context, h *headers.Header) error {\n43: \tobject, err := t.persistence.OpenBlob(ctx, t.files.StorageMemfileHeaderPath(), storage.MemfileHeaderObjectType)\n44: \tif err != nil {\n45: \t\treturn err\n46: \t}\n47: \n48: \tserialized, err := headers.Serialize(h.Metadata, h.Mapping)\n49: \tif err != nil {\n50: \t\treturn fmt.Errorf(\"error when serializing memfile header: %w\", err)\n51: \t}\n52: \n53: \terr = object.Put(ctx, serialized)\n54: \tif err != nil {\n55: \t\treturn fmt.Errorf(\"error when uploading memfile header: %w\", err)\n56: \t}\n57: \n58: \treturn nil\n59: }\n60: \n61: func (t *TemplateBuild) uploadMemfile(ctx context.Context, memfilePath string) error {\n62: \tobject, err := t.persistence.OpenSeekable(ctx, t.files.StorageMemfilePath(), storage.MemfileObjectType)\n63: \tif err != nil {\n64: \t\treturn err\n65: \t}\n66: \n67: \terr = object.StoreFile(ctx, memfilePath)\n68: \tif err != nil {\n69: \t\treturn fmt.Errorf(\"error when uploading memfile: %w\", err)\n70: \t}\n71: \n72: \treturn nil\n73: }"
    },
    {
      "path": "packages/shared/pkg/storage/storage.go",
      "start": 39,
      "end": 40,
      "symbol": "MemoryChunkSize",
      "excerpt": "39: \t// MemoryChunkSize must always be bigger or equal to the block size.\n40: \tMemoryChunkSize = 4 * 1024 * 1024 // 4 MB"
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 901,
      "end": 906,
      "symbol": "makeChunkKey",
      "excerpt": "901: func makeChunkKey(prefix string, offset int64) string {\n902: \tbuf := make([]byte, 0, len(prefix)+20)\n903: \tbuf = append(buf, prefix...)\n904: \tbuf = strconv.AppendInt(buf, offset, 10)\n905: \treturn string(buf)\n906: }"
    },
    {
      "path": "packages/shared/pkg/storage/storage_mooncake.go",
      "start": 1754,
      "end": 1793,
      "symbol": "mooncakeSeekable.StoreFile",
      "excerpt": "1754: \tfor offset := int64(0); offset < size; offset += MemoryChunkSize {\n1755: \t\toff := offset\n1756: \t\tg.Go(func() error {\n1757: \t\t\tif err := groupCtx.Err(); err != nil {\n1758: \t\t\t\treturn err\n1759: \t\t\t}\n1760: \n1761: \t\t\tend := min(off+MemoryChunkSize, size)\n1762: \t\t\tbuf, releaseBuf, err := getChunkBuffer(groupCtx)\n1763: \t\t\tif err != nil {\n1764: \t\t\t\treturn err\n1765: \t\t\t}\n1766: \t\t\tdefer releaseBuf()\n1767: \t\t\tchunk := buf[:int(end-off)]\n1768: \n1769: \t\t\tif _, err := file.ReadAt(chunk, off); err != nil && !errors.Is(err, io.EOF) {\n1770: \t\t\t\treturn fmt.Errorf(\"failed to read chunk at %d from %s: %w\", off, localPath, err)\n1771: \t\t\t}\n1772: \n1773: \t\t\tcChunk := C.CBytes(chunk)\n1774: \t\t\tdefer C.free(cChunk)\n1775: \t\t\treturn o.store.Put(o.chunkKey(off), unsafe.Slice((*byte)(cChunk), len(chunk)), nil)\n1776: \t\t})\n1777: \t}\n1778: \n1779: \tif err := g.Wait(); err != nil {\n1780: \t\treturn fmt.Errorf(\"failed to upload chunks: %w\", err)\n1781: \t}\n1782: \n1783: \t// Write metadata last so that readers see a consistent object only when fully written.\n1784: \tmeta := &mooncakeObjectMeta{Size: size, ChunkSize: MemoryChunkSize}\n1785: \tmetaBytes, err := json.Marshal(meta)\n1786: \tif err != nil {\n1787: \t\treturn fmt.Errorf(\"failed to marshal metadata: %w\", err)\n1788: \t}\n1789: \n1790: \tif err := o.store.Put(o.path, metaBytes, nil); err != nil {\n1791: \t\treturn fmt.Errorf(\"failed to write metadata: %w\", err)\n1792: \t}\n1793: \tstoreCachedMetadata(o.path, meta)"
    }
  ],
  "modelEvidence": {
    "path": "site/research/kasandbox-architecture-20261106/report.js",
    "sourceType": "current report demonstration, not repository runtime code",
    "excerpt": "const dirty=[2,3,10],zero=[6],M=1048576,B=4*M; mappingAt(x) maps dirty block i=x/2 to dirty.indexOf(i)*2 MiB; zero block 6 returns Nil."
  }
}
