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 | # ChartNova atomic notification outbox — isolated schema review |
| 2 | |
| 3 | **Status:** draft PR; schema, functions and roll-backed test fixtures only. **No Neon schema has been modified.** No Cloudflare Worker version, user data, market price, bot token, outgoing message or live-alert state has changed. |
| 4 | |
| 5 | ## Why this is an independent PR |
| 6 | |
| 7 | PR [#33](https://github.com/davoodmehraban89/VELYQOR/pull/33) implements pure informational alert and notification state transitions but explicitly leaves the multi-Worker **transactional, durable outbox** incomplete. The existing PostgreSQL foundation on main: |
| 8 | - [0001_market_core.sql](../../db/migrations/0001_market_core.sql) — source prices, instruments and providers |
| 9 | - [0002_workspaces.sql](../../db/migrations/0002_workspaces.sql) — \`app_users\` |
| 10 | - [0003_phase1_persistence_hardening.sql](../../db/migrations/0003_phase1_persistence_hardening.sql) — provenance and price uniqueness. |
| 11 | |
| 12 | This PR adds only a **new, separately versioned** [0004_notification_outbox.sql](../../db/migrations/0004_notification_outbox.sql) and [SQL invariants](../../db/tests/notification_outbox_invariants.sql), starting independently from main. The untracked/local **0005_navasan_private_daily_ohlc.sql is untouched and must remain blocked.** |
| 13 | |
| 14 | ## Design contracts and security boundaries |
| 15 | |
| 16 | - All outbound notifications are **information only**. This schema contains neither a broker order endpoint nor a trade-execution trigger. |
| 17 | - \`notification_outbox\` links \`app_users\` to an opaque \`recipient_ref UUID\`; no Bale chat IDs, email addresses, notification text, bot tokens, API keys or sensitive upstream payloads are stored. |
| 18 | - \`UNIQUE(user_id,event_key,channel,recipient_ref)\` ensures duplicate logical enqueues do not produce multiple messages for the same user and channel. Event-key generation must be versioned by the future alert-engine integrator. |
| 19 | - \`notification_outbox_enqueue\` inserts idempotently with \`ON CONFLICT DO NOTHING\` and resolves the existing ID without overwriting unrelated state. |
| 20 | - \`notification_outbox_claim\` uses one database transaction, \`SELECT ... FOR UPDATE SKIP LOCKED\` and an atomic UPDATE to acquire at most one available item per claim. A random lease token and bounded lease duration protect against unrelated workers completing each other's claims. |
| 21 | - **Lease expiry is terminal \`UNKNOWN\`**, not an automatic re-send: the previous worker might have delivered a message and crashed before storing the result. |
| 22 | - \`notification_outbox_finish\` accepts only **SENT with a provider message ID**, **REJECTED** with explicit provider non-delivery or **UNKNOWN** for timeout/ambiguous responses. A verified rejected message can be retried after a configured delay, at most \`max_attempts\` times. An unknown response **cannot be auto-retried**. |
| 23 | - Provider acceptance is **not read confirmation**. \`notification_outbox_acknowledge\` requires a user ID match, a previously accepted message, and a later timestamp; the caller **must separately authenticate the user** and obtain actual affirmative acknowledgement. |
| 24 | - SQL functions use \`SECURITY INVOKER\` and \`REVOKE ... FROM PUBLIC\`. **No runtime service DB role grants are created**. The server-side authorization boundary remains **blocked until an explicit role, RLS/grants and actual user identity mapping are reviewed**. |
| 25 | - There is **no user-facing Worker path**, persistent signed-in workflow or bot send call in this PR. No API changed. |
| 26 | |
| 27 | ## Concurrency and delivery trade-off |
| 28 | |
| 29 | This protocol favors **at-most-one attempted send per ambiguous claim** over automatic liveness. This is not the same as exactly-once delivery. In particular, failure after claiming but before sending also causes \`UNKNOWN\`, requiring explicit human reconciliation. It is safer than duplicate notifications but can lead to missed notifications and is **not acceptable as a guaranteed real-time trading alert**. |
| 30 | |
| 31 | Persistent row authorization must be enforced by an approved server-only Neon role, plus user-scoped queries. A generic front-end database connection must **not** receive permission to enqueue/claim/finish other users' alerts. No RLS permissions or broad runtime grants are assumed here. This outbox must not be activated until there is a verified recipient resolution store, written market-data redisplay rights and separately approved sender integration. |
| 32 | |
| 33 | ## Test evidence and acceptance |
| 34 | |
| 35 | - \`db/tests/notification_outbox_invariants.sql\` runs inside \`BEGIN/ROLLBACK\` against a **fresh GitHub Actions PostgreSQL service**. It tests unique enqueue, invalid metadata, claim exclusion, wrong lease token, completion receipt, no duplicate completion, cross-user ACK, early ACK, retry-delay and attempt caps, ambiguous result, expired lease, late worker response, unauthorized SQL state edits, and user isolation. |
| 36 | - The existing [CI workflow](../../.github/workflows/ci.yml) already applies \`db/migrations/*.sql\` and runs \`db/tests/*.sql\` in an ephemeral PostgreSQL 18 database. No database connection string or account-level secrets are used. |
| 37 | - **Two actual concurrent PostgreSQL client sessions passed** in [GitHub Actions run 39843468112](https://github.com/davoodmehraban89/VELYQOR/actions/runs/39843468112), job **120050433647**, using [scripts/check-outbox-concurrency.mjs](../../scripts/check-outbox-concurrency.mjs). The log reported `OUTBOX_CROSS_SESSION_VERIFIED`: two independent enqueues returned the same event ID, exactly one database row was stored, a second Worker-style connection skipped the locked row, no second claim followed commit, and attempt count remained 1. The test **refuses to run outside `CI=true`, `velyqor_test`, `localhost`**. This is stronger than the single-connection invariant suite but **NOT** a high-volume/live-Neon proof or guarantee of recipient delivery. |
| 38 | - Only the CI outcome on the **exact final head SHA** should be declared as final. Further production safety checks require actual tenant authorization, source licensing, an independent reviewer and measured failover; no live-data testing was conducted. |
| 39 | - Main workflow is unchanged, so the migration remains isolated in this PR until authorized. |
| 40 | |
| 41 | ## Deployment, rollback and incomplete items |
| 42 | |
| 43 | **Deployment is prohibited at this point** without a separate owner's authorization and a reviewed rollback plan. If eventually applied to a test branch, validate schema and privilege inventory without reading private records. Before any live release, explicitly approve a separate Neon schema migration and safe rollback for queued events; do not drop active notification data. |
| 44 | |
| 45 | For the current Draft PR, rollback simply means closing the unmerged branch. No live database change exists, hence no data deletion is necessary. |
| 46 | |
| 47 | Still incomplete / **تأییدنشده**: live database schema inspection, approved runtime DB roles/permissions, multi-worker stress test, recipient binding and storage, exact event payload/channel semantics, data-provider publication rights, real user identity, actual Bale delivery, SLO, full live Worker integration, iOS browser outage root cause, and three truly independent reviewer approvals. **The schema may pass CI and yet not be production-ready.** |