Skip to content

The agent that grows with you

fix(gateway): defer context files until active turn

KoNit-K87661da31451
diff --git a/gateway/run.py b/gateway/run.py
index 989494b0ba8..93365348cb3 100644
--- a/gateway/run.py
+++ b/gateway/run.py
@@ -944,17 +944,12 @@ def _warm_turn_machinery_sync() -> int:
"""Synchronously initialize first-turn prerequisites (executor thread); returns the schema count.
Covers the lazy init seen in skeleton turns: ``run_agent`` import graph, tool schemas (+ ``check_fn``
- TTL cache), context files, the local Python toolchain probe (#106064)."""
+ TTL cache), and the local Python toolchain probe (#106064). Context files remain lazy because they
+ need the active turn's agent and model context."""
import run_agent # noqa: F401 # heavy import graph, cached in sys.modules
import model_tools
tool_defs = model_tools.get_tool_definitions(quiet_mode=True)
- try:
- from agent.prompt_builder import build_context_files_prompt
-
- build_context_files_prompt()
- except Exception:
- logger.debug("context-file warm-up failed (non-fatal)", exc_info=True)
from hermes_cli.config import load_config_readonly
agent_cfg = load_config_readonly().get("agent")
diff --git a/tests/gateway/test_startup_environment_probe.py b/tests/gateway/test_startup_environment_probe.py
index 4c0b18b9e52..60d395ba0dd 100644
--- a/tests/gateway/test_startup_environment_probe.py
+++ b/tests/gateway/test_startup_environment_probe.py
@@ -2,6 +2,7 @@
import pytest
+from agent import prompt_builder
import model_tools
from gateway import run as gateway_run
from tools import env_probe
@@ -37,6 +38,16 @@ def test_warmup_leaves_probe_cached_for_first_prompt(tmp_path, monkeypatch):
assert calls == [1] # single worker; the first turn reuses the cache
+def test_warmup_does_not_build_context_files_without_turn_context(tmp_path, monkeypatch):
+ monkeypatch.setattr(
+ prompt_builder,
+ "build_context_files_prompt",
+ lambda: pytest.fail("gateway warm-up must not build context files"),
+ )
+
+ _warm(tmp_path, monkeypatch, {}, "local")
+
+
@pytest.mark.parametrize("agent_section,backend", [({"environment_probe": False}, "local"), ({}, "ssh")])
def test_warmup_skips_probe_when_disabled_or_remote(tmp_path, monkeypatch, agent_section, backend):
monkeypatch.setattr(env_probe, "_build_probe_line", lambda: pytest.fail("host inspected"))