Skip to content

An open-source, faster Devin alternative

TypeScript

bundle: content-address the blob (sha256 of the .tar.gz), delete the fileset digest

Igor Zalutski6c2db48c3a5c
diff --git a/cmd/oc/internal/bundle/bundle.go b/cmd/oc/internal/bundle/bundle.go
new file mode 100644
index 00000000..1c07fc1e
--- /dev/null
+++ b/cmd/oc/internal/bundle/bundle.go
@@ -0,0 +1,88 @@
+// Package bundle packs a fileset into a .tar.gz and content-addresses it.
+//
+// The content address is the STANDARD blob digest — sha256 of the packed bytes
+// (as OCI addresses blobs), NOT a digest of a canonical serialization of the
+// fileset. That distinction is the whole point: the CLI, the host, and the box
+// all hash the SAME uploaded object (the CLI hashes what it uploads; the server
+// hashes the object it received; the box hashes the object it fetched), so no
+// party ever recomputes an address from scratch. There is therefore no canonical
+// form to reproduce across languages — sha256 is sha256 everywhere — and no
+// hand-rolled tar codec or JSON escaper. The tar is plain transport (stdlib
+// archive/tar); it need not be canonical because its bytes are not the address.
+package bundle
+
+import (
+ "archive/tar"
+ "bytes"
+ "compress/gzip"
+ "crypto/sha256"
+ "encoding/hex"
+ "time"
+)
+
+// File is one entry in a bundle. Mode is normalized to 0o644 or 0o755.
+type File struct {
+ Path string // bundle-root-relative, forward-slash separated
+ Mode int // 0o644 | 0o755
+ Content []byte
+}
+
+// NormalizeMode collapses an on-disk mode to the two the bundle allows: 0o755 if
+// any execute bit is set, else 0o644.
+func NormalizeMode(mode int) int {
+ if mode&0o111 != 0 {
+ return 0o755
+ }
+ return 0o644
+}
+
+// Digest is the content address of the packed bundle: "sha256:" + sha256(blob).
+// The caller hashes exactly the bytes it uploads; the server and box verify by
+// hashing exactly the bytes they hold — no reproduction, no lockstep.
+func Digest(blob []byte) string {
+ sum := sha256.Sum256(blob)
+ return "sha256:" + hex.EncodeToString(sum[:])
+}
+
+// Pack writes the fileset to a gzip-compressed tar with the standard library.
+// Entries are sorted and mtime is pinned to the epoch so the output is stable
+// for identical input (dedup-friendly), but nothing depends on the exact bytes —
+// the digest is over whatever Pack produces, and the box unpacks with system tar.
+func Pack(files []File) ([]byte, error) {
+ sorted := append([]File(nil), files...)
+ sortByPath(sorted)
+
+ var buf bytes.Buffer
+ zw := gzip.NewWriter(&buf)
+ tw := tar.NewWriter(zw)
+ for _, f := range sorted {
+ if err := tw.WriteHeader(&tar.Header{
+ Name: f.Path,
+ Mode: int64(f.Mode),
+ Size: int64(len(f.Content)),
+ Typeflag: tar.TypeReg,
+ ModTime: time.Unix(0, 0),
+ }); err != nil {
+ return nil, err
+ }
+ if _, err := tw.Write(f.Content); err != nil {
+ return nil, err
+ }
+ }
+ if err := tw.Close(); err != nil {
+ return nil, err
+ }
+ if err := zw.Close(); err != nil {
+ return nil, err
+ }
+ return buf.Bytes(), nil
+}
+
+// sortByPath orders entries by path bytes (insertion sort; a bundle is small).
+func sortByPath(files []File) {
+ for i := 1; i < len(files); i++ {
+ for j := i; j > 0 && files[j-1].Path > files[j].Path; j-- {
+ files[j-1], files[j] = files[j], files[j-1]
+ }
+ }
+}
diff --git a/cmd/oc/internal/bundle/bundle_test.go b/cmd/oc/internal/bundle/bundle_test.go
new file mode 100644
index 00000000..506ef426
--- /dev/null
+++ b/cmd/oc/internal/bundle/bundle_test.go
@@ -0,0 +1,125 @@
+package bundle
+
+import (
+ "archive/tar"
+ "bytes"
+ "compress/gzip"
+ "io"
+ "strings"
+ "testing"
+)
+
+var files = []File{
+ {Path: "artifact.json", Mode: 0o644, Content: []byte(`{"entry":"oc.js"}`)},
+ {Path: "oc.js", Mode: 0o644, Content: []byte("export const x = 1;\n")},
+ {Path: "skills/triage/SKILL.md", Mode: 0o644, Content: []byte("---\nname: triage\n---\n")},
+ {Path: "bin/run.sh", Mode: 0o755, Content: []byte("#!/bin/sh\necho hi\n")},
+}
+
+// TestDigestIsBlobSha256 pins the contract: the address is sha256 of the packed
+// bytes, so it is exactly what any party recomputes by hashing the same object.
+func TestDigestIsBlobSha256(t *testing.T) {
+ blob, err := Pack(files)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := Digest(blob); got != Digest(blob) || !strings.HasPrefix(got, "sha256:") || len(got) != len("sha256:")+64 {
+ t.Fatalf("unexpected digest shape: %s", got)
+ }
+ // Re-hashing the identical bytes (as the server/box do) yields the same value.
+ same := Digest(append([]byte(nil), blob...))
+ if same != Digest(blob) {
+ t.Fatalf("digest not a pure function of the bytes: %s vs %s", same, Digest(blob))
+ }
+}
+
+// TestPackDeterministic keeps identical input -> identical bytes (skill dedup).
+func TestPackDeterministic(t *testing.T) {
+ a, err := Pack(files)
+ if err != nil {
+ t.Fatal(err)
+ }
+ // Input order must not matter (Pack sorts).
+ rev := make([]File, len(files))
+ for i, f := range files {
+ rev[len(files)-1-i] = f
+ }
+ b, err := Pack(rev)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(a, b) {
+ t.Fatal("Pack is not deterministic across input order")
+ }
+ if Digest(a) != Digest(b) {
+ t.Fatal("digest depends on input order")
+ }
+}
+
+// TestPackRoundTrip unpacks with a standard tar reader (what system tar / a real
+// TS reader does) and checks the fileset survives, including a long path the old
+// hand-rolled ustar codec had to special-case.
+func TestPackRoundTrip(t *testing.T) {
+ long := "skills/" + strings.Repeat("a/", 60) + "SKILL.md" // ~130 bytes
+ in := append(append([]File(nil), files...), File{Path: long, Mode: 0o644, Content: []byte("x")})
+ blob, err := Pack(in)
+ if err != nil {
+ t.Fatal(err)
+ }
+ out, err := readTarGz(blob)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(out) != len(in) {
+ t.Fatalf("unpacked %d, want %d", len(out), len(in))
+ }
+ byPath := map[string]File{}
+ for _, f := range out {
+ byPath[f.Path] = f
+ }
+ for _, want := range in {
+ got, ok := byPath[want.Path]
+ if !ok {
+ t.Fatalf("missing %q after round trip", want.Path)
+ }
+ if got.Mode != want.Mode || !bytes.Equal(got.Content, want.Content) {
+ t.Errorf("%q changed: mode %o/%o", want.Path, got.Mode, want.Mode)
+ }
+ }
+}
+
+func TestNormalizeMode(t *testing.T) {
+ cases := map[int]int{0o644: 0o644, 0o600: 0o644, 0o755: 0o755, 0o700: 0o755, 0o777: 0o755}
+ for in, want := range cases {
+ if got := NormalizeMode(in); got != want {
+ t.Errorf("NormalizeMode(%o) = %o, want %o", in, got, want)
+ }
+ }
+}
+
+func readTarGz(tarGz []byte) ([]File, error) {
+ zr, err := gzip.NewReader(bytes.NewReader(tarGz))
+ if err != nil {
+ return nil, err
+ }
+ tr := tar.NewReader(zr)
+ var out []File
+ for {
+ hdr, err := tr.Next()
+ if err == io.EOF {
+ break
+ }
+ if err != nil {
+ return nil, err
+ }
+ if hdr.Typeflag != tar.TypeReg {
+ continue
+ }
+ content, err := io.ReadAll(tr)
+ if err != nil {
+ return nil, err
+ }
+ out = append(out, File{Path: hdr.Name, Mode: int(hdr.Mode) & 0o7777, Content: content})
+ }
+ return out, nil
+}
diff --git a/cmd/oc/internal/commands/agent_deploy_flue.go b/cmd/oc/internal/commands/agent_deploy_flue.go
index 94e12a07..398e474a 100644
--- a/cmd/oc/internal/commands/agent_deploy_flue.go
+++ b/cmd/oc/internal/commands/agent_deploy_flue.go
@@ -21,7 +21,7 @@ import (
"github.com/opensandbox/opensandbox/cmd/oc/internal/client"
"github.com/opensandbox/opensandbox/cmd/oc/internal/credscan"
- "github.com/opensandbox/opensandbox/cmd/oc/internal/filesetdigest"
+ "github.com/opensandbox/opensandbox/cmd/oc/internal/bundle"
"github.com/spf13/cobra"
)
@@ -69,17 +69,18 @@ func deployFlue(cmd *cobra.Command, sc *client.Client, dir string, m *manifest,
return err
}
- // 4. Hash dist-oc/ into the content-addressed digest + canonical tar.gz.
+ // 4. Pack dist-oc/ into the tar.gz and content-address it: the digest is
+ // sha256 of the blob the server and box will hash byte-for-byte.
outDir := filepath.Join(dir, flueBuildOutputDir)
files, err := readArtifactFiles(outDir)
if err != nil {
return err
}
- digest := filesetdigest.Digest(files)
- tarGz, err := filesetdigest.TarGz(files)
+ tarGz, err := bundle.Pack(files)
if err != nil {
return fmt.Errorf("pack artifact: %w", err)
}
+ digest := bundle.Digest(tarGz)
if len(tarGz) > flueArtifactMaxBytes {
return fmt.Errorf("artifact is %d bytes, over the %d MiB limit", len(tarGz), flueArtifactMaxBytes>>20)
}
@@ -172,11 +173,11 @@ func runFlueBuild(ctx context.Context, dir string) error {
// readArtifactFiles walks the build output into a fileset with normalized modes,
// requiring artifact.json (the manifest the host validates + pins).
-func readArtifactFiles(outDir string) ([]filesetdigest.File, error) {
+func readArtifactFiles(outDir string) ([]bundle.File, error) {
if info, err := os.Stat(outDir); err != nil || !info.IsDir() {
return nil, fmt.Errorf("build output %s not found — did oc-flue-build run?", outDir)
}
- var files []filesetdigest.File
+ var files []bundle.File
hasManifest := false
err := filepath.WalkDir(outDir, func(p string, d fs.DirEntry, walkErr error) error {
if walkErr != nil {
@@ -201,9 +202,9 @@ func readArtifactFiles(outDir string) ([]filesetdigest.File, error) {
if rel == "artifact.json" {
hasManifest = true
}
- files = append(files, filesetdigest.File{
+ files = append(files, bundle.File{
Path: rel,
- Mode: filesetdigest.NormalizeMode(int(st.Mode().Perm())),
+ Mode: bundle.NormalizeMode(int(st.Mode().Perm())),
Content: content,
})
return nil
diff --git a/cmd/oc/internal/commands/agent_deploy_flue_test.go b/cmd/oc/internal/commands/agent_deploy_flue_test.go
index 3dd407c4..41ca04bf 100644
--- a/cmd/oc/internal/commands/agent_deploy_flue_test.go
+++ b/cmd/oc/internal/commands/agent_deploy_flue_test.go
@@ -22,7 +22,7 @@ import (
"testing"
"github.com/opensandbox/opensandbox/cmd/oc/internal/client"
- "github.com/opensandbox/opensandbox/cmd/oc/internal/filesetdigest"
+ "github.com/opensandbox/opensandbox/cmd/oc/internal/bundle"
"github.com/opensandbox/opensandbox/cmd/oc/internal/output"
"github.com/spf13/cobra"
)
@@ -107,15 +107,15 @@ func TestDeployFlueEndToEnd(t *testing.T) {
writeFile(t, filepath.Join(dir, "node_modules", ".bin", "oc-flue-build"), buildScript, 0o755)
// What the CLI must produce from that dist-oc/ (mode normalized to 0644).
- expectedFiles := []filesetdigest.File{
+ expectedFiles := []bundle.File{
{Path: "artifact.json", Mode: 0o644, Content: []byte(e2eArtifactBody + "\n")},
{Path: "oc.js", Mode: 0o644, Content: []byte(e2eOcBody + "\n")},
}
- expectedDigest := filesetdigest.Digest(expectedFiles)
- expectedTarGz, err := filesetdigest.TarGz(expectedFiles)
+ expectedTarGz, err := bundle.Pack(expectedFiles)
if err != nil {
t.Fatal(err)
}
+ expectedDigest := bundle.Digest(expectedTarGz)
// Fake control plane.
f := &fakeCP{}
diff --git a/cmd/oc/internal/filesetdigest/filesetdigest.go b/cmd/oc/internal/filesetdigest/filesetdigest.go
deleted file mode 100644
index b9670f04..00000000
--- a/cmd/oc/internal/filesetdigest/filesetdigest.go
+++ /dev/null
@@ -1,90 +0,0 @@
-// Package filesetdigest is the Go implementation of the OpenComputer bundle
-// fileset digest — the content-addressing scheme shared by skill bundles and
-// Flue framework artifacts.
-//
-// This is one of three implementations of a single contract (flue-slice.md
-// contract 4). It MUST stay byte-identical to:
-// - TS host: sessions-api/src/v3/core/skill-bundle-canonical.ts (filesetDigest)
-// - TS box: oc-runtimes/adapter-core/src/skills.ts (filesetDigest)
-//
-// A shared golden vector (a fixed fileset -> a fixed digest) locks the three in
-// lockstep; see testdata/golden-fileset.json + filesetdigest_test.go.
-//
-// The digest hashes a length-prefixed binary framing, NOT a serialization
-// format. Reproducing it in another language is three primitives with no edge
-// cases — sort paths by UTF-8 bytes, big-endian uint32, sha256 of raw bytes —
-// so there is nothing to escape and no JSON.stringify dialect to match. (An
-// earlier scheme hashed JSON.stringify output, which forced a hand-rolled
-// ECMA-262 string escaper here to match JavaScript; that is what this replaces.)
-//
-// Pre-image:
-//
-// entries sorted by path, compared as raw UTF-8 bytes
-// buf = u32be(len(entries))
-// ‖ for each entry: sha256(content)[32] ‖ u32be(mode) ‖ u32be(len(path)) ‖ path_utf8
-// digest = "sha256:" + lowerhex(sha256(buf))
-package filesetdigest
-
-import (
- "crypto/sha256"
- "encoding/binary"
- "encoding/hex"
- "sort"
-)
-
-// DigestScheme is the hash-function prefix on every digest ("sha256:<hex>").
-// The hash is over the canonical framing below, not the raw file bytes — the
-// same "sha256: means sha256 of our canonical form" convention as OCI descriptors.
-const DigestScheme = "sha256:"
-
-// File is one entry in a bundle fileset. Mode is the POSIX file mode, normalized
-// to 0o644 or 0o755 by the caller (NormalizeMode) — the digest and the tar must
-// use the same value.
-type File struct {
- Path string // bundle-root-relative, forward-slash separated (e.g. "skills/triage/SKILL.md")
- Mode int // 0o644 | 0o755
- Content []byte
-}
-
-// NormalizeMode collapses an on-disk mode to the two the contract allows:
-// 0o755 if any execute bit is set, else 0o644. Matches the CLI/worker skill
-// reader convention (agent.go readSkills).
-func NormalizeMode(mode int) int {
- if mode&0o111 != 0 {
- return 0o755
- }
- return 0o644
-}
-
-// Digest returns the fileset digest ("ocfs1:" + hex). Byte-for-byte identical to
-// the TS filesetDigest for the same fileset.
-func Digest(files []File) string {
- type entry struct {
- path string
- mode int
- sum [32]byte
- }
- entries := make([]entry, len(files))
- for i, f := range files {
- entries[i] = entry{path: f.Path, mode: f.Mode, sum: sha256.Sum256(f.Content)}
- }
- // Bytewise sort by path. Go string comparison is over the raw bytes, matching
- // Node's Buffer.compare(Buffer.from(path,"utf8"), …) — NOT String.localeCompare
- // or a default JS Array.sort (which orders by UTF-16 code units).
- sort.Slice(entries, func(i, j int) bool { return entries[i].path < entries[j].path })
-
- h := sha256.New()
- var u32 [4]byte
- binary.BigEndian.PutUint32(u32[:], uint32(len(entries)))
- h.Write(u32[:])
- for _, e := range entries {
- h.Write(e.sum[:])
- binary.BigEndian.PutUint32(u32[:], uint32(e.mode))
- h.Write(u32[:])
- path := []byte(e.path)
- binary.BigEndian.PutUint32(u32[:], uint32(len(path)))
- h.Write(u32[:])
- h.Write(path)
- }
- return DigestScheme + hex.EncodeToString(h.Sum(nil))
-}
diff --git a/cmd/oc/internal/filesetdigest/filesetdigest_test.go b/cmd/oc/internal/filesetdigest/filesetdigest_test.go
deleted file mode 100644
index 81863318..00000000
--- a/cmd/oc/internal/filesetdigest/filesetdigest_test.go
+++ /dev/null
@@ -1,154 +0,0 @@
-package filesetdigest
-
-import (
- "archive/tar"
- "bytes"
- "compress/gzip"
- "crypto/sha256"
- "encoding/base64"
- "encoding/hex"
- "io"
- "strings"
- "testing"
-)
-
-// goldenFiles is the SHARED cross-repo golden vector (flue-slice.md contract 4).
-// The same fileset must produce goldenDigest in all three implementations:
-// this package, oc-runtimes/adapter-core, and sessions-api. It includes a real
-// binary file (logo.png) so a utf8 round-trip in ANY impl would change the
-// digest. Kept byte-identical to testdata/golden-fileset.json.
-var goldenFiles = []File{
- {Path: "artifact.json", Mode: 0o644, Content: []byte("{\"entry\":\"oc.js\",\"profile_version\":1}\n")},
- {Path: "oc.js", Mode: 0o644, Content: []byte("export const x = 1;\n")},
- {Path: "skills/triage/SKILL.md", Mode: 0o644, Content: []byte("---\nname: triage\n---\nhello\n")},
- {Path: "skills/triage/logo.png", Mode: 0o644, Content: mustB64("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==")},
- {Path: "bin/run.sh", Mode: 0o755, Content: []byte("#!/bin/sh\necho hi\n")},
-}
-
-func mustB64(s string) []byte {
- b, err := base64.StdEncoding.DecodeString(s)
- if err != nil {
- panic(err)
- }
- return b
-}
-
-// The ocfs1 fileset digest of goldenFiles. Regenerated across all three repos
-// whenever the algorithm changes (it must not, without a scheme bump).
-const goldenDigest = "sha256:cab805ea6500039cdd09da801f90b1d6aff177710f106acc647a875c2dcdceea"
-
-func TestGoldenDigest(t *testing.T) {
- if got := Digest(goldenFiles); got != goldenDigest {
- t.Fatalf("digest mismatch\n got: %s\nwant: %s", got, goldenDigest)
- }
-}
-
-func TestDigestIndependentOfInputOrder(t *testing.T) {
- // Reversed input must yield the same digest (entries are bytewise-sorted).
- rev := make([]File, len(goldenFiles))
- for i, f := range goldenFiles {
- rev[len(goldenFiles)-1-i] = f
- }
- if got := Digest(rev); got != Digest(goldenFiles) {
- t.Fatalf("digest is order-dependent: got %s want %s", got, Digest(goldenFiles))
- }
-}
-
-// TestTarGzRoundTrip reproduces the server/box verify path: pack the fileset,
-// unpack it with a standard tar reader, and recompute the digest from the
-// unpacked bytes. It must equal Digest(files) — the check the deploy-verify
-// executor and the box materializer both perform before pinning.
-func TestTarGzRoundTrip(t *testing.T) {
- tarGz, err := TarGz(goldenFiles)
- if err != nil {
- t.Fatalf("TarGz: %v", err)
- }
- unpacked, err := readTarGz(tarGz)
- if err != nil {
- t.Fatalf("read: %v", err)
- }
- if len(unpacked) != len(goldenFiles) {
- t.Fatalf("unpacked %d files, want %d", len(unpacked), len(goldenFiles))
- }
- if got := Digest(unpacked); got != Digest(goldenFiles) {
- t.Fatalf("round-trip digest mismatch: got %s want %s", got, Digest(goldenFiles))
- }
- byPath := map[string]File{}
- for _, f := range unpacked {
- byPath[f.Path] = f
- }
- for _, want := range goldenFiles {
- got, ok := byPath[want.Path]
- if !ok {
- t.Fatalf("missing %q after round trip", want.Path)
- }
- if got.Mode != want.Mode {
- t.Errorf("%q mode = %o, want %o", want.Path, got.Mode, want.Mode)
- }
- if !bytes.Equal(got.Content, want.Content) {
- t.Errorf("%q content changed", want.Path)
- }
- }
-}
-
-// TestTarGzLongPath is the case the old hand-rolled ustar codec had to special-
-// case (name/prefix split, "path too long" errors): a path over 100 bytes must
-// round-trip cleanly through the standard tar codec.
-func TestTarGzLongPath(t *testing.T) {
- long := "skills/" + strings.Repeat("a/", 60) + "SKILL.md" // ~130 bytes
- files := []File{{Path: long, Mode: 0o644, Content: []byte("x")}}
- tarGz, err := TarGz(files)
- if err != nil {
- t.Fatalf("TarGz long path: %v", err)
- }
- unpacked, err := readTarGz(tarGz)
- if err != nil {
- t.Fatalf("read long path: %v", err)
- }
- if len(unpacked) != 1 || unpacked[0].Path != long {
- t.Fatalf("long path did not round-trip: %+v", unpacked)
- }
-}
-
-func TestNormalizeMode(t *testing.T) {
- cases := map[int]int{0o644: 0o644, 0o600: 0o644, 0o755: 0o755, 0o700: 0o755, 0o777: 0o755}
- for in, want := range cases {
- if got := NormalizeMode(in); got != want {
- t.Errorf("NormalizeMode(%o) = %o, want %o", in, got, want)
- }
- }
-}
-
-func readTarGz(tarGz []byte) ([]File, error) {
- zr, err := gzip.NewReader(bytes.NewReader(tarGz))
- if err != nil {
- return nil, err
- }
- tr := tar.NewReader(zr)
- var out []File
- for {
- hdr, err := tr.Next()
- if err == io.EOF {
- break
- }
- if err != nil {
- return nil, err
- }
- if hdr.Typeflag != tar.TypeReg {
- continue
- }
- content, err := io.ReadAll(tr)
- if err != nil {
- return nil, err
- }
- out = append(out, File{Path: hdr.Name, Mode: int(hdr.Mode) & 0o7777, Content: content})
- }
- return out, nil
-}
-
-func TestContentShaSanity(t *testing.T) {
- sum := sha256.Sum256([]byte("hello"))
- if hex.EncodeToString(sum[:]) != "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" {
- t.Fatal("sha256 sanity failed")
- }
-}
diff --git a/cmd/oc/internal/filesetdigest/fixture_test.go b/cmd/oc/internal/filesetdigest/fixture_test.go
deleted file mode 100644
index 36862e66..00000000
--- a/cmd/oc/internal/filesetdigest/fixture_test.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package filesetdigest
-
-import (
- "encoding/base64"
- "encoding/json"
- "os"
- "testing"
-)
-
-// goldenFixture mirrors testdata/golden-fileset.json — the SHARED cross-language
-// golden vector for contract 4. The same file is consumed by the sessions-api and
-// oc-runtimes test suites; filesetDigest(files) must equal `digest` in all three.
-// Regenerate with scratch gen-fixture.mjs (reference JS) if the vector changes.
-type goldenFixture struct {
- Digest string `json:"digest"`
- Files []struct {
- Path string `json:"path"`
- Mode int `json:"mode"`
- ContentBase64 string `json:"contentBase64"`
- } `json:"files"`
-}
-
-func loadFixture(t *testing.T) goldenFixture {
- t.Helper()
- raw, err := os.ReadFile("testdata/golden-fileset.json")
- if err != nil {
- t.Fatalf("read fixture: %v", err)
- }
- var fx goldenFixture
- if err := json.Unmarshal(raw, &fx); err != nil {
- t.Fatalf("parse fixture: %v", err)
- }
- return fx
-}
-
-// TestGoldenFixtureDigest is the C4 lockstep assertion: our Digest over the
-// fixture's fileset equals the fixture's digest (the value the TS suites also
-// assert). Also cross-checks the in-code golden constant against the file.
-func TestGoldenFixtureDigest(t *testing.T) {
- fx := loadFixture(t)
- if fx.Digest != goldenDigest {
- t.Fatalf("fixture digest %s != in-code goldenDigest %s", fx.Digest, goldenDigest)
- }
- files := make([]File, len(fx.Files))
- for i, f := range fx.Files {
- content, err := base64.StdEncoding.DecodeString(f.ContentBase64)
- if err != nil {
- t.Fatalf("decode %q content: %v", f.Path, err)
- }
- files[i] = File{Path: f.Path, Mode: f.Mode, Content: content}
- }
- if got := Digest(files); got != fx.Digest {
- t.Fatalf("Digest(fixture files) = %s, want %s", got, fx.Digest)
- }
-}
diff --git a/cmd/oc/internal/filesetdigest/tar.go b/cmd/oc/internal/filesetdigest/tar.go
deleted file mode 100644
index f64b60dd..00000000
--- a/cmd/oc/internal/filesetdigest/tar.go
+++ /dev/null
@@ -1,45 +0,0 @@
-package filesetdigest
-
-import (
- "archive/tar"
- "bytes"
- "compress/gzip"
- "time"
-)
-
-// TarGz packs a fileset into a gzip-compressed tar for upload. The tar is pure
-// TRANSPORT — the content address is the fileset Digest, which the server and
-// box re-derive from the UNPACKED bytes, so the tar does NOT need to be
-// canonical or byte-reproducible. We therefore use the standard library
-// (archive/tar) on both ends instead of a hand-rolled minimal ustar codec: the
-// reader handles whatever framing the writer emits (ustar or PAX for long
-// paths), so there is no matched-writer/reader pair to keep in lockstep.
-//
-// ModTime is pinned to the epoch so repeated packs of the same input are stable
-// (nice for caching/diffing), but nothing depends on it.
-func TarGz(files []File) ([]byte, error) {
- var buf bytes.Buffer
- zw := gzip.NewWriter(&buf)
- tw := tar.NewWriter(zw)
- for _, f := range files {
- if err := tw.WriteHeader(&tar.Header{
- Name: f.Path,
- Mode: int64(f.Mode),
- Size: int64(len(f.Content)),
- Typeflag: tar.TypeReg,
- ModTime: time.Unix(0, 0),
- }); err != nil {
- return nil, err
- }
- if _, err := tw.Write(f.Content); err != nil {
- return nil, err
- }
- }
- if err := tw.Close(); err != nil {
- return nil, err
- }
- if err := zw.Close(); err != nil {
- return nil, err
- }
- return buf.Bytes(), nil
-}
diff --git a/cmd/oc/internal/filesetdigest/testdata/golden-fileset.json b/cmd/oc/internal/filesetdigest/testdata/golden-fileset.json
deleted file mode 100644
index 97a35569..00000000
--- a/cmd/oc/internal/filesetdigest/testdata/golden-fileset.json
+++ /dev/null
@@ -1,31 +0,0 @@
-{
- "note": "Shared golden vector for the OpenComputer bundle fileset digest (flue-slice.md contract 4, sha256 scheme). The fileset digest over RAW file bytes MUST equal `digest` in all implementations: cmd/oc filesetdigest (Go), sessions-api skill-bundle-canonical.ts (filesetDigest), oc-runtimes adapter-core skills.ts. Pre-image = u32be(count) then per entry (sorted by UTF-8 path bytes) sha256(content)[32] ‖ u32be(mode) ‖ u32be(len(path)) ‖ path_utf8; digest = 'sha256:' + hex(sha256(preimage)). `mode` is decimal (0o644=420, 0o755=493). `contentBase64` is base64 of the RAW file bytes — the fileset INCLUDES a binary PNG (skills/triage/logo.png), so any impl that decodes to a utf8 string before hashing diverges. Transport is a plain (non-canonical) tar.gz — the digest is over the unpacked fileset, so no tar bytes are pinned here.",
- "digest": "sha256:cab805ea6500039cdd09da801f90b1d6aff177710f106acc647a875c2dcdceea",
- "files": [
- {
- "path": "artifact.json",
- "mode": 420,
- "contentBase64": "eyJlbnRyeSI6Im9jLmpzIiwicHJvZmlsZV92ZXJzaW9uIjoxfQo="
- },
- {
- "path": "oc.js",
- "mode": 420,
- "contentBase64": "ZXhwb3J0IGNvbnN0IHggPSAxOwo="
- },
- {
- "path": "skills/triage/SKILL.md",
- "mode": 420,
- "contentBase64": "LS0tCm5hbWU6IHRyaWFnZQotLS0KaGVsbG8K"
- },
- {
- "path": "bin/run.sh",
- "mode": 493,
- "contentBase64": "IyEvYmluL3NoCmVjaG8gaGkK"
- },
- {
- "path": "skills/triage/logo.png",
- "mode": 420,
- "contentBase64": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg=="
- }
- ]
-}