Files
w3cjandClaude Opus 5.5 299ddb1476 refactor(state): image records in images/<key>/, rebuild counters per backend and --from, tool image records, cache/
- Image records are images/<key>/record.json (JSON, unknown fields
  ignored; name, chain, from, digest, libc, built). state.Images lists
  the folders; AnyImage needs a record.json; RemoveImage deletes the
  folder. Lima's cache cleaner reads records through state.Images.
- Rebuild counters are images/rebuilds.json, keyed by (backend, from as
  given, name), changed under images/locks/rebuilds. image.Inputs takes
  them through an image.Rebuilds interface. A rebuild on one backend no
  longer outdates the others' images, and two --from references that
  wrote the same counter name (node:24, node-24) stay apart.
  paths.RebuildFile, RebuildName and unsafeName are gone.
- Tool images: <file>.sha256, .target and .idle become one <file>.json
  (sha256, libc, format, backend, built, idle_since; state.ToolImage).
  BACKEND and LIBC read the record; the fallbacks (format matching,
  a VM's target, the current target) go, with toolTargets, vmToolTargets,
  formatBackend and gc's backendTools (toolsOn does it).
  Share.IdleFor owns reading idle_since.
- cache/ (Dirs.Cache) holds mise/, nix-cache/ and from-digests.json.
  With the cache gone, from.Recorded takes the newest base record's
  digest for the reference, so no key changes.
- Key check: the rebuild counter is the one input that moved and changes
  keys (deliberately: per backend and exact from). The --from digest
  moved to cache/ and stays an input, kept stable by the records'
  fallback. Records, mise and the Nix cache are outputs.
- Tests: state's image, rebuild, digest and tool record tests; image key
  cases per backend and exact from; build.TestRebuildIsPerBackend;
  from.TestRecordedWithoutTheCache; cli.TestToolsOn; rebuild.txtar
  (Docker's and smolvm's rebuilds keep Lima's and the other --from's
  bases), gc_caches.txtar and from.txtar (a missing cache/); the
  testscript command json_set; e2e reads the JSON records.
- Docs: space, images, settings (retention), internals images,
  overview, network, security, contributing (json_set).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-03 07:37:36 -07:00

36 lines
958 B
Go

package state
import (
"bytes"
"encoding/json"
"os"
)
// readJSON reads a JSON file only devvm writes into v. Fields v doesn't
// have are ignored, so a newer devvm's additions read; a file that isn't
// JSON is a CorruptError naming it. A missing file is an error that
// matches fs.ErrNotExist.
func readJSON(path string, v any) error {
data, err := os.ReadFile(path) //nolint:gosec // paths come from paths.Dirs
if err != nil {
return err
}
if err := json.Unmarshal(data, v); err != nil {
return &CorruptError{Path: path, Reason: "not JSON devvm reads: " + err.Error()}
}
return nil
}
// writeJSON writes v to path as indented JSON, one field per line, as
// writeFile does: atomically, the user's alone.
func writeJSON(path string, v any) error {
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetEscapeHTML(false)
enc.SetIndent("", " ")
if err := enc.Encode(v); err != nil {
return err
}
return writeFile(path, b.Bytes())
}