Skip to content

fix(ts): let emailState set the body budget, as Python does (#589 parity) - #713

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-email-state-max-chars
Sep 29, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
Divit-aggarwal:fix/ts-email-state-max-chars

Conversation

@Divit-aggarwal

Copy link
Copy Markdown
Contributor

What

emailState takes a new last parameter, maxChars (default 3000), and passes it to cleanEmailBody. This matches Python's email_state(..., max_chars=) from #589.

Why

emailState cleaned the body with the default budget and gave no way to change it, so every state body stopped at 3000 characters. For a long message the last paragraphs, which can hold the real request or the phishing ask, were cut before the model saw them. That includes the windowed long-state scan, which exists to read past one window. Putting max_chars into extra only added a field to the model's input.

const s = emailState(subject, longBody, null, true, {}, 8000);
// before: no way to raise the budget; body cut at 3000 chars
// after:  body kept up to 8000 chars

It is the last parameter so existing positional calls keep their meaning, and it is ignored when clean is false, as in Python.

How it was verified

cd laya-ts
npx tsc -p tsconfig.json --noEmit   # exit 0
npm test -- --run                   # 23 files, all tests passed
npm run test:package                # package test passed
cd ..
ruff check laya/ --select=E9,F63,F7,F82,F401,F811 --line-length=120   # All checks passed
python -m compileall -q laya/ tests/                                   # exit 0

The two Python commands are the CI gates; no Python was changed. The new test fails on main and passes with this change.

Scope

  • TypeScript only: one parameter in emailState and its test
  • Additive: the default is unchanged, so every existing call behaves as before
  • Parity fix only. No prediction-accuracy claim is made

Related: #589 (the Python change).

Written with Claude Code; I've reviewed the change and re-run the verification myself.

🤖 Generated with Claude Code

…shorM#589 parity)

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 (NandhaKishorM#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>

@Bruce-Yii Bruce-Yii 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.

This matches Python on the points that matter, and I confirmed them by running email_state on main: the default budget is 3000, max_chars=8000 widens the body to the full 5027 characters, clean=False returns the body whole, and the keyword is ignored when clean=False. The TS matches on all four, the default is unchanged so existing callers are untouched, and the new test does go red on main.

Two things to close out.

The positional order diverges, and the #589 leak is still reachable. Python has max_chars as the fifth parameter; here it is sixth, after extra. That is the right call for backwards compatibility, but the consequence is that a port of the Python call shape does nothing, silently:

emailState("s", body, null, true, 8000)                // body still 3000, no error
emailState("s", body, null, true, { max_chars: 8000 }) // keys include "max_chars", body still 3000

The second line is the bug #589 removed, and it survives because max_chars is not a named parameter here — so a caller following the Python documentation reaches for extra and gets an injected state field and an uncut budget, which is exactly the combination from the issue. A JSDoc line on emailState spelling out the TS order, and warning that max_chars belongs in the new parameter rather than in extra, would close it.

Only one of the four new assertions discriminates. Against main, emailState("s", body).body is already 3000, long already has no max_chars key, and clean=false already returns body identically. Only expect(long.body).toContain("wire the money") fails. Since 8000 exceeds the body length, nothing is actually cut, so the suite never shows the budget being applied. A bound case would:

expect(emailState("s", body, null, true, {}, 4000).body).toHaveLength(4000);

and a case for the extra leak above would pin it so a future change cannot reintroduce it unnoticed.

@NandhaKishorM
NandhaKishorM merged commit d195ba5 into NandhaKishorM:main Sep 29, 2026
20 of 21 checks passed
@NandhaKishorM

Copy link
Copy Markdown
Owner

Passed checks and merged. Thank you @Divit-aggarwal! It ships in 0.3.22.

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.

3 participants