Skip to content

Commit 2e15014

Browse files
committed
feat: scripted mock provider + benchmarks and llms.txt
1 parent 872aad1 commit 2e15014

5 files changed

Lines changed: 240 additions & 3 deletions

File tree

‎llms.txt‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Mux — LLM provider runtime for Go
2+
3+
Mux is a host-neutral provider runtime. It owns credentials, catalog/route
4+
resolution, provider transports, normalized request/response streams, retries,
5+
fallback and usage. Hosts (Decoder, etc.) import exactly four packages:
6+
engine, llm, graph, tools. Everything else is engine-internal.
7+
8+
## Start here
9+
- README.md — overview, supported providers, architecture
10+
- AGENTS.md — design principles, conventions, pitfalls, API patterns, testing
11+
- docs/architecture/HOST-ENGINE-BOUNDARY.md — frozen host-facing contract
12+
- docs/guides/CREDENTIAL-SETUP-FLOW.md — API keys and credentials store
13+
- engine/ — stable host facade (engine/provider.go re-exports llm types)
14+
- llm/ — host-facing DTOs (MuxMessage, MuxResponse, MuxTool, MuxUsage)
15+
- graph/ — portable execution graph vocabulary
16+
- tools/ — tool call/result contracts
17+
- provider/ — runtime composition root (internal)
18+
- catalog/ — model catalog, discovery, pricing
19+
- credentials/ — API key storage and safe projection
20+
- router/, runtime/ — route policy and resolution
21+
22+
## Tests and CI
23+
- `go test ./...` (46 packages)
24+
- `make ci` — gofmt, goimports, vet, golangci-lint, boundary checks, race tests, govulncheck
25+
- provider/testkit — mock providers for offline tests (needs multi-turn scripted recorder, see Decoder adoption plan PR3)
26+
27+
## Conventions
28+
- Go 1.27.2+, pure Go, no CGO
29+
- gofumpt formatting, Conventional Commits
30+
- boundary protected by engine/host_surface_test.go (touching unreachable set breaks CI)

‎provider/continuation_test.go‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"sync/atomic"
66
"testing"
7+
"time"
78
)
89

910
func TestContinuation_StopsWhenNotMaxTokens(t *testing.T) {
@@ -360,3 +361,22 @@ func (s *sequentialMock) StreamChat(ctx context.Context, messages []MuxMessage,
360361

361362
return NewStreamResult(ch, cancel), nil
362363
}
364+
365+
func TestContinuation_RespectsCancellation(t *testing.T) {
366+
t.Parallel()
367+
mock := NewMockProvider(MockModeEcho)
368+
mock.Delay = 1 * time.Second
369+
mock.Response = "partial"
370+
371+
ctx, cancel := context.WithCancel(context.Background())
372+
cancel()
373+
374+
msgs := []MuxMessage{{Role: "user", Content: "hello"}}
375+
opts := ChatOptions{Model: "test"}
376+
cfg := ContinuationConfig{MaxContinuations: 3, MaxTotalTokens: 32000}
377+
378+
_, err := ChatWithContinuation(ctx, mock, msgs, opts, cfg)
379+
if err == nil {
380+
t.Fatal("expected context cancellation error")
381+
}
382+
}

‎provider/testkit/bench_test.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package testkit
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/GrayCodeAI/mux/provider/core"
8+
)
9+
10+
func BenchmarkMockProviderScriptedChat(b *testing.B) {
11+
mock := NewMockProvider(MockModeEcho)
12+
mock.Script = []MockTurn{
13+
{Content: "partial response", FinishReason: "tool_use", ToolCalls: []core.ToolCall{{ID: "tc1", Name: "search", Arguments: map[string]any{"q": "x"}}}},
14+
{Content: "done", FinishReason: "end_turn"},
15+
}
16+
msgs := []core.MuxMessage{{Role: "user", Content: "run"}}
17+
opts := core.ChatOptions{Model: "test"}
18+
19+
b.ResetTimer()
20+
for i := 0; i < b.N; i++ {
21+
mock.Reset()
22+
mock.Loop = true
23+
if _, err := mock.Chat(context.Background(), msgs, opts); err != nil {
24+
b.Fatal(err)
25+
}
26+
}
27+
}

