Merge remote-tracking branch 'origin/fix/45-skillscriptrun-ignores-context-cannot-be-canceledtimed-out'
This commit is contained in:
commit
366e8f412b
4 files changed
+140
-17
No files matched your search
@@ -227,7 +227,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"`
|
||||
}
|
||||
@@ -238,7 +238,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 //nolint:nilerr // same: a rejection message lets the model retry with a valid path
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/agentloop"
|
||||
"github.com/samcharles93/ai-sdk/runtime"
|
||||
@@ -246,3 +247,47 @@ func TestInProcessRunnerPreservesCallerCancellation(t *testing.T) {
|
||||
t.Fatalf("Run error = %v, want context.Canceled", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScriptToolHonorsContextCancellation proves that the run_go_script
|
||||
// tool handler discards the incoming context (the handler is declared as
|
||||
// func(_ context.Context, input string) and calls skillscript.Run without
|
||||
// forwarding ctx). Every other execution path in archie-core respects
|
||||
// context cancellation (SubprocessRunner uses exec.CommandContext,
|
||||
// InProcessRunner checks context.Cause), but a cancelled/timed-out context
|
||||
// has zero effect on run_go_script — a blocking script wedges the
|
||||
// goroutine permanently.
|
||||
func TestScriptToolHonorsContextCancellation(t *testing.T) {
|
||||
workspace := t.TempDir()
|
||||
src := `package main
|
||||
|
||||
func main() {
|
||||
select {}
|
||||
}
|
||||
`
|
||||
if err := os.WriteFile(filepath.Join(workspace, "blocker.go"), []byte(src), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel() // immediately cancelled — the tool should return promptly
|
||||
|
||||
tool := scriptToolSet(workspace)["run_go_script"]
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
tool.Execute(ctx, `{"path":"blocker.go"}`)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// After the fix, the tool returns quickly because the cancelled
|
||||
// context propagates through to skillscript.Run, which selects
|
||||
// on ctx.Done() and abandons the blocking EvalPath goroutine.
|
||||
t.Log("run_go_script returned promptly (context was honored)")
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Error("run_go_script ignores context cancellation: " +
|
||||
"the handler discards its context parameter (declared as _) " +
|
||||
"and calls skillscript.Run without forwarding it (issue #45)")
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func writeScript(t *testing.T, src string) string {
|
||||
@@ -92,3 +93,36 @@ func TestRunMissingFile(t *testing.T) {
|
||||
t.Fatal("Run() error = nil, want an error for a missing file")
|
||||
}
|
||||
}
|
||||
|
||||
// TestRunBlocksIndefinitelyOnBlockingScript proves that skillscript.Run
|
||||
// has no cancellation mechanism. A skill script that blocks forever
|
||||
// (select{}) wedges the calling goroutine permanently. Every other
|
||||
// execution path in archie-core supports context cancellation
|
||||
// (exec.CommandContext in SubprocessRunner, context.Cause checks in
|
||||
// InProcessRunner), but Run cannot be canceled or timed out because it
|
||||
// takes no context.Context and calls i.EvalPath synchronously.
|
||||
func TestRunBlocksIndefinitelyOnBlockingScript(t *testing.T) {
|
||||
path := writeScript(t, `package main
|
||||
|
||||
func main() {
|
||||
select {}
|
||||
}
|
||||
`)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
Run(path)
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
// Run returned from a script that should block forever.
|
||||
// This is unexpected — the script contains select{}.
|
||||
t.Log("Run returned unexpectedly from a select{} script")
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Error("skillscript.Run blocks forever on a select{} script: " +
|
||||
"Run needs a context.Context parameter for cancellation " +
|
||||
"(issue #45)")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user