Record the downloaded audio object generation
This commit is contained in:
@@ -37,7 +37,7 @@ Materialize canonical current-session inputs before processing stages.
|
||||
- rejects duplicate explicit local audio sources after resolution.
|
||||
- gives distinct local source paths with the same basename deterministic unique
|
||||
prepared filenames so neither source is overwritten.
|
||||
- downloads S3 audio into a temporary sibling before verifying and installing the prepared copy.
|
||||
- downloads S3 audio into a temporary sibling before verifying and installing the prepared copy; its recorded identity comes from the same response as the bytes.
|
||||
- writes effective autocorrection rules as a Seriatim envelope, with checksum,
|
||||
size, and source provenance, including an empty list when no rules apply.
|
||||
- renders standalone effective campaign, session, and canonical party documents,
|
||||
|
||||
@@ -270,6 +270,9 @@ reject it later. This bounds that transfer, not total disk consumption.
|
||||
|
||||
## Stage 7: Record the S3 audio generation actually downloaded
|
||||
|
||||
**Complete:** Prepared audio records use response-bound identity and size; tests
|
||||
cover same- and different-sized replacements, reuse, rollback, and failed staging.
|
||||
|
||||
Read `audit/04-concurrency.md`, lines 223–274 (CN-04),
|
||||
`internal/audio/s3_audio.go`, and preparation's S3 materialization/reuse paths.
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ type S3MaterializeResult struct {
|
||||
Checksum string
|
||||
Reused bool
|
||||
Downloaded bool
|
||||
Object storage.ObjectInfo
|
||||
}
|
||||
|
||||
// MaterializeS3Audio verifies the recorded prepared copy or installs a fresh download.
|
||||
@@ -61,18 +62,19 @@ func MaterializeS3Audio(ctx context.Context, req S3MaterializeRequest) (S3Materi
|
||||
if req.Reporter != nil {
|
||||
req.Reporter.Progress("reusing prepared audio", "audio", filepath.Base(req.DestPath))
|
||||
}
|
||||
return S3MaterializeResult{Checksum: checksum, Reused: true}, nil
|
||||
return S3MaterializeResult{Checksum: checksum, Reused: true, Object: req.Object}, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if err := downloadObjectAtomic(ctx, req.Store, key, req.DestPath, req.Object.Size, req.Reporter); err != nil {
|
||||
observed, err := downloadObjectAtomic(ctx, req.Store, key, req.DestPath, req.Reporter)
|
||||
if err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("download s3 audio object %q: %w", key, err)
|
||||
}
|
||||
checksum, err := artifacts.SHA256File(req.DestPath)
|
||||
if err != nil {
|
||||
return S3MaterializeResult{}, fmt.Errorf("checksum downloaded audio: %w", err)
|
||||
}
|
||||
return S3MaterializeResult{Checksum: checksum, Downloaded: true}, nil
|
||||
return S3MaterializeResult{Checksum: checksum, Downloaded: true, Object: observed}, nil
|
||||
}
|
||||
|
||||
func validateLocalAudio(path string, expectedSize int64) error {
|
||||
@@ -92,30 +94,33 @@ func validateLocalAudio(path string, expectedSize int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, destPath string, expectedSize int64, reporter logging.Reporter) error {
|
||||
func downloadObjectAtomic(ctx context.Context, store storage.ObjectStore, key, destPath string, reporter logging.Reporter) (storage.ObjectInfo, error) {
|
||||
var observed storage.ObjectInfo
|
||||
temporaryPath, err := writeAudioTemp(destPath, func(destination io.Writer) error {
|
||||
if reporter != nil {
|
||||
reporter.Progress("downloading audio", "audio", filepath.Base(destPath))
|
||||
}
|
||||
return storage.DownloadTo(ctx, store, key, destination)
|
||||
var streamErr error
|
||||
observed, streamErr = storage.StreamObject(ctx, store, key, destination, nil)
|
||||
return streamErr
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
return observed, err
|
||||
}
|
||||
defer func() { _ = os.Remove(temporaryPath) }()
|
||||
if err := validateLocalAudio(temporaryPath, expectedSize); err != nil {
|
||||
return fmt.Errorf("validate downloaded audio: %w", err)
|
||||
if err := validateLocalAudio(temporaryPath, observed.Size); err != nil {
|
||||
return observed, fmt.Errorf("validate downloaded audio: %w", err)
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
return observed, err
|
||||
}
|
||||
if err := fileops.InstallSiblingTempFile(temporaryPath, destPath, fileops.WorkspaceFileMode); err != nil {
|
||||
return err
|
||||
return observed, err
|
||||
}
|
||||
if reporter != nil {
|
||||
reporter.Progress("audio downloaded", "audio", filepath.Base(destPath))
|
||||
}
|
||||
return nil
|
||||
return observed, nil
|
||||
}
|
||||
|
||||
func writeAudioTemp(destination string, write func(io.Writer) error) (string, error) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package audio
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -76,7 +77,7 @@ func TestMaterializeS3AudioFailedDownloadPreservesDestination(t *testing.T) {
|
||||
if err := os.WriteFile(dest, []byte("original"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := S3MaterializeRequest{Store: fake, Object: storage.ObjectInfo{Key: "audio/a.flac", Size: 100}, DestPath: dest}
|
||||
req := S3MaterializeRequest{Store: shortResponseStore{fake}, Object: storage.ObjectInfo{Key: "audio/a.flac", Size: 100}, DestPath: dest}
|
||||
if _, err := MaterializeS3Audio(context.Background(), req); err == nil {
|
||||
t.Fatal("expected size mismatch")
|
||||
}
|
||||
@@ -88,12 +89,21 @@ func TestMaterializeS3AudioFailedDownloadPreservesDestination(t *testing.T) {
|
||||
if err != nil || len(entries) != 1 {
|
||||
t.Fatalf("entries = %v, %v", entries, err)
|
||||
}
|
||||
req.Store = fake
|
||||
req.Object.Size = 5
|
||||
if _, err := MaterializeS3Audio(context.Background(), req); err != nil {
|
||||
t.Fatalf("retry: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
type shortResponseStore struct{ storage.ObjectStore }
|
||||
|
||||
func (s shortResponseStore) Read(ctx context.Context, key string) (storage.ObjectInfo, io.ReadCloser, error) {
|
||||
info, body, err := s.ObjectStore.Read(ctx, key)
|
||||
info.Size, info.SizeKnown = 100, true
|
||||
return info, body, err
|
||||
}
|
||||
|
||||
func TestMaterializeS3AudioCanceledRequestCanRetry(t *testing.T) {
|
||||
fake := &storage.FakeBackend{}
|
||||
fake.SeedObject(storage.FakeObject{Key: "audio/a.flac", Data: []byte("audio")})
|
||||
|
||||
@@ -476,8 +476,8 @@ func materializeS3AudioInputs(ctx context.Context, env *Env, m *manifest.Manifes
|
||||
}
|
||||
*inputs = append(*inputs, manifest.InputRecord{
|
||||
Kind: "audio", Path: workPath, Checksum: result.Checksum, Source: "s3",
|
||||
S3Bucket: strings.TrimSpace(env.Config.Pipeline.Storage.S3.Bucket), S3Key: obj.Key,
|
||||
S3Size: obj.Size, S3ETag: obj.ETag,
|
||||
S3Bucket: strings.TrimSpace(env.Config.Pipeline.Storage.S3.Bucket), S3Key: result.Object.Key,
|
||||
S3Size: result.Object.Size, S3ETag: result.Object.ETag,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -369,8 +369,8 @@ func TestPrepareStageS3AudioReusesPreparedCopy(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Metadata["audio_reused"] != 1 || len(fake.Downloads) != 1 {
|
||||
t.Fatalf("result=%#v downloads=%d", result.Metadata, len(fake.Downloads))
|
||||
if result.Metadata["audio_reused"] != 1 || len(fake.Reads) != 1 {
|
||||
t.Fatalf("result=%#v reads=%d", result.Metadata, len(fake.Reads))
|
||||
}
|
||||
dest := filepath.Join(sessionPathsForEnv(env, m.SessionID).AudioDir, "alice.flac")
|
||||
mustReadFileEquals(t, dest, "alice")
|
||||
@@ -378,8 +378,8 @@ func TestPrepareStageS3AudioReusesPreparedCopy(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err = (prepareStage{}).Run(context.Background(), env, m)
|
||||
if err != nil || result.Metadata["audio_s3_downloads"] != 1 || len(fake.Downloads) != 2 {
|
||||
t.Fatalf("result=%#v downloads=%d error=%v", result, len(fake.Downloads), err)
|
||||
if err != nil || result.Metadata["audio_s3_downloads"] != 1 || len(fake.Reads) != 2 {
|
||||
t.Fatalf("result=%#v reads=%d error=%v", result, len(fake.Reads), err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -849,3 +849,64 @@ func TestPrepareGlossaryFailurePreservesManifest(t *testing.T) {
|
||||
t.Fatalf("manifest changed: %+v", m.Inputs)
|
||||
}
|
||||
}
|
||||
|
||||
type replacingAudioStore struct {
|
||||
*storage.FakeBackend
|
||||
key string
|
||||
replacement []byte
|
||||
replace bool
|
||||
}
|
||||
|
||||
func (s *replacingAudioStore) List(ctx context.Context, prefix string) ([]storage.ObjectInfo, error) {
|
||||
result, err := s.FakeBackend.List(ctx, prefix)
|
||||
if err == nil && s.replace {
|
||||
s.replace = false
|
||||
s.SeedObject(storage.FakeObject{Key: s.key, Data: s.replacement, ETag: "generation-b"})
|
||||
}
|
||||
return result, err
|
||||
}
|
||||
|
||||
func TestPrepareRecordsDownloadedAudioGeneration(t *testing.T) {
|
||||
for _, replacement := range []string{"other", "a longer recording"} {
|
||||
t.Run(replacement, func(t *testing.T) {
|
||||
env, m := setupPrepareEnv(t)
|
||||
env.Config.Session.Campaign = "forsaken"
|
||||
env.Config.Session.Inputs.AudioDir = ""
|
||||
env.Config.Session.Inputs.AudioFiles = nil
|
||||
env.Config.Session.Inputs.AudioS3 = &config.SessionAudioS3Input{Prefix: "audio/"}
|
||||
env.Config.Pipeline.Storage.S3 = &config.StorageS3Config{Bucket: "archive", RootPrefix: "dnd"}
|
||||
m.RunID = "20260515T031522Z-a1b2c3d4"
|
||||
key := "dnd/campaigns/forsaken/sessions/2026-05-03/audio/alice.flac"
|
||||
fake := &storage.FakeBackend{}
|
||||
fake.SeedObject(storage.FakeObject{Key: key, Data: []byte("alice"), ETag: "generation-a"})
|
||||
store := &replacingAudioStore{FakeBackend: fake, key: key, replacement: []byte(replacement), replace: true}
|
||||
env.ObjectStore = store
|
||||
if _, err := (prepareStage{}).Run(context.Background(), env, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
input := findManifestInput(t, m.Inputs, "audio")
|
||||
if input.S3Key != key || input.S3ETag != "generation-b" || input.S3Size != int64(len(replacement)) {
|
||||
t.Fatalf("identity = %+v", input)
|
||||
}
|
||||
mustReadFileEquals(t, input.Path, replacement)
|
||||
checksum, err := artifacts.SHA256File(input.Path)
|
||||
if err != nil || input.Checksum != checksum {
|
||||
t.Fatalf("checksum=%q want=%q err=%v", input.Checksum, checksum, err)
|
||||
}
|
||||
result, err := (prepareStage{}).Run(context.Background(), env, m)
|
||||
if err != nil || result.Metadata["audio_reused"] != 1 {
|
||||
t.Fatalf("reuse=%v error=%v", result, err)
|
||||
}
|
||||
fake.SeedObject(storage.FakeObject{Key: key, Data: []byte("alice"), ETag: "generation-a"})
|
||||
result, err = (prepareStage{}).Run(context.Background(), env, m)
|
||||
if err != nil || result.Metadata["audio_s3_downloads"] != 1 {
|
||||
t.Fatalf("rollback=%v error=%v", result, err)
|
||||
}
|
||||
input = findManifestInput(t, m.Inputs, "audio")
|
||||
if input.S3ETag != "generation-a" || input.S3Size != 5 {
|
||||
t.Fatalf("rollback identity = %+v", input)
|
||||
}
|
||||
mustReadFileEquals(t, input.Path, "alice")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user