fix: SonaTrajectoryService correctly uses native @ruvector/sona API#121
Open
Andycodeman wants to merge 2 commits intoruvnet:mainfrom
Open
fix: SonaTrajectoryService correctly uses native @ruvector/sona API#121Andycodeman wants to merge 2 commits intoruvnet:mainfrom
Andycodeman wants to merge 2 commits intoruvnet:mainfrom
Conversation
Fix three bugs preventing native SONA engine integration: 1. initialize() - stored module object instead of SonaEngine instance. Now uses SonaEngine.withConfig() for full learning parameter control. 2. recordTrajectory() - called nonexistent methods (recordStep, record, addStep). Now uses beginTrajectory → addTrajectoryStep → endTrajectory pipeline. 3. predict() - called nonexistent methods (predict, selectAction). Now uses findPatterns() to query learned patterns. Added stateToEmbedding() for deterministic hash-based embeddings when no external embedding is provided. Added forceLearn() and getNativeStats() for monitoring/testing. All changes maintain full backward compatibility with in-memory storage and frequency-based prediction fallback. Tested with 54 assertions across 10 test categories - all passing. Fixes ruvnet/ruflo#1243
54 assertions across 10 test categories: - Native engine initialization with SonaEngine.withConfig() - Single trajectory recording - 120-trajectory bulk recording with forceLearn() - Pattern search after learning - Prediction with native source - Embedding determinism and L2 normalization - Backward compatibility - New methods (forceLearn, getNativeStats) - Error recovery with bad inputs - RL methods backward compat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
SonaTrajectoryServicein agentdb has three critical bugs that completely prevent native SONA engine integration. As a result, all trajectory learning silently falls back to a basic JavaScript frequency counter — the Micro-LoRA, EWC++, and pattern clustering features of@ruvector/sonaare never invoked.This PR fixes all three bugs and includes a comprehensive test suite.
Source File Not Found in Git
During investigation, we discovered that
SonaTrajectoryService.tsdoes not exist anywhere in the git source — not onmain, not on any of the 18 feature branches, and not in the commit history. The file first appeared in the npm-publisheddist/starting atagentdb@3.0.0-alpha.4and has been present throughalpha.10, but the TypeScript source was never committed.This is part of a broader pattern: the published npm package contains files that diverge from the git source tree:
GNNService,GraphTransformerService,SemanticRouter,SonaTrajectoryServiceHierarchicalMemory,MemoryConsolidation,QUICConnection,QUICConnectionPool,StreamingEmbeddingServiceLLMRouterexists in both git source and npm dist underservices/Since no TypeScript source exists to patch, this PR adds the fixed JavaScript file at
packages/agentdb/src/services/SonaTrajectoryService.js— the correct location for the build system (tscwithrootDir: ".",outDir: "./dist",include: ["src/**/*"]).The maintainer may want to:
GNNService.ts, etc.) that are also only in npm distWhat is broken today (current version — broken since alpha.4)
The Silent Failure Chain
The current
SonaTrajectoryServiceappears to work but actually does nothing with the native engine. These bugs have existed since the file first appeared inalpha.4and are present in every version throughalpha.10.The broken method names (
recordStep||record||addStep,predict||selectAction) appear to be guesses at the@ruvector/sonaAPI rather than calls to its actual methods (beginTrajectory,addTrajectoryStep,endTrajectory,findPatterns).initialize()stores the module, not an engine instanceThe
@ruvector/sonamodule exports{ SonaEngine: [class] }. The current code looks forSONA/Sona(wrong class name), the constructor lookup fails, then thetypeof === 'object'branch stores the raw module. Nowthis.sonais the module, not an engine instance.recordTrajectory()calls nonexistent methods — silently catches and dropsSince none of these methods exist on
SonaEngine, every trajectory bypasses the native engine entirely. The emptycatch {}block ensures no error is ever reported.predict()calls nonexistent methods — silently falls backPredictions always come from the simple frequency counter, never from learned SONA patterns.
Real-world impact
findPatterns()is never calledengineType: 'native'andavailable: truewhile doing nothing with the native engineWhat this PR fixes
Fix 1:
initialize()— Proper engine instantiationSonaEngine.withConfig()for full control over learning parametersqualityThreshold: 0.1(vs default 0.5) ensures trajectories aren't filtered out during k-means clusteringoptions.hiddenDimparameter for caller configurationFix 2:
recordTrajectory()— Correct native pipelinebeginTrajectory->addTrajectoryStep->endTrajectorypipelinestateToEmbedding()methodtick()to trigger the instant learning loopFix 3:
predict()— UsesfindPatterns()for real predictionsfindPatterns()which is the actual SonaEngine query APIsource: 'native-sona'to distinguish from JS fallbackNew methods added
stateToEmbedding(state)— Deterministic hash-based embedding generatorforceLearn()— Triggers a background learning cycle on the native enginegetNativeStats()— Returns native engine statisticsTesting
Unit tests: 54/54 passing
Test suite:
packages/agentdb/tests/sona-trajectory-native-api.test.mjsSonaEngine.withConfig()creates proper engine instanceforceLearn()produces stored patternsfindPatterns()returnsJsLearnedPatternobjects with centroid, clusterSize, avgQualitysource: 'native-sona'with pattern-derived confidencefrequencyPredict()still workforceLearn()andgetNativeStats()return expected typesLive MCP tool verification
Also verified end-to-end through the claude-flow MCP tools:
trajectory-start->trajectory-step->trajectory-endall working correctlypattern-storeandpattern-searchwith 384-dim ONNX embeddings workingVersion history of the bug
alpha.0–alpha.3alpha.4(first appearance)alpha.5–alpha.8alpha.9(current installed)alpha.10(latest)Breaking changes
None. Full backward compatibility maintained.
Related issues
Fixes ruvnet/ruflo#1243
Files changed
packages/agentdb/src/services/SonaTrajectoryService.js— New file (source was never committed to git; only existed in npm-publisheddist/). Contains the fixed native API integration.packages/agentdb/tests/sona-trajectory-native-api.test.mjs— New test suite (54 assertions)🤖 Generated with claude-flow