Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Next.js App Router in JavaScript, Tailwind CSS with shadcn/ui, Cloudflare D1 thr
- **Actions**: every customer action is one `defineAction` in `lib/actions/<group>.js` (schemas, permission, handler). `runAction` is the only way to run one: from the dashboard's server actions, the REST API (`app/api/v1`) and MCP (`/mcp`, `lib/mcp/server.js`). The docs, the OpenAPI document and the llms files are generated from the same schemas, and `tests/parity.test.mjs` fails when an action is missing from any of them.
- **Accounts**: `scoped(account, filter)` (`lib/account-scope.js`) adds the account to every query, and `lib/docdb/model.js` refuses customer queries without one. `requireAccount()` for pages and server actions, `authenticateRequest()` for the API and MCP (`lib/auth.js`, `lib/api-keys.js`).
- **MCP and OAuth**: API keys, or OAuth access tokens from Better Auth's OAuth provider (`lib/auth.js`, `lib/oauth.js`, `lib/oauth-rules.js`), with the consent page at `app/oauth/consent`. A rotation of `BETTER_AUTH_SECRET` keeps the old value in `BETTER_AUTH_SECRET_PREVIOUS` (`docs/AUTH-ROTATION.md`, `scripts/rotate-auth-secret.sh`): anything that checks a signature made from the secret tries `getAppSecrets()` (`lib/app-keys.js` for links), never signs with a previous one, and the OAuth signing key is kept readable by `lib/auth-keyring.js`. A new token or link kind uses `lib/app-keys.js`, not its own `hkdfSync` of `getAppSecret()`. `scripts/check-mcp-auth.mjs` is the post-deploy check.
- **Agent safety**: a key or an OAuth grant has a level (`read_only`, `drafts`, `send_with_approval`, `full`, `signups`) that allows some scope classes, and every action has a class in `lib/action-scopes.js` (the one table: a test fails for an action without one, or without a newsletter target). `runAction` sends keys and apps through `lib/agent-guard.js`: level, the key's one newsletter, daily cap, then either the action or an approval request (`lib/approvals.js`, `ApprovalRequest`, answered `202`/`approval_required`) when its class is send or destructive for a send with approval key or a newsletter that asks (`Newsletter.agentApproval`). Deciding an approval and that setting are `dashboardOnly` actions: in the registry, but not in `ACTIONS`, so no route, tool or docs page. Calls that change something or read contacts go in `AgentActivity` (ids and counts, never bodies), the cron purges it (`lib/agent-housekeeping.js`). A new action needs its class in `lib/action-scopes.js`.
- **Agent safety**: a key or an OAuth grant has a level (`read_only`, `drafts`, `send_with_approval`, `full`, `signups`) that allows some scope classes, and every action has a class in `lib/action-scopes.js` (the one table: a test fails for an action without one, or without a newsletter target). `runAction` sends keys and apps through `lib/agent-guard.js`: level, the key's one newsletter, daily cap, then either the action or an approval request (`lib/approvals.js`, `ApprovalRequest`, answered `202`/`approval_required`) when its class is send or destructive for a send with approval key or a newsletter that asks (`Newsletter.agentApproval`, on from the start for accounts with `agentApprovalByDefault`, and also holding billing changes). A key's `allowedActions` and `allowedIps` (`lib/ip-allowlist.js`) and an app's one newsletter (from the consent page) are checked there too. Deciding an approval (by the owner or a team approver, `lib/team.js` `getApproverNewsletters`, optionally with a code by email) and those settings are `dashboardOnly` actions: in the registry, but not in `ACTIONS`, so no route, tool or docs page. Calls that change something or read contacts go in `AgentActivity` (ids and counts, never bodies), with team members' dashboard changes (`lib/actions/run-action.js`), and the cron purges it by each account's retention (`lib/agent-housekeeping.js`). A new action needs its class in `lib/action-scopes.js`.
- **Jobs**: QStash calls `app/api/jobs/*`: sending batches, scheduled sends, first-send checks, the purge of deleted accounts, the steps of an account export (`lib/account-export.js`: each step publishes the next, and the files wait in the private `EXPORTS` bucket for a signed download), the steps of taking a deleted tag or custom field off a large list (`lib/bulk-removal.js`, `remove-from-contacts`), the steps of a post import (`lib/post-import.js`: Substack, beehiiv, WordPress or Ghost posts become sent issues, the export waiting in `EXPORTS` under `imports/`, and their old addresses become redirects that `proxy.js` serves through `lib/redirect-routing.js`), the steps of an audio version (`lib/audio.js`, `make-audio`: one part of an issue read per step, the parts waiting in `EXPORTS` under `audio/`), the steps of saving a large segment as a list (`lib/segment-snapshots.js`, `snapshot-segment`) and `run-automations`, which a QStash schedule calls every 5 minutes (`lib/automations.js`: automation steps are a tree in `lib/automation-rules.js`, and an enrolment points at a step by its stable ID; segment conditions are one engine, `lib/segment-rules.js` and `lib/segments.js`, for segments, campaign audiences and automation branches). `run-series` is the same for recurring series (`lib/series.js`): the active ones that are due read their feed (`lib/feed-fetch.js`, the only fetch of a customer's address besides webhooks and the subfolder check, guarded by `lib/feed-url.js`) and make a draft or a send of its new items; an RSS block in an issue is read once as the send starts (`lib/feed-resolve.js`, called from `queueCampaign`). A cron calls `app/api/cron/hourly` with `CRON_SECRET`: PostShiba's team status, the extra emails Polar hasn't been told about (`lib/extra-emails.js`) dedicated IPs out of line with their add-on (`lib/dedicated-ip.js`), the day of every dedicated IP's warm-up (`lib/ip-warmup.js`) and the stock of spare IPs (`lib/ip-inventory.js`). Another calls `app/api/cron/postmaster` every day: Gmail's spam rates from Google Postmaster Tools (`lib/postmaster.js`), connected on `/admin`.
- **Email providers**: `lib/email-provider` switches between `ses.js` and `postshiba.js` (tenants, domains, sends), and their events become one shape (`lib/email-event-rules.js`). FlockStack's own emails (sign-in codes, alerts) go through `lib/platform-email.js`: Resend, or the provider.
- **Webhooks**: outgoing events leave only through `emitWebhookEvent` (`lib/webhooks.js`), and every type is in `lib/webhook-events.js` with its example, which the actions, docs, dashboard and tests read. Each delivery attempt is a QStash job (`app/api/jobs/deliver-webhook`) that signs the body Standard Webhooks style (`lib/webhook-signing.js`) and publishes its own retry; addresses pass `lib/webhook-url.js` at creation and before every attempt. The delivery log keeps the latest deliveries per endpoint only. `integrations/` holds the Zapier and Make apps, separate packages that call the REST API and aren't part of the build.
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ FlockStack is a newsletter app: write an issue, send it to your list, publish it
## Features

- **AI agents and the dashboard.** Everything the dashboard does also works from an AI agent through the MCP server, and from code through the REST API, with the same checks and limits. ChatGPT, Claude and Grok Bot connect with OAuth; Claude Code, Codex and Cursor with an API key. MCP marks sending as an action that can't be undone, so agents ask you first.
- **Built for agents you can trust.** Agents propose, you approve: a key or connected app can be read only, drafts only, send with approval or full access, limited to one newsletter, capped at a number of calls a day and set to expire. With send with approval, the sends, schedules and deletes an agent asks for wait on an Approvals page with the exact input and who asked, and run once, as that key, when you approve (an agent can never approve its own request). Everything an agent changes is in an Agent activity log (ids and counts only, kept 90 days). See [Agent safety](#agent-safety).
- **Built for agents you can trust.** Agents propose, you approve: a key or connected app can be read only, drafts only, send with approval or full access, limited to one newsletter, capped at a number of calls a day and set to expire. With send with approval, or on a newsletter that asks (the default for new accounts), the sends, schedules, deletes and billing changes an agent asks for wait on an Approvals page with the exact input and who asked, and run once, as that key, when you approve (an agent can never approve its own request). Team members you name can approve too, and approving can take a code by email. A key can also be limited to some actions and some IP addresses. Everything an agent changes is in an Agent activity log you can filter by client and export (ids and counts only, kept 90 days unless you choose 30, 180 or 365). See [Agent safety](#agent-safety).
- **Write, review, send.** Write in an editor with slash commands, images and boxes, or send Markdown or a complete HTML email through the API and MCP. Review shows what to fix before sending: subject, preview text, unsubscribe link, Gmail's 100 KB clip, postal address, sender. Then send it, schedule it, or start with your most engaged readers and send to more later. Send by email and web, email only or web only, under the issue's own sender name, with test emails to up to 10 people.
- **Undo send.** After you press Send in the dashboard, the send waits 30 seconds (off, 10, 30 or 60 per newsletter, in its settings). A bar at the bottom of every dashboard page counts down, "Sending to 4,812 readers in 28 s", with Undo, which gives back the draft exactly as it was, and Send now. Nothing is queued, counted against your plan or emailed until the wait is over, the bar comes back on a reload and shows in a second tab, and sends from the API and AI agents (which have their own approval) go out at once. The plan's limits are checked when you press Send and again when it goes out.
- **A ready-to-send check.** The review step lists what to fix and what to look at before you send, each with one plain sentence and a link to the fix: the subject (blocked when empty, flagged past about 60 characters or when it shouts), preview text, the sender address and domain, the postal address, the unsubscribe link and Gmail's 100 KB clipping, the first 20 links (opened from the server by a short job, with the same limits as a webhook: public hosts only, three redirects at most), pictures with no alt text or over 1 MB, merge tags some contacts have no value for (how many, and what they see instead), who it goes to, the plan's limits and the Free plan's footer. A blocker keeps Send off; a warning never does. "Send a test to me" and phone and desktop previews, light and dark, sit beside it. `check_campaign` returns the same list to the REST API and MCP, so an agent can look before it asks to send, and the approval page shows it under an agent's request.
Expand Down Expand Up @@ -653,9 +653,9 @@ ChatGPT, Claude and Grok Bot add `https://<your-app>/mcp` as a connector or cust

### Agent safety

Every action has a scope class (read, write-draft, write, send, destructive, admin) in `lib/action-scopes.js`, and a key's or app's level allows some of them: read only, drafts only (campaigns, templates, segments and automations as drafts, and the content plan), send with approval (all but keys, billing and money; its send and destructive classes wait for the owner) and full. `tests/agent-scopes.test.mjs` fails when an action has no class, and checks each level over the whole registry. A key can be limited to one newsletter, capped at calls a day and given an expiry (`create_api_key`). Keys and apps from before levels keep their meaning.
Every action has a scope class (read, write-draft, write, send, destructive, admin) in `lib/action-scopes.js`, and a key's or app's level allows some of them: read only, drafts only (campaigns, templates, segments and automations as drafts, and the content plan), send with approval (all but keys, billing and money; its send and destructive classes wait for the owner) and full. `tests/agent-scopes.test.mjs` fails when an action has no class, and checks each level over the whole registry. A key can be limited to one newsletter, capped at calls a day, given an expiry, limited to a list of actions and to IP addresses (`create_api_key`); a connected app can be limited to one newsletter on the consent page. Keys and apps from before levels keep their meaning.

An action that waits for approval answers `202` with `status: "approval_required"` (REST) or a tool result with the same fields (MCP) and is stored in `ApprovalRequest` (`lib/agent-guard.js`, `lib/approvals.js`): the owner approves or rejects it on **Approvals** (`/approvals`), approving runs it once as the key that asked, requests expire after 24 hours, and `get_approval_status` and the `approval.decided` webhook tell the agent. "Ask me before an agent sends" is a per-newsletter switch on the same page that makes even a full access key wait. Deciding is a registry action marked `dashboardOnly`, so no key, app or tool can do it. **Agent activity** (`/agent-activity`, `list_agent_activity`) logs what keys and apps changed or read of contacts, with ids and counts only, for 90 days; the hourly cron purges it. The docs page is `/docs/agent-safety`.
An action that waits for approval answers `202` with `status: "approval_required"` (REST) or a tool result with the same fields (MCP) and is stored in `ApprovalRequest` (`lib/agent-guard.js`, `lib/approvals.js`): the owner approves or rejects it on **Approvals** (`/approvals`), approving runs it once as the key that asked, requests expire after 24 hours, and `get_approval_status` and the `approval.decided` webhook tell the agent. "Ask me before an agent sends" is a per-newsletter switch on the same page that makes even a full access key wait, billing changes included; accounts made since it became the default (`Account.agentApprovalByDefault`) start every newsletter with it on. The owner can name team approvers per newsletter and ask for a code by email to approve sends and deletes. Deciding and these settings are registry actions marked `dashboardOnly`, so no key, app or tool can do them. **Agent activity** (`/agent-activity`, `list_agent_activity`, `export_agent_activity`) logs what keys and apps changed or read of contacts, team members' dashboard changes and the owner's changes to these settings, with ids and counts only, for 90 days or the owner's choice; the hourly cron purges it. The docs page is `/docs/agent-safety`.

## Contributing

Expand Down
Loading
Loading