package main import ( "bytes" "encoding/json" "fmt" "os" "os/exec" "path/filepath" "time" ) type observation struct { Composition struct { Steps []struct { Generation struct { Report struct { Assembly struct { Selected int `json:"selected_mask"` Calls int `json:"model_calls"` Predict int64 `json:"predict_ns"` Passed int `json:"passed"` Total int `json:"total"` FieldsPassed int `json:"fields_passed"` FieldsTotal int `json:"fields_total"` Attempts []struct { Mask int `json:"mask"` } `json:"attempts"` Prediction struct { Proposed int `json:"proposed_mask"` } `json:"prediction"` } `json:"record_assembly"` } `json:"report"` } `json:"generation"` } `json:"steps"` } `json:"composition"` Runtime struct { Passed int `json:"finite_passed"` Total int `json:"finite_total"` Calls int `json:"model_calls"` Replayed bool `json:"runtime_replayed"` } `json:"runtime"` } type row struct { Swap int `json:"swapped_choice_mask"` Mode string `json:"mode"` Expected int `json:"expected_selected_mask"` Selected int `json:"selected_mask"` First int `json:"first_attempt_mask"` Attempts int `json:"attempted_candidates"` FirstCorrect bool `json:"first_candidate_correct"` Predict int64 `json:"prediction_ns"` ModelCalls int `json:"construction_model_calls"` NativePassed int `json:"native_passed"` NativeTotal int `json:"native_total"` Wall int64 `json:"command_wall_ns"` File string `json:"observation_file"` } func require(ok bool, message string) { if !ok { panic(message) } } func replace(b []byte, before, after string) []byte { require(bytes.Count(b, []byte(before)) == 1, "replacement must be unique: "+before) return bytes.Replace(b, []byte(before), []byte(after), 1) } func main() { require(len(os.Args) == 6, "arguments: binary, source, cases, model, output") binary, sourcePath, cases, model, out := os.Args[1], os.Args[2], os.Args[3], os.Args[4], os.Args[5] source, err := os.ReadFile(sourcePath) if err != nil { panic(err) } if err = os.Mkdir(out, 0755); err != nil { panic(err) } rows := []row{} for mask := 0; mask < 8; mask++ { variant := append([]byte(nil), source...) if mask&1 != 0 { variant = replace(variant, "source: suffix || visible", "source: suffix && visible") variant = replace(variant, `alternative "suffix && visible"`, `alternative "suffix || visible"`) } if mask&2 != 0 { variant = replace(variant, "stem: input, bytes:", "stem: stem, bytes:") variant = replace(variant, `alternative "stem"`, `alternative "input"`) } if mask&4 != 0 { variant = replace(variant, "bytes: 0}", "bytes: bytes}") variant = replace(variant, `alternative "bytes"`, `alternative "0"`) } path := filepath.Join(out, fmt.Sprintf("order-%d.gooo", mask)) if err = os.WriteFile(path, variant, 0644); err != nil { panic(err) } for _, mode := range []string{"fixed", "model"} { args := []string{"body-compose", "--entry", "Classify", "--source", path, "--cases", cases} if mode == "model" { args = append(args, "--model", model) } command := exec.Command(binary, args...) var stderr bytes.Buffer command.Stderr = &stderr start := time.Now() raw, err := command.Output() elapsed := time.Since(start).Nanoseconds() if err != nil { panic(fmt.Sprintf("mask %d %s: %s %v", mask, mode, stderr.String(), err)) } filename := fmt.Sprintf("order-%d-%s.json", mask, mode) if err = os.WriteFile(filepath.Join(out, filename), raw, 0644); err != nil { panic(err) } var result observation if err = json.Unmarshal(raw, &result); err != nil { panic(err) } require(len(result.Composition.Steps) == 1, "one classifier step required") a := result.Composition.Steps[0].Generation.Report.Assembly expected := 7 ^ mask require(a.Selected == expected && a.Passed == 5 && a.Total == 5 && a.FieldsPassed == 15 && a.FieldsTotal == 15, "construction semantics changed") require(len(a.Attempts) > 0, "no attempted candidate") r := result.Runtime require(r.Passed == 12 && r.Total == 12 && r.Calls == 0 && r.Replayed, "native result changed") if mode == "model" { require(a.Calls == 1, "model must predict once") } else { require(a.Calls == 0, "fixed mode used model") } rows = append(rows, row{mask, mode, expected, a.Selected, a.Attempts[0].Mask, len(a.Attempts), a.Attempts[0].Mask == expected, a.Predict, a.Calls, r.Passed, r.Total, elapsed, filename}) } } encoded, err := json.MarshalIndent(map[string]any{"schema": "gooo/filename-choice-order-ablation/v1", "rows": rows, "scope": "One filename policy under all eight independent front/back swaps of its three choices. Same construction and runtime cases. No retraining. Wall time includes compiler construction and native build/run; caches and host load uncontrolled."}, "", " ") if err != nil { panic(err) } if err = os.WriteFile(filepath.Join(out, "summary.json"), append(encoded, '\n'), 0644); err != nil { panic(err) } fmt.Println(string(encoded)) }