fix(tui): approval nav lockups, shell non-diff preview, hint dedup

Three QA-found issues in the approval gate:

- displayState gated the in-session/approval surfaces on hasApproval
  before sessionEntered, so moving the list cursor onto a session with a
  pending gate auto-opened it, and `l` back-to-list couldn't escape
  (the gate re-popped). Require sessionEntered first.
- The band ran every preview through the two-column diff renderer, so a
  shell tool's argv JSON rendered as an identical-column "diff". Detect
  real unified diffs (isUnifiedDiff); render other previews as plain
  text, and drop the ^x fullscreen hint when there's nothing to expand.
- The footer duplicated the band's approve/reject/steer/diff keys. It now
  shows navigation (l back / e events / q quit) instead.
This commit is contained in:
2026-06-03 01:16:04 +04:00
parent 3600ec6897
commit b56f0e88ca
5 changed files with 100 additions and 26 deletions
+19
View File
@@ -84,6 +84,25 @@ func PreviewFrame(kind string, w, h int) string {
} }
} }
case "approval-shell":
m.connected = true
m.currentModel = "llama-cpp:default"
m.sessions = sampleSessions()
m.selectedID = "04a546aa"
m.sessionEntered = true
if s := m.session("04a546aa"); s != nil {
s.CurrentStage = "execute_script"
s.Events = sampleEvents()
s.Pending = &Approval{
RequestID: "req-2", SessionID: "04a546aa", Tier: "T2", Risk: "MEDIUM",
ToolName: "shell",
Preview: `{"argv":["bash","scripts/healthcheck.sh"]}`,
Rationale: []string{
"[INTERPRETER_EXECUTION] Tool 'shell' invokes interpreter 'bash'",
},
}
}
case "diff": case "diff":
m.connected = true m.connected = true
m.currentModel = "llama-cpp:default" m.currentModel = "llama-cpp:default"
+42
View File
@@ -78,6 +78,48 @@ func parseUnifiedDiff(diff string) []diffRow {
return rows return rows
} }
// isUnifiedDiff reports whether s looks like a unified diff (file/hunk headers)
// rather than arbitrary preview text such as a shell argv JSON. Non-diff previews
// must not go through the two-column renderer (it would show identical columns).
func isUnifiedDiff(s string) bool {
return strings.Contains(s, "@@ -") || strings.HasPrefix(s, "--- ") || strings.Contains(s, "\n--- ")
}
// previewPlainLines splits non-diff preview text into display lines.
func previewPlainLines(s string) []string {
return strings.Split(strings.TrimRight(s, "\n"), "\n")
}
// previewRowCount is the number of content rows a preview occupies — diff rows
// for a real diff, plain lines otherwise. Drives band sizing and scroll bounds.
func previewRowCount(s string) int {
if isUnifiedDiff(s) {
return len(parseUnifiedDiff(s))
}
return len(previewPlainLines(s))
}
// renderPreviewLines renders non-diff preview text as dim plain lines (single
// column), up to maxRows starting at off, each truncated to width.
func (m Model) renderPreviewLines(content string, width, maxRows, off int) []string {
lines := previewPlainLines(content)
if off < 0 {
off = 0
}
if off > len(lines) {
off = len(lines)
}
end := off + maxRows
if end > len(lines) {
end = len(lines)
}
out := make([]string, 0, end-off)
for _, ln := range lines[off:end] {
out = append(out, m.theme.span(truncate(ln, width), m.theme.P.Dim))
}
return out
}
// hunkStarts reads the starting old/new line numbers from an @@ -a,b +c,d @@ marker. // hunkStarts reads the starting old/new line numbers from an @@ -a,b +c,d @@ marker.
func hunkStarts(h string) (int, int) { func hunkStarts(h string) (int, int) {
old, newer := 1, 1 old, newer := 1, 1
+9 -13
View File
@@ -210,25 +210,21 @@ func NewModel(client *ws.Client) Model {
} }
} }
// displayState derives the active screen, matching the Kotlin extension. // displayState derives the active screen. A session must be *entered*
// (sessionEntered) before its in-session or approval surfaces show — otherwise
// merely moving the list cursor onto a session with a pending gate would yank
// you into the approval, and `l` back-to-list couldn't escape it.
func (m Model) displayState() DisplayState { func (m Model) displayState() DisplayState {
if m.selectedID == "" { if m.selectedID == "" || !m.sessionEntered {
return StateIdle return StateIdle
} }
hasApproval := false if m.approvalDismissed {
return StateInSession
}
if s := m.session(m.selectedID); s != nil && s.Pending != nil { if s := m.session(m.selectedID); s != nil && s.Pending != nil {
hasApproval = true
}
switch {
case m.approvalDismissed:
return StateInSession
case hasApproval:
return StateApproval return StateApproval
case m.sessionEntered:
return StateInSession
default:
return StateIdle
} }
return StateInSession
} }
func (m Model) session(id string) *Session { func (m Model) session(id string) *Session {
+28 -12
View File
@@ -58,7 +58,7 @@ func mbg(t Theme, s string, fg lipgloss.Color) string {
func (m Model) approvalBandHeight() int { func (m Model) approvalBandHeight() int {
rows, rat := 0, 0 rows, rat := 0, 0
if s := m.session(m.selectedID); s != nil && s.Pending != nil { if s := m.session(m.selectedID); s != nil && s.Pending != nil {
rows = len(parseUnifiedDiff(s.Pending.Preview)) rows = previewRowCount(s.Pending.Preview)
if n := len(s.Pending.Rationale); n > 0 { if n := len(s.Pending.Rationale); n > 0 {
rat = n + 1 // rationale lines + trailing blank rat = n + 1 // rationale lines + trailing blank
} }
@@ -114,8 +114,6 @@ func (m Model) renderApprovalBand(h int) string {
if diffH < 1 { if diffH < 1 {
diffH = 1 diffH = 1
} }
rows := parseUnifiedDiff(a.Preview)
body := make([]string, 0, innerH) body := make([]string, 0, innerH)
body = append(body, m.justify(leftHdr, rightHdr, textW, t.P.Bg), "") body = append(body, m.justify(leftHdr, rightHdr, textW, t.P.Bg), "")
for _, r := range a.Rationale { for _, r := range a.Rationale {
@@ -125,8 +123,14 @@ func (m Model) renderApprovalBand(h int) string {
if len(a.Rationale) > 0 { if len(a.Rationale) > 0 {
body = append(body, "") body = append(body, "")
} }
var content []string
if isUnifiedDiff(a.Preview) {
content = m.renderSplitDiff(parseUnifiedDiff(a.Preview), textW, diffH, 0)
} else {
content = m.renderPreviewLines(a.Preview, textW, diffH, 0)
}
diffStart := len(body) diffStart := len(body)
body = append(body, m.renderSplitDiff(rows, textW, diffH, 0)...) body = append(body, content...)
for len(body) < diffStart+diffH { for len(body) < diffStart+diffH {
body = append(body, "") body = append(body, "")
} }
@@ -150,8 +154,11 @@ func (m Model) approvalActions() string {
return lipgloss.NewStyle().Foreground(t.P.Accent).Background(t.P.Bg).Bold(true).Render(k) + return lipgloss.NewStyle().Foreground(t.P.Accent).Background(t.P.Bg).Bold(true).Render(k) +
t.span(" "+l, t.P.Faint) t.span(" "+l, t.P.Faint)
} }
parts := []string{hint("a", "approve"), hint("A", "auto"), hint("s", "steer"), parts := []string{hint("a", "approve"), hint("A", "auto"), hint("s", "steer"), hint("r", "reject")}
hint("r", "reject"), hint("^x", "fullscreen"), hint("esc", "later")} if s := m.session(m.selectedID); s != nil && s.Pending != nil && isUnifiedDiff(s.Pending.Preview) {
parts = append(parts, hint("^x", "fullscreen"))
}
parts = append(parts, hint("esc", "later"))
return strings.Join(parts, t.span(" ", t.P.Faint)) return strings.Join(parts, t.span(" ", t.P.Faint))
} }
@@ -167,8 +174,7 @@ func (m Model) diffBodyHeight() int {
// diffMaxScroll is the largest scroll offset that still fills the body with // diffMaxScroll is the largest scroll offset that still fills the body with
// diff rows, keeping the last page anchored to the bottom of the modal. // diff rows, keeping the last page anchored to the bottom of the modal.
func (m Model) diffMaxScroll() int { func (m Model) diffMaxScroll() int {
rows := parseUnifiedDiff(m.currentDiff()) max := previewRowCount(m.currentDiff()) - m.diffBodyHeight()
max := len(rows) - m.diffBodyHeight()
if max < 0 { if max < 0 {
max = 0 max = 0
} }
@@ -180,7 +186,7 @@ func (m Model) diffModal() string {
w := m.modalWidth() w := m.modalWidth()
bodyH := m.diffBodyHeight() bodyH := m.diffBodyHeight()
diff := m.currentDiff() diff := m.currentDiff()
rows := parseUnifiedDiff(diff) isDiff := isUnifiedDiff(diff)
off := m.diffScrollOffset off := m.diffScrollOffset
if off > m.diffMaxScroll() { if off > m.diffMaxScroll() {
@@ -191,12 +197,22 @@ func (m Model) diffModal() string {
} }
var b strings.Builder var b strings.Builder
b.WriteString(m.titleLine("diff")) title := "preview"
if isDiff {
title = "diff"
}
b.WriteString(m.titleLine(title))
if tgt := diffTarget(diff); tgt != "" { if tgt := diffTarget(diff); tgt != "" {
b.WriteString(mbg(t, " "+tgt, t.P.Dim)) b.WriteString(mbg(t, " "+tgt, t.P.Dim))
} }
b.WriteString(mbg(t, " ("+itoa(len(rows))+" rows)", t.P.Faint) + "\n\n") b.WriteString(mbg(t, " ("+itoa(previewRowCount(diff))+" rows)", t.P.Faint) + "\n\n")
for _, ln := range m.renderSplitDiff(rows, w-6, bodyH, off) { var lines []string
if isDiff {
lines = m.renderSplitDiff(parseUnifiedDiff(diff), w-6, bodyH, off)
} else {
lines = m.renderPreviewLines(diff, w-6, bodyH, off)
}
for _, ln := range lines {
b.WriteString(ln + "\n") b.WriteString(ln + "\n")
} }
b.WriteString("\n" + modalHints(t, [][2]string{{"↑↓", "scroll"}, {"^x/esc", "close"}})) b.WriteString("\n" + modalHints(t, [][2]string{{"↑↓", "scroll"}, {"^x/esc", "close"}}))
+2 -1
View File
@@ -139,7 +139,8 @@ func (m Model) renderFooter() string {
case m.editMode == ModeInsert: case m.editMode == ModeInsert:
hints = []string{hint("enter", "send"), hint("esc", "normal"), hint("↑↓", "history")} hints = []string{hint("enter", "send"), hint("esc", "normal"), hint("↑↓", "history")}
case m.displayState() == StateApproval: case m.displayState() == StateApproval:
hints = []string{hint("a", "approve"), hint("A", "auto"), hint("s", "steer"), hint("r", "reject"), hint("x", "diff"), hint("esc", "later")} // Approval actions live in the band itself; the footer offers navigation only.
hints = []string{hint("l", "back"), hint("e", "events"), hint("q", "quit")}
case m.displayState() == StateIdle: case m.displayState() == StateIdle:
hints = []string{hint("i", "name"), hint("/", "filter"), hint("enter", "open"), hint("jk", "move"), hint("w", "workflows"), hint("p", "cmds"), hint("q", "quit")} hints = []string{hint("i", "name"), hint("/", "filter"), hint("enter", "open"), hint("jk", "move"), hint("w", "workflows"), hint("p", "cmds"), hint("q", "quit")}
default: // StateInSession default: // StateInSession