Skip to content

fix(core): save agent state on user interrupt to prevent session loss#1970

Open
Buktal wants to merge 2 commits into
agentscope-ai:mainfrom
Buktal:fix/interrupt-state-save
Open

fix(core): save agent state on user interrupt to prevent session loss#1970
Buktal wants to merge 2 commits into
agentscope-ai:mainfrom
Buktal:fix/interrupt-state-save

Conversation

@Buktal

@Buktal Buktal commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.0-SNAPSHOT

Description

When a user interrupt (InterruptedException) occurs during ReActAgent execution,
saveStateToSession is skipped because the flatMap chain in doCall() never fires
after the exception. This causes all in-memory session changes (user input, partial
agent replies) to be lost if the process is restarted or the session migrates to
another instance.

The fix adds saveStateToSession(scope) in handleInterrupt()'s USER branch, after
the recovery message is appended to context. If persistence fails (e.g. network
timeout), the error is logged and the user still receives the recovery message.

Changes:

  • ReActAgent.java: USER interrupt branch now saves agent state before returning the
    recovery message, with onErrorResume fallback

Closes #1968

Checklist

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test — 2177 tests, 0 failures)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

When a user interrupt (InterruptedException) occurs during ReActAgent execution,
saveStateToSession is skipped because the flatMap chain in doCall() never fires.
This causes the in-memory session changes to be lost on restart.

The fix adds saveStateToSession in handleInterrupt's USER branch, after the
recovery message is appended, with an onErrorResume fallback to ensure the user
still receives the recovery message even if persistence fails.

Closes agentscope-ai#1968
@Buktal Buktal requested a review from a team June 30, 2026 07:16
HarnessAgentTest.runtimeContextConcreteFilesystemSurvivesSessionDefaulting
fails intermittently when JUnit can't delete @tempdir because the OS hasn't
released file handles. Pre-deleting child files before JUnit's own cleanup
reduces the race condition window.
@codecov

codecov Bot commented Jun 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 0.00% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@itxaiohanglover itxaiohanglover 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.

Clean fix — saving state before returning the recovery message prevents session loss on interrupt. The onErrorResume fallback to log and continue is the right call; a failed save shouldn't block the user. The temp dir cleanup in tests is a nice addition.

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.

[Bug]: Session State Not Saved After Interruption

2 participants