From 4cae37f9501b194b46c2e7c7dcded11a8cbd4c55 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 10 Oct 2026 02:18:48 +0100 Subject: [PATCH] Allow portable drafts to publish to the connected server --- README.md | 4 +- docs/cli.md | 10 ++- docs/installation-verification.md | 3 +- internal/cli/app.go | 16 +++-- internal/cli/app_test.go | 4 +- internal/cli/portable_draft_test.go | 82 +++++++++++++++++++++++++ internal/draft/draft.go | 11 ++++ internal/draft/portable_test.go | 23 +++++++ internal/tui/drafts.go | 7 +-- internal/tui/drafts_test.go | 6 +- internal/tui/pipeline.go | 1 + internal/tui/portable_draft_test.go | 95 +++++++++++++++++++++++++++++ 12 files changed, 242 insertions(+), 20 deletions(-) create mode 100644 internal/cli/portable_draft_test.go create mode 100644 internal/draft/portable_test.go create mode 100644 internal/tui/portable_draft_test.go diff --git a/README.md b/README.md index 72b0b0ec..50df1865 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,9 @@ commands support `--json` for agents and scripts. In the TUI, press **d** (or use **:drafts**) to browse retained drafts, including CLI-created ones. **e** reopens a draft, **v** validates it, and **s** opens explicit submission confirmation, asking for a name first if needed. Drafts remain retained -after submission. +after submission. Drafts are portable: submit to the currently connected server. +When that differs from the origin, submission creates a new pipeline rather than +applying the origin's version number to an existing destination pipeline. From a run, press **P** / **:pipeline** to open the pipeline version it used. Initial run input also records `pwd`. Press **r** / **:retry** on a failed run diff --git a/docs/cli.md b/docs/cli.md index 75f4a38c..d39182e4 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -84,8 +84,14 @@ publishes it. CLI `pipelines new` retains its existing direct-OpenCode behavior; Press `d` on the dashboard or use `:drafts` to browse the same retained drafts as `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. For unnamed drafts, submission asks you to +`r` refreshes the list. Drafts can be validated, edited, and submitted to whichever +server you are connected to. Same-server submissions preserve create-only/version +guards. A different destination imports the draft as a new pipeline, ignoring +the origin's version number and refusing an existing destination name; the +confirmation shows the destination. Retained source and origin metadata stay +unchanged. For an edit draft, CLI `submit --expected-version N` can explicitly +approve an update against version N on the destination (or `--force` can explicitly +skip that version check). 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. diff --git a/docs/installation-verification.md b/docs/installation-verification.md index 7188db93..4a345a09 100644 --- a/docs/installation-verification.md +++ b/docs/installation-verification.md @@ -74,7 +74,8 @@ Authoring regression tests exercise idempotent skill installation, preservation user modifications, one direct OpenCode launch with a short skill/context reference, and an unnamed new draft without CLI questions. TUI integration checks browse a CLI-style retained draft, validate through the server, require explicit submission -confirmation, preserve create-only guards, and reject a different server. +confirmation, preserve create-only guards, and allow portable drafts to be imported +on a different server without inheriting the origin's version number. An isolated installed-binary PTY check also opened a CLI-created draft in the TUI, reopened the same file through a mock OpenCode executable, and explicitly submitted diff --git a/internal/cli/app.go b/internal/cli/app.go index 61311718..ee204ede 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -465,9 +465,9 @@ func (a App) Run(ctx context.Context, args []string) error { if metaErr != nil && !os.IsNotExist(metaErr) { return metaErr } - if d.Server != "" && d.Server != o.url { - return fmt.Errorf("draft belongs to server %s; select it with --url", d.Server) - } + origin := d + crossServer := d.Server != "" && d.Server != o.url + d = d.ForServer(o.url) name := o.name if name == "" { name = d.Name @@ -482,8 +482,14 @@ func (a App) Run(ctx context.Context, args []string) error { if o.force { expected = 0 } - request = protocol.Message{Type: protocol.Submit, Name: name, Source: source, CreateOnly: d.CreateOnly || o.createOnly, ExpectedVersion: expected} - if err := a.confirmAction("Publish "+name+" as an immutable version? Included schedules activate on acceptance.", o.yes); err != nil { + createOnly := d.CreateOnly || o.createOnly + // An explicit destination version (or force) permits intentional + // updates from an edit draft; never inherit the origin version. + if crossServer && (o.expected > 0 || o.force) { + createOnly = origin.CreateOnly || o.createOnly + } + request = protocol.Message{Type: protocol.Submit, Name: name, Source: source, CreateOnly: createOnly, ExpectedVersion: expected} + if err := a.confirmAction("Publish "+name+" to "+o.url+" as an immutable version? Included schedules activate on acceptance.", o.yes); err != nil { return err } } diff --git a/internal/cli/app_test.go b/internal/cli/app_test.go index e16cc2d9..ff178bf0 100644 --- a/internal/cli/app_test.go +++ b/internal/cli/app_test.go @@ -214,13 +214,13 @@ func TestCLIAuthorOpenCodeReferencesGlobalSkillAndLeavesDraft(t *testing.T) { } } -func TestCLIBoundedFilesServerMismatchAndNoninteractiveGuards(t *testing.T) { +func TestCLIBoundedFilesPortableDraftsAndNoninteractiveGuards(t *testing.T) { run, _, url := setup(t) d, err := draft.Create("test", pipeline.Template([]string{"worker"}, true), url+"-other", true, 0) if err != nil { t.Fatal(err) } - if _, err = run("submit", d.Path); err == nil || !strings.Contains(err.Error(), "belongs") { + if _, err = run("submit", d.Path); err != nil { t.Fatal(err) } os.WriteFile(d.Path, bytes.Repeat([]byte("x"), (1<<20)+1), 0600) diff --git a/internal/cli/portable_draft_test.go b/internal/cli/portable_draft_test.go new file mode 100644 index 00000000..302cd2e8 --- /dev/null +++ b/internal/cli/portable_draft_test.go @@ -0,0 +1,82 @@ +package cli_test + +import ( + "context" + "fmt" + "os" + "testing" + + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" +) + +func TestCLIEditDraftCanPublishToAnotherServerWithoutOriginVersion(t *testing.T) { + _, origin, originURL := setup(t) + run, destination, _ := setup(t) + ctx := context.Background() + source := pipeline.Template([]string{"worker"}, true) + for i := 0; i < 7; i++ { + if _, err := origin.Submit(ctx, "portable", pipeline.Template([]string{fmt.Sprintf("origin_v%d", i+1)}, true)); err != nil { + t.Fatal(err) + } + } + d, err := draft.Create("portable", source, originURL, false, 7) + if err != nil { + t.Fatal(err) + } + metadata, err := os.ReadFile(d.Path + ".json") + if err != nil { + t.Fatal(err) + } + if _, err := run("submit", d.Path); err != nil { + t.Fatal("cross-server publication blocked", err) + } + items, err := destination.List(ctx) + if err != nil || len(items) != 1 || items[0].Version != 1 { + t.Fatal("origin base version leaked to destination", items, err) + } + updated := pipeline.Template([]string{"worker", "reviewer"}, true) + if err := os.WriteFile(d.Path, []byte(updated), 0600); err != nil { + t.Fatal(err) + } + if _, err := run("submit", d.Path); err == nil { + t.Fatal("import overwrote existing destination name without approval") + } + items, _ = destination.List(ctx) + if items[0].Version != 1 { + t.Fatal("destination collision changed published history") + } + if _, err := run("submit", d.Path, "--expected-version", "1"); err != nil { + t.Fatal("explicit destination version update blocked", err) + } + items, _ = destination.List(ctx) + if items[0].Version != 2 { + t.Fatal("explicit destination update did not publish a version", items) + } + updated = pipeline.Template([]string{"worker", "reviewer", "publisher"}, true) + if err := os.WriteFile(d.Path, []byte(updated), 0600); err != nil { + t.Fatal(err) + } + if _, err := run("submit", d.Path, "--expected-version", "99"); err == nil { + t.Fatal("wrong explicit destination version accepted") + } + if _, err := run("submit", d.Path, "--force"); err != nil { + t.Fatal("explicit forced destination update blocked", err) + } + items, _ = destination.List(ctx) + if items[0].Version != 3 { + t.Fatal("forced destination update did not publish a version", items) + } + items, _ = origin.List(ctx) + if len(items) != 1 || items[0].Version != 7 { + t.Fatal("origin pipeline was modified", items) + } + retained, err := os.ReadFile(d.Path + ".json") + if err != nil || string(retained) != string(metadata) { + t.Fatal("import rewrote original draft metadata", err) + } + text, err := draft.Read(d.Path) + if err != nil || text != updated { + t.Fatal("publication changed retained source", err) + } +} diff --git a/internal/draft/draft.go b/internal/draft/draft.go index 5d1bf832..c1a381ca 100644 --- a/internal/draft/draft.go +++ b/internal/draft/draft.go @@ -19,6 +19,17 @@ type Draft struct { BaseVersion int `json:"base_version,omitempty"` } +// ForServer projects publication scope onto a destination without changing the +// retained draft. Version numbers belong to the origin, not another server. +func (d Draft) ForServer(server string) Draft { + if d.Server != "" && d.Server != server { + d.CreateOnly = true + d.BaseVersion = 0 + } + d.Server = server + return d +} + func Directory() (string, error) { if dir := os.Getenv("PIPELINES_DRAFT_DIR"); dir != "" { return filepath.Abs(dir) diff --git a/internal/draft/portable_test.go b/internal/draft/portable_test.go new file mode 100644 index 00000000..d20125fe --- /dev/null +++ b/internal/draft/portable_test.go @@ -0,0 +1,23 @@ +package draft + +import "testing" + +func TestPublicationScopeForServer(t *testing.T) { + original := Draft{Path: "/retained/draft.md", Name: "demo", Server: "ws://origin/connect", BaseVersion: 7} + same := original.ForServer(original.Server) + if same != original { + t.Fatal("same-server version guard changed", same) + } + imported := original.ForServer("ws://destination/connect") + if imported.Server != "ws://destination/connect" || imported.BaseVersion != 0 || !imported.CreateOnly || imported.Path != original.Path || imported.Name != original.Name { + t.Fatal("cross-server scope leaked or draft identity changed", imported) + } + if original.Server != "ws://origin/connect" || original.BaseVersion != 7 || original.CreateOnly { + t.Fatal("projection changed retained draft") + } + legacy := Draft{Name: "legacy", BaseVersion: 2} + bound := legacy.ForServer("ws://destination/connect") + if bound.BaseVersion != 2 || bound.CreateOnly { + t.Fatal("unbound legacy draft lost its version guard", bound) + } +} diff --git a/internal/tui/drafts.go b/internal/tui/drafts.go index c286303e..ec01d0c7 100644 --- a/internal/tui/drafts.go +++ b/internal/tui/drafts.go @@ -56,12 +56,7 @@ func (m Model) selectedDraft() (draft.Draft, string, error) { if err != nil { return d, "", err } - if d.Server != "" && d.Server != m.url { - return d, "", fmt.Errorf("draft belongs to a different server: %s", d.Server) - } - if d.Server == "" { - d.Server = m.url - } + d = d.ForServer(m.url) source, err := draft.Read(d.Path) return d, source, err } diff --git a/internal/tui/drafts_test.go b/internal/tui/drafts_test.go index ee67a416..c899683a 100644 --- a/internal/tui/drafts_test.go +++ b/internal/tui/drafts_test.go @@ -169,7 +169,7 @@ func TestDraftNamePromptCancelAndInvalidName(t *testing.T) { } } -func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) { +func TestDraftBrowserAcceptsOtherServerAndKeepsUnnamedDraft(t *testing.T) { t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) d, err := draft.Create("wrong", "source", "ws://another-server", true, 0) if err != nil { @@ -180,8 +180,8 @@ func TestDraftBrowserRejectsWrongServerAndKeepsUnnamedDraft(t *testing.T) { m = updated.(Model) updated, cmd := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) m = updated.(Model) - if cmd != nil || m.confirmOpen || !strings.Contains(m.draftError, "different server") { - t.Fatal("server affinity bypassed", d, m.draftError) + if cmd != nil || !m.confirmOpen || m.draftError != "" || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 { + t.Fatal("portable draft was blocked or kept origin version scope", d, m.draftError) } m.pendingDraft = &editedMsg{path: d.Path, name: "", source: "source", createOnly: true} m.confirmOpen = true diff --git a/internal/tui/pipeline.go b/internal/tui/pipeline.go index e58f63d9..8d8cb42e 100644 --- a/internal/tui/pipeline.go +++ b/internal/tui/pipeline.go @@ -359,6 +359,7 @@ func (m Model) confirmView(height int) string { lines[0] = "Create pipeline" lines[3] = "Creates a NEW pipeline only when you submit." } + lines = append(lines, "Destination: "+m.url) if strings.Contains(m.pendingDraft.source, "```schedule") { lines = append(lines, "WARNING: submitting activates the included schedule.") } diff --git a/internal/tui/portable_draft_test.go b/internal/tui/portable_draft_test.go new file mode 100644 index 00000000..659c6e64 --- /dev/null +++ b/internal/tui/portable_draft_test.go @@ -0,0 +1,95 @@ +package tui + +import ( + "context" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/draft" + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/server" + tea "github.com/charmbracelet/bubbletea" +) + +func TestTUIEditDraftPublishesToConnectedServerAsNewPipeline(t *testing.T) { + t.Setenv("PIPELINES_DRAFT_DIR", t.TempDir()) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + store, err := pipeline.Open(filepath.Join(t.TempDir(), "destination.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + srv := httptest.NewServer(server.Handler(ctx, store)) + defer srv.Close() + url := "ws" + strings.TrimPrefix(srv.URL, "http") + "/connect" + source := pipeline.Template([]string{"worker"}, true) + d, err := draft.Create("portable", source, "ws://127.0.0.1:8080/connect", false, 7) + if err != nil { + t.Fatal(err) + } + metadata, err := os.ReadFile(d.Path + ".json") + if err != nil { + t.Fatal(err) + } + m := New(ctx, url) + connected := m.connect()().(connectedMsg) + defer connected.conn.CloseNow() + updated, read := m.Update(connected) + m = updated.(Model) + updated, _ = m.Update(read()) + m = updated.(Model) + updated, _ = m.openDrafts() + m = updated.(Model) + updated, validate := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'v'}}) + m = updated.(Model) + updated, _ = m.Update(validate()) + m = updated.(Model) + if !strings.Contains(m.draftError, "Validation passed") { + t.Fatal("cross-server validation blocked", m.draftError) + } + updated, send := m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) + m = updated.(Model) + items, _ := store.List(ctx) + if send != nil || !m.confirmOpen || !m.pendingDraft.createOnly || m.pendingDraft.baseVersion != 0 || len(items) != 0 || !strings.Contains(m.confirmView(20), url) { + t.Fatal("portable draft bypassed review or retained origin scope", m.pendingDraft, m.draftError) + } + updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + if send == nil { + t.Fatal("publication blocked") + } + if _, ok := send().(sentMsg); !ok { + t.Fatal("publication send failed") + } + updated, _ = m.Update(m.read()()) + m = updated.(Model) + items, _ = store.List(ctx) + if len(items) != 1 || items[0].Version != 1 || items[0].Name != "portable" || m.confirmOpen { + t.Fatal("destination publication failed", items, m.submitNotice) + } + updated, _ = m.openDrafts() + m = updated.(Model) + updated, _ = m.updateDrafts(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'s'}}) + m = updated.(Model) + updated, send = m.updateConfirm(tea.KeyMsg{Type: tea.KeyEnter}) + m = updated.(Model) + send() + updated, _ = m.Update(m.read()()) + m = updated.(Model) + if !strings.Contains(m.submitNotice, "already exists") { + t.Fatal("destination collision not guarded", m.submitNotice) + } + retained, err := os.ReadFile(d.Path + ".json") + if err != nil || string(retained) != string(metadata) { + t.Fatal("publication changed origin metadata", err) + } + text, err := draft.Read(d.Path) + if err != nil || text != source { + t.Fatal("publication changed source", err) + } +}