Skip to content

bug: SonaEngine.getStats() returns Rust struct string, JSON.parse crashes in sona-wrapper.js #257

@PogeystickJoe

Description

@PogeystickJoe

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:

  1. Rust side: Return serde_json::to_string(&stats) from the NAPI binding
  2. 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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions