{"record":{"id":"b2b4de4a20d206cd","repo":"different-ai/openwork","slug":"invalid-json","errorCode":"invalid_json","errorMessage":"invalid_json","messagePattern":"invalid_json","errorType":"error_code","errorClass":"SafeProbeFailure","httpStatus":null,"severity":"warning","filePath":"apps/server/src/agent-context-cloud-probe.ts","lineNumber":491,"sourceCode":"  }\n  const bytes = new Uint8Array(size);\n  let offset = 0;\n  for (const chunk of chunks) {\n    bytes.set(chunk, offset);\n    offset += chunk.byteLength;\n  }\n  try {\n    return new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n  } catch {\n    throw new SafeProbeFailure(\"invalid_utf8\");\n  }\n}\n\nfunction parseJson(text: string): unknown {\n  try {\n    return JSON.parse(text);\n  } catch {\n    throw new SafeProbeFailure(\"invalid_json\");\n  }\n}\n\nfunction parseSse(text: string): unknown {\n  const messages: unknown[] = [];\n  let event = \"\";\n  let data: string[] = [];\n  const dispatch = () => {\n    if (data.length === 0) {\n      event = \"\";\n      return;\n    }\n    if (event && event !== \"message\") throw new SafeProbeFailure(\"invalid_json\");\n    messages.push(parseJson(data.join(\"\\n\")));\n    event = \"\";\n    data = [];\n  };\n  for (const line of text.split(/\\r\\n|\\r|\\n/u)) {","sourceCodeStart":473,"sourceCodeEnd":509,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/server/src/agent-context-cloud-probe.ts#L473-L509","documentation":"parseJson wraps JSON.parse; any parse failure becomes SafeProbeFailure('invalid_json'). The cloud probe uses it to decode response bodies (and SSE data payloads), treating non-JSON responses as a controlled probe failure rather than a crash.","triggerScenarios":"readJsonRpcPayload or dispatch receiving text that is not valid JSON — HTML error pages, empty bodies, truncated responses, plain-text messages.","commonSituations":"Probe hits a login/HTML page instead of the JSON-RPC endpoint; server returns 200 with an error page; response truncated by an intermediary; wrong port serving a different app.","solutions":["Verify the probe URL points at the JSON-RPC endpoint (correct port/path).","Check the response status/content-type before parsing and surface a clearer error.","Inspect the raw text (log first N chars) to see what the server actually returned.","Fix server-side errors causing HTML/plain-text error pages."],"exampleFix":"// before\ntry {\n  return JSON.parse(text);\n} catch {\n  throw new SafeProbeFailure(\"invalid_json\");\n}\n// after — include a preview for diagnosis\ntry {\n  return JSON.parse(text);\n} catch {\n  throw new SafeProbeFailure(\"invalid_json\"); // caller should preview text.slice(0, 200) for diagnosis\n}","handlingStrategy":"validation","validationCode":"const text = await readBoundedBody(response, deadline, budget);\nif (!text.trimStart().startsWith(\"{\") && !text.trimStart().startsWith(\"[\")) {\n  return { ok: false, reason: \"non_json_response\", preview: text.slice(0, 120) };\n}","typeGuard":"function isJsonObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === \"object\" && v !== null && !Array.isArray(v);\n}","tryCatchPattern":"try {\n  const payload = parseJson(text);\n} catch (error) {\n  if (error instanceof SafeProbeFailure && error.code === \"invalid_json\") {\n    return { ok: false, reason: \"invalid_json\" }; // record probe finding\n  }\n  throw error;\n}","preventionTips":["Validate response status (2xx) and content-type before parsing.","Check the body starts with a JSON token before JSON.parse.","Confirm the probe URL/port targets the JSON-RPC server.","Log a short text preview to diagnose HTML error pages."],"tags":["json","parsing","probe"],"backgroundTag":"invalid-json-response","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}