feat(taskstate): count completed runs in the per-workflow stats

The transition table moved the no-change outcomes to StatusCompleted, so a
run that opens no pull request now ends in a status the per-workflow stats
did not count at all: it was not merged, not in review, not parked, and the
workflows page would have shown it as a run that went nowhere. The count
travels the whole State Store path, since every dashboard consumer reads it
through the gRPC client rather than the local store.

- WorkflowStats filters `completed` beside merged and pr_open; the SQL is
  the count, so a wrong filter reads as zero rather than failing, and
  TestWorkflowStatsCountsCompletedRuns holds it against a real store.
- storecontract.WorkflowStat gains Completed (json "completed"), the proto
  message gains field 9, and workflowStatProto/workflowStatValue carry it;
  TestWorkflowStatProtoCarriesEveryField round-trips every field by name.
- The gateway's chat task report names the status.
- The dashboard counts a completed run as delivered in GatePulseCard's
  totals and badge numerator, and the WorkflowStat/WorkflowStats/WorkflowRow
  interfaces carry the field.
- TestOutcomeSitesAreTransitionsFromRunning is the guard that keeps the
  producer and the table agreeing: it re-reads this package's tc.Outcome
  assignment sites from source and requires each status to be one
  internal/taskstate allows from running, so a new or moved site has to be
  adjudicated against the table instead of discovered on a live run.
This commit is contained in:
sam committed 2026-09-26 04:01:08 +10:00
1 parent ded863e0dc
commit 36b25f78cf
14 files changed
+279 -10

No files matched your search

