Skip to content

update-shape.py: four functions run past Sonar's cognitive-complexity limit of 15, and cmd_apply at 47 holds every refusal, the copy, the re-pin, the validators, the rollback and the landing in one body #133

Closed

Description

@brettheap

Lane: openreposhape-2 (openRepoShape-2)

What is wrong. SonarCloud's python:S3776 (cognitive complexity, limit 15)
names four functions in update-shape.py on main at d682127. The rule counts
+1 per branch, loop and boolean-operator sequence, plus one per level of
nesting, so a number this far over the line is a statement about how many
decisions a reader has to hold at once — not about length.

function line Sonar says complexipy 8.0.1 says
classify 433 24 24
upstream_copies 586 19 21
rewrite_manifest 788 16 15
cmd_apply 1145 47 59
$ curl -s 'https://sonarcloud.io/api/issues/search?componentKeys=opensoft_openRepoShape&rules=python:S3776&statuses=OPEN&ps=100'
update-shape.py 433  | Refactor this function to reduce its Cognitive Complexity from 24 to the 15 allowed.
update-shape.py 586  | Refactor this function to reduce its Cognitive Complexity from 19 to the 15 allowed.
update-shape.py 788  | Refactor this function to reduce its Cognitive Complexity from 16 to the 15 allowed.
update-shape.py 1145 | Refactor this function to reduce its Cognitive Complexity from 47 to the 15 allowed.

Each one has grown by accretion rather than by design:

  • classify (update-shape.py:433) is one loop whose body answers three
    "cannot be compared" questions and then walks a five-branch verdict ladder
    over three derived booleans. The docstring's four byte-strings (D, R, P, T)
    are the whole idea; the reader has to reconstruct it from the ladder.
  • upstream_copies (update-shape.py:586) does two unrelated jobs in one
    body: an ast scan that harvests the upstream module's top-level string and
    tuple constants, and then the mapping of the three copy lists onto Copy
    objects.
  • rewrite_manifest (update-shape.py:788) locates the shape: block with
    two scans before it rewrites a line inside it, so finding the block and
    editing it are interleaved.
  • cmd_apply (update-shape.py:1145) is the extreme case at 47. One body
    carries: the --accept-local unknown-path refusal, --add resolution and
    its notes, the nothing-to-do exit, the update-conflict refusal, the
    update-local-drift refusal, the confirmation, two nested closures that
    remember every byte written for the rollback, the copy/kept/added print
    loops, both pin rewrites, the validator run and its rollback refusal, the
    branch/push/pull-request landing, and the three except/finally arms that
    put the tree back. A reader who wants to check one refusal reads all of it.

What would fix it. Move code, change nothing. Extract one helper per
decision, in this file's existing convention for private helpers
(adopt-project.py and scripts/repo_shape.py both use a leading underscore),
each with a docstring in the file's voice saying what it decides and that it
was split out of its parent:

  • classify keeps the loop and hands each row to a per-row helper, which
    keeps the three guards and hands the verdict ladder to a second helper.
  • upstream_copies keeps the parse and the two failure exits, and calls one
    helper for the ast constant scan and one for the list-to-Copy mapping.
  • rewrite_manifest calls a helper that returns the shape: block's bounds
    (and raises update-manifest-no-shape), and keeps the rewrite.
  • cmd_apply keeps its sequence and its try/except/finally and delegates
    each phase: the three refusals, the rollback ledger (the two closures become
    a small object with an undo), the write pass, the re-pin, the validators,
    and the landing.

The law for this change: behaviour byte-identical. No refusal id, no
message text, no exit code, no print order, no file written and no exception
type may change. tests/test_update_shape.py, tests/test_update_shape_add.py
and tests/test_commit_trailers.py assert the exact refusal ids
(update-branch-required, update-conflict, update-local-drift,
update-add-would-overwrite, …), the exact NEXT spellings including
--trailer "Lane: $LANES_LANE", the rollback-on-red-validator behaviour and
the per-file --accept-local/--add semantics from #85 and #29; they are the
proof, and every one of them must still pass unchanged.

cmd_check (update-shape.py:911) is deliberately NOT in scope: Sonar does
not flag it, and touching it would put a function Sonar scores at the limit
into this pull request's new code for no gain.

Activity

  1. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T03:09Z) CLAIMED — session 983ba16a, on Brett Heap's word 'lets fix the 35 sonar issues'. Branch refactor/update-shape-cognitive-complexity, worktree ~/projects/openRepoShape--s3776-update-shape on Eagle, from origin/main d682127. Scope: the four python:S3776 functions in update-shape.py (classify, upstream_copies, rewrite_manifest, cmd_apply) split into named helpers until each scores 15 or less, with every refusal id, message, print order and exit code byte-identical.

  2. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T03:39Z) PULL REQUEST OPEN: #139 (head 184becb).

  3. brettheap commented on Sep 12, 2026

    @brettheap
    ContributorAuthor

    (lane openreposhape-2 (openRepoShape-2), 2026-09-12T14:47:00Z) MERGED — #139 → aa5912e (squash), on Brett Heap's word 'go, fan out on all of it, hold the doctor refactor'. update-shape.py: the four functions Sonar scored 24/19/16/47 for cognitive complexity now read 1/2/7/8 — classify hands each pinned row to _classify_row and the verdict ladder to _row_verdict, upstream_copies asks _tuple_constants and _listed_copies, rewrite_manifest asks _shape_block_bounds, and cmd_apply's phases and its two undo loops become fourteen helpers and a Rollback class; every refusal id, message, side-effect order and the --trailer path byte-identical; no pinned copy touched; complexipy before/after in the PR; PR-scoped Sonar total 0; Copilot two rounds, three doc nits taken, one rename declined on the record; 1136 passed / 1 skipped. Object complete.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions