{"record":{"id":"4e1408cc88e48be0","repo":"ruvnet/ruflo","slug":"invalid-cfp-format-expected-magic-cfp1-got","errorCode":null,"errorMessage":"Invalid CFP format: expected magic 'CFP1', got '${parsed.magic}'","messagePattern":"Invalid CFP format: expected magic 'CFP1', got '(.+?)'","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/transfer/serialization/cfp.ts","lineNumber":153,"sourceCode":"  }\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}`);\n  }\n\n  if (!cfp.version) {\n    errors.push('Missing version');\n  }","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/transfer/serialization/cfp.ts#L135-L171","documentation":"After JSON.parse succeeds, deserializeCFP checks parsed.magic === 'CFP1'. The magic field is the format's version discriminator; anything else — CFP0, CFP2, a missing magic (undefined), or an unrelated JSON document — is rejected with this error showing the value it found. This is a format/version guard, separating 'valid JSON but wrong format' from the JSON syntax failure in the sibling check.","triggerScenarios":"deserializeCFP on JSON that is not a CFP document (any plain JSON file); on a future/incompatible CFP version (magic 'CFP2'); on a document where the magic field was dropped by a transform; on test fixtures hand-written without { magic: 'CFP1' }.","commonSituations":"Pointing the transfer import at the wrong file (e.g. a .json config mistaken for an exported pattern); schema evolution where producers bump the magic; middleware that strips unknown fields; fixtures built by hand instead of serializeToJson.","solutions":["Confirm the file is a CFP export produced by serializeToJson/serializeToBuffer — check the first bytes contain \"magic\":\"CFP1\"","If you are creating fixtures, build them via serializeCFP/serializeToJson instead of hand-writing JSON","A different magic value means a format version this reader does not support — upgrade the package or re-export as CFP1","Verify you passed the right path/CID; ordinary JSON config files will parse but fail this check"],"exampleFix":"// before\nconst cfp = deserializeCFP(await fs.readFile('settings.json')); // -> Invalid CFP format: expected magic 'CFP1', got 'undefined'\n\n// after\nconst cfp = deserializeCFP(await fs.readFile('pattern.cfp.json')); // exported via serializeCFP, magic === 'CFP1'\n\n// fixture authoring:\nconst fixture = serializeCFP(createEmptyCFP()); // guarantees magic: 'CFP1'","handlingStrategy":"validation","validationCode":"// Pre-flight the magic field so a wrong file fails with your own message\nconst parsed = JSON.parse(raw) as { magic?: string };\nif (parsed.magic !== 'CFP1') {\n  throw new Error(`Not a CFP1 document (magic=${JSON.stringify(parsed.magic)}) — wrong file or unsupported version`);\n}\nconst cfp = deserializeCFP(raw);","typeGuard":"function looksLikeCFP(value: unknown): value is { magic: 'CFP1' } {\n  return typeof value === 'object' && value !== null && (value as { magic?: unknown }).magic === 'CFP1';\n}","tryCatchPattern":"try {\n  const cfp = deserializeCFP(raw);\n} catch (e) {\n  if (/expected magic 'CFP1'/.test(String((e as Error).message))) {\n    throw new Error('Wrong or unsupported CFP version — check that the file is a CFP export (magic CFP1)');\n  }\n  throw e;\n}","preventionTips":["Generate fixtures and exports via the library's own serialize functions so magic is always correct","When accepting CFP files from users, check parsed.magic === 'CFP1' before full deserialization","Treat other magic values (CFP2, undefined) as version/format mismatches, not corruption"],"tags":["serialization","cfp","magic-bytes","format-validation","versioning"],"backgroundTag":"invalid-file-format","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}