| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Correlates fixed-sink checkpoint intents with ordinary terminal records. |
| 9 |
* |
| 10 |
* The fixed and ordinary journals are merged only during support collection. |
| 11 |
* This policy keeps exact checkpoint-ID matching in one place so required |
| 12 |
* evidence selection and ranked-journal compaction cannot disagree. |
| 13 |
*/ |
| 14 |
final class ABJ_404_Solution_CheckpointIntentCorrelation { |
| 15 |
|
| 16 |
/** |
| 17 |
* @param array<int, string> $lines |
| 18 |
* @return array<string, bool> |
| 19 |
*/ |
| 20 |
public static function closedCheckpointIds(array $lines): array { |
| 21 |
$closed = array(); |
| 22 |
foreach ($lines as $line) { |
| 23 |
$record = json_decode($line, true); |
| 24 |
if (!is_array($record) || self::isIntent($record)) { |
| 25 |
continue; |
| 26 |
} |
| 27 |
$checkpointId = self::checkpointId($record); |
| 28 |
if ($checkpointId !== '') { |
| 29 |
$closed[$checkpointId] = true; |
| 30 |
} |
| 31 |
} |
| 32 |
return $closed; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* @param array<int, string> $lines |
| 37 |
* @param array<string, bool> $closed |
| 38 |
* @return array<int, string> |
| 39 |
*/ |
| 40 |
public static function withoutClosedIntents(array $lines, array $closed): array { |
| 41 |
return array_values(array_filter($lines, static function (string $line) use ($closed): bool { |
| 42 |
$record = json_decode($line, true); |
| 43 |
if (!is_array($record) || !self::isIntent($record)) { |
| 44 |
return true; |
| 45 |
} |
| 46 |
$checkpointId = self::checkpointId($record); |
| 47 |
return $checkpointId === '' || !isset($closed[$checkpointId]); |
| 48 |
})); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Fixed-sink intents whose exact ordinary checkpoint never reached disk. |
| 53 |
* |
| 54 |
* @param array<int, string> $lines |
| 55 |
* @return array<int, string> |
| 56 |
*/ |
| 57 |
public static function unmatchedIntentLines(array $lines): array { |
| 58 |
$closed = self::closedCheckpointIds($lines); |
| 59 |
return array_values(array_filter($lines, static function (string $line) use ($closed): bool { |
| 60 |
$record = json_decode($line, true); |
| 61 |
if (!is_array($record) || !self::isIntent($record)) { |
| 62 |
return false; |
| 63 |
} |
| 64 |
$checkpointId = self::checkpointId($record); |
| 65 |
return $checkpointId !== '' && !isset($closed[$checkpointId]); |
| 66 |
})); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Remove keyed intents after RequiredCheckpointEvidence reserved them. |
| 71 |
* |
| 72 |
* Malformed or unkeyed intents remain rankable because they cannot be |
| 73 |
* correlated safely. |
| 74 |
* |
| 75 |
* @param array<int, string> $lines |
| 76 |
* @return array<int, string> |
| 77 |
*/ |
| 78 |
public static function withoutKeyedIntents(array $lines): array { |
| 79 |
return array_values(array_filter($lines, static function (string $line): bool { |
| 80 |
$record = json_decode($line, true); |
| 81 |
return !is_array($record) |
| 82 |
|| !self::isIntent($record) |
| 83 |
|| self::checkpointId($record) === ''; |
| 84 |
})); |
| 85 |
} |
| 86 |
|
| 87 |
/** @param array<mixed, mixed> $record Decoded JSON at the untrusted journal boundary. */ |
| 88 |
private static function isIntent(array $record): bool { |
| 89 |
return ($record['envelope'] ?? '') |
| 90 |
=== ABJ_404_Solution_CheckpointRecordFactory::ENVELOPE_INTENT; |
| 91 |
} |
| 92 |
|
| 93 |
/** @param array<mixed, mixed> $record Decoded JSON at the untrusted journal boundary. */ |
| 94 |
private static function checkpointId(array $record): string { |
| 95 |
return is_string($record['checkpoint_id'] ?? null) |
| 96 |
? $record['checkpoint_id'] |
| 97 |
: ''; |
| 98 |
} |
| 99 |
} |
| 100 |
|