Intelligence, Advanced Charting & Trading Platform
risk/chartnova-alert-runtime-atomic-2026-10-22
diagnostics/chartnova-blank-screen-2026-10-15
diagnostics/chartnova-stage2-v27-preview-2026-10-15
docs/chartnova-stage1-reconcile-manifest-2026-10-15
feature/chartnova-ios-widget
feature/chartnova-light-catalog-lazy-series-2026-10-15
feature/chartnova-market-terminal
feature/chartnova-multimarket-chart-ws-foundation
feature/chartnova-neon-direct-history
feature/chartnova-neon-history
feature/paper-trading-risk-controls-2026-10-14
fix/alpha-vantage-live-payload
fix/chartnova-progressive-loading-2026-10-15
fix/live-proof-runner-cwd-2026-10-14
foundation/phase-0
foundation/postgres-core
main
ops/chartnova-emergency-rollback-2026-10-15
phase-1/candle-builder
phase-1/data-proof-artifact
phase-1/data-proof-metrics
phase-1/data-proof-report
phase-1/external-gates-state
phase-1/gold-api-fallback
phase-1/ingestion-pipeline
phase-1/live-proof-runner
phase-1/market-calendar
phase-1/market-domain
phase-1/neon-live-proof
phase-1/persistence-contract
phase-1/postgres-ci
phase-1/provider-adapters
phase-1/provider-recovery
phase-1/quality-propagation
phase-1/spike-detection
phase-1/xau-provider-adapter
risk/chartnova-alert-runtime-atomic-2026-10-22
risk/chartnova-atomic-outbox-2026-10-22
risk/chartnova-seven-track-safety-2026-10-21
safety/p0-triage-2026-10-13
| 1 | -- Disposable CI PostgreSQL only: no production Neon writes. |
| 2 | BEGIN; |
| 3 | |
| 4 | DO $$ |
| 5 | DECLARE |
| 6 | v_user UUID := 'f1111111-1111-4111-8111-111111111111'; |
| 7 | v_other UUID := 'f2222222-2222-4222-8222-222222222222'; |
| 8 | v_recipient UUID := 'f3333333-3333-4333-8333-333333333333'; |
| 9 | t TIMESTAMPTZ := '2026-10-22 12:00:00+00'; |
| 10 | before_state JSONB; |
| 11 | crossed_state JSONB; |
| 12 | result RECORD; |
| 13 | event_id TEXT := 'threshold1:2026-10-22T12:01:00.000Z:0'; |
| 14 | n INTEGER; |
| 15 | current_status TEXT; |
| 16 | BEGIN |
| 17 | INSERT INTO public.app_users(id) VALUES (v_user),(v_other); |
| 18 | |
| 19 | before_state := jsonb_build_object('alert',jsonb_build_object( |
| 20 | 'id','threshold1','phase','ARMED', |
| 21 | 'lastTimestamp','2026-10-22T12:00:00.000Z', |
| 22 | 'highWater',1000, |
| 23 | 'consecutive',0)); |
| 24 | crossed_state := jsonb_build_object('alert',jsonb_build_object( |
| 25 | 'id','threshold1','phase','TRIGGERED', |
| 26 | 'lastTimestamp','2026-10-22T12:01:00.000Z', |
| 27 | 'triggeredAt','2026-10-22T12:01:00.000Z', |
| 28 | 'eventId',event_id, |
| 29 | 'highWater',1100, |
| 30 | 'consecutive',1), |
| 31 | 'gate',jsonb_build_object( |
| 32 | 'suspended',false, |
| 33 | 'lastSampleAt','2026-10-22T12:01:00.000Z', |
| 34 | 'lastAccepted',jsonb_build_object( |
| 35 | 'timestamp','2026-10-22T12:01:00.000Z', |
| 36 | 'provider','S1', |
| 37 | 'symbol','USD_IRT_FREE', |
| 38 | 'side','SELL', |
| 39 | 'unit','IRT_PER_USD', |
| 40 | 'price',1100 |
| 41 | ) |
| 42 | )); |
| 43 | |
| 44 | -- Safe rule-state writes require no user notification at all. |
| 45 | SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',0, |
| 46 | before_state,NULL,NULL,NULL,t); |
| 47 | IF result.outcome <> 'STATE_SAVED' OR result.new_revision <> 1 |
| 48 | OR result.outbox_id IS NOT NULL THEN |
| 49 | RAISE EXCEPTION 'initial safe state CAS did not commit as expected'; |
| 50 | END IF; |
| 51 | |
| 52 | -- Client that read old revision 0 cannot replace revision 1. |
| 53 | SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',0, |
| 54 | crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 55 | IF result.outcome <> 'STALE_REVISION' OR result.new_revision <> 1 THEN |
| 56 | RAISE EXCEPTION 'stale-version write overwrote durable state'; |
| 57 | END IF; |
| 58 | |
| 59 | -- Even an otherwise valid alert cannot enqueue without current consent. |
| 60 | BEGIN |
| 61 | PERFORM * FROM public.alert_rule_commit(v_user,'threshold1',1, |
| 62 | crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 63 | RAISE EXCEPTION 'unconsented notification was queued'; |
| 64 | EXCEPTION WHEN insufficient_privilege THEN NULL; |
| 65 | END; |
| 66 | |
| 67 | SELECT count(*) INTO n FROM public.notification_outbox |
| 68 | WHERE user_id=v_user; |
| 69 | IF n <> 0 THEN RAISE EXCEPTION 'failed consent check left message in outbox'; END IF; |
| 70 | SELECT revision INTO n FROM public.alert_rule_runtime |
| 71 | WHERE user_id=v_user AND rule_key='threshold1'; |
| 72 | IF n <> 1 THEN RAISE EXCEPTION 'failed consent check changed runtime revision'; END IF; |
| 73 | |
| 74 | INSERT INTO public.alert_notification_consents |
| 75 | (user_id,recipient_ref,channel,consented_at) |
| 76 | VALUES (v_user,v_recipient,'BALE',t-interval '10 minutes'); |
| 77 | -- Simulate a forged trigger with a suspended quality gate. It must not |
| 78 | -- create any outbox record or advance revision even with valid consent. |
| 79 | BEGIN |
| 80 | PERFORM * FROM public.alert_rule_commit(v_user,'threshold1',1, |
| 81 | jsonb_set(crossed_state,'{gate,suspended}','true'::jsonb), |
| 82 | event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 83 | RAISE EXCEPTION 'suspended gate enqueued an informational alert'; |
| 84 | EXCEPTION WHEN invalid_parameter_value THEN NULL; |
| 85 | END; |
| 86 | SELECT count(*) INTO n FROM public.notification_outbox WHERE user_id=v_user; |
| 87 | IF n <> 0 THEN RAISE EXCEPTION 'suspended gate wrote outbox'; END IF; |
| 88 | |
| 89 | |
| 90 | -- Two changes happen in the SAME SQL transaction, no write half-completion. |
| 91 | SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',1, |
| 92 | crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 93 | IF result.outcome <> 'TRIGGERED_ENQUEUED' |
| 94 | OR result.new_revision <> 2 OR result.outbox_id IS NULL THEN |
| 95 | RAISE EXCEPTION 'trigger/enqueue was not atomic'; |
| 96 | END IF; |
| 97 | |
| 98 | SELECT count(*) INTO n FROM public.notification_outbox AS o |
| 99 | WHERE o.user_id=v_user AND o.event_key=event_id |
| 100 | AND o.status='PENDING' AND o.recipient_ref=v_recipient; |
| 101 | IF n <> 1 THEN RAISE EXCEPTION 'trigger enqueue did not create one row'; END IF; |
| 102 | |
| 103 | SELECT count(*) INTO n FROM public.alert_rule_runtime AS r |
| 104 | WHERE r.user_id=v_user AND r.rule_key='threshold1' |
| 105 | AND r.last_event_key=event_id AND r.revision=2 |
| 106 | AND r.state #>> '{alert,phase}'='TRIGGERED'; |
| 107 | IF n <> 1 THEN RAISE EXCEPTION 'trigger state not saved with event'; END IF; |
| 108 | |
| 109 | -- No automatic re-arming or duplicate OCO event from another Worker. |
| 110 | SELECT * INTO result FROM public.alert_rule_commit(v_user,'threshold1',2, |
| 111 | crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 112 | IF result.outcome <> 'ALREADY_TERMINAL' OR result.new_revision <> 2 THEN |
| 113 | RAISE EXCEPTION 'duplicate event was not rejected'; |
| 114 | END IF; |
| 115 | SELECT count(*) INTO n FROM public.notification_outbox WHERE event_key=event_id; |
| 116 | IF n <> 1 THEN RAISE EXCEPTION 'duplicate outbox message created'; END IF; |
| 117 | |
| 118 | -- Consent may be revoked AFTER an informational alert was queued. |
| 119 | -- The future sender must never receive the previously queued message. |
| 120 | UPDATE public.alert_notification_consents |
| 121 | SET revoked_at = t + interval '2 minutes' |
| 122 | WHERE user_id=v_user AND recipient_ref=v_recipient AND channel='BALE'; |
| 123 | |
| 124 | SELECT count(*) INTO n |
| 125 | FROM public.alert_notification_claim_consented(t+interval '3 minutes',30); |
| 126 | IF n <> 0 THEN RAISE EXCEPTION 'revoked recipient was returned to sender'; END IF; |
| 127 | SELECT status INTO current_status FROM public.notification_outbox |
| 128 | WHERE user_id=v_user AND event_key=event_id; |
| 129 | IF current_status <> 'UNKNOWN' THEN |
| 130 | RAISE EXCEPTION 'revoked queued message not terminally suppressed'; |
| 131 | END IF; |
| 132 | SELECT count(*) INTO n |
| 133 | FROM public.alert_notification_claim_consented(t+interval '4 minutes',30); |
| 134 | IF n <> 0 THEN RAISE EXCEPTION 'revoked notification was re-claimed'; END IF; |
| 135 | |
| 136 | -- Active opt-in remains retrievable only through the consent-aware claim. |
| 137 | INSERT INTO public.alert_notification_consents |
| 138 | (user_id,recipient_ref,channel,consented_at) |
| 139 | VALUES(v_other,v_recipient,'BALE',t-interval '10 minutes'); |
| 140 | PERFORM public.notification_outbox_enqueue(v_other,'consented-alert', |
| 141 | 'BALE',v_recipient,3); |
| 142 | SELECT * INTO result |
| 143 | FROM public.alert_notification_claim_consented(t+interval '5 minutes',30); |
| 144 | IF result.event_key <> 'consented-alert' OR result.claim_token IS NULL THEN |
| 145 | RAISE EXCEPTION 'consented claimant did not receive authorized pending event'; |
| 146 | END IF; |
| 147 | IF public.alert_notification_egress_allowed( |
| 148 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '5 minutes 1 second') THEN |
| 149 | RAISE EXCEPTION 'unconsumed send permit bypassed egress gate'; |
| 150 | END IF; |
| 151 | IF public.alert_notification_begin_egress( |
| 152 | result.outbox_id, result.claim_token, gen_random_uuid(),'BALE', |
| 153 | t+interval '5 minutes 1 second') THEN |
| 154 | RAISE EXCEPTION 'foreign recipient reference obtained send permission'; |
| 155 | END IF; |
| 156 | IF public.alert_notification_begin_egress( |
| 157 | result.outbox_id, result.claim_token, v_recipient,'EITA', |
| 158 | t+interval '5 minutes 1 second') THEN |
| 159 | RAISE EXCEPTION 'wrong channel obtained Bale message send permission'; |
| 160 | END IF; |
| 161 | IF NOT public.alert_notification_begin_egress( |
| 162 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '5 minutes 1 second') THEN |
| 163 | RAISE EXCEPTION 'first valid send permit not issued'; |
| 164 | END IF; |
| 165 | IF public.alert_notification_begin_egress( |
| 166 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '5 minutes 2 seconds') THEN |
| 167 | RAISE EXCEPTION 'repeated send permit enabled duplicate outbound message'; |
| 168 | END IF; |
| 169 | IF NOT public.alert_notification_egress_allowed( |
| 170 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '5 minutes 2 seconds') THEN |
| 171 | RAISE EXCEPTION 'single-use permit did not authorize legitimate egress'; |
| 172 | END IF; |
| 173 | IF public.alert_notification_egress_allowed( |
| 174 | result.outbox_id, gen_random_uuid(), v_recipient,'BALE', t+interval '5 minutes 1 second') THEN |
| 175 | RAISE EXCEPTION 'wrong lease token authorized message egress'; |
| 176 | END IF; |
| 177 | IF public.alert_notification_egress_allowed( |
| 178 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '6 minutes') THEN |
| 179 | RAISE EXCEPTION 'expired lease permitted message egress'; |
| 180 | END IF; |
| 181 | PERFORM public.notification_outbox_finish(result.outbox_id,result.claim_token, |
| 182 | 'UNKNOWN',NULL,t+interval '5 minutes 5 seconds'); |
| 183 | |
| 184 | -- A revoked oldest message must not starve a valid queued notification. |
| 185 | PERFORM public.notification_outbox_enqueue(v_user,'revoked-oldest', |
| 186 | 'BALE',v_recipient,3); |
| 187 | PERFORM public.notification_outbox_enqueue(v_other,'active-after-revoked', |
| 188 | 'BALE',v_recipient,3); |
| 189 | UPDATE public.notification_outbox |
| 190 | SET created_at = t-interval '2 hours' |
| 191 | WHERE event_key='revoked-oldest'; |
| 192 | UPDATE public.notification_outbox |
| 193 | SET created_at = t-interval '1 hour' |
| 194 | WHERE event_key='active-after-revoked'; |
| 195 | |
| 196 | SELECT * INTO result FROM public.alert_notification_claim_consented( |
| 197 | t+interval '6 minutes',30); |
| 198 | IF result.event_key <> 'active-after-revoked' THEN |
| 199 | RAISE EXCEPTION 'revoked queue head starved consenting recipient'; |
| 200 | END IF; |
| 201 | SELECT status INTO current_status FROM public.notification_outbox |
| 202 | WHERE event_key='revoked-oldest'; |
| 203 | IF current_status <> 'UNKNOWN' THEN |
| 204 | RAISE EXCEPTION 'revoked queued message not terminated during scan'; |
| 205 | END IF; |
| 206 | PERFORM public.notification_outbox_finish(result.outbox_id,result.claim_token, |
| 207 | 'UNKNOWN',NULL,t+interval '6 minutes 5 seconds'); |
| 208 | |
| 209 | -- Consent can be revoked after a valid claim but BEFORE egress. |
| 210 | -- A second independent DB check must fail even if the lease is current. |
| 211 | PERFORM public.notification_outbox_enqueue(v_other,'egress-after-revocation', |
| 212 | 'BALE',v_recipient,3); |
| 213 | SELECT * INTO result FROM public.alert_notification_claim_consented( |
| 214 | t+interval '7 minutes',30); |
| 215 | IF result.event_key <> 'egress-after-revocation' THEN |
| 216 | RAISE EXCEPTION 'unexpected queue item in revocation preflight'; |
| 217 | END IF; |
| 218 | |
| 219 | UPDATE public.alert_notification_consents |
| 220 | SET revoked_at = t + interval '7 minutes 1 second' |
| 221 | WHERE user_id=v_other AND recipient_ref=v_recipient AND channel='BALE'; |
| 222 | |
| 223 | IF public.alert_notification_egress_allowed( |
| 224 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '7 minutes 2 seconds') THEN |
| 225 | RAISE EXCEPTION 'revoked consent still authorized outbound message'; |
| 226 | END IF; |
| 227 | IF public.alert_notification_begin_egress( |
| 228 | result.outbox_id, result.claim_token, v_recipient,'BALE', t+interval '7 minutes 2 seconds') THEN |
| 229 | RAISE EXCEPTION 'revoked consent issued an outbound send permit'; |
| 230 | END IF; |
| 231 | IF public.alert_notification_egress_allowed( |
| 232 | result.outbox_id, gen_random_uuid(), v_recipient,'BALE', t+interval '7 minutes 2 seconds') THEN |
| 233 | RAISE EXCEPTION 'foreign claim permitted outbound message'; |
| 234 | END IF; |
| 235 | PERFORM public.notification_outbox_finish(result.outbox_id,result.claim_token, |
| 236 | 'UNKNOWN',NULL,t+interval '7 minutes 3 seconds'); |
| 237 | |
| 238 | -- A revoked recipient never qualifies, even when the rule is valid. |
| 239 | UPDATE public.alert_notification_consents |
| 240 | SET revoked_at = t-interval '1 minute' |
| 241 | WHERE user_id=v_other AND recipient_ref=v_recipient AND channel='BALE'; |
| 242 | |
| 243 | BEGIN |
| 244 | PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0, |
| 245 | crossed_state,event_id,'BALE',v_recipient,t+interval '1 minute'); |
| 246 | RAISE EXCEPTION 'revoked consent accepted'; |
| 247 | EXCEPTION WHEN insufficient_privilege THEN NULL; |
| 248 | END; |
| 249 | |
| 250 | -- External event keys must match state, trigger and source timestamp. |
| 251 | BEGIN |
| 252 | PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0, |
| 253 | crossed_state,'wrong-event-id','BALE',v_recipient,t+interval '1 minute'); |
| 254 | RAISE EXCEPTION 'mismatched event identity was accepted'; |
| 255 | EXCEPTION WHEN invalid_parameter_value THEN NULL; |
| 256 | END; |
| 257 | |
| 258 | -- Illegal direct jump into a NOTIFIED state is disallowed. |
| 259 | BEGIN |
| 260 | PERFORM * FROM public.alert_rule_commit(v_other,'threshold1',0, |
| 261 | jsonb_build_object('alert',jsonb_build_object('id','threshold1','phase','NOTIFIED')), |
| 262 | NULL,NULL,NULL,t+interval '1 minute'); |
| 263 | RAISE EXCEPTION 'unverified NOTIFIED state accepted'; |
| 264 | EXCEPTION WHEN invalid_parameter_value THEN NULL; |
| 265 | END; |
| 266 | |
| 267 | -- Public ACLs on both new tables/functions must be absent. |
| 268 | IF EXISTS ( |
| 269 | SELECT 1 FROM information_schema.role_table_grants |
| 270 | WHERE grantee='PUBLIC' AND table_name IN |
| 271 | ('alert_rule_runtime','alert_notification_consents') |
| 272 | ) THEN |
| 273 | RAISE EXCEPTION 'unsafe PUBLIC table grant'; |
| 274 | END IF; |
| 275 | |
| 276 | IF EXISTS ( |
| 277 | SELECT 1 |
| 278 | FROM pg_catalog.pg_proc AS p |
| 279 | JOIN pg_catalog.pg_namespace AS ns ON ns.oid = p.pronamespace |
| 280 | CROSS JOIN LATERAL pg_catalog.aclexplode( |
| 281 | COALESCE(p.proacl,pg_catalog.acldefault('f',p.proowner)) |
| 282 | ) AS acl |
| 283 | WHERE ns.nspname='public' AND p.proname IN |
| 284 | ('alert_rule_commit','alert_notification_claim_consented', |
| 285 | 'alert_notification_egress_allowed','alert_notification_begin_egress') |
| 286 | AND acl.grantee=0 AND acl.privilege_type='EXECUTE' |
| 287 | ) THEN |
| 288 | RAISE EXCEPTION 'unsafe PUBLIC execute grant'; |
| 289 | END IF; |
| 290 | END $$; |
| 291 | |
| 292 | ROLLBACK; |