sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Add an overridable `Model.compact()` and route summarization through it

Open jimbrub opened this issue 1 month ago • 1 comments

Human Overview

Summarization should go through the model, so each provider can decide how to compact. Model.compact() gets a working default that does today's client-side summary, and providers that support native compaction override it, the same way countTokens() works.

SDK Language

Both / Not language-specific

Problem Statement

SummarizeStrategy builds the summary itself, on the client. A provider that can compact server-side, keeping preserved thinking valid, has no way to take over. The strategy also has nothing to tell it where a safe cut point is (a user-prompt boundary, never between a toolUse and its toolResult).

Proposed Solution

Part 1 of design 0022:

  • Add a concrete Model.compact(), not an abstract one. The default is today's client-side summary, and it returns [summary, ...preserved] or undefined.
    • TS: compact(messages, options?: CompactOptions) with preservedMessages, systemPrompt, toolSpecs, instructions.
    • Python: compact(messages, *, preserved_messages=0, tool_specs=None, system_prompt=None, instructions=None).
  • Add a shared compactionSplitIndex helper. It moves the cut back to a user-prompt boundary, never splits a toolUse/toolResult pair, and never summarizes pinned messages.
  • The strategy decides when to compact and the model decides how. Message-level summaries in SummarizeStrategy always call model.compact() (through _compact()), passing the live system prompt and tool specs, and SummarizeConfig.systemPrompt as instructions.
  • BaseOffloadStrategy gains _isRetroactive (_isMessageLevel || _preserveRecent > 0). A retroactive per-block summary calls compact() only when the model reports serverCompaction === true. Otherwise it keeps today's behavior.

Done when:

  • Python and TypeScript have unit tests for the default compact(), compactionSplitIndex (tool pairs, pinned messages, user-prompt boundaries), and the strategy routing.
  • Summarization output and behavior are unchanged for models that don't override compact().

Use Case

A provider adds native compaction by overriding one method, and every summarize strategy (context_manager="auto", agentic, proactive_summarization) picks it up.

Additional Context

  • Blocks: #5401, #5404
  • Design: team/designs/0022-server-side-compaction-support.md (#5242)
  • Reference (earlier prototype, not the spec): #5287

jimbrub avatar Oct 23 '26 14:10 jimbrub

As-built notes from #5470, which resolves this issue. They record where the implementation differs from the spec above.

  • Name and shape. The method shipped as Model.summarize(messages, *, tool_specs, system_prompt, system_prompt_content, instructions, cancel_signal) and returns SummarizeResult { summary, usage }. It is not compact() returning [summary, ...preserved]. The strategy keeps ownership of the cut (_compaction_split_index) and of placement. It is marked experimental: treat it as a placeholder until the API follow-up below settles the final shape.

  • Routing. For now, the strategy detects an override by function identity. A model counts as overriding when summarize is replaced, by a subclass or on the instance. Only an override gets the conversation's opening prefix; every other selection gets the base summary. The Model.summarize docstring states this as the contract. #5466 replaces the identity check with a capability.

  • Behavior change for models that don't override. The "unchanged" acceptance line does not fully hold:

    • The base summary keeps message 0 verbatim.
    • A recency pass's cut moves off the last message, so a text summary never joins the live turn.
    • With preserve_recent ≤ 1 (or 2 with a trailing tool pair), a pass whose only remaining candidate is the last message is now skipped. In the reviewer's fuzz, that is 85 of 3,000 histories that main summarizes. The built-in configs never hit it.

    These are described in the PR. The skipped case is tracked in #5736.

Follow-ups: #5733 (final API shape), #5734 (evaluation), #5735 (summarizer prompt), #5736 (skipped passes).

jimbrub avatar Nov 02 '26 16:11 jimbrub