carlos / platform Public

media: convert SDR video up to 1080p for 3 hours, not 2

LiveKit Cloud records up to 3 hours per file, so the default video_max
rises from 2 hours to 3 (spec amendment A9). HDR up to 1080p stays at 75
minutes and above 1080p at 30.

At the spike's 5.85 times real time for long-class 1080p SDR, 3 hours fit
plan 1's 40-minute rule with a 40 GiB source (187 minutes, rounded down to
180). At the slowest SDR run, 5.42, they end 41.3 minutes into the
50-minute deadline. TestDefaultMaximumsFitTheLongClass holds that
arithmetic, so a default past what the engine converts fails the gate.

A long video still reserves at weight 2, so 3 hours hold 360 allowance
minutes until the job settles; a done 3-hour SDR job is charged 180. The
fit, the reservation and the door's 402 need no code change. The
simulation gains grant invariants under I8: a row's grant and deadline
stay within its class's figures, a done row's source is no longer than
its grant, and each Run's payload carries the row's grant and the
deployment's maximums. New witnesses cover each, and a 3-hour job end to
end.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 180cb5feb78f43ab7a5b20b9870075ba8618f150 parent 65951fb
6 files changed, +355 −11
  • internal/console/apimedia_test.go +17 −1
  • internal/media/limits.go +8 −1
  • internal/media/media_test.go +66 −7
  • internal/mediajobs/jobs_test.go +117 −1
  • internal/sim/mediajobs/invariants_test.go +52 −0
  • internal/sim/mediajobs/witness_test.go +95 −1
diff --git a/internal/console/apimedia_test.go b/internal/console/apimedia_test.go
index be5ea48a4..0a7020ba0 100644
--- a/internal/console/apimedia_test.go
+++ b/internal/console/apimedia_test.go
@@ -640,6 +640,22 @@ func TestGetFailedMessageComesFromTheDeck(t *testing.T) {
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)
}
+ // SDR up to 1080p 1 s over its 3-hour maximum (spec §11 A9): the
+ // handler's failure result for a 1080p H.264 source of 3 h 0 min 1 s,
+ // named with the SDR maximum.
+ sdrProbe := []byte(`{"streams":[{"index":0,"codec_type":"video","codec_name":"h264","width":1920,"height":1080,` +
+ `"avg_frame_rate":"30/1","r_frame_rate":"30/1","color_transfer":"bt709"}],"format":{"duration":"10801.000000"}}`)
+ sdrLong, refused := mediahandler.ProbeFailure("0123456789abcdef0123456789abcdef", media.PresetVideo720p, media.JobLimits{
+ SourceBytes: l.VideoBytes, GrantedMS: l.VideoMax.Milliseconds(), GrantedBy: "class",
+ MaxMS: l.VideoMax.Milliseconds(), MaxHDRMS: l.MaxHDR().Milliseconds(), MaxHiMS: l.VideoMaxHi.Milliseconds(), OutputBytes: media.MaxPutBytes,
+ }, sdrProbe)
+ if !refused || sdrLong.Error != media.CodeSourceTooLong {
+ t.Fatalf("the handler's probe check answered %+v, refused %v", sdrLong, refused)
+ }
+ fail("source_too_long", sdrLong)
+ if r := f.get(sub.job.ID); r.job.Error == nil || r.job.Error.Message != "the file is longer than 3 hours, the maximum for this preset" {
+ t.Fatalf("source_too_long for SDR: %s", r.raw)
+ }
fail("needs_long_class", media.Result{V: 1, Execution: "x", Error: "needs_long_class"})
if r := f.get(sub.job.ID); r.job.Error == nil ||
r.job.Error.Message != "the file is longer than 10 minutes, the most the short class converts. submit it again in the long class" {
@@ -764,7 +780,7 @@ func TestMediaCopyCoversEveryCode(t *testing.T) {
t.Errorf("no phrase for store check %s", c)
}
}
- for d, want := range map[time.Duration]string{2 * time.Hour: "2 hours", time.Hour: "1 hour", 30 * time.Minute: "30 minutes",
+ for d, want := range map[time.Duration]string{2 * time.Hour: "2 hours", 3 * time.Hour: "3 hours", time.Hour: "1 hour", 30 * time.Minute: "30 minutes",
10 * time.Minute: "10 minutes", 90 * time.Minute: "90 minutes", 45 * time.Second: "45 seconds"} {
if got := mediaDuration(d); got != want {
t.Errorf("mediaDuration(%s) = %q, want %q", d, got, want)
diff --git a/internal/media/limits.go b/internal/media/limits.go
index 57bcaa377..97355b5f5 100644
--- a/internal/media/limits.go
+++ b/internal/media/limits.go
@@ -11,6 +11,13 @@ import (
// per-file maximums (spec §7.1), the per-account allowance used when there
// is no plan catalog (§7.3), and the fair-use figures (§7.2).
//
+// VideoMax is 3 hours (spec §11 A9, Paul 2026-11-25), so a LiveKit Cloud
+// recording, up to 3 hours a file, converts in one job: at the spike's
+// 5.85 times real time for 1080p SDR on the long class, 3 hours fit the
+// plan's 40-minute rule with a 40 GiB source, and at its slowest SDR run
+// (5.42) they still end inside the 50-minute deadline.
+// TestDefaultMaximumsFitTheLongClass holds the arithmetic.
+//
// VideoMaxHDR is the maximum for an HDR video source up to 1080p, held to
// VideoMax (MaxHDR): the spike converted 1080p HDR at 2.71 times real
// time on the long class, against 5.85 for SDR, so two hours of it do not
@@ -29,7 +36,7 @@ type Limits struct {
// DefaultLimits is what ParseLimits("") gives.
func DefaultLimits() Limits {
return Limits{
- VideoMax: 2 * time.Hour,
+ VideoMax: 3 * time.Hour,
VideoMaxHDR: 75 * time.Minute,
VideoMaxHi: 30 * time.Minute,
VideoBytes: 40 << 30,
diff --git a/internal/media/media_test.go b/internal/media/media_test.go
index 19dc08c8b..fca39ef26 100644
--- a/internal/media/media_test.go
+++ b/internal/media/media_test.go
@@ -120,7 +120,7 @@ func TestClassTable(t *testing.T) {
// Above 1080p converts on the long video class only (spec §11 A7):
// at 4 vCPU 4K HDR ran at 0.27 times real time.
{ClassKey{PresetVideo720p, ClassShort}, 4096, 8192, 15 * time.Minute, 10 * time.Minute, false},
- {ClassKey{PresetVideo720p, ClassLong}, 16384, 32768, 50 * time.Minute, 2 * time.Hour, true},
+ {ClassKey{PresetVideo720p, ClassLong}, 16384, 32768, 50 * time.Minute, 3 * time.Hour, true},
{ClassKey{PresetAudioAAC, ClassShort}, 1024, 2048, 10 * time.Minute, 2 * time.Hour, false},
{ClassKey{PresetAudioAAC, ClassLong}, 2048, 4096, 50 * time.Minute, 8 * time.Hour, false},
} {
@@ -182,7 +182,7 @@ func TestParseLimits(t *testing.T) {
t.Fatal(err)
}
want := Limits{
- VideoMax: 2 * time.Hour, VideoMaxHDR: 75 * time.Minute, VideoMaxHi: 30 * time.Minute, VideoBytes: 40 << 30,
+ VideoMax: 3 * time.Hour, VideoMaxHDR: 75 * time.Minute, VideoMaxHi: 30 * time.Minute, VideoBytes: 40 << 30,
AudioMax: 8 * time.Hour, AudioBytes: 10 << 30, AccountMinutes: 0,
Short: 6, Long: 2, AccountShort: 2, AccountLong: 1, AccountActive: 20,
PerMinute: 30, Burst: 10, Queue: 200,
@@ -220,9 +220,11 @@ func TestParseLimits(t *testing.T) {
}
}
-// TestMaxDuration: 2 hours for SDR up to 1080p, 75 minutes for HDR up to
-// 1080p (the spike's 1080p HDR ran at 2.71 times real time on the long
-// class, spec §11 A7), 30 minutes above 1080p whatever the transfer.
+// TestMaxDuration: 3 hours for SDR up to 1080p (spec §11 A9, Paul
+// 2026-11-25: a LiveKit Cloud recording is up to 3 hours a file), 75
+// minutes for HDR up to 1080p (the spike's 1080p HDR ran at 2.71 times
+// real time on the long class, spec §11 A7), 30 minutes above 1080p
+// whatever the transfer.
func TestMaxDuration(t *testing.T) {
l := DefaultLimits()
for _, c := range []struct {
@@ -231,8 +233,9 @@ func TestMaxDuration(t *testing.T) {
hdr bool
want time.Duration
}{
- {PresetVideo720p, 1920, 1080, false, 2 * time.Hour},
- {PresetVideo720p, 1080, 1920, false, 2 * time.Hour},
+ {PresetVideo720p, 1920, 1080, false, 3 * time.Hour},
+ {PresetVideo720p, 1080, 1920, false, 3 * time.Hour},
+ {PresetVideo720p, 1280, 720, false, 3 * time.Hour},
{PresetVideo720p, 1920, 1080, true, 75 * time.Minute},
{PresetVideo720p, 1080, 1920, true, 75 * time.Minute},
{PresetVideo720p, 2560, 1440, false, 30 * time.Minute},
@@ -260,6 +263,62 @@ func TestMaxDuration(t *testing.T) {
}
}
+// TestDefaultMaximumsFitTheLongClass (spec §11 A9): each default video
+// maximum against the spike's measurements on the long class at medium
+// (docs/superpowers/reviews/2026-11-16-media-conversion-spike.md, Task 5),
+// so raising one past what the engine converts in its window fails here.
+//
+// First plan 1 Task 5 Step 4's rule, D = (40 min - t_start - S/B) x v
+// rounded down to 15 minutes, with t_start the p95 start (31.7 s), B the
+// p5 download (95.3 MB/s), S the video size maximum and v the cell's
+// speed. Then the 50-minute deadline and the one-hour grant (paul R-K) at
+// the slowest long-class 1080p SDR run, the 30 GiB source at 5.42 times
+// real time, with a worst-case upload: the output at the 2,500 kbit/s
+// video ceiling plus 128 kbit/s of audio, sent at B. At 5.42 the 40-minute
+// rule gives 165 minutes, so 3 hours rests on the 1-hour run's 5.85 for
+// the rule and on the deadline's margin for the slower host (A9).
+func TestDefaultMaximumsFitTheLongClass(t *testing.T) {
+ l := DefaultLimits()
+ const (
+ budget = 40 * time.Minute
+ start = 31700 * time.Millisecond
+ bps = 95.3e6 // bytes a second
+ )
+ secs := func(bytes float64) time.Duration { return time.Duration(bytes / bps * float64(time.Second)) }
+ download := secs(float64(l.VideoBytes))
+ rule := func(v float64) time.Duration {
+ d := time.Duration(float64(budget-start-download) * v)
+ return d - d%(15*time.Minute)
+ }
+ for _, c := range []struct {
+ name string
+ max time.Duration
+ speed float64
+ }{
+ {"SDR up to 1080p", l.VideoMax, 5.85},
+ {"HDR up to 1080p", l.MaxHDR(), 2.71},
+ {"above 1080p", l.VideoMaxHi, 1.20},
+ } {
+ if got := rule(c.speed); got < c.max {
+ t.Errorf("%s: the plan's rule at %.2f times real time gives %s, under the %s maximum", c.name, c.speed, got, c.max)
+ }
+ }
+ long, _ := Class(ClassKey{PresetVideo720p, ClassLong})
+ const slowSDR = 5.42
+ encode := time.Duration(float64(l.VideoMax) / slowSDR)
+ upload := secs((2500e3 + 128e3) / 8 * l.VideoMax.Seconds())
+ if out := (2500e3 + 128e3) / 8 * l.VideoMax.Seconds(); out >= MaxPutBytes {
+ t.Errorf("%s at the bitrate ceiling is %.0f bytes, over one PUT's %d", l.VideoMax, out, int64(MaxPutBytes))
+ }
+ run := download + encode + upload
+ if run > long.Deadline {
+ t.Errorf("%s of SDR at %.2f: download %s, encode %s, upload %s = %s, over the long class's %s deadline", l.VideoMax, slowSDR, download, encode, upload, run, long.Deadline)
+ }
+ if total := start + run + StopGrace; total > GrantLifetime {
+ t.Errorf("%s of SDR at %.2f takes %s from RunTask, past the %s grant", l.VideoMax, slowSDR, total, GrantLifetime)
+ }
+}
+
func TestEphemeralGiB(t *testing.T) {
for _, c := range []struct {
bytes int64
diff --git a/internal/mediajobs/jobs_test.go b/internal/mediajobs/jobs_test.go
index c2ed70304..4ef54882b 100644
--- a/internal/mediajobs/jobs_test.go
+++ b/internal/mediajobs/jobs_test.go
@@ -16,6 +16,7 @@ import (
"github.com/carlosframework/platform/internal/media"
"github.com/carlosframework/platform/internal/media/mediatest"
+ "github.com/carlosframework/platform/internal/mediahandler"
)
var (
@@ -2206,7 +2207,7 @@ func TestLongVideoReservesTwice(t *testing.T) {
key, preset, class string
want int64
}{
- {"uploads/long.mov", video, media.ClassLong, 20 * (2 * time.Hour).Milliseconds()},
+ {"uploads/long.mov", video, media.ClassLong, 20 * (3 * time.Hour).Milliseconds()},
{"uploads/short.mov", video, "", 10 * ((15 * time.Minute).Milliseconds() + stop)},
{"uploads/long.m4a", audio, media.ClassLong, 1 * (8 * time.Hour).Milliseconds()},
} {
@@ -2263,3 +2264,118 @@ func TestLegacyRowsKeepTheirWeight(t *testing.T) {
t.Fatalf("legacy rows charged %d units, want %d", u.Charged, want)
}
}
+
+// TestThreeHourSDRConverts (spec §11 A9, Paul 2026-11-25: LiveKit Cloud
+// records up to 3 hours a file): with the default limits a long video is
+// granted 3 hours and reserved at weight 2, 360 allowance minutes; its
+// payload names the three maximums; the codec's own probe check, given
+// that payload, converts 3 hours of SDR up to 1080p and refuses 1 ms more,
+// while HDR up to 1080p keeps 75 minutes and above 1080p keeps 30. Done at
+// 3 hours of 1080p, the job is charged 180 minutes at weight 1.
+func TestThreeHourSDRConverts(t *testing.T) {
+ h := newHarness(t, "", nil)
+ j := h.submit("acct1", "shop", "uploads/stream.mp4", video, media.ClassLong, 3<<30)
+ r, task := h.run(j)
+ if r.GrantedS != 3*3600 || r.DeadlineS != 50*60 {
+ t.Fatalf("granted %d s, deadline %d s; want 10800 and 3000", r.GrantedS, r.DeadlineS)
+ }
+ if u := h.usage("acct1", h.now); u.Reserved != 20*(3*time.Hour).Milliseconds() || u.Charged != 0 {
+ t.Fatalf("usage %+v, want 360 minutes reserved", u)
+ }
+ req := h.eng.Requests(r.Execution)
+ var p media.Payload
+ if len(req) != 1 || json.Unmarshal(req[0].Payload, &p) != nil {
+ t.Fatalf("run requests %+v", req)
+ }
+ want := media.JobLimits{SourceBytes: 40 << 30, GrantedMS: 10_800_000, GrantedBy: grantedByClass,
+ MaxMS: 10_800_000, MaxHDRMS: 4_500_000, MaxHiMS: 1_800_000, OutputBytes: media.MaxPutBytes}
+ if p.Limits != want {
+ t.Fatalf("payload limits %+v, want %+v", p.Limits, want)
+ }
+ for _, c := range []struct {
+ name string
+ width, height int
+ transfer, sec string
+ want string
+ }{
+ {"3 h of 1080p SDR", 1920, 1080, "bt709", "10800.000000", ""},
+ {"3 h and 1 ms of 1080p SDR", 1920, 1080, "bt709", "10800.001000", media.CodeSourceTooLong},
+ {"3 h of portrait 720p", 720, 1280, "", "10800.000000", ""},
+ {"3 h and 1 ms of portrait 720p", 720, 1280, "", "10800.001000", media.CodeSourceTooLong},
+ {"76 min of 1080p SDR", 1920, 1080, "bt709", "4560.000000", ""},
+ {"75 min of 1080p HLG", 1920, 1080, "arib-std-b67", "4500.000000", ""},
+ {"75 min and 1 ms of 1080p HLG", 1920, 1080, "arib-std-b67", "4500.001000", media.CodeSourceTooLong},
+ {"75 min and 1 ms of 1080p PQ", 1080, 1920, "smpte2084", "4500.001000", media.CodeSourceTooLong},
+ {"30 min of 4K", 3840, 2160, "bt709", "1800.000000", ""},
+ {"30 min and 1 ms of 4K", 3840, 2160, "bt709", "1800.001000", media.CodeSourceTooLong},
+ {"30 min and 1 ms of 1440p", 2560, 1440, "", "1800.001000", media.CodeSourceTooLong},
+ } {
+ probe := fmt.Sprintf(`{"streams":[{"index":0,"codec_type":"video","codec_name":"h264","width":%d,"height":%d,`+
+ `"avg_frame_rate":"30/1","r_frame_rate":"30/1","color_transfer":%q}],"format":{"duration":%q}}`, c.width, c.height, c.transfer, c.sec)
+ res, refused := mediahandler.ProbeFailure(r.Execution, video, p.Limits, []byte(probe))
+ switch {
+ case c.want == "" && refused:
+ t.Errorf("%s: refused %s, want it to convert", c.name, res.Error)
+ case c.want != "" && (!refused || res.Error != c.want):
+ t.Errorf("%s: refused %v with %q, want %s", c.name, refused, res.Error, c.want)
+ }
+ }
+ h.now = h.now.Add(32 * time.Minute)
+ h.codecSucceedsAt(r, (3 * time.Hour).Milliseconds(), 1920, 1080)
+ h.finish(task, media.ExitDone)
+ h.track()
+ got := h.row(j)
+ if got.State != stateDone || got.SourceMS != 10_800_000 || got.WeightTenths != 10 {
+ t.Fatalf("done row: %s source %d ms weight %d", got.State, got.SourceMS, got.WeightTenths)
+ }
+ if m, err := h.jobs.ChargedMinutes(tctx, "acct1", h.now); err != nil || m != 180 {
+ t.Fatalf("charged %d minutes, want 180 (%v)", m, err)
+ }
+}
+
+// TestThreeHourGrantFitsTwiceTheAllowance (spec §7.3, §11 A7 and A9): a
+// long video reserves at weight 2, so 3 hours need 360 allowance minutes
+// free. An account with a limit of 180 (R = 360) is granted the full 3
+// hours with a reservation of exactly twice its limit, and nothing else of
+// its fits until that settles; at 179 (R = 358) the allowance grants 179
+// minutes, and the codec answers allowance_exceeded for 3 hours of SDR.
+// A 3-hour done job charges 180 minutes, under the door's 402 at twice
+// 180.
+func TestThreeHourGrantFitsTwiceTheAllowance(t *testing.T) {
+ h := newHarness(t, "account_long=2,long=4", map[string]int64{"acct1": 180, "acct2": 179})
+ a := h.submit("acct1", "shop", "uploads/stream-1.mp4", video, media.ClassLong, 3<<30)
+ ra, task := h.run(a)
+ if ra.GrantedS != 10800 || ra.DeadlineS != 3000 {
+ t.Fatalf("limit 180: granted %d s, deadline %d s; want 10800 and 3000", ra.GrantedS, ra.DeadlineS)
+ }
+ if u := h.usage("acct1", h.now); u.Charged+u.Reserved != 2*180*unitsPerMinute {
+ t.Fatalf("limit 180: usage %+v, want exactly twice the limit", u)
+ }
+ b := h.submit("acct1", "shop", "uploads/stream-2.mp4", video, media.ClassLong, 3<<30)
+ if h.dispatch(media.ClassLong) != nil || h.row(b).State != stateQueued {
+ t.Fatal("a second long video dispatched with R = 0")
+ }
+ h.now = h.now.Add(32 * time.Minute)
+ h.codecSucceedsAt(ra, (3 * time.Hour).Milliseconds(), 1920, 1080)
+ h.finish(task, media.ExitDone)
+ h.track()
+ if m, err := h.jobs.ChargedMinutes(tctx, "acct1", h.now); err != nil || m != 180 || m/2 >= 180 {
+ t.Fatalf("limit 180: charged %d minutes, want 180, under the 402 at 360 (%v)", m, err)
+ }
+
+ c := h.submit("acct2", "shop", "uploads/stream-3.mp4", video, media.ClassLong, 3<<30)
+ rc, _ := h.run(c)
+ if rc.GrantedS != 179*60 || rc.DeadlineS != 3000 {
+ t.Fatalf("limit 179: granted %d s, deadline %d s; want 10740 and 3000", rc.GrantedS, rc.DeadlineS)
+ }
+ var p media.Payload
+ if req := h.eng.Requests(rc.Execution); len(req) != 1 || json.Unmarshal(req[0].Payload, &p) != nil ||
+ p.Limits.GrantedMS != 179*60000 || p.Limits.GrantedBy != grantedByAllowance || p.Limits.MaxMS != 10_800_000 {
+ t.Fatalf("limit 179: payload limits %+v", p.Limits)
+ }
+ probe := []byte(`{"streams":[{"index":0,"codec_type":"video","codec_name":"h264","width":1920,"height":1080,` +
+ `"avg_frame_rate":"30/1","r_frame_rate":"30/1"}],"format":{"duration":"10800.000000"}}`)
+ if res, refused := mediahandler.ProbeFailure(rc.Execution, video, p.Limits, probe); !refused || res.Error != media.CodeAllowanceExceeded {
+ t.Fatalf("limit 179, 3 h of SDR: refused %v with %q, want allowance_exceeded", refused, res.Error)
+ }
+}
diff --git a/internal/sim/mediajobs/invariants_test.go b/internal/sim/mediajobs/invariants_test.go
index 3bf080a2c..5ba56e2e8 100644
--- a/internal/sim/mediajobs/invariants_test.go
+++ b/internal/sim/mediajobs/invariants_test.go
@@ -60,6 +60,16 @@ import (
// 0.1 for audio; a settled row is charged at the weight its row
// stores, which is never above that reservation weight and is 2 only
// on a long video whose codec reported a source above 1080p.
+// - I8 (grant, spec §7.3, §11 A2 and A9). Every dispatched row's
+// granted_s is at most its class's granted figure (the deployment's
+// video_max or audio_max on a long row) and its deadline_s at most
+// its class's deadline, so no reservation passes what its class can
+// cost; a done row's source_ms is at most its granted_s, so its
+// charge is at most w × granted_s. Every Run that was not rejected
+// carries the row's grant and the deployment's maximums in its
+// payload: GrantedMS is granted_s, so the codec converts and reports
+// no more than was reserved, and MaxMS, MaxHDRMS and MaxHiMS are the
+// configured duration maximums for the preset.
// - I3 (weight). A charged row settles at weight 2 exactly when it is a
// long video and the job table read, in the step it settled, a result
// whose source facts its codec wrote above 1080p. Any other charged
@@ -542,6 +552,27 @@ func check(w *world) {
}
}
}
+ // I8 (grant).
+ for _, r := range rows {
+ if r.Execution == "" {
+ continue
+ }
+ k := media.ClassKey{Preset: r.Preset, Class: r.Class}
+ spec, ok := media.Class(k)
+ if !ok {
+ w.fail("I8", "row %s was dispatched in class %s, which has no row", shortID(r.JobID), k)
+ continue
+ }
+ if most := w.limits.Granted(k).Milliseconds(); r.GrantedS*1000 > most {
+ w.fail("I8", "row %s %s was granted %d s, over its class's %d ms", shortID(r.JobID), k, r.GrantedS, most)
+ }
+ if most := min(spec.Deadline, media.MaxDeadline).Milliseconds(); r.DeadlineS*1000 > most {
+ w.fail("I8", "row %s %s has a deadline of %d s, over its class's %d ms", shortID(r.JobID), k, r.DeadlineS, most)
+ }
+ if r.State == "done" && r.SourceMS > r.GrantedS*1000 {
+ w.fail("I8", "row %s is done with a %d ms source, longer than its %d s grant", shortID(r.JobID), r.SourceMS, r.GrantedS)
+ }
+ }
for _, a := range accounts {
if limit := w.allowance[a]; limit > 0 {
if most := 2 * limit * 600000; used[a] > most {
@@ -555,6 +586,9 @@ func check(w *world) {
if r.Misconfigured != "" {
w.fail("I8", "an execution of %s was run on a revision refused for %s", r.Class, r.Misconfigured)
}
+ if row, ok := byExec[r.Execution]; ok && !r.Rejected {
+ w.checkPayload(row)
+ }
if row, ok := byExec[r.Execution]; ok && r.At.Sub(time.UnixMilli(row.DispatchedAt)) > media.GrantMintSlack {
w.fail("I1", "row %s's execution was run %s after its dispatch, past the %s the pass's bounds allow", shortID(row.JobID), r.At.Sub(time.UnixMilli(row.DispatchedAt)), media.GrantMintSlack)
}
@@ -710,3 +744,21 @@ func (w *world) checkPrune(prune func() error) error {
}
return nil
}
+
+// checkPayload is I8 (grant) for one Run: the payload's limits are the
+// row's grant and the deployment's duration maximums for its preset.
+func (w *world) checkPayload(r row) {
+ _, p, ok := w.lastRequest(r.Execution)
+ if !ok {
+ return // lastRequest reported a payload that does not decode
+ }
+ maxMS, hdrMS, hiMS := w.limits.AudioMax.Milliseconds(), int64(0), int64(0)
+ if r.Preset == media.PresetVideo720p {
+ maxMS, hdrMS, hiMS = w.limits.VideoMax.Milliseconds(), w.limits.MaxHDR().Milliseconds(), w.limits.VideoMaxHi.Milliseconds()
+ }
+ l := p.Limits
+ if l.GrantedMS != r.GrantedS*1000 || l.MaxMS != maxMS || l.MaxHDRMS != hdrMS || l.MaxHiMS != hiMS {
+ w.fail("I8", "execution %s's payload grants %d ms with maximums %d, %d and %d ms; its row was granted %d s, and the deployment's maximums are %d, %d and %d ms",
+ shortID(r.Execution), l.GrantedMS, l.MaxMS, l.MaxHDRMS, l.MaxHiMS, r.GrantedS, maxMS, hdrMS, hiMS)
+ }
+}
diff --git a/internal/sim/mediajobs/witness_test.go b/internal/sim/mediajobs/witness_test.go
index 142d0279b..769c4998c 100644
--- a/internal/sim/mediajobs/witness_test.go
+++ b/internal/sim/mediajobs/witness_test.go
@@ -610,6 +610,50 @@ func TestWitnessAbove1080pWeighsTwice(t *testing.T) {
}
}
+// TestWitnessThreeHourSDR (spec §11 A9): with the default limits, an
+// account with a limit of 180 (R = 360 minutes) runs a long video, which
+// reserves at weight 2 and so is granted the full 3 hours, exactly twice
+// the limit; its codec reports 3 hours of 1080p SDR, charged 180 at
+// weight 1. A codec reporting 1 ms over its 3-hour grant fails the job,
+// and the charge stays inside the reservation. Every step is checked
+// against I1 to I9, the payload's grant and maximums among them.
+func TestWitnessThreeHourSDR(t *testing.T) {
+ w := newWitness(t, "", map[string]int64{"acct1": 180})
+ w.object(shop, "uploads/stream-1.mp4", 3<<30)
+ job, task := w.runJob(t, shop, "uploads/stream-1.mp4", video, media.ClassLong)
+ r := w.rowOf(t, shop, job.ID)
+ if r.GrantedS != 10800 || r.DeadlineS != 3000 || reservation(r) != 2*180*600000 {
+ t.Fatalf("long video: granted %d s, deadline %d s, reserved %d", r.GrantedS, r.DeadlineS, reservation(r))
+ }
+ if _, p, _ := w.lastRequest(r.Execution); p.Limits.GrantedMS != 10_800_000 || p.Limits.MaxMS != 10_800_000 ||
+ p.Limits.MaxHDRMS != 4_500_000 || p.Limits.MaxHiMS != 1_800_000 {
+ t.Fatalf("payload limits %+v", p.Limits)
+ }
+ w.now = w.now.Add(32 * time.Minute)
+ w.codecSucceedsAs(r.Execution, (3 * time.Hour).Milliseconds(), 0, sourcePicture{1920, 1080, false})
+ w.finish(t, task, media.ExitDone)
+ w.track(t)
+ r = w.rowOf(t, shop, job.ID)
+ if r.State != "done" || r.SourceMS != 10_800_000 || r.WeightTenths != 10 || charge(r) != 180*600000 {
+ t.Fatalf("3 h done: %s source %d weight %d charge %d", r.State, r.SourceMS, r.WeightTenths, charge(r))
+ }
+ if m, err := w.jobs.ChargedMinutes(ctx, "acct1", w.now); err != nil || m != 180 {
+ t.Fatalf("charged %d minutes, want 180 (%v)", m, err)
+ }
+
+ other := appRef{"acct2", "shop"}
+ w.object(other, "uploads/stream-2.mp4", 3<<30)
+ oj, ot := w.runJob(t, other, "uploads/stream-2.mp4", video, media.ClassLong)
+ or := w.rowOf(t, other, oj.ID)
+ w.now = w.now.Add(32 * time.Minute)
+ w.codecSucceedsAs(or.Execution, (3*time.Hour).Milliseconds()+1, 0, sourcePicture{1920, 1080, false})
+ w.finish(t, ot, media.ExitDone)
+ w.track(t)
+ if or = w.rowOf(t, other, oj.ID); or.State != "failed" || or.SourceMS != 0 || charge(or) > reservation(or) {
+ t.Fatalf("1 ms over the grant: %s %q source %d charge %d reservation %d", or.State, or.Error, or.SourceMS, charge(or), reservation(or))
+ }
+}
+
// TestWitnessRetiredBeforeAndAfterTheRead (spec §11 A7; Astra round 1,
// P2-1): a long 4K video whose app is retired after the job table read its
// result settles failed retired at weight 2, the weight of the facts it
@@ -870,7 +914,7 @@ func TestCheckerCatchesEachInvariant(t *testing.T) {
f.w.insertRow(x)
f.w.engine.SetLag(mediatest.Lag{Create: 5 * time.Minute})
f.w.engine.Script(mediatest.RunAmbiguousCreatedLate)
- if _, err := f.w.rec.Run(ctx, media.RunRequest{Execution: x.Execution, Class: media.ClassKey{Preset: video, Class: media.ClassShort}}); err == nil {
+ if _, err := f.w.rec.Run(ctx, media.RunRequest{Execution: x.Execution, Class: media.ClassKey{Preset: video, Class: media.ClassShort}, Payload: f.w.payloadFor(t, x, nil)}); err == nil {
t.Fatal("the scripted ambiguous answer did not happen")
}
check(f.w)
@@ -915,6 +959,37 @@ func TestCheckerCatchesEachInvariant(t *testing.T) {
x.Execution, x.GrantedS, x.DeadlineS = strings.Repeat("8", 32), 7200, 3000
f.w.insertRow(x)
}},
+ {"I8", "a long video granted past the video maximum", func(t *testing.T, f fixture) {
+ x := f.r
+ x.Class, x.TaskARN, x.Key = media.ClassLong, "", "uploads/clip-6.mov"
+ x.JobID = media.JobID(x.Key, x.ETag, video, media.ClassLong)
+ x.Execution, x.GrantedS, x.DeadlineS = strings.Repeat("9", 32), int64(f.w.limits.VideoMax/time.Second)+1, 3000
+ f.w.insertRow(x)
+ }},
+ {"I8", "a long video with a deadline past its class's", func(t *testing.T, f fixture) {
+ x := f.r
+ x.Class, x.TaskARN, x.Key = media.ClassLong, "", "uploads/clip-6.mov"
+ x.JobID = media.JobID(x.Key, x.ETag, video, media.ClassLong)
+ x.Execution, x.GrantedS, x.DeadlineS = strings.Repeat("9", 32), 600, 3001
+ f.w.insertRow(x)
+ }},
+ {"I8", "a done row with a source longer than its grant", func(t *testing.T, f fixture) {
+ f.w.exec(t, `UPDATE media_jobs SET granted_s = ? WHERE job_id = ?`, f.d.SourceMS/1000-1, f.d.JobID)
+ }},
+ {"I8", "a Run whose payload grants more than its row", func(t *testing.T, f fixture) {
+ b := f.w.payloadFor(t, f.r, func(l *media.JobLimits) { l.GrantedMS++ })
+ f.w.engine.Script(mediatest.RunAmbiguousNotCreated)
+ if _, err := f.w.rec.Run(ctx, media.RunRequest{Execution: f.r.Execution, Class: media.ClassKey{Preset: video, Class: media.ClassShort}, Payload: b}); err == nil {
+ t.Fatal("the scripted transport error did not happen")
+ }
+ }},
+ {"I8", "a Run whose payload names a shorter video maximum", func(t *testing.T, f fixture) {
+ b := f.w.payloadFor(t, f.r, func(l *media.JobLimits) { l.MaxMS -= 60000 })
+ f.w.engine.Script(mediatest.RunAmbiguousNotCreated)
+ if _, err := f.w.rec.Run(ctx, media.RunRequest{Execution: f.r.Execution, Class: media.ClassKey{Preset: video, Class: media.ClassShort}, Payload: b}); err == nil {
+ t.Fatal("the scripted transport error did not happen")
+ }
+ }},
{"I1", "an execution Run more than GrantMintSlack after its dispatch", func(t *testing.T, f fixture) {
f.w.now = f.w.now.Add(media.GrantMintSlack + time.Second)
f.w.engine.Script(mediatest.RunRejectedFinal) // no task, so only the late Run is wrong
@@ -973,6 +1048,25 @@ func TestCheckerCatchesEachInvariant(t *testing.T) {
}
}
+// payloadFor is a Run payload for row r's execution as the job table
+// writes its limits (I8, grant): r's grant and the deployment's
+// maximums, edited by mutate when it is not nil.
+func (w *world) payloadFor(t *testing.T, r row, mutate func(*media.JobLimits)) []byte {
+ t.Helper()
+ l := media.JobLimits{GrantedMS: r.GrantedS * 1000, MaxMS: w.limits.AudioMax.Milliseconds()}
+ if r.Preset == video {
+ l.MaxMS, l.MaxHDRMS, l.MaxHiMS = w.limits.VideoMax.Milliseconds(), w.limits.MaxHDR().Milliseconds(), w.limits.VideoMaxHi.Milliseconds()
+ }
+ if mutate != nil {
+ mutate(&l)
+ }
+ b, err := json.Marshal(media.Payload{V: media.PayloadVersion, Execution: r.Execution, Preset: r.Preset, Limits: l})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return b
+}
+
// settledFailed is a video row of class that failed engine_failed and
// settled at weight, charged 1 s, with no task ever observed (so I3's
// charge checks pass and only the weight is in question).