mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
* feat(i18n): layered catalogs — plugin packs and user overlay over bundled locales
* feat(tui): i18n layer — en catalog, nanostore runtime, RPC pack loader, _keys.tui.json emitter
ui-tui/src/i18n/: en.ts (facade over topical siblings under en/), types.ts
(Translations + dotted TranslationKey derived from en), runtime.ts ($locale/
$catalog atoms, translateFrom active→en→key, pack merge with string→fn
wrapping for {0}/{1} placeholders), loader.ts (display.language →
i18n.catalog {lang, surface:'tui'}, English when the method is missing),
useT()/useLocale() hooks, t() for non-React code. useConfigSync feeds the
loader from the existing config.get full hydration. `npm run i18n:keys`
writes locales/_keys.tui.json (sorted flat key list) and runs before build.
* feat(plugins): provides_locales manifest field, ctx.register_locale/register_locale_dir, manifest-only language packs
* chore(tui): split en catalog siblings by lane (slash sibling)
* feat(plugins): validate language packs — parse, text-only, key-subset WARN against en / _keys exports
* feat(tui_gateway): i18n.languages / i18n.catalog RPC + regenerated contracts
* feat(config): display.language accepts any supported_languages() id, refuses unknown ids with the list
* docs(i18n): language packs user guide, pluggable display.language, plugin developer section, AGENTS notes
* feat(plugins): report language-pack layers in the mid-run activation summary
* feat(tui): wire status bar, composer placeholders, hotkey help and approval/clarify/confirm prompts through i18n
StatusRule maps compared state values (ready/running…/summoning) to catalog
text at render via displayStatus(); hotkeys()/placeholder() resolve lazily so
a pack that arrives after boot applies. Catalog grows to 81 keys.
* feat(desktop): pluggable app locales — registry, host.i18n.registerAppLocale, backend packs, keys emitter
- Locale widens to string (BundledLocale keeps the union); TRANSLATIONS stays
the bundled record and every consumer resolves through the registry.
- src/i18n/registry.ts: registerAppLocale(id, {endonym, rtl, translations})
layers partial packs (nested or flat dotted) over bundled/en via
mergeTranslations; a string over a function-valued en entry becomes a
positional {0}/{1} formatter; $appLocaleVersion bumps so translators
re-render; per-source disposers + replaceAppLocaleSource for atomic swaps.
- Backend packs: i18n.languages + i18n.catalog {surface:'desktop'} feed the
registry as source 'backend' (method-not-found is silent); re-synced on
socket open, display.language change and profile switch. A saved pack-only
language is promoted once its pack registers.
- SDK: host.i18n.registerAppLocale / languageOptions; ctx.i18n.registerAppLocale
tracked for unload. Docs in the desktop plugin SDK guide + skill reference.
- Language switcher lists bundled ∪ registered ∪ backend, endonym-only; RTL
from the registry (applyDocumentLocale takes rtl).
- npm run i18n:keys emits locales/_keys.desktop.json (wired into build).
* i18n(cli): route /topup + /subscription copy through t() (cli.billing.*, cli.subscription.*)
Module-level copy tables and modal choice tuples in cli_billing_mixin.py froze
English at import, before display.language was known. They are now key tables /
builder functions evaluated at call time; every user-facing line in the /usage
balance block, /subscription and the five /topup screens reads the catalog.
Choice VALUES stay English identifiers. Fragment-assembled status lines
(Plan: … → cancels · $x left · renews …) become full templates.
* i18n(gateway): exec-approval card contract + base/run/run_busy/run_inbound replies through t()
- base_exec_approval: EA_* English constants stay; add ea_header_text()/ea_reason_label_text()/
ea_smart_deny_line_text()/ea_default_reason_text()/ea_action_labels()/approval_timed_out_notice()
accessors; deadline + timed-out notice resolve via gateway.exec_approval.*
- BasePlatformAdapter._EA_HEADER/_EA_REASON_LABEL/_EA_SMART_DENY_LINE/_EA_ACTION_LABELS become
properties (adapters still shadow them with markup class attrs)
- run.py: provider error replies table holds catalog keys; _CONTEXT_OVERFLOW_REPLY -> _context_overflow_reply()
- run_busy/run_inbound: typed approval + slash-confirm matchers accept English ∪ approval.inputs.* (t())
- locales/en.yaml: gateway.exec_approval/busy/errors/... namespaces
* i18n(cli): wire modal, loops, agent-setup mixins through t() (cli.* keys)
* i18n(platforms): route Slack, Matrix and Feishu user-facing text through t()
Exec-approval markup overrides (_EA_HEADER/_EA_REASON_LABEL/_EA_SMART_DENY_LINE/
_EA_ACTION_LABELS) become per-call properties over the shared
gateway.exec_approval.* contract keys, so Slack's 3000-char section budget
measures the resolved template. Slack _APPROVAL_DECISIONS/_CONFIRM_DECISIONS,
Feishu _APPROVAL_LABEL_MAP and Matrix _EA_LEGEND/_EA_TYPED_HINT turn into
key tables resolved at click time; the Matrix typed hints become whole
sentences per offered tier instead of spliced fragments. Slack button labels
are cut to 75 chars and select placeholders to 150 after translation; the
model-facing clarify fallback answer ('choice N') stays English while the
card copy localizes.
locales/en.yaml gains the gateway.exec_approval.* contract keys plus the
platform.shared.* / platform.slack.* / platform.matrix.* / platform.feishu.*
namespaces (and the keys for the other adapters wired in follow-up commits).
* i18n(gateway): run_turn / run_turn_runner / approval-settle copy through t()
- status hints, proxy errors, background task notices, progress heartbeats, session info lines
- tool progress chrome (tool_head/tool_pending/tool_preview/tool_verbose) shared by base.format_tool_event
- run_turn_runner:1406 Chinese clarify placeholder -> gateway.clarify.native_stream_placeholder (zh text kept in zh.yaml)
- _UNEXPECTED_SILENCE_REPLY/_CLARIFY_EXPIRED_NOTICE -> accessor functions
* i18n(platforms): route Google Chat and Teams user-facing text through t()
Google Chat clarify card, typing placeholder, orphan-card labels and the whole
/setup-files reply set (module constants become platform.google_chat.setup_files.*
keys resolved at reply time). The attachment-fallback notice that shipped
hardcoded in Spanish is keyed with an English en value; es.yaml carries the
original Spanish text for those four keys.
Teams approval card header/reason use the gateway.exec_approval.* contract,
_APPROVAL_LABELS becomes a key table resolved at click time, and the meeting
summary writer resolves its section headings/fallbacks per render.
* i18n(platforms): route LINE, WeCom, email, DingTalk, IRC and Home Assistant text through t()
LINE default copy constants become catalog keys resolved in __init__ (the
LINE_*_TEXT / extra.* operator overrides still win); the busy-ack bypass
matcher keys on the leading emoji marker only, so it keeps firing once the
gateway busy heads are localized. WeCom media size/format notices that shipped
hardcoded in Chinese are keyed with English en values and zh.yaml carries the
original Chinese text. DingTalk emotion bubbles resolve per send.
* i18n(cli): route /model switch output and -q status lines through t() (cli.model.*, cli.single_query.*)
Switch-summary labels shared with the gateway reuse gateway.model.* keys
(provider/context/max-output/capabilities/prompt-caching); CLI-only variants
(glyph or no-backtick forms) live under cli.model.*. The hand-padded /model usage
block becomes a (form, description-key) table padded at render time so the
command syntax stays fixed while descriptions translate. -q 'Error:' reuses
gateway.model.error_prefix.
* i18n(cli): route TUI panel/hint/placeholder copy through t() (cli.tui.*)
_APPROVAL_CHOICE_LABELS and _TUI_MODAL_HINTS become key tables resolved at
render time; vault/sudo panel bodies are one catalog value per panel split on
newline; inline plurals use <key>_one/<key>_other. Adds the cli.* namespace
(shared/tui/voice/render/subagents/dock) to locales/en.yaml.
* i18n(cli): voice/wake-word CLI copy through t() (cli.voice.*)
RuntimeError texts raised in _voice_start_recording are human copy (callers
print {e}) and are keyed; the Termux requirement-check match stays English.
Wake state ids stay internal, only their labels localize.
* i18n(cli): live-work dock, subagent monitor and render copy through t()
cli.subagents.* / cli.dock.* / cli.render.*; count fragments pluralize via
_one/_other keys, verdict table holds keys resolved at paint time so width
clipping measures the translated text.
* i18n(gateway): unauthorized/pairing, voice, topics, shutdown, startup, notifications, kanban pings through t()
* i18n(cli): move tips + composer placeholders into the catalog (tips.tNNN / tips.placeholder.pNN)
get_random_tip()/get_random_composer_placeholder() pick a key from the English catalog
(the parity baseline, probed once per process) and resolve it through t() for the active
language, so language packs translate tips like any other string. Also lands the cli.*
en.yaml namespace consumed by the CLI info/help/error-copy wiring in the next commit.
* i18n(cli): wire chat-turn + session mixins through t(); kanban log trimmer matches t() output
* test(cli): assert TUI/dock/voice copy via t(key); prove labels resolve at render time
Pinned-English assertions in the approval-UI, live-work dock and voice tests now
go through the catalog. New test swaps the catalog after import and checks the
approval panel + hint row follow it (the reason _APPROVAL_CHOICE_LABELS and
_TUI_MODAL_HINTS became key tables).
* i18n(cli): route CLI info/help/error copy through t() (cli.* namespace)
cli_info_mixin: /help consumes CommandDef.describe() (added to commands.py: slash.<name>.description
with fallback to .description), section titles/skill/quick-command headers, /tools, /toolsets,
/usage labels, /context, /whoami, /insights, /gateway status, tool-progress labels, bang-shell
denials, MCP config-watch + /reload-mcp confirm/reload lines, /reload-skills, and the session-store
warning all read the catalog at call time (module-level label tables became functions so the
active language is honoured after startup). cli.py: worktree cleanup, tirith warning, show_config
(labels re-padded at print time), quick/plugin/skill slash-command errors, ambiguous-command hint,
stdin error, gateway start, profile warning. cli_chat_error_copy / cli_unknown_command /
cli_output / cli_init_mixin: chat error panel copy, did-you-mean lines, n-more / yes-no prompt
(localized affirmative initial alongside 'y'), unknown-toolsets warning.
* i18n(w1a): wire agent display/explainers/approval + slash registry/help through t()
- hermes_cli/commands.py: CommandDef.describe() resolves slash.<name>.description
at call time; category labels via slash.category.*; help/alias/usage suffixes
via slash.shared.*; gateway_help_lines and commands_platforms/slash_exec use them.
- agent/display.py: display.verb.* resolved at call time via get_tool_verb();
bridge/spinner/thinking-verb/cute-row/failure/preview/diff text via display.*.
- agent/turn_explainers.py: exit-reason / persistence-cause tables become call-time
lookups (explainer.exit.*, explainer.persistence.*, explainer.file_mutation.*).
- agent/background_review.py, session_activity.py, context_breakdown.py,
status_output.py: review summaries, iteration progress, context notices.
- tools/approval.py, approval_context.py: approval.summary.*, approval.noun.*,
approval.window.* pluralized keys.
- gateway/slash_commands*.py: remaining raw strings (busy, whoami, platform,
bundles, memory, skills, approvals, set_home, diff, update, debug, profile,
heartbeat, refine, review, subgoal, loop, retry, compress codex path, save,
sessions, model guard/errors, agents rows, topup, login). HISTORY_UNREADABLE
keeps its English constant; callers use history_unreadable() ->
gateway.shared.history_unreadable.
- locales/en.yaml: new approval/display/explainer/slash blocks + gateway leftovers.
* i18n(telegram): route adapter chat copy through t()
Approval card (header/reason/smart-deny as HTML-escaped properties), inline
button labels, callback toasts (cut at Telegram's 200-char cap), model/choice
pickers, clarify/update/slash-confirm prompts, gmail-triage labels and the
inbound-media failure notice now come from the catalog. _UNAUTHORIZED is a
lazy _unauthorized() so the import no longer binds a language. The command
menu carries a language+payload fingerprint (forum scopes re-register on
change) and BotCommand descriptions are cut at 256.
Adds gateway.exec_approval.* (WAVE2 contract), platform.telegram.*,
platform.discord.* and the slash.*.description keys the Discord table shares
with the CLI registry to locales/en.yaml.
* i18n(gateway/platforms): whatsapp_cloud, yuanbao, weixin, signal, api_server copy through t()
- whatsapp_cloud: clarify list/buttons, approve/deny + slash-confirm labels via platform.whatsapp.* (t()-then-truncate at 20/24/72 caps); _EA_HEADER becomes a property wrapping ea_header_text()
- yuanbao: SLOW_RESPONSE_MESSAGE -> slow_response_message() (platform.yuanbao.slow_response_notice; zh keeps the original text); cron-wrapper markers centralized as module constants for strip_cron_wrapper
- api_server: PROVIDER_AUTH_FAILED_LABEL/PROVIDER_RATE_LIMITED_LABEL stay English for run.py matchers; user_text() renders via t()
- signal/_format_wait, weixin voice caption, openai_routes transformed notice
- run_turn: second _UNEXPECTED_SILENCE_REPLY consumer -> accessor
* i18n(discord): route adapter chat copy through t()
Native slash-command table becomes _NATIVE_SLASH_COMMAND_SPECS holding catalog
keys; _native_slash_commands() resolves descriptions, parameter descriptions
and Choice names for the active language, each cut at Discord's 100-char cap,
and the app-command sync fingerprint now includes get_language() so a
display.language change re-syncs. Exec-approval card (gateway.exec_approval.*
contract), slash-confirm / clarify / update views, model+choice pickers,
thread creation, forum titles, voice acks, the response-truncation notice,
the unauthorized-slash security alert and the media upload-size notices all
read from platform.discord.*. Decorator-declared button labels are relabelled
in __init__ (80-char cap); embed titles cut at 256, select placeholders at
150, option label/description at 100. _UNAUTHORIZED is a lazy _unauthorized().
* i18n(cli): wire status-bar, stream, terminal mixins + terminal_input through t(); rename kwargs that shadow t(key)
* i18n: wire hermes_cli/cli_commands_mixin.py slash-command copy through t()
- 431 new leaves under cli.commands.<cmd>.* in locales/en.yaml; 12 rows reuse
existing gateway.* keys (rollback, diff, resume, branch, btw, model, reasoning)
via a _gt() helper so CLI and gateway replies stay identical.
- Module-level English tables (_BUSY_MODE_*, _REASONING_TOGGLES, _HATCH_PROGRESS,
_DIFF_LABELS, _LOCAL_ENGINE_LINES) become call-time catalog lookups keyed by id.
- Verb tables (Enabling/Disabling, Paused/Resumed/Triggered, planned/done,
Updating/Generating) are one full template per variant; plurals use
<key>_one/<key>_other via _tn(); hand-padded column labels (/snapshot list)
translate the value and re-pad at the call site.
- Multi-line usage blocks are single catalog values split with _lines().
- Model-facing system notes and DB-stored reasons stay English (EXCLUDED).
* tests: assert /handoff, /worktree, /login CLI copy via t(key) instead of pinned English
* test(i18n): pin Telegram/Discord adapter catalog wiring
Lazy unauthorized notice, exec-approval contract keys, HTML escaping before
Telegram <b> wrapping, 200-char toast / 256-char BotCommand caps, Discord
100-char app-command text and 80-char button caps, and language-bearing
command-menu fingerprints on both platforms.
* i18n: reconcile cli.shared on/off vs enabled/disabled after lane merge
* i18n: describe() in TUI-gateway slash listings; localize TUI exit resume hint
* i18n(tr): translate bundled catalog + tui pack
* i18n(ja): translate bundled catalog + tui pack
* i18n(ko): translate bundled catalog + tui pack
* i18n(zh): translate bundled catalog + tui pack
* i18n(fr): translate bundled catalog + tui pack
* i18n(af): translate bundled catalog + tui pack
* i18n(uk): translate bundled catalog + tui pack
* i18n(ar): translate bundled catalog + tui pack
* i18n(pt): translate bundled catalog + tui pack
* i18n(it): translate bundled catalog + tui pack
* i18n(es): translate bundled catalog + tui pack
* i18n(zh-hant): translate bundled catalog + tui pack
* i18n(ru): translate bundled catalog + tui pack
* i18n(hu): translate bundled catalog + tui pack
* i18n(hu): translate pre-existing English-valued leftovers (kanban wake, /context, /status, fast labels)
* i18n(de): translate bundled catalog + tui pack
* i18n(ga): translate bundled catalog + tui pack
* test(i18n): fixture matches _normalize_lang(lang, home) signature
* i18n(tui): scaffold userMessages/slashCmd en siblings
* i18n(tui): wire secure prompts + content tables
* feat(tui): i18n — wire billing, subscription, connection-setup and journey overlays
Adds en siblings billing.ts / subscription.ts / connection.ts (namespaces
billing, subscription, connection, journey) and routes every user-facing
literal in billingOverlay, subscriptionOverlay, connectionSetupOverlay and
journey through useT()/messages(). Module-level label tables became lazy
(scopeStillDeniedResult(), verbOf(T, action)); auto-reload rows dispatch on
stable ids instead of label text. Regenerates locales/_keys.tui.json.
* i18n(tui): wire slash ops/wake replies
* i18n(tui): wire pickers (modelPicker, activeSessionSwitcher, petPicker)
* i18n(tui): wire slash core/debug/setup replies
* i18n(tui): wire hubs (agents overlay/panel/controls, skills, plugins)
* i18n(tui): wire slash session/topup/subscription replies
* i18n(tui): wire chat bits (branding, thinking, messageLine, loaders, todo, queued, banner, entry)
* i18n(tui): register t3 siblings (pickers, hubs, secure, content, chatBits) and regenerate keys
* i18n(tui): wire userMessages copy through the userMessages namespace
* i18n(tui): lazy-copy test for userMessages, regenerate _keys.tui.json
* i18n(tui): wire session/gateway/lib text through the TUI catalog (lane t2)
Adds en siblings session.ts, gatewayMsg.ts, libText.ts (namespaces session,
gatewayMsg, libText) and routes user-facing literals in app/{useMainApp,
useSessionLifecycle,useInputHandlers,turnController,createServerRequestHandler,
setupHandoff,createGatewayEventHandler}.ts, gatewayClient displayed reasons,
lib/*, domain/*, hooks/* through t()/messages(). Status-bar state values that
code compares against, backend-matched strings, log lines, model-bound text,
and machine 'error:' prefixes stay literal. Regenerates locales/_keys.tui.json
(232 keys).
* i18n: serve bundled locales/<lang>.tui.yaml under overlay/packs; TUI pack parity test; regen _keys.tui.json (1250)
* i18n: translate pre-existing English stubs in bundled locales (424 leaves, 14 locales)
* tui: i18n-export-en script (English templates for pack translators)
* docs(i18n): bundled TUI packs are the bottom layer of the tui surface
* i18n(ru): translate TUI pack
* i18n(ar): translate TUI pack
* i18n(es): translate TUI pack
* i18n(pt): translate TUI pack
* i18n(ko): translate TUI pack
* i18n(de): translate TUI pack
* i18n(ja): translate TUI pack
* i18n(fr): translate TUI pack
* i18n(tr): translate TUI pack
* i18n(it): translate TUI pack
* i18n(zh): translate TUI pack
* i18n(zh-hant): translate TUI pack
* i18n(hu): translate TUI pack
* i18n(uk): translate TUI pack
1,169 missing keys translated; 81 pre-existing kept byte-identical. Parity OK missing=0 extra=0 placeholder_mismatch=0 empty=0.
Deliberately identical to en: chatBits.branding.mcpSummary ({0} MCP), chatBits.thinking.agentsHint ((/agents)), session.main.voiceStt (◉ STT), session.main.voiceTtsSuffix ( [tts]), slashCmd.core.help.tuiSection (TUI), slashCmd.core.history.hermesTag (Hermes #{0}), slashCmd.debug.heapdump.heapPath (heapdump: {0}), slashCmd.debug.mem.rss (rss), subscription.stepUp.title (Remote Spending — product feature name, as in core catalog), content.faces.* (glyph-only kaomoji).
* i18n(ga): translate TUI pack
* i18n(af): translate TUI pack
* plugin_guard: locale catalogs in language packs step down the agent-config family
A translated status line such as "Updating AGENTS.md" in locales/<lang>.yaml is UI text the loader
reads as a string leaf; it cannot edit a file. The bundled en.yaml itself tripped agent_config_mod
at critical, making any faithful language pack uninstallable. Injection shapes keep full severity.
* plugin_validate_locales: read key exports with utf-8-sig (Windows footgun lint)
* i18n(relay): route relay adapter prompt copy through t(); drop dead import-bound approval header
Adds platform.relay.* (5 keys) to en and all 16 bundled locales, reusing the sibling platform
translations for the confirm buttons and the Other option.
* ci: fix TUI import order, MDX table pipe, main's overflow-warning wording in all locales; fresh-install fixture carries the i18n kernel
- ui-tui/src/i18n/en.ts: perfectionist/sort-imports (slash before slashCmd)
- docs plugins/index.md: escape the | inside the provides_locales table cell (MDX parsed <id> as JSX)
- display.notice.uncompressed_context_overflow: adopt main's wording (names compression.enabled: false
and /compact) in en + 16 locales; the guardrail test pins that phrase
- tests/scripts/test_fresh_source_install.py: the installer tail now resolves CLI text through
agent.i18n, so the fixture tree carries the i18n kernel + en.yaml (not the agent runtime)
* docs(desktop-plugin-sdk): double-backtick the template-literal example (MDX evaluated ${n})
* test(e2e): display.language is validated against the live language set; exclude it from the arbitrary-string set property
* commands: keep the localized COMMANDS/COMMANDS_BY_CATEGORY module __getattr__ after the compat block removal
* build: never write locales/_keys.*.json from the desktop/TUI builds; regenerate the committed desktop key export
The desktop build regenerated locales/_keys.desktop.json in the checkout, so a
hermes update that rebuilt the app left the tree dirty (Desktop update E2E:
'M locales/_keys.desktop.json'). The key exports are committed artifacts pinned
to en.ts by apps/desktop/scripts/i18n-keys.test.mjs and ui-tui i18n:keys:check;
builds read them, never write them. Regenerated after main's new desktop strings.
* test: unbreak two main-red timing tests the PR merge-ref inherits
- test_local_runtime racing fake publishes the modern state record (legacy pid-only
records are rejected since 65ff3ad353; main has been red on this test since)
- test_run_progress_topics ManyProgressLinesAgent waits for the first bubble instead of
a fixed 0.35s, which a loaded CI runner does not always meet
* chore(i18n): regenerate desktop key catalog for main's new strings (model pricing, copy changelog)
* test(e2e): torture-chamber fd monitor confirms a deleted sidecar is still held before calling it a leak
SQLite's WAL last-close unlinks -shm before closing its descriptor (unixShmUnmap, then
unixShmPurge), so a healthy close shows a (deleted) -shm for microseconds; the 20ms poll
occasionally caught that window on the short-lived opener and failed the episode.
* chore(i18n): regenerate desktop key catalog for main's telemetry/consent strings
* chore(i18n): regenerate desktop key catalog after main sync
---------
Co-authored-by: Teknium <[email protected]>
1118 lines
65 KiB
Python
1118 lines
65 KiB
Python
"""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 inside the bodies to avoid
|
|
the import cycle."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import contextlib
|
|
import dataclasses
|
|
import logging
|
|
import os
|
|
import shlex
|
|
from typing import Optional, Union
|
|
|
|
from agent.i18n import t
|
|
from agent.turn_context import extract_api_content_sidecar
|
|
from gateway.config import Platform
|
|
from gateway.platforms.base import EphemeralReply
|
|
from gateway.platforms.event import MessageEvent, MessageType
|
|
from gateway.session import SessionSource, build_session_key, is_shared_multi_user_session
|
|
from gateway.session_transcript import TranscriptReadError
|
|
from gateway.slash_commands_branch_thread import (
|
|
BRANCH_THREAD_PLATFORMS, branch_dest_source, branch_thread_parent, format_thread_ref, parse_branch_args,
|
|
)
|
|
from gateway.slash_commands_status import history_unreadable
|
|
|
|
logger = logging.getLogger("gateway.run") # log-record parity with gateway/run.py
|
|
|
|
# Bound on the off-loop agent cleanup during /new; past it the reset proceeds and the teardown
|
|
# finishes (or leaks) in its worker thread rather than blocking the event loop.
|
|
_RESET_CLEANUP_TIMEOUT_S = 30.0
|
|
# chat_type values whose session key is per-user (DM-like), incl. the unknown/blank case.
|
|
_DM_CHAT_TYPES = {"dm", "direct", "private", ""}
|
|
|
|
_BRANCH_COPIED_FIELDS = ("content", "tool_calls", "tool_call_id", "finish_reason", "reasoning",
|
|
"reasoning_content", "reasoning_details", "codex_reasoning_items",
|
|
"codex_message_items", "timestamp")
|
|
|
|
|
|
def _sattr(obj, name: str) -> str:
|
|
"""``str(getattr(obj, name) or "")`` — normalized identity field for origin comparisons."""
|
|
return str(getattr(obj, name, "") or "")
|
|
|
|
|
|
def _manual_compression_reply_lines(summary: dict, compressor, focus_topic) -> list[str]:
|
|
"""Manual /compress confirmation lines, surfacing summariser/aux-model failures.
|
|
``_last_compress_aborted`` = no usable summary, messages unchanged. Provider exception text is
|
|
force-redacted at this UI boundary even when global redaction is off; an aux model recovered
|
|
via main is an info note so the user can fix their config."""
|
|
lines = [f"🗜️ {summary['headline']}"]
|
|
if focus_topic:
|
|
lines.append(t("gateway.compress.focus_line", topic=focus_topic))
|
|
lines.append(summary["token_line"])
|
|
if summary["note"]:
|
|
lines.append(summary["note"])
|
|
summary_err = getattr(compressor, "_last_summary_error", None)
|
|
if summary_err:
|
|
from agent.redact import redact_sensitive_text
|
|
summary_err = redact_sensitive_text(summary_err, force=True)
|
|
aux_fail_model = getattr(compressor, "_last_aux_model_failure_model", None)
|
|
if getattr(compressor, "_last_compress_aborted", False):
|
|
lines.append(t("gateway.compress.aborted", error=(summary_err or t("gateway.shared.unknown_error"))))
|
|
elif aux_fail_model:
|
|
aux_err = getattr(compressor, "_last_aux_model_failure_error", None) or t("gateway.shared.unknown_error")
|
|
lines.append(t("gateway.compress.aux_failed", model=aux_fail_model, error=aux_err))
|
|
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)."""
|
|
with contextlib.suppress(Exception):
|
|
from tools.env_passthrough import clear_env_passthrough
|
|
clear_env_passthrough()
|
|
with contextlib.suppress(Exception):
|
|
from tools.credential_files import clear_credential_files
|
|
clear_credential_files()
|
|
|
|
|
|
def _branch_row(msg: dict) -> dict:
|
|
"""/branch child row; keeps the api_content sidecar so the branch's first turn replays the
|
|
parent's exact wire bytes (warm provider prompt cache) instead of a cold prefill."""
|
|
row = {k: msg.get(k) for k in _BRANCH_COPIED_FIELDS}
|
|
row["role"] = msg.get("role", "user")
|
|
row["tool_name"] = msg.get("tool_name") or msg.get("name")
|
|
row["api_content"] = extract_api_content_sidecar(msg)
|
|
return row
|
|
|
|
|
|
def _strip_resume_name(parts: list[str]) -> str:
|
|
"""Join the non-flag /resume tokens; strip literal ``<...>``/``[...]``/quotes typed from the
|
|
usage hint (mirrors the CLI)."""
|
|
name = " ".join(p for p in parts if p not in {"--all", "--cross-room"}).strip()
|
|
if len(name) >= 2 and (name[0], name[-1]) in {("<", ">"), ("[", "]"), ('"', '"'), ("'", "'")}:
|
|
name = name[1:-1].strip()
|
|
return name
|
|
|
|
|
|
class GatewaySessionCommandsMixin:
|
|
"""Session-transcript slash commands (/new, /resume, /sessions, /branch, /title, /save, /undo, /retry, /topic, /compress)."""
|
|
|
|
# ------------------------------------------------------------------ /new, /reset
|
|
|
|
async def _cleanup_old_agent_for_reset(self, session_key: str) -> None:
|
|
"""Close the old agent's tool resources (sandboxes, browsers, subprocesses) before eviction.
|
|
Blocking work on the event loop (confirm-button click) → offloaded with a bounded timeout.
|
|
wait_for cancels the await, not the worker thread: a wedged teardown keeps running (or
|
|
leaks); the reset proceeds either way."""
|
|
_old_agent = self._cached_agent_for(session_key)
|
|
if _old_agent is None:
|
|
return
|
|
try:
|
|
await asyncio.wait_for(
|
|
self._run_housekeeping_in_executor(self._cleanup_agent_resources, _old_agent),
|
|
timeout=_RESET_CLEANUP_TIMEOUT_S)
|
|
except asyncio.TimeoutError:
|
|
logger.warning(
|
|
"Agent resource cleanup for session %s exceeded %ss during /new reset; proceeding with "
|
|
"reset (the worker thread is left to finish on its own). (#35994)",
|
|
session_key, _RESET_CLEANUP_TIMEOUT_S)
|
|
except Exception as cleanup_exc:
|
|
logger.warning(
|
|
"Agent resource cleanup for session %s failed during /new reset: %s (#35994)",
|
|
session_key, cleanup_exc)
|
|
|
|
async def _fire_session_reset_hooks(self, source: SessionSource, session_key: str, old_sid,
|
|
new_sid) -> None:
|
|
"""Session-boundary hooks: plugin finalize (off-loop + bounded — trace exports can block
|
|
arbitrarily), then session:end and session:reset."""
|
|
platform_value = source.platform.value if source.platform else ""
|
|
with contextlib.suppress(Exception):
|
|
await self._finalize_session_off_loop(
|
|
session_id=old_sid, platform=platform_value, reason="new_session",
|
|
old_session_id=old_sid, new_session_id=new_sid)
|
|
hook_payload = {"platform": platform_value, "user_id": source.user_id, "session_key": session_key}
|
|
await self.hooks.emit("session:end", dict(hook_payload))
|
|
await self.hooks.emit("session:reset", dict(hook_payload))
|
|
|
|
async def _handle_reset_command(self, event: MessageEvent) -> Union[str, EphemeralReply]:
|
|
"""Handle /new or /reset command."""
|
|
source = event.source
|
|
session_key = self._session_key_for_source(source)
|
|
self._invalidate_session_run_generation(session_key, reason="session_reset")
|
|
# Evict the running-agent slot now that the generation is bumped: the in-flight run's own
|
|
# guarded release (old generation) returns False and would leave a zombie slot that silently
|
|
# drops all later messages. Idempotent, so the run's finally calling it again is harmless.
|
|
self._release_running_agent_state(session_key)
|
|
# Snapshot the old entry so on_session_finalize can report the expiring session id.
|
|
# Evict the running-agent slot now that the generation is bumped. The in-flight run's own guarded
|
|
# release (run_generation=old) will return False and leave its dead agent behind; clearing here
|
|
# keeps the slot from becoming a zombie that silently drops all later messages (#28686). Idempotent,
|
|
# so the run's finally calling it again is harmless.
|
|
old_entry = self.session_store._entries.get(session_key)
|
|
await self._cleanup_old_agent_for_reset(session_key)
|
|
self._evict_cached_agent(session_key)
|
|
# Conversation boundary: ALL conversation-scoped per-session state + security state in one
|
|
# funnel call (see _CONVERSATION_SCOPED_STATE in gateway/run.py).
|
|
self._clear_conversation_scope(session_key, reason="session_reset")
|
|
# In-flight async delegations end WITH the conversation: once the id rotates their
|
|
# completions have no live owner. Expire by durable id, routing key as legacy fallback.
|
|
with contextlib.suppress(Exception):
|
|
from tools.async_delegation import interrupt_for_session
|
|
interrupt_for_session(session_key=session_key, reason="session_reset",
|
|
parent_session_id=str(getattr(old_entry, "session_id", "") or ""))
|
|
_reset_process_scoped_tool_state()
|
|
|
|
new_entry = await self.async_session_store.reset_session(session_key)
|
|
_old_sid = old_entry.session_id if old_entry else None
|
|
await self._fire_session_reset_hooks(source, session_key, _old_sid,
|
|
new_entry.session_id if new_entry else None)
|
|
# Scoped to the profile serving this source so a multiplexed /new banner reports the
|
|
# profile's model, not the base config's.
|
|
try:
|
|
session_info = await asyncio.to_thread(self._reset_notice_session_info, source)
|
|
except Exception:
|
|
session_info = ""
|
|
if new_entry:
|
|
default_header = t("gateway.reset.header_default")
|
|
else: # no existing session: create one
|
|
new_entry = await self.async_session_store.get_or_create_session(source, force_new=True)
|
|
default_header = t("gateway.reset.header_new")
|
|
header = await asyncio.to_thread(self._telegram_topic_new_header, source) or default_header
|
|
_title_arg = event.get_command_args().strip()
|
|
if _title_arg and self._session_db and new_entry:
|
|
header = await self._reset_titled_header(header, new_entry.session_id, _title_arg)
|
|
# Telegram DM topic lane: rebind (chat_id, thread_id) → session_id so the next message uses
|
|
# the fresh session instead of switching back to the old one.
|
|
if await asyncio.to_thread(self._is_telegram_topic_lane, source) and new_entry is not None:
|
|
try:
|
|
await asyncio.to_thread(self._record_telegram_topic_binding, source, new_entry)
|
|
except Exception:
|
|
logger.debug("Failed to rebind Telegram topic after /new", exc_info=True)
|
|
_new_sid = new_entry.session_id if new_entry else None
|
|
# Plugin on_session_reset hook (new session guaranteed to exist); best-effort.
|
|
try:
|
|
from hermes_cli.lifecycle import invoke_hook as _invoke_hook
|
|
_invoke_hook("on_session_reset", session_id=_new_sid, reason="new_session",
|
|
platform=source.platform.value if source.platform else "",
|
|
old_session_id=_old_sid, new_session_id=_new_sid)
|
|
except Exception:
|
|
pass
|
|
try:
|
|
from hermes_cli.tips import get_random_tip
|
|
_tip_line = t("gateway.reset.tip", tip=get_random_tip())
|
|
except Exception:
|
|
_tip_line = ""
|
|
body = f"{header}\n\n{session_info}" if session_info else header
|
|
return EphemeralReply(f"{body}{_tip_line}")
|
|
|
|
async def _reset_titled_header(self, header: str, session_id: str, title_arg: str) -> str:
|
|
"""``/new <title>``: titled header on success, else the header plus a rejection note."""
|
|
from hermes_state import SessionDB
|
|
note = ""
|
|
try:
|
|
sanitized = SessionDB.sanitize_title(title_arg)
|
|
except ValueError as e:
|
|
sanitized = None
|
|
note = t("gateway.reset.title_rejected", error=str(e))
|
|
if sanitized:
|
|
try:
|
|
await self._session_db.set_session_title(session_id, sanitized)
|
|
header = t("gateway.reset.header_titled", title=sanitized)
|
|
except ValueError as e:
|
|
note = t("gateway.reset.title_error_untitled", error=str(e))
|
|
except Exception:
|
|
pass
|
|
elif not note: # sanitize_title returned empty (whitespace-only / unprintable)
|
|
note = t("gateway.reset.title_empty_untitled")
|
|
return header + note
|
|
|
|
# ------------------------------------------------------- origin / ownership guards
|
|
|
|
def _gateway_session_origin_for_id(self, session_id: str) -> Optional[SessionSource]:
|
|
"""Best-effort origin lookup for gateway session IDs."""
|
|
lookup = getattr(type(self.session_store), "lookup_by_session_id", None)
|
|
if callable(lookup):
|
|
entry = lookup(self.session_store, session_id)
|
|
return getattr(entry, "origin", None) if entry is not None else None
|
|
# Test doubles / older stores lack the public lookup; fail closed when nothing resolves.
|
|
entries = getattr(self.session_store, "_entries", {}) or {}
|
|
return next((getattr(e, "origin", None) for e in entries.values()
|
|
if getattr(e, "session_id", None) == session_id), None)
|
|
|
|
@staticmethod
|
|
def _same_matrix_room(current: SessionSource, origin: Optional[SessionSource]) -> bool:
|
|
# thread_id is part of the session key, so another thread of the SAME room is a DIFFERENT
|
|
# session; non-threaded rooms compare "" == "".
|
|
return (origin is not None and origin.platform == Platform.MATRIX
|
|
and current.platform == Platform.MATRIX and origin.chat_id == current.chat_id
|
|
and _sattr(current, "thread_id") == _sattr(origin, "thread_id"))
|
|
|
|
def _same_origin_chat(self, current: SessionSource, origin: Optional[SessionSource]) -> bool:
|
|
"""Platform-agnostic counterpart to ``_same_matrix_room``. Per-participant sessions must be
|
|
participant-scoped here too, else a co-member could resume another member's live session
|
|
(IDOR); only an explicitly shared group/thread shares."""
|
|
if origin is None or current is None:
|
|
return False
|
|
if origin.platform != current.platform or origin.chat_id != current.chat_id:
|
|
return False
|
|
# thread_id is part of every session key: threads of one chat are DIFFERENT sessions.
|
|
if _sattr(current, "thread_id") != _sattr(origin, "thread_id"):
|
|
return False
|
|
if _sattr(current, "chat_type").lower() in _DM_CHAT_TYPES:
|
|
# An equal non-empty chat_id IS the DM key. build_session_key falls back to the
|
|
# participant (``user_id_alt or user_id`` — Signal/Feishu key on alt) only without a
|
|
# chat_id; mirror that and fail closed on a missing/different participant.
|
|
if _sattr(current, "chat_id"):
|
|
return True
|
|
cur_pid = str(current.user_id_alt or current.user_id or "")
|
|
org_pid = str(origin.user_id_alt or origin.user_id or "")
|
|
return bool(cur_pid) and cur_pid == org_pid
|
|
# Non-DM: a shared key is one session for everyone; a per-user key compares the participant
|
|
# it is built from, failing closed when either side lacks one.
|
|
if self._is_shared_session_source(current):
|
|
return True
|
|
cur_pid = current.user_id_alt or current.user_id
|
|
org_pid = origin.user_id_alt or origin.user_id
|
|
return bool(cur_pid and org_pid) and cur_pid == org_pid
|
|
|
|
def _is_shared_session_source(self, source: SessionSource) -> bool:
|
|
"""Whether *source*'s session key is shared by every participant (not per-user); mirrors
|
|
build_session_key's isolation rules so the guards stay in lock-step with the key."""
|
|
return is_shared_multi_user_session(
|
|
source, group_sessions_per_user=getattr(self.config, "group_sessions_per_user", True),
|
|
thread_sessions_per_user=getattr(self.config, "thread_sessions_per_user", False))
|
|
|
|
def _resume_caller_is_admin(self, source: SessionSource) -> bool:
|
|
"""Whether *source* is an EXPLICITLY-configured admin (cross-origin /resume, /sessions).
|
|
Stricter than ``SlashAccessPolicy.is_admin()``, which is True for every caller when slash
|
|
gating is DISABLED — the default config would make everyone cross-origin-capable (IDOR)."""
|
|
try:
|
|
from gateway.slash_access import policy_for_runner_source
|
|
policy = policy_for_runner_source(self, source)
|
|
uid = getattr(source, "user_id", None)
|
|
return bool(policy.enabled and uid and policy.is_admin(uid))
|
|
except Exception:
|
|
return False
|
|
|
|
def _persisted_row_proves_owner(self, source: SessionSource, row: dict) -> bool:
|
|
"""Whether a persisted (inactive) session *row* provably belongs to *source*'s session key.
|
|
Rows once stored only source + user_id, so the persisted chat/thread origin is compared too
|
|
and legacy NULL rows fail closed. The table has no user_id_alt column, so an alt-keyed
|
|
(Signal/Feishu) caller is never proven by user_id alone (CWE-639)."""
|
|
caller_src = source.platform.value if source.platform else None
|
|
row_src = row.get("source")
|
|
caller_uid = _sattr(source, "user_id")
|
|
if not caller_uid:
|
|
return False
|
|
row_thread = str(row.get("thread_id") or "")
|
|
if not (row_src and caller_src and str(row_src) == str(caller_src)
|
|
and row_thread == _sattr(source, "thread_id")):
|
|
return False # blank/legacy source cannot prove the platform; other thread = other session
|
|
row_uid = str(row.get("user_id") or "")
|
|
row_chat = str(row.get("chat_id") or "")
|
|
caller_chat = _sattr(source, "chat_id")
|
|
caller_keys_on_alt = bool(_sattr(source, "user_id_alt"))
|
|
if _sattr(source, "chat_type").lower() in _DM_CHAT_TYPES:
|
|
# A no-chat_id DM is keyed PURELY on the participant (alt-keyed caller fails closed).
|
|
if caller_keys_on_alt and not (row_chat and caller_chat):
|
|
return False
|
|
return bool(row_uid) and row_uid == caller_uid and row_chat == caller_chat
|
|
# Non-DM: both sides must carry chat_id and match (legacy NULL-chat rows fail closed).
|
|
if not (row_chat and caller_chat and row_chat == caller_chat):
|
|
return False
|
|
# A SHARED group/thread is one session for everyone: same-chat proof suffices (a user-id
|
|
# check would block co-members). A per-user key still requires the same owner.
|
|
if self._is_shared_session_source(source):
|
|
return True
|
|
if caller_keys_on_alt:
|
|
return False
|
|
return bool(row_uid) and row_uid == caller_uid
|
|
|
|
async def _resume_target_allowed(self, source: SessionSource, target_id: str,
|
|
allow_override: bool = False) -> bool:
|
|
"""Whether *source* may resume session *target_id* (IDOR guard for every adapter). The live
|
|
origin decides when the target is active; otherwise the DB row must PROVE ownership or fail
|
|
closed. Admin ``--all`` bypasses."""
|
|
if allow_override and self._resume_caller_is_admin(source):
|
|
return True
|
|
# Only a real SessionSource origin decides; unresolvable/error falls through to DB scoping.
|
|
try:
|
|
origin = self._gateway_session_origin_for_id(target_id)
|
|
except Exception:
|
|
origin = None
|
|
if isinstance(origin, SessionSource):
|
|
return self._same_origin_chat(source, origin)
|
|
try:
|
|
row = await self._session_db.get_session(target_id) or {}
|
|
except Exception:
|
|
return False
|
|
return self._persisted_row_proves_owner(source, row)
|
|
|
|
async def _resume_row_visible(self, source: SessionSource, row: dict, allow_all: bool) -> bool:
|
|
"""Whether a listing *row* belongs to the caller's origin (blocks cross-origin enumeration of
|
|
ids/previews); Matrix is room-scoped, ``--all`` needs a configured admin everywhere."""
|
|
if allow_all and self._resume_caller_is_admin(source):
|
|
return True
|
|
sid = str(row.get("id") or "")
|
|
if source.platform == Platform.MATRIX:
|
|
return self._same_matrix_room(source, self._gateway_session_origin_for_id(sid))
|
|
return await self._resume_target_allowed(source, sid, allow_override=False)
|
|
|
|
# ------------------------------------------------------------------ /retry, /undo
|
|
|
|
async def _handle_retry_command(self, event: MessageEvent) -> str:
|
|
"""Handle /retry command - re-send the last user message."""
|
|
# The canonical projection skips bookkeeping rows (role=user + display_kind) and pure
|
|
# handoffs while still recognizing a real ask embedded in a compaction carrier.
|
|
from agent.context_compressor import (
|
|
history_before_user_originated_turn, retryable_user_text, split_user_originated_turn,
|
|
user_originated_turn_view)
|
|
|
|
source = event.source
|
|
session_entry = await self.async_session_store.get_or_create_session(source)
|
|
try:
|
|
history = await self.async_session_store.load_transcript(session_entry.session_id)
|
|
except TranscriptReadError:
|
|
return history_unreadable()
|
|
last_user_idx = next((i for i in range(len(history) - 1, -1, -1)
|
|
if user_originated_turn_view(history[i]) is not None), None)
|
|
if last_user_idx is None:
|
|
return t("gateway.retry.no_previous")
|
|
# Resolve text + scaffold-preserving prefix BEFORE any write; messaging retries cannot
|
|
# reconstruct attachments, so media/unknown content is rejected with the session untouched.
|
|
try:
|
|
truncated, live_view = history_before_user_originated_turn(history, last_user_idx)
|
|
last_user_msg = retryable_user_text(live_view.get("content"))
|
|
handoff, _ = split_user_originated_turn(history[last_user_idx])
|
|
except ValueError as exc:
|
|
return t("gateway.retry.unsafe", error=exc)
|
|
|
|
if handoff is not None:
|
|
# Composite carrier (one row = retained summary + live ask): the carrier-aware rewind
|
|
# archives it and inserts the pure scaffold atomically, reselecting the latest carrier
|
|
# on the same snapshot so a concurrent newer turn is never removed for stale text.
|
|
try:
|
|
# Plain turns keep the existing rewrite path below; #84078 owns its separate
|
|
# archive_dropped/prefix-CAS semantics.
|
|
rewind_result = await self.async_session_store.rewind_session(
|
|
session_entry.session_id, 1, require_retryable_composite=True)
|
|
except ValueError as exc:
|
|
return t("gateway.retry.unsafe", error=exc)
|
|
if rewind_result is None:
|
|
return t("gateway.retry.failed_unchanged")
|
|
last_user_msg = rewind_result["target_text"]
|
|
# active_only preserves the active=0/compacted=1 archive left by in-place compaction.
|
|
elif not await self.async_session_store.rewrite_transcript(
|
|
session_entry.session_id, truncated, active_only=True, reject_active_turn_lease=True):
|
|
return t("gateway.retry.failed_unchanged")
|
|
session_entry.last_prompt_tokens = 0 # transcript was truncated
|
|
self._record_model_friction("retry", source, session_entry.session_id)
|
|
return await self._handle_message(MessageEvent(
|
|
text=last_user_msg, message_type=MessageType.TEXT, source=source,
|
|
raw_message=event.raw_message, channel_prompt=event.channel_prompt))
|
|
|
|
def _record_model_friction(self, signal: str, source, session_id: str, turns: int = 1) -> None:
|
|
"""Slash dispatch does not install the routed profile's scope, so a multiplexed runner
|
|
names the owning home explicitly."""
|
|
from hermes_cli.observability.shared_metrics_model import record_model_friction
|
|
home = None
|
|
if getattr(getattr(self, "config", None), "multiplex_profiles", False):
|
|
with contextlib.suppress(Exception):
|
|
home = self._resolve_profile_home_for_source(source)
|
|
record_model_friction(signal, session_id=session_id, hermes_home=home, turns=turns)
|
|
|
|
async def _handle_undo_command(self, event: MessageEvent) -> str:
|
|
"""Handle /undo [N] — back up N user turns (default 1), soft-deleting the truncated rows and
|
|
echoing the backed-up text; evicts the cached agent so the next turn rebuilds from the
|
|
active-only transcript."""
|
|
source = event.source
|
|
n = 1
|
|
raw_args = event.get_command_args().strip()
|
|
if raw_args:
|
|
try:
|
|
n = max(1, int(raw_args.split()[0]))
|
|
except ValueError:
|
|
return t("gateway.undo.invalid_count", arg=raw_args.split()[0])
|
|
session_entry = await self.async_session_store.get_or_create_session(source)
|
|
result = await self.async_session_store.rewind_session(session_entry.session_id, n)
|
|
if result is None:
|
|
return t("gateway.undo.nothing")
|
|
session_entry.last_prompt_tokens = 0 # transcript was truncated
|
|
self._record_model_friction("undo", source, session_entry.session_id, result.get("turns_undone") or 1)
|
|
try:
|
|
# The cache is keyed by the profile-namespaced key; a bare build_session_key(source)
|
|
# yields ``agent:main:…`` and misses for every secondary profile.
|
|
self._evict_cached_agent(self._session_key_for_source(source))
|
|
except Exception as e:
|
|
logger.debug("undo: cached-agent eviction skipped: %s", e)
|
|
target_text = result["target_text"]
|
|
preview = target_text[:200] + "..." if len(target_text) > 200 else target_text
|
|
return t("gateway.undo.removed", turns=result["turns_undone"],
|
|
count=result["rewound_count"], preview=preview)
|
|
|
|
# --------------------------------------------------------------------- /compress
|
|
|
|
async def _handle_compress_command(self, event: MessageEvent) -> str:
|
|
"""Profile-scoping wrapper around manual /compress: multiplexed gateways resolve credentials
|
|
through the fail-closed per-profile secret scope, which slash dispatch (unlike ``_run_agent``)
|
|
does not install — unscoped, /compress would raise ``UnscopedSecretError``."""
|
|
if not getattr(getattr(self, "config", None), "multiplex_profiles", False):
|
|
return await self._handle_compress_command_inner(event)
|
|
from gateway.run import _profile_runtime_scope
|
|
with _profile_runtime_scope(self._resolve_profile_home_for_source(event.source)):
|
|
return await self._handle_compress_command_inner(event)
|
|
|
|
async def _compress_codex_app_server_session(self, session_key: str, session_id: str) -> str:
|
|
"""Manual /compress for codex_app_server sessions: compacts the LIVE cached agent's
|
|
app-server thread (``force=True`` bypasses the ``codex_app_server_auto`` gate) and keeps it
|
|
cached. A temporary agent or a mirror rewrite cannot shrink the server-side thread.
|
|
|
|
See #73503.
|
|
"""
|
|
from gateway.run import _AGENT_PENDING_SENTINEL
|
|
|
|
agent = self._cached_agent_for(session_key, lockless_fallback=True)
|
|
if agent is None or agent is _AGENT_PENDING_SENTINEL or getattr(agent, "_codex_session", None) is None:
|
|
return t("gateway.compress.codex_nothing")
|
|
compressor = getattr(agent, "context_compressor", None)
|
|
count_before = getattr(compressor, "compression_count", 0)
|
|
try:
|
|
await self._run_in_executor_with_context(
|
|
lambda: agent._compress_context([], "", force=True, task_id=session_id or "default"))
|
|
except Exception as exc:
|
|
return t("gateway.compress.failed", error=exc)
|
|
if getattr(compressor, "compression_count", 0) > count_before:
|
|
return t("gateway.compress.codex_compacted")
|
|
return t("gateway.compress.codex_incomplete")
|
|
|
|
async def _handle_compress_command_inner(self, event: MessageEvent) -> str:
|
|
"""Handle /compress -- manually compress conversation context; ``/compress <focus>`` tells
|
|
the summariser what to preserve. Flags/positional forms are parsed by the shared core."""
|
|
from agent.conversation_compression_manual import MIN_MESSAGES, parse_compress_args
|
|
|
|
source = event.source
|
|
session_entry = await self.async_session_store.get_or_create_session(source)
|
|
try:
|
|
history = await self.async_session_store.load_transcript(session_entry.session_id)
|
|
except TranscriptReadError:
|
|
return history_unreadable()
|
|
if not history or len(history) < MIN_MESSAGES:
|
|
return t("gateway.compress.not_enough")
|
|
request = parse_compress_args(event.get_command_args() or "")
|
|
_agg_note = t("gateway.compress.aggressive_unsupported") if request.aggressive else ""
|
|
if request.aggressive and not request.preview:
|
|
return _agg_note
|
|
if request.preview:
|
|
return _compress_preview_reply(history, request.partial, request.keep_last, request.focus_topic, _agg_note)
|
|
try:
|
|
return await self._run_manual_compression(source, session_entry, history, request)
|
|
except Exception as e:
|
|
logger.warning("Manual compress failed: %s", e)
|
|
return t("gateway.compress.failed", error=e)
|
|
|
|
async def _run_manual_compression(self, source, session_entry, history: list, request) -> str:
|
|
"""Build a temporary agent, run the shared compress core, persist, and describe the outcome."""
|
|
from agent.conversation_compression import finalize_context_engine_compression_notification
|
|
from agent.conversation_compression_manual import compress_now, render_compress_result
|
|
from gateway.run import _platform_config_key
|
|
|
|
session_key = self._session_key_for_source(source)
|
|
# Platform + stable gateway session key bind this agent (for external context engines) to
|
|
# the original conversation, not a default "cli" host.
|
|
platform_key = _platform_config_key(source.platform) if source.platform else None
|
|
model, runtime_kwargs = self._resolve_session_agent_runtime(source=source, session_key=session_key)
|
|
if str(runtime_kwargs.get("api_mode") or "").lower() == "codex_app_server":
|
|
# Context lives in the server-side thread of the LIVE cached agent; a temporary agent
|
|
# has none (and finally-eviction would destroy the real context).
|
|
return await self._compress_codex_app_server_session(session_key, session_entry.session_id)
|
|
if not runtime_kwargs.get("api_key"):
|
|
return t("gateway.compress.no_provider")
|
|
# FULL transcript (tool results included), like auto-compress: user/assistant-only starves
|
|
# tool-result pruning and can trip the protect-first/last early-return.
|
|
msgs = [m for m in history if m.get("role") in {"user", "assistant", "tool"}]
|
|
# Assign, not setdefault (a resolver value would be a stale placeholder); platform only when
|
|
# known so None -> "cli" holds.
|
|
if platform_key is not None:
|
|
runtime_kwargs["platform"] = platform_key
|
|
runtime_kwargs["gateway_session_key"] = session_key
|
|
# Same reasoning setting as a live turn (session ``/reasoning`` > per-model > global): without it
|
|
# the transport applies its default effort — a 400 on non-reasoning models.
|
|
runtime_kwargs["reasoning_config"] = self._resolve_session_reasoning_config(source=source, model=model)
|
|
|
|
tmp_agent = await self._build_manual_compression_agent(session_entry.session_id, model, runtime_kwargs)
|
|
try:
|
|
# Not a bare run_in_executor: the profile secret scope is a contextvar the default
|
|
# executor hop would drop, failing aux-client credential resolution closed.
|
|
result = await self._run_in_executor_with_context(
|
|
lambda: compress_now(tmp_agent, msgs, request, system_message="", skip_without_window=True,
|
|
task_id=session_entry.session_id or "default"))
|
|
if result.status == "nothing_to_do":
|
|
return t("gateway.compress.nothing_to_do")
|
|
if result.status != "compressed":
|
|
return "\n".join(render_compress_result(result))
|
|
await self._persist_manual_compression(tmp_agent, session_entry, source, result.after_messages)
|
|
finalize_context_engine_compression_notification(tmp_agent, committed=True)
|
|
compressor = tmp_agent.context_compressor
|
|
summary = result.summary
|
|
finally:
|
|
finalize_context_engine_compression_notification(tmp_agent, committed=False)
|
|
self._evict_cached_agent(session_key) # next turn rebuilds the prompt from current files
|
|
# Off-loop + bounded: teardown can block on subprocess/network/SQLite.
|
|
await self._cleanup_agent_resources_off_loop(tmp_agent, context="manual compression")
|
|
return "\n".join(_manual_compression_reply_lines(summary, compressor, request.focus_topic))
|
|
|
|
async def _build_manual_compression_agent(self, session_id: str, model, runtime_kwargs: dict):
|
|
"""Build the throwaway AIAgent that performs a manual /compress rewrite of *session_id*."""
|
|
from run_agent import AIAgent
|
|
from gateway.run import _GATEWAY_HYGIENE_PLATFORM, _seed_hygiene_system_prompt
|
|
from hermes_cli.config import load_config as _load_cfg
|
|
from utils import is_truthy_value as _is_truthy
|
|
|
|
# _compress_context may persist its cached system prompt, and this agent runs outside the
|
|
# live session's prompt environment — restore the exact live prompt so provider blocks stay.
|
|
session_row = None
|
|
get_session = getattr(self._session_db, "get_session", None)
|
|
if callable(get_session):
|
|
try:
|
|
session_row = await get_session(session_id)
|
|
except Exception as exc:
|
|
logger.warning(
|
|
"Manual compression could not restore the system prompt for session %s: %s. "
|
|
"Preserving an empty prompt so the live turn rebuilds it with its configured "
|
|
"providers.", session_id, exc, exc_info=True)
|
|
|
|
# compression.checkpoint_required needs the memory provider loaded so _compress_context()
|
|
# can write the pre-compression checkpoint; otherwise keep the fast path (no provider init).
|
|
_checkpoint_required = _is_truthy(
|
|
((_load_cfg() or {}).get("compression") or {}).get("checkpoint_required"),
|
|
default=False)
|
|
tmp_agent = AIAgent(**runtime_kwargs, model=model, max_iterations=4, quiet_mode=True,
|
|
skip_memory=not _checkpoint_required, enabled_toolsets=["memory"],
|
|
session_id=session_id,
|
|
session_db=getattr(self._session_db, "_db", self._session_db))
|
|
_seed_hygiene_system_prompt(tmp_agent, session_row)
|
|
# Real platform during construction (context engines bind correctly); the stamp afterwards
|
|
# only marks this agent as no real surface. Since #104414 Platform is not a restore-identity
|
|
# field, so it no longer forces the next live turn to rebuild; the seed's retain flag is what
|
|
# keeps the reduced-toolset build out of the session row (#122822).
|
|
tmp_agent.platform = _GATEWAY_HYGIENE_PLATFORM
|
|
tmp_agent._print_fn = lambda *a, **kw: None
|
|
# close() must not end the rotated session the gateway entry now points at.
|
|
tmp_agent._end_session_on_close = False
|
|
return tmp_agent
|
|
|
|
async def _persist_manual_compression(self, tmp_agent, session_entry, source, compressed) -> None:
|
|
"""Commit a manual /compress result to the session store. Rotation (new continuation id)
|
|
writes the compressed messages into the NEW session so the original stays searchable;
|
|
persist BEFORE repointing so a failed write is fatal and old history stays reachable.
|
|
In-place compaction already archived + inserted rows, and a rewrite would DELETE the
|
|
archive; an unchanged id without in-place means rotation FAILED."""
|
|
new_session_id = tmp_agent.session_id
|
|
if new_session_id != session_entry.session_id:
|
|
if not await self.async_session_store.rewrite_transcript(new_session_id, compressed):
|
|
raise RuntimeError(
|
|
f"failed to persist compressed transcript for session {new_session_id}")
|
|
session_entry.session_id = new_session_id
|
|
await self.async_session_store._save()
|
|
await asyncio.to_thread(self._sync_telegram_topic_binding, source, session_entry,
|
|
reason="compress-command")
|
|
elif not getattr(tmp_agent, "_last_compaction_in_place", False):
|
|
logger.warning(
|
|
"Manual /compress: session rotation did not occur (session_id unchanged) and in-place "
|
|
"mode is off — preserving original transcript instead of overwriting it (#44794).")
|
|
await self.async_session_store.update_session(session_entry.session_key, last_prompt_tokens=0)
|
|
|
|
# ------------------------------------------------------------------------ /topic
|
|
|
|
async def _handle_topic_command(self, event: MessageEvent, args: str = "") -> str:
|
|
"""Handle /topic for Telegram DM user-managed topic sessions."""
|
|
source = event.source
|
|
if source.platform != Platform.TELEGRAM or source.chat_type != "dm":
|
|
return t("gateway.topic.not_telegram_dm")
|
|
if not self._session_db:
|
|
return self._session_db_unavailable_reply()
|
|
|
|
# Defense in depth: /topic mutates SQLite side tables, so re-check the allowlist here.
|
|
try:
|
|
if not self._is_user_authorized_for_source(source):
|
|
return t("gateway.topic.unauthorized")
|
|
except Exception:
|
|
logger.debug("Topic auth check failed", exc_info=True)
|
|
|
|
args = event.get_command_args().strip()
|
|
if args.lower() in {"help", "?", "-h", "--help"}:
|
|
return self._telegram_topic_help_text()
|
|
if args.lower() in {"off", "disable", "stop"}:
|
|
return await self._disable_telegram_topic_mode_for_chat(source)
|
|
if args:
|
|
if not source.thread_id:
|
|
return t("gateway.topic.restore_needs_topic")
|
|
return await self._restore_telegram_topic_session(event, args)
|
|
|
|
capabilities = await self._get_telegram_topic_capabilities(source)
|
|
if capabilities.get("checked"):
|
|
blocked_key = None
|
|
if capabilities.get("has_topics_enabled") is False:
|
|
blocked_key = "gateway.topic.topics_disabled"
|
|
elif capabilities.get("allows_users_to_create_topics") is False:
|
|
blocked_key = "gateway.topic.topics_user_disallowed"
|
|
if blocked_key: # debounced BotFather screenshot
|
|
if self._should_send_telegram_capability_hint(source):
|
|
await self._send_telegram_topic_setup_image(source)
|
|
return t(blocked_key)
|
|
|
|
profile_name = self._telegram_topic_profile_name(source)
|
|
try:
|
|
await self._session_db.enable_telegram_topic_mode(
|
|
chat_id=str(source.chat_id), user_id=str(source.user_id), profile_name=profile_name,
|
|
has_topics_enabled=capabilities.get("has_topics_enabled"),
|
|
allows_users_to_create_topics=capabilities.get("allows_users_to_create_topics"))
|
|
except Exception as exc:
|
|
logger.exception("Failed to enable Telegram topic mode")
|
|
return t("gateway.topic.enable_failed", error=exc)
|
|
|
|
if not source.thread_id:
|
|
await self._ensure_telegram_system_topic(source)
|
|
return await self._telegram_topic_root_status_message(source)
|
|
try:
|
|
binding = await self._session_db.get_telegram_topic_binding(
|
|
chat_id=str(source.chat_id), thread_id=str(source.thread_id),
|
|
profile_name=profile_name)
|
|
except Exception:
|
|
logger.debug("Failed to read Telegram topic binding", exc_info=True)
|
|
binding = None
|
|
if not binding:
|
|
return t("gateway.topic.thread_ready")
|
|
session_id = str(binding.get("session_id") or "")
|
|
try:
|
|
title = await self._session_db.get_session_title(session_id)
|
|
except Exception:
|
|
title = None
|
|
return t("gateway.topic.bound_status", label=title or t("gateway.topic.untitled_session"),
|
|
session_id=session_id)
|
|
|
|
# ------------------------------------------------------------------ /save, /title
|
|
|
|
async def _handle_save_command(self, event: MessageEvent) -> str:
|
|
"""Handle /save — export the current session and send it as a document."""
|
|
import tempfile
|
|
from hermes_cli.session_export import (
|
|
SAVE_TRANSCRIPT_FORMATS, SAVE_USAGE, default_save_filename, normalize_save_format,
|
|
render_session_for_save)
|
|
|
|
parts = event.get_command_args().split()
|
|
redact = bool(parts) and parts[-1].lower() in ("redact", "--redact")
|
|
if redact:
|
|
parts = parts[:-1]
|
|
if not parts:
|
|
return SAVE_USAGE
|
|
try:
|
|
fmt = normalize_save_format(parts[0])
|
|
except ValueError as e:
|
|
return f"{e}\n\n{SAVE_USAGE}"
|
|
|
|
source = event.source
|
|
session_entry = await self.async_session_store.get_or_create_session(source)
|
|
session_id = session_entry.session_id
|
|
if not self._session_db:
|
|
return t("gateway.shared.session_db_unavailable")
|
|
# Never trust path separators from chat input; the filename is only echoed to the platform.
|
|
filename = parts[1] if len(parts) > 1 else default_save_filename(session_id, fmt)
|
|
filename = os.path.basename(filename) or default_save_filename(session_id, fmt)
|
|
export_data = await self._session_db.export_session(session_id, include_compacted=fmt in SAVE_TRANSCRIPT_FORMATS)
|
|
if not export_data:
|
|
return t("gateway.save.no_messages", session_id=session_id)
|
|
if redact:
|
|
from hermes_cli.session_export_md import redact_session_data
|
|
export_data = redact_session_data(export_data)
|
|
temp_dir = tempfile.mkdtemp(prefix="hermes_save_")
|
|
temp_path = os.path.join(temp_dir, filename)
|
|
try:
|
|
# Off-loop: render + write scale with transcript size (multi-MB) and would stall the loop.
|
|
def _render_and_write() -> None:
|
|
rendered = render_session_for_save(export_data, fmt)
|
|
with open(temp_path, "w", encoding="utf-8") as f:
|
|
f.write(rendered)
|
|
|
|
await asyncio.to_thread(_render_and_write)
|
|
# Profile-aware: under multiplex the requester's bot lives in _profile_adapters, not self.adapters.
|
|
adapter = self._delivery_adapter_for(source)
|
|
if not adapter:
|
|
return t("gateway.save.no_adapter")
|
|
await adapter.send_document(chat_id=source.chat_id, file_path=temp_path,
|
|
caption=t("gateway.save.caption", filename=filename), file_name=filename)
|
|
return t("gateway.save.complete")
|
|
except Exception as e:
|
|
logger.warning("Session /save failed: %s", e)
|
|
return t("gateway.save.failed", error=e)
|
|
finally:
|
|
with contextlib.suppress(Exception):
|
|
os.remove(temp_path)
|
|
os.rmdir(temp_dir)
|
|
|
|
async def _handle_title_command(self, event: MessageEvent) -> str:
|
|
"""Handle /title command — set or show the current session's title."""
|
|
source = event.source
|
|
session_entry = await self.async_session_store.get_or_create_session(source)
|
|
session_id = session_entry.session_id
|
|
if not self._session_db:
|
|
return self._session_db_unavailable_reply()
|
|
|
|
# The session may only exist in session_store so far (first command in a new session).
|
|
# The messaging origin is persisted so a later /resume of this titled-but-inactive session
|
|
# can prove it belongs to the caller's chat/thread (IDOR scoping).
|
|
if await self._session_db.get_session_title(session_id) is None:
|
|
with contextlib.suppress(Exception): # may already exist
|
|
await self._session_db.create_session(
|
|
session_id=session_id,
|
|
source=source.platform.value if source.platform else "unknown",
|
|
user_id=source.user_id, chat_id=source.chat_id, chat_type=source.chat_type,
|
|
thread_id=source.thread_id)
|
|
title_arg = event.get_command_args().strip()
|
|
if not title_arg:
|
|
title = await self._session_db.get_session_title(session_id)
|
|
if title:
|
|
return t("gateway.title.current_with_title", session_id=session_id, title=title)
|
|
return t("gateway.title.current_no_title", session_id=session_id)
|
|
try:
|
|
from hermes_state import SessionDB
|
|
sanitized = SessionDB.sanitize_title(title_arg)
|
|
except ValueError as e:
|
|
return t("gateway.shared.warn_passthrough", error=e)
|
|
if not sanitized:
|
|
return t("gateway.title.empty_after_clean")
|
|
try:
|
|
if not await self._session_db.set_session_title(session_id, sanitized):
|
|
return t("gateway.title.not_found")
|
|
except ValueError as e:
|
|
return t("gateway.shared.warn_passthrough", error=e)
|
|
# Mirror the title onto the Telegram forum topic name (auto titles already do this).
|
|
try:
|
|
await asyncio.to_thread(self._schedule_telegram_topic_title_rename, source, session_id, sanitized)
|
|
except Exception:
|
|
logger.debug("Failed to rename Telegram topic from /title", exc_info=True)
|
|
return t("gateway.title.set_to", title=sanitized)
|
|
|
|
# -------------------------------------------------------------- /resume, /sessions
|
|
|
|
async def _list_titled_sessions(self, source, session_key: str, allow_all: bool) -> list[dict]:
|
|
"""Titled sessions visible to the caller (origin-scoped unless admin ``--all``)."""
|
|
widen = allow_all and self._resume_caller_is_admin(source)
|
|
# Rank by lineage activity, not root started_at: a lineage compressed for days is projected
|
|
# onto its live tip and must sit where the user last touched it (#114271).
|
|
sessions = await self._session_db.list_sessions_rich(
|
|
source=source.platform.value if source.platform else None,
|
|
session_key=None if widen else session_key, limit=10, order_by_last_active=True)
|
|
titled = [s for s in sessions if s.get("title")][:10]
|
|
return [s for s in titled if await self._resume_row_visible(source, s, allow_all)]
|
|
|
|
async def _resolve_resume_target(self, source, session_key: str, name: str, allow_all: bool):
|
|
"""``(target_id, name)`` for a numbered choice, session id or title; else the error reply."""
|
|
if name.isdigit():
|
|
try:
|
|
titled = await self._list_titled_sessions(source, session_key, allow_all)
|
|
except Exception as e:
|
|
logger.debug("Failed to list titled sessions for numeric resume: %s", e)
|
|
return t("gateway.resume.list_failed", error=e)
|
|
index = int(name)
|
|
if index < 1 or index > len(titled):
|
|
return t("gateway.resume.out_of_range", index=index)
|
|
target = titled[index - 1]
|
|
target_id, name = target.get("id"), target.get("title") or name
|
|
else: # session id first, then title
|
|
session = await self._session_db.get_session(name)
|
|
target_id = session["id"] if session else await self._session_db.resolve_session_by_title(name)
|
|
if not target_id:
|
|
return t("gateway.resume.not_found", name=name)
|
|
# Follow compression continuations to the live transcript (matches CLI /resume).
|
|
try:
|
|
# Follow that chain so gateway /resume matches CLI behavior (#15000).
|
|
target_id = await self._session_db.resolve_resume_session_id(target_id)
|
|
except Exception as e:
|
|
logger.debug("Failed to resolve resume continuation for %s: %s", target_id, e)
|
|
return target_id, name
|
|
|
|
async def _resume_access_denied_reply(self, source, target_id: str, name: str, allow_all: bool,
|
|
allow_cross_room: bool) -> Optional[str]:
|
|
"""IDOR guard: a session id/title is a routing handle, not authority — bind /resume to the
|
|
caller's own room (Matrix) or platform/user/chat (other adapters)."""
|
|
if source.platform == Platform.MATRIX:
|
|
target_origin = self._gateway_session_origin_for_id(target_id)
|
|
if self._same_matrix_room(source, target_origin) or allow_cross_room:
|
|
return None
|
|
if target_origin is None:
|
|
return t("gateway.resume.matrix_blocked_no_origin", name=name)
|
|
return t("gateway.resume.matrix_blocked_other_room", name=name,
|
|
room=target_origin.chat_name or target_origin.chat_id)
|
|
if await self._resume_target_allowed(source, target_id, allow_override=(allow_all or allow_cross_room)):
|
|
return None
|
|
return t("gateway.resume.blocked_not_owner", name=name)
|
|
|
|
async def _handle_resume_command(self, event: MessageEvent) -> str:
|
|
"""Handle /resume command — list or switch to a previous session."""
|
|
if not self._session_db:
|
|
return self._session_db_unavailable_reply()
|
|
source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
|
|
session_key = self._session_key_for_source(source)
|
|
try:
|
|
parts = shlex.split(event.get_command_args().strip())
|
|
except ValueError as exc:
|
|
return t("gateway.resume.parse_error", error=exc)
|
|
allow_all = "--all" in parts
|
|
allow_cross_room = "--cross-room" in parts
|
|
name = _strip_resume_name(parts)
|
|
if not name:
|
|
try:
|
|
titled = await self._list_titled_sessions(source, session_key, allow_all)
|
|
return self._resume_listing_reply(source, titled, allow_all)
|
|
except Exception as e:
|
|
logger.debug("Failed to list titled sessions: %s", e)
|
|
return t("gateway.resume.list_failed", error=e)
|
|
|
|
resolved = await self._resolve_resume_target(source, session_key, name, allow_all)
|
|
if isinstance(resolved, str):
|
|
return resolved
|
|
target_id, name = resolved
|
|
denied = await self._resume_access_denied_reply(source, target_id, name, allow_all, allow_cross_room)
|
|
if denied is not None:
|
|
return denied
|
|
current_entry = await self.async_session_store.get_or_create_session(source)
|
|
if current_entry.session_id == target_id:
|
|
return t("gateway.resume.already_on", name=name)
|
|
self._release_running_agent_state(session_key)
|
|
new_entry = await self.async_session_store.switch_session(
|
|
session_key, target_id, preserve_prompt_pin=False,
|
|
)
|
|
if not new_entry:
|
|
return t("gateway.resume.switch_failed")
|
|
# Conversation boundary: all conversation-scoped state + security state in one funnel call.
|
|
# Conversation boundary: clear ALL conversation-scoped per-session state (model/reasoning overrides
|
|
# #10702, one-turn restores, model notes, last-resolved cache #58403, /queue overflow) + security
|
|
# state in one funnel call. See _CONVERSATION_SCOPED_STATE in gateway/run.py.
|
|
self._clear_conversation_scope(session_key, reason="resume")
|
|
# switch_session keeps the route's persisted /model pin (a re-pin is not a boundary,
|
|
# #119864); /resume IS one, and the funnel above clears only in-memory state — without this
|
|
# the next turn's _rehydrate_session_model_override resurrects the pin it just cleared.
|
|
await self.async_session_store.set_model_override(session_key, None)
|
|
# Evict so the next turn rebuilds with the right session_id — the cached AIAgent's memory
|
|
# provider cached _session_id at initialize() and would keep writing to the wrong session.
|
|
self._evict_cached_agent(session_key)
|
|
title = await self._session_db.get_session_title(target_id) or name
|
|
try:
|
|
history = await self.async_session_store.load_transcript(target_id)
|
|
except TranscriptReadError:
|
|
# The resume itself succeeded; only the count is missing — say so rather than "empty".
|
|
return t("gateway.resume.resumed_no_count", title=title) + "\n" + history_unreadable()
|
|
msg_count = len([m for m in history if m.get("role") == "user"]) if history else 0
|
|
if source.platform == Platform.MATRIX and allow_cross_room:
|
|
msg_part = t("gateway.resume.msg_count_one" if msg_count == 1 else "gateway.resume.msg_count_other",
|
|
count=msg_count) if msg_count else ""
|
|
return t("gateway.resume.matrix_cross_room_success", title=title,
|
|
room=source.chat_name or source.chat_id, msg_part=msg_part)
|
|
if not msg_count:
|
|
return t("gateway.resume.resumed_no_count", title=title)
|
|
if msg_count == 1:
|
|
return t("gateway.resume.resumed_one", title=title, count=msg_count)
|
|
return t("gateway.resume.resumed_many", title=title, count=msg_count)
|
|
|
|
def _resume_listing_reply(self, source, titled: list[dict], allow_all: bool) -> str:
|
|
"""Numbered /resume list; a non-admin ``--all`` falls back to same-origin scoping and says so
|
|
(sibling of the /sessions notice)."""
|
|
scope_note = None
|
|
if allow_all and not self._resume_caller_is_admin(source):
|
|
scope_note = t("gateway.resume.all_requires_admin")
|
|
if not titled:
|
|
if source.platform == Platform.MATRIX and not allow_all:
|
|
return t("gateway.resume.matrix_no_named_sessions")
|
|
base = t("gateway.resume.no_named_sessions")
|
|
return f"{base}\n{scope_note}" if scope_note else base
|
|
lines = [t("gateway.resume.list_header")]
|
|
for idx, s in enumerate(titled[:10], start=1):
|
|
title = s["title"]
|
|
if source.platform == Platform.MATRIX and allow_all:
|
|
origin = self._gateway_session_origin_for_id(str(s.get("id") or ""))
|
|
if origin:
|
|
title = f"{title} — {origin.chat_name or origin.chat_id}"
|
|
preview = s.get("preview", "")[:40]
|
|
preview_part = t("gateway.resume.list_preview_suffix", preview=preview) if preview else ""
|
|
lines.append(t("gateway.resume.list_item_numbered", index=idx, title=title, preview_part=preview_part))
|
|
if scope_note:
|
|
lines.append(scope_note)
|
|
lines.append(t("gateway.resume.list_footer_numbered"))
|
|
return "\n".join(lines)
|
|
|
|
async def _handle_sessions_command(self, event: MessageEvent) -> str:
|
|
"""Handle /sessions — list previous sessions for gateway chats."""
|
|
if not self._session_db:
|
|
return self._session_db_unavailable_reply()
|
|
from hermes_cli.session_listing import (
|
|
format_gateway_session_listing, parse_session_listing_args, query_session_listing)
|
|
try:
|
|
include_all, include_unnamed, target, search_query = parse_session_listing_args(
|
|
event.get_command_args().strip())
|
|
except ValueError as exc:
|
|
return t("gateway.resume.parse_error", error=exc)
|
|
if search_query == "":
|
|
return t("gateway.sessions.usage_search")
|
|
if target:
|
|
return await self._handle_resume_command(dataclasses.replace(event, text=f"/resume {target}"))
|
|
source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
|
|
session_key = self._session_key_for_source(source)
|
|
# `/sessions all` is admin-only like `/resume --all` (else any caller could enumerate other
|
|
# origins' ids/titles/previews); a non-admin gets explicit feedback, not a silent narrowing.
|
|
cross_origin = include_all and self._resume_caller_is_admin(source)
|
|
scope_notice = None
|
|
if include_all and not cross_origin:
|
|
scope_notice = t("gateway.sessions.all_requires_admin")
|
|
current_entry = await self.async_session_store.get_or_create_session(source)
|
|
rows = await asyncio.to_thread(
|
|
query_session_listing, getattr(self._session_db, "_db", self._session_db),
|
|
source=source.platform.value if source.platform else None,
|
|
session_key=None if cross_origin else session_key,
|
|
current_session_id=current_entry.session_id, include_current_session=True,
|
|
include_all_sources=cross_origin, include_unnamed=include_unnamed,
|
|
search_query=search_query,
|
|
# Search filters in SQL: over-fetch so origin-invisible matches don't consume the page.
|
|
limit=50 if search_query else 10, exclude_sources=["tool"])
|
|
if not cross_origin:
|
|
rows = [row for row in rows if await self._resume_row_visible(source, row, allow_all=False)]
|
|
rows = rows[:10]
|
|
if search_query:
|
|
title = t("gateway.sessions.title_matching", query=search_query)
|
|
else:
|
|
title = t("gateway.sessions.title_all" if include_unnamed else "gateway.sessions.title_named")
|
|
return format_gateway_session_listing(rows, include_source=cross_origin, title=title,
|
|
notice=scope_notice)
|
|
|
|
# ----------------------------------------------------------------------- /branch
|
|
|
|
async def _handle_branch_command(self, event: MessageEvent) -> str:
|
|
"""Handle /branch [--here] [name] — fork the current session into an independent copy.
|
|
|
|
Thread-capable platforms (Discord/Telegram/Slack/Matrix) open a NEW sibling thread bound
|
|
to the clone and leave this chat on the original session; ``--here`` (and every platform
|
|
without threads) switches the current chat onto the clone instead (#66023).
|
|
"""
|
|
import json as _json
|
|
import uuid as _uuid
|
|
from datetime import datetime as _dt
|
|
|
|
if not self._session_db:
|
|
return self._session_db_unavailable_reply()
|
|
source = event.source
|
|
session_key = self._session_key_for_source(source)
|
|
current_entry = await self.async_session_store.get_or_create_session(source)
|
|
try:
|
|
history = await self.async_session_store.load_transcript(current_entry.session_id)
|
|
except TranscriptReadError:
|
|
return history_unreadable()
|
|
if not history:
|
|
return t("gateway.branch.no_conversation")
|
|
new_session_id = f"{_dt.now().strftime('%Y%m%d_%H%M%S')}_{_uuid.uuid4().hex[:6]}"
|
|
stay_here, branch_title = parse_branch_args(event.get_command_args())
|
|
if not branch_title:
|
|
current_title = await self._session_db.get_session_title(current_entry.session_id)
|
|
branch_title = await self._session_db.get_next_title_in_lineage(current_title or "branch")
|
|
parent_session_id = current_entry.session_id
|
|
# The thread is created BEFORE the clone so a failed create never orphans a branch row;
|
|
# ``None`` = branch in place (--here, no threads here, or the adapter could not open one).
|
|
dest_source = None if stay_here else await self._branch_open_thread(source, branch_title)
|
|
in_place = dest_source is None
|
|
if in_place:
|
|
dest_source = source
|
|
dest_key = session_key if in_place else self._session_key_for_source(dest_source)
|
|
# Full origin (same shape as the reset path in gateway/session.py); the live entry's origin
|
|
# may hold richer metadata than the triggering event's source (#82633). A thread branch
|
|
# is routed by the NEW thread, so its origin is the destination.
|
|
_branch_origin_json = None
|
|
with contextlib.suppress(Exception):
|
|
_origin = (current_entry.origin or source) if in_place else dest_source
|
|
_branch_origin_json = _json.dumps(_origin.to_dict())
|
|
# ``_branched_from`` keeps the branch visible in /resume and /sessions after the parent is
|
|
# reopened and re-ended. ALL routing columns go in at CREATE time: a crash before
|
|
# switch_session() records the peer would otherwise leave the branch unroutable.
|
|
# The child sends the parent's exact system prompt: a row without one makes the branch's
|
|
# first turn rebuild (re-probing the workspace) and forfeits the warm cache the copied
|
|
# transcript buys.
|
|
try:
|
|
parent = await self._session_db.get_session(parent_session_id)
|
|
await self._session_db.create_session(
|
|
session_id=new_session_id,
|
|
source=source.platform.value if source.platform else "gateway",
|
|
model=(self.config.get("model", {}) or {}).get("default") if isinstance(self.config, dict) else None,
|
|
model_config={"_branched_from": parent_session_id},
|
|
parent_session_id=parent_session_id, user_id=dest_source.user_id,
|
|
session_key=dest_key, chat_id=dest_source.chat_id, chat_type=dest_source.chat_type,
|
|
thread_id=dest_source.thread_id, origin_json=_branch_origin_json,
|
|
display_name=current_entry.display_name, system_prompt=(parent or {}).get("system_prompt") or None)
|
|
except Exception as e:
|
|
logger.error("Failed to create branch session: %s", e)
|
|
return t("gateway.branch.create_failed", error=e)
|
|
|
|
# Chunked transactions; best-effort — a failed copy still yields a usable (partial) branch.
|
|
with contextlib.suppress(Exception):
|
|
# Copy conversation history to the new session in bounded-chunk transactions (see #23254): one
|
|
# txn per row was the removed write-amplification pattern, and a history can be hundreds of
|
|
# rows.
|
|
await self._session_db.append_messages_batch(
|
|
new_session_id, [_branch_row(msg) for msg in history], chunk_rows=500)
|
|
with contextlib.suppress(Exception):
|
|
await self._session_db.set_session_title(new_session_id, branch_title)
|
|
if not in_place:
|
|
# Materialize the thread's own entry, then point IT at the clone; ``session_key`` (this
|
|
# chat) is never touched, so the original conversation stays live here.
|
|
await self.async_session_store.get_or_create_session(dest_source)
|
|
new_entry = await self.async_session_store.switch_session(dest_key, new_session_id)
|
|
if not new_entry:
|
|
return t("gateway.branch.switch_failed")
|
|
self._clear_session_boundary_security_state(dest_key)
|
|
self._evict_cached_agent(dest_key)
|
|
msg_count = len([m for m in history if m.get("role") == "user"])
|
|
if in_place:
|
|
key = "gateway.branch.branched_one" if msg_count == 1 else "gateway.branch.branched_many"
|
|
reply = t(key, title=branch_title, count=msg_count, parent=parent_session_id, new=new_session_id)
|
|
if not stay_here and source.platform in BRANCH_THREAD_PLATFORMS:
|
|
reply += "\n" + t("gateway.branch.thread_fallback")
|
|
return reply
|
|
key = "gateway.branch.branched_thread_one" if msg_count == 1 else "gateway.branch.branched_thread_many"
|
|
return t(key, title=branch_title, count=msg_count, parent=parent_session_id, new=new_session_id,
|
|
thread=format_thread_ref(source.platform, dest_source.thread_id))
|
|
|
|
async def _branch_open_thread(self, source: SessionSource, title: str) -> Optional[SessionSource]:
|
|
"""Open the sibling thread a plain ``/branch`` clones into; the destination source, or
|
|
None when this chat cannot host one (in-place fallback)."""
|
|
parent_id = branch_thread_parent(source)
|
|
adapter = self._delivery_adapter_for(source) if parent_id else None
|
|
if adapter is None:
|
|
return None
|
|
try:
|
|
thread_id = await adapter.create_handoff_thread(parent_id, title)
|
|
except Exception:
|
|
logger.warning("Branch: create_handoff_thread failed on %s; branching in place",
|
|
source.platform.value, exc_info=True)
|
|
return None
|
|
if not thread_id:
|
|
return None
|
|
# Discord only answers un-mentioned follow-ups in threads it has participated in.
|
|
threads = getattr(adapter, "_threads", None)
|
|
if threads is not None:
|
|
await threads.mark_async(str(thread_id))
|
|
return branch_dest_source(source, parent_id=parent_id, thread_id=str(thread_id), title=title)
|