ruvnet/RuView · error · Error

brain corpus exceeds 1000 records

Error message

brain corpus exceeds 1000 records

What it means

Raised at auth.py:258 when 'scheme, token = authorization.split()' raises ValueError, which happens whenever the header does not split into exactly two whitespace-separated parts: only a scheme with no token ('Bearer'), or extra segments ('Bearer a b', 'Bearer token extra').

Source

Thrown at harness/homecore/src/brain.js:88

export function loadBrain(path = CORPUS_PATH) {
  const raw = readFileSync(path, 'utf8').replace(/\r\n/g, '\n');
  if (Buffer.byteLength(raw) > 1_048_576) throw new Error('brain corpus exceeds 1 MiB');
  const records = raw.split('\n').filter(Boolean).map((line, index) => {
    if (Buffer.byteLength(line) > 16_384) {
      throw new Error(`brain line ${index + 1}: exceeds 16 KiB`);
    }
    let record;
    try {
      record = JSON.parse(line);
    } catch (error) {
      throw new Error(`brain line ${index + 1}: ${error.message}`);
    }
    const errors = validateBrainRecord(record, { canonical: true });
    if (errors.length) throw new Error(`brain line ${index + 1}: ${errors.join('; ')}`);
    return Object.freeze(record);
  });
  if (records.length > 1000) throw new Error('brain corpus exceeds 1000 records');
  const ids = new Set();
  for (const record of records) {
    if (ids.has(record.id)) throw new Error(`duplicate brain id: ${record.id}`);
    ids.add(record.id);
  }
  return { records, digest: sha256(raw), bytes: Buffer.byteLength(raw) };
}

function terms(value) {
  return new Set(String(value).toLowerCase().match(/[a-z0-9][a-z0-9_-]{1,}/g) || []);
}

export function searchBrain(query, { limit = 8, path = CORPUS_PATH } = {}) {
  const wanted = terms(query);
  if (!wanted.size) return [];
  const { records, digest } = loadBrain(path);
  return records.map((record) => {
    const title = terms(record.title);

View on GitHub (pinned to 4685618388)

Solutions

  1. Ensure the header is exactly two parts: one scheme word, one whitespace-free JWT: 'Authorization: Bearer <token>'
  2. Re-copy the JWT without line breaks and verify it has no internal spaces/newlines
  3. Strip the token value before building the header: f"Bearer {token.strip()}"

Example fix

# before
headers = {"Authorization": f"Bearer {token} extra"}
# after
headers = {"Authorization": f"Bearer {token.strip()}"}
Defensive patterns

Strategy: validation

Validate before calling

def wellformed_header(token: str) -> str:
    """Build a header the split()-based parser accepts: exactly two parts."""
    token = token.strip()
    assert token and " " not in token and "\n" not in token, "token contains whitespace"
    return f"Bearer {token}"

Type guard

def parses_as_bearer_pair(header_value: str) -> bool:
    try:
        scheme, token = header_value.split()
    except ValueError:
        return False
    return bool(scheme) and bool(token)

Try / catch

try:
    await middleware._authenticate_request(request)
except AuthenticationError as e:
    if str(e) == "Invalid authorization header format":
        return json_response({"error": "header must be 'Bearer <token>'"}, 401)
    raise

Prevention

When it happens

Trigger: Sending 'Authorization: Bearer' with no JWT; a token containing embedded whitespace (from copy-paste line wrapping); clients appending a trailing parameter like 'Bearer abc, charset=UTF-8'; multiple spaces are fine for split() but three tokens are not.

Common situations: Copy-pasting a JWT that has been wrapped across lines in a terminal/chat; hand-built header string concatenation bugs; scripts echoing tokens with appended newline handled elsewhere but trailing junk here; OAuth-style 'Bearer token, extra' syntax.

Related errors


AI-assisted analysis of ruvnet/RuView@4685618388 (2026-08-16). Data as JSON: /api/errors/66d365ae2d288be0. Report an issue: GitHub.