diff --git a/internal/agentexec/inprocess.go b/internal/agentexec/inprocess.go index b38d642a6..a974b3253 100644 --- a/internal/agentexec/inprocess.go +++ b/internal/agentexec/inprocess.go @@ -210,7 +210,7 @@ func scriptToolSet(workspace string) core.ToolSet { "run_go_script", "Run a Yaegi-interpreted Go script (e.g. a skill's bundled scripts/*.go helper) and return everything it printed.", json.RawMessage(params), - func(_ context.Context, input string) (string, error) { + func(ctx context.Context, input string) (string, error) { var args struct { Path string `json:"path"` } @@ -221,7 +221,7 @@ func scriptToolSet(workspace string) core.ToolSet { if rel, err := filepath.Rel(workspace, full); err != nil || strings.HasPrefix(rel, "..") { return "run_go_script rejected: path escapes the workspace", nil } - out, err := skillscript.Run(full) + out, err := skillscript.RunContext(ctx, full) if err != nil { return fmt.Sprintf("run_go_script failed: %v\n%s", err, out), nil } diff --git a/internal/skillscript/yaegi.go b/internal/skillscript/yaegi.go index 60631990d..c60cfa12c 100644 --- a/internal/skillscript/yaegi.go +++ b/internal/skillscript/yaegi.go @@ -7,8 +7,10 @@ package skillscript import ( "bytes" + "context" "fmt" "os" + "time" "github.com/traefik/yaegi/interp" "github.com/traefik/yaegi/stdlib/unrestricted" @@ -16,31 +18,73 @@ import ( "github.com/samcharles93/archie-core/internal/yaegiutil" ) +// defaultTimeout protects callers that use Run (the legacy, context-free +// signature) from a script that blocks forever. Callers that have a +// context should use RunContext instead. +const defaultTimeout = 1 * time.Second + // Run interprets the Go script at path and executes its main function, // returning everything it wrote to stdout and stderr. The script runs // in-process (interpreted, not sandboxed) with unrestricted symbols // (os/exec included -- these scripts wrap external tools the way a shell // script would), so a panic inside it is recovered and returned as an // error rather than taking down the daemon. +// +// Run is a convenience wrapper around RunContext with a short built-in +// timeout. Callers that need precise cancellation or a different deadline +// should use RunContext directly. func Run(path string) (string, error) { + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) + defer cancel() + return RunContext(ctx, path) +} + +// RunContext interprets the Go script at path and executes its main +// function, returning everything it wrote to stdout and stderr. +// The script is executed in a goroutine; if ctx is cancelled or exceeds +// its deadline before the script finishes, RunContext returns the +// context error and abandons the goroutine (yaegi does not support +// external cancellation of an in-progress eval). +func RunContext(ctx context.Context, path string) (string, error) { if _, statErr := os.Stat(path); statErr != nil { return "", statErr } - return yaegiutil.Safe(path, func() (string, error) { - var buf bytes.Buffer - i, err := yaegiutil.New(interp.Options{Stdout: &buf, Stderr: &buf}, unrestricted.Symbols) - if err != nil { - return "", err - } + // Check context before spawning the goroutine. + if err := ctx.Err(); err != nil { + return "", err + } - // EvalPath treats a package-main source file the way `go run` does: - // evaluating it executes its main() directly. Unlike `go build`, it - // does not complain about a missing main() -- such a script simply - // produces no output. - if _, err := i.EvalPath(path); err != nil { - return "", fmt.Errorf("yaegi: run %s: %w", path, err) - } - return buf.String(), nil - }) + type result struct { + output string + err error + } + + ch := make(chan result, 1) + go func() { + out, err := yaegiutil.Safe(path, func() (string, error) { + var buf bytes.Buffer + i, err := yaegiutil.New(interp.Options{Stdout: &buf, Stderr: &buf}, unrestricted.Symbols) + if err != nil { + return "", err + } + + // EvalPath treats a package-main source file the way `go run` does: + // evaluating it executes its main() directly. Unlike `go build`, it + // does not complain about a missing main() -- such a script simply + // produces no output. + if _, err := i.EvalPath(path); err != nil { + return "", fmt.Errorf("yaegi: run %s: %w", path, err) + } + return buf.String(), nil + }) + ch <- result{out, err} + }() + + select { + case r := <-ch: + return r.output, r.err + case <-ctx.Done(): + return "", ctx.Err() + } }