CodeSampleX

示例

github.com/pkg/errors v0.9.1

已验证示例 — golang github.com/pkg/errors v0.9.1. contract 在 go 1.26 · linux alpine/x64 · docker 上运行并通过: errors.New and errors.Errorf create errors with stack…

sha256:d05be37f7de04833720bbb4de694b8c60f138cc04d3d9ca948d20bb55f2a3309

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26-alpine@sha256:28d89ee9cc0f…
2026-08-28

案例

HOW
目标
verify pkg:golang/github.com/pkg/errors@v0.9.1
包
创建时间
2026-08-28T00:14:06Z

契约

  1. errors.New and errors.Errorf create errors with stack trace capture and formatted messages
  2. errors.Wrap and errors.Wrapf annotate an error with context message and stack trace, returning nil for a nil error
  3. errors.WithMessage and errors.WithMessagef add context message without altering the cause, returning nil for a nil error
  4. errors.WithStack attaches stack trace information to an existing error without altering its message
  5. errors.Cause recursively unwraps nested wrapped errors to retrieve the root underlying cause
  6. Formatting wrapped errors with %+v outputs the full multi-frame stack trace

文件

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

下载源代码构件 (tar.gz)

源代码

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/pkg/errors@v0.9.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/pkg/errors@v0.9.1
Constraints:
  - executionContext: node
Required runtime conditions:
  - ecosystem: npm
  - language: javascript
  - moduleSystem: cjs
  - packageManager: npm@10.9.8
  - runtime: node@22.23.2

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:ee9bf7dd4bfbaa2af1da9bfb6bf54b76eab2a11455a930614bb3bfa90008757f","contract":["errors.New and errors.Errorf create errors with stack trace capture and formatted messages","errors.Wrap and errors.Wrapf annotate an error with context message and stack trace, returning nil for a nil error","errors.WithMessage and errors.WithMessagef add context message without altering the cause, returning nil for a nil error","errors.WithStack attaches stack trace information to an existing error without altering its message","errors.Cause recursively unwraps nested wrapped errors to retrieve the root underlying cause","Formatting wrapped errors with %+v outputs the full multi-frame stack trace"],"goal":"verify pkg:golang/github.com/pkg/errors@v0.9.1","kind":"HOW","packages":["pkg:golang/github.com/pkg/errors@v0.9.1"],"schemaVersion":1},"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/pkg/errors@v0.9.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/pkg/errors@v0.9.1","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/pkg/errors v0.9.1
go.sum
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
main.go
package main

import (
	"fmt"

	"github.com/pkg/errors"
)

func parseConfig(filename string) error {
	return errors.Errorf("file %q not found", filename)
}

func initializeService() error {
	err := parseConfig("config.json")
	if err != nil {
		return errors.Wrap(err, "failed to initialize service")
	}
	return nil
}

func main() {
	err := initializeService()
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		fmt.Printf("Cause: %v\n", errors.Cause(err))
		fmt.Printf("Stack trace:\n%+v\n", err)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/pkg/errors@v0.9.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/pkg/errors@v0.9.1"
  ],
  "constraints": {
    "executionContext": "node"
  },
  "runtimeConditions": {
    "ecosystem": "npm",
    "language": "javascript",
    "moduleSystem": "cjs",
    "packageManager": "npm@10.9.8",
    "runtime": "node@22.23.2"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"strings"

	"github.com/pkg/errors"
)

