diff --git a/agent/sandbox.go b/agent/sandbox.go index 61b250281..940f45b0a 100644 --- a/agent/sandbox.go +++ b/agent/sandbox.go @@ -93,7 +93,7 @@ func ExecInSandbox(ctx context.Context, argv []string, workingDir string, timeou } info, ok := SandboxRunInfoFromContext(ctx) if !ok || info.TaskID == "" { - return sandbox.ExecResponse{}, fmt.Errorf("%w: missing task sandbox context (no local fallback)", sandbox.ErrRunnerUnavailable) + return sandbox.ExecResponse{}, fmt.Errorf("%w: no task sandbox run info in context (no local fallback)", sandbox.ErrMissingSandboxContext) } if ex.Runner == nil { err := fmt.Errorf("%w: runner client is not configured", sandbox.ErrRunnerUnavailable) @@ -145,6 +145,12 @@ func (t *sandboxBashTool) Call(ctx context.Context, input json.RawMessage, tc *a } resp, err := ExecInSandbox(ctx, []string{"bash", "-c", in.Command}, workDir, in.TimeoutMS, env) if err != nil { + if errors.Is(err, sandbox.ErrMissingSandboxContext) { + // Standalone chat agents carry no task sandbox context (#805): say so, + // name the supported workflow, and stop the model from retrying Bash + // (the retry loop only burns LLM turns). + return actool.Errorf("Error: Bash cannot run here: this standalone conversation has no sandbox execution context, and ARTEX_SANDBOX=required allows no local fallback. Do not retry Bash in this conversation; ask the operator to run this work as a task (or set ARTEX_SANDBOX=off)."), nil + } return actool.Errorf(fmt.Sprintf("Error: sandbox execution failed (ARTEX_SANDBOX=required, no local fallback): %v", err)), nil } return formatSandboxExecResult(resp, tc), nil diff --git a/agent/sandbox_missing_context_test.go b/agent/sandbox_missing_context_test.go new file mode 100644 index 000000000..b869d6304 --- /dev/null +++ b/agent/sandbox_missing_context_test.go @@ -0,0 +1,71 @@ +package agent + +import ( + "context" + "encoding/json" + "errors" + "strings" + "testing" + + "github.com/Autumn-27/artex/sandbox" + actool "github.com/Autumn-27/norma/tool" +) + +// TestExecInSandboxMissingContextIsNotRunnerUnavailable covers the error- +// reporting slice of issue #805: a standalone chat agent's Bash call fails +// with "sandbox runner unavailable: missing task sandbox context" even when +// the runner is healthy, sending operators debugging a service that was never +// called. Missing execution context is a different failure category and must +// be reported as such. +func TestExecInSandboxMissingContextIsNotRunnerUnavailable(t *testing.T) { + SetSandboxExecutor(&SandboxExecutor{Mode: sandbox.ModeRequired}) + t.Cleanup(func() { SetSandboxExecutor(nil) }) + + // No SandboxRunInfo on the context: the standalone chat path (issue #805). + _, err := ExecInSandbox(context.Background(), []string{"true"}, "", 1000, nil) + if err == nil { + t.Fatal("missing sandbox context must fail closed") + } + if !errors.Is(err, sandbox.ErrMissingSandboxContext) { + t.Fatalf("missing context must match ErrMissingSandboxContext, got %v", err) + } + if sandbox.IsUnavailable(err) { + t.Fatalf("missing context is not a runner-outage: IsUnavailable must be false, got %v", err) + } + + // Context present but runner gone: still classified as runner-unavailable, + // so the two failure categories stay distinguishable. + ctx := WithSandboxRunInfo(context.Background(), SandboxRunInfo{TaskID: "42"}) + _, err = ExecInSandbox(ctx, []string{"true"}, "", 1000, nil) + if err == nil || !sandbox.IsUnavailable(err) { + t.Fatalf("nil runner with task context must stay runner-unavailable, got %v", err) + } + if errors.Is(err, sandbox.ErrMissingSandboxContext) { + t.Fatalf("runner-outage must not match ErrMissingSandboxContext, got %v", err) + } +} + +// TestSandboxBashToolMissingContextGuidesOperator: in required mode the Bash +// tool must tell the model WHY execution is impossible and to stop retrying, +// instead of a generic failure it will retry while burning LLM turns (#805). +func TestSandboxBashToolMissingContextGuidesOperator(t *testing.T) { + SetSandboxExecutor(&SandboxExecutor{Mode: sandbox.ModeRequired}) + t.Cleanup(func() { SetSandboxExecutor(nil) }) + + tool := &sandboxBashTool{} + in, _ := json.Marshal(map[string]any{"command": "true"}) + res, err := tool.Call(context.Background(), in, &actool.ToolContext{}) + if err != nil { + t.Fatalf("tool call returned Go error: %v", err) + } + text := strings.ToLower(res.Flatten()) + if !strings.Contains(text, "context") { + t.Fatalf("error must name the missing execution context, got %q", res.Flatten()) + } + if !strings.Contains(text, "task") { + t.Fatalf("error must point at the supported workflow (run as a task), got %q", res.Flatten()) + } + if strings.Contains(text, "runner unavailable") { + t.Fatalf("error must not blame the runner for a context problem, got %q", res.Flatten()) + } +} diff --git a/sandbox/types.go b/sandbox/types.go index 9995e26d1..392b349dd 100644 --- a/sandbox/types.go +++ b/sandbox/types.go @@ -58,6 +58,11 @@ var ( ErrBadRequest = errors.New("sandbox runner invalid request") ErrNotFound = errors.New("sandbox task not found") ErrDockerUnavailable = errors.New("sandbox runner cannot reach docker") + // ErrMissingSandboxContext marks a call that never reached the runner: the + // context carries no task sandbox run info (e.g. a standalone chat agent, + // #805). It is deliberately not runner-unavailable: the runner may be + // healthy, and IsUnavailable must not send operators debugging it. + ErrMissingSandboxContext = errors.New("missing sandbox execution context") ) // ExecRequest is the request body for POST /v1/tasks/{id}/exec.