Skip to content

fix(nodejs): validate sendAndWait timeout to fix malformed error message#1700

Open
gimenete wants to merge 3 commits into
github:mainfrom
gimenete:gimenete/fix-sendandwait-timeout-validation
Open

fix(nodejs): validate sendAndWait timeout to fix malformed error message#1700
gimenete wants to merge 3 commits into
github:mainfrom
gimenete:gimenete/fix-sendandwait-timeout-validation

Conversation

@gimenete

Copy link
Copy Markdown

Summary

Fixes the malformed timeout error reported in #915.

When session.sendAndWait() received a non-numeric timeout (e.g. an object or NaN), the value was passed straight to setTimeout(), which coerces an invalid delay to 0ms. The session then rejected almost immediately with a misleading message:

Timeout after [object Object]ms waiting for session.idle

instead of surfacing the real problem (an invalid argument).

This PR validates the timeout argument up front and throws a clear TypeError when it is not a non-negative finite number of milliseconds:

sendAndWait timeout must be a non-negative number of milliseconds (got object)

This also fixes the secondary symptom of a spurious immediate timeout caused by the NaN/object → 0ms coercion.

Changes

  • nodejs/src/session.ts: validate timeout in sendAndWait() before use; document the new @throws in the JSDoc.
  • nodejs/test/client.test.ts: add regression tests covering object, NaN, and negative timeouts, asserting the error message no longer contains [object Object].

Scope note

#915 also reports that resumeSession() with customAgents never reaches idle. The SDK already forwards customAgents in the session.resume payload, so that behavior is on the CLI runtime side and is not addressable in this SDK. This PR therefore targets only the malformed-message bug.

Testing

From nodejs/:

  • npx vitest run test/client.test.ts -t "sendAndWait timeout validation" (fails before the fix, passes after)
  • npx vitest run test/client.test.ts — all 111 pass
  • npm run lint — 0 errors
  • npm run format:check — clean
  • npm run typecheck — clean

When sendAndWait() received a non-numeric timeout (e.g. an object or NaN),
the value was passed straight to setTimeout(), which coerced it to a 0ms
delay. The session then rejected almost immediately with a misleading
"Timeout after [object Object]ms waiting for session.idle" message rather
than surfacing the real problem.

Validate the timeout argument up front and throw a clear TypeError when it
is not a non-negative finite number of milliseconds, so callers get an
actionable error instead of a spurious immediate timeout.

Note: this addresses the malformed-message bug from github#915. The separate
report that resumeSession() with customAgents never reaches idle is a
CLI runtime behavior and is not addressable in the SDK.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gimenete gimenete requested a review from a team as a code owner June 17, 2026 09:49
Copilot AI review requested due to automatic review settings June 17, 2026 09:49

Copilot AI 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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR adds validation for the CopilotSession.sendAndWait() timeout argument to prevent non-numeric values from being coerced by setTimeout(), and adds regression tests to ensure a clear TypeError is thrown for invalid timeouts.

Changes:

  • Add runtime guard in sendAndWait() to reject non-finite, non-number, or negative timeouts with a TypeError
  • Document the new TypeError behavior in the JSDoc
  • Add Jest tests covering object, NaN, and negative timeout inputs (regression for #915)

Reviewed changes

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

File Description
nodejs/test/client.test.ts Adds regression tests ensuring invalid timeout inputs fail with a clear error.
nodejs/src/session.ts Implements and documents invalid timeout validation to prevent setTimeout() coercion.

Comment thread nodejs/test/client.test.ts Outdated
Comment thread nodejs/test/client.test.ts Outdated
Comment thread nodejs/src/session.ts Outdated
gimenete and others added 2 commits June 17, 2026 11:55
- Report a null timeout as "null" instead of "object" in the error message
- Capture the rejection once per test instead of invoking sendAndWait twice
- Assert the full error message (including the sendAndWait prefix) so the
  regression tests don't match unrelated errors

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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