threecode/compact

Context management: when the conversation approaches the model's context window, older turns are collapsed into a single synthetic recap via a meta-call to the same model (with a dedicated terse system prompt). That recap replaces everything except the system prompt and the last SummarizeKeepRecent messages. Summarization is lossy and expensive, so it runs at most once per turn, only when the context crosses the threshold.

Types

ContextAction = enum
  caNone,                   ## within budget, nothing to do
  caSummarize                ## over threshold and enough history to make it worthwhile

Consts

SummarizerSystemPrompt = """You are summarizing an earlier coding session for later recall. Compress the messages below into one paragraph covering: files read/written, commands run and outcomes, current state (tests green? uncommitted changes? what decision was reached?). Omit everything that's been superseded. No filler."""
SummaryPrefix = "Earlier in this session: "

Procs

proc applySummary(messages: JsonNode; summary: string;
                  keepRecent = SummarizeKeepRecent): int {....raises: [],
    tags: [RootEffect], forbids: [].}
Rewrites messages in place to [system, synthetic_user_summary, ...last keepRecent] and returns the number of messages collapsed (i.e. removed from the middle). Returns 0 without touching messages if the prerequisites are not met: array shape, a system message at index 0, and at least keepRecent + 4 messages to justify the call.
proc contextWindowFor(model: string): int {....raises: [], tags: [], forbids: [].}
Heuristic fallback for models off the known-good table (experimental, or a provider/model pair we haven't curated). Substring match on well-known slugs; no known collisions in practice. Known-good pairs should go through contextWindowFor(p) instead, which consults KnownGoodCombos for the exact window.
proc contextWindowFor(p: Profile): int {....raises: [], tags: [], forbids: [].}
Known-good-aware context window. A curated (provider, model) pair returns its exact advertised window; everything else falls back to the substring heuristic in the model: string overload. The table is the source of truth because provider curation (which GLM build a provider actually serves) matters as much as the family.
proc decideContextAction(promptTokens, windowTokens, msgCount: int;
                         keepRecent = SummarizeKeepRecent;
                         threshold = SummarizeThresholdFrac): ContextAction {.
    ...raises: [], tags: [], forbids: [].}
Pure policy helper. Given a fresh usage reading and the current message count, decide whether to run the lossy summarizer or do nothing.
proc summarizeHistory(messages: JsonNode; p: Profile;
                      keepRecent = SummarizeKeepRecent): int {.
    ...raises: [Exception, IOError, KeyError], tags: [RootEffect, ReadDirEffect,
    ReadEnvEffect, ReadIOEffect, WriteIOEffect, TimeEffect], forbids: [].}
Collapse old turns into one synthetic user recap via a meta-model call. Returns the number of messages dropped; 0 if we bailed (too few messages, missing system prompt, empty profile, or the summarizer call failed). On failure messages is left untouched.