feat(gateway): notify users when session auto-resets (#2519)
When a session expires (daily schedule or idle timeout) and is
automatically reset, send a notification to the user explaining
what happened:
◐ Session automatically reset (inactive for 24h).
Conversation history cleared.
Use /resume to browse and restore a previous session.
Adjust reset timing in config.yaml under session_reset.
Notifications are suppressed when:
- The expired session had no activity (no tokens used)
- The platform is excluded (api_server, webhook by default)
- notify: false in config
Changes:
- session.py: _should_reset() returns reason string ('idle'/'daily')
instead of bool; SessionEntry gains auto_reset_reason and
reset_had_activity fields; old entry's total_tokens checked
- config.py: SessionResetPolicy gains notify (bool, default: true)
and notify_exclude_platforms (default: api_server, webhook)
- run.py: sends notification via adapter.send() before processing
the user's message, with activity + platform checks
- 13 new tests
Config (config.yaml):
session_reset:
notify: true
notify_exclude_platforms: [api_server, webhook]
This commit is contained in:
parent
5e5ad634a1
commit
cd2280d1a3
4 changed files with 282 additions and 12 deletions
|
|
@ -1694,12 +1694,54 @@ class GatewayRunner:
|
|||
# If the previous session expired and was auto-reset, prepend a notice
|
||||
# so the agent knows this is a fresh conversation (not an intentional /reset).
|
||||
if getattr(session_entry, 'was_auto_reset', False):
|
||||
context_prompt = (
|
||||
"[System note: The user's previous session expired due to inactivity. "
|
||||
"This is a fresh conversation with no prior context.]\n\n"
|
||||
+ context_prompt
|
||||
)
|
||||
reset_reason = getattr(session_entry, 'auto_reset_reason', None) or 'idle'
|
||||
if reset_reason == "daily":
|
||||
context_note = "[System note: The user's session was automatically reset by the daily schedule. This is a fresh conversation with no prior context.]"
|
||||
else:
|
||||
context_note = "[System note: The user's previous session expired due to inactivity. This is a fresh conversation with no prior context.]"
|
||||
context_prompt = context_note + "\n\n" + context_prompt
|
||||
|
||||
# Send a user-facing notification explaining the reset, unless:
|
||||
# - notifications are disabled in config
|
||||
# - the platform is excluded (e.g. api_server, webhook)
|
||||
# - the expired session had no activity (nothing was cleared)
|
||||
try:
|
||||
policy = self.session_store.config.get_reset_policy(
|
||||
platform=source.platform,
|
||||
session_type=getattr(source, 'chat_type', 'dm'),
|
||||
)
|
||||
platform_name = source.platform.value if source.platform else ""
|
||||
had_activity = getattr(session_entry, 'reset_had_activity', False)
|
||||
should_notify = (
|
||||
policy.notify
|
||||
and had_activity
|
||||
and platform_name not in policy.notify_exclude_platforms
|
||||
)
|
||||
if should_notify:
|
||||
adapter = self.adapters.get(source.platform)
|
||||
if adapter:
|
||||
if reset_reason == "daily":
|
||||
reason_text = f"daily schedule at {policy.at_hour}:00"
|
||||
else:
|
||||
hours = policy.idle_minutes // 60
|
||||
mins = policy.idle_minutes % 60
|
||||
duration = f"{hours}h" if not mins else f"{hours}h {mins}m" if hours else f"{mins}m"
|
||||
reason_text = f"inactive for {duration}"
|
||||
notice = (
|
||||
f"◐ Session automatically reset ({reason_text}). "
|
||||
f"Conversation history cleared.\n"
|
||||
f"Use /resume to browse and restore a previous session.\n"
|
||||
f"Adjust reset timing in config.yaml under session_reset."
|
||||
)
|
||||
await adapter.send(
|
||||
source.chat_id, notice,
|
||||
metadata=getattr(event, 'metadata', None),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.debug("Auto-reset notification failed (non-fatal): %s", e)
|
||||
|
||||
session_entry.was_auto_reset = False
|
||||
session_entry.auto_reset_reason = None
|
||||
|
||||
# Load conversation history from transcript
|
||||
history = self.session_store.load_transcript(session_entry.session_id)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue