generated from coulomb/repo-seed
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:
31
Application/Helper/CorrelationEngine.hs
Normal file
31
Application/Helper/CorrelationEngine.hs
Normal file
@@ -0,0 +1,31 @@
|
||||
module Application.Helper.CorrelationEngine where
|
||||
|
||||
import IHP.Prelude
|
||||
import Generated.Types
|
||||
import IHP.ModelSupport (sqlQuery)
|
||||
import Database.PostgreSQL.Simple (Only(..))
|
||||
|
||||
-- | For a hub, compute the correlation score per annotation category:
|
||||
-- fraction of traceability chains ending in a positive outcome signal
|
||||
-- (signal_type IN ('success', 'adoption', 'satisfaction')).
|
||||
computeAnnotationCorrelations ::
|
||||
(?modelContext :: ModelContext) =>
|
||||
Id Hub -> IO [(Text, Double, Int)]
|
||||
-- ^ [(category, score, sample_count)]
|
||||
computeAnnotationCorrelations hubId =
|
||||
sqlQuery
|
||||
"SELECT a.category, \
|
||||
\ COALESCE(AVG(CASE WHEN os.signal_type IN ('success','adoption','satisfaction') \
|
||||
\ THEN 1.0 ELSE 0.0 END), 0) AS score, \
|
||||
\ COUNT(os.id)::int AS sample_count \
|
||||
\ FROM annotations a \
|
||||
\ JOIN widgets w ON w.id = a.widget_id \
|
||||
\ JOIN requirement_candidates rc ON rc.source_widget_id = w.id \
|
||||
\ JOIN requirements r ON r.candidate_id = rc.id \
|
||||
\ JOIN decision_records dr ON dr.requirement_id = r.id \
|
||||
\ JOIN deployment_records dep ON dep.decision_id = dr.id \
|
||||
\ JOIN outcome_signals os ON os.deployment_id = dep.id \
|
||||
\ WHERE w.hub_id = ? \
|
||||
\ GROUP BY a.category \
|
||||
\ ORDER BY score DESC"
|
||||
[hubId]
|
||||
@@ -4,6 +4,8 @@ import IHP.Prelude
|
||||
import IHP.ModelSupport
|
||||
import Generated.Types
|
||||
import Data.Time.Clock (addUTCTime, getCurrentTime)
|
||||
import qualified Data.Aeson as A
|
||||
import qualified Data.HashMap.Strict as H
|
||||
|
||||
-- | Friction score formula (documented):
|
||||
--
|
||||
@@ -62,3 +64,35 @@ scoreBand s
|
||||
| s < 40 = "bg-yellow-100 text-yellow-800"
|
||||
| s < 60 = "bg-orange-100 text-orange-800"
|
||||
| otherwise = "bg-red-100 text-red-800"
|
||||
|
||||
-- | Read per-hub AdaptiveThresholdConfig and apply weight_overrides
|
||||
-- to friction component scores before summing. Falls back to global
|
||||
-- defaults when no config exists for the hub.
|
||||
-- weight_overrides keys: "annotation", "error", "regression", "stale"
|
||||
applyAdaptiveWeights ::
|
||||
(?modelContext :: ModelContext) =>
|
||||
Id Hub ->
|
||||
Int -> -- annotationCount
|
||||
Int -> -- errorEventCount
|
||||
Bool -> -- regressionFlag
|
||||
Int -> -- staleCandidateCount
|
||||
IO Int
|
||||
applyAdaptiveWeights hubId annCount errCount isRegressed staleCount = do
|
||||
mConfig <- query @AdaptiveThresholdConfig
|
||||
|> filterWhere (#hubId, hubId)
|
||||
|> fetchOneOrNothing
|
||||
let overrides = maybe mempty (.weightOverrides) mConfig
|
||||
w k def = case overrides of
|
||||
A.Object o -> case H.lookup k o of
|
||||
Just (A.Number n) -> round (n * fromIntegral def) :: Int
|
||||
_ -> def
|
||||
_ -> def
|
||||
annW = w "annotation" 5
|
||||
errW = w "error" 10
|
||||
regW = w "regression" 20
|
||||
staleW = w "stale" 8
|
||||
raw = annCount * annW
|
||||
+ errCount * errW
|
||||
+ (if isRegressed then regW else 0)
|
||||
+ staleCount * staleW
|
||||
pure (min 100 raw)
|
||||
|
||||
182
Application/Migration/1744243200-ihf-phase12-platform-memory.sql
Normal file
182
Application/Migration/1744243200-ihf-phase12-platform-memory.sql
Normal file
@@ -0,0 +1,182 @@
|
||||
-- IHF Phase 12 — Platform Memory and Continuous Learning
|
||||
-- Workplan: IHUB-WP-0013
|
||||
|
||||
-- outcome_correlations
|
||||
CREATE TABLE outcome_correlations (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
annotation_category TEXT NOT NULL REFERENCES annotation_category_registry(name),
|
||||
correlation_type TEXT NOT NULL DEFAULT 'annotation_predictor',
|
||||
correlation_score DOUBLE PRECISION NOT NULL,
|
||||
sample_count INTEGER NOT NULL DEFAULT 0,
|
||||
computed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
CHECK (correlation_type IN ('annotation_predictor', 'routing_quality', 'pattern_quality'))
|
||||
);
|
||||
|
||||
CREATE INDEX outcome_correlations_hub_idx ON outcome_correlations (hub_id);
|
||||
CREATE INDEX outcome_correlations_score_idx ON outcome_correlations (correlation_score DESC);
|
||||
|
||||
-- pattern_performance_records
|
||||
CREATE TABLE pattern_performance_records (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
widget_pattern_id UUID NOT NULL REFERENCES widget_patterns(id),
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
adoption_count INTEGER NOT NULL DEFAULT 0,
|
||||
positive_outcome_count INTEGER NOT NULL DEFAULT 0,
|
||||
total_outcome_count INTEGER NOT NULL DEFAULT 0,
|
||||
mean_outcome_value DOUBLE PRECISION,
|
||||
outcome_rank INTEGER,
|
||||
calibrated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
UNIQUE (widget_pattern_id, hub_id)
|
||||
);
|
||||
|
||||
CREATE INDEX pattern_performance_pattern_idx ON pattern_performance_records (widget_pattern_id);
|
||||
CREATE INDEX pattern_performance_rank_idx ON pattern_performance_records (hub_id, outcome_rank);
|
||||
|
||||
-- adaptive_threshold_configs
|
||||
CREATE TABLE adaptive_threshold_configs (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id) UNIQUE,
|
||||
weight_overrides JSONB NOT NULL DEFAULT '{}',
|
||||
bottleneck_threshold_override DOUBLE PRECISION,
|
||||
calibration_date TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
notes TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX adaptive_threshold_hub_idx ON adaptive_threshold_configs (hub_id);
|
||||
|
||||
-- institutional_knowledge_entries
|
||||
CREATE TABLE institutional_knowledge_entries (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
decision_record_id UUID REFERENCES decision_records(id),
|
||||
summary TEXT NOT NULL,
|
||||
summary_tsv TSVECTOR GENERATED ALWAYS AS (to_tsvector('english', summary)) STORED,
|
||||
tags JSONB NOT NULL DEFAULT '[]',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX institutional_knowledge_hub_idx ON institutional_knowledge_entries (hub_id);
|
||||
CREATE INDEX institutional_knowledge_fts_idx ON institutional_knowledge_entries USING GIN (summary_tsv);
|
||||
|
||||
-- learning_insights
|
||||
CREATE TABLE learning_insights (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
insight_type TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
evidence_links JSONB NOT NULL DEFAULT '[]',
|
||||
is_actioned BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
computed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
CHECK (insight_type IN (
|
||||
'annotation_predictor',
|
||||
'threshold_calibration',
|
||||
'pattern_ranking',
|
||||
'routing_improvement'
|
||||
))
|
||||
);
|
||||
|
||||
CREATE INDEX learning_insights_hub_idx ON learning_insights (hub_id);
|
||||
CREATE INDEX learning_insights_type_idx ON learning_insights (insight_type);
|
||||
|
||||
-- Extend core tables — outcome_summary for retroactive lineage enrichment
|
||||
-- GAAF rule 3: contracts/core/ updated separately
|
||||
ALTER TABLE decision_records
|
||||
ADD COLUMN outcome_summary JSONB NULL;
|
||||
|
||||
ALTER TABLE requirement_candidates
|
||||
ADD COLUMN outcome_summary JSONB NULL;
|
||||
|
||||
-- Retroactive lineage enrichment trigger (T06)
|
||||
CREATE OR REPLACE FUNCTION enrich_lineage_on_outcome()
|
||||
RETURNS TRIGGER LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
v_dec_id UUID;
|
||||
v_req_id UUID;
|
||||
v_cand_id UUID;
|
||||
v_summary JSONB;
|
||||
BEGIN
|
||||
-- Walk chain upward from the new outcome_signal
|
||||
SELECT decision_id INTO v_dec_id
|
||||
FROM deployment_records WHERE id = NEW.deployment_id;
|
||||
|
||||
IF v_dec_id IS NOT NULL THEN
|
||||
v_summary := jsonb_build_object(
|
||||
'signal_type', NEW.signal_type,
|
||||
'value', NEW.value,
|
||||
'observed_at', NEW.observed_at
|
||||
);
|
||||
|
||||
-- Append to decision_records.outcome_summary (non-append-only column)
|
||||
UPDATE decision_records
|
||||
SET outcome_summary = COALESCE(outcome_summary, '[]'::jsonb) || v_summary
|
||||
WHERE id = v_dec_id;
|
||||
|
||||
SELECT requirement_id INTO v_req_id
|
||||
FROM decision_records WHERE id = v_dec_id;
|
||||
|
||||
IF v_req_id IS NOT NULL THEN
|
||||
SELECT candidate_id INTO v_cand_id
|
||||
FROM requirements WHERE id = v_req_id;
|
||||
|
||||
IF v_cand_id IS NOT NULL THEN
|
||||
UPDATE requirement_candidates
|
||||
SET outcome_summary = COALESCE(outcome_summary, '[]'::jsonb) || v_summary
|
||||
WHERE id = v_cand_id;
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
|
||||
CREATE TRIGGER trg_enrich_lineage
|
||||
AFTER INSERT ON outcome_signals
|
||||
FOR EACH ROW EXECUTE FUNCTION enrich_lineage_on_outcome();
|
||||
|
||||
-- Batch enrichment helper: called on-demand by EnrichLineageAction
|
||||
-- Applies the same logic as enrich_lineage_on_outcome() for a given signal id
|
||||
CREATE OR REPLACE FUNCTION enrich_lineage_on_outcome_batch(p_signal_id UUID)
|
||||
RETURNS VOID LANGUAGE plpgsql AS $$
|
||||
DECLARE
|
||||
v_sig RECORD;
|
||||
v_dec_id UUID;
|
||||
v_req_id UUID;
|
||||
v_cand_id UUID;
|
||||
v_summary JSONB;
|
||||
BEGIN
|
||||
SELECT * INTO v_sig FROM outcome_signals WHERE id = p_signal_id;
|
||||
|
||||
SELECT decision_id INTO v_dec_id
|
||||
FROM deployment_records WHERE id = v_sig.deployment_id;
|
||||
|
||||
IF v_dec_id IS NOT NULL THEN
|
||||
v_summary := jsonb_build_object(
|
||||
'signal_type', v_sig.signal_type,
|
||||
'value', v_sig.value,
|
||||
'observed_at', v_sig.observed_at
|
||||
);
|
||||
|
||||
UPDATE decision_records
|
||||
SET outcome_summary = COALESCE(outcome_summary, '[]'::jsonb) || v_summary
|
||||
WHERE id = v_dec_id;
|
||||
|
||||
SELECT requirement_id INTO v_req_id
|
||||
FROM decision_records WHERE id = v_dec_id;
|
||||
|
||||
IF v_req_id IS NOT NULL THEN
|
||||
SELECT candidate_id INTO v_cand_id
|
||||
FROM requirements WHERE id = v_req_id;
|
||||
|
||||
IF v_cand_id IS NOT NULL THEN
|
||||
UPDATE requirement_candidates
|
||||
SET outcome_summary = COALESCE(outcome_summary, '[]'::jsonb) || v_summary
|
||||
WHERE id = v_cand_id;
|
||||
END IF;
|
||||
END IF;
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
@@ -1005,3 +1005,98 @@ ALTER TABLE agent_proposals
|
||||
ADD COLUMN tokens_out INTEGER;
|
||||
|
||||
CREATE INDEX agent_proposals_agent_registration_idx ON agent_proposals (agent_registration_id);
|
||||
|
||||
-- ============================================================
|
||||
-- Phase 12 — Platform Memory and Continuous Learning
|
||||
-- ============================================================
|
||||
|
||||
-- outcome_correlations: links annotation signals to downstream outcome quality
|
||||
-- GAAF: correlation_type CHECK constraint
|
||||
CREATE TABLE outcome_correlations (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
annotation_category TEXT NOT NULL REFERENCES annotation_category_registry(name),
|
||||
correlation_type TEXT NOT NULL DEFAULT 'annotation_predictor',
|
||||
correlation_score DOUBLE PRECISION NOT NULL,
|
||||
sample_count INTEGER NOT NULL DEFAULT 0,
|
||||
computed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
CHECK (correlation_type IN ('annotation_predictor', 'routing_quality', 'pattern_quality'))
|
||||
);
|
||||
|
||||
CREATE INDEX outcome_correlations_hub_idx ON outcome_correlations (hub_id);
|
||||
CREATE INDEX outcome_correlations_score_idx ON outcome_correlations (correlation_score DESC);
|
||||
|
||||
-- pattern_performance_records: per-pattern historical outcome quality
|
||||
CREATE TABLE pattern_performance_records (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
widget_pattern_id UUID NOT NULL REFERENCES widget_patterns(id),
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
adoption_count INTEGER NOT NULL DEFAULT 0,
|
||||
positive_outcome_count INTEGER NOT NULL DEFAULT 0,
|
||||
total_outcome_count INTEGER NOT NULL DEFAULT 0,
|
||||
mean_outcome_value DOUBLE PRECISION,
|
||||
outcome_rank INTEGER,
|
||||
calibrated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
UNIQUE (widget_pattern_id, hub_id)
|
||||
);
|
||||
|
||||
CREATE INDEX pattern_performance_pattern_idx ON pattern_performance_records (widget_pattern_id);
|
||||
CREATE INDEX pattern_performance_rank_idx ON pattern_performance_records (hub_id, outcome_rank);
|
||||
|
||||
-- adaptive_threshold_configs: per-hub friction weight overrides
|
||||
CREATE TABLE adaptive_threshold_configs (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id) UNIQUE,
|
||||
weight_overrides JSONB NOT NULL DEFAULT '{}',
|
||||
bottleneck_threshold_override DOUBLE PRECISION,
|
||||
calibration_date TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
notes TEXT
|
||||
);
|
||||
|
||||
CREATE INDEX adaptive_threshold_hub_idx ON adaptive_threshold_configs (hub_id);
|
||||
|
||||
-- institutional_knowledge_entries: distilled decision summaries
|
||||
-- GIN index for full-text search (PostgreSQL tsvector, no extension needed)
|
||||
CREATE TABLE institutional_knowledge_entries (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
decision_record_id UUID REFERENCES decision_records(id),
|
||||
summary TEXT NOT NULL,
|
||||
summary_tsv TSVECTOR GENERATED ALWAYS AS (to_tsvector('english', summary)) STORED,
|
||||
tags JSONB NOT NULL DEFAULT '[]',
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX institutional_knowledge_hub_idx ON institutional_knowledge_entries (hub_id);
|
||||
CREATE INDEX institutional_knowledge_fts_idx ON institutional_knowledge_entries USING GIN (summary_tsv);
|
||||
|
||||
-- learning_insights: platform-level insights with evidence links
|
||||
-- GAAF: insight_type CHECK constraint
|
||||
CREATE TABLE learning_insights (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
|
||||
hub_id UUID NOT NULL REFERENCES hubs(id),
|
||||
insight_type TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
evidence_links JSONB NOT NULL DEFAULT '[]',
|
||||
is_actioned BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
computed_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
CHECK (insight_type IN (
|
||||
'annotation_predictor',
|
||||
'threshold_calibration',
|
||||
'pattern_ranking',
|
||||
'routing_improvement'
|
||||
))
|
||||
);
|
||||
|
||||
CREATE INDEX learning_insights_hub_idx ON learning_insights (hub_id);
|
||||
CREATE INDEX learning_insights_type_idx ON learning_insights (insight_type);
|
||||
|
||||
-- Extend core tables with outcome_summary (retroactive lineage enrichment)
|
||||
-- GAAF rule 3: /contracts/core/ updated in T01/T06
|
||||
ALTER TABLE decision_records
|
||||
ADD COLUMN outcome_summary JSONB NULL;
|
||||
|
||||
ALTER TABLE requirement_candidates
|
||||
ADD COLUMN outcome_summary JSONB NULL;
|
||||
|
||||
Reference in New Issue
Block a user