CodeSampleX

Sample

github.com/klauspost/compress v1.20.0: snappy.NewWriter

Verified sample for golang github.com/klauspost/compress v1.20.0: snappy.NewWriter. The contract ran on go 1.26 · linux debian/x64 · docker and passed.

sha256:906b116b6715b2ed6624afe25459a4d42d93347893c1259e94f66d90ad2c06a6

This network offers one thing: a sample that builds. It ran the sample in a sandbox and kept the signed receipt. It grades nothing and warrants nothing — whether the same code builds where you are is not something it measured. How many distinct signing keys filed a passing contract receipt. One is the author alone; more than one means somebody else built it too. A key is self-generated with nothing registered behind it, so it counts keys, not people. MIT-0

Execution evidence

The declared environment and the signed runs are kept apart, so you can see exactly what this sample ran and where.

Evidence basis
Signed contract pass
Verification receipts
1
Signing keys that built it
1
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-03

Case

HOW
Goal
verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0
Packages
Symbols
  • snappy.NewWriter
Created
2026-09-03T00:33:55Z

Contract

  1. snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer
  2. snappy.Writer compresses data written through Write method into Snappy framing stream
  3. snappy.Writer.Close flushes and finalizes the compressed stream
  4. snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer
  5. decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter

Files

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

Download the source artifact (tar.gz)

Source

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/klauspost/compress@v1.20.0
Demonstrate these symbols/APIs:
  - snappy.NewWriter

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:70a636d0e648acb736f7d38ad504f87242fb0c58045f0c22306ee5269c32c93b","contract":["snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer","snappy.Writer compresses data written through Write method into Snappy framing stream","snappy.Writer.Close flushes and finalizes the compressed stream","snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer","decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter"],"goal":"verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0","kind":"HOW","packages":["pkg:golang/github.com/klauspost/compress@v1.20.0"],"schemaVersion":1,"symbols":["snappy.NewWriter"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/klauspost/compress@v1.20.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/klauspost/compress@v1.20.0","symbols":["snappy.NewWriter"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/klauspost/compress v1.20.0
go.sum
github.com/klauspost/compress v1.20.0 h1:a3C1ke2ohxFymNlb2HWAHjDeKCI90scRskErZkR0ezA=
github.com/klauspost/compress v1.20.0/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"

	"github.com/klauspost/compress/snappy"
)

func main() {
	var buf bytes.Buffer
	w := snappy.NewWriter(&buf)

	data := []byte("Streaming compression with klauspost/compress snappy.NewWriter")
	if _, err := w.Write(data); err != nil {
		panic(err)
	}

	if err := w.Close(); err != nil {
		panic(err)
	}

	compressedLen := buf.Len()
	r := snappy.NewReader(&buf)
	decompressed, err := io.ReadAll(r)
	if err != nil {
		panic(err)
	}

	fmt.Printf("Compressed %d bytes down to %d bytes\n", len(data), compressedLen)
	fmt.Printf("Decompressed string: %s\n", string(decompressed))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify snappy.NewWriter in pkg:golang/github.com/klauspost/compress@v1.20.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/klauspost/compress@v1.20.0"
  ],
  "symbols": [
    "snappy.NewWriter"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"

	"github.com/klauspost/compress/snappy"
)

func main() {
	// 1. snappy.NewWriter returns a non-nil *snappy.Writer wrapping the target io.Writer
	var buf bytes.Buffer
	w := snappy.NewWriter(&buf)
	if w == nil {
		fmt.Fprintln(os.Stderr, "FAIL: snappy.NewWriter returned nil")
		os.Exit(1)
	}

	// 2. snappy.Writer compresses data written through Write method into Snappy framing stream
	input1 := []byte("CodeSampleX contract verification: snappy.Writer streaming compression.")
	n, err := w.Write(input1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write failed: %v\n", err)
		os.Exit(1)
	}
	if n != len(input1) {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write short write: got %d want %d\n", n, len(input1))
		os.Exit(1)
	}

	// 3. snappy.Writer.Close flushes and finalizes the compressed stream
	if err := w.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Close failed: %v\n", err)
		os.Exit(1)
	}
	compressed1 := buf.Bytes()
	if len(compressed1) == 0 {
		fmt.Fprintln(os.Stderr, "FAIL: compressed stream is empty after Close")
		os.Exit(1)
	}
	snappyMagic := []byte("\xff\x06\x00\x00sNaPpY")
	if !bytes.HasPrefix(compressed1, snappyMagic) {
		fmt.Fprintf(os.Stderr, "FAIL: compressed stream missing Snappy magic header\n")
		os.Exit(1)
	}

	// 4. snappy.Writer.Reset redirects subsequent compressed writes to a new io.Writer
	var buf2 bytes.Buffer
	w.Reset(&buf2)
	input2 := []byte("Second stream payload following snappy.Writer.Reset call.")
	n2, err := w.Write(input2)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write after Reset failed: %v\n", err)
		os.Exit(1)
	}
	if n2 != len(input2) {
		fmt.Fprintf(os.Stderr, "FAIL: w.Write after Reset short write: got %d want %d\n", n2, len(input2))
		os.Exit(1)
	}
	if err := w.Close(); err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: w.Close after Reset failed: %v\n", err)
		os.Exit(1)
	}
	compressed2 := buf2.Bytes()
	if !bytes.HasPrefix(compressed2, snappyMagic) {
		fmt.Fprintf(os.Stderr, "FAIL: second stream missing Snappy magic header\n")
		os.Exit(1)
	}

	// 5. decompression with snappy.NewReader recovers the exact original bytes written by snappy.NewWriter
	r1 := snappy.NewReader(bytes.NewReader(compressed1))
	decomp1, err := io.ReadAll(r1)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAll on stream 1 failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decomp1, input1) {
		fmt.Fprintf(os.Stderr, "FAIL: stream 1 mismatch: got %q want %q\n", decomp1, input1)
		os.Exit(1)
	}

	r2 := snappy.NewReader(bytes.NewReader(compressed2))
	decomp2, err := io.ReadAll(r2)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: ReadAll on stream 2 failed: %v\n", err)
		os.Exit(1)
	}
	if !bytes.Equal(decomp2, input2) {
		fmt.Fprintf(os.Stderr, "FAIL: stream 2 mismatch: got %q want %q\n", decomp2, input2)
		os.Exit(1)
	}

	fmt.Println("All contract assertions passed successfully.")
}

Origin Seeder

anonymous