Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(scheduler): fire on_task_timeout for tasks that time out out of id order

thejesh231aaa7a479b31
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b8e3766..ea63b83 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
+- **Task timeout triggers fire for every task**: `on_task_timeout` used the task id as a watermark, but tasks time out in deadline order. Once a newer task timed out first, an older task that timed out later was never fired for. Fired timeouts are now kept in a persisted set, like met goals; on first start the set is seeded with the tasks already timed out, so nothing replays.
- **LLM connection-test logs stay one line**: the response body written by a connection test has CR and LF replaced with spaces before it is logged, so the body cannot start a second log record.
- **Benchmark judge validation**: grading and the judge's falsification battery require one verdict per ground-truth ID, actual boolean decisions and gates, and matches to the supplied scanner findings. Invalid grading responses use the existing three-attempt budget and abort without writing scores, rather than counting duplicate IDs or truthy strings such as `"false"`.
- **Live log subscribers can no longer crash the process**: a client disconnect closes its log channel. The sink now delivers a line while it still holds the subscriber lock, so that close cannot land in the middle of the send.
diff --git a/db/triggers.go b/db/triggers.go
index 932de91..98ab3ba 100644
--- a/db/triggers.go
+++ b/db/triggers.go
@@ -189,13 +189,14 @@ ORDER BY n.id`, lastID)
return out, rows.Err()
}
-// TimedOutTasksSince returns tasks that reached status='timeout' with id > lastID,
-// ordered by id (monotonic watermark → no double-fire across restarts).
-func (d *DB) TimedOutTasksSince(lastID int64) ([]TaskEvent, error) {
+// TimedOutTasks returns all live tasks in status='timeout', ordered by id. Tasks
+// time out in deadline order, not id order, so the scheduler filters out the
+// ones it already fired for via its persisted fired-set, not an id watermark.
+func (d *DB) TimedOutTasks() ([]TaskEvent, error) {
rows, err := d.Query(`
SELECT id, description, goal FROM tasks
-WHERE status='timeout' AND deleted_at IS NULL AND id > $1
-ORDER BY id`, lastID)
+WHERE status='timeout' AND deleted_at IS NULL
+ORDER BY id`)
if err != nil {
return nil, err
}
@@ -206,7 +207,7 @@ ORDER BY id`, lastID)
if err := rows.Scan(&e.NodeID, &e.TaskDesc, &e.TaskGoal); err != nil {
return nil, err
}
- e.TaskID = e.NodeID // task id doubles as NodeID for the watermark
+ e.TaskID = e.NodeID // task id doubles as NodeID for the fired-set
e.Summary = e.TaskGoal
out = append(out, e)
}
diff --git a/server/scheduler.go b/server/scheduler.go
index 0ad124f..cd07c67 100644
--- a/server/scheduler.go
+++ b/server/scheduler.go
@@ -28,7 +28,7 @@ type Scheduler struct {
const (
schedKeyLastFinding = "last_finding_id" // watermark: max finding node id fired for
schedKeyFiredGoals = "fired_goals" // JSON array of goal node ids already fired
- schedKeyLastTimeout = "last_timeout_id" // watermark: max task id fired for task timeout
+ schedKeyFiredTimeouts = "fired_timeouts" // JSON array of task ids already fired for task timeout
schedKeyLastToolCall = "last_toolcall_id" // watermark: max activity id fired for tool call
schedKeyLastTaskCreate = "last_taskcreate_id" // watermark: max task id fired for task create
)
@@ -77,18 +77,16 @@ func (sc *Scheduler) init() {
set[e.NodeID] = true
}
}
- sc.saveFiredGoalSet(set)
+ sc.saveFiredSet(schedKeyFiredGoals, set)
}
- if sc.mustState(schedKeyLastTimeout) == "" {
- var maxID int64
- if evs, err := sc.pg.TimedOutTasksSince(0); err == nil {
+ if sc.mustState(schedKeyFiredTimeouts) == "" {
+ set := map[int64]bool{}
+ if evs, err := sc.pg.TimedOutTasks(); err == nil {
for _, e := range evs {
- if e.NodeID > maxID {
- maxID = e.NodeID
- }
+ set[e.NodeID] = true
}
}
- _ = sc.pg.SetSchedState(schedKeyLastTimeout, strconv.FormatInt(maxID, 10))
+ sc.saveFiredSet(schedKeyFiredTimeouts, set)
}
if sc.mustState(schedKeyLastToolCall) == "" {
var maxID int64
@@ -195,7 +193,7 @@ func (sc *Scheduler) fireGoals(triggers []*db.AgentTrigger) {
}
// Mark goals as consumed whether or not a trigger is active, so enabling an
// on_goal_met trigger later doesn't replay every already-met goal.
- fired := sc.firedGoalSet()
+ fired := sc.firedSet(schedKeyFiredGoals)
changed := false
for _, e := range events {
if fired[e.NodeID] {
@@ -212,12 +210,14 @@ func (sc *Scheduler) fireGoals(triggers []*db.AgentTrigger) {
}
}
if changed {
- sc.saveFiredGoalSet(fired)
+ sc.saveFiredSet(schedKeyFiredGoals, fired)
}
}
// fireTaskTimeouts fires on_task_timeout triggers for tasks that newly reached
-// status='timeout' above the persisted watermark (task id → no double-fire).
+// status='timeout'. Tasks time out in deadline order, not id order, so fired
+// tasks are kept in a persisted set (like goals) rather than an id watermark:
+// a watermark would skip an older task that times out after a newer one.
func (sc *Scheduler) fireTaskTimeouts(triggers []*db.AgentTrigger) {
var want []*db.AgentTrigger
for _, tr := range triggers {
@@ -225,17 +225,8 @@ func (sc *Scheduler) fireTaskTimeouts(triggers []*db.AgentTrigger) {
want = append(want, tr)
}
}
- last, _ := strconv.ParseInt(sc.mustState(schedKeyLastTimeout), 10, 64)
- events, err := sc.pg.TimedOutTasksSince(last)
- if err != nil || len(events) == 0 {
- return
- }
- // Advance the watermark even with no active trigger — see fireFindings.
- maxID := last
- for _, e := range events {
- if e.NodeID > maxID {
- maxID = e.NodeID
- }
+ // Consume the timeouts even with no active trigger — see fireFindings.
+ for _, e := range sc.newTaskTimeouts() {
if len(want) == 0 {
continue
}
@@ -244,7 +235,28 @@ func (sc *Scheduler) fireTaskTimeouts(triggers []*db.AgentTrigger) {
sc.s.StartTriggeredRun(tr.AgentKey, fmt.Sprintf("Timeout trigger · task#%d", e.TaskID), tr.TaskTimeoutMessage+msgCtx, e.TaskID, true, e.TaskDesc, e.TaskGoal)
}
}
- _ = sc.pg.SetSchedState(schedKeyLastTimeout, strconv.FormatInt(maxID, 10))
+}
+
+// newTaskTimeouts returns the timed-out tasks not fired for yet and records
+// them as consumed.
+func (sc *Scheduler) newTaskTimeouts() []db.TaskEvent {
+ events, err := sc.pg.TimedOutTasks()
+ if err != nil || len(events) == 0 {
+ return nil
+ }
+ fired := sc.firedSet(schedKeyFiredTimeouts)
+ var out []db.TaskEvent
+ for _, e := range events {
+ if fired[e.NodeID] {
+ continue
+ }
+ fired[e.NodeID] = true
+ out = append(out, e)
+ }
+ if len(out) > 0 {
+ sc.saveFiredSet(schedKeyFiredTimeouts, fired)
+ }
+ return out
}
// fireTaskCreates fires on_task_create triggers for tasks newly created above the
@@ -347,9 +359,9 @@ func (sc *Scheduler) mustState(key string) string {
return v
}
-func (sc *Scheduler) firedGoalSet() map[int64]bool {
+func (sc *Scheduler) firedSet(key string) map[int64]bool {
out := map[int64]bool{}
- raw := sc.mustState(schedKeyFiredGoals)
+ raw := sc.mustState(key)
if raw == "" {
return out
}
@@ -362,13 +374,13 @@ func (sc *Scheduler) firedGoalSet() map[int64]bool {
return out
}
-func (sc *Scheduler) saveFiredGoalSet(set map[int64]bool) {
+func (sc *Scheduler) saveFiredSet(key string, set map[int64]bool) {
ids := make([]int64, 0, len(set))
for id := range set {
ids = append(ids, id)
}
b, _ := json.Marshal(ids)
- if err := sc.pg.SetSchedState(schedKeyFiredGoals, string(b)); err != nil {
- log.Printf("[scheduler] save fired goals failed: %v", err)
+ if err := sc.pg.SetSchedState(key, string(b)); err != nil {
+ log.Printf("[scheduler] save %s failed: %v", key, err)
}
}
diff --git a/server/scheduler_timeout_test.go b/server/scheduler_timeout_test.go
new file mode 100644
index 0000000..434ae47
--- /dev/null
+++ b/server/scheduler_timeout_test.go
@@ -0,0 +1,58 @@
+package server
+
+import (
+ "testing"
+
+ "github.com/Autumn-27/artex/db"
+)
+
+// Tasks time out in deadline order, not in id order: a task created later with
+// a shorter timeout can time out first. Each timeout must still fire once.
+func TestSchedulerTaskTimeoutsFireOutOfIDOrder(t *testing.T) {
+ skipWithoutDSN(t)
+ m, err := NewManager(t.TempDir(), "")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { m.Close() })
+ sc := &Scheduler{pg: m.pg}
+ sc.newTaskTimeouts() // consume timeouts other tests left behind
+
+ var ids [2]int64
+ for i := range ids {
+ task, err := m.pg.CreateTaskWithOptions("timeout order", "goal", db.TaskCreateOptions{})
+ if err != nil {
+ t.Fatal(err)
+ }
+ ids[i] = task.ID
+ t.Cleanup(func() { _ = m.pg.DeleteTask(task.ID) })
+ }
+ older, newer := ids[0], ids[1]
+ timeOut := func(id int64) {
+ t.Helper()
+ if _, err := m.pg.Exec(`UPDATE tasks SET status='timeout' WHERE id=$1`, id); err != nil {
+ t.Fatal(err)
+ }
+ }
+ fired := func() []int64 {
+ var out []int64
+ for _, e := range sc.newTaskTimeouts() {
+ if e.TaskID == older || e.TaskID == newer {
+ out = append(out, e.TaskID)
+ }
+ }
+ return out
+ }
+
+ timeOut(newer)
+ if got := fired(); len(got) != 1 || got[0] != newer {
+ t.Fatalf("after the newer task timed out: fired %v, want [%d]", got, newer)
+ }
+ timeOut(older)
+ if got := fired(); len(got) != 1 || got[0] != older {
+ t.Fatalf("after the older task timed out: fired %v, want [%d]", got, older)
+ }
+ if got := fired(); len(got) != 0 {
+ t.Fatalf("second scan fired %v again, want nothing", got)
+ }
+}