v2/docs/TRANSFORMER_INITIALIZATION_ISSUE.md
Date: January 25, 2025 Resolution: [email protected] detects npx environment and skips transformer loading Versions: [email protected], [email protected] Impact: Zero - Clean user experience with appropriate messaging
Previous Issue: October 25, 2025 Affected Versions: [email protected], [email protected] Impact: Low - System worked but showed confusing errors
Solution Implemented:
Result:
npx claude-flow@alpha memory store "test" "value"
[Embeddings] NPX environment detected - using hash-based embeddings
[Embeddings] For semantic search, install globally: npm install -g claude-flow
✅ Stored successfully
No more ONNX/WASM errors! Clean user experience maintained.
When using npx claude-flow@alpha memory commands in v2.7.24, the local transformer model (Xenova/all-MiniLM-L6-v2) failed to initialize in npx temporary directories due to ONNX runtime and WASM backend issues. The system gracefully fell back to hash-based embeddings, maintaining full functionality but showing confusing error messages.
Before fix:
npx claude-flow@alpha memory store "test" "Local embeddings working!"
[Embeddings] Initializing local embedding model (Xenova/all-MiniLM-L6-v2)...
[Embeddings] First run will download ~23MB model...
Error: /onnxruntime_src/include/onnxruntime/core/common/logging/logging.h:371
static const onnxruntime::logging::Logger& onnxruntime::logging::LoggingManager::DefaultLogger()
Attempt to use DefaultLogger but none has been registered.
Something went wrong during model construction (most likely a missing operation).
Using `wasm` as a fallback.
[Embeddings] Failed to initialize: no available backend found. ERR:
[Embeddings] Falling back to hash-based embeddings
✅ ✅ Stored successfully in ReasoningBank ← Still works!
After fix (v2.7.25):
npx claude-flow@alpha memory store "test" "Clean experience!"
[Embeddings] NPX environment detected - using hash-based embeddings
[Embeddings] For semantic search, install globally: npm install -g claude-flow
✅ Stored successfully in ReasoningBank
Error: Attempt to use DefaultLogger but none has been registered
Location: onnxruntime-node backend initialization
Why it happens:
Error: no available backend found
Why it happens:
Memory system fully functional
Graceful degradation
Hash-based embeddings
Semantic search quality
True vector similarity
# Install globally (not via npx)
npm install -g claude-flow@alpha
# Now transformers work better
claude-flow memory store "test" "value"
# ✅ Real transformer embeddings may work
# Just use it - fallback works automatically
npx claude-flow@alpha memory store "key" "value"
npx claude-flow@alpha memory query "search"
# ✅ Works with hash-based similarity
Set environment variable to skip transformer initialization:
export SKIP_TRANSFORMERS=true
npx claude-flow@alpha memory store "key" "value"
# ✅ Skips transformer loading, uses hash directly
File: agentic-flow/src/reasoningbank/utils/embeddings.ts
async function initializeEmbeddings(): Promise<boolean> {
// Skip if in npx environment (known issue)
const isNpx = process.env.npm_config_user_agent?.includes('npx') ||
process.cwd().includes('_npx');
if (isNpx && !process.env.FORCE_TRANSFORMERS) {
console.log('[Embeddings] Detected npx environment');
console.log('[Embeddings] Using hash-based embeddings (set FORCE_TRANSFORMERS=true to try local model)');
return false;
}
// Try to initialize...
}
File: agentic-flow/src/reasoningbank/utils/embeddings.ts
import { env } from '@xenova/transformers';
async function initializeEmbeddings(): Promise<boolean> {
try {
// Configure ONNX runtime before loading
if (env.backends?.onnx) {
// Disable ONNX runtime in npx (use WASM instead)
env.backends.onnx = false;
}
// Force WASM backend with proper configuration
if (env.backends?.wasm) {
env.backends.wasm.numThreads = 1;
env.backends.wasm.simd = false; // Disable SIMD for compatibility
}
// Now load pipeline
embeddingPipeline = await pipeline(
'feature-extraction',
'Xenova/all-MiniLM-L6-v2',
{ device: 'wasm' } // Force WASM backend
);
return true;
} catch (error) {
console.log('[Embeddings] Transformer initialization failed, using hash-based embeddings');
return false;
}
}
File: agentic-flow/package.json
{
"dependencies": {
"better-sqlite3": "^11.10.0",
// ... other deps
},
"optionalDependencies": {
"@xenova/transformers": "^3.2.0"
},
"peerDependencies": {
"@xenova/transformers": "^3.0.0"
},
"peerDependenciesMeta": {
"@xenova/transformers": {
"optional": true
}
}
}
Then update embeddings.ts to check if module exists:
let transformersAvailable = false;
try {
await import('@xenova/transformers');
transformersAvailable = true;
} catch {
console.log('[Embeddings] @xenova/transformers not installed - using hash-based embeddings');
transformersAvailable = false;
}
# Test with npx (uses fallback)
npx claude-flow@alpha memory store "test" "value"
npx claude-flow@alpha memory query "test"
# ✅ Should work with hash-based embeddings
# Test with local install (may use transformers)
npm install -g claude-flow@alpha
claude-flow memory store "test" "value"
# ✅ May use real transformers if environment supports it
# Store data
npx claude-flow@alpha memory store "key1" "value1"
npx claude-flow@alpha memory store "key2" "value2"
# List data
npx claude-flow@alpha memory list
# ✅ Should show both entries
# Query data
npx claude-flow@alpha memory query "key"
# ✅ Should find both (text matching)
✅ Positive:
⚠️ Limitations:
✅ Faster startup:
⚠️ Lower quality:
Add section:
## Memory System: Semantic Search
The memory system includes semantic search capabilities with automatic fallback:
### Local Installation (Recommended for Semantic Search)
```bash
npm install -g claude-flow@alpha
claude-flow memory query "authentication"
# ✅ Attempts to use local transformer model
npx claude-flow@alpha memory query "authentication"
# ✅ Uses hash-based text matching (fallback)
Note: Due to npx environment limitations, local transformer models may not initialize. The system automatically falls back to hash-based embeddings which provide basic text similarity matching.
For best semantic search quality, install globally instead of using npx.
---
## Related Issues
- GitHub Issue #840: SQLite memory command fixes (resolved)
- GitHub Issue (NEW): Transformer initialization in npx environments
- Upstream: @xenova/transformers ONNX/WASM initialization in temporary directories
---
## Recommendations
### For Users:
1. **Use local installation** if semantic search quality matters
2. **Use npx** if basic text matching is sufficient
3. **Be aware** that "semantic search enabled" message appears even with fallback
### For Developers:
1. **Document fallback behavior** clearly in user-facing messages
2. **Make transformers optional** in package.json
3. **Add environment detection** to skip transformer loading in npx
4. **Fix ONNX logger registration** for environments where it's possible
5. **Test in npx** as part of CI/CD to catch these issues
---
## ✅ Resolution Implemented (v2.7.25)
The transformer initialization issue has been **completely resolved** in v2.7.25 with [email protected].
**What was implemented:**
1. ✅ NPX environment detection
2. ✅ Automatic skip of transformer loading in npx
3. ✅ Clean, helpful user messaging
4. ✅ Full backward compatibility
**Implementation Details ([email protected]):**
```typescript
// NPX detection added to embeddings.ts
const isNpx = process.env.npm_config_user_agent?.includes('npx') ||
process.cwd().includes('_npx') ||
process.cwd().includes('npm/_npx');
if (isNpx && !process.env.FORCE_TRANSFORMERS) {
console.log('[Embeddings] NPX environment detected - using hash-based embeddings');
console.log('[Embeddings] For semantic search, install globally: npm install -g claude-flow');
return false; // Skip transformer initialization cleanly
}
Benefits:
Status: ✅ RESOLVED - No further action needed
The transformer initialization issue in npx was a known limitation that was being handled gracefully with fallback. The system remained fully functional with hash-based embeddings as a fallback. Users who needed true semantic search could install globally.
Priority: Medium - System worked, but showed confusing error messages
Resolution Timeline: