Git designed for AI agents.
graft-resolve: no silently ignored settings, and no llm start without grammars
Federico Eliaaa8e0019a4a5
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex 181d3a0..7340f9f 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -291,8 +291,18 @@ The grammars are C code compiled with cgo (the MIT-licensedthose languages, in the `tools/graft-agents` module only; the gitai serverhas no such dependency). Building graft-resolve therefore needs a C compilerand `CGO_ENABLED=1`, the default when a compiler is installed. A-graft-resolve built without cgo fails closed: it rejects every `llm` answer-for a file that has a grammar, and `graft-resolve bench run` refuses to start.+graft-resolve built without cgo refuses to start with an `llm` strategy+configured, with a message naming that requirement. The syntax check itself+fails closed in such a build (it rejects every file that has a grammar), and+`graft-resolve bench run` refuses to start.++With the `openai` provider, `effort` (`minimal`, `low`, `medium`, `high` or+`xhigh`) is sent as OpenRouter's `reasoning` parameter (`{"effort": ...}`) on+every call; configure it only for an endpoint that takes that parameter. The+provider takes no `options`, and a configuration that sets any, or an effort+it does not know, fails at startup. No setting is dropped silently. An llm+strategy also refuses to start in a graft-resolve built without cgo (see+above).The provider's API keys are withheld from the model: they are read from theenvironment or a file by the provider, never put in the prompt. Everythingdiff --git a/tools/graft-agents/cmd/graft-resolve/metrics_test.go b/tools/graft-agents/cmd/graft-resolve/metrics_test.goindex 0cb1b89..69a8ac2 100644--- a/tools/graft-agents/cmd/graft-resolve/metrics_test.go+++ b/tools/graft-agents/cmd/graft-resolve/metrics_test.go@@ -1,3 +1,5 @@+//go:build cgo+package mainimport (diff --git a/tools/graft-agents/llm/openai/effort_test.go b/tools/graft-agents/llm/openai/effort_test.gonew file mode 100644index 0000000..2867699--- /dev/null+++ b/tools/graft-agents/llm/openai/effort_test.go@@ -0,0 +1,73 @@+package openai++import (+ "context"+ "strings"+ "testing"++ "github.com/feder-cr/git-ai/tools/graft-agents/llm"+)++func TestEffortSentAsReasoning(t *testing.T) {+ f := newFakeServer(t, scripted(chatOK("ok")))+ p := newProvider(t, f, func(c *llm.Config) { c.Effort = " Low " })++ if _, err := p.Complete(context.Background(), llm.Request{Prompt: "hi"}); err != nil {+ t.Fatal(err)+ }+ r, ok := f.requests()[0].body["reasoning"].(map[string]any)+ if !ok || r["effort"] != "low" || len(r) != 1 {+ t.Fatalf("reasoning = %v, want {\"effort\": \"low\"}", f.requests()[0].body["reasoning"])+ }+}++func TestEffortKeptOnSchemaFallback(t *testing.T) {+ // The effort is part of every request, whatever response format the call falls back to.+ f := newFakeServer(t, scripted(+ reply{status: 400, body: `{"error":{"message":"response_format json_schema is not supported"}}`},+ chatOK(`{"a":1}`),+ ))+ p := newProvider(t, f, func(c *llm.Config) { c.Effort = "high" })+ if _, err := p.Complete(context.Background(), llm.Request{Prompt: "hi", JSONSchema: map[string]any{"type": "object"}}); err != nil {+ t.Fatal(err)+ }+ if n := len(f.requests()); n != 2 {+ t.Fatalf("%d requests, want the json_schema one and its fallback", n)+ }+ for i, rec := range f.requests() {+ if r, _ := rec.body["reasoning"].(map[string]any); r["effort"] != "high" {+ t.Fatalf("request %d: reasoning = %v", i, rec.body["reasoning"])+ }+ }+}++func TestNoEffortSendsNoReasoning(t *testing.T) {+ f := newFakeServer(t, scripted(chatOK("ok")))+ p := newProvider(t, f)+ if _, err := p.Complete(context.Background(), llm.Request{Prompt: "hi"}); err != nil {+ t.Fatal(err)+ }+ if _, ok := f.requests()[0].body["reasoning"]; ok {+ t.Fatalf("reasoning sent without an effort: %v", f.requests()[0].body)+ }+}++func TestNewRejectsUnsupportedSettings(t *testing.T) {+ for name, tc := range map[string]struct {+ cfg llm.Config+ want string+ }{+ "unknown effort": {llm.Config{Type: "openai", Model: "m", Effort: "turbo"}, `invalid effort "turbo"`},+ "options": {llm.Config{Type: "openai", Model: "m", Options: map[string]string{"region": "eu", "a": "b"}}, "unsupported options [a region]"},+ } {+ _, err := New(tc.cfg)+ if err == nil || !strings.Contains(err.Error(), tc.want) {+ t.Errorf("%s: err = %v, want %q", name, err, tc.want)+ }+ }+ for _, e := range []string{"minimal", "low", "medium", "high", "xhigh"} {+ if _, err := New(llm.Config{Type: "openai", Model: "m", Effort: e}); err != nil {+ t.Errorf("effort %s: %v", e, err)+ }+ }+}diff --git a/tools/graft-agents/llm/openai/openai.go b/tools/graft-agents/llm/openai/openai.goindex 45fcd8a..2910fed 100644--- a/tools/graft-agents/llm/openai/openai.go+++ b/tools/graft-agents/llm/openai/openai.go@@ -22,6 +22,7 @@ import ("net/url""os""regexp"+ "sort""strconv""strings""sync/atomic"@@ -89,6 +90,7 @@ type Provider struct {apiKey stringmodel stringmaxTokens int+ effort stringtimeout time.Durationclient *http.ClientmaxResponseBytes int64@@ -109,6 +111,20 @@ func New(cfg llm.Config) (*Provider, error) {if cfg.MaxTokens < 0 {return nil, errors.New("openai: max_tokens must not be negative")}+ // No setting is dropped silently: the provider supports no options, and an+ // effort it cannot send is an error.+ if len(cfg.Options) > 0 {+ keys := make([]string, 0, len(cfg.Options))+ for k := range cfg.Options {+ keys = append(keys, k)+ }+ sort.Strings(keys)+ return nil, fmt.Errorf("openai: unsupported options %v: this provider takes none", keys)+ }+ effort := strings.ToLower(strings.TrimSpace(cfg.Effort))+ if effort != "" && !efforts[effort] {+ return nil, fmt.Errorf("openai: invalid effort %q (want minimal, low, medium, high or xhigh)", cfg.Effort)+ }timeout, err := cfg.TimeoutOrDefault()if err != nil {return nil, err@@ -132,6 +148,7 @@ func New(cfg llm.Config) (*Provider, error) {apiKey: os.Getenv(keyEnv),model: model,maxTokens: cfg.MaxTokens,+ effort: effort,timeout: timeout,client: &http.Client{},maxResponseBytes: defaultMaxResponseBytes,@@ -272,11 +289,22 @@ func appendSchemaInstruction(system string, schemaJSON []byte) string {return system + "\n\n" + instruction}+// efforts are the reasoning efforts the provider sends.+var efforts = map[string]bool{"minimal": true, "low": true, "medium": true, "high": true, "xhigh": true}+type chatRequest struct {- Model string `json:"model"`- Messages []message `json:"messages"`- MaxTokens int `json:"max_tokens,omitempty"`- ResponseFormat any `json:"response_format,omitempty"`+ Model string `json:"model"`+ Messages []message `json:"messages"`+ MaxTokens int `json:"max_tokens,omitempty"`+ ResponseFormat any `json:"response_format,omitempty"`+ Reasoning *reasoning `json:"reasoning,omitempty"`+}++// reasoning is OpenRouter's reasoning parameter, sent when an effort is+// configured. An endpoint that does not know it may reject the request, so+// configure an effort only for an endpoint that takes it.+type reasoning struct {+ Effort string `json:"effort"`}type message struct {@@ -290,6 +318,9 @@ func (p *Provider) body(system, prompt string, maxTokens int, format any) []byteMaxTokens: maxTokens,ResponseFormat: format,}+ if p.effort != "" {+ req.Reasoning = &reasoning{Effort: p.effort}+ }if system != "" {req.Messages = append(req.Messages, message{Role: "system", Content: system})}diff --git a/tools/graft-agents/resolve/budget_test.go b/tools/graft-agents/resolve/budget_test.goindex 12ab0aa..1a801c7 100644--- a/tools/graft-agents/resolve/budget_test.go+++ b/tools/graft-agents/resolve/budget_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (diff --git a/tools/graft-agents/resolve/llm_fix_test.go b/tools/graft-agents/resolve/llm_fix_test.goindex cad22a8..1775d87 100644--- a/tools/graft-agents/resolve/llm_fix_test.go+++ b/tools/graft-agents/resolve/llm_fix_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (diff --git a/tools/graft-agents/resolve/llm_gate2_test.go b/tools/graft-agents/resolve/llm_gate2_test.goindex 8b84ef0..a39b004 100644--- a/tools/graft-agents/resolve/llm_gate2_test.go+++ b/tools/graft-agents/resolve/llm_gate2_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (diff --git a/tools/graft-agents/resolve/llm_messages_test.go b/tools/graft-agents/resolve/llm_messages_test.goindex 7f35f70..ee871ab 100644--- a/tools/graft-agents/resolve/llm_messages_test.go+++ b/tools/graft-agents/resolve/llm_messages_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (diff --git a/tools/graft-agents/resolve/llm_nocgo_test.go b/tools/graft-agents/resolve/llm_nocgo_test.gonew file mode 100644index 0000000..f3cb099--- /dev/null+++ b/tools/graft-agents/resolve/llm_nocgo_test.go@@ -0,0 +1,21 @@+//go:build !cgo++package resolve++import (+ "strings"+ "testing"+)++// TestLLMStrategyRefusesToStartWithoutGrammars checks that a build without cgo refuses an llm+// strategy at startup, naming the build requirement, instead of rejecting every answer later.+func TestLLMStrategyRefusesToStartWithoutGrammars(t *testing.T) {+ cfg, err := ParseConfig([]byte("[[strategy]]\ntype = \"llm\"\n[strategy.llm]\ntype = \"command\"\ncommand = [\"true\"]\n"))+ if err != nil {+ t.Fatal(err)+ }+ _, err = cfg.Build()+ if err == nil || !strings.Contains(err.Error(), "CGO_ENABLED=1") || !strings.Contains(err.Error(), "C compiler") {+ t.Fatalf("err = %v, want a refusal naming the build requirement", err)+ }+}diff --git a/tools/graft-agents/resolve/llm_strategy.go b/tools/graft-agents/resolve/llm_strategy.goindex 54db40d..a167d5b 100644--- a/tools/graft-agents/resolve/llm_strategy.go+++ b/tools/graft-agents/resolve/llm_strategy.go@@ -8,6 +8,7 @@ import ("github.com/feder-cr/git-ai/tools/graft-agents/llm""github.com/feder-cr/git-ai/tools/graft-agents/protocol"+ "github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax")// The "llm" strategy resolves content conflicts with a language model. It is@@ -68,6 +69,11 @@ type llmStrategy struct {}func newLLMStrategy(spec StrategySpec) (Strategy, error) {+ // Every llm answer gets the syntax check, which needs the tree-sitter grammars: a build+ // without them would reject every answer for those languages, so it does not start.+ if !syntax.Available {+ return nil, fmt.Errorf("strategy %q: the llm strategy needs the tree-sitter grammars of its syntax check, which this graft-resolve lacks: build it with CGO_ENABLED=1 and a C compiler", spec.Name)+ }if _, ok := spec.Options["min_confidence"]; ok {return nil, fmt.Errorf("strategy %q: min_confidence is gone: the model reports no confidence and an answer is accepted on the checks alone; remove the option", spec.Name)}diff --git a/tools/graft-agents/resolve/llm_strategy_test.go b/tools/graft-agents/resolve/llm_strategy_test.goindex 1274a34..0a10a10 100644--- a/tools/graft-agents/resolve/llm_strategy_test.go+++ b/tools/graft-agents/resolve/llm_strategy_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (