Compare commits
1 Commits
fix/499-ha
...
feat/lazy-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9ba3bfe4fa |
@@ -2304,6 +2304,9 @@ class GatewayRunner:
|
||||
# Get or create session
|
||||
session_entry = self.session_store.get_or_create_session(source)
|
||||
session_key = session_entry.session_key
|
||||
|
||||
# Lazy session creation: persist to SQLite on first real message
|
||||
self.session_store.ensure_db_session(session_entry)
|
||||
|
||||
# Emit session:start for new or auto-reset sessions
|
||||
_is_new_session = (
|
||||
|
||||
@@ -383,6 +383,11 @@ 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
|
||||
|
||||
# Lazy session creation: tracks whether the session record has been
|
||||
# written to SQLite. New sessions start False; the DB write is
|
||||
# deferred until the first user message arrives.
|
||||
_db_persisted: bool = True
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
result = {
|
||||
@@ -763,11 +768,10 @@ class SessionStore:
|
||||
except Exception as e:
|
||||
logger.debug("Session DB operation failed: %s", e)
|
||||
|
||||
if self._db and db_create_kwargs:
|
||||
try:
|
||||
self._db.create_session(**db_create_kwargs)
|
||||
except Exception as e:
|
||||
print(f"[gateway] Warning: Failed to create SQLite session: {e}")
|
||||
# Lazy session creation: defer DB write until first user message.
|
||||
# Mark the entry as not yet persisted; ensure_db_session() will
|
||||
# flush it when the gateway receives an actual message.
|
||||
entry._db_persisted = False
|
||||
|
||||
# Seed new DM thread sessions with parent DM session history.
|
||||
# When a bot reply creates a Slack thread and the user responds in it,
|
||||
@@ -806,6 +810,26 @@ class SessionStore:
|
||||
|
||||
return entry
|
||||
|
||||
def ensure_db_session(self, entry: SessionEntry) -> None:
|
||||
"""Lazily persist a session to SQLite on first user message.
|
||||
|
||||
Called by the gateway message handler when a real message arrives.
|
||||
If the session is already persisted, this is a no-op.
|
||||
"""
|
||||
if entry._db_persisted or not self._db:
|
||||
return
|
||||
try:
|
||||
source_val = entry.platform.value if entry.platform else "unknown"
|
||||
user_id = entry.origin.user_id if entry.origin else None
|
||||
self._db.create_session(
|
||||
session_id=entry.session_id,
|
||||
source=source_val,
|
||||
user_id=user_id,
|
||||
)
|
||||
entry._db_persisted = True
|
||||
except Exception as e:
|
||||
logger.warning("Failed to lazily create SQLite session: %s", e)
|
||||
|
||||
def update_session(
|
||||
self,
|
||||
session_key: str,
|
||||
@@ -865,11 +889,8 @@ class SessionStore:
|
||||
except Exception as e:
|
||||
logger.debug("Session DB operation failed: %s", e)
|
||||
|
||||
if self._db and db_create_kwargs:
|
||||
try:
|
||||
self._db.create_session(**db_create_kwargs)
|
||||
except Exception as e:
|
||||
logger.debug("Session DB operation failed: %s", e)
|
||||
# Lazy: defer DB create until first message
|
||||
new_entry._db_persisted = False
|
||||
|
||||
return new_entry
|
||||
|
||||
|
||||
@@ -69,7 +69,7 @@ class OwnedTwilioNumber:
|
||||
|
||||
|
||||
def _hermes_home() -> Path:
|
||||
return Path(os.environ.get("HERMES_HOME", str(Path.home() / ".hermes")))
|
||||
return Path(os.environ.get("HERMES_HOME", "~/.hermes")).expanduser()
|
||||
|
||||
|
||||
def _env_path() -> Path:
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
"""Tests for hardcoded path fixes in optional skills."""
|
||||
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def hermes_agent_root():
|
||||
return Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
class TestNoHardcodedPaths:
|
||||
def test_telephony_no_tilde_default(self, hermes_agent_root):
|
||||
"""telephony.py should not use ~/.hermes as a default path."""
|
||||
telephony = hermes_agent_root / "optional-skills/productivity/telephony/scripts/telephony.py"
|
||||
content = telephony.read_text()
|
||||
# The _hermes_home function should not use ~/
|
||||
lines = content.split("\n")
|
||||
for line in lines:
|
||||
if "_hermes_home" in line and "def " not in line:
|
||||
assert '"~/.hermes"' not in line, f"Hardcoded ~/.hermes found in telephony.py: {line.strip()}"
|
||||
|
||||
def test_memento_uses_path_home(self, hermes_agent_root):
|
||||
"""memento_cards.py should use Path.home() not hardcoded paths."""
|
||||
memento = hermes_agent_root / "optional-skills/productivity/memento-flashcards/scripts/memento_cards.py"
|
||||
content = memento.read_text()
|
||||
assert "Path.home()" in content, "memento_cards.py should use Path.home()"
|
||||
|
||||
def test_canvas_no_hardcoded_home(self, hermes_agent_root):
|
||||
"""canvas_api.py docstrings are OK, but code should not hardcode paths."""
|
||||
canvas = hermes_agent_root / "optional-skills/productivity/canvas/scripts/canvas_api.py"
|
||||
content = canvas.read_text()
|
||||
# Check that there's no code (non-docstring) using ~/.hermes as a path
|
||||
lines = content.split("\n")
|
||||
for line in lines:
|
||||
stripped = line.strip()
|
||||
if stripped.startswith("#") or stripped.startswith('"'):
|
||||
continue # comments and strings are OK
|
||||
if "~/.hermes" in stripped and ("Path(" in stripped or "os.path" in stripped):
|
||||
pytest.fail(f"Hardcoded ~/.hermes path in code: {stripped}")
|
||||
Reference in New Issue
Block a user