Documentation
¶
Overview ¶
Package microcompact implements time-based micro-compaction: when the gap since the last assistant message exceeds a threshold (cache TTL), replace older tool_result content with a placeholder before sending the next request. The cache has expired anyway, so clearing tool results shrinks what gets re-cached without changing functional context — recent tool_results are kept intact.
Mirrors src/services/compact/microCompact.ts maybeTimeBasedMicrocompact. CC's cache-editing path (cache_edits API) is Anthropic-internal beta and not implemented here — time-based is the public-build version.
Index ¶
Constants ¶
const ClearedMessage = "[Old tool result content cleared]"
ClearedMessage is the placeholder substituted for cleared tool_result text.
const DefaultKeepRecent = 5
DefaultKeepRecent matches CC's keepRecent default. The model retains enough recent tool context to keep working while older noisy results (long file reads, big greps) shrink to the placeholder.
const DefaultThreshold = 60 * time.Minute
DefaultThreshold matches CC's gapThresholdMinutes default. The server's prompt cache has a 1h TTL — past that, the prefix would be re-tokenized regardless, so clearing is free.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Result ¶
type Result struct {
Messages []api.Message
TokensSaved int
Cleared int // number of tool_results replaced
Kept int // number of tool_results kept intact
Triggered bool
}
Result describes what changed.
func Apply ¶
func Apply(messages []api.Message, lastAssistantTime time.Time, threshold time.Duration, keepRecent int) Result
Apply runs time-based microcompact on messages. lastAssistantTime is the timestamp of the most recent assistant message; if zero, Apply is a no-op (no history yet). Returns the original slice unchanged when the gap is below threshold or there's nothing eligible to clear.