autocompact

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package autocompact installs pi-go's two-stage context compaction as a pre-turn hook: shed superseded tool results at the lower threshold, summarize at the upper one.

It lives outside internal/cli so that every agent entry point can install the same hook — the interactive TUI, the one-shot CLI, the ACP server and the piagent SDK. A long agent session re-sends its whole transcript on every turn, so an entry point without compaction pays that growth in full.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildHook

func BuildHook(deps Deps) agent.PreTurnHook

BuildHook returns a pre-turn hook that runs the two-stage compaction. It returns nil when compaction cannot run — no session service, no token tracker, or disabled by config — so a caller can install the result unconditionally. It reads the body size — context after the stable cached prefix — from the token tracker, so a large but fully cached system prompt and tool block never trigger compaction on their own.

The hook never aborts a turn on compaction failure: being unable to reclaim context is a degraded state, not a reason to refuse the user's request. The error is reported and the turn proceeds.

func ConfigFrom

func ConfigFrom(cfg config.Config) pisession.AutoCompactConfig

ConfigFrom maps the user's config.json settings onto an AutoCompactConfig, leaving any field the user did not set at its default. Every numeric field is guarded by its own zero-check, so an unset field keeps its default rather than compacting at 0%.

func MeterCallback

func MeterCallback(m *Meter) llmagent.AfterModelCallback

MeterCallback returns an ADK after-model callback that feeds m from each response's usage metadata. It never rewrites the response.

A streaming turn yields many responses and the callback sees each one; the last prompt count wins, which is the one describing the whole request.

Types

type ContextMeter

type ContextMeter interface {
	BodyTokens() int64
	ContextWindowSize() int64
	SetLastPromptTokens(int64)
}

ContextMeter reports how much of the model's context window the live transcript occupies. *guardrail.Tracker satisfies it, and so does Meter; the interface is what keeps this package off internal/guardrail, which piagent may not reach even transitively.

BodyTokens is the figure the thresholds are measured against: tokens after the stable cached prefix, so a large but fully-cached system prompt never pushes a session toward compaction on its own.

type Deps

type Deps struct {
	SessionSvc *pisession.FileService
	Tracker    ContextMeter
	Deduper    *tools.ResultDeduper
	Cfg        pisession.AutoCompactConfig
	Log        *logger.Logger

	// SummarizerLLM writes the handoff summary. Nil falls back to a degraded
	// placeholder that names the files touched — lossy, but it still reclaims
	// the context rather than leaving the session stuck over budget.
	SummarizerLLM llmmodel.LLM

	// Notify surfaces the outcome to the user. Compaction silently discarding
	// history is exactly the kind of thing that should not be silent.
	Notify func(string)
}

Deps are the pieces a pre-turn compaction pass needs.

type Meter

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

Meter is a standalone ContextMeter for callers that have no guardrail tracker to read — piagent, which is barred from importing internal/guardrail and in any case wants none of the daily-budget half of it.

It measures and never enforces: nothing here can refuse a request. Feed it with MeterCallback.

func NewMeter

func NewMeter() *Meter

NewMeter returns a meter with an unknown context window. Compaction stays off until Meter.SetContextWindowSize is given a positive size, which is the same thing an unresolvable window means everywhere else.

func (*Meter) BodyTokens

func (m *Meter) BodyTokens() int64

BodyTokens reports the tokens accumulated after the stable cached prefix.

func (*Meter) ContextWindowSize

func (m *Meter) ContextWindowSize() int64

ContextWindowSize reports the model's context window, or 0 if unknown.

func (*Meter) Observe

func (m *Meter) Observe(promptTokens int64)

Observe records the prompt-token count of one LLM response. promptTokens is inclusive of cache reads, as every provider reports it.

func (*Meter) SetContextWindowSize

func (m *Meter) SetContextWindowSize(size int64)

SetContextWindowSize records the model's context window, in tokens.

func (*Meter) SetLastPromptTokens

func (m *Meter) SetLastPromptTokens(n int64)

SetLastPromptTokens overwrites the prompt-token baseline, and clears the prefix baseline with it. A compaction pass calls this because it knows the post-pass count exactly; the rewritten window has a new stable prefix by definition, so the old one no longer applies.

A non-positive n clears the prefix without setting a new count.

Jump to

Keyboard shortcuts

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