Implements the full PRD (docs/prds/yaegi-plugin-system.md): repos can now define .archie/gate.go for diff-inspecting quality rules shell commands can't express, .archie/stages/*.go for custom pipeline steps, and skills can ship Go scripts run via a new run_go_script agent tool — all interpreted in-process with Yaegi, with panics recovered so a bad script can't take down the daemon. Hot reload (phase 4) needed no code: every loader reads from disk per call and every task gets a fresh clone, so nothing is ever cached to go stale.
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
// Package wfeval discovers and interprets a repo's custom workflow
|
|
// stages: .archie/stages/*.go files, each exporting a Stage() function
|
|
// that returns a workflow.Stage. Split from internal/workflow (and its
|
|
// generated symbol table in wfextract) to avoid the import cycle that
|
|
// would result from workflow depending on its own extracted symbols.
|
|
package wfeval
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
|
|
"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"
|
|
)
|
|
|
|
// stagesDir is where a repo's custom stage scripts live, relative to
|
|
// the worktree root.
|
|
const stagesDir = ".archie/stages"
|
|
|
|
// Discover loads every .archie/stages/*.go file in dir and returns the
|
|
// workflow.Stage each exports, in filename order. A missing directory is
|
|
// not an error — it returns (nil, nil), meaning no custom stages are
|
|
// configured for this repo.
|
|
func Discover(dir string) ([]workflow.Stage, error) {
|
|
entries, err := os.ReadDir(filepath.Join(dir, stagesDir))
|
|
if err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil, nil
|
|
}
|
|
return nil, fmt.Errorf("read %s: %w", stagesDir, err)
|
|
}
|
|
|
|
var names []string
|
|
for _, e := range entries {
|
|
if !e.IsDir() && filepath.Ext(e.Name()) == ".go" {
|
|
names = append(names, e.Name())
|
|
}
|
|
}
|
|
sort.Strings(names)
|
|
|
|
stages := make([]workflow.Stage, 0, len(names))
|
|
for _, name := range names {
|
|
stage, err := loadStage(filepath.Join(dir, stagesDir, name))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", filepath.Join(stagesDir, name), err)
|
|
}
|
|
stages = append(stages, stage)
|
|
}
|
|
return stages, nil
|
|
}
|
|
|
|
// loadStage interprets one custom stage script and calls its exported
|
|
// Stage() function. The script runs in-process (interpreted, not
|
|
// sandboxed), so a panic inside it is recovered and returned as an error
|
|
// rather than taking down the daemon.
|
|
func loadStage(path string) (stage workflow.Stage, err error) {
|
|
src, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return workflow.Stage{}, err
|
|
}
|
|
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
stage, err = workflow.Stage{}, fmt.Errorf("panicked: %v", r)
|
|
}
|
|
}()
|
|
|
|
i := interp.New(interp.Options{})
|
|
if err := i.Use(stdlib.Symbols); err != nil {
|
|
return workflow.Stage{}, fmt.Errorf("yaegi: load stdlib symbols: %w", err)
|
|
}
|
|
if err := i.Use(wfextract.Symbols); err != nil {
|
|
return workflow.Stage{}, fmt.Errorf("yaegi: load workflow symbols: %w", err)
|
|
}
|
|
|
|
if _, err := i.Eval(string(src)); err != nil {
|
|
return workflow.Stage{}, fmt.Errorf("yaegi: evaluate: %w", err)
|
|
}
|
|
|
|
v, err := i.Eval("stages.Stage")
|
|
if err != nil {
|
|
return workflow.Stage{}, fmt.Errorf("yaegi: does not export func Stage() workflow.Stage: %w", err)
|
|
}
|
|
factory, ok := v.Interface().(func() workflow.Stage)
|
|
if !ok {
|
|
return workflow.Stage{}, fmt.Errorf("yaegi: Stage has the wrong signature (want func() workflow.Stage)")
|
|
}
|
|
|
|
return factory(), nil
|
|
}
|