CodeSampleX

Ejemplo

golang.org/x/tools v0.49.0

Muestra verificada para golang golang.org/x/tools v0.49.0. El contrato se ejecutó en go 1.26 · linux debian/x64 · docker y pasó.

sha256:7cc0f6a26eb180a4caec0328d4d8f57b1f622c1128f079e3522360c41679541e

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 go

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
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-02

Caso

HOW
Objetivo
verify pkg:golang/golang.org/x/tools@v0.49.0
Paquetes
Creado
2026-09-02T20:02:57Z

Contrato

  1. astutil.PathEnclosingInterval finds the enclosing AST node path from innermost node to root file for an exact identifier
  2. astutil.PathEnclosingInterval reports exact true when the query interval encompasses an isolated AST expression node
  3. astutil.PathEnclosingInterval reports exact false when the interval spans across multiple sibling statements
  4. astutil.PathEnclosingInterval treats zero-length intervals with identical start and end positions as single-byte lookups
  5. astutil.PathEnclosingInterval returns the enclosing binary expression node when positioned on an arithmetic operator

Archivos

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

Descargar el artefacto de código fuente (tar.gz)

Código fuente

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/golang.org/x/tools@v0.49.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/tools@v0.49.0

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.
astinterval.go
package astinterval

import (
	"fmt"
	"go/ast"
	"go/parser"
	"go/token"

	"golang.org/x/tools/go/ast/astutil"
)

// ParseSource parses a Go source code string into an AST file and token.FileSet.
func ParseSource(src string) (*token.FileSet, *ast.File, error) {
	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "src.go", src, 0)
	if err != nil {
		return nil, nil, fmt.Errorf("parse error: %w", err)
	}
	return fset, file, nil
}

// FindEnclosingPath returns the path of AST nodes enclosing the [start, end) interval.
func FindEnclosingPath(root *ast.File, start, end token.Pos) ([]ast.Node, bool) {
	return astutil.PathEnclosingInterval(root, start, end)
}

// FindInnermostNode returns the innermost AST node enclosing the [start, end) interval.
func FindInnermostNode(root *ast.File, start, end token.Pos) (ast.Node, bool) {
	path, exact := astutil.PathEnclosingInterval(root, start, end)
	if len(path) == 0 {
		return nil, false
	}
	return path[0], exact
}

// FindNodeAtPoint returns the AST node at a single point position.
func FindNodeAtPoint(root *ast.File, pos token.Pos) (ast.Node, bool) {
	return FindInnermostNode(root, pos, pos)
}
csx.json
{"case":{"caseId":"case:sha256:2fd3fddd4bab9df420a576fcb34e4c7f3d53bfed33cf1759092391475632764c","contract":["astutil.PathEnclosingInterval finds the enclosing AST node path from innermost node to root file for an exact identifier","astutil.PathEnclosingInterval reports exact true when the query interval encompasses an isolated AST expression node","astutil.PathEnclosingInterval reports exact false when the interval spans across multiple sibling statements","astutil.PathEnclosingInterval treats zero-length intervals with identical start and end positions as single-byte lookups","astutil.PathEnclosingInterval returns the enclosing binary expression node when positioned on an arithmetic operator"],"goal":"verify pkg:golang/golang.org/x/tools@v0.49.0","kind":"HOW","packages":["pkg:golang/golang.org/x/tools@v0.49.0"],"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/golang.org/x/tools@v0.49.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/tools@v0.49.0","verifierAdapter":"golang@1"}
go.mod
module example.com/astutil-interval

go 1.25.0

require golang.org/x/tools v0.49.0
go.sum
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/tools@v0.49.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/tools@v0.49.0"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"go/ast"
	"go/token"
	"os"

	"example.com/astutil-interval"
	"golang.org/x/tools/go/ast/astutil"
)

