| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "os" |
| 8 | |
| 9 | "the-valley/flow/internal/run" |
| 10 | "the-valley/flow/internal/stream" |
| 11 | "the-valley/flow/internal/tmux" |
| 12 | "github.com/spf13/cobra" |
| 13 | ) |
| 14 | |
| 15 | var logsCmd = &cobra.Command{ |
| 16 | Use: "logs <run-id>", |
| 17 | Short: "View agent logs", |
| 18 | Long: `Shows agent output. By default shows the live tmux pane if running, |
| 19 | or replays from saved log if finished. |
| 20 | |
| 21 | Modes: |
| 22 | --live Show live pane or replay from log (default) |
| 23 | --replay Re-format the saved JSONL log |
| 24 | --raw Dump the raw JSONL log file`, |
| 25 | Args: cobra.ExactArgs(1), |
| 26 | RunE: func(cmd *cobra.Command, args []string) error { |
| 27 | id := args[0] |
| 28 | raw, _ := cmd.Flags().GetBool("raw") |
| 29 | replay, _ := cmd.Flags().GetBool("replay") |
| 30 | ctx := cmd.Context() |
| 31 | tmuxClient := tmux.NewExecClient() |
| 32 | |
| 33 | store, err := sessionStore() |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | state, err := store.Load(id) |
| 38 | if err != nil { |
| 39 | return fmt.Errorf("no run found with id: %s", id) |
| 40 | } |
| 41 | |
| 42 | if raw { |
| 43 | return showRawLog(state) |
| 44 | } |
| 45 | if replay { |
| 46 | return replayLog(state) |
| 47 | } |
| 48 | return showLive(ctx, state, tmuxClient) |
| 49 | }, |
| 50 | } |
| 51 | |
| 52 | func showRawLog(s *run.State) error { |
| 53 | if s.LogFile == nil { |
| 54 | return fmt.Errorf("no log file for run %s", s.ID) |
| 55 | } |
| 56 | f, err := os.Open(*s.LogFile) |
| 57 | if err != nil { |
| 58 | return fmt.Errorf("opening log: %w", err) |
| 59 | } |
| 60 | defer f.Close() |
| 61 | _, err = io.Copy(os.Stdout, f) |
| 62 | return err |
| 63 | } |
| 64 | |
| 65 | func replayLog(s *run.State) error { |
| 66 | if s.LogFile == nil { |
| 67 | return fmt.Errorf("no log file for run %s", s.ID) |
| 68 | } |
| 69 | f, err := os.Open(*s.LogFile) |
| 70 | if err != nil { |
| 71 | return fmt.Errorf("opening log: %w", err) |
| 72 | } |
| 73 | defer f.Close() |
| 74 | return stream.FormatStream(f, os.Stdout) |
| 75 | } |
| 76 | |
| 77 | func showLive(ctx context.Context, s *run.State, tc tmux.Client) error { |
| 78 | // Try live tmux pane first |
| 79 | if s.TmuxPane != nil && tc.PaneExists(ctx, *s.TmuxPane) { |
| 80 | output, err := tc.CapturePane(ctx, *s.TmuxPane, 500) |
| 81 | if err != nil { |
| 82 | return fmt.Errorf("capturing pane: %w", err) |
| 83 | } |
| 84 | fmt.Print(output) |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // Fall back to replay |
| 89 | if s.LogFile != nil { |
| 90 | return replayLog(s) |
| 91 | } |
| 92 | |
| 93 | fmt.Printf("No live pane or log file available for run %s.\n", s.ID) |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | func init() { |
| 98 | logsCmd.Flags().Bool("raw", false, "Dump raw JSONL log") |
| 99 | logsCmd.Flags().Bool("replay", false, "Re-format saved log through formatter") |
| 100 | rootCmd.AddCommand(logsCmd) |
| 101 | } |