bug: SonaEngine.getStats() returns Rust struct string, JSON.parse crashes in sona-wrapper.js
Summary
SonaEngine.getStats() in ruvector 0.2.12 crashes with a SyntaxError because the native Rust binding returns a Rust struct debug string (e.g., CoordinatorStats { trajectories_buffered: 0, ... }) but the JavaScript wrapper at dist/core/sona-wrapper.js:231 calls JSON.parse() on it.
Environment
- ruvector: 0.2.12
- Node.js: 22.17.1
- Platform: Windows (MINGW64), win32-x64
- Install method: npm
Steps to Reproduce
const { SonaEngine } = require('ruvector');
const sona = new SonaEngine(384);
console.log(sona.getStats()); // CRASHES
Expected Behavior
getStats() should return a JavaScript object like:
{ trajectories_buffered: 0, trajectories_dropped: 0, buffer_success_rate: 1.0, patterns_stored: 0, ewc_tasks: 0, instant_enabled: true, background_enabled: true }
Actual Behavior
The native binding returns a Rust debug-format string:
CoordinatorStats { trajectories_buffered: 0, trajectories_dropped: 0, buffer_success_rate: 1.0, patterns_stored: 0, ewc_tasks: 0, instant_enabled: true, background_enabled: true }
Then sona-wrapper.js line 231 does JSON.parse() on this string, which throws:
SyntaxError: Unexpected token 'C', "Coordinato"... is not valid JSON
Root Cause
The Rust NAPI binding for getStats() appears to use format!("{:?}", stats) or Display impl instead of serializing to JSON via serde. The wrapper assumes JSON output.
Workaround
// Use string coercion instead of the wrapper's getStats()
const statsStr = '' + sona.getStats(); // Works but returns unparseable string
Suggested Fix
Either:
- Rust side: Return
serde_json::to_string(&stats)from the NAPI binding - JS side: Parse the Rust struct string format, or catch the error and return raw
File Reference
dist/core/sona-wrapper.js, line 231:
getStats() {
return JSON.parse(this._native.getStats()); // <-- crashes here
}
Fixed in [email protected].
The SonaEngine.getStats() wrapper now handles both JSON and Rust debug format strings. Added fallback parser that converts Rust debug format (StructName { field: value, ... }) to JavaScript objects.
The fix gracefully handles:
- Valid JSON responses (preferred)
- Rust Debug format strings (fallback)
- Unrecognized formats (returns
{ raw: string })
Update with: npm install [email protected]