@@ -108,6 +108,7 @@ func (s *Store) WorkflowStats(ctx context.Context) ([]storecontract.WorkflowStat
Workflow: r.Workflow,
Runs: int(r.Runs),
Merged: int(r.Merged),
Completed: int(r.Completed),
PROpen: int(r.PrOpen),
Parked: int(r.Parked),
AvgTokens: int(r.AvgTokens),
@@ -295,6 +295,7 @@ const workflowStats = `-- name: WorkflowStats :many
SELECT workflow,
COUNT(*)::int AS runs,
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
COUNT(*) FILTER (WHERE status = 'completed')::int AS completed,
COUNT(*) FILTER (WHERE status = 'pr_open')::int AS pr_open,
COUNT(*) FILTER (WHERE status = 'parked')::int AS parked,
CAST(AVG(tokens_used) AS bigint) AS avg_tokens,
@@ -310,6 +311,7 @@ type WorkflowStatsRow struct {
Workflow string
Runs int32
Merged int32
Completed int32
PrOpen int32
Parked int32
AvgTokens int64
@@ -330,6 +332,7 @@ func (q *Queries) WorkflowStats(ctx context.Context) ([]WorkflowStatsRow, error)
&i.Workflow,
&i.Runs,
&i.Merged,
&i.Completed,
&i.PrOpen,
&i.Parked,
&i.AvgTokens,
@@ -39,6 +39,7 @@ ORDER BY id;
SELECT workflow,
COUNT(*)::int AS runs,
COUNT(*) FILTER (WHERE status = 'merged')::int AS merged,
COUNT(*) FILTER (WHERE status = 'completed')::int AS completed,
COUNT(*) FILTER (WHERE status = 'pr_open')::int AS pr_open,
COUNT(*) FILTER (WHERE status = 'parked')::int AS parked,
CAST(AVG(tokens_used) AS bigint) AS avg_tokens,
@@ -0,0 +1,48 @@
package postgres
import (
"testing"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// TestWorkflowStatsCountsCompletedRuns pins the per-workflow count the
// transition table made reachable: a run that finishes with no pull request
// (a no-change build, a triage that needed no code) ends in taskstate.Completed,
// so the workflows page has to be able to see those runs at all. The count is a
// status filter in SQL, which fails silently when it is wrong -- a typo'd or
// missing filter reads as zero, not as an error -- so it is asserted against the
// store rather than left to the dashboard to notice.
func TestWorkflowStatsCountsCompletedRuns(t *testing.T) {
pool, q := migrated(t)
store := New(pool)
finished := insertChatTask(t, q)
merged := insertChatTask(t, q)
finish := func(id int64, to string) {
t.Helper()
if err := store.Transition(t.Context(), id, taskstate.Queued, taskstate.Running, ""); err != nil {
t.Fatalf("queued -> running: %v", err)
}
if err := store.Transition(t.Context(), id, taskstate.Running, to, ""); err != nil {
t.Fatalf("running -> %s: %v", to, err)
}
}
finish(finished.ID, taskstate.Completed)
finish(merged.ID, taskstate.Merged)
stats, err := store.WorkflowStats(t.Context())
if err != nil {
t.Fatalf("WorkflowStats: %v", err)
}
if len(stats) != 1 {
t.Fatalf("WorkflowStats = %+v, want one row for %q", stats, finished.Workflow)
}
got := stats[0]
if got.Workflow != merged.Workflow {
t.Fatalf("WorkflowStats row is %q, want %q", got.Workflow, merged.Workflow)
}
if got.Runs != 2 || got.Completed != 1 || got.Merged != 1 {
t.Errorf("WorkflowStats = %+v, want runs 2, completed 1, merged 1: the completed count has to be its own status, not a share of merged", got)
}
}
+4 -4
View File
@@ -282,8 +282,8 @@ func bindingInputsValue(s string) map[string]binding.InputSource {
func workflowStatProto(w storecontract.WorkflowStat) *pb.WorkflowStat {
return &pb.WorkflowStat{
Workflow: w.Workflow, Runs: int64(w.Runs), Merged: int64(w.Merged),
PrOpen: int64(w.PROpen), Parked: int64(w.Parked), AvgTokens: int64(w.AvgTokens),
AvgSteps: w.AvgSteps, TotalTokens: int64(w.TotalToken),
Completed: int64(w.Completed), PrOpen: int64(w.PROpen), Parked: int64(w.Parked),
AvgTokens: int64(w.AvgTokens), AvgSteps: w.AvgSteps, TotalTokens: int64(w.TotalToken),
}
}
@@ -293,8 +293,8 @@ func workflowStatValue(w *pb.WorkflowStat) storecontract.WorkflowStat {
}
return storecontract.WorkflowStat{
Workflow: w.Workflow, Runs: int(w.Runs), Merged: int(w.Merged),
PROpen: int(w.PrOpen), Parked: int(w.Parked), AvgTokens: int(w.AvgTokens),
AvgSteps: w.AvgSteps, TotalToken: int(w.TotalTokens),
Completed: int(w.Completed), PROpen: int(w.PrOpen), Parked: int(w.Parked),
AvgTokens: int(w.AvgTokens), AvgSteps: w.AvgSteps, TotalToken: int(w.TotalTokens),
}
}
@@ -5,6 +5,7 @@ import (
"testing"
"time"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
@@ -49,3 +50,38 @@ func TestTaskProtoCarriesEveryDomainField(t *testing.T) {
}
}
}
// TestWorkflowStatProtoCarriesEveryField is the same contract for the
// per-workflow metric row, and the reason it exists is the same: the dashboard
// reads these counts off the wire, so a count that only the producer knows
// (Completed, the runs that finished with no pull request to open) would read
// as zero everywhere without anything failing. Each field gets a value of its
// own, so a mapping that assigns two of them from the same source -- or drops
// one -- is caught by name.
func TestWorkflowStatProtoCarriesEveryField(t *testing.T) {
want := storecontract.WorkflowStat{}
v := reflect.ValueOf(&want).Elem()
for i := range v.NumField() {
field := v.Field(i)
switch {
case field.Kind() == reflect.String:
field.SetString(v.Type().Field(i).Name + "-value")
case field.Kind() == reflect.Float64:
field.SetFloat(float64(i) + 0.5)
case field.CanInt():
field.SetInt(int64(i) + 1)
default:
t.Fatalf("field %s has kind %s, which this test does not know how to fill; extend it alongside the field", v.Type().Field(i).Name, field.Kind())
}
}
got := reflect.ValueOf(workflowStatValue(workflowStatProto(want)))
for i := range v.NumField() {
name := v.Type().Field(i).Name
gotField := got.Field(i).Interface()
wantField := v.Field(i).Interface()
if !reflect.DeepEqual(gotField, wantField) {
t.Errorf("WorkflowStat.%s did not survive the proto round trip: got %v, want %v", name, gotField, wantField)
}
}
}