{"record":{"id":"e7233c7190bfab88","repo":"JuliusBrussee/caveman","slug":"cave-compaction-option-invalid","errorCode":"cave_compaction_option_invalid","errorMessage":"cave_compaction_option_invalid","messagePattern":"cave_compaction_option_invalid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/compaction.ts","lineNumber":100,"sourceCode":"  keepRecentTokens: 8_000,\n  summaryMaxTokens: 2_048,\n  minYieldTokens: 20_000,\n  headroomCalls: 3,\n  pinnedUserTokens: 20_000,\n});\n\nexport function normalizeCompaction(options: CompactionOptions = {}): NormalizedCompaction {\n  const merged = {\n    maxCompactions: options.maxCompactions ?? DEFAULTS.maxCompactions,\n    keepRecentTokens: options.keepRecentTokens ?? DEFAULTS.keepRecentTokens,\n    summaryMaxTokens: options.summaryMaxTokens ?? DEFAULTS.summaryMaxTokens,\n    minYieldTokens: options.minYieldTokens ?? DEFAULTS.minYieldTokens,\n    headroomCalls: options.headroomCalls ?? DEFAULTS.headroomCalls,\n    pinnedUserTokens: options.pinnedUserTokens ?? DEFAULTS.pinnedUserTokens,\n  };\n  for (const value of Object.values(merged)) {\n    if (!Number.isSafeInteger(value) || value <= 0) {\n      throw new Error(\"cave_compaction_option_invalid\");\n    }\n  }\n  return Object.freeze({\n    ...merged,\n    summarizerModel: options.summarizerModel,\n  });\n}\n\n/** Version of the summary contract the summarizer is asked to emit. */\nexport const SUMMARY_SCHEMA_VERSION = 1;\n\n/**\n * The sectioned summary the summarizer must produce.\n *\n * Structure rather than prose keeps required fields explicit. `constraintsRestated`\n * is a restatement only — the pinned buffer is the carrier, and a summary that\n * becomes the only carrier reproduces the failure this design exists to avoid.\n */","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/compaction.ts#L82-L118","documentation":"normalizeCompaction validates user-supplied compaction options (minYieldTokens, headroomCalls, pinnedUserTokens, etc.) after applying defaults. Every merged value must be a positive safe integer; otherwise it throws the coded error cave_compaction_option_invalid.","triggerScenarios":"Passing a compaction option that is 0, negative, fractional (e.g. 0.5), NaN, or exceeds Number.MAX_SAFE_INTEGER; passing a non-numeric value from parsed JSON/YAML config.","commonSituations":"Config files with token budgets written as strings; '0' used to mean 'disable' when 0 is rejected; copying a percentage (e.g. 0.25) where an absolute token count is expected.","solutions":["Set all numeric compaction options to positive safe integers","Remove the invalid option to fall back to the library default","Convert string values from config to Number and validate before passing"],"exampleFix":"// before\nnormalizeCompaction({ minYieldTokens: 0.5 })\n// after\nnormalizeCompaction({ minYieldTokens: 512 })","handlingStrategy":"validation","validationCode":"const opts = { minYieldTokens: 512, headroomCalls: 3, pinnedUserTokens: 128 };\nfor (const [k, v] of Object.entries(opts)) {\n  if (!Number.isSafeInteger(v) || v <= 0) throw new Error(`${k} must be a positive integer, got ${v}`);\n}","typeGuard":"const isValidCompactionOption = (v: unknown): v is number =>\n  typeof v === \"number\" && Number.isSafeInteger(v) && v > 0;","tryCatchPattern":"try {\n  const cfg = normalizeCompaction(userOptions);\n} catch (err) {\n  if ((err as Error).message === \"cave_compaction_option_invalid\") {\n    console.error(\"Compaction options must be positive safe integers; using defaults\");\n  } else throw err;\n}","preventionTips":["Never use 0 or fractions for token/call budgets; omit the option to disable via defaults","Coerce and validate numeric config values loaded from JSON/YAML (they may arrive as strings)","Use whole token counts, not percentages, for yield/headroom/pinned options"],"tags":["config","validation","compaction"],"backgroundTag":"invalid-config-value","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}