RuVector icon indicating copy to clipboard operation
RuVector copied to clipboard

fix(ruvector-gnn): RuvectorLayer constructor panics instead of returning Result

Open ruvnet opened this issue 9 months ago • 1 comments

Summary

MultiHeadAttention::new() and RuvectorLayer::new() in ruvector-gnn use assert!() / panic!() for input validation instead of returning Result. When called from NAPI-RS or WASM bindings, this causes a fatal abort() that crashes the entire host process — no try/catch can intercept it.

Reproduction

const { RuvectorLayer } = require('@ruvector/gnn');

// This kills the process — no exception, just abort()
const layer = new RuvectorLayer(384, 128, 3, 0.1);
// hidden_dim=128 is NOT divisible by heads=3 → panic
thread '<unnamed>' panicked at crates/ruvector-gnn/src/layer.rs:97:9:
Embedding dimension must be divisible by number of heads
fatal runtime error: failed to initiate panic, error 5, aborting
Aborted (core dumped)

Root Cause

Two issues:

  1. Rust-side (crates/ruvector-gnn/src/layer.rs): MultiHeadAttention::new() uses assert!() instead of Result — panics across FFI boundary = process abort
  2. JS-side (downstream agentdb GNNService): passes config.layers (=3) as the heads parameter instead of config.heads, so 128 % 3 ≠ 0 triggers the panic

Fix

  • MultiHeadAttention::new() → returns Result<Self, GnnError>
  • RuvectorLayer::new() → returns Result<Self, GnnError>
  • All WASM/NAPI wrappers propagate errors as catchable JS exceptions
  • Also fixed pre-existing mmap.rs test compilation errors (grad_offset returns Option<usize>)

Affected Versions

Impact

Surface Before After
Native Rust panic!() → process abort Err(GnnError::LayerConfig(...))
NAPI-RS (Node.js) abort() → uncatchable crash Catchable napi::Error
WASM WASM trap Catchable JsError

ruvnet avatar Feb 26 '26 16:02 ruvnet

Additional finding in ruvector 0.2.12 (Windows win32-x64):

The fix described in this issue may not have shipped correctly in 0.2.12. On Windows, the constructor is completely non-functional — not panicking, just missing:

const { gnnWrapper } = require('ruvector');

// gnnWrapper.RuvectorLayer exists as a property...
console.log(typeof gnnWrapper.RuvectorLayer); // "function"

// ...but instantiation fails with non-panic error:
new gnnWrapper.RuvectorLayer(384, 128);
// Error: NativeRuvectorLayer is not a constructor

// Same for TensorCompress:
new gnnWrapper.TensorCompress();
// Error: NativeTensorCompress is not a constructor

// And differentiableSearch:
gnnWrapper.differentiableSearch(vectors, query, 2);
// Error: nativeDifferentiableSearch is not a function

However, isGnnAvailable() returns true and isNative() returns true, which is misleading since the native constructors don't work.

Environment: ruvector 0.2.12, Node.js 22.17.1, Windows MINGW64 win32-x64

This may be a Windows-specific binary packaging issue — the JS wrappers exist but the native .node bindings they delegate to may not have been included for win32-x64.

PogeystickJoe avatar Mar 13 '26 00:03 PogeystickJoe