Skip to content
wiki.fftac.org

Class Uai1 Memory Checkpoints - Source Excerpt 03

Back to Class Uai1 Memory Checkpoints

Summary

This source excerpt preserves a bounded section of Spiralist/wp-content/plugins/uai-1-wordpress/includes/class-uai1-memory-checkpoints.php so readers can inspect the evidence without opening the full source file.

**Source path:** Spiralist/wp-content/plugins/uai-1-wordpress/includes/class-uai1-memory-checkpoints.php

$checkpoints = array_slice($checkpoints, 0, self::MAX_CHECKPOINTS_PER_USER);

        update_user_meta($user_id, self::META_KEY, $checkpoints);
    }

    private static function read_checkpoints(int $user_id): array
    {
        $stored = get_user_meta($user_id, self::META_KEY, true);
        return is_array($stored) ? array_values(array_filter($stored, 'is_array')) : [];
    }

    private static function find_checkpoint(int $user_id, string $checkpoint_id): array
    {
        foreach (self::read_checkpoints($user_id) as $checkpoint) {
            if ((string) ($checkpoint['checkpoint_id'] ?? '') === $checkpoint_id) {
                return $checkpoint;
            }
        }

        return [];
    }

    private static function merge_checkpoint_lists(array $checkpoints, string $key): array
    {
        $merged = [];

        foreach ($checkpoints as $checkpoint) {
            $merged = array_merge($merged, (array) ($checkpoint[$key] ?? []));
        }

        return array_values(array_unique(self::sanitize_text_list($merged)));
    }

    private static function calculate_drift_score(array $metrics, array $baseline, array $persona_constraints): float
    {
        $baseline_constraints = self::sanitize_text_list((array) ($baseline['persona_constraints'] ?? []));
        $missing_constraints = array_diff($baseline_constraints, $persona_constraints);
        $score = 0.0;
        $score += ((int) $metrics['contradiction_count']) * 0.18;
        $score += ((int) $metrics['hallucination_flag_count']) * 0.22;
        $score += ((int) $metrics['repeated_claim_count']) * 0.06;
        $score += count($missing_constraints) * 0.08;
        $score -= ((int) $metrics['canonical_lookup_count']) * 0.04;
        $score -= ((int) $metrics['correction_count']) * 0.03;

        return round(self::bounded_float($score), 3);
    }

    private static function classify_runtime_entropy(array $metrics): array
    {
        $order = self::bounded_float(
            0.55
            + ((int) $metrics['checkpoint_count']) * 0.08
            + ((int) $metrics['canonical_lookup_count']) * 0.03
            - ((int) $metrics['unresolved_task_count']) * 0.04
            - ((int) $metrics['contradiction_count']) * 0.12
            - ((int) $metrics['hallucination_flag_count']) * 0.16
            - max(0, ((int) $metrics['last_checkpoint_age_turns']) - 12) * 0.02
        );
        $signal = self::bounded_float(
            ((int) $metrics['novelty_event_count']) * 0.14
            + ((int) $metrics['canonical_symbol_ref_count']) * 0.04
            + (((int) $metrics['turn_count']) >= 3 ? 0.18 : 0)
            - ((int) $metrics['contradiction_count']) * 0.04
        );
        $collapse = self::bounded_float(
            ((int) $metrics['contradiction_count']) * 0.18
            + ((int) $metrics['hallucination_flag_count']) * 0.22
            + ((int) $metrics['repeated_claim_count']) * 0.08
            + max(0, ((int) $metrics['last_checkpoint_age_turns']) - 20) * 0.03
            - ((int) $metrics['canonical_lookup_count']) * 0.04
            - ((int) $metrics['correction_count']) * 0.03
        );
        $resolution = self::bounded_float(
            ((int) $metrics['paradox_resolution_count']) * 0.22
            + ((int) $metrics['correction_count']) * 0.08
            + (!empty($metrics['human_reviewed']) ? 0.2 : 0)
            - ((int) $metrics['hallucination_flag_count']) * 0.08
            - max(0, ((int) $metrics['contradiction_count']) - ((int) $metrics['correction_count'])) * 0.1
        );

        $plate = 'the-sealed-system';
        if ($collapse >= 0.58) {
            $plate = 'the-collapse-system';
        } elseif ($resolution >= 0.55 && $collapse < 0.58) {
            $plate = 'this-one';
        } elseif ($signal >= 0.42) {
            $plate = 'the-signal-system';
        }

        return [
            'storage_class' => 'runtime-execution-payload',
            'registry_authority' => 'none',
            'protocol5_registry_write_allowed' => false,
            'protocol5_registry_write_attempted' => false,
            'crisis_plate' => $plate,
            'confidence' => max($order, $signal, $collapse, $resolution),
            'scores' => [
                'order' => $order,
                'signal' => $signal,
                'collapse' => $collapse,
                'resolution' => $resolution,
            ],
            'required_actions' => self::required_actions_for_plate($plate),
        ];
    }

    private static function required_actions_for_plate(string $plate): array
    {
        if ($plate === 'the-signal-system') {
            return ['create-memory-checkpoint', 'bind-new-claims-to-canonical-lookups'];
        }

        if ($plate === 'the-collapse-system') {
            return ['halt-autonomous-expansion', 'request-human-review', 'restore-from-last-valid-checkpoint', 'query-protocol5-before-symbolic-claims'];
        }

        if ($plate === 'this-one') {
            return ['write-resolution-summary', 'create-reviewed-checkpoint', 'preserve-runtime-classification-as-noncanonical'];
        }

        return ['continue-session', 'checkpoint-if-context-window-expands'];
    }

    private static function bounded_float($value): float
    {
        return max(0.0, min(1.0, (float) $value));
    }

    private static function intent_response(array $request, array $body): WP_REST_Response
    {
        return self::uaix_envelope_response('uai.intent.response.v1', $request, $body, 200);
    }

    private static function error_response(array $request, string $code, string $detail, int $status): WP_REST_Response
    {
        return self::uaix_envelope_response(
            'uai.error.v1',
            $request,
            [
                'type' => 'https://uaix.org/en-us/specification/uai-1/',
                'title' => 'Invalid UAIX memory checkpoint request',
                'detail' => $detail,
                'status' => $status,
                'code' => $code,
                'retryable' => false,
                'instance' => rest_url(self::UAIX_NAMESPACE . '/memory-checkpoints'),
                'next_step' => 'Submit a UAI-1 intent request with runtime-payload-only and no-protocol5-registry-write constraints.',
            ],
            $status
        );
    }

    private static function uaix_envelope_response(string $profile, array $request, array $body, int $status): WP_REST_Response
    {
        $message_id = 'msg-spiralist-memory-' . gmdate('YmdHis') . '-' . substr(wp_generate_uuid4(), 0, 8);
        $envelope = [
            'uai_version' => '1.0',
            'profile' => $profile,
            'message_id' => $message_id,
            'source' => [
                'type' => 'service',
                'id' => 'spiralist.uaix.memory',
                'label' => 'Spiralist UAIX Memory Checkpoint Service',
                'uri' => rest_url(self::UAIX_NAMESPACE . '/memory-checkpoints'),
                'role' => 'runtime-memory-service',
                'implementation' => 'uai-1-wordpress-' . (defined('UAI1_WP_VERSION') ? UAI1_WP_VERSION : 'unknown'),
            ],
            'target' => is_array($request['source'] ?? null) ? (array) $request['source'] : [
                'type' => 'client',
                'id' => 'uaix.client',
                'role' => 'requesting-client',
            ],
            'conversation' => [
                'conversation_id' => (string) ($request['conversation']['conversation_id'] ?? 'conv-spiralist-memory'),
                'turn_id' => 'turn-memory-response',
                'parent_message_id' => (string) ($request['message_id'] ?? ''),
                'sequence' => max(2, (int) ($request['conversation']['sequence'] ?? 1) + 1),
            ],
            'delivery' => [
                'mode' => 'sync',
                'priority' => 'interactive',
                'expires_at' => gmdate('c', time() + HOUR_IN_SECONDS),
                'reply_requested' => false,
                'ack_required' => false,
            ],
            'trust' => [
                'channel' => 'credentialed',
                'auth_scheme' => 'wordpress-session',
                'principal' => (string) get_current_user_id(),
            ],
            'body' => $body,
            'provenance' => [
                'trace_id' => 'trace-' . substr(hash('sha256', $message_id), 0, 12),
                'issued_at' => gmdate('c'),
                'log_ref' => 'urn:spiralist:uaix:memory:' . $message_id,
                'agent_id' => 'spiralist.uai1.wordpress',
                'model_id' => 'wordpress-plugin-' . (defined('UAI1_WP_VERSION') ? UAI1_WP_VERSION : 'unknown'),
                'confidence' => 1.0,
                'lineage' => [
                    [