Compare commits

..
Author SHA1 Message Date
BaaRai 2c514a4cd9 perf(server): look up only the requested task archive blocker in getTask
getTask aggregated task_relations for the whole database on every 5-second detail poll to read one entry. TaskArchiveBlocker scopes the same predicate (shared with TaskArchiveBlockers through archiveBlockerScope) to one source task; listTasks keeps the global map.
2026-11-16 06:46:52 +01:00
BaaRai d005bfe5ba Merge pull request #250 from BaaRai/fix/llm-usage-ts-index
fix(db): index llm_usage.ts for the usage time-window queries
2026-11-16 00:28:24 +01:00
5 changed files with 67 additions and 9 deletions
+22 -5
View File
@@ -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"`
+20
View File
@@ -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) {
+9 -2
View File
@@ -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.
+4 -2
View File
@@ -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)
}
+12
View File
@@ -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) {