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(runeval): extract elapsedMs and orderedNodeIDs from BuildFacts (#259)

fx-kurumi8bfa2e473394
diff --git a/internal/runeval/facts.go b/internal/runeval/facts.go
index f738e8fa..133d0d08 100644
--- a/internal/runeval/facts.go
+++ b/internal/runeval/facts.go
@@ -37,16 +37,7 @@ func BuildFacts(run domain.Run, catalog domain.RunEvalCatalog) domain.RunEvalFac
if facts.ArtifactCount == 0 {
facts.ArtifactCount = run.ArtifactCount
}
- if !run.CreatedAt.IsZero() {
- end := run.UpdatedAt
- if end.IsZero() {
- end = time.Now().UTC()
- }
- facts.WallDurationMs = end.Sub(run.CreatedAt).Milliseconds()
- if facts.WallDurationMs < 0 {
- facts.WallDurationMs = 0
- }
- }
+ facts.WallDurationMs = elapsedMs(run.CreatedAt, run.UpdatedAt)
nodeMeta := map[string]domain.WorkflowNode{}
for _, node := range run.WorkflowSnapshot.Nodes {
nodeMeta[node.ID] = node
@@ -110,18 +101,7 @@ func BuildFacts(run domain.Run, catalog domain.RunEvalCatalog) domain.RunEvalFac
bumpError(errorCounts, ev.NodeID, ev.Message)
}
}
- ids := make([]string, 0, len(run.Nodes)+len(nodeMeta))
- seen := map[string]bool{}
- for _, node := range run.WorkflowSnapshot.Nodes {
- ids = append(ids, node.ID)
- seen[node.ID] = true
- }
- for id := range run.Nodes {
- if !seen[id] {
- ids = append(ids, id)
- }
- }
- for _, id := range ids {
+ for _, id := range orderedNodeIDs(run) {
nr := run.Nodes[id]
meta := nodeMeta[id]
fact := domain.RunEvalNodeFact{
@@ -142,17 +122,8 @@ func BuildFacts(run domain.Run, catalog domain.RunEvalCatalog) domain.RunEvalFac
fact.ToolCalls = nr.ToolCalls
fact.Usage = nr.Usage
fact.Error = clip(nr.Error, maxErrorText)
- if !nr.StartedAt.IsZero() {
- end := nr.FinishedAt
- if end.IsZero() {
- end = time.Now().UTC()
- }
- fact.DurationMs = end.Sub(nr.StartedAt).Milliseconds()
- if fact.DurationMs < 0 {
- fact.DurationMs = 0
- }
- facts.ExecDurationMs += fact.DurationMs
- }
+ fact.DurationMs = elapsedMs(nr.StartedAt, nr.FinishedAt)
+ facts.ExecDurationMs += fact.DurationMs
}
facts.Nodes = append(facts.Nodes, fact)
}
@@ -162,6 +133,40 @@ func BuildFacts(run domain.Run, catalog domain.RunEvalCatalog) domain.RunEvalFac
return facts
}
+// elapsedMs returns the non-negative milliseconds between start and end.
+// A zero start yields 0; a zero end means the span is still open and is
+// measured up to now.
+func elapsedMs(start, end time.Time) int64 {
+ if start.IsZero() {
+ return 0
+ }
+ if end.IsZero() {
+ end = time.Now().UTC()
+ }
+ ms := end.Sub(start).Milliseconds()
+ if ms < 0 {
+ return 0
+ }
+ return ms
+}
+
+// orderedNodeIDs lists the workflow snapshot nodes in declaration order,
+// followed by any node that only exists in the run's runtime state.
+func orderedNodeIDs(run domain.Run) []string {
+ ids := make([]string, 0, len(run.Nodes)+len(run.WorkflowSnapshot.Nodes))
+ seen := map[string]bool{}
+ for _, node := range run.WorkflowSnapshot.Nodes {
+ ids = append(ids, node.ID)
+ seen[node.ID] = true
+ }
+ for id := range run.Nodes {
+ if !seen[id] {
+ ids = append(ids, id)
+ }
+ }
+ return ids
+}
+
func keepDigest(ev domain.TelemetryEvent) bool {
switch ev.Category {
case "lifecycle", "tool", "verification", "error", "usage", "delivery", "sandbox":
diff --git a/internal/runeval/facts_helpers_test.go b/internal/runeval/facts_helpers_test.go
new file mode 100644
index 00000000..9598c9df
--- /dev/null
+++ b/internal/runeval/facts_helpers_test.go
@@ -0,0 +1,75 @@
+package runeval
+
+import (
+ "testing"
+ "time"
+
+ "agentflow/internal/domain"
+)
+
+func TestElapsedMs(t *testing.T) {
+ start := time.Date(2026, 9, 16, 1, 0, 0, 0, time.UTC)
+ if got := elapsedMs(time.Time{}, start); got != 0 {
+ t.Fatalf("zero start: got %d, want 0", got)
+ }
+ if got := elapsedMs(start, start.Add(1500*time.Millisecond)); got != 1500 {
+ t.Fatalf("closed span: got %d, want 1500", got)
+ }
+ if got := elapsedMs(start, start.Add(-time.Minute)); got != 0 {
+ t.Fatalf("negative span: got %d, want 0", got)
+ }
+ open := time.Now().UTC().Add(-2 * time.Second)
+ if got := elapsedMs(open, time.Time{}); got < 2000 {
+ t.Fatalf("open span: got %d, want >= 2000", got)
+ }
+ if got := elapsedMs(time.Now().UTC().Add(time.Hour), time.Time{}); got != 0 {
+ t.Fatalf("future start with open end: got %d, want 0", got)
+ }
+}
+
+func TestOrderedNodeIDsKeepsSnapshotOrderThenRuntimeExtras(t *testing.T) {
+ run := domain.Run{
+ WorkflowSnapshot: domain.WorkflowDefinition{Nodes: []domain.WorkflowNode{{ID: "b"}, {ID: "a"}}},
+ Nodes: map[string]*domain.NodeRun{
+ "a": {NodeID: "a"},
+ "extra": {NodeID: "extra"},
+ },
+ }
+ got := orderedNodeIDs(run)
+ want := []string{"b", "a", "extra"}
+ if len(got) != len(want) {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+ for i := range want {
+ if got[i] != want[i] {
+ t.Fatalf("got %v, want %v", got, want)
+ }
+ }
+ if ids := orderedNodeIDs(domain.Run{}); len(ids) != 0 {
+ t.Fatalf("empty run: got %v", ids)
+ }
+}
+
+func TestBuildFactsDurationsUseElapsedMs(t *testing.T) {
+ start := time.Date(2026, 9, 16, 1, 0, 0, 0, time.UTC)
+ run := domain.Run{
+ CreatedAt: start,
+ UpdatedAt: start.Add(-time.Second),
+ WorkflowSnapshot: domain.WorkflowDefinition{Nodes: []domain.WorkflowNode{{ID: "a"}, {ID: "b"}, {ID: "c"}}},
+ Nodes: map[string]*domain.NodeRun{
+ "a": {StartedAt: start, FinishedAt: start.Add(3 * time.Second)},
+ "b": {StartedAt: start, FinishedAt: start.Add(2 * time.Second)},
+ "c": {},
+ },
+ }
+ facts := BuildFacts(run, domain.RunEvalCatalog{})
+ if facts.WallDurationMs != 0 {
+ t.Fatalf("wall duration should clamp to 0, got %d", facts.WallDurationMs)
+ }
+ if facts.ExecDurationMs != 5000 {
+ t.Fatalf("exec duration: got %d, want 5000", facts.ExecDurationMs)
+ }
+ if len(facts.Nodes) != 3 || facts.Nodes[0].DurationMs != 3000 || facts.Nodes[2].DurationMs != 0 {
+ t.Fatalf("unexpected node facts: %+v", facts.Nodes)
+ }
+}