Skip to content

repo_shape.py: _split_flow and _split_key_flow take the character after a backslash inside a double-quoted flow item, so pins: ["x\",y"] reads as the one item x",y its block twin reads, not as x\ and y" (#213) - #244

Merged
brettheap merged 2 commits into
mainfrom
fix/yaml-flow-escaped-quote
Oct 11, 2026

Conversation

@brettheap

@brettheap brettheap commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Lane: openreposhape-5 (openRepoShape-5)
Claim: #213 (comment)

What

scripts/repo_shape.py has two scanners for a flow collection, _split_flow (splits [a, b] and {k: v, ...} at top-level commas) and _split_key_flow (splits one flow-mapping entry at its key's :). Neither skipped the character after a backslash inside a double-quoted item, as _strip_comment and _split_key already do. So \" ended the item at the escaped quote, and the , or ] after it read as structure.

  • _split_flow now takes the character after a backslash while inside a DOUBLE-quoted item (scripts/repo_shape.py, the loop at _split_flow).
  • _split_key_flow is now return _split_key(text). Since v0 (86bf553) it was _split_key re-spelled as a for loop with the backslash skip left out; nothing else differed. _split_key already returns at the first top-level :, so a value that opens a flow is still fine, and already returns None at a [ or { before the colon, as the old loop did.
  • tests/test_yaml_flow_quotes.py (new): the issue's table, the block-sequence twin, the escaped-backslash guard, two single-quoted controls, and (fix round 1) eight cases for a backslash that is the last character of a double-quoted flow item, which pin that the reader refuses or reads it as plain text and never dies with a StopIteration.

A single-quoted item has no backslash escape and keeps its toggle ('' is its only escape). No new refusal in the code, no new escape table, nothing else in the reader changes.

On INVALID YAML the reads move, and not always the way they were. A double-quoted flow item that never closes now reads as plain text, the same as its block twin (k: ["a\", b] is ['"a\\", b'] in a flow and in a block; PyYAML refuses it). Over a raw corpus of 654,312 documents, 607,073 of which PyYAML refuses, 264 of those read differently: 12 that main refused are now read, 7 that main read are now refused by the existing refusals, 95 swap one wrong read for another, and 150 change only the refusal text. All are invalid YAML. The root cause is _scalar (scripts/repo_shape.py:284), which treats any text that starts and ends with " as quoted and which predates this change: - "a\" reads a\ in a block on main too. It is left for a follow-up issue.

Why

The same text read differently in a flow collection and in a block sequence, and a valid document could be refused. Of the four writers here that emit a non-empty flow list, three write only values a regex or a choice list admits (pins, allow_empty_legs, also_matches), and the fourth, referent_chain (scripts/shape_materialize.py:1010-1011), writes the operator-given chain links verbatim, unchecked (a GitHub repository name holds neither a quote nor a backslash, so a real chain does not reach it). Nothing else this repository writes can put a quote or a backslash into a flow item. A person editing the adoption plan, or json.dumps of a list, does. Reads on main, from the issue:

text main now
pins: ["x\",y"] ['x\\', 'y"'] (one item read as two) ['x",y'], equal to the block twin
pins: ["a\"]", "b"] ['a"]", "b'] (two items read as one) ['a"]', 'b']
pins: [["x\",y"], z] YamlError: not a flow collection [['x",y'], 'z']
pins: {k: "x\",y", j: 2} YamlError: flow mapping entry without a key {'k': 'x",y', 'j': 2}
pins: {"a\": b": 1} {'a\\': 'b": 1'} {'a": b': 1}

Evidence

Left out

  • A quote character in the MIDDLE of a plain flow item still opens a quote: k: [it's, a] reads ["it's, a"] while its block twin reads ["it's", "a"], and k: [a"b, c] is misread the same way. That is a misread this change did not cause and does not fix; it needs its own change and its own differential check. It is NOT unchanged for every input in that class. k: [a"b\", c] read correctly on main, by accident: the \" closed the quote that the " in the middle of the item had opened, so main read ['a"b\\"', 'c'], equal to its block twin and to PyYAML. It now reads ['a"b\\", c'], one item, because the \" is taken as an escape inside the quote the middle " opened. In the adversarial review's raw corpus (47,239 valid documents) 5 regress, all of them in this class, and 6 are fixed; in a corpus built dense in this class (50,000 valid documents) 88 are right on main and wrong now, 4,574 are wrong on main and right now, and 19,934 are wrong on both. The follow-up fixes both directions, and nothing this repository writes puts a quote in the middle of a plain flow item (a referent_chain link would have to be typed that way, which a repository name cannot hold).
  • The invalid-YAML reads above (an unterminated double-quoted item), and the _scalar check behind them. Left for a follow-up issue.
  • docs/cli.md is untouched; no CLI surface changed.
  • Only targeted files were run, not the full suite. The new tests write their files as bytes, so they are LF on every platform.

Closes #213

🤖 Generated with Claude Code

Summary by Sourcery

Fix YAML flow parsing so escaped quotes and backslashes are handled consistently across sequences and mappings.

Bug Fixes:

  • Ensure escaped characters inside double-quoted YAML flow items do not terminate the item or get interpreted as collection structure, keeping flow parsing consistent with block parsing.
  • Correct flow-mapping key parsing for escaped quotes and prevent dangling backslashes from causing an unexpected scanner exception.

Enhancements:

  • Add focused coverage for escaped quotes, escaped backslashes, single-quoted controls, nested flow collections, and malformed trailing backslashes.

Tests:

  • Add YAML flow-quote regression tests covering parsing results, block-flow parity, refusal behavior, and file-loading errors.

brettheap and others added 2 commits October 11, 2026 17:23
… after a backslash inside a double-quoted flow item, so `pins: ["x\",y"]` reads as the one item `x",y` its block twin reads, not as `x\` and `y"` (#213)

The YAML subset reader's two flow scanners ended a double-quoted item at
the next `"` and never looked at a backslash, so an escaped quote ended
the item early and the `,` or `]` after it was read as structure.
`_strip_comment` and `_split_key` already take the character after a
backslash inside double quotes, and `_scalar` resolves `\"`, so the same
scalar read one way in a block and another in a flow. On main 20e13f0:
`["x\",y"]` read as `x\`, `y"`; `["a\"]", "b"]` as the one item
`a"]", "b`; `[["x\",y"], z]` and `{k: "x\",y", j: 2}` were refused; and
`{"a\": b": 1}` read as the key `a\`.

`_split_flow` now appends the character after a backslash inside a
double-quoted item and moves past it. `_split_key_flow` now returns
`_split_key(text)`: since v0 (86bf553) it was `_split_key` written again
as a for loop with the skip left out, and nothing else in the two bodies
differs, so one scanner cannot drift from the other again. A
single-quoted item has no backslash escape; its `''` is still read by
toggling twice, unchanged.

Evidence: old and new agree (results and exceptions) on all 229,624 of
266,430 generated inputs whose double-quoted spans hold no backslash,
and the new `_split_key_flow` equals the old `_split_key` on all of
them. 50,000 `json.dumps` strings read back unchanged as a flow item, a
block item, a flow-mapping key and value, and nested: 0 mismatches,
against 16,132 on main.

tests/test_yaml_flow_quotes.py pins the issue's five rows and the
flow/block twin (all six fail on main), plus an escaped backslash
before the closing quote and a single-quoted sequence and mapping,
which pass on main and pin what must not change: each fails under a
mutant (a look-back fix; `_split_key_flow` left unfixed; the skip
applied in single quotes too).

Left out: no new escape, no new refusal, and the reader's header still
says "single-level" flow collections although `_flow_value` nests.

Lane: openreposhape-5 (openRepoShape-5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…e-quoted flow item's text reads as plain text or is refused, never a StopIteration, and the module docstring names the one writer that can quote a flow item

Review nit N1 on 8c656f4: no test pinned the `""` default of
`next(chars, "")` in `_split_flow`. With `next(chars)` all nine tests passed,
and `parse_yaml('k: ["a\\]')` died with a bare StopIteration out of the
scanner, so `load_yaml` would print a traceback instead of a REFUSED line.

tests/test_yaml_flow_quotes.py, eight new tests:

- four texts whose double-quoted item ends in a backslash right before the
  closing bracket or brace (`k: ["a\]`, `k: ["\]`, `k: {a: "x\}`,
  `k: [["\]]`). On this head each reads as plain text, `"a\` and so on, and
  each equals its block twin, which the test also reads. Not valid YAML
  (PyYAML refuses all four), so they pin that the reader does not crash.
- a mapping key ending in the backslash before the closing brace
  (`k: {"a\}`): a YamlError, `flow mapping entry without a key`.
- the same backslash at the very end of the text, for a sequence and a
  mapping: a YamlError, `not a flow collection`. `_flow` refuses these before
  `_split_flow` runs, so they do not guard the default; they pin the other
  half of "at the end".
- one read through `load_yaml` from a file written as bytes: `Refusal`
  `yaml-unparsable`, the file named.

Under the mutant `next(chars)`, six of the eight fail with StopIteration (the
four before the closer, the key, the file); the two at the very end of the
text pass, as above. On the head, 17 passed here and 11 in
test_load_yaml_refusals. The nine tests of 8c656f4 pass under the mutant, as
the review found.

Also corrects the module docstring, which said nothing this repository
writes puts a quote in a flow item: the `referent_chain` writer in
`shape_materialize.py` writes the operator's chain links as given (N4).
scripts/repo_shape.py is not touched.

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

sourcery-ai Bot commented Oct 11, 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 1 day and 3 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Oct 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

Fix flow parsing so escaped quotes and backslashes in double-quoted items do not prematurely terminate scanners or expose following commas, brackets, or colons as structure. The change unifies flow-mapping key parsing with the existing escape-aware scanner and adds regression tests covering valid flow/block parity and safe handling of malformed dangling escapes.

Sequence diagram for flow-mapping key parsing

sequenceDiagram
    participant Flow as _flow
    participant KeyFlow as _split_key_flow
    participant Key as _split_key
    participant Scanner as Escape-aware scanner

    Flow->>KeyFlow: _split_key_flow(entry)
    KeyFlow->>Key: _split_key(entry)
    Key->>Scanner: scan quotes, escapes, and top-level colon
    Scanner-->>Key: key/value boundary
    Key-->>KeyFlow: unquoted key and value
    KeyFlow-->>Flow: mapping entry
Loading

Flow diagram for escape-aware YAML collection scanning

flowchart LR
    Input["YAML flow collection text"] --> Scanner["_split_flow"]
    Scanner --> Quote{double-quoted item?}
    Quote -->|yes| Escape{backslash?}
    Escape -->|yes| Consume["consume next character"]
    Escape -->|no| Structure["recognize quote and top-level structure"]
    Consume --> Scanner
    Structure --> Result["split only at top-level commas"]
    Quote -->|no| Structure
Loading

File-Level Changes

Change Details Files
Make flow scanners honor backslash escapes within double-quoted items so escaped quotes cannot be mistaken for collection structure.
  • Skip the character following each backslash while scanning double-quoted flow items.
  • Preserve existing single-quote toggling and correctly handle escaped backslashes.
  • Add focused coverage for sequences, mappings, nested flows, block-flow parity, single-quoted controls, and dangling backslashes.
scripts/repo_shape.py
tests/test_yaml_flow_quotes.py
Reuse the established key scanner for flow-mapping entries to apply consistent quote and escape handling.
  • Replace the duplicated flow-key loop with a direct _split_key call.
  • Retain support for values that open flow collections and existing top-level-colon behavior.
scripts/repo_shape.py

Assessment against linked issues

Issue Objective Addressed Explanation
#213 Make double-quoted flow items and flow-mapping keys honor backslash escapes, so escaped quotes do not prematurely terminate items or cause commas, brackets, or colons inside them to be treated as structure. ✅
#213 Preserve the existing behavior for single-quoted flow items and correctly handle escaped backslashes, including avoiding scanner crashes when a backslash is the final character encountered. ✅
#213 Add regression tests covering the issue's flow/block consistency cases, flow sequence and mapping nesting, quoted keys and values, and relevant escape 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

@brettheap

Copy link
Copy Markdown
Contributor Author

(lane openRepoShape-5, 2026-10-11T20:07Z) @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@sonarqubecloud

Copy link
Copy Markdown

@brettheap
brettheap merged commit 404760f into main Oct 11, 2026
5 checks passed
@brettheap
brettheap deleted the fix/yaml-flow-escaped-quote branch October 11, 2026 21:18
brettheap added a commit that referenced this pull request Oct 11, 2026
…fix/plan-report-stdout-encoding (#214)

One conflict, scripts/shape_materialize.py, the single add/add hunk right
after write_lf: #199's write_paths_listing (from main) and this branch's
escape_what_stdout_cannot_encode with SURROGATEESCAPE_THEN_BACKSLASHREPLACE,
_registered_surrogateescape_then_backslashreplace,
_surrogateescape_then_backslashreplace and _is_escaped_byte. Resolved by
keeping both sides whole, this branch's block first and write_paths_listing
after it, two blank lines between. Every other file merged without a conflict
and is taken as main has it. No code was written for the merge.

Lane: openreposhape-5 (openRepoShape-5)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
brettheap added a commit that referenced this pull request Oct 11, 2026
…ng-paths

main moved to 4835fb4: #224 (e7d1720, update-shape.py), #244 (404760f,
repo_shape.py `_split_flow`) and #247 (4835fb4, the plan writer escapes
every character that is not printable). The textual merge is clean. The
next commit takes #247's one assertion that this branch's spelling
changes.

Lane: openreposhape-3 (openRepoShape-3)
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
brettheap added a commit that referenced this pull request Oct 11, 2026
…, #213 and #199 land beside #190, and the `repo_shape` import list carries #199's `double_quoted` with #190's `redacted`, `remote_is_a_path`, `remote_key` and `remote_local_path`

Main moved from 54aa69a to 4835fb4: e7d1720 (#208 via #224), 404760f
(#213 via #244) and 4835fb4 (#199 via #247). One conflict hunk, in
adopt-project.py, and it takes BOTH sides:

1. The `repo_shape` import list. #199 adds `double_quoted`, which `y`
   now spells every quoted plan scalar with; #190 adds `redacted`,
   `remote_is_a_path`, `remote_key` and `remote_local_path`, which its
   relative-url check calls (`_Resolution.same_as` and `__str__`,
   `_default_remote`, `_relative_url_findings`). Main's line is kept as
   main wrote it, and #190's four names follow `parse_yaml` in the
   list's order. Either side's list alone leaves a name the merged code
   calls undefined.

Merged with no conflict:

- adopt-project.py, #199's other hunks: `_PLAIN_RE` ending at `\Z`,
  `y` through `double_quoted`, `_spelled`'s docstring, `_shown` and the
  four report lines that print through it, and `write_paths_listing`
  in the `shape_materialize` import and in `_extract_leg`. None touches
  a #190 line. #190's findings and NOTE already print every path and
  url through `_spelled`, which is what `_shown` falls back to for a
  name that is not printable.
- scripts/repo_shape.py: #199's escape table (`DOUBLE_QUOTED_ESCAPES`
  with `\r`, `CODE_POINT_ESCAPES`, `double_quoted`, `_escaped`,
  `_escape_at`, the one-pass `_unescape`), with its module-docstring
  line and its `_BLOCK_SENTINEL` comment, and #213's `_split_key_flow`
  and `_split_flow`, are all in the YAML reader at the top of the
  file. #190's `REMOTE_PORT_RE`, `REMOTE_HOST_ALIASES` and
  `remote_key` are in the remote helpers some 1,400 lines below, and
  call nothing main changed. Each side's added and removed lines are
  the same before and after the merge, byte for byte.
- update-shape.py (#208), scripts/shape_materialize.py (#199) and
  main's three test files are main's blobs; #190's three test files
  are this branch's.

#190's added and removed lines against 4835fb4 are its lines against
54aa69a, but for the import hunk.

Lane: openreposhape-1 (openRepoShape-1)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
brettheap added a commit that referenced this pull request Oct 11, 2026
…) into fix/adopt-record-submodule-placement

Brings in e7d1720 (#208), 404760f (#213) and 4835fb4 (#199) on top of
54aa69a. No rebase.

#199 landed first, so this branch is the one that lands second, and it
takes #199's side of the double-quoted escape section, as cd2a622's message
and lane openRepoShape-2's review of e167d8b both said it would.

Conflicts, and how each was resolved:

- scripts/repo_shape.py, two hunks (textual): the escape section and
  `_unescape`'s docstring. Both sides added `CODE_POINT_ESCAPES`,
  `_HEX_DIGITS` and an `_escape_at` whose body is the same line for line,
  and both reworded the docstring; #199 also added `r` to
  `DOUBLE_QUOTED_ESCAPES` and the writer, `double_quoted` with `_escaped`
  and `_ESCAPE_OF`. The auto-merge had also left this branch's own copies
  of `CODE_POINT_ESCAPES`, `_HEX_DIGITS` and `_escape_at` beside main's.
  Resolved as main's file, byte for byte, plus this branch's one change to
  it that is not about escapes: 379763c's `path_spelling_fault` section and
  its line in the module docstring, applied with `git apply`. `git diff
  origin/main -- scripts/repo_shape.py` is that 51-line addition alone.

Merged without a conflict, and checked:

- adopt-project.py: main's `y` now quotes with `double_quoted`, and this
  branch's `submodule_record` still quotes with `_quoted`, which reads
  main's table now, so it writes a carriage return as `\r`, where it wrote
  `\x0d`. The two writers give the same text for every character to U+2FFF,
  every surrogate and seven more (14,343 compared, 0 different). The next
  commit removes `_quoted`.
- scripts/shape_materialize.py: main's `write_paths_listing` and this
  branch's `path_spelling_fault` import and `LEG_SEGMENT_REASONS`.

Every merged module compiles. On this merge, tests/test_windows_paths.py,
test_adopt_plan_escapes.py, test_yaml_flow_quotes.py,
test_manifest_validator.py and the record's two name tests in
test_adopt_submodules.py: 218 passed.

Lane: openreposhape-1 (openRepoShape-1)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
brettheap added a commit that referenced this pull request Oct 12, 2026
…e-notice

It merged with no conflict. Main brings six commits since 54aa69a:

- e7d1720, update-shape.py (#208 via #224);
- 404760f, scripts/repo_shape.py's double-quoted flow items (#213 via
  #244), with tests/test_yaml_flow_quotes.py;
- 4835fb4, adopt-project.py's plan writer escapes (#199 via #247), with
  tests/test_adopt_plan_escapes.py;
- b538015, scaffold-project.py's leg paths against the shape's own
  paths (#197 via #234);
- 150c41d, update-shape.py's `apply --add` (#202 via #233);
- 39ac59c, scaffold-project.py's leg mount `--force` (#229 via #235).

Two of them touch adopt-project.py, and no hunk of either meets this
branch's:

- 4835fb4 changes the plan writer (`y`, `_PLAIN_RE`), adds `_shown` to
  the plan and check reports, and writes the `--paths-from-file` listing
  in `_extract_leg` through `write_paths_listing`. This branch changes
  other lines of `_extract_leg`: it reads the tree digest before the
  push and pushes through `_push_the_leg`. The signature is the same on
  both sides, so main's new direct `_extract_leg` test calls it as
  written.
- b538015 moves `_overlap`, `_first_and_more`, `MOVE_THE_LEG` and
  `CASE_ASIDE` into scripts/shape_materialize.py, and imports the first
  three back under the same names. This branch adds no use of any of
  them. A `symtable` check finds no name that the merged adopt-project.py,
  scaffold-project.py or scripts/shape_materialize.py reads and nothing
  binds.

`git diff -U0` of this merge against 39ac59c equals the branch's against
54aa69a, apart from the hunk headers. On the merged tree,
tests/test_adopt_plan_escapes.py, tests/test_yaml_flow_quotes.py,
tests/test_scaffold_leg_path_collisions.py and
tests/test_adopt_leg_path_collisions.py give 275 passed.

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