convention

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package convention computes per-package AST statistics that describe the package's prevailing idioms — error handling style, logging library, naming patterns, concurrency primitives. CKV exposes these raw stats so the agent (or a SKILL) can read what conventions a package follows before proposing edits.

CKV deliberately does no interpretation here: stats are numbers and counts. Summarization belongs to the agent's SKILL, where the LLM can phrase them in context-appropriate prose.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Aggregator

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

Aggregator accumulates per-file stats then resolves into a per-package map.

func NewAggregator

func NewAggregator() *Aggregator

NewAggregator creates an empty accumulator. Safe to use without initialization via the zero value as long as ObservePackage is called before Result.

func (*Aggregator) ObserveFile

func (a *Aggregator) ObserveFile(file string, src []byte) error

ObserveFile parses src and folds its stats into the aggregator. file is the repo-relative path (used to derive the package key as filepath.Dir and to detect *_test.go).

func (*Aggregator) Result

func (a *Aggregator) Result() map[string]Stats

Result returns the per-package stats. Keys are package paths (filepath.Dir of the first observed file in that package). The map is fresh; subsequent ObserveFile calls do not mutate it.

type Stats

type Stats struct {
	// Errors counts the error-construction style:
	//   "fmt.Errorf_wrap"   — fmt.Errorf with a %w verb
	//   "fmt.Errorf_plain"  — fmt.Errorf without %w
	//   "errors.New"        — errors.New
	//   "pkg/errors.Wrap"   — github.com/pkg/errors.Wrap (legacy)
	Errors map[string]int

	// Loggers counts well-known logger call sites:
	//   "log15", "zap", "slog", "stdlib_log"
	Loggers map[string]int

	// Receivers counts struct receiver short-name length distribution.
	// Keys are the short names ("s", "srv", "store", ...).
	Receivers map[string]int

	// NewConstructors counts the number of top-level `func New*` and
	// `func MustNew*` constructors (idiom for builder vs zero-value).
	NewConstructors int

	// TestFiles counts *_test.go files in the package.
	TestFiles int

	// TableDriven counts `for _, tc := range cases` / `for _, tt`
	// patterns — the conventional table-driven test idiom.
	TableDriven int

	// TestifyUses counts imports of stretchr/testify.
	TestifyUses int

	// Mutexes counts sync.Mutex / sync.RWMutex declarations.
	Mutexes int

	// Channels counts make(chan ...) call sites.
	Channels int

	// ErrGroupUses counts uses of golang.org/x/sync/errgroup.
	ErrGroupUses int

	// FileCount is the total number of files contributing to these
	// stats; useful for normalizing other counters.
	FileCount int
}

Stats is the per-package statistic bundle. The map shape uses `any` so adding a new metric in v2 does not break consumers (the agent's SKILL filters keys it understands and ignores the rest).

func (Stats) Summary

func (s Stats) Summary(pkg string) string

Summary is a short, deterministic prose summary suitable as embed text for a ChunkConvention chunk. Deterministic order matters because chunk_id is content-hashed.

func (Stats) ToMap

func (s Stats) ToMap() map[string]any

ToMap returns Stats as a generic map. Suitable for JSON round-trip and for the ConventionStats column on the chunk.

Jump to

Keyboard shortcuts

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