{"modulePath":"github.com/memcode-ai/memcode","version":"v0.56.16","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package compaction\n\nPackage compaction is the PURE, testable core of in-session context compaction: deciding WHERE to cut a running conversation and rendering the cut-off part into a plain transcript for an Anthropic summarizer. It owns no model call and no I/O — the runtime orchestrates those — so the load-bearing invariant (never split a tool\\_use from its tool\\_result) can be proven in isolation.\n\nThe doctrine (three layers — hot raw turns / warm summary / cold session log) lives in COMPACTION.md. This package implements the warm layer's mechanics.\n\n## Constants\n\n```go\nconst OmittedToolResult = \"[earlier tool output omitted to keep this turn within the context window]\"\n```\nOmittedToolResult is the LEGACY blank placeholder; offloadRef now writes a TYPED pointer back to the source instead (below). Kept so isOffloaded still recognizes results elided by older builds.\n\n## Functions\n\n```go\nfunc CountTurns(messages []wire.Message) int\n```\nCountTurns reports how many user-turn boundaries a message slice contains — used for the \"summarized N earlier turns\" telemetry and status line.\n\n```go\nfunc EstimateTokens(messages []wire.Message) int\n```\nEstimateTokens approximates the prompt size of a message slice the way the gateway's router does: ~4 chars/token over all text and tool-result content.\n\n```go\nfunc EvictStaleToolResults(messages []wire.Message, keepRecent int) int\n```\nEvictStaleToolResults offloads the payload of OLD tool\\_result blocks — replacing each with a TYPED pointer (offloadRef) back to its source — so a long turn stops re-sending stale grep/read/test dumps every iteration. It is RELEVANCE-FIRST, not purely age-based:\n\n  - a SUPERSEDED read (an earlier read of a file that's read AGAIN later) is offloaded eagerly, even if recent — the newer copy is authoritative, so the older is dead weight; and\n  - among what's left, the most recent keepRecent stay verbatim (the active working set).\n\nA file read's pointer is lossless (the file is on disk; re-read to restore); other outputs name how to re-fetch. It NEVER removes a block — only swaps payload for a pointer — so tool\\_use↔ tool\\_result adjacency holds. Returns how many it offloaded.\n\n```go\nfunc EvictStaleToolResultsOpts(messages []wire.Message, keepRecent int, opts EvictOpts) int\n```\nEvictStaleToolResultsOpts is EvictStaleToolResults with options (hot-path pinning).\n\n```go\nfunc Plan(messages []wire.Message, keepRecent int) (head, tail []wire.Message, ok bool)\n```\nPlan splits a conversation into a HEAD to summarize and a TAIL to keep verbatim, cutting at a user-turn boundary so the last keepRecent turns survive raw and no tool\\_use/tool\\_result pair is ever divided. ok is false when the session is still short enough that there's nothing worth compacting (≤ keepRecent turns).\n\n```go\nfunc Render(messages []wire.Message) string\n```\nRender flattens the head messages into a plain-text transcript for the summarizer. It is deliberately lossy in the safe direction: tool mechanics are reduced to a one-liner and tool output is clipped, while user/assistant prose is preserved nearly whole. The output carries NO doctrine — it is pure session content; the compaction prompt lives server-side.\n\n## Types\n\n```go\ntype EvictOpts struct {\n\t// Pinned reports whether a read_file path is HOT — the session has observed\n\t// it being re-read (typically right after a prior eviction), so evicting it\n\t// again just forces another re-read (the measured read-evict-reread thrash:\n\t// the same 9 files re-read 13-14x in one session). A pinned path's LATEST\n\t// live read survives even past the keepRecent cutoff. Superseded duplicates\n\t// of a pinned path still offload — that is deduplication, not thrash.\n\tPinned func(path string) bool\n}\n```\nEvictOpts tunes EvictStaleToolResultsOpts beyond the keep count.\n\n","path":"github.com/memcode-ai/memcode/internal/agent/compaction","name":"compaction","synopsis":"Package compaction is the PURE, testable core of in-session context compaction: deciding WHERE to cut a running conversation and rendering the cut-off part into a plain transcript for an Anthropic summarizer.","isRedistributable":true}
