{"record":{"id":"965615e5b69f0016","repo":"ruvnet/RuView","slug":"brain-line-index-1-exceeds-16-kib","errorCode":null,"errorMessage":"brain line ${index + 1}: exceeds 16 KiB","messagePattern":"brain line (.+?): exceeds 16 KiB","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"harness/homecore/src/brain.js","lineNumber":76,"sourceCode":"  }\n  if (!Array.isArray(record.tags) || record.tags.some((tag) => typeof tag !== 'string')) {\n    errors.push('tags must be strings');\n  }\n  if ((record.content || '').length > 8192) errors.push('content exceeds 8192 characters');\n  if ((record.title || '').length > 200) errors.push('title exceeds 200 characters');\n  if (canonical && record.reviewed !== true) errors.push('canonical records must be reviewed');\n  const combined = `${record.title || ''}\\n${record.content || ''}`;\n  if (SECRET.test(combined)) errors.push('record appears to contain a secret');\n  if (INJECTION.test(combined)) errors.push('record contains instruction-like prompt injection');\n  return errors;\n}\n\nexport function loadBrain(path = CORPUS_PATH) {\n  const raw = readFileSync(path, 'utf8').replace(/\\r\\n/g, '\\n');\n  if (Buffer.byteLength(raw) > 1_048_576) throw new Error('brain corpus exceeds 1 MiB');\n  const records = raw.split('\\n').filter(Boolean).map((line, index) => {\n    if (Buffer.byteLength(line) > 16_384) {\n      throw new Error(`brain line ${index + 1}: exceeds 16 KiB`);\n    }\n    let record;\n    try {\n      record = JSON.parse(line);\n    } catch (error) {\n      throw new Error(`brain line ${index + 1}: ${error.message}`);\n    }\n    const errors = validateBrainRecord(record, { canonical: true });\n    if (errors.length) throw new Error(`brain line ${index + 1}: ${errors.join('; ')}`);\n    return Object.freeze(record);\n  });\n  if (records.length > 1000) throw new Error('brain corpus exceeds 1000 records');\n  const ids = new Set();\n  for (const record of records) {\n    if (ids.has(record.id)) throw new Error(`duplicate brain id: ${record.id}`);\n    ids.add(record.id);\n  }\n  return { records, digest: sha256(raw), bytes: Buffer.byteLength(raw) };","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/ruvnet/RuView/blob/4685618388a5e49fad5b3005806f3bdd6a7c25c3/harness/homecore/src/brain.js#L58-L94","documentation":"The AuthenticationMiddleware.dispatch catch-all (auth.py:215) converts any unexpected exception raised during request authentication into HTTP 500 'Authentication service error'. The original exception is logged as 'Authentication middleware error: {e}' but never surfaced to the client. It fires only for exceptions that are neither AuthenticationError nor AuthorizationError.","triggerScenarios":"A bug inside _authenticate_request's non-auth code paths (e.g. a malformed user record making user['roles'] raise KeyError); failure while instantiating dependencies during middleware init; an ImportError in the lazy 'from src.api.middleware.auth import token_blacklist' import inside verify_token blowing up as a generic exception.","commonSituations":"Deploying where src.api.middleware.auth (the blacklist module) is missing or shadowed; corrupted/partial user records (missing 'roles' key) hitting _add_auth_headers; upgrading a dependency that changes an internal API used by the middleware.","solutions":["Read the server log and find the 'Authentication middleware error: ...' line - fix that root cause, not the 500 itself","Verify the src.api.middleware.auth module (token_blacklist) imports cleanly: python -c \"from src.api.middleware.auth import token_blacklist\"","Ensure user records always contain username/email/roles/is_active keys so the user-info dict construction cannot KeyError","Add a regression test covering the failing request path once the cause is known"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import importlib\n\ndef auth_dependencies_importable() -> bool:\n    \"\"\"The lazy blacklist import inside verify_token must resolve.\"\"\"\n    try:\n        importlib.import_module(\"src.api.middleware.auth\")\n        return True\n    except ImportError:\n        return False","typeGuard":null,"tryCatchPattern":"# Server-side: this 500 means a bug, not a client problem.\ntry:\n    response = client.get(\"/api/x\", headers=auth_headers)\nexcept HTTPError:\n    if response.status_code == 500 and response.json()[\"detail\"] == \"Authentication service error\":\n        check_server_log_for(\"Authentication middleware error\")\n    raise","preventionTips":["Smoke-test the full auth path (login -> protected call) after deployment","Keep user records schema-complete (username/email/roles/is_active)","Never ignore the logged root cause behind the generic 500"],"tags":["auth","middleware","http-500","fastapi"],"backgroundTag":null,"analyzedSha":"4685618388a5e49fad5b3005806f3bdd6a7c25c3","analyzedAt":"2026-08-16T06:09:40.886Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}