feat(WP-0013): IHF Phase 12 — Platform Memory and Continuous Learning

Closes the long-range feedback loop: outcome signals now enrich the full
traceability chain and feed back into routing, triage, and AI proposals.

Schema (T01):
- outcome_correlations (CHECK correlation_type)
- pattern_performance_records
- adaptive_threshold_configs
- institutional_knowledge_entries (GIN tsvector FTS)
- learning_insights (CHECK insight_type)
- ALTER TABLE decision_records + requirement_candidates: outcome_summary JSONB
- AFTER INSERT trigger trg_enrich_lineage on outcome_signals
- contracts/core/ updated (outcome-summary-columns-v1, append-only addendum)

Correlation engine (T02):
- Application/Helper/CorrelationEngine.hs: pure annotation→outcome SQL
- Web/Controller/OutcomeCorrelations.hs: ComputeCorrelationsAction + index

Pattern performance (T03):
- Web/Controller/PatternPerformance.hs: ComputePatternPerformanceAction

Adaptive thresholds (T04):
- Web/Controller/AdaptiveThresholds.hs: CalibrateThresholdsAction
- Application/Helper/FrictionScore.hs: applyAdaptiveWeights

Institutional knowledge (T05):
- DistilDecisionAction in DecisionRecords controller
- Web/Controller/InstitutionalKnowledge.hs: QueryKnowledgeBaseAction

Lineage enrichment (T06):
- Web/Controller/LineageEnrichment.hs: EnrichLineageAction (batch backfill)
- enrich_lineage_on_outcome_batch() PL/pgSQL helper in migration

Learning dashboard (T07):
- Web/Controller/LearningDashboard.hs: 5-panel autoRefresh view
- "Learning" nav link in FrontController

API v2 learning endpoints (T08):
- GET /api/v2/outcome-correlations, /pattern-performance, /knowledge-base/{id}
- OpenAPI schemas: OutcomeCorrelation, PatternPerformanceRecord, InstitutionalKnowledgeEntry

GAAF scorecard + docs (T09):
- Core 3.8→3.9, Functional 3.6→3.8, overall 3.61→3.68
- CLAUDE.md: IHF v0.2 complete, no active workplan

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-01 23:14:15 +00:00
parent 9643173618
commit 0f505feb2d
28 changed files with 1574 additions and 17 deletions

View File

@@ -0,0 +1,62 @@
module Web.View.AdaptiveThresholds.Index where
import Web.View.Prelude
import Data.Time (diffUTCTime)
data IndexView = IndexView
{ hubs :: ![Hub]
, configs :: ![AdaptiveThresholdConfig]
, insights :: ![LearningInsight]
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">Adaptive Thresholds</h1>
</div>
<div class="grid grid-cols-1 gap-4 mb-8">
{forM_ hubs (renderHubCard configs)}
</div>
<h2 class="text-lg font-semibold text-gray-800 mb-3">Recent Calibration Insights</h2>
<div class="space-y-3">
{forM_ insights renderInsight}
</div>
</div>
|]
where
renderHubCard cs h =
let mCfg = find (\c -> c.hubId == h.id) cs
in [hsx|
<div class="bg-white shadow rounded-lg p-5">
<div class="flex justify-between items-start">
<div>
<h3 class="font-semibold text-gray-900">{h.name}</h3>
{case mCfg of
Nothing -> [hsx|<p class="text-sm text-gray-400 mt-1">Not calibrated</p>|]
Just cfg -> [hsx|
<p class="text-sm text-gray-600 mt-1">
Last calibrated: {show cfg.calibrationDate}
</p>
<p class="text-sm text-gray-500">{maybe "" id cfg.notes}</p>
|]}
</div>
<form method="POST" action={CalibrateThresholdsAction { hubIdForThreshold = h.id }}>
{csrfTokenTag}
<button type="submit"
class="px-3 py-1.5 text-sm bg-indigo-600 text-white rounded hover:bg-indigo-700">
Calibrate
</button>
</form>
</div>
</div>
|]
renderInsight i = [hsx|
<div class="bg-white shadow rounded-sm p-4 border-l-4 border-indigo-400">
<p class="text-sm font-medium text-gray-800">{i.title}</p>
<p class="text-sm text-gray-600 mt-1">{i.body}</p>
<p class="text-xs text-gray-400 mt-1">{show i.computedAt}</p>
</div>
|]

View File

