mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
116 lines
4.4 KiB
Go
116 lines
4.4 KiB
Go
package agent
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"iter"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/Autumn-27/artex/db"
|
|
"github.com/Autumn-27/norma/llm"
|
|
actool "github.com/Autumn-27/norma/tool"
|
|
)
|
|
|
|
type impactProvider struct {
|
|
calls int
|
|
response func(context.Context, int, llm.CompletionRequest) (llm.Message, string, error)
|
|
}
|
|
|
|
func (p *impactProvider) Complete(ctx context.Context, r llm.CompletionRequest) (llm.Message, string, llm.Usage, error) {
|
|
p.calls++
|
|
m, s, e := p.response(ctx, p.calls, r)
|
|
return m, s, llm.Usage{}, e
|
|
}
|
|
func (p *impactProvider) Stream(context.Context, llm.CompletionRequest) iter.Seq2[llm.StreamEvent, error] {
|
|
return func(y func(llm.StreamEvent, error) bool) { y(llm.StreamEvent{}, context.Canceled) }
|
|
}
|
|
func impactTool(name string, args any) llm.Message {
|
|
raw, _ := json.Marshal(args)
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{{Type: llm.BlockToolUse, ID: "impact-tool", Name: name, Input: raw}}}
|
|
}
|
|
func impactText() llm.Message {
|
|
return llm.Message{Role: llm.RoleAssistant, Content: []llm.ContentBlock{{Type: llm.BlockText, Text: "fixture round ended"}}}
|
|
}
|
|
func impactFixture(t *testing.T) (*db.DB, *db.ExplorationStore, int64) {
|
|
t.Helper()
|
|
d := testDB(t)
|
|
t.Cleanup(func() { d.Close() })
|
|
task, err := d.CreateTask("Offline impact review", "Keep fixture goal open", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { d.DeleteTask(task.ID) })
|
|
s := d.Exploration(task.ExplorationID)
|
|
if _, err = s.AddGoal(map[string]any{"text": "Keep fixture goal open"}, "fixture"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return d, s, task.ID
|
|
}
|
|
func impactRecord(t *testing.T, v map[string]any) {
|
|
t.Helper()
|
|
b, _ := json.Marshal(v)
|
|
t.Log(string(b))
|
|
|
|
}
|
|
|
|
// Identical on baseline and patched source: a normal per-round timeout must
|
|
// preserve the SDK's bounded checkpoint phase on a still-live parent context.
|
|
func TestPlanningPreservesBudgetSettlement(t *testing.T) {
|
|
d, s, id := impactFixture(t)
|
|
proofGoal, err := s.AddGoal(map[string]any{"text": "Accept the already observed fixture result"}, "fixture")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
step, err := s.AppendActivity(db.Activity{Worker: "fixture", Kind: "tool_result", Tool: "fixture_compare", Detail: "fixture A=2, B=3; difference=1"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
evidence, err := s.AddNode("fact", map[string]any{"summary": "observed fixture result", "evidence": "fixture A=2, B=3; difference=1", "confidence": "observed", "source": "tool_output", "provenance": factProvenanceRecorded, "provenance_activity_id": step}, 0, "confirmed", "fixture", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old := ToolResolve()
|
|
t.Cleanup(func() { SetToolResolve(old) })
|
|
SetToolResolve(func(_ context.Context, _ string, base []actool.CoreTool) []actool.CoreTool {
|
|
out := []actool.CoreTool{readTool("impact_wait", "Fixture wait until this tool's deadline", obj(map[string]any{}), func(ctx context.Context, _ json.RawMessage) (actool.Result, error) {
|
|
<-ctx.Done()
|
|
return actool.Result{}, ctx.Err()
|
|
})}
|
|
for _, tool := range base {
|
|
if tool.Name() == "prove_goal" {
|
|
out = append(out, tool)
|
|
}
|
|
}
|
|
return out
|
|
})
|
|
provider := &impactProvider{response: func(_ context.Context, n int, _ llm.CompletionRequest) (llm.Message, string, error) {
|
|
switch n {
|
|
case 1:
|
|
return impactTool("impact_wait", map[string]any{}), "tool_use", nil
|
|
case 2:
|
|
return impactTool("prove_goal", map[string]any{"goal_id": proofGoal, "evidence_id": evidence, "reason": "the supplied fixture result was already verified"}), "tool_use", nil
|
|
default:
|
|
return impactText(), "end_turn", nil
|
|
}
|
|
}}
|
|
p := NewPlanner(provider, "fixture", t.TempDir(), nil, 100000, 12)
|
|
p.SetNonStreaming(func() bool { return true })
|
|
p.SetRunTimeout(250 * time.Millisecond)
|
|
ctx, cancel := context.WithTimeout(t.Context(), 3*time.Second)
|
|
defer cancel()
|
|
start := time.Now()
|
|
_, _, err = p.Plan(ctx, id, nil, s, "Keep fixture goal open", nil, nil, nil)
|
|
var facts int
|
|
if e := d.QueryRow(`SELECT count(*) FROM exploration_edges WHERE exploration_id=$1 AND dst_id=$2 AND rel='proves'`, s.ID(), proofGoal).Scan(&facts); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
errorText := ""
|
|
if err != nil {
|
|
errorText = err.Error()
|
|
}
|
|
impactRecord(t, map[string]any{"calls": provider.calls, "persisted_goal_proofs": facts, "elapsed_ms": time.Since(start).Milliseconds(), "parent_alive": ctx.Err() == nil, "error": errorText})
|
|
if facts != 1 {
|
|
t.Errorf("per-round timeout discarded normal settlement: persisted_goal_proofs=%d calls=%d", facts, provider.calls)
|
|
}
|
|
}
|