Repository navigation
Expand file tree
/
Copy pathtransform_test.go
More file actions
273 lines (243 loc) · 8.35 KB
/
Copy pathtransform_test.go
File metadata and controls
273 lines (243 loc) · 8.35 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
package omnigent
import (
"errors"
"fmt"
"iter"
"strings"
"testing"
)
// blockSeq replays a fixed list, so a transform's behaviour is decided by the
// transform and not by a server's timing.
func blockSeq(blocks ...Block) iter.Seq2[Block, error] {
return func(yield func(Block, error) bool) {
for _, b := range blocks {
if !yield(b, nil) {
return
}
}
}
}
func collectBlocks(t *testing.T, seq iter.Seq2[Block, error]) ([]Block, []error) {
t.Helper()
var blocks []Block
var errs []error
for block, err := range seq {
if err != nil {
errs = append(errs, err)
continue
}
blocks = append(blocks, block)
}
return blocks, errs
}
func names(blocks []Block) string {
var out []string
for _, b := range blocks {
out = append(out, fmt.Sprintf("%T", b))
}
return strings.Join(out, ",")
}
// tag appends a marker to every TextChunk, so composing two tags records the
// order they ran in. Two filtering transforms would commute and prove nothing.
func tag(marker string) Transform {
return func(seq iter.Seq2[Block, error]) iter.Seq2[Block, error] {
return func(yield func(Block, error) bool) {
for block, err := range seq {
if chunk, ok := block.(TextChunk); ok && err == nil {
chunk.Text += marker
block = chunk
}
if !yield(block, err) {
return
}
}
}
}
}
func TestPipeAppliesTransformsInTheOrderWritten(t *testing.T) {
t.Parallel()
blocks, errs := collectBlocks(t, Pipe(blockSeq(TextChunk{Text: "start"}), tag("-a"), tag("-b")))
if len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
// "a" ran first, so its marker sits closer to the original text.
if got := blocks[0].(TextChunk).Text; got != "start-a-b" {
t.Errorf("Text = %q, want start-a-b; Pipe ran the transforms out of order", got)
}
}
func TestSkipBlocksDropsOnlyWhatItSelects(t *testing.T) {
t.Parallel()
blocks, _ := collectBlocks(t, SkipBlocks(IsBlock[ReasoningBlock], IsBlock[RetryBlock])(
blockSeq(
ReasoningBlock{ReasoningText: "why"},
TextChunk{Text: "a"},
RetryBlock{Source: "tool"},
CompactionBlock{},
)))
if got := names(blocks); got != "omnigent.TextChunk,omnigent.CompactionBlock" {
t.Errorf("got %s", got)
}
}
// TestTransformsNeverSwallowAnError pins the property every transform shares. A
// dropped error leaves a caller reading a truncated turn as a complete one.
func TestTransformsNeverSwallowAnError(t *testing.T) {
t.Parallel()
boom := errors.New("stream died")
failing := func(yield func(Block, error) bool) {
yield(TextChunk{Text: "partial"}, nil)
yield(nil, boom)
}
for name, transform := range map[string]Transform{
"SkipBlocks": SkipBlocks(IsBlock[TextChunk]),
"OnlyBlocks": OnlyBlocks(IsBlock[ResponseEndBlock]),
"OnlyAgent": OnlyAgent("nobody"),
"SkipIntermediateEnds": SkipIntermediateEnds(),
"MergeTextAcrossIterations": MergeTextAcrossIterations(),
} {
t.Run(name, func(t *testing.T) {
_, errs := collectBlocks(t, transform(failing))
if len(errs) != 1 || !errors.Is(errs[0], boom) {
t.Errorf("%s dropped the error: got %v", name, errs)
}
})
}
}
func TestOnlyAgentKeepsOneAgentAndEmptyKeepsAll(t *testing.T) {
t.Parallel()
from := func(agent, text string) Block {
return TextChunk{blockCtx: blockAt(BlockContext{Agent: agent}), Text: text}
}
seq := func() iter.Seq2[Block, error] {
return blockSeq(from("", "root"), from("sub", "child"), from("", "root2"))
}
blocks, _ := collectBlocks(t, OnlyAgent("sub")(seq()))
if len(blocks) != 1 || blocks[0].(TextChunk).Text != "child" {
t.Errorf("OnlyAgent(sub) kept %d blocks", len(blocks))
}
all, _ := collectBlocks(t, OnlyAgent("")(seq()))
if len(all) != 3 {
t.Errorf("OnlyAgent(\"\") kept %d blocks, want all 3", len(all))
}
}
// TestSkipIntermediateEndsKeepsTheLastEndOnly pins the tool-loop case: a turn that
// looped three times must read as finishing once.
func TestSkipIntermediateEndsKeepsTheLastEndOnly(t *testing.T) {
t.Parallel()
blocks, _ := collectBlocks(t, SkipIntermediateEnds()(blockSeq(
TextChunk{Text: "a"},
ResponseEndBlock{Status: "completed"},
ToolGroup{},
ResponseEndBlock{Status: "completed"},
TextChunk{Text: "b"},
// Consecutive, deliberately: with a block between every end, holding the
// first and holding the last cannot be told apart.
ResponseEndBlock{Status: "completed"},
ResponseEndBlock{Status: "failed"},
)))
if got := names(blocks); got != "omnigent.TextChunk,omnigent.ToolGroup,omnigent.TextChunk,omnigent.ResponseEndBlock" {
t.Fatalf("got %s", got)
}
// The one kept is the last, not the first.
if last := blocks[len(blocks)-1].(ResponseEndBlock); last.Status != "failed" {
t.Errorf("kept the end with status %q, want the last one", last.Status)
}
}
func TestSkipIntermediateEndsEmitsNothingExtraWhenThereIsNoEnd(t *testing.T) {
t.Parallel()
blocks, _ := collectBlocks(t, SkipIntermediateEnds()(blockSeq(TextChunk{Text: "a"})))
if got := names(blocks); got != "omnigent.TextChunk" {
t.Errorf("got %s", got)
}
}
// TestMergeTextAcrossIterationsReportsOneAnswer pins that a looped turn renders as
// one answer, and that the merged block lands before the end.
func TestMergeTextAcrossIterationsReportsOneAnswer(t *testing.T) {
t.Parallel()
ctx := BlockContext{Agent: "coder", Iteration: 2}
blocks, _ := collectBlocks(t, MergeTextAcrossIterations()(blockSeq(
TextChunk{Text: "one "},
TextDone{FullText: "one "},
ToolGroup{},
TextDone{blockCtx: blockAt(ctx), FullText: "two"},
ResponseEndBlock{blockCtx: blockAt(ctx), Status: "completed"},
)))
if got := names(blocks); got != "omnigent.TextChunk,omnigent.ToolGroup,omnigent.TextDone,omnigent.ResponseEndBlock" {
t.Fatalf("got %s", got)
}
merged := blocks[2].(TextDone)
if merged.FullText != "one two" {
t.Errorf("FullText = %q, want %q", merged.FullText, "one two")
}
if merged.Context().Agent != "coder" {
t.Errorf("the merged block lost its context: %+v", merged.Context())
}
}
// TestMergeTextAcrossIterationsFlushesADroppedStream pins that a stream ending
// without a terminal response still reports the text it gathered.
func TestMergeTextAcrossIterationsFlushesADroppedStream(t *testing.T) {
t.Parallel()
ctx := BlockContext{Agent: "coder", Iteration: 3}
blocks, _ := collectBlocks(t, MergeTextAcrossIterations()(blockSeq(
TextDone{blockCtx: blockAt(ctx), FullText: "partial answer"},
)))
if len(blocks) != 1 {
t.Fatalf("got %d blocks, want the flushed TextDone", len(blocks))
}
flushed := blocks[0].(TextDone)
if flushed.FullText != "partial answer" {
t.Errorf("FullText = %q", flushed.FullText)
}
// No terminal response arrived, so the context can only come from the text the
// transform accumulated.
if flushed.Context().Agent != "coder" || flushed.Context().Iteration != 3 {
t.Errorf("the flushed block lost its context: %+v", flushed.Context())
}
}
func TestMergeTextAcrossIterationsMarksCodeBlocks(t *testing.T) {
t.Parallel()
blocks, _ := collectBlocks(t, MergeTextAcrossIterations()(blockSeq(
TextDone{FullText: "see ```go\nx := 1\n```"},
ResponseEndBlock{Status: "completed"},
)))
if !blocks[0].(TextDone).HasCodeBlocks {
t.Error("a fenced block was not reported")
}
}
// TestMergeTextNeedsTheEndsFoldedFirst pins the composition its doc now names.
//
// The transform flushes at every ResponseEndBlock, so a sequence carrying several —
// a stream read across more than one turn — yields one answer per end. Composing
// SkipIntermediateEnds first joins them into one.
func TestMergeTextJoinsAcrossEndsOnlyWhenTheyAreFolded(t *testing.T) {
t.Parallel()
twoTurns := func() iter.Seq2[Block, error] {
return blockSeq(
TextDone{FullText: "one "},
ResponseEndBlock{Status: "completed"},
ToolGroup{},
TextDone{FullText: "two"},
ResponseEndBlock{Status: "completed"},
)
}
alone, _ := collectBlocks(t, MergeTextAcrossIterations()(twoTurns()))
var answers []string
for _, b := range alone {
if done, ok := b.(TextDone); ok {
answers = append(answers, done.FullText)
}
}
if len(answers) != 2 {
t.Errorf("alone: got %d answers %v, want one per end", len(answers), answers)
}
composed, _ := collectBlocks(t, Pipe(twoTurns(),
SkipIntermediateEnds(), MergeTextAcrossIterations()))
answers = nil
for _, b := range composed {
if done, ok := b.(TextDone); ok {
answers = append(answers, done.FullText)
}
}
if len(answers) != 1 || answers[0] != "one two" {
t.Errorf("composed: got %v, want one merged answer", answers)
}
}