Compare commits

...

1 Commits

Author SHA1 Message Date
Timmy
d05e213075 feat: marathon session limits — cap, checkpoint, rotate (#326)
Some checks failed
Forge CI / smoke-and-build (pull_request) Failing after 28s
- max_messages (default 200) on SessionResetPolicy
- message_count tracking on SessionEntry, persisted
- 'message_limit' reset reason in _should_reset
- auto-checkpoint + session rotation at cap
- near-limit warnings (85%/100%) in ephemeral prompt
- get_message_limit_info() and reset_message_count() APIs
- 24 new tests, 60 existing session tests pass
2026-04-13 20:18:46 -04:00
4 changed files with 190 additions and 6 deletions

View File

@@ -107,6 +107,7 @@ class SessionResetPolicy:
mode: str = "both" # "daily", "idle", "both", or "none"
at_hour: int = 4 # Hour for daily reset (0-23, local time)
idle_minutes: int = 1440 # Minutes of inactivity before reset (24 hours)
max_messages: int = 200 # Max messages per session before forced checkpoint+restart (0 = unlimited)
notify: bool = True # Send a notification to the user when auto-reset occurs
notify_exclude_platforms: tuple = ("api_server", "webhook") # Platforms that don't get reset notifications
@@ -115,6 +116,7 @@ class SessionResetPolicy:
"mode": self.mode,
"at_hour": self.at_hour,
"idle_minutes": self.idle_minutes,
"max_messages": self.max_messages,
"notify": self.notify,
"notify_exclude_platforms": list(self.notify_exclude_platforms),
}
@@ -125,12 +127,14 @@ class SessionResetPolicy:
mode = data.get("mode")
at_hour = data.get("at_hour")
idle_minutes = data.get("idle_minutes")
max_messages = data.get("max_messages")
notify = data.get("notify")
exclude = data.get("notify_exclude_platforms")
return cls(
mode=mode if mode is not None else "both",
at_hour=at_hour if at_hour is not None else 4,
idle_minutes=idle_minutes if idle_minutes is not None else 1440,
max_messages=max_messages if max_messages is not None else 200,
notify=notify if notify is not None else True,
notify_exclude_platforms=tuple(exclude) if exclude is not None else ("api_server", "webhook"),
)

View File

@@ -2343,6 +2343,8 @@ class GatewayRunner:
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.]"
elif reset_reason == "message_limit":
context_note = "[System note: The user's previous session hit the message limit and was rotated. This is a fresh session.]"
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
@@ -2368,16 +2370,17 @@ class GatewayRunner:
if adapter:
if reset_reason == "daily":
reason_text = f"daily schedule at {policy.at_hour}:00"
elif reset_reason == "message_limit":
reason_text = f"reached {policy.max_messages} message limit"
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."
f"◐ Session rotated ({reason_text}). "
f"Use /resume to restore previous session.\n"
f"Adjust limits in config.yaml under session_reset."
)
try:
session_info = self._format_session_info()
@@ -3073,6 +3076,27 @@ class GatewayRunner:
last_prompt_tokens=agent_result.get("last_prompt_tokens", 0),
)
# Marathon limit (#326): auto-rotate at cap
try:
_pl = self.session_store.get_message_limit_info(session_key)
if _pl["at_limit"] and _pl["max_messages"] > 0:
logger.info("[Marathon] Session %s at limit (%d/%d). Rotating.",
session_key, _pl["message_count"], _pl["max_messages"])
try:
from tools.checkpoint_manager import CheckpointManager
_cp = _hermes_home / "config.yaml"
if _cp.exists():
import yaml as _y
with open(_cp) as _f: _d = _y.safe_load(_f) or {}
_cs = _d.get("checkpoints", {})
if _cs.get("enabled"):
CheckpointManager(max_checkpoints=_cs.get("max_checkpoints", 20)).create_checkpoint(
_cs.get("working_dir") or os.getcwd(), label=f"marathon-{session_entry.session_id[:8]}")
except Exception: pass
ne = self.session_store.reset_session(session_key)
if ne: logger.info("[Marathon] Rotated: %s -> %s", session_entry.session_id, ne.session_id)
except Exception: pass
# Auto voice reply: send TTS audio before the text response
_already_sent = bool(agent_result.get("already_sent"))
if self._should_send_voice_reply(event, response, agent_messages, already_sent=_already_sent):
@@ -6538,6 +6562,17 @@ class GatewayRunner:
if self._ephemeral_system_prompt:
combined_ephemeral = (combined_ephemeral + "\n\n" + self._ephemeral_system_prompt).strip()
# Marathon limit warning (#326)
try:
_li = self.session_store.get_message_limit_info(session_key)
if _li["near_limit"] and not _li["at_limit"]:
combined_ephemeral = (combined_ephemeral + "\n\n" +
f"[SESSION LIMIT: {_li['message_count']} msgs. Only {_li['remaining']} left before rotation at {_li['max_messages']}. Wrap up.]").strip()
elif _li["at_limit"]:
combined_ephemeral = (combined_ephemeral + "\n\n" +
f"[SESSION LIMIT REACHED at {_li['max_messages']} msgs. FINAL response. Summarize and exit.]").strip()
except Exception: pass
# Re-read .env and config for fresh credentials (gateway is long-lived,
# keys may change without restart).
try:

