TenantAtlas/app/Support/OpsUx/OperationUxPresenter.php
ahmido 3c3daae405 feat: normalize operator outcome taxonomy (#186)
## Summary
- introduce a shared operator outcome taxonomy with semantic axes, severity bands, and next-action policy
- apply the taxonomy to operations, evidence/review completeness, baseline semantics, and restore semantics
- harden badge rendering, tenant-safe filtering/search behavior, and operator-facing summary/notification wording
- add the spec kit artifacts, reference documentation, and regression coverage for diagnostic-vs-primary state handling

## Testing
- focused Pest coverage for taxonomy registry and badge guardrails
- operations presentation and notification tests
- evidence, baseline, restore, and tenant-scope regression tests

## Notes
- Livewire v4.0+ compliance is preserved in the existing Filament v5 stack
- panel provider registration remains unchanged in bootstrap/providers.php
- no new globally searchable resource was added; adopted resources remain tenant-safe and out of global search where required
- no new destructive action family was introduced; existing actions keep their current authorization and confirmation behavior
- no new frontend asset strategy was introduced; existing deploy flow with filament:assets remains unchanged

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #186
2026-03-22 12:13:34 +00:00

208 lines
7.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Support\OpsUx;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Support\OperationCatalog;
use App\Support\RedactionIntegrity;
use Filament\Notifications\Notification as FilamentNotification;
final class OperationUxPresenter
{
public const int QUEUED_TOAST_DURATION_MS = 4000;
public const int FAILURE_MESSAGE_MAX_CHARS = 140;
/**
* Queued intent feedback toast (ephemeral, not persisted).
*/
public static function queuedToast(string $operationType): FilamentNotification
{
$operationLabel = OperationCatalog::label($operationType);
return FilamentNotification::make()
->title("{$operationLabel} queued")
->body('Queued for execution. Open the run for progress and next steps.')
->info()
->duration(self::QUEUED_TOAST_DURATION_MS);
}
/**
* Canonical dedupe feedback when a matching run is already active.
*/
public static function alreadyQueuedToast(string $operationType): FilamentNotification
{
$operationLabel = OperationCatalog::label($operationType);
return FilamentNotification::make()
->title("{$operationLabel} already queued")
->body('A matching run is already queued or running. No action needed unless it stays stuck.')
->info()
->duration(self::QUEUED_TOAST_DURATION_MS);
}
/**
* Terminal DB notification payload.
*
* Note: We intentionally return the built Filament notification builder to
* keep DB formatting consistent with existing Notification classes.
*/
public static function terminalDatabaseNotification(OperationRun $run, ?Tenant $tenant = null): FilamentNotification
{
$operationLabel = OperationCatalog::label((string) $run->type);
$presentation = self::terminalPresentation($run);
$bodyLines = [$presentation['body']];
$failureMessage = self::surfaceFailureDetail($run);
if ($failureMessage !== null) {
$bodyLines[] = $failureMessage;
}
$guidance = self::surfaceGuidance($run);
if ($guidance !== null) {
$bodyLines[] = $guidance;
}
$summary = SummaryCountsNormalizer::renderSummaryLine(is_array($run->summary_counts) ? $run->summary_counts : []);
if ($summary !== null) {
$bodyLines[] = $summary;
}
$integritySummary = RedactionIntegrity::noteForRun($run);
if (is_string($integritySummary) && trim($integritySummary) !== '') {
$bodyLines[] = trim($integritySummary);
}
$notification = FilamentNotification::make()
->title("{$operationLabel} {$presentation['titleSuffix']}")
->body(implode("\n", $bodyLines))
->status($presentation['status']);
if ($tenant instanceof Tenant) {
$notification->actions([
\Filament\Actions\Action::make('view')
->label('View run')
->url(OperationRunUrl::view($run, $tenant)),
]);
}
return $notification;
}
public static function surfaceGuidance(OperationRun $run): ?string
{
$uxStatus = OperationStatusNormalizer::toUxStatus($run->status, $run->outcome);
$nextStepLabel = self::firstNextStepLabel($run);
return match ($uxStatus) {
'queued' => 'No action needed yet. The run is waiting for a worker.',
'running' => 'No action needed yet. The run is currently in progress.',
'succeeded' => 'No action needed.',
'partial' => $nextStepLabel !== null
? 'Next step: '.$nextStepLabel.'.'
: (self::requiresFollowUp($run)
? 'Review the affected items before rerunning.'
: 'No action needed unless the recorded warnings were unexpected.'),
'blocked' => $nextStepLabel !== null
? 'Next step: '.$nextStepLabel.'.'
: 'Review the blocked prerequisite before retrying.',
default => $nextStepLabel !== null
? 'Next step: '.$nextStepLabel.'.'
: 'Review the run details before retrying.',
};
}
public static function surfaceFailureDetail(OperationRun $run): ?string
{
$failureMessage = (string) (($run->failure_summary[0]['message'] ?? '') ?? '');
return self::sanitizeFailureMessage($failureMessage);
}
/**
* @return array{titleSuffix: string, body: string, status: string}
*/
private static function terminalPresentation(OperationRun $run): array
{
$uxStatus = OperationStatusNormalizer::toUxStatus($run->status, $run->outcome);
return match ($uxStatus) {
'succeeded' => [
'titleSuffix' => 'completed successfully',
'body' => 'Completed successfully.',
'status' => 'success',
],
'partial' => [
'titleSuffix' => self::requiresFollowUp($run) ? 'needs follow-up' : 'completed with review notes',
'body' => 'Completed with follow-up.',
'status' => 'warning',
],
'blocked' => [
'titleSuffix' => 'blocked by prerequisite',
'body' => 'Blocked by prerequisite.',
'status' => 'warning',
],
default => [
'titleSuffix' => 'execution failed',
'body' => 'Execution failed.',
'status' => 'danger',
],
};
}
private static function requiresFollowUp(OperationRun $run): bool
{
if (self::firstNextStepLabel($run) !== null) {
return true;
}
$counts = SummaryCountsNormalizer::normalize(is_array($run->summary_counts) ? $run->summary_counts : []);
return (int) ($counts['failed'] ?? 0) > 0;
}
private static function firstNextStepLabel(OperationRun $run): ?string
{
$context = is_array($run->context) ? $run->context : [];
$nextSteps = $context['next_steps'] ?? null;
if (! is_array($nextSteps)) {
return null;
}
foreach ($nextSteps as $nextStep) {
if (! is_array($nextStep)) {
continue;
}
$label = trim((string) ($nextStep['label'] ?? ''));
if ($label !== '') {
return $label;
}
}
return null;
}
private static function sanitizeFailureMessage(string $failureMessage): ?string
{
$failureMessage = trim($failureMessage);
if ($failureMessage === '') {
return null;
}
$failureMessage = RunFailureSanitizer::sanitizeMessage($failureMessage);
if (mb_strlen($failureMessage) > self::FAILURE_MESSAGE_MAX_CHARS) {
$failureMessage = mb_substr($failureMessage, 0, self::FAILURE_MESSAGE_MAX_CHARS - 1).'…';
}
return $failureMessage !== '' ? $failureMessage : null;
}
}