Repository navigation
Expand file tree
/
Copy pathmemory_availability_test.go
More file actions
121 lines (118 loc) · 3.79 KB
/
Copy pathmemory_availability_test.go
File metadata and controls
121 lines (118 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package remote
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
"github.com/Agent-Field/codeaf/internal/session"
"github.com/Agent-Field/codeaf/internal/store"
)
// A model switch takes effect at the next request boundary, and memory holds
// across it both ways: the person's lines are not a casualty of a small window,
// so the same live connection reads memory as available before and after.
func TestRemoteMemoryHoldsAcrossAutomaticProfileChanges(t *testing.T) {
t.Setenv("CODEAF_PROMPT_PROFILE", "")
for _, initial := range []string{"test/small", "test/large"} {
t.Run(initial, func(t *testing.T) {
brain, err := store.Open(filepath.Join(t.TempDir(), "graph.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = brain.Close() })
model := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
_, _ = io.WriteString(w, `{"data":[]}`)
return
}
// The real session also sends non-streaming naming and memory
// requests, so those callers need their ordinary JSON response.
var request struct {
Stream bool `json:"stream"`
}
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if !request.Stream {
w.Header().Set("Content-Type", "application/json")
_, _ = io.WriteString(w, `{"choices":[{"message":{"role":"assistant","content":"done"},"finish_reason":"stop"}]}`)
return
}
w.Header().Set("Content-Type", "text/event-stream")
_, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"done\"},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n")
}))
t.Cleanup(model.Close)
far, err := session.New(session.Config{Workspace: t.TempDir(), Model: initial, APIKey: "fixture", BaseURL: model.URL + "/v1", Memory: brain, ContextWindowFor: func(model string) int {
if model == "test/small" {
return 16385
}
return 128000
}})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = far.Close() })
loop, err := Loopback(Hello{Version: Version}, Options{Boot: func(Hello) (*Engine, error) { return &Engine{Agent: far}, nil }})
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = loop.Close() })
handle := loop.Client.Agent()
// MEMORY IS NOT A PROFILE CASUALTY: a small window and a large one both
// carry the person's lines, so the reading is true on both.
wantInitial := initial == "test/large"
if !handle.Remembers() || !far.Remembers() {
t.Fatal("initial profile memory availability is wrong")
}
next := []string{"test/large", "test/small"}
if wantInitial {
next = []string{"test/small", "test/large"}
}
for _, model := range next {
handle.SetModel(model)
ctx, cancel := context.WithTimeout(t.Context(), 10*time.Second)
events, err := handle.Submit(ctx, "hello")
if err != nil {
cancel()
t.Fatal(err)
}
done := false
for !done {
select {
case ev, ok := <-events:
if !ok {
done = true
break
}
if ev.Kind == session.EventError {
cancel()
t.Fatal(ev.Err)
}
case <-ctx.Done():
cancel()
t.Fatal("profile transition request did not finish")
}
}
cancel()
want := true
if far.Remembers() != want {
t.Fatalf("engine memory=%v for %s", far.Remembers(), model)
}
if handle.Remembers() != want {
t.Fatalf("remote memory=%v for %s, want %v without a new welcome", handle.Remembers(), model, want)
}
if want {
if _, err := handle.Memories(""); err != nil {
t.Fatal(err)
}
} else if _, err := handle.Memories(""); err == nil {
t.Fatal("lean engine allowed memory")
}
}
})
}
}