codebase

package
v0.0.0-...-87e9104 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatCoverageResponse

func FormatCoverageResponse(report *CoverageReport) string

FormatCoverageResponse formats the coverage report for MCP output.

func FormatSymbolDrift

func FormatSymbolDrift(drifts []SymbolDrift) string

FormatSymbolDrift renders drift report for display.

func IsExcludedDir

func IsExcludedDir(name string) bool

IsExcludedDir checks if a directory should be skipped during walking. Uses the IgnoreChecker if available, otherwise falls back to the dir name check.

func RenderRepoMap

func RenderRepoMap(rm *RepoMap, maxTokens int) string

RenderRepoMap formats the repo map for injection into the system prompt. Shows directory tree with exported symbols, kinds, and line counts. Files sorted by modification time (recent first) for relevance. Respects a token budget (approximate: 4 chars ≈ 1 token).

Types

type CCppLang

type CCppLang struct{}

CCppLang implements ModuleDetector and ImportParser for C/C++ projects.

func (*CCppLang) DetectModules

func (c *CCppLang) DetectModules(projectRoot string) ([]Module, error)

DetectModules discovers C/C++ modules by reading compile_commands.json, falling back to directory-based heuristics with Makefile/CMakeLists.txt markers.

func (*CCppLang) Extensions

func (c *CCppLang) Extensions() []string

func (*CCppLang) Language

func (c *CCppLang) Language() string

func (*CCppLang) ParseImports

func (c *CCppLang) ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

ParseImports extracts #include "..." edges from a C/C++ source file. System includes (#include <...>) are skipped since they're external.

type CoverageReport

type CoverageReport struct {
	TotalModules int
	CoveredCount int
	PartialCount int
	BlindCount   int
	Modules      []ModuleCoverage
}

CoverageReport is the full coverage report for a project.

func ComputeCoverage

func ComputeCoverage(ctx context.Context, db *sql.DB) (*CoverageReport, error)

ComputeCoverage calculates decision coverage for all modules. It joins codebase_modules with affected_files via path prefix matching.

type CoverageStatus

type CoverageStatus string

CoverageStatus represents how well a module is governed by decisions.

const (
	CoverageCovered CoverageStatus = "covered" // ≥1 active decision covers files in this module
	CoveragePartial CoverageStatus = "partial" // has decisions but they're stale or low R_eff
	CoverageBlind   CoverageStatus = "blind"   // no decisions reference files in this module
)

type FileSymbols

type FileSymbols struct {
	Path     string   // relative path from project root
	Language string   // "go", "python", "javascript", "typescript", "rust", "c", "cpp"
	Lines    int      // total line count
	Symbols  []Symbol // extracted symbols, sorted by line
	ModTime  int64    // modification time (unix nano) for recency sorting
}

FileSymbols holds symbols extracted from a single file.

type GoLang

type GoLang struct{}

GoLang implements ModuleDetector and ImportParser for Go projects.

func (*GoLang) DetectModules

func (g *GoLang) DetectModules(projectRoot string) ([]Module, error)

DetectModules discovers Go packages by walking directories for .go files. Each directory containing .go files (excluding _test.go-only dirs) is a module.

func (*GoLang) Extensions

func (g *GoLang) Extensions() []string

func (*GoLang) Language

func (g *GoLang) Language() string

func (*GoLang) ParseImports

func (g *GoLang) ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

ParseImports extracts import edges from a Go source file using go/parser.

type IgnoreChecker

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

IgnoreChecker determines if paths should be excluded from scanning. It respects .gitignore (local + global), .haftignore, and a minimal set of hardcoded dirs that should always be skipped (.git, .haft).

func GetIgnoreChecker

func GetIgnoreChecker(projectRoot string) *IgnoreChecker

GetIgnoreChecker returns a cached IgnoreChecker for the given project root.

func NewIgnoreChecker

func NewIgnoreChecker(projectRoot string) *IgnoreChecker

NewIgnoreChecker builds an IgnoreChecker for the given project root. Reads .gitignore, global git ignore files, and .haftignore.

func (*IgnoreChecker) IsIgnored

func (ic *IgnoreChecker) IsIgnored(relPath string) bool

