CodeSampleX

Exemplo

google.golang.org/grpc v1.82.1

Amostra verificada para golang google.golang.org/grpc v1.82.1. O contrato rodou em go 1.26 · linux debian/x64 · docker e passou.

sha256:d716a72ac0fb1e585930798bb8f464a19632debf957bd2a4f03e54dabe5c5a28

Esta rede oferece uma coisa: uma amostra que compila. Ela a executou em um sandbox e guardou o recibo assinado. Não classifica nem garante nada — se o mesmo código compila onde você está, ela não mediu. Quantas chaves de assinatura distintas enviaram um recibo de contrato aprovado. Uma é só o autor; mais de uma significa que outra pessoa também o compilou. Uma chave é gerada por conta própria e não tem identidade registrada por trás, então conta chaves, não pessoas. MIT-0

Evidência de execução

O ambiente declarado e as execuções assinadas ficam separados, para você ver exatamente o que esta amostra executou e onde.

Base da evidência
Contrato assinado aprovado
Recibos de verificação
1
Chaves de assinatura que o compilaram
1
Ambiente declarado go linux 24 · ubuntu · glibc 2.39 x64 go go go

Ambientes das execuções de verificação

Ambiente Contrato Etapas Execução
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-10-12

Caso

HOW
Objetivo
verify pkg:golang/google.golang.org/grpc@v1.82.1
Pacotes
Ambiente
go
Criado
2026-10-12T21:01:04Z

Contrato

  1. status.Error creates an error preserving codes.NotFound and error message, recoverable via status.FromError.
  2. status.Code extracts codes.NotFound from a status error and codes.OK from nil.
  3. metadata.Pairs and metadata.NewOutgoingContext attach multi-value key metadata to an outgoing context.
  4. grpc.NewServer initializes a functional gRPC server instance that stops cleanly via Stop.

Arquivos

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • spec.json
  • test/contract_test.go

Baixar o artefato de código-fonte (tar.gz)

Código-fonte

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/google.golang.org/grpc@v1.82.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/google.golang.org/grpc@v1.82.1

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:c378f7ea7d368b0d97503c1d0978e2595bf8cf195093eff69b2854af880fcfad","contract":["status.Error creates an error preserving codes.NotFound and error message, recoverable via status.FromError.","status.Code extracts codes.NotFound from a status error and codes.OK from nil.","metadata.Pairs and metadata.NewOutgoingContext attach multi-value key metadata to an outgoing context.","grpc.NewServer initializes a functional gRPC server instance that stops cleanly via Stop."],"goal":"verify pkg:golang/google.golang.org/grpc@v1.82.1","kind":"HOW","packages":["pkg:golang/google.golang.org/grpc@v1.82.1"],"schemaVersion":1},"contractCommand":["go","test","./..."],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","moduleSystem":"go.mod","os":"linux","osVersionBucket":"24","packageManager":"go","runtime":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/google.golang.org/grpc@v1.82.1"],"schemaVersion":1,"subject":"pkg:golang/google.golang.org/grpc@v1.82.1","verifierAdapter":"golang@1"}
go.mod
module example.com/grpcverify

go 1.25.0

require google.golang.org/grpc v1.82.1

