
Product
Socket Firewall Now Blocks Malicious VS Code and Open VSX Extensions
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.
@holoscript/ai-validator
Advanced tools
AI hallucination validation layer for HoloScript - prevents invalid AI-generated code from breaking user workflows
AI hallucination validation layer for HoloScript. Prevents invalid AI-generated code from breaking user workflows by detecting common LLM hallucination patterns.
pnpm add @holoscript/ai-validator
import { AIValidator } from '@holoscript/ai-validator';
const validator = new AIValidator({
hallucinationThreshold: 50,
provider: 'anthropic',
strict: false,
});
// Validate AI-generated code
const result = await validator.validate(aiGeneratedCode);
if (!result.valid) {
console.error('Validation failed:');
result.errors.forEach((err) => {
console.log(`[${err.severity}] ${err.message}`);
if (err.suggestion) {
console.log(` Suggestion: ${err.suggestion}`);
}
});
} else {
console.log(`✓ Valid code (hallucination score: ${result.metadata.hallucinationScore}/100)`);
}
AIValidatorMain validation class with configurable rules.
interface ValidatorConfig {
strict?: boolean; // Reject warnings as errors (default: false)
knownTraits?: string[]; // Custom trait registry
hallucinationThreshold?: number; // Max hallucination score 0-100 (default: 50)
detectHallucinations?: boolean; // Enable pattern detection (default: true)
provider?: 'openai' | 'anthropic' | 'gemini' | 'local' | 'unknown';
}
validate(code: string): Promise<ValidationResult>Validates HoloScript code and returns detailed results.
const result = await validator.validate(code);
console.log('Valid:', result.valid);
console.log('Errors:', result.errors.length);
console.log('Warnings:', result.warnings.length);
console.log('Hallucination Score:', result.metadata.hallucinationScore);
getStats(): ValidatorStatsReturns validation statistics.
const stats = validator.getStats();
console.log('Known traits:', stats.knownTraits);
console.log('Hallucination patterns:', stats.hallucinationPatterns);
validateAICode(code, config?)Convenience function for one-off validation.
import { validateAICode } from '@holoscript/ai-validator';
const result = await validateAICode(aiCode, {
provider: 'openai',
hallucinationThreshold: 60,
});
Validates code against HoloScript parser:
// ❌ Invalid: Triple braces
cube {{{
@color(red)
}}}
// ✅ Valid: Correct syntax
cube {
@color(red)
}
Checks balanced braces and proper nesting:
// ❌ Invalid: Unclosed brace
cube {
@color(red)
// ✅ Valid: Balanced
cube {
@color(red)
}
Ensures traits exist in known registry:
// ❌ Invalid: Unknown trait
cube {
@magic_flying
@ai_powered
}
// ✅ Valid: Known traits
cube {
@grabbable
@physics
}
Detects common LLM hallucination patterns:
| Pattern | Score | Example |
|---|---|---|
| AI-like traits | 30 | @ai_powered, @smart_detection |
| Triple braces | 50 | {{{ or }}} |
| OOP syntax | 40 | class, extends, implements |
| Placeholders | 60 | [PLACEHOLDER], [YOUR_VALUE] |
| Task-marker comments | 20 | Line comments starting with common task labels |
| HTML/XML | 35 | <cube>...</cube> |
| JavaScript | 35 | function createCube() |
| Template literals | 45 | @color("${variable}") |
| Excessive repetition | 25 | 5+ identical traits |
Warns about style and performance issues:
// ⚠️ Warning: Empty object
cube {
}
// ⚠️ Warning: Very long line
cube { @position(0, 0, 0, 0, 0, 0, ...[100 values]...) }
Detects markdown code fences:
const validator = new AIValidator({ provider: 'openai' });
// ❌ Invalid: Markdown fences
```holoscript
cube { @color(red) }
// ✅ Valid: No fences cube { @color(red) }
### Anthropic (Claude)
Allows explanatory comments:
```typescript
const validator = new AIValidator({ provider: 'anthropic' });
// ✅ Valid: Claude-style comments allowed
cube {
// This is a red cube
@color(red)
}
The validator calculates a hallucination score (0-100) based on detected patterns:
const result = await validator.validate(suspiciousCode);
if (result.metadata.hallucinationScore > 70) {
// Regenerate with stricter constraints
regenerateCode({ moreStrict: true });
}
import { AIValidator } from '@holoscript/ai-validator';
const validator = new AIValidator({
provider: 'anthropic',
hallucinationThreshold: 50,
});
// In MCP tool handler
async function handleGenerateObject(args: any) {
const generatedCode = await generateWithLLM(args.prompt);
const validation = await validator.validate(generatedCode);
if (!validation.valid) {
// Provide feedback to LLM for regeneration
const feedback = validation.errors.map((e) => e.message).join('\n');
return await generateWithLLM(args.prompt, { feedback });
}
return generatedCode;
}
import { AIValidator } from '@holoscript/ai-validator';
import { HoloScriptSandbox } from '@holoscript/security-sandbox';
const validator = new AIValidator();
const sandbox = new HoloScriptSandbox();
// Validate THEN sandbox
async function executeSafely(aiCode: string) {
// Step 1: Validate
const validation = await validator.validate(aiCode);
if (!validation.valid) {
throw new Error(`Invalid code: ${validation.errors[0].message}`);
}
// Step 2: Sandbox
const result = await sandbox.executeHoloScript(aiCode, {
source: 'ai-generated',
});
return result;
}
async function generateWithValidation(prompt: string, maxAttempts = 3) {
for (let i = 0; i < maxAttempts; i++) {
const code = await llm.generate(prompt);
const validation = await validator.validate(code);
if (validation.valid) {
return code;
}
// Provide validation feedback to LLM
const feedback = [
'Your previous attempt had errors:',
...validation.errors.map((e) => `- ${e.message}`),
...validation.errors.map((e) => (e.suggestion ? ` Try: ${e.suggestion}` : '')),
].join('\n');
prompt = `${prompt}\n\nFeedback from previous attempt:\n${feedback}`;
}
throw new Error('Failed to generate valid code after max attempts');
}
// Strict for production
const prodValidator = new AIValidator({
hallucinationThreshold: 30,
strict: true,
});
// Lenient for development
const devValidator = new AIValidator({
hallucinationThreshold: 70,
strict: false,
});
// Set provider for better validation
const validator = new AIValidator({
provider: detectProvider(apiKey), // 'openai', 'anthropic', etc.
});
// Add custom traits for domain-specific validation
const validator = new AIValidator({
knownTraits: [...DEFAULT_TRAITS, '@custom_physics', '@special_rendering'],
});
const result = await validator.validate(code);
if (!result.valid) {
// Log for debugging
console.error('Validation errors:', result.errors);
// Send to monitoring
monitoring.trackValidationFailure({
provider: result.metadata.provider,
hallucinationScore: result.metadata.hallucinationScore,
errors: result.errors.length,
});
// Provide user feedback
showUserError('Generated code was invalid. Please try again.');
}
MIT © Brian X Base Team
FAQs
AI hallucination validation layer for HoloScript - prevents invalid AI-generated code from breaking user workflows
The npm package @holoscript/ai-validator receives a total of 11 weekly downloads. As such, @holoscript/ai-validator popularity was classified as not popular.
We found that @holoscript/ai-validator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.

Research
More than 140 Mastra npm packages were compromised in a supply chain attack that used a typosquatted dependency to deliver a cross-platform infostealer during installation.

Research
/Security News
A new npm package tests AI malware scanners with prompt injection, safety-triggering comments, context flooding, and obfuscated JavaScript.