Skip to content

recorded_gitlink() reads the index before HEAD, so a staged leg bump validates - #25

Merged
brettheap merged 2 commits into
mainfrom
fix/recorded-gitlink-index-first
Sep 4, 2026
Merged

brettheap merged 2 commits into
mainfrom
fix/recorded-gitlink-index-first

Conversation

@brettheap

@brettheap brettheap commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Lane: repo-shape
Claim: #20 (comment)

Closes #20.

What was wrong

scripts/repo_shape.py::recorded_gitlink() read git ls-tree HEAD -- <path>
FIRST and fell back to the index (git ls-files -s) only when HEAD recorded
no gitlink there. Its own docstring said the index fallback exists for
"precisely the moment the lockstep rule is about to be broken" — the order was
inverted against its stated reason.

The moment it bites

An operator bumps a leg — git add legs/<Leg> moves the gitlink in the INDEX
— edits contracts/<role>-pin.yaml to match, and runs make validate before
committing, which is the whole point of running it. HEAD still records the old
gitlink, so templates/assembly-root/scripts/validate-pins.py reported a FALSE
pin-gitlink-mismatch on a correct bump, and
templates/family-root/scripts/validate-family.py reported
member-gitlink-mismatch the same way. The only way to clear the finding was
to commit the thing the validator had just called wrong.

It failed in the other direction too, and that half is worse: with the gitlink
staged at a new commit and the pin left behind, reading HEAD compared the pin
against the commit it agrees with and printed pins ok — a green check over
drift, one commit before the drift lands.

What changed

Index first, HEAD second. In a clean tree the two agree and the order is
invisible; in CI, which validates a checkout of a commit, behaviour is
unchanged. The order decides the answer only when the two disagree, and then
the index is what the next commit will record. HEAD stays as the fallback, for
a path the commit records and the index does not. The docstring now says index
first, HEAD second, and why.

A second defect, found on the way: the fallback could never have worked
anyway. Both listings were parsed at column 2, but they do not agree on that
column — ls-tree emits <mode> SP <type> SP <oid> TAB <path> while
ls-files -s emits <mode> SP <oid> SP <stage> TAB <path>. Reading the index
therefore returned the STAGE, the literal string 0, rather than a commit.
It never surfaced because HEAD answered first in every tree anyone ran this
in. The oid column now travels beside each argv in GITLINK_LISTINGS rather
than being assumed the same for both.

No copy of repo_shape.py lives under templates/: scripts/shape_materialize.py
copies scripts/repo_shape.py out of this repository's own tree into a
scaffolded assembly root (COPIED_FROM_SHAPE) and family root
(FAMILY_COPIED_FROM_SHAPE), so there is one source file and nothing to keep
in lockstep here.

What the tests prove

Three tests, each of which FAILS on main and passes here:

  • test_a_staged_bump_with_the_pin_moved_to_match_passes — a
    staged-but-uncommitted spec leg bump whose pin moved with it now validates
    (before: pin-gitlink-mismatch, exit 1).
  • test_a_staged_bump_the_pin_did_not_follow_is_still_a_finding — the same
    staged bump with the pin left behind is still a finding, so the index
    winning is not "whichever answer passes" (before: pins ok, exit 0).
  • test_validate_accepts_a_member_bump_that_is_staged_but_not_committed — the
    family's half of the same reading, a staged member bump with the
    members[].pin.commit row moved to match.

Each uses an EMPTY commit for the leg, so the tree is unchanged, the pinned
tree_sha256 still recomputes, and the gitlink is the only fact that moved.
Full suite, run as CI runs it (python3 -m pytest tests -q): 349 passed
before, 353 passed after
, no existing test changed.

Follow-up commit: stage 0 or nothing

Taken from Copilot's review. A conflicted merge leaves the index holding the
same path at stages 1, 2 and 3 — base, ours and theirs — and no stage 0, and
ls-files -s lists them in that order, so reading the first 160000 row
would have answered with the MERGE BASE. None of the three is a commit
anybody is about to make, so an unmerged path now falls through to HEAD,
which is what the fallback is for. test_an_unmerged_index_falls_back_to_head
writes those stages straight into a scaffolded project's index with
update-index --index-info and asserts the validator still passes; without
the stage filter it reports pin-gitlink-mismatch against the base commit.
The family test's manifest edit is also pinned to one occurrence.

How a project picks this up

The same way it picks up any change to a copied shape file:
python3 update-shape.py, which re-copies scripts/repo_shape.py and re-pins
contracts/shape-pin.yaml.

