Skip to content

hermes: follow gateway and cron sessions and register /te command - #259

Open
Tejas Lamba (TejasLamba2006) wants to merge 2 commits into
microsoft:mainfrom
TejasLamba2006:feat/hermes-gateway-plugin
Open

Tejas Lamba (TejasLamba2006) wants to merge 2 commits into
microsoft:mainfrom
TejasLamba2006:feat/hermes-gateway-plugin

Conversation

@TejasLamba2006

@TejasLamba2006 Tejas Lamba (TejasLamba2006) commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Addresses part of #232.

Extends plugin/hermes to follow gateway (api_server, discord, telegram, slack, whatsapp, signal, matrix, mattermost, email, sms, webhook) and cron sessions, handle on_session_reset, and register /te via ctx.register_command.

Changes

  • Per-session directory resolution: Never read os.getcwd() or process-wide TERMINAL_CWD (both belong to the gateway daemon and are shared across chats). Resolve each session's repository from its sessions.cwd row in state.db; for gateway and cron sessions whose row has none, fall back to the chat's terminal_cwd in gateway_routing, then to the task-local directory Hermes binds to the running turn (ctx.session_cwd() / agent.runtime_cwd.scoped_session_cwd()). If none is bound yet, retry on the next hook rather than associating the session with the gateway's directory.
  • Session finalization: Treat cron runs as single-turn (oneshot) so they fold at on_session_end, and register on_session_reset alongside on_session_finalize so /new ends the session it leaves on surfaces that emit reset without finalize. Unfinalized gateway sessions still recover and fold on the next SessionStart in that repository after STALE_S.
  • /te slash command: Register Bridge.command via ctx.register_command declaring session_id and session_key so Hermes passes the calling session context, pass --cwd <dir> and cwd=<dir> to the te subprocess, respect explicit --cwd flags, and fail closed with a clear message when a repo-scoped command has no session directory.
  • Tests: Add offline unit and real-dispatcher (TE_FOLD_SYNC=1, real state.db) integration tests in tests/test_hermes_plugin.py covering concurrent multi-repository gateway and cron isolation, finalization/reset/stale recovery, and /te status execution from an unrelated process directory.

Follow sessions started through the API server, messaging platforms and cron, taking a gateway session's directory from TERMINAL_CWD when its row has none. Register /te through register_command where Hermes offers it. Subagents stay unfollowed, since a session's fold already prices them.
@jackhaohuang

Copy link
Copy Markdown
Collaborator

Thanks for the PR! The overall approach looks good. A few concerns before merging:

  1. Working directory: /te doesn't explicitly pass cwd, so it may operate on the gateway's directory rather than the session's repository.
  2. Gateway fallback: Using TERMINAL_CWD could associate different gateway sessions with the wrong repository. Could we use session-specific directory resolution instead?
  3. Testing: Could you add an integration test or manual verification for gateway/cron sessions, especially multi-repository isolation and session finalization?

Otherwise, the changes look reasonable!

…ere, end sessions on reset

A gateway session whose row records no cwd no longer takes the gateway's
TERMINAL_CWD, which every chat shares. It takes its chat's routed
terminal_cwd from gateway_routing, then the directory scoped to the
running turn, and is retried at its next hook if it has neither.

/te runs in the calling session's directory (session_id, session_key),
passes --cwd and cwd, honors an explicit --cwd, and says so when it has
no directory. on_session_reset ends the session a /new leaves.

Tests cover two repositories through the real dispatcher with gateway
and cron sessions, finalize, reset, stale recovery, and /te status.
@TejasLamba2006

Copy link
Copy Markdown
Contributor Author

Thanks for the review jackhaohuang! Addressed all three points in e5bb7fe:

  1. /te working directory: Bridge.command now declares session_id and session_key (which Hermes passes to register_command handlers), resolves that session's directory, and passes both --cwd and cwd= to the te subprocess. Explicit --cwd arguments are honored as given, and repo-scoped commands fail closed (te: no working directory for this session; pass --cwd PATH) instead of reading the gateway process directory.
  2. Per-session directory resolution (no TERMINAL_CWD): Removed os.environ.get("TERMINAL_CWD"). When a gateway or cron row in state.db has no cwd, the plugin now checks the session's gateway_routing entry (metadata.terminal_cwd) and then Hermes's turn-scoped directory (ctx.session_cwd() / agent.runtime_cwd.scoped_session_cwd()). If neither is set yet, the session stays unfollowed and is re-checked on its next hook rather than borrowing a shared directory.
  3. Finalization & integration tests: Added on_session_reset alongside on_session_finalize so /new folds the outgoing session, and added real-dispatcher (TE_FOLD_SYNC=1, real state.db) integration tests in tests/test_hermes_plugin.py covering concurrent api_server, discord, telegram, and cron sessions across two repositories while os.getcwd() and TERMINAL_CWD point at a third directory, plus stale gateway session recovery and /te status isolation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants