Git designed for AI agents.
graft-resolve: llm answers must parse, with tree-sitter (WP3)
Federico Elia84232cb2313e
diff --git a/doc/graft/RESOLVE.md b/doc/graft/RESOLVE.mdindex 1ae2362..24244e3 100644--- a/doc/graft/RESOLVE.md+++ b/doc/graft/RESOLVE.md@@ -272,10 +272,26 @@ asks for a JSON answer:and acceptance comes from the checks alone. The former `min_confidence`option is gone; a configuration that still sets it fails at startup.- Any answer containing conflict markers is rejected.+- The answer must parse. graft-resolve parses it with the tree-sitter grammar+ of its language, chosen by extension: TypeScript (`.ts`, `.mts`, `.cts`),+ TSX (`.tsx`), JavaScript (`.js`, `.mjs`, `.cjs`, `.jsx`), Python (`.py`,+ `.pyi`), Go (`.go`), Rust (`.rs`) and Java (`.java`). Any ERROR or MISSING+ node rejects the answer, and the reason gives the line and column of the+ first one. This is a parse, not a build: names, types and imports are not+ checked, and a few things a compiler refuses still parse (a JSX closing tag+ that does not match its opening tag, for one). Every other file gets the+ conflict-marker rule only. The check is always on for `llm` answers and+ does not apply to the other strategies.- The configured checks still run afterwards.- For large files the strategy sends only the conflict hunks with context andreassembles the file locally.+The grammars are C code compiled with cgo (the MIT-licensed+`github.com/tree-sitter/go-tree-sitter` bindings and the grammar modules of+those languages, in the `tools/graft-agents` module only; the gitai server+has no such dependency). Building graft-resolve therefore needs a C compiler+and `CGO_ENABLED=1`, the default when a compiler is installed.+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. Everythingelse is sent to the provider, so the three versions of a file and both commitdiff --git a/tools/graft-agents/go.mod b/tools/graft-agents/go.modindex 9008f95..eea79e2 100644--- a/tools/graft-agents/go.mod+++ b/tools/graft-agents/go.mod@@ -2,12 +2,22 @@ module github.com/feder-cr/git-ai/tools/graft-agentsgo 1.26.0-require github.com/anthropics/anthropic-sdk-go v1.80.0+require (+ github.com/anthropics/anthropic-sdk-go v1.80.0+ github.com/tree-sitter/go-tree-sitter v0.25.0+ github.com/tree-sitter/tree-sitter-go v0.25.0+ github.com/tree-sitter/tree-sitter-java v0.23.5+ github.com/tree-sitter/tree-sitter-javascript v0.25.0+ github.com/tree-sitter/tree-sitter-python v0.25.0+ github.com/tree-sitter/tree-sitter-rust v0.24.2+ github.com/tree-sitter/tree-sitter-typescript v0.23.2+)require (github.com/bahlo/generic-list-go v0.2.0 // indirectgithub.com/buger/jsonparser v1.1.2 // indirectgithub.com/invopop/jsonschema v0.14.0 // indirect+ github.com/mattn/go-pointer v0.0.1 // indirectgithub.com/pb33f/ordered-map/v2 v2.3.1 // indirectgithub.com/standard-webhooks/standard-webhooks/libraries v0.0.1 // indirectgithub.com/tidwall/gjson v1.18.0 // indirectdiff --git a/tools/graft-agents/go.sum b/tools/graft-agents/go.sumindex 8efaba9..85fa82d 100644--- a/tools/graft-agents/go.sum+++ b/tools/graft-agents/go.sum@@ -10,6 +10,8 @@ github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=github.com/invopop/jsonschema v0.14.0 h1:MHQqLhvpNUZfw+hM3AZDYK7jxO8FZoQeQM77g8iyZjg=github.com/invopop/jsonschema v0.14.0/go.mod h1:ygm6C2EaVNMBDPpaPlnOA2pFAxBnxGjFlMZABxm9n2I=+github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=+github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=github.com/pb33f/ordered-map/v2 v2.3.1 h1:5319HDO0aw4DA4gzi+zv4FXU9UlSs3xGZ40wcP1nBjY=github.com/pb33f/ordered-map/v2 v2.3.1/go.mod h1:qxFQgd0PkVUtOMCkTapqotNgzRhMPL7VvaHKbd1HnmQ=github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=@@ -28,6 +30,34 @@ github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=+github.com/tree-sitter/go-tree-sitter v0.25.0 h1:sx6kcg8raRFCvc9BnXglke6axya12krCJF5xJ2sftRU=+github.com/tree-sitter/go-tree-sitter v0.25.0/go.mod h1:r77ig7BikoZhHrrsjAnv8RqGti5rtSyvDHPzgTPsUuU=+github.com/tree-sitter/tree-sitter-c v0.23.4 h1:nBPH3FV07DzAD7p0GfNvXM+Y7pNIoPenQWBpvM++t4c=+github.com/tree-sitter/tree-sitter-c v0.23.4/go.mod h1:MkI5dOiIpeN94LNjeCp8ljXN/953JCwAby4bClMr6bw=+github.com/tree-sitter/tree-sitter-cpp v0.23.4 h1:LaWZsiqQKvR65yHgKmnaqA+uz6tlDJTJFCyFIeZU/8w=+github.com/tree-sitter/tree-sitter-cpp v0.23.4/go.mod h1:doqNW64BriC7WBCQ1klf0KmJpdEvfxyXtoEybnBo6v8=+github.com/tree-sitter/tree-sitter-embedded-template v0.23.2 h1:nFkkH6Sbe56EXLmZBqHHcamTpmz3TId97I16EnGy4rg=+github.com/tree-sitter/tree-sitter-embedded-template v0.23.2/go.mod h1:HNPOhN0qF3hWluYLdxWs5WbzP/iE4aaRVPMsdxuzIaQ=+github.com/tree-sitter/tree-sitter-go v0.25.0 h1:cEB0Q3LHgZtS+ECHx9wcP7AwzoOddJFQCVmytX42cVU=+github.com/tree-sitter/tree-sitter-go v0.25.0/go.mod h1:Jrx8QqYN0v7npv1fJRH1AznddllYiCMUChtVjxPK040=+github.com/tree-sitter/tree-sitter-html v0.23.2 h1:1UYDV+Yd05GGRhVnTcbP58GkKLSHHZwVaN+lBZV11Lc=+github.com/tree-sitter/tree-sitter-html v0.23.2/go.mod h1:gpUv/dG3Xl/eebqgeYeFMt+JLOY9cgFinb/Nw08a9og=+github.com/tree-sitter/tree-sitter-java v0.23.5 h1:J9YeMGMwXYlKSP3K4Us8CitC6hjtMjqpeOf2GGo6tig=+github.com/tree-sitter/tree-sitter-java v0.23.5/go.mod h1:NRKlI8+EznxA7t1Yt3xtraPk1Wzqh3GAIC46wxvc320=+github.com/tree-sitter/tree-sitter-javascript v0.25.0 h1:ZkWETb66/w8cc13yhfnNuHOLDQWl3BnKlH6f9AdR88c=+github.com/tree-sitter/tree-sitter-javascript v0.25.0/go.mod h1:lmGD1EJdCA+v0S1u2fFgepMg/opzSg/4pgFym2FPGAs=+github.com/tree-sitter/tree-sitter-json v0.24.8 h1:tV5rMkihgtiOe14a9LHfDY5kzTl5GNUYe6carZBn0fQ=+github.com/tree-sitter/tree-sitter-json v0.24.8/go.mod h1:F351KK0KGvCaYbZ5zxwx/gWWvZhIDl0eMtn+1r+gQbo=+github.com/tree-sitter/tree-sitter-php v0.23.11 h1:iHewsLNDmznh8kgGyfWfujsZxIz1YGbSd2ZTEM0ZiP8=+github.com/tree-sitter/tree-sitter-php v0.23.11/go.mod h1:T/kbfi+UcCywQfUNAJnGTN/fMSUjnwPXA8k4yoIks74=+github.com/tree-sitter/tree-sitter-python v0.25.0 h1:O6XD9v8U1LOcRc3cNj9nM7XufrtEBezE6VrpRrHZDf0=+github.com/tree-sitter/tree-sitter-python v0.25.0/go.mod h1:cpdthSy/Yoa28aJFBscFHlGiU+cnSiSh1kuDVtI8YeM=+github.com/tree-sitter/tree-sitter-ruby v0.23.1 h1:T/NKHUA+iVbHM440hFx+lzVOzS4dV6z8Qw8ai+72bYo=+github.com/tree-sitter/tree-sitter-ruby v0.23.1/go.mod h1:kUS4kCCQloFcdX6sdpr8p6r2rogbM6ZjTox5ZOQy8cA=+github.com/tree-sitter/tree-sitter-rust v0.24.2 h1:NL4nF67ib21RMzzfvkmXlVwe45vvhW10DVyO+D0z/W0=+github.com/tree-sitter/tree-sitter-rust v0.24.2/go.mod h1:hfeGWic9BAfgTrc7Xf6FaOAguCFJRo3RBbs7QJ6D7MI=+github.com/tree-sitter/tree-sitter-typescript v0.23.2 h1:/Odvphn18PniVixb9e97X0DbNVsU6Qocv9mfkyzdXwU=+github.com/tree-sitter/tree-sitter-typescript v0.23.2/go.mod h1:zjzMXT/Ulffel2xfOcAkQQkiAkmgnbtPGlFQw/5X4xA=go.yaml.in/yaml/v4 v4.0.0-rc.2 h1:/FrI8D64VSr4HtGIlUtlFMGsm7H7pWTbj6vOLVZcA6s=go.yaml.in/yaml/v4 v4.0.0-rc.2/go.mod h1:aZqd9kCMsGL7AuUv/m/PvWLdg5sjJsZ4oHDEnfPPfY0=golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=diff --git a/tools/graft-agents/resolve/bench/bench_test.go b/tools/graft-agents/resolve/bench/bench_test.goindex ee6024d..4f95ac8 100644--- a/tools/graft-agents/resolve/bench/bench_test.go+++ b/tools/graft-agents/resolve/bench/bench_test.go@@ -291,9 +291,12 @@ func TestReportCounts(t *testing.T) {rep := NewReport("test", []Result{{Family: FamilyGo, Outcome: OutcomeCorrect, Exact: true},{Family: FamilyGo, Outcome: OutcomeWrong},- {Family: FamilyPython, Outcome: OutcomeRejected},+ {Family: FamilyPython, Outcome: OutcomeRejected, HumanSyntaxError: "syntax check: a.py does not parse"},{Family: FamilyPython, Outcome: OutcomeNotSent, Reason: NotSentGenerated},})+ if rep.Total.HumanSyntax != 1 || rep.Families[FamilyPython].HumanSyntax != 1 {+ t.Fatalf("human syntax failures %+v", rep.Families)+ }if rep.Total.Cases != 4 || rep.Total.Accepted() != 2 || rep.Total.ErrorRate() != 0.5 {t.Fatalf("totals %+v", rep.Total)}@@ -304,7 +307,7 @@ func TestReportCounts(t *testing.T) {if err := rep.WriteMarkdown(&b); err != nil {t.Fatal(err)}- if !strings.Contains(b.String(), "| total | 4 | 1 (1) | 1 | 1 | 1 |") {+ if !strings.Contains(b.String(), "| total | 4 | 1 (1) | 1 | 1 | 1 |") || !strings.Contains(b.String(), "reject the right answer: 1 (go 0, python 1)") {t.Fatalf("markdown:\n%s", b.String())}}diff --git a/tools/graft-agents/resolve/bench/report.go b/tools/graft-agents/resolve/bench/report.goindex a0ed608..f8b4070 100644--- a/tools/graft-agents/resolve/bench/report.go+++ b/tools/graft-agents/resolve/bench/report.go@@ -18,6 +18,8 @@ type Counts struct {Model int `json:"model_error"`NotRun int `json:"not_run_cap"`Abstained int `json:"abstained"`+ // HumanSyntax counts the cases whose human resolution fails the syntax check.+ HumanSyntax int `json:"human_fails_syntax"`}// Accepted is the number of results that would land: correct and wrong.@@ -34,6 +36,9 @@ func (c Counts) ErrorRate() float64 {func (c *Counts) add(r Result) {c.Cases+++ if r.HumanSyntaxError != "" {+ c.HumanSyntax+++ }switch r.Outcome {case OutcomeCorrect:c.Correct++@@ -108,6 +113,12 @@ func (rep Report) WriteMarkdown(w io.Writer) error {}row("total", rep.Total)b.WriteString("\nThe error rate is wrong / (correct + wrong): the share of the results that would land and differ from the human resolution. Correct means equal after normalizing line endings and trailing whitespace; exact means byte-identical.\n")+ syntaxParts := make([]string, 0, len(families))+ for _, f := range families {+ syntaxParts = append(syntaxParts, fmt.Sprintf("%s %d", f, rep.Families[f].HumanSyntax))+ }+ fmt.Fprintf(&b, "\nHuman resolutions that fail the syntax check themselves, where the check would reject the right answer: %d (%s).\n",+ rep.Total.HumanSyntax, strings.Join(syntaxParts, ", "))if len(rep.NotSent) > 0 {reasons := make([]string, 0, len(rep.NotSent))for r := range rep.NotSent {diff --git a/tools/graft-agents/resolve/bench/run.go b/tools/graft-agents/resolve/bench/run.goindex 511f7f5..f393442 100644--- a/tools/graft-agents/resolve/bench/run.go+++ b/tools/graft-agents/resolve/bench/run.go@@ -11,6 +11,7 @@ import ("github.com/feder-cr/git-ai/tools/graft-agents/protocol""github.com/feder-cr/git-ai/tools/graft-agents/resolve"+ "github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax")// Outcomes of a case.@@ -45,6 +46,10 @@ type Result struct {Strategy string `json:"strategy,omitempty"`Reason string `json:"reason,omitempty"`Seconds float64 `json:"seconds"`+ // HumanSyntaxError is set when the human resolution of a case that was sent fails the+ // syntax check itself: the check would reject the right answer there (a grammar gap, or a+ // human merge that did not parse).+ HumanSyntaxError string `json:"human_syntax_error,omitempty"`}// Sample returns n cases of cases, stratified by family: each family's cases are shuffled with the@@ -158,6 +163,15 @@ func (r Runner) runCase(ctx context.Context, c Case, maxBytes int64) (Result, erreturn res, nil}+ if human != nil {+ if err := syntax.Check(ctx, c.Path, human); err != nil {+ if ctx.Err() != nil {+ return res, ctx.Err()+ }+ res.HumanSyntaxError = err.Error()+ }+ }+oursMessage, err := g.message(ctx, c.Ours)if err != nil {return res, errdiff --git a/tools/graft-agents/resolve/llm_strategy.go b/tools/graft-agents/resolve/llm_strategy.goindex 8c8378e..844816c 100644--- a/tools/graft-agents/resolve/llm_strategy.go+++ b/tools/graft-agents/resolve/llm_strategy.go@@ -40,8 +40,9 @@ import (// the file is limited to 8M cells, roughly 2800 changed lines on each side),// more conflicting regions than max_regions, or a region whose prompt exceeds// max_prompt_bytes. A rejected or failed answer is a strategy failure and the-// chain continues as well. Checks configured for the path still run on the-// accepted answer.+// chain continues as well. An answer for a path with a tree-sitter grammar+// (package syntax: TypeScript, JavaScript, Python, Go, Rust, Java) must parse,+// or it is a check failure; the checks configured for the path run after.//// The prompt carries the commit messages of both sides (request.messages) as// the intent of each side. The model reports no confidence: an answer is@@ -139,6 +140,9 @@ func (s *llmStrategy) Resolve(ctx context.Context, f File, req protocol.Request)return Resolution{Action: protocol.ActionWrite, Mode: f.Ours.Mode, Content: res.Content}, true, nil}+// syntaxChecked marks the llm answers for the syntax check (resolveFile).+func (s *llmStrategy) syntaxChecked() {}+// llmBytes keeps an empty side distinct from an absent one: ResolveLLM treats// nil as absent.func llmBytes(b []byte) []byte {diff --git a/tools/graft-agents/resolve/llm_syntax_test.go b/tools/graft-agents/resolve/llm_syntax_test.gonew file mode 100644index 0000000..b66021e--- /dev/null+++ b/tools/graft-agents/resolve/llm_syntax_test.go@@ -0,0 +1,97 @@+package resolve++import (+ "context"+ "errors"+ "strings"+ "testing"++ "github.com/feder-cr/git-ai/tools/graft-agents/llm"+ "github.com/feder-cr/git-ai/tools/graft-agents/protocol"+)++const wp3Config = `+[[strategy]]+type = "llm"++[strategy.llm]+type = "wp19-resolve-fake"+`++func wp3Conflict(path string) protocol.FileConflict {+ return protocol.FileConflict{+ Path: path,+ Kind: protocol.KindContent,+ Base: wp19Side(protocol.ModeFile, "package a\n\nvar x = 1\n"),+ Ours: wp19Side(protocol.ModeFile, "package a\n\nvar x = 2\n"),+ Theirs: wp19Side(protocol.ModeFile, "package a\n\nvar x = 3\n"),+ }+}++func TestLLMAnswerMustParse(t *testing.T) {+ wp19SetAnswer(func(context.Context, llm.Request) (*llm.Response, error) {+ return wp19Answer("package a\n\nvar x = 2 +\n"), nil+ })+ r := wp19Build(t, wp3Config)+ _, err := r.Resolve(context.Background(), wp19Request(wp3Conflict("a.go")))+ var unresolved *UnresolvedError+ if !errors.As(err, &unresolved) || len(unresolved.Failures) != 1 {+ t.Fatalf("err = %v, want the file unresolved", err)+ }+ attempts := unresolved.Failures[0].Attempts+ if len(attempts) != 1 || attempts[0].Kind != AttemptCheckFailed {+ t.Fatalf("attempts %+v, want one check failure", attempts)+ }+ if msg := attempts[0].Err.Error(); !strings.Contains(msg, "a.go does not parse as go") || !strings.Contains(msg, "line ") {+ t.Fatalf("reason %q", msg)+ }+}++func TestLLMAnswerThatParsesIsAccepted(t *testing.T) {+ wp19SetAnswer(func(context.Context, llm.Request) (*llm.Response, error) {+ return wp19Answer("package a\n\nvar x = 2 + 3\n"), nil+ })+ r := wp19Build(t, wp3Config)+ resp, err := r.Resolve(context.Background(), wp19Request(wp3Conflict("a.go")))+ if err != nil {+ t.Fatal(err)+ }+ if string(resp.Files[0].Content) != "package a\n\nvar x = 2 + 3\n" {+ t.Fatalf("content %q", resp.Files[0].Content)+ }+}++// TestLLMAnswerWithoutGrammarGetsMarkerRuleOnly checks that a path with no grammar is not parsed:+// any content without conflict markers is accepted.+func TestLLMAnswerWithoutGrammarGetsMarkerRuleOnly(t *testing.T) {+ wp19SetAnswer(func(context.Context, llm.Request) (*llm.Response, error) {+ return wp19Answer("package a\n\nvar x = 2 +\n"), nil+ })+ r := wp19Build(t, wp3Config)+ if _, err := r.Resolve(context.Background(), wp19Request(wp3Conflict("a.go.txt"))); err != nil {+ t.Fatalf("path without a grammar was parsed: %v", err)+ }+}++// TestOtherStrategiesAreNotSyntaxChecked checks that the syntax check belongs to the llm answers:+// a union of two Go lines that does not parse is still the union strategy's answer.+func TestOtherStrategiesAreNotSyntaxChecked(t *testing.T) {+ cfg, err := ParseConfig([]byte("[[strategy]]\ntype = \"union\"\npaths = [\"*.go\"]\n"))+ if err != nil {+ t.Fatal(err)+ }+ r, err := cfg.Build()+ if err != nil {+ t.Fatal(err)+ }+ f := protocol.FileConflict{+ Path: "a.go",+ Kind: protocol.KindContent,+ Base: wp19Side(protocol.ModeFile, "package a\n\nfunc F() {\n}\n"),+ Ours: wp19Side(protocol.ModeFile, "package a\n\nfunc F() {\n\tx := (\n}\n"),+ Theirs: wp19Side(protocol.ModeFile, "package a\n\nfunc F() {\n\ty := [\n}\n"),+ }+ if _, err := r.Resolve(context.Background(), wp19Request(f)); err != nil {+ t.Fatalf("union answer rejected: %v", err)+ }+}diff --git a/tools/graft-agents/resolve/resolve.go b/tools/graft-agents/resolve/resolve.goindex e54484c..fecbfff 100644--- a/tools/graft-agents/resolve/resolve.go+++ b/tools/graft-agents/resolve/resolve.go@@ -34,6 +34,7 @@ import ("time""github.com/feder-cr/git-ai/tools/graft-agents/protocol"+ "github.com/feder-cr/git-ai/tools/graft-agents/resolve/syntax")// DefaultCheckTimeout bounds one check command when the check sets no timeout.@@ -64,6 +65,12 @@ type Strategy interface {Resolve(ctx context.Context, f File, req protocol.Request) (Resolution, bool, error)}+// syntaxChecked is implemented by the strategies whose answers always get the syntax check+// (package syntax): the llm strategy. The check runs before the configured checks.+type syntaxChecked interface {+ syntaxChecked()+}+// Recorder is implemented by strategies that want to learn accepted answers.type Recorder interface {Record(ctx context.Context, f File, res Resolution) error@@ -370,6 +377,15 @@ func (r *Resolver) resolveFile(ctx context.Context, req protocol.Request, f Fileattempts = append(attempts, Attempt{Strategy: b.Name, Kind: AttemptRejected, Err: err})continue}+ if _, ok := b.Strategy.(syntaxChecked); ok && res.Action == protocol.ActionWrite && res.Mode != protocol.ModeSymlink {+ if err := syntax.Check(ctx, f.Path, res.Content); err != nil {+ if ctxErr := ctx.Err(); ctxErr != nil {+ return protocol.ResolvedFile{}, "", nil, ctxErr+ }+ attempts = append(attempts, Attempt{Strategy: b.Name, Kind: AttemptCheckFailed, Err: err})+ continue+ }+ }if err := r.runChecks(ctx, f.Path, res); err != nil {if ctxErr := ctx.Err(); ctxErr != nil {return protocol.ResolvedFile{}, "", nil, ctxErrdiff --git a/tools/graft-agents/resolve/syntax/syntax.go b/tools/graft-agents/resolve/syntax/syntax.gonew file mode 100644index 0000000..b7cf0e5--- /dev/null+++ b/tools/graft-agents/resolve/syntax/syntax.go@@ -0,0 +1,146 @@+// Package syntax checks that a resolved file still parses, with the tree-sitter grammar of its+// language. It is the syntax check of PLAN10 (3.2): any ERROR or MISSING node in the tree rejects+// the content, with the position of the first one. A path whose extension has no grammar here is+// 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).+package syntax++import (+ "context"+ "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.+const (+ TypeScript = "typescript"+ TSX = "tsx"+ JavaScript = "javascript"+ Python = "python"+ Go = "go"+ Rust = "rust"+ 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,+ ".js": JavaScript, ".mjs": JavaScript, ".cjs": JavaScript, ".jsx": JavaScript,+ ".py": Python, ".pyi": Python,+ ".go": Go,+ ".rs": Rust,+ ".java": Java,+}++// 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) {+ lang, ok := extensions[strings.ToLower(path.Ext(p))]+ return lang, ok+}++// Error is a syntax error in checked content.+type Error struct {+ Path string+ Language string+ // Line and Column are 1-based; Column counts bytes.+ Line, Column int+ // Missing is the kind of the node the parser had to insert, empty for an ERROR node (text+ // that fits nowhere in the grammar).+ Missing string+}++func (e *Error) Error() string {+ what := "a syntax error"+ if e.Missing != "" {+ what = fmt.Sprintf("a missing %q", e.Missing)+ }+ return fmt.Sprintf("syntax check: %s does not parse as %s: %s at line %d, column %d", e.Path, e.Language, what, e.Line, e.Column)+}++// Check parses content as the language of p and returns an *Error for the first ERROR or MISSING+// node of the tree. It returns nil when content parses, and when p has no grammar. It fails with+// ctx's error when ctx ends during the parse.+func Check(ctx context.Context, p string, content []byte) error {+ lang, ok := LanguageOf(p)+ 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+}diff --git a/tools/graft-agents/resolve/syntax/syntax_test.go b/tools/graft-agents/resolve/syntax/syntax_test.gonew file mode 100644index 0000000..ed0a603--- /dev/null+++ b/tools/graft-agents/resolve/syntax/syntax_test.go@@ -0,0 +1,118 @@+package syntax++import (+ "context"+ "errors"+ "strings"+ "testing"+)++func TestCheckEveryGrammar(t *testing.T) {+ for _, tc := range []struct {+ path, valid, invalid string+ }{+ {"a.ts", "const x: number = 1;\nexport function f(a: string): string { return a; }\n", "const x: number = ;\n"},+ {"a.tsx", "export const A = () => <div className=\"a\">{1}</div>;\n", "export const A = (): JSX.Element => <div>{1</div>;\n"},+ {"a.js", "const a = [1, 2].map((x) => x * 2);\n", "const a = [1, 2.map((x) => x * 2);\n"},+ {"a.jsx", "const A = () => <p>hi</p>;\n", "const A = () => <p>hi</p;\n"},+ {"a.py", "def f(x):\n return x + 1\n", "def f(x)\n return x + 1\n"},+ {"a.go", "package a\n\nfunc F(x int) int { return x + 1 }\n", "package a\n\nfunc F(x int) int { return x + }\n"},+ {"a.rs", "fn f(x: i32) -> i32 { x + 1 }\n", "fn f(x: i32) -> i32 { x + }\n"},+ {"A.java", "class A { int f(int x) { return x + 1; } }\n", "class A { int f(int x) { return x + 1 } }\n"},+ } {+ t.Run(tc.path, func(t *testing.T) {+ if err := Check(context.Background(), tc.path, []byte(tc.valid)); err != nil {+ t.Fatalf("valid content rejected: %v", err)+ }+ err := Check(context.Background(), tc.path, []byte(tc.invalid))+ var se *Error+ if !errors.As(err, &se) {+ t.Fatalf("invalid content: err = %v, want a syntax error", err)+ }+ if se.Path != tc.path || se.Line < 1 || se.Column < 1 {+ t.Fatalf("error %+v", se)+ }+ })+ }+}++// TestCheckGrammarLimits records what the grammars accept although a compiler would not: the+// check is a parse, not a build. A JSX closing tag that does not match its opening tag parses.+func TestCheckGrammarLimits(t *testing.T) {+ for p, src := range map[string]string{+ "a.tsx": "export const A = () => <div>{1}</span>;\n",+ "a.go": "package a\n\nfunc F() int { return undefinedName }\n",+ "A.java": "class A { void f() { int x = \"not an int\"; } }\n",+ } {+ if err := Check(context.Background(), p, []byte(src)); err != nil {+ t.Errorf("%s: %v (if the grammar now rejects this, update the documented limits)", p, err)+ }+ }+}++func TestCheckReportsFirstErrorPosition(t *testing.T) {+ src := "package a\n\nfunc F() {}\n\nfunc G() { x := }\n\nfunc H() { y := }\n"+ err := Check(context.Background(), "a.go", []byte(src))+ var se *Error+ if !errors.As(err, &se) {+ t.Fatalf("err = %v", err)+ }+ if se.Line != 5 || se.Language != Go {+ t.Fatalf("error %+v, want the first one, on line 5", se)+ }+ if !strings.Contains(se.Error(), "a.go does not parse as go") || !strings.Contains(se.Error(), "line 5") {+ t.Fatalf("message %q", se.Error())+ }+}++func TestCheckReportsMissingNode(t *testing.T) {+ err := Check(context.Background(), "A.java", []byte("class A { int f() { return 1 } }\n"))+ var se *Error+ if !errors.As(err, &se) {+ t.Fatalf("err = %v", err)+ }+ if se.Missing != ";" || !strings.Contains(se.Error(), `missing ";"`) {+ t.Fatalf("error %+v (%v), want a missing \";\"", se, se)+ }+}++func TestCheckLeavesOtherLanguagesAlone(t *testing.T) {+ for _, p := range []string{"README.md", "a.c", "Makefile", "a.json", "a.yaml", "go.mod", "a.min.css"} {+ if _, ok := LanguageOf(p); ok {+ t.Fatalf("%s has a grammar", p)+ }+ if err := Check(context.Background(), p, []byte("{{{ not code")); err != nil {+ t.Fatalf("%s checked: %v", p, err)+ }+ }+}++func TestLanguageOf(t *testing.T) {+ for p, want := range map[string]string{+ "src/a.TS": TypeScript, "a.d.ts": TypeScript, "a.mts": TypeScript, "a.cts": TypeScript, "a.tsx": TSX,+ "a.js": JavaScript, "a.mjs": JavaScript, "a.cjs": JavaScript, "a.jsx": JavaScript,+ "a.py": Python, "a.pyi": Python, "a.go": Go, "lib.rs": Rust, "A.java": Java,+ } {+ if got, ok := LanguageOf(p); !ok || got != want {+ t.Errorf("LanguageOf(%q) = %q, %v; want %q", p, got, ok, want)+ }+ }+}++func TestCheckEmptyContent(t *testing.T) {+ // An empty Python or JavaScript file is a valid program.+ for _, p := range []string{"a.py", "a.js", "a.ts"} {+ if err := Check(context.Background(), p, nil); err != nil {+ t.Fatalf("%s: %v", p, err)+ }+ }+}++func TestCheckHonoursContext(t *testing.T) {+ ctx, cancel := context.WithCancel(context.Background())+ cancel()+ big := strings.Repeat("x = [1, 2, 3]\n", 200000)+ if err := Check(ctx, "a.py", []byte(big)); !errors.Is(err, context.Canceled) {+ t.Fatalf("err = %v, want context.Canceled", err)+ }+}