Skip to content

The agent that grows with you

fix(gateway): keep a stale Bot pin whose Bot Chat was deleted

Tekniume5eb6b36fe78
diff --git a/gateway/session_bot.py b/gateway/session_bot.py
index de960448d69..60dfd500111 100644
--- a/gateway/session_bot.py
+++ b/gateway/session_bot.py
@@ -126,7 +126,7 @@ async def _scan_owner_receipts(authority, home):
for path, record, mtime in pins:
key, age, legacy = record['delivery_id'], time.time() - mtime, 'owner' in record
if (record['profile_home'] != str(home)
- or _admission_at(authority, record['session_id'], 'bot:' + key) is not None):
+ or _pin_committed(authority, record, 'bot:' + key)):
_warn_malformed(path, _record_shape_error(path, record))
continue
cleared = False
@@ -148,6 +148,17 @@ async def _scan_owner_receipts(authority, home):
return root, records, barrier
+def _pin_committed(authority, record, request_id):
+ """Whether the pin's admission may have committed: a live row, or a deleted target (its
+ admissions moved to retired receipts keyed by the route principal the pin never recorded)."""
+ if _admission_at(authority, record['session_id'], request_id) is not None:
+ return True
+ from hermes_state_mutation_retirement import RETIRED_PREFIX
+ with authority.db._read_ctx() as conn:
+ return conn.execute('SELECT 1 FROM state_meta WHERE key=?',
+ (RETIRED_PREFIX + record['session_id'],)).fetchone() is not None
+
+
def _legacy_order(path, record):
"""``_migrate``'s FIFO key of a legacy ticket, or of the pin laid over one (it keeps the ticket's
fields). A pin without an integer order holds every legacy ticket until it resolves."""
diff --git a/tests/gateway/test_bot_receipt_stale_pin.py b/tests/gateway/test_bot_receipt_stale_pin.py
index 5310e2a1a2c..8be4122539d 100644
--- a/tests/gateway/test_bot_receipt_stale_pin.py
+++ b/tests/gateway/test_bot_receipt_stale_pin.py
@@ -145,3 +145,38 @@ async def test_a_scan_outside_the_owner_withdraws_nothing(bot, caplog):
await scan_receipts(bot.home)
assert path.exists()
assert _scan_warnings(caplog, KEY)
+
+
+
+async def test_a_committed_pin_on_a_deleted_bot_chat_is_kept_and_the_resend_does_not_run_twice(bot, monkeypatch):
+ """A delete moves the admission rows to retired receipts keyed by the route principal, which
+ the pin never recorded: a deleted target must read as "may have committed", never withdrawn."""
+ import asyncio
+ from gateway.session_bot import deliver, recover_bot_deliveries
+ from gateway.session_local import create_local_session
+ from gateway.session_local_title import title_new_session
+ real = type(bot.authority).admit_automation
+ async def committed_then_refused(self, *args):
+ await real(self, *args)
+ raise RuntimeStoreError('stale_epoch')
+ monkeypatch.setattr(type(bot.authority), 'admit_automation', committed_then_refused)
+ with pytest.raises(RuntimeStoreError, match='stale_epoch'):
+ await deliver(bot.connection, dict(id=KEY, profile='default', message='run once'))
+ monkeypatch.setattr(type(bot.authority), 'admit_automation', real)
+ for _ in range(250):
+ rows = list_session_admissions(bot.authority.db, session_id=bot.chat, pending_only=False)
+ if rows and all(row['status'] == 'terminal' for row in rows):
+ break
+ await asyncio.sleep(0.02)
+ assert bot.authority.db.delete_session(bot.chat)
+ path = bot.home / 'runtime' / 'bot_live_delivery' / f'{KEY}.json'
+ _age(path, HOUR)
+ await recover_bot_deliveries(bot.authority)
+ assert path.exists()
+ chat = create_local_session(bot.authority, bot.connection.actor, {
+ 'request_id': 'bot2', 'source': 'gui', 'cwd': str(bot.home), 'model': 'fixture', 'toolsets': []})
+ title_new_session(bot.authority, chat, 'Bot Chat')
+ with pytest.raises(RuntimeStoreError, match='unknown_execution'):
+ await deliver(bot.connection, dict(id=KEY, profile='default', message='run once'))
+ assert list_session_admissions(bot.authority.db, session_id=chat.session_id, pending_only=False) == []