Files
hermes-agent/gateway/session_worker_construct.py
Teknium 1ae678efdb fix(worker): managed workers send the configured instruction and prefill examples
The in-process constructor passes ephemeral_system_prompt=combined_ephemeral (the gateway
prompt from agent.system_prompt / display.personality / channel overrides, plus the frozen -s
skills blocks) and prefill_messages=runner._prefill_messages. agent/managed_worker.execute passed
only policy.skills_prompt and no prefill, so a managed turn silently dropped the configured
instruction and few-shot examples while still completing.

construct_v1 (the owner-resolved construction inputs introduced for the fallback chain) now also
carries:
- ephemeral_system_prompt: TurnRunner's combined prompt for a LOCAL turn, resolved on the owner
  through runner._get_system_prompt_for_channel under the owning profile scope, joined with the
  frozen skills_prompt exactly as run_turn_runner does. It stays API-time only (the agent appends
  it at request build), so the persisted system prefix is unchanged;
- prefill_messages: runner._prefill_messages, as passed in process.
Bypass sessions never read the profile: config-only/safe carry no configured prompt and no
prefill (safe mode additionally drops prefill in agent_init); their frozen launch skills still
apply. The child validates the closed shape; an older owner's frame constructs as before.

Red/green (tests/gateway/test_managed_worker_construct.py):
- test_managed_turn_sends_configured_instruction_and_prefill (real daemon, loopback model,
  agent.system_prompt + prefill_messages_file): against the previous commit the managed request
  has `'system_mark': [False], 'prefill': []` while in-process has the marker and both prefill
  messages; green: identical placement in both, and the stored system prompt row contains no
  marker in either mode.
- test_construct_inputs_follow_the_session_mode extended: ordinary resolves prompt+prefill,
  config-only/safe never call the profile readers, malformed fields are refused.

Review: https://github.com/NousResearch/hermes-agent/pull/106742#pullrequestreview-5461749257
2026-10-08 19:08:59 -07:00

71 lines
3.5 KiB
Python

"""Agent-construction inputs the owner resolves for a managed worker (``construct_v1``).
The in-process turn passes these to ``AIAgent`` from the owner's runner state; a fresh worker
cannot reconstruct them (its config readers serve the frozen session snapshot, and a bypass
worker must never reload the profile), so the owner resolves them per turn, mode-appropriately,
and they ride the hydrated request beside ``turn_v1``. The child validates the closed shape.
"""
import json
KEYS = frozenset({'fallback_model', 'ephemeral_system_prompt', 'prefill_messages'})
def construct_inputs(authority, policy, chat_id):
"""Owner side, per turn (off the owner loop: config reads)."""
from gateway.session_authorities import owner_scope
with owner_scope(authority):
return {'fallback_model': _fallback_chain(authority, policy),
'ephemeral_system_prompt': _ephemeral_prompt(authority, policy, chat_id),
'prefill_messages': _prefill(authority, policy)}
def _fallback_chain(authority, policy):
"""The chain ``_build_fresh_agent`` gives an in-process turn. Ordinary sessions keep the
profile refresh semantics (re-read per turn, last-known-good per home); a config-only session
takes its frozen explicit snapshot (never the profile); safe mode carries none."""
if policy.safe_mode:
return None
if policy.ignore_user_config:
from hermes_cli.fallback_config import get_fallback_chain
return get_fallback_chain(policy.config(authority)) or None
refresh = getattr(authority.runner, '_refresh_fallback_model', None)
return refresh() if refresh is not None else None
def _ephemeral_prompt(authority, policy, chat_id):
"""``TurnRunner``'s combined ephemeral prompt for a LOCAL turn (no platform context block):
the gateway prompt (``agent.system_prompt`` / ``display.personality`` / channel override) plus
the frozen ``-s`` skills blocks. API-time only, never part of the persisted system prefix.
A bypass session never reads the profile: only its frozen launch additions apply."""
configured = None
resolve = getattr(authority.runner, '_get_system_prompt_for_channel', None)
if not policy.ignore_user_config and resolve is not None:
from gateway.config import Platform
configured = resolve(Platform.LOCAL, chat_id or '')
return '\n\n'.join(p for p in (configured, policy.skills_prompt) if p) or None
def _prefill(authority, policy):
"""The runner's prefill examples, exactly as the in-process constructor passes them. A bypass
session never loads the profile's prefill file (and the safe policy drops prefill anyway)."""
if policy.ignore_user_config:
return None
return getattr(authority.runner, '_prefill_messages', None) or None
def construct_kwargs(frame):
"""Child side: the validated ``AIAgent`` keyword arguments; a frame without the object
(older owner) constructs exactly as before."""
inputs = json.loads(frame['policy'].get('request_json') or '{}').get('construct_v1')
if inputs is None:
return {}
if (not isinstance(inputs, dict) or set(inputs) != KEYS
or not _dict_list(inputs['fallback_model']) or not _dict_list(inputs['prefill_messages'])
or not isinstance(inputs['ephemeral_system_prompt'], (str, type(None)))):
raise ValueError('invalid_managed_worker_bootstrap')
return dict(inputs)
def _dict_list(value):
return value is None or (isinstance(value, list) and all(isinstance(e, dict) for e in value))