CodeSampleX

Beispiel

github.com/prometheus/procfs v0.22.0: FS.Stat

Verifiziertes Beispiel für golang github.com/prometheus/procfs v0.22.0: FS.Stat. Der Vertrag lief auf go 1.26 · linux debian/x64 · docker und bestand.

sha256:b2b7ecca28db42606567dc42a5ed5a5a756c2690369fac1046d9286ec008950f

Dieses Netzwerk bietet eine Sache: ein Sample, das baut. Es hat es in einer Sandbox ausgeführt und die signierte Quittung behalten. Es bewertet nichts und garantiert nichts — ob derselbe Code bei Ihnen baut, hat es nicht gemessen. Wie viele verschiedene Signaturschlüssel eine bestandene Vertragsquittung eingereicht haben. Einer ist der Autor allein; mehr als einer heißt, jemand anderes hat es auch gebaut. Ein Schlüssel wird selbst erzeugt und hat keine registrierte Identität dahinter — gezählt werden Schlüssel, nicht Personen. MIT-0

Ausführungsbelege

Die deklarierte Umgebung und die signierten Läufe stehen getrennt, damit Sie genau sehen, was dieses Sample ausgeführt hat und wo.

Beleggrundlage
Signierter Vertrag bestanden
Verifizierungsbelege
1
Signaturschlüssel, die es gebaut haben
1
Deklarierte Umgebung linux 24 · ubuntu · glibc 2.39 x64 go

Umgebungen der Verifizierungsläufe

Umgebung Contract Stufen Lauf
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-05

Fall

HOW
Ziel
verify pkg:golang/github.com/prometheus/procfs@v0.22.0
Pakete
Symbole
  • procfs.FS.Stat
Erstellt
2026-09-05T07:26:50Z

Contract

  1. procfs.NewDefaultFS() returns (FS, error) where error is nil
  2. fs.Stat() returns (Stat, error) where error is nil and BootTime is positive
  3. procfs.FS.Stat returns an error when reading from a proc mount missing stat
  4. fs.Stat() parses BootTime, ProcessCreated, ContextSwitches, and CPUTotal metrics from procfs mount

Dateien

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

Quellartefakt herunterladen (tar.gz)

Quelltext

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 pkg:golang/github.com/prometheus/procfs@v0.22.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/prometheus/procfs@v0.22.0
Demonstrate these symbols/APIs:
  - procfs.FS.Stat

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:a64ee4c6f75d8458a9844971537464feb237c724f025d585aa8ae0a5d1cbbab0","contract":["procfs.NewDefaultFS() returns (FS, error) where error is nil","fs.Stat() returns (Stat, error) where error is nil and BootTime is positive","procfs.FS.Stat returns an error when reading from a proc mount missing stat","fs.Stat() parses BootTime, ProcessCreated, ContextSwitches, and CPUTotal metrics from procfs mount"],"goal":"verify pkg:golang/github.com/prometheus/procfs@v0.22.0","kind":"HOW","packages":["pkg:golang/github.com/prometheus/procfs@v0.22.0"],"schemaVersion":1,"symbols":["procfs.FS.Stat"]},"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/prometheus/procfs@v0.22.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/prometheus/procfs@v0.22.0","symbols":["procfs.FS.Stat"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/prometheus/procfs v0.22.0

require golang.org/x/sys v0.47.0 // indirect
go.sum
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/prometheus/procfs v0.22.0 h1:6q9+/JL9IKAPbCmBrv9n5O5Ty3NKnciV5X7YGw0oics=
github.com/prometheus/procfs v0.22.0/go.mod h1:CvmFr/GVhIjIvWJZW3tgkODBQMRIf0EyWMQLHCHab58=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
main.go
package main

import (
	"fmt"
	"os"

	"github.com/prometheus/procfs"
)

// ReadStat returns current system kernel and process statistics.
func ReadStat() (procfs.Stat, error) {
	fs, err := procfs.NewDefaultFS()
	if err != nil {
		return procfs.Stat{}, fmt.Errorf("procfs access failed: %w", err)
	}
	return fs.Stat()
}

func main() {
	stat, err := ReadStat()
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to read proc stat: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("BootTime=%d ContextSwitches=%d ProcessesCreated=%d Running=%d Blocked=%d\n",
		stat.BootTime, stat.ContextSwitches, stat.ProcessCreated, stat.ProcessesRunning, stat.ProcessesBlocked)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/prometheus/procfs@v0.22.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/prometheus/procfs@v0.22.0"
  ],
  "symbols": [
    "procfs.FS.Stat"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/prometheus/procfs"
)

func main() {
	// 1. Verify procfs.NewDefaultFS returns a valid FS instance
	fs, err := procfs.NewDefaultFS()
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewDefaultFS failed: %v\n", err)
		os.Exit(1)
	}

	// 2. Verify fs.Stat parses real /proc/stat
	stat, err := fs.Stat()
	if err != nil {
		fmt.Fprintf(os.Stderr, "fs.Stat failed: %v\n", err)
		os.Exit(1)
	}
	if stat.BootTime == 0 {
		fmt.Fprintf(os.Stderr, "expected positive BootTime, got: %d\n", stat.BootTime)
		os.Exit(1)
	}

	// 3. Verify fs.Stat fails when stat file is missing
	tmpDir, err := os.MkdirTemp("", "proctest*")
	if err != nil {
		fmt.Fprintf(os.Stderr, "os.MkdirTemp failed: %v\n", err)
		os.Exit(1)
	}
	defer os.RemoveAll(tmpDir)

	mockFS, err := procfs.NewFS(tmpDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "procfs.NewFS failed: %v\n", err)
		os.Exit(1)
	}

	_, err = mockFS.Stat()
	if err == nil {
		fmt.Fprintf(os.Stderr, "expected error when stat file is missing, got nil\n")
		os.Exit(1)
	}

	// 4. Verify mock stat parsing of BootTime, ProcessCreated, and CPUTotal
	mockContent := []byte("cpu  100 20 30 500 10 0 5 2 0 0\n" +
		"cpu0 50 10 15 250 5 0 2 1 0 0\n" +
		"cpu1 50 10 15 250 5 0 3 1 0 0\n" +
		"intr 1000 0 10\n" +
		"ctxt 5000\n" +
		"btime 1700000000\n" +
		"processes 4200\n" +
		"procs_running 2\n" +
		"procs_blocked 1\n")
	if err := os.WriteFile(filepath.Join(tmpDir, "stat"), mockContent, 0644); err != nil {
		fmt.Fprintf(os.Stderr, "os.WriteFile failed: %v\n", err)
		os.Exit(1)
	}

	parsedStat, err := mockFS.Stat()
	if err != nil {
		fmt.Fprintf(os.Stderr, "mockFS.Stat failed: %v\n", err)
		os.Exit(1)
	}

	if parsedStat.BootTime != 1700000000 {
		fmt.Fprintf(os.Stderr, "expected BootTime 1700000000, got: %d\n", parsedStat.BootTime)
		os.Exit(1)
	}
	if parsedStat.ProcessCreated != 4200 {
		fmt.Fprintf(os.Stderr, "expected ProcessCreated 4200, got: %d\n", parsedStat.ProcessCreated)
		os.Exit(1)
	}
	if parsedStat.ProcessesRunning != 2 {
		fmt.Fprintf(os.Stderr, "expected ProcessesRunning 2, got: %d\n", parsedStat.ProcessesRunning)
		os.Exit(1)
	}
	if parsedStat.ProcessesBlocked != 1 {
		fmt.Fprintf(os.Stderr, "expected ProcessesBlocked 1, got: %d\n", parsedStat.ProcessesBlocked)
		os.Exit(1)
	}
	if parsedStat.ContextSwitches != 5000 {
		fmt.Fprintf(os.Stderr, "expected ContextSwitches 5000, got: %d\n", parsedStat.ContextSwitches)
		os.Exit(1)
	}
	if parsedStat.CPUTotal.User != 1.0 || parsedStat.CPUTotal.Idle != 5.0 {
		// note: procfs converts USER_HZ (100) to seconds (float64) so 100 jiffies = 1.0s, 500 jiffies = 5.0s
		fmt.Fprintf(os.Stderr, "unexpected CPUTotal: %+v\n", parsedStat.CPUTotal)
		os.Exit(1)
	}

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

Ursprungs-Seeder

anonym