Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ are gone, rexdiff marks the comment as stale.
| `\|` | side by side or top to bottom |
| `1` … `5` | overview, guide, diff, all files, or pull request |
| `r` | in the guide: write it again |
| `a` | in the guide: pick the agent that writes it |
| `?` | all keys |

## Tabs
Expand All @@ -84,9 +85,13 @@ are gone, rexdiff marks the comment as stale.
works with no forge and no login.
- **Guide**: the changes in the order of their meaning, as on [pulls.review].
An agent groups the files into sections with a title and a summary; the diff
of each file shows beside its section. rexdiff runs the first of `claude -p`,
`pi -p`, and `codex exec -` that is installed, or `guide_agent`. It keeps
each guide, so the agent writes it once. Press `r` to write it again.
of each file shows beside its section. rexdiff runs the first of claude, pi,
codex, opencode, cursor-agent, and agy that is installed, or `guide_agent`.
Press `a` to pick another agent; in a terminal with the kitty graphics
protocol (Rex, Ghostty, kitty), the list shows their logos. opencode,
cursor-agent, and agy take the diff as one argument, so a diff over about
1 MB (128 KB on Linux) turns them off. It keeps each
guide, so the agent writes it once. Press `r` to write it again.
- **Diff**: the changes, one file at a time. A wide pane shows them side by
side; a narrow one, top to bottom. Binary files are not diffed.

Expand All @@ -110,8 +115,9 @@ Other settings:
- `markdown_view`
- `file_list`
- `file_list_width`
- `guide_agent`: the command that writes the guide. It reads the prompt on its
standard input, for example `"claude -p --model sonnet"`
- `guide_agent`: the agent that writes the guide: `claude`, `pi`, `codex`,
`opencode`, `cursor-agent`, or `agy`, or a command that reads the prompt on
its standard input, for example `"claude -p --model sonnet"`
- `diff_signs`: `true` marks changed lines with `+` and `-`, in place of a coloured bar
- `cursor_line`: `false` turns off the tint of the line at the cursor
- `renderer`: `"kitty"` draws the screen as pictures with the kitty graphics
Expand Down
2 changes: 1 addition & 1 deletion cmd/rexdiff/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ local settings = {
-- key = "shift+cmd+r", -- the key of the toggle, or false for none
-- placement = "split", -- split, zoomed, tab, or overlay
-- layout = "unified", -- split (side by side when it fits) or unified
-- guide_agent = "claude -p", -- the agent that writes the Guide tab
-- guide_agent = "claude", -- the agent that writes the Guide tab: claude, pi, codex, opencode, cursor-agent, agy, or a command
-- renderer = "kitty", -- draw with the kitty graphics protocol, with no flicker
}

Expand Down
10 changes: 6 additions & 4 deletions tui/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ type config struct {
FileList position `json:"file_list"`
// FileListWidth is in columns, minListWidth or more.
FileListWidth int `json:"file_list_width"`
// GuideAgent is the command that writes the Guide tab, for example
// "claude -p". It reads the prompt on its standard input and prints
// the answer. "" picks the first of claude, pi, and codex that is
// installed.
// GuideAgent is the agent that writes the Guide tab: a name of
// guideAgents, for example "agy", or a command, for example
// "claude -p --model sonnet", that reads the prompt on its standard
// input and prints the answer. "" picks the first of guideAgents that
// is installed.
GuideAgent string `json:"guide_agent"`
// DiffSigns marks a changed line with + or -, in place of a bar.
DiffSigns bool `json:"diff_signs"`
Expand Down Expand Up @@ -163,6 +164,7 @@ type keybindings struct {
NavigatorShrink []string `json:"navigator-shrink"`
OpenPR []string `json:"open-pr"`
Refresh []string `json:"refresh"`
GuideAgent []string `json:"guide-agent"`
Keys []string `json:"keys"`
Quit []string `json:"quit"`
QuitDiscard []string `json:"quit-discard"`
Expand Down
189 changes: 168 additions & 21 deletions tui/guide.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tui

// The Guide tab reads the changes in the order of their meaning, as
// pulls.review does. An agent CLI (claude, pi, or codex) groups the
// pulls.review does. An agent CLI (claude, pi, codex, and others) groups the
// changed files into sections, each with a title and a short summary. The
// tab shows a section at the left and the diff of one of its files at the
// right. The guide of a pair of trees is kept on disk, so the agent writes
Expand All @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"slices"
"strings"
"time"
Expand All @@ -41,13 +42,48 @@ const (
)

// guideAgents are the agent CLIs that can write the guide, in the order
// that rexdiff tries them. Each one reads the prompt on its standard input.
var guideAgents = []struct{ name, command string }{
{"claude", "claude -p"},
{"pi", "pi -p"},
{"codex", "codex exec -"},
// that rexdiff tries them. Each command reads the prompt on its standard
// input. opencode, cursor-agent, and agy take the prompt only as an
// argument, so their commands pass the standard input as one, and a big
// diff does not fit. opencode and cursor-agent run in their modes that do
// not edit, since the diff can hold instructions.
var guideAgents = []agentCLI{
{"claude", "claude -p", false},
{"pi", "pi -p", false},
{"codex", "codex exec -", false},
{"opencode", `opencode run --agent plan "$(cat)"`, true},
{"cursor-agent", `cursor-agent -p --mode ask --trust "$(cat)"`, true},
{"agy", `agy -p "$(cat)"`, true},
}

// agentCLI is an agent CLI that can write the guide, and its command. arg
// tells that the command takes the prompt as an argument.
type agentCLI struct {
name, command string
arg bool
}

// maxArg is the longest prompt, in bytes, that an agent can take as an
// argument. Linux caps one argument (MAX_ARG_STRLEN), and macOS all the
// arguments and the environment together (ARG_MAX). A variable, so that
// tests can set it.
var maxArg = func() int {
if runtime.GOOS == "linux" {
return 128<<10 - 1
}
env := 0
for _, e := range os.Environ() {
env += len(e) + 1 + 8 // the text, its NUL, and its pointer
}
return 1<<20 - env - 4<<10 // ponytail: 4 KiB for the other arguments
}()

// bigPrompt reports whether a prompt of n bytes does not fit an argument.
func bigPrompt(n int) bool { return n > maxArg }

// megabytes writes n bytes in MB, for the messages about a big diff.
func megabytes(n int) string { return fmt.Sprintf("%.1f MB", float64(n)/(1<<20)) }

// guideSection is one part of the guide: the files of one change.
type guideSection struct {
Title string `json:"title"`
Expand Down Expand Up @@ -161,18 +197,24 @@ func (m *model) guideNote() string {
case g.running:
return m.guideProgress()
case g.err != "":
return g.err + " · press r to try again"
return g.err + " · press r to try again, a to pick the agent"
case len(m.files) == 0:
return cmp.Or(m.changes.note, "no changes to explain")
}
return "press r to write a guide"
return "press r to write a guide, a to pick the agent"
}

// guideProgress tells which agent writes the guide, and for how long.
func (m *model) guideProgress() string {
name, _, _ := strings.Cut(m.guide.agent, " ")
secs := int(time.Since(m.guide.started).Seconds())
return fmt.Sprintf("%s is reading the changes… %ds", name, secs)
return fmt.Sprintf("%s is reading the changes… %ds", agentName(m.guide.agent), secs)
}

// agentName returns the program of the command of an agent, for example
// "claude" for "claude -p".
func agentName(command string) string {
name, _, _ := strings.Cut(strings.TrimSpace(command), " ")
return name
}

// guideLines renders h lines of the guide column: the section, its
Expand All @@ -195,7 +237,13 @@ func (m *model) guideLines(h int) (lines []string, first, outline int) {
lines = append(lines, "")
switch {
case m.guide.running:
add(noteStyle, "↻ "+m.guideProgress())
icon := cmp.Or(m.agentLogo(agentName(m.guide.agent)), noteStyle.Render("↻")+" ")
for i, l := range strings.Split(wrapWords(m.guideProgress(), w-2-logoCols-1), "\n") {
if i > 0 {
icon = " "
}
lines = append(lines, " "+icon+" "+noteStyle.Render(l))
}
lines = append(lines, "")
case m.guide.trees != m.shownTrees:
add(noteStyle, "↻ the changes moved on · r to rewrite")
Expand Down Expand Up @@ -276,17 +324,22 @@ func (m *model) writeGuide() tea.Cmd {
m.renderGuide()
return nil
}
agent := m.guideAgent()
if agent == "" {
m.guide.err = "no agent to write the guide: install claude or pi, or set guide_agent"
prompt := guidePrompt(m.guideCommits(), m.files)
agent := m.guideAgent(len(prompt))
if err := checkAgent(agent, len(prompt)); err != "" {
m.guide.err = err
m.renderGuide()
return nil
}
if bigPrompt(len(prompt)) {
m.flash(fmt.Sprintf("the diff is big (%s): %s can take long", megabytes(len(prompt)), agentName(agent)))
}
m.guide.running, m.guide.started, m.guide.agent, m.guide.err = true, time.Now(), agent, ""
m.renderGuide()

ctx, dir, trees := m.ctx, m.repo.Root(), m.shownTrees
prompt := guidePrompt(m.guideCommits(), m.files)
// The logos go with the first frame, after Bubble Tea is on the
// alternate screen, where the terminal keeps them.
return tea.Batch(frame(m.guide.started), func() tea.Msg {
out, err := runAgent(ctx, dir, agent, prompt)
if err != nil {
Expand Down Expand Up @@ -350,20 +403,114 @@ func (m *model) onGuide(msg guideMsg) {
}
}

// guideAgent returns the command that writes the guide: the setting, or
// the first agent CLI that is installed, or "".
func (m *model) guideAgent() string {
if m.cfg.GuideAgent != "" {
return m.cfg.GuideAgent
// guideAgent returns the command that writes a guide of a prompt of n
// bytes: the agent that the user picked, or the setting, or the first
// agent CLI that is installed and takes the prompt, or "". The pick and
// the setting are a name of guideAgents or a command.
func (m *model) guideAgent(n int) string {
if pick := cmp.Or(m.guidePick, m.cfg.GuideAgent); pick != "" {
for _, a := range guideAgents {
if a.name == pick {
return a.command
}
}
return pick
}
for _, a := range guideAgents {
if _, err := exec.LookPath(a.name); err == nil {
if installed(a.name) && !(a.arg && bigPrompt(n)) {
return a.command
}
}
return ""
}

// checkAgent returns why the agent command cannot write a guide of a
// prompt of n bytes, or "". It checks only the agent CLIs of guideAgents:
// a command of the setting can start with a variable, as in
// "FOO=1 claude -p", and the shell tells when it finds no program.
func checkAgent(command string, n int) string {
if command == "" {
return "no agent found"
}
for _, a := range guideAgents {
switch {
case a.command != command:
case !installed(a.name):
return fmt.Sprintf("agent %q not found in PATH", a.name)
case a.arg && bigPrompt(n):
return fmt.Sprintf("the diff is too big for %s (%s, at most %s)", a.name, megabytes(n), megabytes(maxArg))
}
}
return ""
}

// installed reports whether the PATH has program.
func installed(program string) bool {
_, err := exec.LookPath(program)
return err == nil
}

// openGuideAgentPicker lets the user pick the agent that writes the
// guide. The list has the agent CLIs, and the command of the setting if
// it is not one of them.
//
// A diff that is too big for an argument turns off the agents that take
// the prompt as one, and marks the others, which take long on it.
func (m *model) openGuideAgentPicker() tea.Cmd {
ov := newOverlay(overlayGuideAgent, "which agent writes the guide?")
n := len(guidePrompt(m.guideCommits(), m.files))
cur := m.guideAgent(n)
var items []pickItem
for _, a := range guideAgents {
it := pickItem{label: a.name, id: a.command, logo: m.agentLogoID(a.name)}
var tags []string
switch {
case !installed(a.name):
tags, it.off = []string{"not installed"}, a.name+" is not installed"
case a.arg && bigPrompt(n):
tags, it.off = []string{"diff too big"}, checkAgent(a.command, n)
default:
if a.command == cur {
tags = append(tags, "current")
}
if bigPrompt(n) {
tags = append(tags, "big diff, slow")
}
}
it.tag = strings.Join(tags, " · ")
items = append(items, it)
}
if c := m.cfg.GuideAgent; c != "" && !slices.ContainsFunc(guideAgents, func(a agentCLI) bool {
return a.name == c || a.command == c
}) {
tag := ""
if c == cur {
tag = "current"
}
items = append(items, pickItem{label: "guide_agent", detail: c, tag: tag, id: c})
}
ov.setItems(items)
ov.cur = max(slices.IndexFunc(items, func(it pickItem) bool { return it.id == cur }), 0)
m.open(ov)
return m.sendLogos()
}

// pickGuideAgent makes the agent of item it of the picker the agent of
// the guide, and writes the guide with it.
func (m *model) pickGuideAgent(it pickItem) tea.Cmd {
command := it.id
switch {
case it.off != "":
m.flash(it.off)
return nil
case m.guide.running:
m.flash("wait for " + agentName(m.guide.agent) + " to finish")
return nil
}
m.guidePick = command
return m.writeGuide()
}

// guideCommits returns the subjects of the commits of the branch, which
// tell the agent the intent and the language of the changes.
func (m *model) guideCommits() []string {
Expand Down
8 changes: 6 additions & 2 deletions tui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type keyMap struct {
Search, Find, GotoLine, Rendered, Split, Wrap key.Binding
NavPosition, NavHide, NavGrow, NavShrink key.Binding
OpenPR, Refresh, Keys, Quit, QuitDiscard key.Binding
// GuideAgent picks the agent that writes the guide, in the Guide tab.
GuideAgent key.Binding
// Upgrade installs a newer release. It works only while the footer
// tells about a newer release.
Upgrade key.Binding
Expand Down Expand Up @@ -106,6 +108,7 @@ func defaultKeys() keyMap {
NavShrink: bind([]string{">"}, ">", "narrow list"),
OpenPR: bind([]string{"o"}, "o", "open PR in browser"),
Refresh: bind([]string{"r"}, "r", "refresh"),
GuideAgent: bind([]string{"a"}, "a", "pick guide agent"),
Upgrade: bind([]string{"U"}, "U", "update rexdiff"),
Keys: bind([]string{"?"}, "?", "all keys"),
Quit: bind([]string{"q", "ctrl+c"}, "q", "quit"),
Expand Down Expand Up @@ -210,6 +213,7 @@ func (k *keyMap) bindings(c keybindings) []binding {
{"navigator-shrink", c.NavigatorShrink, &k.NavShrink},
{"open-pr", c.OpenPR, &k.OpenPR},
{"refresh", c.Refresh, &k.Refresh},
{"guide-agent", c.GuideAgent, &k.GuideAgent},
{"keys", c.Keys, &k.Keys},
{"quit", c.Quit, &k.Quit},
{"quit-discard", c.QuitDiscard, &k.QuitDiscard},
Expand Down Expand Up @@ -273,7 +277,7 @@ func (h helpKeys) ShortHelp() []key.Binding {
return []key.Binding{k.Down, k.Refresh, k.TabOverview, k.Keys, k.Quit}
case h.tab == tabGuide:
return []key.Binding{
k.Comment, k.Send, k.NextFile, hint(k.Refresh, "r", "rewrite guide"), k.Split, k.Keys, k.Quit,
k.Comment, k.Send, k.NextFile, hint(k.Refresh, "r", "rewrite guide"), k.GuideAgent, k.Split, k.Keys, k.Quit,
}
}
return []key.Binding{k.Comment, k.Send, k.NextHunk, k.NextFile, k.Split, k.Keys, k.Quit}
Expand All @@ -295,7 +299,7 @@ func (h helpKeys) FullHelp() [][]key.Binding {
view := []key.Binding{
k.Search, k.Find, k.Rendered, k.Split, k.Wrap, k.NavPosition, k.NavHide, k.NavGrow,
}
other := []key.Binding{k.OpenPR, k.Refresh, k.Keys, k.Quit, k.QuitDiscard}
other := []key.Binding{k.OpenPR, k.Refresh, k.GuideAgent, k.Keys, k.Quit, k.QuitDiscard}
return [][]key.Binding{move, scopes, comments, view, other}
}

Expand Down
Loading
Loading