import type { RuntimeProcessingCadence } from '../framework/runtime.js'; import { freshClawclawGameState, isClawclawRuntimeState } from './runtime-state.js'; /** A local cycle reuses an observation; it never claims a new backend snapshot. */ export function createClawclawProcessingCadence(intervalMs: number): RuntimeProcessingCadence { return { intervalMs, isStateUpdate: input => isClawclawRuntimeState(input.state) && input.state.cause !== 'agent_events_query' && input.state.cause !== 'control', createState: ({ latestInput, latestStateFrame, cycle }) => { const latest = latestInput.state; if (!isClawclawRuntimeState(latest)) throw new Error('CCL processing cycles require ClawClaw Runtime State.'); const previous = latestStateFrame?.state; const game = previous && freshClawclawGameState(previous); const canReuse = game && latestStateFrame?.connectionId === latestInput.connectionId && latest.stage !== 'room' && latest.stage !== 'matching'; const { control: _control, ...state } = latest; return { ...state, cause: cycle.notificationOnly ? 'control' : 'processing_cycle', ...(cycle.notificationOnly ? { control: { type: 'perception_notifications' } } : {}), game_snapshot: canReuse ? { status: 'cached', state: structuredClone(game), ...(isClawclawRuntimeState(previous) && previous.game_snapshot.status === 'fresh' ? { origin: previous.game_snapshot.origin } : {}) } : { status: 'not_updated' }, }; } }; }