Skip to content

fix(terminal): guard os.getcwd() against deleted CWD in _get_env_config - #33377

Closed
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/terminal-cleanup-getcwd-filenotfound
Closed

fix(terminal): guard os.getcwd() against deleted CWD in _get_env_config#33377
liuhao1024 wants to merge 1 commit into
NousResearch:mainfrom
liuhao1024:fix/terminal-cleanup-getcwd-filenotfound

Conversation

@liuhao1024

@liuhao1024 liuhao1024 commented May 27, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

_get_env_config() calls os.getcwd() unconditionally for the local backend. When the process's current working directory has been deleted (common on Arch Linux where /tmp is tmpfs and stale namespaces are reaped aggressively), this raises FileNotFoundError.

The cleanup thread calls _get_env_config() every 60 seconds. Although the exception is caught by the thread's except Exception handler, it logs a full traceback to errors.log on every tick — creating a recurring, noisy warning that never resolves.

Related Issue

Fixes #33367

Type of Change

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

Changes Made

  • tools/terminal_tool.py: Guard os.getcwd() against deleted CWD
  • tests/tools/test_terminal_cleanup_cwd.py: Regression tests for FileNotFoundError and OSError

How to Test

  1. Run pytest tests/ -q — all tests should pass
  2. Verify the specific scenario described above is resolved

Checklist

Code

  • I've read the Contributing Guide
  • My commit messages follow Conventional Commits (fix(scope):, feat(scope):, etc.)
  • I searched for existing PRs to make sure this isn't a duplicate
  • My PR contains only changes related to this fix/feature (no unrelated commits)
  • I've run pytest tests/ -q and all tests pass
  • I've added tests for my changes (required for bug fixes, strongly encouraged for features)
  • I've tested on my platform: macOS 26.4.1

Documentation & Housekeeping

  • I've updated relevant documentation (README, docs/, docstrings) — or N/A
  • I've updated cli-config.yaml.example if I added/changed config keys — or N/A
  • I've updated CONTRIBUTING.md or AGENTS.md if I changed architecture and workflows — or N/A
  • I've considered cross-platform impact (Windows, macOS) per the compatibility guide — or N/A

Code Intelligence

  • Analyzed: Function:tools/terminal_tool.py:_get_env_config (callers: 10+ including terminal_tool, file_tools, code_execution_tool, cleanup_thread_worker, prompt_builder)
  • Blast radius: LOW — single line change with safe fallback; all callers benefit from the guard
  • Related patterns: os.getcwd() is also called at line 978 (docker CWD passthrough) but only when env_type == "docker", which is not the affected code path

When the process's current working directory is deleted (e.g. tmpfs
cleanup on Arch Linux), os.getcwd() raises FileNotFoundError.  The
cleanup thread calls _get_env_config() every 60 seconds, so this error
repeats indefinitely in errors.log even though the exception is caught.

Wrap os.getcwd() in a try/except and fall back to the home directory.
Add regression tests covering both FileNotFoundError and OSError.

Fixes NousResearch#33367
@alt-glitch alt-glitch added type/bug Something isn't working tool/terminal Terminal execution and process management backend/local Local shell execution P2 Medium — degraded but workaround exists labels May 27, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to open #4982 and #10233 (same os.getcwd() guard for cli.py), and merged #19933 (same guard for process_registry.py). This PR targets a different call site (tools/terminal_tool.py:_get_env_config) — complementary fix, not a duplicate.

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the focused report and regression coverage. This is now implemented on main.

  • Automated hermes-sweeper review verified tools/terminal_tool.py:11961207: _safe_getcwd() handles a deleted CWD and falls back to TERMINAL_CWD or the home directory.
  • _get_env_config() uses that helper for the local backend at tools/terminal_tool.py:1294.
  • Regression coverage is present at tests/tools/test_terminal_task_cwd.py:231247.
  • The implementation landed in ad69d3edc7359310233fa789b300bfcc5a5c3f7a and is included in v2026.6.5.

The current implementation also routes the Docker cwd-passthrough and debug-config call sites through the same helper.

@teknium1 teknium1 closed this Jul 13, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend/local Local shell execution P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main tool/terminal Terminal execution and process management type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: terminal_tool cleanup thread raises recurring FileNotFoundError in _get_env_config

3 participants