Spaces:
Sleeping
Sleeping
System Stability Patch: Fix trust proxy, chainable NeDB models, absolute upload paths, and sentinel stack safety
210d827 | const axios = require('axios'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| // Sentinel Log Buffer | |
| const sentinelLogs = []; | |
| const addLog = (msg, type = 'info') => { | |
| const logEntry = { | |
| id: Date.now() + Math.random().toString(36).substr(2, 5), | |
| time: new Date().toISOString(), | |
| message: msg, | |
| type: type | |
| }; | |
| sentinelLogs.push(logEntry); | |
| if (sentinelLogs.length > 100) sentinelLogs.shift(); // Keep last 100 | |
| console.log(`[SENTINEL] ${msg}`); | |
| }; | |
| exports.getLogs = () => sentinelLogs; | |
| exports.handleCommand = async (command) => { | |
| addLog(`Architect Signal Received: ${command}`, "info"); | |
| const prompt = `SYSTEM_COMMAND_INTERFACE: | |
| The Architect (Johan) is communicating with you, the NEURAL SENTINEL v12.5. | |
| RECENT_LOGS: | |
| ${JSON.stringify(sentinelLogs.slice(-5))} | |
| TASK: | |
| Respond as an elite, technical background service. Be concise, professional, and slightly futuristic. If asked about system status, refer to the logs. | |
| ARCHITECT_MESSAGE: ${command}`; | |
| try { | |
| const response = await axios.post( | |
| 'https://api.cerebras.ai/v1/chat/completions', | |
| { | |
| model: 'llama3.1-8b', | |
| messages: [{ role: 'user', content: prompt }], | |
| temperature: 0.7 | |
| }, | |
| { headers: { 'Authorization': `Bearer ${process.env.CEREBRAS_API_KEY}`, 'Content-Type': 'application/json' } } | |
| ); | |
| const reply = response.data.choices[0].message.content; | |
| addLog(reply, "milestone"); | |
| return reply; | |
| } catch (err) { | |
| addLog(`Communication Error: ${err.message}`, "error"); | |
| return "!! INTERLINK_FAILURE"; | |
| } | |
| }; | |
| addLog("Neural Sentinel v12.5 initialized. Monitoring for core breaches...", "milestone"); | |
| /** | |
| * NEURAL SENTINEL v12.5 [LITE] | |
| * Role: Autonomous Background Repair Unit | |
| */ | |
| exports.performSurgery = async (errorStack, originalUrl) => { | |
| addLog(`Breach detected in Node: ${originalUrl}`, "error"); | |
| // Identify the likely file from the error stack | |
| const stackLines = errorStack ? errorStack.split('\n') : []; | |
| if (stackLines.length < 2) { | |
| addLog("Stack trace too short to identify faulty module.", "warning"); | |
| return; | |
| } | |
| const fileMatch = stackLines[1].match(/at (.*):(\d+):(\d+)/) || stackLines[1].match(/\((.*):(\d+):(\d+)\)/); | |
| if (!fileMatch) { | |
| addLog("Could not localize breach in stack trace.", "warning"); | |
| return; | |
| } | |
| let filePath = fileMatch[1]; | |
| // Normalization for Render/Production paths | |
| if (!fs.existsSync(filePath)) { | |
| const baseName = path.basename(filePath); | |
| const potentialPaths = [ | |
| path.join(__dirname, '..', baseName), | |
| path.join(__dirname, '..', 'controllers', baseName), | |
| path.join(__dirname, '..', 'services', baseName) | |
| ]; | |
| filePath = potentialPaths.find(p => fs.existsSync(p)) || filePath; | |
| } | |
| if (!fs.existsSync(filePath) || filePath.includes('node_modules')) { | |
| addLog(`Breach file unreachable: ${path.basename(filePath)}`, "warning"); | |
| return; | |
| } | |
| addLog(`Isolating faulty module: ${path.basename(filePath)}`, "info"); | |
| const code = fs.readFileSync(filePath, 'utf8'); | |
| const prompt = `SYSTEM_FAILURE_LOG: | |
| URL: ${originalUrl} | |
| ERROR: ${errorStack} | |
| OFFENDING_FILE_CODE: | |
| ${code} | |
| TASK: | |
| Identify the logic error. Return ONLY the complete, fixed code for the file. | |
| No explanations. No markdown. Just raw code.`; | |
| try { | |
| addLog("Initiating high-speed neural diagnostic...", "info"); | |
| const response = await axios.post( | |
| 'https://api.cerebras.ai/v1/chat/completions', | |
| { | |
| model: 'llama3.1-8b', | |
| messages: [{ role: 'user', content: prompt }], | |
| temperature: 0 | |
| }, | |
| { headers: { 'Authorization': `Bearer ${process.env.CEREBRAS_API_KEY}`, 'Content-Type': 'application/json' } } | |
| ); | |
| let fixedCode = response.data.choices[0].message.content; | |
| fixedCode = fixedCode.replace(/```javascript/g, '').replace(/```/g, '').trim(); | |
| if (fixedCode && fixedCode.length > 10) { | |
| fs.writeFileSync(`${filePath}.bak`, code); | |
| fs.writeFileSync(filePath, fixedCode); | |
| addLog(`Surgery Successful. Neural patch deployed to: ${path.basename(filePath)}`, "milestone"); | |
| } | |
| } catch (err) { | |
| addLog(`Diagnostic Failed: ${err.message}`, "error"); | |
| } | |
| }; | |