//! The ProtocolMachine: ties coroutines, sessions, and scheduler together.
//!
//! Execution follows the Lean spec pattern:
//! - `exec_instr` fetches, dispatches to per-instruction step functions
//! - Each step function owns its type checking via `SessionStore::lookup_type`
//! - Results are bundled in `StepPack` and committed atomically via `commit_pack`
//! - Blocking never advances type state
//!
//! This module is a runtime surface over the canonical `ProtocolMachineKernel` execution
//! contract. Driver layers call into `ProtocolMachineKernel` and do not redefine instruction
//! semantics.

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::json;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Duration;
use telltale_types::{LocalTypeR, ValType};

use crate::bridge::{IdentityGuardBridge, IdentityVerificationBridge};
use crate::buffer::{BufferConfig, EnqueueResult};
use crate::clock::SimClock;
use crate::commit_common::{apply_output_condition_gate, effect_trace_entry_for_event};
use crate::communication_replay::{
    CommunicationConsumeResult, CommunicationConsumption, CommunicationConsumptionArtifact,
    CommunicationIdentity, CommunicationReplayError, CommunicationReplayMode,
    CommunicationStepKind, DefaultCommunicationConsumption,
};
use crate::coroutine::{
    BlockReason, CoroStatus, Coroutine, Fault, KnowledgeFact, ProgressToken, Value,
};
use crate::determinism::{DeterminismMode, EffectDeterminismTier};
#[cfg(test)]
use crate::effect::SendDecisionInput;
use crate::effect::{
    infer_effect_interface_and_operation, CorruptionType, EffectExchangeRecord, EffectFailure,
    EffectHandler, EffectOutcome, EffectRequest, EffectResponse, EffectResult, EffectTraceEntry,
    ReplayEffectHandler, SendDecision, TopologyPerturbation,
};
use crate::exec;
use crate::faults::{
    speculation_fault_abort_requires_active, speculation_fault_disabled,
    speculation_fault_join_requires_active, transfer_fault_delegation_guard_violation,
};
use crate::guard::{GuardLayer, InMemoryGuardLayer, LayerId};
use crate::identity::IdentityModel;
use crate::instr::{Endpoint, Instr, InvokeAction, PC};
use crate::instruction_semantics::{
    decode_branch_label_payload, decode_endpoint_fact,
    endpoint_from_reg as decode_endpoint_from_reg,
};
use crate::intern::{EdgeId, EdgeSymbolTable, StringId, SymbolTable};
use crate::kernel::{KernelMachine, ProtocolMachineKernel};
use crate::loader::CodeImage;
use crate::output_condition::{OutputConditionCheck, OutputConditionPolicy};
use crate::persistence::{NoopPersistence, PersistenceModel};
use crate::refinement::{
    cooperative_refinement_slice, ProtocolMachineRefinementSlice, RefinementSliceError,
};
use crate::scheduler::{SchedPolicy, Scheduler};
use crate::semantic_objects::{
    protocol_machine_semantic_objects, OperationInstance, OperationPhase, OutstandingEffect,
    OutstandingEffectStatus, ProgressContract, ProgressState, ProgressTransition,
    ProtocolMachineSemanticObjects,
};
use crate::serialization::{
    canonical_replay_fragment_v1, semantic_audit_log_v1, CanonicalReplayFragmentV1,
    SemanticAuditRecord,
};
#[allow(unused_imports)]
use crate::session::SessionHostMutation;
use crate::session::{
    unfold_if_var_with_scope, AuthorityArtifact, AuthorityAuditEvent, AuthorityAuditRecord,
    AuthorityWitnessId, CancellationWitness, ClosedSessionSummary, Edge, FragmentOwnerId,
    OwnershipError, OwnershipReceipt, OwnershipScope, OwnershipTerminalReason, SessionId,
    SessionStatus, SessionStore, SessionStoreMemoryUsage, TimeoutWitness,
};
use crate::transfer_semantics::{
    decode_transfer_request, delegation_receipt, endpoint_owner_ids, move_endpoint_bundle,
    validate_delegation_coherence, DelegationAuditRecord, DelegationReceipt, DelegationStatus,
};
use crate::verification::{DefaultVerificationModel, VerificationModel};

include!("engine/runtime_state/mod.rs");
include!("engine/protocol_machine_config.rs");
include!("engine/protocol_machine_error_and_step_pack.rs");
include!("engine/runtime_exec/mod.rs");
include!("engine/validation.rs");
include!("engine/topology_and_dispatch.rs");
include!("engine/instruction_effects.rs");
include!("engine/instruction_tag.rs");
include!("engine/instruction_choice.rs");
include!("engine/open_commit.rs");
include!("engine/kernel_impl.rs");

#[cfg(test)]
mod tests {
    include!("../tests/unit/protocol_machine/tests_handlers_core.rs");
    include!("../tests/unit/protocol_machine/tests_handlers_edge.rs");
    include!("../tests/unit/protocol_machine/tests_runtime_progress.rs");
    include!("../tests/unit/protocol_machine/tests_monitor_persist.rs");
    include!("../tests/unit/protocol_machine/tests_compiler_topology.rs");
    include!("../tests/unit/protocol_machine/tests_flow_policy_faults.rs");
    include!("../tests/unit/protocol_machine/tests_semantic_state.rs");
}
