Skip to content

templates/assembly-root/scripts/{validate-pins,validate-manifest,bootstrap}.py: split all eight cognitive-complexity-15+ functions into helpers, behaviour unchanged (#132) - #140

Merged
brettheap merged 9 commits into
mainfrom
refactor/assembly-template-scripts-cognitive-complexity
Sep 12, 2026
Merged

brettheap merged 9 commits into
mainfrom
refactor/assembly-template-scripts-cognitive-complexity

Conversation

@brettheap

@brettheap brettheap commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

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

Closes #132.

What was wrong

python:S3776 (cognitive complexity) flagged eight functions across the
assembly-root template's three digest-pinned, dependency-free scripts, all
over the 15 cap now that Brett Heap lifted the hold on that rule
("lets fix the 35 sonar issues", 2026-09-12T02:1xZ):

file function cc before cc after how it was split
validate-pins.py _check_leg 28 2 _load_leg_pin, _check_gitlink_matches_pin, _check_pin_digest, _check_workflow_refs_for_leg — one per fact being compared
validate-pins.py _check_shape_pin 17 4 _check_shape_pin_header (the pin's own fields) + _check_shape_copy (one files: row, called per loop iteration)
validate-pins.py _check_neutral_product_pins 32 2 _check_neutral_pin_entry_and_load, _check_neutral_pin_commit_and_digest_fields, _recompute_neutral_pin_digest, _check_one_neutral_product_pin (one call per loop entry)
validate-pins.py main 19 2 _parse_pin_sources, _load_legs_to_check, _run_all_checks (the one call inside main's try), _emit_report
validate-manifest.py _naming_findings 38 5 _naming_chain_field_finding, _naming_classification_findings, _naming_referent_findings, _naming_referent_declared_findings — one per question the function asked about a leg's naming: record
validate-manifest.py _findings 69 4 one helper per manifest field (_envelope_findings, _id_findings, _name_finding, _elected_by_finding, _elected_on_finding, _topic_finding, _reference_finding, _visibility_finding, _shape_findings, _neutral_product_pins_type_finding, _roles_finding, _owners_span_finding) plus a per-leg helper table (_leg_findings calling _leg_role_classification_finding and _leg_path_findings) for the loop over legs; the nested bad() closure became the module-level _finding() formatter
bootstrap.py read_authority 27 7 _load_register_rows, _row_report_line, _report_one_register (one call per register file found)
bootstrap.py main 30 8 _load_root_and_manifest (both refusal-guarded steps behind one try), _checkout_all_tracking_branches (step a), _run_neutral_validators (step b)

The first four were done and pushed before this session picked up the lane
(commits a4eef2a, 13065fc, b0f0a43, 653a01b); this PR's own work is
the last four, one commit per function (ef418d5, e58d21d, fd4837b,
f8d834e) plus a ninth, below, that only the PR's own SonarCloud analysis
found:

file function cc before cc after how it was split
validate-manifest.py _naming_referent_declared_findings 20 (Sonar; local complexipy had scored it 13, under the cap) 0 _how_referent_reached (the if/elif/else that built how_reached) plus one helper per one of the function's two already-independent, sequential if statements — _naming_referent_declared_mismatch_finding and _naming_referent_missing_pin_finding (commit 59fa834)

What landed

Three digest-pinned files touched, hunks kept minimal:

  • templates/assembly-root/scripts/validate-pins.py
  • templates/assembly-root/scripts/validate-manifest.py
  • templates/assembly-root/scripts/bootstrap.py

scripts/repo_shape.py (imported by all three) was read but not edited —
it belongs to a different cluster. No signature that anything else imports
or calls was changed; every message, exit code, exception type, side-effect
order and git-command sequence is byte-for-byte the same as before. This
moves code; it does not fix anything, and no bug was noticed while doing it
— nothing to file as a follow-up.

How it was proven

  • Local complexipy <file> -mx 15 after every split: all sixteen new
    helpers and all eight original functions pass (main in
    validate-pins.py 2, _findings 4, read_authority 7, main in
    bootstrap.py 8, highest new helper 13).
  • Per-function targeted runs before each commit: tests/test_manifest_validator.py
    (38 passed), tests/test_scaffold_e2e.py (44 passed), tests/test_naming_policy.py,
    tests/test_adopt_e2e.py, tests/test_adopt_spec_only.py,
    tests/test_reference_default.py, tests/test_scaffold_pin_and_reuse.py
    (276 passed together), tests/test_update_shape.py (36 passed).
  • Full suite in two bounded foreground chunks after the eighth commit:
    tests/test_[a-m]*.py — 366 passed, 1 skipped (the known flake-free skip);
    tests/test_[n-z]*.py — 770 passed. Total 1136 passed / 1 skipped, matching
    the count the brief expected on main.
  • No tool's --help text changed (no argparse call touched), so
    render-cli-reference.py was not re-run; tests/test_cli_reference.py
    passed unchanged as part of the [a-m] chunk.
  • The PR's own SonarCloud analysis (the authority complexipy only
    approximates) was queried after CI went green and came back with exactly
    one python:S3776 left, in a function complexipy had scored under the
    cap; tests/test_manifest_validator.py (38 passed) and
    tests/test_scaffold_e2e.py (44 passed) were re-run after splitting it
    further, and the query will be repeated once CI reruns on that push.

A workstation reboot mid-session corrupted the shared git object store with
zero-byte objects (this branch's head among them); the orchestrator
quarantined them and re-fetched from origin. git fsck --connectivity-only
came back clean (only harmless dangling objects, no missing/broken links)
and every commit, test run and complexipy measurement above was re-verified
against the recovered tree before this PR was opened.

🤖 Generated with Claude Code

brettheap and others added 8 commits September 11, 2026 23:11
…four decisions into helpers, cognitive complexity 28 -> 2 (#132)

_check_leg mixed four independent questions in one function: whether the pin
file's own fields look right, whether the gitlink agrees with the pin's
commit, whether the leg's tree digest recomputes, and whether every workflow
`@<sha>` reference agrees with the gitlink. SonarCloud's python:S3776 flagged
it at cognitive complexity 28, well over the 15 cap now that the hold on
python:S3776 is lifted.

Split into `_load_leg_pin`, `_check_gitlink_matches_pin`, `_check_pin_digest`
and `_check_workflow_refs_for_leg`, one per question, each named for what it
decides. `_check_leg` is now a five-line sequence of calls in the original
order. Every message, exit code, exception type and the sequence of findings
is unchanged — this moves code, it does not fix anything.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…pin's header check and per-file loop body into helpers, cognitive complexity 17 -> 4 (#132)

_check_shape_pin mixed checking the pin file's own commit fields with looping
over every pinned file's digest, which SonarCloud's python:S3776 flagged at
cognitive complexity 17.

Split into `_check_shape_pin_header` (the commit/kind/manifest-agreement
checks) and `_check_shape_copy` (one `files:` row, called once per loop
iteration instead of holding the row logic inline). `_check_shape_pin` keeps
the same `before = len(report.findings)` bracket around the loop so the
"digests match" note still depends on nothing having been found, exactly as
before. Every message, exit code and finding order is unchanged.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…l_product_pins' per-entry recheck into helpers, cognitive complexity 32 -> 2 (#132)

The whole re-check of one `neutral_product_pins:` entry — loading its pin
file, checking the commit and digest fields, resolving where to recompute
the referent's tree digest from, and comparing it — lived inline in the loop
body, which SonarCloud's python:S3776 flagged at cognitive complexity 32.

Split into `_check_neutral_pin_entry_and_load`, `_check_neutral_pin_commit_
and_digest_fields`, `_recompute_neutral_pin_digest` and
`_check_one_neutral_product_pin` (one call per loop iteration), each named
for the one question it answers. Every `continue` in the original loop
becomes a `return None` from the helper that used to hold it, and the
caller's `if ... is None: return` reads the same as the loop moving to the
next entry. Every message, exit code, exception type and finding/skip order
is unchanged.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…nt setup, per-leg checks and report emission into helpers, cognitive complexity 19 -> 2 (#132)

`main` built the `--pin-source` lookup tables, loaded the manifest and its
legs, ran every check, and printed the report, all inline — SonarCloud's
python:S3776 flagged it at cognitive complexity 19, driven mostly by the
list comprehension filtering non-assembly legs and the sequence of Refusal
guards around the manifest.

Split into `_parse_pin_sources`, `_load_legs_to_check`, `_run_all_checks`
(the single call inside `main`'s one `try`) and `_emit_report`. `main` is
now argument setup, the `try`/`except Refusal`, and one final call. Every
message, exit code and the exact sequence of checks (per-leg, then the
shape pin, then the neutral-product pins) is unchanged.

This finishes templates/assembly-root/scripts/validate-pins.py's four
flagged functions for #132 (`_check_leg`, `_check_shape_pin`,
`_check_neutral_product_pins`, `main`); the other two files it names are
next.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…indings' four questions into helpers, cognitive complexity 38 -> 5 (#132)

_naming_findings checked one leg's optional `naming:` record against the
naming policy in a single flat run: the chain field's shape, everything the
classifier's own result implies (a broken referent chain, its warnings,
form/role/also_matches agreement), whether a `<Domainx><Product>` name's
descendant_referent is one of its real forms, and — when referent_declared
is true — whether the pin file for the referent this leg actually holds
exists. SonarCloud's python:S3776 flagged it at cognitive complexity 38.

Split into `_naming_chain_field_finding`, `_naming_classification_findings`,
`_naming_referent_findings` and `_naming_referent_declared_findings`, one per
question. `_naming_referent_findings` returns its findings alongside a bool
standing in for the original function's early `return out` inside
`if not referents:`, so `_naming_findings` still stops there when a name
claims no descent. Every message, exit code and finding/warning order is
unchanged — this moves code, it does not fix anything.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…' per-field and per-leg checks into helpers, cognitive complexity 69 -> 4 (#132)

_findings ran every check of `project.yaml` inline — the envelope
(schema_version/kind/schema), id/name/election/topic, the optional
reference/visibility fields, the shape pin, and then a loop over `legs`
whose body itself classified each leg's name, checked its `naming:` record
and validated its path, all appending to one closure-captured `out` through
a local `bad()`. SonarCloud's python:S3776 flagged it at cognitive
complexity 69, by far the largest in this cluster.

Split into one helper per field this schema requires or admits
(`_envelope_findings`, `_id_findings`, `_name_finding`, `_elected_by_finding`,
`_elected_on_finding`, `_topic_finding`, `_reference_finding`,
`_visibility_finding`, `_shape_findings`, `_neutral_product_pins_type_finding`,
`_roles_finding`, `_owners_span_finding`) plus a per-leg helper table for the
loop (`_leg_findings`, calling `_leg_role_classification_finding` and
`_leg_path_findings`), each named for the one question it answers and called
from `_findings` in the exact original order — the id check's `bad()` closure
becomes the module-level `_finding()` formatter every one of them uses.
`_leg_findings` mutates the `owners` set and `paths` dict the loop already
threaded through, so the collision and owners-span checks still see the same
accumulated state, and every `continue` in the original loop is a `return`
from the helper that used to hold it. Every message, exit code and finding
order is unchanged — this moves code, it does not fix anything.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…per-register and per-row reporting into helpers, cognitive complexity 27 -> 7 (#132)

read_authority mixed the degrade-line guard, the loop over every register
file found, reading and defaulting its rows, and the loop over each row
checking whether it named this project, all in one function. SonarCloud's
python:S3776 flagged it at cognitive complexity 27.

Split into `_load_register_rows` (one register file's rows, `None` standing
in for the original inner loop's `continue` on an unreadable file),
`_row_report_line` (one row, formatted or `None` when it matches nothing)
and `_report_one_register` (one register file start to finish, called once
per file found). `read_authority` is now the degrade-line guard, building
`repositories`/`prefixes`, and one call per register file. Every message,
exit code and report order is unchanged — this moves code, it does not fix
anything.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
…p (a) and step (b) into helpers, cognitive complexity 30 -> 8 (#132)

`main` built the root and manifest behind two separate try/except Refusal
blocks, then ran step (a)'s tracking-branch placement, step (b)'s validator
loop and upstream notice, and the final pass/fail summary, all inline.
SonarCloud's python:S3776 flagged it at cognitive complexity 30 — the last
of this cluster's eight functions.

Split into `_load_root_and_manifest` (both steps behind `main`'s now-single
`try`, so either one's `Refusal` is still the same early exit 2),
`_checkout_all_tracking_branches` (step a) and `_run_neutral_validators`
(step b, returning the failed-labels list `main` used to accumulate
locally). `main` is now argument setup, the one `try`, and a five-line
sequence of calls in the original order. Every message, exit code and the
exact sequence of git commands `checkout_tracking_branch` runs is
unchanged — this moves code, it does not fix anything.

This finishes the eight functions #132 named across
templates/assembly-root/scripts/{validate-pins.py,validate-manifest.py,
bootstrap.py}.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Copilot AI lite review requested due to automatic review settings September 12, 2026 14:41
@brettheap

Copy link
Copy Markdown
Contributor Author

@codex review

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 3 days and 14 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@sourcery-ai

sourcery-ai Bot commented Sep 12, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR is a behavior-preserving refactor of the three assembly-root scripts: the eight functions exceeding cognitive-complexity limits are decomposed into focused helpers, while public signatures, control-flow effects, diagnostics, side-effect order, and CLI behavior remain unchanged. Review should focus on helper boundaries and preservation of early returns, loop state, report ordering, and exception behavior; the reported validation suite and complexity checks pass.

Sequence diagram for the refactored pin validation flow

sequenceDiagram
    participant CLI
    participant Main as validate-pins.main
    participant Checks as _run_all_checks
    participant Leg as _check_leg
    participant Shape as _check_shape_pin
    participant Neutral as _check_neutral_product_pins
    participant Report as _emit_report

    CLI->>Main: parse arguments
    Main->>Checks: _run_all_checks(root, report, keyed_sources, default_source)
    Checks->>Leg: _check_leg(root, leg, report, refs)
    Leg->>Leg: _load_leg_pin(...)
    Leg->>Leg: _check_gitlink_matches_pin(...)
    Leg->>Leg: _check_pin_digest(...)
    Leg->>Leg: _check_workflow_refs_for_leg(...)
    Checks->>Shape: _check_shape_pin(root, manifest, report)
    Shape->>Shape: _check_shape_pin_header(...)
    Shape->>Shape: _check_shape_copy(...) per files row
    Checks->>Neutral: _check_neutral_product_pins(...)
    Neutral->>Neutral: _check_one_neutral_product_pin(...) per entry
    Main->>Report: _emit_report(report, quiet)
    Report-->>CLI: exit code and diagnostics
Loading

Sequence diagram for the refactored bootstrap flow

sequenceDiagram
    participant CLI
    participant Main as bootstrap.main
    participant Load as _load_root_and_manifest
    participant Branches as _checkout_all_tracking_branches
    participant Validators as _run_neutral_validators
    participant Authority as read_authority

    CLI->>Main: parse arguments
    Main->>Load: _load_root_and_manifest(args)
    Main->>Branches: _checkout_all_tracking_branches(root, sub_legs, manifest, branch_arg)
    Branches->>Branches: checkout_tracking_branch(...) per leg
    Main->>Validators: _run_neutral_validators(root, manifest, skip)
    Validators->>Validators: _run(...) per validator
    Main->>Authority: read_authority(root, legs)
    Authority->>Authority: _report_one_register(...) per register
    Authority->>Authority: _load_register_rows(register_path)
    Authority->>Authority: _row_report_line(...) per matching row
Loading

Flow diagram for the refactored manifest findings pipeline

flowchart TD
    Find[_findings]
    Envelope[_envelope_findings]
    Fields[Field-specific finding helpers]
    Legs[_leg_findings]
    Naming[_naming_findings]
    Paths[_leg_path_findings]
    Owners[_owners_span_finding]

    Find --> Envelope
    Find --> Fields
    Find --> Legs
    Legs --> Naming
    Legs --> Paths
    Find --> Owners
Loading

File-Level Changes

Change Details Files
Decomposed pin validation into focused helpers while preserving validation order, diagnostics, refusal behavior, and report output.
  • Separated per-leg pin loading, gitlink comparison, digest verification, and workflow-reference checks.
  • Split shape-pin header validation from per-file copy checks.
  • Extracted neutral-product pin entry validation, field validation, digest recomputation, and per-entry orchestration.
  • Moved pin-source parsing, leg loading, check orchestration, and report emission out of main.
templates/assembly-root/scripts/validate-pins.py
Decomposed manifest validation into field-specific and per-leg finding helpers without changing finding semantics.
  • Extracted naming-record checks for chain fields, classification, referents, and declared referent state.
  • Extracted manifest envelope, identity, metadata, shape, roles, and neutral-pin type checks.
  • Extracted per-leg role/path checks and owners-span validation; replaced the local formatter closure with _finding().
  • Preserved accumulated owner/path state and early-return behavior across the leg loop.
templates/assembly-root/scripts/validate-manifest.py
Split bootstrap reporting and workflow stages into helpers while retaining command sequencing and error handling.
  • Extracted register loading, row formatting, and per-register reporting.
  • Extracted root/manifest loading, tracking-branch checkout, and neutral-validator execution.
  • Kept refusal handling, validator failure accumulation, output text, and exit behavior unchanged.
templates/assembly-root/scripts/bootstrap.py

Assessment against linked issues

Issue Objective Addressed Explanation
#132 Refactor the eight flagged functions in validate-pins.py, validate-manifest.py, and bootstrap.py so that each function and its resulting helpers remain at or below Sonar's cognitive-complexity cap of 15. ✅
#132 Preserve the existing behavior of the digest-pinned scripts, including messages, exit codes, exception handling, output ordering, file and repository side effects, and git-command execution order. ✅
#132 Apply the restructuring to the shipped template copies without modifying the unrelated scripts/repo_shape.py or introducing dependencies that violate the Python 3.9-safe, dependency-free requirement. ✅

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved review findings; the refactor preserves existing behavior.

Pull request overview

Refactors eight high-complexity functions in the assembly-root validation and bootstrap scripts while preserving behavior.

Changes:

  • Split pin, manifest, and naming validation into focused helpers.
  • Extracted bootstrap workflow and authority-reporting helpers.
  • Preserved interfaces, messages, ordering, and exit codes.
File summaries
File Summary
templates/assembly-root/scripts/validate-pins.py Decomposes pin and digest checks.
templates/assembly-root/scripts/validate-manifest.py Decomposes manifest and naming checks.
templates/assembly-root/scripts/bootstrap.py Decomposes bootstrap steps and authority reporting.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: f8d834eec7

ℹ️ 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".

…eferent_declared_findings' two questions into helpers, cognitive complexity 20 -> 0 (#132)

The PR's own SonarCloud analysis (opensoft_openRepoShape#140) still flagged
one function after the first eight: local `complexipy` had scored
`_naming_referent_declared_findings` at 13, under the 15 cap, but
python:S3776's own recompute put it at 20 — the two ternary-in-string
expressions building `how_reached` and the missing-pin message weigh more
under Sonar's actual rule than complexipy's approximation of it.

Split into `_how_referent_reached` (the if/elif/else that used to build
`how_reached`, called instead of assigned inline), and one helper per
independent `if` the function already had — `referent_declared`'s mismatch
against what was reached, and the missing-pin-file check that runs only
when it is `true` — since both were already separate, sequential checks
that never depended on each other's result. Every message, exit code and
finding order is unchanged.

Lane: openreposhape-2 (openRepoShape-2)

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Copilot AI review requested due to automatic review settings September 12, 2026 14:57
@sonarqubecloud

Copy link
Copy Markdown

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No unresolved review comments remain, and all reviewers assessed it as ready.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@brettheap
brettheap merged commit 6e9ae93 into main Sep 12, 2026
6 checks passed
@brettheap
brettheap deleted the refactor/assembly-template-scripts-cognitive-complexity branch September 12, 2026 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants