Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions server/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,17 @@ func (e *Engine) Run(ctx context.Context, t *Task) {
}
return
}
// Loud, not silent (#830): a chat-spawned task can carry scope rows that
// grant no egress (agent-stamped company links stay unapproved), leaving
// the proxy allowlist empty so every connection 403s with no hint why.
if e.m != nil && t.Store != nil {
if tid, perr := strconv.ParseInt(t.ID, 10, 64); perr == nil {
if msg := emptyEgressScopeWarning(e.m.pg, tid, taskSandboxRunInfo(e.m.pg, e.sandboxDataDir, t.ID)); msg != "" {
log.Printf("[sandbox] task %s: %s", t.ID, msg)
_, _ = t.Store.AppendActivity(db.Activity{Kind: "system", IsError: true, Summary: msg})
}
}
}
} else if e.sandboxMode == sandbox.ModeOff && t.Store != nil {
_, _ = t.Store.AppendActivity(db.Activity{
Kind: "system",
Expand Down
16 changes: 16 additions & 0 deletions server/sandbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,19 @@ func taskSandboxRunInfo(pg *db.DB, dataDir, taskID string) agent.SandboxRunInfo
}
return info
}

// emptyEgressScopeWarning describes the chat-spawn scope trap (#830): the task
// carries task_scope rows, but none of them widen the sandbox egress
// allowlist (agent-stamped rows are coverage-only until an operator approves
// them), so the per-task proxy starts empty and denies every connection. ""
// when there is nothing to warn about.
func emptyEgressScopeWarning(pg *db.DB, tid int64, info agent.SandboxRunInfo) string {
if pg == nil || tid <= 0 || len(info.ScopeDomains) > 0 || len(info.ScopeCIDRs) > 0 {
return ""
}
var n int
if err := pg.QueryRow(`SELECT count(*) FROM task_scope WHERE task_id=$1`, tid).Scan(&n); err != nil || n == 0 {
return ""
}
return fmt.Sprintf("sandbox egress empty: this task has %d scope row(s), but none currently grant egress, so every connection from the sandbox will be denied. Agent-added scope is coverage-only until an operator approves it; once the scope is approved, the egress proxy is recreated with the new allowlist without restarting the task.", n)
}
91 changes: 91 additions & 0 deletions server/sandbox_empty_egress_warning_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package server

import (
"os"
"strconv"
"strings"
"testing"
"time"

"github.com/Autumn-27/artex/db"
)

// TestEmptyEgressScopeWarning covers the chat-spawn scope trap of issue #830:
// spawn_task stamps the company link approved_by_operator=false, so the task
// starts with an empty sandbox egress allowlist and every connection from the
// sandbox is denied, with nothing in the UI saying why. The engine must warn
// loudly when a task carries scope rows whose allowlist still comes up empty.
func TestEmptyEgressScopeWarning(t *testing.T) {
dsn := strings.TrimSpace(os.Getenv("ARTEX_PG_DSN"))
if dsn == "" {
t.Skip("ARTEX_PG_DSN not set")
}
pg, err := db.Open(dsn)
if err != nil {
db.SkipPGUnavailable(t, err)
}
t.Cleanup(func() { _ = pg.Close() })

suffix := strconv.FormatInt(time.Now().UnixNano(), 36)
compID, _, _, _, _, err := pg.Companies().CreateCompanyWithScope(
"EmptyEgressCo-"+suffix, "",
[]db.ScopeInput{{Kind: "cidr", Value: "203.0.113.0/24"}},
"test fixture",
true,
)
if err != nil {
t.Fatalf("CreateCompanyWithScope: %v", err)
}

warn := func(t *testing.T, taskID int64) string {
t.Helper()
info := taskSandboxRunInfo(pg, t.TempDir(), strconv.FormatInt(taskID, 10))
return emptyEgressScopeWarning(pg, taskID, info)
}

// Case 1, the trap: a chat-spawned task carries the company link stamped by
// the agent (approved_by_operator=false). The allowlist is empty and the
// operator must hear about it.
trap, err := pg.CreateTaskWithOptions("empty-egress-trap-"+suffix, "goal", db.TaskCreateOptions{
CompanyIDs: []int64{compID},
CompanyScopeByAgent: true,
})
if err != nil {
t.Fatalf("CreateTaskWithOptions(trap): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(trap.ID) })
trapInfo := taskSandboxRunInfo(pg, t.TempDir(), strconv.FormatInt(trap.ID, 10))
if len(trapInfo.ScopeDomains)+len(trapInfo.ScopeCIDRs) != 0 {
t.Fatalf("precondition: agent-stamped company must grant no egress, got domains=%v cidrs=%v",
trapInfo.ScopeDomains, trapInfo.ScopeCIDRs)
}
if w := warn(t, trap.ID); w == "" {
t.Fatal("task with scope rows and an empty allowlist must warn (issue #830): got no warning")
} else if !strings.Contains(w, "approv") {
t.Fatalf("warning must point the operator at approval, got %q", w)
}

// Case 2: the same company selected by the operator at creation. The
// allowlist is non-empty, so there is nothing to warn about.
okTask, err := pg.CreateTaskWithOptions("empty-egress-ok-"+suffix, "goal", db.TaskCreateOptions{
CompanyIDs: []int64{compID},
})
if err != nil {
t.Fatalf("CreateTaskWithOptions(ok): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(okTask.ID) })
if w := warn(t, okTask.ID); w != "" {
t.Fatalf("operator-approved scope must not warn, got %q", w)
}

// Case 3: a scopeless task. The allowlist is empty by design; no rows, no
// warning.
bare, err := pg.CreateTaskWithOptions("empty-egress-bare-"+suffix, "goal", db.TaskCreateOptions{})
if err != nil {
t.Fatalf("CreateTaskWithOptions(bare): %v", err)
}
t.Cleanup(func() { _ = pg.DeleteTask(bare.ID) })
if w := warn(t, bare.ID); w != "" {
t.Fatalf("scopeless task must not warn, got %q", w)
}
}
Loading