Skip to content

Promote develop to main: hold build_dist's lazily imported audit module - #1432

Merged
ptr727 merged 1 commit into
mainfrom
develop
Sep 7, 2026
Merged

ptr727 merged 1 commit into
mainfrom
develop

Conversation

@ptr727

@ptr727 ptr727 commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Promote develop to main. One commit, #1430, which settles #1424.

scripts/build_dist.py's _fence_step reached into spec/audit.py once per line and re-resolved
the module every time, scanning sys.path and re-entering import audit. The accessor is now a
cached zero-argument _audit_module(), the shape scripts/carry.py took for the same defect on
#1423.

Over the 24 authored skills, 4644 lines across the 40 Markdown files they comprise, warm in one
process, best of five: 17.5 ms re-resolved against 0.9 ms held, where calling audit directly
costs 0.7 ms. One --check run makes 18708 of these calls, four times that corpus, and it runs
on every pull request.

Worth reading before merging

The first draft used functools.cache, which is 3.9+. scripts/skills_install.py imports
build_dist at module scope, and three install wrappers commit to Python 3.7+ because that
path runs before the fleet's own toolchain exists to install a newer interpreter. The draft would
have broken install-skills at import on a 3.7 or 3.8 host, and every gate passed it, since ruff
and mypy both target 3.13. Proven on a real CPython 3.8.20 rather than argued from a version table,
and fixed with lru_cache(maxsize=None).

Nothing gates that floor, which is now filed as #1431.

The local review also caught that holding the module made an existing test vacuous: the
sys.path guard could be deleted with the whole suite still green. It now clears the cache between
calls and covers that guard again.

CodeRabbit reviewed this one after being rate limited on #1423 and #1426 all evening, and raised
that the laziness test read the child interpreter's absolute state rather than the import delta,
which a startup hook could break. Fixed, with a skip rather than a vacuous pass where the delta
cannot be read at all.

Closes #1424

🤖 Generated with Claude Code

…or It Per Line (#1430)

Closes #1424.

`scripts/build_dist.py`'s `_fence_step` calls into `spec/audit.py` once
per line, and it scanned
`sys.path` and re-entered `import audit` on every one of them. The
accessor splits out as a cached
zero-argument `_audit_module()`, the shape `scripts/carry.py` took for
the same defect on #1423.

## Measured

Over the 24 authored skills, 4644 lines across the 40 Markdown files
they comprise, warm in one
process, best of five:

| | one pass |
| --- | --- |
| re-resolved per line | 17.5 ms |
| module held | 0.9 ms |
| calling `audit` directly | 0.7 ms |

One `--check` run makes **18708** of these calls, four times that
corpus, because it scans the
authored tree and both generated trees. It runs on every pull request.

## The part that was nearly a regression

The first draft used `functools.cache`. That is 3.9+, and
`scripts/skills_install.py` imports this
module **at module scope**, while `host-setup/linux/install-skills.sh`,
`host-setup/windows/install-skills.ps1` and `scripts/skills_install.sh`
each commit to **Python
3.7+**, because that path runs before the fleet's own toolchain exists
to install a newer
interpreter. `spec/host-tools.json`'s `python3` entry records that floor
and why it sits below the
fleet's own.

So the draft would have broken `install-skills` at import on a 3.7 or
3.8 host, on the one path
whose whole purpose is surviving an old one, and the laziness this
function already had was there
for exactly that reason.

Verified by running it rather than by reading a version table, on a real
CPython 3.8.20:

```
this branch      import build_dist: OK    import skills_install: OK    audit imported? False
functools.cache  import build_dist: BROKE -> module 'functools' has no attribute 'cache'
```

`functools.lru_cache(maxsize=None)` is 3.2+ and identical for a
zero-argument function. Ruff's own
`py313` target wants to rewrite it back to `functools.cache`, so the
`noqa: UP033` carries that
reason inline.

## Tests

Three, each proven by removing what it guards:

- The module is resolved **once** however many lines are read.
- Importing this module still does **not** import `audit`, run in a
fresh interpreter. This is the
laziness the install path depends on, and it was load-bearing and
untested before now.
- The existing `sys.path` case. Holding the module made it **vacuous**:
the accessor's body then
runs once per process, so the `not in sys.path` guard could be deleted
with the whole suite still
green. It now clears the cache between calls and covers that guard
again. Caught by the local
  review pass, which mutation-proved it on both sides of the merge base.

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Bug Fixes**
* Improved build checks by reusing audit functionality efficiently
across repeated validation runs.
* Preserved compatibility with supported Python versions by loading
audit functionality only when needed.
* **Tests**
* Added coverage for repeated validation calls and fresh-interpreter
import behavior.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 7, 2026 23:06
@qodo-code-review

Copy link
Copy Markdown

ⓘ Qodo reviews are paused because your trial has ended. Ask your workspace admin to add credits to resume reviews. Manage billing

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 49 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 93d449f2-1088-4aae-bc64-ffbbdbe77aba

📥 Commits

Reviewing files that changed from the base of the PR and between 1d52152 and 5ae7cf8.

📒 Files selected for processing (2)
  • scripts/build_dist.py
  • scripts/tests/test_build_dist.py

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.

@ptr727

ptr727 commented Sep 7, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
⚠️ Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copilot AI left a comment

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.

🟢 Approval recommended

The change preserves the required lazy-import behavior for older-Python bootstrap paths, improves performance as intended, and adds targeted test coverage for the new caching semantics.

Pull request overview

This PR promotes develop to main by landing the change that avoids repeatedly re-resolving spec/audit.py during scripts/build_dist.py runs, while preserving the intentional lazy import needed by the skills installer path.

Changes:

  • Add a cached _audit_module() accessor (via functools.lru_cache(maxsize=None)) and route _fence_step() through it to avoid per-line sys.path scanning and repeated import resolution.
  • Strengthen tests to (a) ensure the audit module is resolved once across many _fence_step calls, (b) keep coverage for the sys.path guard by clearing the cache between calls, and (c) assert that importing build_dist does not import audit in a fresh interpreter.
File summaries
File Description
scripts/build_dist.py Introduces a cached lazy accessor for audit and updates _fence_step to use it, reducing repeated import resolution overhead.
scripts/tests/test_build_dist.py Adds regression tests for the caching behavior and verifies build_dist remains lazy with respect to importing audit.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@ptr727
ptr727 merged commit bb352c6 into main Sep 7, 2026
9 checks passed
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.

build_dist.py Re-Resolves the Lazily Imported Audit Module on Every Line It Reads

2 participants