#!/usr/bin/env node /** * Add Cultural DNA Instructions (inst_085-089) to instruction-history.json * * Reads draft instructions, validates, and appends to instruction history */ const fs = require('fs'); const path = require('path'); // Paths const INSTRUCTION_HISTORY_PATH = path.join(__dirname, '..', '.claude', 'instruction-history.json'); const DRAFT_INSTRUCTIONS_PATH = path.join(__dirname, '..', 'docs', 'outreach', 'draft-instructions-085-089.json'); async function main() { console.log('═══════════════════════════════════════════════════════════'); console.log(' ADD CULTURAL DNA INSTRUCTIONS (inst_085-089)'); console.log('═══════════════════════════════════════════════════════════\n'); // 1. Read existing instruction history console.log('📖 Reading instruction-history.json...'); const instructionHistory = JSON.parse(fs.readFileSync(INSTRUCTION_HISTORY_PATH, 'utf8')); console.log(` ✓ Current version: ${instructionHistory.version}`); console.log(` ✓ Total instructions: ${instructionHistory.instructions.length}`); console.log(` ✓ Active instructions: ${instructionHistory.instructions.filter(i => i.active).length}\n`); // 2. Read draft instructions console.log('📖 Reading draft-instructions-085-089.json...'); const draftData = JSON.parse(fs.readFileSync(DRAFT_INSTRUCTIONS_PATH, 'utf8')); const newInstructions = draftData.new_instructions; console.log(` ✓ Draft instructions: ${newInstructions.length}`); console.log(` ✓ IDs: ${newInstructions.map(i => i.id).join(', ')}\n`); // 3. Validate: Check if instructions already exist console.log('🔍 Validating instructions...'); for (const newInst of newInstructions) { const existing = instructionHistory.instructions.find(i => i.id === newInst.id); if (existing) { console.error(` ✗ ERROR: Instruction ${newInst.id} already exists!`); process.exit(1); } } console.log(' ✓ No duplicate IDs found\n'); // 4. Validate: Check schema compliance console.log('🔍 Validating schema compliance...'); for (const newInst of newInstructions) { const requiredFields = ['id', 'text', 'timestamp', 'quadrant', 'persistence', 'temporal_scope', 'verification_required', 'explicitness', 'source', 'session_id', 'parameters', 'active', 'notes', 'examples']; for (const field of requiredFields) { if (!(field in newInst)) { console.error(` ✗ ERROR: ${newInst.id} missing required field: ${field}`); process.exit(1); } } } console.log(' ✓ All instructions have required fields\n'); // 5. Add instructions console.log('➕ Adding instructions to history...'); instructionHistory.instructions.push(...newInstructions); console.log(` ✓ Added ${newInstructions.length} instructions`); console.log(` ✓ New total: ${instructionHistory.instructions.length}\n`); // 6. Update metadata console.log('📝 Updating metadata...'); const newVersion = incrementVersion(instructionHistory.version); instructionHistory.version = newVersion; instructionHistory.last_updated = new Date().toISOString(); console.log(` ✓ Version: ${instructionHistory.version} → ${newVersion}`); console.log(` ✓ Last updated: ${instructionHistory.last_updated}\n`); // 7. Recalculate stats console.log('📊 Recalculating stats...'); const active = instructionHistory.instructions.filter(i => i.active); const byQuadrant = {}; const byPersistence = {}; for (const inst of active) { byQuadrant[inst.quadrant] = (byQuadrant[inst.quadrant] || 0) + 1; byPersistence[inst.persistence] = (byPersistence[inst.persistence] || 0) + 1; } instructionHistory.stats = { total_instructions: instructionHistory.instructions.length, active_instructions: active.length, by_quadrant: byQuadrant, by_persistence: byPersistence }; console.log(` ✓ Total instructions: ${instructionHistory.stats.total_instructions}`); console.log(` ✓ Active instructions: ${instructionHistory.stats.active_instructions}`); console.log(` ✓ By quadrant:`, byQuadrant); console.log(` ✓ By persistence:`, byPersistence); console.log(''); // 8. Write updated file console.log('💾 Writing updated instruction-history.json...'); const updatedJson = JSON.stringify(instructionHistory, null, 2); fs.writeFileSync(INSTRUCTION_HISTORY_PATH, updatedJson, 'utf8'); console.log(' ✓ File written successfully\n'); // 9. Summary console.log('═══════════════════════════════════════════════════════════'); console.log(' SUCCESS: Cultural DNA Instructions Added'); console.log('═══════════════════════════════════════════════════════════\n'); console.log('📋 Added instructions:'); for (const inst of newInstructions) { console.log(` • ${inst.id}: ${inst.text.substring(0, 70)}...`); } console.log(''); console.log('📋 Next steps:'); console.log(' 1. Sync to MongoDB: node scripts/sync-instructions-to-db.js'); console.log(' 2. Verify in dashboard: http://localhost:9000/admin/audit-analytics.html'); console.log(' 3. Create pre-commit hook: Task 1.4'); console.log(''); } function incrementVersion(version) { const parts = version.split('.'); parts[1] = parseInt(parts[1]) + 1; return parts.join('.'); } main().catch(err => { console.error('\n❌ ERROR:', err.message); console.error(err.stack); process.exit(1); });