CodeSampleX

Exemple

github.com/go-kit/kit v0.4.0: log.Logger, log.NewJSONLogger, log.NewLogfmtLogger

Échantillon vérifié pour golang github.com/go-kit/kit v0.4.0: log.Logger, log.NewJSONLogger, log.NewLogfmtLogger. Le contrat s'est exécuté sur go 1.26 …

sha256:ccfa30ac66db1800a090be76da5438f5cf1aa4ba5061ca6ff5040edced5b7b6a

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify pkg:golang/github.com/go-kit/kit@v0.4.0
Paquets
Symboles
  • log.Logger
  • log.NewJSONLogger
  • log.NewLogfmtLogger
  • log.Valuer
  • log.With
Environnement
go 1.26.6
Créé
2026-08-28T09:38:48Z

Contrat

  1. log.NewLogfmtLogger writes key-value pairs in logfmt format separated by spaces with newline termination
  2. log.With prepends contextual key-value pairs and evaluates log.Valuer dynamically on each Log invocation
  3. log.NewJSONLogger encodes structured key-value pairs into valid JSON objects with typed primitives

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code 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 pkg:golang/github.com/go-kit/kit@v0.4.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-kit/kit@v0.4.0
Required runtime conditions:
  - ecosystem: golang
  - language: go
  - packageManager: go@1.26.6
  - runtime: go@1.26.6

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:b2c61ff9d1a682ab78bc3112267f8680fe150aae930e6de322d4a2afb5734658","contract":["log.NewLogfmtLogger writes key-value pairs in logfmt format separated by spaces with newline termination","log.With prepends contextual key-value pairs and evaluates log.Valuer dynamically on each Log invocation","log.NewJSONLogger encodes structured key-value pairs into valid JSON objects with typed primitives"],"goal":"verify pkg:golang/github.com/go-kit/kit@v0.4.0","kind":"HOW","packages":["pkg:golang/github.com/go-kit/kit@v0.4.0"],"schemaVersion":1,"symbols":["log.Logger","log.NewJSONLogger","log.NewLogfmtLogger","log.Valuer","log.With"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","language":"go","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/go-kit/kit@v0.4.0"],"schemaVersion":1,"subject":"pkg:golang/github.com/go-kit/kit@v0.4.0","symbols":["log.Logger","log.NewJSONLogger","log.NewLogfmtLogger","log.Valuer","log.With"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require github.com/go-kit/kit v0.4.0

require (
	github.com/go-logfmt/logfmt v0.6.1 // indirect
	github.com/go-stack/stack v1.8.1 // indirect
)
go.sum
github.com/go-kit/kit v0.4.0 h1:KeVK+Emj3c3S4eRztFuzbFYb2BAgf2jmwDwyXEri7Lo=
github.com/go-kit/kit v0.4.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.6.1 h1:4hvbpePJKnIzH1B+8OR/JPbTx37NktoI9LE2QZBBkvE=
github.com/go-logfmt/logfmt v0.6.1/go.mod h1:EV2pOAQoZaT1ZXZbqDl5hrymndi4SY9ED9/z6CO0XAk=
github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw=
github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
main.go
package main

import (
	"bytes"
	"fmt"
	"sync/atomic"

	"github.com/go-kit/kit/log"
)

func main() {
	// 1. Create a logfmt logger writing to a buffer
	var logfmtBuf bytes.Buffer
	logger := log.NewLogfmtLogger(&logfmtBuf)

	// Decorate logger with contextual fields (static component and dynamic request counter)
	var reqCount int64
	contextLogger := log.With(
		logger,
		"component", "service-handler",
		"req_id", log.Valuer(func() interface{} {
			return atomic.AddInt64(&reqCount, 1)
		}),
	)

	// Log events using the contextual logger
	_ = contextLogger.Log("event", "processing_request", "path", "/api/v1/resource")
	_ = contextLogger.Log("event", "request_complete", "status", 200)

	fmt.Println("--- Logfmt Output ---")
	fmt.Print(logfmtBuf.String())

	// 2. Create a JSON logger writing to a buffer
	var jsonBuf bytes.Buffer
	jsonLogger := log.NewJSONLogger(&jsonBuf)
	jsonLogger = log.With(jsonLogger, "app", "sample-app")

	_ = jsonLogger.Log("msg", "application started", "port", 8080)
	_ = jsonLogger.Log("msg", "health check ok", "healthy", true)

	fmt.Println("--- JSON Output ---")
	fmt.Print(jsonBuf.String())
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/go-kit/kit@v0.4.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-kit/kit@v0.4.0"
  ],
  "symbols": [
    "log.Logger",
    "log.NewJSONLogger",
    "log.NewLogfmtLogger",
    "log.Valuer",
    "log.With"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract.go
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"
	"reflect"
	"sync/atomic"

	"github.com/go-kit/kit/log"
)

func main() {
	// Assertion 1: log.NewLogfmtLogger writes key-value pairs in logfmt format separated by spaces with newline termination
	{
		var buf bytes.Buffer
		logger := log.NewLogfmtLogger(&buf)

		if err := logger.Log("key1", "value1", "key2", "value2"); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: %v\n", err)
			os.Exit(1)
		}

		expected1 := "key1=value1 key2=value2\n"
		if buf.String() != expected1 {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected %q, got %q\n", expected1, buf.String())
			os.Exit(1)
		}

		buf.Reset()
		if err := logger.Log("msg", "hello world"); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: %v\n", err)
			os.Exit(1)
		}

		expectedQuoted := "msg=\"hello world\"\n"
		if buf.String() != expectedQuoted {
			fmt.Fprintf(os.Stderr, "assertion 1 failed: expected quoted %q, got %q\n", expectedQuoted, buf.String())
			os.Exit(1)
		}
	}

	// Assertion 2: log.With prepends contextual key-value pairs and evaluates log.Valuer dynamically on each Log invocation
	{
		var buf bytes.Buffer
		baseLogger := log.NewLogfmtLogger(&buf)

		var counter int64
		dynamicValuer := log.Valuer(func() interface{} {
			return atomic.AddInt64(&counter, 1)
		})

		contextLogger := log.With(baseLogger, "component", "auth", "seq", dynamicValuer)

		if err := contextLogger.Log("action", "login", "user", "alice"); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: %v\n", err)
			os.Exit(1)
		}

		line1 := buf.String()
		expectedLine1 := "component=auth seq=1 action=login user=alice\n"
		if line1 != expectedLine1 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected line 1 %q, got %q\n", expectedLine1, line1)
			os.Exit(1)
		}

		buf.Reset()
		if err := contextLogger.Log("action", "logout", "user", "alice"); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: %v\n", err)
			os.Exit(1)
		}

		line2 := buf.String()
		expectedLine2 := "component=auth seq=2 action=logout user=alice\n"
		if line2 != expectedLine2 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected line 2 %q, got %q\n", expectedLine2, line2)
			os.Exit(1)
		}

		if counter != 2 {
			fmt.Fprintf(os.Stderr, "assertion 2 failed: expected counter 2, got %d\n", counter)
			os.Exit(1)
		}
	}

	// Assertion 3: log.NewJSONLogger encodes structured key-value pairs into valid JSON objects with typed primitives
	{
		var buf bytes.Buffer
		jsonLogger := log.NewJSONLogger(&buf)
		contextJSON := log.With(jsonLogger, "app", "sample-service")

		if err := contextJSON.Log("event", "audit", "status_code", 200, "success", true); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: %v\n", err)
			os.Exit(1)
		}

		var parsed map[string]interface{}
		if err := json.Unmarshal(buf.Bytes(), &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: invalid json %q: %v\n", buf.String(), err)
			os.Exit(1)
		}

		expectedParsed := map[string]interface{}{
			"app":         "sample-service",
			"event":       "audit",
			"status_code": float64(200),
			"success":     true,
		}

		if !reflect.DeepEqual(parsed, expectedParsed) {
			fmt.Fprintf(os.Stderr, "assertion 3 failed: expected map %v, got %v\n", expectedParsed, parsed)
			os.Exit(1)
		}
	}

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

Seeder d'origine

anonyme