|
| 1 | +package ollama |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/sunholo-data/ailang/internal/ai" |
| 12 | +) |
| 13 | + |
| 14 | +// TestResolveOllamaNumCtx_DefaultOmits pins the fix for a config that could not |
| 15 | +// work: both option maps in this package hardcoded num_ctx 8192 while the eval |
| 16 | +// harness sends these models 28k-44k-token prompts, and asked for up to 32768 |
| 17 | +// tokens of generation inside that same 8192. Omitting the option lets ollama |
| 18 | +// size context from the model (measured 262144 for qwen3.6:35b-a3b-mxfp8), which |
| 19 | +// is what the /v1 lanes — opencode and pi — already get. |
| 20 | +func TestResolveOllamaNumCtx_DefaultOmits(t *testing.T) { |
| 21 | + t.Setenv("AILANG_OLLAMA_NUM_CTX", "") |
| 22 | + if n, ok := resolveOllamaNumCtx(); ok { |
| 23 | + t.Errorf("default must send NO num_ctx so the server sizes it; got %d", n) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestResolveOllamaNumCtx_EnvPins(t *testing.T) { |
| 28 | + t.Setenv("AILANG_OLLAMA_NUM_CTX", "65536") |
| 29 | + n, ok := resolveOllamaNumCtx() |
| 30 | + if !ok || n != 65536 { |
| 31 | + t.Errorf("resolveOllamaNumCtx() = (%d, %v), want (65536, true)", n, ok) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// A junk or non-positive value must fall back to "let the server decide" rather |
| 36 | +// than to some invented number — an unparseable override is not a request for a |
| 37 | +// small context. |
| 38 | +func TestResolveOllamaNumCtx_RejectsJunk(t *testing.T) { |
| 39 | + for _, v := range []string{"0", "-1", "lots", "8192.5"} { |
| 40 | + t.Setenv("AILANG_OLLAMA_NUM_CTX", v) |
| 41 | + if n, ok := resolveOllamaNumCtx(); ok { |
| 42 | + t.Errorf("AILANG_OLLAMA_NUM_CTX=%q should be ignored, got %d", v, n) |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// captureOllamaBody stands up a fake ollama and returns the request body the |
| 48 | +// client actually sent. |
| 49 | +func captureOllamaBody(t *testing.T, send func(c *Client) error) string { |
| 50 | + t.Helper() |
| 51 | + var body string |
| 52 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 53 | + b, _ := io.ReadAll(r.Body) |
| 54 | + body = string(b) |
| 55 | + w.Header().Set("Content-Type", "application/x-ndjson") |
| 56 | + _, _ = w.Write([]byte(`{"message":{"role":"assistant","content":"ok"},"done":true}` + "\n")) |
| 57 | + })) |
| 58 | + defer srv.Close() |
| 59 | + t.Setenv("OLLAMA_HOST", srv.URL) |
| 60 | + c, err := NewClient(WithEndpoint(srv.URL)) |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("NewClient: %v", err) |
| 63 | + } |
| 64 | + if err := send(c); err != nil { |
| 65 | + t.Fatalf("send: %v", err) |
| 66 | + } |
| 67 | + return body |
| 68 | +} |
| 69 | + |
| 70 | +// TestOllamaWire_NoNumCtxByDefault asserts on the BYTES, not the resolver: both |
| 71 | +// option maps in this package used to pin num_ctx to 8192, which sat below the |
| 72 | +// 28k-44k prompts the eval harness sends these models — so the model answered |
| 73 | +// having seen a fraction of the task, while the same map asked for up to 32768 |
| 74 | +// tokens of generation inside that 8192. The wire must now carry no num_ctx, so |
| 75 | +// ollama sizes context from the model itself. |
| 76 | +func TestOllamaWire_NoNumCtxByDefault(t *testing.T) { |
| 77 | + t.Setenv("AILANG_OLLAMA_NUM_CTX", "") |
| 78 | + |
| 79 | + t.Run("generate path", func(t *testing.T) { |
| 80 | + body := captureOllamaBody(t, func(c *Client) error { |
| 81 | + _, err := c.Generate(context.Background(), &ai.Request{Model: "qwen3.5", UserPrompt: "hi"}) |
| 82 | + return err |
| 83 | + }) |
| 84 | + if strings.Contains(body, "num_ctx") { |
| 85 | + t.Errorf("Generate must not pin num_ctx; body: %s", body) |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("native chat path", func(t *testing.T) { |
| 90 | + t.Setenv("AILANG_OLLAMA_NATIVE_TOOLS", "1") |
| 91 | + body := captureOllamaBody(t, func(c *Client) error { |
| 92 | + _, err := c.Step(context.Background(), &ai.Request{ |
| 93 | + Model: "qwen3.5", |
| 94 | + Messages: []ai.Message{{Role: "user", Content: "hi"}}, |
| 95 | + Tools: []ai.ToolSchema{{Name: "noop", Description: "n", Parameters: `{"type":"object"}`}}, |
| 96 | + }) |
| 97 | + return err |
| 98 | + }) |
| 99 | + if strings.Contains(body, "num_ctx") { |
| 100 | + t.Errorf("native /api/chat must not pin num_ctx; body: %s", body) |
| 101 | + } |
| 102 | + }) |
| 103 | +} |
| 104 | + |
| 105 | +// The override still has to reach the wire, or it is not a control. |
| 106 | +func TestOllamaWire_NumCtxOverrideReachesWire(t *testing.T) { |
| 107 | + t.Setenv("AILANG_OLLAMA_NUM_CTX", "65536") |
| 108 | + body := captureOllamaBody(t, func(c *Client) error { |
| 109 | + _, err := c.Generate(context.Background(), &ai.Request{Model: "qwen3.5", UserPrompt: "hi"}) |
| 110 | + return err |
| 111 | + }) |
| 112 | + if !strings.Contains(body, `"num_ctx":65536`) { |
| 113 | + t.Errorf("AILANG_OLLAMA_NUM_CTX=65536 must appear on the wire; body: %s", body) |
| 114 | + } |
| 115 | +} |
0 commit comments