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)

perf(server): look up only the requested task archive blocker in getTask

BaaRai2c514a4cd98c
diff --git a/db/task_archives.go b/db/task_archives.go
index adadbb7..f312000 100644
--- a/db/task_archives.go
+++ b/db/task_archives.go
@@ -79,15 +79,20 @@ type TaskArchive struct {
UpdatedAt time.Time `json:"updated_at"`
}
+// archiveBlockerScope selects the live direct dependents that block archiving
+// their source task. TaskArchiveBlockers and TaskArchiveBlocker share it so the
+// list and the detail view cannot disagree.
+const archiveBlockerScope = `
+FROM task_relations relation
+JOIN tasks child ON child.id=relation.task_id AND child.deleted_at IS NULL
+LEFT JOIN task_archives pending ON pending.task_id=child.id
+WHERE (pending.id IS NULL OR pending.state NOT IN ('archive_queued','archiving'))`
+
// TaskArchiveBlockers returns one live direct dependent for every source task
// that cannot currently be archived. Dependents already queued for archiving do
// not block their source because the FIFO worker will compact them first.
func (d *DB) TaskArchiveBlockers() (map[int64]int64, error) {
- rows, err := d.Query(`SELECT relation.source_task_id, MIN(child.id)
-FROM task_relations relation
-JOIN tasks child ON child.id=relation.task_id AND child.deleted_at IS NULL
-LEFT JOIN task_archives pending ON pending.task_id=child.id
-WHERE pending.id IS NULL OR pending.state NOT IN ('archive_queued','archiving')
+ rows, err := d.Query(`SELECT relation.source_task_id, MIN(child.id)` + archiveBlockerScope + `
GROUP BY relation.source_task_id`)
if err != nil {
return nil, err
@@ -104,6 +109,18 @@ GROUP BY relation.source_task_id`)
return blockers, rows.Err()
}
+// TaskArchiveBlocker returns the live direct dependent that blocks taskID, or
+// 0 when nothing blocks it. Same rules as TaskArchiveBlockers, scoped to one
+// source task so getTask does not aggregate the whole relation table per poll.
+func (d *DB) TaskArchiveBlocker(taskID int64) (int64, error) {
+ var dependent sql.NullInt64
+ if err := d.QueryRow(`SELECT MIN(child.id)`+archiveBlockerScope+`
+AND relation.source_task_id=$1`, taskID).Scan(&dependent); err != nil {
+ return 0, err
+ }
+ return dependent.Int64, nil
+}
+
type TaskArchivePage struct {
Items []TaskArchive `json:"items"`
Total int `json:"total"`
diff --git a/db/task_archives_test.go b/db/task_archives_test.go
index b016777..aefab19 100644
--- a/db/task_archives_test.go
+++ b/db/task_archives_test.go
@@ -362,6 +362,7 @@ func TestTaskArchiveBlockersIgnoreQueuedDependents(t *testing.T) {
if blockers[source.ID] != dependent.ID {
t.Fatalf("source blocker=%d, want dependent %d", blockers[source.ID], dependent.ID)
}
+ assertPerTaskBlockers(t, d, blockers, map[int64]int64{source.ID: dependent.ID, dependent.ID: 0})
if err := d.SetPaused(dependent.ID, true); err != nil {
t.Fatal(err)
}
@@ -375,6 +376,25 @@ func TestTaskArchiveBlockersIgnoreQueuedDependents(t *testing.T) {
if blocker := blockers[source.ID]; blocker != 0 {
t.Fatalf("queued dependent must not block source, got %d", blocker)
}
+ assertPerTaskBlockers(t, d, blockers, map[int64]int64{source.ID: 0, dependent.ID: 0})
+}
+
+// assertPerTaskBlockers checks TaskArchiveBlocker against the expected blocker
+// of each task and against the global TaskArchiveBlockers map.
+func assertPerTaskBlockers(t *testing.T, d *DB, global map[int64]int64, want map[int64]int64) {
+ t.Helper()
+ for taskID, wantBlocker := range want {
+ got, err := d.TaskArchiveBlocker(taskID)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got != wantBlocker {
+ t.Fatalf("TaskArchiveBlocker(%d)=%d, want %d", taskID, got, wantBlocker)
+ }
+ if got != global[taskID] {
+ t.Fatalf("TaskArchiveBlocker(%d)=%d, TaskArchiveBlockers()[%d]=%d", taskID, got, taskID, global[taskID])
+ }
+ }
}
func TestRecoverInterruptedArchiveRequiresManualRetry(t *testing.T) {
diff --git a/server/dto.go b/server/dto.go
index 2a9963d..5ef895d 100644
--- a/server/dto.go
+++ b/server/dto.go
@@ -82,9 +82,16 @@ func applyTaskArchiveBlocker(dto *TaskDTO, blockers map[int64]int64) {
if err != nil {
return
}
- if dependentID := blockers[taskID]; dependentID > 0 {
- dto.ArchiveBlockedBy = strconv.FormatInt(dependentID, 10)
+ applyTaskArchiveBlockerID(dto, blockers[taskID])
+}
+
+// applyTaskArchiveBlockerID records dependentID as the task's archive blocker;
+// 0 (nothing blocks it) leaves the DTO unchanged.
+func applyTaskArchiveBlockerID(dto *TaskDTO, dependentID int64) {
+ if dto == nil || dependentID <= 0 {
+ return
}
+ dto.ArchiveBlockedBy = strconv.FormatInt(dependentID, 10)
}
// TokenTotalDTO is a whole-task (all agents) token aggregate.
diff --git a/server/server.go b/server/server.go
index 56e7473..3840c8f 100644
--- a/server/server.go
+++ b/server/server.go
@@ -1932,8 +1932,10 @@ func (s *Server) getTask(w http.ResponseWriter, r *http.Request) {
log.Printf("[tasks] detail metrics query failed for %s — metrics zeroed: %v", t.ID, err)
}
s.applyTaskMetrics(&dto, t, metrics)
- archiveBlockers, _ := s.m.PG().TaskArchiveBlockers()
- applyTaskArchiveBlocker(&dto, archiveBlockers)
+ // Best effort: only this task's blocker, not the whole relation table.
+ taskID, _ := strconv.ParseInt(t.ID, 10, 64)
+ blocker, _ := s.m.PG().TaskArchiveBlocker(taskID)
+ applyTaskArchiveBlockerID(&dto, blocker)
writeJSON(w, 200, dto)
}
diff --git a/server/task_llm_test.go b/server/task_llm_test.go
index 88a113f..afd2caa 100644
--- a/server/task_llm_test.go
+++ b/server/task_llm_test.go
@@ -184,6 +184,18 @@ func TestApplyTaskArchiveBlocker(t *testing.T) {
if !strings.Contains(string(raw), `"archive_blocked_by_task_id":"11"`) {
t.Fatalf("archive blocker missing from payload: %s", raw)
}
+
+ byID := taskDTO(&Task{ID: "7"}, "paused")
+ applyTaskArchiveBlockerID(&byID, 12)
+ if byID.ArchiveBlockedBy != "12" {
+ t.Fatalf("archive blocker by id=%q, want 12", byID.ArchiveBlockedBy)
+ }
+ none := taskDTO(&Task{ID: "7"}, "paused")
+ applyTaskArchiveBlockerID(&none, 0)
+ if none.ArchiveBlockedBy != "" {
+ t.Fatalf("no blocker set %q", none.ArchiveBlockedBy)
+ }
+ applyTaskArchiveBlockerID(nil, 12) // must not panic
}
func TestTaskLLMStateSnapshotIsConcurrentAndDetached(t *testing.T) {