🤖 Generated with Claude Code

https://claude.ai/code/session_015WekBEXf55Xv64psGe5t3T

…validates

THE VALIDATOR ANSWERED ABOUT THE COMMIT BEING REPLACED. `recorded_gitlink()`
read `git ls-tree HEAD` first and fell back to the index only when HEAD
recorded no gitlink at that path — so at the one moment the answer matters,
it was the wrong one. An operator bumps a leg (`git add legs/<Leg>` moves the
gitlink in the INDEX), edits `contracts/<role>-pin.yaml` to match, and runs
`make validate` before committing, which is the whole point of running it.
HEAD still records the old gitlink, so `validate-pins.py` reported a FALSE
`pin-gitlink-mismatch` on a correct bump; `validate-family.py` did the same
with `member-gitlink-mismatch`. The only way to clear the finding was to
commit the thing the validator had just called wrong.

It failed in the other direction too, and that half is worse: with the gitlink
staged at a new commit and the pin left behind, reading HEAD compared the pin
against the commit it agrees with and printed `pins ok`. A green check over
drift, one commit before the drift lands.

The docstring already said the index fallback exists for "precisely the moment
the lockstep rule is about to be broken" — the order was simply inverted
against its own stated reason. Index first, HEAD second. In a clean tree the
two agree and the order is invisible; in CI, which checks out a commit,
nothing changes at all. It decides the answer only when they disagree, and
then the index is what the next commit will record. HEAD stays as the
fallback, for a path the commit records and the index does not.

THE FALLBACK COULD NEVER HAVE WORKED ANYWAY. Both listings were parsed at
column 2, but they do not agree on that column: `ls-tree` emits
`<mode> SP <type> SP <oid> TAB <path>` and `ls-files -s` emits
`<mode> SP <oid> SP <stage> TAB <path>`. Reading the index therefore returned
the STAGE — the literal string `0` — rather than a commit. That never
surfaced because HEAD answered first in every tree anyone ran this in. The
oid column now travels beside each argv in `GITLINK_LISTINGS` rather than
being assumed to be the same for both.

Three tests, each failing before this change: a staged-but-uncommitted spec
leg bump whose pin moved with it now PASSES (it reported
`pin-gitlink-mismatch`); the same bump with the pin left behind is still a
finding (it printed `pins ok`); and the family's half of the same reading, a
staged member bump with the `members[].pin.commit` row moved to match. Each
uses an EMPTY commit for the leg, so the tree is unchanged, the pinned
`tree_sha256` still recomputes, and the gitlink is the only fact that moved.
352 pass, from 349.

A project picks this up the way it picks up any change to a copied shape
file: `python3 update-shape.py`, which re-copies `scripts/repo_shape.py` and
re-pins `contracts/shape-pin.yaml`.

Lane: repo-shape

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_015WekBEXf55Xv64psGe5t3T
Copilot AI lite review requested due to automatic review settings September 4, 2026 05:02
@sourcery-ai

sourcery-ai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Updates gitlink resolution to use the index first, ensuring validators evaluate the commit a staged change would create rather than the superseded HEAD commit, fixes command-specific OID parsing, and adds regression tests for both passing and failing staged bumps.

Sequence diagram for staged gitlink validation

sequenceDiagram
    participant Operator
    participant Validator
    participant recorded_gitlink
    participant Index
    participant HEAD

    Operator->>Validator: validate-pins.py or validate-family.py
    Validator->>recorded_gitlink: recorded_gitlink(repo, path)
    recorded_gitlink->>Index: git ls-files -s -- path
    Index-->>recorded_gitlink: staged gitlink OID
    recorded_gitlink-->>Validator: index OID
    Validator->>Validator: compare pin commit with index OID
    Validator-->>Operator: pass or mismatch finding
Loading

File-Level Changes

Change Details Files
Make staged gitlinks authoritative when validating lockstep pins, while retaining HEAD as a fallback.
  • Changed recorded_gitlink() to inspect the index before HEAD.
  • Added per-command OID column metadata so ls-files and ls-tree output are parsed correctly.
  • Updated the docstring to explain staged-validation semantics and fallback behavior.
scripts/repo_shape.py
Add regression coverage for staged leg and family-member bumps before commit.
  • Verify a staged bump with its pin updated passes validation.
  • Verify a staged bump with a stale pin remains a validation error.
  • Cover staged family-member validation separately.
  • Use empty commits to isolate gitlink selection from tree-digest changes.
