Files
DeepSeek-Reasonix/cmd/e2ebench/swebench.go
T
YHH 4fbbca6a99 refactor(go): adopt modern stdlib idioms and gate them (#7831)
* refactor(go): adopt modern stdlib idioms across kernel and desktop

Mechanical sweep with golangci-lint's modernize/intrange/copyloopvar/
usestdlibvars fixers plus hand-fixes for what they could not rewrite:
slices.Contains, min/max, slices.Backward, WaitGroup.Go, reflect.TypeFor,
maps.Copy, strings.Cut/CutPrefix/SplitSeq, strings.Builder, range-over-int.
Net -549 lines with no behavior change.

Three loops in session.go stay index-based: slices.Backward yields element
copies, so the fixer's rewrite silently dropped writes to DecisionReceipts
and ToolCalls (caught by TestDelivery* and TestDependentSameBatch*). All 49
slices.Backward sites were audited for that shape; only these three write
through the index, and they carry a nolint with the reason.

CaptureAfter's scan loop had an empty body and never ran any logic, so the
fixer could not express it. Drop it and the accessor it was the only caller
of. omitzero is deliberately untouched: turning omitempty into omitzero
changes the JSON written for nested structs, which is a wire-format change
to session files and the extension protocol.

* build(lint): gate the modern-idiom linters

Nothing stopped the codebase from drifting back to pre-1.22 idioms. Both
modules are now clean for modernize, intrange, copyloopvar, and
usestdlibvars, so enable them rather than leaving the sweep as a one-off.

omitzero stays disabled with the reason inline; usetesting, perfsprint, and
errorlint still have residue and land separately.
2026-08-07 15:30:33 +08:00

158 lines
5.6 KiB
Go

package main
import (
"encoding/json"
"fmt"
"slices"
"strings"
"reasonix/internal/ablation"
)
// SWE-bench mode runs the agent inside the official per-instance evaluation
// container, so it can execute the repo's tests exactly like the harnesses it
// is compared against, then hands the resulting patch to the official grader.
type swebenchInstance struct {
InstanceID string `json:"instance_id"`
Repo string `json:"repo"`
BaseCommit string `json:"base_commit"`
Problem string `json:"problem_statement"`
Difficulty string `json:"difficulty"`
}
// swebenchImage builds the registry name of an instance's evaluation image.
// The harness mangles "__" to "_1776_" only when a namespace is set, so the
// local image key it prints is not the name you can pull.
func swebenchImage(namespace, instanceID string) string {
return fmt.Sprintf("%s/sweb.eval.x86_64.%s:latest",
namespace, strings.ReplaceAll(instanceID, "__", "_1776_"))
}
// swebenchContainer is the throwaway container name for one attempt. It is
// distinct from the grader's own container so a stale agent container can never
// be mistaken for an evaluation in progress.
func swebenchContainer(instanceID string) string {
return "rxagent." + strings.ReplaceAll(instanceID, "__", ".")
}
// testbedShell wraps a command so it runs against the instance's conda
// environment. The images ship miniconda with the repo's dependencies in an env
// named "testbed"; a bare `docker exec` misses it and every import fails.
func testbedShell(command string) []string {
return []string{"bash", "-lc",
"source /opt/miniconda3/bin/activate && conda activate testbed && cd /testbed && " + command}
}
// permissionFlag maps a benchmark permission posture onto the CLI flag. auto is
// the unattended default. The alternative posture exists because comparable
// harnesses run without the dynamic-shell gate, so measuring against them under
// the gate measures our permission policy rather than the agent.
func permissionFlag(mode string) (string, error) {
switch mode {
case "", "auto":
return "--permission-mode=auto", nil
case "yolo":
return "--permission-mode=bypassPermissions", nil
default:
return "", fmt.Errorf("unknown permission mode %q (want auto or yolo)", mode)
}
}
func swebenchAgentArgs(metricsPath, model, profile, permission string, arm ablation.Set, maxSteps int, prompt string) []string {
posture, err := permissionFlag(permission)
if err != nil {
panic(err) // validated at flag-parse time; reaching here is a wiring bug
}
args := []string{"run", posture, "--metrics", metricsPath}
if model != "" {
args = append(args, "--model", model)
}
if maxSteps > 0 {
args = append(args, "--max-steps", fmt.Sprint(maxSteps))
}
args = appendBenchmarkProfileArgs(args, profile)
if !arm.Empty() {
args = append(args, "--ablate", arm.String())
}
return append(args, prompt)
}
// swebenchPrompt is the task text the agent sees. It carries the issue and the
// working rules, and deliberately withholds the test patch and the
// FAIL_TO_PASS list — those are the answer key.
func swebenchPrompt(inst swebenchInstance) string {
var b strings.Builder
b.WriteString("Resolve the following issue in the repository at /testbed.\n\n")
b.WriteString("<issue>\n")
b.WriteString(strings.TrimSpace(inst.Problem))
b.WriteString("\n</issue>\n\n")
b.WriteString("The repository is a git checkout at the commit where the issue reproduces. ")
b.WriteString("Edit the source to fix it, and run the project's own tests to check your work. ")
b.WriteString("Do not commit, and do not modify any test file — the fix is graded by tests you cannot see.\n")
return b.String()
}
// swebenchPrediction is one line of the predictions file the official grader
// reads. Field names are the harness's, not ours.
type swebenchPrediction struct {
InstanceID string `json:"instance_id"`
Model string `json:"model_name_or_path"`
Patch string `json:"model_patch"`
}
func encodePredictions(model string, patches map[string]string, order []string) (string, error) {
var b strings.Builder
for _, id := range order {
patch, ok := patches[id]
if !ok {
continue
}
line, err := json.Marshal(swebenchPrediction{InstanceID: id, Model: model, Patch: patch})
if err != nil {
return "", err
}
b.Write(line)
b.WriteByte('\n')
}
return b.String(), nil
}
// swebenchReport is the subset of the grader's JSON summary we consume. Unknown
// fields are ignored so a harness upgrade that adds counters does not break us.
type swebenchReport struct {
ResolvedIDs []string `json:"resolved_ids"`
UnresolvedIDs []string `json:"unresolved_ids"`
ErrorIDs []string `json:"error_ids"`
EmptyPatchIDs []string `json:"empty_patch_ids"`
IncompleteIDs []string `json:"incomplete_ids"`
}
// swebenchReportPath is where run_evaluation writes its summary: the model name
// from the predictions file joined with the run id, in the working directory.
func swebenchReportPath(model, runID string) string {
return model + "." + runID + ".json"
}
// gradedClass maps one instance's grader outcome onto our published failure
// taxonomy. An id the grader never mentions is reported as unknown rather than
// silently counted as unsolved.
func (r swebenchReport) gradedClass(instanceID string) string {
if slices.Contains(r.ResolvedIDs, instanceID) {
return "solved"
}
if slices.Contains(r.EmptyPatchIDs, instanceID) {
return "no_patch"
}
if slices.Contains(r.ErrorIDs, instanceID) {
return "grader_error"
}
if slices.Contains(r.IncompleteIDs, instanceID) {
return "eval_timeout"
}
if slices.Contains(r.UnresolvedIDs, instanceID) {
return "wrong_patch"
}
return "ungraded"
}