m/market labResearch, with a record.
Source on GitHub ↗

replay.py

1"""Compose original-operation playback with the existing financial engine."""23from __future__ import annotations45from dataclasses import asdict6from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Union, cast78from sim_platform.domain.models import ContractError9from sim_platform.resources import cleanup_after_error10from sim_platform.market.prepared.reader import PreparedMarket11from sim_platform.market.prepared.session import PreparedSession12from sim_platform.market.prepared.publications import PublicationReader13from sim_platform.market.models import NewsRecord14from sim_platform.decisions.policy.models import DecisionConfig15from sim_platform.decisions.policy.policies import HumanPolicy16from sim_platform.execution.models import ExecutionConfig17from sim_platform.replay.comparison import financial_values, financial_events18from sim_platform.replay.commands import apply_recorded_command19from sim_platform.replay.plan import decode_plan2021if TYPE_CHECKING:  # Annotation only.22    from sim_platform.decisions.policy.contracts import Policy23    from sim_platform.decisions.policy.runner import PolicyDecisionRunner24    from sim_platform.replay.plan import RecordedCommand25    from ..execution_operations import ApplicationOperations2627    PreparedReader = Union[PreparedMarket, PublicationReader]28    # Creates the destination runner: new_runner(profile, run_id, maker, market, policy).29    ReplayRunnerFactory = Callable[[str, str, str, PreparedSession, Policy], PolicyDecisionRunner]3031# The outcome: 'run_id', 'matched', 'commands', 'financial', 'replay', 'model_calls',32# 'events', 'operations' and 'events_verified'.33ReplayResult = Dict[str, Any]343536def validate_destination(37    operations: ApplicationOperations, profile: str, account: str, seed: Dict[str, Any]38) -> None:39    """Refuse a destination whose initial financial state differs before creating a run."""40    initial = seed['initial_account']41    actual = asdict(operations.snapshot(profile, account))42    if actual['currency'] != initial['currency'] or financial_values(actual) != financial_values(43        initial44    ):45        raise ContractError('Replay destination must match the original initial account')464748def validate_source(49    reader: PreparedReader,50    commands: List[RecordedCommand],51    terminal: Dict[str, Any],52    cancelled: Callable[[], bool],53) -> None:54    """Check all recorded source prefixes before any destination financial action."""55    final_position = terminal['position']56    if int(final_position['boundary']) > reader.manifest['frames']:57        raise ContractError('recorded terminal prefix exceeds source')58    for command in commands:59        if cancelled():60            raise ContractError('Replay cancelled')61        prefix = reader.prefix(int(command['effective_ns']))62        if prefix != int(command['prepared_prefix']) or reader.source_sequence(prefix) != int(63            command['source_sequence']64        ):65            raise ContractError('prepared Replay source prefix differs from the record')666768def apply_commands(69    operations: ApplicationOperations,70    runner: PolicyDecisionRunner,71    market: PreparedSession,72    profile: str,73    commands: List[RecordedCommand],74    seed: Dict[str, Any],75    old_orders: Dict[str, Any],76    cancelled: Callable[[], bool],77) -> None:78    """Apply recorded operations in order; model/policy code is never called."""79    run_id = runner.run_id80    new_orders: Dict[str, Any] = {}81    for command in commands:82        if cancelled():83            raise ContractError('Replay cancelled')84        stamp = int(command['effective_ns'])85        if not seed['start_ns'] <= stamp < seed['end_ns']:86            raise ContractError('recorded command outside experiment interval')87        while market.next_event_time_ns is not None and market.next_event_time_ns <= stamp:88            if cancelled():89                raise ContractError('Replay cancelled')90            runner.pump(1)91        if market.merged_prefix != int(command['prepared_prefix']):92            raise ContractError('prepared Replay source prefix differs from the record')93        position = dict(94            sequence=str(market.snapshot.sequence),95            time_ns=str(stamp),96            boundary=str(market.merged_prefix),97            ordinal=command['admission_sequence'],98        )99        if command.get('financial_effect') is False:100            continue101        apply_recorded_command(102            operations,103            runner,104            profile,105            run_id,106            position,107            command['payload'],108            old_orders,109            new_orders,110        )111112113def finish_and_verify(114    operations: ApplicationOperations,115    runner: PolicyDecisionRunner,116    market: PreparedSession,117    profile: str,118    archive: Dict[str, Any],119    terminal: Dict[str, Any],120    expected: Dict[str, Any],121    commands: List[RecordedCommand],122    cancelled: Callable[[], bool],123) -> ReplayResult:124    """Advance to the sealed endpoint and compare recalculated financial evidence."""125    run_id = runner.run_id126    reader = market.reader127    final_position = terminal['position']128    target = int(final_position['boundary'])129    if target > reader.manifest['frames']:130        raise ContractError('recorded terminal prefix exceeds source')131    while market.merged_prefix < target:132        if cancelled():133            raise ContractError('Replay cancelled')134        runner.pump(1)135    status = archive['end']['status']136    if market.snapshot.sequence != int(final_position['sequence']):137        raise ContractError('recorded terminal source sequence mismatch')138    runner.finish_recorded_replay(139        status, 'recorded operation Replay', time_ns=int(final_position['time_ns'])140    )141    observed = asdict(runner.snapshot)142    events = financial_events(operations.run_events(profile, run_id))143    final = next(r['details'] for r in archive['records'] if r.get('event') == 'prepared_final')144    if 'events' in final and events != final['events']:145        raise ContractError('prepared operation Replay financial events mismatch')146    if financial_values(observed) != financial_values(expected):147        raise ContractError('prepared operation Replay financial result mismatch')148    return dict(149        run_id=run_id,150        matched=True,151        commands=len(commands),152        financial=financial_values(observed),153        replay=True,154        model_calls=0,155        events=events,156        operations=commands,157        events_verified='events' in final,158    )159160161def _replay_merged(162    operations: ApplicationOperations,163    new_runner: ReplayRunnerFactory,164    profile: str,165    account: str,166    archive: Dict[str, Any],167    *,168    cancelled: Callable[[], bool],169) -> ReplayResult:170    """Replay a run on several symbols: one prepared artifact per symbol, merged again."""171    from sim_platform.market.prepared.merged import MergedMarket172173    seed, expected, commands, terminal, old_orders = decode_plan(archive)174    sessions: List[PreparedSession] = []175    market: Any = None176    runner: Optional[PolicyDecisionRunner] = None177    run_id: Optional[str] = None178    try:179        for artifact in seed['artifacts']:180            prepared = PreparedMarket(artifact)181            try:182                prepared.validate(cancelled=cancelled)183                start = min(seed['start_ns'], prepared.manifest['last_ns'])184                sessions.append(PreparedSession.from_reader(prepared, start))185            except Exception as exc:186                cleanup_after_error(exc, prepared.close)187                raise188        market = MergedMarket.from_sessions(sessions, seed['start_ns'])189        validate_source(market.reader, commands, terminal, cancelled)190        policy = HumanPolicy()191        source = dict(192            execution_mode='live',193            prepared=market.prepared_descriptor(),194            start_origin=dict(sequence=market.snapshot.sequence, time_ns=market.time_ns),195            universe=list(market.universe),196            replay=True,197        )198        maker, run_id = operations.create_managed_decision_run(199            profile,200            account,201            list(market.universe),202            policy,203            DecisionConfig(mode='manual'),204            ExecutionConfig(**seed['execution']),205            source=source,206        )207        runner = new_runner(profile, run_id, maker, market, policy)208        runner.begin_recorded_replay()209        runner.play()210        apply_commands(operations, runner, market, profile, commands, seed, old_orders, cancelled)211        return finish_and_verify(212            operations, runner, market, profile, archive, terminal, expected, commands, cancelled213        )214    finally:215        if runner is not None:216            runner.close()217        elif run_id is not None:218            operations.abort_execution_run(profile, run_id)219        if market is not None:220            market.close()221        else:222            for session in sessions:223                session.close()224225226def replay(227    operations: ApplicationOperations,228    new_runner: ReplayRunnerFactory,229    profile: str,230    account: str,231    archive: Dict[str, Any],232    *,233    cancelled: Callable[[], bool] = lambda: False,234) -> ReplayResult:235    """Validate, prepare, apply and verify one sealed prepared-operation recording."""236    seed, expected, commands, terminal, old_orders = decode_plan(archive)237    validate_destination(operations, profile, account, seed)238    if seed.get('artifacts'):239        return _replay_merged(240            operations, new_runner, profile, account, archive, cancelled=cancelled241        )242    reader: PreparedReader = PreparedMarket(seed['artifact'])243    market: Optional[PreparedSession] = None244    runner: Optional[PolicyDecisionRunner] = None245    run_id: Optional[str] = None246    try:247        # The reader is still the PreparedMarket opened above.248        cast(PreparedMarket, reader).validate(cancelled=cancelled)249        if seed.get('publications'):250            reader = PublicationReader(reader, tuple(NewsRecord(**n) for n in seed['publications']))251        validate_source(reader, commands, terminal, cancelled)252        market = PreparedSession.from_reader(reader, seed['start_ns'])253        policy = HumanPolicy()254        source = dict(255            execution_mode='live',256            prepared=market.prepared_descriptor(),257            start_origin=dict(sequence=market.snapshot.sequence, time_ns=market.time_ns),258            replay=True,259        )260        maker, run_id = operations.create_managed_decision_run(261            profile,262            account,263            reader.manifest['symbol'],264            policy,265            DecisionConfig(mode='manual'),266            ExecutionConfig(**seed['execution']),267            source=source,268        )269        runner = new_runner(profile, run_id, maker, market, policy)270        runner.begin_recorded_replay()271        runner.play()272        apply_commands(operations, runner, market, profile, commands, seed, old_orders, cancelled)273        return finish_and_verify(274            operations, runner, market, profile, archive, terminal, expected, commands, cancelled275        )276    finally:277        if runner is not None:278            runner.close()279        elif run_id is not None:280            operations.abort_execution_run(profile, run_id)281        if market is not None:282            market.close()283        else:284            reader.close()