- Introduce `-config-overlay` flag and `config.LoadOverlay()` to merge a base config with an overlay file, only overriding fields the overlay sets. This enables docker-compose to mount the base config from the host and apply only deployment-specific overrides. - Create `internal/natsrpc` package with reusable NATS request/reply primitives (Envelope, Client, Call, RegisterAll, Respond). Migrate storerpc, worktreerpc, and forgerpc to use it, eliminating duplicated marshal/unmarshal and error handling. - Create `internal/yaegiutil` package with shared interpreter construction (stdlib + extra symbols), generic export resolution (Resolve), and panic recovery (Safe). Migrate all Yaegi-based surfaces (gate scripts, daemon plugins, skill plugins, workflow stages, skill scripts) to use it, reducing boilerplate and centralizing panic safety. - Simplify `config.docker.toml` to contain only overlay fields (paths, container config) and remove the duplicated repos list – the base config is now the single source of truth. - Update docker-compose.yml to pass both -config and -config-overlay flags and mount the base config from the host. - Add tests for LoadOverlay, DockerConfigOverlaysExampleConfig, Envelope, and yaegiutil utilities.
71 lines
2.6 KiB
Go
71 lines
2.6 KiB
Go
// Package yaegiutil holds the interpreter-construction and safe-eval
|
|
// helpers shared by every Yaegi-interpreted extension surface in
|
|
// archie-core (gate scripts, workflow stages, skill plugins, daemon
|
|
// plugins, skill scripts). Each surface differs in what symbol tables it
|
|
// loads, what export it resolves, and how it invokes that export, but
|
|
// all of them build an interpreter the same way and must recover a
|
|
// panic from interpreted code — including a panic during the call
|
|
// itself, not just during eval — rather than let it take down the
|
|
// daemon.
|
|
package yaegiutil
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
|
|
"github.com/traefik/yaegi/interp"
|
|
"github.com/traefik/yaegi/stdlib"
|
|
)
|
|
|
|
// New returns a Yaegi interpreter with the standard library and any
|
|
// extraSymbols loaded. Each interpreted file must get its own
|
|
// interpreter when it declares package main — Yaegi cannot co-evaluate
|
|
// two package-main files with colliding symbols (e.g. two Stage()
|
|
// functions) in the same interpreter.
|
|
func New(opts interp.Options, extraSymbols ...map[string]map[string]reflect.Value) (*interp.Interpreter, error) {
|
|
i := interp.New(opts)
|
|
if err := i.Use(stdlib.Symbols); err != nil {
|
|
return nil, fmt.Errorf("yaegi: load stdlib symbols: %w", err)
|
|
}
|
|
for _, s := range extraSymbols {
|
|
if err := i.Use(s); err != nil {
|
|
return nil, fmt.Errorf("yaegi: load symbols: %w", err)
|
|
}
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
// Resolve evaluates src in i and type-asserts exportPath's value
|
|
// (e.g. "main.Stage", "gate.Check") to T. It does not itself recover
|
|
// panics — wrap the call that invokes the returned T in Safe, since a
|
|
// panic can happen either during Eval (e.g. an init()) or during the
|
|
// caller's later invocation of the resolved function.
|
|
func Resolve[T any](i *interp.Interpreter, src, exportPath string) (result T, err error) {
|
|
if _, err := i.Eval(src); err != nil {
|
|
return result, fmt.Errorf("evaluate: %w", err)
|
|
}
|
|
v, err := i.Eval(exportPath)
|
|
if err != nil {
|
|
return result, fmt.Errorf("does not export %s: %w", exportPath, err)
|
|
}
|
|
fn, ok := v.Interface().(T)
|
|
if !ok {
|
|
return result, fmt.Errorf("%s is %T, want %T", exportPath, v.Interface(), result)
|
|
}
|
|
return fn, nil
|
|
}
|
|
|
|
// Safe runs fn and recovers any panic — interpreted code is untrusted
|
|
// relative to the daemon's own logic, so a nil dereference or
|
|
// out-of-range index inside it must become an error, not a crash.
|
|
// label identifies the interpreted source in the resulting error.
|
|
func Safe[R any](label string, fn func() (R, error)) (result R, err error) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
var zero R
|
|
result, err = zero, fmt.Errorf("yaegi: %s panicked: %v", label, r)
|
|
}
|
|
}()
|
|
return fn()
|
|
}
|