{"record":{"id":"6b8c2f34dccc64dd","repo":"paperclipai/paperclip","slug":"failed-to-parse-json-at-filepath-err-instanc","errorCode":null,"errorMessage":"Failed to parse JSON at ${filePath}: ${err instanceof Error ? err.message : String(err)}","messagePattern":"Failed to parse JSON at (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"cli/src/client/context.ts","lineNumber":64,"sourceCode":"  if (process.env.PAPERCLIP_CONTEXT) return path.resolve(process.env.PAPERCLIP_CONTEXT);\n  return findContextFileFromAncestors(process.cwd()) ?? resolveDefaultContextPath();\n}\n\nexport function defaultClientContext(): ClientContext {\n  return {\n    version: 2,\n    currentProfile: DEFAULT_PROFILE,\n    profiles: {\n      [DEFAULT_PROFILE]: {},\n    },\n  };\n}\n\nfunction parseJson(filePath: string): unknown {\n  try {\n    return JSON.parse(fs.readFileSync(filePath, \"utf-8\"));\n  } catch (err) {\n    throw new Error(`Failed to parse JSON at ${filePath}: ${err instanceof Error ? err.message : String(err)}`);\n  }\n}\n\nfunction toStringOrUndefined(value: unknown): string | undefined {\n  return typeof value === \"string\" && value.trim().length > 0 ? value.trim() : undefined;\n}\n\nfunction normalizeProfile(value: unknown): ClientContextProfile {\n  if (typeof value !== \"object\" || value === null || Array.isArray(value)) return {};\n  const profile = value as Record<string, unknown>;\n  const persona = profile.persona === \"board\" || profile.persona === \"agent\"\n    ? profile.persona\n    : undefined;\n\n  return {\n    apiBase: toStringOrUndefined(profile.apiBase),\n    companyId: toStringOrUndefined(profile.companyId),\n    persona,","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/paperclipai/paperclip/blob/67001ec6eb96ae601aa27bc91d9b2415d665334a/cli/src/client/context.ts#L46-L82","documentation":"Thrown by parseJson() in cli/src/client/context.ts when the CLI context file (selected via --context or the default ~/.paperclip/context.json) exists but is not valid JSON. The raw JSON.parse error message is appended so the user can locate the syntax fault. The context file holds CLI profiles (apiBase, apiKey env refs, companyId, persona) and is written by `paperclipai context set`.","triggerScenarios":"Running any CLI command after the context file got corrupted: hand-edited and saved with a trailing comma / unquoted key / comment, partially written due to a crash mid-write, or overwritten by another tool. The file exists (so readFileSync succeeds) but JSON.parse throws.","commonSituations":"Manual edit of ~/.paperclip/context.json. A prior `paperclipai context set` was killed mid-write. Merge conflict left conflict markers in the file. JSON5-style syntax (comments, unquoted keys) mistakenly used.","solutions":["Open the path from the error message and fix the JSON syntax error cited.","Reset to a valid empty profile: write `{ \"version\": 2, \"currentProfile\": \"default\", \"profiles\": { \"default\": {} } }`.","Re-establish settings via `paperclipai context set` instead of hand-editing.","If unsure which profile is corrupt, back up the file and recreate it profile by profile."],"exampleFix":"// before — ~/.paperclip/context.json\n{\n  // my settings\n  \"version\": 2,\n  currentProfile: \"default\",\n  ...\n}\n\n// after\n{\n  \"version\": 2,\n  \"currentProfile\": \"default\",\n  \"profiles\": { \"default\": {} }\n}","handlingStrategy":"validation","validationCode":"import fs from 'node:fs';\nfunction tryLoadContext(path: string): unknown | null {\n  if (!fs.existsSync(path)) return null;\n  const text = fs.readFileSync(path, 'utf-8');\n  try { return JSON.parse(text); }\n  catch { console.warn(`Context file ${path} is corrupt; ignoring. Fix or run: paperclipai context set`); return null; }\n}","typeGuard":"function isContextFile(v: unknown): v is { version: number; currentProfile: string; profiles: Record<string, unknown> } {\n  return typeof v === 'object' && v !== null &&\n    typeof (v as any).version === 'number' &&\n    typeof (v as any).currentProfile === 'string' &&\n    typeof (v as any).profiles === 'object';\n}","tryCatchPattern":"try {\n  readContext(contextPath);\n} catch (err) {\n  const msg = err instanceof Error ? err.message : String(err);\n  if (msg.startsWith('Failed to parse JSON at')) {\n    console.error(`Context file corrupt: ${msg}. Back it up and reset with: paperclipai context set`);\n    process.exit(2);\n  }\n  throw err;\n}","preventionTips":["Never hand-edit ~/.paperclip/context.json; use `paperclipai context set`.","Commit a known-good context file in dotfiles so a corrupt one can be restored.","Avoid JSON5 syntax (comments/trailing commas) in the context file.","If scripting writes to it, write atomically (temp file + rename) to avoid partial writes."],"tags":["config","cli","json","context"],"backgroundTag":null,"analyzedSha":"67001ec6eb96ae601aa27bc91d9b2415d665334a","analyzedAt":"2026-08-12T12:05:45.408Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}