gunk-dev/the-valley

dashboard_pipeline.go

source28abc5737165main's published tip history
5.7 KiB
1package cmd
2
3import (
4 "context"
5 "log/slog"
6 "sort"
7 "time"
8
9 "the-valley/flow/internal/git"
10 "the-valley/flow/internal/github"
11 "the-valley/flow/internal/integration"
12 "the-valley/flow/internal/project"
13 "the-valley/flow/internal/run"
14)
15
16// repoGroup is a set of runs and PRs belonging to the same repository.
17type repoGroup struct {
18 Repo string
19 Runs []*run.State
20 PRMap map[string]*prStatus
21}
22
23// markRunFailed sets sentinel finalization values on a stale run and
24// cleans up its worktree and branch. Errors during cleanup are logged
25// but do not prevent the state from being saved.
26func markRunFailed(store run.StateStore, s *run.State) {
27 cost := float64(-1)
28 dur := int64(0)
29 s.CostUSD = &cost
30 s.DurationMS = &dur
31 s.TmuxPane = nil
32 cleanupWorktree(context.Background(), store, git.NewExecClient(), s, false)
33 if err := store.Save(s); err != nil {
34 slog.Warn("failed to save stale run state", "id", s.ID, "err", err)
35 }
36}
37
38// isAgentRunning checks if a run's agent is currently active in tmux.
39func (m *dashboardModel) isAgentRunning(s *run.State) bool {
40 return s.IsAgentRunningWith(m.tmuxDeps)
41}
42
43// agentRunning is isAgentRunning read from the snapshot taken when states,
44// GitHub status or the tick last arrived, so rendering and scrolling don't
45// query tmux for every run on every keypress. Runs missing from the snapshot
46// are checked live.
47func (m *dashboardModel) agentRunning(s *run.State) bool {
48 if running, ok := m.runningSnap[s.ID]; ok {
49 return running
50 }
51 return m.isAgentRunning(s)
52}
53
54// snapshotRunning records which agents are running right now.
55func (m *dashboardModel) snapshotRunning() {
56 snap := make(map[string]bool, len(m.states))
57 for _, s := range m.states {
58 if s.Type != "session" {
59 snap[s.ID] = m.isAgentRunning(s)
60 }
61 }
62 m.runningSnap = snap
63}
64
65// agentStatusLabel returns a display label for a non-running agent. A pushed
66// branch reads SUBMIT, or READY when a valley knows it as a change, until
67// an integration check (v) says it landed (INTEGRATED, or INTEGRATED~ for a
68// content match) or was deleted (GONE), or the valley's record says where
69// the change stands (APPROVED, UNCERTAIN, STALE, REJECTED, FAILED,
70// WITHDRAWN, SUPERSEDED, DRAFT). A row reads REVIEW for a draft awaiting
71// review instead (dashRow.statusLabel).
72func agentStatusLabel(s *run.State, v integration.Verdict, valley bool) string {
73 if s.NeedsAttention != nil {
74 return "ATTN"
75 }
76 if s.PRURL != nil {
77 return "PR"
78 }
79 if s.PushedSHA != nil {
80 return pushedLabel(v, valley)
81 }
82 return "EXITED"
83}
84
85// pushedLabel is the label of pushed work whose check says v; valley says a
86// valley knows it as a change.
87func pushedLabel(v integration.Verdict, valley bool) string {
88 switch v {
89 case integration.Integrated:
90 return "INTEGRATED"
91 case integration.IntegratedContent:
92 return "INTEGRATED~"
93 case integration.Gone:
94 return "GONE"
95 case integration.Ready:
96 return "READY"
97 case integration.Approved:
98 return "APPROVED"
99 case integration.Uncertain:
100 return "UNCERTAIN"
101 case integration.Stale:
102 return "STALE"
103 case integration.ChecksFailed:
104 return "CHECKS FAILED"
105 case integration.Conflicting:
106 return "CONFLICTING"
107 case integration.Rejected:
108 return "REJECTED"
109 case integration.Failed:
110 return "FAILED"
111 case integration.Withdrawn:
112 return "WITHDRAWN"
113 case integration.Superseded:
114 return "OLDER VERSION"
115 case integration.Draft:
116 return "DRAFT"
117 }
118 if valley {
119 return "READY"
120 }
121 return "SUBMIT"
122}
123
124// groupByRepo organizes states into repo groups, sorted by repo name.
125func groupByRepo(states []*run.State) []repoGroup {
126 reg, _ := project.Load()
127 groups := make(map[string]*repoGroup)
128 for _, s := range states {
129 if s.Type == "session" {
130 continue
131 }
132 repo := repoFromState(s, reg)
133 g, ok := groups[repo]
134 if !ok {
135 g = &repoGroup{
136 Repo: repo,
137 PRMap: make(map[string]*prStatus),
138 }
139 groups[repo] = g
140 }
141 g.Runs = append(g.Runs, s)
142 }
143
144 var result []repoGroup
145 for _, g := range groups {
146 result = append(result, *g)
147 }
148 sort.Slice(result, func(i, j int) bool {
149 return result[i].Repo < result[j].Repo
150 })
151 return result
152}
153
154// repoFromState extracts the repo identifier from a run state, normalizing
155// against the project registry so that different forms of the same repo
156// (e.g. "site" vs "maintainer/site") group together.
157func repoFromState(s *run.State, reg *project.Registry) string {
158 if s.TargetRepo != nil && *s.TargetRepo != "" {
159 return project.NormalizeRepoName(*s.TargetRepo, reg)
160 }
161 if s.PRURL != nil {
162 ownerRepo := repoFromPRURL(*s.PRURL)
163 if ownerRepo != "(unknown)" {
164 return project.NormalizeRepoName(ownerRepo, reg)
165 }
166 return ownerRepo
167 }
168 return "(local)"
169}
170
171// repoFromPRURL extracts "owner/repo" from a GitHub PR URL, returning the
172// "(unknown)" sentinel when the URL can't be parsed. The parsing itself lives
173// in github.OwnerRepoFromPRURL so there's a single source of truth.
174func repoFromPRURL(prURL string) string {
175 if slug := github.OwnerRepoFromPRURL(prURL); slug != "" {
176 return slug
177 }
178 return "(unknown)"
179}
180
181// computeTotalCost sums the cost across all runs. For runs without final cost,
182// it includes the budget as an upper bound.
183func computeTotalCost(states []*run.State) float64 {
184 var total float64
185 for _, s := range states {
186 if s.CostUSD != nil {
187 total += *s.CostUSD
188 }
189 }
190 return total
191}
192
193// computeSessionDuration returns the duration from the oldest active run's creation time to now.
194func computeSessionDuration(states []*run.State) time.Duration {
195 if len(states) == 0 {
196 return 0
197 }
198 var oldest time.Time
199 for _, s := range states {
200 t, err := time.Parse(time.RFC3339, s.CreatedAt)
201 if err != nil {
202 continue
203 }
204 if oldest.IsZero() || t.Before(oldest) {
205 oldest = t
206 }
207 }
208 if oldest.IsZero() {
209 return 0
210 }
211 return time.Since(oldest)
212}