Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 77 additions & 9 deletions agent/context_compressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def _strip_persistence_markers(messages: List[Dict[str, Any]]) -> None:
_AUTO_FOCUS_MAX_TURNS = 3
_AUTO_FOCUS_TURN_MAX_CHARS = 260
_AUTO_FOCUS_MAX_CHARS = 700
_ACTIVE_TASK_MAX_CHARS = 1400
# Keep a short run of recent messages verbatim even when the token budget is
# already exhausted. The public ``protect_last_n`` default is intentionally
# high for small/light tails, but using all 20 as a hard floor here would bring
Expand All @@ -321,6 +322,9 @@ def _strip_persistence_markers(messages: List[Dict[str, Any]]) -> None:
# the summary, the downstream model may re-emit it as an active directive on
# the next turn, triggering bogus attachment sends (#14665).
_MEDIA_DIRECTIVE_RE = re.compile(r"MEDIA:\S+")
_HISTORICAL_TASK_SECTION_RE = re.compile(
rf"(?ms)^{re.escape(HISTORICAL_TASK_HEADING)}\s*\n.*?(?=^## |\Z)"
)


def _dedupe_append(items: list[str], value: str, *, limit: int) -> None:
Expand Down Expand Up @@ -2142,25 +2146,25 @@ def _generate_summary(
_template_sections = f"""{HISTORICAL_TASK_HEADING}
[THE SINGLE MOST IMPORTANT FIELD. Capture the user's most recent unfulfilled
input verbatim — the exact words they used. This includes:
- Explicit task assignments ("refactor the auth module")
- Questions awaiting an answer ("waarom staat X op Y?", "wat zijn de volgende stappen?")
- Decisions awaiting input ("optie A of B?")
- Explicit task assignments ("<specific user task>")
- Questions awaiting an answer ("<specific user question>")
- Decisions awaiting input ("<option A or B?>")
- Ongoing discussions where the assistant owes the next substantive reply
A conversation where the user just asked a question IS an active task — the
task is "answer that question with full context". Do NOT write "None" merely
because the user did not issue an imperative command; reserve "None" for the
rare case where the last exchange was fully resolved and the user said
something like "thanks, that's all".
If multiple items are outstanding, list only the ones NOT yet completed.
Continuation should pick up exactly here. Examples:
"User asked: 'Now refactor the auth module to use JWT instead of sessions'"
"User asked: 'Waarom stond provider ineens op openrouter?' — needs investigation + answer"
"User chose option A; awaiting implementation of step 2"
This historical snapshot must identify the latest unresolved user input precisely. Examples:
"User asked: '<exact latest user request>'"
"User asked: '<exact latest user question>' — needs investigation + answer"
"User chose <option>; awaiting implementation of <specific next step>"
If the user's most recent message was a reverse signal (stop, undo, roll
back, never mind, just verify, change of topic) that supersedes earlier
work, write the reverse signal verbatim and DO NOT carry forward the
cancelled task. Example: "User asked: 'Stop the i18n refactor and just
verify the current diff' — earlier i18n in-flight work is cancelled."
cancelled task. Example: "User asked: '<exact reverse signal>' — earlier
in-flight work is cancelled."
If no outstanding task exists, write "None."]

## Goal
Expand Down Expand Up @@ -2322,6 +2326,7 @@ def _generate_summary(
# Redact the summary output as well — the summarizer LLM may
# ignore prompt instructions and echo back secrets verbatim.
summary = redact_sensitive_text(content.strip())
summary = self._ground_historical_task_snapshot(summary, turns_to_summarize)
# Store for iterative updates on next compaction
self._previous_summary = summary
self._clear_compression_failure_cooldown()
Expand Down Expand Up @@ -2593,6 +2598,69 @@ def _derive_auto_focus_topic(
focus = focus[: _AUTO_FOCUS_MAX_CHARS - 1].rstrip() + "…"
return focus

@classmethod
def _latest_user_task_snapshot(
cls,
messages: List[Dict[str, Any]],
) -> Optional[str]:
"""Return a deterministic task-snapshot line from the newest real user turn.

The LLM summarizer is allowed to compress prose, but it must not invent
the "what is the active task?" anchor from a prompt example or stale
prior summary. This helper extracts the anchor locally from the exact
compacted turns so the summary can be grounded before it becomes live
context.
"""
# Reuse the runtime's real-user predicate so the deterministic
# snapshot can never anchor on user-role scaffolding (todo
# snapshots, truncation notices, background-process reports) —
# the exact class of turn this grounding exists to bypass.
from agent.conversation_compression import _is_real_user_message

for msg in reversed(messages):
if msg.get("role") != "user":
continue
if not _is_real_user_message(msg):
continue
content = msg.get("content")
text = redact_sensitive_text(_content_text_for_contains(content).strip())
if not text:
continue
text = re.sub(r"\s+", " ", text)
if len(text) > _ACTIVE_TASK_MAX_CHARS:
text = text[: _ACTIVE_TASK_MAX_CHARS - 15].rstrip() + " ...[truncated]"
return (
f"User asked (deterministic, from compacted turns): {text!r}\n"
"Historical only; newer protected-tail messages after this summary win."
)
return None

@classmethod
def _ground_historical_task_snapshot(
cls,
summary: str,
messages: List[Dict[str, Any]],
) -> str:
"""Force the task snapshot section to match a real user turn when possible."""
snapshot = cls._latest_user_task_snapshot(messages)
if not snapshot:
return summary

body = cls._strip_summary_prefix(summary)
# Keep the section terminated with a blank line: re.sub consumes the
# section's trailing newlines, and without restoring them the next
# "## " heading is glued onto the snapshot line — corrupting the
# markdown and making the heading invisible to this same regex on the
# next iterative compaction (which would then delete every following
# section via the \Z branch).
replacement = f"{HISTORICAL_TASK_HEADING}\n{snapshot}\n\n"
if _HISTORICAL_TASK_SECTION_RE.search(body):
grounded = _HISTORICAL_TASK_SECTION_RE.sub(
lambda _m: replacement, body, count=1
)
return grounded.strip()
return f"{replacement}{body}".strip()

@classmethod
def _find_latest_context_summary(
cls,
Expand Down
202 changes: 171 additions & 31 deletions agent/conversation_compression.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,43 +415,147 @@ def conversation_history_after_compression(agent: Any, messages: list) -> Option
return None


def _ensure_compressed_has_user_turn(original_messages: list, compressed: list) -> None:
"""Preserve a real user turn when a compressor returns assistant/tool-only context.

On repeated compaction the protected head decays to the system prompt only,
the middle summary can land as ``role="assistant"``, and a tool-heavy tail
can be all assistant/tool — so the compacted transcript can legitimately
contain zero user messages. Strict chat templates (LM Studio / llama.cpp
Jinja) then fail with "No user query found in messages" (#55677).

The restored turn is appended at the END: the guard only runs when
``compressed`` currently ends with an assistant/tool message (any existing
user turn — including a todo-snapshot append — short-circuits the
``any()`` check), so appending a user message never creates consecutive
same-role messages. ``_fresh_compaction_message_copy`` copies the message
and strips the ``_db_persisted`` marker so the rotation/in-place flush
still persists the restored row to the new session (#57491).

If the pre-compression transcript itself carried no user turn at all
(near-impossible — every real conversation opens with a user request —
but kept as a defensive backstop), a minimal continuation marker is
appended instead so strict templates still see a user message.
_SYNTHETIC_USER_PREFIXES = (
"[System: Your previous response was truncated",
"[System: The previous response was cut off",
"[System: Your previous tool call",
"[Your active task list was preserved across context compression]",
"[IMPORTANT: Background process ",
)


def _message_text(message: Any) -> str:
content = message.get("content") if isinstance(message, dict) else None
if isinstance(content, str):
return content
if isinstance(content, list):
return "\n".join(
str(part.get("text") or part.get("content") or "")
for part in content
if isinstance(part, dict)
)
return ""


_SYNTHETIC_USER_FLAGS = (
"_todo_snapshot_synthetic",
"_empty_recovery_synthetic",
"_verification_stop_synthetic",
"_pre_verify_synthetic",
)


def _is_real_user_message(message: Any) -> bool:
"""Distinguish human intent from user-role runtime scaffolding.

A compaction summary pinned to ``role="user"`` (the compressor flips the
summary role to preserve alternation when the tail starts with an
assistant message) is scaffolding too: treating it as human intent would
short-circuit anchor restoration with a message the model is explicitly
told NOT to act on.
"""
if any(isinstance(msg, dict) and msg.get("role") == "user" for msg in compressed):
return
from agent.context_compressor import _fresh_compaction_message_copy
if not isinstance(message, dict) or message.get("role") != "user":
return False
if any(message.get(flag) for flag in _SYNTHETIC_USER_FLAGS):
return False
text = _message_text(message).strip()
if not text:
return False
if text.startswith(_SYNTHETIC_USER_PREFIXES):
return False
from agent.context_compressor import ContextCompressor

return not ContextCompressor._is_context_summary_content(text)


def _merge_anchor_into_user_message(target: dict, anchor: dict) -> None:
"""Fold the human anchor into an existing user-role scaffolding turn.

Used only when every insertion slot would create two consecutive
user-role messages. The anchor text leads (it is the active task), the
scaffolding content is preserved after it, and the synthetic flags are
cleared because the merged turn now carries real human intent.
"""
anchor_content = anchor.get("content")
target_content = target.get("content")
if isinstance(anchor_content, list) or isinstance(target_content, list):
anchor_parts = (
list(anchor_content)
if isinstance(anchor_content, list)
else [{"type": "text", "text": str(anchor_content or "")}]
)
target_parts = (
list(target_content)
if isinstance(target_content, list)
else [{"type": "text", "text": str(target_content or "")}]
)
target["content"] = anchor_parts + target_parts
else:
merged = f"{anchor_content or ''}\n\n{target_content or ''}".strip()
target["content"] = merged
for flag in _SYNTHETIC_USER_FLAGS:
target.pop(flag, None)


def _insert_real_user_anchor(messages: list, anchor: dict) -> None:
"""Insert the latest human turn without breaking role alternation."""

for msg in reversed(original_messages):
if not isinstance(msg, dict) or msg.get("role") != "user":
def _role(msg: Any) -> Optional[str]:
return msg.get("role") if isinstance(msg, dict) else None

# Preferred: the summary boundary — before the first assistant message
# not already preceded by a user turn. The left neighbour is then
# non-user by construction and the right neighbour is an assistant.
for index, message in enumerate(messages):
if _role(message) != "assistant":
continue
compressed.append(_fresh_compaction_message_copy(msg))
previous_role = _role(messages[index - 1]) if index > 0 else None
if previous_role != "user":
messages.insert(index, anchor)
return
# Every assistant is user-preceded (or there are none). Appending is
# safe whenever the transcript does not already end with a user turn.
if not messages or _role(messages[-1]) != "user":
messages.append(anchor)
return
# The transcript ends with a user-role message and no slot avoids
# user/user adjacency.
from agent.context_compressor import ContextCompressor

if ContextCompressor._is_context_summary_content(
_message_text(messages[-1])
):
# Never merge into a compaction summary: the summary prefix must
# stay at the start of its message for downstream summary detection.
# Appending after it makes the anchor "the latest user message after
# the summary" — exactly what the handoff prefix instructs — and the
# adjacent user turns are merged summary-first by
# repair_message_sequence before the next API call.
messages.append(anchor)
return
# Trailing user-role scaffolding (e.g. the todo snapshot): merge instead
# of inserting a consecutive same-role message (#55677 strict templates).
_merge_anchor_into_user_message(messages[-1], anchor)


def _ensure_compressed_has_user_turn(original_messages: list, compressed: list) -> None:
"""Preserve human intent, not merely a synthetic user-role placeholder."""
if any(_is_real_user_message(message) for message in compressed):
return
from agent.context_compressor import _fresh_compaction_message_copy

for message in reversed(original_messages):
if _is_real_user_message(message):
_insert_real_user_anchor(
compressed,
_fresh_compaction_message_copy(message),
)
return
compressed.append({
"role": "user",
"content": (
"Continue from the compressed conversation context above. "
"This marker exists because the compacted transcript contained "
"no preserved user turn."
"This marker exists because no human user turn was available."
),
})

Expand Down Expand Up @@ -780,6 +884,25 @@ def _release_lock() -> None:
_release_lock()
return messages, _existing_sp

if not compressed:
logger.error(
"context compression returned an empty transcript; refusing to "
"rotate session=%s so the parent remains resumable",
agent.session_id or "none",
)
try:
agent._emit_warning(
"⚠ Compression returned an empty transcript. "
"No session split was performed; conversation continues unchanged."
)
except Exception:
pass
_existing_sp = getattr(agent, "_cached_system_prompt", None)
if not _existing_sp:
_existing_sp = agent._build_system_prompt(system_message)
_release_lock()
return messages, _existing_sp

try:
summary_error = getattr(agent.context_compressor, "_last_summary_error", None)
if summary_error:
Expand Down Expand Up @@ -809,7 +932,11 @@ def _release_lock() -> None:

todo_snapshot = agent._todo_store.format_for_injection()
if todo_snapshot:
compressed.append({"role": "user", "content": todo_snapshot})
compressed.append({
"role": "user",
"content": todo_snapshot,
"_todo_snapshot_synthetic": True,
})
_ensure_compressed_has_user_turn(messages, compressed)

agent._invalidate_system_prompt()
Expand Down Expand Up @@ -961,7 +1088,20 @@ def _release_lock() -> None:
# refresh the stored system prompt and reset the flush cursor so the
# next turn re-bases its append diff.
agent._session_db.update_system_prompt(agent.session_id, new_system_prompt)
agent._last_flushed_db_idx = 0
if in_place:
agent._last_flushed_db_idx = 0
else:
# A headless turn can be killed before its finalizer. Persist
# the rotated child's compacted handoff at the boundary so
# the new session is immediately resumable.
agent._session_db.replace_messages(agent.session_id, compressed)
agent._last_flushed_db_idx = len(compressed)
agent._flushed_db_message_session_id = agent.session_id
agent._flushed_db_message_ids = {
id(message)
for message in compressed
if isinstance(message, dict)
}
except Exception as e:
# If the rotation rolled back to the parent (orphan-avoidance
# above), agent.session_id is the still-indexed parent and
Expand Down
1 change: 1 addition & 0 deletions scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"antydizajn@gmail.com": "antydizajn", # PR #36043 salvage (auxiliary: route custom:<name> through named-provider arm + Palantir Bearer auth)
"252620095+briandevans@users.noreply.github.com": "briandevans", # PR #64951 salvage (lmstudio: clamp max/ultra reasoning effort)
"kar.iskakov@gmail.com": "karfly", # PR #64012 salvage (gateway: surface extended reasoning efforts)
"enzo.eliott.adami@gmail.com": "enzo-adami", # PR #66637 salvage (compression: preserve human intent and durable handoffs)
"kimyeon30@naver.com": "rlaehddus302", # PR #61985 salvage (gateway: secondary-adapter auth callback profile)
"burke@autreymail.com": "bautrey", # PR #66479 salvage (gateway reliability hardening: Bedrock liveness, supervised watchers, launchd respawn throttle)
"agungsubastian1963@gmail.com": "aguung", # PR #64461 salvage (gateway: multiplex secret_scope for authz/Slack/webhooks)
Expand Down
Loading
Loading