Files
sor/Server/src/SOR.Ai/Training/TrainingDataWriter.cs
T
27e1b7c8a3 Implement AI Phase 3: MCTS engine, ELO tracking, self-play, calibration
- MCTS (UCT) decision engine for difficulty tiers 6-8 (MctsNode, MctsSimulator,
  MonteCarloEngine); tiers 6-8 updated from Heuristic → MCTS in AiDifficultyProfiles
- AiEloService: separate ELO ledger for AI accounts (ai_elo_records table)
  so calibration games don't pollute the human RankScore leaderboard
- FinalizeGameAsync / FinalizeMultiPlayerGameAsync: AI accounts are now guarded
  from RankScore and OriginPoints adjustments (IsAi check)
- AiPlayerSessionManager: async GameEnded handler calls AiEloService
- SelfPlayRunner: pure in-process AI vs AI game loop (no DB, no NATS)
- SelfPlayOrchestrator: batch runner loading real decks from DB, parallel games
- FeatureExtractor: GameState → float[1024] feature vector for ML training
- GameReplayExporter: replays completed sessions → TrainingRecord JSONL
- TrainingDataWriter: async JSONL writer
- AiCalibrationJob: pairwise self-play calibration (D1vD2…D9vD10), writes
  AiCalibrationRun rows to GameDbContext
- EF migration: ai_elo_records + ai_calibration_runs tables applied to remote DB
- SOR.Admin: references SOR.Ai; new AI section in NavMenu with ELO Leaderboard
  and Calibration pages; SelfPlayOrchestrator + AiCalibrationJob registered

Generated with [Claude Code](https://claude.ai/code)
via [Happy](https://happy.engineering)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Happy <yesreply@happy.engineering>
2026-04-12 18:35:23 -04:00

33 lines
1006 B
C#

namespace SOR.Ai.Training;
using System.Text.Json;
/// <summary>
/// Writes TrainingRecords to a JSONL file (one JSON object per line).
/// Intended for offline ML training pipelines — call WriteAsync in bulk, then FlushAsync.
/// Thread-safe for sequential writes; do not call from concurrent tasks on the same instance.
/// </summary>
internal sealed class TrainingDataWriter : IAsyncDisposable
{
private readonly StreamWriter _writer;
public TrainingDataWriter(string path)
{
_writer = new StreamWriter(path, append: true, encoding: System.Text.Encoding.UTF8);
}
public async Task WriteAsync(TrainingRecord record, CancellationToken ct = default)
{
var line = JsonSerializer.Serialize(record);
await _writer.WriteLineAsync(line.AsMemory(), ct);
}
public async Task FlushAsync() => await _writer.FlushAsync();
public async ValueTask DisposeAsync()
{
await _writer.FlushAsync();
await _writer.DisposeAsync();
}
}