carlos / platform Public

media: settle a stop seen before its time in the next pass

Fix round 2 of Task 2 (Astra P2): a pass that began before midnight
could see a task ECS stopped after it, and endedAt then dropped the
valid stop time for the pass's clock, charging the earlier month.
endedAt is the one timestamp rule: a valid stop after the pass's now
defers settlement, the row keeping its place and reservation, and the
next pass settles it at ECS's stop time.

Tests: TestStopAfterThePassBeganWaits (success and failure across
midnight on 30 November) and TestEndedAtFallback; go test
./internal/mediajobs/ ./internal/sim/mediajobs/ pass, 200 seeds.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev abfa911cf7d9aa9320dc6aa19b41b130350f7753 parent ec59b0c
2 files changed, +92 −17
  • internal/mediajobs/jobs_test.go +70 −7
  • internal/mediajobs/track.go +22 −10

CI passed — run details

diff --git a/internal/mediajobs/jobs_test.go b/internal/mediajobs/jobs_test.go
index 17013c9e9..f5cdfeb05 100644
--- a/internal/mediajobs/jobs_test.go
+++ b/internal/mediajobs/jobs_test.go
@@ -1204,7 +1204,7 @@ func TestDelayedTrackingAcrossMonthEnd(t *testing.T) {
}
// TestEndedAtFallback: with no usable stop time from ECS, a settlement
-// ends at the observation time.
+// ends at the observation time; a stop time after it defers settlement.
func TestEndedAtFallback(t *testing.T) {
now := testStart
start := now.Add(-10 * time.Minute)
@@ -1212,18 +1212,81 @@ func TestEndedAtFallback(t *testing.T) {
name string
t media.Task
want time.Time
+ ok bool
}{
- {"stop time", media.Task{StartedAt: start, StoppedAt: now.Add(-time.Minute)}, now.Add(-time.Minute)},
- {"no stop time", media.Task{StartedAt: start}, now},
- {"stop before start", media.Task{StartedAt: start, StoppedAt: start.Add(-time.Second)}, now},
- {"stop after now", media.Task{StartedAt: start, StoppedAt: now.Add(time.Hour)}, now},
+ {"stop time", media.Task{StartedAt: start, StoppedAt: now.Add(-time.Minute)}, now.Add(-time.Minute), true},
+ {"stop time equal to now", media.Task{StartedAt: start, StoppedAt: now}, now, true},
+ {"no stop time", media.Task{StartedAt: start}, now, true},
+ {"stop before start", media.Task{StartedAt: start, StoppedAt: start.Add(-time.Second)}, now, true},
+ {"stop after now", media.Task{StartedAt: start, StoppedAt: now.Add(time.Second)}, time.Time{}, false},
} {
- if got := endedAt(c.t, now); got != c.want.UnixMilli() {
- t.Errorf("%s: ended %s, want %s", c.name, time.UnixMilli(got).UTC(), c.want)
+ got, ok := endedAt(c.t, now)
+ if ok != c.ok || ok && got != c.want.UnixMilli() {
+ t.Errorf("%s: ended %s ok %v, want %s ok %v", c.name, time.UnixMilli(got).UTC(), ok, c.want, c.ok)
}
}
}
+// TestStopAfterThePassBeganWaits (Astra P2, round 2): a pass that began at
+// 23:59:59 on 30 November sees a task ECS stopped at 00:00:01 on 1
+// December. It does not settle it, so the reservation is held; the next
+// pass settles it at the stop time, in December.
+func TestStopAfterThePassBeganWaits(t *testing.T) {
+ nov, dec := time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC), time.Date(2026, 12, 1, 0, 0, 0, 0, time.UTC)
+ passStart := time.Date(2026, 11, 30, 23, 59, 59, 0, time.UTC)
+ stop := time.Date(2026, 12, 1, 0, 0, 1, 0, time.UTC)
+ for _, c := range []struct {
+ name string
+ exit int
+ state string
+ code string
+ charge int64 // units
+ }{
+ {"success", media.ExitDone, stateDone, "", 10 * 60000},
+ {"failure", media.ExitFailed, stateFailed, media.CodeEngineFailed, 10 * (5 * time.Minute).Milliseconds()},
+ } {
+ t.Run(c.name, func(t *testing.T) {
+ h := newHarness(t, "", nil)
+ h.now = stop.Add(-5*time.Minute - time.Second)
+ a := h.submit("acct1", "shop", "uploads/a.mov", video, "", 10<<20)
+ r, task := h.run(a)
+ h.now = stop
+ if c.exit == media.ExitDone {
+ h.codecSucceeds(r, 60000)
+ }
+ h.finish(task, c.exit)
+ if err := h.jobs.Pass(tctx, passStart); err != nil {
+ t.Fatal(err)
+ }
+ got := h.row(a)
+ if got.State != stateRunning || got.Settled || got.TaskStopped || got.ChargedMS != 0 {
+ t.Fatalf("after the pass that began before the stop: %+v", got)
+ }
+ if u := h.usage("acct1", nov); u.Charged != 0 || u.Reserved != 10*930*1000 {
+ t.Fatalf("November after the first pass: %+v, want the reservation held", u)
+ }
+ if h.live(media.ClassShort) != 1 {
+ t.Fatal("the place was released before the row settled")
+ }
+ h.now = stop.Add(media.PassEvery)
+ if err := h.jobs.Pass(tctx, h.now); err != nil {
+ t.Fatal(err)
+ }
+ got = h.row(a)
+ wantState(t, got, c.state, c.code, (5 * time.Minute).Milliseconds())
+ if !got.EndedAt.Equal(stop) {
+ t.Fatalf("ended_at %s, want ECS's stop %s", got.EndedAt, stop)
+ }
+ if u := h.usage("acct1", dec); u.Charged != c.charge || u.Reserved != 0 {
+ t.Fatalf("December: %+v, want %d units charged", u, c.charge)
+ }
+ if u := h.usage("acct1", nov); u != (Usage{}) {
+ t.Fatalf("November: %+v, want nothing", u)
+ }
+ })
+ }
+}
+
// TestUploadRetryETagFilled (Astra P2): an output the handler reports with
// an empty ETag (an upload retry met 412) validates against its HEAD, and
// the stored result carries the HEAD's ETag, still after cleanup.
diff --git a/internal/mediajobs/track.go b/internal/mediajobs/track.go
index a8c365d63..aa98b99cb 100644
--- a/internal/mediajobs/track.go
+++ b/internal/mediajobs/track.go
@@ -79,16 +79,23 @@ func runtimeMS(r Job, t media.Task) int64 {
return ms
}
-// endedAt is when a stopped task ended, in Unix milliseconds: ECS's stop
-// time, so the charge lands in the month the task stopped, however late
-// the pass observes it (a restart across midnight on the last day of the
-// month). The fallback, when ECS gives no usable stop time (none, before
-// the start, or after now), is the observation time now.
-func endedAt(t media.Task, now time.Time) int64 {
- if t.StoppedAt.IsZero() || t.StoppedAt.Before(t.StartedAt) || t.StoppedAt.After(now) {
- return stamp(now)
+// endedAt is when a stopped task ended, in Unix milliseconds, and the one
+// timestamp rule of settlement. It is ECS's stop time, so the charge lands
+// in the month the task stopped, however late the pass observes it (a
+// restart across midnight on the last day of the month). When ECS gives no
+// usable stop time (none, or before the start), the fallback is the
+// observation time now. A valid stop time after now (the task stopped
+// while this pass, which began before it, was running) gives ok false: the
+// row is not settled in this pass, keeps its place and its reservation,
+// and the next pass settles it at that stop time.
+func endedAt(t media.Task, now time.Time) (ms int64, ok bool) {
+ if t.StoppedAt.IsZero() || t.StoppedAt.Before(t.StartedAt) {
+ return stamp(now), true
+ }
+ if t.StoppedAt.After(now) {
+ return 0, false
}
- return stamp(t.StoppedAt)
+ return stamp(t.StoppedAt), true
}
// trackOne settles one row from its task's record.
@@ -106,7 +113,12 @@ func (j *Jobs) trackOne(ctx context.Context, r Job, t media.Task, now time.Time)
if t.StartedAt.IsZero() {
return j.neverStarted(ctx, k, true, now)
}
- ran, ended := runtimeMS(r, t), endedAt(t, now)
+ ended, ok := endedAt(t, now)
+ if !ok {
+ j.logf("track %s: stopped at %s, after this pass began; settled next pass", k, t.StoppedAt.UTC().Format(time.RFC3339Nano))
+ return nil
+ }
+ ran := runtimeMS(r, t)
if retired {
return j.failStop(ctx, k, media.CodeRetired, ran, "", ended, now)
}