Skip to content

fix: guard os.getcwd() against deleted CWD (FileNotFoundError)#4982

Open
lbruceolson wants to merge 1 commit into
NousResearch:mainfrom
lbruceolson:fix/safe-getcwd-deleted-cwd
Open

fix: guard os.getcwd() against deleted CWD (FileNotFoundError)#4982
lbruceolson wants to merge 1 commit into
NousResearch:mainfrom
lbruceolson:fix/safe-getcwd-deleted-cwd

Conversation

@lbruceolson

Copy link
Copy Markdown

What does this PR do?

Python's os.getcwd() raises FileNotFoundError when the shell's current working directory no longer exists on disk. This happens when a terminal (e.g. Warp) is open in a directory that gets deleted or renamed while the pane is still sitting in it. The result is hermes crashing before it can even display the banner.

This PR adds a _safe_getcwd() helper that wraps os.getcwd() in a try/except FileNotFoundError and falls back to the user's home directory (~). All six bare os.getcwd() call sites in cli.py are replaced with _safe_getcwd():

  • load_cli_config() — initial CWD resolution
    • HermesCLI.show_banner() — banner display
    • HermesCLI checkpoint command handler
    • HermesCLI._show_config()/config display
    • HermesCLI.run() tools-refresh path
    • preprocess_context_references() call — context file expansion

Related Issue

No existing issue. Discovered during normal use with the Warp terminal on Linux.

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)

Reproducer

  1. Open a terminal pane in any directory (e.g. /tmp/test-dir)
    1. Delete that directory from another shell: rm -rf /tmp/test-dir
    1. Launch hermes from the original pane
    1. Without this fix: FileNotFoundError: [Errno 2] No such file or directory
    1. With this fix: hermes starts normally, CWD falls back to ~

Terminals like Warp aggressively track the CWD in each pane. If a
directory is deleted or renamed while a pane is sitting in it, the
shell's CWD pointer becomes stale. Python's os.getcwd() then raises
FileNotFoundError on the next launch, crashing hermes before it can
even show the banner.

Add a _safe_getcwd() helper that catches FileNotFoundError and falls
back to the user's home directory. Replace all six bare os.getcwd()
call sites in cli.py with _safe_getcwd():

- load_cli_config()         – initial CWD resolution
- HermesCLI.show_banner()   – banner display
- HermesCLI (checkpoint cmd) – checkpoint command handler
- HermesCLI._show_config()  – /config display
- HermesCLI.run() (refresh) – tools-refresh path
- preprocess_context_references() call – context file expansion
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #10233 — same os.getcwd() FileNotFoundError guard in CLI startup.

1 similar comment
@alt-glitch

Copy link
Copy Markdown
Collaborator

Likely duplicate of #10233 — same os.getcwd() FileNotFoundError guard in CLI startup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants