The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): write a local chat's cron result as a note, not a turn
unsupportedpastels2352d1d22c1f
diff --git a/cron/scheduler_delivery.py b/cron/scheduler_delivery.pyindex b3711eb21b2..70a7d08e086 100644--- a/cron/scheduler_delivery.py+++ b/cron/scheduler_delivery.py@@ -1896,7 +1896,7 @@ def _deliver_result(from gateway.session_cron import deliver_to_local_chatfrom hermes_constants import get_hermes_homelocal_error = deliver_to_local_chat(get_hermes_home(), target["chat_id"],- bot_chat_message(job, content), diagnostic=for_failure)+ cleaned_delivery_content, diagnostic=for_failure)if local_error:delivery_errors.append(local_error)continuediff --git a/gateway/session_completion_notes.py b/gateway/session_completion_notes.pyindex bcb60876dee..31614062b3b 100644--- a/gateway/session_completion_notes.py+++ b/gateway/session_completion_notes.py@@ -8,8 +8,9 @@ it (``completion_admission``), results already admitted are left out of a new baoutcome, note or turn, covers the whole batch. When that admission comes up for execution with noviewer attached (the authority's own subscriber records) the executor records it in the session'shistory, where the next turn sees it, and settles the admission without a model call. Queue order, restart survival and-exactly-once are the admission contract's. With a viewer attached it runs as the turn it always was;-messaging, cron, Kanban, ``/loop`` and heartbeat wakes never reach here.+exactly-once are the admission contract's. With a viewer attached it runs as the turn it always was,+except a local cron result, which is always a note; messaging, Kanban, ``/loop`` and heartbeat wakes+never reach here."""import jsonfrom functools import partial@@ -17,6 +18,9 @@ from functools import partial# ``_completion_delivery_identity`` kinds: an async delegation's result, a background process's exit# and its watch-pattern notices._NOTED_PRODUCERS = frozenset({'async_delegation', 'completion', 'watch_match', 'watch_disabled'})+# A cron result for a local chat (``session_cron.deliver_to_local_chat``) is finished text: a note+# with or without a viewer, since a turn over it would only spend a model call (#143547 CRON-11).+_ALWAYS_NOTED = frozenset({'cron'})def _producer(identity):@@ -29,7 +33,7 @@ def _producer(identity):def noted_completion(row):"""*row* is a local completion admission: a note when it runs with no viewer attached."""descriptor = (row or {}).get('payload', {}).get('local_automation_v1')- return descriptor is not None and _producer(descriptor['identity']) in _NOTED_PRODUCERS+ return descriptor is not None and _producer(descriptor['identity']) in _NOTED_PRODUCERS | _ALWAYS_NOTEDasync def note_if_unviewed(authority, ref, row):@@ -40,13 +44,13 @@ async def note_if_unviewed(authority, ref, row):return Nonefrom gateway.session_runtime_workers import tracked_writelive = authority.sessions.get(ref.session_id)- if live is None or live.subscribers:+ descriptor = row['payload']['local_automation_v1']+ if live is None or (live.subscribers and _producer(descriptor['identity']) not in _ALWAYS_NOTED):# The claim left a closed row closed for a note; the turn reopens it as any first turn does.from gateway.session_local_recovery import reopen_local_sessionawait tracked_write(authority, partial(reopen_local_session, authority, ref))return Nonefrom gateway.session_viewer_close import _physical_target- descriptor = row['payload']['local_automation_v1']metadata = {'completion_note': sorted(set(descriptor.get('identities') or ()) | {descriptor['identity']})}metadata.update({key: descriptor[key] for key in ('display_text', 'notification_category') if key in descriptor})target = _physical_target(authority, ref.session_id)diff --git a/gateway/session_cron.py b/gateway/session_cron.pyindex be61f4446ac..406e8621de2 100644--- a/gateway/session_cron.py+++ b/gateway/session_cron.py@@ -51,9 +51,10 @@ async def _admit_local_delivery(authority, chat_id, text, metadata):def deliver_to_local_chat(home, chat_id, text, *, diagnostic=False):- """Admit a cron result into local chat *chat_id*'s FIFO, as a Kanban wake. A local chat has no- push transport (``LocalSessionAdapter.send`` only acknowledges a turn's own output), so only the- owner in this process can deliver. Called off the owner loop; None once admitted, else the error."""+ """Admit a cron result into local chat *chat_id*'s FIFO; at the head it is written into the chat's+ history as a note, never a turn (``session_completion_notes``). A local chat has no push transport+ (``LocalSessionAdapter.send`` only acknowledges a turn's own output), so only the owner in this+ process can deliver. Called off the owner loop; None once admitted, else the error."""owner = owner_for_home(home)if owner is None:return f"local chat {chat_id}: no gateway in this process owns its session"diff --git a/tests/gateway/test_local_chat_delivery.py b/tests/gateway/test_local_chat_delivery.pyindex 8693bfbd451..8736ce3e034 100644--- a/tests/gateway/test_local_chat_delivery.py+++ b/tests/gateway/test_local_chat_delivery.py@@ -77,9 +77,14 @@ async def test_overdue_heartbeat_fires_once_with_a_classic_cli_also_polling(tmp_@pytest.mark.asyncio@pytest.mark.parametrize('live_adapters', [False, True], ids=['no-local-adapter-yet', 'in-gateway-ticker'])-async def test_cron_result_for_a_local_origin_is_admitted_not_acknowledged(tmp_path, monkeypatch, live_adapters):[email protected]('viewer', [False, True], ids=['unviewed', 'viewed'])+async def test_cron_result_for_a_local_origin_is_a_note_not_a_turn(tmp_path, monkeypatch, live_adapters, viewer):from cron import scheduler_delivery as sd- runner, authority, ref, _ = await _local(tmp_path, monkeypatch)+ from tests.gateway.test_session_end_delegations import _record_turns+ from tests.gateway.test_unviewed_completion_notes import _target+ runner, authority, ref, actor = await _local(tmp_path, monkeypatch)+ if viewer:+ authority.sessions[ref.session_id].subscribers['viewer'] = actorjob = {'id': 'j1', 'name': 'digest', 'deliver': 'origin','origin': {'platform': 'local', 'chat_id': ref.session_id}}# The ticker delivers from its own thread; the owner admits on its loop.@@ -87,7 +92,15 @@ async def test_cron_result_for_a_local_origin_is_admitted_not_acknowledged(tmp_padapters=runner.adapters if live_adapters else None,loop=asyncio.get_running_loop())assert error is None- assert len(_admissions(authority, ref, 'CRON_RESULT_j1')) == 1+ turns = _record_turns(runner)+ await authority._drain(ref)+ assert turns == [], 'the finished result must not cost a model turn'+ [row] = _admissions(authority, ref, 'CRON_RESULT_j1')+ assert row['outcome'] == 'completed'+ [note] = [m for m in authority.db.get_messages(_target(runner, authority, ref))+ if 'CRON_RESULT_j1' in str(m.get('content'))]+ assert note['role'] == 'user' and note['display_metadata']['completion_admission'] == row['admission_id']+ assert 'summarize for the chat' not in note['content'], 'no model framing on a note'@pytest.mark.asyncio