tractatus/scripts/parse-and-update-safeguards-document.js
TheFlow 0e6be3eaf1 refactor: remove website code and fix critical startup crashes (Phase 8)
CRITICAL FIX: Server would CRASH ON STARTUP (multiple import errors)

REMOVED (2 scripts):
1. scripts/framework-watchdog.js
   - Monitored .claude/session-state.json (OUR Claude Code setup)
   - Monitored .claude/token-checkpoints.json (OUR file structure)
   - Implementers won't have our .claude/ directory

2. scripts/init-db.js
   - Created website collections: blog_posts, media_inquiries, case_submissions
   - Created website collections: resources, moderation_queue, users, citations
   - Created website collections: translations, koha_donations
   - Next steps referenced deleted scripts (npm run seed:admin)

REWRITTEN (2 files):

src/models/index.js (29 lines → 27 lines)
- REMOVED imports: Document, BlogPost, MediaInquiry, CaseSubmission, Resource
- REMOVED imports: ModerationQueue, User (all deleted in Phase 2)
- KEPT imports: AuditLog, DeliberationSession, GovernanceLog, GovernanceRule
- KEPT imports: Precedent, Project, SessionState, VariableValue, VerificationLog
- Result: Only framework models exported

src/server.js (284 lines → 163 lines, 43% reduction)
- REMOVED: Imports to deleted middleware (csrf-protection, response-sanitization)
- REMOVED: Stripe webhook handling (/api/koha/webhook)
- REMOVED: Static file caching (for deleted public/ directory)
- REMOVED: Static file serving (public/ deleted in Phase 6)
- REMOVED: CSRF token endpoint
- REMOVED: Website homepage with "auth, documents, blog, admin" references
- REMOVED: Instruction sync (scripts/sync-instructions-to-db.js reference)
- REMOVED: Hardcoded log path (${process.env.HOME}/var/log/tractatus/...)
- REMOVED: Website-specific security middleware
- KEPT: Security headers, rate limiting, CORS, body parsers
- KEPT: API routes, governance services, MongoDB connections
- RESULT: Clean framework-only server

RESULT: Repository can now start without crashes, all imports resolve

🤖 Generated with Claude Code
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-21 22:17:02 +13:00

107 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
/**
* Parse Architectural Safeguards Document into Sections
* Updates database with section metadata for card-based rendering
*/
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 { parseDocumentSections } = require('../src/utils/document-section-parser');
const { markdownToHtml } = require('../src/utils/markdown.util');
async function parseAndUpdateDocument() {
try {
console.log('\n=== Parsing Architectural Safeguards Document ===\n');
const mdPath = path.resolve('docs/research/ARCHITECTURAL-SAFEGUARDS-Against-LLM-Hierarchical-Dominance-Prose.md');
const slug = 'architectural-safeguards-against-llm-hierarchical-dominance-prose';
// Read markdown file
console.log('📄 Reading markdown file...');
const rawContent = await fs.readFile(mdPath, 'utf-8');
// Parse into sections
console.log('🔍 Parsing document into sections...');
const sections = parseDocumentSections(rawContent);
console.log(`✓ Found ${sections.length} sections`);
// Convert each section's markdown to HTML
console.log('🔄 Converting sections to HTML...');
sections.forEach(section => {
section.content_html = markdownToHtml(section.content);
});
console.log('✓ Converted all sections to HTML');
// Connect to database
await connect();
// Find existing document
console.log('📊 Finding document in database...');
const doc = await Document.findBySlug(slug);
if (!doc) {
console.error('❌ Error: Document not found in database');
await close();
process.exit(1);
}
console.log('✓ Found document');
// Update document with sections and move to getting-started
console.log('💾 Updating document with sections...');
const updated = await Document.update(doc._id, {
sections: sections,
category: 'getting-started',
order: 2 // Prominently placed in getting-started
});
if (!updated) {
console.error('❌ Error: Document update failed');
await close();
process.exit(1);
}
console.log('✓ Document updated successfully!');
console.log('\n📊 Document Details:');
console.log(` Title: ${doc.title}`);
console.log(` Slug: ${doc.slug}`);
console.log(` Category: getting-started (moved from research-theory)`);
console.log(` Order: 2`);
console.log(` Sections: ${sections.length}`);
console.log('\n📋 Section Breakdown:');
const categoryCounts = {};
sections.forEach(section => {
categoryCounts[section.category] = (categoryCounts[section.category] || 0) + 1;
});
Object.entries(categoryCounts).forEach(([category, count]) => {
console.log(` ${category}: ${count} sections`);
});
console.log('\n✅ Document now has card-based rendering enabled!');
console.log(` View at: https://agenticgovernance.digital/docs.html?doc=${slug}`);
await close();
} catch (error) {
console.error('\n❌ Update failed:', error.message);
console.error(error.stack);
process.exit(1);
}
}
// Run if called directly
if (require.main === module) {
parseAndUpdateDocument();
}
module.exports = parseAndUpdateDocument;