gunk-dev/the-valley

report.go

source88a5a486b52fmain's published tip history
4.7 KiB
1package cmd
2
3import (
4 "encoding/json"
5 "fmt"
6 "os"
7 "path/filepath"
8 "time"
9
10 "the-valley/flow/internal/event"
11 "the-valley/flow/internal/report"
12 "the-valley/flow/internal/run"
13 "github.com/spf13/cobra"
14)
15
16var reportCmd = &cobra.Command{
17 Use: "report",
18 Short: "Report on klaus's runs from the session event logs",
19}
20
21var reportTimeCmd = &cobra.Command{
22 Use: "time [--since <duration>] [--repo <r>] [--current] [--json]",
23 Short: "Report where agent wall-clock time goes",
24 Long: `Reads the session event logs (events.jsonl in every session under
25~/.klaus/sessions, or only the current session's with --current) and reports,
26per repository and overall:
27
28 RUNS runs that started in the window, by dispatch reason
29 SETUP launch's work before the agent starts (fetch, worktree, pane)
30 AGENT the agents' own work, from pane start to finalize
31 FINALIZE finalize's work (salvage, push checks, data ref, cleanup)
32 CHECKS _pre-review and the build, test and lint commands agents ran,
33 by kind; part of AGENT. A command line that also ran
34 _pre-review counts without the pre-review's time
35 WAIT time a change had no run going, from the last end among its
36 earlier runs to the next one's start: on review, CI or the
37 operator; unknown after a run with no end
38 APPROVAL time from a PR's last run to its first approval
39 FOLLOW-UPS runs that continued a change (CI, review and conflict fixes,
40 --pr, --change, budget resumes), per change
41 CONSULT klaus consult time; REVIEW, klaus review time
42 COST agent cost where the backend reports it (Claude)
43
44Runs that share a change ID in a repo, or a PR, are one change, so a PR's
45runs from before change IDs join its later ones, and a Change-Id copied into
46another repo is another change. A run's repo is its repo_key: owner/repo,
47valley:<name>, or the repo's short name; every name a run's events give its
48repo counts, so a renamed repo's runs stay one change. Events from before
49klaus recorded phases count a run's agent time from agent:started to its last
50event, with no setup or finalize time, and a run with no recorded dispatch
51reason counts as "unknown".
52
53--since takes a Go duration or whole days or weeks (7d, 2w, 36h); 0 means all
54time. --repo keeps one repo: a repo key, a valley's name for it, or its last
55path component. --json prints the report as JSON, durations in milliseconds.`,
56 Args: cobra.NoArgs,
57 RunE: func(cmd *cobra.Command, args []string) error {
58 sinceFlag, _ := cmd.Flags().GetString("since")
59 repo, _ := cmd.Flags().GetString("repo")
60 current, _ := cmd.Flags().GetBool("current")
61 asJSON, _ := cmd.Flags().GetBool("json")
62
63 window, err := report.ParseSince(sinceFlag)
64 if err != nil {
65 return err
66 }
67 now := time.Now()
68 opts := report.Options{Until: now, Repo: repo}
69 if window > 0 {
70 opts.Since = now.Add(-window)
71 }
72
73 dirs, err := reportSessionDirs(current)
74 if err != nil {
75 return err
76 }
77 var sessions [][]event.Event
78 for _, dir := range dirs {
79 evts, err := event.NewLog(dir).Read()
80 if err != nil {
81 fmt.Fprintf(cmd.ErrOrStderr(), "warning: reading %s: %v\n", event.NewLog(dir).Path(), err)
82 continue
83 }
84 if evts != nil {
85 sessions = append(sessions, evts)
86 }
87 }
88
89 rep := report.Build(sessions, opts)
90 out := cmd.OutOrStdout()
91 if asJSON {
92 enc := json.NewEncoder(out)
93 enc.SetIndent("", " ")
94 return enc.Encode(rep)
95 }
96 return rep.WriteText(out)
97 },
98}
99
100// reportSessionDirs returns the session directories whose event logs the
101// report reads: every session's, or the current one's.
102func reportSessionDirs(current bool) ([]string, error) {
103 if current {
104 store, err := sessionStore()
105 if err != nil {
106 return nil, err
107 }
108 hds, ok := store.(*run.HomeDirStore)
109 if !ok {
110 return nil, fmt.Errorf("the current session keeps no event log")
111 }
112 return []string{hds.BaseDir()}, nil
113 }
114 root, err := run.SessionsDir()
115 if err != nil {
116 return nil, err
117 }
118 entries, err := os.ReadDir(root)
119 if os.IsNotExist(err) {
120 return nil, nil
121 }
122 if err != nil {
123 return nil, fmt.Errorf("reading sessions dir: %w", err)
124 }
125 var dirs []string
126 for _, e := range entries {
127 if e.IsDir() {
128 dirs = append(dirs, filepath.Join(root, e.Name()))
129 }
130 }
131 return dirs, nil
132}
133
134func init() {
135 reportTimeCmd.Flags().String("since", "7d", "Only runs, consults and reviews that started within this long ago (e.g. 7d, 2w, 36h; 0 for all time)")
136 reportTimeCmd.Flags().String("repo", "", "Only this repo: owner/repo, valley:<name>, or a repo's name")
137 reportTimeCmd.Flags().Bool("current", false, "Only the current session's events (default: every session's)")
138 reportTimeCmd.Flags().Bool("json", false, "Print the report as JSON, durations in milliseconds")
139 reportCmd.AddCommand(reportTimeCmd)
140 rootCmd.AddCommand(reportCmd)
141}