Files
gstack/supabase/migrations/006_live_traffic_and_pulse_summary.sql
T
Garry Tanandcapy-ai[bot] 12cc21872d Telemetry backend only accepts writes through its own endpoint, and tests stop sending to it (GST-3234) (#4537)
The telemetry backend now takes writes only through its validated
endpoint, signed-in Supabase users can read or write nothing, and
gstack's tests can no longer send telemetry to the live project.

Why: a signed-in prober read two views on 2026-11-19, probes wrote about
2,000 junk rows through the open insert policy, and 41% of all skill-run
rows were test-suite traffic.

Nothing changes for users until the migrations and edge functions are
deployed; the deploy order is under Deploy.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
Review-Finding: p1/red-team#1 reader=red-team
Review-Finding: supabase/migrations/008_close_anon_writes.sql:9:published-number reader=core
Review-Finding: p2/outside-voice#3 reader=outside-voice
2026-11-19 20:31:31 +00:00

141 lines
6.2 KiB
PL/PgSQL

-- 006_live_traffic_and_pulse_summary.sql
-- Separates real use from test traffic and moves community-pulse's counting
-- into SQL.
--
-- Why: until v1.86.0.0 test/telemetry.test.ts posted to this project, and
-- older checkouts still do when their tests run; about 41% of skill_run rows
-- to 2026-11-19 carry test-fixture session ids (`a-1`, `inj-3`, ...). The
-- community-pulse function also counted only the first 1,000 rows of a week
-- and reported update-check rows as "weekly active".
--
-- What counts as live (telemetry_live_events):
-- * source is 'live' (rows from before this column default to 'live'),
-- * a numeric gstack version other than 0.0.0 (probes used 0.0.0-*),
-- * an event type the ingest function accepts, and
-- * for skill_run, a session id in the shape bin/gstack-skill-start mints
-- (<pid>-<epoch> or <pid>-<epoch>-<suffix>); for upgrade_prompted, the
-- shape bin/gstack-update-check mints (update-<pid>-<epoch>).
--
ALTER TABLE telemetry_events ADD COLUMN IF NOT EXISTS source TEXT NOT NULL DEFAULT 'live';
CREATE INDEX IF NOT EXISTS idx_telemetry_received_at ON telemetry_events (received_at);
CREATE OR REPLACE VIEW telemetry_live_events WITH (security_invoker = true) AS
SELECT *
FROM telemetry_events
WHERE source = 'live'
AND gstack_version ~ '^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?$'
AND gstack_version NOT LIKE '0.0.0%'
AND event_type IN ('skill_run', 'upgrade_prompted', 'upgrade_completed', 'onboarding',
'first_task_scaffold_shown', 'handoff', 'route', 'attack_attempt')
AND (
(event_type = 'skill_run' AND session_id ~ '^[0-9]+-[0-9]+(-[0-9A-Za-z]+)?$')
OR (event_type = 'upgrade_prompted' AND session_id ~ '^update-[0-9]+-[0-9]+$')
OR event_type NOT IN ('skill_run', 'upgrade_prompted')
);
REVOKE ALL ON telemetry_live_events FROM PUBLIC, anon, authenticated;
-- Same columns as 001's view; it now reads live rows only.
CREATE OR REPLACE VIEW crash_clusters WITH (security_invoker = true) AS
SELECT
error_class,
gstack_version,
COUNT(*) as total_occurrences,
COUNT(DISTINCT installation_id) as identified_users,
COUNT(*) - COUNT(installation_id) as anonymous_occurrences,
MIN(event_timestamp) as first_seen,
MAX(event_timestamp) as last_seen
FROM telemetry_live_events
WHERE outcome = 'error' AND error_class IS NOT NULL
GROUP BY error_class, gstack_version
ORDER BY total_occurrences DESC;
REVOKE ALL ON crash_clusters FROM PUBLIC, anon, authenticated;
-- One call returns everything community-pulse shows, counted over every live
-- row of the last 7 days (by server receive time). weekly_active is distinct
-- community-tier installs that ran a skill; anonymous-tier use has no id and
-- is not in it. Attack domains and layers need 5 distinct installs (k-anonymity).
CREATE OR REPLACE FUNCTION community_pulse_summary()
RETURNS jsonb
LANGUAGE sql
STABLE
SECURITY INVOKER
SET search_path = public
AS $$
WITH week AS (
SELECT * FROM telemetry_live_events WHERE received_at >= now() - interval '7 days'
),
prev AS (
SELECT * FROM telemetry_live_events
WHERE received_at >= now() - interval '14 days' AND received_at < now() - interval '7 days'
),
runs AS (
SELECT * FROM week WHERE event_type = 'skill_run' AND skill IS NOT NULL AND skill NOT LIKE '\_%'
),
active AS (
SELECT
(SELECT COUNT(DISTINCT installation_id) FROM runs) AS this_week,
(SELECT COUNT(DISTINCT installation_id) FROM prev
WHERE event_type = 'skill_run' AND skill IS NOT NULL AND skill NOT LIKE '\_%') AS last_week
),
attacks AS (
SELECT * FROM week WHERE event_type = 'attack_attempt'
)
SELECT jsonb_build_object(
'weekly_active', (SELECT this_week FROM active),
'change_pct', (SELECT CASE WHEN last_week > 0
THEN round(((this_week - last_week)::numeric / last_week) * 100)::int
ELSE 0 END FROM active),
'weekly_update_checks', (
SELECT COUNT(*) FROM update_checks
WHERE checked_at >= now() - interval '7 days'
AND gstack_version ~ '^[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?$'
AND gstack_version NOT LIKE '0.0.0%'
),
'top_skills', COALESCE((
SELECT jsonb_agg(jsonb_build_object('skill', skill, 'count', n) ORDER BY n DESC, skill)
FROM (SELECT skill, COUNT(*) AS n FROM runs GROUP BY skill ORDER BY n DESC, skill LIMIT 10) t
), '[]'::jsonb),
'crashes', COALESCE((
SELECT jsonb_agg(jsonb_build_object(
'error_class', error_class, 'gstack_version', gstack_version,
'total_occurrences', total_occurrences, 'identified_users', identified_users))
FROM (SELECT * FROM crash_clusters LIMIT 5) t
), '[]'::jsonb),
'versions', COALESCE((
SELECT jsonb_agg(jsonb_build_object('version', gstack_version, 'count', n) ORDER BY n DESC, gstack_version)
FROM (SELECT gstack_version, COUNT(*) AS n FROM runs GROUP BY gstack_version
ORDER BY n DESC, gstack_version LIMIT 5) t
), '[]'::jsonb),
'security', jsonb_build_object(
'attacks_last_7_days', (SELECT COUNT(*) FROM attacks),
'top_attack_domains', COALESCE((
SELECT jsonb_agg(jsonb_build_object('domain', security_url_domain, 'count', n) ORDER BY n DESC, security_url_domain)
FROM (SELECT security_url_domain, COUNT(*) AS n FROM attacks
WHERE security_url_domain IS NOT NULL
GROUP BY security_url_domain
HAVING COUNT(DISTINCT installation_id) >= 5
ORDER BY n DESC, security_url_domain LIMIT 10) t
), '[]'::jsonb),
'top_attack_layers', COALESCE((
SELECT jsonb_agg(jsonb_build_object('layer', security_layer, 'count', n) ORDER BY n DESC, security_layer)
FROM (SELECT security_layer, COUNT(*) AS n FROM attacks
WHERE security_layer IS NOT NULL
GROUP BY security_layer
HAVING COUNT(DISTINCT installation_id) >= 5) t
), '[]'::jsonb),
'verdict_distribution', COALESCE((
SELECT jsonb_agg(jsonb_build_object('verdict', security_verdict, 'count', n) ORDER BY n DESC, security_verdict)
FROM (SELECT security_verdict, COUNT(*) AS n FROM attacks
WHERE security_verdict IS NOT NULL GROUP BY security_verdict) t
), '[]'::jsonb)
)
);
$$;
REVOKE EXECUTE ON FUNCTION community_pulse_summary() FROM PUBLIC, anon, authenticated;
GRANT EXECUTE ON FUNCTION community_pulse_summary() TO service_role;