tractatus/scripts/add-sections-to-17-docs.js
TheFlow 2af47035ac 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

139 lines
5.6 KiB
JavaScript

#!/usr/bin/env node
/**
* Add Card View Sections to 17 Documents
*
* Adds card view sections to:
* - 5 newly imported archives
* - 12 existing documents without sections
*/
require('dotenv').config();
const fs = require('fs');
const path = require('path');
const { connect, close } = require('../src/utils/db.util');
const Document = require('../src/models/Document.model');
const { processMarkdownFile } = require('./generate-card-sections.js');
const DOCS_TO_PROCESS = [
// 5 newly imported archives
{ slug: 'case-studies-real-world-llm-failure-modes-appendix', mdPath: 'docs/markdown/case-studies.md' },
{ slug: 'implementation-guide-python-examples', mdPath: 'docs/api/examples-python.md' },
{ slug: 'tractatus-framework-enforcement-claude-code', mdPath: 'docs/claude-code-framework-enforcement.md' },
{ slug: 'research-topic-concurrent-session-architecture', mdPath: 'docs/research/concurrent-session-architecture-limitations.md' },
{ slug: 'research-topic-rule-proliferation-transactional-overhead', mdPath: 'docs/research/rule-proliferation-and-transactional-overhead.md' },
// 5 technical reference docs without sections
{ slug: 'implementation-roadmap-24-month-deployment-plan', mdPath: 'docs/markdown/implementation-roadmap-24-month-deployment-plan.md' },
{ slug: 'api-reference-complete', mdPath: 'docs/markdown/api-reference-complete.md' },
{ slug: 'api-javascript-examples', mdPath: 'docs/api/examples-javascript.md' },
{ slug: 'api-python-examples', mdPath: 'docs/api/examples-python.md' },
{ slug: 'openapi-specification', mdPath: 'docs/markdown/openapi-specification.md' },
// 5 case studies without sections
{ slug: 'the-27027-incident-a-case-study-in-pattern-recognition-bias', mdPath: 'docs/case-studies/27027-incident-detailed-analysis.md' },
{ slug: 'when-frameworks-fail-and-why-thats-ok', mdPath: 'docs/case-studies/when-frameworks-fail-oct-2025.md' },
{ slug: 'our-framework-in-action-detecting-and-correcting-ai-fabrications', mdPath: 'docs/case-studies/framework-in-action-oct-2025.md' },
{ slug: 'real-world-ai-governance-a-case-study-in-framework-failure-and-recovery', mdPath: 'docs/case-studies/real-world-governance-case-study-oct-2025.md' },
{ slug: 'case-studies-real-world-llm-failure-modes', mdPath: 'docs/markdown/case-studies.md' },
// 2 Phase 5 PoC summaries
{ slug: 'phase-5-poc-session-1-summary', mdPath: 'docs/markdown/phase-5-session1-summary.md' },
{ slug: 'phase-5-poc-session-2-summary', mdPath: 'docs/markdown/phase-5-session2-summary.md' }
];
async function addSectionsToDocument(docInfo) {
console.log(`\n📄 Processing: ${docInfo.slug}`);
try {
// Check if document exists
const doc = await Document.findBySlug(docInfo.slug);
if (!doc) {
console.log(` ❌ Document not found in database`);
return { success: false, reason: 'not_found' };
}
// Check if already has sections
if (doc.sections && doc.sections.length > 0) {
console.log(` ⏭️ Already has ${doc.sections.length} sections, skipping`);
return { success: false, reason: 'has_sections' };
}
// Build full path to markdown file
const fullPath = path.join('/home/theflow/projects/tractatus', docInfo.mdPath);
// Check if markdown file exists
if (!fs.existsSync(fullPath)) {
console.log(` ❌ Markdown file not found: ${fullPath}`);
return { success: false, reason: 'md_not_found' };
}
// Generate sections
console.log(` 📝 Generating sections from: ${docInfo.mdPath}`);
const sections = await processMarkdownFile(fullPath);
if (!sections || sections.length === 0) {
console.log(` ⚠️ No sections generated (possibly no H2 headers)`);
return { success: false, reason: 'no_sections' };
}
// Update document with sections
const updated = await Document.update(doc._id.toString(), { sections });
if (!updated) {
console.log(` ❌ Failed to update document`);
return { success: false, reason: 'update_failed' };
}
console.log(` ✅ Added ${sections.length} sections`);
return { success: true, sections: sections.length };
} catch (error) {
console.error(` ❌ Error: ${error.message}`);
return { success: false, error: error.message };
}
}
async function main() {
try {
console.log('🚀 Adding Card View Sections to 17 Documents\n');
console.log('═══════════════════════════════════════════════════\n');
await connect();
let added = 0;
let skipped = 0;
let notFound = 0;
let failed = 0;
for (const docInfo of DOCS_TO_PROCESS) {
const result = await addSectionsToDocument(docInfo);
if (result.success) {
added++;
} else if (result.reason === 'has_sections') {
skipped++;
} else if (result.reason === 'not_found' || result.reason === 'md_not_found') {
notFound++;
} else {
failed++;
}
}
console.log('\n═══════════════════════════════════════════════════');
console.log('\n📊 Summary:');
console.log(` ✅ Added sections: ${added}`);
console.log(` ⏭️ Skipped (already have sections): ${skipped}`);
console.log(` ❌ Not found: ${notFound}`);
console.log(` ❌ Failed: ${failed}`);
console.log(` 📦 Total processed: ${DOCS_TO_PROCESS.length}`);
await close();
} catch (error) {
console.error('\n❌ Fatal error:', error);
process.exit(1);
}
}
main();