Reduce comparison bundle recognition reads

This commit is contained in:
2026-08-13 03:52:20 +00:00
parent 0516ee148d
commit fb891fad07
4 changed files with 210 additions and 31 deletions

View File

@@ -4,6 +4,8 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
@@ -222,6 +224,75 @@ func TestPlanDestinationAcceptsRecognizedReplacement(t *testing.T) {
}
}
func TestRecognizeBundleReadsOnlyRequiredArtifactContents(t *testing.T) {
for _, reportCount := range []int{2, 12} {
t.Run(fmt.Sprintf("%d reports", reportCount), func(t *testing.T) {
bundle := testBundleWithReports(t, reportCount)
directory := publishTestBundle(t, bundle)
bytesRead := make(map[string]int)
opens := make(map[string]int)
operations := defaultRecognitionOperations
operations.open = func(path string) (io.ReadCloser, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
name := filepath.Base(path)
opens[name]++
return &countingReadCloser{ReadCloser: file, count: func(n int) {
bytesRead[name] += n
}}, nil
}
if _, err := recognizeBundle(directory, operations); err != nil {
t.Fatalf("recognizeBundle() error = %v", err)
}
for _, name := range []string{ManifestFilename, DataPackageFilename} {
data := readFile(t, filepath.Join(directory, name))
if opens[name] != 1 || bytesRead[name] != len(data) {
t.Fatalf("%s opens/bytes = %d/%d, want 1/%d", name, opens[name], bytesRead[name], len(data))
}
}
for _, report := range bundle.Reports {
if opens[report.Path] != 1 || bytesRead[report.Path] != 0 {
t.Fatalf("report %s opens/bytes = %d/%d, want 1/0", report.Path, opens[report.Path], bytesRead[report.Path])
}
}
})
}
}
func TestReplacementUsesEarlyAndFinalRecognition(t *testing.T) {
workingDirectory := t.TempDir()
target := filepath.Join(workingDirectory, "comparison-daily")
bundle := testBundleWithReports(t, 12)
initialPlan, err := PlanDestination(workingDirectory, target, false)
if err != nil {
t.Fatal(err)
}
if _, err := Publish(context.Background(), initialPlan, bundle); err != nil {
t.Fatal(err)
}
recognitions := 0
recognize := func(directory string) (Manifest, error) {
recognitions++
return RecognizeBundle(directory)
}
plan, err := planDestination(workingDirectory, target, true, recognize)
if err != nil {
t.Fatal(err)
}
if _, err := publish(context.Background(), plan, bundle, publishOperations{
rename: os.Rename, removeAll: os.RemoveAll, recognize: recognize,
}); err != nil {
t.Fatal(err)
}
if recognitions != 2 {
t.Fatalf("full bundle recognitions = %d, want early and final authorization", recognitions)
}
}
func TestPublishReauthorizesMovedDestination(t *testing.T) {
tests := []struct {
name string
@@ -995,6 +1066,41 @@ func testBundle() LogicalBundle {
}
}
func testBundleWithReports(t *testing.T, count int) LogicalBundle {
t.Helper()
dataPackage := []byte("report: daily\n")
manifest := validManifest()
manifest.Total, manifest.Succeeded, manifest.Failed = count, count, 0
manifest.Results = make([]Result, count)
reports := make([]BundleReport, count)
for i := range manifest.Results {
position := i + 1
profileID := fmt.Sprintf("weather-%d", position)
path, err := ReportFilename(position, count, profileID)
if err != nil {
t.Fatal(err)
}
manifest.Results[i] = Result{
Position: position, ProfileID: profileID, ModelName: "gpt-5-mini",
Status: StatusSucceeded, ValidationStatus: "passed", ReportPath: path,
}
reports[i] = BundleReport{Position: position, Path: path, Markdown: []byte("# Daily\n")}
}
manifest.DataPackage.SHA256 = SHA256(dataPackage)
return LogicalBundle{Manifest: manifest, DataPackage: dataPackage, Reports: reports}
}
type countingReadCloser struct {
io.ReadCloser
count func(int)
}
func (reader *countingReadCloser) Read(buffer []byte) (int, error) {
n, err := reader.ReadCloser.Read(buffer)
reader.count(n)
return n, err
}
func assertMode(t *testing.T, path string, want os.FileMode) {
t.Helper()
info, err := os.Stat(path)