Skip to content

fix(email): don't take an opening "Thanks for ..." line for the sign-off - #111

Closed
ahmedembeddedxx wants to merge 1 commit into
NandhaKishorM:mainfrom
ahmedembeddedxx:fix/email-signoff-false-positive
Closed

ahmedembeddedxx wants to merge 1 commit into
NandhaKishorM:mainfrom
ahmedembeddedxx:fix/email-signoff-false-positive

Conversation

@ahmedembeddedxx

@ahmedembeddedxx ahmedembeddedxx commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

clean_email_body treats any short line that starts with a closing word as the sign-off and cuts everything below it. The most common opener in a support reply is exactly such a line, so the request is deleted and only the greeting survives:

from laya import email_state

email_state("Refund", "Hi,\nThanks for getting back to me.\nThe refund still has not arrived.\nCan you check?")["body"]
# before: 'Hi,'
# after:  'Hi,\nThanks for getting back to me.\nThe refund still has not arrived.\nCan you check?'

Agent.predict / Router.predict then classifies an email it can no longer see, and nothing warns.
Routing reads the same body, so the email can also go to the wrong checkpoint:

Router().route(email_state("Lieferung", "Hallo,\nBestellung 4471 ist nicht angekommen.\nBitte prüfen Sie das."), {})
# before: body 'Hallo,'  -> english       ("English Latin text")
# after:  full body      -> multilingual  ("Latin script but language looks like 'de', not English")

Cause

laya/email.py, in _SIGNATURE_MARKERS:

re.compile(r"^\s*(best|kind|warm|many thanks|thanks|thank you|regards|cheers|sincerely)[\w ,!.]*$", re.I)

After the keyword, [\w ,!.]* accepts any run of words, so a whole sentence qualifies as long as it fits in the 40-character limit. best, kind and warm on their own also match ordinary words and prefixes ("Best time to…", "Kind of urgent…", "Bestellung…"). For emails of nine lines or fewer the search starts at line 1, so the second line of a short email is always a candidate.

Change

A sign-off line now has to be a closing phrase (the same set as before, except that kind and warm need regards or wishes after them). The sender's name may follow, as up to three capitalised tokens: "Thanks, Alice", "Best regards, Dr. Jane Doe", "Regards, Łukasz". _CLOSING matches the phrase. The name check lives in _is_sign_off so it can use str.isupper() and accept non-ASCII names. The -- and "Sent from my…" markers are unchanged, and so is the search window.

Before / after

line that decides the cut before after
Thanks for getting back to me. + request 'Hi,' full body
Thank you for your reply. + request 'Hello,' full body
Best way to reach me is by phone. + request 'Hi support,' full body
Cheers for the quick fix yesterday. + request 'Hey,' full body
Thanks, I tried that already. + request 'Hi,' full body
Bestellung 4471 ist nicht angekommen. + request 'Hallo,' full body
Thanks, / Best regards, / Thanks again! / Best regards, Dr. Jane Doe / Regards, Łukasz cut cut

Trade-off

A closing written as a sentence (Thanks for your help,, Thank you for your time.) with a name after it used to be stripped. Now it stays, so the body ends with that line and the name. I couldn't find a rule that separates it from an opener like Thanks for getting back to me. without cutting real requests again. Like #94, this errs towards keeping text: an extra pleasantry does no harm, but a missing request does.

Verification

tests/test_email.py gains 8 opener cases and 22 sign-off cases, for 41 in total.

  • main (573e5b6): 33 passed, 8 failed. All 8 failures are opener cases. Every sign-off case already passes on main, so real closings are cut exactly as before.
  • this branch: 41 passed, 0 failed.
  • The other suites CI runs still pass (test_router, test_criteria, test_download, test_shortlist, test_decision_model, test_packaging), as do the CI ruff check and compileall. Tested on Python 3.11 with CPU-only torch.

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you, deleting the request after a "Thanks for ..." opener is a nasty failure for support mail. Today's main moved a lot (35 PRs merged), so this now conflicts. Could you rebase onto the latest main? The email suite grew today (#200, #115), so please re-run your cases after rebasing. I'd like to land this.

The sign-off pattern accepted any short line that started with a closing
word, followed by anything made of word characters, spaces and ",!.".
So an opener like "Thanks for getting back to me." or "Best time to reach
me is after 5pm." was taken for the signature, and everything below it
(the actual request) was cut. For emails of nine lines or fewer the
search starts at line 1, so the most common reply opener in support mail
reduced the body to "Hi,". The bare "best" prefix also matched words like
"Bestellung" ("order" in German).

A sign-off line now has to be a closing phrase, optionally followed by up
to three capitalised name tokens ("Thanks, Alice", "Best regards, Dr.
Jane Doe"). A closing written as a sentence ("Thanks for your help,") is
no longer stripped and stays in the body. Where the two goals conflict,
the cleaner keeps text, as in NandhaKishorM#94.
@ahmedembeddedxx
ahmedembeddedxx force-pushed the fix/email-signoff-false-positive branch from 722d384 to 6c00f25 Compare September 23, 2026 08:04
@ahmedembeddedxx

Copy link
Copy Markdown
Contributor Author

Rebased onto main (010bace). The fix itself is unchanged; the conflicts were around it.

One thing worth flagging from the merge: #200 added the Portuguese/Spanish sign-offs with the rule that they "match only on their own ... unlike the English marker no trailing words are allowed". This PR is that same rule for English, so both markers now behave the same way. In clean_email_body the sign-off test sits beside the device-footer clause #200 added, and both are kept.

Re-ran everything after the rebase: tests/test_email.py is 46/46 here, including the Portuguese and Spanish cases from #200 and #115. Reverting only laya/email.py to main fails the 8 opener cases, so the suite still pins the bug. The other 15 suites CI runs, ruff and compileall pass on 3.10, 3.11, 3.12 and 3.13.

@ahmedembeddedxx

Copy link
Copy Markdown
Contributor Author

#132 landed the same fix, so I'm closing this in favour of it — same rule (closing word, optional continuation, at most a name), and it reaches every opener case this branch had: all 7 of mine are kept on main now.

Two things from my suite that #132 does not reach, so they are not lost: Warmest regards,, Thanks and regards, and Regards, Łukasz were cut before #132 and are kept now. I've opened #245 with those, on top of #132.

Thanks for the review, and no objection to the outcome — the bug is fixed either way.

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you for rebasing, and for pointing out that the English sign-off rule should behave like the Portuguese and Spanish one from #200. #132 landed first and fixes the same bug with a single _SIGNATURE_MARKERS pattern, so the two now conflict. I ran all 30 of your cases against the new main: 27 pass. The 3 that still fail are real sign-offs left in the body: Thanks and regards,, Warmest regards,, and Regards, Łukasz (the name pattern only allows ASCII capitals). Could you rebase onto main and extend that one pattern to cover them, keeping your 30 cases as the tests? Then there is one rule instead of two, and I will merge it.

@NandhaKishorM

Copy link
Copy Markdown
Owner

Thank you. #245 extends #132's single rule to cover the closings from your cases (Warmest regards,, Thanks and regards,, Regards, Łukasz), so this is covered and I'm closing it in favour of that one. Your 30 cases were what showed the gap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants