Skip to content

The agent that grows with you

fix(gateway): refuse goal-card criterion actions that cannot act

yoniebans5721a5066f9f
diff --git a/gateway/session_control_actions.py b/gateway/session_control_actions.py
index 9053978a00d..48fde484910 100644
--- a/gateway/session_control_actions.py
+++ b/gateway/session_control_actions.py
@@ -16,39 +16,32 @@ _ACTION_COMMANDS = {
'goal.clear': ('goal', 'clear'), 'goal.unwait': ('goal', 'unwait'),
'loop.pause': ('loop', 'pause'), 'loop.resume': ('loop', 'resume'), 'loop.stop': ('loop', 'stop'),
'heartbeat.pause': ('heartbeat', 'pause'), 'heartbeat.resume': ('heartbeat', 'resume'),
- 'heartbeat.clear': ('heartbeat', 'clear'), 'subgoal.clear': ('subgoal', 'clear'),
+ 'heartbeat.clear': ('heartbeat', 'clear'),
}
+_SUBGOAL_ACTIONS = frozenset({'subgoal.add', 'subgoal.remove', 'subgoal.clear'})
_ENVELOPE = ('type', 'output', 'notice', 'message', 'display')
-def _command(action, args):
- """``(name, arg)`` of the slash command for *action*, or ``('subgoal', None)`` for an add, whose
- free text must not be parsed as a /subgoal verb ("clear the cache" is a criterion, not a clear)."""
+def _validate(action, args):
if not isinstance(args, dict) or set(args) - {'text', 'index'}:
raise RuntimeStoreError('invalid_params')
- if action in _ACTION_COMMANDS:
- return _ACTION_COMMANDS[action]
- if action == 'subgoal.remove':
- index = args.get('index')
- if type(index) is not int or index < 1:
- raise RuntimeStoreError('invalid_params')
- return 'subgoal', f'remove {index}'
- if action == 'subgoal.add':
- if not isinstance(args.get('text'), str) or not args['text'].strip():
- raise RuntimeStoreError('invalid_params')
- return 'subgoal', None
- raise RuntimeStoreError('invalid_params')
+ if action == 'subgoal.remove' and (type(args.get('index')) is not int or args['index'] < 1):
+ raise RuntimeStoreError('invalid_params')
+ if action == 'subgoal.add' and (not isinstance(args.get('text'), str) or not args['text'].strip()):
+ raise RuntimeStoreError('invalid_params')
+ if action not in _ACTION_COMMANDS and action not in _SUBGOAL_ACTIONS:
+ raise RuntimeStoreError('invalid_params')
-async def _add_subgoal(connection, ref, params, text):
- """``/subgoal <text>``'s add branch (``slash_commands_goals``) on the session's goal manager."""
+async def _subgoal(connection, ref, params, action, args):
+ """A criterion button on the session's goal manager. A goal that is gone or an index that no longer
+ exists is a refusal the card shows; the typed /subgoal command reports the same cases as text."""
from pathlib import Path
from agent.i18n import t
from gateway.platforms.base import MessageEvent
from gateway.run import _profile_runtime_scope
from gateway.session_busy_controls import authorize
from gateway.session_local import authorize_local_source
- from gateway.slash_commands_goals import _mgr_call
authority = connection.authority
authorize(connection, ref, params, 'session:control')
live = authority.sessions[ref.session_id]
@@ -59,12 +52,25 @@ async def _add_subgoal(connection, ref, params, text):
mgr, _entry = await authority.runner._get_goal_manager_for_event(
MessageEvent(text='/subgoal', source=live.source))
if mgr is None:
- return {'type': 'exec', 'output': t('gateway.goal.unavailable')}
+ raise RuntimeStoreError('command_unavailable', detail=t('gateway.goal.unavailable'))
if not mgr.has_goal():
- return {'type': 'exec', 'output': t('gateway.subgoal.no_goal')}
- added, err = _mgr_call('/subgoal', mgr.add_subgoal, text)
- index = len(mgr.state.subgoals) if mgr.state else 0
- return {'type': 'exec', 'output': err or t('gateway.subgoal.added', index=index, text=added)}
+ raise RuntimeStoreError('goal_not_active', detail=t('gateway.subgoal.no_goal'))
+ try:
+ if action == 'subgoal.add':
+ added = mgr.add_subgoal(args['text'].strip())
+ output = t('gateway.subgoal.added', index=len(mgr.state.subgoals), text=added)
+ elif action == 'subgoal.remove':
+ removed = mgr.remove_subgoal(args['index'])
+ output = t('gateway.subgoal.removed', index=args['index'], text=removed)
+ else:
+ count = mgr.clear_subgoals()
+ output = (t('gateway.subgoal.cleared_one' if count == 1 else 'gateway.subgoal.cleared_other', count=count)
+ if count else t('gateway.subgoal.none_to_clear'))
+ except IndexError as exc:
+ raise RuntimeStoreError('invalid_params', field='index', detail=str(exc)) from exc
+ except (RuntimeError, ValueError) as exc:
+ raise RuntimeStoreError('invalid_params', detail=str(exc)) from exc
+ return {'type': 'exec', 'output': output}
def publish(authority, session_id, *, only_if_present=False):
@@ -96,17 +102,19 @@ async def control(connection, ref, params):
if not isinstance(action, str):
raise RuntimeStoreError('invalid_params')
args = {} if params.get('args') is None else params['args']
- name, arg = _command(action.strip(), args)
+ action = action.strip()
+ _validate(action, args)
if ref.session_id not in connection.authority.sessions:
raise RuntimeStoreError('not_found')
scope = {key: params[key] for key in ('session_id', 'profile') if key in params}
# Other viewers (another Desktop window, Ink) repaint their cards from session.control.update, not the
- # reply: execute_command publishes it for the automation commands, the subgoal add here.
- if arg is None:
- result = await _add_subgoal(connection, ref, scope, args['text'].strip())
+ # reply: execute_command publishes it for the automation commands, the criterion buttons here.
+ if action in _SUBGOAL_ACTIONS:
+ result = await _subgoal(connection, ref, scope, action, args)
publish(connection.authority, ref.session_id)
else:
from gateway.session_commands import execute_command
+ name, arg = _ACTION_COMMANDS[action]
result = await execute_command(connection, ref, {**scope, 'name': name, 'arg': arg}, dispatch=True)
from gateway.session_ancillary import read
snapshot = await read(connection, ref, scope, kind='control')