Skip to content

Add Swift file length budget CI guard - #3223

Merged
lawrencecchen merged 3 commits into
mainfrom
feat-swift-file-length-budget
Apr 28, 2026
Merged

lawrencecchen merged 3 commits into
mainfrom
feat-swift-file-length-budget

Conversation

@lawrencecchen

@lawrencecchen lawrencecchen commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary:\n- Add a Swift file length budget for cmux-owned Swift files at or above 500 lines.\n- Add a guard script that fails when tracked files grow or new files cross the threshold without an explicit budget update.\n- Wire the guard into workflow-guard-tests.\n\nTesting:\n- python3 -m py_compile scripts/swift_file_length_budget.py\n- python3 scripts/swift_file_length_budget.py --budget .github/swift-file-length-budget.tsv\n- ./tests/test_ci_swift_file_length_budget.sh\n- ./tests/test_ci_swift_warning_budget.sh\n- ./tests/test_ci_self_hosted_guard.sh\n- ./tests/test_ci_unit_test_spm_retry.sh\n- ./tests/test_ci_scheme_testaction_debug.sh\n- ./tests/test_ci_create_dmg_pinned.sh\n- node scripts/release_asset_guard.test.js\n- ./tests/test_ci_ghosttykit_checksum_present.sh\n\nNote:\n- ./tests/test_ci_ghosttykit_checksum_verification.sh fails locally on macOS because the fixture tar includes ._GhosttyKit.xcframework, but this existing guard passed in CI on #3220.


Summary by cubic

Adds and enforces a Swift file length budget for cmux-owned files ≥500 lines. CI fails if a tracked file grows or a new file crosses the threshold without updating the budget.

  • New Features

    • Guard script (scripts/swift_file_length_budget.py) scans Sources/CLI/Packages/cmuxTests/cmuxUITests, ignores vendor/external sources, compares against .github/swift-file-length-budget.tsv, and fails on overages or new tracked files; now includes strict TSV parsing, duplicate entry checks, clear errors (no tracebacks), and resolves relative budget paths inside the repo.
    • Adds the initial budget file.
    • CI runs both the guard test (tests/test_ci_swift_file_length_budget.sh) and a direct enforcement step in .github/workflows/ci.yml.
  • Migration

    • If CI fails: split/shrink the file, or refresh the budget if intentional:
      python3 scripts/swift_file_length_budget.py --budget .github/swift-file-length-budget.tsv --write-budget
    • Reduce budget numbers when files shrink.

Written for commit 9f07bb7. Summary will update on new commits. Review in cubic

Summary by CodeRabbit

  • Chores

    • Enhanced the CI pipeline with Swift file length budget validation, automatically enforcing file size standards to maintain code quality and prevent excessive growth in individual Swift files.
  • Tests

    • Introduced automated tests for Swift file length budget enforcement, covering budget initialization, file tracking, threshold validation, and error scenarios.

@vercel

vercel Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
cmux Ready Ready Preview, Comment Apr 28, 2026 10:49am
cmux-staging Building Building Preview, Comment Apr 28, 2026 10:49am

@coderabbitai

coderabbitai Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

This PR adds Swift file-length budget enforcement to the CI pipeline by introducing a Python script that validates Swift source files against a line-count budget, integrating it into the CI workflow with guard checks, and providing comprehensive shell tests for validation.

Changes

Cohort / File(s) Summary
CI Workflow Integration
.github/workflows/ci.yml
Adds two guard checks to the workflow-guard-tests job: executes a shell script to validate the Swift file-length budget guard and runs a Python script to enforce the budget using TSV configuration.
Swift File-Length Budget Enforcement
scripts/swift_file_length_budget.py
New Python script that scans .swift files, compares line counts against a TSV-based budget, enforces per-file limits, reports budget exceeded/untracked files, and supports --write-budget mode to generate the budget baseline.
Budget Enforcement Testing
tests/test_ci_swift_file_length_budget.sh
New shell test that validates the budget script in an isolated repository, verifying budget creation, baseline passing, untracked file detection, budget overflow handling, and error reporting for malformed budget files.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Suggested labels

codex

Poem

🐰 A rabbit hops through Swift source lines,
With budgets tracked by TSV designs,
Each file now measured, costs constrained,
No bloated code shall go unnamed!
Guard the gates with Python's care,
File lengths bounded, clean and fair! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding a Swift file length budget CI guard, which is clearly reflected in the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed PR description covers summary of changes and testing performed, but omits demo video section and incomplete checklist compliance.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat-swift-file-length-budget

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b5a3bef89

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

Comment thread .github/workflows/ci.yml
@greptile-apps

greptile-apps Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces a Swift file-length budget guard: a Python script (scripts/swift_file_length_budget.py) that compares actual line counts of cmux-owned Swift files against a checked-in TSV budget (.github/swift-file-length-budget.tsv), failing CI when any tracked file grows past its allocation or a new file crosses the 500-line threshold without an explicit budget entry. The guard and its shell test are wired into the workflow-guard-tests CI job. All P1/P0 logic is correct; the three findings below are P2 display and test-coverage improvements.

Confidence Score: 4/5

Safe to merge; CI pass/fail logic is correct and all findings are P2 display/coverage issues.

All three findings are P2: an off-by-one in the delta label for untracked files at exactly the threshold, a misleading actual=0 in reduction hints for below-threshold files, and a missing test case for new untracked files above threshold. None affect correctness of CI enforcement.

scripts/swift_file_length_budget.py (delta display logic) and tests/test_ci_swift_file_length_budget.sh (missing untracked-new-file coverage)

Important Files Changed

Filename Overview
scripts/swift_file_length_budget.py New guard script with two minor issues: off-by-one in delta display for untracked files at exactly the threshold, and the reduction hint can show actual=0 for files that shrank below threshold rather than being deleted.
tests/test_ci_swift_file_length_budget.sh New test covers budget write, pass, growth-violation, and malformed-budget cases; missing coverage for the "new untracked file above threshold" failure path which is the guard's primary use case.
.github/swift-file-length-budget.tsv Initial budget snapshot for 75 cmux-owned Swift files at or above 500 lines; correctly sorted by descending line count.
.github/workflows/ci.yml Minimal addition wiring test_ci_swift_file_length_budget.sh into the workflow-guard-tests job alongside the existing Swift warning budget check.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[CI: workflow-guard-tests] --> B[test_ci_swift_file_length_budget.sh]
    B --> C[swift_file_length_budget.py]
    C --> D[collect_file_lengths\nscans Sources/ CLI/ Packages/\ncmuxTests/ cmuxUITests/]
    D --> E[tracked_file_lengths\nfilter >= threshold 500]
    E --> F{--write-budget?}
    F -- yes --> G[write_budget\n.github/swift-file-length-budget.tsv]
    F -- no --> H[load_budget\n.github/swift-file-length-budget.tsv]
    H --> I[compare_budget\nactual vs allowed]
    I --> J{violations?}
    J -- new file >= threshold\nnot in budget --> K[exit 1\nBudget exceeded]
    J -- tracked file\ngrew past allowed --> K
    J -- none --> L[exit 0\nBudget respected]
    L --> M[print reduction hints\nif budget can shrink]
Loading

Reviews (1): Last reviewed commit: "ci: add Swift file length budget" | Re-trigger Greptile

Comment thread scripts/swift_file_length_budget.py
Comment thread scripts/swift_file_length_budget.py
Comment thread tests/test_ci_swift_file_length_budget.sh

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@scripts/swift_file_length_budget.py`:
- Around line 190-211: The code currently uses args.budget as-is so relative
budget paths are resolved from the current CWD; update the main flow so
args.budget is resolved against repo_root (the Path stored in repo_root) before
any exists/read/write operations: after repo_root = args.repo_root.resolve(...)
compute a resolved_budget Path by joining repo_root with args.budget when
args.budget is relative (or always replace args.budget with repo_root /
args.budget and .resolve(strict=False)), then use that resolved args.budget for
write_budget, exists(), load_budget and the printed messages so all budget
reads/writes are rooted at the selected repo_root.
- Around line 66-79: The parser currently overwrites duplicate relative-path
rows silently; modify the reader so after parsing rel_path but before assigning
budget[rel_path] it checks if rel_path already exists in the budget dict and if
so raises a ValueError like f"{path}:{line_number}: duplicate budget entry for
{rel_path!r}" to fail early; locate the code that uses variables budget, path,
line_number, rel_path, count (the block that currently does budget[rel_path] =
count) and add this duplicate check there.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 75e55190-b7e1-4ff8-b386-a70922cbf713

📥 Commits

Reviewing files that changed from the base of the PR and between 8fba6de and ce0df03.

⛔ Files ignored due to path filters (1)
  • .github/swift-file-length-budget.tsv is excluded by !**/*.tsv
📒 Files selected for processing (3)
  • .github/workflows/ci.yml
  • scripts/swift_file_length_budget.py
  • tests/test_ci_swift_file_length_budget.sh

Comment thread scripts/swift_file_length_budget.py
Comment thread scripts/swift_file_length_budget.py
@lawrencecchen
lawrencecchen merged commit 6c2892d into main Apr 28, 2026
27 checks passed
@lawrencecchen
lawrencecchen deleted the feat-swift-file-length-budget branch April 28, 2026 10:52

This branch was successfully deployed

1 active deployment
Preview – cmux — 9f07bb73 Deployed Apr 28, 2026 by vercel[bot]
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.

1 participant