Skip to content

Go Port Phase 4: Native tree-sitter AST extraction engine - #8

Merged
kristofer merged 7 commits into
mainfrom
copilot/implement-phase-4-ast-extraction-engine
Apr 28, 2026
Merged

kristofer merged 7 commits into
mainfrom
copilot/implement-phase-4-ast-extraction-engine

Conversation

Copilot AI commented Apr 27, 2026 •

Copy link
Copy Markdown

Implements Phase 4 of the Go port plan: a parallel AST extraction engine using native CGO go-tree-sitter bindings (no WASM). Covers grammar registry, parser pooling, per-language configs, and a parallel orchestrator with hash-based change detection.

Grammar Registry (grammars.go)

  • Lazy sync.Once initialization per language via GrammarRegistry
  • DetectLanguage() with .h → C/C++ heuristic (keyword scan for namespace, class, template<, #pragma once, ::)
  • IsLanguageSupported() for 16 languages; Dart/Pascal/Liquid return false (no go-tree-sitter grammar available)

Parser Wrapper (tree_sitter.go)

  • Global parserPool (capped at 32 idle parsers) shared across all goroutines
  • ParseSource(lang, source) → *sitter.Tree; ExtractFromSource(path, source, lang) → *ExtractionResult

Language Configs (languages/)

Data-driven LangConfig structs — node-type lists plus callback hooks — one per language group:

File Languages
typescript.go TypeScript, TSX, JavaScript, JSX
python.go Python
golang.go Go (receiver-type extraction, type_declaration → struct/interface/alias)
rust.go Rust (struct, trait, impl-block method hoisting)
java.go Java (modifier-based visibility)
other.go C#, PHP, Ruby, Swift, Kotlin, C, C++, Svelte

Core Walker (extractor.go)

Generic recursive AST visitor driven by LangConfig. Produces Node/Edge/UnresolvedReference with:

  • Scope stack → qualified names (Outer::Inner::method)
  • contains edges from every parent to child symbol
  • Node IDs as kind:sha256(path:kind:name:line)[:32] (matches TypeScript format)
  • Special dispatch for Go type_declaration, Rust impl_item, Python decorated_definition, TS export_statement

Orchestrator (orchestrator.go)

  • ScanFiles() with full ** glob matching against include/exclude patterns
  • IndexAll(): fan-out to runtime.NumCPU() workers; SHA-256 hash-based skip for unchanged files
  • SyncFiles(changedFiles): same pipeline restricted to a provided file list
  • DB interface for storage (matches existing db package method signatures)

Tests

16 tests covering language detection, symbol extraction (TypeScript, Python, Go, Rust, Java), edge generation, hash change detection, parallel indexing, SyncFiles, and unresolved import refs.

Copilot AI linked an issue Apr 27, 2026 that may be closed by this pull request
Copilot AI and others added 5 commits April 27, 2026 19:14
Add the complete tree-sitter-based AST extraction engine for the Go port
of CodeGraph. All 15 tests pass.

## Changes

### New packages

**internal/extraction/languages/** (package ):
-  –  struct with node-type lists and callback
  functions;  dispatcher
-  – TypeScript/JavaScript/JSX/TSX config
-  – Python config (docstring extraction, visibility heuristic)
-  – Go config (type_declaration → struct/interface/alias,
  method_declaration vs function_declaration)
-  – Rust config (impl_item body for methods, visibility_modifier)
-  – Java config (modifiers-based visibility and export)
-  – C#, PHP, Ruby, Swift, Kotlin, C/C++, Svelte configs

**internal/extraction/**:
-  –  (lazy  per language),
  , , extension → Language map
-  –  (tree traversal), , helper types
-  – , ,
-  –  (file scanning, parallel indexing,
  hash-based change detection), ,

### Test coverage (extraction_test.go)
- TestDetectLanguage / TestDetectLanguageCppHeuristic
- TestIsLanguageSupported
- TestExtractTypeScript / TestExtractTypeScriptAsync
- TestExtractPython / TestExtractGo / TestExtractRust / TestExtractJava
- TestExtractEdges
- TestHashContent / TestScanFiles
- TestParallelIndexing / TestHashChangeDetection
- TestUnresolvedRefs

Co-authored-by: Copilot <[email protected]>

Co-authored-by: kristofer <[email protected]>
- Document 16-char SHA-256 prefix to explain parity with TypeScript impl
- Fix channel buffer sizes (no spurious +1)
- Propagate DB upsert errors into IndexResult.Errors instead of silently ignoring them

Co-authored-by: Copilot <[email protected]>

Co-authored-by: kristofer <[email protected]>
- Add parserPoolMaxSize constant (was magic number 32)
- Fix redundant []rune double-conversion in isExported check
- Add FilesOversized to IndexResult to distinguish size-skipped vs hash-skipped
- Use fileOutcome named type instead of anonymous struct in processFile
- Handle filepath.Match error explicitly in matchesGlob

Co-authored-by: Copilot <[email protected]>

Co-authored-by: kristofer <[email protected]>
Copilot AI changed the title [WIP] Implement phase 4 of the AST extraction engine Go Port Phase 4: Native tree-sitter AST extraction engine Apr 27, 2026
Copilot AI requested a review from kristofer April 27, 2026 19:41
@kristofer

Copy link
Copy Markdown
Owner

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Apr 27, 2026

Copy link
Copy Markdown
Author

@copilot resolve the merge conflicts in this pull request

Merge conflicts resolved in commit 7892e30. The only conflicts were in go.mod and go.sum — Phase 3 had added modernc.org/sqlite while Phase 4 added github.com/smacker/go-tree-sitter. Both dependencies are now present and all tests pass (including the new internal/db tests from Phase 3).

@kristofer
kristofer marked this pull request as ready for review April 28, 2026 12:16
@kristofer
kristofer merged commit a2f571a into main Apr 28, 2026
2 of 14 checks passed
@kristofer
kristofer deleted the copilot/implement-phase-4-ast-extraction-engine branch April 28, 2026 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement phase 4 of the GO_PORT_PLAN.md

2 participants