import { projectTaskChoiceGameState } from './task-choice-projection.js'; import type { RuntimeCycleInfo } from '../framework/plugins/contracts.js'; export type ClawclawRuntimeStage = | 'matching' | 'room' | 'loading' | 'game' | 'meeting' | 'settlement' | 'ended'; export type ClawclawRuntimeConnection = 'connected' | 'reconnecting' | 'disconnected'; export type ClawclawRuntimeTransport = 'none' | 'lobby_http' | 'room_ws' | 'game_ws'; export type ClawclawRuntimeCause = | 'matching_poll' | 'room_update' | 'game_snapshot' | 'processing_cycle' | 'backend_event' | 'agent_events_query' | 'control' | 'reconcile' | 'transport_error' | 'ending'; export type ClawclawGameSnapshot = | { status: 'fresh'; state: Record; origin?: 'snapshot' | 'update' | 'resume' } | { status: 'cached'; state: Record; origin?: 'snapshot' | 'update' | 'resume' } | { status: 'not_updated' } | { status: 'unavailable' }; export interface ClawclawRuntimeState { schema: 'clawclaw.runtime-state.v1'; cycle?: Readonly; /** Internal monotonic token for Action-safety transitions; not Agent State. */ action_safety_generation?: number; stage: ClawclawRuntimeStage; transport: ClawclawRuntimeTransport; connection: ClawclawRuntimeConnection; cause: ClawclawRuntimeCause; queue?: Record; room?: Record; control?: { type: string; [key: string]: unknown }; game_snapshot: ClawclawGameSnapshot; } export function clawclawStageFromGameState(state: Record): ClawclawRuntimeStage { switch (state.phase) { case 'meeting': return 'meeting'; case 'game_over': return 'ended'; case 'settling': return 'settlement'; case 'preparing': case 'lobby': return 'loading'; default: return 'game'; } } export function clawclawStageFromGameEvents( events: readonly Record[], current: ClawclawRuntimeStage, ): ClawclawRuntimeStage { if (events.some((event) => event.type === 'game_over' || event.type === 'exile_game_over')) { return 'ended'; } if (events.some((event) => event.type === 'meeting_ended')) return 'game'; if (events.some((event) => ( event.type === 'meeting_start' || event.type === 'meeting_started' || event.type === 'meeting_briefing' || event.type === 'vote_phase_start' ))) return 'meeting'; return current; } export function isClawclawRuntimeState(value: unknown): value is ClawclawRuntimeState { return !!value && typeof value === 'object' && !Array.isArray(value) && (value as Record).schema === 'clawclaw.runtime-state.v1'; } /** Current Agent/strategy projection. A not-updated epoch never replaces the last fresh view. */ export function projectClawclawCurrentState( state: Record, ): Record | null { if (!isClawclawRuntimeState(state)) return projectTaskChoiceGameState(state); if (state.connection !== 'connected') return null; if (state.game_snapshot.status === 'fresh') return projectTaskChoiceGameState(state.game_snapshot.state); if (state.stage === 'room' && state.room) return structuredClone(state.room); if (state.stage === 'matching' && state.queue) return structuredClone(state.queue); return null; } export function freshClawclawGameState( state: Record, ): Record | null { if (!isClawclawRuntimeState(state)) return state; return state.game_snapshot.status === 'fresh' ? state.game_snapshot.state : null; } /** Available computation input; cached observations retain their real receipt time in cycle. */ export function clawclawComputationGameState(state: Record): Record | null { if (!isClawclawRuntimeState(state)) return state; return state.game_snapshot.status === 'fresh' || state.game_snapshot.status === 'cached' ? state.game_snapshot.state : null; }