Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(email): let email_state set the body budget it already enforced

Aashish57fc0f5a976c
diff --git a/laya/email.py b/laya/email.py
index a4a6dcf2..606f8410 100644
--- a/laya/email.py
+++ b/laya/email.py
@@ -159,7 +159,11 @@ def _strip_disclaimer(paragraph: str) -> str:
def clean_email_body(body: str, max_chars: int = 3000) -> str:
- """Remove quoted email history, signatures and disclaimers to keep input focused."""
+ """Remove quoted email history, signatures and disclaimers to keep input focused.
+
+ `max_chars` is the length the result is cut to, 3000 characters unless raised -- see
+ `email_state`, which takes the same budget and passes it through.
+ """
text = (body or "").replace("\r\n", "\n").replace("\r", "\n").replace("\\n", "\n")
# Bound regex work before the expensive patterns below: _DISCLAIMER uses
# [^.]{0,60/80/100} alternations whose cost grows with input length, and only
@@ -195,11 +199,22 @@ def clean_email_body(body: str, max_chars: int = 3000) -> str:
return text[:max_chars]
-def email_state(subject: str, body: str, sender: Optional[str] = None, clean: bool = True, **extra) -> Dict:
- """Construct a clean state dictionary for email classification."""
+def email_state(subject: str, body: str, sender: Optional[str] = None, clean: bool = True,
+ max_chars: int = 3000, **extra) -> Dict:
+ """Construct a clean state dictionary for email classification.
+
+ `max_chars` is the budget `clean_email_body` cuts the body to, and it is worth raising for a
+ long message: at the default the body stops after 3000 characters, so a request that arrives in
+ the last paragraphs never reaches the model -- including through `predict_long`, which scans a
+ state in windows precisely so it can read past one window's worth. Ignored when `clean=False`,
+ which passes the body through whole.
+
+ Any other keyword becomes a field of the state, so it is read by the model; a typo here is an
+ input mutation, not an error.
+ """
state = {
"subject": (subject or "").strip(),
- "body": clean_email_body(body) if clean else (body or ""),
+ "body": clean_email_body(body, max_chars=max_chars) if clean else (body or ""),
}
if sender:
state["from"] = sender
diff --git a/tests/test_email.py b/tests/test_email.py
index 0d13424a..8a7b897c 100644
--- a/tests/test_email.py
+++ b/tests/test_email.py
@@ -22,7 +22,13 @@ The last block guards a different kind of silence. `email_questions` was defined
in `laya/presets.py`, and `laya/__init__.py` re-exports the `presets` one. Editing the copy in
`laya/email.py` moved `laya.email.email_questions` and left `laya.email_questions` where it was,
with no test and no lint failing.
+
+The block at the end is the same shape again: `clean_email_body` takes a `max_chars` budget and
+`email_state` cut every body to its default, so a request in the last paragraphs of a long email was
+deleted before the model saw it -- while the keyword a caller would use to stop that,
+`email_state(..., max_chars=8000)`, fell into `**extra` and became a field of the state instead.
"""
+import inspect
import os
import sys
@@ -501,6 +507,78 @@ check(
)
+# --------------------------------- email_state owns the body budget (a `clean_email_body` control)
+# `clean_email_body(body, max_chars=...)` takes a budget and `email_state` called it with none, so
+# the state was always cut at 3000 characters with no lever. The one call a reader reaches for,
+# `email_state(subject, body, max_chars=8000)`, went to `**extra` instead, which merges the keyword
+# into the state that `serialize_state` renders into the model's input: the budget stayed at 3000
+# and a `"max_chars": 8000` field was added to what the checkpoint reads.
+LONG = ("The account review covers the last twelve statements and the support thread that goes with "
+ "them, including the charges that were disputed and the credits that were applied. ") * 40
+REQUEST = "Please refund the duplicate monthly charge of 49 dollars from the account ending 4417."
+LONG_BODY = LONG + "\n\n" + REQUEST
+
+check(
+ "budget/default still cuts at 3000",
+ len(email_state("Billing", LONG_BODY)["body"]),
+ 3000,
+)
+check_true(
+ "budget/the default cuts away a late request",
+ REQUEST not in email_state("Billing", LONG_BODY)["body"],
+ "a 3000-character cap should not reach the closing line of a 7000-character body",
+)
+check_true(
+ "budget/raising max_chars keeps the late request",
+ REQUEST in email_state("Billing", LONG_BODY, max_chars=8000)["body"],
+ "the whole point of the lever",
+)
+check(
+ "budget/max_chars reaches clean_email_body unchanged",
+ email_state("Billing", LONG_BODY, max_chars=8000)["body"],
+ clean_email_body(LONG_BODY, max_chars=8000),
+)
+check_true(
+ "budget/max_chars is not a field of the state",
+ "max_chars" not in email_state("Billing", LONG_BODY, max_chars=8000),
+ "it belongs to the cleaning, not to what the model reads",
+)
+check(
+ "budget/clean=False passes the body whole",
+ email_state("Billing", LONG_BODY, clean=False, max_chars=100)["body"],
+ LONG_BODY,
+)
+# `max_chars` is added last, so a call that already passed four positional arguments still means
+# subject, body, sender, clean. Reading the order off the signature is what fails if it moves.
+check(
+ "budget/positional order is unchanged",
+ [p.name for p in inspect.signature(email_state).parameters.values()][:5],
+ ["subject", "body", "sender", "clean", "max_chars"],
+)
+check(
+ "budget/sender still lands under from",
+ email_state("Billing", "Short body", sender="[email protected]").get("from"),
+)
+# The two defaults are literals on purpose -- a signature is documentation, and `max_chars: int =
+# 3000` reads better than a constant's name -- so this is what keeps the pair from drifting.
+def budget_default(fn):
+ param = inspect.signature(fn).parameters.get("max_chars")
+ return None if param is None else param.default
+
+
+check(
+ "budget/the two defaults are the same value",
+ budget_default(email_state),
+ budget_default(clean_email_body),
+)
+check(
+ "budget/email_state cuts where clean_email_body promises",
+ email_state("Billing", LONG_BODY)["body"],
+ clean_email_body(LONG_BODY),
+)
+
+
print("\n%d passed, %d failed" % (len(PASS), len(FAIL)))
for f in FAIL:
print(" FAIL " + f)