The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(gateway): ask the workspace question without building the toolset list twice
kshitijk4poordb44c46208f1
diff --git a/agent/coding_context.py b/agent/coding_context.pyindex bdc62b52b53..840e5e04cf5 100644--- a/agent/coding_context.py+++ b/agent/coding_context.py@@ -327,7 +327,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 [self.profile.toolset, *_enabled_mcp_servers(config)]+ return focus_toolsets(config) # the one builder of the posture's listdef 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 —@@ -397,6 +397,12 @@ def is_coding_context(*, platform: Optional[str] = None, cwd: Optional[str | Patreturn resolve_runtime_mode(platform=platform, cwd=cwd, config=config).is_coding+def in_focus_workspace(*, platform: Optional[str] = None, cwd: Optional[str | Path] = None, config: Optional[dict[str, Any]] = None) -> bool:+ """Whether *cwd* selects the coding posture's toolsets (``focus`` and a code workspace), without+ building them; ``focus_toolsets`` builds them once the decision is made."""+ return _coding_mode(config) == "focus" and is_coding_context(platform=platform, cwd=cwd, config=config)++def coding_selection(*, platform: Optional[str] = None, cwd: Optional[str | Path] = None, config: Optional[dict[str, Any]] = None) -> Optional[list[str]]:"""Toolset selection for the coding posture (``None`` unless ``focus`` and active)."""if _coding_mode(config) != "focus": # only focus swaps toolsets: skip the workspace probediff --git a/gateway/session_policy.py b/gateway/session_policy.pyindex 2645980bf16..c4a7f5842d9 100644--- a/gateway/session_policy.py+++ b/gateway/session_policy.py@@ -397,7 +397,6 @@ def _resolve_toolsets(params, config, source, safe_mode, coding_posture=False):"""Validate explicit toolsets and return the session's enabled toolset set. Without a pick, a*coding_posture* session runs ``focus_toolsets`` instead of the profile default while the profilestays on ``focus``; the gateway decides it, never the client."""- from agent.coding_context import focus_toolsetsfrom hermes_cli.tools_config import _get_platform_toolsfrom toolsets import validate_toolsetexplicit = params.get('toolsets')@@ -406,7 +405,10 @@ def _resolve_toolsets(params, config, source, safe_mode, coding_posture=False):or ('desktop_ui' in explicit and source != 'gui')):raise RuntimeStoreError('invalid_params')config.setdefault('platform_toolsets', {})['acp' if source == 'acp' else 'cli'] = explicit- posture = focus_toolsets(config) if explicit is None and coding_posture else None+ posture = None+ if explicit is None and coding_posture:+ from agent.coding_context import focus_toolsets+ posture = focus_toolsets(config)enabled = set(posture) if posture is not None else _get_platform_tools(config, 'acp' if source == 'acp' else 'cli')if explicit is not None:from toolsets import resolve_toolset@@ -434,9 +436,9 @@ def _resolve_toolsets(params, config, source, safe_mode, coding_posture=False):def _in_coding_workspace(config, source, cwd):"""Whether *cwd* selects the coding posture (``agent.coding_context: focus`` in a code workspace).A detection failure keeps the profile default rather than refusing the session."""- from agent.coding_context import coding_selection+ from agent.coding_context import in_focus_workspacetry:- return coding_selection(platform=SURFACES[source], cwd=cwd, config=config) is not None+ return in_focus_workspace(platform=SURFACES[source], cwd=cwd, config=config)except Exception:logger.debug('coding posture detection failed for %s; keeping the profile default', cwd, exc_info=True)return Falsediff --git a/tests/gateway/test_session_policy_coding_posture.py b/tests/gateway/test_session_policy_coding_posture.pyindex c780cb11e5f..f3d74be0b85 100644--- a/tests/gateway/test_session_policy_coding_posture.py+++ b/tests/gateway/test_session_policy_coding_posture.py@@ -95,7 +95,6 @@ def test_only_session_create_opts_into_the_workspace_posture(project):assert build_policy({'source': 'cli', 'cwd': str(project)}, FOCUS).toolsets == ('terminal',)-@pytest.mark.asyncioasync def test_session_create_selects_the_posture_for_its_named_folder(project, tmp_path, monkeypatch):"""The real ``session.create`` path (``create_local_session``), not a direct ``build_policy`` call."""@@ -111,8 +110,11 @@ async def test_session_create_selects_the_posture_for_its_named_folder(project,monkeypatch.setattr(run, '_load_gateway_config', lambda: {**FOCUS, 'model': {'default': 'm'}})store = SessionStore(tmp_path / 'sessions', GatewayConfig())runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False)- authority = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='posture')- actor = Principal('owner', str(tmp_path), frozenset({'session:create', 'session:read'}), 'socket')- ref = create_local_session(authority, actor, {'request_id': 'r', 'source': 'cli', 'cwd': str(project)})- policy = local_receipt(store._db, ref.session_id)['policy']+ try:+ authority = await initialize_session_authority(runner, profile_id=str(tmp_path), instance_id='posture')+ actor = Principal('owner', str(tmp_path), frozenset({'session:create', 'session:read'}), 'socket')+ ref = create_local_session(authority, actor, {'request_id': 'r', 'source': 'cli', 'cwd': str(project)})+ policy = local_receipt(store._db, ref.session_id)['policy']+ finally:+ store._db.close()assert policy['coding_posture'] is True and policy['toolsets'] == ['coding']diff --git a/website/docs/developer-guide/gateway-local-sessions.md b/website/docs/developer-guide/gateway-local-sessions.mdindex b665c9b2295..8da45145e8e 100644--- a/website/docs/developer-guide/gateway-local-sessions.md+++ b/website/docs/developer-guide/gateway-local-sessions.md@@ -74,8 +74,9 @@ OpenAI API-to-local session affinity; those remain separate integration paths.the client-surface toolsets (`project`, `desktop_ui`, `catalog`). GUI policy isindependent of `HERMES_DESKTOP` and of the attaching viewer's identity. Without explicit`toolsets`, the gateway applies the coding posture (`agent.coding_context: focus` in a- code workspace, `agent/coding_context.py`) from a client-supplied `cwd`, decided once at- creation; clients never send it as a pick. `source: tool`, Kanban workers and migrated+ code workspace, `agent/coding_context.py`) from the `cwd` a `session.create` names,+ decided once at creation; clients never send it as a pick. Only `session.create` applies+ it, never for `source: tool`: Kanban workers, cron jobs, A2A, hosted rooms and migratedlegacy rows keep the profile's toolsets.- Optional flat creation fields: `cwd` (existing absolute gateway-local directory),`model` (nonempty model identifier on the daemon's configured provider), and `toolsets`