From bba6827ec261ee1cd2480f02670fa949cb087c94 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Wed, 22 Jul 2026 03:50:00 +1000 Subject: [PATCH] feat: build workflows from skill stage plugins MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit New skillbuild package: BuildWorkflow() loads Yaegi-interpreted stage plugins from a skill's plugins/ directory and constructs a runnable workflow.Workflow. Uses wfextract symbol tables for type access. A skill directory with metadata.archie.workflow: greet and stage plugins in plugins/*.go IS the workflow definition — no Go code changes needed. Stage plugins export: func Stage() workflow.Stage or the fallback signature: func Stage() (string, func(context.Context, *workflow.TaskContext) error) Stages are loaded in filename order. Missing plugins dir is not an error (matches wfeval.Discover pattern). Test: skill with two plugins (hello, world) produces a 2-stage workflow that modifies BuildSummary sequentially. --- internal/workflow/skillbuild/skillbuild.go | 95 ++++++++++++ .../workflow/skillbuild/skillbuild_test.go | 144 ++++++++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 internal/workflow/skillbuild/skillbuild.go create mode 100644 internal/workflow/skillbuild/skillbuild_test.go diff --git a/internal/workflow/skillbuild/skillbuild.go b/internal/workflow/skillbuild/skillbuild.go new file mode 100644 index 000000000..c1945af6d --- /dev/null +++ b/internal/workflow/skillbuild/skillbuild.go @@ -0,0 +1,95 @@ +// Package skillbuild constructs Workflows from skill catalog entries by +// loading Yaegi-interpreted stage plugins from the skill's plugins/ directory. +package skillbuild + +import ( + "context" + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "github.com/traefik/yaegi/interp" + "github.com/traefik/yaegi/stdlib" + + "github.com/samcharles93/archie-core/internal/workflow" + "github.com/samcharles93/archie-core/internal/workflow/wfextract" +) + +// SkillWorkflow is the subset of a catalog entry needed to build a +// workflow from a skill's stage plugins. +type SkillWorkflow struct { + Workflow string // from metadata.archie.workflow + Dir string // skill directory name +} + +// BuildWorkflow loads stage plugins from a skill's plugins/ directory +// and returns a workflow.Workflow. Stages are Yaegi-interpreted .go files +// sorted by filename. Each file must export: +// +// func Stage() workflow.Stage +func BuildWorkflow(worktree string, entry SkillWorkflow) (workflow.Workflow, error) { + pluginsDir := filepath.Join(worktree, ".agents", "skills", entry.Dir, "plugins") + entries, err := os.ReadDir(pluginsDir) + if os.IsNotExist(err) { + return workflow.Workflow{Name: entry.Workflow}, nil + } + if err != nil { + return workflow.Workflow{}, fmt.Errorf("read plugins dir %s: %w", pluginsDir, err) + } + + var names []string + for _, e := range entries { + if !e.IsDir() && strings.HasSuffix(e.Name(), ".go") { + names = append(names, e.Name()) + } + } + sort.Strings(names) + + stages := make([]workflow.Stage, 0, len(names)) + for _, name := range names { + path := filepath.Join(pluginsDir, name) + src, err := os.ReadFile(path) + if err != nil { + return workflow.Workflow{}, fmt.Errorf("read plugin %s: %w", path, err) + } + stage, err := loadStage(name, string(src)) + if err != nil { + return workflow.Workflow{}, fmt.Errorf("plugin %s: %w", name, err) + } + stages = append(stages, stage) + } + + return workflow.Workflow{Name: entry.Workflow, Stages: stages}, nil +} + +// loadStage interprets a single .go plugin source via Yaegi, using the +// wfextract symbol table so plugins can reference workflow types. +func loadStage(filename string, src string) (workflow.Stage, error) { + i := interp.New(interp.Options{}) + if err := i.Use(stdlib.Symbols); err != nil { + return workflow.Stage{}, fmt.Errorf("stdlib: %w", err) + } + if err := i.Use(wfextract.Symbols); err != nil { + return workflow.Stage{}, fmt.Errorf("wfextract: %w", err) + } + if _, err := i.Eval(src); err != nil { + return workflow.Stage{}, fmt.Errorf("eval: %w", err) + } + v, err := i.Eval("main.Stage") + if err != nil { + return workflow.Stage{}, fmt.Errorf("resolve Stage: %w", err) + } + fn, ok := v.Interface().(func() workflow.Stage) + if !ok { + // Fallback for test plugins: func() (string, func(...) error) + rawFn, rawOk := v.Interface().(func() (string, func(context.Context, *workflow.TaskContext) error)) + if rawOk { + name, run := rawFn() + return workflow.Stage{Name: name, Run: run}, nil + } + return workflow.Stage{}, fmt.Errorf("Stage is %T, want func() workflow.Stage", v.Interface()) + } + return fn(), nil +} diff --git a/internal/workflow/skillbuild/skillbuild_test.go b/internal/workflow/skillbuild/skillbuild_test.go new file mode 100644 index 000000000..b54a988b6 --- /dev/null +++ b/internal/workflow/skillbuild/skillbuild_test.go @@ -0,0 +1,144 @@ +package skillbuild + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/samcharles93/archie-core/internal/workflow" +) + +// ── workflows built from skill plugins ──────────────────────────────── + +func TestBuildWorkflowFromSkillPlugins(t *testing.T) { + dir := t.TempDir() + skillDir := filepath.Join(dir, ".agents", "skills", "archie-wf-greet") + pluginsDir := filepath.Join(skillDir, "plugins") + if err := os.MkdirAll(pluginsDir, 0o755); err != nil { + t.Fatal(err) + } + + if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`--- +name: archie-wf-greet +description: A custom greeting workflow built entirely from plugins. +version: 1.0.0 +metadata: + archie: + workflow: greet +--- +Greet the world, then say goodbye. +`), 0o644); err != nil { + t.Fatal(err) + } + + if err := os.WriteFile(filepath.Join(pluginsDir, "01-hello.go"), []byte(`package main + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/workflow" +) + +func Stage() (string, func(context.Context, *workflow.TaskContext) error) { + return "hello", func(ctx context.Context, tc *workflow.TaskContext) error { + tc.BuildSummary = "hello from plugin" + return nil + } +} +`), 0o644); err != nil { + t.Fatal(err) + } + + if err := os.WriteFile(filepath.Join(pluginsDir, "02-world.go"), []byte(`package main + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/workflow" +) + +func Stage() (string, func(context.Context, *workflow.TaskContext) error) { + return "world", func(ctx context.Context, tc *workflow.TaskContext) error { + tc.BuildSummary = tc.BuildSummary + " world" + return nil + } +} +`), 0o644); err != nil { + t.Fatal(err) + } + + entry := SkillWorkflow{Workflow: "greet", Dir: "archie-wf-greet"} + wf, err := BuildWorkflow(dir, entry) + if err != nil { + t.Fatal(err) + } + if wf.Name != "greet" { + t.Errorf("Workflow.Name = %q, want greet", wf.Name) + } + if len(wf.Stages) != 2 { + t.Fatalf("got %d stages, want 2", len(wf.Stages)) + } + if wf.Stages[0].Name != "hello" { + t.Errorf("stage[0].Name = %q, want hello", wf.Stages[0].Name) + } + if wf.Stages[1].Name != "world" { + t.Errorf("stage[1].Name = %q, want world", wf.Stages[1].Name) + } + + // Run the workflow — stages must execute in order. + tc := &workflow.TaskContext{BuildSummary: ""} + for _, s := range wf.Stages { + if err := s.Run(context.Background(), tc); err != nil { + t.Fatalf("stage %s failed: %v", s.Name, err) + } + } + if tc.BuildSummary != "hello from plugin world" { + t.Errorf("BuildSummary = %q, want 'hello from plugin world'", tc.BuildSummary) + } +} + +func TestBuildWorkflowMissingSkillReturnsEmptyWorkflow(t *testing.T) { + // Missing plugins directory is not an error — same pattern as + // wfeval.Discover. Returns a Workflow with no stages. + entry := SkillWorkflow{Workflow: "nonexistent", Dir: "nonexistent"} + wf, err := BuildWorkflow(t.TempDir(), entry) + if err != nil { + t.Fatal(err) + } + if len(wf.Stages) != 0 { + t.Errorf("got %d stages, want 0 for nonexistent skill", len(wf.Stages)) + } +} + +func TestBuildWorkflowNoStagesReturnsEmptyWorkflow(t *testing.T) { + dir := t.TempDir() + skillDir := filepath.Join(dir, ".agents", "skills", "archie-wf-empty") + if err := os.MkdirAll(skillDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(skillDir, "SKILL.md"), []byte(`--- +name: archie-wf-empty +description: No stages +version: 1.0.0 +metadata: + archie: + workflow: empty +--- +Body. +`), 0o644); err != nil { + t.Fatal(err) + } + + entry := SkillWorkflow{Workflow: "empty", Dir: "archie-wf-empty"} + wf, err := BuildWorkflow(dir, entry) + if err != nil { + t.Fatal(err) + } + if len(wf.Stages) != 0 { + t.Errorf("got %d stages, want 0", len(wf.Stages)) + } + if wf.Name != "empty" { + t.Errorf("Workflow.Name = %q, want empty", wf.Name) + } +}