func main() {
	if err := runContract(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract verification failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All pkg/errors contract assertions passed successfully.")
}

func runContract() error {
	// 1. errors.New and errors.Errorf create errors with stack trace and formatting
	newErr := errors.New("base error message")
	if newErr == nil || newErr.Error() != "base error message" {
		return fmt.Errorf("errors.New failed, got: %v", newErr)
	}

	formattedErr := errors.Errorf("formatted error with code %d", 404)
	if formattedErr == nil || formattedErr.Error() != "formatted error with code 404" {
		return fmt.Errorf("errors.Errorf failed, got: %v", formattedErr)
	}

	formattedStackTrace := fmt.Sprintf("%+v", formattedErr)
	if !strings.Contains(formattedStackTrace, "contract.go") {
		return fmt.Errorf("expected stack trace from errors.Errorf to contain 'contract.go', got:\n%s", formattedStackTrace)
	}

	// 2. errors.Wrap and errors.Wrapf annotate error with context and stack trace, nil safety
	if errors.Wrap(nil, "ignored context") != nil {
		return fmt.Errorf("expected errors.Wrap(nil, ...) to return nil")
	}
	if errors.Wrapf(nil, "ignored %s", "context") != nil {
		return fmt.Errorf("expected errors.Wrapf(nil, ...) to return nil")
	}

	baseErr := errors.New("disk full")
	wrappedErr := errors.Wrap(baseErr, "write failed")
	if wrappedErr.Error() != "write failed: disk full" {
		return fmt.Errorf("expected 'write failed: disk full', got: %q", wrappedErr.Error())
	}

	wrappedFormattedErr := errors.Wrapf(baseErr, "write to device %s failed", "/dev/sda1")
	if wrappedFormattedErr.Error() != "write to device /dev/sda1 failed: disk full" {
		return fmt.Errorf("expected 'write to device /dev/sda1 failed: disk full', got: %q", wrappedFormattedErr.Error())
	}

	// 3. errors.WithMessage and errors.WithMessagef add context message without adding stack trace, nil safety
	if errors.WithMessage(nil, "ignored") != nil {
		return fmt.Errorf("expected errors.WithMessage(nil, ...) to return nil")
	}
	if errors.WithMessagef(nil, "ignored %d", 1) != nil {
		return fmt.Errorf("expected errors.WithMessagef(nil, ...) to return nil")
	}

	withMsgErr := errors.WithMessage(baseErr, "operation timeout")
	if withMsgErr.Error() != "operation timeout: disk full" {
		return fmt.Errorf("expected 'operation timeout: disk full', got: %q", withMsgErr.Error())
	}

	withMsgfErr := errors.WithMessagef(baseErr, "step %d failed", 2)
	if withMsgfErr.Error() != "step 2 failed: disk full" {
		return fmt.Errorf("expected 'step 2 failed: disk full', got: %q", withMsgfErr.Error())
	}

	// 4. errors.WithStack attaches stack trace without modifying message, nil safety
	if errors.WithStack(nil) != nil {
		return fmt.Errorf("expected errors.WithStack(nil) to return nil")
	}

	plainErr := fmt.Errorf("plain standard error")
	stackedErr := errors.WithStack(plainErr)
	if stackedErr.Error() != "plain standard error" {
		return fmt.Errorf("expected message preserved in WithStack, got: %q", stackedErr.Error())
	}
	stackedOutput := fmt.Sprintf("%+v", stackedErr)
	if !strings.Contains(stackedOutput, "contract.go") {
		return fmt.Errorf("expected WithStack output to include stack trace with contract.go, got:\n%s", stackedOutput)
	}

	// 5. errors.Cause recursively retrieves the root underlying error
	if errors.Cause(nil) != nil {
		return fmt.Errorf("expected errors.Cause(nil) to be nil")
	}

	rootErr := errors.New("root cause failure")
	layer1 := errors.WithMessage(rootErr, "layer 1")
	layer2 := errors.Wrap(layer1, "layer 2")
	layer3 := errors.WithStack(layer2)
	layer4 := errors.WithMessagef(layer3, "layer 4: attempt %d", 3)

	cause := errors.Cause(layer4)
	if cause != rootErr {
		return fmt.Errorf("expected Cause to return rootErr, got: %v", cause)
	}

	// 6. Formatting wrapped errors with %+v prints all layers and stack frames
	nestedOutput := fmt.Sprintf("%+v", layer2)
	if !strings.Contains(nestedOutput, "root cause failure") || !strings.Contains(nestedOutput, "layer 1") || !strings.Contains(nestedOutput, "layer 2") {
		return fmt.Errorf("expected %+v output to include all wrap messages, got:\n%s", nestedOutput)
	}

	return nil
}

原始种子者

匿名