Improve workflow UI, retain stage diffs, and add portable pipeline transfers

Make OpenCode authoring responsive and expand the managed skill references. Add conflict-aware import/export, real-provider reference workflows, and tracked V2 generation for reliable budget accounting.
This commit is contained in:
2026-10-10 20:55:42 +01:00
parent a66d540125
commit c42a19f7d3
81 changed files with 5593 additions and 539 deletions
+33 -3
View File
@@ -31,6 +31,9 @@ type App struct {
}
type options struct {
url, name, prompt, template, workspace, paths, key, opencode, model string
output string
conflict string
all bool
json, source, noOpen, yes, allow, createOnly, force, wait bool
version, expected int
timeout time.Duration
@@ -61,6 +64,14 @@ func (a App) flags(command string) (*flag.FlagSet, *options) {
}
case "show", "export", "versions":
f.IntVar(&o.version, "version", 0, "Immutable version (0 = latest)")
if command == "export" {
f.StringVar(&o.output, "output", "", "Save selected definitions as a ZIP bundle")
f.BoolVar(&o.all, "all", false, "Export all pipelines without selection")
}
case "import":
f.StringVar(&o.conflict, "on-conflict", "ask", "Different existing definitions: ask, keep, both, or import")
f.BoolVar(&o.all, "all", false, "Select every pipeline in the bundle")
f.BoolVar(&o.yes, "yes", false, "Confirm publication (schedules may activate)")
case "template":
f.BoolVar(&o.source, "source", false, "Print original Markdown instead of metadata")
case "submit":
@@ -560,7 +571,10 @@ func (a App) Run(ctx context.Context, args []string) error {
}
}
return nil
case "show", "versions", "export":
case "export":
if len(f.Args()) != 1 || o.output != "" || o.all {
return a.exportBundle(ctx, o, f.Args(), call, resolve)
}
value, err := one()
if err != nil {
return err
@@ -576,10 +590,26 @@ func (a App) Run(ctx context.Context, args []string) error {
if r.Detail == nil {
return fmt.Errorf("missing pipeline detail")
}
if command == "export" {
_, err = fmt.Fprint(a.Out, r.Detail.Source)
_, err = fmt.Fprint(a.Out, r.Detail.Source)
return err
case "import":
return a.importBundle(ctx, o, f.Args(), call)
case "show", "versions":
value, err := one()
if err != nil {
return err
}
p, err := resolve(value)
if err != nil {
return err
}
r, err := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: o.version})
if err != nil {
return err
}
if r.Detail == nil {
return fmt.Errorf("missing pipeline detail")
}
if command == "versions" {
if o.json {
return a.output(r.Detail.Versions)
+4 -1
View File
@@ -12,7 +12,8 @@ var usage = map[string]string{
"list": "list [--json]",
"show": "show <name|id> [--version N] [--json]",
"versions": "versions <name|id> [--json]",
"export": "export <name|id> [--version N]",
"export": "export [name|id ...] [--output FILE.zip] [--all] [--version N]",
"import": "import [FILE.zip] [--all] [--on-conflict ask|keep|both|import] [--yes] [--json]",
"templates": "templates [--json]",
"template": "template <key> [--source | --json]",
"run": "run [name|id] [prompt] [--json [--wait]]",
@@ -30,6 +31,8 @@ var usage = map[string]string{
}
var notes = map[string]string{
"export": "Without a name, select pipelines with Space and save a ZIP in the current directory. --output writes a portable definition bundle; --all skips selection. One name without --output prints Markdown as before. No runs, credentials or old versions are exported.",
"import": "Select definitions from a ZIP. Identical existing definitions are skipped. For differing names, choose keep existing, keep both under a new name, or use imported as a new version (history retained). --on-conflict supplies an explicit automation policy; --yes only approves final publication. Schedules may activate; no run history is imported.",
"new": "Create an optional template copy or a one-stage starter. Opens OpenCode with @pipelines; the skill asks any missing questions, including the name. No submission is performed.",
"edit": "Omit the name to open fuzzy selection. Original immutable source is copied to a private draft. OpenCode edits that draft, never the live definition.",
"validate": "Uses the same server parser as submission. Does not persist or activate schedules. Structural validity does not prove runtime/tool availability.",
+79
View File
@@ -0,0 +1,79 @@
package cli
import (
"bufio"
"fmt"
"strings"
"unicode/utf8"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/transfer"
"git.a-wan.net/alan/pipelines/internal/tui"
"github.com/pmezard/go-difflib/difflib"
)
type importPlan struct {
entry transfer.Entry
expected int
}
type importSkip struct {
Name string `json:"name"`
Reason string `json:"reason"`
}
func importCopyName(name string, reserved map[string]bool) string {
for i := 1; ; i++ {
suffix := " (imported)"
if i > 1 {
suffix = fmt.Sprintf(" (imported %d)", i)
}
base := name
if len(base)+len(suffix) > 200 {
base = base[:200-len(suffix)]
for !utf8.ValidString(base) {
base = base[:len(base)-1]
}
}
copy := base + suffix
if !reserved[copy] {
return copy
}
}
}
func (a App) chooseImportConflict(reader *bufio.Reader, e transfer.Entry, existing *protocol.PipelineDetail) (string, error) {
fmt.Fprintf(a.Out, "\nPipeline %q differs: existing v%d · imported v%d.\n", e.Name, existing.Version, e.Version)
for {
fmt.Fprint(a.Err, "[k] Keep existing · [b] Keep both (new name) · [i] Use imported (new version) · [d] View diff · [c] Cancel: ")
line, err := reader.ReadString('\n')
if err != nil && len(line) == 0 {
return "", fmt.Errorf("import canceled; choose --on-conflict for automation")
}
switch strings.ToLower(strings.TrimSpace(line)) {
case "k", "keep":
return "keep", nil
case "b", "both":
return "both", nil
case "i", "import":
return "import", nil
case "c", "cancel", "":
return "", fmt.Errorf("import canceled; no definitions published")
case "d", "diff":
if strings.Count(existing.Source, "\n")+strings.Count(e.Source, "\n") > 10000 {
fmt.Fprintln(a.Out, "Diff too large for this preview; export the existing source to compare locally.")
continue
}
patch, err := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{A: difflib.SplitLines(existing.Source), B: difflib.SplitLines(e.Source), FromFile: "existing", ToFile: "imported", Context: 3})
if err != nil {
return "", err
}
if len(patch) > 128<<10 {
patch = patch[:128<<10]
fmt.Fprintln(a.Out, "Diff preview truncated to 128 KiB.")
}
fmt.Fprintln(a.Out, tui.HighlightDiff(patch))
default:
fmt.Fprintln(a.Err, "Choose k, b, i, d, or c.")
}
}
}
+76
View File
@@ -0,0 +1,76 @@
package cli
import (
"bytes"
"context"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/transfer"
"path/filepath"
"strings"
"testing"
"unicode/utf8"
)
func TestImportUpdateRejectsConcurrentPublication(t *testing.T) {
ctx := context.Background()
s, err := pipeline.Open(filepath.Join(t.TempDir(), "race.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
old := pipeline.Template([]string{"old"}, true)
incoming := pipeline.Template([]string{"incoming"}, true)
concurrent := pipeline.Template([]string{"concurrent"}, true)
s.Submit(ctx, "work", old)
bundle := filepath.Join(t.TempDir(), "import.zip")
if err = transfer.Write(bundle, []transfer.Entry{{Name: "work", Version: 1, Source: incoming}}); err != nil {
t.Fatal(err)
}
call := func(req protocol.Message) (protocol.Message, error) {
switch req.Type {
case protocol.List:
items, err := s.List(ctx)
return protocol.Message{Pipelines: items}, err
case protocol.GetPipeline:
detail, err := s.Get(ctx, req.PipelineID, req.Version)
return protocol.Message{Detail: detail}, err
case protocol.Validate:
g, err := pipeline.Parse(req.Source)
return protocol.Message{Type: protocol.Validated, Graph: &g}, err
case protocol.Submit:
if req.ExpectedVersion != 1 || req.CreateOnly {
t.Fatal("unguarded update", req)
}
if _, err := s.Submit(ctx, "work", concurrent); err != nil {
t.Fatal(err)
}
_, err := s.SubmitChecked(ctx, req.Name, req.Source, req.CreateOnly, req.ExpectedVersion)
return protocol.Message{}, err
}
t.Fatal("unexpected request", req)
return protocol.Message{}, nil
}
var output bytes.Buffer
app := App{Out: &output, Err: &output, In: strings.NewReader("")}
err = app.importBundle(ctx, &options{all: true, yes: true, json: true, conflict: "import"}, []string{bundle}, call)
if err == nil || !strings.Contains(err.Error(), "draft conflict") {
t.Fatal("concurrent update overwritten", err)
}
items, _ := s.List(ctx)
detail, _ := s.Get(ctx, items[0].ID, 0)
if detail.Source != concurrent || detail.Version != 2 {
t.Fatal(detail)
}
}
func TestImportCopyNameFitsUnicodeLimit(t *testing.T) {
name := strings.Repeat("界", 66)
reserved := map[string]bool{}
a := importCopyName(name, reserved)
reserved[a] = true
b := importCopyName(name, reserved)
if a == b || len(a) > 200 || len(b) > 200 || !utf8.ValidString(a) || !utf8.ValidString(b) {
t.Fatal(a, b)
}
}
+239
View File
@@ -0,0 +1,239 @@
package cli
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
"git.a-wan.net/alan/pipelines/internal/transfer"
"git.a-wan.net/alan/pipelines/internal/tui"
)
func (a App) exportBundle(ctx context.Context, o *options, pos []string, call callFunc, resolve resolveFunc) error {
if o.all && len(pos) > 0 {
return fmt.Errorf("--all cannot be combined with names")
}
r, err := call(protocol.Message{Type: protocol.List})
if err != nil {
return err
}
var selected []protocol.PipelineSummary
if len(pos) > 0 {
for _, name := range pos {
p, err := resolve(name)
if err != nil {
return err
}
selected = append(selected, p)
}
} else if o.all {
selected = r.Pipelines
} else {
if o.json {
return fmt.Errorf("export --json requires names or --all")
}
selected, err = tui.PickPipelines(ctx, r.Pipelines, "Select pipelines to export")
if err != nil {
return err
}
}
if len(selected) == 0 {
return fmt.Errorf("no pipelines selected")
}
var entries []transfer.Entry
seen := map[int64]bool{}
for _, p := range selected {
if seen[p.ID] {
continue
}
seen[p.ID] = true
version := o.version
if version == 0 {
version = p.Version
}
detail, err := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: version})
if err != nil {
return err
}
if detail.Detail == nil {
return fmt.Errorf("missing definition for %q", p.Name)
}
entries = append(entries, transfer.Entry{Name: detail.Detail.Name, Version: detail.Detail.Version, Source: detail.Detail.Source})
}
filename := o.output
if filename == "" {
filename = "pipelines-export-" + time.Now().Format("20060102-150405.000000000") + ".zip"
}
filename, err = filepath.Abs(filename)
if err != nil {
return err
}
if err = transfer.Write(filename, entries); err != nil {
return err
}
if o.json {
return a.output(map[string]any{"path": filename, "pipelines": len(entries)})
}
_, err = fmt.Fprintf(a.Out, "Exported %d pipelines to %s\n", len(entries), filename)
return err
}
func (a App) importBundle(ctx context.Context, o *options, pos []string, call callFunc) error {
if o.conflict != "ask" && o.conflict != "keep" && o.conflict != "both" && o.conflict != "import" {
return fmt.Errorf("--on-conflict must be ask, keep, both, or import")
}
input := a.In
if input == nil {
input = os.Stdin
}
reader := bufio.NewReader(input)
a.In = reader
if len(pos) > 1 {
return fmt.Errorf("import accepts one ZIP path")
}
filename := ""
if len(pos) == 1 {
filename = pos[0]
} else {
if o.json {
return fmt.Errorf("import --json requires a ZIP path")
}
fmt.Fprint(a.Out, "Pipeline bundle ZIP path: ")
line, err := reader.ReadString('\n')
if err != nil && len(line) == 0 {
return fmt.Errorf("import canceled")
}
filename = strings.TrimSpace(line)
}
entries, err := transfer.Read(filename)
if err != nil {
return err
}
if !o.all {
if o.json {
return fmt.Errorf("import --json requires --all")
}
items := make([]protocol.PipelineSummary, len(entries))
for i, e := range entries {
items[i] = protocol.PipelineSummary{ID: int64(i + 1), Name: e.Name, Version: e.Version}
}
picked, err := tui.PickPipelines(ctx, items, "Select pipelines to import")
if err != nil {
return err
}
selected := map[int64]bool{}
for _, p := range picked {
selected[p.ID] = true
}
var keep []transfer.Entry
for i, e := range entries {
if selected[int64(i+1)] {
keep = append(keep, e)
}
}
entries = keep
}
// All selected sources/names are checked before any publication.
existing, err := call(protocol.Message{Type: protocol.List})
if err != nil {
return err
}
names := map[string]protocol.PipelineSummary{}
reserved := map[string]bool{}
for _, p := range existing.Pipelines {
names[p.Name] = p
reserved[p.Name] = true
}
for _, e := range entries {
reserved[e.Name] = true
}
plans := []importPlan{}
skipped := []importSkip{}
for _, e := range entries {
result, err := call(protocol.Message{Type: protocol.Validate, Source: e.Source})
if err != nil {
return fmt.Errorf("validate %q: %w", e.Name, err)
}
if result.Type != protocol.Validated || result.Graph == nil {
return fmt.Errorf("pipeline %q failed validation", e.Name)
}
plan := importPlan{entry: e}
if p, exists := names[e.Name]; exists {
r, err := call(protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: p.Version})
if err != nil {
return err
}
if r.Detail == nil {
return fmt.Errorf("missing existing definition for %q", e.Name)
}
if r.Detail.Source == e.Source {
skipped = append(skipped, importSkip{Name: e.Name, Reason: "identical"})
continue
}
choice := o.conflict
if choice == "ask" {
if o.json {
return fmt.Errorf("pipeline %q differs from existing v%d; choose --on-conflict keep, both, or import (no changes published)", e.Name, r.Detail.Version)
}
choice, err = a.chooseImportConflict(reader, e, r.Detail)
if err != nil {
return err
}
}
switch choice {
case "keep":
skipped = append(skipped, importSkip{Name: e.Name, Reason: "kept-existing"})
continue
case "both":
plan.entry.Name = importCopyName(e.Name, reserved)
reserved[plan.entry.Name] = true
case "import":
plan.expected = r.Detail.Version
}
}
plans = append(plans, plan)
}
if !o.json {
for _, s := range skipped {
fmt.Fprintf(a.Out, "Skipped %q · %s\n", s.Name, s.Reason)
}
for _, plan := range plans {
action := "new pipeline v1"
if plan.expected > 0 {
action = fmt.Sprintf("update v%d → v%d (history retained)", plan.expected, plan.expected+1)
}
fmt.Fprintf(a.Out, " %q · exported v%d → %s\n", plan.entry.Name, plan.entry.Version, action)
}
}
if len(plans) > 0 {
if err = a.confirmAction(fmt.Sprintf("Publish %d imported definitions? Schedules in their definitions will activate; existing history is retained.", len(plans)), o.yes); err != nil {
return err
}
}
imported := []string{}
updated := []string{}
for _, plan := range plans {
e := plan.entry
_, err := call(protocol.Message{Type: protocol.Submit, Name: e.Name, Source: e.Source, CreateOnly: plan.expected == 0, ExpectedVersion: plan.expected})
if err != nil {
if o.json {
_ = a.output(map[string]any{"imported": imported, "updated": updated, "skipped": skipped, "failed": e.Name})
}
return fmt.Errorf("import stopped at %q after publishing %d definitions (%v): %w; accepted definitions were retained", e.Name, len(imported), imported, err)
}
imported = append(imported, e.Name)
if plan.expected > 0 {
updated = append(updated, e.Name)
}
}
if o.json {
return a.output(map[string]any{"imported": imported, "updated": updated, "skipped": skipped})
}
_, err = fmt.Fprintf(a.Out, "Published %d definitions (%d updates, %d skipped): %v\n", len(imported), len(updated), len(skipped), imported)
return err
}
+207
View File
@@ -0,0 +1,207 @@
package cli_test
import (
"bytes"
"context"
"git.a-wan.net/alan/pipelines/internal/cli"
"git.a-wan.net/alan/pipelines/internal/pipeline"
"git.a-wan.net/alan/pipelines/internal/transfer"
"path/filepath"
"strings"
"testing"
)
func TestExportImportAcrossServersCreateOnly(t *testing.T) {
ctx := context.Background()
sourceRun, source, _ := setup(t)
destRun, dest, _ := setup(t)
for _, name := range []string{"alpha", "beta"} {
if _, err := source.Submit(ctx, name, pipeline.Template([]string{"one"}, true)); err != nil {
t.Fatal(err)
}
}
updated := pipeline.Template([]string{"changed", "review"}, true)
if _, err := source.Submit(ctx, "alpha", updated); err != nil {
t.Fatal(err)
}
bundle := filepath.Join(t.TempDir(), "pipelines.zip")
if _, err := sourceRun("export", "alpha", "beta", "--output", bundle, "--json"); err != nil {
t.Fatal(err)
}
entries, err := transfer.Read(bundle)
if err != nil || len(entries) != 2 || entries[0].Version != 2 || entries[0].Source != updated {
t.Fatal(entries, err)
}
text, err := sourceRun("export", "alpha", "--version", "1")
if err != nil || text != pipeline.Template([]string{"one"}, true) {
t.Fatal("legacy Markdown export broken", text, err)
}
if _, err = destRun("import", bundle, "--all", "--json"); err == nil {
t.Fatal("import bypassed confirmation")
}
items, _ := dest.List(ctx)
if len(items) != 0 {
t.Fatal("canceled import changed destination")
}
output, err := destRun("import", bundle, "--all", "--yes", "--json")
if err != nil || !strings.Contains(output, "alpha") {
t.Fatal(output, err)
}
items, _ = dest.List(ctx)
if len(items) != 2 {
t.Fatal(items)
}
for _, item := range items {
if item.Version != 1 {
t.Fatal("source version incorrectly used on destination", item)
}
if item.Name == "alpha" {
detail, _ := dest.Get(ctx, item.ID, 1)
if detail.Source != updated {
t.Fatal("source altered", detail)
}
}
}
if output, err = destRun("import", bundle, "--all", "--yes", "--json"); err != nil || !strings.Contains(output, "identical") {
t.Fatal("identical import not skipped", output, err)
}
items, _ = dest.List(ctx)
for _, item := range items {
if item.Version != 1 {
t.Fatal("duplicate import changed history")
}
}
if _, err = sourceRun("export", "--all", "--output", bundle, "--json"); err == nil {
t.Fatal("export overwrote existing file")
}
}
func TestImportConflictPoliciesPreserveHistory(t *testing.T) {
for _, policy := range []string{"ask", "keep", "both", "import"} {
t.Run(policy, func(t *testing.T) {
ctx := context.Background()
run, store, _ := setup(t)
old := pipeline.Template([]string{"old"}, true)
incoming := pipeline.Template([]string{"new"}, true)
if _, err := store.Submit(ctx, "work", old); err != nil {
t.Fatal(err)
}
// Keep-both must avoid both destination names and names elsewhere in this bundle.
if _, err := store.Submit(ctx, "work (imported)", old); err != nil {
t.Fatal(err)
}
file := filepath.Join(t.TempDir(), "conflict.zip")
if err := transfer.Write(file, []transfer.Entry{{Name: "work", Version: 7, Source: incoming}, {Name: "work (imported 2)", Version: 1, Source: incoming}}); err != nil {
t.Fatal(err)
}
output, err := run("import", file, "--all", "--yes", "--json", "--on-conflict", policy)
if policy == "ask" {
if err == nil || !strings.Contains(err.Error(), "differs") {
t.Fatal("implicit update allowed", output, err)
}
items, _ := store.List(ctx)
if len(items) != 2 {
t.Fatal("conflict published other entries")
}
return
}
if err != nil {
t.Fatal(output, err)
}
items, _ := store.List(ctx)
expectedCount := 3
if policy == "both" {
expectedCount = 4
}
if len(items) != expectedCount {
t.Fatal(items)
}
for _, p := range items {
if p.Name == "work" {
detail, _ := store.Get(ctx, p.ID, 0)
want := old
version := 1
if policy == "import" {
want = incoming
version = 2
}
if detail.Source != want || detail.Version != version {
t.Fatal("wrong retained choice", detail)
}
historical, _ := store.Get(ctx, p.ID, 1)
if historical.Source != old {
t.Fatal("history overwritten")
}
}
if p.Name == "work (imported 3)" {
detail, _ := store.Get(ctx, p.ID, 0)
if detail.Source != incoming || detail.Version != 1 {
t.Fatal(detail)
}
}
}
if policy == "both" && !strings.Contains(output, "work (imported 3)") {
t.Fatal("copy name not reported", output)
}
})
}
}
func TestInteractiveImportConflictDiffAndConfirmation(t *testing.T) {
for _, input := range []string{"d\ni\ny\n", "b\ny\n", "k\n", "i\nno\n", "c\n"} {
t.Run(strings.ReplaceAll(input, "\n", "-"), func(t *testing.T) {
_, store, url := setup(t)
ctx := context.Background()
old := pipeline.Template([]string{"old"}, true)
incoming := pipeline.Template([]string{"new"}, true)
store.Submit(ctx, "work", old)
file := filepath.Join(t.TempDir(), "conflict.zip")
if err := transfer.Write(file, []transfer.Entry{{Name: "work", Version: 4, Source: incoming}}); err != nil {
t.Fatal(err)
}
var out, stderr bytes.Buffer
err := (cli.App{Out: &out, Err: &stderr, In: strings.NewReader(input)}).Run(ctx, []string{"--url", url, "import", file, "--all"})
if input == "c\n" || input == "i\nno\n" {
if err == nil {
t.Fatal("cancellation bypassed")
}
} else if err != nil {
t.Fatal(out.String(), stderr.String(), err)
}
items, _ := store.List(ctx)
for _, p := range items {
if p.Name == "work" {
detail, _ := store.Get(ctx, p.ID, 0)
want := old
if strings.HasPrefix(input, "d") {
want = incoming
}
if detail.Source != want {
t.Fatal("wrong conflict choice", detail)
}
}
}
if strings.HasPrefix(input, "b") && len(items) != 2 {
t.Fatal("both did not create a copy", items)
}
if strings.HasPrefix(input, "d") && (!strings.Contains(out.String(), "-# stage old") || !strings.Contains(out.String(), "+# stage new")) {
t.Fatal("conflict diff missing", out.String())
}
})
}
}
func TestImportValidatesEntireSelectionBeforePublishing(t *testing.T) {
run, store, _ := setup(t)
bundle := filepath.Join(t.TempDir(), "bad.zip")
if err := transfer.Write(bundle, []transfer.Entry{{Name: "valid", Version: 1, Source: pipeline.Template([]string{"one"}, true)}, {Name: "invalid", Version: 1, Source: "not a pipeline"}}); err != nil {
t.Fatal(err)
}
if _, err := run("import", bundle, "--all", "--yes", "--json"); err == nil {
t.Fatal("invalid import accepted")
}
items, _ := store.List(context.Background())
if len(items) != 0 {
t.Fatal("preflight published partial selection", items)
}
}