Skip to content

The agent that grows with you

fix: a finite prompt answered without running settles failed

Tekniuma1fc4cab887a
diff --git a/gateway/run_turn.py b/gateway/run_turn.py
index 6d2f9c12683..665f8280071 100644
--- a/gateway/run_turn.py
+++ b/gateway/run_turn.py
@@ -2357,11 +2357,18 @@ class GatewayTurnMixin(GatewayTurnPrepareMixin, GatewayTurnHygieneMixin, Gateway
Keys: "final_response", "messages", "api_calls", "completed"."""
if self._get_proxy_url():
- return await self._run_agent_via_proxy(
+ result = await self._run_agent_via_proxy(
message=message, context_prompt=context_prompt, history=history, source=source,
session_id=session_id, session_key=session_key, run_generation=run_generation,
event_message_id=event_message_id, scheduled_heartbeat=scheduled_heartbeat,
)
+ # The remote turn is this admission's executed turn (``TurnRunner.run_sync`` records
+ # the local one): without it a finite run would settle as a no-execution refusal.
+ from gateway.session_results import execution_result
+ captured = execution_result.get()
+ if captured is not None and 'result' not in captured:
+ captured['result'] = result
+ return result
from run_agent import AIAgent
diff --git a/gateway/session_ingress.py b/gateway/session_ingress.py
index 9349f025864..b90e9a4f2e5 100644
--- a/gateway/session_ingress.py
+++ b/gateway/session_ingress.py
@@ -126,9 +126,11 @@ async def execute_admission(authority, ref, row):
# A turn that never ran because it failed (agent initialization raised, history
# unreadable) recorded its own failure via ``record_unexecuted_failure`` on every
# surface; any other reply here is a deliberate notice. An API caller asked for
- # work, so its receipt is a failure either way, never a completed apology.
+ # work, so its receipt is a failure either way, never a completed apology; so did
+ # a finite (``chat -q`` / ``-z``) prompt, whose exit code is the only verdict a
+ # script reads, unless the prompt was a command and the reply is its answer.
result = {'final_response': response or '', 'messages': []}
- if is_api:
+ if is_api or (row['payload'].get('finite') is True and not event.get_command()):
result = {'final_response': '', 'messages': [], 'failed': True, 'completed': False,
'error': response or 'The admitted turn failed.'}
# The drain commits this under the stream lock so no viewer reads `terminal`
diff --git a/tests/gateway/test_admission_results.py b/tests/gateway/test_admission_results.py
index 80a53e2c6ff..f39a458a27b 100644
--- a/tests/gateway/test_admission_results.py
+++ b/tests/gateway/test_admission_results.py
@@ -106,3 +106,43 @@ async def test_a_turn_that_failed_before_running_settles_failed_on_every_surface
assert complete[-1]['status'] == ('error' if fails else 'complete')
finally:
db.close()
+
+
[email protected]('text,outcome', [('do the work', 'failed'), ('/status', 'completed')],
+ ids=['refused-prompt', 'command-answer'])
+async def test_a_finite_prompt_answered_without_running_settles_failed(tmp_path, monkeypatch, text, outcome):
+ """``hermes chat -q`` exits on the committed outcome. A handler that answers a finite prompt
+ with a notice and never runs the turn (drain gate, lease timeout, any stub refusal) did not do
+ the asked work, so the admission settles failed and the CLI exits non-zero; a finite slash
+ command's reply IS its answer and still completes."""
+ import asyncio
+ from types import SimpleNamespace
+ from gateway.config import Platform
+ from gateway.session import SessionSource
+ from gateway.session_authority import LiveSession, SessionAuthority
+ from gateway.session_contract import Principal, SessionRef, Submission
+ from gateway.session_results import admission_result
+ from hermes_cli.turn_exit import turn_exit_code
+ from hermes_state_runtime import get_session_admission
+
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ db = SessionDB(tmp_path / 'state.db')
+ db.create_session('s', source='cli')
+
+ async def handle(event):
+ return 'Gateway is draining for maintenance; your message was not processed.'
+ runner = SimpleNamespace(_draining=False, config=SimpleNamespace(multiplex_profiles=False),
+ _adapter_for_source=lambda source: None, _handle_message=handle)
+ authority = SessionAuthority(runner, profile_id='p', instance_id='owner', db=db,
+ epoch=begin_runtime_epoch(db, instance_id='owner'))
+ authority.sessions['s'] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id='c', user_id='human'), 'route')
+ try:
+ receipt = await authority.submit(Principal('human', 'p', frozenset({'session:submit'}), 't'),
+ Submission('r1', SessionRef('p', 's'), {'text': text, 'finite': True}, 'queue'))
+ await asyncio.wait_for(authority.sessions['s'].task, 10)
+ assert get_session_admission(db, admission_id=receipt.admission_id)['outcome'] == outcome
+ result = admission_result(db, receipt.admission_id)['result']
+ assert turn_exit_code(result, kanban_worker=False) == (1 if outcome == 'failed' else 0)
+ finally:
+ db.close()