feat: wire Layer 2 daemon plugin loader into archied startup

Daemon plugins (Yaegi-interpreted .go files) are now loaded from the
configured plugin_dir at startup. Each file must export a 'Plugin'
variable using the _Plugin wrapper struct, which bridges Yaegi types
to the Go plugin.Plugin interface.

Changes:
- internal/plugin/pluginextract/ — generated Yaegi symbol table for
  the plugin package (mirrors wfextract pattern), including the
  _Plugin interface wrapper that lets interpreted code implement
  Go interfaces
- LoadDir now accepts optional symbol tables via variadic parameter,
  avoiding an import cycle between plugin and pluginextract
- Plugin tests rewritten as behavioral (not structural), verifying
  actual Yaegi interpretation of valid, invalid, and missing plugins
- config.PluginDir field for specifying the plugin directory
- daemon.PluginRegistry field for holding loaded plugins
- main.go wires plugin loading: LoadDir at startup, register into
  daemon, log each loaded plugin

Why: the Plugin interface and LoadDir existed but weren't connected
to the daemon. Skills define workflows; daemon plugins are the
extension point for forge resolvers, storage backends, and other
core-daemon concerns.
This commit is contained in:
sam committed 2026-07-22 04:11:02 +10:00
1 parent 8958a82233
commit f38ba5d515
8 files changed
+256 -35

No files matched your search

+161 -26
View File
@@ -1,45 +1,180 @@
package plugin
package plugin_test
import "testing"
import (
"os"
"path/filepath"
"testing"
// ── regression: Core plugin registry nonexistent ─────────────────────
"github.com/samcharles93/archie-core/internal/plugin"
"github.com/samcharles93/archie-core/internal/plugin/pluginextract"
)
// ── Plugin interface and registry ────────────────────────────────────
func TestPluginInterfaceExists(t *testing.T) {
// PRD section 5 Layer 2: plugins implement Name(), Version().
// The Plugin interface must be usable for type assertions from
// Yaegi-interpreted code.
var p Plugin
_ = p // ensures the type exists at compile time
// Verify the methods exist on the interface.
var p plugin.Plugin
_ = p
type nameVersioner interface {
Name() string
Version() string
}
var _ nameVersioner = p // compile-time check
var _ nameVersioner = p
}
func TestRegistryExists(t *testing.T) {
// A registry holds loaded plugins and dispatches extension points.
r := &Registry{}
r := &plugin.Registry{}
if r == nil {
t.Error("Gap: Registry type is not defined. " +
"Define a Registry that holds loaded plugins and dispatches " +
"extension calls (forge, ticketing, storage, secrets, notify).")
t.Fatal("Registry type is not defined")
}
}
func TestLoadPluginsFromConfigDir(t *testing.T) {
// The daemon loads .go files from ~/.config/archie/plugins/ at
// startup. Failed plugins are logged and skipped.
plugins, err := LoadDir("/nonexistent/path")
if err != nil {
t.Error("Gap: LoadDir function is not defined. " +
"Define LoadDir(path string) ([]Plugin, error) that discovers " +
"and evaluates .go files from ~/.config/archie/plugins/.")
// ── LoadDir behavioral tests ─────────────────────────────────────────
var symbols = pluginextract.Symbols
func TestLoadDirLoadsValidPlugin(t *testing.T) {
// LoadDir must Yaegi-interpret .go files and return Plugins whose
// exported "Plugin" variable satisfies the Plugin interface.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "hello.go"), []byte(`package main
import "github.com/samcharles93/archie-core/internal/plugin"
var Plugin = plugin._Plugin{
WName: func() string { return "hello" },
WVersion: func() string { return "1.0.0" },
}
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := plugin.LoadDir(dir, symbols)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 1 {
t.Fatalf("LoadDir returned %d plugins, want 1", len(plugins))
}
if plugins[0].Name() != "hello" {
t.Errorf("plugin.Name() = %q, want hello", plugins[0].Name())
}
if plugins[0].Version() != "1.0.0" {
t.Errorf("plugin.Version() = %q, want 1.0.0", plugins[0].Version())
}
}
func TestLoadDirSkipsInvalidPlugin(t *testing.T) {
// A .go file that doesn't compile must be skipped — not crash the loader.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "broken.go"), []byte(`package main
this is not valid Go syntax @@@@
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := plugin.LoadDir(dir, symbols)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 0 {
t.Errorf("LoadDir returned %d plugins from broken source, want 0", len(plugins))
}
}
func TestLoadDirSkipsFileWithoutPluginExport(t *testing.T) {
// A valid Go file without a "Plugin" export must be skipped.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "notaplugin.go"), []byte(`package main
func Something() string { return "nope" }
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := plugin.LoadDir(dir, symbols)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 0 {
t.Errorf("LoadDir returned %d plugins from file without Plugin export, want 0", len(plugins))
}
}
func TestLoadDirSkipsNonGoFiles(t *testing.T) {
// Only .go files are loaded. Other files are ignored.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "readme.md"), []byte(`# Not a plugin`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "hello.go"), []byte(`package main
import "github.com/samcharles93/archie-core/internal/plugin"
var Plugin = plugin._Plugin{
WName: func() string { return "hello" },
WVersion: func() string { return "1.0.0" },
}
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := plugin.LoadDir(dir, symbols)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 1 {
t.Fatalf("LoadDir returned %d plugins, want 1 (only .go files count)", len(plugins))
}
}
func TestLoadDirMultiplePlugins(t *testing.T) {
// Multiple valid plugins must all be loaded, sorted by filename.
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "01-alpha.go"), []byte(`package main
import "github.com/samcharles93/archie-core/internal/plugin"
var Plugin = plugin._Plugin{
WName: func() string { return "alpha" },
WVersion: func() string { return "1.0.0" },
}
`), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "02-beta.go"), []byte(`package main
import "github.com/samcharles93/archie-core/internal/plugin"
var Plugin = plugin._Plugin{
WName: func() string { return "beta" },
WVersion: func() string { return "2.0.0" },
}
`), 0o644); err != nil {
t.Fatal(err)
}
plugins, err := plugin.LoadDir(dir, symbols)
if err != nil {
t.Fatal(err)
}
if len(plugins) != 2 {
t.Fatalf("LoadDir returned %d plugins, want 2", len(plugins))
}
if plugins[0].Name() != "alpha" {
t.Errorf("plugin[0].Name() = %q, want alpha (sorted by filename)", plugins[0].Name())
}
if plugins[1].Name() != "beta" {
t.Errorf("plugin[1].Name() = %q, want beta", plugins[1].Name())
}
}
func TestLoadDirNonexistentDirReturnsNil(t *testing.T) {
plugins, err := plugin.LoadDir("/nonexistent/path/for/plugins")
if err != nil {
t.Fatal(err)
}
if plugins != nil {
// Even an empty directory should return empty slice, not nil.
_ = plugins
t.Errorf("LoadDir returned %v, want nil for nonexistent directory", plugins)
}
}