-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprefill_test.go
More file actions
394 lines (365 loc) · 14.2 KB
/
Copy pathprefill_test.go
File metadata and controls
394 lines (365 loc) · 14.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package cogito
import (
"context"
"reflect"
"strings"
"sync"
"testing"
"github.com/sashabaranov/go-openai"
)
func TestPrepareAgentToolsDisabled(t *testing.T) {
o := defaultOptions()
if got := prepareAgentTools(o, nil); got != nil {
t.Fatalf("spawning disabled: want nil, got %d tools", len(got))
}
}
func TestPrepareAgentToolsReturnsFourAndInitializes(t *testing.T) {
o := defaultOptions()
o.Apply(EnableAgentSpawning)
tools := prepareAgentTools(o, nil)
if len(tools) != 4 {
t.Fatalf("want 4 agent tools, got %d", len(tools))
}
if o.agentManager == nil {
t.Fatal("agentManager must be auto-created")
}
if o.messageInjectionChan == nil {
t.Fatal("messageInjectionChan must be auto-created")
}
names := map[string]bool{}
for _, tl := range tools {
names[tl.Tool().Function.Name] = true
}
for _, want := range []string{"spawn_agent", "check_agent", "get_agent_result", "send_agent_message"} {
if !names[want] {
t.Fatalf("missing agent tool %q (got %v)", want, names)
}
}
}
// askToolForTest builds a minimal registered tool for the prefill tests. It
// reuses the echo-style ToolDefinition shape the agent tests already build
// (newNamedTool in agent_definitions_test.go) rather than inventing a new one.
func askToolForTest() ToolDefinitionInterface { return newNamedTool("ask") }
// captureLLM records every request it was handed and returns an empty reply, so
// the caller's loop terminates without any tool being executed.
type captureLLM struct {
mu sync.Mutex
requests []openai.ChatCompletionRequest
last openai.ChatCompletionRequest
n int
}
func (c *captureLLM) Ask(ctx context.Context, f Fragment) (Fragment, error) { return f, nil }
func (c *captureLLM) CreateChatCompletion(ctx context.Context, req openai.ChatCompletionRequest) (LLMReply, LLMUsage, error) {
c.mu.Lock()
c.requests = append(c.requests, req)
c.last = req
c.n++
c.mu.Unlock()
return LLMReply{ChatCompletionResponse: openai.ChatCompletionResponse{
Choices: []openai.ChatCompletionChoice{{Message: openai.ChatCompletionMessage{Role: "assistant", Content: ""}}},
}}, LLMUsage{}, nil
}
// messageSig returns a role+content signature of the messages carried by the
// request at index i, for readable failure output. Assertions compare the
// messages structurally (see request); this is only for printing a diff a human
// can read.
func (c *captureLLM) messageSig(i int) []string {
c.mu.Lock()
defer c.mu.Unlock()
if i >= len(c.requests) {
return nil
}
sig := make([]string, 0, len(c.requests[i].Messages))
for _, m := range c.requests[i].Messages {
sig = append(sig, m.Role+": "+m.Content)
}
return sig
}
// request returns the full request at index i, so tests can compare the whole
// prompt prefix (tool schemas including parameters and descriptions, message
// ToolCalls and ToolCallID) rather than a lossy summary of it.
func (c *captureLLM) request(i int) (openai.ChatCompletionRequest, bool) {
c.mu.Lock()
defer c.mu.Unlock()
if i >= len(c.requests) {
return openai.ChatCompletionRequest{}, false
}
return c.requests[i], true
}
// toolNames returns the function names of the tools carried by the request at
// index i, or nil when no such request was made.
func (c *captureLLM) toolNames(i int) []string {
c.mu.Lock()
defer c.mu.Unlock()
if i >= len(c.requests) {
return nil
}
names := make([]string, 0, len(c.requests[i].Tools))
for _, t := range c.requests[i].Tools {
names = append(names, t.Function.Name)
}
return names
}
func TestPrefillSendsOneTokenRequestWithTools(t *testing.T) {
llm := &captureLLM{}
f := NewFragment(
openai.ChatCompletionMessage{Role: "system", Content: "SYSTEM PROMPT"},
openai.ChatCompletionMessage{Role: "user", Content: "hi"},
)
err := Prefill(context.Background(), llm, f, WithTools(askToolForTest()))
if err != nil {
t.Fatalf("Prefill: %v", err)
}
if llm.n != 1 {
t.Fatalf("want exactly 1 LLM call, got %d", llm.n)
}
if llm.last.MaxTokens != 1 {
t.Fatalf("want MaxTokens=1, got %d", llm.last.MaxTokens)
}
// The registered tool must be in the request. The set is larger than one:
// sink state is on by default, so the real turn also offers the sink tool
// and Prefill must offer it too (see the equivalence test below).
var sawAsk bool
for _, tl := range llm.last.Tools {
if tl.Function.Name == "ask" {
sawAsk = true
}
}
if !sawAsk {
t.Fatalf("registered tool missing from the prefill request, got %v", llm.toolNames(0))
}
var sawSystem bool
for _, m := range llm.last.Messages {
if m.Role == "system" && m.Content == "SYSTEM PROMPT" {
sawSystem = true
}
}
if !sawSystem {
t.Fatal("system prompt missing from the prefill request")
}
}
func TestPrefillExecutesNoTools(t *testing.T) {
llm := &captureLLM{}
called := false
f := NewFragment(openai.ChatCompletionMessage{Role: "user", Content: "hi"})
_ = Prefill(context.Background(), llm, f, WithToolCallBack(func(tc *ToolChoice, st *SessionState) ToolCallDecision {
called = true
return ToolCallDecision{Approved: true}
}))
if called {
t.Fatal("Prefill must never reach the tool-call path")
}
}
// TestPrefillSendsSameToolSetAsExecuteTools is the point of the whole feature: a
// Prefill that primes a DIFFERENT prompt prefix than the real turn still
// succeeds, still costs the full prefill, and leaves no symptom. So assert the
// tool set Prefill sends equals — by function name, in order — the tool set the
// first real ExecuteTools request sends, for a config that mixes an ordinary
// registered tool with the injected agent-spawning tools.
func TestPrefillSendsSameToolSetAsExecuteTools(t *testing.T) {
opts := func() []Option {
return []Option{
EnableAgentSpawning,
WithTools(askToolForTest()),
WithIterations(1),
// A manipulator rewrites the conversation on the real turn; if Prefill
// skips it the cached prefix is for a prompt nobody will ask for.
WithMessagesManipulator(func(msgs []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
return append([]openai.ChatCompletionMessage{{Role: "system", Content: "INJECTED"}}, msgs...)
}),
}
}
prefillLLM := &captureLLM{}
f := NewFragment(
openai.ChatCompletionMessage{Role: "system", Content: "SYSTEM PROMPT"},
openai.ChatCompletionMessage{Role: "user", Content: "hi"},
)
if err := Prefill(context.Background(), prefillLLM, f, opts()...); err != nil {
t.Fatalf("Prefill: %v", err)
}
execLLM := &captureLLM{}
if _, err := ExecuteTools(execLLM, f, opts()...); err != nil {
t.Fatalf("ExecuteTools: %v", err)
}
want := execLLM.toolNames(0)
got := prefillLLM.toolNames(0)
// Fail loudly rather than pass vacuously if either side sent no tools.
if len(want) == 0 {
t.Fatalf("ExecuteTools sent no tools on its first request (%d requests made) - the comparison would be vacuous", execLLM.n)
}
if len(got) == 0 {
t.Fatalf("Prefill sent no tools (%d requests made) - the comparison would be vacuous", prefillLLM.n)
}
if len(got) != len(want) {
t.Fatalf("tool set differs: Prefill sent %v, ExecuteTools sent %v", got, want)
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("tool set differs at %d: Prefill sent %v, ExecuteTools sent %v", i, got, want)
}
}
// Names in order are not enough: the schemas are part of the prompt the
// server tokenizes, so parameters and descriptions must match too. Compare
// the tool definitions structurally.
wantReq, ok := execLLM.request(0)
if !ok {
t.Fatal("ExecuteTools made no request")
}
gotReq, ok := prefillLLM.request(0)
if !ok {
t.Fatal("Prefill made no request")
}
if !reflect.DeepEqual(gotReq.Tools, wantReq.Tools) {
t.Fatalf("tool schemas differ structurally:\nPrefill: %#v\nExecuteTools: %#v", gotReq.Tools, wantReq.Tools)
}
// The messages are the larger half of the cached prefix; any divergence there
// costs the whole prefill just as silently as a tool-schema divergence. Compare
// them structurally so ToolCalls/ToolCallID/Name are covered, not just
// role+content.
if len(wantReq.Messages) == 0 || len(gotReq.Messages) == 0 {
t.Fatalf("empty message set (prefill %d, execute %d) - the comparison would be vacuous", len(gotReq.Messages), len(wantReq.Messages))
}
if !reflect.DeepEqual(gotReq.Messages, wantReq.Messages) {
t.Fatalf("message prefix differs structurally:\nPrefill: %q\nExecuteTools: %q\nraw prefill: %#v\nraw execute: %#v",
prefillLLM.messageSig(0), execLLM.messageSig(0), gotReq.Messages, wantReq.Messages)
}
}
// TestPrefillRejectsOptionsItDoesNotModel locks the loud-failure contract: for
// option sets whose real first request is NOT the tool-selection request Prefill
// reproduces, Prefill must error instead of burning a full prefill on a prefix
// nobody will ask for.
func TestPrefillRejectsOptionsItDoesNotModel(t *testing.T) {
for _, tc := range []struct {
name string
opts []Option
want string
}{
{"forceReasoning", []Option{WithForceReasoning(), WithTools(askToolForTest())}, "force reasoning"},
{"forceReasoningTool", []Option{WithForceReasoningTool(), WithTools(askToolForTest())}, "force reasoning"},
{"autoPlan", []Option{EnableAutoPlan, WithTools(askToolForTest())}, "auto plan"},
// startWithAction makes the first loop iteration execute the given tools
// directly, so ExecuteTools sends no tool-selection request at all and
// there is no prefix for Prefill to prime.
{"startWithAction", []Option{WithStartWithAction(&ToolChoice{Name: "ask"}), WithTools(askToolForTest())}, "start with action"},
} {
t.Run(tc.name, func(t *testing.T) {
llm := &captureLLM{}
f := NewFragment(openai.ChatCompletionMessage{Role: "user", Content: "hi"})
err := Prefill(context.Background(), llm, f, tc.opts...)
if err == nil {
t.Fatal("want an error naming the unsupported option, got nil")
}
if !strings.Contains(err.Error(), tc.want) {
t.Fatalf("error must name the option: want it to contain %q, got %q", tc.want, err.Error())
}
// Rejecting late (after the call) would defeat the point: the wasted
// prefill is the cost we are avoiding.
if llm.n != 0 {
t.Fatalf("Prefill must reject before calling the LLM, made %d calls", llm.n)
}
})
}
}
// TestExecuteToolsWithStartWithActionSendsNoToolSelection pins the premise the
// startWithAction rejection rests on: with a starting action, ExecuteTools' first
// loop iteration takes the startingActions branch and issues no tool-selection
// completion at all. If this ever stops holding, the rejection above is wrong and
// this test says so instead of Prefill silently going back to priming a prefix
// nobody asks for.
func TestExecuteToolsWithStartWithActionSendsNoToolSelection(t *testing.T) {
llm := &captureLLM{}
f := NewFragment(openai.ChatCompletionMessage{Role: "user", Content: "hi"})
if _, err := ExecuteTools(llm, f,
WithTools(askToolForTest()),
WithIterations(1),
WithStartWithAction(&ToolChoice{Name: "ask", Arguments: map[string]any{"text": "hi"}}),
); err != nil {
t.Fatalf("ExecuteTools: %v", err)
}
if llm.n != 0 {
t.Fatalf("startWithAction must skip tool selection, but ExecuteTools sent %d completion request(s): %v", llm.n, llm.messageSig(0))
}
}
// TestPrefillMirrorsAutoImproveSystemPrompt covers the one diverging option
// Prefill does model: ExecuteTools prepends the stored AutoImprove system prompt
// before its first tool-selection call, so Prefill must too.
func TestPrefillMirrorsAutoImproveSystemPrompt(t *testing.T) {
const stored = "STORED IMPROVED PROMPT"
opts := func() []Option {
return []Option{
WithTools(askToolForTest()),
WithIterations(1),
WithAutoImproveState(&AutoImproveState{SystemPrompt: stored}),
}
}
f := NewFragment(
openai.ChatCompletionMessage{Role: "system", Content: "SYSTEM PROMPT"},
openai.ChatCompletionMessage{Role: "user", Content: "hi"},
)
prefillLLM := &captureLLM{}
if err := Prefill(context.Background(), prefillLLM, f, opts()...); err != nil {
t.Fatalf("Prefill: %v", err)
}
execLLM := &captureLLM{}
if _, err := ExecuteTools(execLLM, f, opts()...); err != nil {
t.Fatalf("ExecuteTools: %v", err)
}
gotReq, ok := prefillLLM.request(0)
if !ok {
t.Fatal("Prefill made no request")
}
wantReq, ok := execLLM.request(0)
if !ok {
t.Fatal("ExecuteTools made no request")
}
var sawStored bool
for _, m := range gotReq.Messages {
if strings.Contains(m.Content, stored) {
sawStored = true
}
}
if !sawStored {
t.Fatalf("AutoImprove system prompt missing from the prefill request: %q", prefillLLM.messageSig(0))
}
if !reflect.DeepEqual(gotReq.Messages, wantReq.Messages) {
t.Fatalf("message prefix differs structurally:\nPrefill: %q\nExecuteTools: %q",
prefillLLM.messageSig(0), execLLM.messageSig(0))
}
if !reflect.DeepEqual(gotReq.Tools, wantReq.Tools) {
t.Fatalf("tool schemas differ:\nPrefill: %#v\nExecuteTools: %#v", gotReq.Tools, wantReq.Tools)
}
}
// TestPrefillDoesNotMutateFragment pins the doc comment's "not mutated" claim
// directly. The equivalence test structurally cannot catch a mutation: it runs
// Prefill first on the same fragment, so anything Prefill wrote would be visible
// to ExecuteTools as well and the two would still compare equal.
func TestPrefillDoesNotMutateFragment(t *testing.T) {
f := NewFragment(
openai.ChatCompletionMessage{Role: "system", Content: "SYSTEM PROMPT"},
openai.ChatCompletionMessage{Role: "user", Content: "hi"},
)
before := append([]openai.ChatCompletionMessage(nil), f.Messages...)
beforeLen := len(f.Messages)
llm := &captureLLM{}
err := Prefill(context.Background(), llm, f,
WithTools(askToolForTest()),
// AutoImprove and the manipulator are the two paths that build a
// different message list from the caller's; neither may write back.
WithAutoImproveState(&AutoImproveState{SystemPrompt: "STORED IMPROVED PROMPT"}),
WithMessagesManipulator(func(msgs []openai.ChatCompletionMessage) []openai.ChatCompletionMessage {
return append([]openai.ChatCompletionMessage{{Role: "system", Content: "INJECTED"}}, msgs...)
}),
)
if err != nil {
t.Fatalf("Prefill: %v", err)
}
if llm.n == 0 {
t.Fatal("Prefill made no request - the assertion would be vacuous")
}
if len(f.Messages) != beforeLen {
t.Fatalf("Prefill changed the caller's message count: %d -> %d (%#v)", beforeLen, len(f.Messages), f.Messages)
}
if !reflect.DeepEqual(f.Messages, before) {
t.Fatalf("Prefill mutated the caller's fragment:\nbefore: %#v\nafter: %#v", before, f.Messages)
}
}