{"record":{"id":"bc0da89ce66bb987","repo":"stablyai/orca","slug":"invalid-process-cpu-time-value","errorCode":null,"errorMessage":"Invalid process CPU time: ${value}","messagePattern":"Invalid process CPU time: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"config/scripts/macos-computer-helper-owner-loss-metrics.mjs","lineNumber":26,"sourceCode":"}\n\nexport function median(values) {\n  const sorted = [...values].sort((left, right) => left - right)\n  const middle = Math.floor(sorted.length / 2)\n  return sorted.length % 2 === 0 ? (sorted[middle - 1] + sorted[middle]) / 2 : sorted[middle]\n}\n\nexport function percentile(values, fraction) {\n  const sorted = [...values].sort((left, right) => left - right)\n  return sorted[Math.max(0, Math.ceil(sorted.length * fraction) - 1)]\n}\n\nfunction parseCpuTimeSeconds(value) {\n  const [dayOrTime, clock] = value.includes('-') ? value.split('-', 2) : [null, value]\n  const days = dayOrTime === null ? 0 : Number(dayOrTime)\n  const parts = clock.split(':').map(Number)\n  if (!Number.isFinite(days) || parts.some((part) => !Number.isFinite(part))) {\n    throw new Error(`Invalid process CPU time: ${value}`)\n  }\n  const seconds = parts.pop() ?? 0\n  const minutes = parts.pop() ?? 0\n  const hours = parts.pop() ?? 0\n  return days * 86_400 + hours * 3_600 + minutes * 60 + seconds\n}\n\nexport function processSnapshot(pid) {\n  const raw = execFileSync(\n    'ps',\n    ['-o', 'rss=', '-o', 'time=', '-o', 'command=', '-p', String(pid)],\n    { encoding: 'utf8' }\n  ).trim()\n  const match = raw.match(/^(\\d+)\\s+(\\S+)\\s+(.+)$/)\n  if (!match) {\n    throw new Error(`Could not inspect process ${pid}: ${raw}`)\n  }\n  return {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/stablyai/orca/blob/1136503c6a231a16dce8f921f6fadb63d181e8db/config/scripts/macos-computer-helper-owner-loss-metrics.mjs#L8-L44","documentation":"parseCpuTimeSeconds parses the `time` column from ps output (format: [D-]HH:MM:SS or MM:SS). It splits on '-' for days, then on ':' for hours:minutes:seconds. If any component fails Number.isFinite after conversion, the format is unrecognized.","triggerScenarios":"The ps `time=` value for a PID does not match the expected D-HH:MM:SS or HH:MM:SS format. Causes: non-C locale changing number separators; a process with zero CPU time showing an unusual format; ps output truncation at column width.","commonSituations":"LC_ALL/LC_NUMERIC locale changes the decimal separator or time format; macOS version differences in ps time formatting; extremely long-running processes with day counts exceeding expected digits; zombie processes with no accumulated time.","solutions":["Pin LC_ALL=C when calling ps to ensure consistent POSIX formatting","Log the raw ps `time=` value to inspect the actual format before parsing","Handle the parse failure with a fallback (return 0 or skip the sample)"],"exampleFix":"// before: ps inherits shell locale, may produce non-POSIX time format\nconst raw = execFileSync('ps', ['-o', 'time=', '-p', String(pid)], { encoding: 'utf8' })\n\n// after: pin locale to C for deterministic parsing\nconst raw = execFileSync('ps', ['-o', 'time=', '-p', String(pid)], {\n  encoding: 'utf8',\n  env: { ...process.env, LC_ALL: 'C' }\n})","handlingStrategy":"try-catch","validationCode":"// Pin locale for deterministic ps output before calling processSnapshot\nprocess.env.LC_ALL = 'C'\nprocess.env.LC_NUMERIC = 'C'","typeGuard":null,"tryCatchPattern":"try {\n  const seconds = parseCpuTimeSeconds(rawTime)\n} catch (error) {\n  if (error.message.includes('Invalid process CPU time')) {\n    // ps time format is unexpected — log raw value and use a fallback\n    console.warn('Unparseable CPU time:', rawTime, '— defaulting to 0')\n    return 0\n  }\n  throw error\n}","preventionTips":["Pin LC_ALL=C in the environment when calling ps to ensure POSIX-standard time formatting","Log the raw ps time column when parsing fails to diagnose format mismatches","Handle edge cases: zombie processes may show no accumulated CPU time"],"tags":["process-metrics","parsing","ps"],"backgroundTag":null,"analyzedSha":"1136503c6a231a16dce8f921f6fadb63d181e8db","analyzedAt":"2026-08-12T23:15:58.167Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}