IsIgnored returns true if the relative path should be excluded.

type ImportEdge

type ImportEdge struct {
	SourceModule string // module that imports
	TargetModule string // module being imported
	SourceFile   string // file containing the import
	ImportPath   string // raw import path as written in source
}

ImportEdge represents a dependency between two modules.

type ImportParser

type ImportParser interface {
	// ParseImports extracts import edges from a single source file.
	// Returns edges with raw import paths. Caller resolves to modules.
	ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

	// Extensions returns file extensions this parser handles.
	Extensions() []string
}

ImportParser extracts import/dependency edges from source files.

type JSTSLang

type JSTSLang struct{}

JSTSLang implements ModuleDetector and ImportParser for JavaScript/TypeScript.

func (*JSTSLang) DetectModules

func (j *JSTSLang) DetectModules(projectRoot string) ([]Module, error)

DetectModules discovers JS/TS packages by looking for package.json files. Handles monorepo workspaces.

func (*JSTSLang) Extensions

func (j *JSTSLang) Extensions() []string

func (*JSTSLang) Language

func (j *JSTSLang) Language() string

func (*JSTSLang) ParseImports

func (j *JSTSLang) ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

ParseImports extracts import/require edges from a JS/TS file.

type Module

type Module struct {
	ID        string // auto-generated from path, e.g., "mod-internal-auth"
	Path      string // relative path from project root
	Name      string // human-readable name, e.g., "auth"
	Lang      string // go, js, ts, python, rust, mixed, unknown
	FileCount int    // number of source files
}

Module represents a detected module/package in the codebase.

type ModuleCoverage

type ModuleCoverage struct {
	Module        Module
	Status        CoverageStatus
	DecisionCount int
	DecisionIDs   []string
}

ModuleCoverage describes the decision coverage for a single module.

type ModuleDetector

type ModuleDetector interface {
	// DetectModules walks the project and returns discovered modules.
	DetectModules(projectRoot string) ([]Module, error)

	// Language returns the language this detector handles.
	Language() string
}

ModuleDetector discovers module/package boundaries in a project.

type ModuleGovernanceGap

type ModuleGovernanceGap struct {
	Module Module
	Files  []string
}

ModuleGovernanceGap reports that a module touched by a new decision has no prior active decision coverage.

func FindFirstDecisionModules

func FindFirstDecisionModules(ctx context.Context, db *sql.DB, affectedFiles []string) ([]ModuleGovernanceGap, error)

FindFirstDecisionModules returns touched modules that currently have no active decision coverage. The caller can use this to warn that a decision is establishing the first explicit architectural context for a module.

type ModuleImpactInfo

type ModuleImpactInfo struct {
	ModuleID    string
	ModulePath  string
	DecisionIDs []string
	IsBlind     bool
}

ModuleImpactInfo describes a module affected by dependency propagation.

func EnrichDriftWithImpact

func EnrichDriftWithImpact(ctx context.Context, db *sql.DB, driftFiles []string) ([]ModuleImpactInfo, error)

EnrichDriftWithImpact adds dependency propagation to drift reports. For each drifted file, resolves to a module, finds dependents, and looks up their decisions.

type PythonLang

type PythonLang struct{}

PythonLang implements ModuleDetector and ImportParser for Python projects.

func (*PythonLang) DetectModules

func (p *PythonLang) DetectModules(projectRoot string) ([]Module, error)

DetectModules discovers Python packages by looking for __init__.py or pyproject.toml.

func (*PythonLang) Extensions

func (p *PythonLang) Extensions() []string

func (*PythonLang) Language

func (p *PythonLang) Language() string

func (*PythonLang) ParseImports

func (p *PythonLang) ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

ParseImports extracts import edges from a Python source file.

type Registry

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

Registry maps file extensions to language detectors and import parsers.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a registry with all supported languages.

func (*Registry) Detectors

func (r *Registry) Detectors() []ModuleDetector

Detectors returns all registered module detectors.

func (*Registry) ParserForFile

func (r *Registry) ParserForFile(path string) ImportParser

ParserForFile returns the import parser for a file, or nil if unsupported.

type RepoMap

type RepoMap struct {
	Files      []FileSymbols
	TotalFiles int
	TotalSyms  int
}

RepoMap is the complete symbol map for a repository.

func BuildRepoMap

func BuildRepoMap(projectRoot string, maxFiles int) (*RepoMap, error)

BuildRepoMap scans the project and extracts symbols from all supported files.

type RustLang

type RustLang struct{}

RustLang implements ModuleDetector and ImportParser for Rust projects.

func (*RustLang) DetectModules

func (r *RustLang) DetectModules(projectRoot string) ([]Module, error)

DetectModules discovers Rust crates/modules by looking for Cargo.toml and mod.rs/lib.rs.

func (*RustLang) Extensions

func (r *RustLang) Extensions() []string

func (*RustLang) Language

func (r *RustLang) Language() string

func (*RustLang) ParseImports

func (r *RustLang) ParseImports(filePath string, projectRoot string) ([]ImportEdge, error)

ParseImports extracts use/mod edges from a Rust source file.

type Scanner

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

Scanner detects modules and builds the dependency graph for a project.

func NewScanner

func NewScanner(db *sql.DB) *Scanner

NewScanner creates a new codebase scanner.

func (*Scanner) GetDependents

func (s *Scanner) GetDependents(ctx context.Context, moduleID string) ([]string, error)

GetDependents returns modules that depend on the given module (1-hop).

func (*Scanner) GetModules

func (s *Scanner) GetModules(ctx context.Context) ([]Module, error)

GetModules returns all stored modules.

func (*Scanner) ModulesLastScanned

func (s *Scanner) ModulesLastScanned(ctx context.Context) time.Time

ModulesLastScanned returns the time of the last module scan, or zero if never scanned.

func (*Scanner) ResolveFileToModule

func (s *Scanner) ResolveFileToModule(ctx context.Context, filePath string) (string, error)

ResolveFileToModule finds the most specific module for a file path (longest prefix match).

func (*Scanner) ScanDependencies

func (s *Scanner) ScanDependencies(ctx context.Context, projectRoot string) ([]ImportEdge, error)

ScanDependencies parses imports across all modules and builds the dependency graph.

func (*Scanner) ScanModules

func (s *Scanner) ScanModules(ctx context.Context, projectRoot string) ([]Module, error)

ScanModules detects all modules in the project and stores them in the DB. Respects .gitignore, global git ignore, and .haftignore.

type Symbol

type Symbol struct {
	Name     string // symbol name
	Kind     string // "func", "type", "interface", "class", "method", "const"
	Line     int    // 1-based line number
	Exported bool   // starts with uppercase (Go) or is exported
}

Symbol represents an extracted code symbol (function, type, class, etc.)

type SymbolDrift

type SymbolDrift struct {
	FilePath   string `json:"file_path"`
	SymbolName string `json:"symbol_name"`
	SymbolKind string `json:"symbol_kind"`
	Status     string `json:"status"` // "unchanged", "modified", "added", "removed"
	OldLine    int    `json:"old_line,omitempty"`
	NewLine    int    `json:"new_line,omitempty"`
}

SymbolDrift describes how a single symbol changed between baseline and current.

func CompareSymbolSnapshots

func CompareSymbolSnapshots(baseline []SymbolSnapshot, current []SymbolSnapshot) []SymbolDrift

CompareSymbolSnapshots compares baseline snapshots against current state.

type SymbolSnapshot

type SymbolSnapshot struct {
	FilePath   string `json:"file_path"`
	SymbolName string `json:"symbol_name"`
	SymbolKind string `json:"symbol_kind"` // func, type, class, interface, method
	Line       int    `json:"line"`        // 1-based start line
	EndLine    int    `json:"end_line"`    // 1-based end line
	Hash       string `json:"hash"`        // SHA256 of the symbol's source text
}

SymbolSnapshot captures a symbol's identity and content hash at a point in time.

func ExtractSymbolSnapshots

func ExtractSymbolSnapshots(projectRoot, relPath string) ([]SymbolSnapshot, error)

ExtractSymbolSnapshots extracts symbol-level hashes from a file using tree-sitter. Returns one snapshot per symbol, each with a content hash of the symbol's source text.

Jump to

Keyboard shortcuts

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