Skip to content

fix(privileged-context): Reject statements with helpful error message (fixes bug 2027926)#61

Open
dmose wants to merge 1 commit intomozilla:mainfrom
dmose:fix-statement-fails
Open

fix(privileged-context): Reject statements with helpful error message (fixes bug 2027926)#61
dmose wants to merge 1 commit intomozilla:mainfrom
dmose:fix-statement-fails

Conversation

@dmose
Copy link
Copy Markdown
Member

@dmose dmose commented Mar 31, 2026

Fixes bug 2027926 - Models sometimes pass JavaScript statements (const/let/var declarations) to evaluate_privileged_script, which wraps input as return(expression). This regularly causes issues like Error: Script execution failed: SyntaxError: expected expression, got keyword 'const', which slow things down and wastes tokens.

Adds some verbiage to the tool description explicitly telling callers not to pass statements without wrapping them in an IIFE, along with an isLikelyStatement() heuristic that detects statement keywords and returns a clear error message suggesting an IIFE.

@dmose dmose changed the title fix(privileged-context): Reject statements with helpful error message fix(privileged-context): Reject statements with helpful error message (fixes bug 2027926) Mar 31, 2026
@dmose
Copy link
Copy Markdown
Member Author

dmose commented Mar 31, 2026

Note that this may also be useful for non-privileged contexts, but I started just fixing the version of the issue I happened to be running into. That seems like it could be a follow-on fix, if needed.

@juliandescottes juliandescottes self-requested a review March 31, 2026 06:40
Copy link
Copy Markdown
Collaborator

@juliandescottes juliandescottes left a comment

Choose a reason for hiding this comment

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

Makes sense and following up for regular script evaluation as well.

@dmose dmose force-pushed the fix-statement-fails branch 3 times, most recently from 968200d to 115b26f Compare April 1, 2026 03:56
Models sometimes pass JavaScript statements (const/let/var declarations)
to evaluate_chrome_script, which wraps input as return(expression),
which causes tool aborts, resulting in slowdown and wasted tokens.

Adds isLikelyStatement() validation that detects statement keywords
and returns a clear error message suggesting the IIFE workaround.
Also updates tool description to warn against statement usage.
@dmose dmose force-pushed the fix-statement-fails branch from 115b26f to 93864bf Compare April 1, 2026 04:10
@dmose
Copy link
Copy Markdown
Member Author

dmose commented Apr 1, 2026

OK, ready to merge, I think. But I don't have merge privs, so I'll have to defer doing that myself. :-)

Copy link
Copy Markdown
Collaborator

@juliandescottes juliandescottes left a comment

Choose a reason for hiding this comment

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

@dmose two little things to adjust if you are still around today, otherwise I can merge and followup.

@@ -0,0 +1,127 @@
/**
* Tests for statement detection and rejection in privileged context tools
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The suggestion was to move the existing tests to privileged-context-state.test.ts (since they are not really testing a tool, but rather the side effect of using a tool on the privileged context "state")


vi.mock('../../src/index.js', () => ({
getFirefox: () => mockGetFirefox(),
}));
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can we just move that to Privileged Context Tool Handlers and drop the comment now ?

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