repomap

package
v1.10.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 5, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package repomap provides tree-sitter-based tag extraction for building repository maps with ranked symbol importance.

Index

Constants

View Source
const (
	LookupHit  = "hit"
	LookupMiss = "miss"
)

Cache lookup result constants — closed enum per Phase 53 D-04.

View Source
const (
	ExtractorTreesitter = "treesitter"
	ExtractorLSP        = "lsp"
	ExtractorFallback   = "fallback"
)

Extractor type constants — closed enum per Phase 53 D-07.

Variables

This section is empty.

Functions

func ElideSingle

func ElideSingle(source []byte, tag Tag, bodyStart, bodyEnd uint) string

ElideSingle takes pre-computed body range and returns the elided text for a single tag. Useful for rendering individual tags without re-parsing.

func EstimateTokens

func EstimateTokens(s string) int

EstimateTokens approximates token count as len(s)/4. Per D-12: character-based token estimation, no external dependency.

func LangFromExt

func LangFromExt(path string) string

LangFromExt maps file extensions to language identifiers used by tree-sitter.

Types

type ElisionRenderer

type ElisionRenderer struct {
	// contains filtered or unexported fields
}

ElisionRenderer produces compact, token-efficient views of source files by showing definition signatures with bodies replaced by an ellipsis marker. Per D-13: full signature + ellipsis. Per D-14: struct/class fields shown. Per D-15: elision at render time using byte ranges from tags.

func NewElisionRenderer

func NewElisionRenderer(registry *treesitter.GrammarRegistry) *ElisionRenderer

NewElisionRenderer creates an ElisionRenderer using the shared grammar registry.

func (*ElisionRenderer) RenderFile

func (r *ElisionRenderer) RenderFile(source []byte, lang string, tags []Tag) string

RenderFile produces a compact elided view of the def tags in a file. Only def tags are rendered; ref tags are ignored. Bodies of functions/methods are replaced with an ellipsis marker. Struct/class fields are preserved (D-14).

type FallbackExtractor

type FallbackExtractor struct{}

FallbackExtractor produces def-only tags from LSP documentSymbol responses for languages without tree-sitter grammars. Per D-07, all symbols are mapped as TagDef; no references are extracted via this path.

func NewFallbackExtractor

func NewFallbackExtractor() *FallbackExtractor

NewFallbackExtractor creates a new FallbackExtractor.

func (*FallbackExtractor) Extract

func (f *FallbackExtractor) Extract(ctx context.Context, requester SymbolRequester, filePath string, uri string) ([]Tag, error)

Extract calls textDocument/documentSymbol via the given requester and returns def-only tags. Nested DocumentSymbol children produce qualified names (e.g. "ClassName.methodName") per D-03.

type FileGraph

type FileGraph struct {
	// Edges maps source file -> target file -> weight.
	Edges map[string]map[string]float64
	// Files is the set of all file nodes in the graph.
	Files map[string]bool
	// contains filtered or unexported fields
}

FileGraph is an in-memory directed graph where nodes are file paths and edges represent cross-file ref-to-def relationships weighted by reference count. Per D-02: in-memory only, no persistence of edges.

func BuildGraph

func BuildGraph(cache *TagCache) (*FileGraph, error)

BuildGraph constructs a FileGraph from all cached tags. Per D-01: file-level graph with cross-file ref-to-def edges. Per D-03: edge weight = sqrt(reference count).

func NewFileGraph

func NewFileGraph() *FileGraph

NewFileGraph creates an empty FileGraph with initialized maps.

func (*FileGraph) EdgeCount

func (g *FileGraph) EdgeCount() int

EdgeCount returns the total number of edges across all source nodes.

func (*FileGraph) EnrichFromLSP

func (g *FileGraph) EnrichFromLSP(sourceFile string, refs []Location)

EnrichFromLSP adds cross-file reference edges from LSP references. Called opportunistically when a WorkerLease is already active. Per D-10: opportunistic only. Per D-11: additive edges.

func (*FileGraph) NodeCount

func (g *FileGraph) NodeCount() int

NodeCount returns the number of file nodes in the graph.

func (*FileGraph) PageRank

func (g *FileGraph) PageRank(damping float64, epsilon float64, maxIter int, personalization map[string]float64) map[string]float64

PageRank computes PageRank scores for all files in the graph using power iteration. If personalization is non-nil and non-empty, it is used as the teleportation vector (Personalized PageRank); otherwise uniform teleportation is used.

Parameters:

  • damping: probability of following a link (typically 0.85)
  • epsilon: convergence threshold (sum of abs rank differences)
  • maxIter: maximum number of iterations
  • personalization: optional map of file -> teleport weight

Per T-28-02: maxIter caps computation to mitigate DoS on large graphs.

func (*FileGraph) RankFiles

func (g *FileGraph) RankFiles(damping float64, personalization map[string]float64) []RankedFile

RankFiles runs PageRank and returns files sorted descending by score. Convenience wrapper with default epsilon=1e-6 and maxIter=100.

type Location

type Location struct {
	URI  string
	Line int
}

Location is a minimal reference location used by EnrichFromLSP. It avoids importing the gen package into the repomap package.

type MetricsSink

type MetricsSink interface {
	// RepoMapLookup increments helix_repomap_lookups_total for a
	// (language, result) pair. result MUST be one of the LookupXxx
	// constants below — closed enum per Phase 53 D-04.
	RepoMapLookup(language, result string)

	// RepoMapExtractObserve records the elapsed seconds of a single
	// extractor invocation (cache-miss path only). extractor MUST be one
	// of the ExtractorXxx constants below — closed enum per Phase 53 D-07.
	RepoMapExtractObserve(language, extractor string, seconds float64)
}

MetricsSink is the minimal surface repomap needs from the observability layer. Implemented ad-hoc by *obs.Metrics (compile-time-checked at wire-up in internal/daemon/wiring_test.go); repomap itself never imports internal/obs (Phase 53 D-15).

Method signatures are frozen to match the *obs.Metrics helpers declared in internal/obs/metrics.go (Phase 53 D-15). If those helper signatures drift, the wiring_test.go compile-time assertion fails — fix is to ALIGN this interface, NOT to mutate internal/obs/metrics.go.

type NoopSink

type NoopSink struct{}

NoopSink is used by tests and bootstrap paths where metrics are not wired. All methods are lock-free no-ops; a value-receiver keeps them trivially inlinable.

func (NoopSink) RepoMapExtractObserve

func (NoopSink) RepoMapExtractObserve(string, string, float64)

RepoMapExtractObserve implements MetricsSink.

func (NoopSink) RepoMapLookup

func (NoopSink) RepoMapLookup(string, string)

RepoMapLookup implements MetricsSink.

type RankedFile

type RankedFile struct {
	Path  string
	Score float64
}

RankedFile pairs a file path with its PageRank score. Used by the rendering layer to produce token-budgeted output.

type SymbolRequester

type SymbolRequester interface {
	Request(ctx context.Context, method string, params, result interface{}) error
}

SymbolRequester abstracts the LSP documentSymbol request for testability. WorkerLease from lspool satisfies this interface via its Request method.

type Tag

type Tag struct {
	Name      string
	Kind      TagKind
	File      string
	Line      int
	Column    int
	StartByte uint
	EndByte   uint
}

Tag represents a single definition or reference extracted from a source file. Per D-01: two kinds only (def/ref). Per D-02: flat, no scope nesting. Per D-04: byte offsets only, no raw text stored.

type TagCache

type TagCache struct {
	// contains filtered or unexported fields
}

TagCache provides SQLite-backed persistence for extracted tags with mtime-based invalidation. Tags survive daemon restarts and client reconnects. The cache uses a separate tags.db file independent from the memory store (per D-09).

func NewTagCache

func NewTagCache(dbPath string) (*TagCache, error)

NewTagCache opens (or creates) the SQLite tag cache at dbPath, initializes the schema, and configures WAL mode with a busy timeout. Follows the same pattern as internal/memory/index.go.

func (*TagCache) AllFiles

func (c *TagCache) AllFiles() (map[string][]Tag, error)

AllFiles returns all cached file paths with their tags. Used by graph building to iterate the entire tag cache.

func (*TagCache) Clear

func (c *TagCache) Clear() error

Clear removes all cached tags for all files.

func (*TagCache) Close

func (c *TagCache) Close() error

Close closes the underlying database connection.

func (*TagCache) GetOrExtract

func (c *TagCache) GetOrExtract(filePath string, extractFn func() ([]Tag, error)) ([]Tag, error)

GetOrExtract returns cached tags for filePath if the file's mtime has not changed. On cache miss or mtime mismatch, it calls extractFn to get fresh tags, stores them, and returns them.

Per D-10: file-level mtime invalidation. Per D-12: lazy (no eager warming). Per T-27-05: all SQL uses parameterized queries only.

func (*TagCache) InvalidateFile

func (c *TagCache) InvalidateFile(filePath string) error

InvalidateFile removes all cached tags for the given file.

func (*TagCache) SetMetricsSink

func (c *TagCache) SetMetricsSink(sink MetricsSink)

SetMetricsSink wires the MetricsSink at startup (called from internal/daemon/daemon.go post-init wiring; see Phase 53 D-15). Safe to call multiple times; overwrites the previous sink. A nil argument is normalized to NoopSink{} so the GetOrExtract emission sites never need nil-checks.

Decision (Plan 53-03 setter pattern): adding the sink as a setter rather than a NewTagCache constructor argument avoids touching every existing NewTagCache caller (skill repomap, tests, future seams). Mirrors the existing post-init wiring pattern at daemon.go:290-323 (12a/12b/12c).

func (*TagCache) Version

func (c *TagCache) Version() int64

Version returns a monotonically increasing counter that increments whenever the cache contents change (store, invalidate, clear). Used by the graph builder to detect cache changes and avoid unnecessary rebuilds.

type TagExtractor

type TagExtractor struct {
	// contains filtered or unexported fields
}

TagExtractor extracts def/ref tags from source files using tree-sitter queries. Queries are compiled once per language and reused across files.

func NewTagExtractor

func NewTagExtractor(registry *treesitter.GrammarRegistry) (*TagExtractor, error)

NewTagExtractor creates a TagExtractor with compiled queries for all supported languages. Returns an error if any query fails to compile.

func (*TagExtractor) Close

func (e *TagExtractor) Close()

Close releases all compiled queries.

func (*TagExtractor) Extract

func (e *TagExtractor) Extract(source []byte, filePath string, lang string) ([]Tag, error)

Extract parses the given source and returns all def/ref tags for the specified language.

type TagKind

type TagKind string

TagKind distinguishes definition tags from reference tags.

const (
	// TagDef marks a symbol definition (function, type, class, etc.).
	TagDef TagKind = "def"
	// TagRef marks a symbol reference (call site, type usage, etc.).
	TagRef TagKind = "ref"
)

type TreeRenderer

type TreeRenderer struct {
	// contains filtered or unexported fields
}

TreeRenderer produces tree-structured, token-budgeted views of ranked files with elided symbol definitions. It combines the PageRank ranking from FileGraph with the ElisionRenderer from Phase 27 to produce compact repo maps.

func NewTreeRenderer

func NewTreeRenderer(elider *ElisionRenderer, cache *TagCache, rootDir string) *TreeRenderer

NewTreeRenderer creates a TreeRenderer for the given workspace root.

func (*TreeRenderer) RenderBudgeted

func (r *TreeRenderer) RenderBudgeted(ranked []RankedFile, budget int) string

RenderBudgeted renders ranked files as a tree, using binary search on file count to maximize coverage within the token budget. Per D-13: prune lowest-ranked files first via binary search. At least 1 file is always included regardless of budget.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL