Skip to content

ci: gate file size, function complexity and import cycles - #3765

Open
jordan-simonovski wants to merge 4 commits into
mainfrom
jordansimonovski/file-size-and-complexity-gates
Open

jordan-simonovski wants to merge 4 commits into
mainfrom
jordansimonovski/file-size-and-complexity-gates

Conversation

@jordan-simonovski

Copy link
Copy Markdown
Contributor

Summary

The 300-line file limit only existed as a sentence in the docs, and 239 source files are over it, nine of them past 2,000 lines. This makes the limit, and a set of function-level limits, something CI enforces, so the numbers stop rising while they are worked down.

Outcomes

  • A PR that pushes a source file past 300 lines, or adds lines to a file already over it, fails the ratchet. Tests, stories, migrations and .d.ts files are exempt.
  • A PR cannot raise a package's count of long functions (over 150 lines), complex functions (over 20), deep nesting (over 4), long parameter lists (over 4), nested ternaries or import cycles.
  • Each run of the lint job shows, on its summary page, the largest files and the warning count per rule and package against its limit. The report is advisory and cannot fail a run.
  • Moving a large file, or splitting it into parts no longer than the original, is accepted with yarn ratchet:update.

Behaviour changes for contributors

  • A small fix that adds lines to an oversized file now fails CI. The new code goes in another file, or the PR runs yarn ratchet:update --allow-growth and says why. Plain yarn ratchet:update only records files that shrank or moved.
  • Pre-commit now rejects a changed line that carries one of the new warnings, for example an edit to an existing nested ternary.
  • cli and hdx-eval had a warning limit of 0. They now sit at 74 and 44, the count the new rules find today.

Key decisions

  • The new lint rules are warnings held at today's count, not errors. Errors would need about 1,000 existing violations fixed or suppressed first.
  • File size is tracked per file, not as a count per package. A count would let a 4,000-line file keep growing.
  • The thresholds (150, 20, 4, 4) are a starting point and are open to debate in review.

Known limits

  • Two PRs that each pass can together take a file over 300 lines or add a warning, which fails main until one is fixed. 22 files sit between 281 and 300 lines.
  • A function already over a limit can keep growing, because the lint limit is a count.
  • The warning limits were measured on macOS. If CI on ubuntu counts differently, this PR's lint job will show it.

How to reproduce

Starting point: a checkout of this branch, yarn install done.

On main:

  1. Append a line to packages/api/src/utils/zod.ts (1,494 lines).
  2. Run yarn ratchet. It passes.

On this branch:

  1. Append a line to packages/api/src/utils/zod.ts.
  2. Run yarn ratchet. It fails with 1495 lines > baseline 1494.
  3. Run yarn ratchet:update. It refuses to record the growth.
  4. Run node scripts/ci/quality-report.mjs to see the report the lint job publishes.

How to test on Vercel preview

N/A, non-UI change.

The 300-line file limit was only written down, and 239 source files are
over it. The ratchet now fails when a file crosses 300 lines or a file
already over it grows. oxlint warns on long or complex functions, deep
nesting, long parameter lists, nested ternaries and import cycles, with
each package's warning limit set to today's count so it cannot rise.

The lint job writes a summary of file sizes and warnings by rule to the
run page so the totals are visible while they are worked down.
@jordan-simonovski
jordan-simonovski requested a review from a team as a code owner November 22, 2026 23:01
@vercel

vercel Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

2 Skipped Deployments
Project Deployment Actions Updated
hyperdx-oss Ignored Ignored Preview Nov 23, 2026 5:53am UTC
hyperdx-storybook Ignored Ignored Preview Nov 23, 2026 5:53am UTC

Request Review

@changeset-bot

changeset-bot Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: d342943

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the review/tier-3 Standard — full human review required label Nov 22, 2026
@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

🟡 Tier 3 — Standard

Introduces new logic, modifies core functionality, or touches areas with non-trivial risk.

Why this tier:

  • Diff size: 586 production lines changed (Tier 2 max: < 250)
  • Cross-layer change: touches frontend (packages/app) + backend (packages/api) + shared utils (packages/common-utils)

Additional context: touches background tasks or the delivery pipeline lightly (27 lines, under the 30-line bar for Tier 4), 1 file(s) in private internal-tooling packages, excluded from the line count

Review process: Full human review — logic, architecture, edge cases.
SLA: First-pass feedback within 1 business day.

Stats
  • Production files changed: 9
  • Production lines changed: 586 (+ 142 in test files, excluded from tier calculation)
  • Branch: jordansimonovski/file-size-and-complexity-gates
  • Author: jordan-simonovski

To override this classification, remove the review/tier-3 label and apply a different review/tier-* label. Manual overrides are preserved on subsequent pushes.

@github-actions

Copy link
Copy Markdown
Contributor

Duplicate code analysis

No new duplicate code introduced by this PR.


jscpd compared this branch against origin/main
across app, api and common-utils. Only new clones are listed; the codebase
currently holds 108 in total (1.13%).
Run yarn dupes locally to reproduce, or yarn dupes:all for the full picture.

Comment thread scripts/ci/ratchet.mjs Outdated
}

/** A baselined file that is gone, or now under the limit, and was at least this long. */
function movedFrom(lines, current, baseline) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 P2 — movedFrom accepts any new large file as a 'move' if some unrelated baselined file has dropped out and was at least as long

movedFrom matches on line count alone. It also treats a file that shrank under 300 lines as a move source. Shrinkage is only a warning (compareSizes line 177), so stale entries stay in the committed baseline. Any later PR can then add an unrelated new file, for example a 900-line one while fixtures.ts: 1010 is stale, and plain yarn ratchet:update records it without --allow-growth. One source can also license any number of new files. Fix: require a real link to the source, such as the same basename or the source deleted in this diff, and consume each source once, or limit the parts' total to the source's size. Add a test where an unrelated new file and a stale entry exist together.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d342943, same change as the P1 thread: sources must be deleted, each is consumed by line count, and a same-named source is preferred. Not requiring a matching basename, since splitting a file into differently named parts is the case this exists for.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

PR Review

If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:

  • 🔴 P0/P1 in code this PR adds or changes: fix before asking for review.
  • 🟡 P2 in code this PR changes: your call. Fix if small, otherwise reply.
  • 🔵 P3 is collapsed at the bottom: nobody needs to act on it before merge.
  • Anything about surrounding code, or asking you to widen the change (hoist a helper,
    dedupe with another file, fix other call sites): never fix here, whatever the
    priority. Reply with one sentence; if it is P0, say so plainly so a human sees it.

One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.

3 finding(s): 🔴 0 P0/P1 · 🟡 2 P2 · 🔵 1 P3

1 posted as inline comment(s) on the changed lines. 1 listed below. 1 P3 folded at the bottom.

Findings outside the changed lines

  • 🟡 P2 scripts/ci/quality-report.mjs:703 — Warnings with no rule ID are grouped under a null row in the 'By rule' table → toResults in scripts/ci/oxlint-warnings.mjs sets ruleId: null for diagnostics that have no code. One example is unused disable directives, which oxlint/base.jsonc reports as warnings (reportUnusedDisableDirectives: "warn"). rules[w.ruleId] turns that key into the string "null", so the report shows a row named `null`. Fix: count them under a readable key, e.g. rules[w.ruleId ?? 'unused-disable-directive / other'].
🔵 1 P3 — no action needed before merge
  • 🔵 P3 scripts/ci/quality-report.mjs:679 — commentBody repeats the excess-line and per-package total maths from sizeSection/lintSection → The excess-line sum (n - MAX_FILE_LINES), the per-package rule total, and the ${pkg} ${total}/${limit ?? '?'} string are each written twice in this file: once for the job summary and once for the PR comment. A change to one copy can leave the summary and the comment showing different numbers. Fix: pull out excessLines(large) and packageTotals(byPackage) helpers and use them in all three functions.

Priority comes from a separate grading pass over the finished review. P3 is folded here instead of posted inline. No finding blocks merge automatically; a maintainer decides.

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Deep Review

✅ No critical issues found. This is a well-constructed CI-tooling change: the ratchet logic is sound, the advisory report step is correctly isolated from the gate (continue-on-error + outcome == 'success' gating), the third-party action is SHA-pinned, and pull-requests: write is inert for forks because the workflow runs on pull_request (not pull_request_target). The three prior inline review threads are all addressed in d34294306: findMoves now requires the move source to be gone from disk (!exists(file)), consumes source capacity by line count, and prefers a same-basename source; the report step builds common-utils first with timeout-minutes: 3.

🟡 P2 — recommended

None.

🔵 P3 nitpicks (2)
  • scripts/ci/quality-report.mjs:93 — lintPackage reads the ceiling with /--max-warnings (\d+)/, which only matches the space-separated form; a package switching to --max-warnings=N would silently render its limit as ? in the report while oxlint-warnings.mjs still enforces it, so the report would understate the gate.
    • Fix: Match both spellings, e.g. /--max-warnings[= ](\d+)/, to mirror parseArgs in oxlint-warnings.mjs.
  • scripts/ci/__tests__/ratchet.test.mjs:172 — the escape-hatch baseline has a guard test asserting the committed ratchet-baseline.json is not already breached by the real repo, but there is no parallel guard for the new file-size-baseline.json, so a baseline that is stale versus the CI environment is caught only by the live ratchet step, not by a unit test.
    • Fix: Add a test asserting compareSizes(collectLargeFiles(repoRoot), <committed file-size baseline>, exists).failed === false.

Reviewers (5): security, correctness, maintainability, testing, previous-comments.

Testing gaps:

  • No test asserts the PR-comment step stays fork/dependabot-gated; a future loosening of the if: condition would go undetected.
  • Line- and warning-count baselines were measured on macOS while CI runs ubuntu (author-acknowledged); divergence is self-revealing on this PR's own lint job but has no offline guard.
  • pull-requests: write is safe only because the trigger is pull_request; a later switch to pull_request_target would make the fork gate the sole barrier against an untrusted write token.

No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond

@github-actions

github-actions Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

E2E Test Results

✅ All tests passed • 471 passed • 1 skipped • 1065s

Status Count
✅ Passed 471
❌ Failed 0
⚠️ Flaky 13
⏭️ Skipped 1

Tests ran across 4 shards in parallel.

View full report →

The report was only on the run summary page, which nobody opens. The lint
job now posts it as one comment per PR, updated on each push, with the
tables collapsed.
Comment thread scripts/ci/ratchet.mjs Outdated
}

/** A baselined file that is gone, or now under the limit, and was at least this long. */
function movedFrom(lines, current, baseline) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔴 P1 — movedFrom counts a file that shrank under 300 lines as a move source, so plain ratchet:update accepts new oversized files

movedFrom treats any baseline file missing from current as the place a file moved from. current only lists files over 300 lines, so a file that still exists but dropped to 300 lines or fewer also counts. Example: a PR trims packages/app/src/ChartUtils.tsx (baseline 1612) to 290 lines and grows an unrelated new foo.ts to 1500 lines. CI says "moved or split from ChartUtils.tsx", and nextSizeBaseline records foo.ts without --allow-growth. A single freed entry can also license any number of new files at once. Only count baseline paths that no longer exist on disk (pass the set of existing files, or check existsSync), and let each source cover at most the line total it freed. Add a test where the source file still exists but shrank.

Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d342943: a move source must now be gone from disk, and it covers new files only up to its own line count. Tests added for a shrunk-but-present source and for one source split across several files.

run: node scripts/ci/ratchet.mjs
# Advisory: the gates are the ratchet above and each package's
# --max-warnings. This only makes the totals visible on the run page.
- name: Code quality report

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 P2 — Quality report runs type-aware oxlint on every package, but upstream dist is only built for affected projects

The lint step uses nx affected, so on a PR that touches no package (docs or CI only) common-utils is never built. The report then lints all five packages type-aware with @hyperdx/common-utils/dist/* unresolved. The comment can post counts that don't match what ci:lint gates against each --max-warnings limit. Fix: build common-utils before this step, or report only the packages nx affected linted. Two more risks: it runs under if: always() after a failed install, and the 2-minute timeout covers five type-aware runs.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in d342943: the report step builds common-utils first, and its timeout is 3 minutes. Left if: always(): a failed install makes the step fail, which continue-on-error absorbs and which skips the comment.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Code quality report

  • 239 source files over 300 lines, 100577 lines over the limit in total
  • oxlint warnings (count/limit): api 437/437, app 1161/1161, cli 74/74, common-utils 233/233, hdx-eval 44/44

Full report

A baselined file that shrank under 300 lines, or any unrelated deleted
file, let a new oversized file through plain ratchet:update. A source now
has to be gone from disk and covers new files only up to its own line
count. Also builds common-utils before the quality report so its
type-aware counts match what ci:lint gates on.
runs-on: ubuntu-24.04
permissions:
contents: read
pull-requests: write

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 P2 — The whole lint job now gets pull-requests: write, though only the last comment step needs it

Every step in the job runs with a token that can write to PRs, including yarn install lifecycle scripts, nx tasks and PR-authored scripts, and so do push runs, which never comment. Only the comment step needs that scope. Fix: drop pull-requests: write from lint, upload quality-comment.md as an artifact, and post it from a small follow-up job (needs: lint, if: github.event_name == 'pull_request') that alone has pull-requests: write.

Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond

This branch was successfully deployed

2 active (outdated) deployments
Preview – hyperdx-oss — 3a23bbd1 Deployed Nov 22, 2026 by vercel[bot]
Preview – hyperdx-storybook — 3a23bbd1 Deployed Nov 22, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review/tier-3 Standard — full human review required

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants