fix: pass a real ScopeHandle to Node withScope callbacks#196
Conversation
The Node.js quick start and the nemo-relay-node package README passed the
informational handle from `withScope` straight into `event`, `toolCallExecute`,
and `llmCallExecute`. Those APIs expect either `null` (the current scope) or a
real `ScopeHandle`, so the plain handle object failed with "Failed to recover
`ScopeHandle` type from napi value" on the first call. The rejected promise then
skipped `deregisterSubscriber`, leaving the subscriber's ThreadsafeFunction
ref'd and hanging the process.
The LLM example also built the request with `new LlmRequest(...)`, but
`llmCallExecute` takes a plain `{ headers, content }` JSON object.
Update both examples to pass `null` for the active scope and a plain request
object, matching the behavior covered by the Node binding tests.
Signed-off-by: Zhongxuan Wang <daniewang@nvidia.com>
WalkthroughThis PR refactors promise-aware callback invocation to support non-JSON argument builders, enabling ChangesScopeHandle Callback Materialization
🎯 3 (Moderate) | ⏱️ ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@crates/node/README.md`:
- Line 80: Update the explanatory comment that mentions handle to wrap the
literal null in inline code formatting; specifically change the comment "//
`handle` describes the active scope; pass null to target the current scope." so
that the word null is surrounded by backticks (i.e., `null`) to match
documentation formatting and coding guidelines for code elements.
In `@docs/getting-started/quick-start/nodejs.mdx`:
- Line 60: The sentence "For lifecycle calls inside the scope, pass null to
target the current scope." should format the literal null as inline code; update
the docs text in docs/getting-started/quick-start/nodejs.mdx so that `null` is
wrapped in backticks (i.e., change the plain word null to a monospace code
element) to follow the project's docs formatting guidelines for code elements.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Enterprise
Run ID: 747dbda6-9d62-47e3-bb0b-0a004321b7d9
📒 Files selected for processing (2)
crates/node/README.mddocs/getting-started/quick-start/nodejs.mdx
📜 Review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Check / Run
- GitHub Check: Preview docs
🧰 Additional context used
📓 Path-based instructions (22)
**/*.{md,rst,html,txt}
📄 CodeRabbit inference engine (.agents/skills/review-doc-style/assets/nvidia-style-brand-terminology.md)
**/*.{md,rst,html,txt}: Always spellNVIDIAin all caps. Do not useNvidia,nvidia,nVidia,nVIDIA, orNV.
Usean NVIDIAbefore a noun because the name starts with an 'en' sound.
Do not add a registered trademark symbol afterNVIDIAwhen referring to the company.
Use trademark symbols with product names only when the document type or legal guidance requires them.
Verify official capitalization, spacing, and hyphenation for product names.
Precede NVIDIA product names withNVIDIAon first mention when it is natural and accurate.
Do not rewrite product names for grammar or title-case rules.
Preserve third-party product names according to the owner's spelling.
Include the company name and full model qualifier on first use when it helps identify the model.
Preserve the official capitalization and punctuation of model names.
Use shorter family names only after the full name is established.
Spell out a term on first use and put the acronym in parentheses unless the acronym is widely understood by the intended audience.
Use the acronym on later mentions after it has been defined.
For long documents, reintroduce the full term if readers might lose context.
Form plurals of acronyms withs, not an apostrophe, such asGPUs.
In headings, common acronyms can remain abbreviated. Spell out the term in the first or second sentence of the body.
Common terms such asCPU,GPU,PC,API, andUIusually do not need to be spelled out for developer audiences.
Files:
crates/node/README.md
**/*.{md,rst,html}
📄 CodeRabbit inference engine (.agents/skills/review-doc-style/assets/nvidia-style-brand-terminology.md)
Link the first mention of a product name when the destination helps the reader.
Files:
crates/node/README.md
**/*.md
📄 CodeRabbit inference engine (.agents/skills/contribute-integration/SKILL.md)
Documentation must be updated if activation or usage changed
**/*.md: Use title case consistently in technical documentation headings
Avoid quotation marks, ampersands, and exclamation marks in headings
Keep product, event, research, and whitepaper names in their official title case
Use title case for table headers
Do not force social-media sentence case into technical docs
Format code elements, commands, parameters, package names, and expressions in monospace
Format directories, file names, and paths in monospace using backticks
Use angle brackets inside monospace for variables inside paths, such as/home/<username>/.login
Format error messages and strings in quotation marks, keeping literal code strings in code formatting when clearer
Format UI buttons, menus, fields, and labels in bold
Use angle brackets between UI labels for menu paths, such as File > Save As
Use italics for new terms on first use, sparingly and only when introducing the term
Use italics for publication titles
Format keyboard shortcuts in plain text, such as Press Ctrl+Alt+Delete
Use owner/repo link text for GitHub repositories, preferring[NVIDIA/NeMo](link)over prose references like 'the GitHub repo'
Introduce every code block with a complete sentence
Do not make a code block complete the grammar of the previous sentence
Do not continue a sentence after a code block
Use syntax highlighting when the format supports it for code blocks
Avoid the word 'snippet' unless the surrounding docs already use it as a term of art
Keep inline method, function, and class references consistent with nearby docs, omitting empty parentheses for prose readability when no call is shown
Use descriptive anchor text that matches the destination title when possible for links
Avoid raw URLs in running text
Avoid generic anchor text such as 'here,' 'this page,' and 'read more'
Include acronyms in link text when a linked term includes an acronym
Do not link long sentences or multiple sentences
Avoid links ...
Files:
crates/node/README.md
{crates/adaptive/**,python/nemo_relay/adaptive.py,python/nemo_relay/plugin.py,go/nemo_relay/adaptive/**,go/nemo_relay/!(adaptive)/**,**/node/**,**/wasm/**}
📄 CodeRabbit inference engine (.agents/skills/maintain-optimizer/SKILL.md)
Keep adaptive surface in sync across crates/adaptive, shared plugin behavior in core and bindings, Python adaptive/plugin wrappers in python/nemo_relay/adaptive.py and python/nemo_relay/plugin.py, Go adaptive helpers under go/nemo_relay/adaptive plus shared plugin helpers in go/nemo_relay, and Node/WebAssembly adaptive helpers and plugin wrappers
Files:
crates/node/README.md
{crates/adaptive/**,python/nemo_relay/plugin.py,go/nemo_relay/**,**/node/**,**/wasm/**}
📄 CodeRabbit inference engine (.agents/skills/maintain-optimizer/SKILL.md)
{crates/adaptive/**,python/nemo_relay/plugin.py,go/nemo_relay/**,**/node/**,**/wasm/**}: Maintain consistent plugin lifecycle across all language bindings (Python, Go, Node/WebAssembly, and Rust)
Keep plugin context surfaces aligned across all language implementations
Files:
crates/node/README.md
**/{docs,examples,**/*.md,*.patch,*.diff,.github,*.sh,*.yaml,*.yml}
📄 CodeRabbit inference engine (.agents/skills/rename-surfaces/SKILL.md)
Update documentation, examples, CI configuration, and patch artifacts when performing rename operations
Files:
crates/node/README.md
**/*.{md,rst,txt}
📄 CodeRabbit inference engine (.agents/skills/review-doc-style/assets/nvidia-style-guide.md)
Spell
NVIDIAin all caps. Do not useNvidia,nvidia, orNV.
Files:
crates/node/README.md
**/*.{md,rst}
📄 CodeRabbit inference engine (.agents/skills/review-doc-style/assets/nvidia-style-guide.md)
**/*.{md,rst}: Format commands, code elements, expressions, package names, file names, and paths as inline code.
Use descriptive link text. Avoid raw URLs and weak anchors such as "here" or "read more."
Use title case consistently for technical documentation headings.
Introduce code blocks, lists, tables, and images with complete sentences.
Write procedures as imperative steps. Keep steps parallel and split long procedures into smaller tasks.
Prefer active voice, present tense, short sentences, contractions, and plain English.
Usecanfor possibility and reservemayfor permission.
Useafterfor temporal relationships instead ofonce.
Preferrefer tooverseewhen the wording points readers to another resource.
Avoid culture-specific idioms, unnecessary Latinisms, jokes, and marketing exaggeration in technical docs.
Spell out months in body text, avoid ordinal dates, and use clear time zones.
Spell out whole numbers from zero through nine unless they are technical values, parameters, versions, or UI values.
Use numerals for 10 or greater and include commas in thousands.
Do not add trademark symbols to learning-oriented docs unless the source, platform, or legal guidance explicitly requires them.
Files:
crates/node/README.md
{docs/**,README.md,CONTRIBUTING.md,**/*.md}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
Run docs link validation with
just docs-linkcheckwhen links change
Files:
crates/node/README.mddocs/getting-started/quick-start/nodejs.mdx
{docs/**,README.md,**/Cargo.toml,**/package.json,**/*.md}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
Ensure renamed public surfaces are reflected consistently in manifests and docs for large or public-facing changes
Files:
crates/node/README.mddocs/getting-started/quick-start/nodejs.mdx
**/*.{md,mdx,py,sh,yaml,yml,toml,json}
📄 CodeRabbit inference engine (.agents/skills/contribute-docs/SKILL.md)
Keep package names, repo references, and build commands current
Files:
crates/node/README.mddocs/getting-started/quick-start/nodejs.mdx
**/*.{html,md,mdx}
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Include SPDX license header in HTML and Markdown files using HTML comment syntax
Files:
crates/node/README.mddocs/getting-started/quick-start/nodejs.mdx
**/README.md
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Update relevant crate or package README when that surface changed
Files:
crates/node/README.md
**/*.{rs,py,js,ts,tsx,jsx,go,sh,toml,yaml,yml,md}
📄 CodeRabbit inference engine (AGENTS.md)
Keep SPDX headers on source, docs, scripts, and configuration files. The project is Apache-2.0.
Files:
crates/node/README.md
crates/{python,ffi,node,wasm}/**/*
⚙️ CodeRabbit configuration file
crates/{python,ffi,node,wasm}/**/*: Treat binding changes as public API changes. Check for parity with the other language bindings, FFI ownership/lifetime safety,
callback error propagation, stable type conversion, and consistent async/stream semantics.
Flag changes that update one binding without corresponding tests or documentation for the same surface elsewhere.
Files:
crates/node/README.md
{docs/**,README.md,CONTRIBUTING.md}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
{docs/**,README.md,CONTRIBUTING.md}: For docs-only changes, run targeted checks only if commands, package names, or examples changed. Usejust docsfor docs-site builds andjust docs-linkcheckwhen links changed
Run docs site build withjust docs
Files:
docs/getting-started/quick-start/nodejs.mdx
{docs/**,README.md}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
Verify README and docs entry points still match current package names and paths for large or public-facing changes
Files:
docs/getting-started/quick-start/nodejs.mdx
{docs/**,examples/**,README.md}
📄 CodeRabbit inference engine (.agents/skills/validate-change/SKILL.md)
Verify examples still run with documented commands for large or public-facing changes
Files:
docs/getting-started/quick-start/nodejs.mdx
**/*.mdx
📄 CodeRabbit inference engine (.agents/skills/contribute-docs/SKILL.md)
In MDX files, top-of-file comments must use JSX comment delimiters: {/* to open and */} to close. Do not use HTML comments for MDX SPDX headers.
MDX top-of-file SPDX comments must use {/* ... */} delimiters instead of HTML comment delimiters (Must-Fix)
Files:
docs/getting-started/quick-start/nodejs.mdx
docs/**/*.{md,mdx}
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Update embedded documentation snippets, patch docs, and binding-support notes if examples or supported bindings changed
Files:
docs/getting-started/quick-start/nodejs.mdx
docs/**
📄 CodeRabbit inference engine (CONTRIBUTING.md)
Run
just docsor./scripts/build-docs.sh htmlto regenerate ignored Fern API reference pages before validation for documentation site changes
Files:
docs/getting-started/quick-start/nodejs.mdx
{docs/**,README.md,CONTRIBUTING.md,RELEASING.md,SECURITY.md}
⚙️ CodeRabbit configuration file
{docs/**,README.md,CONTRIBUTING.md,RELEASING.md,SECURITY.md}: Review documentation for technical accuracy against the current API, command correctness, and consistency across language bindings.
Flag stale examples, missing SPDX headers where required, and instructions that no longer match CI or pre-commit behavior.
Files:
docs/getting-started/quick-start/nodejs.mdx
willkill07
left a comment
There was a problem hiding this comment.
Manually specifying a handle should work though. What was the raised panic?
@willkill07 Hey Will, yes I think manually specifying a handle does work; here it's mainly a rejected Promise / thrown napi error; I should've included the error output: And then it's stuck there forever. It's because Therefore, I made |
|
@zhongxuanwang-nv i think this PR should also fix that bug -- in other languages, we do get a scope handle back. |
Node's withScope handed its callback a plain JSON object instead of a
real ScopeHandle, so passing it back into event, toolCallExecute, or
llmCallExecute threw "Failed to recover ScopeHandle type from napi
value" on the first call, and the skipped deregisterSubscriber left the
process hung. Materialize a real ScopeHandle on the JS thread inside the
threadsafe-function call (via a new Arg0Builder path in PromiseAwareFn)
so the callback receives a usable handle, matching the Rust, Python, and
WebAssembly bindings.
Keep the Node quick start passing the handle and send the LLM request as
a plain { headers, content } object, and add a Node test that reuses the
callback handle across event, pushScope, toolCallExecute, and
llmCallExecute.
Signed-off-by: Zhongxuan Wang <daniewang@nvidia.com>
Thanks Will! I think the new proposed changes fixed that to make it consistent with other languages :) |
willkill07
left a comment
There was a problem hiding this comment.
Approving with one open question
LlmRequest delivers headers and content to llmCallExecute correctly, so the quick start needs no plain-object request. Restore the quick start to its original LlmRequest usage; the scope-handle fix alone makes the example run end to end. Signed-off-by: Zhongxuan Wang <daniewang@nvidia.com>
|
/merge |
…201) #### Overview The package READMEs' "Getting Started" examples register a subscriber, emit events, then deregister without calling flush. Native subscriber delivery is non-blocking, so an example that exits right after deregistering can lose queued events. This updates all four package READMEs to flush before deregister, matching the quickstarts (which already flush). - [x] I confirm this contribution is my own work, or I have the right to submit it under this project's license. - [x] I searched existing issues and open pull requests, and this does not duplicate existing work. #### Details **Functional fixes** (verified by running — the example dropped events without flush): - `python/nemo_relay/README.md`: printed nothing, or intermittently a single partial line; the `to_dict()` / `to_json()` callback never ran. Adds `nemo_relay.subscribers.flush()` before each `deregister(...)`. - `go/nemo_relay/README.md`: flaky — dropped the trailing tool-end and scope-end events on exit (2 of 3 runs partial). Adds `defer nemo.FlushSubscribers()`, ordered to run after the deferred `scope.Pop` emits the scope-end event and before the deferred deregister. **Parity** (not currently broken; updated so every README models the same pattern): - `crates/node/README.md`: Node keeps the event loop alive via the ref'd ThreadsafeFunction, so it already delivers; adds `flushSubscribers()` for consistency. - `crates/wasm/README.md`: WASM's `flushSubscribers()` is a single-threaded no-op (tested as such); adds it for consistency. #### Where should the reviewer start? The four README diffs — each adds a flush before `deregister(...)` (Node/WASM also import `flushSubscribers`). Verified by running each example with the flush added: events deliver deterministically (Go 5/5, Python and Node 3/3 across repeated runs). WASM's `flushSubscribers` is an exported no-op (`crates/wasm/src/api/mod.rs`; covered by `crates/wasm/tests-js/scope_tests.mjs`). Note: the `crates/node/README.md` example also depends on the Node binding change in #196 (pass a real `ScopeHandle` to `withScope` callbacks). That is orthogonal to this flush change — #196 is code-only and does not touch the README. #### Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to) - Relates to: #196 (Node `withScope` ScopeHandle; orthogonal, touches the same Node README example) ## Summary by CodeRabbit * **Documentation** * Updated Getting Started examples across Python, Node.js, WebAssembly, and Go language bindings to reflect the proper sequence for flushing subscribers before deregistration. Authors: - Zhongxuan (Daniel) Wang (https://github.com/zhongxuanwang-nv) Approvers: - Will Killian (https://github.com/willkill07) URL: #201
Overview
withScopein the Node.js binding handed its callback a plain JSON object ({ uuid, name, scopeType, ... }) instead of a realScopeHandle. Passing that object back intoevent,toolCallExecute, orllmCallExecute(which acceptnullor aScopeHandle) threwinternal error: Failed to recover ScopeHandle type from napi valueon the first call, and because the rejection skippedderegisterSubscriber, the process hung. This fixes the binding so the callback receives a real, reusableScopeHandle, matching the Rust, Python, and WebAssembly bindings where the scope helper hands back a usable handle. The existing Node quick start andnemo-relay-nodeREADME examples already pass the handle, so they now run as written with no doc changes.Details
Root cause: Node's
with_scopeinvokes the callback across aThreadsafeFunctionboundary, and anapi_wrap'dScopeHandlecannot cross that boundary, so the previous code serialized the handle to a plain JSON object. That object carries no wrapped native handle, sonapi_unwrapfails when it is later passed to an API typedOption<&ScopeHandle>.Changes:
crates/node/src/promise_call.rs: add anArg0Builder(Box<dyn FnOnce(&Env) -> napi::Result<JsUnknown> + Send>) and acall_with_arg0method so the first callback argument can be materialized on the JS thread inside the threadsafe call. The existingcall/call_with_json_next/call_with_stream_nextJSON paths are unchanged.crates/node/src/api/mod.rs:with_scopenow builds a realScopeHandleinstance through that builder (ToNapiValue::to_napi_value) instead ofserde_json::json!({...}), so the callback receives a handle it can pass straight intoevent,toolCallExecute, andllmCallExecute. The auto-pop and error paths are unchanged.crates/node/tests/scope_tests.mjs: addcallback receives a reusable ScopeHandle, which passes the callback handle intoevent,pushScope(as an explicit parent),toolCallExecute, andllmCallExecuteand asserts the results.This aligns Node with the existing cross-binding behavior and does not change the shared runtime contract or the other bindings. No documentation changes are required: the quick start and README already pass the handle, and
LlmRequestis delivered tollmCallExecutecorrectly, so those examples run unchanged once the binding is fixed.Validation:
just build-nodeandjust test-node: 226/226 pass, including the new test.{ echo: 'hello' }, and the LLM result, then exits0with no hang (previously it threw on the firstevent(...)call and hung).cargo fmt --all,cargo clippy -p nemo-relay-node --all-targets -- -D warnings, anduv run pre-commitpass.Where should the reviewer start?
Start with
with_scopeincrates/node/src/api/mod.rs: the callback argument is now a realScopeHandlebuilt on the JS thread through the newArg0Builderpath incrates/node/src/promise_call.rs. Cross-checkcrates/node/tests/scope_tests.mjs(callback receives a reusable ScopeHandle), which reuses the handle acrossevent,pushScope,toolCallExecute, andllmCallExecute— the calls that previously threw.Related Issues: (use one of the action keywords Closes / Fixes / Resolves / Relates to)