Record run input directory and retry failed runs with original inputs
This commit is contained in:
@@ -13,7 +13,7 @@ import (
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
var commands = []string{":logs", ":new", ":drafts", ":templates", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":pipeline", ":home", ":quit"}
|
||||
var commands = []string{":logs", ":new", ":drafts", ":templates", ":refresh", ":ping", ":edit", ":submit", ":versions", ":run", ":runs", ":retry", ":pipeline", ":home", ":quit"}
|
||||
|
||||
func (m Model) footerContent() string {
|
||||
label := fmt.Sprintf(" LOGS %d · :logs ", len(m.lines))
|
||||
@@ -82,6 +82,10 @@ func (m Model) pageCommands() []string {
|
||||
detailPage := m.keys.detail && !m.runOpen
|
||||
for _, command := range commands {
|
||||
switch command {
|
||||
case ":retry":
|
||||
if m.selectedFailedRun() <= 0 {
|
||||
continue
|
||||
}
|
||||
case ":pipeline":
|
||||
if id, version := m.selectedRunPipeline(); id <= 0 || version <= 0 {
|
||||
continue
|
||||
@@ -145,6 +149,8 @@ func (m Model) updatePalette(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
m.paletteOpen = false
|
||||
m.keys.palette = false
|
||||
switch command {
|
||||
case ":retry":
|
||||
return m.retrySelectedRun()
|
||||
case ":pipeline":
|
||||
return m.openRunPipeline()
|
||||
case ":drafts":
|
||||
|
||||
@@ -75,7 +75,7 @@ func (k keyMap) ShortHelp() []key.Binding {
|
||||
return []key.Binding{key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "template")), key.NewBinding(key.WithKeys("v"), key.WithHelp("v", "render/source")), key.NewBinding(key.WithKeys("enter"), key.WithHelp("Enter", "use template")), k.cancel}
|
||||
}
|
||||
if k.runs {
|
||||
return []key.Binding{k.commands, key.NewBinding(key.WithKeys("p"), key.WithHelp("p", "pipeline")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "select/scroll")), key.NewBinding(key.WithKeys("pgup", "pgdown"), key.WithHelp("PgUp/Dn", "page")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("Esc", "back"))}
|
||||
return []key.Binding{k.commands, key.NewBinding(key.WithKeys("p"), key.WithHelp("p", "pipeline")), key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "retry failed")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "select/scroll")), key.NewBinding(key.WithKeys("pgup", "pgdown"), key.WithHelp("PgUp/Dn", "page")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("Esc", "back"))}
|
||||
}
|
||||
if k.detail {
|
||||
return []key.Binding{k.commands, k.search, key.NewBinding(key.WithKeys("n", "p"), key.WithHelp("n/p", "next/prev stage")), key.NewBinding(key.WithKeys("tab"), key.WithHelp("Tab", "pane")), key.NewBinding(key.WithKeys("r"), key.WithHelp("r", "runs")), key.NewBinding(key.WithKeys("j", "k"), key.WithHelp("j/k", "scroll")), key.NewBinding(key.WithKeys("esc"), key.WithHelp("esc", "home"))}
|
||||
@@ -289,6 +289,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
} else if msg.Type == protocol.Run || msg.Type == protocol.RunStarted {
|
||||
accepted := m.runOpen && msg.RunDetail != nil && ((msg.Type == protocol.Run && msg.RunDetail.ID == m.requestedRunID) || (msg.Type == protocol.RunStarted && msg.ID == m.runStartRequest))
|
||||
if accepted {
|
||||
if msg.Type == protocol.RunStarted {
|
||||
m.runStartRequest = ""
|
||||
}
|
||||
m.keys.runs = true
|
||||
m.runDetail = msg.RunDetail
|
||||
m.requestedRunID = msg.RunDetail.ID
|
||||
@@ -304,6 +307,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
}
|
||||
if strings.HasPrefix(msg.ID, "run-") && m.runOpen {
|
||||
m.runError = msg.Error
|
||||
if msg.ID == m.runStartRequest {
|
||||
m.runStartRequest = ""
|
||||
}
|
||||
}
|
||||
if msg.ID != "" && msg.ID == m.detailRequest {
|
||||
m.detailLoading = false
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package tui
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/server"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
)
|
||||
|
||||
func TestRetryShortcutAndPaletteSendServerRetryWithoutReplacingDirectory(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "retry.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
store.Submit(ctx, "workflow", pipeline.Template([]string{"one"}, true))
|
||||
items, _ := store.List(ctx)
|
||||
d, _ := store.Get(ctx, items[0].ID, 1)
|
||||
srv := httptest.NewServer(server.Handler(ctx, store))
|
||||
defer srv.Close()
|
||||
id, _, err := store.CreateRun(ctx, d, "failed", filepath.Join(t.TempDir(), "missing"), false, nil, map[string]any{"prompt": "original task"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.FinishRun(ctx, id, "failed", "error", "original failure", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
run, err := store.GetRun(ctx, id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := New(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect")
|
||||
connected := m.connect()().(connectedMsg)
|
||||
defer connected.conn.CloseNow()
|
||||
updated, read := m.Update(connected)
|
||||
m = updated.(Model)
|
||||
updated, read = m.Update(read())
|
||||
m = updated.(Model)
|
||||
for _, palette := range []bool{false, true} {
|
||||
m.runOpen = true
|
||||
m.keys.runs = true
|
||||
m.runDetail = run
|
||||
m.requestedRunID = id
|
||||
m.runs = []protocol.RunSummary{run.RunSummary}
|
||||
if !slices.Contains(m.pageCommands(), ":retry") {
|
||||
t.Fatal("failed run missing retry action")
|
||||
}
|
||||
var send tea.Cmd
|
||||
if palette {
|
||||
m.runDetail = nil
|
||||
m.requestedRunID = 0
|
||||
m.commandInput = newInput()
|
||||
m.commandInput.SetValue(":retry")
|
||||
updated, send = m.updatePalette(tea.KeyMsg{Type: tea.KeyEnter})
|
||||
} else {
|
||||
updated, send = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
|
||||
}
|
||||
m = updated.(Model)
|
||||
if send == nil || m.runStartRequest == "" || m.runDetail != nil || m.requestedRunID != 0 {
|
||||
t.Fatal("retry did not start cleanly")
|
||||
}
|
||||
updated, _ = m.Update(receivedMsg(protocol.Message{Type: protocol.Run, RunDetail: run}))
|
||||
m = updated.(Model)
|
||||
if m.runDetail != nil {
|
||||
t.Fatal("stale poll restored original run during retry")
|
||||
}
|
||||
if _, ok := send().(sentMsg); !ok {
|
||||
t.Fatal("retry request failed")
|
||||
}
|
||||
updated, read = m.Update(read())
|
||||
m = updated.(Model)
|
||||
if !strings.Contains(m.runError, "workspace is unavailable") || m.runStartRequest != "" {
|
||||
t.Fatal("retry replaced original directory or stuck loading after server error", m.runError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetryResponseClearsPendingRequestAndAllowsAnotherFailedAttempt(t *testing.T) {
|
||||
m := New(context.Background(), "")
|
||||
m.runOpen = true
|
||||
m.keys.runs = true
|
||||
m.runStartRequest = "run-2"
|
||||
run := &protocol.RunDetail{RunSummary: protocol.RunSummary{ID: 3, Status: "failed"}, RetryOf: 1, Input: map[string]any{"prompt": "task", "pwd": "/project"}}
|
||||
updated, _ := m.Update(receivedMsg(protocol.Message{Type: protocol.RunStarted, ID: "run-2", RunDetail: run}))
|
||||
m = updated.(Model)
|
||||
if m.runDetail != run || m.requestedRunID != 3 || m.runStartRequest != "" || m.selectedFailedRun() != 3 {
|
||||
t.Fatal("retry response was not displayed or another retry is blocked")
|
||||
}
|
||||
if !slices.Contains(m.pageCommands(), ":retry") {
|
||||
t.Fatal("failed retry lacks retry command")
|
||||
}
|
||||
for _, status := range []string{"queued", "running", "succeeded", "canceled"} {
|
||||
m.runDetail.Status = status
|
||||
if slices.Contains(m.pageCommands(), ":retry") {
|
||||
t.Fatal("retry action visible for nonfailed run", status)
|
||||
}
|
||||
updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
|
||||
m = updated.(Model)
|
||||
if cmd != nil || m.runError != "Select a failed run to retry." {
|
||||
t.Fatal("nonfailed run accepted", status)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,38 @@ import (
|
||||
"github.com/charmbracelet/x/ansi"
|
||||
)
|
||||
|
||||
func (m Model) selectedFailedRun() int64 {
|
||||
if !m.runOpen || m.runStartRequest != "" {
|
||||
return 0
|
||||
}
|
||||
if m.runDetail != nil {
|
||||
if m.runDetail.Status == "failed" {
|
||||
return m.runDetail.ID
|
||||
}
|
||||
return 0
|
||||
}
|
||||
if m.requestedRunID > 0 {
|
||||
return 0
|
||||
}
|
||||
runs := m.visibleRuns()
|
||||
if len(runs) > 0 {
|
||||
r := runs[min(m.runIndex, len(runs)-1)]
|
||||
if r.Status == "failed" {
|
||||
return r.ID
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (m Model) retrySelectedRun() (tea.Model, tea.Cmd) {
|
||||
id := m.selectedFailedRun()
|
||||
if id <= 0 {
|
||||
m.runError = "Select a failed run to retry."
|
||||
return m, nil
|
||||
}
|
||||
return m.runRequest(protocol.Message{Type: protocol.RetryRun, RunID: id, RequestKey: fmt.Sprintf("retry-%d", time.Now().UnixNano())})
|
||||
}
|
||||
|
||||
func (m Model) selectedRunPipeline() (int64, int) {
|
||||
if !m.runOpen {
|
||||
return 0, 0
|
||||
@@ -279,6 +311,9 @@ func (m Model) runSectionLines(width int) []string {
|
||||
editScope = strings.Join(r.WritePaths, ", ")
|
||||
}
|
||||
content := [][]string{summary, valueLines(r.Input), valueLines(r.Output), {"Select a stage to inspect its tool calls."}, {"Workspace: " + r.Workspace, "Source hash: " + r.ContentHash, fmt.Sprintf("Shell/edit enabled: %t", r.AllowTools), "Stage tools: defined by the pinned pipeline version", "Edit scope: " + editScope}}
|
||||
if r.RetryOf > 0 {
|
||||
content[4] = append(content[4], fmt.Sprintf("Retry of run #%d", r.RetryOf))
|
||||
}
|
||||
if m.runStepIndex > 0 && m.runStepIndex <= len(r.Steps) {
|
||||
step := r.Steps[m.runStepIndex-1]
|
||||
summary = []string{"Status: " + m.progress(m.stepProgressStatus(step)), "Route: " + outcomeLabel(step.Outcome, step.Outcome) + " → " + step.Target, "Started: " + dateLabel(step.StartedAt), "Finished: " + dateLabel(step.FinishedAt), "Duration: " + runDuration(step.StartedAt, step.FinishedAt)}
|
||||
@@ -353,12 +388,12 @@ func (m Model) runsView(height int) string {
|
||||
if m.runError != "" {
|
||||
rows = append(rows, m.runError)
|
||||
}
|
||||
return panel(m.width, height, []string{bar(title, fmt.Sprint(len(runs)), max(1, m.width-4), true)}, rows, "j/k select · Enter inspect · p pipeline", true)
|
||||
return panel(m.width, height, []string{bar(title, fmt.Sprint(len(runs)), max(1, m.width-4), true)}, rows, "j/k select · Enter inspect · p pipeline · r retry failed", true)
|
||||
}
|
||||
r := m.runDetail
|
||||
header := bar(fmt.Sprintf("#%d %s · v%d", r.ID, r.Name, r.Version), m.progress(r.Status)+" · "+runDuration(r.CreatedAt, r.FinishedAt), m.width, false)
|
||||
if height <= 6 {
|
||||
return fitBlock([]string{header, m.runSelectionLabel(), "p pipeline · Tab pane · Esc back"}, m.width, height)
|
||||
return fitBlock([]string{header, m.runSelectionLabel(), "p pipeline · r retry failed · Tab pane · Esc back"}, m.width, height)
|
||||
}
|
||||
innerHeight := height - 2
|
||||
sidebar := m.runSidebarWidth()
|
||||
@@ -398,7 +433,7 @@ func (m Model) runsView(height int) string {
|
||||
lines := m.runSectionLines(max(1, rightWidth-4))
|
||||
page := max(0, innerHeight-4)
|
||||
top := max(0, min(m.runTop, max(0, len(lines)-page)))
|
||||
footer := fmt.Sprintf("%d–%d / %d · s/i/o/t/m sections · p pipeline · PgUp/Dn · Esc back", min(top+1, len(lines)), min(top+page, len(lines)), len(lines))
|
||||
footer := fmt.Sprintf("%d–%d / %d · s/i/o/t/m sections · p pipeline · r retry failed · PgUp/Dn · Esc back", min(top+1, len(lines)), min(top+page, len(lines)), len(lines))
|
||||
label := m.runSelectionLabel()
|
||||
if m.runStepIndex > 0 && m.runStepIndex <= len(r.Steps) {
|
||||
label = fmt.Sprintf("Details · %s · step %d", r.Steps[m.runStepIndex-1].Stage, r.Steps[m.runStepIndex-1].Sequence)
|
||||
|
||||
+12
-2
@@ -31,8 +31,16 @@ func (m Model) runRequest(request protocol.Message) (tea.Model, tea.Cmd) {
|
||||
m.runCollapsed = nil
|
||||
m.runError = ""
|
||||
}
|
||||
if request.Type == protocol.RunPipeline {
|
||||
if request.Workspace == "" {
|
||||
if request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun {
|
||||
if request.Type == protocol.RetryRun {
|
||||
m.runOpen = true
|
||||
m.requestedRunID = 0
|
||||
m.runDetail = nil
|
||||
m.runStepIndex = 0
|
||||
m.runTop = 0
|
||||
m.runCollapsed = nil
|
||||
}
|
||||
if request.Type == protocol.RunPipeline && request.Workspace == "" {
|
||||
cwd, err := os.Getwd()
|
||||
if err != nil {
|
||||
m.appendLine("cannot determine current directory: " + err.Error())
|
||||
@@ -158,6 +166,8 @@ func (m Model) updateRuns(msg tea.KeyMsg) (tea.Model, tea.Cmd) {
|
||||
switch msg.String() {
|
||||
case "p":
|
||||
return m.openRunPipeline()
|
||||
case "r":
|
||||
return m.retrySelectedRun()
|
||||
case "esc":
|
||||
if m.runDetail != nil || m.requestedRunID > 0 || m.runStartRequest != "" {
|
||||
m.runDetail = nil
|
||||
|
||||
Reference in New Issue
Block a user