Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(domain): split run timing end and interval merge out of computeRunTiming (#351)

fx-kurumia54cb29d178e
diff --git a/internal/domain/run_timing.go b/internal/domain/run_timing.go
index c1ca2204..a13f950b 100644
--- a/internal/domain/run_timing.go
+++ b/internal/domain/run_timing.go
@@ -67,15 +67,16 @@ func RunExecutionIntervals(run Run, events []RunEvent, now time.Time) ([]RunExec
return intervals, known
}
+type timingInterval struct{ start, end time.Time }
+
func computeRunTiming(run Run, events []RunEvent, now time.Time) (RunTiming, []RunExecutionInterval, bool) {
- type interval struct{ start, end time.Time }
- var intervals []interval
+ var intervals []timingInterval
open := map[string]time.Time{}
var lastEvent, terminalAt time.Time
started := false
closeNode := func(nodeID string, at time.Time) {
if start, ok := open[nodeID]; ok {
- intervals = append(intervals, interval{start, at})
+ intervals = append(intervals, timingInterval{start, at})
delete(open, nodeID)
}
}
@@ -107,19 +108,7 @@ func computeRunTiming(run Run, events []RunEvent, now time.Time) (RunTiming, []R
}
out := RunTiming{AsOf: now}
- end := now
- if runTerminal(run.Status) {
- end = terminalAt
- if end.IsZero() {
- end = lastEvent
- }
- if end.IsZero() {
- end = run.UpdatedAt
- }
- if end.IsZero() {
- end = now
- }
- }
+ end := runTimingEnd(run, terminalAt, lastEvent, now)
if !started {
intervals = intervals[:0]
open = map[string]time.Time{}
@@ -129,7 +118,7 @@ func computeRunTiming(run Run, events []RunEvent, now time.Time) (RunTiming, []R
}
switch {
case !node.FinishedAt.IsZero():
- intervals = append(intervals, interval{node.StartedAt, node.FinishedAt})
+ intervals = append(intervals, timingInterval{node.StartedAt, node.FinishedAt})
case node.Status == "running":
open[nodeID] = node.StartedAt
}
@@ -142,15 +131,47 @@ func computeRunTiming(run Run, events []RunEvent, now time.Time) (RunTiming, []R
}
for _, start := range open {
if openEnd.After(start) {
- intervals = append(intervals, interval{start, openEnd})
+ intervals = append(intervals, timingInterval{start, openEnd})
+ }
+ }
+
+ merged, total, known := mergeExecutionIntervals(intervals, run.CreatedAt, end)
+ out.ExecutionMs = total.Milliseconds()
+ if !run.CreatedAt.IsZero() && end.After(run.CreatedAt) {
+ out.WallMs = end.Sub(run.CreatedAt).Milliseconds()
+ }
+ if out.WallMs < out.ExecutionMs {
+ out.WallMs = out.ExecutionMs
+ }
+ out.WaitingMs = out.WallMs - out.ExecutionMs
+ return out, merged, known
+}
+
+// runTimingEnd picks the instant a run's timing stops: now while the run is
+// open, otherwise the terminal run event, falling back to the last event, the
+// run's UpdatedAt, and finally now.
+func runTimingEnd(run Run, terminalAt, lastEvent, now time.Time) time.Time {
+ if !runTerminal(run.Status) {
+ return now
+ }
+ for _, at := range []time.Time{terminalAt, lastEvent, run.UpdatedAt} {
+ if !at.IsZero() {
+ return at
}
}
+ return now
+}
- var clipped []interval
+// mergeExecutionIntervals clips intervals to [createdAt, end], unions the
+// overlapping ones, and returns the merged spans with their total duration.
+// known reports whether any interval was a valid observation, so an observed
+// zero can be told apart from missing timing evidence.
+func mergeExecutionIntervals(intervals []timingInterval, createdAt, end time.Time) ([]RunExecutionInterval, time.Duration, bool) {
+ var clipped []timingInterval
known := false
for _, iv := range intervals {
- if !run.CreatedAt.IsZero() && iv.start.Before(run.CreatedAt) {
- iv.start = run.CreatedAt
+ if !createdAt.IsZero() && iv.start.Before(createdAt) {
+ iv.start = createdAt
}
if iv.end.After(end) {
iv.end = end
@@ -177,13 +198,5 @@ func computeRunTiming(run Run, events []RunEvent, now time.Time) (RunTiming, []R
total += cur.end.Sub(cur.start)
merged = append(merged, RunExecutionInterval{StartedAt: cur.start, FinishedAt: cur.end})
}
- out.ExecutionMs = total.Milliseconds()
- if !run.CreatedAt.IsZero() && end.After(run.CreatedAt) {
- out.WallMs = end.Sub(run.CreatedAt).Milliseconds()
- }
- if out.WallMs < out.ExecutionMs {
- out.WallMs = out.ExecutionMs
- }
- out.WaitingMs = out.WallMs - out.ExecutionMs
- return out, merged, known
+ return merged, total, known
}
diff --git a/internal/domain/run_timing_split_test.go b/internal/domain/run_timing_split_test.go
new file mode 100644
index 00000000..f41a472e
--- /dev/null
+++ b/internal/domain/run_timing_split_test.go
@@ -0,0 +1,72 @@
+package domain
+
+import (
+ "testing"
+ "time"
+)
+
+func TestRunTimingEndFallbacks(t *testing.T) {
+ base := time.Date(2026, 11, 13, 9, 0, 0, 0, time.UTC)
+ now := base.Add(time.Hour)
+ terminal, last, updated := base.Add(10*time.Minute), base.Add(20*time.Minute), base.Add(30*time.Minute)
+ cases := []struct {
+ name string
+ status string
+ terminalAt, lastEvent time.Time
+ updatedAt time.Time
+ want time.Time
+ }{
+ {"open run uses now", "running", terminal, last, updated, now},
+ {"terminal event wins", "succeeded", terminal, last, updated, terminal},
+ {"falls back to last event", "failed", time.Time{}, last, updated, last},
+ {"falls back to updatedAt", "cancelled", time.Time{}, time.Time{}, updated, updated},
+ {"falls back to now", "succeeded", time.Time{}, time.Time{}, time.Time{}, now},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ run := Run{Status: tc.status, UpdatedAt: tc.updatedAt}
+ if got := runTimingEnd(run, tc.terminalAt, tc.lastEvent, now); !got.Equal(tc.want) {
+ t.Fatalf("runTimingEnd = %v, want %v", got, tc.want)
+ }
+ })
+ }
+}
+
+func TestMergeExecutionIntervalsClipsAndUnions(t *testing.T) {
+ base := time.Date(2026, 11, 13, 9, 0, 0, 0, time.UTC)
+ at := func(m int) time.Time { return base.Add(time.Duration(m) * time.Minute) }
+ intervals := []timingInterval{
+ {at(20), at(30)},
+ {at(-5), at(5)}, // clipped to createdAt
+ {at(25), at(40)}, // overlaps the first one
+ {at(50), at(70)}, // clipped to end
+ {at(80), at(90)}, // entirely after end: dropped
+ }
+ merged, total, known := mergeExecutionIntervals(intervals, at(0), at(60))
+ if !known {
+ t.Fatal("known = false, want true")
+ }
+ want := []RunExecutionInterval{{at(0), at(5)}, {at(20), at(40)}, {at(50), at(60)}}
+ if len(merged) != len(want) {
+ t.Fatalf("merged = %v, want %v", merged, want)
+ }
+ for i := range want {
+ if !merged[i].StartedAt.Equal(want[i].StartedAt) || !merged[i].FinishedAt.Equal(want[i].FinishedAt) {
+ t.Fatalf("merged[%d] = %v, want %v", i, merged[i], want[i])
+ }
+ }
+ if total != 35*time.Minute {
+ t.Fatalf("total = %v, want 35m", total)
+ }
+}
+
+func TestMergeExecutionIntervalsKnownZero(t *testing.T) {
+ base := time.Date(2026, 11, 13, 9, 0, 0, 0, time.UTC)
+ merged, total, known := mergeExecutionIntervals([]timingInterval{{base, base}}, time.Time{}, base.Add(time.Minute))
+ if !known || total != 0 || len(merged) != 0 {
+ t.Fatalf("got merged=%v total=%v known=%v, want empty, 0, true", merged, total, known)
+ }
+ if _, _, known := mergeExecutionIntervals(nil, time.Time{}, base); known {
+ t.Fatal("known = true for no intervals")
+ }
+}