fix(cli): restore session cwd on resume (-c) - #38256
Closed
MorAlekss wants to merge 1 commit into
Closed
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Restores the session working directory when resuming a CLI session with -c.
Root cause
_init_agent()incli.pyloaded conversation history and reopened the session in the DB but never restored the working directory. Thesessionstable already has acwdcolumn andupdate_session_cwd()exists, but the CLI path never wrote to or read from it, only the TUI gateway did.Behavioral change
Before:
hermes -c/hermes --resume <id>restores conversation history but starts in the launch directory, ignoring anycdthe agent performed during the previous session.After: the working directory is restored to where the previous session left off.
What changed
tools/terminal_tool.pyget_live_terminal_cwd(task_id), a public getter for the terminal environment's liveenv.cwd. Symmetric with the existingregister_task_env_overridesandclear_task_env_overridespair.run_agent.py_ensure_db_session(): passescwd=os.getcwd()tocreate_session()so the initial working directory is persisted on session creation._persist_session(): callsupdate_session_cwd()with the live terminal cwd after every turn so anycdperformed during the session is persisted for the next resume.cli.py_init_agent()resume block: readssession_meta["cwd"]and callsos.chdir()to restore the working directory before the agent starts. Follows design fix(cli): CLI/TUI on local backend always uses launch directory, ignores terminal.cwd #19242: local CLI backend does not touchTERMINAL_CWD, usesos.chdir()instead.tests/cli/test_cli_resume_cwd.pyTestResumeCwdRestore: canary test that fails without the fix, guard for NULL cwd in pre-fix session rows, guard for deleted directory that must not crash.What is NOT changed
tui_gateway/server.py.TERMINAL_CWDis never modified, the local backend contract (fix(cli): CLI/TUI on local backend always uses launch directory, ignores terminal.cwd #19242) is preserved.Implements the fix discussed in Discord (May 31) following a user report about the agent losing its working directory after resume. @teknium1 noted: "this is a good point. I'll see if I can make resume and -c reload with the last working dir it was in."