on main help sign in
fix(events): prevent early events from being dropped before init (#2611)
commitd9f857fb29317f25d2af8f7104802d88b3417a5b authorChristian Rocha <christian@rocha.is> dateMon 13 Apr 2026, 11:04 (6 months ago) parent047cce5
3 files changed+30−16
internal/cmd/run.go+4−1 internal/cmd/session.go+17−9 internal/cmd/stats.go+9−6
internal/cmd/run.go+4−1
@@ -86,7 +86,6 @@ crush run --continue "Follow up on your last response"
8686 }
8787
8888 event.SetNonInteractive(true)
89− event.AppInitialized()
9089
9190 switch {
9291 case sessionID != "":
@@ -102,6 +101,8 @@ crush run --continue "Follow up on your last response"
102101 }
103102 defer cleanup()
104103
104+ event.AppInitialized()
105+
105106 if sessionID != "" {
106107 sess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)
107108 if err != nil {
@@ -127,6 +128,8 @@ crush run --continue "Follow up on your last response"
127128 }
128129 defer cleanup()
129130
131+ event.AppInitialized()
132+
130133 if !ws.Config().IsConfigured() {
131134 return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
132135 }
internal/cmd/session.go+17−9
@@ -107,13 +107,16 @@ func sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func()
107107 dataDir, _ := cmd.Flags().GetString("data-dir")
108108 ctx := cmd.Context()
109109
110+ cfg, err := config.Init("", dataDir, false)
111+ if err != nil {
112+ return nil, nil, nil, fmt.Errorf("failed to initialize config: %w", err)
113+ }
110114 if dataDir == "" {
111− cfg, err := config.Init("", "", false)
112− if err != nil {
113− return nil, nil, nil, fmt.Errorf("failed to initialize config: %w", err)
114− }
115115 dataDir = cfg.Config().Options.DataDirectory
116116 }
117+ if shouldEnableMetrics(cfg.Config()) {
118+ event.Init()
119+ }
117120
118121 conn, err := db.Connect(ctx, dataDir)
119122 if err != nil {
@@ -130,7 +133,6 @@ func sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func()
130133
131134func runSessionList(cmd *cobra.Command, _ []string) error {
132135 event.SetNonInteractive(true)
133− event.SessionListed(sessionListJSON)
134136
135137 ctx, svc, cleanup, err := sessionSetup(cmd)
136138 if err != nil {
@@ -138,6 +140,8 @@ func runSessionList(cmd *cobra.Command, _ []string) error {
138140 }
139141 defer cleanup()
140142
143+ event.SessionListed(sessionListJSON)
144+
141145 list, err := svc.sessions.List(ctx)
142146 if err != nil {
143147 return fmt.Errorf("failed to list sessions: %w", err)
@@ -253,7 +257,6 @@ func resolveSessionID(ctx context.Context, svc session.Service, id string) (sess
253257
254258func runSessionShow(cmd *cobra.Command, args []string) error {
255259 event.SetNonInteractive(true)
256− event.SessionShown(sessionShowJSON)
257260
258261 ctx, svc, cleanup, err := sessionSetup(cmd)
259262 if err != nil {
@@ -261,6 +264,8 @@ func runSessionShow(cmd *cobra.Command, args []string) error {
261264 }
262265 defer cleanup()
263266
267+ event.SessionShown(sessionShowJSON)
268+
264269 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
265270 if err != nil {
266271 return err
@@ -280,7 +285,6 @@ func runSessionShow(cmd *cobra.Command, args []string) error {
280285
281286func runSessionDelete(cmd *cobra.Command, args []string) error {
282287 event.SetNonInteractive(true)
283− event.SessionDeletedCommand(sessionDeleteJSON)
284288
285289 ctx, svc, cleanup, err := sessionSetup(cmd)
286290 if err != nil {
@@ -288,6 +292,8 @@ func runSessionDelete(cmd *cobra.Command, args []string) error {
288292 }
289293 defer cleanup()
290294
295+ event.SessionDeletedCommand(sessionDeleteJSON)
296+
291297 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
292298 if err != nil {
293299 return err
@@ -315,7 +321,6 @@ func runSessionDelete(cmd *cobra.Command, args []string) error {
315321
316322func runSessionRename(cmd *cobra.Command, args []string) error {
317323 event.SetNonInteractive(true)
318− event.SessionRenamed(sessionRenameJSON)
319324
320325 ctx, svc, cleanup, err := sessionSetup(cmd)
321326 if err != nil {
@@ -323,6 +328,8 @@ func runSessionRename(cmd *cobra.Command, args []string) error {
323328 }
324329 defer cleanup()
325330
331+ event.SessionRenamed(sessionRenameJSON)
332+
326333 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
327334 if err != nil {
328335 return err
@@ -351,7 +358,6 @@ func runSessionRename(cmd *cobra.Command, args []string) error {
351358
352359func runSessionLast(cmd *cobra.Command, _ []string) error {
353360 event.SetNonInteractive(true)
354− event.SessionLastShown(sessionLastJSON)
355361
356362 ctx, svc, cleanup, err := sessionSetup(cmd)
357363 if err != nil {
@@ -359,6 +365,8 @@ func runSessionLast(cmd *cobra.Command, _ []string) error {
359365 }
360366 defer cleanup()
361367
368+ event.SessionLastShown(sessionLastJSON)
369+
362370 list, err := svc.sessions.List(ctx)
363371 if err != nil {
364372 return fmt.Errorf("failed to list sessions: %w", err)
internal/cmd/stats.go+9−6
@@ -121,18 +121,21 @@ type HourDayHeatmapPt struct {
121121}
122122
123123func runStats(cmd *cobra.Command, _ []string) error {
124− event.StatsViewed()
125−
126124 dataDir, _ := cmd.Flags().GetString("data-dir")
127125 ctx := cmd.Context()
128126
127+ cfg, err := config.Init("", dataDir, false)
128+ if err != nil {
129+ return fmt.Errorf("failed to initialize config: %w", err)
130+ }
129131 if dataDir == "" {
130− cfg, err := config.Init("", "", false)
131− if err != nil {
132− return fmt.Errorf("failed to initialize config: %w", err)
133− }
134132 dataDir = cfg.Config().Options.DataDirectory
135133 }
134+ if shouldEnableMetrics(cfg.Config()) {
135+ event.Init()
136+ }
137+
138+ event.StatsViewed()
136139
137140 conn, err := db.Connect(ctx, dataDir)
138141 if err != nil {