Files
inter-hub/Web/Controller/AdaptiveThresholds.hs
tegwick 2106000cc7
Some checks failed
Test / test (push) Has been cancelled
fix: resolve all GHC 9.10.3 / IHP 1.5 compile errors (all 616 modules load)
Fix 13 modules that blocked compilation on Alpine:

- FrontController: remove annotationLauncherScript helper (IHP Html is a
  constrained type synonym); add (?context, ?request) constraint to
  defaultLayout matching what setLayout expects
- HubCapabilityManifests: switch JSONB fill to paramList+toJSON; fix dynamic
  SQL Text→Query via fromString/cs; void sqlExec; add Control.Monad.void
- Hubs: replace raw Array sqlQuery with filterWhereIn query builder;
  fix isInList validators
- DecisionRecords: remove unregistered DistilDecisionAction; fix hub
  resolution chain via candidateId→sourceWidgetId; BridgeResponse(..)
- RequirementCandidates: BridgeResponse(..); remove @Widget type apps from
  fetchOneOrNothing; void ConfidenceAnnotation createRecord
- AdaptiveThresholds: fix sqlQuery tuple param (Only hubId)
- AgentDelegations, AgentRegistrations, Widgets: BridgeResponse(..)
- Annotations, DeploymentRecords, GovernanceTemplates: minor type fixes
- DecisionRecords/Edit view: extract formAction before HSX block

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-29 10:46:50 +02:00

88 lines
3.6 KiB
Haskell

module Web.Controller.AdaptiveThresholds where
-- IHF Phase 12 — Platform Memory (IHUB-WP-0013 T04)
import Web.Controller.Prelude
import Web.View.AdaptiveThresholds.Index
import IHP.ModelSupport (sqlQuery)
import Database.PostgreSQL.Simple (Only(..))
import qualified Data.Aeson as A
instance Controller AdaptiveThresholdsController where
beforeAction = ensureIsUser
action AdaptiveThresholdsAction = do
hubs <- query @Hub |> orderByAsc #name |> fetch
configs <- query @AdaptiveThresholdConfig |> fetch
insights <- query @LearningInsight
|> filterWhere (#insightType, "threshold_calibration")
|> orderByDesc #computedAt
|> limit 10
|> fetch
render IndexView { hubs, configs, insights }
action CalibrateThresholdsAction { hubIdForThreshold } = do
let hubId = hubIdForThreshold
-- Step 1: find weak-predictor categories (score < 0.3)
weakCats <- sqlQuery
"SELECT annotation_category FROM outcome_correlations \
\ WHERE hub_id = ? AND correlation_score < 0.3"
(Only hubId)
:: IO [Only Text]
-- Step 2: compute bottleneck threshold override = mean friction score
-- for widgets with at least one negative outcome signal
[Only mBottleneckOverride] <- sqlQuery
"SELECT AVG(fs.score) \
\ FROM friction_scores fs \
\ JOIN widgets w ON w.id = fs.widget_id \
\ WHERE w.hub_id = ? \
\ AND EXISTS ( \
\ SELECT 1 FROM outcome_signals os \
\ JOIN deployment_records dep ON dep.id = os.deployment_id \
\ JOIN decision_records dr ON dr.id = dep.decision_id \
\ JOIN requirements r ON r.id = dr.requirement_id \
\ JOIN requirement_candidates rc ON rc.id = r.candidate_id \
\ WHERE rc.source_widget_id = w.id \
\ AND os.signal_type NOT IN ('success','adoption','satisfaction') \
\ )"
(Only hubId)
:: IO [Only (Maybe Double)]
now <- getCurrentTime
let weakNote = "Weak predictor categories (score < 0.3): "
<> intercalate ", " (map fromOnly weakCats)
-- Step 3: upsert AdaptiveThresholdConfig
existing <- query @AdaptiveThresholdConfig
|> filterWhere (#hubId, hubId)
|> fetchOneOrNothing
case existing of
Just cfg ->
cfg
|> set #bottleneckThresholdOverride mBottleneckOverride
|> set #calibrationDate now
|> set #notes (Just weakNote)
|> updateRecord
Nothing ->
newRecord @AdaptiveThresholdConfig
|> set #hubId hubId
|> set #weightOverrides (A.Object mempty)
|> set #bottleneckThresholdOverride mBottleneckOverride
|> set #calibrationDate now
|> set #notes (Just weakNote)
|> createRecord
-- Step 4: write LearningInsight
newRecord @LearningInsight
|> set #hubId hubId
|> set #insightType "threshold_calibration"
|> set #title "Adaptive threshold calibration completed"
|> set #body ("Calibrated friction thresholds. " <> weakNote
<> maybe "" (\b -> " Bottleneck override: " <> show b) mBottleneckOverride)
|> set #evidenceLinks (A.toJSON ([] :: [A.Value]))
|> createRecord
setSuccessMessage "Threshold calibration complete"
redirectTo AdaptiveThresholdsAction