Files
archie/internal/gate/gateeval/yaegi.go
T
sam c0d108a5d0 feat: add Yaegi-based plugin system (gates, custom stages, skill scripts)
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.
2026-07-22 00:54:46 +10:00

68 lines
2.2 KiB
Go

// Package gateeval evaluates a repo's .archie/gate.go via Yaegi. Split
// from internal/gate (which only declares the GateContext/Finding types)
// because the generated symbol table in gateextract imports internal/gate —
// this package sits above both to avoid the resulting import cycle.
package gateeval
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
"github.com/samcharles93/archie-core/internal/gate"
"github.com/samcharles93/archie-core/internal/gate/gateextract"
)
// scriptPath is the per-repo custom gate script, relative to the
// worktree root.
const scriptPath = ".archie/gate.go"
// Evaluate loads and runs the repo's .archie/gate.go against gctx. A
// missing script is not an error — it returns (nil, nil), meaning no
// custom gate is configured for this repo. The script runs in-process
// (interpreted, not sandboxed), so a panic inside it — nil dereference,
// out-of-range index — is recovered and returned as an error rather than
// taking down the daemon.
func Evaluate(gctx gate.GateContext) (findings []gate.Finding, err error) {
src, err := os.ReadFile(filepath.Join(gctx.Dir, scriptPath))
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, nil
}
return nil, fmt.Errorf("read %s: %w", scriptPath, err)
}
defer func() {
if r := recover(); r != nil {
findings, err = nil, fmt.Errorf("yaegi: %s panicked: %v", scriptPath, r)
}
}()
i := interp.New(interp.Options{})
if err := i.Use(stdlib.Symbols); err != nil {
return nil, fmt.Errorf("yaegi: load stdlib symbols: %w", err)
}
if err := i.Use(gateextract.Symbols); err != nil {
return nil, fmt.Errorf("yaegi: load gate symbols: %w", err)
}
if _, err := i.Eval(string(src)); err != nil {
return nil, fmt.Errorf("yaegi: evaluate %s: %w", scriptPath, err)
}
v, err := i.Eval("gate.Check")
if err != nil {
return nil, fmt.Errorf("yaegi: %s does not export func Check(GateContext) []Finding: %w", scriptPath, err)
}
check, ok := v.Interface().(func(gate.GateContext) []gate.Finding)
if !ok {
return nil, fmt.Errorf("yaegi: %s Check has the wrong signature (want func(gate.GateContext) []gate.Finding)", scriptPath)
}
return check(gctx), nil
}