File size: 10,352 Bytes
4827377
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
 * RecordLoader - Load and validate AgentDeck match records
 *
 * Per SPEC-VIEWER §5.1:
 * - V1: MUST reject schema versions < 1.3
 * - V2: MUST extract all `type: "gameplay"` events into frames
 * - V3: MUST preserve frame ordering by `context.turn_index`
 * - V4: MUST normalize state keys to camelCase for JS consumption
 */

const RecordLoader = {
  /**
   * Minimum supported schema version
   */
  MIN_SCHEMA_VERSION: '1.3',

  /**
   * Validate a match record JSON
   * @param {object} json - Raw JSON object
   * @returns {{valid: boolean, errors: string[]}}
   */
  validate(json) {
    const errors = [];

    // Check required top-level fields
    if (!json.schema_version) {
      errors.push('Missing required field: schema_version');
    } else if (this._compareVersions(json.schema_version, this.MIN_SCHEMA_VERSION) < 0) {
      errors.push(`Unsupported schema version: ${json.schema_version}. Requires ${this.MIN_SCHEMA_VERSION}+`);
    }

    if (!json.match_id) errors.push('Missing required field: match_id');
    if (!json.game) errors.push('Missing required field: game');
    if (!Array.isArray(json.players)) errors.push('Missing required field: players (must be array)');
    if (!Array.isArray(json.events)) errors.push('Missing required field: events (must be array)');

    return {
      valid: errors.length === 0,
      errors
    };
  },

  /**
   * Load and parse a match record
   * @param {object} json - Raw JSON object
   * @returns {MatchData}
   * @throws {Error} If validation fails
   */
  load(json) {
    const validation = this.validate(json);
    if (!validation.valid) {
      throw new Error(`Invalid record: ${validation.errors.join(', ')}`);
    }

    // Extract gameplay frames from events
    const frames = this._extractFrames(json.events);
    const lifecycle = this._extractLifecycle(json.events);

    // Warn if no gameplay events
    if (frames.length === 0) {
      console.warn('RecordLoader: No gameplay events found in record');
    }

    // Extract outcome info from metadata
    const matchMeta = json.metadata?.match || {};

    return {
      schemaVersion: json.schema_version,
      matchId: json.match_id,
      game: json.game,
      players: json.players,
      winner: json.winner || null,
      seed: json.seed,
      frames: frames,
      finalState: this._normalizeState(json.final_state || {}),
      metadata: this._normalizeMetadata(json.metadata || {}),
      lifecycle,
      // Outcome details
      outcome: matchMeta.outcome || (json.winner ? 'victory' : 'draw'),
      forfeitReason: matchMeta.forfeit_reason || null,
      forfeitingPlayer: matchMeta.forfeiting_player || null
    };
  },

  /**
   * Load from a File object (drag-drop or file picker)
   * @param {File} file
   * @returns {Promise<MatchData>}
   */
  async loadFromFile(file) {
    const text = await file.text();
    let json;
    try {
      json = JSON.parse(text);
    } catch (_error) {
      throw new Error('Invalid JSON format');
    }
    return this.load(json);
  },

  /**
   * Extract gameplay frames from events array
   * @private
   */
  _extractFrames(events) {
    const frames = [];
    let frameIndex = 0;

    for (const event of events) {
      // Skip non-gameplay events (RC3: unknown types must not crash)
      if (event.type !== 'gameplay') continue;

      const data = event.data || {};
      const context = event.context || {};
      const contextIndex = context.turn_index ?? context.phase_index;
      const dataIndex = data.turn_index ?? data.phase_index;
      const turnIndex =
        typeof contextIndex === 'number'
          ? contextIndex
          : typeof dataIndex === 'number'
            ? dataIndex
            : frameIndex;

      frames.push({
        index: turnIndex,
        turnNumber:
          data.turn_context?.turn_number ||
          data.state_before?.turn ||
          data.metadata?.turn_number ||
          turnIndex + 1,
        player: data.player || 'Unknown',
        action: data.action || 'UNKNOWN',
        reasoning: data.reasoning || null,
        stateBefore: this._normalizeState(data.state_before || {}),
        stateAfter: this._normalizeState(data.state_after || {}),
        timestamp: event.timestamp || 0,
        prompt: this._extractPromptData(data)
      });

      frameIndex += 1;
    }

    // Sort by turn_index to ensure correct order (PI1)
    frames.sort((a, b) => a.index - b.index);

    return frames;
  },

  /**
   * Normalize game state to camelCase (V4)
   * @private
   */
  _normalizeState(state) {
    const normalized = this._normalizeKeys(state || {});
    return {
      health: normalized.health || {},
      potions: normalized.potions || {},
      turn: normalized.turn || 0,
      lastAction: normalized.lastAction || {},
      ...Object.fromEntries(
        Object.entries(normalized).filter(
          ([key]) => !['health', 'potions', 'turn', 'lastAction'].includes(key)
        )
      )
    };
  },

  /**
   * Recursively normalize snake_case object keys to camelCase.
   * @private
   */
  _normalizeKeys(value, preserveObjectKeys = false) {
    if (Array.isArray(value)) {
      return value.map((item) => this._normalizeKeys(item));
    }

    if (value && typeof value === 'object') {
      return Object.fromEntries(
        Object.entries(value).map(([key, nested]) => {
          const normalizedKey = preserveObjectKeys ? key : this._snakeToCamel(key);
          return [
            normalizedKey,
            this._normalizeKeys(nested, this._preservesNestedKeys(normalizedKey))
          ];
        })
      );
    }

    return value;
  },

  /**
   * State fields that are dictionaries keyed by player/model/user-provided ids.
   * @private
   */
  _preservesNestedKeys(key) {
    return ['health', 'potions', 'lastAction', 'scores', 'caseIndex', 'processed'].includes(key);
  },

  /**
   * Convert a snake_case key to camelCase.
   * @private
   */
  _snakeToCamel(key) {
    return String(key).replace(/_([a-z])/g, (_match, letter) => letter.toUpperCase());
  },

  /**
   * Extract prompt data if available
   * @private
   */
  _extractPromptData(data) {
    const prompt = data.prompt || data.metadata || {};

    if (!prompt.prompt_text && !prompt.raw_prompt) {
      return null;
    }

    return {
      promptText: prompt.prompt_text || prompt.raw_prompt || '',
      responseText: prompt.response_text || prompt.raw_response || '',
      duration: prompt.duration || 0
    };
  },

  /**
   * Extract lifecycle events used by debug renderers.
   * @private
   */
  _extractLifecycle(events) {
    return {
      handshakes: this._extractHandshakes(events),
      conclusions: this._extractConclusions(events)
    };
  },

  /**
   * Extract per-player handshake details from lifecycle events.
   * @private
   */
  _extractHandshakes(events) {
    const byPlayer = new Map();
    const eventTypes = new Set([
      'player_handshake_start',
      'player_handshake_complete',
      'player_handshake_abort'
    ]);

    for (const event of events) {
      if (!eventTypes.has(event.type)) continue;

      const data = event.data || {};
      const player = data.player || 'Unknown';
      const ts = Number(event.timestamp || 0);

      if (!byPlayer.has(player)) {
        byPlayer.set(player, {
          player,
          status: 'started',
          accepted: null,
          promptText: '',
          responseText: '',
          normalizedResponse: '',
          reason: null,
          timestamp: ts
        });
      }

      const entry = byPlayer.get(player);
      const promptText = data.prompt_text || data.prompt?.prompt_text || '';
      const responseText =
        data.response_text ||
        data.prompt?.response_text ||
        data.normalized_response ||
        '';

      if (promptText) entry.promptText = promptText;
      if (responseText) entry.responseText = responseText;
      if (data.normalized_response) entry.normalizedResponse = data.normalized_response;
      if (typeof data.accepted === 'boolean') entry.accepted = data.accepted;
      if (data.reason) entry.reason = data.reason;
      if (ts) entry.timestamp = ts;

      if (event.type === 'player_handshake_complete') {
        entry.status = 'complete';
        if (entry.accepted === null) entry.accepted = true;
      } else if (event.type === 'player_handshake_abort') {
        entry.status = 'aborted';
        if (entry.accepted === null) entry.accepted = false;
      }
    }

    return Array.from(byPlayer.values()).sort((a, b) => a.timestamp - b.timestamp);
  },

  /**
   * Extract conclusion/reflection details.
   * @private
   */
  _extractConclusions(events) {
    const entries = [];

    for (const event of events) {
      if (event.type !== 'player_conclusion') continue;
      const data = event.data || {};
      const reflectionText = data.reflection_text || data.reflection || '';
      const responseText = data.response_text || reflectionText || '';
      const promptText = data.prompt_text || data.prompt?.prompt_text || '';

      entries.push({
        player: data.player || 'Unknown',
        reflectionText,
        responseText,
        promptText,
        timestamp: Number(event.timestamp || 0)
      });
    }

    return entries.sort((a, b) => a.timestamp - b.timestamp);
  },

  /**
   * Normalize metadata
   * @private
   */
  _normalizeMetadata(metadata) {
    return {
      startedAt: metadata.started_at || null,
      endedAt: metadata.ended_at || null,
      sessionId: metadata.session_id || null,
      playerSummaries: metadata.player_summaries || [],
      playerConfigs: metadata.player_configs || {},
      gameConfig: metadata.game_config || {},
      match: metadata.match || {}
    };
  },

  /**
   * Compare semantic versions
   * @private
   * @returns {number} -1 if a < b, 0 if equal, 1 if a > b
   */
  _compareVersions(a, b) {
    const partsA = a.split('.').map(Number);
    const partsB = b.split('.').map(Number);

    for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
      const numA = partsA[i] || 0;
      const numB = partsB[i] || 0;
      if (numA < numB) return -1;
      if (numA > numB) return 1;
    }
    return 0;
  }
};

// Export for module systems, also available as global
if (typeof module !== 'undefined' && module.exports) {
  module.exports = RecordLoader;
}