Skip to content

The agent that grows with you

refactor(gateway/slash): collapse /whoami, /platform, /stop, /footer, /verbose duplication; extract /compress preview; module docstrings

Teknium2e9a5e9f327e
diff --git a/gateway/slash_commands.py b/gateway/slash_commands.py
index 98dcc90e3f2..c5c308ce74c 100644
--- a/gateway/slash_commands.py
+++ b/gateway/slash_commands.py
@@ -381,39 +381,16 @@ class GatewaySlashCommandsMixin(
scope = "DM" if chat_type.lower() in {"dm", "direct", "private", ""} else "group/channel"
user_id = (source.user_id if source else None) or "?"
+ head = f"**You** — {platform} ({scope})\nUser ID: `{user_id}`\n"
if not policy.enabled:
- return (
- f"**You** — {platform} ({scope})\n"
- f"User ID: `{user_id}`\n"
- f"Tier: unrestricted (no admin list configured for this scope)\n"
- f"Slash commands: all available"
- )
-
+ return head + "Tier: unrestricted (no admin list configured for this scope)\nSlash commands: all available"
if policy.is_admin(user_id):
- return (
- f"**You** — {platform} ({scope})\n"
- f"User ID: `{user_id}`\n"
- f"Tier: **admin**\n"
- f"Slash commands: all available"
- )
-
- # Non-admin user. Show what's actually reachable.
- floor = ["help", "whoami"] # mirrors slash_access._ALWAYS_ALLOWED_FOR_USERS
- configured = sorted(policy.user_allowed_commands)
- # Combine + dedupe, preserve order: floor first, then operator additions.
- seen: set[str] = set()
- runnable: list[str] = []
- for c in floor + configured:
- if c not in seen:
- seen.add(c)
- runnable.append(c)
+ return head + "Tier: **admin**\nSlash commands: all available"
+ # Non-admin user: show what's actually reachable. Floor first (mirrors
+ # slash_access._ALWAYS_ALLOWED_FOR_USERS), then operator additions, deduped in order.
+ runnable = list(dict.fromkeys(["help", "whoami"] + sorted(policy.user_allowed_commands)))
runnable_str = ", ".join(f"/{c}" for c in runnable) if runnable else "(none)"
- return (
- f"**You** — {platform} ({scope})\n"
- f"User ID: `{user_id}`\n"
- f"Tier: user\n"
- f"Slash commands you can run: {runnable_str}"
- )
+ return head + f"Tier: user\nSlash commands you can run: {runnable_str}"
async def _handle_kanban_command(self, event: MessageEvent) -> str:
"""Handle /kanban — delegate to the shared kanban CLI.
@@ -535,26 +512,20 @@ class GatewaySlashCommandsMixin(
session_entry = await self.async_session_store.get_or_create_session(source)
session_key = session_entry.session_key
+ async def _stop(key: str, invalidation_reason: str) -> None:
+ await self._interrupt_and_clear_session(
+ key, source, interrupt_reason=_INTERRUPT_REASON_STOP, invalidation_reason=invalidation_reason,
+ )
+
agent = self._running_agents.get(session_key)
if agent is _AGENT_PENDING_SENTINEL:
# Force-clean the sentinel so the session is unlocked.
- await self._interrupt_and_clear_session(
- session_key,
- source,
- interrupt_reason=_INTERRUPT_REASON_STOP,
- invalidation_reason="stop_command_pending",
- )
+ await _stop(session_key, "stop_command_pending")
logger.info("STOP (pending) for session %s — sentinel cleared", session_key)
return EphemeralReply(t("gateway.stop.stopped_pending"))
if agent:
- # Force-clean the session lock so a truly hung agent doesn't
- # keep it locked forever.
- await self._interrupt_and_clear_session(
- session_key,
- source,
- interrupt_reason=_INTERRUPT_REASON_STOP,
- invalidation_reason="stop_command_handler",
- )
+ # Force-clean the session lock so a truly hung agent doesn't keep it locked forever.
+ await _stop(session_key, "stop_command_handler")
return EphemeralReply(t("gateway.stop.stopped"))
# No run under the caller's own key. In a per-user thread (thread_sessions_per_user=True) a
@@ -563,12 +534,7 @@ class GatewaySlashCommandsMixin(
sibling_keys = self._sibling_thread_run_keys(source, session_key)
if sibling_keys and self._is_user_authorized(source):
for sibling_key in sibling_keys:
- await self._interrupt_and_clear_session(
- sibling_key,
- source,
- interrupt_reason=_INTERRUPT_REASON_STOP,
- invalidation_reason="stop_command_thread_sibling",
- )
+ await _stop(sibling_key, "stop_command_thread_sibling")
logger.info(
"STOP (thread sibling) by %s — interrupted %d run(s) in thread: %s",
session_key,
@@ -604,47 +570,27 @@ class GatewaySlashCommandsMixin(
action = (parts[0] if parts else "list").lower()
target = parts[1].lower() if len(parts) > 1 else ""
- # Resolve platform name (case-insensitive, value match)
- def _resolve_platform(name: str):
- if not name:
- return None
- for p in Platform.__members__.values():
- if p.value.lower() == name:
- return p
- return None
-
+ failed = getattr(self, "_failed_platforms", {}) or {}
if action == "list":
- lines = ["**Gateway platforms**"]
connected = sorted(p.value for p in self.adapters)
- if connected:
- lines.append("Connected: " + ", ".join(connected))
- else:
- lines.append("Connected: (none)")
- failed = getattr(self, "_failed_platforms", {}) or {}
- if failed:
- for p, info in failed.items():
- if info.get("paused"):
- reason = info.get("pause_reason") or "paused"
- lines.append(
- f" · {p.value} — PAUSED ({reason}). "
- f"Resume with `/platform resume {p.value}`."
- )
- else:
- attempts = info.get("attempts", 0)
- lines.append(
- f" · {p.value} — retrying (attempt {attempts})"
- )
- else:
+ lines = ["**Gateway platforms**", "Connected: " + (", ".join(connected) if connected else "(none)")]
+ for p, info in failed.items():
+ if info.get("paused"):
+ reason = info.get("pause_reason") or "paused"
+ lines.append(f" · {p.value} — PAUSED ({reason}). Resume with `/platform resume {p.value}`.")
+ else:
+ lines.append(f" · {p.value} — retrying (attempt {info.get('attempts', 0)})")
+ if not failed:
lines.append("Failed/paused: (none)")
return "\n".join(lines)
if action in {"pause", "resume"}:
if not target:
return f"Usage: /platform {action} <name>"
- platform = _resolve_platform(target)
+ # Resolve platform name (case-insensitive, value match)
+ platform = next((p for p in Platform.__members__.values() if p.value.lower() == target), None)
if platform is None:
return f"Unknown platform: {target}"
- failed = getattr(self, "_failed_platforms", {}) or {}
if action == "pause":
if platform not in failed:
return (
@@ -1288,7 +1234,6 @@ class GatewaySlashCommandsMixin(
config_path = _gateway_config_home() / "config.yaml"
platform_key = _platform_config_key(event.source.platform)
- # --- check config gate ------------------------------------------------
try:
user_config = _load_gateway_config()
gate_enabled = is_truthy_value(
@@ -1301,7 +1246,7 @@ class GatewaySlashCommandsMixin(
if not gate_enabled:
return t("gateway.verbose.not_enabled")
- # --- cycle mode (per-platform) ----------------------------------------
+ # Cycle mode (per-platform).
cycle = ["off", "new", "all", "verbose", "log"]
# Read current effective mode for this platform via the resolver
from gateway.display_config import resolve_display_setting
@@ -1311,7 +1256,6 @@ class GatewaySlashCommandsMixin(
new_mode = cycle[(cycle.index(current) + 1) % len(cycle)]
description = t(f"gateway.verbose.mode_{new_mode}")
- # Save to display.platforms.<platform>.tool_progress
try:
_nested_dict(user_config, "display", "platforms", platform_key)["tool_progress"] = new_mode
atomic_config_write(config_path, user_config)
@@ -1375,7 +1319,6 @@ class GatewaySlashCommandsMixin(
config_path = _gateway_config_home() / "config.yaml"
platform_key = _platform_config_key(event.source.platform)
- # --- parse argument -------------------------------------------------
arg = ""
try:
text = (getattr(event, "message", None) or "").strip()
@@ -1386,7 +1329,6 @@ class GatewaySlashCommandsMixin(
except Exception:
arg = ""
- # --- load config ----------------------------------------------------
try:
user_config: dict = _load_gateway_config()
except Exception as e:
@@ -1413,7 +1355,6 @@ class GatewaySlashCommandsMixin(
else:
return t("gateway.footer.usage")
- # --- write global flag ---------------------------------------------
try:
_nested_dict(user_config, "display", "runtime_footer")["enabled"] = new_state
atomic_config_write(config_path, user_config)
diff --git a/gateway/slash_commands_model.py b/gateway/slash_commands_model.py
index 3d0344b31dd..3e28c999c92 100644
--- a/gateway/slash_commands_model.py
+++ b/gateway/slash_commands_model.py
@@ -1,4 +1,5 @@
-"""Gateway slash commands that switch or tune the model route: /model, /codex-runtime, /reasoning, /fast, /personality.
+"""Gateway slash commands that switch or tune the model route:
+/model, /codex-runtime, /reasoning, /fast, /personality.
Split out of ``gateway/slash_commands.py``; bound onto ``GatewayRunner`` through
``GatewaySlashCommandsMixin``. Origin internals are imported lazily (``from gateway.slash_commands
@@ -143,7 +144,7 @@ def _model_provider_listing_lines(providers) -> list[str]:
class GatewayModelCommandsMixin:
- """Gateway slash commands that switch or tune the model route: /model, /codex-runtime, /reasoning, /fast, /personality."""
+ """Model-route slash commands (/model, /codex-runtime, /reasoning, /fast, /personality)."""
async def _perform_model_switch(
self,
diff --git a/gateway/slash_commands_session.py b/gateway/slash_commands_session.py
index e7ed1b12ba8..62831611765 100644
--- a/gateway/slash_commands_session.py
+++ b/gateway/slash_commands_session.py
@@ -1,4 +1,5 @@
-"""Gateway slash commands that rotate, switch, fork or rewrite the session transcript: /new, /resume, /sessions, /branch, /title, /save, /undo, /retry, /topic, /compress.
+"""Gateway slash commands that rotate, switch, fork or rewrite the session transcript:
+/new, /resume, /sessions, /branch, /title, /save, /undo, /retry, /topic, /compress.
Split out of ``gateway/slash_commands.py``; bound onto ``GatewayRunner`` through
``GatewaySlashCommandsMixin``. Origin internals are imported lazily (``from gateway.slash_commands
@@ -62,6 +63,25 @@ def _manual_compression_reply_lines(summary: dict, compressor, focus_topic) -> l
return lines
+def _compress_preview_reply(history, partial: bool, keep_last, focus_topic, agg_note: str) -> str:
+ """``/compress --preview``: report what WOULD be compressed — no agent, no writes."""
+ from agent.model_metadata import estimate_request_tokens_rough
+ from hermes_cli.partial_compress import summarize_compress_preview
+
+ pv_msgs = [
+ {"role": m.get("role"), "content": m.get("content")}
+ for m in history
+ if m.get("role") in {"user", "assistant"} and m.get("content")
+ ]
+ report = summarize_compress_preview(
+ pv_msgs, partial, keep_last, focus_topic, estimate_request_tokens_rough(pv_msgs)
+ )
+ lines = [f"🗜️ {line}" for line in report["lines"]]
+ if agg_note:
+ lines.append(agg_note)
+ return "\n".join(lines)
+
+
def _reset_process_scoped_tool_state() -> None:
"""Drop env-passthrough and credential-file state at a conversation boundary (best-effort)."""
try:
@@ -92,7 +112,7 @@ def _branch_row(msg: dict) -> dict:
class GatewaySessionCommandsMixin:
- """Gateway slash commands that rotate, switch, fork or rewrite the session transcript: /new, /resume, /sessions, /branch, /title, /save, /undo, /retry, /topic, /compress."""
+ """Session-transcript slash commands (/new, /resume, /sessions, /branch, /title, /save, /undo, /retry, /topic, /compress)."""
async def _handle_reset_command(self, event: MessageEvent) -> Union[str, EphemeralReply]:
"""Handle /new or /reset command."""
@@ -683,7 +703,6 @@ class GatewaySessionCommandsMixin:
parse_partial_compress_args,
rejoin_compressed_head_and_tail,
split_history_for_partial_compress,
- summarize_compress_preview,
)
from agent.conversation_compression import (
finalize_context_engine_compression_notification,
@@ -703,20 +722,7 @@ class GatewaySessionCommandsMixin:
return _agg_note
if _preview:
- # Report what WOULD be compressed — no agent, no writes.
- from agent.model_metadata import estimate_request_tokens_rough
- _pv_msgs = [
- {"role": m.get("role"), "content": m.get("content")}
- for m in history
- if m.get("role") in {"user", "assistant"} and m.get("content")
- ]
- report = summarize_compress_preview(
- _pv_msgs, partial, keep_last, focus_topic, estimate_request_tokens_rough(_pv_msgs)
- )
- lines = [f"🗜️ {line}" for line in report["lines"]]
- if _aggressive:
- lines.append(_agg_note)
- return "\n".join(lines)
+ return _compress_preview_reply(history, partial, keep_last, focus_topic, _agg_note)
try:
from agent.manual_compression_feedback import summarize_manual_compression