Skip to content

repo_shape.py: load_yaml refuses a YAML file holding a non-UTF-8 byte as REFUSED yaml-unreadable naming the byte, so adopt-project.py check and execute and the validators no longer die with a UnicodeDecodeError traceback and exit 1 on a hand-edited plan or manifest (#192) - #193

Merged
brettheap merged 1 commit into
mainfrom
fix/yaml-unreadable-decode
Oct 9, 2026

Conversation

@brettheap

@brettheap brettheap commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Lane: openreposhape-2 (openRepoShape-2)
Claim: #192 (comment)

What

scripts/repo_shape.py load_yaml now catches UnicodeDecodeError beside OSError and raises Refusal("yaml-unreadable", "<path>: not UTF-8 at byte <n> (<reason>)", "Remediation: ..."). The remediation says every YAML file the tool reads must be UTF-8, names the byte (counting from 0), and says to re-save the file as UTF-8 or replace that byte. The docstring says why it is its own except: UnicodeDecodeError is a ValueError, and read_text raises it after the open has succeeded, so the OSError clause never sees it. parse_yaml and every other module are untouched.

Not chosen: catching it in each reader (about thirty call sites; shape-doctor.py already carries its own UnicodeDecodeError clause as a workaround, now redundant and left alone); decoding with errors="replace" (a fail-closed reader must not turn a bad byte into U+FFFD and pass a mangled leg path as a different path); widening the except to ValueError (it would swallow a bug). No caller relied on the old exception: each call site that names UnicodeDecodeError also names Refusal.

Why (repro on main)

On main at f25d805, a plan with a raw 0xff in a leg path:

python3 adopt-project.py plan --source Thing --project Northwind --org testorg --elected-by 'Test Human' --elected-on 2026-09-02 --out plan.yaml
python3 - <<'PY'
b = open("plan.yaml", "rb").read()
open("bad-plan.yaml", "wb").write(b.replace(b"spec_path: spec", b"spec_path: sp\xffec", 1))
PY
python3 adopt-project.py check --plan bad-plan.yaml                                        # exit 1
python3 adopt-project.py execute --plan bad-plan.yaml --local-remote-dir remotes --yes     # exit 1

Both end, after a traceback through Plan.load and load_yaml:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 1298: invalid start byte

with no FINDING or REFUSED line. Nothing is damaged (no leg exists yet), but the contract is that an unreadable input is a REFUSED yaml-unreadable line, a remediation and exit 2, as an unreadable or unparsable file already is. With this change, the same two commands:

REFUSED yaml-unreadable: bad-plan.yaml: not UTF-8 at byte 1298 (invalid start byte)
Remediation: every YAML file this tool reads must be UTF-8. Byte 1298, counting from 0 at the start of the file, is the first one that is not: re-save the file as UTF-8, or replace that byte with the character it was meant to be, and run the command again.
exit=2

and remotes/ is not created.

Tests

New, 19:

  • tests/test_load_yaml_refusals.py (new, 11): the function itself. Four ways a file is not UTF-8 (Latin-1 é, a UTF-16 byte order mark, a sequence cut off at the end of the file, a bad byte in a comment), each as yaml-unreadable naming the file, the offset and Python's reason, and each remediation; the missing-file, unparsable and non-ASCII-but-UTF-8 neighbours, so the new except is shown not to swallow valid text.
  • tests/test_adopt_execute_refusals.py (+7): adopt-project.py check and execute each with the 0xff in a value, in a comment and last in the file: exit 2, REFUSED yaml-unreadable: <path>: with the offset and a remediation, no traceback; execute also leaves no --local-remote-dir, no --work-dir, no leg and no source branch. Plus cmd_execute raising the Refusal in process.
  • tests/test_manifest_validator.py (+1): validate-manifest.py on a project.yaml with a 0xff in a value, beside test_an_unparsable_manifest_refuses. The doctor has no yaml-unparsable test to mirror, so the validator it runs is the reader tested.

16 of the 19 fail on main at f25d805 with the traceback above; the other three are the neighbours in test_load_yaml_refusals.py, which pass on both. (The commit message says 15: it was written before the in-process cmd_execute test had been run against main, and that is the sixteenth.)

Run on this branch:

  • test_load_yaml_refusals.py, test_adopt_execute_refusals.py, test_manifest_validator.py, test_adopt_plan.py: 141 passed, 1 skipped (the MedxEHR clone, absent here)
  • test_shape_doctor.py: 164 passed
  • test_naming_policy.py, test_pin_validator.py, test_shape_advisory.py, test_bump_leg.py: 244 passed
  • python3 scripts/render-cli-reference.py --check: docs/cli.md is what the generator produces

The full suite was not run here; CI runs it on Linux, macOS and Windows.

Closes #192

🤖 Generated with Claude Code

…te as `REFUSED yaml-unreadable` naming the byte, so `adopt-project.py check` and `execute` and the validators no longer die with a `UnicodeDecodeError` traceback and exit 1 on a hand-edited plan or manifest (#192)

`load_yaml` caught `OSError` around `read_text(encoding="utf-8")` and `YamlError` around the parse, and nothing for the decode. `UnicodeDecodeError` is a `ValueError`, and `read_text` raises it after the open has succeeded, so the `OSError` clause never saw it and a plan, manifest or pin with one byte that is not UTF-8 (a raw `0xff` in a leg path, in the issue's probe) escaped every reader as a traceback: exit 1, no `FINDING` or `REFUSED` line, where a missing file is `REFUSED yaml-unreadable` and exit 2. It is now the same code with a detail that names the file, the byte offset and Python's reason (`not UTF-8 at byte 1298 (invalid start byte)`), and a remediation that says the file must be UTF-8 and where to look. Nothing is created: `execute` reads the plan first, so no work directory, local remote or leg exists afterwards.

Not chosen: catching it in each reader (about thirty call sites, and `shape-doctor.py` already carries its own `UnicodeDecodeError` clause as the workaround this makes redundant, left alone here); decoding with `errors="replace"` (a fail-closed reader that turned a bad byte into U+FFFD would pass a mangled leg path as a different path); widening the `except` to `ValueError` (it would also swallow a bug). `parse_yaml` and every other module are untouched; no caller relied on the `UnicodeDecodeError`, since each that names it also names `Refusal`.

Tests: `tests/test_load_yaml_refusals.py` (new, 11: the four ways a file is not UTF-8, the remediation, and the missing-file, unparsable and non-ASCII-but-UTF-8 neighbours), `tests/test_adopt_execute_refusals.py` (+7: `check` and `execute` each with the byte in a value, in a comment and last in the file, exit 2, `REFUSED yaml-unreadable`, no traceback, nothing made under `--local-remote-dir`; and `cmd_execute` raising the `Refusal`) and `tests/test_manifest_validator.py` (+1: `validate-manifest.py`). 15 of them fail on main at f25d805, with the traceback. 141 passed and 1 skipped in those two files with `tests/test_adopt_plan.py`, 164 passed in `tests/test_shape_doctor.py`, 244 passed in `test_naming_policy.py`, `test_pin_validator.py`, `test_shape_advisory.py` and `test_bump_leg.py`; `render-cli-reference.py --check` ok, `docs/cli.md` unchanged. The full suite was not run.

Closes #192

Lane: openreposhape-2 (openRepoShape-2)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@sourcery-ai

sourcery-ai Bot commented Oct 9, 2026

Copy link
Copy Markdown

Sorry @brettheap, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 6 hours and 9 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@brettheap

Copy link
Copy Markdown
Contributor Author

@codex review

@sourcery-ai

sourcery-ai Bot commented Oct 9, 2026

Copy link
Copy Markdown

Reviewer's Guide

Centralize handling of non-UTF-8 YAML in load_yaml by translating UnicodeDecodeError into a detailed yaml-unreadable Refusal, then verify the behavior directly and through adoption and manifest-validation entry points, including fail-closed execution with no side effects.

Sequence diagram for centralized non-UTF-8 YAML refusal

sequenceDiagram
    participant Command
    participant Reader as load_yaml
    participant File as YAML file
    participant Parser as parse_yaml

    Command->>Reader: load_yaml(path)
    Reader->>File: read_text(encoding=utf-8)
    alt UnicodeDecodeError
        File-->>Reader: UnicodeDecodeError(start, reason)
        Reader-->>Command: Refusal("yaml-unreadable", path, byte, reason)
    else UTF-8 text
        File-->>Reader: text
        Reader->>Parser: parse_yaml(text)
        Parser-->>Reader: YAML value
        Reader-->>Command: YAML value
    end
Loading

Sequence diagram for fail-closed adoption execution

sequenceDiagram
    participant User
    participant Execute as adopt-project.py execute
    participant Reader as load_yaml
    participant File as bad-plan.yaml
    participant Resources as Remote and work resources

    User->>Execute: execute --plan bad-plan.yaml --yes
    Execute->>Reader: load_yaml(path)
    Reader->>File: read_text(encoding=utf-8)
    File-->>Reader: UnicodeDecodeError
    Reader-->>Execute: Refusal("yaml-unreadable", byte, reason, remediation)
    Execute-->>User: REFUSED yaml-unreadable and exit 2
    Execute-->>Resources: No resource creation or mutation
Loading

File-Level Changes

Change Details Files
Convert UTF-8 decoding failures into the existing structured YAML unreadable refusal.
  • Catch UnicodeDecodeError separately from OSError because read_text raises it after opening and it is a ValueError.
  • Report the file path, zero-based failing byte offset, Python decode reason, and UTF-8 remediation while preserving the original exception as the cause.
  • Leave YAML parsing and all readers unchanged so the centralized behavior applies across plans, manifests, validators, and other tools.
scripts/repo_shape.py
Add direct coverage for unreadable YAML classification and diagnostic details.
  • Exercise invalid Latin-1, UTF-16 BOM, truncated UTF-8, and invalid-comment bytes.
  • Verify yaml-unreadable code, path, offset, decode reason, remediation, chained cause, and refusal formatting.
  • Confirm missing files and unparsable YAML retain their existing classifications and valid non-ASCII UTF-8 remains accepted.
tests/test_load_yaml_refusals.py
Verify adoption commands fail closed without traceback or side effects.
  • Test check and execute with invalid bytes in values, comments, and at EOF.
  • Assert exit 2, structured refusal output, no traceback, and no execute-created remotes, work directory, leg, or source branch.
  • Verify in-process execute raises Refusal rather than UnicodeDecodeError.
tests/test_adopt_execute_refusals.py
Extend manifest validation coverage to the centralized refusal behavior.
  • Verify a non-UTF-8 project manifest returns exit 2 with path, byte offset, remediation, and no success output or traceback.
tests/test_manifest_validator.py

Assessment against linked issues

Issue Objective Addressed Explanation
#192 Make load_yaml treat UnicodeDecodeError from non-UTF-8 YAML files as a yaml-unreadable Refusal, including the file path, invalid byte offset, and remediation, rather than allowing a traceback. ✅
#192 Ensure adoption plan check and execute commands fail closed with a REFUSED yaml-unreadable response and exit code 2, without creating repositories, work directories, remotes, or branches. ✅
#192 Ensure other readers that use load_yaml, particularly manifest validation, retain the same refusal behavior for non-UTF-8 input and add regression coverage for the affected readers and decoding edge cases. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sonarqubecloud

sonarqubecloud Bot commented Oct 9, 2026

Copy link
Copy Markdown

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: e20b50b37c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T13:56:07.717430Z e20b50b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@brettheap

Copy link
Copy Markdown
Contributor Author

(lane openRepoShape-2, 2026-10-09T14:19:12Z) Full suite on this head e20b50b, run from a detached worktree in py-bench with git-filter-repo 2.47.0 on PATH, git 2.43: 1667 passed, 1 skipped in 24m (the MedxEHR clone skip, as on main; the count is higher than earlier heads because main f25d805 brought #187's tests). All three OS jobs pass, Sonar passed, Codex found no issues. Log: opensoft/brett-wip handoffs/xFactory/attachments/lane-openRepoShape-2/openRepoShape-2-suite-pr193-e20b50b.log. Merging waits on Brett's word.

@brettheap
brettheap merged commit 22c9fdd into main Oct 9, 2026
5 checks passed
@brettheap
brettheap deleted the fix/yaml-unreadable-decode branch October 9, 2026 18:42
brettheap added a commit that referenced this pull request Oct 9, 2026
…o fix/adopt-split-submodule-plan

Lane: openreposhape-1 (openRepoShape-1)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
brettheap added a commit that referenced this pull request Oct 11, 2026
…t-project.py writes a recorded submodule's unprintable character as one, so a submodule named with a no-break space, U+3000, a zero-width space, NEL or U+2028 is held to its row and moving it to the other leg is no longer COMPLIANT (#189)

P3-1 of lane openRepoShape-2's review of e167d8b. `_quoted` spelled every
character `str.isprintable` refuses as `ascii()` does (`\xa0`, ` `)
and a byte that is not UTF-8 as `\xe9`, and `repo_shape._unescape` resolved
only `\n \t \" \\`. So the row for `vendor/nb\xa0sp` read back with a literal
backslash, named no gitlink, and the doctor called the submodule `kept`; a
real adoption of each of six such names, moved to the other leg, stayed
COMPLIANT (scratchpad/fix189e/logs/probe-names-dbf8887.log).

- scripts/repo_shape.py: `_unescape` also resolves `CODE_POINT_ESCAPES`,
  YAML's `\xNN`, `\uNNNN` and `\UNNNNNNNN`, with YAML's meaning: the code
  point, never a byte. One with too few digits, or past U+10FFFF, keeps its
  backslash, and `\\x41` is still a backslash and `x41`.
- adopt-project.py: `_quoted` writes only the escapes those two tables
  resolve: the named ones for a backslash, a quote, a newline and a tab, and
  the shortest code-point escape for every other unprintable character. A
  byte that is not UTF-8 is now `\udce9`, which reads back as the surrogate
  the path holds, where `\xe9` read back as U+00E9, another name.

The doctor reads `project.yaml` with the standard's own `repo_shape`
(shape-doctor.py imports it from SHAPE_ROOT/scripts), and the project's own
validator with its copy, which `execute` writes from the same standard.

PINNED FILE: scripts/repo_shape.py. Every assembly root and every family
holder reads `upstream-changed scripts/repo_shape.py` in `update-shape.py
check` until its estate re-pins with `apply`, as for #156 and #193; this
repository has no digest of it to update. `apply` copies every
upstream-changed row together and rolls back if the project's validators go
red, so the record's validator never runs beside an older reader.

HOW THIS MEETS #199 (fix/adopt-plan-linebreak-names, not on main). #199
adds the same reader to repo_shape.py and more: it also resolves `\r`, and
it adds the writer `double_quoted` from the same tables. Whichever lands
second takes #199's side of repo_shape.py's escape section, deletes
`_quoted`, `_escaped` and `_NAMED_ESCAPES` here, and spells both
`submodule_record` fields with `double_quoted`. A record this branch writes
reads back unchanged under #199's reader, because both spell by YAML's code
points; the one difference is a carriage return, `\x0d` here and `\r` there.

Tests: the reader's escape table gains the code points and the cases that
must keep their backslash (tests/test_windows_paths.py); the record's
round-trip names gain nine unprintable ones and a literal `\x41`, and the
non-UTF-8 name now reads back as itself (tests/test_adopt_submodules.py);
and the doctor holds six such names, written by `submodule_record`, to
their rows: `recorded` in code, MISPLACED (6 paths) once staged in spec
(tests/test_shape_doctor.py).

Lane: openreposhape-1 (openRepoShape-1)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant