chunk

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package chunk defines the chunker contract for the document corpus and the line-window fallback chunker. Structural chunkers live in subpackages (chunk/markdown, chunk/gosrc).

Chunkers are pure functions of file bytes: no I/O, no network, no model. The corpus design depends on that -- the no-LLM-in-ingest pillar from docs/document-corpus.md is enforced by an import-graph test over this package tree, which is why nothing here may import the root memstore package (it carries an LLM client) or anything that reaches the network.

Index

Constants

View Source
const (
	// Target is what the splitter aims for when it must split.
	Target = 2000
	// Max is the hard ceiling above which a section or declaration must
	// split. A single atomic block (fence, table, statement) over Max becomes
	// an oversized chunk; half a code fence retrieves as noise, an oversized
	// chunk is merely inefficient.
	Max = 8000
	// Min is the floor below which a piece merges with its siblings.
	Min = 400
)

Sizing thresholds in non-whitespace characters (docs/document-chunking.md). Non-whitespace characters, not tokens: token counts would put a model dependency inside the ingest path and shift every boundary when the model changes. Not bytes: indentation would inflate measured size, severely so in nested code.

View Source
const LineWindowVersion = 1

LineWindowVersion identifies boundary-affecting behavior of the line-window chunker.

Variables

This section is empty.

Functions

func TrimTrailingWS

func TrimTrailingWS(content []byte, start, end int) int

TrimTrailingWS returns the end offset of span [start, end) with trailing whitespace removed. It never trims past start.

func Weight

func Weight(b []byte) int

Weight is the sizing measure: the number of non-whitespace characters (runes, not bytes) in b.

Types

type Chunk

type Chunk struct {
	Ordinal   int
	Content   string
	ByteStart int
	ByteEnd   int
	LineStart int
	LineEnd   int

	// Markdown-derived context.
	HeadingPath  string // ancestor headings only, never the chunk's own
	HeadingLevel int
	Lang         string // set on split-out fences

	// Code-derived context.
	Package     string
	ImportPath  string
	Symbol      string
	Receiver    string
	DeclKind    string // func | method | type | const | var | import | package_doc
	Exported    bool
	Signature   string
	ScopePath   string // package > receiver > symbol
	ImportsUsed []string
}

Chunk is one verbatim span of a file. Content must equal file[ByteStart:ByteEnd] exactly; the derived fields carry context for embed-time assembly and are never prepended to Content.

type Chunker

type Chunker interface {
	Chunk(path string, content []byte) (*Result, error)
	// Version identifies boundary-affecting behavior. Bump it on any change
	// that can move a cut; a differing version is grounds for re-ingest even
	// when the file hash matches.
	Version() int
	// Strategy names the chunker on the document row (e.g. "markdown", "go",
	// "line-window").
	Strategy() string
}

Chunker cuts one file's bytes into verbatim chunks.

path is provenance context only -- it may inform derived metadata (import path, package naming) but must never influence where boundaries fall: chunking must be a pure function of content for file_sha256 to be a sufficient staleness check.

type LineIndex

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

LineIndex maps byte offsets to 1-based line numbers for one file.

func NewLineIndex

func NewLineIndex(content []byte) *LineIndex

NewLineIndex builds the index for content.

func (*LineIndex) Line

func (ix *LineIndex) Line(off int) int

Line returns the 1-based line number containing byte offset off.

func (*LineIndex) LineStart

func (ix *LineIndex) LineStart(off int) int

LineStart returns the byte offset of the start of the line containing off.

type LineWindow

type LineWindow struct{}

LineWindow is the fallback chunker for plain text with no structure to cut on: non-markdown text files, and source files that fail to parse (docs/code-chunking.md). It packs whole lines into windows of roughly Target non-whitespace characters and, unlike the structural chunkers, keeps a small overlap between windows -- with no structural boundary to cut at, the overlap is what keeps a match near a window edge retrievable with its context.

func NewLineWindow

func NewLineWindow() *LineWindow

NewLineWindow returns the fallback chunker.

func (*LineWindow) Chunk

func (l *LineWindow) Chunk(_ string, content []byte) (*Result, error)

Chunk implements Chunker. path is unused: the fallback derives nothing.

func (*LineWindow) Strategy

func (l *LineWindow) Strategy() string

Strategy implements Chunker.

func (*LineWindow) Version

func (l *LineWindow) Version() int

Version implements Chunker.

type Result

type Result struct {
	Chunks      []Chunk
	Title       string          // from front matter when present
	FrontMatter json.RawMessage // parsed front matter as JSON; nil when absent
	Generated   bool            // file carries a "Code generated ... DO NOT EDIT." line
}

Result is one file's chunking output plus the document-level provenance the chunker derives from the bytes.

Directories

Path Synopsis
Package gosrc implements the Go source chunker from docs/code-chunking.md: cAST-style split-then-merge over the stdlib AST.
Package gosrc implements the Go source chunker from docs/code-chunking.md: cAST-style split-then-merge over the stdlib AST.
Package markdown implements the markdown chunker from docs/document-chunking.md: front matter parsed into document fields, sections segmented at headings, oversized sections split at top-level block boundaries, undersized sibling sections merged, fences and tables never split.
Package markdown implements the markdown chunker from docs/document-chunking.md: front matter parsed into document fields, sections segmented at headings, oversized sections split at top-level block boundaries, undersized sibling sections merged, fences and tables never split.

Jump to

Keyboard shortcuts

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