Files
inter-hub/Web/View/DecisionRecords/Show.hs
Bernd Worsch 878d2577ae
Some checks failed
Test / test (push) Has been cancelled
feat(P4): IHF Phase 4 complete — Outcome Observation and Antifragility Loop
Closes the IHF improvement loop. Full antifragility chain now traversable:
Widget → Annotation → Candidate → Requirement → Decision → Deployment → OutcomeSignal

New artifacts:
- DeploymentRecord (immutable, links DecisionRecord to a deployed version)
- OutcomeSignal (append-only; DB trigger prevents UPDATE/DELETE)
- ChangeEvaluation (one-per-deployment; UNIQUE constraint; 1–5 score)

New capabilities:
- DeploymentRecordsController (index, show, new, create)
- RecordOutcomeSignalAction — capture improved/regressed/neutral/inconclusive signals
- Pre/post comparison panel on deployment show (±30-day event/annotation counts)
- Regression detection — improved signal followed by high/critical annotation
- ChangeEvaluation — idempotent score+rationale per deployment
- Recurrence tracking — cycle count per widget, leaderboard
- AntifragilityDashboardAction (autoRefresh, 5 panels) per hub
- Phase 4 integration tests (T01–T08 logic coverage)
- docs/phase4-summary.md; SCOPE.md updated to Phase 4 complete

State Hub: workstream 07e9c860 → completed

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-29 12:27:30 +00:00

257 lines
12 KiB
Haskell

module Web.View.DecisionRecords.Show where
import Web.Types
import Generated.Types
import IHP.Prelude
import IHP.ViewPrelude
data ShowView = ShowView
{ record :: !DecisionRecord
, policyRefs :: ![PolicyReference]
, implRefs :: ![ImplementationChangeReference]
, deploymentRecords :: ![DeploymentRecord]
, evaluations :: ![ChangeEvaluation]
, mRequirement :: !(Maybe Requirement)
, mCandidate :: !(Maybe RequirementCandidate)
, users :: ![User]
}
instance View ShowView where
html ShowView { .. } = [hsx|
<div class="mb-6 flex items-center gap-2 text-sm text-gray-500">
<a href={DecisionRecordsAction} class="hover:text-gray-700">Decisions</a>
<span>/</span>
<span>{record.title}</span>
</div>
<div class="max-w-3xl space-y-6">
<!-- Header card -->
<div class="bg-white rounded-lg border border-gray-200 px-6 py-5">
<div class="flex items-start justify-between mb-3">
<h1 class="text-2xl font-semibold">{record.title}</h1>
<div class="flex gap-2 ml-4">
<span class={outcomeClass record.outcome <> " text-xs px-2 py-0.5 rounded font-medium"}>
{record.outcome}
</span>
<a href={EditDecisionRecordAction { decisionRecordId = record.id }}
class="text-sm border border-gray-300 px-3 py-1.5 rounded hover:bg-gray-50">
Edit
</a>
</div>
</div>
<div class="text-xs text-gray-400 mb-3">
Decided by: {userName users record.decidedBy} · {show record.decidedAt}
</div>
<div class="mb-3">
<p class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-1">Rationale</p>
<p class="text-sm text-gray-700 leading-relaxed">{record.rationale}</p>
</div>
{maybe mempty renderNotes record.notes}
</div>
<!-- Linked requirement -->
<div class="bg-white rounded-lg border border-gray-200 px-6 py-4">
<h2 class="text-sm font-semibold text-gray-700 mb-2">Linked Requirement</h2>
{case mRequirement of
Nothing -> [hsx|<p class="text-sm text-gray-400">No requirement linked.</p>|]
Just req -> [hsx|
<a href={ShowRequirementAction { requirementId = req.id }}
class="text-sm text-indigo-600 hover:text-indigo-800">{req.title}</a>
|]}
</div>
<!-- Source candidate -->
{maybe mempty renderCandidateSection mCandidate}
<!-- Policy references -->
<div class="bg-white rounded-lg border border-gray-200 px-6 py-4">
<h2 class="text-sm font-semibold text-gray-700 mb-3">Policy References</h2>
{forEach policyRefs renderPolicyRef}
<form method="POST" action={AddPolicyReferenceAction { decisionRecordId = record.id }}
class="mt-3 flex items-end gap-2">
{hiddenField "authenticity_token"}
<div>
<label class="text-xs text-gray-500 block mb-1">Scope</label>
<select name="policyScope"
class="text-sm border border-gray-300 rounded px-2 py-1.5">
<option value="internal">internal</option>
<option value="external">external</option>
<option value="regulatory">regulatory</option>
<option value="contractual">contractual</option>
<option value="architectural">architectural</option>
</select>
</div>
<div class="flex-1">
<label class="text-xs text-gray-500 block mb-1">Constraint note (optional)</label>
<input type="text" name="constraintNote"
class="w-full text-sm border border-gray-300 rounded px-2 py-1.5"
placeholder="e.g. GDPR Art. 17 right-to-erasure" />
</div>
<button type="submit"
class="text-sm bg-gray-100 border border-gray-300 px-3 py-1.5 rounded hover:bg-gray-200">
Add
</button>
</form>
</div>
<!-- Deployments -->
<div class="bg-white rounded-lg border border-gray-200 px-6 py-4">
<div class="flex items-center justify-between mb-3">
<h2 class="text-sm font-semibold text-gray-700">Deployments</h2>
{if null implRefs
then mempty
else [hsx|
<a href={(pathTo NewDeploymentRecordAction) <> "?decisionId=" <> show record.id}
class="text-xs border border-indigo-300 text-indigo-600 px-3 py-1 rounded hover:bg-indigo-50">
New Deployment
</a>
|]}
</div>
{if null deploymentRecords
then [hsx|<p class="text-sm text-gray-400">No deployments recorded yet.</p>|]
else [hsx|{forEach deploymentRecords (renderDeploymentRow evaluations)}|]}
</div>
<!-- Implementation references -->
<div class="bg-white rounded-lg border border-gray-200 px-6 py-4">
<h2 class="text-sm font-semibold text-gray-700 mb-3">Implementation References</h2>
{forEach implRefs renderImplRef}
<form method="POST" action={AddImplementationRefAction { decisionRecordId = record.id }}
class="mt-3 flex items-end gap-2">
{hiddenField "authenticity_token"}
<div>
<label class="text-xs text-gray-500 block mb-1">System</label>
<select name="system"
class="text-sm border border-gray-300 rounded px-2 py-1.5">
<option value="github">github</option>
<option value="linear">linear</option>
<option value="jira">jira</option>
<option value="other">other</option>
</select>
</div>
<div class="flex-1">
<label class="text-xs text-gray-500 block mb-1">Work item ref</label>
<input type="text" name="workItemRef"
class="w-full text-sm border border-gray-300 rounded px-2 py-1.5"
placeholder="e.g. #1234, PROJ-456" />
</div>
<button type="submit"
class="text-sm bg-gray-100 border border-gray-300 px-3 py-1.5 rounded hover:bg-gray-200">
Add
</button>
</form>
</div>
</div>
|]
renderNotes :: Text -> Html
renderNotes notes = [hsx|
<div class="mt-2">
<p class="text-xs font-semibold text-gray-500 uppercase tracking-wide mb-1">Notes</p>
<p class="text-sm text-gray-600 italic">{notes}</p>
</div>
|]
renderCandidateSection :: RequirementCandidate -> Html
renderCandidateSection c = [hsx|
<div class="bg-white rounded-lg border border-gray-200 px-6 py-4">
<h2 class="text-sm font-semibold text-gray-700 mb-2">Source Candidate</h2>
<a href={ShowRequirementCandidateAction { requirementCandidateId = c.id }}
class="text-sm text-indigo-600 hover:text-indigo-800">{c.title}</a>
</div>
|]
renderPolicyRef :: PolicyReference -> Html
renderPolicyRef ref = [hsx|
<div class="flex items-start justify-between py-2 border-b border-gray-100 last:border-0">
<div class="flex items-center gap-2 text-sm">
<span class={policyScopeClass ref.policyScope <> " text-xs px-2 py-0.5 rounded font-medium"}>
{ref.policyScope}
</span>
{maybe mempty (\n -> [hsx|<span class="text-gray-600">{n}</span>|]) ref.constraintNote}
<span class="text-xs text-gray-400">{show ref.createdAt}</span>
</div>
<form method="POST"
action={DeletePolicyReferenceAction { policyReferenceId = ref.id }}>
{hiddenField "authenticity_token"}
<button type="submit"
class="text-xs text-red-500 hover:text-red-700 ml-2">Remove</button>
</form>
</div>
|]
renderImplRef :: ImplementationChangeReference -> Html
renderImplRef ref = [hsx|
<div class="flex items-start justify-between py-2 border-b border-gray-100 last:border-0">
<div class="flex items-center gap-2 text-sm">
<span class={systemBadgeClass ref.system <> " text-xs px-2 py-0.5 rounded font-medium"}>
{ref.system}
</span>
<span class="font-mono text-gray-700">{ref.workItemRef}</span>
<span class="text-xs text-gray-400">{show ref.linkedAt}</span>
</div>
<form method="POST"
action={DeleteImplementationRefAction { implementationChangeReferenceId = ref.id }}>
{hiddenField "authenticity_token"}
<button type="submit"
class="text-xs text-red-500 hover:text-red-700 ml-2">Remove</button>
</form>
</div>
|]
outcomeClass :: Text -> Text
outcomeClass "accepted" = "bg-green-100 text-green-800"
outcomeClass "rejected" = "bg-red-100 text-red-800"
outcomeClass "deferred" = "bg-gray-100 text-gray-600"
outcomeClass "split" = "bg-purple-100 text-purple-800"
outcomeClass "merged" = "bg-indigo-100 text-indigo-800"
outcomeClass "reframed" = "bg-orange-100 text-orange-800"
outcomeClass _ = "bg-gray-100 text-gray-600"
policyScopeClass :: Text -> Text
policyScopeClass "regulatory" = "bg-red-50 text-red-700 border border-red-200"
policyScopeClass "contractual" = "bg-orange-50 text-orange-700 border border-orange-200"
policyScopeClass "external" = "bg-yellow-50 text-yellow-700 border border-yellow-200"
policyScopeClass "architectural"= "bg-blue-50 text-blue-700 border border-blue-200"
policyScopeClass _ = "bg-gray-50 text-gray-600 border border-gray-200"
systemBadgeClass :: Text -> Text
systemBadgeClass "github" = "bg-gray-800 text-white"
systemBadgeClass "linear" = "bg-violet-100 text-violet-800"
systemBadgeClass "jira" = "bg-blue-100 text-blue-800"
systemBadgeClass _ = "bg-gray-100 text-gray-600"
renderDeploymentRow :: [ChangeEvaluation] -> DeploymentRecord -> Html
renderDeploymentRow evals dr = [hsx|
<div class="flex items-center justify-between py-2 border-b border-gray-100 last:border-0">
<div class="flex items-center gap-2 text-sm">
<a href={ShowDeploymentRecordAction { deploymentRecordId = dr.id }}
class="font-mono text-indigo-600 hover:text-indigo-800">{dr.versionRef}</a>
<span class="text-xs text-gray-400">{show dr.deployedAt}</span>
</div>
{maybe mempty renderEvalSummary mEval}
</div>
|]
where
mEval = find (\e -> e.deploymentId == dr.id) evals
renderEvalSummary :: ChangeEvaluation -> Html
renderEvalSummary ev = [hsx|
<span class={scoreClass ev.score <> " text-xs px-2 py-0.5 rounded font-medium"}>
{starsFor ev.score}
</span>
|]
starsFor :: Int16 -> Text
starsFor n = pack (Prelude.replicate (fromIntegral n) '★') <> pack (Prelude.replicate (5 - fromIntegral n) '☆')
scoreClass :: Int16 -> Text
scoreClass n
| n <= 2 = "bg-red-100 text-red-800"
| n == 3 = "bg-yellow-100 text-yellow-800"
| otherwise = "bg-green-100 text-green-800"
userName :: [User] -> Maybe (Id User) -> Text
userName _ Nothing = ""
userName users (Just uid) = maybe "(unknown)" (.name) (find (\u -> u.id == uid) users)