Explaining Findings
Description explains a finding type’s business rule. Optional CanExplain adds
current facts for one occurrence, shared by Console, Filament, and MCP.
Implement the contract
Section titled “Implement the contract”Add DataHealth\Contracts\CanExplain and implement explain(): array. This
complete example uses the same order rule as the quick start:
<?php
declare(strict_types=1);
namespace App\DataHealth;
use App\Models\Order;use DataHealth\Contracts\CanDetect;use DataHealth\Contracts\CanExplain;use DataHealth\Contracts\CanResolve;use DataHealth\Contracts\CanVerify;use DataHealth\Finding;use Illuminate\Support\Facades\DB;
class PaidOrderMarkedPending extends Finding implements CanDetect, CanVerify, CanResolve, CanExplain{ public static function detect(): null { Order::query() ->whereNotNull('paid_at') ->where('status', 'pending') ->eachById(fn (Order $order) => self::found($order));
return null; }
public function explain(): array { $order = $this->model->refresh();
if (! $order instanceof Order) { throw new \LogicException('Expected an Order model.'); }
return [ 'expected' => 'An order with a payment timestamp must not remain pending.', 'actual' => [ 'order_id' => $order->getKey(), 'paid_at' => $order->getRawOriginal('paid_at'), 'status' => $order->status, ], 'still_applies' => $order->paid_at !== null && $order->status === 'pending', 'observed_at' => now()->toIso8601String(), ]; }
public function verify(): bool { $order = $this->model->refresh();
if (! $order instanceof Order) { throw new \LogicException('Expected an Order model.'); }
return $order->paid_at === null || $order->status !== 'pending'; }
public function resolve(): bool { if (! $this->model instanceof Order) { throw new \LogicException('Expected an Order model.'); }
return DB::transaction(function (): bool { $order = Order::query()->lockForUpdate()->findOrFail($this->model->getKey());
if ($order->paid_at === null || $order->status !== 'pending') { return true; }
return $order->update(['status' => 'paid']); }); }}Explanation fields
Section titled “Explanation fields”| Field | Type | Meaning |
|---|---|---|
expected |
string | The expected state or conditional business rule. |
actual |
array with string keys | Explicitly selected, JSON-serializable observed values. |
still_applies |
bool | Whether this occurrence currently violates the rule. |
observed_at |
string | ISO 8601 time of the observation. |
Refresh relevant models and relationships before reading facts. Share the applicability predicate with verification when the rule is complex, so the two operations agree. Throw when data cannot be read or input is invalid. Missing models and failed reads are not evidence that a problem is gone.
explain() must change neither business data nor finding status. Do not call
DataHealth::verify() or DataHealth::resolve() from it. Only select facts
appropriate for the intended audience; avoid serializing whole models or secrets.
An observation establishes an inconsistency, not an unproven cause or consequence.
Read an explanation
Section titled “Read an explanation”use DataHealth\Facades\DataHealth;use DataHealth\Models\FindingRecord;
$record = FindingRecord::query()->findOrFail(123);$explanation = DataHealth::explain($record);The manager throws when the finding does not implement CanExplain or when its
implementation fails. Interfaces are independent: an explainable finding does
not have to implement detection, verification, or resolution.
Use php artisan health:explain 123 for direct console output. Record-browser
inspection includes the description, explanation, and a separate error if the
explanation cannot be read. The Filament record detail page displays expectation,
observed values, current applicability, and observation time. The
MCP integration returns these details through
get_finding; list calls do not run explanations.
Current state and historical evidence
Section titled “Current state and historical evidence”Explanations are computed on demand. They do not modify or replace stored context, participate in deduplication, or create new records. Context identifies the occurrence; keep changing diagnostic values outside it.
still_applies: false does not close an active finding. Run verification to record
that conclusion. observed_at describes the current observation, whereas
last_detected_at describes the stored detection. Historical evidence requires
separate application storage; explanations do not provide a detection snapshot.
Generate a placeholder
Section titled “Generate a placeholder”php artisan health:make:finding PaidOrderMarkedPending --verify --resolve --explain--explain adds the contract and an unfinished method that throws until implemented.
It can also be included in data-health.make_finding.interfaces.