CodeSampleX

サンプル

github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8

検証済みサンプル — golang github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:f56888f6865ee7b3f2e0abcc066bf3b0c4a8f74aa60d18ff0fa7d4a87e241d08

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 go 1.26 linux 24 · ubuntu · glibc 2.39 x64 go 1.26 go go 1

検証実行環境

環境 コントラクト ステージ 実行日
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-19

ケース

HOW
ゴール
verify pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8
パッケージ
環境
go 1.26.6
作成日
2026-09-19T03:39:32Z

コントラクト

  1. conc.WaitGroup executes concurrent tasks and waits for completion
  2. conc.WaitGroup.WaitAndRecover catches panics from child goroutines and returns a Recovered object
  3. pool.NewWithResults executes tasks with a concurrency limit and collects results
  4. iter.Map concurrently transforms slice elements while preserving input order
  5. conc.WaitGroup propagates panic to caller when Wait is called

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • sample.go
  • 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/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8
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:bfea8aa16932cac9cef16239cb3b0828764e0ca179e2dabfb98179343f8f033a","contract":["conc.WaitGroup executes concurrent tasks and waits for completion","conc.WaitGroup.WaitAndRecover catches panics from child goroutines and returns a Recovered object","pool.NewWithResults executes tasks with a concurrency limit and collects results","iter.Map concurrently transforms slice elements while preserving input order","conc.WaitGroup propagates panic to caller when Wait is called"],"goal":"verify pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8","kind":"HOW","packages":["pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8"],"schemaVersion":1},"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/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8"],"schemaVersion":1,"subject":"pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8","verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8
go.sum
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
sample.go
package sample

import (
	"github.com/sourcegraph/conc"
	"github.com/sourcegraph/conc/iter"
	"github.com/sourcegraph/conc/panics"
	"github.com/sourcegraph/conc/pool"
)

// RunConcurrentTasks executes all given functions in separate goroutines using conc.WaitGroup.
func RunConcurrentTasks(tasks []func()) {
	var wg conc.WaitGroup
	for _, task := range tasks {
		wg.Go(task)
	}
	wg.Wait()
}

// RunSafeTask executes a task with panic catching via conc.WaitGroup.WaitAndRecover.
func RunSafeTask(task func()) *panics.Recovered {
	var wg conc.WaitGroup
	wg.Go(task)
	return wg.WaitAndRecover()
}

// ComputeResultsWithPool runs tasks returning results up to maxConcurrency goroutines.
func ComputeResultsWithPool[T any](maxConcurrency int, tasks []func() T) []T {
	p := pool.NewWithResults[T]().WithMaxGoroutines(maxConcurrency)
	for _, task := range tasks {
		p.Go(task)
	}
	return p.Wait()
}

// MapSlice maps elements concurrently using conc/iter.Map.
func MapSlice[T any, R any](input []T, f func(*T) R) []R {
	return iter.Map(input, f)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/sourcegraph/conc@v0.3.1-0.20240121214520-5f936abd7ae8"
  ],
  "runtimeConditions": {
    "ecosystem": "golang",
    "language": "go",
    "packageManager": "go@1.26.6",
    "runtime": "go@1.26.6"
  }
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"sync/atomic"

	"github.com/sourcegraph/conc"
	"sample"
)

func main() {
	if err := runTests(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract test failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All contract tests passed successfully.")
}

func runTests() error {
	// 1. conc.WaitGroup executes concurrent tasks and waits for completion
	{
		var count int64
		numTasks := 10
		tasks := make([]func(), numTasks)
		for i := 0; i < numTasks; i++ {
			tasks[i] = func() {
				atomic.AddInt64(&count, 1)
			}
		}
		sample.RunConcurrentTasks(tasks)
		if atomic.LoadInt64(&count) != int64(numTasks) {
			return fmt.Errorf("test 1: expected count %d, got %d", numTasks, count)
		}
	}

	// 2. conc.WaitGroup.WaitAndRecover catches panics from child goroutines and returns a Recovered object
	{
		panicMsg := "test panic message"
		recovered := sample.RunSafeTask(func() {
			panic(panicMsg)
		})
		if recovered == nil {
			return fmt.Errorf("test 2: expected non-nil Recovered, got nil")
		}
		if recovered.Value != panicMsg {
			return fmt.Errorf("test 2: expected panic value %q, got %v", panicMsg, recovered.Value)
		}
	}

	// 3. pool.NewWithResults executes tasks with a concurrency limit and collects results
	{
		tasks := []func() int{
			func() int { return 1 },
			func() int { return 2 },
			func() int { return 3 },
			func() int { return 4 },
		}
		results := sample.ComputeResultsWithPool(2, tasks)
		expected := []int{1, 2, 3, 4}
		if len(results) != len(expected) {
			return fmt.Errorf("test 3: expected length %d, got %d", len(expected), len(results))
		}
		for i, v := range results {
			if v != expected[i] {
				return fmt.Errorf("test 3: index %d expected %d, got %d", i, expected[i], v)
			}
		}
	}

	// 4. iter.Map concurrently transforms slice elements while preserving input order
	{
		input := []int{1, 2, 3, 4, 5}
		mapped := sample.MapSlice(input, func(val *int) int {
			return (*val) * 10
		})
		expected := []int{10, 20, 30, 40, 50}
		if len(mapped) != len(expected) {
			return fmt.Errorf("test 4: expected length %d, got %d", len(expected), len(mapped))
		}
		for i, v := range mapped {
			if v != expected[i] {
				return fmt.Errorf("test 4: index %d expected %d, got %d", i, expected[i], v)
			}
		}
	}

	// 5. conc.WaitGroup propagates panic to caller when Wait is called
	{
		panicked := false
		func() {
			defer func() {
				if r := recover(); r != nil {
					panicked = true
				}
			}()
			var wg conc.WaitGroup
			wg.Go(func() {
				panic("propagated panic")
			})
			wg.Wait()
		}()
		if !panicked {
			return fmt.Errorf("test 5: expected wg.Wait() to propagate child panic, but did not panic")
		}
	}

	return nil
}

オリジンシーダー

匿名