| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "golang.org/x/net/http2" |
| 7 | "reasonix/internal/agent/testutil" |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/provider" |
| 10 | "reasonix/internal/tool" |
| 11 | ) |
| 12 | |
| 13 | func TestHTTP2FailureDoesNotReplayCommittedOrSpeculativeTools(t *testing.T) { |
| 14 | for _, committed := range []bool{false, true} { |
| 15 | t.Run(map[bool]string{false: "speculative", true: "committed"}[committed], func(t *testing.T) { |
| 16 | writer := &countingWriterTool{} |
| 17 | reg := tool.NewRegistry() |
| 18 | reg.Add(writer) |
| 19 | call := provider.ToolCall{ID: "write-once", Name: "write_file", Arguments: `{"path":"x.txt","content":"once"}`} |
| 20 | failure := provider.StreamInterrupt(http2.StreamError{StreamID: 1, Code: http2.ErrCodeInternal}, provider.StreamInterruptPrematureEOF) |
| 21 | turns := []testutil.Turn{} |
| 22 | if committed { |
| 23 | turns = append(turns, testutil.Turn{ToolCalls: []provider.ToolCall{call}}) |
| 24 | } |
| 25 | turns = append(turns, testutil.Turn{Chunks: []provider.Chunk{ |
| 26 | {Type: provider.ChunkToolCall, ToolCall: &call}, |
| 27 | {Type: provider.ChunkError, Err: failure}, |
| 28 | }}, testutil.Turn{Text: "manual continuation complete"}) |
| 29 | p := testutil.NewMock("m", turns...) |
| 30 | sink := &recordSink{} |
| 31 | a := New(p, reg, NewSession(""), Options{}, sink) |
| 32 | err := a.Run(withNoClosedLoop(t.Context()), "write once") |
| 33 | if provider.HTTP2TransportCode(err) != "INTERNAL_ERROR" { |
| 34 | t.Fatalf("failure was hidden: %v", err) |
| 35 | } |
| 36 | wantWrites, wantCalls := int32(0), 1 |
| 37 | if committed { |
| 38 | wantWrites, wantCalls = 1, 2 |
| 39 | } |
| 40 | if writer.calls.Load() != wantWrites || p.CallCount() != wantCalls || len(sink.kinds(event.Retrying)) != 0 { |
| 41 | t.Fatalf("tool/request replayed: writes=%d calls=%d", writer.calls.Load(), p.CallCount()) |
| 42 | } |
| 43 | if err := a.Run(withNoClosedLoop(t.Context()), "continue"); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if writer.calls.Load() != wantWrites || p.CallCount() != wantCalls+1 { |
| 47 | t.Fatalf("manual continuation replayed a tool: writes=%d calls=%d", writer.calls.Load(), p.CallCount()) |
| 48 | } |
| 49 | }) |
| 50 | } |
| 51 | } |
| 52 |