carlos / platform Public

media: sim hands stale launches to Launch; fix the twenty-minute witness

Task 1 review, fix round 1.

TestWitnessTwentyMinutesLeft advanced 10 minutes against a 570 s
deadline, so the fake's deadline bound (start + 600 s) ended the task
before codec's exit 0 and a correct Track would report
deadline_exceeded. It now advances 9 minutes.

The sim no longer drops a held launch older than GrantMintSlack: it
calls Launch, which must refuse it (controller ruling, noted on the
Launch declaration). I1 now reports a Run more than GrantMintSlack after
its row's dispatch, and I5 a Grant that late (recorded by a grants
wrapper); each has a checker witness. A refused stale launch counts as
"no task" for I3.

The simulation stays red until Task 2. Not pushed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev af947da04e74f079349afb98ed195d6a4d63ae49 parent 8ae1128
5 files changed, +106 −23
  • internal/mediajobs/jobs.go +6 −1
  • internal/sim/mediajobs/actions_test.go +30 −12
  • internal/sim/mediajobs/invariants_test.go +17 −2
  • internal/sim/mediajobs/witness_test.go +20 −1
  • internal/sim/mediajobs/world_test.go +33 −7
diff --git a/internal/mediajobs/jobs.go b/internal/mediajobs/jobs.go
index a182251fb..a1c3a86f3 100644
--- a/internal/mediajobs/jobs.go
+++ b/internal/mediajobs/jobs.go
@@ -165,7 +165,12 @@ func (j *Jobs) DispatchNext(ctx context.Context, class string, now time.Time) (*
return nil, errNotImplemented
}
-// Launch mints the grant and starts the task of a dispatched row.
+// Launch mints the grant and starts the task of a dispatched row. It
+// refuses a launch more than media.GrantMintSlack after the row's
+// dispatched_at (a console that stalled between DispatchNext and Launch):
+// no Grant and no Run, and the row settles through the ordinary paths.
+// The cleanup bound (dispatched_at + GrantLifetime + GrantMintSlack)
+// relies on it.
func (j *Jobs) Launch(ctx context.Context, l *Launch, now time.Time) error {
return errNotImplemented
}
diff --git a/internal/sim/mediajobs/actions_test.go b/internal/sim/mediajobs/actions_test.go
index b5014d9b4..88704b9eb 100644
--- a/internal/sim/mediajobs/actions_test.go
+++ b/internal/sim/mediajobs/actions_test.go
@@ -174,6 +174,10 @@ func actRecover(w *world) bool {
func actDispatch(w *world) bool {
class := []string{media.ClassShort, media.ClassLong}[w.rng.Intn(2)]
+ before := map[string]string{}
+ for _, r := range w.readRows() {
+ before[r.id()] = r.Execution
+ }
l, err := w.jobs.DispatchNext(ctx, class, w.now)
var mis *media.Misconfigured
w.errAccepted("dispatch "+class, err, errors.As(err, &mis))
@@ -181,20 +185,28 @@ func actDispatch(w *world) bool {
w.logf("dispatch %s: nothing (%v)", class, err)
return false
}
- w.held = append(w.held, heldLaunch{l: l, at: w.now})
+ h := heldLaunch{l: l, at: w.now}
+ for _, r := range w.readRows() {
+ if r.State == "running" && r.Execution != "" && r.Execution != before[r.id()] {
+ h.execution = r.Execution
+ }
+ }
+ w.held = append(w.held, h)
w.logf("dispatch %s: launch held (%d held)", class, len(w.held))
w.events["dispatched"]++
return true
}
// actLaunch launches one held dispatch. An error is accepted only when the
-// engine answered this launch's Run ambiguously.
+// engine answered this launch's Run ambiguously, or the launch is stale.
//
// The console launches in the pass that dispatched, and the job table's
// cleanup bound (dispatched_at + GrantLifetime + GrantMintSlack) assumes
-// the grant is minted within GrantMintSlack of dispatch. A held launch
-// older than that is dropped, as a console that stalled that long would
-// have been restarted: the row goes to recovery.
+// the grant is minted within GrantMintSlack of dispatch. A launch older
+// than that (a console that stalled between DispatchNext and Launch) is
+// still handed to Launch, which must refuse it: no Grant and no Run (I1
+// and I5 report a late Run or Grant); the row then settles through the
+// ordinary paths.
func actLaunch(w *world) bool {
if len(w.held) == 0 {
return false
@@ -202,16 +214,22 @@ func actLaunch(w *world) bool {
i := w.rng.Intn(len(w.held))
h := w.held[i]
w.held = append(w.held[:i], w.held[i+1:]...)
- if w.now.Sub(h.at) > media.GrantMintSlack {
- w.logf("launch: dropped, dispatched %s ago", w.now.Sub(h.at))
- w.events["launch-stale-dropped"]++
- return false
- }
+ stale := w.now.Sub(h.at) > media.GrantMintSlack
before := len(w.runs)
err := w.jobs.Launch(ctx, h.l, w.now)
- ambiguous := len(w.runs) > before && w.runs[len(w.runs)-1].Ambiguous
+ ran := len(w.runs) > before
+ if stale {
+ w.logf("launch: stale, dispatched %s ago: ran=%v err=%v", w.now.Sub(h.at), ran, err)
+ w.events["launch-stale"]++
+ if !ran && h.execution != "" {
+ w.rejected[h.execution] = true
+ }
+ w.errAccepted("launch", err, true) // the refusal may come back as an error
+ return true
+ }
+ ambiguous := ran && w.runs[len(w.runs)-1].Ambiguous
w.errAccepted("launch", err, ambiguous)
- if len(w.runs) > before {
+ if ran {
r := w.runs[len(w.runs)-1]
w.logf("launch %s: rejected=%v ambiguous=%v err=%v", r.Class, r.Rejected, r.Ambiguous, err)
switch {
diff --git a/internal/sim/mediajobs/invariants_test.go b/internal/sim/mediajobs/invariants_test.go
index 97d6907b9..945c687ff 100644
--- a/internal/sim/mediajobs/invariants_test.go
+++ b/internal/sim/mediajobs/invariants_test.go
@@ -15,7 +15,9 @@ import (
// queued or running row, and the pass finds work only by querying
// rows. Checked: such a row's state is queued or running, and every
// task ECS shows running belongs to a row with that execution and
-// task_stopped 0.
+// task_stopped 0, and no execution is Run more than GrantMintSlack
+// after its row's dispatch (a stalled launch the row's recovery and
+// cleanup bounds no longer cover).
// - I2. Charged exactly once. settled goes from false to true in the
// transaction that writes charged_ms and the terminal state, and
// nothing writes charged_ms afterwards. Checked: settled exactly when
@@ -29,7 +31,9 @@ import (
// set, or has task_stopped false is never deleted.
// - I5. One execution. A done row's outputs are the keys of its own
// execution_id, and no other execution ever held a request for them.
-// Checked also: they were never deleted.
+// Checked also: they were never deleted, and no execution's outputs
+// are granted more than GrantMintSlack after its row's dispatch (a PUT
+// could then land after the row's cleanup deleted the outputs).
// - I6. Capacity. The number of rows with a dispatched execution and
// task_stopped false never exceeds the dispatch cap, whatever their
// state or retirement: per class, deployment-wide and per account.
@@ -355,6 +359,17 @@ 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.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)
+ }
+ }
+ }
+ for i := range w.grantLog {
+ if g := &w.grantLog[i]; !g.Checked {
+ g.Checked = true
+ if row, ok := byExec[g.Execution]; ok && g.At.Sub(time.UnixMilli(row.DispatchedAt)) > media.GrantMintSlack {
+ w.fail("I5", "row %s's outputs were granted %s after its dispatch, so a PUT could land after its cleanup", shortID(row.JobID), g.At.Sub(time.UnixMilli(row.DispatchedAt)))
+ }
}
}
diff --git a/internal/sim/mediajobs/witness_test.go b/internal/sim/mediajobs/witness_test.go
index 4e2e8937e..ff7f3d299 100644
--- a/internal/sim/mediajobs/witness_test.go
+++ b/internal/sim/mediajobs/witness_test.go
@@ -442,7 +442,9 @@ func TestWitnessTwentyMinutesLeft(t *testing.T) {
if err := w.engine.Start(vt.ARN, w.now); err != nil {
t.Fatal(err)
}
- w.now = w.now.Add(10 * time.Minute)
+ // 9 minutes: inside the 570 s deadline, so codec's exit 0 is what
+ // stops the task (at 10 minutes the deadline container would have).
+ w.now = w.now.Add(9 * time.Minute)
w.codecSucceeds(rv.Execution, (9 * time.Minute).Milliseconds(), 0)
w.finish(t, vt, media.ExitDone)
w.track(t)
@@ -668,6 +670,23 @@ func TestCheckerCatchesEachInvariant(t *testing.T) {
x.Execution, x.GrantedS, x.DeadlineS = strings.Repeat("8", 32), 7200, 3000
f.w.insertRow(x)
}},
+ {"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
+ if _, err := f.w.rec.Run(ctx, media.RunRequest{Execution: f.r.Execution, Class: media.ClassKey{Preset: video, Class: media.ClassShort}}); err == nil {
+ t.Fatal("the scripted rejection did not happen")
+ }
+ }},
+ {"I5", "outputs granted more than GrantMintSlack after dispatch", func(t *testing.T, f fixture) {
+ f.w.now = f.w.now.Add(media.GrantMintSlack + time.Second)
+ gr := media.GrantRequest{Bucket: f.r.Bucket, SourceKey: f.r.Key, SourceETag: f.r.ETag, OutputPrefix: media.OutputPrefix(f.r.Key, f.r.ETag, video, f.r.Execution), Execution: f.r.Execution}
+ for _, n := range media.OutputNames(video) {
+ gr.Outputs = append(gr.Outputs, media.OutputFile{Name: n, ContentType: media.ContentType(n)})
+ }
+ if _, _, err := f.w.recGrants.Grant(ctx, gr); err != nil {
+ t.Fatal(err)
+ }
+ }},
{"I8", "an execution run on a refused revision", func(t *testing.T, f fixture) {
k := media.ClassKey{Preset: audio, Class: media.ClassShort}
f.w.engine.Misconfigure(k, "depends_on")
diff --git a/internal/sim/mediajobs/world_test.go b/internal/sim/mediajobs/world_test.go
index a04ba689e..1759f53e4 100644
--- a/internal/sim/mediajobs/world_test.go
+++ b/internal/sim/mediajobs/world_test.go
@@ -99,6 +99,7 @@ func (s *storeGuard) newIncarnation() string {
type runRecord struct {
Execution string
Class media.ClassKey
+ At time.Time
Misconfigured string // the check the class's revision failed at Run time; "" when sound
Rejected bool
Ambiguous bool
@@ -112,7 +113,7 @@ type recEngine struct {
}
func (e *recEngine) Run(c context.Context, req media.RunRequest) (string, error) {
- rr := runRecord{Execution: req.Execution, Class: req.Class}
+ rr := runRecord{Execution: req.Execution, Class: req.Class, At: e.w.now}
var mis *media.Misconfigured
if err := e.f.CheckRevision(c, req.Class); errors.As(err, &mis) {
rr.Misconfigured = mis.Check
@@ -146,11 +147,33 @@ func (e *recEngine) CheckRevision(c context.Context, k media.ClassKey) error {
return e.f.CheckRevision(c, k)
}
-// heldLaunch is a dispatched row waiting for Launch, and when it was
-// dispatched.
+// heldLaunch is a dispatched row waiting for Launch, when it was
+// dispatched, and the execution dispatch gave its row.
type heldLaunch struct {
- l *mj.Launch
- at time.Time
+ l *mj.Launch
+ at time.Time
+ execution string
+}
+
+// grantRecord is one Grant the job table minted.
+type grantRecord struct {
+ Execution string
+ At time.Time
+ Checked bool
+}
+
+// recGrants passes every call to the fake grants and records each Grant.
+type recGrants struct {
+ *mediatest.FakeGrants
+ w *world
+}
+
+func (g *recGrants) Grant(c context.Context, r media.GrantRequest) (media.Transfers, time.Time, error) {
+ tr, exp, err := g.FakeGrants.Grant(c, r)
+ if err == nil {
+ g.w.grantLog = append(g.w.grantLog, grantRecord{Execution: r.Execution, At: g.w.now})
+ }
+ return tr, exp, err
}
// destroyEvent is a store destroy (or app purge) the world carried out:
@@ -183,7 +206,9 @@ type world struct {
purging map[appRef]bool
runs []runRecord
- rejected map[string]bool // executions a Run definitively rejected
+ grantLog []grantRecord
+ recGrants *recGrants
+ rejected map[string]bool // executions known to have no task: a Run definitively rejected, or a stale launch refused before any Run
ecs map[string]media.Task // the last record ECS showed for each execution, kept after it expires
prev map[string]row // the rows at the last check, by rowKey
destroyed []destroyEvent
@@ -235,6 +260,7 @@ func buildWorld(seed int64, rng *rand.Rand, verbose bool, limits media.Limits, a
w.engine = mediatest.NewFakeEngine(clock)
w.rec = &recEngine{w: w, f: w.engine}
w.grants = mediatest.NewFakeGrants(clock)
+ w.recGrants = &recGrants{FakeGrants: w.grants, w: w}
w.stores = &storeGuard{w: w, mu: map[appRef]*sync.Mutex{}, recs: map[appRef]*storeRec{}}
for _, a := range accounts {
for _, n := range appNames {
@@ -265,7 +291,7 @@ func (w *world) restart() {
w.held = nil
w.jobs = mj.New(w.db, mj.Deps{
Engine: w.rec,
- Grants: w.grants,
+ Grants: w.recGrants,
Stores: w.stores,
Allowance: func(_ context.Context, account string) (int64, error) {
return w.allowance[account], nil