Skip to content

fix(source-control): PR-body linkage validator trim is quadratic on long trailing whitespace and can time out #7069

Closed

Description

@kyle-sexton

Context

The source-control PR-body linkage validator (pr-linkage-validator.sh, shared by the pr-body-linkage-gate and pr-linkage-mcp-gate hooks) takes quadratic time on a section with a long run of trailing whitespace. trim_to strips trailing blanks with ${s%"${s##*[![:space:]]}"}; bash's longest-prefix match *[![:space:]] re-scans the string for each candidate position. Measured timings for trim_to on x plus N spaces: 2,000 blanks 0.24 s, 4,000 blanks 1.03 s, 8,000 blanks 4.15 s (each doubling quadruples). About 18,000 blanks goes past the hooks' 15 s timeout. A timed-out PreToolUse hook does not block, so a body built to have that much trailing whitespace skips the linkage gate.

Proposed work

  • Replace the trailing-trim parameter expansion in trim_to with a linear form (for example, read the non-blank prefix with a single anchored [[ =~ ]] match, or strip with an extglob %%+([[:space:]]) only after confirming it is linear in this bash). Check the leading-trim expansion for the same shape.
  • Check the other per-section and per-line helpers in the validator for the same pattern.

Acceptance criteria

  • A test feeds a PR body whose section ends in 20,000 trailing blanks and the validator returns its verdict well inside the hook timeout.
  • The existing pr-body-linkage-gate.test.sh and pr-linkage-mcp-gate.test.sh suites still pass.

References

  • plugins/source-control/hooks/pr-linkage-validator.sh (trim_to, called from section_content_to).
  • plugins/source-control/hooks/hooks.json (15 s timeouts).
  • The validator's own linkage::split_lines comment records an earlier quadratic fix of the same kind (per-line copies).

Metadata

Field Value
Category general
Area source-control hooks
Ecosystem bash

Activity

  1. added
    needs-triage
    on Oct 11, 2026
  2. added theissue type on Oct 11, 2026
  3. github-actions commented on Oct 11, 2026

    @github-actions
    Contributor

    Thanks for the detailed report. I confirmed trim_to in plugins/source-control/hooks/pr-linkage-validator.sh uses ${s%"${s##*[![:space:]]}"} for trailing-blank trimming, which matches the quadratic-rescan shape you describe, and that the hooks in hooks.json run under a 15s timeout. Since a timed-out PreToolUse hook doesn't block, a PR body engineered with enough trailing whitespace in a section would indeed skip the linkage gate rather than fail it.

    The repro steps, the specific function/line, and the two acceptance criteria (20,000-blank test case, existing gate test suites still passing) are clear enough to act on directly - nothing further needed from you right now.

    I didn't find an exact duplicate. #3509 and the other perf(*) hook-timeout issues cover timeouts caused by many external-process spawns, which is a different root cause than this parameter-expansion rescan, so I'm not marking this one as a duplicate of those.

    Automated triage. A maintainer reviews it.

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

    needs-triage

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions