Skip to content

fix(email): email_state swallowed max_chars into the model's input - #589

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-email-state-max-chars
Sep 27, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:feat-email-state-max-chars

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

clean_email_body(body, max_chars=3000) takes a budget. email_state, the helper that calls it,
called it with no argument -- and ends in **extra, which merges unknown keywords into the state
dictionary that serialize_state renders into the model's input
.

So the call a reader of docs/reference/helpers.md reaches for,
email_state(subject, body, max_chars=8000), did two things at once: it left the body cut at 3000
characters, and it added a "max_chars": 8000 field to what the checkpoint reads. No error, no
warning, and the state now describes a budget the caller never asked the model about.

The cost is not cosmetic. email_state is the documented way to prepare an email, and 3000
characters is where the body stops -- so the last paragraphs of a long message never reach
predict_long, the API that exists precisely to read past one window's worth. On a phished
invoice, that is the sentence that matters.

Change

laya/email.py: email_state(..., clean=True, max_chars=3000, **extra) forwards the budget to
clean_email_body, placed after clean so an existing positional call keeps its meaning. Its
docstring -- the text docs/reference/helpers.md renders -- now names the budget, says when to
raise it, says clean=False passes the body whole, and says the remaining **extra keywords become
model input.

No new constant: both defaults stay literal, because a signature is the documentation here (griffe
prints max_chars: int = 3000, not a name), and tests/test_email.py compares the two defaults
instead.

Measured

laya-bench/probe_email_state_max_chars.py on a 7175-character body whose request is in the last
paragraph, English checkpoint, cpu. Before = 4066d5d (laya-bench/email_state_max_chars_before.log),
after = this branch (…_after.log). Decisions are compared at the level the model commits to:
label for a choice, probability >= 0.5 for a noul, rounded integer for a score.

email_state("Billing", BODY, max_chars=8000):

before after
state keys ['body', 'max_chars', 'subject'] ['body', 'subject']
body the model sees 3000 chars 7174 chars
predict_long answers is_phishing=False is_spam=False is_phishing=True is_spam=True
usage.input_tokens 4936 13611

The capped row, for reference, is is_phishing=False is_spam=False at 4901 tokens -- so before this
change the "widening" call bought 35 tokens of injected field and no extra email. The after row now
matches the hand-cleaned reference (clean_email_body(body, max_chars=8000) assembled into a state
by hand): same decisions, same 13611 tokens.

What did not move, which matters as much as what did:

  • the default is unchanged -- 3000 characters on both trees, so every existing caller is untouched;
  • predict (one window) gives the same answers for the capped and the widened body on both trees
    (single-window path: short != wide : False), because a 512-token state was never going to read
    paragraph forty -- the cap costs an answer exactly on the long-document path;
  • category stayed billing and urgency/needs_reply stayed put in all five rows. Two of the
    preset's five questions flip; this fixes the budget, not the checkpoint's category sense.

Tests

python tests/test_email.py: 105 → 115 passed (both measured). The ten new checks cover the default
cut, the raised cut keeping the late request, max_chars reaching clean_email_body unchanged,
max_chars not appearing as a state field, clean=False passing the body whole, the positional
order, and the two signatures agreeing on the default.

Gate check (laya-bench/mutate_email_state_budget_gate.py, restored after every variant --
restored: True):

  0  as shipped                  exit=0  115 passed, 0 failed  failures: none
  m1 never forward the budget    exit=1  113 passed, 2 failed  failures: raising max_chars keeps the late request,
                                                               max_chars reaches clean_email_body unchanged
  m2 forward to the wrong branch exit=1  112 passed, 3 failed  + clean=False passes the body whole
  m3 ignore the parameter        exit=1  113 passed, 2 failed  failures: as m1
  m4 drop it into **extra again  exit=1  110 passed, 5 failed  + max_chars is not a field of the state,
                                                               + positional order is unchanged,
                                                               + the two defaults are the same value
  m5 change the positional order exit=1  114 passed, 1 failed  failures: positional order is unchanged
  m6 clean a body asked for whole exit=1 114 passed, 1 failed  failures: clean=False passes the body whole
  m7 let the two defaults drift  exit=1  113 passed, 2 failed  failures: the two defaults are the same value,
                                                               email_state cuts where clean_email_body promises

m4 is the one that keeps this honest: reverting to the shipped signature is caught by the
state-field check, not just by the budget checks.

Other gates on this tree: ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120
→ All checks passed!; python -m compileall -q laya/ tests/; tests/test_packaging.py → 43
passed; tests/test_hooks_api.py → 131 passed; zensical build --strict --clean → No issues found, and the built site/reference/helpers/index.html now carries max_chars: int = 3000 in
both signatures (one occurrence before, four mentions of max_chars after).

Overlap

Two open PRs touch laya/email.py: #503 (@PerryLink) and #468 (@lloyd-aloysius), both inside
clean_email_body's marker lists (roughly lines 15-160). This change is in email_state (line 198)
and its call of clean_email_body, so the hunks are ~40 lines apart; both of them also append to
tests/test_email.py, so whoever lands last takes a tail conflict there. Neither changes the body
budget, and #468's symptom (a device footer eating the request) is different from this one (a
length cap sitting before the request) -- though both are "the request never reached the model", so
it is worth reading the two together.

`clean_email_body` takes `max_chars` and `email_state` called it with none, so every state body
stopped at 3000 characters. `email_state` ends in `**extra`, which merges keywords into the state
that `serialize_state` renders into the model's input, so the call a reader reaches for --
`email_state(subject, body, max_chars=8000)` -- neither raised the budget nor failed: it added a
`max_chars` field to what the checkpoint reads.

The cap is invisible for a state that fits one window, but `predict_long` scans a state in windows
precisely to read past one, so for a long message the last paragraphs -- on a phished invoice, the
phishing -- were cut before the model saw them. `max_chars` is now an explicit parameter after
`clean`, so existing positional calls keep their meaning and the default stays 3000.
@NandhaKishorM
NandhaKishorM merged commit 1c84cb9 into NandhaKishorM:main Sep 27, 2026
22 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Thanks @aashish254, merged. It ships in 0.3.21.

NandhaKishorM pushed a commit that referenced this pull request Sep 29, 2026
…ity)

emailState cleaned the body with cleanEmailBody's default budget and gave no way
to change it, so every state body stopped at 3000 characters. Python's
email_state takes max_chars for this (#589): for a long message the last
paragraphs, which can hold the actual request or the phishing ask, were cut
before the model saw them, including through the windowed long-state scan that
exists to read past one window.

Add maxChars as the last parameter, defaulting to 3000, and pass it to
cleanEmailBody. It is last so existing positional calls keep their meaning, and
it is ignored when clean is false, as in Python.

This is a parity fix; it makes no prediction-accuracy claim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
NandhaKishorM added a commit that referenced this pull request Sep 29, 2026
fix(ts): let emailState set the body budget, as Python does (#589 parity)
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