tractatus/scripts/update-document-metadata.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

149 lines
3.8 KiB
JavaScript

/**
* Update Document Metadata and Ordering
* Sets proper order, category, and audience for public documents
*/
const { MongoClient } = require('mongodb');
// MongoDB connection
const MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost:27017/tractatus_dev';
const DB_NAME = process.env.MONGODB_DB || 'tractatus_dev';
// Document metadata updates
const DOCUMENT_UPDATES = [
// GETTING STARTED (order: 1-3)
{
slug: 'architectural-overview-and-research-status',
order: 1,
category: 'reference',
audience: 'general',
visibility: 'public'
},
{
slug: 'core-concepts-of-the-tractatus-framework',
order: 2,
category: 'conceptual',
audience: 'general',
visibility: 'public'
},
{
slug: 'implementation-guide',
order: 3,
category: 'practical',
audience: 'technical',
visibility: 'public'
},
// FRAMEWORK DETAILS (order: 4-6)
{
slug: 'tractatus-ai-safety-framework-core-values-and-principles',
order: 4,
category: 'conceptual',
audience: 'general',
visibility: 'public'
},
{
slug: 'case-studies-real-world-llm-failure-modes',
order: 5,
category: 'practical',
audience: 'general',
visibility: 'public'
},
{
slug: 'ai-governance-business-case-template-tractatus-framework',
order: 6,
category: 'practical',
audience: 'business',
visibility: 'public'
},
// REFERENCE (order: 7)
{
slug: 'tractatus-agentic-governance-system-glossary-of-terms',
order: 7,
category: 'reference',
audience: 'general',
visibility: 'public'
}
];
async function main() {
console.log('=== Updating Document Metadata ===\n');
let client;
try {
// Connect to MongoDB
console.log('Connecting to MongoDB...');
client = await MongoClient.connect(MONGODB_URI);
const db = client.db(DB_NAME);
const collection = db.collection('documents');
console.log('✓ Connected\n');
let updated = 0;
let notFound = 0;
// Update each document
for (const doc of DOCUMENT_UPDATES) {
console.log(`Updating: ${doc.slug}`);
console.log(` Order: ${doc.order} | Category: ${doc.category} | Audience: ${doc.audience}`);
const result = await collection.updateOne(
{ slug: doc.slug },
{
$set: {
order: doc.order,
category: doc.category,
audience: doc.audience,
visibility: doc.visibility
}
}
);
if (result.matchedCount > 0) {
console.log(` ✓ Updated\n`);
updated++;
} else {
console.log(` ⚠ Not found in database\n`);
notFound++;
}
}
// Summary
console.log('=== Summary ===\n');
console.log(`✓ Updated: ${updated} documents`);
if (notFound > 0) {
console.log(`⚠ Not found: ${notFound} documents`);
}
console.log(`\nTotal processed: ${DOCUMENT_UPDATES.length}`);
// Verify organization
console.log('\n=== Verification ===\n');
const publicDocs = await collection.find({ visibility: 'public' })
.sort({ order: 1 })
.project({ title: 1, order: 1, category: 1, audience: 1 })
.toArray();
console.log('Public documents (sorted by order):');
publicDocs.forEach(doc => {
console.log(` ${doc.order}. ${doc.title}`);
console.log(` Category: ${doc.category} | Audience: ${doc.audience}\n`);
});
const archivedCount = await collection.countDocuments({ visibility: 'archived' });
console.log(`\n📦 Archived: ${archivedCount} documents`);
console.log(`📖 Public: ${publicDocs.length} documents`);
} catch (error) {
console.error('\n✗ Error:', error.message);
console.error(error.stack);
process.exit(1);
} finally {
if (client) await client.close();
}
}
// Run if called directly
if (require.main === module) {
main();
}
module.exports = { main };