{"record":{"id":"0b16563932887fa6","repo":"ruvnet/ruflo","slug":"label-must-be-valid-json","errorCode":null,"errorMessage":"${label} must be valid JSON","messagePattern":"(.+?) must be valid JSON","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/commands/policy.ts","lineNumber":23,"sourceCode":"  PolicyState,\n} from '@claude-flow/security';\nimport type { Command, CommandContext, CommandResult } from '../types.js';\nimport { output } from '../output.js';\nimport {\n  autoMigratePolicyStateIfNeeded,\n  evaluatePolicyRequest,\n  loadPolicyState,\n  revokePolicyApproval,\n  setPolicyBudget,\n  setPolicyMode,\n  upsertPolicyRule,\n  verifyPolicyLedger,\n} from '../services/policy-runtime.js';\n\nfunction argJson<T>(value: string | undefined, label: string): T {\n  if (!value) throw new Error(`${label} requires a JSON argument`);\n  try { return JSON.parse(value) as T; }\n  catch { throw new Error(`${label} must be valid JSON`); }\n}\n\nfunction print(data: unknown): CommandResult {\n  output.writeln(JSON.stringify(data, null, 2));\n  return { success: true, exitCode: 0, data };\n}\n\nfunction requireInteractiveAdministrator(): void {\n  if (!process.stdin.isTTY || !process.stdout.isTTY) {\n    throw new Error('policy administration requires an interactive local terminal');\n  }\n}\n\nexport const policyCommand: Command = {\n  name: 'policy',\n  description: 'Agentic policy engine — evaluate actions, manage rules/approvals, and verify the decision ledger (ADR-324)',\n  options: [\n    { name: 'mode', type: 'string', description: 'Policy mode: legacy | observe | enforce' },","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/commands/policy.ts#L5-L41","documentation":"policy.ts's argJson() throws when the supplied JSON string reaches the CLI but fails JSON.parse. The argument is present; it is just not strict JSON — quoting damage or syntax errors are the usual cause.","triggerScenarios":"JSON containing single quotes inside a single-quoted shell string, trailing commas, unquoted keys, smart quotes from pasting, or an unquoted JSON string that the shell split into multiple argv entries (so JSON.parse sees only a fragment like '{id:').","commonSituations":"Windows cmd/PowerShell where single quotes don't group arguments; hand-typing JSON at the prompt; JSON produced by echo with variable interpolation inserting stray characters.","solutions":["Validate the payload first: echo '<payload>' | jq . — jq pinpoints the exact syntax error","On POSIX shells wrap the JSON in single quotes and use double quotes inside it","On Windows, escape inner double quotes or build the JSON in a variable via a tool and pass it through","Remove trailing commas and comments — only strict JSON parses"],"exampleFix":"# before\nruflo policy evaluate '{\"identity\": {\"id\":\"a1\",},}'\n\n# after\nruflo policy evaluate '{\"identity\":{\"id\":\"agent-1\",\"type\":\"agent\"},\"action\":{\"type\":\"deploy\",\"environment\":\"production\"}}'","handlingStrategy":"validation","validationCode":"function mustParse<T>(s: string, label: string): T {\n  try { return JSON.parse(s) as T; }\n  catch { throw new Error(`${label} is not valid JSON — check quoting/trailing commas`); }\n}\nconst payload = mustParse(rawArg, 'policy payload'); // run before the CLI does","typeGuard":"function isParsableJson(s: string): boolean {\n  try { JSON.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await runPolicyCli(['rule', 'add', raw]);\n} catch (err) {\n  if (err instanceof Error && err.message.includes('must be valid JSON')) {\n    // re-quote the payload (single-quote outer, double-quote inner) and retry once\n  } else throw err;\n}","preventionTips":["Pipe payloads through jq before invoking the CLI to prove they parse","Single-quote outside, double-quote inside on POSIX; on Windows write JSON to a file-based flow or escape carefully","Never build JSON by string concatenation of user input"],"tags":["cli","policy","json","quoting"],"backgroundTag":"json-parse-error","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}