Skip to content

The agent that grows with you

fix(gateway): keep local routing timestamps on the store clock so recovery sweeps finish

teknium11bd373bf91d1
diff --git a/gateway/session_lifecycle.py b/gateway/session_lifecycle.py
index 1c037af6262..7ba7a4f7589 100644
--- a/gateway/session_lifecycle.py
+++ b/gateway/session_lifecycle.py
@@ -249,9 +249,13 @@ class SessionLifecycleMixin:
to avoid touching long-idle sessions. Sets ``resume_pending=True`` so the next incoming message on
the same session_key auto-resumes from the existing transcript.
"""
+ from gateway.config import Platform
cutoff = _now() - timedelta(seconds=max_age_seconds)
def _mark(entry: SessionEntry) -> bool:
+ # Canonical local routes recover through their durable FIFO, never a synthetic resume.
+ if entry.origin is not None and entry.origin.platform == Platform.LOCAL:
+ return False
if entry.resume_pending or entry.suspended or entry.updated_at < cutoff:
return False
entry.resume_pending = True
diff --git a/gateway/session_local.py b/gateway/session_local.py
index 4f4a1a27702..a7e6f07c3f3 100644
--- a/gateway/session_local.py
+++ b/gateway/session_local.py
@@ -118,8 +118,8 @@ def create_local_session(authority, actor, params, *, trusted_policy=None, trust
if not isinstance(request_id, str) or not request_id or len(request_id) > 256:
raise RuntimeStoreError('invalid_params')
from dataclasses import asdict
- from datetime import datetime, timezone
from gateway.session import SessionEntry
+ from gateway.session_lifecycle import _now
from gateway.session_local_recovery import local_identity, restore_local_session
from hermes_state_local import commit_local_session
authority._require_admission_open()
@@ -141,7 +141,9 @@ def create_local_session(authority, actor, params, *, trusted_policy=None, trust
from gateway.session_local_recovery import local_source
source = local_source(authority, sid, actor.subject)
route = authority.runner.session_store._generate_session_key(source)
- now = datetime.now(timezone.utc)
+ # Same clock as every other routing entry: the recovery sweeps compare against a
+ # naive cutoff, and one aware entry aborts the whole iteration.
+ now = _now()
entry = SessionEntry(route, sid, now, now, origin=source, platform=Platform.LOCAL)
commit_local_session(authority.db, epoch=authority.epoch, receipt={
'profile_id': authority.profile_id, 'principal_id': actor.subject, 'request_id': request_id,
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index e13ea8d13e0..a0640e15f54 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -6,7 +6,7 @@ The historical ID becomes the stable logical owner without copying its messages.
Compression and reset may later advance the physical target, never FIFO identity.
"""
from dataclasses import asdict, replace
-from datetime import datetime, timezone
+from datetime import datetime
from pathlib import Path
from gateway.config import Platform
@@ -91,7 +91,7 @@ def adopt_legacy_session(authority, actor, row):
from gateway.session_local_recovery import local_source
source = local_source(authority, sid, actor.subject)
route = authority.runner.session_store._generate_session_key(source)
- now = datetime.fromtimestamp(row['started_at'], timezone.utc)
+ now = datetime.fromtimestamp(row['started_at'])
entry = SessionEntry(route, row['id'], now, now, origin=source, platform=Platform.LOCAL)
receipt = {'profile_id': authority.profile_id, 'principal_id': actor.subject, 'request_id': request_id,
'session_id': sid, 'route': route, 'entry': entry.to_dict(), 'policy': asdict(policy),
diff --git a/tests/gateway/test_local_session_recovery.py b/tests/gateway/test_local_session_recovery.py
index 30d137304ac..f27fcb72988 100644
--- a/tests/gateway/test_local_session_recovery.py
+++ b/tests/gateway/test_local_session_recovery.py
@@ -70,3 +70,32 @@ async def test_local_create_receipt_survives_cold_authority_atomically(tmp_path,
def test_real_daemon_recovers_only_authorized_never_started_local_work(tmp_path):
from tests.gateway.fixtures.local_recovery_probe import probe
print(json.dumps(probe(tmp_path)))
+
+
+async def test_local_entry_timestamps_keep_fallback_recovery_sweeping_other_routes(tmp_path, monkeypatch):
+ """A canonical local entry must not abort the naive-cutoff recovery sweep that marks
+ a recent marker-less messaging route ``resume_pending``."""
+ from gateway.config import GatewayConfig, Platform
+ from gateway.session import SessionSource, SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Principal
+ from gateway.session_local import create_local_session
+ from gateway import run
+
+ monkeypatch.setattr(run, '_load_gateway_config', lambda: {'platform_toolsets': {'cli': []}})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False)
+ authority = await initialize_session_authority(runner, profile_id='fixture', instance_id='first')
+ actor = Principal('owner', 'fixture', frozenset({'session:create', 'session:read'}), 'socket')
+ ref = create_local_session(authority, actor, {'request_id': 'r', 'source': 'gui', 'cwd': str(tmp_path),
+ 'model': 'frozen', 'toolsets': []})
+ telegram = SessionSource(platform=Platform.TELEGRAM, chat_id='7', user_id='7', chat_type='dm')
+ entry = store.get_or_create_session(telegram)
+ assert not entry.resume_pending
+ # Cold store: only the durable index feeds the sweep.
+ cold = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ assert cold.suspend_recently_active(120) >= 1
+ assert cold.lookup_by_session_key(entry.session_key).resume_pending
+ # The canonical local route recovers through its durable FIFO, not the legacy marker.
+ assert not cold.lookup_by_session_key(authority.sessions[ref.session_id].route).resume_pending