#!/usr/bin/env node /** * Import 5 Missing Archives Documents * * Imports the 5 MD files that are in Archives but not in the database: * 1. Case Studies - Real-World LLM Failure Modes * 2. Python API Integration Examples * 3. Tractatus Framework Enforcement for Claude Code * 4. Concurrent Session Architecture Limitations * 5. Rule Proliferation and Transactional Overhead */ require('dotenv').config(); const fs = require('fs').promises; const path = require('path'); const { connect, close } = require('../src/utils/db.util'); const Document = require('../src/models/Document.model'); const { markdownToHtml, extractTOC, generateSlug } = require('../src/utils/markdown.util'); const FILES_TO_IMPORT = [ { path: '/home/theflow/projects/tractatus/docs/markdown/case-studies.md', slug: 'case-studies-real-world-llm-failure-modes-appendix', title: 'Appendix B: Case Studies - Real-World LLM Failure Modes', category: 'archives', order: 2, archiveNote: 'Internal project tracking document. Not relevant for public documentation.' }, { path: '/home/theflow/projects/tractatus/docs/api/examples-python.md', slug: 'implementation-guide-python-examples', title: 'Implementation Guide: Python Code Examples', category: 'archives', order: 3, archiveNote: 'Internal project tracking document. Not relevant for public documentation.' }, { path: '/home/theflow/projects/tractatus/docs/claude-code-framework-enforcement.md', slug: 'tractatus-framework-enforcement-claude-code', title: 'Tractatus Framework Enforcement for Claude Code', category: 'archives', order: 4, archiveNote: 'Development tool documentation. See Implementation Guide for production deployment.' }, { path: '/home/theflow/projects/tractatus/docs/research/concurrent-session-architecture-limitations.md', slug: 'research-topic-concurrent-session-architecture', title: 'Research Topic: Concurrent Session Architecture Limitations in Claude Code Governance', category: 'archives', order: 5, archiveNote: 'Research analysis. See Architectural Overview for current framework status.' }, { path: '/home/theflow/projects/tractatus/docs/research/rule-proliferation-and-transactional-overhead.md', slug: 'research-topic-rule-proliferation-transactional-overhead', title: 'Research Topic: Rule Proliferation and Transactional Overhead in AI Governance', category: 'archives', order: 6, archiveNote: 'Research analysis. See Architectural Overview for current framework status.' } ]; function extractFrontMatter(content) { const frontMatterRegex = /^---\n([\s\S]*?)\n---\n([\s\S]*)$/; const match = content.match(frontMatterRegex); if (!match) { return { frontMatter: {}, content }; } const frontMatterText = match[1]; const remainingContent = match[2]; const frontMatter = {}; frontMatterText.split('\n').forEach(line => { const [key, ...valueParts] = line.split(':'); if (key && valueParts.length > 0) { const value = valueParts.join(':').trim(); frontMatter[key.trim()] = value.replace(/^["']|["']$/g, ''); } }); return { frontMatter, content: remainingContent }; } async function importDocument(fileInfo) { console.log(`\nšŸ“„ Importing: ${fileInfo.title}`); console.log(` File: ${fileInfo.path}`); try { // Read markdown file const rawContent = await fs.readFile(fileInfo.path, 'utf-8'); const { frontMatter, content } = extractFrontMatter(rawContent); // Convert to HTML const content_html = markdownToHtml(content); // Extract TOC const toc = extractTOC(content); // Check if document already exists const existing = await Document.findBySlug(fileInfo.slug); if (existing) { console.log(` āš ļø Document already exists: ${fileInfo.slug}`); console.log(` Skipping import.`); return { success: false, reason: 'exists' }; } // Create document const doc = await Document.create({ title: fileInfo.title, slug: fileInfo.slug, quadrant: frontMatter.quadrant || null, persistence: frontMatter.persistence || 'MEDIUM', audience: frontMatter.audience || 'general', visibility: 'archived', category: fileInfo.category, order: fileInfo.order, archiveNote: fileInfo.archiveNote, content_html, content_markdown: content, toc, metadata: { author: frontMatter.author || 'John Stroh', version: frontMatter.version || '1.0', document_code: frontMatter.identifier || null, tags: [], original_filename: path.basename(fileInfo.path), migrated_at: new Date() }, search_index: content.toLowerCase(), translations: {}, download_formats: {} }); console.log(` āœ… Imported successfully`); console.log(` ID: ${doc._id}`); console.log(` Slug: ${doc.slug}`); return { success: true, doc }; } catch (error) { console.error(` āŒ Error importing: ${error.message}`); return { success: false, error: error.message }; } } async function main() { try { console.log('šŸš€ Importing 5 Missing Archives Documents\n'); console.log('═══════════════════════════════════════════════════\n'); await connect(); let imported = 0; let skipped = 0; let failed = 0; for (const fileInfo of FILES_TO_IMPORT) { const result = await importDocument(fileInfo); if (result.success) { imported++; } else if (result.reason === 'exists') { skipped++; } else { failed++; } } console.log('\n═══════════════════════════════════════════════════'); console.log('\nšŸ“Š Import Summary:'); console.log(` āœ… Imported: ${imported}`); console.log(` ā­ļø Skipped (already exists): ${skipped}`); console.log(` āŒ Failed: ${failed}`); console.log(` šŸ“¦ Total: ${FILES_TO_IMPORT.length}`); await close(); } catch (error) { console.error('\nāŒ Fatal error:', error); process.exit(1); } } main();