{"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/6b01dc5a687b26b3e218f796de45ec51f8fa9e8c/v3/@claude-flow/cli/src/commands/policy.ts#L5-L41","documentation":"Thrown by argJson() when JSON.parse() rejects the supplied string. It signals the argument was present but not well-formed JSON, so the policy engine refuses to interpret it rather than guess at a partial structure. Distinct from error 200, which fires on a missing argument.","triggerScenarios":"Passing an unquoted/incorrectly-quoted object to rule add / budget set / evaluate, e.g. `policy budget set {id:x}` (unquoted braces, bad JSON), trailing commas, single-quoted JSON keys, or a JSON string whose outer quotes the shell consumed.","commonSituations":"Shell quoting mistakes (double quotes around a JSON body containing double quotes), hand-typing JSON with trailing commas or unquoted keys, or piping a non-JSON value into the positional slot.","solutions":["Validate the string with `echo '<json>' | jq .` before running the command — jq reports the exact parse error.","Single-quote the whole JSON blob in the shell so internal double quotes survive: `'{\"id\":\"x\"}'`.","Generate the JSON with a tool (jq -c, node -e) and pass its stdout rather than hand-typing."],"exampleFix":"// before\nruflo policy evaluate {identity:{id:\"a\"}}\n// after\nruflo policy evaluate '{\"identity\":{\"id\":\"agent-1\",\"type\":\"agent\"},\"action\":{\"type\":\"deploy\",\"environment\":\"production\"}}'","handlingStrategy":"validation","validationCode":"function safeJsonParse<T>(s: string | undefined, label: string): T {\n  if (!s) throw new Error(`${label} requires a JSON argument`);\n  try { return JSON.parse(s) as T; }\n  catch { throw new Error(`${label} must be valid JSON`); }\n}\n// pre-check before the CLI call:\nsafeJsonParse(process.env.POLICY_JSON, 'policy arg');","typeGuard":"function isJsonString(s: string): boolean {\n  try { JSON.parse(s); return true; } catch { return false; }\n}","tryCatchPattern":"try {\n  await runPolicyCommand(argv);\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e);\n  if (msg.endsWith('must be valid JSON')) {\n    console.error('Argument was not valid JSON. Validate with: echo %s | jq .', arg);\n    process.exit(2);\n  }\n  throw e;\n}","preventionTips":["Lint every JSON argument with `jq .` before the CLI call.","Single-quote blobs to protect embedded double quotes from the shell.","Build blobs with jq -c to guarantee valid output."],"tags":["cli","validation","json","policy","ruflo"],"backgroundTag":null,"analyzedSha":"6b01dc5a687b26b3e218f796de45ec51f8fa9e8c","analyzedAt":"2026-08-12T13:20:50.148Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}