{"record":{"id":"ba7f2508d6abde7a","repo":"jackwener/OpenCLI","slug":"label-returned-malformed-rows","errorCode":null,"errorMessage":"${label} returned malformed rows","messagePattern":"(.+?) returned malformed rows","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/grok/export-utils.js","lineNumber":56,"sourceCode":"    try {\n        parsed = new URL(raw);\n    } catch {\n        throw makeError(`invalid url for conversation ${id}`);\n    }\n    const host = parsed.hostname.toLowerCase();\n    const match = parsed.pathname.match(/^\\/c\\/([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\\/?$/i);\n    if (parsed.protocol !== 'https:' || (host !== 'grok.com' && !host.endsWith('.grok.com')) || !match) {\n        throw makeError(`invalid url for conversation ${id}`);\n    }\n    if (match[1].toLowerCase() !== id) {\n        throw makeError(`url id mismatch for conversation ${id}`);\n    }\n    return `https://grok.com/c/${id}`;\n}\n\nexport function normalizeConversationRows(rows, label) {\n    if (!Array.isArray(rows)) {\n        throw new CommandExecutionError(`${label} returned malformed rows`, 'Expected rows to be an array.');\n    }\n    return rows.map((row, index) => {\n        if (!row || typeof row !== 'object' || Array.isArray(row)) {\n            throw new CommandExecutionError(`${label} returned a malformed row`, `Row ${index + 1} is not an object.`);\n        }\n        const id = String(row.id || '').trim().toLowerCase();\n        if (!GROK_CONVERSATION_ID_RE.test(id)) {\n            throw new CommandExecutionError(`${label} returned a malformed row`, `Row ${index + 1} is missing a valid Grok conversation id.`);\n        }\n        return {\n            id,\n            title: row.title == null || row.title === '' ? '' : String(row.title),\n            date: row.date == null || row.date === '' ? '' : String(row.date),\n            url: normalizeGrokUrl(row.url, id, (reason) => new CommandExecutionError(`${label} returned a malformed row`, reason)),\n        };\n    });\n}\n","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/grok/export-utils.js#L38-L74","documentation":"normalizeConversationRows validates the rows array returned by the Grok history-collection script (used by validRows). If the evaluate result is not an array it throws CommandExecutionError('<label> returned malformed rows', 'Expected rows to be an array.'); individual entries must additionally be objects with a valid UUID-shaped Grok conversation id and a valid https://grok.com/c/<uuid> URL.","triggerScenarios":"collectHistory's page.evaluate returns null/undefined/a non-array (history never loaded, logged-out or bot-check page, evaluate interrupted), or returns an array whose entries are not objects, lack a UUID id, or carry a URL that is not a well-formed grok.com/c/<uuid> link.","commonSituations":"Grok UI redesign changing the DOM the scraper reads so the script returns a different shape; scraping while signed out or behind a Cloudflare challenge; version drift between the opencli Grok scripts and the live site; a conversation whose URL pattern changed (e.g. different path prefix) failing per-row validation.","solutions":["Confirm the automation browser is authenticated to grok.com and not showing a login or bot-check page, then re-run.","Retry, or increase --max-scrolls, so the history list fully loads before rows are collected.","If using a manifestPath instead, verify it is a JSON array exported by grok/export with id and grok.com/c/<uuid> url fields (manifest rows are validated by normalizeManifestRows).","If it fails on every run, update the opencli Grok collection script — the site's DOM/URL format likely changed."],"exampleFix":"// before (selector drift yields a non-array)\nconst rows = await page.evaluate(() => document.querySelectorAll('a[href^=\"/c/\"]'));\n// after (materialize into an array of plain objects)\nconst rows = await page.evaluate(() =>\n  Array.from(document.querySelectorAll('a[href^=\"/c/\"]'), (a) => ({\n    id: a.href.split('/c/')[1] || '',\n    title: a.textContent?.trim() || '',\n    url: a.href,\n  }))\n);","handlingStrategy":"validation","validationCode":"function looksLikeGrokRows(v) {\n  const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;\n  return Array.isArray(v) && v.every((r) => r && typeof r === 'object' &&\n    uuid.test(String(r.id || '')) && /^https:\\/\\/([a-z0-9-]+\\.)?grok\\.com\\/c\\/[0-9a-f-]{36}\\/?$/i.test(String(r.url || '')));\n}","typeGuard":"function isConversationRowArray(v) {\n  return Array.isArray(v) && v.length > 0 && v.every((r) => r !== null && typeof r === 'object' && !Array.isArray(r));\n}","tryCatchPattern":"try {\n  const rows = await collectHistory(page, { offset, limit, maxScrolls });\n} catch (err) {\n  if (err instanceof CommandExecutionError && /malformed rows|malformed row/.test(err.message)) {\n    // history DOM changed or page not loaded: re-authenticate, raise maxScrolls, or fall back to a manifestPath\n    return runWithManifest(kwargs.manifestPath, { offset, limit });\n  }\n  throw err;\n}","preventionTips":["Pin/track opencli versions and re-verify scraping after Grok UI updates.","Prefer a manifestPath exported by grok/export (validated by normalizeManifestRows) over live scraping when possible.","Ensure the history list has loaded (enough scrolls) before collecting rows.","Reject non-grok.com/c/<uuid> URLs at the data source instead of at validation time."],"tags":["browser-automation","payload-shape","dom-scraping"],"backgroundTag":"unexpected-payload-shape","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}