diff --git a/internal/agentexec/inprocess_test.go b/internal/agentexec/inprocess_test.go index 1439df633..3503e1f22 100644 --- a/internal/agentexec/inprocess_test.go +++ b/internal/agentexec/inprocess_test.go @@ -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)") + } +} diff --git a/internal/skillscript/yaegi_test.go b/internal/skillscript/yaegi_test.go index 399fa5678..b605a016d 100644 --- a/internal/skillscript/yaegi_test.go +++ b/internal/skillscript/yaegi_test.go @@ -4,6 +4,7 @@ import ( "os" "path/filepath" "testing" + "time" ) func writeScript(t *testing.T, src string) string { @@ -90,3 +91,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)") + } +}