Skip to content

fix(condenser): never forget the leading SystemPromptEvent (#5148) - #5241

Merged
juanmichelini merged 2 commits into
mainfrom
fix/5148-condenser-keep-first-system
Sep 26, 2026
Merged

juanmichelini merged 2 commits into
mainfrom
fix/5148-condenser-keep-first-system

Conversation

@juanmichelini

@juanmichelini juanmichelini commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

HUMAN:

LLM summarizing condenser could drop the leading SystemPromptEvent when summarizing; keep it so the system turn always leads.


AGENT:

Why

When the LLMSummarizingCondenser collapsed history into a summary, it could omit a leading SystemPromptEvent, so the condensed view no longer started with a system message. That violates the repo-wide invariant that every LLM request must open with a system message before the first user message.

Summary

  • openhands/sdk/context/condenser/llm_summarizing_condenser.py: when producing the condensed event list, the condenser now always preserves a leading SystemPromptEvent (re-prepending it if the summary displaced it).

Issue Number

Fixes #5148

How to Test

Run uv run pytest tests/sdk/context/condenser/test_llm_summarizing_condenser.py -q. The added test feeds history whose first event is a SystemPromptEvent, forces a summary, and asserts the condensed view still opens with that system event. All 45 tests pass.

This PR was created by an AI agent (OpenHands) on behalf of the user.

Type

  • Bug fix

🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)

• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server

Variants & Base Images

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:5e30669-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-5e30669-python \
  ghcr.io/openhands/agent-server:5e30669-python

All tags pushed for this build

ghcr.io/openhands/agent-server:5e30669-golang-amd64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-golang-amd64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-golang-amd64
ghcr.io/openhands/agent-server:5e30669-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:5e30669-golang-arm64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-golang-arm64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-golang-arm64
ghcr.io/openhands/agent-server:5e30669-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:5e30669-java-amd64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-java-amd64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-java-amd64
ghcr.io/openhands/agent-server:5e30669-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:5e30669-java-arm64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-java-arm64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-java-arm64
ghcr.io/openhands/agent-server:5e30669-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:5e30669-python-amd64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-amd64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-amd64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-amd64
ghcr.io/openhands/agent-server:5e30669-python-arm64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-arm64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-arm64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-arm64
ghcr.io/openhands/agent-server:5e30669-python-minimal-amd64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-minimal-amd64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-minimal-amd64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:5e30669-python-minimal-arm64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-minimal-arm64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-minimal-arm64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:5e30669-python-slim-amd64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-slim-amd64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-slim-amd64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:5e30669-python-slim-arm64
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-slim-arm64
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-slim-arm64
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:5e30669-golang
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-golang
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-golang
ghcr.io/openhands/agent-server:5e30669-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:5e30669-java
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-java
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-java
ghcr.io/openhands/agent-server:5e30669-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:5e30669-python-minimal
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-minimal
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-minimal
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-minimal
ghcr.io/openhands/agent-server:5e30669-python-slim
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python-slim
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python-slim
ghcr.io/openhands/agent-server:5e30669-python-node-runtime-slim
ghcr.io/openhands/agent-server:5e30669-python
ghcr.io/openhands/agent-server:5e3066959bbd11ebffa4fc9aa59f4fcbb863e379-python
ghcr.io/openhands/agent-server:fix-5148-condenser-keep-first-system-python
ghcr.io/openhands/agent-server:5e30669-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 5e30669-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 5e30669-python-amd64) are also available if needed

`LLMSummarizingCondenser._get_forgotten_events()` computed the start of
the forgetting range from ``keep_first`` alone. With ``keep_first=0`` the
leading ``SystemPromptEvent`` could be forgotten, leaving the condensed
view opening with a non-system message and violating the repo-wide
invariant that every LLM request must open with a system message before
the first user message.

Floor the forgetting range start just past the leading
``SystemPromptEvent`` (via a small ``_leading_system_prompt_index``
helper), so it is never included in the forgotten set. This also covers
the ``keep_first=0`` edge case.

