Skip to content

The agent that grows with you

docs(plugins): slash command handlers run in the gateway process

unsupportedpastels8af0911efc91
diff --git a/tests/gateway/test_cli_plugin_command_in_gateway.py b/tests/gateway/test_cli_plugin_command_in_gateway.py
index 3417da1c5eb..afdaaf6a70b 100644
--- a/tests/gateway/test_cli_plugin_command_in_gateway.py
+++ b/tests/gateway/test_cli_plugin_command_in_gateway.py
@@ -36,6 +36,15 @@ def register(ctx):
def boom(arg):
raise RuntimeError("probe handler exploded")
ctx.register_command("probe-boom", boom, description="Always fails")
+
+ def where(arg): # what the plugin docs tell a handler to read instead of os.getcwd()/os.environ
+ from agent.runtime_cwd import resolve_agent_cwd
+ from gateway.session_context import get_session_env
+ return "WHERE=" + json.dumps({"cwd": str(resolve_agent_cwd()), "getcwd": os.getcwd(),
+ "source": get_session_env("HERMES_SESSION_SOURCE"),
+ "platform": get_session_env("HERMES_SESSION_PLATFORM"),
+ "key": get_session_env("HERMES_SESSION_KEY")})
+ ctx.register_command("probe-cwd", where, description="Where the handler runs")
'''
# One command per gateway life: A serves /alpha, the restarted B only /beta.
_LIFE = '''import os
@@ -147,7 +156,7 @@ def test_cli_plugin_command_runs_in_the_gateway_and_reaches_its_mcp_registry(tmp
out = capsys.readouterr().out
calls = list(wire.calls)
typed_outs = {}
- for typed in ('/probe_under', '/probe_only', '/probe-boom'):
+ for typed in ('/probe_under', '/probe_only', '/probe-boom', '/probe-cwd'):
assert c._process_unregistered_slash(typed, typed) is True
typed_outs[typed] = capsys.readouterr().out
finally:
@@ -159,6 +168,10 @@ def test_cli_plugin_command_runs_in_the_gateway_and_reaches_its_mcp_registry(tmp
assert 'UNDERSCORE_ONLY' in typed_outs['/probe_only'], typed_outs
# main's local path printed a failing handler's error; the gateway path must not swallow it
assert 'Plugin command error: probe handler exploded' in typed_outs['/probe-boom'], typed_outs
+ where = json.loads(typed_outs['/probe-cwd'].split('WHERE=', 1)[1].strip())
+ # the session's folder and session vars are bound around the handler; the process cwd is the gateway's
+ assert where['cwd'] == str(work) and where['getcwd'] != str(work), where
+ assert where['platform'] == 'local' and where['key'], where
assert proc.pid != os.getpid()
assert f'PLUGIN_PID={proc.pid} ' in out, out # the handler ran in the gateway process
mcp = json.loads(json.loads(out.split('MCP=', 1)[1].strip())['result'])
diff --git a/website/docs/developer-guide/plugins/index.md b/website/docs/developer-guide/plugins/index.md
index 9cfb32e985d..e6c64a4f521 100644
--- a/website/docs/developer-guide/plugins/index.md
+++ b/website/docs/developer-guide/plugins/index.md
@@ -1569,6 +1569,20 @@ def register(ctx):
ctx.register_command("check", handler=_handle_check, description="Run async check")
```
+**Where handlers run:** a slash command handler runs in the gateway process that owns the session, also when the user types the command in the classic `hermes chat` CLI. The handler is not running in the user's terminal or directory, so:
+
+- Return the text to show. `print()` writes to the gateway's stdout, and the user never sees it.
+- Take the session's working directory from `agent.runtime_cwd.resolve_agent_cwd()`, not `os.getcwd()`, which is the gateway's own directory. Resolve relative paths against it.
+- Read session identity with `gateway.session_context.get_session_env("HERMES_SESSION_PLATFORM")` and the other `HERMES_SESSION_*` names, not `os.environ`.
+
+```python
+from agent.runtime_cwd import resolve_agent_cwd
+
+def _handle_todo(raw_args: str) -> str:
+ todo = resolve_agent_cwd() / "TODO.md" # the session's folder, not the gateway's
+ return todo.read_text() if todo.exists() else "No TODO.md here."
+```
+
### Dispatch tools from slash commands
Slash command handlers that need to orchestrate tools (spawn a subagent via `delegate_task`, call `file_edit`, etc.) should use `ctx.dispatch_tool()` instead of reaching into framework internals. The parent-agent context (workspace hints, spinner, model inheritance) is wired up automatically.
diff --git a/website/docs/user-guide/features/plugins.md b/website/docs/user-guide/features/plugins.md
index 0434973a0bb..bab8fdbb449 100644
--- a/website/docs/user-guide/features/plugins.md
+++ b/website/docs/user-guide/features/plugins.md
@@ -120,6 +120,16 @@ Every `ctx.*` API below is available inside a plugin's `register(ctx)` function.
| Call an MCP tool (capability-gated) | `ctx.call_mcp(server, tool, arguments, timeout=30)` — see [Calling MCP servers from plugins](#calling-mcp-servers-from-plugins) |
| Register an inference backend (LLM provider) | `register_provider(ProviderProfile(...))` in `plugins/model-providers/<name>/__init__.py` — see [Model Provider Plugins](../../developer-guide/model-provider-plugin.md) (uses a separate discovery system) |
+:::note Slash command handlers run in the gateway
+A `/command` registered with `ctx.register_command()` runs in the gateway process that owns the session, beside the plugin's hooks, tools and MCP servers. That includes commands typed in the classic `hermes chat` CLI. So a handler should:
+
+- **Return its text** instead of calling `print()`. Printed output goes to the gateway's stdout, not to the user's chat.
+- **Read the session's working directory with `agent.runtime_cwd.resolve_agent_cwd()`**, not `os.getcwd()`, which is the gateway's own directory. The gateway pins the session's directory while the handler runs.
+- **Read session identity with `gateway.session_context.get_session_env("HERMES_SESSION_PLATFORM")`** (and the other `HERMES_SESSION_*` names), not `os.environ`. These values are bound per session around the handler.
+
+See [Register slash commands](../../developer-guide/plugins/index.md#register-slash-commands).
+:::
+
## Plugin discovery
| Source | Path | Use case |