Skip to content

Core context condensation implementation - #61

Merged
csmith49 merged 18 commits into
mainfrom
feat/condenser-core
Sep 3, 2025
Merged

csmith49 merged 18 commits into
mainfrom
feat/condenser-core

Conversation

@csmith49

@csmith49 csmith49 commented Sep 2, 2025 •

Copy link
Copy Markdown
Contributor

This PR ports over the core of the condensation logic from the OSS repo.

This PR adds:

  1. Condensation and CondensationRequest events.
  2. The View class and associated tests.
  3. The Condenser and RollingCondenser base classes.
  4. A trivial NoOpCondenser for testing/placeholder purposes.
  5. Condensation logic in CodeActAgent.step.

This represents a complete solution for #56 and most of a solution for #55.

Some changes made from the source:

  1. Condensation objects don't track start/stop IDs, just a list of IDs to forget. New ID structure is UUID-based and so not range(...)-able like the integers of old.
  2. Condensers return View | Condensation, but the Condensation is an actual event and not a shallow wrapper around an event.
  3. Much of the State-based metadata tracking has been removed. IIRC it was only ever used for experiments but there are easier ways now (especially with the cleaner logging).
  4. There's currently no configuration. Will tackle that when specific condensers are ported over.

@enyst

enyst commented Sep 2, 2025

Copy link
Copy Markdown
Member

Condensation objects don't track start/stop IDs, just a list of IDs to forget

👍 I don’t see a reason not to.

I do think it’s worth thinking about designing condensation in such way that per tokens is easy/possible.

V1 seems like a good time to consider if / how we could switch to make per tokens the primary way, if not the only way. We now know what works and how (summaries, browser mask), and I do think the ability to set an approximate token limit is important. Just now we’re looking at two SOTA LLMs, Sonnet-4 and GPT-5, which have different context window limits and likely different ‘tolerance’ at different levels.

Condensation per tokens has somehow become kinda difficult or too weird to do, in OpenHands V0. I think maybe we can try to think about either

  • design / understand at least, if not implement per tokens, first, or
  • make sure that the serialization/execution flow/LLM/condenser implementation can support per tokens…

What do you think?

@csmith49

csmith49 commented Sep 2, 2025

Copy link
Copy Markdown
Contributor Author

Condensation per tokens has somehow become kinda difficult or too weird to do, in OpenHands V0. I think maybe we can try to think about either

  • design / understand at least, if not implement per tokens, first, or
  • make sure that the serialization/execution flow/LLM/condenser implementation can support per tokens…

I think the execution flow from v0 was actually amenable to this kind of token-first condensation, so I've ported it over mostly unchanged. The real challenge in v0 was in getting good per-token estimates for each event, which was made tricky by:

  1. Events being transformed into messages in one big context-sensitive pass,
  2. Unclear mapping between messages and events via IDs, and
  3. No reliable access to tokenizers.

Assuming we've got a decent way to get tokens per event in v1 it's trivial to make a condenser that goes by token count: in RollingCondenser.should_condense you just count the tokens on the input events and say True if they get to some (model-specific?) threshold.

@csmith49
csmith49 marked this pull request as ready for review September 2, 2025 17:13

# The relevant summary is always in the last condensation event (i.e., the most recent one).
for event in reversed(events):
if isinstance(event, Condensation):

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.

If I understand correctly the status in V1, we continue to have a store for events, and another for ConversationState. Could we think about moving the Condensation event to state?

The difference should be (is?) that what is stored in state can change, from a session restore to another. While the events, I think, will continue to not change.

If I switch from Sonnet-4 (200k or what context it has now) to GPT-5 (400k), or the other way around, then the old condensation event will not “match”. It seems the natural behavior of the software would be to recompute and replace it, save the new one for next time. 🤔 Granted, with events it would add another, so it should work too. What do you think makes sense, or is this a matter of choice/preference?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think there's multiple ways to engineer this, but my preference would be to keep the condensation information in the events. Two main reasons:

First, for experiments, debugging, and anything else that wants reproducibility, I think we want to be storing all condensations -- when they happen, which events they touch, and what their resulting summary is (if anything). That's possible to store in the state but we'd need to sync the condensations with the events anyways and that's unnecessary complexity (both during implementation and trying to piece together the history of a run after the fact).

Second, the condensation events have a huge impact on what the agent sees and consequently what action it takes next. I think conceptually it fits with the other events in the state.

If I switch from Sonnet-4 (200k or what context it has now) to GPT-5 (400k), or the other way around, then the old condensation event will not “match”. It seems the natural behavior of the software would be to recompute and replace it, save the new one for next time. 🤔 Granted, with events it would add another, so it should work too. What do you think makes sense, or is this a matter of choice/preference?

Switching from the larger context model to the smaller context model shouldn't be a problem, we'd run into a context window error and condense immediately. Going the other way does mean the condensation isn't "tuned" for that particular model, but recomputing also seems strange to me: the agent has already taken some actions conditioned on the old summaries. Either way you can't get a true roll-out of the new model.

The difference should be (is?) that what is stored in state can change, from a session restore to another. While the events, I think, will continue to not change.

I like this framing and we'd probably be well-served trying to codify these assumptions. I recall at one point we were recomputing condensations when loading old sessions because the history kept getting off-track with the stored view and it took us ages to figure out -- the fix was shoving all this info in the append-only history.

Honestly, I'd be in favor of taking this even further and lifting certain state-changes up to events. Why not have an event that records when a model was changed or some tool was added?

@csmith49
csmith49 requested a review from xingyaoww September 2, 2025 18:52

@xingyaoww xingyaoww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add an examples/agent_with_condenser.py to illustrate how we can use condenser in an agent loop?

@csmith49

csmith49 commented Sep 3, 2025

Copy link
Copy Markdown
Contributor Author

I can, but until I add the other implementations it won't actually do any condensation. Are you okay with me adding that example in the next PR?

@xingyaoww

Copy link
Copy Markdown
Contributor

@csmith49 ahh in that case let's defer it to future PRs

@openhands-ai

openhands-ai Bot commented Sep 3, 2025

Copy link
Copy Markdown

Looks like there are a few issues preventing this PR from being merged!

  • GitHub Actions are failing:
    • Pre-commit checks

If you'd like me to help, just leave a comment, like

@OpenHands please fix the failing actions on PR #61 at branch `feat/condenser-core`

Feel free to include any additional details that might help me get this PR into a better state.

You can manage your notification settings

@xingyaoww xingyaoww left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few nit, otherwise LGTM. Feel free to merge directly when you've addressed them

Comment thread openhands/sdk/agent/agent/agent.py Outdated
llm_convertible_events = condensation_result.events

case Condensation():
state.events.append(condensation_result)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the .append should be handled by the on_event callback now - so no need to re-append

| CondensationRequest
)


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You want to add these two to __all__?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i kinda want to rename this condenser.py

@csmith49
csmith49 merged commit ab89807 into main Sep 3, 2025
@csmith49
csmith49 deleted the feat/condenser-core branch September 3, 2025 18:06
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.

3 participants