Hmbown/CodeWhale · error · Error

Invalid pet duration.

Error message

Invalid pet duration.

What it means

compilePetTelemetry() validates its durationMs parameter: it must be a finite, non-negative number of milliseconds. NaN, Infinity, negative values, or non-numeric input mean the caller computed an invalid trace duration, so compilation is refused.

Solutions

  1. Clamp/validate before calling: pass Number.isFinite(d) && d >= 0 ? d : 0.
  2. Fix the timestamp source so end - start is a finite non-negative number (parse with Number() or Date.parse, default missing fields).
  3. Check units: ensure both endpoints are in milliseconds and correctly ordered.
  4. If origin offsetting is intended, note durationMs is reduced by originMs after validation — pass a duration that stays non-negative after that subtraction.

Example fix

// before
const durationMs = end - start; // NaN or negative when fields missing
compilePetTelemetry(events, durationMs);
// after
const raw = end - start;
const durationMs = Number.isFinite(raw) && raw >= 0 ? raw : 0;
compilePetTelemetry(events, durationMs);
Defensive patterns

Strategy: validation

Validate before calling

function isValidDuration(ms) {
  return typeof ms === 'number' && Number.isFinite(ms) && ms >= 0;
}
if (!isValidDuration(durationMs)) durationMs = 0;

Type guard

function isFiniteNonNegative(v) {
  return typeof v === 'number' && Number.isFinite(v) && v >= 0;
}

Try / catch

try {
  compilePetTelemetry(events, durationMs);
} catch (e) {
  if (e.message === 'Invalid pet duration.') {
    compilePetTelemetry(events, 0); // fall back to onset-only buckets
  } else throw e;
}

Prevention

When it happens

Trigger: Calling compilePetTelemetry with durationMs as NaN (e.g. from an invalid Date or arithmetic on undefined), Infinity, a negative number, or a string — e.g. durationMs = end - start where start/end are undefined or strings.

Common situations: Timestamps parsed from malformed telemetry rows yielding NaN; unit mistakes (seconds vs milliseconds producing negative or tiny values); a Date.now() typo or missing field making end - start negative; JSON fields serialized as strings.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/94f42a89757cf623. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/tui/pet_watch/pet-native.js:1171

        this.sequence = packet.sequence;
        if (previous === undefined || packet.sequence <= previous)
            return;
        return packet;
    }
}
exports.PetLiveTape = PetLiveTape;
const order = (a, b) => a < b ? -1 : a > b ? 1 : 0;
const keyOf = (e) => JSON.stringify([e.traceId, e.id]);
const isContainer = (e) => e.attributes['whalesong.container'] === true
    || e.attributes['codewhale.container'] === true;
/** Compile a single trace. Unknown-duration spans provide onsets, not occupancy.
 * Updates of the same trace/id replace earlier snapshots rather than double count.
 * An endpoint onset gets its own bucket; intervals use [start, end). */
function compilePetTelemetry(input, durationMs = 0, firstSequence = 0, originMs = 0) {
    if (input.length > 250_000)
        throw new Error('Pet input exceeds 250000 events.');
    if (!Number.isFinite(durationMs) || durationMs < 0)
        throw new Error('Invalid pet duration.');
    if (!Number.isSafeInteger(firstSequence) || firstSequence < 0)
        throw new Error('Invalid first pet bucket.');
    if (!Number.isFinite(originMs))
        throw new Error('Invalid pet clock origin.');
    durationMs = Math.max(0, durationMs - originMs);
    const unique = new Map();
    const traces = new Set();
    for (const e of input) {
        if (e.schemaVersion !== 1 || !e.id || !e.traceId || !model_js_1.CATEGORIES.includes(e.category)
            || !Number.isFinite(e.startTime) || !Number.isFinite(e.endTime)
            || e.startTime < 0 || e.endTime < e.startTime || !e.attributes)
            throw new Error('Invalid event-v1 pet input. Import through importTrace first.');
        traces.add(e.traceId);
        unique.set(keyOf(e), e);
    }
    if (traces.size > 1)
        throw new Error('Select one trace for the pet.');
    const parents = new Set([...unique.values()].filter(e => e.parentId).map(e => e.parentId));

View on GitHub (pinned to 73e0f67d83)