Skip to content
Python

Make the compaction summary reliable, and stop losing the task statement (OPE-189)

Devika Verma72e62e87b0a0
diff --git a/coworker/agent.py b/coworker/agent.py
index 7e904188..311113cb 100644
--- a/coworker/agent.py
+++ b/coworker/agent.py
@@ -604,9 +604,17 @@ def build_engine(
)
# OPE-186 change 3: a configured compaction cap makes the summariser fire earlier
# than the built-in 250,000-token cap. The window still comes from the model matrix.
+ # OPE-189: the summariser's own output ceiling rides the same settings dict; unset
+ # keys fall back to the engine's defaults, so setting either one alone is safe.
+ _compaction_overrides: dict[str, Any] = {}
if config.compaction_cap_tokens:
- _cap_tokens = int(config.compaction_cap_tokens)
- engine.compaction_settings = lambda: {"cap_tokens": _cap_tokens}
+ _compaction_overrides["cap_tokens"] = int(config.compaction_cap_tokens)
+ if config.compaction_summary_max_tokens:
+ _compaction_overrides["summary_max_tokens"] = int(
+ config.compaction_summary_max_tokens
+ )
+ if _compaction_overrides:
+ engine.compaction_settings = lambda: dict(_compaction_overrides)
engine.executor = executor # type: ignore[attr-defined]
engine.todo = todo # type: ignore[attr-defined]
engine.agent_name = agent.name # type: ignore[attr-defined]
diff --git a/coworker/compaction.py b/coworker/compaction.py
index 1958dc7c..cd25b0cb 100644
--- a/coworker/compaction.py
+++ b/coworker/compaction.py
@@ -27,18 +27,43 @@ DEFAULT_CONTEXT_WINDOW = 128_000
# The newest slice kept verbatim, as a fraction of the trigger (a token budget, not a
# turn count — one huge tool loop shouldn't starve the working set).
KEEP_RECENT_FRACTION = 0.25
-# The summarizer call itself: tools off, modest ceiling.
-SUMMARY_MAX_TOKENS = 3_000
+# The summarizer call itself: tools off. OPE-189: 3,000 was too low for a reasoning model
+# — the budget is shared with the model's thinking, and in a 15-task trial Kimi K3
+# spent it thinking on 60 of 103 summary calls, so the summary itself was cut off or never
+# started. Overridable per run ("summary_max_tokens" in the compaction settings).
+SUMMARY_MAX_TOKENS = 16_000
+# A summary is the coworker's ONLY memory of the span, so "not empty" is too low a bar to
+# accept one. Below this, or missing the sections, it is refused and retried (OPE-189).
+SUMMARY_MIN_CHARS = 400
+# Lowercase fragments of the eight section headings, used to sanity-check a summary. The
+# first is the anchor: a reply that slipped into continuing the session never has it.
+SUMMARY_SECTION_MARKERS = (
+ "primary request",
+ "key concepts",
+ "artifacts and files",
+ "errors and fixes",
+ "user messages",
+ "pending tasks",
+ "current work",
+ "next step",
+)
# Per-message clip when rendering the span for the summarizer; tool results are the
# first casualty (huge and mostly stale — a file read 40 turns ago is better re-read).
_SPAN_TOOL_RESULT_CLIP = 400
_SPAN_BUDGET_CHARS = 400_000
-# User messages preserved mechanically in the compacted block ("trimmed of pasted bulk").
-# The list is capped to the newest N across repeated compactions — otherwise it appends
-# forever and the block slowly reclaims the window it freed. Dropped ones stay counted
-# (their intent lives in the summary, which is asked to list user messages too).
-_USER_MESSAGE_CLIP = 600
-_USER_MESSAGES_MAX = 40
+# User messages preserved mechanically in the compacted block. They cannot simply all be
+# kept — otherwise the list appends forever and the block slowly reclaims the window it
+# freed — but OPE-189 showed both halves of the old rule (clip each to 600 chars, keep the
+# newest 40) losing the one message that matters. An agent session's FIRST user message is
+# its mandate and never goes stale, and a task statement routinely runs past 600 chars: on
+# a 15-task trial 8 of the 15 task prompts were cut mid-sentence, dropping the output
+# path and the required field names the task checks. So: pin the first message whole,
+# then fill backwards from the newest with a token budget. Dropped ones (now from the
+# MIDDLE) stay counted, and the block says where the gap is.
+_USER_PIN_MAX_TOKENS = 4_000 # a pasted opening message still can't blow the block
+_USER_BUDGET_FRACTION = 0.08 # of the compaction trigger, so a lowered trigger scales too
+_USER_BUDGET_MIN = 2_000
+_USER_BUDGET_MAX = 20_000 # what 8% comes to at the default trigger
_TRIM_FRACTION = 0.10
@@ -287,30 +312,68 @@ def _text_of(content: Any) -> str:
return "" if content is None else str(content)
-def extract_user_messages(
- span: list[dict[str, Any]], *, clip: int = _USER_MESSAGE_CLIP
-) -> list[str]:
- """Every user message in the span, chronological, trimmed of pasted bulk. Preserved
- mechanically — the summarizer is also asked to list them, but user words are the
- ground truth of intent and must not depend on an LLM remembering to include them."""
+def extract_user_messages(span: list[dict[str, Any]]) -> list[str]:
+ """Every user message in the span, chronological, VERBATIM (whitespace normalized).
+ Preserved mechanically — the summarizer is also asked to list them, but user words are
+ the ground truth of intent and must not depend on an LLM remembering to include them.
+ Fitting them to a budget is `fit_user_messages`'s job, not this one's: nothing is
+ clipped here, so the caller always has the real text to work from."""
out: list[str] = []
for msg in span:
if msg.get("role") != "user":
continue
text = " ".join(_text_of(msg.get("content")).split())
- if not text:
- continue
- out.append(text[: clip - 1] + "…" if len(text) > clip else text)
+ if text:
+ out.append(text)
return out
-def _cap_user_messages(
- messages: list[str], *, prior_dropped: int, limit: int = _USER_MESSAGES_MAX
+def user_message_budget(trigger: int) -> int:
+ """Token budget for the preserved user messages: a fraction of the compaction trigger,
+ bounded. A FIXED budget would be wrong at both ends — 20,000 tokens against a 60,000
+ trigger spends a third of the space compaction just freed."""
+ return max(_USER_BUDGET_MIN, min(_USER_BUDGET_MAX, int(_USER_BUDGET_FRACTION * trigger)))
+
+
+def _clip_to_tokens(text: str, tokens: int) -> str:
+ limit = max(1, tokens) * 4 # the chars/4 estimate used everywhere in this module
+ return text if len(text) <= limit else text[: limit - 1] + "…"
+
+
+def fit_user_messages(
+ messages: list[str],
+ *,
+ prior_dropped: int,
+ budget_tokens: int,
+ pin_tokens: int = _USER_PIN_MAX_TOKENS,
) -> tuple[list[str], int]:
- """Newest-`limit` slice plus the running total of everything ever dropped."""
- if len(messages) <= limit:
- return messages, prior_dropped
- return messages[-limit:], prior_dropped + (len(messages) - limit)
+ """The preserved list: the FIRST message pinned whole, then the newest filling
+ `budget_tokens` backwards. Returns (kept, running total ever dropped).
+
+ Pinning is the point. The first user message states why the session exists — the task,
+ the spec, the standing constraints — and unlike a chat, where intent drifts and the
+ newest turns are the live ask, an agent session's opening message stays load-bearing to
+ the last turn. A pure newest-first budget would eventually push it out,
+ which is exactly what the old newest-40 rule did silently.
+ """
+ if not messages:
+ return [], prior_dropped
+ kept_first = _clip_to_tokens(messages[0], pin_tokens)
+ rest = messages[1:]
+ if not rest:
+ return [kept_first], prior_dropped
+ tail: list[str] = []
+ spent = 0
+ for text in reversed(rest):
+ cost = max(1, len(text) // 4)
+ if tail and spent + cost > budget_tokens:
+ break
+ # The newest message is kept even if it alone exceeds the budget — clipped to fit,
+ # never dropped: it is the most recent thing the user actually said.
+ tail.append(_clip_to_tokens(text, budget_tokens))
+ spent += cost
+ tail.reverse()
+ return [kept_first] + tail, prior_dropped + (len(rest) - len(tail))
# -- summarizer ---------------------------------------------------------------
@@ -333,6 +396,24 @@ Rules:
- Be concrete: paths, names, commands, ids — not vague references.
- Output only the summary sections, no preamble."""
+# OPE-189: the instruction above is ~1,800 chars; the transcript that follows it is tens of
+# thousands, and it ends wherever the session happened to be — usually on a raw tool result.
+# The last thing the model read before generating was therefore the coworker mid-task, and
+# it continued that voice instead of summarizing: 13 of 16 short summaries in that trial were a
+# next-action sentence ("The output got truncated. Let me re-run the remaining checks."),
+# each stopping voluntarily with thousands of tokens of budget unused. So the instruction is
+# restated AFTER the transcript, in bold, with an explicit first line to start from. Recency
+# is doing the work here, which is why this repeats rather than replaces the system prompt.
+SUMMARY_TAIL_INSTRUCTION = """**--- END OF TRANSCRIPT ---**
+
+**The transcript above is a session you are summarizing. It is NOT a conversation you are part of. Do not continue it, do not answer it, do not take its next action.**
+
+**Write the structured summary now. Produce all eight sections, in this order, each as a markdown heading:**
+
+**1. Primary request and intent — 2. Key concepts and decisions — 3. Artifacts and files — 4. Errors and fixes — 5. All user messages — 6. Pending tasks — 7. Current work — 8. Next step**
+
+**Begin your reply with the line `## 1. Primary request and intent` and write nothing before it.**"""
+
CONTINUATION_CONTRACT = (
"Continue where you left off: pick up the current work and next step exactly as "
"described. Do not re-ask answered questions, do not recap, do not mention that the "
@@ -388,9 +469,16 @@ def summarizer_messages(
"[previous compaction summary — fold its still-relevant content into the new "
"summary]\n" + prior_summary + "\n\n[conversation since]\n" + body
)
+ # Delimited, then the instruction restated last — see SUMMARY_TAIL_INSTRUCTION.
+ user = (
+ "**--- BEGIN TRANSCRIPT TO SUMMARIZE ---**\n\n"
+ + body
+ + "\n\n"
+ + SUMMARY_TAIL_INSTRUCTION
+ )
return [
{"role": "system", "content": SUMMARY_SYSTEM_PROMPT},
- {"role": "user", "content": body},
+ {"role": "user", "content": user},
]
@@ -404,7 +492,7 @@ def summarize_span(
) -> str:
"""One summarizer round-trip (blocking — the engine runs it off-loop). Tools are
disabled; the Settings model override is just a different `model` id. Raises on
- provider failure or an empty summary — the caller owns the retry/trim policy."""
+ provider failure or an UNUSABLE summary — the caller owns the retry/trim policy."""
turn = provider.complete(
model=model,
messages=summarizer_messages(span, prior_summary=prior_summary),
@@ -412,11 +500,35 @@ def summarize_span(
max_tokens=max_tokens,
)
text = (getattr(turn, "text", None) or "").strip()
- if not text:
- raise RuntimeError("summarizer returned an empty summary")
+ problem = summary_quality_problem(text)
+ if problem:
+ raise RuntimeError(f"summarizer returned an unusable summary: {problem}")
return text
+def summary_quality_problem(text: Optional[str]) -> Optional[str]:
+ """Why this summary can't stand in for the span, or None if it can.
+
+ OPE-189: the old bar was "not empty", which accepted a 61-character sentence as the
+ coworker's whole memory of 75 turns. The checks are deliberately coarse — a summary
+ that is merely thin still beats falling back to the no-summary trim — and aimed at the
+ two shapes actually observed: nothing at all, and a next-action sentence in the
+ coworker's voice (which never carries the section headings).
+ """
+ stripped = (text or "").strip()
+ if not stripped:
+ return "empty"
+ if len(stripped) < SUMMARY_MIN_CHARS:
+ return f"too short ({len(stripped)} chars, minimum {SUMMARY_MIN_CHARS})"
+ low = stripped.lower()
+ if SUMMARY_SECTION_MARKERS[0] not in low:
+ return f"does not open the sections (no '{SUMMARY_SECTION_MARKERS[0]}' heading)"
+ found = sum(1 for marker in SUMMARY_SECTION_MARKERS if marker in low)
+ if found * 2 < len(SUMMARY_SECTION_MARKERS):
+ return f"only {found} of {len(SUMMARY_SECTION_MARKERS)} sections present"
+ return None
+
+
# -- building + applying a compaction -----------------------------------------
@@ -427,6 +539,8 @@ def build_state(
model: str,
keep_tokens: int,
prior: Optional[CompactionState] = None,
+ summary_max_tokens: int = SUMMARY_MAX_TOKENS,
+ user_budget_tokens: int = _USER_BUDGET_MAX,
) -> Optional[CompactionState]:
"""Summarize everything older than the picked boundary into a new CompactionState.
On repeated compaction the prior summary heads the new span. Returns None when there
@@ -442,10 +556,12 @@ def build_state(
model,
span,
prior_summary=prior.summary_text if prior is not None else "",
+ max_tokens=summary_max_tokens,
)
- users, dropped = _cap_user_messages(
+ users, dropped = fit_user_messages(
prior_users + extract_user_messages(span),
prior_dropped=prior.user_messages_dropped if prior is not None else 0,
+ budget_tokens=user_budget_tokens,
)
return CompactionState(
boundary_index=boundary,
@@ -463,6 +579,7 @@ def trim_state(
*,
prior: Optional[CompactionState] = None,
fraction: float = _TRIM_FRACTION,
+ user_budget_tokens: int = _USER_BUDGET_MAX,
) -> Optional[CompactionState]:
"""The no-LLM fallback: advance the boundary past ~`fraction` of the outbound
messages. No summary — but the mechanical block and the user-message list (never
@@ -488,9 +605,10 @@ def trim_state(
+ "(Older turns were trimmed to fit the context window; no summary is available "
"for them. Re-read files and re-run commands if earlier results are needed.)"
)
- users, dropped = _cap_user_messages(
+ users, dropped = fit_user_messages(
prior_users + extract_user_messages(span),
prior_dropped=prior.user_messages_dropped if prior is not None else 0,
+ budget_tokens=user_budget_tokens,
)
return CompactionState(
boundary_index=boundary,
@@ -517,12 +635,15 @@ def compacted_block(state: CompactionState) -> str:
parts += ["", state.working_state]
if state.user_messages:
parts += ["", "## User messages in the compacted span (verbatim, chronological)"]
+ # The first is pinned, so any omission is a gap in the MIDDLE — say so there, or
+ # the note reads as if the opening request were the thing that went missing.
+ parts += [f"- {state.user_messages[0]}"]
if state.user_messages_dropped:
parts += [
- f"({state.user_messages_dropped} earlier user messages omitted — "
+ f"- ({state.user_messages_dropped} older user messages omitted here — "
"their intent is covered by the summary above)"
]
- parts += [f"- {u}" for u in state.user_messages]
+ parts += [f"- {u}" for u in state.user_messages[1:]]
if state.transcript_path:
parts += [
"",
diff --git a/coworker/config.py b/coworker/config.py
index 262ad159..ee9c018c 100644
--- a/coworker/config.py
+++ b/coworker/config.py
@@ -61,6 +61,11 @@ class Config:
# 1M-window model fired once in 445 long sessions. Lower it (e.g. 60000) to
# summarise long sessions earlier. Environment override: COWORKER_COMPACTION_CAP_TOKENS.
compaction_cap_tokens: Optional[int] = None
+ # OPE-189: output ceiling for the summariser call, in tokens. Unset = 16,000. On a
+ # reasoning model the budget is shared with the model's thinking, so a low value means
+ # the summary itself never gets written. Environment override:
+ # COWORKER_COMPACTION_SUMMARY_MAX_TOKENS.
+ compaction_summary_max_tokens: Optional[int] = None
allowed_commands: list[str] = field(
default_factory=lambda: list(DEFAULT_ALLOWED_COMMANDS)
)
@@ -112,6 +117,7 @@ _FIELDS = {
"reasoning_effort",
"tool_result_max_bytes",
"compaction_cap_tokens",
+ "compaction_summary_max_tokens",
"allowed_commands",
"auto_allow",
"allowed_domains",
@@ -166,6 +172,7 @@ MAX_OUTPUT_TOKENS_ENV = "COWORKER_MAX_OUTPUT_TOKENS"
REASONING_EFFORT_ENV = "COWORKER_REASONING_EFFORT"
TOOL_RESULT_MAX_BYTES_ENV = "COWORKER_TOOL_RESULT_MAX_BYTES"
COMPACTION_CAP_TOKENS_ENV = "COWORKER_COMPACTION_CAP_TOKENS"
+COMPACTION_SUMMARY_MAX_TOKENS_ENV = "COWORKER_COMPACTION_SUMMARY_MAX_TOKENS"
def _nonnegative_int(value: Any, source: str) -> Optional[int]:
@@ -269,4 +276,33 @@ def load_config(
f"compaction_cap_tokens must be a positive integer, got {raw_comp!r} ({COMPACTION_CAP_TOKENS_ENV})"
)
cfg.compaction_cap_tokens = parsed_comp
+ cfg.compaction_summary_max_tokens = _positive_int_setting(
+ cfg.compaction_summary_max_tokens,
+ "compaction_summary_max_tokens",
+ COMPACTION_SUMMARY_MAX_TOKENS_ENV,
+ )
return cfg
+
+
+def _positive_int_setting(
+ value: Any, name: str, env_var: str
+) -> Optional[int]:
+ """A config.toml value overridden by `env_var`; both must be positive integers."""
+ if value is not None and (
+ isinstance(value, bool) or not isinstance(value, int) or value <= 0
+ ):
+ raise ValueError(
+ f"{name} must be a positive integer, got {value!r} (config.toml)"
+ )
+ raw = (os.environ.get(env_var) or "").strip()
+ if not raw:
+ return value
+ try:
+ parsed = int(raw)
+ except ValueError:
+ parsed = 0
+ if parsed <= 0:
+ raise ValueError(
+ f"{name} must be a positive integer, got {raw!r} ({env_var})"
+ )
+ return parsed
diff --git a/coworker/engine.py b/coworker/engine.py
index f282c1b2..13ce8f72 100644
--- a/coworker/engine.py
+++ b/coworker/engine.py
@@ -744,6 +744,7 @@ class TurnEngine:
)
cfg.setdefault("threshold_pct", _compaction.DEFAULT_THRESHOLD_PCT)
cfg.setdefault("cap_tokens", _compaction.DEFAULT_CAP_TOKENS)
+ cfg.setdefault("summary_max_tokens", _compaction.SUMMARY_MAX_TOKENS)
return cfg
def _compaction_due(self) -> bool:
@@ -782,9 +783,13 @@ class TurnEngine:
pct = float(cfg["threshold_pct"])
cap = int(cfg["cap_tokens"])
window = cfg.get("context_window")
- keep = int(
- _compaction.KEEP_RECENT_FRACTION
- * _compaction.trigger_tokens(window, threshold_pct=pct, cap_tokens=cap)
+ trigger = _compaction.trigger_tokens(window, threshold_pct=pct, cap_tokens=cap)
+ keep = int(_compaction.KEEP_RECENT_FRACTION * trigger)
+ # OPE-189: both budgets scale with the trigger, so lowering it to save tokens can't
+ # be undone by a block that keeps spending the freed space.
+ user_budget = _compaction.user_message_budget(trigger)
+ summary_max = int(
+ cfg.get("summary_max_tokens") or _compaction.SUMMARY_MAX_TOKENS
)
model = str(cfg.get("model") or "") or self.model
@@ -795,6 +800,8 @@ class TurnEngine:
model=model,
keep_tokens=keep,
prior=self.compaction_state,
+ summary_max_tokens=summary_max,
+ user_budget_tokens=user_budget,
)
state: Optional[_compaction.CompactionState] = None
@@ -855,7 +862,11 @@ class TurnEngine:
self._last_context_tokens = None # stale once the outbound view shrank
return "Context compacted — earlier turns were summarized"
if failed or force:
- trimmed = _compaction.trim_state(self.messages, prior=self.compaction_state)
+ trimmed = _compaction.trim_state(
+ self.messages,
+ prior=self.compaction_state,
+ user_budget_tokens=user_budget,
+ )
if trimmed is not None:
self.compaction_state = trimmed
self._last_context_tokens = None
diff --git a/coworker/providers/matrix.py b/coworker/providers/matrix.py
index 748a39fc..aaee241c 100644
--- a/coworker/providers/matrix.py
+++ b/coworker/providers/matrix.py
@@ -134,6 +134,17 @@ MATRIX: dict[str, ModelEntry] = {
"anthropic:claude-haiku-4-5": ModelEntry(
"Claude Haiku 4.5 · Anthropic", _AGENTIC_VISION, 200_000
),
+ # Claude 4.5/4.6: kept for comparability with published evaluations of those
+ # models, so OpenWorker can be measured against them on the same model.
+ "anthropic:claude-opus-4-6": ModelEntry(
+ "Claude Opus 4.6 · Anthropic", _AGENTIC_VISION, 200_000
+ ),
+ "anthropic:claude-opus-4-5": ModelEntry(
+ "Claude Opus 4.5 · Anthropic", _AGENTIC_VISION, 200_000
+ ),
+ "anthropic:claude-sonnet-4-5": ModelEntry(
+ "Claude Sonnet 4.5 · Anthropic", _AGENTIC_VISION, 200_000
+ ),
# Gemini 3 (thought signatures required in tool loops — carried via the `_gemini`
# message sidecar, see gemini_provider.py; ids from the vendor catalog 2026-07-22).
"gemini:gemini-3.1-pro-preview": ModelEntry(
diff --git a/tests/test_compaction.py b/tests/test_compaction.py
index a480bfb6..6c937555 100644
--- a/tests/test_compaction.py
+++ b/tests/test_compaction.py
@@ -5,6 +5,7 @@ import json
import pytest
+from coworker import compaction as C
from coworker.compaction import (
CompactionState,
DEFAULT_CAP_TOKENS,
@@ -78,8 +79,11 @@ def convo(turns=6, bulk=2000):
class FakeSummarizer:
- def __init__(self, text="## Summary\nall good", fail_times=0):
- self.text = text
+ # The default must satisfy the OPE-189 quality gate (all eight sections, past the
+ # minimum length) — an unusable summary is now refused, not stored. GOOD_SUMMARY is
+ # defined further down the module and resolved when a fake is constructed.
+ def __init__(self, text=None, fail_times=0):
+ self.text = GOOD_SUMMARY if text is None else text
self.fail_times = fail_times
self.calls = []
@@ -170,7 +174,9 @@ def test_working_state_empty_span():
assert extract_working_state([user("hi"), assistant("yo")]) == ""
-def test_user_messages_extracted_verbatim_and_clipped():
+def test_user_messages_extracted_verbatim_and_uncut():
+ # OPE-189: extraction no longer clips — a 1,400-char task statement reached the block
+ # cut mid-sentence, losing the output path and the field names the grader checks.
span = [
user("first ask"),
assistant("a"),
@@ -181,7 +187,7 @@ def test_user_messages_extracted_verbatim_and_clipped():
out = extract_user_messages(span)
assert out[0] == "first ask"
assert out[1] == "second [image]"
- assert out[2].endswith("…") and len(out[2]) <= 600
+ assert out[2] == "bulk " + "z" * 2000 # verbatim; fitting is fit_user_messages' job
# -- summarizer seam ----------------------------------------------------------
@@ -192,19 +198,69 @@ def test_summarizer_messages_clip_tool_results_and_fold_prior():
msgs = summarizer_messages(span, prior_summary="OLD SUMMARY")
body = msgs[1]["content"]
assert "OLD SUMMARY" in body
- assert len(body) < 3000 # the 2500-char tool result got clipped hard
+ # The 2500-char tool result got clipped hard; the tail instruction is the only other bulk.
+ assert len(body) < 3000 + len(C.SUMMARY_TAIL_INSTRUCTION)
assert msgs[0]["role"] == "system" and "Primary request and intent" in msgs[0]["content"]
-def test_summarize_span_passes_model_and_raises_on_empty():
- fake = FakeSummarizer(text="## ok")
- out = summarize_span(fake, "prov:model-x", [user("hi")])
- assert out == "## ok"
+def test_summarizer_instruction_comes_after_the_transcript():
+ """OPE-189: recency decides. With the instruction only up front, the last thing the
+ model read was the coworker mid-task and it continued that voice instead of
+ summarizing. The transcript is delimited and the instruction restated last."""
+ span = [user("go"), *tool_turn("run_shell", {"command": "ls"}, "a.txt b.txt")]
+ body = summarizer_messages(span)[1]["content"]
+ assert body.startswith("**--- BEGIN TRANSCRIPT TO SUMMARIZE ---**")
+ assert body.rstrip().endswith(C.SUMMARY_TAIL_INSTRUCTION)
+ assert body.index("[tool result]") < body.index("**--- END OF TRANSCRIPT ---**")
+ assert "## 1. Primary request and intent" in C.SUMMARY_TAIL_INSTRUCTION
+
+
+GOOD_SUMMARY = "\n".join(
+ f"## {i}. {name}\ndetail line with enough substance to clear the minimum length bar"
+ for i, name in enumerate(
+ [
+ "Primary request and intent",
+ "Key concepts and decisions",
+ "Artifacts and files",
+ "Errors and fixes",
+ "All user messages",
+ "Pending tasks",
+ "Current work",
+ "Next step",
+ ],
+ start=1,
+ )
+)
+
+
+def test_summarize_span_passes_model_and_budget():
+ fake = FakeSummarizer(text=GOOD_SUMMARY)
+ out = summarize_span(fake, "prov:model-x", [user("hi")], max_tokens=9999)
+ assert out == GOOD_SUMMARY
assert fake.calls[0]["model"] == "prov:model-x"
assert fake.calls[0]["tools"] is None
+ assert fake.calls[0]["max_tokens"] == 9999
+
- with pytest.raises(RuntimeError):
- summarize_span(FakeSummarizer(text=" "), "m", [user("hi")])
+def test_summarize_span_refuses_an_unusable_summary():
+ """OPE-189: 'not empty' accepted a 61-char next-action sentence as the coworker's whole
+ memory of 75 turns. Empty, too short, and role-slipped replies are all refused so the
+ caller's retry (then trim) policy runs."""
+ for bad in (
+ " ",
+ "The output got truncated. Let me re-run the remaining checks.",
+ "## Artifacts and files\n" + "detail " * 200, # long, but not the anchor section
+ ):
+ with pytest.raises(RuntimeError):
+ summarize_span(FakeSummarizer(text=bad), "m", [user("hi")])
+
+
+def test_summary_quality_problem_names_the_fault():
+ assert C.summary_quality_problem(GOOD_SUMMARY) is None
+ assert C.summary_quality_problem("") == "empty"
+ assert "too short" in C.summary_quality_problem("## 1. Primary request and intent")
+ slipped = "Let me re-run the remaining checks. " * 20
+ assert "does not open the sections" in C.summary_quality_problem(slipped)
# -- build + repeated compaction ----------------------------------------------
@@ -212,7 +268,7 @@ def test_summarize_span_passes_model_and_raises_on_empty():
def test_build_state_and_outbound_view():
msgs = convo(turns=6)
- fake = FakeSummarizer(text="## Summary\nthe gist")
+ fake = FakeSummarizer(text=GOOD_SUMMARY + "\nthe gist")
state = build_state(
msgs, provider=fake, model="m", keep_tokens=estimate_tokens(msgs[-4:]) + 10
)
@@ -315,32 +371,61 @@ def test_is_context_overflow():
assert not is_context_overflow(Exception("connection reset"))
-def test_user_messages_capped_across_repeated_compactions():
- # The mechanical user-message list must not grow forever — newest _USER_MESSAGES_MAX
- # survive, the rest stay counted so the block's "omitted" note is honest.
- from coworker.compaction import _USER_MESSAGES_MAX
-
+def test_user_messages_pin_the_first_and_budget_the_rest():
+ """OPE-189: the list must not grow forever, but the OLD rule (newest 40) silently threw
+ away the opening message — the one that says why the session exists. Now the first is
+ pinned and the newest fill a token budget; the gap is in the middle."""
msgs = [{"role": "system", "content": "s"}]
- for i in range(120):
- msgs.append({"role": "user", "content": f"ask {i}"})
+ msgs.append({"role": "user", "content": "THE ORIGINAL TASK: " + "spec " * 100})
+ msgs.append({"role": "assistant", "content": "starting"})
+ for i in range(300):
+ msgs.append({"role": "user", "content": f"ask {i} " + "filler " * 40})
msgs.append({"role": "assistant", "content": f"answer {i}"})
state = None
while True:
- nxt = trim_state(msgs, prior=state, fraction=0.4)
+ nxt = trim_state(msgs, prior=state, fraction=0.4, user_budget_tokens=2_000)
if nxt is None:
break
state = nxt
assert state is not None
- assert len(state.user_messages) <= _USER_MESSAGES_MAX
- assert state.user_messages_dropped > 0
- assert state.user_messages[-1].startswith("ask") # newest survive, oldest dropped
-
+ assert state.user_messages[0].startswith("THE ORIGINAL TASK:") # pinned, never dropped
+ # The newest preserved message is from the end of the run (the very last turns stay in
+ # the verbatim tail rather than the block, so this is the newest that was compacted).
+ assert state.user_messages[-1].startswith("ask 29")
+ assert state.user_messages_dropped > 0 # the middle is what gives
block = compacted_block(state)
- assert f"{state.user_messages_dropped} earlier user messages omitted" in block
+ assert "older user messages omitted here" in block
+ assert block.index("THE ORIGINAL TASK") < block.index("omitted here")
restored = CompactionState.from_dict(state.as_dict())
- assert restored is not None
- assert restored.user_messages_dropped == state.user_messages_dropped
assert restored.user_messages == state.user_messages
+ assert restored.user_messages_dropped == state.user_messages_dropped
+
+
+def test_fit_user_messages_pins_first_clips_giants_and_counts_drops():
+ budget = 100 # tokens, i.e. ~400 chars
+ msgs = ["FIRST " + "a" * 200, "old " + "b" * 400, "mid", "newest"]
+ kept, dropped = C.fit_user_messages(msgs, prior_dropped=3, budget_tokens=budget)
+ assert kept[0] == msgs[0] # pinned whole: well under the pin cap
+ assert kept[-1] == "newest"
+ assert dropped >= 3 # running total carries forward
+ assert "mid" in kept # cheap messages still fit
+
+ # A first message far over the pin cap is clipped, never dropped.
+ huge = "X" * 100_000
+ kept, _ = C.fit_user_messages([huge, "later"], prior_dropped=0, budget_tokens=budget)
+ assert kept[0].startswith("XXX") and kept[0].endswith("…")
+ assert len(kept[0]) <= C._USER_PIN_MAX_TOKENS * 4
+
+ # A single newest message over budget is clipped to fit, not dropped.
+ kept, _ = C.fit_user_messages(["first", huge], prior_dropped=0, budget_tokens=budget)
+ assert len(kept) == 2 and kept[1].endswith("…")
+
+
+def test_user_message_budget_scales_with_the_trigger():
+ assert C.user_message_budget(250_000) == 20_000 # at the default trigger
+ assert C.user_message_budget(60_000) == 4_800 # a lowered trigger keeps its saving
+ assert C.user_message_budget(1_000) == C._USER_BUDGET_MIN # floor
+ assert C.user_message_budget(10_000_000) == C._USER_BUDGET_MAX # ceiling
diff --git a/tests/test_compaction_engine.py b/tests/test_compaction_engine.py
index aabd0651..b1501453 100644
--- a/tests/test_compaction_engine.py
+++ b/tests/test_compaction_engine.py
@@ -16,7 +16,22 @@ from coworker.providers import (
from coworker.providers.base import TokenUsage
from coworker.tools import ToolRegistry
-SUMMARY = "## Primary request and intent\nkeep building the report"
+# Must clear the OPE-189 quality gate: all eight sections, past the minimum length. A
+# summary that fails it is refused and the engine falls back to trimming, which is what
+# these tests would otherwise be asserting against.
+SUMMARY = "\n".join(
+ f"## {name}\nenough detail in this section to clear the summary minimum length"
+ for name in (
+ "Primary request and intent",
+ "Key concepts and decisions",
+ "Artifacts and files",
+ "Errors and fixes",
+ "All user messages",
+ "Pending tasks",
+ "Current work",
+ "Next step",
+ )
+)
class CompactingProvider(ProviderClient):
diff --git a/tests/test_compaction_transcript.py b/tests/test_compaction_transcript.py
index 89ac3e32..e210f5d6 100644
--- a/tests/test_compaction_transcript.py
+++ b/tests/test_compaction_transcript.py
@@ -15,7 +15,20 @@ from coworker.permissions import PermissionEngine
from coworker.providers import AssistantTurn, ModelCapabilities, ProviderClient
from coworker.tools import ToolRegistry
-SUMMARY = "## Primary request and intent\nkeep building the report"
+# Must clear the OPE-189 quality gate: all eight sections, past the minimum length.
+SUMMARY = "\n".join(
+ f"## {name}\nenough detail in this section to clear the summary minimum length"
+ for name in (
+ "Primary request and intent",
+ "Key concepts and decisions",
+ "Artifacts and files",
+ "Errors and fixes",
+ "All user messages",
+ "Pending tasks",
+ "Current work",
+ "Next step",
+ )
+)
class CompactingProvider(ProviderClient):
@@ -120,6 +133,56 @@ def test_compaction_cap_config_and_env(tmp_path, monkeypatch):
raise AssertionError("a zero cap must be rejected")
+def test_summary_budget_config_and_env(tmp_path, monkeypatch):
+ """OPE-189: the summariser's output ceiling is a setting, not a constant — on a
+ reasoning model it is shared with the model's thinking."""
+ from coworker.config import COMPACTION_SUMMARY_MAX_TOKENS_ENV
+
+ monkeypatch.delenv(COMPACTION_SUMMARY_MAX_TOKENS_ENV, raising=False)
+ (tmp_path / ".coworker").mkdir()
+ cfg_file = tmp_path / ".coworker" / "config.toml"
+ cfg_file.write_text("compaction_summary_max_tokens = 24000\n", encoding="utf-8")
+ cfg = load_config(tmp_path, global_path=tmp_path / "no-global.toml")
+ assert cfg.compaction_summary_max_tokens == 24000
+ monkeypatch.setenv(COMPACTION_SUMMARY_MAX_TOKENS_ENV, "9000")
+ cfg = load_config(tmp_path, global_path=tmp_path / "no-global.toml")
+ assert cfg.compaction_summary_max_tokens == 9000 # env wins
+ monkeypatch.setenv(COMPACTION_SUMMARY_MAX_TOKENS_ENV, "0")
+ try:
+ load_config(tmp_path, global_path=tmp_path / "no-global.toml")
+ except ValueError as exc:
+ assert "compaction_summary_max_tokens" in str(exc)
+ else:
+ raise AssertionError("a zero budget must be rejected")
+
+
+def test_engine_passes_the_configured_budgets_to_the_summarizer(tmp_path):
+ """The trigger drives both derived budgets, and the summariser call gets the ceiling."""
+ seen: dict = {}
+
+ class Recorder(CompactingProvider):
+ def complete(self, *, model, messages, tools=None, **settings):
+ seen.update(settings)
+ return super().complete(model=model, messages=messages, tools=tools, **settings)
+
+ engine = TurnEngine(
+ provider=Recorder([AssistantTurn(text="done", finish_reason="stop")]),
+ registry=ToolRegistry(),
+ permissions=PermissionEngine(workspace_root=tmp_path),
+ model="gpt-5.5",
+ messages=_history(),
+ )
+ engine.compaction_settings = lambda: {
+ "cap_tokens": 400,
+ "threshold_pct": 0.8,
+ "context_window": 100_000,
+ "summary_max_tokens": 7_777,
+ }
+ _collect(engine)
+ assert seen.get("max_tokens") == 7_777
+ assert C.user_message_budget(400) == C._USER_BUDGET_MIN # floor at a tiny trigger
+
+
def test_render_transcript_is_readable_and_skips_system():
text = C.render_transcript(_history(turns=2), upto=7)
assert text.startswith("# Compacted transcript")