truncate

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package truncate holds the line and byte walks of Pi's truncateHead and truncateTail (packages/coding-agent/src/core/tools/truncate.ts), shared by the tools' result truncation, the bash-execution block's context limit and durable's truncateHead, which import cycles keep in different packages.

Both walks cut once. truncate.ts splits its input into lines and joins the kept ones with "\n", but the lines it keeps are contiguous, so the joined text is a substring of the input: the head ends where its last kept line ends, the tail starts where its first kept line starts and ends before a trailing newline. The cuts find those offsets by scanning newlines from one end, read at most maxBytes+1 bytes of the input, and return substrings, so a result allocates nothing.

Index

Constants

View Source
const (
	DefaultMaxBytes = 50 * 1024 // upstream: packages/coding-agent/src/core/tools/truncate.ts:DEFAULT_MAX_BYTES
	DefaultMaxLines = 2_000     // upstream: packages/coding-agent/src/core/tools/truncate.ts:DEFAULT_MAX_LINES
)

The default limits of truncateHead and truncateTail.

Variables

This section is empty.

Functions

func CountLines

func CountLines(content string) int

CountLines is len(splitLinesForCounting(content)) (truncate.ts): the "\n"-separated lines, a trailing newline ending the last line rather than starting an empty one.

func FirstLineBytes

func FirstLineBytes(content string) int

FirstLineBytes is the byte length of content's first counted line (truncateHead's lines[0]), 0 for empty content.

Types

type HeadCut

type HeadCut struct {
	End, Lines int
	ByBytes    bool
}

HeadCut is truncateHead's walk over content's lines: it keeps whole lines from the start while they fit in maxLines lines and maxBytes bytes, counting a joining newline before every line but the first. The kept text is content[:End]. ByBytes reports that a line's bytes stopped the walk.

func CutHead

func CutHead(content string, maxBytes, maxLines int) HeadCut

CutHead walks content as truncateHead does; see HeadCut.

type Result

type Result struct {
	Content               string
	Truncated             bool
	TruncatedBy           string // "lines" | "bytes" | ""
	TotalLines            int
	TotalBytes            int
	OutputLines           int
	OutputBytes           int
	LastLinePartial       bool
	FirstLineExceedsLimit bool
}

Result is truncateHead's or truncateTail's result: the kept content and what was cut.

func TruncateHead

func TruncateHead(content string, maxBytes, maxLines int) Result

TruncateHead keeps the first maxLines lines and maxBytes bytes of content, whichever limit is hit first, and never a partial line: when the first line alone exceeds maxBytes the content is empty and FirstLineExceedsLimit is set (truncate.ts truncateHead).

func TruncateTail

func TruncateTail(content string, maxBytes, maxLines int) Result

TruncateTail keeps the last maxLines lines and maxBytes bytes of content, whichever limit is hit first (truncate.ts truncateTail). When the last line alone exceeds maxBytes, its end is kept and LastLinePartial is set.

func TruncateTailBytes

func TruncateTailBytes(content []byte, maxBytes, maxLines int) Result

TruncateTailBytes is TruncateTail(string(content)), copying only the kept text.

type TailCut

type TailCut struct {
	Start, End, Lines int
	ByBytes, Partial  bool
}

TailCut is truncateTail's walk over content's lines: it keeps whole lines from the end while they fit in maxLines lines and maxBytes bytes. When the last line alone exceeds maxBytes its end is kept, cut forward to a UTF-8 character boundary, and Partial is set. The kept text is content[Start:End]; End excludes a trailing newline. ByBytes reports that a line's bytes stopped the walk.

func CutTail

func CutTail[T ~string | ~[]byte](content T, maxBytes, maxLines int) TailCut

CutTail walks content as truncateTail does; see TailCut. It reads only the bytes it keeps plus the one before them, so it takes the accumulators' []byte tails as well as strings.

type TailWindow

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

TailWindow is TruncateTail of a stream of chunks without keeping the stream: Append folds a chunk in and Result equals TruncateTail of everything appended so far. TruncateTail's walk reads only the last maxBytes+1 bytes of its input, so the window keeps maxBytes+2 bytes (a trailing newline is not a counted line, so a window of maxBytes+1 could let a cut first line fit by exactly the byte the newline hides) and the totals are counted as the chunks arrive. A block that re-truncates its whole output on every frame costs the output length per frame; this costs the window.

The kept bytes are the last keep bytes of buf. buf grows to twice keep before it drops what is older, so a chunk costs its own length: the bytes the window keeps move once per keep bytes appended, not on every chunk.

func NewTailWindow

func NewTailWindow(maxBytes, maxLines int) *TailWindow

NewTailWindow returns an empty window for TruncateTail's limits.

func (*TailWindow) Append

func (w *TailWindow) Append(chunk string)

Append adds a chunk to the end of the content.

func (*TailWindow) Reset

func (w *TailWindow) Reset(content string)

Reset replaces the content with content.

func (*TailWindow) Result

func (w *TailWindow) Result() Result

Result is TruncateTail of the whole content. It copies only the kept text.

Jump to

Keyboard shortcuts

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