Skip to content

The agent that grows with you

feat(config): context_file_read_timeout key + narrow reader catch

kshitijk4poorcfe88a1f7dcb
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.py
index e5ea3f5d1cd..21ca9d8643b 100644
--- a/agent/prompt_builder.py
+++ b/agent/prompt_builder.py
@@ -59,13 +59,27 @@ logger = logging.getLogger(__name__)
from tools.threat_patterns import scan_for_threats as _scan_for_threats
-# Read deadline for context files (SOUL.md, AGENTS.md, .cursorrules, ...).
+# Default read deadline for context files (SOUL.md, AGENTS.md, .cursorrules,
+# ...); overridable via ``context_file_read_timeout`` in config.yaml.
# Intentionally short: network-backed filesystems (iCloud Drive, OneDrive,
# NFS) can fault-in an evicted file and block a cold read indefinitely, which
# stalls system-prompt assembly before the first turn.
_CONTEXT_FILE_READ_TIMEOUT_SECS = 5.0
+def _get_context_file_read_timeout() -> float:
+ """``context_file_read_timeout`` from config.yaml, else the 5s default."""
+ try:
+ from hermes_cli.config import load_config_readonly
+
+ val = load_config_readonly().get("context_file_read_timeout")
+ if isinstance(val, (int, float)) and val > 0:
+ return float(val)
+ except Exception as e:
+ logger.debug("Could not read context_file_read_timeout from config: %s", e)
+ return _CONTEXT_FILE_READ_TIMEOUT_SECS
+
+
def _read_text_with_timeout(path: Path, timeout: Optional[float] = None) -> Optional[str]:
"""``path.read_text()`` on a daemon thread so a slow file can't stall startup.
@@ -75,13 +89,13 @@ def _read_text_with_timeout(path: Path, timeout: Optional[float] = None) -> Opti
``try/except`` handling at each site is unchanged.
"""
if timeout is None:
- timeout = _CONTEXT_FILE_READ_TIMEOUT_SECS
+ timeout = _get_context_file_read_timeout()
result: "queue.Queue[tuple[bool, object]]" = queue.Queue(maxsize=1)
def _reader() -> None:
try:
result.put((True, path.read_text(encoding="utf-8")))
- except BaseException as exc: # re-raised on the caller thread
+ except Exception as exc: # re-raised on the caller thread
result.put((False, exc))
threading.Thread(target=_reader, daemon=True, name=f"context-read:{path.name}").start()
diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py
index 08ab1789e86..e55911a3685 100644
--- a/hermes_cli/config_defaults.py
+++ b/hermes_cli/config_defaults.py
@@ -754,6 +754,11 @@ DEFAULT_CONFIG = {
# and override the dynamic behavior. Separate from read_file tool limits.
"context_file_max_chars": None,
+ # Seconds to wait for a single context file read before skipping it with a
+ # warning. Guards startup against network-backed filesystems (iCloud Drive,
+ # OneDrive, NFS) that can block a cold read on an evicted file.
+ "context_file_read_timeout": 5.0,
+
# Maximum characters returned by a single read_file call. Reads that
# exceed this are rejected with guidance to use offset+limit.
# 100K chars ≈ 25–35K tokens across typical tokenisers.
diff --git a/tests/agent/test_prompt_builder.py b/tests/agent/test_prompt_builder.py
index d580e4195c8..116db36f691 100644
--- a/tests/agent/test_prompt_builder.py
+++ b/tests/agent/test_prompt_builder.py
@@ -1153,7 +1153,7 @@ class TestContextFileReadTimeout:
# over: an earlier test re-imports agent.prompt_builder, so the
# sys.modules entry can be a different module object.
pb_mod = sys.modules[build_context_files_prompt.__module__]
- monkeypatch.setattr(pb_mod, "_CONTEXT_FILE_READ_TIMEOUT_SECS", 0.05)
+ monkeypatch.setattr(pb_mod, "_get_context_file_read_timeout", lambda: 0.05)
original_read_text = Path.read_text
diff --git a/tests/agent/test_subdirectory_hints.py b/tests/agent/test_subdirectory_hints.py
index 2fe6e125d42..3b1e8b69fbe 100644
--- a/tests/agent/test_subdirectory_hints.py
+++ b/tests/agent/test_subdirectory_hints.py
@@ -128,7 +128,7 @@ class TestSubdirectoryHintTracker:
# Patch the module object the hint tracker's helper closes over.
pb_mod = sys.modules[sh_mod._read_text_with_timeout.__module__]
- monkeypatch.setattr(pb_mod, "_CONTEXT_FILE_READ_TIMEOUT_SECS", 0.05)
+ monkeypatch.setattr(pb_mod, "_get_context_file_read_timeout", lambda: 0.05)
original_read_text = Path.read_text
diff --git a/website/docs/user-guide/features/context-files.md b/website/docs/user-guide/features/context-files.md
index b5c628213d4..2906c4f780b 100644
--- a/website/docs/user-guide/features/context-files.md
+++ b/website/docs/user-guide/features/context-files.md
@@ -190,6 +190,7 @@ This scanner protects against common injection patterns, but it's not a substitu
| Limit | Value |
|-------|-------|
| Max chars per file | `context_file_max_chars` when set; otherwise dynamic (scales with model context window, floor 20,000, ceiling 500,000) |
+| Read timeout per file | `context_file_read_timeout` (default 5 seconds); a file that takes longer to read — e.g. on iCloud Drive, OneDrive or NFS — is skipped with a warning |
| Head truncation ratio | 70% |
| Tail truncation ratio | 20% |
| Truncation marker | 10% (shows char counts and suggests using file tools) |