fix: parse DeepSchema hook target files as YAML, not JSON - #349
Merged
Conversation
The deepschema_write PostToolUse hook used Path.suffix to decide between yaml.safe_load and json.loads. For files like `.deepreview` whose entire name is treated as a hidden-file marker, Path.suffix is "" — so the hook fell through to json.loads and reported "File is not valid JSON" on every write/edit, even though the file was a perfectly valid YAML .deepreview. Fix: parse the target file (and the referenced JSON Schema file) with yaml.safe_load unconditionally. YAML is a superset of JSON, so both formats are accepted regardless of file extension. Mirrors the approach PR #338 already shipped for the workflow quality gate (jobs/mcp/quality_gate.py). Also handle the SchemaError case that becomes possible once a parser accepts free-form text: if the schema file parses to something that isn't a JSON Schema object or boolean, return "Cannot read JSON Schema: not a JSON Schema object" before invoking the validator. Updates DW-REQ-011.7.3 to match. Adds a regression test for dot-prefixed files (the user-reported case) and revises two existing tests that were asserting on the old "File is not valid JSON" / "schema isn't JSON" contracts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Extract `_write_name_required_schema(tmp_path, slug, matcher)` helper in test_write_hook.py and use it across all 5 JSON-Schema-validation tests (removes ~90 lines of fixture duplication). - Convert function docstrings to leading comments on three tests so the traceability `# THIS TEST VALIDATES A HARD REQUIREMENT` comments remain the first thing inside the function body (matches the repo convention established in PR #346). - deepschema_write.py: cast `schema_data` (typed `dict | bool` after the spec-compliant shape check) to `dict[str, Any]` for the call to `validate_against_schema`; `jsonschema.validate` accepts bool schemas at runtime but our wrapper's type signature only declares dict. All 43 tests in the affected files still pass. ruff + mypy clean. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The DeepSchema PostToolUse hook (
deepschema_write) reported a false-positiveFile is not valid JSON: Expecting value: line 1 column 1 (char 0)on every Write/Edit of.deepreviewfiles (and any other dot-prefixed filename).Root cause: the hook keyed off
Path.suffixto decide betweenyaml.safe_loadandjson.loads. For a file literally named.deepreview, Python treats the leading dot as a hidden-file marker andPath(".deepreview").suffix == "", so the file fell through to the JSON branch andjson.loadsfailed on the first character of valid YAML.Fix: parse both the target file and the referenced JSON Schema file with
yaml.safe_loadunconditionally. YAML is a superset of JSON, so both formats are accepted regardless of file extension. This mirrors the approach #338 shipped for the workflow quality gate (jobs/mcp/quality_gate.py) — which this bug pre-dated but #338 did not also fix, becausedeepschema_write.pyhad its own parallel code path.DW-REQ-011.7.3updated to match the new behavior.test_json_schema_validation_of_yaml_file_with_no_extension) covers the user-reported case with a.myappconfigtarget (chosen over.deepreviewto avoid collision with the bundled standard deepreview schema).tests/unit/test_deepschema_write_hook.pywere checking the old "File is not valid JSON" / "schema file isn't JSON" contracts; both rewritten for the new semantics, plus a new guard: if the referenced JSON Schema parses to something that isn't an object or boolean (e.g., a bare YAML string), returnCannot read JSON Schema: not a JSON Schema objectbefore invoking the validator (which would otherwise crash with ajsonschema.SchemaError).python_code_review: extracts_write_name_required_schema(tmp_path, slug, matcher)helper used by all 5 JSON-Schema-validation tests, and converts three function docstrings to leading comments so the traceability# THIS TEST VALIDATES ...comments remain the first thing inside the function body (matches the convention from feat: add requirements traceability annotations and new LA-REQ tests #346).json.loadssweepPer request, I audited all remaining
json.loads/json.loadcall sites insrc/after this fix. 13 remain — none read user-authored content that could reasonably be YAML:cli/install.py:52,setup/claude.py:26~/.claude/settings.jsoncli/jobs.py:56,jobs/mcp/state.py(7 calls).deepwork/tmp/sessions/.../state.jsonhooks/wrapper.py:273review/schema.py:17deepreview_schema.jsonjobs/schema.py:18job.schema.jsondeepschema/schema.py:13deepschema.schema.jsonTest plan
uv run pytest tests/unit/deepschema/test_write_hook.py tests/unit/test_deepschema_write_hook.py— 43 passeduv run pytest(full suite) — 1230 passed (coverage 97.95% matches pre-existingmainbaseline)uv run ruff check && uv run ruff format --check && uv run mypyon changed files — clean🤖 Generated with Claude Code