@@ -0,0 +1,58 @@
module Web.View.InstitutionalKnowledge.Index where
import Web.View.Prelude
data IndexView = IndexView
{ entries :: ![InstitutionalKnowledgeEntry]
, hubs :: ![Hub]
, mQuery :: !(Maybe Text)
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">Institutional Knowledge Base</h1>
</div>
<form method="GET" action={QueryKnowledgeBaseAction} class="mb-6 flex gap-3 items-end">
<div class="flex-1">
<label class="block text-sm font-medium text-gray-700 mb-1">Search</label>
<input type="text" name="q"
value={fromMaybe "" mQuery}
placeholder="Search knowledge entries…"
class="w-full border border-gray-300 rounded-md px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-blue-500" />
</div>
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Hub</label>
<select name="hubId" class="border border-gray-300 rounded-md px-3 py-2 text-sm">
<option value="">All hubs</option>
{forM_ hubs \h -> [hsx|<option value={show h.id}>{h.name}</option>|]}
</select>
</div>
<button type="submit"
class="px-4 py-2 bg-blue-600 text-white rounded-md text-sm hover:bg-blue-700">
Search
</button>
</form>
{if null entries
then [hsx|<p class="text-gray-500 text-sm">No entries found.</p>|]
else [hsx|
<div class="space-y-4">
{forM_ entries renderEntry}
</div>
|]}
</div>
|]
where
renderEntry e = [hsx|
<div class="bg-white shadow rounded-lg p-5">
<div class="flex justify-between items-start">
<p class="text-sm text-gray-800 leading-relaxed flex-1 mr-4">{e.summary}</p>
<a href={ShowInstitutionalKnowledgeAction { knowledgeEntryId = e.id }}
class="text-sm text-blue-600 hover:underline whitespace-nowrap">View</a>
</div>
<p class="text-xs text-gray-400 mt-2">{show e.createdAt}</p>
</div>
|]

View File

@@ -0,0 +1,39 @@
module Web.View.InstitutionalKnowledge.Show where
import Web.View.Prelude
data ShowView = ShowView
{ entry :: !InstitutionalKnowledgeEntry
, hub :: !Hub
, mDecision :: !(Maybe DecisionRecord)
}
instance View ShowView where
html ShowView { .. } = [hsx|
<div class="p-6 max-w-3xl">
<div class="mb-4">
<a href={InstitutionalKnowledgeAction} class="text-sm text-blue-600 hover:underline">
Knowledge Base
</a>
</div>
<div class="bg-white shadow rounded-lg p-6">
<div class="flex justify-between items-start mb-4">
<span class="text-xs bg-blue-100 text-blue-800 px-2 py-1 rounded">{hub.name}</span>
<span class="text-xs text-gray-400">{show entry.createdAt}</span>
</div>
<p class="text-gray-800 leading-relaxed text-sm">{entry.summary}</p>
{case mDecision of
Nothing -> mempty
Just dr -> [hsx|
<div class="mt-5 border-t pt-4">
<p class="text-xs font-medium text-gray-500 uppercase mb-2">Source Decision</p>
<a href={ShowDecisionRecordAction { decisionRecordId = dr.id }}
class="text-sm text-blue-600 hover:underline">
{dr.title}
</a>
<span class="ml-2 text-xs text-gray-500">({dr.outcome})</span>
</div>
|]}
</div>
</div>
|]

View File

@@ -0,0 +1,151 @@
module Web.View.LearningDashboard.Show where
import Web.View.Prelude
import Data.Time (diffUTCTime, getCurrentTime, nominalDay)
data ShowView = ShowView
{ topCorrelations :: ![OutcomeCorrelation]
, patternRankings :: ![PatternPerformanceRecord]
, thresholdStatus :: ![(Hub, Maybe AdaptiveThresholdConfig)]
, recentInsights :: ![LearningInsight]
, knowledgeHighlights :: ![InstitutionalKnowledgeEntry]
}
instance View ShowView where
html ShowView { .. } = [hsx|
<div class="p-6">
<h1 class="text-2xl font-bold text-gray-900 mb-6">Learning Dashboard</h1>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
{-- Panel 1: Top annotation predictors --}
<div class="bg-white shadow rounded-lg p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4">Top Annotation Predictors</h2>
{if null topCorrelations
then [hsx|<p class="text-sm text-gray-400">No data run correlation analysis per hub.</p>|]
else [hsx|
<div class="space-y-2">
{forM_ topCorrelations renderCorrelation}
</div>
|]}
</div>
{-- Panel 2: Pattern performance ranking --}
<div class="bg-white shadow rounded-lg p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4">Pattern Performance Ranking</h2>
{if null patternRankings
then [hsx|<p class="text-sm text-gray-400">No data run pattern performance per hub.</p>|]
else [hsx|
<div class="space-y-2">
{forM_ patternRankings renderPatternRank}
</div>
|]}
</div>
</div>
<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
{-- Panel 3: Adaptive threshold status --}
<div class="bg-white shadow rounded-lg p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4">Adaptive Threshold Status</h2>
<div class="space-y-2">
{forM_ thresholdStatus renderThresholdStatus}
</div>
</div>
{-- Panel 4: Recent learning insights --}
<div class="bg-white shadow rounded-lg p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4">Recent Insights</h2>
{if null recentInsights
then [hsx|<p class="text-sm text-gray-400">No insights yet.</p>|]
else [hsx|
<div class="space-y-3">
{forM_ recentInsights renderInsight}
</div>
|]}
</div>
{-- Panel 5: Knowledge base highlights --}
<div class="bg-white shadow rounded-lg p-5">
<h2 class="text-base font-semibold text-gray-800 mb-4">Knowledge Highlights</h2>
{if null knowledgeHighlights
then [hsx|<p class="text-sm text-gray-400">No entries yet distil a decision.</p>|]
else [hsx|
<div class="space-y-3">
{forM_ knowledgeHighlights renderKnowledge}
</div>
|]}
</div>
</div>
</div>
|]
where
renderCorrelation c = [hsx|
<div class="flex items-center gap-3">
<span class="text-sm text-gray-700 w-32 truncate">{c.annotationCategory}</span>
<div class="flex-1 bg-gray-200 rounded-full h-2">
<div class={barColor c.correlationScore}
style={"width:" <> show (round (c.correlationScore * 100) :: Int) <> "%"}></div>
</div>
<span class="text-xs text-gray-500">{show (round (c.correlationScore * 100) :: Int)}%</span>
</div>
|]
barColor s
| s >= 0.7 = "h-2 rounded-full bg-green-500" :: Text
| s >= 0.4 = "h-2 rounded-full bg-yellow-500"
| otherwise = "h-2 rounded-full bg-red-400"
renderPatternRank r =
let rate = if r.totalOutcomeCount > 0
then fromIntegral r.positiveOutcomeCount / fromIntegral r.totalOutcomeCount :: Double
else 0.0
in [hsx|
<div class="flex items-center gap-2">
<span class="text-xs text-gray-400 w-5">{maybe "-" show r.outcomeRank}</span>
<a href={ShowWidgetPatternAction { widgetPatternId = r.widgetPatternId }}
class="text-sm text-blue-600 hover:underline truncate flex-1">
{show r.widgetPatternId}
</a>
<span class="text-xs text-gray-500">{show (round (rate * 100) :: Int)}%</span>
</div>
|]
renderThresholdStatus (h, mCfg) =
let driftClass = case mCfg of
Nothing -> "text-red-500" :: Text
Just cfg -> "text-green-600"
label = case mCfg of
Nothing -> "Not calibrated"
Just cfg -> "Calibrated"
in [hsx|
<div class="flex justify-between text-sm">
<span class="text-gray-700">{h.name}</span>
<span class={driftClass}>{label}</span>
</div>
|]
renderInsight i = [hsx|
<div class="border-l-4 border-indigo-400 pl-3">
<p class="text-xs font-medium text-gray-700">{i.title}</p>
<p class="text-xs text-gray-400">{insightTypeBadge i.insightType}</p>
</div>
|]
insightTypeBadge t = case t of
"annotation_predictor" -> "Annotation predictor" :: Text
"threshold_calibration" -> "Threshold calibration"
"pattern_ranking" -> "Pattern ranking"
"routing_improvement" -> "Routing improvement"
_ -> t
renderKnowledge e = [hsx|
<div>
<a href={ShowInstitutionalKnowledgeAction { knowledgeEntryId = e.id }}
class="text-sm text-blue-600 hover:underline">
{take 80 e.summary <> if length e.summary > 80 then "" else ""}
</a>
</div>
|]

View File

@@ -0,0 +1,47 @@
module Web.View.LineageEnrichment.Index where
import Web.View.Prelude
data IndexView = IndexView
{ hubs :: ![Hub]
, counts :: ![(Id Hub, Int)]
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">Lineage Enrichment</h1>
</div>
<p class="text-sm text-gray-600 mb-6">
The <code>trg_enrich_lineage</code> trigger automatically enriches
<code>decision_records.outcome_summary</code> and
<code>requirement_candidates.outcome_summary</code> on every new
outcome signal. Use the buttons below to backfill existing records.
</p>
<div class="grid grid-cols-1 gap-4">
{forM_ hubs (renderHubCard counts)}
</div>
</div>
|]
where
renderHubCard cs h =
let unenriched = maybe 0 snd (find (\(hid, _) -> hid == h.id) cs)
in [hsx|
<div class="bg-white shadow rounded-lg p-5 flex justify-between items-center">
<div>
<h3 class="font-semibold text-gray-900">{h.name}</h3>
<p class="text-sm text-gray-500 mt-1">
{show unenriched} decision(s) pending enrichment
</p>
</div>
<form method="POST" action={EnrichLineageAction { hubIdForLineage = h.id }}>
{csrfTokenTag}
<button type="submit"
class="px-3 py-1.5 text-sm bg-green-600 text-white rounded hover:bg-green-700"
disabled={unenriched == 0}>
{if unenriched == 0 then "Up to date" else "Enrich Now"}
</button>
</form>
</div>
|]