func main() {
	src := `package p

func sample() int {
	x := 42
	y := 10 + 20
	return x + y
}
`
	_, file, err := astinterval.ParseSource(src)
	if err != nil {
		fmt.Fprintf(os.Stderr, "FAIL: failed to parse source: %v\n", err)
		os.Exit(1)
	}

	var fnDecl *ast.FuncDecl
	for _, decl := range file.Decls {
		if fn, ok := decl.(*ast.FuncDecl); ok && fn.Name.Name == "sample" {
			fnDecl = fn
			break
		}
	}
	if fnDecl == nil || fnDecl.Body == nil || len(fnDecl.Body.List) < 3 {
		fmt.Fprintf(os.Stderr, "FAIL: expected sample function with at least 3 statements\n")
		os.Exit(1)
	}

	stmt1, ok1 := fnDecl.Body.List[0].(*ast.AssignStmt)
	stmt2, ok2 := fnDecl.Body.List[1].(*ast.AssignStmt)
	if !ok1 || !ok2 {
		fmt.Fprintf(os.Stderr, "FAIL: expected assignment statements in function body\n")
		os.Exit(1)
	}

	// Assertion 1: astutil.PathEnclosingInterval finds the enclosing AST node path from innermost node to root file for an exact identifier
	{
		identX, ok := stmt1.Lhs[0].(*ast.Ident)
		if !ok || identX.Name != "x" {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected lhs identifier 'x'\n")
			os.Exit(1)
		}

		path, exact := astutil.PathEnclosingInterval(file, identX.Pos(), identX.End())
		if len(path) == 0 {
			fmt.Fprintf(os.Stderr, "FAIL 1: path is empty\n")
			os.Exit(1)
		}
		if innermost, ok := path[0].(*ast.Ident); !ok || innermost.Name != "x" {
			fmt.Fprintf(os.Stderr, "FAIL 1: innermost node is not identifier 'x', got %T\n", path[0])
			os.Exit(1)
		}
		if _, ok := path[len(path)-1].(*ast.File); !ok {
			fmt.Fprintf(os.Stderr, "FAIL 1: root node is not *ast.File, got %T\n", path[len(path)-1])
			os.Exit(1)
		}
		if !exact {
			fmt.Fprintf(os.Stderr, "FAIL 1: expected exact=true for complete identifier interval\n")
			os.Exit(1)
		}
	}

	// Assertion 2: astutil.PathEnclosingInterval reports exact true when the query interval encompasses an isolated AST expression node
	{
		lit, ok := stmt1.Rhs[0].(*ast.BasicLit)
		if !ok || lit.Value != "42" {
			fmt.Fprintf(os.Stderr, "FAIL 2: expected rhs basic lit '42'\n")
			os.Exit(1)
		}

		node, exact := astinterval.FindInnermostNode(file, lit.Pos(), lit.End())
		if !exact {
			fmt.Fprintf(os.Stderr, "FAIL 2: expected exact=true for basic lit interval\n")
			os.Exit(1)
		}
		if matchedLit, ok := node.(*ast.BasicLit); !ok || matchedLit.Value != "42" {
			fmt.Fprintf(os.Stderr, "FAIL 2: expected BasicLit '42', got %v\n", node)
			os.Exit(1)
		}
	}

	// Assertion 3: astutil.PathEnclosingInterval reports exact false when the interval spans across multiple sibling statements
	{
		path, exact := astutil.PathEnclosingInterval(file, stmt1.Pos(), stmt2.End())
		if exact {
			fmt.Fprintf(os.Stderr, "FAIL 3: expected exact=false for multi-statement spanning range\n")
			os.Exit(1)
		}
		if len(path) == 0 {
			fmt.Fprintf(os.Stderr, "FAIL 3: expected non-empty path\n")
			os.Exit(1)
		}
		if _, ok := path[0].(*ast.BlockStmt); !ok {
			fmt.Fprintf(os.Stderr, "FAIL 3: expected enclosing node to be *ast.BlockStmt, got %T\n", path[0])
			os.Exit(1)
		}
	}

	// Assertion 4: astutil.PathEnclosingInterval treats zero-length intervals with identical start and end positions as single-byte lookups
	{
		identX := stmt1.Lhs[0].(*ast.Ident)
		node, exact := astinterval.FindNodeAtPoint(file, identX.Pos())
		if !exact {
			fmt.Fprintf(os.Stderr, "FAIL 4: expected exact=true for point lookup at identifier start\n")
			os.Exit(1)
		}
		if ident, ok := node.(*ast.Ident); !ok || ident.Name != "x" {
			fmt.Fprintf(os.Stderr, "FAIL 4: expected ident 'x' on zero-length lookup, got %T\n", node)
			os.Exit(1)
		}
	}

	// Assertion 5: astutil.PathEnclosingInterval returns the enclosing binary expression node when positioned on an arithmetic operator
	{
		binExpr, ok := stmt2.Rhs[0].(*ast.BinaryExpr)
		if !ok || binExpr.Op != token.ADD {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected binary expression with addition\n")
			os.Exit(1)
		}

		path, exact := astutil.PathEnclosingInterval(file, binExpr.OpPos, binExpr.OpPos+1)
		if !exact {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected exact=true for operator in binary expr\n")
			os.Exit(1)
		}
		if len(path) == 0 {
			fmt.Fprintf(os.Stderr, "FAIL 5: path is empty\n")
			os.Exit(1)
		}
		if matchedBin, ok := path[0].(*ast.BinaryExpr); !ok || matchedBin.Op != token.ADD {
			fmt.Fprintf(os.Stderr, "FAIL 5: expected enclosing *ast.BinaryExpr with '+', got %T\n", path[0])
			os.Exit(1)
		}
	}

	fmt.Println("PASS: all astutil.PathEnclosingInterval contract assertions verified")
}

Seeder de origen

anónimo