mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-12-03 13:11:05 +00:00
103 lines
3.5 KiB
Go
103 lines
3.5 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/Autumn-27/artex/db"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestIntentContextSurvivesLargeGraphAndPaginates(t *testing.T) {
|
|
d := testDB(t)
|
|
defer d.Close()
|
|
task, err := d.CreateTask("large handover", "complete referenced work", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.DeleteTask(task.ID)
|
|
s := d.Exploration(task.ExplorationID)
|
|
source, err := s.AddNode("fact", map[string]any{"summary": "original input", "evidence": strings.Repeat("raw", 1000)}, 0, "confirmed", "fixture", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err = d.Exec(`WITH nodes AS (INSERT INTO exploration_nodes(exploration_id,kind,state,payload) SELECT $1,'fact','confirmed','{"summary":"older noise"}'::jsonb FROM generate_series(1,6000) RETURNING id) INSERT INTO exploration_edges(exploration_id,src_id,rel,dst_id) SELECT $1,$2,'derived_from',id FROM nodes`, s.ID(), source)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
intent, err := s.AddIntent(map[string]any{"summary": "use original input"}, 1, nil, "planner")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.Link(source, db.RelDerivedFrom, intent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 20; i++ {
|
|
id, err := s.AddNode("fact", map[string]any{"summary": "additional reference"}, 0, "confirmed", "fixture", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.Link(id, db.RelDerivedFrom, intent); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
tools := NewToolSet(s, "work#1")
|
|
tools.SetOwnerNode(intent)
|
|
seen := map[int64]bool{}
|
|
before := int64(0)
|
|
for {
|
|
raw, _ := json.Marshal(map[string]any{"before": before, "limit": 5})
|
|
res, err := tools.getIntentContext().Call(t.Context(), raw, nil)
|
|
if err != nil || res.IsError {
|
|
t.Fatal(res, err)
|
|
}
|
|
var page struct {
|
|
References []struct{ ID int64 }
|
|
Total int
|
|
More bool `json:"has_more"`
|
|
Before int64 `json:"next_before"`
|
|
}
|
|
if err = json.Unmarshal([]byte(res.Flatten()), &page); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if page.Total != 21 {
|
|
t.Fatal(page.Total)
|
|
}
|
|
for _, n := range page.References {
|
|
if seen[n.ID] {
|
|
t.Fatal("duplicate")
|
|
}
|
|
seen[n.ID] = true
|
|
}
|
|
if !page.More {
|
|
break
|
|
}
|
|
before = page.Before
|
|
}
|
|
if len(seen) != 21 || !seen[source] {
|
|
t.Fatal("handover lost behind graph window")
|
|
}
|
|
if !strings.Contains(tools.renderIntentReferences(t.Context(), intent), "get_intent_context") && !strings.Contains(tools.renderIntentReferences(t.Context(), intent), "next_before") {
|
|
t.Fatal("missing paging continuation")
|
|
}
|
|
other, err := d.CreateTask("other task", "private", nil, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer d.DeleteTask(other.ID)
|
|
foreign, err := d.Exploration(other.ExplorationID).AddIntent(map[string]any{"summary": "foreign"}, 1, nil, "fixture")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := json.Marshal(map[string]any{"intent_id": foreign})
|
|
res, err := tools.getIntentContext().Call(t.Context(), raw, nil)
|
|
if err != nil || !res.IsError {
|
|
t.Fatal("cross-task intent admitted", res, err)
|
|
}
|
|
}
|
|
|
|
func TestWorkerOverviewBoundsHistoricalDigests(t *testing.T) {
|
|
text := renderWorkerGraphOverview(map[string]any{"planning_continuations": map[string]any{"unrelated": "candidate-only"}, "cold_digests": []map[string]any{{"id": int64(7), "body": strings.Repeat("历史", 1000), "member_count": 80}}}, newDataEnvelope())
|
|
if strings.Contains(text, "candidate-only") || len([]rune(text)) > 2500 || !strings.Contains(text, "body_truncated") || !strings.Contains(text, "expand_digest") {
|
|
t.Fatal("unbounded historical worker input", len([]rune(text)))
|
|
}
|
|
}
|