-
Notifications
You must be signed in to change notification settings - Fork 327
Add hidden entire why overview #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pfleidi
wants to merge
33
commits into
main
Choose a base branch
from
feat/entire-why
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
75cf5e0
Add hidden why command shell
pfleidi 7e55643
Merge branch 'main' of github.com:entireio/cli into feat/entire-why
pfleidi 5a81d53
Validate why command path options
pfleidi 4a5bbb3
Add why blame parsing
pfleidi 3dd4318
Add why checkpoint enrichment
pfleidi 8889aa9
Add why static overview rendering
pfleidi 6b90746
Add why source highlighting
pfleidi 96b5cda
Add perf logging to why command
pfleidi 3d1356a
Add detailed why enrichment perf spans
pfleidi 395957b
Add why enrichment loop timing
pfleidi 1994299
Avoid transcript reads in why overview
pfleidi f37d738
Add interactive why overview TUI
pfleidi 9a65ae8
Compact why TUI blame gutter
pfleidi d1da345
Show checkpoint agents in why output
pfleidi 9cc60ac
Simplify why view to checkpoint IDs
pfleidi 4a4550e
Align why gutter columns
pfleidi 5790d22
Highlight selected why row
pfleidi 1e1aa57
Fix why TUI highlighted line rendering
pfleidi 7f664e1
Keep why TUI rows within viewport
pfleidi 1aa35b1
Label why TUI gutter columns
pfleidi f4acdb3
Show why selected-line metadata
pfleidi 7717fe7
Simplify why command implementation
pfleidi c2f4d92
Format why TUI header metadata
pfleidi 26506d2
Link why TUI commit hashes
pfleidi b4b6691
Merge remote-tracking branch 'origin/main' into feat/entire-why
pfleidi 9c65319
Group why enrichment perf spans
pfleidi cfcd6fa
Use compact git blame porcelain for why
pfleidi c87902c
Remove unused v1 session metadata reader
pfleidi 0702580
Skip why hyperlinks for zero commit hashes
pfleidi de14ea6
Remove unused why blame blocks
pfleidi 166bc49
Avoid eager why highlight fallback rendering
pfleidi bd8d4bd
Simplify why command internals
pfleidi fc52b87
Merge branch 'main' into feat/entire-why
pfleidi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1310,6 +1310,45 @@ func TestReadSessionContent_ByIndex(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestReadSessionMetadataAndPrompts_ReturnsWithoutTranscript(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| repo, _ := setupBranchTestRepo(t) | ||
| store := NewGitStore(repo) | ||
| checkpointID := id.MustCheckpointID("d1d2d3d4d5d7") | ||
| ctx := context.Background() | ||
|
|
||
| err := store.WriteCommitted(ctx, WriteCommittedOptions{ | ||
| CheckpointID: checkpointID, | ||
| SessionID: "session-meta-only", | ||
| Strategy: "manual-commit", | ||
| Prompts: []string{"test prompt"}, | ||
| AuthorName: "Test Author", | ||
| AuthorEmail: "[email protected]", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("WriteCommitted() error = %v", err) | ||
| } | ||
|
|
||
| if _, err := store.ReadSessionContent(ctx, checkpointID, 0); !errors.Is(err, ErrNoTranscript) { | ||
| t.Fatalf("ReadSessionContent() error = %v, want ErrNoTranscript", err) | ||
| } | ||
|
|
||
| content, err := store.ReadSessionMetadataAndPrompts(ctx, checkpointID, 0) | ||
| if err != nil { | ||
| t.Fatalf("ReadSessionMetadataAndPrompts() error = %v", err) | ||
| } | ||
| if content.Metadata.SessionID != "session-meta-only" { | ||
| t.Fatalf("session ID = %q, want session-meta-only", content.Metadata.SessionID) | ||
| } | ||
| if !strings.Contains(content.Prompts, "test prompt") { | ||
| t.Fatalf("prompts = %q, want test prompt", content.Prompts) | ||
| } | ||
| if len(content.Transcript) != 0 { | ||
| t.Fatalf("transcript length = %d, want 0", len(content.Transcript)) | ||
| } | ||
| } | ||
|
|
||
| // writeSingleSession is a test helper that creates a store with a single session | ||
| // and returns the store and checkpoint ID for further testing. | ||
| func writeSingleSession(t *testing.T, cpIDStr, sessionID, transcript string) (*GitStore, id.CheckpointID) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "log/slog" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/entireio/cli/cmd/entire/cli/interactive" | ||
| "github.com/entireio/cli/cmd/entire/cli/logging" | ||
| "github.com/entireio/cli/cmd/entire/cli/paths" | ||
| "github.com/entireio/cli/perf" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| type whyOptions struct { | ||
| Path string | ||
| } | ||
|
|
||
| func newWhyCmd() *cobra.Command { | ||
| var opts whyOptions | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "why [path]", | ||
| Short: "Explain why a file looks the way it does", | ||
| Hidden: true, | ||
| Args: cobra.MaximumNArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| if len(args) > 0 { | ||
| opts.Path = args[0] | ||
| } | ||
| if opts.Path != "" && !canRunWhyTUI(cmd.OutOrStdout()) { | ||
| cleanup := initWhyLogging(cmd.Context()) | ||
| defer cleanup() | ||
| } | ||
| return runWhy(cmd.Context(), cmd.OutOrStdout(), cmd.ErrOrStderr(), opts) | ||
| }, | ||
| } | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runWhy(ctx context.Context, w io.Writer, _ io.Writer, opts whyOptions) (err error) { | ||
| ctx, span := perf.Start(ctx, "why", | ||
| slog.String("path", opts.Path), | ||
| slog.Bool("has_path", opts.Path != "")) | ||
| defer func() { | ||
| span.RecordError(err) | ||
| span.End() | ||
| }() | ||
|
|
||
| _, modeSpan := perf.Start(ctx, "detect_mode") | ||
| canUseTUI := canRunWhyTUI(w) | ||
| modeSpan.End() | ||
| if opts.Path == "" { | ||
| if !canUseTUI { | ||
| return errors.New("path required when not running interactively") | ||
| } | ||
| return errors.New("interactive file browser is not implemented yet") | ||
| } | ||
|
|
||
| _, resolveSpan := perf.Start(ctx, "resolve_path") | ||
| repoRoot, gitPath, _, err := resolveWhyPath(ctx, opts.Path) | ||
| if err != nil { | ||
| resolveSpan.RecordError(err) | ||
| resolveSpan.End() | ||
| return err | ||
| } | ||
| resolveSpan.End() | ||
|
|
||
| data, err := loadWhyViewData(ctx, repoRoot, gitPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if canUseTUI { | ||
| _, renderSpan := perf.Start(ctx, "render_tui") | ||
| if err := runWhyTUI(ctx, w, data); err != nil { | ||
| renderSpan.RecordError(err) | ||
| renderSpan.End() | ||
| return err | ||
| } | ||
| renderSpan.End() | ||
| return nil | ||
| } | ||
|
|
||
| _, renderSpan := perf.Start(ctx, "render_static") | ||
| content := renderWhyStatic(data) | ||
| renderSpan.End() | ||
|
|
||
| _, outputSpan := perf.Start(ctx, "write_output") | ||
| outputExplainContent(w, content, false) | ||
| outputSpan.End() | ||
| return nil | ||
| } | ||
|
|
||
| var canRunWhyTUI = defaultCanRunWhyTUI | ||
|
|
||
| func defaultCanRunWhyTUI(w io.Writer) bool { | ||
| return !IsAccessibleMode() && interactive.IsTerminalWriter(w) && interactive.CanPromptInteractively() | ||
| } | ||
|
|
||
| func initWhyLogging(ctx context.Context) func() { | ||
| if _, err := paths.WorktreeRoot(ctx); err != nil { | ||
| return func() {} | ||
| } | ||
| logging.SetLogLevelGetter(GetLogLevel) | ||
| if err := logging.Init(ctx, ""); err != nil { | ||
| return func() {} | ||
| } | ||
| return logging.Close | ||
| } | ||
|
|
||
| func resolveWhyPath(ctx context.Context, input string) (string, string, string, error) { | ||
| repoRoot, err := paths.WorktreeRoot(ctx) | ||
| if err != nil { | ||
| return "", "", "", fmt.Errorf("not a git repository: %w", err) | ||
| } | ||
| repoRoot = normalizeWhyPathForRel(repoRoot) | ||
|
|
||
| absPath := input | ||
| if !filepath.IsAbs(absPath) { | ||
| absPath, err = filepath.Abs(absPath) | ||
| if err != nil { | ||
| return "", "", "", fmt.Errorf("resolving path %q: %w", input, err) | ||
| } | ||
| } | ||
| absPath = normalizeWhyPathForRel(absPath) | ||
|
|
||
| relPath, err := filepath.Rel(repoRoot, absPath) | ||
| if err != nil { | ||
| return "", "", "", fmt.Errorf("resolving path %q relative to repository: %w", input, err) | ||
| } | ||
| if relPath == "." || strings.HasPrefix(relPath, ".."+string(filepath.Separator)) || relPath == ".." { | ||
| return "", "", "", fmt.Errorf("path %q is outside the repository", input) | ||
| } | ||
|
|
||
| return repoRoot, filepath.ToSlash(relPath), absPath, nil | ||
| } | ||
|
|
||
| func normalizeWhyPathForRel(path string) string { | ||
| cleaned := filepath.Clean(path) | ||
| if resolved, err := filepath.EvalSymlinks(cleaned); err == nil { | ||
| return resolved | ||
| } | ||
| dir := filepath.Dir(cleaned) | ||
| base := filepath.Base(cleaned) | ||
| if resolvedDir, err := filepath.EvalSymlinks(dir); err == nil { | ||
| return filepath.Join(resolvedDir, base) | ||
| } | ||
| return cleaned | ||
| } | ||
|
|
||
| func loadWhyViewData(ctx context.Context, repoRoot, gitPath string) (whyViewData, error) { | ||
| _, blameSpan := perf.Start(ctx, "git_blame") | ||
| blameOutput, err := runGitBlame(ctx, repoRoot, gitPath) | ||
| if err != nil { | ||
| blameSpan.RecordError(err) | ||
| blameSpan.End() | ||
| return whyViewData{}, err | ||
| } | ||
| blameSpan.End() | ||
|
|
||
| _, parseSpan := perf.Start(ctx, "parse_blame") | ||
| lines, err := parseBlamePorcelain(blameOutput) | ||
| if err != nil { | ||
| parseSpan.RecordError(err) | ||
| parseSpan.End() | ||
| return whyViewData{}, fmt.Errorf("parse git blame output: %w", err) | ||
| } | ||
| parseSpan.End() | ||
|
|
||
| _, buildRowsSpan := perf.Start(ctx, "build_rows") | ||
| blocks := collapseWhyBlameBlocks(lines) | ||
| rows := buildWhyBlameRows(lines, blocks) | ||
| buildRowsSpan.End() | ||
|
|
||
| _, openRepoSpan := perf.Start(ctx, "open_repository") | ||
| repo, err := openRepository(ctx) | ||
| if err != nil { | ||
| openRepoSpan.RecordError(err) | ||
| openRepoSpan.End() | ||
| return whyViewData{}, fmt.Errorf("open repository: %w", err) | ||
| } | ||
| openRepoSpan.End() | ||
|
|
||
| _, lookupSpan := perf.Start(ctx, "init_checkpoint_lookup") | ||
| lookup, err := newWhyCheckpointLookup(ctx, repo) | ||
| if err != nil { | ||
| lookupSpan.RecordError(err) | ||
| lookupSpan.End() | ||
| return whyViewData{}, fmt.Errorf("initialize checkpoint lookup: %w", err) | ||
| } | ||
| lookupSpan.End() | ||
|
|
||
| _, enrichSpan := perf.Start(ctx, "enrich_commits") | ||
| commits := enrichWhyCommits(ctx, repo, lookup, blocks) | ||
| enrichSpan.End() | ||
|
|
||
| return whyViewData{ | ||
| GitPath: gitPath, | ||
| Rows: rows, | ||
| Blocks: blocks, | ||
| Commits: commits, | ||
| }, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadSessionMetadataAndPrompts() silently ignores missing/failed reads of metadata.json and prompts (File/Contents errors), returning an empty SessionContent. That can mask corruption or unexpected repo state and leads to incorrect downstream agent/summary enrichment. This method should behave more like ReadSessionMetadata(): return a helpful error when metadata.json is missing/unreadable, and consider surfacing prompt read errors too (at least when the file exists but can't be read).