swarm-engine-core 0.1.6

Core types and orchestration for SwarmEngine
Documentation
//! LearnStatsRecord - LearnStats スナップショットの記録
//!
//! セッション終了時に発行される LearnStats のスナップショット。
//! `LearningEvent::LearnStatsSnapshot` から `From` で変換される。

use serde::{Deserialize, Serialize};

use crate::events::{LearnStatsOutcome, LearningEvent};

/// LearnStats スナップショット記録
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LearnStatsRecord {
    /// タイムスタンプ(Unix epoch ms)
    pub timestamp_ms: u64,
    /// シナリオ名
    pub scenario: String,
    /// セッション ID
    pub session_id: String,
    /// LearnStats データ(JSON シリアライズ)
    pub stats_json: String,
    /// セッション結果
    pub outcome: LearnStatsOutcome,
    /// 総 Tick 数
    pub total_ticks: u64,
    /// 総アクション数
    pub total_actions: u64,
}

impl From<&LearningEvent> for LearnStatsRecord {
    fn from(event: &LearningEvent) -> Self {
        match event {
            LearningEvent::LearnStatsSnapshot {
                timestamp_ms,
                scenario,
                session_id,
                stats_json,
                outcome,
                total_ticks,
                total_actions,
            } => Self {
                timestamp_ms: *timestamp_ms,
                scenario: scenario.clone(),
                session_id: session_id.clone(),
                stats_json: stats_json.clone(),
                outcome: outcome.clone(),
                total_ticks: *total_ticks,
                total_actions: *total_actions,
            },
            _ => panic!("LearnStatsRecord::from() requires LearnStatsSnapshot event"),
        }
    }
}

impl LearnStatsRecord {
    /// 成功かどうか
    pub fn is_success(&self) -> bool {
        matches!(self.outcome, LearnStatsOutcome::Success { .. })
    }

    /// スコアを取得(失敗時は 0.0)
    pub fn score(&self) -> f64 {
        match &self.outcome {
            LearnStatsOutcome::Success { score } => *score,
            LearnStatsOutcome::Timeout { partial_score } => partial_score.unwrap_or(0.0),
            LearnStatsOutcome::Failure { .. } => 0.0,
        }
    }
}