feat(ucepsa): add context-aware stop candidates shadow v0.3.1

This commit is contained in:
Victor Fraile Garcia 2026-07-17 12:46:29 +02:00
parent 73ce89be09
commit e7aa13c433
3 changed files with 1381 additions and 0 deletions

View File

@ -0,0 +1,58 @@
# UCEPSA — LossIntelligence Context-Aware Stops SHADOW v0.3.1
## Objetivo
Cruzar los paros físicos de WISE/RevPi con:
- cobertura válida de telemetría;
- contexto Odoo válido;
- producción legacy autorizada;
- producción manual autorizada.
## Seguridad
La migración 102 solo crea vistas paralelas.
No modifica:
- `li_stop_candidates_v2`;
- `li_stop_groups_v2`;
- `li_shadow_review_queue_v2`;
- `li_operator_stop_queue_v2`;
- `loss_ledger`;
- Odoo;
- RevPi;
- nodos WISE;
- balizas.
`official_ledger_eligible` queda forzado a `false`.
## Vistas nuevas
- `li_stop_context_slices_v1`
- `li_stop_candidates_context_v1`
- `li_stop_groups_context_v1`
- `li_shadow_review_queue_context_v1`
- `li_shadow_review_backlog_context_v1`
- `li_stop_context_diagnostics_v1`
## Comportamiento esperado durante la prueba
CORT-00:
- sesión legacy 1 abierta;
- un paro abierto se muestra como `OPEN_MONITORING`;
- al cerrar físicamente el paro, se convierte en candidato si supera el ruido.
CORT-02:
- `WH/MO/00081`;
- paros de 25 segundos deben quedar como ruido;
- paros superiores al umbral deben agruparse o mostrarse individualmente.
## Cola diaria
La cola diaria context-aware está limitada a las últimas 24 horas para evitar que
los falsos positivos históricos dominen la revisión.
El backlog histórico sigue disponible en una vista separada.

View File

@ -0,0 +1,193 @@
\pset pager off
\echo '================================================'
\echo '1. VISTAS CREADAS'
\echo '================================================'
SELECT
to_regclass(
'mv_reports_ucepsa_prod.li_stop_context_slices_v1'
) AS slices_view,
to_regclass(
'mv_reports_ucepsa_prod.li_stop_candidates_context_v1'
) AS candidates_view,
to_regclass(
'mv_reports_ucepsa_prod.li_stop_groups_context_v1'
) AS groups_view,
to_regclass(
'mv_reports_ucepsa_prod.li_shadow_review_queue_context_v1'
) AS shadow_queue_view,
to_regclass(
'mv_reports_ucepsa_prod.li_stop_context_diagnostics_v1'
) AS diagnostics_view;
\echo '================================================'
\echo '2. INVARIANTES: TODAS DEBEN DEVOLVER 0'
\echo '================================================'
SELECT COUNT(*) AS invalid_slice_intervals
FROM mv_reports_ucepsa_prod.li_stop_context_slices_v1
WHERE segment_to <= segment_from
OR segment_duration_s <= 0;
SELECT COUNT(*) AS candidate_duration_overflow
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE context_segment_duration_s > raw_duration_s + 0.01
OR eligible_duration_s > context_segment_duration_s + 0.01
OR eligible_duration_s < 0;
SELECT COUNT(*) AS open_stops_marked_eligible
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE source_status = 'OPEN'
AND technically_eligible = true;
SELECT COUNT(*) AS official_rows_in_parallel_v1
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE official_ledger_eligible = true;
SELECT COUNT(*) AS official_group_rows_in_parallel_v1
FROM mv_reports_ucepsa_prod.li_stop_groups_context_v1
WHERE official_ledger_eligible = true;
\echo '================================================'
\echo '3. SEGURIDAD SHADOW Y COLA OFICIAL ANTERIOR'
\echo '================================================'
SELECT
mode,
actual_go_live_at
FROM mv_loss_intelligence.deployment_config
WHERE tenant = 'ucepsa'
AND site = 'ucepsa_onpremise'
AND scope_code = 'CUTTERS'
ORDER BY id DESC
LIMIT 1;
SELECT COUNT(*) AS existing_official_operator_queue_rows
FROM mv_reports_ucepsa_prod.li_operator_stop_queue_v2;
\echo '================================================'
\echo '4. POLÍTICA DE PAROS'
\echo '================================================'
SELECT
policy_code,
noise_below_s,
aggregate_below_s,
operator_individual_from_s,
aggregation_window_min,
require_valid_order,
require_closed_event,
require_telemetry_coverage,
telemetry_gap_s,
telemetry_tail_s
FROM mv_loss_intelligence.loss_policies
WHERE tenant = 'ucepsa'
AND site = 'ucepsa_onpremise'
AND policy_code = 'CUTTER_STOPS_V1'
AND is_active = true
ORDER BY id DESC
LIMIT 1;
\echo '================================================'
\echo '5. DIAGNÓSTICO ÚLTIMAS 24 HORAS'
\echo '================================================'
SELECT *
FROM mv_reports_ucepsa_prod.li_stop_context_diagnostics_v1;
\echo '================================================'
\echo '6. CORT-00 — SESIÓN LEGACY 1'
\echo '================================================'
SELECT
source_stop_id,
source_status,
started_at,
ended_at,
ROUND(eligible_duration_s / 60.0, 2)
AS eligible_duration_min,
context_status,
primary_context_source,
primary_context_session_id,
eligibility_status,
technically_eligible,
shadow_capacity_loss_eur,
official_ledger_eligible
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE machine_id = 'CORT-00'
AND primary_context_session_id = 1
ORDER BY source_stop_id, started_at;
\echo '================================================'
\echo '7. CORT-02 — ORDEN WH/MO/00081'
\echo '================================================'
SELECT
source_stop_id,
source_status,
started_at,
ended_at,
ROUND(eligible_duration_s, 2) AS eligible_duration_s,
context_status,
primary_context_source,
order_ref,
eligibility_status,
technically_eligible,
shadow_capacity_loss_eur,
official_ledger_eligible
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE machine_id = 'CORT-02'
AND order_ref = 'WH/MO/00081'
ORDER BY source_stop_id, started_at;
\echo '================================================'
\echo '8. COLA SHADOW CONTEXT-AWARE ÚLTIMAS 24 HORAS'
\echo '================================================'
SELECT
shadow_rank,
source_key,
machine_id,
context_status,
primary_context_source,
primary_context_session_id,
order_ref,
event_count,
started_at,
ended_at,
duration_min,
capacity_loss_eur,
preliminary_priority,
queue_status,
official_ledger_eligible,
source_stop_ids
FROM mv_reports_ucepsa_prod.li_shadow_review_queue_context_v1
ORDER BY shadow_rank
LIMIT 30;
\echo '================================================'
\echo '9. CONFLICTOS RECIENTES'
\echo '================================================'
SELECT
source_stop_id,
machine_id,
started_at,
ended_at,
context_count,
context_sources,
eligibility_status
FROM mv_reports_ucepsa_prod.li_stop_candidates_context_v1
WHERE raw_started_at >= now() - interval '24 hours'
AND context_count > 1
ORDER BY machine_id, started_at;
\echo '================================================'
\echo '10. CONFIRMACIÓN DE NO MODIFICACIÓN'
\echo '================================================'
SELECT
'INFO' AS status,
'102 crea vistas paralelas; no reemplaza v2, no escribe en loss_ledger y no modifica Odoo, WISE, RevPi ni balizas.'
AS description;