Compare commits

...
Author SHA1 Message Date
BaaRai bec7320602 server: write the empty sandbox egress warning once per task start
The warning added in #833 ran in Engine.Run before the started guard, so
every Run call on a task that was already running (resume, intent rerun,
finding follow-up, goal decomposition, POST /api/active) appended another
"sandbox egress empty" activity and log line. It is now checked after the
task has really started, in sandbox=required mode only. The message and
the detection are unchanged, and the sandbox=off note keeps its behaviour.
2026-12-02 17:49:25 +01:00
3 changed files with 209 additions and 11 deletions
+1
View File
@@ -24,6 +24,7 @@ All notable changes to this project are documented in this file. The format is b
- **The OpenAI Responses format no longer defaults to `gpt-5`**: OpenAI shuts `gpt-5` down on 2026-12-11 and lists `gpt-6.2-sol` as its replacement, so `ARTEX_LLM_PROVIDER=openai-responses` without `ARTEX_LLM_MODEL` (or a profile saved through the API without a model) would start failing then. The default is now `gpt-6.2-sol`; a model set in the profile or in `ARTEX_LLM_MODEL` is kept as is. A gateway set with `ARTEX_LLM_BASE_URL` that does not serve `gpt-6.2-sol` needs `ARTEX_LLM_MODEL` set explicitly.
- **`start.sh` stops during a crash backoff**: a SIGTERM or SIGINT that arrived while the supervisor slept before a restart was handled only after the sleep, and the loop then started `artex` again and waited on it, so `docker stop` waited out its grace period and SIGKILLed a freshly started server. The backoff sleep can now be interrupted, and the loop checks for a pending stop before each launch. `scripts/test-start-sh.sh` covers it in CI.
- **A paused task no longer times out**: the deadline coordinator kept counting while a task was paused, so a task paused past its deadline (or restored paused after a restart) ran a final planner round and was set to `timeout` before it could be resumed, and after a resume the engine still used the deadline cached before the pause. The coordinator now waits while the task is paused, and resuming refreshes the cached deadline with the paused interval credited back (#1).
- **The empty sandbox egress warning is written once per task start.** In `ARTEX_SANDBOX=required` mode, the warning added in #833 for a task whose scope rows grant no egress was appended again, as an activity and a log line, on every `Engine.Run` call. The engine calls `Run` again on a task that is already running (resume, intent rerun, finding follow-up, goal decomposition, the active-task endpoint), so the activity feed filled up with copies. The check now runs only when the task really starts.
### Removed
+14 -11
View File
@@ -952,17 +952,6 @@ 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",
@@ -986,6 +975,20 @@ func (e *Engine) Run(ctx context.Context, t *Task) {
e.started.Delete(t.ID)
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.
// Checked only once the task really starts: Run is called again on a task
// that is already running (resume, intent rerun, follow-ups), and each of
// those calls must not append another warning.
if e.sandboxMode == sandbox.ModeRequired && 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})
}
}
}
e.touch(t.ID)
runTaskRoutine(rt, func(loopCtx context.Context) { e.plannerLoop(loopCtx, t) })
runTaskRoutine(rt, func(loopCtx context.Context) { e.claimSweepLoop(loopCtx, t) })
@@ -0,0 +1,194 @@
package server
import (
"context"
"strconv"
"strings"
"testing"
"time"
"github.com/Autumn-27/artex/db"
"github.com/Autumn-27/artex/sandbox"
)
const (
emptyEgressPrefix = "sandbox egress empty"
sandboxOffPrefix = "sandbox=off"
)
// TestEmptyEgressWarningOncePerStart pins the #830 warning (added in #833) to a
// real task start. Engine.Run is called again on a task that is already
// running (resume, intent rerun, finding follow-up, POST /api/active, goal
// decomposition), and those calls only nudge the planner: they must not append
// another "sandbox egress empty" activity or log line each time.
func TestEmptyEgressWarningOncePerStart(t *testing.T) {
skipWithoutDSN(t)
m, err := NewManager(t.TempDir(), "")
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.Close() })
if err := m.SetWorkers(1); err != nil {
t.Fatal(err)
}
suffix := strconv.FormatInt(time.Now().UnixNano(), 36)
approvedCo, _, _, _, _, err := m.pg.Companies().CreateCompanyWithScope("WarnOnceCo-"+suffix, "",
[]db.ScopeInput{{Kind: "cidr", Value: "203.0.113.0/24"}}, "test fixture", true)
if err != nil {
t.Fatalf("CreateCompanyWithScope: %v", err)
}
newTask := func(t *testing.T, name string, opts db.TaskCreateOptions) *Task {
t.Helper()
task, err := m.CreateTaskWithOptions(name+"-"+suffix, "goal", opts)
if err != nil {
t.Fatalf("CreateTaskWithOptions(%s): %v", name, err)
}
t.Cleanup(func() { _ = m.pg.DeleteTask(mustTaskID(t, task.ID)) })
return task
}
newEngine := func(t *testing.T, mode string, task *Task) *Engine {
t.Helper()
e := NewEngine(m)
e.SetSandbox(&stubSandboxClient{health: sandbox.HealthResponse{OK: true, Docker: true}}, mode, t.TempDir())
t.Cleanup(func() { e.StopTask(task.ID) })
return e
}
// run calls Engine.Run n times and returns how many warning log lines it wrote.
run := func(t *testing.T, e *Engine, task *Task, n int) int {
t.Helper()
out := captureLog(t, func() {
for i := 0; i < n; i++ {
e.Run(context.Background(), task)
}
})
return strings.Count(out, "[sandbox] task "+task.ID+": "+emptyEgressPrefix)
}
count := func(t *testing.T, task *Task) (warn, off int) {
t.Helper()
acts, _, err := task.Store.ActivityList(nil, 0, 500)
if err != nil {
t.Fatalf("ActivityList: %v", err)
}
for _, a := range acts {
switch {
case strings.HasPrefix(a.Summary, emptyEgressPrefix):
warn++
if !a.IsError || a.Kind != "system" {
t.Errorf("warning activity must be a system error, got kind=%q is_error=%v", a.Kind, a.IsError)
}
case strings.HasPrefix(a.Summary, sandboxOffPrefix):
off++
}
}
return warn, off
}
// The trap of #830: the agent stamped the company link, nothing is approved.
t.Run("agent stamped company warns once per start", func(t *testing.T) {
task := newTask(t, "warn-once-trap", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}, CompanyScopeByAgent: true})
e := newEngine(t, sandbox.ModeRequired, task)
if logs := run(t, e, task, 3); logs != 1 {
t.Errorf("3 Run calls wrote %d warning log lines, want 1", logs)
}
if warn, off := count(t, task); warn != 1 || off != 0 {
t.Fatalf("3 Run calls on one engine: warnings=%d off-notes=%d, want 1 and 0", warn, off)
}
// A new engine on the same task is a new real start (process restart, boot
// restore): it warns again, once.
e.StopTask(task.ID)
e2 := newEngine(t, sandbox.ModeRequired, task)
if logs := run(t, e2, task, 2); logs != 1 {
t.Errorf("restart: 2 Run calls wrote %d warning log lines, want 1", logs)
}
if warn, _ := count(t, task); warn != 2 {
t.Fatalf("after a restart: warnings=%d, want 2 (one per real start)", warn)
}
})
// The engine already runs the task (its own start is where the warning
// belongs), so every later Run is a nudge and never warns.
t.Run("already started task never warns on a nudge", func(t *testing.T) {
task := newTask(t, "warn-once-started", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}, CompanyScopeByAgent: true})
e := newEngine(t, sandbox.ModeRequired, task)
e.started.Store(task.ID, true)
if logs := run(t, e, task, 3); logs != 0 {
t.Errorf("3 nudges wrote %d warning log lines, want 0", logs)
}
if warn, _ := count(t, task); warn != 0 {
t.Fatalf("3 nudges on a running task: warnings=%d, want 0", warn)
}
})
t.Run("agent added root domain warns once", func(t *testing.T) {
task := newTask(t, "warn-once-agent-domain", db.TaskCreateOptions{})
if _, err := m.pg.Assets().AddAgentScope(mustTaskID(t, task.ID), "root_domain", "agent.example", "test fixture", "agent"); err != nil {
t.Fatalf("AddAgentScope: %v", err)
}
e := newEngine(t, sandbox.ModeRequired, task)
run(t, e, task, 2)
if warn, _ := count(t, task); warn != 1 {
t.Fatalf("agent root_domain only: warnings=%d, want 1", warn)
}
})
// A non-empty allowlist never warns, however many times Run is called.
quiet := []struct {
name string
setup func(t *testing.T) *Task
}{
{"operator selected approved company", func(t *testing.T) *Task {
return newTask(t, "warn-once-op-company", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}})
}},
{"agent company plus intercept allow rule", func(t *testing.T) *Task {
task := newTask(t, "warn-once-intercept", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}, CompanyScopeByAgent: true})
if _, err := m.pg.Exec(`INSERT INTO task_intercept_rules(task_id, enabled, action, kind, pattern) VALUES ($1,true,'allow','domain_suffix','example.test')`, mustTaskID(t, task.ID)); err != nil {
t.Fatalf("insert intercept rule: %v", err)
}
return task
}},
{"agent company approved by the operator", func(t *testing.T) *Task {
task := newTask(t, "warn-once-approved", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}, CompanyScopeByAgent: true})
if _, err := m.pg.Assets().AddOperatorScope(mustTaskID(t, task.ID), "company", strconv.FormatInt(approvedCo, 10), "test fixture"); err != nil {
t.Fatalf("AddOperatorScope(company): %v", err)
}
return task
}},
{"operator root domain", func(t *testing.T) *Task {
task := newTask(t, "warn-once-op-domain", db.TaskCreateOptions{})
if _, err := m.pg.Assets().AddOperatorScope(mustTaskID(t, task.ID), "root_domain", "operator.example", "test fixture"); err != nil {
t.Fatalf("AddOperatorScope(root_domain): %v", err)
}
return task
}},
}
for _, tc := range quiet {
t.Run(tc.name+" does not warn", func(t *testing.T) {
task := tc.setup(t)
e := newEngine(t, sandbox.ModeRequired, task)
run(t, e, task, 2)
if warn, off := count(t, task); warn != 0 || off != 0 {
t.Fatalf("non-empty allowlist: warnings=%d off-notes=%d, want 0 and 0", warn, off)
}
})
}
// sandbox=off has no egress proxy: no egress warning. The off note keeps its
// existing behaviour (one per Run call, not changed here) and must not grow.
t.Run("sandbox off never warns", func(t *testing.T) {
task := newTask(t, "warn-once-off", db.TaskCreateOptions{CompanyIDs: []int64{approvedCo}, CompanyScopeByAgent: true})
e := newEngine(t, sandbox.ModeOff, task)
const runs = 3
if logs := run(t, e, task, runs); logs != 0 {
t.Errorf("sandbox=off wrote %d egress warning log lines, want 0", logs)
}
warn, off := count(t, task)
t.Logf("sandbox=off: %d off-notes after %d Run calls", off, runs)
if warn != 0 {
t.Fatalf("sandbox=off: warnings=%d, want 0", warn)
}
if off < 1 || off > runs {
t.Fatalf("sandbox=off: off-notes=%d after %d Run calls, want between 1 and %d", off, runs, runs)
}
})
}