require (
	golang.org/x/net v0.53.0 // indirect
	golang.org/x/sys v0.43.0 // indirect
	golang.org/x/text v0.36.0 // indirect
	google.golang.org/genproto/googleapis/rpc v0.0.0-20260414002931-afd174a4e478 // indirect
	google.golang.org/protobuf v1.36.11 // indirect
)
go.sum
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
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/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
go.opentelemetry.io/otel/sdk v1.43.0 h1:pi5mE86i5rTeLXqoF/hhiBtUNcrAGHLKQdhg4h4V9Dg=
go.opentelemetry.io/otel/sdk v1.43.0/go.mod h1:P+IkVU3iWukmiit/Yf9AWvpyRDlUeBaRg6Y+C58QHzg=
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260414002931-afd174a4e478 h1:RmoJA1ujG+/lRGNfUnOMfhCy5EipVMyvUE+KNbPbTlw=
google.golang.org/genproto/googleapis/rpc v0.0.0-20260414002931-afd174a4e478/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
google.golang.org/grpc v1.82.1 h1:NnAxzGRA0677vCa4BUkOAnO5+FfQqVl9iUXeD0IqcGE=
google.golang.org/grpc v1.82.1/go.mod h1:yzTZ1TB1Z3SG+LIYaI+WiE8D5+PZ3ArnrSp8zF3+/ZA=
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
sample.go
package grpcverify

import (
	"context"

	"google.golang.org/grpc"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
)

// MakeStatusError creates an error with the given code and message using status.Error.
func MakeStatusError(c codes.Code, msg string) error {
	return status.Error(c, msg)
}

// ExtractStatusCode extracts the status code from an error using status.Code.
func ExtractStatusCode(err error) codes.Code {
	return status.Code(err)
}

// AttachOutgoingMetadata wraps a context with key-value metadata pairs.
func AttachOutgoingMetadata(ctx context.Context, kv ...string) context.Context {
	md := metadata.Pairs(kv...)
	return metadata.NewOutgoingContext(ctx, md)
}

// CreateServer returns an initialized gRPC server instance.
func CreateServer(opts ...grpc.ServerOption) *grpc.Server {
	return grpc.NewServer(opts...)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/google.golang.org/grpc@v1.82.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/google.golang.org/grpc@v1.82.1"
  ]
}
test/contract_test.go
package contract_test

import (
	"context"
	"testing"

	"example.com/grpcverify"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/metadata"
	"google.golang.org/grpc/status"
)

func TestStatusErrorConversion(t *testing.T) {
	msg := "resource was not found"
	err := grpcverify.MakeStatusError(codes.NotFound, msg)
	if err == nil {
		t.Fatal("expected non-nil error")
	}

	st, ok := status.FromError(err)
	if !ok {
		t.Fatal("expected status.FromError to succeed")
	}
	if st.Code() != codes.NotFound {
		t.Fatalf("expected code %v, got %v", codes.NotFound, st.Code())
	}
	if st.Message() != msg {
		t.Fatalf("expected message %q, got %q", msg, st.Message())
	}

	if code := grpcverify.ExtractStatusCode(err); code != codes.NotFound {
		t.Fatalf("expected ExtractStatusCode to return %v, got %v", codes.NotFound, code)
	}

	if code := grpcverify.ExtractStatusCode(nil); code != codes.OK {
		t.Fatalf("expected nil error to have OK code, got %v", code)
	}
}

func TestMetadataContextHandling(t *testing.T) {
	ctx := context.Background()
	ctxWithMD := grpcverify.AttachOutgoingMetadata(ctx, "x-request-id", "req-123", "x-custom-key", "val-a", "x-custom-key", "val-b")

	md, ok := metadata.FromOutgoingContext(ctxWithMD)
	if !ok {
		t.Fatal("expected metadata.FromOutgoingContext to return ok == true")
	}

	reqIDs := md.Get("x-request-id")
	if len(reqIDs) != 1 || reqIDs[0] != "req-123" {
		t.Fatalf("expected x-request-id ['req-123'], got %v", reqIDs)
	}

	customVals := md.Get("x-custom-key")
	if len(customVals) != 2 || customVals[0] != "val-a" || customVals[1] != "val-b" {
		t.Fatalf("expected multi-value slice ['val-a', 'val-b'], got %v", customVals)
	}
}

func TestServerLifecycle(t *testing.T) {
	srv := grpcverify.CreateServer()
	if srv == nil {
		t.Fatal("expected non-nil grpc.Server")
	}
	srv.Stop()
}

Seeder de origem

anônimo