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 | /** |
| 2 | * Server-only notification egress coordinator. |
| 3 | * Consumes a DB-backed ONE-SEND permit, then rechecks consent and finally |
| 4 | * invokes the PR #33 Bale adapter through a trusted injected callback. |
| 5 | * No secrets persisted or logged; this module never self-schedules/retries. |
| 6 | * |
| 7 | * Depends on PR #34 outbox + migration 0006 begin_egress/egress_allowed. |
| 8 | * NOT PRODUCTION-AUTHORIZED: sender role and recipient ownership unverified. |
| 9 | */ |
| 10 | const uuid=/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
| 11 | const beginSql='SELECT public.alert_notification_begin_egress($1::uuid,$2::uuid,$3::uuid,$4::text,$5::timestamptz) AS permitted'; |
| 12 | const egressSql='SELECT public.alert_notification_egress_allowed($1::uuid,$2::uuid,$3::uuid,$4::text,$5::timestamptz) AS permitted'; |
| 13 | const finishSql='SELECT public.notification_outbox_finish($1::uuid,$2::uuid,$3::text,$4::text,$5::timestamptz,60) AS outcome'; |
| 14 | async function permitted(db,sql,params){ |
| 15 | const result=await db.query(sql,params); |
| 16 | if(!Array.isArray(result?.rows)||result.rows.length!==1|| |
| 17 | typeof result.rows[0].permitted!=='boolean') |
| 18 | throw new Error('UNTRUSTED_DB_AUTHORIZATION_RESPONSE'); |
| 19 | return result.rows[0].permitted; |
| 20 | } |
| 21 | async function finish(db,{outboxId,claimToken,decision,messageId,at}){ |
| 22 | const r=await db.query(finishSql,[outboxId,claimToken,decision,messageId,at]); |
| 23 | if(!Array.isArray(r?.rows)||r.rows.length!==1|| |
| 24 | typeof r.rows[0].outcome!=='string') |
| 25 | throw new Error('UNTRUSTED_DB_COMPLETION_RESPONSE'); |
| 26 | return r.rows[0].outcome; |
| 27 | } |
| 28 | export async function dispatchLeasedBale({ |
| 29 | db,baleOneMessage,outboxId,claimToken,recipientRef,resolveRecipient,token,text,at |
| 30 | }={}){ |
| 31 | if(typeof db?.query!=='function'||typeof baleOneMessage!=='function'|| |
| 32 | !uuid.test(outboxId)||!uuid.test(claimToken)||!uuid.test(recipientRef)|| |
| 33 | typeof resolveRecipient!=='function'|| |
| 34 | typeof at!=='string'||!Number.isFinite(Date.parse(at))) |
| 35 | throw new TypeError('invalid server dispatch prerequisites'); |
| 36 | if(typeof token!=='string'||token.length<12|| |
| 37 | typeof text!=='string'||text.length<1||text.length>1000) |
| 38 | throw new TypeError('invalid private dispatch data'); |
| 39 | // The destination must be resolved from a trusted server-side opaque |
| 40 | // recipient registry, NEVER taken from untrusted event/HTTP input. |
| 41 | const resolved=await resolveRecipient({recipientRef,channel:'BALE'}); |
| 42 | if(resolved?.verified!==true||resolved?.recipientRef!==recipientRef|| |
| 43 | typeof resolved.chatId!=='string'|| |
| 44 | !/^[1-9][0-9]{0,15}$/.test(resolved.chatId)) |
| 45 | return {status:'BLOCKED',reason:'RECIPIENT_MAPPING_UNVERIFIED',sent:false}; |
| 46 | const params=[outboxId,claimToken,recipientRef,'BALE',at]; |
| 47 | // Linearization point: this SQL row UPDATE makes a second caller unable |
| 48 | // to obtain another send permit. No network call occurs before success. |
| 49 | if(!await permitted(db,beginSql,params)) |
| 50 | return {status:'BLOCKED',reason:'SEND_PERMIT_NOT_AVAILABLE',sent:false}; |
| 51 | // Check opt-in immediately before network. A revoked recipient is blocked. |
| 52 | if(!await permitted(db,egressSql,params)){ |
| 53 | const state=await finish(db,{outboxId,claimToken,decision:'UNKNOWN', |
| 54 | messageId:null,at}); |
| 55 | return {status:'BLOCKED',reason:'RECIPIENT_NOT_CURRENTLY_AUTHORIZED', |
| 56 | persistedOutcome:state,sent:false}; |
| 57 | } |
| 58 | let outcome; |
| 59 | try{outcome=await baleOneMessage({token,chatId:resolved.chatId,text})} |
| 60 | catch{outcome={status:'UNKNOWN'}} |
| 61 | // Any ambiguous transport error is terminal UNKNOWN, not an auto retry. |
| 62 | const accepted=outcome?.status==='SENT'&& |
| 63 | typeof outcome.messageId==='string'&&outcome.messageId.length>0; |
| 64 | const persistedOutcome=await finish(db,{outboxId,claimToken, |
| 65 | decision:accepted?'SENT':'UNKNOWN', |
| 66 | messageId:accepted?outcome.messageId:null,at}); |
| 67 | if(accepted&&persistedOutcome==='SENT') |
| 68 | return {status:'PROVIDER_ACCEPTED',messageId:outcome.messageId, |
| 69 | persistedOutcome,sent:true,readConfirmed:false}; |
| 70 | return {status:'UNKNOWN',reason:'OUTBOUND_RESULT_UNVERIFIED_OR_NOT_PERSISTED', |
| 71 | persistedOutcome,sent:accepted,readConfirmed:false}; |
| 72 | } |