caelum0x e28bc91c91 fix: pool Insights event caps org-wide instead of per-site
OrgCollector (one Durable Object per org) was tracking each site's monthly
event count independently and comparing it to the org's full plan
events_limit on every site. Since every site of an org is seeded with the
SAME events_limit (apps/console/lib/sites.ts copies the org's single
entitlement onto each app), an org with N sites/apps could store up to N x
its plan's event quota by spreading traffic across sites -- free capacity
the plan never intended to grant, and inconsistent with the console's own
Insights dashboard, which already sums month_events across every app of the
org before comparing to the same events_limit (lib/insights-view.ts).

Track and cap usage per org-month (pooled across every site the org has)
instead of per-site-month. Updated collector-sim.test.mjs's load test, whose
old per-site-independent-limits setup no longer matches real org/site data
shape, and added a collector.test.mjs case that pins the pooled behavior
across two sites of one org. Docs (README.md, contract/INGEST-API.md)
updated to describe the pooled cap.

Verified: apps/insights-edge's full test suite (87 tests across
collector.test.mjs, collector-sim.test.mjs, replay, selfserve, SEO and
tracker-script suites) passes clean after the fix.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-24 18:14:38 +03:00

5.0 KiB

Railyard Insights edge collector

A Cloudflare Worker (RYP-D3) that takes page views and custom events from the /s.js page tag and hands them to Railyard Insights in batches.

  • POST /api/send and POST /api/batch (up to 100 events), in the same shape as the widely used open-source page-analytics payload, so existing tags work.
  • GET /s.js: the page tag. No cookies, no local storage, nothing sent when Do Not Track is on.
  • Site-key check against the SITES KV entries the console writes.
  • Bots, crawlers, link previews and prefetches are answered 202 and dropped.
  • The visitor id is a salted hash that changes every UTC day. The IP is used only to compute it, in memory; it is never stored, logged or sent on.
  • Plan caps per org and month, pooled across every site/app the org has (not a separate allowance per site — a 3-app org cannot store 3x its plan's events by spreading events across its 3 sites). Over the cap the answer is still 202, with x-railyard-capped: 1; those events are counted for usage but not stored. A paused org gets x-railyard-paused: 1.
  • One Durable Object per org buffers events and flushes every 2 seconds in one request, so each site sees at most one downstream request every 2 seconds.
  • Once a minute each org's counts go to the usage meter as insights_events ($0 until paid overage is switched on).
  • Page speed (QY1.2): the page tag sends each page view's LCP, INP and CLS (and FCP and TTFB) once, as a performance event, when the page is hidden. data-vitals="off" on the script tag turns it off. The tag stays under 3 KB gzip.
  • With PRODUCT_ANALYTICS_URL set, every stored event also goes to product analytics in its capture protocol (page views as $pageview, page speed as $web_vitals, a cookieless daily id for anonymous visitors), so the web view there reads the same store as funnels and replay.
  • Agent failures (2026-10-23): a page or SDK can send railyard.track("$agent_failure", { agent, model, error }). Only those three fields are kept (src/agent-failure.mjs); a prompt, an API key or any other field is dropped before anything is stored or forwarded, and the event is missing without an agent and an error. These rows sit in their own "Agent failures" table on the dashboard and the live demo, apart from events/converted. With FAULTLINE_DSN set (a GlitchTip project DSN), each one is also posted to Faultline as an issue tagged agent/model, fingerprinted on (agent, error) so repeats group together.

Live (2026-10-08): https://insights.ereboslab.com with a D1 event store (railyard-insights, binding DB; every stored event is written there at each flush, no IP), GET /api/stats?days=N and POST /api/report behind STATS_TOKEN, and a weekly funnel email (cron Mondays 07:00 UTC) to REPORT_TO through Scribase Mail (key mk_3e204ab960f7f073, from no-reply@mail.menivor.com). Sites are site:<key> entries in the SITES KV. inject/ is a pass-through Worker on zone routes that adds the page tag to HTML at the edge (scribase.com, mail.scribase.com, menivor.com); bidstage.app and yokharita.com carry the tag in their own layouts. Secrets live in ~/.config/products-keys/railyard-insights.env.

Self-serve (2026-10-12): GET / is a public landing page with a sign-up form (email + domain, works without JavaScript). POST /api/sites (JSON or form) creates a site:s_... entry (10,000 stored events a month, self_serve: true, KV list metadata so the weekly run skips it) and a private dashboard link /d/<token> (only the token's SHA-256 is stored, as dash:<hash>), mailed through Scribase Mail and shown at once. A self-serve site stores events only from its own domain and subdomains. GET /api/d/<token>/stats?days=N is the same data as JSON. POST /d/<token>/delete with confirm=<domain> removes the site, the link, the signups row and every stored event. Limits: 3 sign-ups per email and per domain a day, SIGNUP_DAILY_CAP in total. /privacy, /robots.txt, /sitemap.xml. Stats and the weekly report also list page views per utm_source; the owner report counts self-serve sites but never itemises them.

The downstream contract is contract/INGEST-API.md (and its JSON schema). Until Insights ingest is deployed, INGEST_URL is empty and events are counted and capped but not forwarded.

Tests

pnpm install --filter @railyard/insights-edge
node --test test/*.test.mjs        # workerd (local runtime) tests plus a 60 s simulated-clock run
node test/load.mjs 60 1000         # load run in the local runtime; results in LOAD-RESULT.md

Owner steps

  1. wrangler kv namespace create SITES and put the id in wrangler.jsonc.
  2. wrangler secret put VISITOR_SALT_SECRET (32+ random bytes), INGEST_TOKEN, RAGO_API_KEY.
  3. Deploy at the Wave E gate, after Insights ingest is up.
  4. Optional: wrangler secret put FAULTLINE_DSN with the real GlitchTip project DSN to turn on agent-failure forwarding; unset, the dashboard table still fills in, only the Faultline forward is skipped.