cape.agent.skills.fixjson: Diagnose and repair invalid JSON files

This module defines the built-in agent skill "fix-json", which teaches the CAPE agent how to repair a JSON file that cannot be parsed, typically a CAPE control JSON file with a syntax error.

Such files pose a special problem for the file-editor skill: a JSON file with a syntax error cannot be read into a cape.cfdx.cntl.Cntl instance, so it is never part of the Cntl-provided edit allow-list, and the agent would normally be denied edit access to the very file the user asked it to fix. The validate_json() tool therefore registers its target file with cape.agent.agentcntl.EDIT_FILE_ALLOW_LIST, which the cape.agent.skills.fileedit module merges into its allow-list, before reporting the first syntax error with numbered context lines.

cape.agent.skills.fixjson.validate_json(fname: str) → dict

Check a JSON file for syntax errors, reporting the first one

The file must be inside the repo root folder and end with .json. As a side effect, the file is registered with the file-editor skill’s allow-list (via cape.agent.agentcntl.EDIT_FILE_ALLOW_LIST) so that it can be repaired using that skill’s edit_file tool even though it cannot be read into a Cntl instance.

CAPE-style // comments are stripped from each line (outside of quoted strings) before parsing, mirroring the way CAPE reads JSON files. Reported line numbers refer to the original file.

Call:
>>> result = validate_json(fname)
Inputs:
fname: str

Name of JSON file to check

Outputs:
result: dict

Keys include success, file, registered (whether the file was newly added to the allow-list), and valid; for invalid files also error, lineno, colno, eof, hint, and context (numbered lines around the error)