Skip to content

interlens.context

interlens.context

ContextPolicy

Bases: ABC

Decides how to fit a participant's view within its model's context window.

Crucially, fit runs on the typed ViewSegment list, before the family-specific finalize_view folds/merges anything. Operating pre-finalize means the policy can reliably preserve the system block and moderator seed (their origin is still intact) and trim only turn segments, instead of trying to reverse-engineer meaning out of already-folded text.

to_dict

to_dict() -> dict

Serialize as {"kind": ..., **params}. Subclasses with parameters extend the params; the default covers parameterless policies.

Source code in src/interlens/context/context_policy.py
def to_dict(self) -> dict:
	"""Serialize as ``{"kind": ..., **params}``. Subclasses with parameters extend the params; the default
	covers parameterless policies."""
	return {"kind": type(self).__name__}

DropOldestPolicy

Bases: ContextPolicy

Drop the oldest turn segments (preserving system/moderator/private_context) until the view fits.

ErrorPolicy

Bases: ContextPolicy

The safe default: raise if the view exceeds the context window rather than silently dropping content.

Silent truncation reads as "everything fit" when it didn't, which is exactly the kind of quiet data loss this harness avoids. Callers who want trimming opt into DropOldestPolicy/SlidingWindowPolicy explicitly.

SlidingWindowPolicy

SlidingWindowPolicy(
    keep_last: int, keep_system: bool = True
)

Bases: ContextPolicy

Keep the preserved framing plus the most recent keep_last turns; drop older turns.

keep_system=True (the default) preserves the system/moderator/private_context framing regardless of the window; set it False to also let framing fall outside the window (rarely wanted). Unlike DropOldestPolicy this is a fixed-size window rather than a fit-to-budget trim, so it's predictable turn-to-turn.

Source code in src/interlens/context/sliding_window_policy.py
def __init__(self, keep_last: int, keep_system: bool = True):
	self.keep_last = keep_last
	self.keep_system = keep_system

SummarizePolicy

SummarizePolicy(keep_last: int = 4, summarizer=None)

Bases: ContextPolicy

Compress older turns into a single summary segment instead of dropping them outright (the heaviest policy).

Keeps the preserved framing (system / moderator / private_context) and the most recent keep_last turns verbatim, and replaces the older middle turns with one summary segment produced by summarizer — a callable list[str] -> str over the dropped turns' contents. With no summarizer it inserts a neutral placeholder, so it degrades to a labelled drop rather than silently losing content.

summarizer is a live callable and so is not serialized; a loaded template gets summarizer=None and the caller re-injects one if needed.

Source code in src/interlens/context/summarize_policy.py
def __init__(self, keep_last: int = 4, summarizer=None):
	self.keep_last = keep_last
	self.summarizer = summarizer