{"record":{"id":"d14fc5dbf89a4baa","repo":"ruvnet/ruflo","slug":"invalid-cfp-file-e-instanceof-error-e-message","errorCode":null,"errorMessage":"Invalid CFP file: ${e instanceof Error ? e.message : String(e)}","messagePattern":"Invalid CFP file: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/transfer/serialization/cfp.ts","lineNumber":148,"sourceCode":"    case 'cbor.zstd':\n    case 'msgpack':\n      throw new Error(`Serialization format '${format}' is not implemented. Use 'json' instead.`);\n    default:\n      return Buffer.from(json, 'utf-8');\n  }\n}\n\n/**\n * Deserialize CFP from string/buffer\n */\nexport function deserializeCFP(data: string | Buffer): CFPFormat {\n  const str = typeof data === 'string' ? data : data.toString('utf-8');\n\n  let parsed: CFPFormat;\n  try {\n    parsed = JSON.parse(str);\n  } catch (e) {\n    throw new Error(`Invalid CFP file: ${e instanceof Error ? e.message : String(e)}`);\n  }\n\n  // Validate magic bytes\n  if (parsed.magic !== 'CFP1') {\n    throw new Error(`Invalid CFP format: expected magic 'CFP1', got '${parsed.magic}'`);\n  }\n\n  return parsed;\n}\n\n/**\n * Validate CFP document\n */\nexport function validateCFP(cfp: CFPFormat): { valid: boolean; errors: string[] } {\n  const errors: string[] = [];\n\n  if (cfp.magic !== 'CFP1') {\n    errors.push(`Invalid magic bytes: ${cfp.magic}`);","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/transfer/serialization/cfp.ts#L130-L166","documentation":"deserializeCFP(data) first runs JSON.parse over the entire input (string or utf-8-decoded Buffer). When parsing fails it wraps the underlying SyntaxError message into 'Invalid CFP file: ...' — so this specifically means the bytes are not valid JSON at all, before any CFP-specific validation (magic bytes, schema) happens. The wrapped message usually pinpoints the JSON syntax problem (unexpected token, position).","triggerScenarios":"Feeding deserializeCFP a truncated file (partial download/transfer), an empty string, a binary/CBOR payload, an HTML error page saved as .cfp.json, or a file with a BOM/encoding corruption.","commonSituations":"Reading a CFP fetched from a gateway that returned an error page instead of JSON; files truncated by disk-full or interrupted IPFS retrieval; hand-edited JSON with trailing commas; double-encoded or base64-wrapped payloads.","solutions":["Inspect the wrapped message — it names the exact JSON syntax error and position; open the file at that offset","Validate the source is intact: re-fetch the CFP by CID, check file size, or JSON.parse it directly in a scratch script","If the payload is not meant to be JSON (e.g. compressed), decode/decompress before deserializeCFP","For empty files, the producer side failed — regenerate/export the CFP"],"exampleFix":"// before\nconst cfp = deserializeCFP(await fs.readFile('pattern.cfp.json')); // truncated file -> Invalid CFP file: Unexpected end of JSON input\n\n// after\nconst raw = await fs.readFile('pattern.cfp.json', 'utf-8');\nlet cfp;\ntry {\n  cfp = deserializeCFP(raw);\n} catch (e) {\n  if (/Invalid CFP file/.test(String(e?.message))) {\n    console.error('CFP payload is not valid JSON (first 200 chars):', raw.slice(0, 200));\n    throw e; // or re-fetch the file by CID\n  }\n  throw e;\n}","handlingStrategy":"try-catch","validationCode":"// Cheap pre-check: confirm the payload is JSON before handing it to the CFP parser\nimport * as fs from 'node:fs';\nconst raw = fs.readFileSync(file, 'utf-8');\ntry { JSON.parse(raw); } catch {\n  throw new Error(`${file} is not valid JSON — refusing to parse as CFP`);\n}\nconst cfp = deserializeCFP(raw);","typeGuard":null,"tryCatchPattern":"try {\n  const cfp = deserializeCFP(raw);\n} catch (e) {\n  if (/^Invalid CFP file:/.test(String((e as Error).message))) {\n    // syntax-level corruption: log a payload sample for diagnosis, then re-fetch from source\n    console.error('Not valid JSON (first 200 bytes):', String(raw).slice(0, 200));\n    throw new Error(`CFP file corrupt — re-download by CID: ${file}`);\n  }\n  throw e; // magic/schema errors have different messages and different remedies\n}","preventionTips":["Verify file size/checksum after transferring CFP files before parsing","Distinguish this JSON-syntax failure from the magic-bytes failure — the remedies differ (re-download vs wrong file)","Never hand deserializeCFP content from an unvalidated HTTP response; check content-type and parse JSON first"],"tags":["serialization","cfp","json","parsing","corrupt-file"],"backgroundTag":"malformed-json","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}