carlos / platform Public

media: carry the probe's facts in error results; check them

Branch review round 1, P2-3 (Fable). Invariant: the two sides of the
result schema agree. The console names source_too_long's maximum from
the result's source dimensions, but finish wrote only v, execution and
error, so a 4K source over its 30-minute maximum was told the 1080p
maximum of two hours; the console test fabricated the facts.

Once the probe has read the source, a failure's result now carries what
it read (errorFacts: less any dimension or codec the schema refuses).
ParseResult checks an error result's facts and version as it checks a
success result's, and accepts no facts when the probe did not run. The
console test builds its result with mediahandler.ProbeFailure, which is
finish's own construction and is held equal to what Run writes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 64761e626d5a4123ebe037c3dbbcbb9d087cf650 parent f8aedce
8 files changed, +199 −28
  • internal/console/apimedia_test.go +16 −2
  • internal/media/media_test.go +23 −0
  • internal/media/result.go +14 −3
  • internal/mediahandler/hostile_test.go +52 −0
  • internal/mediahandler/probe.go +29 −9
  • internal/mediahandler/realffmpeg_test.go +1 −1
  • internal/mediahandler/run.go +60 −11
  • internal/mediahandler/run_test.go +4 −2
diff --git a/internal/console/apimedia_test.go b/internal/console/apimedia_test.go
index 2f558d4ab..f0ec2710e 100644
--- a/internal/console/apimedia_test.go
+++ b/internal/console/apimedia_test.go
@@ -12,6 +12,7 @@ import (
"github.com/carlosframework/platform/internal/media"
"github.com/carlosframework/platform/internal/media/mediatest"
+ "github.com/carlosframework/platform/internal/mediahandler"
"github.com/carlosframework/platform/internal/mediajobs"
"github.com/carlosframework/platform/internal/objstore"
"github.com/carlosframework/platform/internal/storecfg"
@@ -620,8 +621,21 @@ func TestGetFailedMessageComesFromTheDeck(t *testing.T) {
t.Fatalf("a failed answer carried the result object: %s", r.raw)
}
// The filled messages (pre-flight F5): a 4K source over the 30-minute
- // maximum, and the short class's 10 minutes.
- fail("source_too_long", media.Result{V: 1, Execution: "x", Error: "source_too_long", Source: &media.SourceFacts{Width: 3840, Height: 2160}})
+ // maximum, and the short class's 10 minutes. The 4K result is the one
+ // the handler writes when its probe reads a 40-minute 2160p source
+ // (branch review round 1, P2-3), not a literal: the two sides of the
+ // result object must agree on where the dimensions are.
+ l := media.DefaultLimits()
+ probe := []byte(`{"streams":[{"index":0,"codec_type":"video","codec_name":"hevc","width":3840,"height":2160,` +
+ `"avg_frame_rate":"30/1","r_frame_rate":"30/1"}],"format":{"duration":"2400.000000"}}`)
+ tooLong, refused := mediahandler.ProbeFailure("0123456789abcdef0123456789abcdef", media.PresetVideo720p, media.JobLimits{
+ SourceBytes: l.VideoBytes, GrantedMS: l.VideoMax.Milliseconds(), GrantedBy: "class",
+ MaxMS: l.VideoMax.Milliseconds(), MaxHiMS: l.VideoMaxHi.Milliseconds(), OutputBytes: media.MaxPutBytes,
+ }, probe)
+ if !refused || tooLong.Error != media.CodeSourceTooLong {
+ t.Fatalf("the handler's probe check answered %+v, refused %v", tooLong, refused)
+ }
+ fail("source_too_long", tooLong)
if r := f.get(sub.job.ID); r.job.Error == nil || r.job.Error.Message != "the file is longer than 30 minutes, the maximum for this preset" {
t.Fatalf("source_too_long: %s", r.raw)
}
diff --git a/internal/media/media_test.go b/internal/media/media_test.go
index 0862e98e2..245092289 100644
--- a/internal/media/media_test.go
+++ b/internal/media/media_test.go
@@ -339,6 +339,29 @@ func TestParseResult(t *testing.T) {
if r, err := ParseResult([]byte(`{"v":1,"execution":"`+x+`","error":"no_video"}`), x, PresetVideo720p); err != nil || r.Error != "no_video" {
t.Fatalf("a handler error result: %+v, %v", r, err)
}
+ // An error result carries the probe's facts when the probe ran (branch
+ // review round 1, P2-3): the console names a maximum from them, so they
+ // are held to the same shapes as a success result's.
+ tooLong := Result{V: 1, Execution: x, Error: CodeSourceTooLong,
+ Source: &SourceFacts{DurationMS: 2400000, Width: 3840, Height: 2160, VideoCodec: "hevc", AudioCodec: "aac"}}
+ if r, err := ParseResult(enc(tooLong), x, PresetVideo720p); err != nil || !reflect.DeepEqual(r, tooLong) {
+ t.Fatalf("an error result with source facts: %+v, %v", r, err)
+ }
+ for name, f := range map[string]func(*Result){
+ "source over MaxSide": func(r *Result) { r.Source.Width = MaxSide + 1 },
+ "source width without height": func(r *Result) { r.Source.Height = 0 },
+ "unknown video codec": func(r *Result) { r.Source.VideoCodec = "mjpeg" },
+ "unknown audio codec": func(r *Result) { r.Source.AudioCodec = "<b>" },
+ "version with markup": func(r *Result) { r.FFmpeg = "<b>7</b>" },
+ } {
+ r := tooLong
+ r.Source = &SourceFacts{}
+ *r.Source = *tooLong.Source
+ f(&r)
+ if _, err := ParseResult(enc(r), x, PresetVideo720p); err == nil {
+ t.Errorf("an error result with %s: accepted", name)
+ }
+ }
bad := map[string][]byte{}
r1 := good()
diff --git a/internal/media/result.go b/internal/media/result.go
index d1fd39283..ddf1db0d0 100644
--- a/internal/media/result.go
+++ b/internal/media/result.go
@@ -44,7 +44,9 @@ type Result struct {
// ParseResult decodes a result object strictly: at most MaxResultBytes,
// no unknown field and nothing after the object, version 1, the expected
-// execution, and an error that is empty or a handler code. A success
+// execution, and an error that is empty or a handler code. An error
+// result carries the probe's source facts when the probe ran, held to the
+// shapes below, and none when it did not. A success
// result must carry the source facts and exactly the preset's outputs,
// each once, each of 1 to MaxPutBytes bytes. Everything the console
// later answers is held to the shapes the handler writes (plan 3 Task 6
@@ -77,6 +79,11 @@ func ParseResult(b []byte, execution, preset string) (Result, error) {
if !HandlerCodes[r.Error] {
return Result{}, fmt.Errorf("result error %q is not a handler code", r.Error)
}
+ // The probe's facts, when it ran, are held to the success
+ // result's shapes: the console names a maximum from them.
+ if err := checkFacts(r); err != nil {
+ return Result{}, err
+ }
return r, nil
}
if r.Source == nil {
@@ -128,13 +135,17 @@ var (
"pcm_f64be": true, "pcm_alaw": true, "pcm_mulaw": true}
)
-// checkFacts holds a success result's version and source facts to the
-// handler's shapes (see ParseResult).
+// checkFacts holds a result's version and source facts to the handler's
+// shapes (see ParseResult). An error result has no source facts when the
+// probe did not run.
func checkFacts(r Result) error {
if !versionString(r.FFmpeg) {
return errors.New("result ffmpeg version is not a version")
}
s := r.Source
+ if s == nil {
+ return nil
+ }
if s.VideoCodec != "" && !resultVideoCodecs[s.VideoCodec] {
return errors.New("result source video codec is not one the handler accepts")
}
diff --git a/internal/mediahandler/hostile_test.go b/internal/mediahandler/hostile_test.go
index 0c6285f04..5cfc843c3 100644
--- a/internal/mediahandler/hostile_test.go
+++ b/internal/mediahandler/hostile_test.go
@@ -11,6 +11,7 @@ import (
"os/exec"
"os/signal"
"path/filepath"
+ "reflect"
"slices"
"strconv"
"strings"
@@ -231,6 +232,57 @@ func TestCorpusEndsInNamedErrors(t *testing.T) {
}
}
+// TestErrorResultCarriesProbeFacts (branch review round 1, P2-3): a job
+// refused after its probe writes the probe's facts into the error result,
+// the console names source_too_long's maximum from its dimensions; a fact
+// the result's schema would refuse is left out; and a job refused before
+// the probe ran writes none.
+func TestErrorResultCarriesProbeFacts(t *testing.T) {
+ needChildren(t)
+ src := readFixture(t, "video.mp4")
+ var limits media.JobLimits
+ resultOf := func(t *testing.T, preset, scenario string, data []byte, code string) media.Result {
+ t.Helper()
+ h, p, cfg, _ := corpusSetup(t, preset, scenario, data)
+ limits = p.Limits
+ if got := runFor(t, cfg, p); got != media.ExitFailed {
+ t.Fatalf("Run = %d, want 1; logs %q", got, h.Logs())
+ }
+ wantOnlyResult(t, h, preset, code)
+ b, _ := h.s.Object(resultPath)
+ r, err := media.ParseResult(b, testExec, preset)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return r
+ }
+ t.Run("too long", func(t *testing.T) {
+ r := resultOf(t, media.PresetVideo720p, "probe-lying-10h", src, media.CodeSourceTooLong)
+ want := media.SourceFacts{DurationMS: 36000000, Width: 1280, Height: 720, VideoCodec: "h264"}
+ if r.Source == nil || *r.Source != want {
+ t.Fatalf("source facts %+v, want %+v", r.Source, want)
+ }
+ // ProbeFailure, which the console's tests build their failure
+ // results with, answers exactly what Run wrote.
+ if pf, ok := ProbeFailure(testExec, media.PresetVideo720p, limits, fixture(t, "lying-10h")); !ok || !reflect.DeepEqual(pf, r) {
+ t.Fatalf("ProbeFailure = %+v (%v), Run wrote %+v", pf, ok, r)
+ }
+ })
+ t.Run("dimensions the schema refuses", func(t *testing.T) {
+ r := resultOf(t, media.PresetVideo720p, "probe-bomb-30000", src, media.CodeSourceDimensions)
+ want := media.SourceFacts{DurationMS: 1000, VideoCodec: "h264"}
+ if r.Source == nil || *r.Source != want {
+ t.Fatalf("source facts %+v, want %+v", r.Source, want)
+ }
+ })
+ t.Run("refused before the probe", func(t *testing.T) {
+ r := resultOf(t, media.PresetVideo720p, "fail", readCorpus(t, "playlist-metadata.m3u8"), media.CodeUnsupportedContainer)
+ if r.Source != nil {
+ t.Fatalf("source facts %+v from a job ffprobe never read", r.Source)
+ }
+ })
+}
+
// TestRealCorpusEndsInNamedErrors is TestCorpusEndsInNamedErrors on
// the real ffprobe and ffmpeg (skipped without them, realConfig).
func TestRealCorpusEndsInNamedErrors(t *testing.T) {
diff --git a/internal/mediahandler/probe.go b/internal/mediahandler/probe.go
index 80bad0dd1..f14c882f2 100644
--- a/internal/mediahandler/probe.go
+++ b/internal/mediahandler/probe.go
@@ -595,27 +595,47 @@ func check(f probeFacts, preset string, l media.JobLimits) error {
// inspectSource sniffs, probes and checks the downloaded source,
// answering the demuxer and facts the encodes use. Sniff refusing it
-// means ffprobe never runs.
-func inspectSource(ctx context.Context, cfg Config, p media.Payload) (string, probeFacts, error) {
+// means ffprobe never runs. probed is true once the probe has read the
+// source, the check's refusal included: f then holds what it read.
+func inspectSource(ctx context.Context, cfg Config, p media.Payload) (demuxer string, f probeFacts, probed bool, err error) {
head, err := readHead(sourcePath(cfg))
if err != nil {
// engine.codec.source_unopenable (pending Luna pass). The error
// names the scratch path only.
cfg.Logf("codec: the downloaded source cannot be read: %v", err)
- return "", probeFacts{}, jobError{media.CodeEngineFailed}
+ return "", probeFacts{}, false, jobError{media.CodeEngineFailed}
}
- demuxer, err := sniff(head, p.Preset)
+ demuxer, err = sniff(head, p.Preset)
if err != nil {
- return "", probeFacts{}, err
+ return "", probeFacts{}, false, err
}
- f, err := probe(ctx, cfg, demuxer, p.Preset)
+ f, err = probe(ctx, cfg, demuxer, p.Preset)
if err != nil {
- return "", probeFacts{}, err
+ return "", probeFacts{}, false, err
}
if err := check(f, p.Preset, p.Limits); err != nil {
- return "", probeFacts{}, err
+ return "", f, true, err
}
- return demuxer, f, nil
+ return demuxer, f, true, nil
+}
+
+// ProbeFailure is the failure result the handler writes for a source
+// ffprobe described as probeJSON (its first pass, -of json, with a
+// duration) when the job's checks refuse it under preset and l: the code
+// and errorFacts, or unreadable with no facts when the answer does not
+// parse. ok is false when the checks accept the source. It is finish's
+// own construction (failureResult), so the console's tests can hold
+// their failure results to what the handler writes.
+func ProbeFailure(execution, preset string, l media.JobLimits, probeJSON []byte) (r media.Result, ok bool) {
+ f, err := parseProbe(probeJSON, preset)
+ if err != nil {
+ return failureResult(execution, media.CodeUnreadable, nil), true
+ }
+ var je jobError
+ if err := check(f, preset, l); errors.As(err, &je) {
+ return failureResult(execution, je.code, errorFacts(f, preset)), true
+ }
+ return media.Result{}, false
}
// capBuffer keeps the first max bytes written to it and notes whether
diff --git a/internal/mediahandler/realffmpeg_test.go b/internal/mediahandler/realffmpeg_test.go
index d444480e3..0cc08b663 100644
--- a/internal/mediahandler/realffmpeg_test.go
+++ b/internal/mediahandler/realffmpeg_test.go
@@ -235,7 +235,7 @@ func realInspect(t *testing.T, cfg Config, name, preset string) (probeFacts, err
t.Helper()
makeSource(t, cfg, name)
p := media.Payload{Preset: preset, Limits: limitsFor(preset)}
- _, f, err := inspectSource(context.Background(), cfg, p)
+ _, f, _, err := inspectSource(context.Background(), cfg, p)
return f, err
}
diff --git a/internal/mediahandler/run.go b/internal/mediahandler/run.go
index 59c30b35d..ebde392b5 100644
--- a/internal/mediahandler/run.go
+++ b/internal/mediahandler/run.go
@@ -78,16 +78,24 @@ func runJob(ctx context.Context, cfg Config, p media.Payload) error {
}
gctx, cancel := grantContext(ctx, cfg, p)
defer cancel()
- f, durationMS, version, err := convert(gctx, cfg, p)
+ f, probed, durationMS, version, err := convert(gctx, cfg, p)
+ // Once the probe has read the source, a failure's result carries what
+ // it read (branch review round 1, P2-3).
+ withFacts := func(err error) error {
+ if !probed {
+ return err
+ }
+ return probedError{err: err, facts: errorFacts(f, p.Preset)}
+ }
if err != nil {
- return grantEnded(ctx, gctx, err)
+ return withFacts(grantEnded(ctx, gctx, err))
}
if err := childrenDead(cfg); err != nil {
- return err
+ return withFacts(err)
}
outputs, err := deliver(ctx, cfg, p, f)
if err != nil {
- return err
+ return withFacts(err)
}
r := media.Result{
V: media.ResultVersion,
@@ -99,7 +107,7 @@ func runJob(ctx context.Context, cfg Config, p media.Payload) error {
if err := writeResult(ctx, cfg, p, r); err != nil {
var denied storeDenied
if errors.As(err, &denied) {
- return err // nothing was stored: finish writes the failure result
+ return withFacts(err) // nothing was stored: finish writes the failure result
}
return resultUnwritten{err}
}
@@ -128,10 +136,10 @@ func grantEnded(ctx, gctx context.Context, err error) error {
// version, all under ctx (the grant's). durationMS is what was
// converted: readFMP4's, never more than the granted duration
// (packageOutput, spec A2).
-func convert(ctx context.Context, cfg Config, p media.Payload) (f probeFacts, durationMS int64, version string, err error) {
- demuxer, f, err := inspectSource(ctx, cfg, p)
+func convert(ctx context.Context, cfg Config, p media.Payload) (f probeFacts, probed bool, durationMS int64, version string, err error) {
+ demuxer, f, probed, err := inspectSource(ctx, cfg, p)
if err != nil {
- return f, 0, "", err
+ return f, probed, 0, "", err
}
switch p.Preset {
case media.PresetVideo720p:
@@ -142,10 +150,10 @@ func convert(ctx context.Context, cfg Config, p media.Payload) (f probeFacts, du
err = jobError{media.CodeEngineFailed} // Validate knows only these two
}
if err != nil {
- return f, 0, "", err
+ return f, true, 0, "", err
}
version, err = ffmpegVersion(ctx, cfg)
- return f, durationMS, version, err
+ return f, true, durationMS, version, err
}
// convertVideo is video-720p's: the video and the poster
@@ -187,6 +195,42 @@ func convertAudio(ctx context.Context, cfg Config, p media.Payload, demuxer stri
return durationMS, nil
}
+// errorFacts are a failure result's source facts once the probe has
+// read the source: sourceFacts with the probe's own duration, less any
+// fact the result's schema refuses (a dimension outside 1 to
+// media.MaxSide, a codec the check refuses), which the job's code has
+// already named.
+func errorFacts(f probeFacts, preset string) *media.SourceFacts {
+ s := sourceFacts(f, preset, max(f.DurationMS, 0))
+ if s.Width < 1 || s.Width > media.MaxSide || s.Height < 1 || s.Height > media.MaxSide {
+ s.Width, s.Height = 0, 0
+ }
+ if !videoCodecs[s.VideoCodec] {
+ s.VideoCodec = ""
+ }
+ if !audioCodecs[s.AudioCodec] {
+ s.AudioCodec = ""
+ }
+ return s
+}
+
+// probedError is a job's error after the probe read its source: finish
+// writes facts into the failure result.
+type probedError struct {
+ err error
+ facts *media.SourceFacts
+}
+
+func (e probedError) Error() string { return e.err.Error() }
+
+func (e probedError) Unwrap() error { return e.err }
+
+// failureResult is the result object of a job that failed with code:
+// facts when the probe read the source, else none.
+func failureResult(execution, code string, facts *media.SourceFacts) media.Result {
+ return media.Result{V: media.ResultVersion, Execution: execution, Error: code, Source: facts}
+}
+
// sourceFacts are the result's source facts: the converted duration
// (spec A2: what the job is charged for, at most the grant), the
// source's stored dimensions and video codec (video-720p), and its
@@ -269,9 +313,14 @@ func finish(ctx context.Context, cfg Config, p media.Payload, err error) int {
cfg.Logf("codec: an unexpected %T ends the job", err)
je = jobError{media.CodeEngineFailed}
}
+ var probed probedError
+ var facts *media.SourceFacts
+ if errors.As(err, &probed) {
+ facts = probed.facts
+ }
// engine.codec.failed (pending Luna pass).
cfg.Logf("codec: failed: %s", je.code)
- werr := writeResult(ctx, cfg, p, media.Result{V: media.ResultVersion, Execution: p.Execution, Error: je.code})
+ werr := writeResult(ctx, cfg, p, failureResult(p.Execution, je.code, facts))
switch {
case werr == nil:
case ctx.Err() != nil:
diff --git a/internal/mediahandler/run_test.go b/internal/mediahandler/run_test.go
index 08b11fce7..2cdfa900e 100644
--- a/internal/mediahandler/run_test.go
+++ b/internal/mediahandler/run_test.go
@@ -287,7 +287,8 @@ func TestRunSuccessWritesResultLast(t *testing.T) {
// TestRunNamedErrorWritesResultAndExits1: an M4A with cover art under
// video-720p is no_video (preflight F17). Nothing is encoded, no output
-// is PUT, and the result names the code; Run exits 1.
+// is PUT, and the result names the code with what the probe read (branch
+// review round 1, P2-3); Run exits 1.
func TestRunNamedErrorWritesResultAndExits1(t *testing.T) {
h, p, cfg, record := runJobSetup(t, media.PresetVideo720p, "probe-m4a-cover-art")
if code := runFor(t, cfg, p); code != media.ExitFailed {
@@ -299,7 +300,8 @@ func TestRunNamedErrorWritesResultAndExits1(t *testing.T) {
}
b, _ := h.s.Object(resultPath)
r, err := media.ParseResult(b, testExec, media.PresetVideo720p)
- if err != nil || r.Error != media.CodeNoVideo || r.Source != nil || len(r.Outputs) != 0 {
+ probed := media.SourceFacts{DurationMS: 3000, AudioCodec: "aac"}
+ if err != nil || r.Error != media.CodeNoVideo || r.Source == nil || *r.Source != probed || len(r.Outputs) != 0 {
t.Fatalf("result %s, %v", b, err)
}
if got := passKinds(fakeRuns(t, record)); !slices.Equal(got, []string{"probe"}) {