Skip to content

fix: support email send for go backend - #16895

Merged
yuzhichang merged 2 commits into
infiniflow:mainfrom
Haruko386:mei
Jul 15, 2026
Merged

yuzhichang merged 2 commits into
infiniflow:mainfrom
Haruko386:mei

Conversation

@Haruko386

Copy link
Copy Markdown
Member

Summary

As title

image d2aeeb50dd11cc8dc76bc4743425680e

@Haruko386 Haruko386 added 🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration go Pull requests that update go code labels Jul 14, 2026
@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jul 14, 2026
@Haruko386
Haruko386 marked this pull request as draft July 14, 2026 05:18
@Haruko386
Haruko386 marked this pull request as ready for review July 14, 2026 05:18
@coderabbitai

coderabbitai Bot commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds a registered Email workflow component with SMTP delivery, templated fields, recipient parsing, and soft failures. Runtime canvas states now initialize sys.date, including workflow runs, with tests for date preservation and email delivery behavior.

Changes

Email and runtime execution

Layer / File(s) Summary
Runtime sys.date initialization
internal/agent/runtime/state.go, internal/agent/runtime/state_test.go, internal/agent/canvas/scheduler.go, internal/service/agent.go
Canvas state and workflow runs initialize sys.date when missing or blank while preserving existing values.
SMTP transport dispatch
internal/agent/tool/email.go
Email delivery uses delegated SMTP sending, with implicit TLS on port 465, STARTTLS on other ports, shared SMTP submission, and context-aware deadlines.
Email component contract and invocation
internal/agent/component/email.go
Adds the registered Email component, parameter parsing, template resolution, recipient handling, streaming, output fields, and soft-failure results.
Email behavior validation
internal/agent/component/email_test.go
Tests successful mock-SMTP delivery, sys.date subject resolution, and invalid-configuration soft failures.

Estimated code review effort: 4 (Complex) | ~45 minutes

Suggested labels: ci, 💞 feature, size:L

Suggested reviewers: yuzhichang

Poem

A rabbit mails a note at dawn,
With dates stamped neatly before gone.
TLS hops through the moonlit air,
Soft errors wait with gentle care.
“Email flows!” the bunny sings,
While SMTP sprouts its silver wings.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The required Summary section is present, but it only says 'As title' and lacks the brief purpose and background the template asks for. Replace 'As title' with a brief summary of the email-sending fix and include the background/context from the template.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: enabling email sending support in the Go backend.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/agent/component/email.go`:
- Around line 78-99: After resolving the subject in EmailComponent.Invoke,
remove all carriage return and newline characters before placing subject into
the args map. Preserve the resolved subject content otherwise, and ensure the
sanitized value is what reaches the email tool’s Subject header.

In `@internal/agent/tool/email.go`:
- Around line 165-230: Update sendEmailSTARTTLS and sendEmailSMTPS so the entire
SMTP session has a bounded timeout, not just the SMTPS connection setup. Replace
smtp.SendMail in sendEmailSTARTTLS with explicit dialing and smtp.NewClient,
apply a deadline to the connection covering Hello, authentication, message
delivery, writes, and shutdown, and incorporate ctx cancellation/deadline into
that bound. Apply equivalent deadline handling in sendEmailSMTPS and preserve
existing SMTP behavior and error propagation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ddca67df-7260-407c-9636-d653a912858b

📥 Commits

Reviewing files that changed from the base of the PR and between 3fe9d29 and 28c1dbe.

📒 Files selected for processing (7)
  • internal/agent/canvas/scheduler.go
  • internal/agent/component/email.go
  • internal/agent/component/email_test.go
  • internal/agent/runtime/state.go
  • internal/agent/runtime/state_test.go
  • internal/agent/tool/email.go
  • internal/service/agent.go

Comment thread internal/agent/component/email.go Resolved
Comment thread internal/agent/tool/email.go Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/agent/tool/email.go (1)

262-267: 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Do not return an error if the email has already been accepted.

If wc.Close() on line 259 succeeds, the SMTP server has replied with a 250 OK and accepted the message for delivery. Returning ctx.Err() after this point masks a successful delivery as a failure, which can cause the upstream workflow execution to retry the node and send duplicate emails.

Once the data transmission completes successfully, you should ignore subsequent context cancellations and Quit() errors.

🐛 Proposed fix to prevent retries of successful emails
-	if err := ctx.Err(); err != nil {
-		return err
-	}
-	if err := client.Quit(); err != nil && err != io.EOF {
-		return err
-	}
+	// The email has been accepted by the server. We ignore context cancellation 
+	// and Quit errors here to prevent the caller from retrying a successful delivery.
+	if ctx.Err() == nil {
+		_ = client.Quit()
+	}
 	return nil
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/agent/tool/email.go` around lines 262 - 267, Update the
post-transmission cleanup after wc.Close succeeds so it returns success
regardless of subsequent ctx.Err() or client.Quit() failures. Remove the
context-error check and ignore Quit errors in this completed-delivery path,
while preserving error handling for failures before wc.Close succeeds.
🧹 Nitpick comments (1)
internal/agent/tool/email.go (1)

207-224: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Defer closing the raw connection to avoid manual resource management.

Currently, rawConn is only closed manually if HandshakeContext fails. Deferring rawConn.Close() immediately after successful dialing guarantees the socket is always cleaned up (even in the rare event of a panic during the handshake), and it safely allows removing the manual error-path closure. It is completely safe to close the underlying connection multiple times.

♻️ Proposed refactor
 	rawConn, err := dialer.DialContext(ctx, "tcp", addr)
 	if err != nil {
 		return err
 	}
+	defer rawConn.Close()
 	setEmailDeadline(ctx, rawConn)
 	stopWatch := watchEmailContext(ctx, rawConn)
 	defer stopWatch()
 
 	conn := tls.Client(rawConn, &tls.Config{ServerName: p.SMTPHost, MinVersion: tls.VersionTLS12})
 	if err := conn.HandshakeContext(ctx); err != nil {
-		_ = rawConn.Close()
 		return err
 	}
 	defer conn.Close()
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/agent/tool/email.go` around lines 207 - 224, Update sendEmailSMTPS
immediately after a successful dial to defer rawConn.Close(), then remove the
explicit rawConn.Close() from the HandshakeContext error path while preserving
the existing error return and TLS connection cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@internal/agent/tool/email.go`:
- Around line 262-267: Update the post-transmission cleanup after wc.Close
succeeds so it returns success regardless of subsequent ctx.Err() or
client.Quit() failures. Remove the context-error check and ignore Quit errors in
this completed-delivery path, while preserving error handling for failures
before wc.Close succeeds.

---

Nitpick comments:
In `@internal/agent/tool/email.go`:
- Around line 207-224: Update sendEmailSMTPS immediately after a successful dial
to defer rawConn.Close(), then remove the explicit rawConn.Close() from the
HandshakeContext error path while preserving the existing error return and TLS
connection cleanup.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 3111aa45-b051-4d85-860a-c7fe6b0b5d30

📥 Commits

Reviewing files that changed from the base of the PR and between 28c1dbe and 1815971.

📒 Files selected for processing (2)
  • internal/agent/component/email.go
  • internal/agent/tool/email.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/agent/component/email.go

@Haruko386 Haruko386 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@yuzhichang
yuzhichang merged commit 215d291 into infiniflow:main Jul 15, 2026
3 checks passed
@Haruko386
Haruko386 deleted the mei branch August 18, 2026 05:05
@Haruko386
Haruko386 restored the mei branch August 18, 2026 05:05
@Haruko386
Haruko386 deleted the mei branch August 18, 2026 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration go Pull requests that update go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants