{"record":{"id":"9fafd15d6dc3d86e","repo":"Hmbown/CodeWhale","slug":"maxevents-must-be-in-1-250000","errorCode":null,"errorMessage":"maxEvents must be in [1, 250000].","messagePattern":"maxEvents must be in \\[1, 250000\\]\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/src/core/ingest.ts","lineNumber":222,"sourceCode":"    events.push(e);\n    for (const [i, record] of list(s.events).entries()) {\n      const ea = attributes(record.attributes), time = Number(ns(record.timeUnixNano, 'event.timeUnixNano') - origin) / 1e6;\n      const ename = String(record.name ?? 'span event');\n      events.push({ schemaVersion: 1, id: `${s.spanId}/event/${i}`, traceId: s.traceId, parentId: s.spanId,\n        startTime: time, endTime: time, agentId: e.agentId, name: ename, category: categoryFor(ename, ea),\n        status: ename === 'exception' ? 'error' : 'unknown', attributes: ea, raw: { event: record, spanId: s.spanId, resource: rec.resource, scope: rec.scope } });\n    }\n    if (events.length > maxEvents) throw new Error(`Import exceeds the ${maxEvents.toLocaleString()} event limit, including span events.`);\n  }\n  return { events, origins: new Map([...bases].map(([k, v]) => [k, v.toString()])), warnings };\n}\n\n/** Parse strictly: malformed lines or duplicate identities never disappear silently. */\nexport function importTrace(text: string, filename = 'Imported trace', options: ImportOptions = {}): Trace[] {\n  const mode = options.privacy ?? 'redact', maxEvents = options.maxEvents ?? 250_000;\n  if (!['redact', 'metadata', 'retain'].includes(mode)) throw new Error('Unknown privacy mode.');\n  const maxTraces = options.maxTraces ?? 8;\n  if (!Number.isInteger(maxEvents) || maxEvents < 1 || maxEvents > 250_000) throw new Error('maxEvents must be in [1, 250000].');\n  if (!Number.isInteger(maxTraces) || maxTraces < 1 || maxTraces > 64) throw new Error('maxTraces must be in [1, 64].');\n  if (new TextEncoder().encode(text).length > (options.maxBytes ?? 64 * 1024 * 1024)) throw new Error('File exceeds the 64 MiB MVP import limit. Split the export by trace.');\n  const trimmed = text.replace(/^\\uFEFF/, '').trim();\n  if (!trimmed) throw new Error('The trace file is empty.');\n  let document: unknown;\n  try { document = JSON.parse(trimmed); }\n  catch {\n    document = trimmed.split(/\\r?\\n/).filter(l => l.trim()).map((line, i) => {\n      try { return JSON.parse(line); } catch { throw new Error(`Invalid JSON on nonempty line ${i + 1}. Import cancelled; no rows were skipped.`); }\n    });\n  }\n  // Transform before both normalization and raw retention, so raw cannot bypass redaction.\n  const safe = mode === 'retain' ? (options.redactor ? options.redactor(document, '') : document) : redact(document, '', options.redactor);\n  const root = obj(safe);\n  if(root.format === 'whalesong.evidence/v1') return [evidenceToTrace(validateBundle(root, Math.min(maxEvents, 100_000)))];\n  if (isCodewhaleSession(safe)) {\n    const trace = fromCodewhaleSession(safe, filename, maxEvents);\n    trace.privacy = mode;","sourceCodeStart":204,"sourceCodeEnd":240,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/src/core/ingest.ts#L204-L240","documentation":"importTrace bounds options.maxEvents to an integer in [1, 250000]. Values that are non-integers, zero, negative, or above 250000 would either make the import meaningless or exceed the designed memory budget, so they are rejected before parsing.","triggerScenarios":"Calling importTrace with options.maxEvents = 0, -1, 1e9, 250000.5, NaN, or a numeric string like '1000' (fails Number.isInteger).","commonSituations":"Reading the limit from an env var or CLI flag without parsing to an integer; unit confusion (events vs bytes); copy-pasted values from another tool with a different cap.","solutions":["Pass an integer between 1 and 250000, e.g. maxEvents: 50000.","Coerce and clamp user input: maxEvents: Math.min(250000, Math.max(1, Math.trunc(Number(raw)))).","Omit maxEvents to use the default 250000."],"exampleFix":"// before\nimportTrace(text, 't.json', { maxEvents: Number(process.env.MAX_EVENTS) });\n// after\nconst n = Math.trunc(Number(process.env.MAX_EVENTS));\nimportTrace(text, 't.json', { maxEvents: Math.min(250000, Math.max(1, n || 250000)) });","handlingStrategy":"validation","validationCode":"function clampMaxEvents(raw) {\n  const n = Math.trunc(Number(raw));\n  if (!Number.isInteger(n) || n < 1 || n > 250000) throw new Error(`maxEvents must be an integer in [1, 250000], got ${raw}`);\n  return n;\n}","typeGuard":null,"tryCatchPattern":"try {\n  traces = importTrace(text, file, { maxEvents: opts.maxEvents });\n} catch (e) {\n  if (e instanceof Error && e.message.includes('maxEvents must be in [1, 250000]')) {\n    console.error('Coerce numeric strings via Number() and clamp to [1, 250000]');\n  } else throw e;\n}","preventionTips":["Parse limits from env/CLI with Number()/parseInt and validate with Number.isInteger.","Clamp user-configured values into [1, 250000] at the config boundary.","Omit the option to accept the 250000 default."],"tags":["config","validation","import-options"],"backgroundTag":"value-out-of-range","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}