tests/test_pin_validator.py
tests/test_family.py

Assessment against linked issues

Issue Objective Addressed Explanation
#20 Make recorded_gitlink() consult the index before HEAD so validators evaluate staged gitlink changes, while retaining HEAD as a fallback. ✅
#20 Correctly parse the gitlink object ID from both git ls-files -s and git ls-tree output formats. ✅
#20 Add regression coverage showing that staged, matching gitlink and pin updates pass validation for both pin and family validators, while unmatched staged updates remain findings. ✅

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

sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Sep 4, 2026

@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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

🟡 Changes recommended

recorded_gitlink() can mis-read git ls-files -s entries in an unmerged index (stages 1/2/3), and the new family test uses an unconstrained replace that could rewrite multiple rows and become flaky.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes gitlink/pin validation to compare pins against the staged index state (what the next commit will record) before falling back to HEAD, aligning behavior with how operators actually run make validate during a leg/member bump and correcting an index-parsing defect.

Changes:

  • Update recorded_gitlink() to read the index before HEAD and correctly extract the oid column from both ls-files -s and ls-tree.
  • Add tests covering staged-but-uncommitted leg bumps (both matching-pin and stale-pin cases).
  • Add a test covering staged-but-uncommitted family member bumps where the manifest row moves with the staged gitlink.
File summaries
File Description
scripts/repo_shape.py Make gitlink reading index-first (with corrected oid parsing) so validators reflect staged bumps.
tests/test_pin_validator.py Add coverage for staged leg bump validation (pass when pin matches staged gitlink; fail when it doesn’t).
tests/test_family.py Add coverage for staged family member bump validation when the manifest pin moves with the staged gitlink.
Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Lite

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

Comment thread scripts/repo_shape.py Outdated
Comment on lines +638 to +641
for line in out.splitlines():
fields = line.replace("\t", " ").split()
if len(fields) >= 3 and fields[0] == "160000":
return fields[2].lower()
if len(fields) > oid_at and fields[0] == "160000":
return fields[oid_at].lower()
Comment thread tests/test_family.py Outdated
Comment on lines +381 to +382
manifest_path = holder / "family.yaml"
manifest_path.write_text(manifest_path.read_text().replace(before, after))
Copilot's review of #25, both points taken.

A CONFLICTED MERGE HOLDS THE SAME PATH THREE TIMES. `git ls-files -s` emits
stages 1, 2 and 3 — base, ours and theirs — and no stage 0, and it lists them
in that order, so reading the first `160000` row answered with the MERGE BASE.
None of the three is a commit anybody is about to make, so an unmerged path
now falls through to HEAD, which is what the fallback is for. `stage_at`
travels beside the oid column in `GITLINK_LISTINGS`; `ls-tree` has no stage
column and carries `None`.

`test_an_unmerged_index_falls_back_to_head` writes the three stages straight
into a scaffolded project's index with `update-index --index-info` — what a
conflicted `git merge` leaves behind, without needing two branches that
disagree about a submodule — and asserts the validator still passes. Without
the stage filter it reports `pin-gitlink-mismatch` against the base commit.

The family test's manifest edit is also pinned to ONE occurrence and asserts
the pin it is moving is unique, so a fixture where two members happened to
share a commit could not rewrite both rows.

353 pass, from 352.

Lane: repo-shape

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_015WekBEXf55Xv64psGe5t3T
Copilot AI review requested due to automatic review settings September 4, 2026 05:09
@sonarqubecloud

sonarqubecloud Bot commented Sep 4, 2026

Copy link
Copy Markdown

@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.

Sourcery assessment

Approved.

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

The change is narrowly scoped, aligns behavior with the documented intent, and is backed by targeted regression tests that fail on main and pass with this patch.

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

@brettheap
brettheap merged commit 49773d8 into main Sep 4, 2026
4 checks passed
@brettheap
brettheap deleted the fix/recorded-gitlink-index-first branch September 4, 2026 05:13
brettheap added a commit that referenced this pull request Sep 4, 2026
…dex-first) into fix/reference-by-election-date

One conflict, in adopt-project.py _template_values: main derives project_id
via plan.project_id (#24); this branch reads elected_on once beside it. Both kept.

Lane: codex-intake-shape
Co-Authored-By: Claude Fable 5.1 <[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

Development

Successfully merging this pull request may close these issues.

recorded_gitlink() reads HEAD before the index: a staged leg bump reports a false pin-gitlink-mismatch

2 participants