engine

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 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 engine hosts the squoze optimization pipeline.

v2 pipeline: detect wire format → walk message structures with targeted JSON-path edits (gjson/sjson) → squeeze only provably-safe text (machine output classified by the router) → report savings. Untouched bytes stay byte-identical: sjson edits in place instead of re-marshalling maps, which is what keeps provider prompt caches stable.

Contracts: fail-open (unknown/invalid input passes through), never-elide (error lines survive), savings floor, idempotent markers.

Index

Constants

View Source
const DefaultMemoCapacity = 4096

DefaultMemoCapacity bounds pinned decisions per process (~4k blobs).

View Source
const (
	SkipBodyTooLarge = "body_too_large"
)

Skip reasons reported in Result.SkipReason. They are stable strings: 2papi forwards them verbatim in the X-Gateway-Squoze-Skip response header, so renaming one is a breaking change for anything parsing that header.

View Source
const Version = "0.4.0"

Version is reported by `squoze version` and stamped into response headers.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

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

Engine is the pipeline with its cache-guard state attached.

func Default

func Default() *Engine

Default returns the package-level shared engine.

func NewEngine

func NewEngine(memoCapacity int) *Engine

NewEngine returns an isolated pipeline instance with memory-only originals.

func NewEngineWith

func NewEngineWith(memoCapacity int, orig *store.Originals) *Engine

NewEngineWith attaches an external originals store (e.g. persisted).

func NewEngineWithLimits added in v0.4.0

func NewEngineWithLimits(memoCapacity int, lim Limits) *Engine

NewEngineWithLimits returns an isolated instance bounded by lim. The zero Limits is v0.3.0 behaviour, so NewEngine(cap) and NewEngineWithLimits(cap, Limits{}) are interchangeable.

func (*Engine) Apply

func (e *Engine) Apply(body []byte) ([]byte, Result)

Apply runs the full pipeline: detect → route → squeeze (profile- and memo-aware) → report. Unknown or invalid input passes through byte-for-byte (fail-open).

func (*Engine) Limits added in v0.4.0

func (e *Engine) Limits() Limits

Limits reports the bounds this Engine was built with. The zero value means unbounded, which is what NewEngine produces.

func (*Engine) ResolveOriginal added in v0.2.0

func (e *Engine) ResolveOriginal(ref string) ([]byte, error)

ResolveOriginal recovers the un-elided original text by its reference hash.

type Limits added in v0.4.0

type Limits struct {
	// MaxBodyBytes skips the whole request, without so much as sniffing its
	// wire format, once the body exceeds this many bytes. 0 means no cap.
	//
	// This is the only limit that reports itself: Result.Skipped is set and
	// Result.SkipReason says why, so a gateway can surface "this request was
	// too big to optimize" instead of a silent zero-savings pass.
	MaxBodyBytes int

	// MaxBlockBytes leaves an individual content block alone once it exceeds
	// this many bytes. 0 means no cap. Use it when a single pathological
	// tool_result should not be allowed to dominate a request's latency
	// budget while the rest of the messages still get optimized.
	MaxBlockBytes int

	// MinBlockBytes replaces the built-in 64-byte floor below which a block
	// is not worth examining. 0 keeps the built-in floor; a value below it
	// is ignored, because under 64 bytes no pass can produce savings that
	// survive the marker it would have to write.
	MinBlockBytes int
}

Limits bounds the work one Engine is willing to do on a request.

Every field is opt-in: the zero value is v0.3.0 behaviour, byte for byte. That is not a convention but a tested contract — TestZeroLimitsMatchV030 runs the corpus through NewEngine and NewEngineWithLimits(cap, Limits{}) and requires bytes.Equal, so an embedder that never mentions Limits cannot be affected by this type existing.

The bounds are sizes, never durations. A wall-clock deadline would make the output depend on how loaded the host was, and squoze's cache-safety contract is that identical original bytes always produce byte-identical output — a deadline would silently break every provider prompt cache downstream of it.

type Result

type Result struct {
	Format         wire.Format
	Family         string // model family the request was optimized for
	OriginalBytes  int
	SentBytes      int
	SavedBytes     int
	BlocksSqueezed int
	MemoHits       int // blocks served byte-identical from the decision memo
	Transforms     []string
	DurationMS     float64

	// Skipped is set when the request came back untouched without being
	// examined, which today means one thing: it was larger than
	// Limits.MaxBodyBytes. A request that was examined and simply had nothing
	// worth squeezing is not "skipped" — it reports SavedBytes 0 instead.
	Skipped    bool
	SkipReason string // one of the Skip* constants; "" unless Skipped
}

Result describes one processed request body.

func Process

func Process(body []byte) ([]byte, Result)

Process runs the default engine over a request body.

Jump to

Keyboard shortcuts

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