{"record":{"id":"7840ffddef8d46a9","repo":"Hmbown/CodeWhale","slug":"invalid-first-pet-bucket","errorCode":null,"errorMessage":"Invalid first pet bucket.","messagePattern":"Invalid first pet bucket\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/tui/pet_watch/pet-native.js","lineNumber":1173,"sourceCode":"            return;\n        return packet;\n    }\n}\nexports.PetLiveTape = PetLiveTape;\nconst order = (a, b) => a < b ? -1 : a > b ? 1 : 0;\nconst keyOf = (e) => JSON.stringify([e.traceId, e.id]);\nconst isContainer = (e) => e.attributes['whalesong.container'] === true\n    || e.attributes['codewhale.container'] === true;\n/** Compile a single trace. Unknown-duration spans provide onsets, not occupancy.\n * Updates of the same trace/id replace earlier snapshots rather than double count.\n * An endpoint onset gets its own bucket; intervals use [start, end). */\nfunction compilePetTelemetry(input, durationMs = 0, firstSequence = 0, originMs = 0) {\n    if (input.length > 250_000)\n        throw new Error('Pet input exceeds 250000 events.');\n    if (!Number.isFinite(durationMs) || durationMs < 0)\n        throw new Error('Invalid pet duration.');\n    if (!Number.isSafeInteger(firstSequence) || firstSequence < 0)\n        throw new Error('Invalid first pet bucket.');\n    if (!Number.isFinite(originMs))\n        throw new Error('Invalid pet clock origin.');\n    durationMs = Math.max(0, durationMs - originMs);\n    const unique = new Map();\n    const traces = new Set();\n    for (const e of input) {\n        if (e.schemaVersion !== 1 || !e.id || !e.traceId || !model_js_1.CATEGORIES.includes(e.category)\n            || !Number.isFinite(e.startTime) || !Number.isFinite(e.endTime)\n            || e.startTime < 0 || e.endTime < e.startTime || !e.attributes)\n            throw new Error('Invalid event-v1 pet input. Import through importTrace first.');\n        traces.add(e.traceId);\n        unique.set(keyOf(e), e);\n    }\n    if (traces.size > 1)\n        throw new Error('Select one trace for the pet.');\n    const parents = new Set([...unique.values()].filter(e => e.parentId).map(e => e.parentId));\n    const events = [...unique.values()].filter(e => !isContainer(e)\n        && !(e.category === 'orchestration' && parents.has(e.id)))","sourceCodeStart":1155,"sourceCodeEnd":1191,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tui/pet_watch/pet-native.js#L1155-L1191","documentation":"compilePetTelemetry() in pet-native.js validates its firstSequence parameter before compiling a pet replay timeline. firstSequence is the bin/sequence number the replay starts from, and must be a non-negative safe integer (whole milliseconds-based bucket index). The library throws this error when the caller passes a non-integer (fractional, NaN, Infinity), a negative number, or a value outside the safe-integer range.","triggerScenarios":"Calling compilePetTelemetry(input, durationMs, firstSequence) with firstSequence set to a negative number, a fractional value (e.g. computed via division without Math.floor), NaN/Infinity (e.g. from a failed lookup or JSON parse of 'null'), or a non-number (string from a query param).","commonSituations":"Restoring a checkpoint saved by a different version where the sequence field is null; computing a start bin as (timeMs / binMs) with timeMs undefined so the result is NaN; passing a user-supplied offset string from UI state without Number() conversion; integer overflow beyond Number.MAX_SAFE_INTEGER after multiplying a large timestamp.","solutions":["Compute firstSequence with Math.floor and clamp: firstSequence = Math.max(0, Math.floor(raw));","Validate before calling: Number.isSafeInteger(firstSequence) && firstSequence >= 0, defaulting to 0 when absent.","If restoring from a persisted checkpoint, coerce/repair the stored value (or drop it and restart from 0) instead of passing it raw.","Convert string/unknown inputs with Number() and reject non-numeric values before the call."],"exampleFix":"// before\ncompilePetTelemetry(events, durationMs, elapsedMs / PET_BIN_MS);\n// after\nconst firstSequence = Math.max(0, Math.floor(elapsedMs / PET_BIN_MS));\ncompilePetTelemetry(events, durationMs, firstSequence);","handlingStrategy":"validation","validationCode":"function isValidFirstSequence(n) {\n  return Number.isSafeInteger(n) && n >= 0;\n}\nif (!isValidFirstSequence(firstSequence)) firstSequence = 0;","typeGuard":"const isStartBin = (v) => typeof v === 'number' && Number.isSafeInteger(v) && v >= 0;","tryCatchPattern":"try {\n  compilePetTelemetry(events, durationMs, firstSequence);\n} catch (e) {\n  if (e.message === 'Invalid first pet bucket.') {\n    firstSequence = 0;\n    return compilePetTelemetry(events, durationMs, firstSequence);\n  }\n  throw e;\n}","preventionTips":["Always derive bin indices with Math.floor from a millisecond division","Default optional sequence params to 0 with ?? 0","Validate persisted checkpoints before reusing stored sequence numbers"],"tags":["parameter-validation","javascript"],"backgroundTag":"invalid-argument-value","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-22T16:17:23.217Z"}