factory.py
1"""Platform-owned composition for live Human/Rule experiments.23Factories are injected; lower layers never construct Platform or register roots.4The assembly function owns rollback until it returns the complete runner.5"""67from __future__ import annotations89import multiprocessing10from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union1112from sim_platform.resources import cleanup_after_error13from sim_platform.decisions.policy.live.runner import LivePolicyDecisionRunner14from sim_platform.decisions.policy.live.owner import PolicyOwner, ObservedMarketExecution15from sim_platform.decisions.policy.live.program import TrustedPolicyProgram16from sim_platform.experiments.scheduling.clock import SharedExperimentClock, session_gaps17from sim_platform.experiments.scheduling.host import ExperimentRuntimeHandle18from sim_platform.experiments.recording import ExperimentLog19from sim_platform.experiments.execution.controls import ExperimentControls20from .runtime_policy import PreparedWorkPolicy21from sim_platform.agents.hosting.host import AgentProcessHost22from sim_platform.agents.hosting.protocol import TransportLimits23from sim_platform.persistence.agent_log_files import AgentLogFile, default_agent_log_path24from .binding import RuntimeBinding2526if TYPE_CHECKING: # Annotation only.27 from os import PathLike2829 from sim_platform.decisions.policy.contracts import Policy30 from sim_platform.decisions.policy.models import DecisionConfig31 from sim_platform.decisions.policy.runner import PolicyDecisionRunner32 from sim_platform.experiments.execution.session import ExperimentSession33 from sim_platform.market.prepared.session import PreparedSession3435# Upper bound of a live policy session: the last nanosecond of the day.36SESSION_END_NS = 86_399_999_999_99937# Live policy Runtime settings.38EVENT_BUCKETS = 6439LAG_LIMIT_NS = 100_000_00040# A run fails only if execution stays more than LAG_LIMIT_NS behind live time for this41# long. A single stall (a cold first decision on a loaded machine) is tolerated; commands42# still apply at their stamped time against the exact market prefix, so lag never shows a43# decision later data.44LAG_WINDOW_NS = 1_000_000_00045# Rule child pipe limits: message bytes, pending bytes and pending messages.46RULE_TRANSPORT_LIMITS = (65536, 262144, 8)474849def policy_configuration(market: PreparedSession, config: DecisionConfig) -> Dict[str, Any]:50 """Rule wake policy preserves trigger filtering and minimum decision interval.5152 Human uses the same Runtime lifecycle but never requests decision work.53 Source EOF normally ends the session before its upper bound: midnight of the first54 day, or the end of the recording when it covers more than one day.55 """56 return dict(57 start_ns=getattr(market, 'origin_ns', market.time_ns),58 end_ns=max(SESSION_END_NS, market.reader.manifest['last_ns'] + 1),59 symbol=market.snapshot.symbol,60 event_buckets=EVENT_BUCKETS,61 minimum_interval_ns=config.interval_ns,62 timeout_seconds='30',63 retry=dict(64 max_attempts=1,65 format_max_attempts=1,66 retry_classes=[],67 base_delay_seconds='0.1',68 max_delay_seconds='1',69 ),70 schedule=dict(71 mode='event',72 busy_event_policy='wake_on_pending_event',73 events=(74 []75 if config.mode == 'manual'76 else [dict(kind='market_update', symbol=market.snapshot.symbol)]77 ),78 ),79 )808182def new_rule_host(83 policy: Policy,84 config: DecisionConfig,85 pipeline: ExperimentSession,86 runtime: ExperimentRuntimeHandle,87 lifecycle: RuntimeBinding,88 run_id: str,89) -> Optional[AgentProcessHost]:90 """Reuse the Agent host lifecycle with a trusted policy-specific program."""91 if policy.kind == 'human':92 return None93 return AgentProcessHost(94 pipeline.submit,95 dict(run_id=run_id),96 TransportLimits(*RULE_TRANSPORT_LIMITS),97 program=TrustedPolicyProgram(policy, config),98 work_client=runtime.work_client(),99 on_exit=lambda state, reason: (100 lifecycle.agent_failed(reason or 'rule exited') if state == 'failed' else None101 ),102 )103104105def assemble_live_policy(106 new_core: Callable[[str, str, str, PreparedSession, Policy], PolicyDecisionRunner],107 directory: Union[str, PathLike[str]],108 profile: str,109 run_id: str,110 maker: str,111 market: PreparedSession,112 policy: Policy,113 *,114 pipeline_factory: Callable[..., ExperimentSession],115) -> LivePolicyDecisionRunner:116 """Compose one Session, Live Runtime, log and optional Rule carrier.117118 Every constructed child remains owned during setup, including failures.119 The caller registers only the returned experiment controller root.120 """121 core = new_core(profile, run_id, maker, market, policy)122 runtime = pipeline = host = recorder = None123 try:124 config = policy_configuration(market, core.config)125 origin = getattr(market, 'origin_ns', market.time_ns)126 clock = SharedExperimentClock(127 multiprocessing.get_context('spawn'),128 origin,129 # A manual experiment requests no decision work, so nothing can be in130 # progress at the close and no extra waiting is needed before a skip.131 session_gaps(132 market.reader.manifest,133 origin,134 config['timeout_seconds'] if config['schedule']['events'] else None,135 ),136 )137 config['prepared_artifact'] = str(market.reader.directory)138 from dataclasses import asdict139140 config['prepared_news'] = [asdict(n) for n in getattr(market.reader, 'news', ())]141 runtime = ExperimentRuntimeHandle(PreparedWorkPolicy, config, clock=clock)142 owner = PolicyOwner(core)143 core.bind_observations(owner.observation)144 owner.publish(None)145 recorder = ExperimentLog(AgentLogFile(default_agent_log_path(directory, run_id)), run_id)146 pipeline = pipeline_factory(147 ObservedMarketExecution(core.execution, market, owner.observe_event, owner.publish),148 market,149 clock=clock,150 end_ns=config['end_ns'],151 lag_limit_ns=LAG_LIMIT_NS,152 lag_window_ns=LAG_WINDOW_NS,153 sink=recorder,154 operation=owner.perform,155 )156 lifecycle = RuntimeBinding(157 runtime, pipeline, dict(run_id=run_id, symbol=config['symbol']), owner.capture158 )159 pipeline.bind_lifecycle(lifecycle)160 host = new_rule_host(policy, core.config, pipeline, runtime, lifecycle, run_id)161 runner = LivePolicyDecisionRunner(core, pipeline, runtime, owner, host, recorder)162 runner.barrier = ExperimentControls(163 pipeline,164 runtime,165 decision=host,166 record=lambda value: lifecycle.record_notice('control_ack', value),167 )168 return runner169 except Exception as exc:170 cleanup_after_error(171 exc, *(r.close for r in (pipeline, host, runtime, core, recorder) if r is not None)172 )173 raise