diff --git a/internal/plugin/plugin.go b/internal/plugin/plugin.go index 4b3f95978..d50f78807 100644 --- a/internal/plugin/plugin.go +++ b/internal/plugin/plugin.go @@ -6,6 +6,7 @@ package plugin import ( "fmt" + "log/slog" "os" "path/filepath" "reflect" @@ -68,27 +69,40 @@ func LoadDir(dir string, extraSymbols ...map[string]map[string]reflect.Value) ([ path := filepath.Join(dir, name) src, err := os.ReadFile(path) if err != nil { - continue // skip unreadable files - } - i := interp.New(interp.Options{}) - if err := i.Use(stdlib.Symbols); err != nil { + slog.Default().Warn("skipping unreadable daemon plugin", "file", name, "err", err) continue } - for _, s := range extraSymbols { + // Each plugin file is package main — must use a fresh interpreter + // to avoid symbol collisions between files. + i := interp.New(interp.Options{}) + if err := i.Use(stdlib.Symbols); err != nil { + slog.Default().Warn("skipping plugin — stdlib load failed", "file", name, "err", err) + continue + } + symbolLoadFailed := false + for j, s := range extraSymbols { if err := i.Use(s); err != nil { - continue + slog.Default().Warn("skipping plugin — symbol table load failed", "file", name, "symbolIndex", j, "err", err) + symbolLoadFailed = true + break } } + if symbolLoadFailed { + continue + } if _, err := i.Eval(string(src)); err != nil { - continue // skip files that don't compile + slog.Default().Warn("skipping plugin — compile failed", "file", name, "err", err) + continue } v, err := i.Eval("main.Plugin") if err != nil { - continue // skip files without a Plugin export + slog.Default().Warn("skipping plugin — no Plugin export", "file", name, "err", err) + continue } p, ok := v.Interface().(Plugin) if !ok { - continue // skip files with wrong Plugin type + slog.Default().Warn("skipping plugin — wrong Plugin type", "file", name, "type", fmt.Sprintf("%T", v.Interface())) + continue } plugins = append(plugins, p) } diff --git a/internal/storage/storage.go b/internal/storage/storage.go index 5aebae331..1dcf44583 100644 --- a/internal/storage/storage.go +++ b/internal/storage/storage.go @@ -140,9 +140,14 @@ func NewDockerBackend(cli *client.Client) *DockerBackend { // ensureVolume creates a Docker volume if it doesn't already exist. // It is idempotent: concurrent callers racing to create the same volume // will not see an error — "already exists" is treated as success. +// +// When d.cli is nil (unit tests), ensureVolume returns nil without +// creating a volume. The returned mounts will fail at container create +// time if Docker cannot find the volume. Production code always sets +// cli via NewDockerBackend. func (d *DockerBackend) ensureVolume(ctx context.Context, name string) error { if d.cli == nil { - return nil // nil client for testing + return nil // nil client for testing — volumes must be pre-created } _, err := d.cli.VolumeInspect(ctx, name, client.VolumeInspectOptions{}) if err == nil { diff --git a/internal/workflow/skillbuild/skillbuild.go b/internal/workflow/skillbuild/skillbuild.go index 3b20df58c..7e2cd074e 100644 --- a/internal/workflow/skillbuild/skillbuild.go +++ b/internal/workflow/skillbuild/skillbuild.go @@ -52,7 +52,12 @@ func BuildRegistry(worktree string) (workflow.Registry, error) { sw := SkillWorkflow{Workflow: entry.Workflow, Dir: entry.Dir} wf, err := BuildWorkflow(worktree, sw) if err != nil { - return nil, fmt.Errorf("build workflow %s from skill %s: %w", entry.Workflow, entry.Dir, err) + slog.Default().Warn("skipping broken skill workflow", + "workflow", entry.Workflow, + "skill", entry.Dir, + "err", err, + ) + continue } // Only override the built-in when the skill actually has stages — // an empty workflow means the skill declared intent but has no @@ -107,7 +112,12 @@ func AugmentRegistry(worktree string, base workflow.Registry) (workflow.Registry sw := SkillWorkflow{Workflow: entry.Workflow, Dir: entry.Dir} wf, err := BuildWorkflow(worktree, sw) if err != nil { - return nil, fmt.Errorf("build workflow %s from skill %s: %w", entry.Workflow, entry.Dir, err) + slog.Default().Warn("skipping broken skill workflow in worktree", + "workflow", entry.Workflow, + "skill", entry.Dir, + "err", err, + ) + continue } // Only override the base entry when the skill actually has stages. // An empty workflow means the skill declared intent but has no @@ -149,16 +159,21 @@ func BuildWorkflow(worktree string, entry SkillWorkflow) (workflow.Workflow, err } sort.Strings(names) + // Each plugin file is package main defining Stage() — they must be + // evaluated in separate interpreters to avoid symbol collisions. 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) + slog.Default().Warn("skipping unreadable stage plugin", "plugin", name, "err", err) + continue } - stage, err := loadStage(name, string(src)) + i := newStageInterpreter() + stage, err := loadStage(i, name, string(src)) if err != nil { - return workflow.Workflow{}, fmt.Errorf("plugin %s: %w", name, err) + slog.Default().Warn("skipping broken stage plugin", "plugin", name, "err", err) + continue } stages = append(stages, stage) } @@ -166,15 +181,24 @@ func BuildWorkflow(worktree string, entry SkillWorkflow) (workflow.Workflow, err 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) { +// newStageInterpreter returns a Yaegi interpreter pre-loaded with stdlib +// and workflow symbols. Each plugin file still needs its own interpreter +// because files use package main with duplicate Stage() symbols — they +// cannot be co-evaluated in a single interpreter without collisions. +func newStageInterpreter() *interp.Interpreter { 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) + _ = i.Use(stdlib.Symbols) + _ = i.Use(wfextract.Symbols) + return i +} + +// loadStage interprets a single .go plugin source via Yaegi. Prefer +// passing a pre-configured interpreter from newStageInterpreter() to +// avoid redundant stdlib loading across multiple files. +func loadStage(interp *interp.Interpreter, filename string, src string) (workflow.Stage, error) { + i := interp + if i == nil { + i = newStageInterpreter() } if _, err := i.Eval(src); err != nil { return workflow.Stage{}, fmt.Errorf("eval: %w", err) @@ -185,7 +209,10 @@ func loadStage(filename string, src string) (workflow.Stage, error) { } fn, ok := v.Interface().(func() workflow.Stage) if !ok { - // Fallback for test plugins: func() (string, func(...) error) + // Fallback for test plugins: func() (string, func(...) error). + // NOTE: this constructs Stage with positional fields. If Stage + // gains new fields in the future, this path will silently zero- + // value them. Prefer func() workflow.Stage in production plugins. rawFn, rawOk := v.Interface().(func() (string, func(context.Context, *workflow.TaskContext) error)) if rawOk { name, run := rawFn() diff --git a/internal/workflow/skillbuild/skillbuild_test.go b/internal/workflow/skillbuild/skillbuild_test.go index f279c5749..2d8dffb96 100644 --- a/internal/workflow/skillbuild/skillbuild_test.go +++ b/internal/workflow/skillbuild/skillbuild_test.go @@ -739,3 +739,181 @@ Body. t.Errorf("Workflow.Name = %q, want empty", wf.Name) } } + +// ── resilience: skip broken plugins, don't abort ──────────────────── + +func TestBuildWorkflowSkipsBrokenPlugin(t *testing.T) { + // R2: one broken stage plugin must not abort the entire workflow. + // Valid plugins before and after the broken one must still load. + + dir := t.TempDir() + pluginsDir := filepath.Join(dir, ".agents", "skills", "archie-wf-resilient", "plugins") + if err := os.MkdirAll(pluginsDir, 0o755); err != nil { + t.Fatal(err) + } + + // First plugin — valid. + if err := os.WriteFile(filepath.Join(pluginsDir, "01-setup.go"), []byte(`package main + +import ( + "context" + "github.com/samcharles93/archie-core/internal/workflow" +) + +func Stage() (string, func(context.Context, *workflow.TaskContext) error) { + return "setup", func(ctx context.Context, tc *workflow.TaskContext) error { + tc.BuildSummary = "setup-ran" + return nil + } +} +`), 0o644); err != nil { + t.Fatal(err) + } + + // Second plugin — BROKEN (syntax error). + if err := os.WriteFile(filepath.Join(pluginsDir, "02-broken.go"), []byte(`package main +this is %% NOT VALID GO @@@@ +`), 0o644); err != nil { + t.Fatal(err) + } + + // Third plugin — valid. + if err := os.WriteFile(filepath.Join(pluginsDir, "03-teardown.go"), []byte(`package main + +import ( + "context" + "github.com/samcharles93/archie-core/internal/workflow" +) + +func Stage() (string, func(context.Context, *workflow.TaskContext) error) { + return "teardown", func(ctx context.Context, tc *workflow.TaskContext) error { + tc.BuildSummary = tc.BuildSummary + " teardown-ran" + return nil + } +} +`), 0o644); err != nil { + t.Fatal(err) + } + + if err := os.WriteFile(filepath.Join(filepath.Dir(pluginsDir), "SKILL.md"), []byte(`--- +name: archie-wf-resilient +description: Resilient workflow +version: 1.0.0 +metadata: + archie: + workflow: resilient +--- +Resilient workflow. +`), 0o644); err != nil { + t.Fatal(err) + } + + entry := SkillWorkflow{Workflow: "resilient", Dir: "archie-wf-resilient"} + wf, err := BuildWorkflow(dir, entry) + if err != nil { + t.Fatalf("BuildWorkflow returned error instead of skipping broken plugin: %v", err) + } + if len(wf.Stages) != 2 { + t.Fatalf("got %d stages, want 2 (broken plugin skipped)", len(wf.Stages)) + } + if wf.Stages[0].Name != "setup" { + t.Errorf("stage[0].Name = %q, want setup", wf.Stages[0].Name) + } + if wf.Stages[1].Name != "teardown" { + t.Errorf("stage[1].Name = %q, want teardown", wf.Stages[1].Name) + } + + // Run the surviving stages. + tc := &workflow.TaskContext{BuildSummary: ""} + for _, s := range wf.Stages { + if err := s.Run(context.Background(), tc); err != nil { + t.Fatalf("stage %s: %v", s.Name, err) + } + } + if tc.BuildSummary != "setup-ran teardown-ran" { + t.Errorf("BuildSummary = %q, want 'setup-ran teardown-ran'", tc.BuildSummary) + } +} + +func TestBuildRegistrySkipsBrokenSkill(t *testing.T) { + // R2: a skill with broken plugins must not prevent other skills + // from registering — and must not block daemon startup. + + dir := t.TempDir() + + // Skill 1: valid workflow. + goodDir := filepath.Join(dir, ".agents", "skills", "archie-wf-good", "plugins") + if err := os.MkdirAll(goodDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(goodDir, "01-step.go"), []byte(`package main + +import ( + "context" + "github.com/samcharles93/archie-core/internal/workflow" +) + +func Stage() (string, func(context.Context, *workflow.TaskContext) error) { + return "step", func(ctx context.Context, tc *workflow.TaskContext) error { + return nil + } +} +`), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(filepath.Dir(goodDir), "SKILL.md"), []byte(`--- +name: archie-wf-good +description: Good workflow +version: 1.0.0 +metadata: + archie: + workflow: good +--- +Good. +`), 0o644); err != nil { + t.Fatal(err) + } + + // Skill 2: broken plugins — ALL plugins are broken. + badDir := filepath.Join(dir, ".agents", "skills", "archie-wf-bad", "plugins") + if err := os.MkdirAll(badDir, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(badDir, "01-broken.go"), []byte(`package main +%%% SYNTAX ERROR @@@ +`), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(filepath.Dir(badDir), "SKILL.md"), []byte(`--- +name: archie-wf-bad +description: Broken workflow +version: 1.0.0 +metadata: + archie: + workflow: bad +--- +Broken. +`), 0o644); err != nil { + t.Fatal(err) + } + + reg, err := BuildRegistry(dir) + if err != nil { + t.Fatalf("BuildRegistry returned error instead of skipping broken skill: %v", err) + } + + // Good workflow must still be registered. + if _, ok := reg["good"]; !ok { + t.Error("'good' workflow missing — broken skill blocked registry build") + } + // Bad workflow must not be registered (it had no valid stages). + if _, ok := reg["bad"]; ok { + // It might fall back to built-in "bad" — that's fine. + // But it should NOT be the empty workflow from the broken skill. + t.Log("'bad' workflow present (may be built-in fallback)") + } + // Built-in fallbacks must still exist. + if _, ok := reg["implement"]; !ok { + t.Error("built-in 'implement' missing after broken skill was skipped") + } +}