import type { PerceptionLifecycleChange, PerceptionModuleInput } from '../../../../sdk/perception-plugin/module.js'; import type { RuntimeEpochPluginInput } from '../../../framework/plugins/index.js'; import { clawclawComputationGameState, isClawclawRuntimeState } from '../../runtime-state.js'; import { createCanonicalGameState } from '../../behavior-input.js'; import { ObservationFrameProjector } from '../../observation-frame-projector.js'; export interface PerceptionInputBaseline { scope?: string; gameId?: string; phase?: string; connectionId?: string; connected?: boolean; tick?: number; alive?: boolean; ended?: boolean; } /** Shared, game-owned input projection for mandatory and installed perceptions. */ export function perceptionInputBoundary( input: RuntimeEpochPluginInput>, previous: PerceptionInputBaseline = {}, ) { const rawState = input.epoch.state; const runtime = isClawclawRuntimeState(rawState) ? rawState : undefined; const raw = clawclawComputationGameState(input.frame.state) ?? (rawState.cycle ? null : input.currentState.state); const connected = !runtime || runtime.connection === 'connected'; const game = connected && raw?.game_id ? createCanonicalGameState(raw, input.frame.backendTick) : null; const frame = new ObservationFrameProjector().project(input.frame); const scope = `${input.epoch.scope.sessionId}:${input.epoch.scope.segmentId}`; const initial = previous.scope !== scope || !!game && previous.gameId !== game.game_id; const recovery = !initial && connected && (previous.connected === false || previous.connectionId !== input.epoch.connectionId); const lifecycle: PerceptionLifecycleChange[] = []; if (initial) lifecycle.push({ kind: 'initial' }); else if (recovery) lifecycle.push({ kind: 'recovery' }); else { if (game?.phase && previous.phase && previous.phase !== game.phase) { lifecycle.push({ kind: 'phase_changed', from: previous.phase, to: game.phase }); } if (previous.alive === true && game?.you?.is_alive === false) lifecycle.push({ kind: 'observer_died' }); if (!connected && previous.connected !== false) lifecycle.push({ kind: 'observation_gap' }); } const delta = frame && previous.tick !== undefined && !initial && !recovery ? frame.tick - previous.tick : null; // Repeated local cycles and same-tick revisions are valid; missing visual samples are explicit. const continuous = connected && lifecycle.length === 0 && delta !== null && delta >= 0 && delta <= 3; if (frame && !continuous && !initial && !recovery && lifecycle.length === 0) lifecycle.push({ kind: 'observation_gap' }); if (frame) Object.assign(frame, { tickDelta: delta, continuousFromPrevious: continuous }); const ended = runtime?.stage === 'ended' || game?.phase === 'game_over'; const state: PerceptionInputBaseline = { scope, connectionId: input.epoch.connectionId, connected, ended, gameId: game?.game_id ?? previous.gameId, phase: game?.phase ?? previous.phase, tick: frame?.tick ?? previous.tick, alive: game?.you?.is_alive ?? previous.alive }; if (runtime?.cause === 'agent_events_query') lifecycle.push({ kind: 'snapshot_requested' }); if (ended && !previous.ended) lifecycle.push({ kind: 'segment_ending' }); const common: Pick = { source: { epochId: input.epoch.epochSeq, frameSeq: input.frame.frameSeq, connectionId: input.epoch.connectionId, backendTick: input.frame.backendTick, receivedAtMs: input.frame.receivedAtMs, observedAtMs: input.frame.observedAtMs }, scope: { sessionId: input.epoch.scope.sessionId, segmentId: input.epoch.scope.segmentId }, gameState: game, frame: connected ? frame : null, ...(runtime?.cycle ? { cycle: runtime.cycle } : {}), boundary: initial ? 'initial' : recovery ? 'recovery' : null, lifecycle, roomState: runtime?.room ?? null, queueState: runtime?.queue ?? null, nowMs: input.frame.observedAtMs ?? input.frame.receivedAtMs, }; return { state, common, initial, recovery }; }