View File

@@ -0,0 +1,71 @@
module Web.View.OutcomeCorrelations.Index where
import Web.View.Prelude
data IndexView = IndexView
{ correlations :: ![OutcomeCorrelation]
, hubs :: ![Hub]
, mHubFilter :: !(Maybe (Id Hub))
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">Outcome Correlations</h1>
</div>
<form method="GET" class="mb-6 flex gap-4 items-end">
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Filter by Hub</label>
<select name="hubId" class="border border-gray-300 rounded-md px-3 py-2 text-sm">
<option value="">All hubs</option>
{forM_ hubs \h -> [hsx|
<option value={show h.id}
selected={mHubFilter == Just h.id}>
{h.name}
</option>
|]}
</select>
</div>
<button type="submit" class="px-4 py-2 bg-gray-600 text-white rounded-md text-sm hover:bg-gray-700">
Filter
</button>
</form>
<div class="bg-white shadow rounded-lg overflow-hidden">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Category</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Type</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Score</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Samples</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Computed</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
{forM_ correlations renderRow}
</tbody>
</table>
</div>
</div>
|]
where
renderRow c = [hsx|
<tr>
<td class="px-6 py-4 text-sm font-medium text-gray-900">{c.annotationCategory}</td>
<td class="px-6 py-4 text-sm text-gray-500">{c.correlationType}</td>
<td class="px-6 py-4 text-sm">
<span class={scoreClass c.correlationScore}>
{show (round (c.correlationScore * 100) :: Int)}%
</span>
</td>
<td class="px-6 py-4 text-sm text-gray-500">{show c.sampleCount}</td>
<td class="px-6 py-4 text-sm text-gray-500">{show c.computedAt}</td>
</tr>
|]
scoreClass s
| s >= 0.7 = "inline-block px-2 py-1 text-xs font-semibold bg-green-100 text-green-800 rounded" :: Text
| s >= 0.4 = "inline-block px-2 py-1 text-xs font-semibold bg-yellow-100 text-yellow-800 rounded"
| otherwise = "inline-block px-2 py-1 text-xs font-semibold bg-red-100 text-red-800 rounded"

View File

@@ -0,0 +1,64 @@
module Web.View.PatternPerformance.Index where
import Web.View.Prelude
data IndexView = IndexView
{ records :: ![PatternPerformanceRecord]
, hubs :: ![Hub]
}
instance View IndexView where
html IndexView { .. } = [hsx|
<div class="p-6">
<div class="flex justify-between items-center mb-6">
<h1 class="text-2xl font-bold text-gray-900">Pattern Performance</h1>
</div>
<div class="mb-4 flex gap-2 flex-wrap">
{forM_ hubs \h -> [hsx|
<form method="POST" action={ComputePatternPerformanceAction { hubIdForPerformance = h.id }} class="inline">
{csrfTokenTag}
<button type="submit"
class="px-3 py-1.5 text-sm bg-indigo-600 text-white rounded hover:bg-indigo-700">
Recompute for {h.name}
</button>
</form>
|]}
</div>
<div class="bg-white shadow rounded-lg overflow-hidden">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Rank</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Pattern</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Adoptions</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Positive / Total</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Positive Rate</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Mean Value</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
{forM_ records renderRow}
</tbody>
</table>
</div>
</div>
|]
where
renderRow r =
let rate = if r.totalOutcomeCount > 0
then fromIntegral r.positiveOutcomeCount / fromIntegral r.totalOutcomeCount :: Double
else 0.0
rankLabel = maybe "-" show r.outcomeRank
in [hsx|
<tr>
<td class="px-6 py-4 text-sm text-gray-500">{rankLabel}</td>
<td class="px-6 py-4 text-sm font-medium text-gray-900">
<a href={ShowWidgetPatternAction { widgetPatternId = r.widgetPatternId }}
class="text-blue-600 hover:underline">{show r.widgetPatternId}</a>
</td>
<td class="px-6 py-4 text-sm text-gray-500">{show r.adoptionCount}</td>
<td class="px-6 py-4 text-sm text-gray-500">{show r.positiveOutcomeCount}/{show r.totalOutcomeCount}</td>
<td class="px-6 py-4 text-sm text-gray-500">{show (round (rate * 100) :: Int)}%</td>
<td class="px-6 py-4 text-sm text-gray-500">{maybe "-" show r.meanOutcomeValue}</td>
</tr>
|]