View File

@@ -383,7 +383,10 @@ class SessionEntry:
# survives gateway restarts (the old in-memory _pre_flushed_sessions
# set was lost on restart, causing redundant re-flushes).
memory_flushed: bool = False
# Marathon session limit tracking (#326)
message_count: int = 0
def to_dict(self) -> Dict[str, Any]:
result = {
"session_key": self.session_key,
@@ -402,6 +405,7 @@ class SessionEntry:
"estimated_cost_usd": self.estimated_cost_usd,
"cost_status": self.cost_status,
"memory_flushed": self.memory_flushed,
"message_count": self.message_count,
}
if self.origin:
result["origin"] = self.origin.to_dict()
@@ -438,6 +442,7 @@ class SessionEntry:
estimated_cost_usd=data.get("estimated_cost_usd", 0.0),
cost_status=data.get("cost_status", "unknown"),
memory_flushed=data.get("memory_flushed", False),
message_count=data.get("message_count", 0),
)
@@ -643,6 +648,8 @@ class SessionStore:
)
if policy.mode == "none":
if policy.max_messages > 0 and entry.message_count >= policy.max_messages:
return "message_limit"
return None
now = _now()
@@ -664,7 +671,11 @@ class SessionStore:
if entry.updated_at < today_reset:
return "daily"
# Marathon session limit (#326)
if policy.max_messages > 0 and entry.message_count >= policy.max_messages:
return "message_limit"
return None
def has_any_sessions(self) -> bool:
@@ -822,6 +833,32 @@ class SessionStore:
entry.last_prompt_tokens = last_prompt_tokens
self._save()
def get_message_limit_info(self, session_key: str) -> Dict[str, Any]:
"""Get message count and limit info (#326)."""
with self._lock:
self._ensure_loaded_locked()
entry = self._entries.get(session_key)
if not entry:
return {"message_count": 0, "max_messages": 0, "remaining": 0,
"near_limit": False, "at_limit": False, "threshold": 0.0}
policy = self.config.get_reset_policy(platform=entry.platform, session_type=entry.chat_type)
mm = policy.max_messages
c = entry.message_count
return {"message_count": c, "max_messages": mm,
"remaining": max(0, mm - c) if mm > 0 else float("inf"),
"near_limit": mm > 0 and c >= int(mm * 0.85),
"at_limit": mm > 0 and c >= mm,
"threshold": c / mm if mm > 0 else 0.0}
def reset_message_count(self, session_key: str) -> None:
"""Reset message count to zero (#326)."""
with self._lock:
self._ensure_loaded_locked()
entry = self._entries.get(session_key)
if entry:
entry.message_count = 0
self._save()
def reset_session(self, session_key: str) -> Optional[SessionEntry]:
"""Force reset a session, creating a new session ID."""
db_end_session_id = None
@@ -849,6 +886,7 @@ class SessionStore:
display_name=old_entry.display_name,
platform=old_entry.platform,
chat_type=old_entry.chat_type,
message_count=0,
)
self._entries[session_key] = new_entry
@@ -908,6 +946,7 @@ class SessionStore:
display_name=old_entry.display_name,
platform=old_entry.platform,
chat_type=old_entry.chat_type,
message_count=0,
)
self._entries[session_key] = new_entry
@@ -966,6 +1005,15 @@ class SessionStore:
transcript_path = self.get_transcript_path(session_id)
with open(transcript_path, "a", encoding="utf-8") as f:
f.write(json.dumps(message, ensure_ascii=False) + "\n")
# Increment message count (#326)
if message.get("role") != "session_meta":
with self._lock:
for entry in self._entries.values():
if entry.session_id == session_id:
entry.message_count += 1
self._save()
break
def rewrite_transcript(self, session_id: str, messages: List[Dict[str, Any]]) -> None:
"""Replace the entire transcript for a session with new messages.

View File

@@ -0,0 +1,97 @@
"""Tests for marathon session limits (#326)."""
import pytest
from datetime import datetime
from pathlib import Path
from tempfile import mkdtemp
from gateway.config import GatewayConfig, Platform, SessionResetPolicy
from gateway.session import SessionEntry, SessionSource, SessionStore
def _src(p=Platform.LOCAL, c="test"):
return SessionSource(platform=p, chat_id=c, chat_type="dm", user_id="u1")
def _store(mm=200, mode="both"):
cfg = GatewayConfig()
cfg.default_reset_policy = SessionResetPolicy(mode=mode, max_messages=mm)
return SessionStore(Path(mkdtemp()), cfg)
class TestMaxMessages:
def test_default(self): assert SessionResetPolicy().max_messages == 200
def test_custom(self): assert SessionResetPolicy(max_messages=500).max_messages == 500
def test_unlimited(self): assert SessionResetPolicy(max_messages=0).max_messages == 0
def test_to_dict(self): assert SessionResetPolicy(max_messages=300).to_dict()["max_messages"] == 300
def test_from_dict(self): assert SessionResetPolicy.from_dict({"max_messages": 150}).max_messages == 150
def test_from_dict_default(self): assert SessionResetPolicy.from_dict({}).max_messages == 200
class TestMessageCount:
def test_default(self):
e = SessionEntry(session_key="k", session_id="s", created_at=datetime.now(), updated_at=datetime.now())
assert e.message_count == 0
def test_roundtrip(self):
e = SessionEntry(session_key="k", session_id="s", created_at=datetime.now(), updated_at=datetime.now(), message_count=42)
assert e.to_dict()["message_count"] == 42
def test_from_dict(self):
e = SessionEntry.from_dict({"session_key":"k","session_id":"s","created_at":"2026-01-01T00:00:00","updated_at":"2026-01-01T00:00:00","message_count":99})
assert e.message_count == 99
class TestShouldReset:
def test_at_limit(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 200
assert s._should_reset(e, _src()) == "message_limit"
def test_over_limit(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 250
assert s._should_reset(e, _src()) == "message_limit"
def test_below(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 100
assert s._should_reset(e, _src()) is None
def test_unlimited(self):
s = _store(mm=0, mode="none"); e = s.get_or_create_session(_src()); e.message_count = 9999
assert s._should_reset(e, _src()) is None
def test_custom(self):
s = _store(mm=50); e = s.get_or_create_session(_src()); e.message_count = 50
assert s._should_reset(e, _src()) == "message_limit"
def test_just_under(self):
s = _store(mm=50); e = s.get_or_create_session(_src()); e.message_count = 49
assert s._should_reset(e, _src()) is None
class TestAppendCounting:
def test_user(self):
s = _store(); e = s.get_or_create_session(_src())
s.append_to_transcript(e.session_id, {"role":"user","content":"hi"})
assert s.get_or_create_session(_src()).message_count == 1
def test_two(self):
s = _store(); e = s.get_or_create_session(_src())
s.append_to_transcript(e.session_id, {"role":"user","content":"hi"})
s.append_to_transcript(e.session_id, {"role":"assistant","content":"hey"})
assert s.get_or_create_session(_src()).message_count == 2
def test_meta_ignored(self):
s = _store(); e = s.get_or_create_session(_src())
s.append_to_transcript(e.session_id, {"role":"session_meta","tools":[]})
assert s.get_or_create_session(_src()).message_count == 0
class TestLimitInfo:
def test_at_limit(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 200
i = s.get_message_limit_info(e.session_key)
assert i["at_limit"] and i["near_limit"] and i["remaining"] == 0
def test_near(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 180
i = s.get_message_limit_info(e.session_key)
assert i["near_limit"] and not i["at_limit"] and i["remaining"] == 20
def test_below(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 50
i = s.get_message_limit_info(e.session_key)
assert not i["near_limit"] and not i["at_limit"]
def test_unknown(self):
assert not _store().get_message_limit_info("x")["at_limit"]
class TestResetCount:
def test_reset(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 150
s.reset_message_count(e.session_key)
assert s.get_message_limit_info(e.session_key)["message_count"] == 0
class TestRotation:
def test_fresh(self):
s = _store(); e = s.get_or_create_session(_src()); e.message_count = 200
n = s.reset_session(e.session_key)
assert n is not None and n.message_count == 0 and n.session_id != e.session_id