mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
170 lines
6.7 KiB
Go
170 lines
6.7 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"github.com/Autumn-27/artex/agent"
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/norma/llm"
|
|
"iter"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type planningLoopProvider struct {
|
|
calls atomic.Int32
|
|
response func(int, llm.CompletionRequest) (llm.Message, string, error)
|
|
}
|
|
|
|
func (p *planningLoopProvider) Complete(ctx context.Context, r llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
|
|
m, s, e := p.response(int(p.calls.Add(1)), r)
|
|
return m, s, llm.Usage{}, e
|
|
}
|
|
func (p *planningLoopProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
|
|
return func(y func(llm.StreamEvent, error) bool) { y(llm.StreamEvent{}, context.Canceled) }
|
|
}
|
|
func planningText(text string) llm.Message {
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{{Type: llm.BlockText, Text: text}}}
|
|
}
|
|
func planningTool(name string, input any) llm.Message {
|
|
raw, _ := json.Marshal(input)
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{{Type: llm.BlockToolUse, ID: "loop-tool", Name: name, Input: raw}}}
|
|
}
|
|
func planningLoopFixture(t *testing.T) (*Manager, *Task, *Engine) {
|
|
t.Helper()
|
|
skipWithoutDSN(t)
|
|
m, err := NewManager(t.TempDir(), "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { m.Close() })
|
|
task, err := m.CreateTaskWithOptions("Offline regression: compare supplied fixtures, no network or commands.", "Compare fixture A and B; keep goal open until comparison is checked", db.TaskCreateOptions{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = task.Store.AddGoal(map[string]any{"text": task.Goal}, "fixture"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = m.SetTaskStatus(task.ID, "running"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = m.SetWorkers(1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e := NewEngine(m)
|
|
e.debounce = time.Millisecond
|
|
t.Cleanup(func() { e.StopTask(task.ID); m.pg.DeleteTask(mustTaskID(t, task.ID)) })
|
|
return m, task, e
|
|
}
|
|
func fixtureWorker(t *testing.T) (*agent.Worker, *planningLoopProvider) {
|
|
t.Helper()
|
|
p := &planningLoopProvider{response: func(call int, _ llm.CompletionRequest) (llm.Message, string, error) {
|
|
if call == 1 {
|
|
return planningTool("record_fact", map[string]any{"summary": "Fixture worker executed the queued comparison", "evidence": "fixture A=2, fixture B=3; difference=1", "checkpoint": "Comparison recorded; final acceptance still pending"}), "tool_use", nil
|
|
}
|
|
return planningText("Fixture comparison recorded; final goal acceptance is still pending."), "end_turn", nil
|
|
}}
|
|
w := agent.NewWorker(p, "fixture-worker", t.TempDir(), nil, 100000, 4)
|
|
w.SetNonStreaming(func() bool { return true })
|
|
return w, p
|
|
}
|
|
func waitPlanning(t *testing.T, limit time.Duration, check func() bool) {
|
|
t.Helper()
|
|
until := time.Now().Add(limit)
|
|
for time.Now().Before(until) {
|
|
if check() {
|
|
return
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
t.Fatal("planning condition did not become true before timeout")
|
|
}
|
|
|
|
func TestPlannerProgressEngineDispatchClaimExecute(t *testing.T) {
|
|
m, task, e := planningLoopFixture(t)
|
|
provider := &planningLoopProvider{response: func(call int, _ llm.CompletionRequest) (llm.Message, string, error) {
|
|
if call == 1 {
|
|
return planningText("I will arrange the comparison."), "end_turn", nil
|
|
}
|
|
if call == 2 {
|
|
return planningTool("add_intent", map[string]any{"summary": "Compare supplied fixture A=2 and B=3, record difference without external access"}), "tool_use", nil
|
|
}
|
|
return planningText("Comparison has been queued."), "end_turn", nil
|
|
}}
|
|
p := agent.NewPlanner(provider, "fixture", t.TempDir(), nil, 100000, 4)
|
|
p.SetNonStreaming(func() bool { return true })
|
|
w, workerProvider := fixtureWorker(t)
|
|
e.SetAuthoritativeAgentResolver(func(*Task) (*agent.Planner, *agent.Worker) { return p, w })
|
|
e.Run(t.Context(), task)
|
|
waitPlanning(t, 5*time.Second, func() bool {
|
|
snap, err := task.Store.PlanningSnapshot(t.Context())
|
|
return err == nil && snap.Done == 1
|
|
})
|
|
e.StopTask(task.ID)
|
|
var facts int
|
|
if err := m.pg.QueryRow(`SELECT count(*) FROM exploration_nodes WHERE exploration_id=$1 AND kind='fact' AND payload->>'summary'='Fixture worker executed the queued comparison'`, task.Store.ID()).Scan(&facts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if workerProvider.calls.Load() < 2 || facts != 1 || m.TaskStatus(task.ID) != "running" {
|
|
t.Fatalf("calls=%d facts=%d status=%s", workerProvider.calls.Load(), facts, m.TaskStatus(task.ID))
|
|
}
|
|
t.Logf("actual engine: planner=%d worker=%d persisted_worker_facts=%d task=%s", provider.calls.Load(), workerProvider.calls.Load(), facts, m.TaskStatus(task.ID))
|
|
}
|
|
|
|
func TestPlannerProgressEngineRestartAndResume(t *testing.T) {
|
|
_, task, e := planningLoopFixture(t)
|
|
provider := &planningLoopProvider{response: func(int, llm.CompletionRequest) (llm.Message, string, error) {
|
|
return planningText("No step generated."), "end_turn", nil
|
|
}}
|
|
p := agent.NewPlanner(provider, "fixture", t.TempDir(), nil, 100000, 4)
|
|
p.SetNonStreaming(func() bool { return true })
|
|
w, _ := fixtureWorker(t)
|
|
e.SetAuthoritativeAgentResolver(func(*Task) (*agent.Planner, *agent.Worker) { return p, w })
|
|
snap, err := task.Store.PlanningSnapshot(t.Context())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for range 3 {
|
|
if err = task.Store.SavePlanningProgress(t.Context(), snap, "stalled", "persisted empty rounds", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
e.Run(t.Context(), task)
|
|
time.Sleep(150 * time.Millisecond)
|
|
if provider.calls.Load() != 0 {
|
|
t.Fatal("restart ignored durable stalled state")
|
|
}
|
|
e.Resume(task) // an operator resume: lift the pause, then clear the recovery state
|
|
e.ResumePlanning(task)
|
|
waitPlanning(t, 3*time.Second, func() bool { return provider.calls.Load() >= 2 })
|
|
e.StopTask(task.ID)
|
|
}
|
|
|
|
func TestPlannerProgressEngineRecoveryDeadline(t *testing.T) {
|
|
_, task, e := planningLoopFixture(t)
|
|
provider := &planningLoopProvider{response: func(call int, _ llm.CompletionRequest) (llm.Message, string, error) {
|
|
if call == 1 {
|
|
return planningTool("add_intent", map[string]any{"summary": "Perform the supplied offline comparison"}), "tool_use", nil
|
|
}
|
|
return planningText("queued"), "end_turn", nil
|
|
}}
|
|
p := agent.NewPlanner(provider, "fixture", t.TempDir(), nil, 100000, 4)
|
|
p.SetNonStreaming(func() bool { return true })
|
|
w, _ := fixtureWorker(t)
|
|
e.SetAuthoritativeAgentResolver(func(*Task) (*agent.Planner, *agent.Worker) { return p, w })
|
|
snap, err := task.Store.PlanningSnapshot(t.Context())
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
due := time.Now().Add(100 * time.Millisecond)
|
|
if err = task.Store.SavePlanningProgress(t.Context(), snap, "stalled", "retry due shortly", &due); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
e.Run(t.Context(), task)
|
|
waitPlanning(t, 5*time.Second, func() bool {
|
|
snap, err := task.Store.PlanningSnapshot(t.Context())
|
|
return err == nil && snap.Done == 1
|
|
})
|
|
e.StopTask(task.ID)
|
|
}
|