CodeSampleX

示例

golang.org/x/tools v0.49.0

已验证示例 — golang golang.org/x/tools v0.49.0. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: astutil.PathEnclosingInterval finds the enclosing AST node…

sha256:7cc0f6a26eb180a4caec0328d4d8f57b1f622c1128f079e3522360c41679541e

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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

案例

HOW
目标
verify pkg:golang/golang.org/x/tools@v0.49.0
包
创建时间
2026-09-02T20:02:57Z

契约

  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

文件

  • PROMPT.md
  • astinterval.go
  • csx.json
  • go.mod
  • go.sum
  • 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/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")
}

原始种子者

匿名