Help us shape the future of Go! Take the 2026 Go Developer Survey

headroom

package
v2.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package headroom reduces the token cost of voice search_memory tool results before they are injected into the Gemini Live context window. It is a native Go port of the transferable ideas in https://github.com/chopratejas/headroom — see docs/adr/038-headroom-token-compression.md.

Two mechanisms, both deterministic (no model call):

  1. LossyCompress — markdown-noise-aware truncation of a long Content body to a character budget, on a word boundary. The full body stays retrievable by ID (the existing item-fetch path is the reversible "fetch original" contract, so no separate cache is needed).

  2. Factor — hoist fields that are constant across every row out of the per-row JSON into a shared header, so they are paid for once.

Both are opt-in: with a zero/empty Config the input passes through unchanged.

Headroom's third mechanism — AdaptiveK (Kneedle/SimHash near-duplicate row collapse) — was ported, tested live, and removed: on the X-Ray corpus it omitted zero rows (cells are genuinely distinct), and dropping result rows risks hiding a voice mishear-disambiguation candidate. See ADR-038.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LossyCompress

func LossyCompress(body string, maxChars int) (string, bool)

LossyCompress lossily compresses a single content body: it strips token-wasteful markdown noise and truncates the result to maxChars on a word boundary. It is lossy by design — markdown structure is discarded and the tail is cut — so the result is a compact stand-in, not a faithful copy; the full original stays retrievable by id (the reversible CCR contract). It reports whether anything was dropped (by stripping past maxChars, or by the truncation itself). maxChars <= 0 returns body unchanged.

Stripping is conservative — it removes structure that costs tokens but carries little meaning the model needs from a search snippet:

  • image embeds ![alt](url) → alt text only
  • inline links [text](url) → text only
  • fenced code ```lang\n…\n``` → «code» placeholder
  • heading/quote/list markers at line start
  • collapsed runs of whitespace

It deliberately does NOT touch inline emphasis or the words themselves.

Types

type Config

type Config struct {
	// MaxContentChars caps each row's Content after markdown-noise stripping.
	// 0 disables the LossyCompress step (content passes through whole).
	MaxContentChars int
}

Config tunes compression. The zero value is a no-op (CompressRows returns the input rows unchanged), so callers can gate the feature with a single flag.

type Result

type Result struct {
	Rows []Row
	// Constant holds fields whose value was identical across every row. Keys
	// are a subset of {"kind","scope"}. Empty when nothing was uniform. A
	// factored field is blanked on each Row so the caller's serializer omits it
	// (the value lives once in Constant instead).
	Constant map[string]string
}

Result is the output of CompressRows: the (possibly compressed) rows, plus the fields that were constant across all of them and so factored out.

func CompressRows

func CompressRows(rows []Row, cfg Config) Result

CompressRows lossy-compresses each row's Content to the budget, then factors uniform fields. With a zero Config it returns the rows unchanged and an empty Result.Constant.

type Row

type Row struct {
	ID        string
	Kind      string
	Scope     string
	Title     string
	Content   string
	Truncated bool
}

Row is the compressible projection of a memory item. Callers build these from MemoryItem, run CompressRows, then serialize. Content is replaced in place with its lossy-compressed form; Truncated records whether the body was cut (the model reads this as "more is available via ID").

Jump to

Keyboard shortcuts

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