fix: skillscript.Run ignores context, cannot be canceled/timed out (archie)

Fixes #45
This commit is contained in:
archie committed 2026-07-27 02:41:44 +00:00
1 parent c3aa0a0a02
commit ce0fc1fac0
2 files changed
+61 -17

No files matched your search

+2 -2
View File
@@ -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
}
+59 -15
View File
@@ -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()
}
}