Skip to content

The agent that grows with you

refactor(cli): keep build_sessions_parser under its size cap after `sessions discard`

Teknium15fbb01283b1
diff --git a/hermes_cli/sessions_discard.py b/hermes_cli/sessions_discard.py
index 3dc3188562a..2cff3be1e2a 100644
--- a/hermes_cli/sessions_discard.py
+++ b/hermes_cli/sessions_discard.py
@@ -8,14 +8,12 @@ The lost input is not replayed; it stays in the transcript to resend.
"""
from __future__ import annotations
-import asyncio
import sys
-from hermes_cli.gateway_client import GatewayClientError, connect_gateway
-
async def _resume(client, name):
"""Exact session id first, then title, as ``hermes chat --resume`` / ``-c`` resolve it."""
+ from hermes_cli.gateway_client import GatewayClientError
for key in ("session_id", "title"):
try:
return await client.rpc("session.resume", **{key: name})
@@ -36,6 +34,7 @@ def _confirm(session_id, rows):
async def _discard(args):
+ from hermes_cli.gateway_client import GatewayClientError, connect_gateway
async with connect_gateway() as client:
snapshot = await _resume(client, args.session)
session_id = snapshot.get("session_id") or snapshot["stored_session_id"]
@@ -64,8 +63,23 @@ async def _discard(args):
return 0
+def add_discard_parser(sessions_subparsers) -> None:
+ from hermes_cli.subcommands._shared import add_yes_flag
+ parser = sessions_subparsers.add_parser(
+ "discard", help="Acknowledge turns lost in a gateway crash (unknown admissions) so the session runs again",
+ description="After a gateway crash mid-turn, the lost turn is fenced 'unknown' and blocks the "
+ "session: `hermes chat --resume <id> -q` exits 3 until it is acknowledged. This is the "
+ "non-interactive form of `/discard <admission>`. The lost input is not replayed.")
+ parser.add_argument("session", help="Session ID or title")
+ parser.add_argument("--admission", action="append", metavar="ID",
+ help="Discard only this unknown admission (repeatable; default: every unknown admission)")
+ add_yes_flag(parser, "Do not ask for confirmation (required when stdin is not a TTY)")
+
+
def cmd_discard(args) -> int:
+ import asyncio
from websockets.exceptions import WebSocketException
+ from hermes_cli.gateway_client import GatewayClientError
try:
return asyncio.run(_discard(args))
except (GatewayClientError, OSError, TimeoutError, WebSocketException) as exc:
diff --git a/hermes_cli/subcommands/sessions.py b/hermes_cli/subcommands/sessions.py
index 34f94652f5c..b798a85321f 100644
--- a/hermes_cli/subcommands/sessions.py
+++ b/hermes_cli/subcommands/sessions.py
@@ -12,6 +12,15 @@ def _flag(parser, *names, help, **kw):
parser.add_argument(*names, action="store_true", help=help, **kw)
+def _add_rename_and_discard(sessions_subparsers) -> None:
+ sessions_rename = sessions_subparsers.add_parser(
+ "rename", help="Set or change a session's title")
+ sessions_rename.add_argument("session_id", help="Session ID to rename")
+ sessions_rename.add_argument("title", nargs="+", help="New title for the session")
+ from hermes_cli.sessions_discard import add_discard_parser
+ add_discard_parser(sessions_subparsers)
+
+
def build_sessions_parser(subparsers, *, cmd_sessions: Callable) -> None:
"""Attach the ``sessions`` subcommand to ``subparsers``."""
sessions_parser = subparsers.add_parser(
@@ -263,21 +272,7 @@ def build_sessions_parser(subparsers, *, cmd_sessions: Callable) -> None:
"--report", type=Path, help="JSON report path (defaults to <output>.recovery.json)")
sessions_subparsers.add_parser("stats", help="Show session store statistics")
-
- sessions_discard = sessions_subparsers.add_parser(
- "discard", help="Acknowledge turns lost in a gateway crash (unknown admissions) so the session runs again",
- description="After a gateway crash mid-turn, the lost turn is fenced 'unknown' and blocks the "
- "session: `hermes chat --resume <id> -q` exits 3 until it is acknowledged. This is the "
- "non-interactive form of `/discard <admission>`. The lost input is not replayed.")
- sessions_discard.add_argument("session", help="Session ID or title")
- sessions_discard.add_argument("--admission", action="append", metavar="ID",
- help="Discard only this unknown admission (repeatable; default: every unknown admission)")
- add_yes_flag(sessions_discard, "Do not ask for confirmation (required when stdin is not a TTY)")
-
- sessions_rename = sessions_subparsers.add_parser(
- "rename", help="Set or change a session's title")
- sessions_rename.add_argument("session_id", help="Session ID to rename")
- sessions_rename.add_argument("title", nargs="+", help="New title for the session")
+ _add_rename_and_discard(sessions_subparsers)
sessions_pin = sessions_subparsers.add_parser(
"pin", help="Pin session(s) — durable keep flag, exempt from auto-archive",