Repository navigation
FE-1832: Send an email from a flow - #10693
kostandinang wants to merge 9 commits into
Conversation
The SES, SMTP and dummy transporters move out of hash-api so that a worker can send email. `sendMail` takes an optional sender, Reply-to and plain-text body, returns the message ID, and throws an `EmailSendError` that says whether the email was rejected, wasn't sent for now, or may have been sent. The API keeps its behavior through its own transporter type, which logs a failed send and resolves. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-01a23729-12d1-7170-9bcb-f973b88c359d
Sends one plain-text email to one address from the AI worker, and returns the provider's message ID. Replies go to the address given, or to the person who started the run. Outside production, only hash.ai addresses can be emailed. SES can't tell a repeated send from a new email, so the step sends at most once: only an attempt known not to have sent the email is retried, and a later attempt refuses to send unless the one before it said so. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-01a23729-12d1-7170-9bcb-f973b88c359d
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #10693 +/- ##
==========================================
+ Coverage 70.40% 70.41% +0.01%
==========================================
Files 2126 2133 +7
Lines 225388 225600 +212
Branches 9784 9854 +70
==========================================
+ Hits 158674 158856 +182
- Misses 64671 64682 +11
- Partials 2043 2062 +19
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🔵 Needs a closer look
Irreversible live sending requires maintainer agreement on retry and scheduled Reply-to behavior, plus verification of staging SES configuration, permissions, and real delivery.
No open issues 🎉
What changed in this PR
Adds a Send email flow action for internal staging use, sharing email infrastructure with the API while preserving existing API behavior.
Changes:
- Adds address validation, Reply-to defaults, message ID output, and retry safeguards.
- Moves SES, SMTP, and dummy transporters into shared backend utilities.
- Enables the editor action and adds documentation, unit tests, and flow scenarios.
| File | Description |
|---|---|
yarn.lock |
Updates dependency ownership. |
tests/hash-backend-integration/src/tests/email.test.ts |
Uses the shared SES transporter. |
README.md |
Documents worker email configuration. |
libs/@local/hash-isomorphic-utils/src/flows/validate-flow-definition.test.ts |
Tests email input diagnostics. |
libs/@local/hash-isomorphic-utils/src/flows/email-address.ts |
Adds shared address validation. |
libs/@local/hash-isomorphic-utils/src/flows/email-address.test.ts |
Tests accepted and rejected addresses. |
libs/@local/hash-isomorphic-utils/src/flows/action-definitions.ts |
Defines Send email inputs and output. |
libs/@local/hash-backend-utils/src/flows/signal-step-attempts.ts |
Exposes previous-attempt failure details. |
libs/@local/hash-backend-utils/src/email/smtp-email-transporter.ts |
Adds shared SMTP delivery and failure classification. |
libs/@local/hash-backend-utils/src/email/smtp-email-transporter.test.ts |
Tests SMTP delivery outcomes. |
libs/@local/hash-backend-utils/src/email/shared/email-transporter.ts |
Defines shared email contracts. |
libs/@local/hash-backend-utils/src/email/dummy-email-transporter.ts |
Moves dummy delivery and adds message IDs. |
libs/@local/hash-backend-utils/src/email/aws-ses-email-transporter.ts |
Adds shared SES delivery and single-attempt configuration. |
libs/@local/hash-backend-utils/src/email/aws-ses-email-transporter.test.ts |
Tests SES content, outcomes, and retries. |
libs/@local/hash-backend-utils/src/email.ts |
Creates and exports shared transporters. |
libs/@local/hash-backend-utils/package.json |
Adds email dependencies. |
apps/hash-frontend/src/pages/@/[shortname]/flows/[flow-def-id]/edit.page/shared/step-display.ts |
Enables Send email in the editor. |
apps/hash-frontend/src/pages/@/[shortname]/flows/[flow-def-id]/edit.page/shared/step-display.test.ts |
Updates upcoming-action expectations. |
apps/hash-api/src/graphql/resolvers/knowledge/org/invite-user-to-org.ts |
Uses the API wrapper type. |
apps/hash-api/src/graphql/create-apollo-server.ts |
Updates the transporter type import. |
apps/hash-api/src/graphql/context.ts |
Updates the transporter type import. |
apps/hash-api/src/graph/context-types.ts |
Updates the transporter type import. |
apps/hash-api/src/email/transporters/types.ts |
Removes superseded transporter types. |
apps/hash-api/src/email/transporters/smtp-email-transporter.ts |
Removes the API-local SMTP implementation. |
apps/hash-api/src/email/transporters/index.ts |
Removes obsolete transporter exports. |
apps/hash-api/src/email/transporters/aws-ses-email-transporter.ts |
Removes the API-local SES implementation. |
apps/hash-api/src/email/create-email-transporter.ts |
Wraps shared delivery with existing API error handling. |
apps/hash-api/package.json |
Removes relocated email dependencies. |
apps/hash-ai-worker-ts/src/activities/shared/get-flow-context.ts |
Exposes the run’s trigger identity. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action/get-user-email.ts |
Reads the starter’s email as that user. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action/get-email-transporter.ts |
Configures worker delivery and timeouts. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action/get-earlier-attempt.ts |
Guards retries after uncertain delivery. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action/get-earlier-attempt.test.ts |
Tests retry eligibility. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action.ts |
Implements validation, sending, and delivery outcomes. |
apps/hash-ai-worker-ts/src/activities/flow-activities/send-email-action.test.ts |
Tests action behavior and safeguards. |
apps/hash-ai-worker-ts/src/activities/flow-activities.ts |
Registers the Send email activity. |
apps/hash-ai-worker-ts/scripts/flow-scenarios/scenarios/send-email.scenario.ts |
Adds composed, drafted, and restricted-recipient scenarios. |
Review effort: Balanced
Shorten comments that repeated the retry policy or restated a name, and remove test cases that took the same branch as another case or tested the AWS SDK rather than this code. Co-authored-by: Amp <amp@ampcode.com>
A later attempt of the step read how the attempt before it failed from the run, to decide whether it could send. Only the first attempt sends now, and a send that didn't reach the provider is tried again within it. The step no longer reads the run from Temporal, and `signal-step-attempts.ts` is as it was. Co-authored-by: Amp <amp@ampcode.com>
…rror Not every email client keeps line breaks for `white-space`, so the HTML part of a flow's email uses `<br>`, and escapes the body with lodash rather than by hand. The API logs the provider's own error for a failed send again, with what became of the email. Add tests for the API's transporter not rejecting and for a deployment set to the dummy transporter, and mark the test credentials of the SES stand-in as a Semgrep false positive. Co-authored-by: Amp <amp@ampcode.com>
| ); | ||
| } | ||
|
|
||
| if (Context.current().info.attempt > 1) { |
The AI worker sent every flow email from `ops@hash.ai`. A deployment that can't send from that address can now name its own in `HASH_FLOW_EMAIL_SENDER`. Without it the sender is unchanged. Co-authored-by: Amp <amp@ampcode.com>
Semgrep still lists a finding marked `nosemgrep` in the report CI uploads, and code scanning opens an alert for it, so the check failed on the placeholder secret of the SES stand-in. Setting the variables as one object doesn't match the rule. Co-authored-by: Amp <amp@ampcode.com>
The AI worker no longer has a sender of its own in the code. With the `aws` or `smtp` transporter and no `HASH_FLOW_EMAIL_SENDER`, a Send email step fails as not set up rather than send from the API's system address. The `dummy` transporter keeps its own sender, and the transporter tests use `example.com` senders. Co-authored-by: Amp <amp@ampcode.com>
A Send email step looked up who started the run before it knew whether the worker had an email transporter. It now gets the transporter first, and incomplete email settings fail the step as not set up instead of as an email that wasn't sent. The step's description no longer says the email is from HASH, as the sender is the deployment's, and the README lists `HASH_FLOW_EMAIL_SENDER` with the other email variables. Co-authored-by: Amp <amp@ampcode.com>
Benchmark results
|
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| resolve_policies_for_actor | user: empty, selectivity: high, policies: 2002 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: medium, policies: 1002 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: high, policies: 3314 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: medium, policies: 1527 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: high, policies: 2078 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: medium, policies: 1033 | Flame Graph |
policy_resolution_medium
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| resolve_policies_for_actor | user: empty, selectivity: high, policies: 102 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: medium, policies: 52 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: high, policies: 269 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: medium, policies: 108 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: high, policies: 133 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: medium, policies: 63 | Flame Graph |
policy_resolution_none
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| resolve_policies_for_actor | user: empty, selectivity: high, policies: 2 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: medium, policies: 2 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: high, policies: 8 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: medium, policies: 3 | Flame Graph |
policy_resolution_small
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| resolve_policies_for_actor | user: empty, selectivity: high, policies: 52 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: empty, selectivity: medium, policies: 26 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: high, policies: 94 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: seeded, selectivity: medium, policies: 27 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: high, policies: 66 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: low, policies: 1 | Flame Graph | |
| resolve_policies_for_actor | user: system, selectivity: medium, policies: 29 | Flame Graph |
read_scaling_complete
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| entity_by_id;one_depth | 1 entities | Flame Graph | |
| entity_by_id;one_depth | 10 entities | Flame Graph | |
| entity_by_id;one_depth | 25 entities | Flame Graph | |
| entity_by_id;one_depth | 5 entities | Flame Graph | |
| entity_by_id;one_depth | 50 entities | Flame Graph | |
| entity_by_id;two_depth | 1 entities | Flame Graph | |
| entity_by_id;two_depth | 10 entities | Flame Graph | |
| entity_by_id;two_depth | 25 entities | Flame Graph | |
| entity_by_id;two_depth | 5 entities | Flame Graph | |
| entity_by_id;two_depth | 50 entities | Flame Graph | |
| entity_by_id;zero_depth | 1 entities | Flame Graph | |
| entity_by_id;zero_depth | 10 entities | Flame Graph | |
| entity_by_id;zero_depth | 25 entities | Flame Graph | |
| entity_by_id;zero_depth | 5 entities | Flame Graph | |
| entity_by_id;zero_depth | 50 entities | Flame Graph |
read_scaling_linkless
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| entity_by_id | 1 entities | Flame Graph | |
| entity_by_id | 10 entities | Flame Graph | |
| entity_by_id | 100 entities | Flame Graph | |
| entity_by_id | 1000 entities | Flame Graph | |
| entity_by_id | 10000 entities | Flame Graph |
representative_read_entity
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/block/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/book/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/building/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/organization/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/page/v/2
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/person/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/playlist/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/song/v/1
|
Flame Graph | |
| entity_by_id | entity type ID: https://blockprotocol.org/@alice/types/entity-type/uk-address/v/1
|
Flame Graph |
representative_read_entity_type
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| get_entity_type_by_id | Account ID: bf5a9ef5-dc3b-43cf-a291-6210c0321eba
|
Flame Graph |
representative_read_multiple_entities
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| entity_by_property | traversal_paths=0 | 0 | |
| entity_by_property | traversal_paths=255 | 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true | |
| entity_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false | |
| entity_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true | |
| entity_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true | |
| entity_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true | |
| link_by_source_by_property | traversal_paths=0 | 0 | |
| link_by_source_by_property | traversal_paths=255 | 1,resolve_depths=inherit:1;values:255;properties:255;links:127;link_dests:126;type:true | |
| link_by_source_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:0;links:0;link_dests:0;type:false | |
| link_by_source_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:0;links:1;link_dests:0;type:true | |
| link_by_source_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:0;properties:2;links:1;link_dests:0;type:true | |
| link_by_source_by_property | traversal_paths=2 | 1,resolve_depths=inherit:0;values:2;properties:2;links:1;link_dests:0;type:true |
scenarios
| Function | Value | Mean | Flame graphs |
|---|---|---|---|
| full_test | query-limited | Flame Graph | |
| full_test | query-unlimited | Flame Graph | |
| linked_queries | query-limited | Flame Graph | |
| linked_queries | query-unlimited | Flame Graph |

🌟 Purpose
This PR adds the action for internal use on staging. It sends one plain-text email to one address, from the sender its deployment sets, through the transporters the API already uses, and returns the provider's message ID. Live delivery on staging also needs the AI worker deployment configured, which is outside this repo.
🔍 Changes
hash-apito@local/hash-backend-utils/email.sendMailtakes an optionalfrom,replyToandtext, returns the message ID, and throws anEmailSendErrorwhoseoutcomesays whether the email wasrejected, isunavailablefor now, or isunknown(it may have been sent).EmailTransportertype, which logs a failed send and resolves. The sender, the[STAGING SITE]and[DEV SITE]prefixes and the AWS SDK's own retries are unchanged. The log still carries the provider's own error, now with what became of the email (outcome).to,subject,bodyand an optionalreplyTo, all Text; the output ismessageId. Reply-to defaults to the email of the person who started the run, read from theirUserentity as that user.validateEmailAddressaccepts a single bare address. The editor runs it on typed values through the action'scheck, and the worker runs it on every value, since one can come from a flow input or an earlier step.🤔 Decisions to review
SendEmailhas no idempotency token, so retrying a send that may have succeeded can deliver twice. A 5xx, a timeout or a dropped connection therefore fails the step with "may have been sent"; only throttling (429), a connection that was never made and missing credentials are tried again. This needs agreement before a live send.@hash.aiaddresses are allowed unlessENVIRONMENTisproduction, so a deployment that doesn't say which it is stays restricted. The API'sisProdEnvalso treatsNODE_ENV=productionwithoutENVIRONMENTas production; the worker deliberately doesn't.HASH_FLOW_EMAIL_SENDERand has no default. With theawsorsmtptransporter and no sender, the step fails as not set up, rather than fall back to the API'sSYSTEM_EMAIL_*settings. Incomplete settings, such as a missingAWS_REGION, fail it the same way. Thedummytransporter has a sender of its own. The value isn't checked before sending.dummywhenNODE_ENV=production, so a deployed step can't report an email as sent when nothing was.escape, with<br>for line breaks, as not every email client keeps them forwhite-space. Runs of spaces collapse there.retryCountadds none. If the worker stops between a throttled try and the next, the step fails saying the email may have been sent, although it wasn't.🙅 Left out
HASH_EMAIL_TRANSPORTER=aws,HASH_FLOW_EMAIL_SENDERandAWS_REGIONon the AI worker, and lets the worker's AWS credentials send from that sender.from, so a per-flow sender can be added without changing it.🛡 Tests
singleAttempt.email-address.test.tscovers lists, display names, angle brackets, line breaks and over-long addresses.validate-flow-definition.test.tscovers the check reporting on the right input and a blank Reply-to.send-email-action.test.tscovers the Reply-to default and override, the recipient restriction, scheduled runs, each failure outcome with the number of tries it gets, incomplete worker settings failing the step without a throw, and a later attempt not sending.get-email-transporter.test.tscovers a deployment getting no transporter when it is set to the dummy one or names no sender, and the senderHASH_FLOW_EMAIL_SENDERnames.create-email-transporter.test.tscovers a failed send being logged with the provider's error rather than rejecting.lint:tsc,lint:eslintandtest:unitran locally for the six touched packages, andyarn lint:formatpasses. Everything passed except the frontendtest:unit, which timed out inprocess-editor.integration.test.tsx, a file this PR doesn't touch; that file passes when run alone. The edits since that run touch only the AI worker,hash-backend-utilsandhash-isomorphic-utils, and the three gates pass for each at the current head.send-email.scenario.tsmet 7 of 7 expectations through Temporal with the dummy transporter: a composed email with Reply-toalice@example.com, Draft email into Send email with an explicit Reply-to, and a rejectedada@example.com.awstransporter andHASH_FLOW_EMAIL_SENDER=flows@example.com: a 200 sent with one request; a 429 once sent on the second try, with two requests; a 429 always made three requests and failed; a 400 and a 500 each made one request. Every request was Fromflows@example.com. With no sender, the step failed as not set up and made no request.❓ How to test this?
HASH_EMAIL_TRANSPORTER=dummy.@hash.airecipient and pick the draft's Subject and Body.var/ai-worker/dummy-email-transporter/email-dumps.ymlwith your address as Reply-to.@hash.airestriction.yarn workspace @apps/hash-ai-worker-ts run-flow-scenario scripts/flow-scenarios/scenarios/send-email.scenario.ts.