You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.4 KiB
PHP
106 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/**
|
|
* Fail-closed interpretation of the immutable single-dispatch counter.
|
|
*
|
|
* Zero is the only proof that no Agent mutation crossed the dispatch boundary.
|
|
* One is the only valid dispatched generation. Any other value is corruption:
|
|
* callers must retain the resource fence and must not consume recovery proof
|
|
* until an operator repairs the ledger.
|
|
*/
|
|
final class CloudDispatchLedger
|
|
{
|
|
public const CONFLICT_CODE = 'recovery_dispatch_ledger_conflict';
|
|
private const REPAIR_INCIDENT_CODES = [
|
|
self::CONFLICT_CODE,
|
|
'recovery_dispatch_repair_completion_receipt_missing',
|
|
'recovery_dispatch_repair_receipt_invalid',
|
|
'recovery_dispatch_repair_aggregate_state_conflict',
|
|
'recovery_dispatch_repair_payload_unavailable',
|
|
'recovery_dispatch_repair_operation_identity_conflict',
|
|
'recovery_dispatch_repair_evidence_conflict',
|
|
'recovery_dispatch_repair_instance_state_conflict',
|
|
'recovery_dispatch_repair_ledger_conflict',
|
|
'recovery_dispatch_repair_internal_error',
|
|
];
|
|
|
|
public static function attempts(object $job): int
|
|
{
|
|
$value = $job->dispatch_attempts ?? null;
|
|
if (is_int($value)) {
|
|
return $value;
|
|
}
|
|
if (is_string($value)
|
|
&& preg_match('/^-?[0-9]+$/D', $value) === 1) {
|
|
return (int) $value;
|
|
}
|
|
// A missing, NULL or non-integral value is corruption rather than
|
|
// evidence that the mutation never crossed the dispatch boundary.
|
|
return PHP_INT_MIN;
|
|
}
|
|
|
|
public static function isNeverDispatched(object $job): bool
|
|
{
|
|
return self::attempts($job) === 0;
|
|
}
|
|
|
|
public static function isDispatched(object $job): bool
|
|
{
|
|
return self::attempts($job) === 1;
|
|
}
|
|
|
|
public static function isInvalid(object $job): bool
|
|
{
|
|
return !in_array(self::attempts($job), [0, 1], true);
|
|
}
|
|
|
|
/** Invalid non-zero state is conservatively treated as possibly sent. */
|
|
public static function mayHaveDispatched(object $job): bool
|
|
{
|
|
return !self::isNeverDispatched($job);
|
|
}
|
|
|
|
public static function assertValid(object $job): void
|
|
{
|
|
if (self::isInvalid($job)) {
|
|
throw new CloudRecoveryDispatchLedgerConflict(
|
|
'任务单次派发计数账本异常',
|
|
409
|
|
);
|
|
}
|
|
}
|
|
|
|
public static function assertDispatched(object $job): void
|
|
{
|
|
self::assertValid($job);
|
|
if (!self::isDispatched($job)) {
|
|
throw new CloudError('任务没有跨越 Agent 派发边界', 409);
|
|
}
|
|
}
|
|
|
|
/** @return list<string> */
|
|
public static function repairIncidentCodes(): array
|
|
{
|
|
return self::REPAIR_INCIDENT_CODES;
|
|
}
|
|
|
|
/**
|
|
* A repaired counter remains sticky until the background coordinator has
|
|
* revalidated the immutable terminal row. This closes the brief window in
|
|
* which changing 2 to 0/1 could otherwise release the resource early.
|
|
*/
|
|
public static function hasRepairIncident(object $job): bool
|
|
{
|
|
return (string) ($job->execution_mode ?? '') === 'agent'
|
|
&& (string) ($job->phase ?? '') === 'terminal'
|
|
&& (bool) ($job->finalization_incident ?? false)
|
|
&& in_array(
|
|
(string) ($job->finalization_error_code ?? ''),
|
|
self::REPAIR_INCIDENT_CODES,
|
|
true
|
|
);
|
|
}
|
|
}
|