Skip to content

fix(tier2): enforce chunk budgets during shell expansion - #913

Open
Afloat16 wants to merge 2 commits into
NVIDIA:mainfrom
Afloat16:fix/bound-shell-chunk-expansion-20261126
Open

Afloat16 wants to merge 2 commits into
NVIDIA:mainfrom
Afloat16:fix/bound-shell-chunk-expansion-20261126

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Nested shell functions produce overlapping raw bodies. Skill and bundle context deduplication currently materialize every body and every bounded piece before checking their chunk ceilings. Small nested scripts can therefore allocate many times their source size even though the scan ultimately stops as incomplete.

Pass each file the remaining skill or aggregate bundle budget and produce shell chunks on demand. Stop at the first excess bounded piece, preserve each caller's existing incomplete result and error, and never create embeddings for the oversized scan. Files within the budget retain the same complete chunk text, ordering, and source ranges. The public shell chunker retains its list return type.

Verification

  • I am familiar with the Contributing Guidelines.
  • Added scale regressions with 1,000 and 2,000 nested shell functions for both the individual skill and plugin preflight paths. All four regressions fail before their respective fixes because every overlapping body is copied before the ceiling is checked.
  • Added exact-budget output coverage for Markdown, MDC, Python, and shell, a zero-budget file with no chunks, and shared budgets across files and bundle members. The assertions require incomplete status, exact first-excess metadata, and no embedding request when the ceiling is exceeded.
  • All 13 new tests passed within an 85-test selection covering the existing plugin context budget and private deduplication behavior. The chunking utilities and intra-skill validator selection also passed 311 tests with one existing skip before the plugin entrypoint extension.
  • Measured a real 22,922-byte shell file with 2,000 nested functions using Python tracemalloc. Peak tracked allocation around the individual validator fell from 50,252,965 to 3,933,716 bytes; the real bundle preflight fell from 50,264,921 to 2,380,392 bytes. Both paths retained incomplete status and made no embedding request. Fixture creation is excluded; these are allocation measurements, not end-to-end throughput claims.
  • Full-repository make lint, changed-file formatting, and git diff --check passed.
  • Ran make test on exact published head f9f39d1f4ab012de304b2f35339d1dd8750b2376: 45,554 passed, 338 skipped, 19 deselected under the project's existing defaults. No new skips or test exclusions were added.
  • Ran make build; wheel and source distribution built successfully. All three gates ran on a fresh Ubuntu 24.04 runner with Python 3.12.16 and locked all-extra dependencies: validation job.
  • Review follow-up documents first-excess reporting and covers both boolean and negative chunk-budget rejection.
  • No credentials, private datasets, or proprietary benchmark content were added.

Release Impact

  • Updated CHANGELOG.md.

Signed-off-by: Afloat <ShuffleMonkey@163.com>

@chrisknvidia chrisknvidia left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I reproduced the allocation blow-up on main with the 22,922-byte, 2,000-function nested shell fixture: tracemalloc peak was 50.2 MB for the individual validator and 50.1 MB for the bundle preflight (about 11 s each), dropping to 3.9 MB and 2.3 MB on this branch, with both paths still ending incomplete and no embedding client constructed. That matches the numbers in the description.

Chunk output is unchanged for files within the budget. I chunked 649 files (every .md, .mdc, .py and .sh under tests/ plus a generated set of nested, CRLF, long-line, comment-only, unmatched-brace, unicode, syntax-error and fenced-markdown cases, with and without a line offset) on main and on this branch and the chunk lists are identical, text, ordering, line ranges and split groups included. For every file, max_chunks=N returns the same list, N-1 and 0 raise with the expected first-excess count, bool, negative, float and string budgets are rejected, chunk_shell still returns a list, and no shell generator is left alive after the early exit.

The incomplete-result contract holds too: across 20 validator and preflight scenarios (at budget, one over, far over, budget exactly consumed and then an empty or non-empty file, zero ceiling, reversed file order, shared member budgets at 3 and 4) status, error text, file_path and the zero embedding calls match main. The one visible difference is the reported actual / actual_chunks value, which is now limit + 1 rather than the eager total; see the inline note. Of the 13 new tests, the four scale regressions, the four exact-output tests and the zero-budget test fail on main's source and pass here; the two shared-budget pairs pass on both trees, which is fine as guards. tests/deduplication, tests/tier2 and the Tier 2 command tests pass on both trees with no new failures, and lint, format and git diff --check are clean.

A second note inline on the untested max_chunks type guard, also not blocking. I also did a separate differential run over about 1,100 generated files (shell with nested functions, heredocs, quoted braces, CRLF and unterminated functions, plus Markdown, MDC and Python) at two min_chars settings: every chunk field from chunk_file and chunk_shell is byte-identical between main and this branch. Open PR #394 rewrites the same chunk-count lines in plugin/commands.py, so whichever lands second needs a small rebase.

Comment thread CHANGELOG.md Resolved
Signed-off-by: Afloat <ShuffleMonkey@163.com>

This branch has not been deployed

No deployments
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.

2 participants