Hi, I am trying to implement a feature that restor...
# questions
f
Hi, I am trying to implement a feature that restores a previous trajectory and feeds it to the agent, so its first action is conditioned on that trajectory. The goal is to modify/remove parts of the restored trajectory and observe whether this alters the agent’s behaviour. From a previous discussion https://openhands-ai.slack.com/archives/C06U8UTKSAD/p1753443486468809 I learned that
replay
is not the right feature, since it does not condition the agent on the replayed trajectory and only re executes it. My implementation loads messages from a previous trajectory into
params['messages']
and updates
state.history
accordingly.
Copy code
replay_events, initial_user_action = load_replay_log(
    self.cfg.restore_trajectory_path
)

restored_history = replay_events
state.history = restored_history

replay_events.insert(
    0, initial_user_action
)  # Replay events does not include the first action we need to add it again
restored_messages = self._get_messages(
    replay_events, initial_user_message
)
params['messages'] = self.llm.format_messages_for_llm(restored_messages)
After the final iteration of
step(self, state: State)
,
state.history
contains both the restored trajectory and the new actions. However, at the end of
run_controller
https://github.com/All-Hands-AI/OpenHands/blob/0297b3da18bdaf64233641cc1b48900c3328cc3a/openhands/core/main.py#L230
controller.get_state()
is called, which clears the history https://github.com/All-Hands-AI/OpenHands/blob/0297b3da18bdaf64233641cc1b48900c3328cc3a/openhands/controller/state/state.py#L194 The comment states that history is restored from event stream. I checked the event stream before the agent controller is closed, and it only contains the new actions, not the restored trajectory. How can I add the restored trajectory to the event stream, or what would be the recommended approach? Thanks!
c
Could you please help me understand why is replay not the right feature? Replaying the trajectory would restore the environment to the state it was before, and save the events in the stream, and have them in agent’s history… 🤔
👍 1
If you only give the agent the messages (of the previous run), then they weren’t actually executed in the new run
They can be in context, sure, when going to the LLM, but if they weren’t re-executed, then… well, they are not “real” events this run. What you could do, if you want, is to create an event with the old trajectory
(if you’re sure you don’t want to re-execute)
f
Replaying the trajectory does not put it in the context of the LLM, right? Our idea is that the LLM has consumed that entire restored trajectory, so "knows" about it, i.e. conditioned on it, and then makes the first action based on this
c
It does, though… Doesn’t that work? When the replay finished, • events should exist • agent.history should know them • the actions were re-executed in the environment, and the new observations were also added (in stream and history) So next time the agent performs an action, it should send to the LLM the whole previous context
f
Mmh, so here I mentioned that it didn't seem to work for me https://openhands-ai.slack.com/archives/C06U8UTKSAD/p1753443486468809 Maybe I'm doing something wrong
c
🤔
Where you say that the “first message” doesn’t include the previous trajectory, you mean the entire context sent to the LLM just after replay, doesn’t include previous messages?
It sounds like there’s a bug, because it should… 🤔
f
If I remember correctly, I checked
state.history
and observed that while the restored trajectory is represented in the history, the first action after the replay was independent of what happened before, i.e. it was a response to the initial prompt, not taking the trajectory into account. Then I checked what is actually passed to the model by inspecting
params['messages']
and did not see the messages from the restored trajectory there
c
May I suggest to make a github issue? It would be great if you could • make a new trajectory with whatever you like, that you can share • save it; restart with replay; replay it • and send another message to the agent • save it / stop the run. Both of those json should show us what is missing. Thinking about it, I actually guess we might not have updated replay for the “system message action” that OpenHands now has, since we have made it an event… 🤔
👍 2
If so, it might be easy fix, I don’t know though
That said, pending that. If you wish to make it work as you did above, you could add the trajectory contents, in an event
instead of in params[‘messages’]
m
It would be much better if you could fix an existing feature (that might have broken at some time) rather than create a new one that has the same goal
nod 1
f
@calm-artist-7206 I tried to reproduce the behaviour I thought I saw, but couldn't. So it must have been an error on my side, my apologies. Thank you for the quick answer. I understand the feature much better now.
🙏 1
c
Great to hear, thank you!
🙏 1