Added `BaselineReadinessGate`, resolution propagation, and disclosure semantics logic per Spec 385. Integrates baseline unreadiness into Customer Review Workspace and Review Packs to prevent report generation when identity bindings are unresolved. Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de> Reviewed-on: #456
96 lines
4.3 KiB
PHP
96 lines
4.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Evidence\Sources;
|
|
|
|
use App\Models\Finding;
|
|
use App\Models\ManagedEnvironment;
|
|
use App\Models\OperationRun;
|
|
use App\Services\Evidence\Contracts\EvidenceSourceProvider;
|
|
use App\Support\Baselines\Readiness\BaselineEvidenceReadinessDeriver;
|
|
use App\Support\OperationCatalog;
|
|
use App\Support\OperationRunType;
|
|
|
|
final class BaselineDriftPostureSource implements EvidenceSourceProvider
|
|
{
|
|
public function __construct(
|
|
private readonly BaselineEvidenceReadinessDeriver $readinessDeriver,
|
|
) {}
|
|
|
|
public function key(): string
|
|
{
|
|
return 'baseline_drift_posture';
|
|
}
|
|
|
|
public function collect(ManagedEnvironment $tenant): array
|
|
{
|
|
$findings = Finding::query()
|
|
->where('managed_environment_id', (int) $tenant->getKey())
|
|
->where('finding_type', Finding::FINDING_TYPE_DRIFT)
|
|
->latest('updated_at')
|
|
->get();
|
|
|
|
$latestCompareRun = $this->latestBaselineCompareRun($tenant);
|
|
$latestCompareAt = $latestCompareRun?->completed_at
|
|
?? $latestCompareRun?->updated_at
|
|
?? $latestCompareRun?->created_at;
|
|
$latest = $findings->max('updated_at') ?? $findings->max('created_at') ?? $latestCompareAt;
|
|
$isStale = $latest !== null && $latest->lt(now()->subDays(30));
|
|
|
|
$driftCount = $findings->count();
|
|
$openDriftCount = $findings->filter(fn (Finding $finding): bool => $finding->hasOpenStatus())->count();
|
|
$baselineReadiness = $this->readinessDeriver->deriveForEnvironment(
|
|
tenant: $tenant,
|
|
latestCompareRun: $latestCompareRun,
|
|
driftCount: $driftCount,
|
|
openDriftCount: $openDriftCount,
|
|
measuredAt: $latest,
|
|
isStale: $isStale,
|
|
);
|
|
|
|
return [
|
|
'dimension_key' => $this->key(),
|
|
'state' => (string) ($baselineReadiness['state'] ?? 'missing'),
|
|
'required' => true,
|
|
'source_kind' => 'model_summary',
|
|
'source_record_type' => Finding::class,
|
|
'source_record_id' => null,
|
|
'source_fingerprint' => $findings->max('fingerprint'),
|
|
'measured_at' => $latest,
|
|
'freshness_at' => $latest,
|
|
'summary_payload' => [
|
|
'drift_count' => $driftCount,
|
|
'open_drift_count' => $openDriftCount,
|
|
'latest_compare_run_id' => $latestCompareRun instanceof OperationRun ? (int) $latestCompareRun->getKey() : null,
|
|
'latest_compare_outcome' => $latestCompareRun instanceof OperationRun ? (string) $latestCompareRun->outcome : null,
|
|
'latest_compare_completed_at' => $latestCompareRun?->completed_at?->toIso8601String(),
|
|
'baseline_readiness' => $baselineReadiness,
|
|
],
|
|
'fingerprint_payload' => [
|
|
'latest' => $latest?->format(DATE_ATOM),
|
|
'count' => $driftCount,
|
|
'latest_compare_run_id' => $latestCompareRun instanceof OperationRun ? (int) $latestCompareRun->getKey() : null,
|
|
'latest_compare_outcome' => $latestCompareRun instanceof OperationRun ? (string) $latestCompareRun->outcome : null,
|
|
'latest_compare_completed_at' => $latestCompareRun?->completed_at?->toIso8601String(),
|
|
'baseline_readiness_state' => (string) ($baselineReadiness['readiness_state'] ?? 'unknown'),
|
|
'baseline_readiness_counts' => is_array($baselineReadiness['counts'] ?? null) ? $baselineReadiness['counts'] : [],
|
|
'baseline_readiness_limitations' => is_array($baselineReadiness['limitation_codes'] ?? null) ? $baselineReadiness['limitation_codes'] : [],
|
|
'baseline_readiness_blocker_count' => count(is_array($baselineReadiness['publication_blockers'] ?? null) ? $baselineReadiness['publication_blockers'] : []),
|
|
],
|
|
'sort_order' => 40,
|
|
];
|
|
}
|
|
|
|
private function latestBaselineCompareRun(ManagedEnvironment $tenant): ?OperationRun
|
|
{
|
|
return OperationRun::query()
|
|
->where('managed_environment_id', (int) $tenant->getKey())
|
|
->whereIn('type', OperationCatalog::rawValuesForCanonical(OperationRunType::BaselineCompare->value))
|
|
->latest('completed_at')
|
|
->latest('updated_at')
|
|
->latest('id')
|
|
->first();
|
|
}
|
|
}
|