diff --git a/docs/cli.md b/docs/cli.md index e27ceb0d..e10b104a 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -85,8 +85,10 @@ Press `d` on the dashboard or use `:drafts` to browse the same retained drafts a `pipelines drafts`. Use `n/p` to select, `j/k` to scroll source, `e`/Enter to reopen the existing draft, `v` for server validation, and `s` for submission confirmation. `r` refreshes the list. Submission preserves create-only/version guards and rejects -drafts belonging to another server. An unnamed draft must be named through OpenCode -or CLI `submit --name`. Draft selection/editor completion never publishes it. +drafts belonging to another server. For unnamed drafts, submission asks you to +type a pipeline name directly in the TUI. Enter saves the name in retained metadata +and opens publication confirmation; it does not publish yet. Esc keeps the draft +without saving unfinished name input. Draft selection/editor completion never publishes it. On editor exit, the connected TUI checks the authoritative server before asking to submit. If the draft source is already published (including a newer version after the draft's base that was subsequently superseded), it skips confirmation diff --git a/internal/draft/draft.go b/internal/draft/draft.go index 7dc0c07a..5d1bf832 100644 --- a/internal/draft/draft.go +++ b/internal/draft/draft.go @@ -116,6 +116,45 @@ func List() ([]Draft, error) { return items, nil } +// SetName updates only retained metadata, preserving source and publication scope. +func SetName(path, name string) error { + name = strings.TrimSpace(name) + if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") { + return fmt.Errorf("pipeline name must be 1–200 bytes without line breaks or NUL") + } + d, err := Load(path) + if err != nil { + return err + } + metadata := d.Path + ".json" + info, err := os.Lstat(metadata) + if err != nil { + return err + } + if !info.Mode().IsRegular() { + return fmt.Errorf("draft metadata must be a regular file, not a symlink") + } + d.Name = name + data, err := json.MarshalIndent(d, "", " ") + if err != nil { + return err + } + f, err := os.CreateTemp(filepath.Dir(metadata), ".draft-name-*") + if err != nil { + return err + } + defer os.Remove(f.Name()) + _, writeErr := f.Write(data) + closeErr := f.Close() + if writeErr != nil { + return writeErr + } + if closeErr != nil { + return closeErr + } + return os.Rename(f.Name(), metadata) +} + // Delete only a selected, managed cache draft and its sidecar. Never recurse, // follow a file symlink, or delete an arbitrary external submission source. func Delete(path string) error { diff --git a/internal/tui/drafts_test.go b/internal/tui/drafts_test.go index b205bf65..ee67a416 100644 --- a/internal/tui/drafts_test.go +++ b/internal/tui/drafts_test.go @@ -27,7 +27,7 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) { srv := httptest.NewServer(server.Handler(ctx, store)) defer srv.Close() url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect" - d, err := draft.Create("CLI-created draft", pipeline.Template([]string{"worker"}, true), url, true, 0) + d, err := draft.Create("", pipeline.Template([]string{"worker"}, true), url, true, 0) if err != nil { t.Fatal(err) } @@ -40,7 +40,7 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) { m = updated.(Model) updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}}) m = updated.(Model) - if !m.draftOpen || len(m.draftItems) != 1 || m.draftItems[0].Path != d.Path || !strings.Contains(m.View(), "CLI-created draft") { + if !m.draftOpen || len(m.draftItems) != 1 || m.draftItems[0].Path != d.Path || !strings.Contains(m.View(), "Unnamed draft") { t.Fatal("retained draft not visible") } updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}}) @@ -56,6 +56,22 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) { if send != nil || !m.confirmOpen || len(items) != 0 { t.Fatal("draft selection published without confirmation") } + if !strings.Contains(m.View(), "Name this pipeline") { + t.Fatal("missing name prompt") + } + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("CLI-created draft")}) + m = updated.(Model) + updated, send = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + items, _ = store.List(ctx) + d.Name = "CLI-created draft" + retained, err := draft.Load(d.Path) + if err != nil || retained.Name != d.Name || retained.Server != url || !retained.CreateOnly || retained.BaseVersion != 0 || send != nil || len(items) != 0 || !m.confirmOpen { + t.Fatal("naming lost metadata or published without confirmation", retained, err) + } + if source, err := draft.Read(d.Path); err != nil || source != pipeline.Template([]string{"worker"}, true) { + t.Fatal("naming changed source", err) + } updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) if send == nil { @@ -113,6 +129,46 @@ func TestRetainedDraftBrowserValidatesAndExplicitlySubmits(t *testing.T) { } } +func TestDraftNamePromptCancelAndInvalidName(t *testing.T) { + t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) + d, err := draft.Create("", "source", "", true, 0) + if err != nil { + t.Fatal(err) + } + m := New(context.Background(), "ws://this-server") + updated, _ := m.openDrafts() + m = updated.(Model) + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) + m = updated.(Model) + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if !m.confirmOpen || m.pendingDraft.name != "" || m.submitNotice == "" { + t.Fatal("blank name accepted") + } + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune(strings.Repeat("é", 101))}) + m = updated.(Model) + updated, cmd := m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if cmd != nil || m.pendingDraft.name != "" { + t.Fatal("oversized byte name accepted") + } + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyEsc}) + m = updated.(Model) + retained, err := draft.Load(d.Path) + if err != nil || retained.Name != "" || m.confirmOpen || !m.draftOpen { + t.Fatal("cancel modified draft", retained, err) + } + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) + m = updated.(Model) + updated, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("new name")}) + m = updated.(Model) + updated, cmd = m.Update(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if cmd != nil || m.pendingDraft.name != "new name" { + t.Fatal("canceled input leaked into next prompt") + } +} + func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) d, err := draft.Create("wrong", "source", "ws://another-server", true, 0) @@ -131,7 +187,7 @@ func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) { m.confirmOpen = true updated, cmd = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) m = updated.(Model) - if cmd != nil || !strings.Contains(m.submitNotice, "needs a name") { + if cmd != nil || !strings.Contains(m.submitNotice, "Name must") || !strings.Contains(m.confirmView(20), "Name this pipeline") { t.Fatal("unnamed draft submitted") } } diff --git a/internal/tui/model.go b/internal/tui/model.go index 51b531fb..754038b6 100644 --- a/internal/tui/model.go +++ b/internal/tui/model.go @@ -138,6 +138,7 @@ type Model struct { mutationTitle string mutationCommand func() tea.Cmd pendingDraft *editedMsg + draftNameInput textinput.Model submitID string submitNotice string stageIndex int diff --git a/internal/tui/pipeline.go b/internal/tui/pipeline.go index a1571c27..e58f63d9 100644 --- a/internal/tui/pipeline.go +++ b/internal/tui/pipeline.go @@ -277,6 +277,7 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { if msg.String() == "esc" { m.confirmOpen = false m.keys.confirm = false + m.draftNameInput = newInput() return m, nil } if m.submitID != "" { @@ -287,6 +288,39 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { m.keys.confirm = false return m, nil } + if strings.TrimSpace(m.pendingDraft.name) == "" { + if m.draftNameInput.Prompt != "Name: " { + m.draftNameInput = newInput() + m.draftNameInput.Prompt = "Name: " + m.draftNameInput.Placeholder = "Pipeline name" + m.draftNameInput.Focus() + } + if msg.Type == tea.KeyEnter { + name := strings.TrimSpace(m.draftNameInput.Value()) + if name == "" || len(name) > 200 || strings.ContainsAny(name, "\r\n\x00") { + m.submitNotice = "Name must be 1–200 bytes without line breaks or NUL." + return m, nil + } + if m.pendingDraft.path != "" { + if err := draft.SetName(m.pendingDraft.path, name); err != nil { + m.submitNotice = "Could not save draft name: " + err.Error() + return m, nil + } + } + m.pendingDraft.name = name + for i := range m.draftItems { + if m.draftItems[i].Path == m.pendingDraft.path { + m.draftItems[i].Name = name + } + } + m.draftNameInput = newInput() + m.submitNotice = "" + return m, nil + } + var cmd tea.Cmd + m.draftNameInput, cmd = m.draftNameInput.Update(msg) + return m, cmd + } switch msg.String() { case "e": m.confirmOpen = false @@ -295,10 +329,6 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { return editDraftMode(m.pendingDraft.name, m.pendingDraft.source, true, m.pendingDraft.createOnly, draft.Draft{Path: m.pendingDraft.path, Name: m.pendingDraft.name, Server: m.url, CreateOnly: m.pendingDraft.createOnly, BaseVersion: m.pendingDraft.baseVersion}) }) case "s", "enter": - if strings.TrimSpace(m.pendingDraft.name) == "" { - m.submitNotice = "Draft needs a name. Press e and ask OpenCode to name it, or submit with CLI --name." - return m, nil - } if m.conn == nil { m.submitNotice = "Not connected; draft retained" return m, nil @@ -313,6 +343,17 @@ func (m Model) updateConfirm(msg tea.KeyMsg) (tea.Model, tea.Cmd) { } func (m Model) confirmView(height int) string { + if strings.TrimSpace(m.pendingDraft.name) == "" { + field := m.draftNameInput + if field.Prompt != "Name: " { + field = newInput() + field.Prompt = "Name: " + field.Placeholder = "Pipeline name" + field.Focus() + } + field.Width = max(1, min(60, m.width-12)) + return popup(m.width, height, 76, []string{"Name this pipeline", "", field.View(), m.submitNotice, "Enter save name and review · Esc keep draft"}) + } lines := []string{"Submit edited pipeline", m.pendingDraft.name, "", "Creates a new immutable version.", "s submit · e edit again · Esc keep draft"} if m.pendingDraft.createOnly { lines[0] = "Create pipeline"