{"record":{"id":"bca972e1d1011356","repo":"Hmbown/CodeWhale","slug":"signal-duration-must-be-finite-and-nonnegative-pet-native","errorCode":null,"errorMessage":"Signal duration must be finite and nonnegative.","messagePattern":"Signal duration must be finite and nonnegative\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/ios/Resources/pet-native.js","lineNumber":1318,"sourceCode":"exports.autocorrelation = autocorrelation;\nexports.periodogram = periodogram;\nexports.unionDuration = unionDuration;\nconst model_js_1 = require(\"./model.js\");\nfunction emptyLevel(length, binMs) {\n    const n = model_js_1.CATEGORIES.length * length;\n    return { binMs, length, onsets: new Float64Array(n), activeMs: new Float64Array(n),\n        outputTokens: new Float64Array(n), cost: new Float64Array(n), errors: new Float64Array(n), peak: new Float64Array(n) };\n}\nconst FIELDS = ['onsets', 'activeMs', 'outputTokens', 'cost', 'errors'];\n/** O(events + channels × bins), including long intervals. No span-length inner loop. */\nfunction buildPyramid(events, requestedDuration, maxBins = 16_384) {\n    if (!Number.isInteger(maxBins) || maxBins < 16 || maxBins > 1_048_576)\n        throw new Error('maxBins must be an integer in [16, 1048576].');\n    let duration = requestedDuration ?? 1;\n    for (const e of events)\n        duration = Math.max(duration, e.endTime, e.startTime, e.status === 'error' ? (0, model_js_1.errorOnsetOf)(e) : 0);\n    if (!Number.isFinite(duration) || duration < 0)\n        throw new Error('Signal duration must be finite and nonnegative.');\n    duration = Math.max(1, duration);\n    const binMs = 2 ** Math.ceil(Math.log2(Math.max(1, duration / (maxBins - 1))));\n    const length = Math.floor(duration / binMs) + 1, fine = emptyLevel(length, binMs);\n    const stride = length + 1;\n    const activeDiff = new Float64Array(model_js_1.CATEGORIES.length * stride), tokenDiff = new Float64Array(model_js_1.CATEGORIES.length * stride);\n    for (const e of events) {\n        if (!Number.isFinite(e.startTime) || !Number.isFinite(e.endTime) || e.startTime < 0 || e.endTime < e.startTime)\n            throw new Error(`Invalid interval for ${e.id}.`);\n        const channel = model_js_1.CATEGORIES.indexOf(e.category);\n        if (channel < 0)\n            throw new Error(`Unknown category for ${e.id}.`);\n        const a = Math.floor(e.startTime / binMs), b = Math.floor(e.endTime / binMs);\n        const at = channel * length, diff = channel * stride;\n        fine.onsets[at + a]++;\n        fine.cost[at + a] += e.cost ?? 0;\n        if (e.status === 'error')\n            fine.errors[at + Math.floor((0, model_js_1.errorOnsetOf)(e) / binMs)]++;\n        const d = e.endTime - e.startTime, tokens = e.outputTokens ?? 0;","sourceCodeStart":1300,"sourceCodeEnd":1336,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/ios/Resources/pet-native.js#L1300-L1336","documentation":"buildPyramid derives its signal duration from the requestedDuration argument or the maximum endTime/startTime/error onset across events. If that resolved duration is not a finite nonnegative number (NaN/Infinity from bad event timestamps or a bad requestedDuration), the pyramid geometry cannot be computed and this error is thrown.","triggerScenarios":"Passing a requestedDuration that is NaN/Infinity/negative, or events with non-finite or negative endTime/startTime/errorOnset values that contaminate the Math.max scan.","commonSituations":"Open-ended events with endTime = Infinity, timestamps parsed from malformed strings, or computing requestedDuration from a subtraction that yields NaN.","solutions":["Ensure all event startTime/endTime values are finite nonnegative numbers (errorOnsetOf included for error-status events).","Pass a finite, nonnegative requestedDuration computed at the call site.","Sanitize events before the call: replace open-ended/Infinity endTimes with a concrete value like startTime + 1000.","Run events through importTrace, which validates the same invariants (see the event-v1 error)."],"exampleFix":"// before\nbuildPyramid(events, requestedDuration);\n// after\nconst safe = events.map(e => ({ ...e, endTime: Number.isFinite(e.endTime) ? e.endTime : e.startTime + 1000 }));\nbuildPyramid(safe, Math.max(0, requestedDuration ?? 1));","handlingStrategy":"validation","validationCode":"const finite = v => Number.isFinite(v) && v >= 0;\nif (requestedDuration !== undefined && !finite(requestedDuration)) throw new TypeError('bad duration');\nif (events.some(e => !finite(e.startTime) || !finite(e.endTime))) sanitizeEvents();","typeGuard":null,"tryCatchPattern":"try { buildPyramid(events, d, bins) } catch (err) { if (err.message.includes('Signal duration')) return buildPyramid(sanitize(events), d, bins); throw err; }","preventionTips":["Replace Infinity endTimes on open-ended events with concrete values","Validate timestamps when parsing traces, not at render time","Compute requestedDuration with Number.isFinite-checked math"],"tags":["validation","numeric-input","signal"],"backgroundTag":"invalid-argument-value","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T21:17:16.096Z"}