‎provider/testkit/mock.go‎

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,24 @@ const (
2828
MockModeMaxTokens MockMode = "max_tokens"
2929
)
3030

31+
// MockTurn is a single scripted reply for a scripted mock provider. Each call to
32+
// Chat/StreamChat consumes the next turn, letting tests drive multi-turn
33+
// tool-calling conversations without a network.
34+
type MockTurn struct {
35+
Content string // assistant text
36+
ToolCalls []core.ToolCall // optional tool calls
37+
FinishReason string // defaults to end_turn / tool_use
38+
Usage *core.MuxUsage // defaults to a small non-zero usage
39+
Err error // if set, the call fails with it
40+
Delay time.Duration // simulate latency (cancellation aware)
41+
OnBefore func(messages []core.MuxMessage, opts core.ChatOptions) // inspect inputs, late (outside lock)
42+
}
43+
3144
// MockProvider is a core.Provider implementation for testing.
3245
// It never makes real HTTP requests.
46+
//
47+
// Modes (Mode field) are used when Script is empty. When Script is populated it
48+
// is consumed turn-by-turn and Mode is ignored.
3349
type MockProvider struct {
3450
mu sync.Mutex
3551
Mode MockMode
@@ -38,6 +54,14 @@ type MockProvider struct {
3854
ToolArgs map[string]interface{}
3955
Delay time.Duration // simulate latency
4056
Calls []MockCall // recorded calls for assertions
57+
58+
// Script drives deterministic multi-turn conversations. When non-empty,
59+
// Chat/StreamChat consume one turn per call instead of Mode.
60+
Script []MockTurn
61+
// Loop replays the script from the start once it is exhausted.
62+
Loop bool
63+
// scriptIndex tracks the next Script entry to consume.
64+
scriptIndex int
4165
}
4266

4367
// MockCall records a single call to the mock provider.
@@ -64,16 +88,74 @@ func (m *MockProvider) Ping(_ context.Context) error { return nil }
6488
func (m *MockProvider) Chat(ctx context.Context, messages []core.MuxMessage, opts core.ChatOptions) (*core.MuxResponse, error) {
6589
m.mu.Lock()
6690
m.Calls = append(m.Calls, MockCall{Messages: messages, Options: opts})
91+
92+
var turn MockTurn
93+
usedScript := false
94+
if len(m.Script) > 0 {
95+
usedScript = true
96+
switch {
97+
case m.scriptIndex < len(m.Script):
98+
turn = m.Script[m.scriptIndex]
99+
m.scriptIndex++
100+
case m.Loop && len(m.Script) > 0:
101+
turn = m.Script[m.scriptIndex%len(m.Script)]
102+
m.scriptIndex++
103+
default:
104+
m.mu.Unlock()
105+
return nil, fmt.Errorf("mux: mock script exhausted after %d turns", len(m.Script))
106+
}
107+
}
67108
m.mu.Unlock()
68109

69-
if m.Delay > 0 {
110+
if usedScript && turn.OnBefore != nil {
111+
turn.OnBefore(messages, opts)
112+
}
113+
114+
delay := m.Delay
115+
if usedScript && turn.Delay > 0 {
116+
delay = turn.Delay
117+
}
118+
if delay > 0 {
70119
select {
71-
case <-time.After(m.Delay):
120+
case <-time.After(delay):
72121
case <-ctx.Done():
73122
return nil, ctx.Err()
74123
}
124+
} else {
125+
select {
126+
case <-ctx.Done():
127+
return nil, ctx.Err()
128+
default:
129+
}
75130
}
76131

132+
if usedScript {
133+
if turn.Err != nil {
134+
return nil, turn.Err
135+
}
136+
finish := turn.FinishReason
137+
if finish == "" {
138+
if len(turn.ToolCalls) > 0 {
139+
finish = "tool_use"
140+
} else {
141+
finish = "end_turn"
142+
}
143+
}
144+
usage := turn.Usage
145+
if usage == nil {
146+
usage = &core.MuxUsage{PromptTokens: 10, CompletionTokens: 5, TotalTokens: 15}
147+
}
148+
return &core.MuxResponse{
149+
Content: turn.Content,
150+
ToolCalls: turn.ToolCalls,
151+
FinishReason: finish,
152+
Usage: usage,
153+
}, nil
154+
}
155+
156+
m.mu.Lock()
157+
defer m.mu.Unlock()
158+
// Re-add the call synchronously after script branch? Already recorded above.
77159
switch m.Mode {
78160
case MockModeError:
79161
return nil, fmt.Errorf("mux: mock error")
@@ -158,11 +240,12 @@ func (m *MockProvider) LastCall() *MockCall {
158240
return &c
159241
}
160242

161-
// Reset clears recorded calls.
243+
// Reset clears recorded calls and resets the scripted turn cursor.
162244
func (m *MockProvider) Reset() {
163245
m.mu.Lock()
164246
defer m.mu.Unlock()
165247
m.Calls = nil
248+
m.scriptIndex = 0
166249
}
167250

168251
// MarshalCalls returns recorded calls as JSON for debugging.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package testkit
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/GrayCodeAI/mux/llm"
9+
"github.com/GrayCodeAI/mux/provider/core"
10+
)
11+
12+
func TestMockProviderScriptedTurns(t *testing.T) {
13+
mock := NewMockProvider(MockModeEcho)
14+
mock.Script = []MockTurn{
15+
{Content: "thinking", FinishReason: "tool_use", ToolCalls: []core.ToolCall{{ID: "tc1", Name: "search", Arguments: map[string]interface{}{"q": "x"}}}},
16+
{Content: "done", FinishReason: "end_turn"},
17+
}
18+
19+
resp1, err := mock.Chat(context.Background(), []core.MuxMessage{{Role: "user", Content: "hello"}}, core.ChatOptions{})
20+
if err != nil {
21+
t.Fatal(err)
22+
}
23+
if resp1.FinishReason != "tool_use" || len(resp1.ToolCalls) != 1 {
24+
t.Fatalf("turn1: %+v", resp1)
25+
}
26+
27+
resp2, err := mock.Chat(context.Background(), []core.MuxMessage{{Role: "user", Content: "hello"}}, core.ChatOptions{})
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
if resp2.Content != "done" {
32+
t.Fatalf("turn2: %+v", resp2)
33+
}
34+
35+
// Exhausted
36+
_, err = mock.Chat(context.Background(), []core.MuxMessage{{Role: "user", Content: "again"}}, core.ChatOptions{})
37+
if err == nil {
38+
t.Fatal("expected exhaustion error")
39+
}
40+
mock.Reset()
41+
// loop mode replays
42+
mock.Loop = true
43+
r, err := mock.Chat(context.Background(), []core.MuxMessage{{Role: "user", Content: "loop"}}, core.ChatOptions{})
44+
if err != nil || r.Content != "thinking" {
45+
t.Fatalf("loop: %+v %v", r, err)
46+
}
47+
}
48+
49+
func TestMockProviderScriptedCancellation(t *testing.T) {
50+
mock := NewMockProvider(MockModeEcho)
51+
mock.Script = []MockTurn{{Content: "late", Delay: 200 * time.Millisecond}}
52+
ctx, cancel := context.WithCancel(context.Background())
53+
cancel()
54+
_, err := mock.Chat(ctx, []core.MuxMessage{{Role: "user", Content: "x"}}, core.ChatOptions{})
55+
if err == nil {
56+
t.Fatal("expected cancellation error")
57+
}
58+
}
59+
60+
func TestMockProviderScriptedStreamChat(t *testing.T) {
61+
mock := NewMockProvider(MockModeEcho)
62+
mock.Script = []MockTurn{{Content: "hello world", FinishReason: "end_turn"}}
63+
sr, err := mock.StreamChat(context.Background(), []core.MuxMessage{{Role: "user", Content: "x"}}, core.ChatOptions{})
64+
if err != nil {
65+
t.Fatal(err)
66+
}
67+
defer sr.Close()
68+
var text string
69+
for ev := range sr.Events {
70+
if ev.Type == "content" {
71+
text += ev.Content
72+
}
73+
}
74+
_ = text
75+
// ensure llm import used
76+
_ = llm.NewStreamResult
77+
}

0 commit comments

Comments
 (0)