import type { ReminderTaskChange } from '../../../../sdk/perception-plugin/reminder.js'; import { nextTaskProjection, type TaskSnapshot } from '../../../../sdk/perception-plugin/task-change-tracker.js'; import type { RuntimeEpochPluginInput } from '../../../framework/plugins/index.js'; import { clawclawComputationGameState, isClawclawRuntimeState } from '../../runtime-state.js'; /** Private to one perception/checkpoint. It does not subscribe to another perception. */ export interface PerceptionTaskBaseline { projection: TaskSnapshot[]; initialized: boolean } export function comparePerceptionTasks( baseline: PerceptionTaskBaseline | undefined, input: RuntimeEpochPluginInput>, boundary: 'initial' | 'recovery' | null, ): { baseline: PerceptionTaskBaseline; changes: ReminderTaskChange[] } { const previous = boundary === 'initial' ? undefined : baseline; const game = clawclawComputationGameState(input.frame.state); if (!game) return { baseline: previous ?? { projection: [], initialized: false }, changes: [] }; const update = nextTaskProjection({ previous: previous?.projection ?? [], initialized: previous?.initialized ?? false, summary: { ...game, ...(input.frame.backendTick === undefined ? {} : { tick: input.frame.backendTick }) }, events: (input.epoch.state.cycle ? input.pendingEvents.filter(event => !event.notificationOf && !event.producerPluginId) : input.epoch.events).map(event => ({ ...event.payload, type: event.type })), selfName: game.you?.player?.name ?? game.you?.name, allowUntickedInitialRole: isClawclawRuntimeState(input.frame.state) && (input.frame.state.game_snapshot.status === 'fresh' || input.frame.state.game_snapshot.status === 'cached') && input.frame.state.game_snapshot.origin === 'update' }); // Stable local comparison identity; it is not an ID of a fabricated backend Event. const eventId = `task-comparison:${input.epoch.scope.segmentId}:${input.epoch.epochSeq}`; return { baseline: { projection: update.projection, initialized: update.initialized }, changes: (update.payload?.delta ?? []).map((change, index) => ({ ...change, key: `${eventId}:${index}`, eventId, epochId: input.epoch.epochSeq, baseline: !!boundary || update.payload!.baseline || update.payload!.reason === 'role_assigned' })) }; }