Skip to content

The pre-commit test lint refuses a merge of origin/main, linting main's own lines as added #1456

Closed

Description

@V3RON

What happens. On a branch whose base predates a change to `src/contract/`, `git merge origin/main` cannot be committed: the pre-commit `tests` step fails on lines that are already on `main` and that the branch never touched. The tool guard (`.agents/scripts/tool-guard.mjs`) refuses a non-fast-forward push and says to "merge the remote branch in and push again", so the two together leave no path for a branch that has fallen behind.

Where. `lefthook.yml:33` runs `node .agents/scripts/test-lint.mjs --base HEAD {staged_files}`. During a merge, the staged files include every `src/contract/*.ts` file main changed, and `--base HEAD` (the branch's old tip) counts all of main's lines in them as added (`.agents/scripts/test-lint.mjs:221`). Lines that predate the always-in-scope 8 check, or that a file split moved (`2dfd3ba7`, #1345), then fail it.

Seen. Merging `origin/main` (`14163a99`) into `bug/1103-repro` (base `944d2549`): 41 findings, all in `src/contract/{status,config,device,report,lease}-schemas.ts`, for example `src/contract/config-schemas.ts:62: bound this wire input with .max(), .length() or .regex() ...`. The only file the branch changes, `src/agent-device/gateway-front.test.ts`, was not flagged, and the same change cherry-picked onto `origin/main` committed cleanly.

Found while triaging #1103.

Written by an agent.

Activity

  1. added
    bug:new
    on Nov 8, 2026
  2. self-assigned this
    on Nov 10, 2026
  3. V3RON commented on Nov 10, 2026

    @V3RON
    ContributorAuthor

    Reproduction

    Defect, confirmed as reported. src/test-lint.test.ts, "in a merge the commit hook judges only the lines the merge adds, not those the merged branch brings" (branch bug/1456-repro). The fixture merges a branch that adds an unbounded z.string() in src/contract/a.ts and an it.skip in src/a/old.test.ts. The merge itself adds an it.only. The test runs the hook's command, --base HEAD <staged files>, before the merge is committed. Only the it.only should be flagged. All three are flagged:

    pnpm exec vitest run --project unit src/test-lint.test.ts
    
      "src/a/mine.test.ts:2: never skip, focus or defer a test ... (testing 5)",
    + "src/a/old.test.ts:2: never skip, focus or defer a test ... (testing 5)",
    + "src/contract/a.ts:2: bound this wire input with .max() ... (always-in-scope 8)",
    

    Root cause

    .agents/scripts/comments.mjs:36 (changedFiles, which test-lint.mjs imports) computes merge-base(HEAD, HEAD) = HEAD and diffs it against the working tree, so it never looks at MERGE_HEAD. While a merge is in progress, every line the merged branch brings shows up as added. Main's older lines then fail whatever checks they predate. The symptom shows in test-lint.mjs. movedContractDeclarations (test-lint.mjs:221) uses the same base, but it only excuses lines, so it cannot add a finding.

    Simplest fix

    In changedFiles, when no --head is given and MERGE_HEAD exists, keep only the lines that are added against HEAD and also against MERGE_HEAD:

    const added = addedLines(path, lines.length, range, head);
    // why: in a merge, a line the merged branch already holds is not one this commit adds.
    if (head === undefined && mergeHead() !== null) {
      const theirs = addedLines(path, lines.length, [mergeHead()], head);
      for (const n of added) if (!theirs.has(n)) added.delete(n);
    }

    mergeHead() is git rev-parse -q --verify MERGE_HEAD, or null when no merge is in progress. Tried it:

    pnpm exec vitest run --project unit src/test-lint.test.ts src/comment-lint.test.ts src/comments.test.ts
    Test Files  3 passed (3)   Tests  30 passed (30)
    

    Alternatives rejected

    • skip: [merge] on the lefthook tests step: then lines the merge itself adds, such as conflict resolutions, are never linted. Nothing else runs this lint.
    • A merge-specific --base in lefthook.yml: this needs shell logic in the hook, and the comment lint would stay broken.

    Risk

    • The comment lint also uses changedFiles, so it stops judging the merged branch's comments too. This is intended.
    • A merge whose resolution reformats a line counts that line as added. This is correct, and the same as a normal commit.

    Fast track

    • Defect: yes
    • Fix tried: yes
    • Nothing public added or broken: yes
    • No ADR or rule changed: yes
    • One fix: no — skipping the step in merges is a choice with different output

    Written by an agent.

  4. removed their assignment
    on Nov 10, 2026
  5. added
    bug:ready
    on Nov 10, 2026
  6. self-assigned this
    on Nov 10, 2026
  7. removed
    bug:new
    on Nov 10, 2026
  8. added a commit that references this issue on Nov 10, 2026
    fc8a897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug:ready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions