Skip to content

The agent that grows with you

fix(gateway): a following session drops the coding posture when its profile leaves focus

kshitijk4poorc4e4b5b3b569
diff --git a/agent/coding_context.py b/agent/coding_context.py
index 840e5e04cf5..15cb3beb23a 100644
--- a/agent/coding_context.py
+++ b/agent/coding_context.py
@@ -289,12 +289,15 @@ def _enabled_mcp_servers(config: Optional[dict[str, Any]]) -> list[str]:
return []
+def _posture_toolsets(config: Optional[dict[str, Any]]) -> list[str]:
+ """The coding posture's toolset list; callers own the ``focus`` check."""
+ return [CODING_TOOLSET, *_enabled_mcp_servers(config)]
+
+
def focus_toolsets(config: Optional[dict[str, Any]]) -> Optional[list[str]]:
"""The coding posture's toolsets when ``agent.coding_context`` is ``focus``, else None, for a
session whose workspace decision is already made (no cwd probe)."""
- if _coding_mode(config) != "focus":
- return None
- return [CODING_TOOLSET, *_enabled_mcp_servers(config)]
+ return _posture_toolsets(config) if _coding_mode(config) == "focus" else None
# ── RuntimeMode (the seam) ──────────────────────────────────────────────────
@@ -327,7 +330,7 @@ class RuntimeMode:
apply it only when the user hasn't pinned a selection (``--toolsets``, ``HERMES_TUI_TOOLSETS``)."""
if self.config_mode != "focus" or self.profile.toolset is None:
return None
- return focus_toolsets(config) # the one builder of the posture's list
+ return _posture_toolsets(config) # the focus decision is this mode's own config_mode
def system_prompt_parts(self, valid_tool_names=None, workspace_block: Optional[str] = None) -> tuple[list[str], list[str], list[str]]:
"""Return (prefix, workspace, trailing) posture blocks in the historical flat order —
diff --git a/gateway/session_policy.py b/gateway/session_policy.py
index c4a7f5842d9..6ae31807fc2 100644
--- a/gateway/session_policy.py
+++ b/gateway/session_policy.py
@@ -19,7 +19,7 @@ CREATE_FIELDS = frozenset({'request_id', 'source', 'cwd', 'model', 'toolsets',
# turn (main's Bot Chat row), credentials included: a frozen key never crosses a profile route change.
FOLLOW_SECTIONS = ('model', 'providers', 'custom_providers', 'fallback_providers', 'fallback_model', 'model_aliases',
'platform_toolsets')
-FOLLOW_AGENT_KEYS = ('reasoning_effort', 'reasoning_overrides', 'service_tier', 'disabled_toolsets')
+FOLLOW_AGENT_KEYS = ('reasoning_effort', 'reasoning_overrides', 'service_tier', 'disabled_toolsets', 'coding_context')
# What a pick (create-time ``model`` / committed ``/model``) owns of the ``model:`` block.
SELECTION_KEYS = ('default', 'provider', 'base_url', 'api_mode', 'selection_profile', 'api_key_from', 'key_env',
'api_key_env')
@@ -54,7 +54,8 @@ class LocalSessionPolicy:
# ephemeral prompt never re-reads the skill files, like the CLI's startup preload.
skills_prompt: str | None = None
# Whether creation chose the coding posture (``agent.coding_context: focus`` in a client-named code
- # workspace). A following session replays this decision rather than re-probing its folder each turn.
+ # workspace). A following session replays this decision rather than re-probing its folder each turn: it
+ # drops the posture when its profile leaves ``focus``, and one created outside it never gains it later.
coding_posture: bool = False
def config(self, authority=None, *, secrets=True):
diff --git a/tests/gateway/test_session_policy_coding_posture.py b/tests/gateway/test_session_policy_coding_posture.py
index f3d74be0b85..78cc92b168f 100644
--- a/tests/gateway/test_session_policy_coding_posture.py
+++ b/tests/gateway/test_session_policy_coding_posture.py
@@ -118,3 +118,31 @@ async def test_session_create_selects_the_posture_for_its_named_folder(project,
finally:
store._db.close()
assert policy['coding_posture'] is True and policy['toolsets'] == ['coding']
+
+
+def test_a_following_session_takes_a_profile_coding_context_change(project, tmp_path, monkeypatch):
+ """Replay through the real follow merge (``policy.config(authority)``), not a hand-built dict."""
+ import json
+ from types import SimpleNamespace
+ home = tmp_path / 'home'
+ home.mkdir()
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ config = {**FOCUS, 'model': {'default': 'm'}}
+ (home / 'config.yaml').write_text(json.dumps(config))
+ policy = create({'source': 'cli', 'cwd': str(project), 'follow_profile_config': True}, config)
+ authority = SimpleNamespace(db=SimpleNamespace(db_path=str(home / 'state.db')))
+ assert policy.current_toolsets(policy.config(authority, secrets=False)) == ('coding',)
+ (home / 'config.yaml').write_text(json.dumps({**config, 'agent': {'coding_context': 'off'}}))
+ assert policy.current_toolsets(policy.config(authority, secrets=False)) == ('terminal',)
+
+
+def test_a_resolved_mode_keeps_its_own_focus_decision(project, tmp_path, monkeypatch):
+ """A mode resolved from a supplied ``focus`` config ignores the on-disk ``auto``."""
+ import json
+ from agent.coding_context import resolve_runtime_mode
+ home = tmp_path / 'home'
+ home.mkdir()
+ (home / 'config.yaml').write_text(json.dumps({'agent': {'coding_context': 'auto'}}))
+ monkeypatch.setenv('HERMES_HOME', str(home))
+ mode = resolve_runtime_mode(platform='cli', cwd=project, config={'agent': {'coding_context': 'focus'}})
+ assert mode.toolset_selection() == ['coding']