Files
sow-tools/internal/nwsync/assemble.go
T
archvillainette 978e2913f6
ci / ci (pull_request) Successful in 3m28s
fix(nwsync): pin emitter version, ship the shim, harden manifest reads
Review follow-ups on the same work:

- emitter_version is its own sidecar field, not the build revision. Keying
  the merge check on created_with invalidated every published index on every
  unrelated crucible commit, forcing a re-emit of the whole corpus.
- flake.nix builds cmd/crucible-nwsync, so Nix consumers get the binary the
  docs promise.
- readManifest uses io.ReadFull: bytes.Reader.Read can return a short count
  with no error, so a truncated manifest parsed into zero-padded garbage.
- emit refuses a .mod outright and names why, rather than failing on an
  unknown extension.
- SOURCE_DATE_EPOCH pins the sidecar timestamp; the manifest was already
  deterministic.
- Entry.identity replaces the resref/restype key struct duplicated in emit
  and assemble.

Refs #53.
2026-07-29 12:17:08 +02:00

112 lines
3.5 KiB
Go

package nwsync
import (
"crypto/sha1"
"encoding/json"
"fmt"
"os"
"path/filepath"
)
// AssembleOptions describes one merged manifest.
type AssembleOptions struct {
Order []string // artifact names, highest priority first
EntriesDir string // directory holding <name>.nsym and <name>.nsym.json
OutDir string // repository root; the manifest lands in <out>/manifests
GroupID int // 1 = current, 2 = testing; 0 means absent
ModuleName string
Description string
}
// AssembleResult reports what one assemble run produced.
type AssembleResult struct {
SHA1 string
ManifestPath string
Entries int
}
// Assemble merges the per-artifact NSYM manifests named by Order into one
// manifest. It reads no bulk data at all — only the small index files.
//
// Merge rule is resref shadowing, not concatenation: a resref present in more
// than one artifact resolves to the earliest artifact in Order, which is how
// the game resolves it (upstream's resman adds haks in reverse and lets the
// last one win). Get this backwards and the wrong texture ships silently.
func Assemble(options AssembleOptions) (AssembleResult, error) {
if len(options.Order) == 0 {
return AssembleResult{}, fmt.Errorf("assemble: --order names no artifacts")
}
merged := make([]Entry, 0, 1024)
winner := make(map[Identity]bool, 1024)
var onDiskBytes int64
for _, name := range options.Order {
manifestPath := filepath.Join(options.EntriesDir, name+".nsym")
data, err := os.ReadFile(manifestPath)
if err != nil {
return AssembleResult{}, fmt.Errorf("assemble: no index for %q: %w", name, err)
}
entries, err := readManifest(data)
if err != nil {
return AssembleResult{}, fmt.Errorf("%s: %w", manifestPath, err)
}
sidecar, err := readSidecar(manifestPath + ".json")
if err != nil {
return AssembleResult{}, err
}
// Two producers of blobs means a skewed emitter can write blobs the
// merged manifest quietly disagrees with. Refuse to merge across
// mismatched emitter versions.
if sidecar.EmitterVersion != emitterVersion {
return AssembleResult{}, fmt.Errorf(
"assemble: emitter version mismatch: %s was emitted by emitter %q, this is emitter %q",
name, sidecar.EmitterVersion, emitterVersion)
}
// on_disk_bytes overcounts by the handful of cross-artifact
// duplicates. It is a display statistic; no dedupe pass for it.
onDiskBytes += sidecar.OnDiskBytes
for _, entry := range entries {
identity := entry.identity()
if winner[identity] {
continue
}
winner[identity] = true
merged = append(merged, entry)
}
}
if len(merged) == 0 {
return AssembleResult{}, fmt.Errorf("assemble: merged manifest is empty")
}
data, err := writeManifest(merged)
if err != nil {
return AssembleResult{}, err
}
sha1Hex := fmt.Sprintf("%x", sha1.Sum(data))
manifestPath := filepath.Join(options.OutDir, "manifests", sha1Hex)
sidecar := Sidecar{
ModuleName: options.ModuleName,
Description: options.Description,
GroupID: options.GroupID,
}
if err := writeManifestPair(manifestPath, data, merged, onDiskBytes, sidecar); err != nil {
return AssembleResult{}, err
}
return AssembleResult{SHA1: sha1Hex, ManifestPath: manifestPath, Entries: len(merged)}, nil
}
func readSidecar(path string) (Sidecar, error) {
data, err := os.ReadFile(path)
if err != nil {
return Sidecar{}, fmt.Errorf("assemble: missing sidecar: %w", err)
}
var sidecar Sidecar
if err := json.Unmarshal(data, &sidecar); err != nil {
return Sidecar{}, fmt.Errorf("%s: %w", path, err)
}
return sidecar, nil
}