Files
pipelines/internal/harness/picker.go
T
alan 05ae530442 Make Pipelines Docker-only with OpenCode and Pi
Install current verified harness releases inside Docker, preserve persistent accounts and settings, and provide separate interactive and pipeline model defaults with terminal onboarding and fuzzy pickers.

Add Docker-aware authoring transport, Pi stage execution, bounded workflow controls, reliable final-result readback and usage settlement, and finite short-lived CLI pipe draining. Keep dated installation evidence and packaged authoring guidance consistent.

Verified with a real Docker installation, both harness stages and interactive authors, the built-in plan/implement/review starter, independent host race/vet checks, installer tests, and packaged skill tests. Generated release receipts stay ignored in clean checkouts.
2026-10-11 23:00:25 +01:00

311 lines
8.6 KiB
Go

package harness
import (
"context"
"fmt"
"io"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/ansi"
"github.com/sahilm/fuzzy"
)
const (
defaultPickerWidth = 80
defaultPickerHeight = 24
minPickerWidth = 28
maxVisibleRows = 12
)
type picker struct {
title string
purpose string
step string
options []string
query textinput.Model
index int
selected string
current string
canceled bool
width int
height int
plain bool
}
// matches applies the existing single O(n) fuzzy filter to the full catalog.
func (p picker) matches() []string {
if p.query.Value() == "" {
return p.options
}
var result []string
for _, m := range fuzzy.Find(p.query.Value(), p.options) {
result = append(result, m.Str)
}
return result
}
func (p picker) Init() tea.Cmd { return textinput.Blink }
func (p picker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
p.width, p.height = msg.Width, msg.Height
return p, nil
case tea.KeyMsg:
switch msg.String() {
case "esc", "ctrl+c":
p.canceled = true
return p, tea.Quit
case "down", "ctrl+n", "tab":
if n := len(p.matches()); n > 0 {
p.index = (p.index + 1) % n
}
return p, nil
case "up", "ctrl+p", "shift+tab":
if n := len(p.matches()); n > 0 {
p.index = (p.index + n - 1) % n
}
return p, nil
case "enter":
matches := p.matches()
if len(matches) > 0 {
p.selected = matches[min(p.index, len(matches)-1)]
return p, tea.Quit
}
return p, nil
}
}
var cmd tea.Cmd
previous := p.query.Value()
p.query, cmd = p.query.Update(msg)
// Reset the active row only when the filter actually changed; text cursor
// blinks (textinput.Blink) and unrelated messages must preserve the current
// or arrow-selected index without an O(n) fuzzy recompute. Arrow keys already
// clamp against the match count, and the option catalog never changes here.
if p.query.Value() != previous {
p.index = 0
}
return p, cmd
}
// pickerChrome is the fixed number of non-body lines View always renders: a
// blank, the filter line and a blank above the body, plus a blank and the hint
// below it.
const pickerChrome = 5
// headerHeight counts the wrapped step/title/purpose lines for the given inner
// content width. It shares wrap() with View so the body budget reserves the
// actual heading instead of a fixed guess.
func (p picker) headerHeight(inner int) int {
if inner < 1 {
inner = 1
}
lines := 0
if p.step != "" {
lines += len(wrap(p.step, inner))
}
if p.title != "" {
lines += len(wrap(p.title, inner))
}
if p.purpose != "" {
lines += len(wrap(p.purpose, inner))
}
return lines
}
// innerWidth is the content width View lays lines out with.
func (p picker) innerWidth() int {
width := p.width
if width < minPickerWidth {
width = defaultPickerWidth
}
return width - 2
}
// bodyRows bounds the selectable body to the real terminal height after the
// measured heading and fixed chrome, and to maxVisibleRows; it is never below
// one. View and visibleRows() share it so the measured window and the rendered
// window cannot diverge.
func (p picker) bodyRows(headerLines int) int {
height := p.height
if height <= 0 {
height = defaultPickerHeight
}
rows := height - headerLines - pickerChrome
if rows < 1 {
rows = 1
}
if rows > maxVisibleRows {
rows = maxVisibleRows
}
return rows
}
// visibleRows bounds the selectable window to the body budget and maxVisibleRows.
func (p picker) visibleRows() int {
return p.bodyRows(p.headerHeight(p.innerWidth()))
}
// windowStart centers the visible window on the active item, clamped to bounds.
func (p picker) windowStart(total, rows int) int {
if total <= rows {
return 0
}
start := p.index - rows/2
if start < 0 {
return 0
}
if start > total-rows {
return total - rows
}
return start
}
func (p picker) View() string {
width := p.width
if width < minPickerWidth {
width = defaultPickerWidth
}
inner := width - 2
styles := pickerStyles{plain: p.plain}
query := p.query
// textinput renders its own default prompt ("> ") plus a one-cell cursor
// outside Width. Clear the prompt and reserve the cursor. A value set
// programmatically (SetValue) never runs textinput's own overflow handling,
// so the rendered filter line is truncated explicitly below.
query.Prompt = ""
if inner > len("Filter: ")+1 {
query.Width = inner - len("Filter: ") - 1
}
// Wrap the heading exactly once and measure its line count for the budget.
var lines []string
if p.step != "" {
for _, line := range wrap(p.step, inner) {
lines = append(lines, styles.step(line))
}
}
if p.title != "" {
for _, line := range wrap(p.title, inner) {
lines = append(lines, styles.title(line))
}
}
if p.purpose != "" {
for _, line := range wrap(p.purpose, inner) {
lines = append(lines, styles.muted(line))
}
}
// The measured heading line count drives the body budget so the view fits
// the real terminal height (the same bounded computation visibleRows uses).
rows := p.bodyRows(len(lines))
filter := styles.step("Filter: ") + query.View()
if ansi.StringWidth(filter) > inner {
filter = ansi.Truncate(filter, inner, "…")
}
lines = append(lines, "", filter, "")
matches := p.matches()
if len(matches) == 0 {
// Bound the no-match state to the real body budget so a long query can
// never push the view past the terminal height.
quoted := ansi.Truncate(strconv.Quote(p.query.Value()), max(1, inner-1), "…")
errorLines := wrap("No models match "+quoted+".", inner)
mutedLines := wrap("Clear the filter or press Esc to cancel; defaults are unchanged.", inner)
remaining := rows
for _, line := range errorLines {
if remaining == 0 {
break
}
lines = append(lines, styles.error(ansi.Truncate(line, inner, "…")))
remaining--
}
for _, line := range mutedLines {
if remaining == 0 {
break
}
lines = append(lines, styles.muted(ansi.Truncate(line, inner, "…")))
remaining--
}
} else {
start := p.windowStart(len(matches), rows)
for i := start; i < min(len(matches), start+rows); i++ {
suffix := ""
if matches[i] == p.current {
suffix = " (current)"
}
name := ansi.Truncate(matches[i], max(4, inner-2-len(suffix)), "…")
lines = append(lines, styles.row(name+suffix, i == p.index, inner))
}
}
hint := "Type to filter · ↑/↓ select · Enter confirm · Esc/Ctrl+C cancel"
if inner < len(hint) {
hint = "↑/↓ · Enter · Esc"
}
lines = append(lines, "", styles.muted(hint))
// No trailing newline: the standard renderer must not push a full-height
// view past the terminal and scroll the step/heading off the top.
return strings.Join(lines, "\n")
}
// Selection describes one styled picker: its title, the distinct purpose it
// serves, an optional step label and the saved current value to preselect.
type Selection struct {
Title string
Purpose string
Step string
Options []string
Current string
}
// preselectIndex returns the position of the saved value so it is highlighted.
func preselectIndex(options []string, current string) int {
for i, option := range options {
if option == current {
return i
}
}
return 0
}
// Pick runs the fuzzy picker for one model purpose. It refuses to start Bubble
// Tea on a non-terminal stream so automation fails fast instead of hanging.
func Pick(ctx context.Context, selection Selection, in io.Reader, out io.Writer) (string, error) {
if len(selection.Options) == 0 {
return "", fmt.Errorf("no available models; sign in to the intended harness first")
}
if !isTerminal(in) || !isTerminal(out) {
return "", fmt.Errorf("interactive model selection requires a terminal; pass --model and --pipeline-model (or choose-model's catalog) for automation")
}
query := textinput.New()
query.Placeholder = "provider/model"
query.Focus()
model := picker{
title: selection.Title,
purpose: selection.Purpose,
step: selection.Step,
options: selection.Options,
query: query,
index: preselectIndex(selection.Options, selection.Current),
current: selection.Current,
plain: plainOutput(),
}
result, err := tea.NewProgram(model, tea.WithContext(ctx), tea.WithInput(in), tea.WithOutput(out)).Run()
if err != nil {
return "", err
}
p := result.(picker)
if p.canceled || p.selected == "" {
return "", fmt.Errorf("model selection canceled; defaults unchanged")
}
return p.selected, nil
}
// Select keeps the original convenience entry point for callers without a purpose.
func Select(ctx context.Context, title string, options []string, in io.Reader, out io.Writer) (string, error) {
return Pick(ctx, Selection{Title: title, Options: options}, in, out)
}