current snapshot -> Detected Evidence -> correlation -> * advance comparison anchor. * * This component is deliberately fail-soft and never controls whether the * established Site Review itself succeeds. */ if (!defined('ABSPATH')) { exit; } final class AIFQ_SR_Memory_Processor { private AIFQ_SR_Memory_Manager $manager; private AIFQ_SR_Snapshot_Builder $snapshot_builder; private AIFQ_SR_Snapshot_Differ $differ; private AIFQ_SR_Evidence_Repository $repository; private AIFQ_SR_Evidence_Correlator $correlator; public function __construct( ?AIFQ_SR_Memory_Manager $manager = null, ?AIFQ_SR_Snapshot_Builder $snapshot_builder = null, ?AIFQ_SR_Snapshot_Differ $differ = null, ?AIFQ_SR_Evidence_Repository $repository = null, ?AIFQ_SR_Evidence_Correlator $correlator = null ) { $this->manager = $manager ?? new AIFQ_SR_Memory_Manager(); $this->snapshot_builder = $snapshot_builder ?? new AIFQ_SR_Snapshot_Builder(); $this->differ = $differ ?? new AIFQ_SR_Snapshot_Differ(); $this->repository = $repository ?? new AIFQ_SR_Evidence_Repository(); $this->correlator = $correlator ?? new AIFQ_SR_Evidence_Correlator( [$this->repository, 'find_observed_candidates'], [$this->repository, 'insert_relation'] ); } public function process_successful_review(): bool { try { if (!$this->manager->ensure_foundation()) { return false; } $previous = $this->manager->get_current_snapshot(); if (!AIFQ_SR_Snapshot_Builder::is_valid_snapshot($previous)) { return false; } $current = $this->snapshot_builder->build(); if (!AIFQ_SR_Snapshot_Builder::is_valid_snapshot($current)) { return false; } $records = $this->differ->diff($previous, $current); $all_persisted = true; foreach ($records as $record) { $record['event_id'] = AIFQ_SR_Memory_Identity::uuid(); $record['idempotency_key'] = AIFQ_SR_Memory_Identity::detected_idempotency_key( (string) $previous['snapshot_id'], $record ); $event_id = $this->repository->insert_evidence($record); if (!is_string($event_id) || $event_id === '') { $all_persisted = false; continue; } // Correlation is enrichment. Failure means under-correlation and // must not invalidate trustworthy Detected Evidence. $this->correlator->correlate($event_id, $record); } // A partial Evidence write is allowed to reduce history, but it must // not keep an old comparison anchor alive and later duplicate the // transitions that did persist. Advancing the trustworthy current // state is safer than retrying a widened interval with a new snapshot // id. The return value still reports degraded Evidence persistence. $advanced = $this->manager->advance_snapshot($current); return $all_persisted && $advanced; } catch (Throwable $e) { return false; } } }