Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
fix(security): count the hourly reply limit per Instagram account across members
spacesdrive817e2a49b630
diff --git a/docs/architecture/data-flows/AUTOMATION.md b/docs/architecture/data-flows/AUTOMATION.mdindex 2dc07ff..8c04465 100644--- a/docs/architecture/data-flows/AUTOMATION.md+++ b/docs/architecture/data-flows/AUTOMATION.md@@ -252,7 +252,7 @@ Follow-ups and public replies use the same statuses, without `ignored`. Retry on|---|---|---|| Instance, per hour | 1,000 private replies | `AUTOMATION_GLOBAL_HOURLY_LIMIT` || User, per hour | 500 private replies | `AUTOMATION_USER_HOURLY_LIMIT` |-| Instagram account, per hour | 200 private replies (Instagram allows 750) | `AUTOMATION_ACCOUNT_HOURLY_LIMIT`, at most 750 |+| Instagram account, per hour | 200 private replies (Instagram allows 750), counted across every member's connection to the same Instagram account | `AUTOMATION_ACCOUNT_HOURLY_LIMIT`, at most 750 || Rule, per hour | none | the rule's hourly limit, 1 to 750 || Person, per day per account | 3 replies | `AUTOMATION_RECIPIENT_DAILY_LIMIT` || Instagram account, public replies per hour | 60 | `AUTOMATION_PUBLIC_ACCOUNT_HOURLY_LIMIT`, at most 200 |diff --git a/docs/architecture/database/SCHEMA.md b/docs/architecture/database/SCHEMA.mdindex 97ad193..09f05e1 100644--- a/docs/architecture/database/SCHEMA.md+++ b/docs/architecture/database/SCHEMA.md@@ -52,6 +52,7 @@ The first four files are baselines. Each recreates one table as it stood before| 20 | `alter_tracked_posts_youtube_only`, `drop_tracked_posts_account_id`, `alter_accounts_without_reddit`, `replace_accounts_identity_index` | The Reddit cleanup (#145) || 21 | `create_mcp_access_tokens`, `revoke_browser_access_mcp_access_tokens`, `add_mcp_access_tokens_updated_at_trigger`, `index_mcp_access_tokens_user_created`, the same four for `mcp_client_grants`, `alter_audit_events_subject_types` | The Claude (MCP) tables below (#146) || 22 | `replace_automation_rules_for_ig_user_function` | Comments route only to accounts that still hold an unexpired Instagram token (audit lead L2, #161) |+| 23 | `replace_reserve_automation_send_function` | The hourly account limit counts replies through every connection to the same Instagram account (audit lead L2, #161) |From order 10 on, every migration creates at most one entity, and the migration test enforces it. The files before it were applied to production first and stay as recorded.diff --git a/docs/philosophy/SECURITY.md b/docs/philosophy/SECURITY.mdindex 29f9252..e7c6b5c 100644--- a/docs/philosophy/SECURITY.md+++ b/docs/philosophy/SECURITY.md@@ -159,7 +159,4 @@ Risks found by a security review and left open on purpose, each with the reason| Risk | Why it stays | What would remove it ||---|---|---|| No automated secrets rotation | Rotation needs the owner in the Meta, Google and Supabase dashboards | A rotation runbook with a reminder, or provider APIs that rotate in place |-| With Supabase "Confirm email" off, someone could create a password account for another person's address before they sign up (audit L1) | Whether a later code or Google sign-in keeps that password is Supabase Auth behaviour, not visible in this code | The owner turns on email confirmation once the Send Email hook is set up, or turns off open sign-up for a private instance |-| An Instagram account connected by two Twiligent users routes comments to whichever user's rule wins, without checking that token still works (audit L2) | Needs two users who both connected the same account with working tokens | Route only to accounts whose last token check passed, and count send limits per Instagram identity |-| The shared YouTube API quota is counted per request, not per unit (audit L6) | The quota rule caps those routes at 30 per 10 minutes per user | The quota ledger that YouTube publishing adds |-| The daily token refresh has no subrequest budget, one user's due posts can use the shared publisher's run, and a subrequest overrun can open shared circuit breakers (audit L7 to L9) | Each needs many accounts or posts at once on the Free plan | Per-job subrequest budgets and a per-user cap per run, and not counting the Workers subrequest limit as a platform outage |+| With Supabase "Confirm email" off and open sign-up on, someone could create a password account for another person's address before they sign up (audit L1) | It is a Supabase setting, not code. The self-hosting guide now says to keep Confirm email on, and to close sign-up before ever turning it off. The project's own instance was checked on 2026-11-05: Confirm email is on and the Send Email hook is set | Nothing more in code; an instance owner who ignores the guide carries the risk |diff --git a/site/content/docs/self-host/supabase.mdx b/site/content/docs/self-host/supabase.mdxindex ac6d862..a4af0e2 100644--- a/site/content/docs/self-host/supabase.mdx+++ b/site/content/docs/self-host/supabase.mdx@@ -34,8 +34,9 @@ Open **SQL Editor**, then run each file from `supabase/migrations/` in file nameOpen **Authentication**, then **Sign In / Providers**, then **Email**.-- Turn off **Confirm email** unless you set up [email verification codes](/docs/self-host/email-codes). Otherwise members who create an account with a password wait for a confirmation code that Supabase's own email does not contain.-- To keep the instance private, turn off **Allow new users to sign up** after you and your team have created accounts. Google and emailed codes then only sign in existing members. You can also add members yourself under **Authentication**, **Users**, **Add user**.+- Keep **Confirm email** on and set up [email verification codes](/docs/self-host/email-codes), so a password account only works once its owner enters a code sent to the address.+- If you cannot set up email codes yet, members who create a password account would wait for a code Supabase's own email does not contain. Turn off **Allow new users to sign up** first and add members yourself under **Authentication**, **Users**, **Add user**; only then turn off **Confirm email**. Never leave both open: anyone could then create a password account for an address that is not theirs.+- To keep the instance private, turn off **Allow new users to sign up** after you and your team have created accounts. Google and emailed codes then only sign in existing members.The sign-in page's email field sends a 6-digit sign-in code. Without [email verification codes](/docs/self-host/email-codes), Supabase sends its own **Magic Link** email instead, which by default holds a link rather than a code; the link still signs the person in. To show the code as well, open **Authentication**, **Emails**, **Magic Link** and add `{{ .Token }}` to the template. Supabase's built-in email only sends a few messages an hour, so set up email verification codes, or have members use Google or a password.diff --git a/supabase/migrations/20261105032735_replace_reserve_automation_send_function.sql b/supabase/migrations/20261105032735_replace_reserve_automation_send_function.sqlnew file mode 100644index 0000000..1c621c8--- /dev/null+++ b/supabase/migrations/20261105032735_replace_reserve_automation_send_function.sql@@ -0,0 +1,102 @@+-- replaces reserve_automation_send so the hourly account limit counts every connection to the same Instagram account+create or replace function public.reserve_automation_send(+ p_id text,+ p_attempt_id text,+ p_global_limit integer,+ p_user_limit integer,+ p_account_limit integer,+ p_recipient_limit integer,+ p_message_digest text default null+)+returns table (outcome text, retry_at timestamptz)+language plpgsql+set search_path = ''+as $$+#variable_conflict use_column+declare+ job public.automation_comments%rowtype;+ controls public.automation_accounts%rowtype;+ rule_limit integer;+ hour_ago timestamptz := now() - interval '1 hour';+ free_at timestamptz;+begin+ -- one reservation at a time across every Worker, so two jobs cannot both take the last free send+ perform pg_advisory_xact_lock(hashtext('reserve_automation_send'));++ select * into job from public.automation_comments c where c.id = p_id and c.status = 'processing' for update;+ if not found then+ return query select 'gone'::text, null::timestamptz;+ return;+ end if;++ select * into controls from public.automation_accounts a where a.account_id = job.account_id;+ if controls.paused then+ return query select 'paused'::text, null::timestamptz;+ return;+ end if;+ if controls.cooldown_until > now() then+ return query select 'cooldown'::text, controls.cooldown_until;+ return;+ end if;++ -- a person who comments again and again gets a few replies a day, never a stream of them; retries of this+ -- comment are one reply, so only other comments count+ if job.from_id is not null and (+ select count(distinct t.automation_comment_id) from public.automation_attempts t+ where t.account_id = job.account_id and t.recipient_id = job.from_id and t.kind = 'private_reply'+ and t.started_at > now() - interval '1 day' and t.automation_comment_id <> job.id+ ) >= p_recipient_limit then+ return query select 'recipient_limit'::text, null::timestamptz;+ return;+ end if;++ -- when a scope is full, a send frees up an hour after the oldest one in the window+ select r.hourly_limit into rule_limit from public.automation_rules r where r.id = job.rule_id;+ if rule_limit is not null then+ select case when count(*) >= rule_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.rule_id = job.rule_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'rule_limit'::text, free_at;+ return;+ end if;+ end if;++ -- the account limit belongs to the Instagram account, so replies sent through every member's connection to it count+ select case when count(*) >= p_account_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t+ join public.automation_comments c on c.id = t.automation_comment_id+ where c.ig_user_id = job.ig_user_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ insert into public.automation_accounts (account_id, user_id, limited_until)+ values (job.account_id, job.user_id, free_at)+ on conflict (account_id) do update set limited_until = excluded.limited_until;+ return query select 'account_limit'::text, free_at;+ return;+ end if;++ select case when count(*) >= p_user_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.user_id = job.user_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'user_limit'::text, free_at;+ return;+ end if;++ select case when count(*) >= p_global_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'global_limit'::text, free_at;+ return;+ end if;++ update public.automation_comments c+ set status = 'sending', claimed_at = now(), attempts = c.attempts + 1+ where c.id = p_id;+ insert into public.automation_attempts (id, user_id, account_id, automation_comment_id, rule_id, recipient_id, message_digest)+ values (p_attempt_id, job.user_id, job.account_id, job.id, job.rule_id, job.from_id, p_message_digest);+ return query select 'ok'::text, null::timestamptz;+end;+$$;++-- functions in public are exposed through the Data API, so only the service role may call it+revoke execute on function public.reserve_automation_send(text, text, integer, integer, integer, integer, text) from public, anon, authenticated;+grant execute on function public.reserve_automation_send(text, text, integer, integer, integer, integer, text) to service_role;diff --git a/supabase/rollbacks/20261105032735_replace_reserve_automation_send_function.sql b/supabase/rollbacks/20261105032735_replace_reserve_automation_send_function.sqlnew file mode 100644index 0000000..9df3a7d--- /dev/null+++ b/supabase/rollbacks/20261105032735_replace_reserve_automation_send_function.sql@@ -0,0 +1,99 @@+-- puts back reserve_automation_send with the hourly account limit counted per connected account row+create or replace function public.reserve_automation_send(+ p_id text,+ p_attempt_id text,+ p_global_limit integer,+ p_user_limit integer,+ p_account_limit integer,+ p_recipient_limit integer,+ p_message_digest text default null+)+returns table (outcome text, retry_at timestamptz)+language plpgsql+set search_path = ''+as $$+#variable_conflict use_column+declare+ job public.automation_comments%rowtype;+ controls public.automation_accounts%rowtype;+ rule_limit integer;+ hour_ago timestamptz := now() - interval '1 hour';+ free_at timestamptz;+begin+ -- one reservation at a time across every Worker, so two jobs cannot both take the last free send+ perform pg_advisory_xact_lock(hashtext('reserve_automation_send'));++ select * into job from public.automation_comments c where c.id = p_id and c.status = 'processing' for update;+ if not found then+ return query select 'gone'::text, null::timestamptz;+ return;+ end if;++ select * into controls from public.automation_accounts a where a.account_id = job.account_id;+ if controls.paused then+ return query select 'paused'::text, null::timestamptz;+ return;+ end if;+ if controls.cooldown_until > now() then+ return query select 'cooldown'::text, controls.cooldown_until;+ return;+ end if;++ -- a person who comments again and again gets a few replies a day, never a stream of them; retries of this+ -- comment are one reply, so only other comments count+ if job.from_id is not null and (+ select count(distinct t.automation_comment_id) from public.automation_attempts t+ where t.account_id = job.account_id and t.recipient_id = job.from_id and t.kind = 'private_reply'+ and t.started_at > now() - interval '1 day' and t.automation_comment_id <> job.id+ ) >= p_recipient_limit then+ return query select 'recipient_limit'::text, null::timestamptz;+ return;+ end if;++ -- when a scope is full, a send frees up an hour after the oldest one in the window+ select r.hourly_limit into rule_limit from public.automation_rules r where r.id = job.rule_id;+ if rule_limit is not null then+ select case when count(*) >= rule_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.rule_id = job.rule_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'rule_limit'::text, free_at;+ return;+ end if;+ end if;++ select case when count(*) >= p_account_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.account_id = job.account_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ insert into public.automation_accounts (account_id, user_id, limited_until)+ values (job.account_id, job.user_id, free_at)+ on conflict (account_id) do update set limited_until = excluded.limited_until;+ return query select 'account_limit'::text, free_at;+ return;+ end if;++ select case when count(*) >= p_user_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.user_id = job.user_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'user_limit'::text, free_at;+ return;+ end if;++ select case when count(*) >= p_global_limit then min(t.started_at) + interval '1 hour' end into free_at+ from public.automation_attempts t where t.kind = 'private_reply' and t.started_at > hour_ago;+ if free_at is not null then+ return query select 'global_limit'::text, free_at;+ return;+ end if;++ update public.automation_comments c+ set status = 'sending', claimed_at = now(), attempts = c.attempts + 1+ where c.id = p_id;+ insert into public.automation_attempts (id, user_id, account_id, automation_comment_id, rule_id, recipient_id, message_digest)+ values (p_attempt_id, job.user_id, job.account_id, job.id, job.rule_id, job.from_id, p_message_digest);+ return query select 'ok'::text, null::timestamptz;+end;+$$;++-- functions in public are exposed through the Data API, so only the service role may call it+revoke execute on function public.reserve_automation_send(text, text, integer, integer, integer, integer, text) from public, anon, authenticated;+grant execute on function public.reserve_automation_send(text, text, integer, integer, integer, integer, text) to service_role;diff --git a/supabase/schemas/public/functions/reserve_automation_send.sql b/supabase/schemas/public/functions/reserve_automation_send.sqlindex 5676a79..3423105 100644--- a/supabase/schemas/public/functions/reserve_automation_send.sql+++ b/supabase/schemas/public/functions/reserve_automation_send.sql@@ -63,8 +63,11 @@ beginend if;end if;+ -- the account limit belongs to the Instagram account, so replies sent through every member's connection to it countselect case when count(*) >= p_account_limit then min(t.started_at) + interval '1 hour' end into free_at- from public.automation_attempts t where t.account_id = job.account_id and t.kind = 'private_reply' and t.started_at > hour_ago;+ from public.automation_attempts t+ join public.automation_comments c on c.id = t.automation_comment_id+ where c.ig_user_id = job.ig_user_id and t.kind = 'private_reply' and t.started_at > hour_ago;if free_at is not null theninsert into public.automation_accounts (account_id, user_id, limited_until)values (job.account_id, job.user_id, free_at)