Git designed for AI agents.
graft-resolve: the syntax check fails closed without cgo
Federico Elia52a53a20aaf8
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex 24244e3..66982aa 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -290,7 +290,9 @@ The grammars are C code compiled with cgo (the MIT-licensed`github.com/tree-sitter/go-tree-sitter` bindings and the grammar modules ofthose languages, in the `tools/graft-agents` module only; the gitai serverhas no such dependency). Building graft-resolve therefore needs a C compiler-and `CGO_ENABLED=1`, the default when a compiler is installed.+and `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.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/bench.go b/tools/graft-agents/cmd/graft-resolve/bench.goindex 8910074..3bea1bc 100644--- a/tools/graft-agents/cmd/graft-resolve/bench.go+++ b/tools/graft-agents/cmd/graft-resolve/bench.go@@ -14,6 +14,7 @@ import ("syscall""github.com/feder-cr/git-ai/tools/graft-agents/resolve/bench"+ "github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax")// runBench runs the benchmark subcommands:@@ -127,6 +128,10 @@ func benchRun(ctx context.Context, args []string, stdout, stderr io.Writer) intreturn 2}+ if !syntax.Available {+ fmt.Fprintln(stderr, "graft-resolve bench run: this graft-resolve was built without cgo, so the syntax check would reject every answer; build it with CGO_ENABLED=1")+ return 1+ }res, err := loadResolver(*configPath)if err != nil {fmt.Fprintf(stderr, "graft-resolve bench run: %v\n", err)diff --git a/tools/graft-agents/cmd/graft-resolve/bench_test.go b/tools/graft-agents/cmd/graft-resolve/bench_test.goindex ccd59b4..34afbc9 100644--- a/tools/graft-agents/cmd/graft-resolve/bench_test.go+++ b/tools/graft-agents/cmd/graft-resolve/bench_test.go@@ -10,6 +10,7 @@ import ("testing""github.com/feder-cr/git-ai/tools/graft-agents/resolve/bench"+ "github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax")// gitIn runs git in dir with a fixed identity, given on the command line.@@ -89,6 +90,13 @@ func TestBenchMineAndRun(t *testing.T) {stdout.Reset()stderr.Reset()+ if !syntax.Available {+ // Without cgo the syntax check would reject every answer: the run refuses to start.+ if code := run([]string{"bench", "run", "--manifest", manifest, "--clones", clones}, nil, &stdout, &stderr); code != 1 || !strings.Contains(stderr.String(), "without cgo") {+ t.Fatalf("run without cgo exited %d: %s", code, stderr.String())+ }+ return+ }jsonOut := filepath.Join(work, "report.json")if code := run([]string{"bench", "run", "--manifest", manifest, "--clones", clones, "--label", "trivial", "--json-out", jsonOut}, nil, &stdout, &stderr); code != 0 {t.Fatalf("run exited %d: %s", code, stderr.String())diff --git a/tools/graft-agents/resolve/llm_syntax_test.go b/tools/graft-agents/resolve/llm_syntax_test.goindex b66021e..41bb2e4 100644--- a/tools/graft-agents/resolve/llm_syntax_test.go+++ b/tools/graft-agents/resolve/llm_syntax_test.go@@ -1,3 +1,5 @@+//go:build cgo+package resolveimport (diff --git a/tools/graft-agents/resolve/syntax/nocgo_test.go b/tools/graft-agents/resolve/syntax/nocgo_test.gonew file mode 100644index 0000000..35ed8d1--- /dev/null+++ b/tools/graft-agents/resolve/syntax/nocgo_test.go@@ -0,0 +1,20 @@+//go:build !cgo++package syntax++import (+ "context"+ "errors"+ "testing"+)++// TestCheckFailsClosedWithoutCgo checks that a build without the grammars rejects the content of+// every path that has one, and leaves the other paths alone.+func TestCheckFailsClosedWithoutCgo(t *testing.T) {+ if err := Check(context.Background(), "a.go", []byte("package a\n")); !errors.Is(err, ErrUnavailable) {+ t.Fatalf("err = %v, want ErrUnavailable", err)+ }+ if err := Check(context.Background(), "README.md", []byte("# a\n")); err != nil {+ t.Fatalf("path without a grammar: %v", err)+ }+}diff --git a/tools/graft-agents/resolve/syntax/parse_cgo.go b/tools/graft-agents/resolve/syntax/parse_cgo.gonew file mode 100644index 0000000..750e856--- /dev/null+++ b/tools/graft-agents/resolve/syntax/parse_cgo.go@@ -0,0 +1,87 @@+//go:build cgo++package syntax++import (+ "context"+ "fmt"++ sitter "github.com/tree-sitter/go-tree-sitter"+ golang "github.com/tree-sitter/tree-sitter-go/bindings/go"+ java "github.com/tree-sitter/tree-sitter-java/bindings/go"+ javascript "github.com/tree-sitter/tree-sitter-javascript/bindings/go"+ python "github.com/tree-sitter/tree-sitter-python/bindings/go"+ rust "github.com/tree-sitter/tree-sitter-rust/bindings/go"+ typescript "github.com/tree-sitter/tree-sitter-typescript/bindings/go"+)++// Available reports whether this build parses (it was built with cgo).+const Available = true++var grammars = map[string]*sitter.Language{+ TypeScript: sitter.NewLanguage(typescript.LanguageTypescript()),+ TSX: sitter.NewLanguage(typescript.LanguageTSX()),+ JavaScript: sitter.NewLanguage(javascript.Language()),+ Python: sitter.NewLanguage(python.Language()),+ Go: sitter.NewLanguage(golang.Language()),+ Rust: sitter.NewLanguage(rust.Language()),+ Java: sitter.NewLanguage(java.Language()),+}++func parse(ctx context.Context, lang, p string, content []byte) error {+ parser := sitter.NewParser()+ defer parser.Close()+ if err := parser.SetLanguage(grammars[lang]); err != nil {+ return fmt.Errorf("syntax check: %s grammar: %w", lang, err)+ }++ n := len(content)+ tree := parser.ParseWithOptions(func(i int, _ sitter.Point) []byte {+ if i < n {+ return content[i:]+ }+ return nil+ }, nil, &sitter.ParseOptions{ProgressCallback: func(sitter.ParseState) bool { return ctx.Err() != nil }})+ if tree == nil {+ if err := ctx.Err(); err != nil {+ return err+ }+ return fmt.Errorf("syntax check: %s: the %s parser gave no tree", p, lang)+ }+ defer tree.Close()++ root := tree.RootNode()+ if !root.HasError() {+ return nil+ }+ bad := firstError(root)+ if bad == nil {+ // HasError without an ERROR or MISSING node below cannot happen; reject anyway.+ bad = root+ }+ pos := bad.StartPosition()+ e := &Error{Path: p, Language: lang, Line: int(pos.Row) + 1, Column: int(pos.Column) + 1}+ if bad.IsMissing() {+ e.Missing = bad.Kind()+ }+ return e+}++// firstError returns the first ERROR or MISSING node in document order, descending only into+// nodes that contain an error.+func firstError(root *sitter.Node) *sitter.Node {+ stack := []*sitter.Node{root}+ for len(stack) > 0 {+ node := stack[len(stack)-1]+ stack = stack[:len(stack)-1]+ if node.IsError() || node.IsMissing() {+ return node+ }+ for i := node.ChildCount(); i > 0; i-- {+ if child := node.Child(i - 1); child != nil && child.HasError() {+ stack = append(stack, child)+ }+ }+ }+ return nil+}diff --git a/tools/graft-agents/resolve/syntax/parse_nocgo.go b/tools/graft-agents/resolve/syntax/parse_nocgo.gonew file mode 100644index 0000000..66c73eb--- /dev/null+++ b/tools/graft-agents/resolve/syntax/parse_nocgo.go@@ -0,0 +1,12 @@+//go:build !cgo++package syntax++import "context"++// Available reports whether this build parses (it was built with cgo).+const Available = false++func parse(context.Context, string, string, []byte) error {+ return ErrUnavailable+}diff --git a/tools/graft-agents/resolve/syntax/syntax.go b/tools/graft-agents/resolve/syntax/syntax.goindex b7cf0e5..36d84b9 100644--- a/tools/graft-agents/resolve/syntax/syntax.go+++ b/tools/graft-agents/resolve/syntax/syntax.go@@ -4,22 +4,16 @@// not checked (the marker rule, which every strategy gets, still applies to it).//// The grammars are compiled from C with cgo, so graft-resolve needs a C compiler to build-// (CGO_ENABLED=1).+// (CGO_ENABLED=1). Built without cgo, the package fails closed: Check rejects the content of+// every path that has a grammar with ErrUnavailable, so no answer is taken unchecked.package syntaximport ("context"+ "errors""fmt""path""strings"-- sitter "github.com/tree-sitter/go-tree-sitter"- golang "github.com/tree-sitter/tree-sitter-go/bindings/go"- java "github.com/tree-sitter/tree-sitter-java/bindings/go"- javascript "github.com/tree-sitter/tree-sitter-javascript/bindings/go"- python "github.com/tree-sitter/tree-sitter-python/bindings/go"- rust "github.com/tree-sitter/tree-sitter-rust/bindings/go"- typescript "github.com/tree-sitter/tree-sitter-typescript/bindings/go")// Language names, as Check reports them.@@ -33,16 +27,6 @@ const (Java = "java")-var grammars = map[string]*sitter.Language{- TypeScript: sitter.NewLanguage(typescript.LanguageTypescript()),- TSX: sitter.NewLanguage(typescript.LanguageTSX()),- JavaScript: sitter.NewLanguage(javascript.Language()),- Python: sitter.NewLanguage(python.Language()),- Go: sitter.NewLanguage(golang.Language()),- Rust: sitter.NewLanguage(rust.Language()),- Java: sitter.NewLanguage(java.Language()),-}-var extensions = map[string]string{".ts": TypeScript, ".mts": TypeScript, ".cts": TypeScript,".tsx": TSX,@@ -53,6 +37,9 @@ var extensions = map[string]string{".java": Java,}+// ErrUnavailable is the error of Check, for a path with a grammar, in a build without cgo.+var ErrUnavailable = errors.New("syntax check: unavailable, graft-resolve was built without cgo")+// LanguageOf returns the language whose grammar checks p, chosen by its extension (case does not// matter), and false when there is none.func LanguageOf(p string) (string, bool) {@@ -87,60 +74,5 @@ func Check(ctx context.Context, p string, content []byte) error {if !ok {return nil}-- parser := sitter.NewParser()- defer parser.Close()- if err := parser.SetLanguage(grammars[lang]); err != nil {- return fmt.Errorf("syntax check: %s grammar: %w", lang, err)- }-- n := len(content)- tree := parser.ParseWithOptions(func(i int, _ sitter.Point) []byte {- if i < n {- return content[i:]- }- return nil- }, nil, &sitter.ParseOptions{ProgressCallback: func(sitter.ParseState) bool { return ctx.Err() != nil }})- if tree == nil {- if err := ctx.Err(); err != nil {- return err- }- return fmt.Errorf("syntax check: %s: the %s parser gave no tree", p, lang)- }- defer tree.Close()-- root := tree.RootNode()- if !root.HasError() {- return nil- }- bad := firstError(root)- if bad == nil {- // HasError without an ERROR or MISSING node below cannot happen; reject anyway.- bad = root- }- pos := bad.StartPosition()- e := &Error{Path: p, Language: lang, Line: int(pos.Row) + 1, Column: int(pos.Column) + 1}- if bad.IsMissing() {- e.Missing = bad.Kind()- }- return e-}--// firstError returns the first ERROR or MISSING node in document order, descending only into-// nodes that contain an error.-func firstError(root *sitter.Node) *sitter.Node {- stack := []*sitter.Node{root}- for len(stack) > 0 {- node := stack[len(stack)-1]- stack = stack[:len(stack)-1]- if node.IsError() || node.IsMissing() {- return node- }- for i := node.ChildCount(); i > 0; i-- {- if child := node.Child(i - 1); child != nil && child.HasError() {- stack = append(stack, child)- }- }- }- return nil+ return parse(ctx, lang, p, content)}diff --git a/tools/graft-agents/resolve/syntax/syntax_test.go b/tools/graft-agents/resolve/syntax/syntax_test.goindex ed0a603..ca74e34 100644--- a/tools/graft-agents/resolve/syntax/syntax_test.go+++ b/tools/graft-agents/resolve/syntax/syntax_test.go@@ -1,3 +1,5 @@+//go:build cgo+package syntaximport (