CodeSampleX

Sample

github.com/go-logr/logr v1.4.3

Verified sample for golang github.com/go-logr/logr v1.4.3. The contract ran on go 1.26 · linux alpine/x64 · docker and passed.

sha256:569377d49d7dcc63700a9ff38dd17b6dc9c602de35b21be53f2e516c7626678d

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
2
Signing keys that built it
2
Declared environment linux 24 · ubuntu · glibc 2.39 x64 go

Verification-run environments

Environment Contract Stages Run
go 1.26 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1
2026-08-20
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-07

Case

HOW
Goal
verify pkg:golang/github.com/go-logr/logr@v1.4.3
Packages
Created
2026-08-19T22:43:58Z

Contract

  1. funcr.NewJSON produces structured log entries formatted as valid JSON
  2. logr.ToSlogHandler adapts logr.Logger into a standard library slog.Handler
  3. logr.Marshaler interface marshals custom types via MarshalLog method
  4. logr.NewContextWithSlogLogger and logr.FromContextAsSlogLogger manage slog.Logger across context boundaries
  5. logr.Discard safely drops all log entries without panicking

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 pkg:golang/github.com/go-logr/logr@v1.4.3
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/go-logr/logr@v1.4.3
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:1b26015408f48875f10aed0ae3625fd157519fbb455749e40015dd3b8445752a","contract":["funcr.NewJSON produces structured log entries formatted as valid JSON","logr.ToSlogHandler adapts logr.Logger into a standard library slog.Handler","logr.Marshaler interface marshals custom types via MarshalLog method","logr.NewContextWithSlogLogger and logr.FromContextAsSlogLogger manage slog.Logger across context boundaries","logr.Discard safely drops all log entries without panicking"],"goal":"verify pkg:golang/github.com/go-logr/logr@v1.4.3","kind":"HOW","packages":["pkg:golang/github.com/go-logr/logr@v1.4.3"],"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/go-logr/logr@v1.4.3"],"schemaVersion":1,"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.26.6

require github.com/go-logr/logr v1.4.3
go.sum
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
main.go
package main

import (
	"context"
	"fmt"
	"log/slog"
	"os"

	"github.com/go-logr/logr"
	"github.com/go-logr/logr/funcr"
)

// SecurityPayload demonstrates custom structured marshaling with logr.Marshaler.
type SecurityPayload struct {
	Subject string
	Secret  string
}

// MarshalLog implements logr.Marshaler to sanitize sensitive fields.
func (p SecurityPayload) MarshalLog() any {
	return map[string]string{
		"subject": p.Subject,
		"secret":  "[REDACTED]",
	}
}

// ServiceBridge orchestrates logging between logr and slog.
type ServiceBridge struct {
	jsonLogger logr.Logger
}

// NewServiceBridge creates a ServiceBridge with a JSON funcr logger.
func NewServiceBridge() *ServiceBridge {
	return &ServiceBridge{
		jsonLogger: funcr.NewJSON(func(obj string) {
			fmt.Fprintln(os.Stdout, obj)
		}, funcr.Options{}),
	}
}

// ExecuteWorkflow runs structured logging operations and attaches a slog logger to context.
func (s *ServiceBridge) ExecuteWorkflow(ctx context.Context, subject string) context.Context {
	payload := SecurityPayload{
		Subject: subject,
		Secret:  "raw-token-data",
	}
	s.jsonLogger.Info("executing service workflow", "payload", payload)

	slogHandler := logr.ToSlogHandler(s.jsonLogger)
	slogLogger := slog.New(slogHandler)

	return logr.NewContextWithSlogLogger(ctx, slogLogger)
}

func main() {
	bridge := NewServiceBridge()
	ctx := context.Background()

	ctx = bridge.ExecuteWorkflow(ctx, "worker-node-1")
	if retrieved := logr.FromContextAsSlogLogger(ctx); retrieved != nil {
		retrieved.Info("context slog logger operational", "component", "main")
	}

	discard := logr.Discard()
	discard.Info("discarding background noise")
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/go-logr/logr@v1.4.3",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/go-logr/logr@v1.4.3"
  ],
  "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 (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"log/slog"
	"os"
	"strings"
	"sync"

	"github.com/go-logr/logr"
	"github.com/go-logr/logr/funcr"
)

// AuditPayload implements logr.Marshaler.
type AuditPayload struct {
	AccountID string
	AuthToken string
}

func (a AuditPayload) MarshalLog() any {
	return map[string]string{
		"accountId": a.AccountID,
		"authToken": "[REDACTED]",
	}
}

func main() {
	// Contract 1: funcr.NewJSON produces structured log entries formatted as valid JSON
	{
		var captured string
		var mu sync.Mutex
		logger := funcr.NewJSON(func(obj string) {
			mu.Lock()
			defer mu.Unlock()
			captured = obj
		}, funcr.Options{})

		logger.Info("event recorded", "action", "create_resource", "resourceId", 1024)

		mu.Lock()
		out := captured
		mu.Unlock()

		var parsed map[string]any
		if err := json.Unmarshal([]byte(out), &parsed); err != nil {
			fmt.Fprintf(os.Stderr, "FAIL: output is not valid JSON: %v, raw: %s\n", err, out)
			os.Exit(1)
		}
		if parsed["msg"] != "event recorded" {
			fmt.Fprintf(os.Stderr, "FAIL: expected msg 'event recorded', got %v\n", parsed["msg"])
			os.Exit(1)
		}
		if parsed["action"] != "create_resource" {
			fmt.Fprintf(os.Stderr, "FAIL: expected action 'create_resource', got %v\n", parsed["action"])
			os.Exit(1)
		}
	}

	// Contract 2: logr.ToSlogHandler adapts logr.Logger into a standard library slog.Handler
	{
		var captured string
		var mu sync.Mutex
		baseLogger := funcr.New(func(prefix, args string) {
			mu.Lock()
			defer mu.Unlock()
			captured = args
		}, funcr.Options{})

		slogAdapter := logr.ToSlogHandler(baseLogger)
		slogLogger := slog.New(slogAdapter)
		slogLogger.Info("slog adapter message", "service", "payment-service", "status", "ok")

		mu.Lock()
		out := captured
		mu.Unlock()

		if !strings.Contains(out, `"msg"="slog adapter message"`) && !strings.Contains(out, `slog adapter message`) {
			fmt.Fprintf(os.Stderr, "FAIL: slog adapter did not route message properly: %s\n", out)
			os.Exit(1)
		}
		if !strings.Contains(out, "service") || !strings.Contains(out, "payment-service") {
			fmt.Fprintf(os.Stderr, "FAIL: slog adapter missing key-values: %s\n", out)
			os.Exit(1)
		}
	}

	// Contract 3: logr.Marshaler interface marshals custom types via MarshalLog method
	{
		var captured string
		var mu sync.Mutex
		logger := funcr.New(func(prefix, args string) {
			mu.Lock()
			defer mu.Unlock()
			captured = args
		}, funcr.Options{})

		payload := AuditPayload{
			AccountID: "acc-9988",
			AuthToken: "super-secret-token",
		}
		logger.Info("audit verification", "audit", payload)

		mu.Lock()
		out := captured
		mu.Unlock()

		if strings.Contains(out, "super-secret-token") {
			fmt.Fprintf(os.Stderr, "FAIL: sensitive token leaked into logs: %s\n", out)
			os.Exit(1)
		}
		if !strings.Contains(out, "[REDACTED]") {
			fmt.Fprintf(os.Stderr, "FAIL: expected [REDACTED] in marshaled output: %s\n", out)
			os.Exit(1)
		}
		if !strings.Contains(out, "acc-9988") {
			fmt.Fprintf(os.Stderr, "FAIL: expected account ID in marshaled output: %s\n", out)
			os.Exit(1)
		}
	}

	// Contract 4: logr.NewContextWithSlogLogger and logr.FromContextAsSlogLogger manage slog.Logger across context boundaries
	{
		var buf bytes.Buffer
		slogLogger := slog.New(slog.NewTextHandler(&buf, nil))

		ctx := logr.NewContextWithSlogLogger(context.Background(), slogLogger)
		retrieved := logr.FromContextAsSlogLogger(ctx)
		if retrieved == nil {
			fmt.Fprintf(os.Stderr, "FAIL: FromContextAsSlogLogger returned nil\n")
			os.Exit(1)
		}

		retrieved.Info("context slog message", "tracerId", "trace-771")
		if !strings.Contains(buf.String(), "context slog message") || !strings.Contains(buf.String(), "trace-771") {
			fmt.Fprintf(os.Stderr, "FAIL: retrieved slog logger output mismatch: %s\n", buf.String())
			os.Exit(1)
		}

		nilLogger := logr.FromContextAsSlogLogger(context.Background())
		if nilLogger != nil {
			fmt.Fprintf(os.Stderr, "FAIL: expected nil logger from empty context\n")
			os.Exit(1)
		}
	}

	// Contract 5: logr.Discard safely drops all log entries without panicking
	{
		discard := logr.Discard()
		discard.Info("discard info message", "key", "value")
		discard.V(2).Info("discard verbose message")
		discard.WithName("discardSubsystem").Info("discard named message")
	}

	fmt.Println("PASS: github.com/go-logr/logr@v1.4.3 contract passed")
}

Origin Seeder

anonymous