Part of #5144. Regression test added asserting ``keep_first=0`` keeps
the system message first and does not forget the system prompt event.

Co-authored-by: openhands <openhands@all-hands.dev>
@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-sdk/openhands/sdk/context/condenser
   llm_summarizing_condenser.py1922985%363, 366–367, 370–371, 376, 381, 383–384, 397–398, 474–475, 480, 488, 504–505, 507–509, 514–518, 521, 525, 527–528
TOTAL432111199472% 

@github-actions

github-actions Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@all-hands-bot

Copy link
Copy Markdown
Collaborator

⚠️ Review paused: current-head checks failed

The current head 2ab84424ed7b has failing checks, so no review conversation was started:

  • Validate PR description
  • pre-commit

Fix the checks above and push. Then remove the outstanding all-hands-bot request and request all-hands-bot again: GitHub will not accept a second request while the first is still outstanding. The review starts on the updated head.

This is an automated check - no AI was used to generate this comment.

1 similar comment
@all-hands-bot

Copy link
Copy Markdown
Collaborator

⚠️ Review paused: current-head checks failed

The current head 2ab84424ed7b has failing checks, so no review conversation was started:

  • Validate PR description
  • pre-commit

Fix the checks above and push. Then remove the outstanding all-hands-bot request and request all-hands-bot again: GitHub will not accept a second request while the first is still outstanding. The review starts on the updated head.

This is an automated check - no AI was used to generate this comment.

Co-authored-by: openhands <openhands@all-hands.dev>
@neubig
neubig requested review from all-hands-bot and removed request for all-hands-bot September 23, 2026 22:44

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Assessment

The change fixes the linked issue #5148 in the normal condensation path. _get_forgotten_events now floors the forgetting range just past the leading SystemPromptEvent, so a keep_first of 0 (or any value that would let the system prompt be condensed) can no longer drop it.

I verified the fix on the current head beyond the added unit test:

  • Rebuilt the view through the real consumption path (View.from_events on the full stream plus the Condensation) for keep_first in {0, 1, 2}. In every case the projected messages start with system, summary_offset lands at 1/1/2, and the system prompt's id is not in forgotten_event_ids. Before the fix, keep_first=0 produced a view opening with the user-role summary.
  • Ran the focused suite (45 passed) and the whole tests/sdk/context tree (441 passed), plus ruff check/ruff format --check on both changed files.
  • All current-head GitHub Actions checks are green (the earlier Validate PR description failure was superseded by a later successful run for the same check name on this SHA).

The scope is right for this repository: the change lives in the SDK condenser it fixes, adds no public API surface (the helper is module-private, underscore-prefixed), and alters no serialized event or persisted schema, so there is no compatibility concern. I found no security or lifecycle issue.

Related follow-up (tracked separately, not a blocker here)

The hard_context_reset path in the same class still summarizes view.events in full with summary_offset=0. I reproduced that with a leading system prompt and keep_first=0: all events are forgotten and the resulting view opens with a user-role message. That is issue #5149, which is open and carries its own ready-for-dev label, so it is correctly out of scope for this PR; the new test-comment header references #5148/#5149 while only the #5148 path is actually exercised, so the "never forget" wording is broader than what this change covers.

✅ APPROVED

@neubig neubig left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This restores the system-first invariant in ordinary condensation, including keep_first=0, with focused regression coverage and passing CI. The change is complementary to #5242; whichever merges second should rebase and deduplicate the shared helper/test utilities.

This review was generated by an AI agent (OpenHands) on behalf of @neubig.

@juanmichelini
juanmichelini merged commit d340af2 into main Sep 26, 2026
59 of 61 checks passed
@juanmichelini
juanmichelini deleted the fix/5148-condenser-keep-first-system branch September 26, 2026 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

condenser: ensure the SystemPromptEvent is never forgotten (keep_first=0 / summary-first edge case)

4 participants