{"record":{"id":"e89de0f785b3259f","repo":"affaan-m/ECC","slug":"invalid-limit-value","errorCode":null,"errorMessage":"Invalid limit: ${value}","messagePattern":"Invalid limit: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"scripts/lib/state-store/queries.js","lineNumber":18,"sourceCode":"'use strict';\n\nconst { assertValidEntity } = require('./schema');\n\nconst ACTIVE_SESSION_STATES = ['active', 'running', 'idle'];\nconst SUCCESS_OUTCOMES = new Set(['success', 'succeeded', 'passed']);\nconst FAILURE_OUTCOMES = new Set(['failure', 'failed', 'error']);\nconst CLOSED_WORK_ITEM_STATUSES = new Set(['done', 'closed', 'resolved', 'merged', 'cancelled']);\nconst ATTENTION_WORK_ITEM_STATUSES = new Set(['blocked', 'needs-review', 'failed', 'stalled']);\n\nfunction normalizeLimit(value, fallback) {\n  if (value === undefined || value === null) {\n    return fallback;\n  }\n\n  const parsed = Number.parseInt(value, 10);\n  if (!Number.isFinite(parsed) || parsed <= 0) {\n    throw new Error(`Invalid limit: ${value}`);\n  }\n\n  return parsed;\n}\n\nfunction parseJsonColumn(value, fallback) {\n  if (value === null || value === undefined || value === '') {\n    return fallback;\n  }\n\n  return JSON.parse(value);\n}\n\nfunction stringifyJson(value, label) {\n  try {\n    return JSON.stringify(value);\n  } catch (error) {\n    throw new Error(`Failed to serialize ${label}: ${error.message}`);","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/scripts/lib/state-store/queries.js#L1-L36","documentation":"Thrown by normalizeLimit() in the state-store queries module when a caller-supplied limit value, after Number.parseInt(_, 10), is not a finite positive integer. The function exists to clamp query sizes for list-style DB queries (sessions, runs, decisions), so a zero, negative, NaN, or non-finite value would either return no rows or be passed straight to SQL where it could cause downstream errors.","triggerScenarios":"Calling a query helper with limit: 0, limit: -5, limit: 'abc', limit: NaN, limit: Infinity (Number.isFinite rejects it), limit: 2.5 (parseInt floors to 2 but a downstream guard could still complain in other variants), or limit: '' (parseInt returns NaN).","commonSituations":"CLI --limit flag left empty by the user; reading the value from an env var that is unset (undefined is allowed and falls back, but an empty string is not); config file with limit: 0 meaning 'no limit' that the caller expected to be a sentinel; JSON payload coercion turning null into 0.","solutions":["Omit the limit argument entirely (or pass undefined/null) to use the function's fallback instead of passing 0.","Coerce user input upstream: const limit = Number(input); if (!Number.isInteger(limit) || limit < 1) limit = DEFAULT_PAGE_SIZE;","Treat 0 or negative values as 'use default' in your wrapper instead of forwarding them.","Validate env-derived limits with /^[1-9][0-9]*$/ before parsing."],"exampleFix":"// before\nlistSessions({ limit: 0 });   // -> Invalid limit: 0\n\n// after\nfunction safeLimit(raw, fallback = 50) {\n  const n = Number.parseInt(raw, 10);\n  return Number.isFinite(n) && n > 0 ? n : fallback;\n}\nlistSessions({ limit: safeLimit(input.limit) });","handlingStrategy":"validation","validationCode":"function safeLimit(raw, fallback = 50) {\n  if (raw === undefined || raw === null) return fallback;\n  const n = Number.parseInt(raw, 10);\n  return Number.isFinite(n) && n > 0 ? n : fallback;\n}\n\nlistSessions({ limit: safeLimit(input.limit) });","typeGuard":"function isPositiveLimit(value) {\n  if (value === undefined || value === null) return true;  // falls back\n  const n = Number.parseInt(value, 10);\n  return Number.isFinite(n) && n > 0;\n}","tryCatchPattern":"try {\n  listSessions({ limit });\n} catch (error) {\n  if (/Invalid limit/.test(error.message)) {\n    listSessions({});  // use the library default\n    return;\n  }\n  throw error;\n}","preventionTips":["Pass undefined (not 0) when you want the default — undefined/null are accepted, 0 is not.","Wrap user input in safeLimit at every ingress point.","Document 'omit for default' rather than '0 for default' in your own CLI help.","Validate env-derived limits with a regex before parsing."],"tags":["state-store","pagination","validation","numeric"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}