#!/usr/bin/env node /** * Instruction Database Optimization * * Implements optimization recommendations: * 1. Consolidate inst_024 series (5 → 1) * 2. Archive obsolete PROJECT-scoped instructions * 3. Archive/promote MEDIUM/LOW persistence instructions */ const fs = require('fs'); const path = require('path'); const INSTRUCTION_HISTORY_PATH = path.join(__dirname, '../.claude/instruction-history.json'); const BACKUP_PATH = path.join(__dirname, '../.claude/instruction-history.json.backup'); // Create backup console.log('Creating backup...'); fs.copyFileSync(INSTRUCTION_HISTORY_PATH, BACKUP_PATH); console.log(`✓ Backup created: ${BACKUP_PATH}`); // Load instruction history const history = JSON.parse(fs.readFileSync(INSTRUCTION_HISTORY_PATH, 'utf8')); let modified = 0; console.log('\n=== OPTIMIZATION 1: Consolidate inst_024 Series ===\n'); // Find inst_024 series const inst_024_series = ['inst_024a', 'inst_024b', 'inst_024c', 'inst_024d', 'inst_024e']; // Create consolidated inst_024 const inst_024_consolidated = { "id": "inst_024_CONSOLIDATED", "text": "Session handoff/closedown procedure (executed in order): (1) Kill background processes, (2) Verify instruction history contains all session changes, (3) Document complete git status (modified files, branch state, pending commits), (4) Clean temporary artifacts, (5) Create handoff document OPTIMAL_STARTUP_PROMPT_.md with: system status, completed tasks with file:line references, in-progress tasks with blockers, pending tasks prioritized, instruction changes, known issues, framework health, user decisions needed, TodoWrite JSON, startup sequence. STOP ALL WORK after creating handoff (signals NEW session intent).", "timestamp": new Date().toISOString(), "quadrant": "OPERATIONAL", "persistence": "HIGH", "temporal_scope": "PERMANENT", "verification_required": "MANDATORY", "explicitness": 0.95, "source": "consolidation", "session_id": "2025-10-23-framework-optimization", "parameters": { "verification_required": "MANDATORY", "procedure_steps": 5, "replaces": inst_024_series }, "active": true, "notes": "Consolidated from inst_024a-e (5 instructions). Combines all session handoff steps into single comprehensive procedure. Created during framework optimization 2025-10-23.", "created_date": "2025-10-23", "part_of": "session_closedown_procedure" }; // Deactivate inst_024 series inst_024_series.forEach(id => { const inst = history.instructions.find(i => i.id === id); if (inst && inst.active) { inst.active = false; inst.archived_date = new Date().toISOString(); inst.archived_reason = "Consolidated into inst_024_CONSOLIDATED"; console.log(`✓ Archived ${id}`); modified++; } }); // Add consolidated instruction history.instructions.push(inst_024_consolidated); console.log(`✓ Created inst_024_CONSOLIDATED`); console.log(` Savings: 4 instructions (5 → 1)`); console.log('\n=== OPTIMIZATION 2: Archive Obsolete PROJECT-Scoped ===\n'); // Instructions to archive (identified as obsolete or redundant) const toArchive = [ { id: 'inst_051', reason: 'Replaced by inst_075 (token checkpoint monitoring with automated script)' }, { id: 'inst_060', reason: 'LOW persistence, sed-specific, covered by general Edit tool usage' }, { id: 'inst_059', reason: 'MEDIUM persistence, Write hook behavior is now standard' }, { id: 'inst_011', reason: 'MEDIUM persistence, UI documentation distinction already established' }, { id: 'inst_021', reason: 'MEDIUM persistence, standard development practice, not framework-specific' } ]; toArchive.forEach(({ id, reason }) => { const inst = history.instructions.find(i => i.id === id); if (inst && inst.active) { inst.active = false; inst.archived_date = new Date().toISOString(); inst.archived_reason = reason; console.log(`✓ Archived ${id}`); console.log(` Reason: ${reason}`); modified++; } }); console.log(`\n Savings: ${toArchive.length} instructions`); // Update metadata history.last_updated = new Date().toISOString(); history.version = "3.8"; // Increment version history.optimization_date = new Date().toISOString(); // Write back fs.writeFileSync(INSTRUCTION_HISTORY_PATH, JSON.stringify(history, null, 2)); console.log('\n=== OPTIMIZATION COMPLETE ===\n'); const activeCount = history.instructions.filter(i => i.active).length; const inactiveCount = history.instructions.filter(i => !i.active).length; console.log(`Total instructions: ${history.instructions.length}`); console.log(`Active instructions: ${activeCount}`); console.log(`Inactive instructions: ${inactiveCount}`); console.log(`Total modifications: ${modified + 1}`); // +1 for consolidated instruction if (activeCount < 50) { console.log(`\n✓ TARGET ACHIEVED: ${activeCount} < 50 active instructions!`); } else { console.log(`\n⚠ Target not quite reached: ${activeCount} (goal: <50)`); } console.log(`\nBackup available at: ${BACKUP_PATH}`); console.log(`To restore: cp ${BACKUP_PATH} ${INSTRUCTION_HISTORY_PATH}`);