Skip to content

Handler shape validation guidance#105

Merged
simongdavies merged 4 commits intomainfrom
handler-shape-validation-guidance
May 6, 2026
Merged

Handler shape validation guidance#105
simongdavies merged 4 commits intomainfrom
handler-shape-validation-guidance

Conversation

@simongdavies
Copy link
Copy Markdown
Member

This pull request updates the handler registration system for JavaScript code in the sandbox, clarifying and enforcing stricter requirements for handler function declarations. It improves both the user-facing documentation and the internal validator logic, ensuring that only correctly structured handler functions are accepted. The changes also enhance error messages and add comprehensive tests for various handler declaration scenarios.

Handler declaration requirements and documentation:

  • Updated all user-facing documentation (system-message.ts, index.ts) to clarify that handlers must be declared as function handler(...) { ... } or async function handler(...) { ... } (if using await), with flexible parameter names and optional parameters. Arrow functions and incorrect names are explicitly disallowed, and clear guidance is given for correct usage.

Validator logic improvements:

  • Refactored the Rust validator to provide more specific and actionable error messages for common handler declaration mistakes, such as using arrow functions or incorrect function names. The validator now pinpoints the line of the problem and suggests the correct fix.
  • Removed the requirement that handler functions must have at least one parameter; handlers with zero parameters are now accepted. Parameter names are now flexible and optional, matching the updated documentation.

Handler return detection:

  • Improved the logic that checks for a return statement inside the handler function, ensuring that returns in nested functions or arrow functions are not incorrectly counted as valid handler returns.

Testing:

  • Added comprehensive tests for handler validation, covering correct and incorrect handler declarations, acceptance of custom parameter names, zero-parameter handlers, and rejection of arrow functions and misnamed handlers.

These changes make handler requirements clearer for users and more robustly enforced by the system, reducing common errors and improving the developer experience.

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
…sistency

- Gate return-check on has_handler_function && no signature issue to
  avoid misleading 'MUST return' errors on invalid handler shapes
- Expand check_handler_has_return skip logic to also skip arrow function
  bodies (=> { ... }) and generator declarations (function*)
- Fix conflicting guidance in register_handler tool description: use
  function handler(...) instead of function handler(event) in REQUIRED line
- Add zero-parameter form function handler() to system-message signature list

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 6, 2026 20:51
@simongdavies simongdavies added the enhancement New feature or request label May 6, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR tightens and clarifies the required shape of sandbox handler code (function handler(...) { ... } / async function handler(...) { ... }), updates validator behavior and error guidance accordingly, and expands test coverage to lock in the new expectations.

Changes:

  • Updated user-facing guidance (system prompt + CLI tool docs/help) to explicitly require function handler declarations (including optional params / async guidance) and to disallow arrow-based handlers.
  • Refined the Rust validator to emit more actionable structure errors (including line attribution) and to improve handler-return detection by ignoring returns in nested scopes.
  • Added/expanded tests for handler declaration variants (custom param names, zero-parameter handlers, arrow handler rejection, and module-mode detection).

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/sandbox-tool.test.ts Adds execution tests for module-style handlers with custom parameter names and no parameters.
tests/analysis-guest.test.ts Expands guest validator tests for handler declaration modes and improved error messaging.
src/code-validator/guest/runtime/src/validator.rs Implements stricter handler-shape diagnostics and more precise handler-return detection logic.
src/agent/system-message.ts Updates system prompt guidance to reflect stricter handler declaration requirements.
src/agent/index.ts Updates tool/help text for handler registration and handler code style guidance.

Comment thread src/code-validator/guest/runtime/src/validator.rs
Comment thread src/agent/index.ts Outdated
Comment thread src/code-validator/guest/runtime/src/validator.rs
- Skip misnamed-handler check when valid function handler already exists,
  preventing false positives on nested helpers (e.g. function process())
- Fix inconsistent help text: handler(...) instead of handler(event) to
  match the zero-parameter guidance
- Also skip function( and function*( expression forms in return check
  so anonymous function expression returns are not counted as handler returns

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Keep our PR review fixes (handler validation improvements):
- Nested helper false-positive prevention
- Consistent handler(...) help text
- Anonymous function expression return skip

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
@simongdavies simongdavies merged commit c7f90b7 into main May 6, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants