diff --git a/docs/roadmap/implementation.md b/docs/roadmap/implementation.md index a4897a9..d11a765 100644 --- a/docs/roadmap/implementation.md +++ b/docs/roadmap/implementation.md @@ -176,6 +176,8 @@ dispatch. This stage does not repair CAT-13's missing planning collaborator. ## Stage 4: Make operator inspection consume the same selections +**Complete.** Inspection shares discovery; real preparation and read-only inspection agree in local and S3 regressions. + Read `internal/app/operator_inspection.go` and its local/remote inspection tests. Work: diff --git a/internal/app/audio_inspection_consistency_test.go b/internal/app/audio_inspection_consistency_test.go new file mode 100644 index 0000000..37fc28a --- /dev/null +++ b/internal/app/audio_inspection_consistency_test.go @@ -0,0 +1,113 @@ +package app + +import ( + "context" + "os" + "path/filepath" + "testing" + "time" + + "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" + "gitea.maximumdirect.net/eric/narratio/internal/artifacts" + "gitea.maximumdirect.net/eric/narratio/internal/config" + "gitea.maximumdirect.net/eric/narratio/internal/manifest" + "gitea.maximumdirect.net/eric/narratio/internal/stage" +) + +func TestAudioInspectionAgreesWithPreparation(t *testing.T) { + for _, tc := range []struct { + name string + files []string + directory bool + reject bool + }{ + {name: "same basename", files: []string{"first/speaker.flac", "second/speaker.flac"}}, + {name: "explicit mixed", files: []string{"first/one.mp3", "second/two.unknown"}}, + {name: "directory mixed", files: []string{"audio/one.mp3", "audio/two.WAV"}, directory: true}, + {name: "repeated path", files: []string{"first/one.flac", "first/one.flac"}, reject: true}, + {name: "same stem", files: []string{"first/one.mp3", "second/one.wav"}, reject: true}, + } { + t.Run(tc.name, func(t *testing.T) { + cfg := testConfig(t) + root := filepath.Dir(cfg.SessionPath) + for _, name := range tc.files { + mustWriteFile(t, filepath.Join(root, name), "audio") + } + if tc.directory { + cfg.Session.Inputs.AudioFiles = nil + } else { + cfg.Session.Inputs.AudioFiles = tc.files + } + check := inspectLocalAudioPresence(cfg) + if (check.Err != nil) != tc.reject { + t.Fatalf("inspection error: %v", check.Err) + } + entries, err := os.ReadDir(cfg.Pipeline.Workspace.Root) + if err != nil || len(entries) != 0 { + t.Fatalf("inspection changed workspace: %v %v", entries, err) + } + m := manifest.New(cfg.Session.SessionID, time.Now().UTC()) + var prepare stage.Stage + for _, candidate := range stage.All() { + if candidate.Name() == "prepare" { + prepare = candidate + break + } + } + _, err = prepare.Run(context.Background(), &stage.Env{Config: cfg, ArtifactStore: artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root)}, m) + if (err != nil) != tc.reject { + t.Fatalf("prepare error: %v", err) + } + if !tc.reject { + count := 0 + for _, input := range m.Inputs { + if input.Kind == "audio" { + count++ + } + } + if count != len(check.Paths) { + t.Fatalf("inspection paths %v, prepared %d", check.Paths, count) + } + } + }) + } +} + +func TestRemoteAudioInspectionAgreesWithPreparation(t *testing.T) { + for _, collision := range []bool{false, true} { + cfg := testConfig(t) + cfg.Session.Inputs.AudioDir = "" + cfg.Session.Inputs.AudioS3 = &config.SessionAudioS3Input{Prefix: "audio/"} + cfg.Pipeline.Storage.S3 = &config.StorageS3Config{Bucket: "test", RootPrefix: "root"} + prefix := artifacts.S3AudioPrefix(artifacts.S3SessionPrefix("root", cfg.Session.Campaign, cfg.Session.SessionID), "audio/") + fake := &storage.FakeBackend{} + fake.SeedObject(storage.FakeObject{Key: prefix + "one.flac", Data: []byte("one")}) + name := "two.mp3" + if collision { + name = "one.mp3" + } + fake.SeedObject(storage.FakeObject{Key: prefix + name, Data: []byte("two")}) + check := inspectRemoteAudioPresence(context.Background(), cfg, fake) + if (check.Err != nil) != collision { + t.Fatalf("inspection error: %v", check.Err) + } + if len(fake.Reads) != 0 { + t.Fatal("inspection downloaded audio") + } + entries, err := os.ReadDir(cfg.Pipeline.Workspace.Root) + if err != nil || len(entries) != 0 { + t.Fatalf("inspection changed workspace: %v %v", entries, err) + } + m := manifest.New(cfg.Session.SessionID, time.Now().UTC()) + m.RunID = "20260515T031522Z-a1b2c3d4" + for _, candidate := range stage.All() { + if candidate.Name() == "prepare" { + _, err = candidate.Run(context.Background(), &stage.Env{Config: cfg, ArtifactStore: artifacts.NewLocalStore(cfg.Pipeline.Workspace.Root), ObjectStore: fake}, m) + break + } + } + if (err != nil) != collision { + t.Fatalf("prepare error: %v", err) + } + } +} diff --git a/internal/app/operator_inspection.go b/internal/app/operator_inspection.go index 93f02c9..3d38255 100644 --- a/internal/app/operator_inspection.go +++ b/internal/app/operator_inspection.go @@ -5,13 +5,11 @@ import ( "errors" "fmt" "os" - "path" "path/filepath" - "sort" - "strings" "gitea.maximumdirect.net/eric/narratio/internal/adapters/storage" "gitea.maximumdirect.net/eric/narratio/internal/artifacts" + "gitea.maximumdirect.net/eric/narratio/internal/audio" "gitea.maximumdirect.net/eric/narratio/internal/config" "gitea.maximumdirect.net/eric/narratio/internal/previouscache" ) @@ -88,13 +86,17 @@ func inspectLocalAudioPresence(cfg *config.Config) localAudioCheck { } sessionDir := filepath.Dir(cfg.SessionPath) - resolved, err := resolveLocalInspectionAudioPaths(sessionDir, cfg.Session.Inputs) + sources, err := audio.DiscoverLocal(sessionDir, cfg.Session.Inputs.AudioFiles, cfg.Session.Inputs.AudioDir) if err != nil { return localAudioCheck{Checked: true, Err: err} } + paths := make([]string, 0, len(sources)) + for _, source := range sources { + paths = append(paths, source.Path) + } return localAudioCheck{ Checked: true, - Paths: resolved, + Paths: paths, } } @@ -108,37 +110,15 @@ func inspectRemoteAudioPresence(ctx context.Context, cfg *config.Config, store s sessionPrefix := artifacts.S3SessionPrefix(cfg.Pipeline.Storage.S3.RootPrefix, cfg.Session.Campaign, cfg.Session.SessionID) audioPrefix := artifacts.S3AudioPrefix(sessionPrefix, cfg.Session.Inputs.AudioS3.Prefix) - objects, err := store.List(ctx, audioPrefix) + objects, err := audio.DiscoverS3(ctx, store, audioPrefix) if err != nil { return remoteAudioCheck{Checked: true, Prefix: audioPrefix, Err: err} } - keys := make([]string, 0, len(objects)) - seenBase := map[string]string{} for _, obj := range objects { - key := strings.TrimSpace(obj.Key) - if key == "" || strings.HasSuffix(key, "/") || !isInspectionFlacPath(key) { - continue - } - base := path.Base(key) - if prev, exists := seenBase[base]; exists && prev != key { - return remoteAudioCheck{ - Checked: true, - Prefix: audioPrefix, - Err: fmt.Errorf("duplicate s3 audio basename %q from %q and %q", base, prev, key), - } - } - seenBase[base] = key - keys = append(keys, key) - } - sort.Strings(keys) - if len(keys) == 0 { - return remoteAudioCheck{ - Checked: true, - Prefix: audioPrefix, - Err: fmt.Errorf("no .flac files found under s3 audio prefix %q", audioPrefix), - } + keys = append(keys, obj.Key) } + return remoteAudioCheck{ Checked: true, Prefix: audioPrefix, @@ -204,72 +184,6 @@ func inspectRemoteLocks(ctx context.Context, cfg *config.Config, store storage.O return remoteLocksCheck{Locks: locks} } -func resolveLocalInspectionAudioPaths(sessionDir string, inputs config.SessionInputsConfig) ([]string, error) { - if len(inputs.AudioFiles) > 0 { - out := make([]string, 0, len(inputs.AudioFiles)) - seenBase := map[string]string{} - for _, item := range inputs.AudioFiles { - resolved, err := resolveInspectionPath(sessionDir, item) - if err != nil { - return nil, err - } - if !isInspectionFlacPath(resolved) { - return nil, fmt.Errorf("audio file %q must have .flac extension", resolved) - } - if err := requireInspectionFile(resolved, "audio file"); err != nil { - return nil, err - } - base := filepath.Base(resolved) - if prev, exists := seenBase[base]; exists && prev != resolved { - return nil, fmt.Errorf("duplicate audio basename %q from %q and %q", base, prev, resolved) - } - seenBase[base] = resolved - out = append(out, resolved) - } - sort.Strings(out) - return out, nil - } - - audioDir, err := resolveInspectionPath(sessionDir, inputs.AudioDir) - if err != nil { - return nil, err - } - entries, err := os.ReadDir(audioDir) - if err != nil { - return nil, fmt.Errorf("read audio directory %q: %w", audioDir, err) - } - out := make([]string, 0, len(entries)) - for _, entry := range entries { - if entry.IsDir() { - continue - } - full := filepath.Join(audioDir, entry.Name()) - if !isInspectionFlacPath(full) { - continue - } - if err := requireInspectionFile(full, "audio file"); err != nil { - return nil, err - } - out = append(out, full) - } - if len(out) == 0 { - return nil, fmt.Errorf("no .flac files found in audio directory %q", audioDir) - } - sort.Strings(out) - return out, nil -} - -func resolveInspectionPath(baseDir, inputPath string) (string, error) { - pathValue := strings.TrimSpace(inputPath) - if pathValue == "" { - return "", fmt.Errorf("path is required") - } - if filepath.IsAbs(pathValue) { - return filepath.Clean(pathValue), nil - } - return filepath.Clean(filepath.Join(baseDir, pathValue)), nil -} - func requireInspectionFile(path, label string) error { info, err := os.Stat(path) if err != nil { @@ -284,10 +198,6 @@ func requireInspectionFile(path, label string) error { return nil } -func isInspectionFlacPath(path string) bool { - return strings.EqualFold(filepath.Ext(strings.TrimSpace(path)), ".flac") -} - func previousRequirementSkipped(values []string, name string) bool { for _, value := range values { if value == name {