{"record":{"id":"0a9e9ac7f420399e","repo":"Hmbown/CodeWhale","slug":"invalid-event-v1-pet-input-import-through-importtrace-first","errorCode":null,"errorMessage":"Invalid event-v1 pet input. Import through importTrace first.","messagePattern":"Invalid event-v1 pet input\\. Import through importTrace first\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/tui/pet_watch/pet-native.js","lineNumber":1183,"sourceCode":" * 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)))\n        .map(e => ({ ...e, startTime: e.startTime - originMs, endTime: (e.openEnded ? e.startTime : e.endTime) - originMs,\n        attributes: e.attributes['whalesong.error_onset_ms'] === undefined ? e.attributes\n            : { ...e.attributes, 'whalesong.error_onset_ms': (0, model_js_1.errorOnsetOf)(e) - originMs } }))\n        .sort((a, b) => a.startTime - b.startTime || order(a.id, b.id));\n    let lastOnset = 0;\n    for (const e of events) {\n        durationMs = Math.max(durationMs, e.endTime);\n        lastOnset = Math.max(lastOnset, e.startTime);\n    }\n    const failures = events.filter(e => e.category === 'error' || e.status === 'error').map(model_js_1.errorOnsetOf).sort((a, b) => a - b);","sourceCodeStart":1165,"sourceCodeEnd":1201,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tui/pet_watch/pet-native.js#L1165-L1201","documentation":"compilePetTelemetry() requires its input to already be normalized event-v1 objects (schemaVersion === 1 with id, traceId, a known category, finite non-negative start/end times, and attributes). The library throws this error when any event in the input array fails that shape check, telling the caller to run the raw data through importTrace() first, which produces valid event-v1 records.","triggerScenarios":"Passing raw spans/trace rows (from an OTLP export, a JSON log, or a hand-built object) directly to compilePetTelemetry without importTrace; events missing attributes; endTime earlier than startTime; negative timestamps; category strings not in CATEGORIES (typos like 'orchstration').","commonSituations":"Skipping the documented importTrace step when wiring a custom data source; a producer upgraded its export schema (schemaVersion 2+) so old events fail the v1 check; deserialization dropping the attributes field; clock skew producing endTime < startTime.","solutions":["Call importTrace() on the raw data first and pass its output to compilePetTelemetry.","Log/inspect the offending event (id, category, startTime, endTime, schemaVersion) to find which field violates the contract.","Fix the producer so events carry schemaVersion 1, valid category strings, finite non-negative times with endTime >= startTime, and a non-null attributes object.","If events come from an older schema, migrate/normalize them to event-v1 before compilation."],"exampleFix":"// before\ncompilePetTelemetry(rawSpans, durationMs);\n// after\nconst events = importTrace(rawSpans); // normalizes to event-v1\ncompilePetTelemetry(events, durationMs);","handlingStrategy":"validation","validationCode":"const isEventV1 = (e) =>\n  e.schemaVersion === 1 && !!e.id && !!e.traceId &&\n  Number.isFinite(e.startTime) && Number.isFinite(e.endTime) &&\n  e.startTime >= 0 && e.endTime >= e.startTime && !!e.attributes;\nif (!events.every(isEventV1)) events = importTrace(rawSpans);","typeGuard":"const isEventV1 = (e) =>\n  typeof e === 'object' && e !== null &&\n  e.schemaVersion === 1 && typeof e.id === 'string' &&\n  typeof e.traceId === 'string' && CATEGORIES.includes(e.category) &&\n  Number.isFinite(e.startTime) && Number.isFinite(e.endTime) &&\n  e.startTime >= 0 && e.endTime >= e.startTime &&\n  typeof e.attributes === 'object' && e.attributes !== null;","tryCatchPattern":"try {\n  return compilePetTelemetry(events, durationMs);\n} catch (e) {\n  if (e.message.startsWith('Invalid event-v1 pet input')) {\n    return compilePetTelemetry(importTrace(rawInput), durationMs);\n  }\n  throw e;\n}","preventionTips":["Never feed raw spans directly to compile APIs — always route through importTrace","Assert schemaVersion 1 at the boundary where data enters your pipeline","Validate timestamps (finite, non-negative, end >= start) before compiling"],"tags":["validation","schema","javascript"],"backgroundTag":"schema-validation-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}