From 1f5c0c435e9b43aa746030f05503dacb8c4ad16d Mon Sep 17 00:00:00 2001 From: Eric Rakestraw Date: Mon, 30 Nov 2026 04:06:30 +0000 Subject: [PATCH] Prevent collisions between prepared audio filenames --- docs/roadmap/remediation.md | 7 +++- internal/stage/audio_destinations_test.go | 41 +++++++++++++++++++++++ internal/stage/prepare.go | 26 ++++++++++---- 3 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 internal/stage/audio_destinations_test.go diff --git a/docs/roadmap/remediation.md b/docs/roadmap/remediation.md index da1c501..5720c3a 100644 --- a/docs/roadmap/remediation.md +++ b/docs/roadmap/remediation.md @@ -327,7 +327,12 @@ prerequisites, and map-insertion independence against the same observable order. **Priority:** P2 -**Status:** Pending +**Status:** Complete + +Destination assignment reserves all original basenames and checks generated +names globally in deterministic source order, adding suffixes only on conflict. +Verified distinct prepared bytes, original-name preservation, input-order +independence, and existing duplicate-basename behavior with stage tests. **Stage:** 17; retained local-audio acquisition behavior. diff --git a/internal/stage/audio_destinations_test.go b/internal/stage/audio_destinations_test.go new file mode 100644 index 0000000..7610a8c --- /dev/null +++ b/internal/stage/audio_destinations_test.go @@ -0,0 +1,41 @@ +package stage + +import ( + "crypto/sha256" + "encoding/hex" + "path/filepath" + "reflect" + "testing" +) + +func TestLocalAudioDestinationsPreserveDistinctRecordings(t *testing.T) { + env, m := setupPrepareEnv(t) + paths := sessionPathsForEnv(env, m.SessionID) + first := filepath.Join(t.TempDir(), "one.flac") + second := filepath.Join(t.TempDir(), "one.flac") + digest := sha256.Sum256([]byte(filepath.Clean(first))) + generated := "one-" + hex.EncodeToString(digest[:8]) + ".flac" + third := filepath.Join(t.TempDir(), generated) + inputs := []string{first, second, third} + destinations := localAudioDestinations(inputs) + if reversed := localAudioDestinations([]string{third, second, first}); !reflect.DeepEqual(reversed, destinations) { + t.Fatal("destination ownership depends on input order") + } + if destinations[third] != generated { + t.Fatal("noncolliding original filename changed") + } + owners := make(map[string]bool) + for i, source := range inputs { + if owners[destinations[source]] { + t.Fatal("distinct recordings share a destination") + } + owners[destinations[source]] = true + writeFile(t, source, string(rune('a'+i))) + } + if err := materializeLocalAudioInputs(env, paths, inputs, func(string, string, string) {}); err != nil { + t.Fatal(err) + } + for i, source := range inputs { + mustReadFileEquals(t, filepath.Join(paths.AudioDir, destinations[source]), string(rune('a'+i))) + } +} diff --git a/internal/stage/prepare.go b/internal/stage/prepare.go index 13b0613..3b64aac 100644 --- a/internal/stage/prepare.go +++ b/internal/stage/prepare.go @@ -380,23 +380,37 @@ func materializeLocalAudioInputs(env *Env, paths artifacts.SessionPaths, resolve func localAudioDestinations(audioPaths []string) map[string]string { byBase := make(map[string][]string, len(audioPaths)) + occupied := make(map[string]bool, len(audioPaths)) for _, audioPath := range audioPaths { base := filepath.Base(audioPath) byBase[base] = append(byBase[base], audioPath) + occupied[base] = true } destinations := make(map[string]string, len(audioPaths)) - for base, sources := range byBase { - if len(sources) == 1 { - destinations[sources[0]] = base + ordered := append([]string(nil), audioPaths...) + sort.Strings(ordered) + nextSuffix := make(map[string]int) + for _, source := range ordered { + if _, assigned := destinations[source]; assigned { + continue + } + base := filepath.Base(source) + if len(byBase[base]) == 1 { + destinations[source] = base continue } extension := filepath.Ext(base) stem := strings.TrimSuffix(base, extension) - for _, source := range sources { - digest := sha256.Sum256([]byte(filepath.Clean(source))) - destinations[source] = fmt.Sprintf("%s-%s%s", stem, hex.EncodeToString(digest[:8]), extension) + digest := sha256.Sum256([]byte(filepath.Clean(source))) + generatedStem := stem + "-" + hex.EncodeToString(digest[:8]) + candidate := generatedStem + extension + for occupied[candidate] { + nextSuffix[generatedStem+extension]++ + candidate = fmt.Sprintf("%s-%d%s", generatedStem, nextSuffix[generatedStem+extension], extension) } + destinations[source] = candidate + occupied[candidate] = true } return destinations }