mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
231 lines
8.9 KiB
Go
231 lines
8.9 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/db"
|
|
)
|
|
|
|
// planningRows counts the task's planner_progress rows.
|
|
func planningRows(t *testing.T, g *gateServer, task *Task) int {
|
|
t.Helper()
|
|
var n int
|
|
if err := g.m.pg.QueryRow(`SELECT count(*) FROM planner_progress WHERE exploration_id=$1`, task.ExpID).Scan(&n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return n
|
|
}
|
|
|
|
// seedNeedsAttention stores the planner state that waits for new input or an
|
|
// operator: needs_attention, with no next check.
|
|
func seedNeedsAttention(t *testing.T, g *gateServer, task *Task) {
|
|
t.Helper()
|
|
ctx := t.Context()
|
|
snap, err := task.Store.PlanningSnapshot(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := task.Store.SavePlanningProgress(ctx, snap, "needs_attention", "provider refused the request", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n := planningRows(t, g, task); n != 1 {
|
|
t.Fatalf("seeded planner_progress rows=%d, want 1", n)
|
|
}
|
|
}
|
|
|
|
// resume_planning on a running task clears its planner recovery state and
|
|
// leaves it running, unpaused. Needs PostgreSQL (ARTEX_PG_DSN); skipped
|
|
// without it.
|
|
func TestResumePlanningRunningTask(t *testing.T) {
|
|
g := newGateServer(t)
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
if err := g.m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedNeedsAttention(t, g, task)
|
|
|
|
code, body := g.do(t, "POST", "/api/tasks/"+task.ID+"/control", `{"action":"resume_planning"}`)
|
|
if code != 200 || body["status"] != "running" || body["paused"] != false || body["queued"] != false {
|
|
t.Fatalf("resume_planning: %d %v, want 200 with status running", code, body)
|
|
}
|
|
if n := planningRows(t, g, task); n != 0 {
|
|
t.Fatalf("planner_progress rows=%d after resume_planning, want 0", n)
|
|
}
|
|
if lifecycle := task.lifecycleSnapshot(); lifecycle.Paused || lifecycle.Status != "running" {
|
|
t.Fatalf("lifecycle=%+v, want running and unpaused", lifecycle)
|
|
}
|
|
}
|
|
|
|
// resume_planning on a paused task is the full resume: the pause is lifted,
|
|
// the recovery state cleared, and the result is the one resume returns.
|
|
// Needs PostgreSQL (ARTEX_PG_DSN); skipped without it.
|
|
func TestResumePlanningPausedTaskTakesFullResume(t *testing.T) {
|
|
g := newGateServer(t)
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
if err := g.m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/tasks/" + task.ID + "/control"
|
|
pause := func() {
|
|
t.Helper()
|
|
if code, body := g.do(t, "POST", path, `{"action":"pause"}`); code != 200 || body["paused"] != true {
|
|
t.Fatalf("pause: %d %v", code, body)
|
|
}
|
|
}
|
|
|
|
pause()
|
|
seedNeedsAttention(t, g, task)
|
|
code, viaPlanning := g.do(t, "POST", path, `{"action":"resume_planning"}`)
|
|
if code != 200 {
|
|
t.Fatalf("resume_planning on a paused task: %d %v, want 200", code, viaPlanning)
|
|
}
|
|
if n := planningRows(t, g, task); n != 0 {
|
|
t.Fatalf("planner_progress rows=%d after resume_planning, want 0", n)
|
|
}
|
|
if task.lifecycleSnapshot().Paused || g.s.engine.IsPaused(task.ID) {
|
|
t.Fatalf("task still paused after resume_planning: %+v", task.lifecycleSnapshot())
|
|
}
|
|
|
|
pause()
|
|
code, viaResume := g.do(t, "POST", path, `{"action":"resume"}`)
|
|
if code != 200 || viaPlanning["status"] != viaResume["status"] || viaPlanning["queued"] != viaResume["queued"] ||
|
|
viaPlanning["paused"] != viaResume["paused"] {
|
|
t.Fatalf("resume_planning=%v, resume=%d %v; want the same result", viaPlanning, code, viaResume)
|
|
}
|
|
}
|
|
|
|
// resume_planning on a queued task clears its planner recovery state but
|
|
// keeps the Engine pause, the admission barrier: the task stays queued and
|
|
// paused until it is dequeued. Admission then lifts the pause and starts the
|
|
// planner. Needs PostgreSQL (ARTEX_PG_DSN); skipped without it.
|
|
func TestResumePlanningQueuedTaskKeepsAdmissionBarrier(t *testing.T) {
|
|
g := newGateServer(t)
|
|
defer restoreConcurrencySetting(t, g.m)()
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
t.Cleanup(func() { g.s.engine.StopTask(task.ID) })
|
|
if err := g.m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := g.m.EnqueueTask(task.ID, "resume"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.s.engine.Pause(task.ID, agent.AbortPausedByOrchestrator)
|
|
seedNeedsAttention(t, g, task)
|
|
|
|
code, body := g.do(t, "POST", "/api/tasks/"+task.ID+"/control", `{"action":"resume_planning"}`)
|
|
if code != 200 || body["status"] != "queued" || body["queued"] != true {
|
|
t.Fatalf("resume_planning: %d %v, want 200 with status queued", code, body)
|
|
}
|
|
if n := planningRows(t, g, task); n != 0 {
|
|
t.Fatalf("planner_progress rows=%d after resume_planning, want 0", n)
|
|
}
|
|
if !task.lifecycleSnapshot().Queued || !g.s.engine.IsPaused(task.ID) {
|
|
t.Fatalf("lifecycle=%+v enginePaused=%v, want queued and still paused", task.lifecycleSnapshot(), g.s.engine.IsPaused(task.ID))
|
|
}
|
|
if resets := task.planResets.Load(); resets != 1 {
|
|
t.Fatalf("planResets=%d after resume_planning, want 1", resets)
|
|
}
|
|
|
|
// Admission through the FIFO promotion. With the limit off, the queued
|
|
// task is dequeued whether or not an LLM is configured.
|
|
if err := g.m.SetConcurrency(false, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g.s.reconcileConcurrency()
|
|
if task.lifecycleSnapshot().Queued || g.s.engine.IsPaused(task.ID) || !g.s.engine.Started(task.ID) {
|
|
t.Fatalf("lifecycle=%+v enginePaused=%v started=%v, want admitted, unpaused and started",
|
|
task.lifecycleSnapshot(), g.s.engine.IsPaused(task.ID), g.s.engine.Started(task.ID))
|
|
}
|
|
if n := planningRows(t, g, task); n != 0 {
|
|
t.Fatalf("planner_progress rows=%d after admission, want 0", n)
|
|
}
|
|
if resets := task.planResets.Load(); resets != 1 {
|
|
t.Fatalf("planResets=%d after admission, want 1 (admission adds no reset)", resets)
|
|
}
|
|
}
|
|
|
|
// A terminal task no longer plans: resume_planning is refused and the state
|
|
// kept. Needs PostgreSQL (ARTEX_PG_DSN); skipped without it.
|
|
func TestResumePlanningTerminalTaskConflict(t *testing.T) {
|
|
g := newGateServer(t)
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
if err := g.m.SetTaskStatus(task.ID, "done"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedNeedsAttention(t, g, task)
|
|
|
|
if code, body := g.do(t, "POST", "/api/tasks/"+task.ID+"/control", `{"action":"resume_planning"}`); code != 409 {
|
|
t.Fatalf("resume_planning on a terminal task: %d %v, want 409", code, body)
|
|
}
|
|
if n := planningRows(t, g, task); n != 1 {
|
|
t.Fatalf("planner_progress rows=%d after a refused resume_planning, want 1", n)
|
|
}
|
|
}
|
|
|
|
// A pause whose persistent write fails is compensated with Engine.Resume. That
|
|
// automatic resume lifts the Engine pause but must keep needs_attention: only
|
|
// an operator resume clears it. Needs PostgreSQL (ARTEX_PG_DSN); skipped
|
|
// without it.
|
|
func TestFailedPauseKeepsNeedsAttention(t *testing.T) {
|
|
g := newGateServer(t)
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
if err := g.m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
seedNeedsAttention(t, g, task)
|
|
// The row is already paused in PostgreSQL but not in memory, so
|
|
// ApplyTaskPause fails after the Engine pause was installed.
|
|
if _, err := g.m.pg.Exec(`UPDATE tasks SET paused=true WHERE exploration_id=$1`, task.ExpID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if code, body := g.do(t, "POST", "/api/tasks/"+task.ID+"/control", `{"action":"pause"}`); code == 200 {
|
|
t.Fatalf("pause: %d %v, want the persistent write to fail", code, body)
|
|
}
|
|
if g.s.engine.IsPaused(task.ID) {
|
|
t.Fatal("the failed pause was not compensated: Engine still paused")
|
|
}
|
|
if n := planningRows(t, g, task); n != 1 {
|
|
t.Fatalf("planner_progress rows=%d after a failed pause, want 1 (needs_attention kept)", n)
|
|
}
|
|
}
|
|
|
|
// An operator resume of a paused task clears needs_attention. Needs
|
|
// PostgreSQL (ARTEX_PG_DSN); skipped without it.
|
|
func TestOperatorResumeClearsNeedsAttention(t *testing.T) {
|
|
g := newGateServer(t)
|
|
task := g.newTask(t, db.TaskCreateOptions{})
|
|
if err := g.m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := "/api/tasks/" + task.ID + "/control"
|
|
if code, body := g.do(t, "POST", path, `{"action":"pause"}`); code != 200 || body["paused"] != true {
|
|
t.Fatalf("pause: %d %v", code, body)
|
|
}
|
|
seedNeedsAttention(t, g, task)
|
|
|
|
if code, body := g.do(t, "POST", path, `{"action":"resume"}`); code != 200 {
|
|
t.Fatalf("resume: %d %v, want 200", code, body)
|
|
}
|
|
if n := planningRows(t, g, task); n != 0 {
|
|
t.Fatalf("planner_progress rows=%d after an operator resume, want 0", n)
|
|
}
|
|
if task.lifecycleSnapshot().Paused || g.s.engine.IsPaused(task.ID) {
|
|
t.Fatalf("task still paused after resume: %+v", task.lifecycleSnapshot())
|
|
}
|
|
}
|
|
|
|
// The batch control endpoint keeps accepting only pause and resume.
|
|
func TestBatchControlRefusesResumePlanning(t *testing.T) {
|
|
s := &Server{}
|
|
req := httptest.NewRequest(http.MethodPost, "/api/tasks/control/batch", strings.NewReader(`{"task_ids":["1"],"action":"resume_planning"}`))
|
|
rec := httptest.NewRecorder()
|
|
s.controlTasksBatch(rec, req)
|
|
if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "action must be pause|resume") {
|
|
t.Fatalf("batch resume_planning: %d %s, want 400", rec.Code, rec.Body.String())
|
|
}
|
|
}
|