Skip to content

Refuse invalid count in JSON copy instead of clamping it - #2294

Merged
echelon merged 1 commit into
storytold:mainfrom
Silvertree2010:fix/h3026-json-copy-silently-clamps-an-inv
Oct 30, 2026
Merged

echelon merged 1 commit into
storytold:mainfrom
Silvertree2010:fix/h3026-json-copy-silently-clamps-an-inv

Conversation

@Silvertree2010

@Silvertree2010 Silvertree2010 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Found while testing crates/engine/src/cmd/modify/transform.rs:69 (run_copy) (no existing issue): JSON copy silently clamps an invalid count (0, negative, non-integer or over 10000) instead of refusing it.

Cause

run_copy read count with as_u64().unwrap_or(1).clamp(1, 10_000). Zero, negative, non-integer and non-numeric counts silently became 1, and counts over 10000 silently became 10000. The command reported success with a drawing different from the one requested.

Fix

run_copy now returns an invalid-parameters error (count must be an integer from 1 to 10000) when count is present but not an integer in that range. A missing count still defaults to 1. A regression test covers 0, -3, 2.5, "abc", null, 10001 and 50000, checks the drawing is unchanged after each refusal, and checks that a valid count still copies.

Verified

  • Regression test copy_count::invalid_copy_count_is_refused: fails on main without the fix and passes with it (cargo test -p cadcraft-engine --test it copy_count).
  • cargo xtask ci passes.

Not changed

The interactive copy prompts and the other commands' parameter handling are unchanged.

🤖 Generated with AI by ApolloOffice, see the Co-Authored-By trailers for the models

Summary by CodeRabbit

  • Bug Fixes
    • Copy operations now default to one copy only when no count is provided. Supplied counts must be whole numbers from 1 to 10,000; invalid values return an error instead of being adjusted.

Found while testing crates/engine/src/cmd/modify/transform.rs:69 (run_copy)

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between d864a08 and b7a5b0d.

Files selected for processing: 3
  • crates/engine/src/cmd/modify/transform.rs
  • crates/engine/tests/it/copy_count.rs
  • crates/engine/tests/it/main.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

Walkthrough

run_copy now rejects supplied counts that are not unsigned integers from 1 through 10,000. Integration tests cover invalid counts and a valid count of three.

Changes: Copy Count Validation

Layer / File(s) Summary
Validate and test copy counts
crates/engine/src/cmd/modify/transform.rs, crates/engine/tests/it/copy_count.rs, crates/engine/tests/it/main.rs
run_copy rejects invalid supplied counts instead of defaulting or clamping them. The integration test checks that invalid counts do not change the entity count and that a count of three adds three entities.

Priority: Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • storytold/cadcraft#2220: Moves run_copy and retains the 1–10,000 count handling that this change updates.
  • storytold/cadcraft#2239: Includes the clamping behavior that this change replaces with rejection of invalid supplied counts.

Suggested reviewers: aiongg

Merge Risk: Minimal · up to b7a5b

The copy-count change is ready to merge after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed 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%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within survi… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Refuse invalid count in JSON copy instead of clamping it" matches the main change. The raw summary says run_copy now rejects invalid count values with an error instead of defaulting or clam…
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.
Full details: Title check

Explanation

The title "Refuse invalid count in JSON copy instead of clamping it" matches the main change. The raw summary says run_copy now rejects invalid count values with an error instead of defaulting or clamping them. The objectives confirm the same intent, and the summary lists the added integration test for copy count handling. The title is specific and concise, and it names the behavior change and the affected command.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 3 functions across 3 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@STE1200

STE1200 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

@Silvertree2010, thanks for this series (#2294, #2286, #2269, #2190, and #1252 for unknown keys). Refusing instead of clamping is exactly what agents need; see the error part of #1039.

One thing these PRs can't do yet: tell a caller which parameter was wrong without parsing the message. EngineError::BadParams only carries {cmd, msg}, and bad(cmd, msg) is called about 1,000 times in crates/engine/src.

I'd like to propose a small core, separate from your PRs. BadParams gets an optional param and a helper bad_param(cmd, param, msg). MCP and the control channel return param with the error. Existing bad(...) calls stay as they are, so nothing conflicts. Your refusals could then name the key, e.g. bad_param("copy", "count", "must be an integer from 1 to 10000").

Would that fit how you're working through these? If yes, I'll open it as a small PR and leave the call sites to whoever touches them.

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

Tested on Windows 10 Pro x64 (Rust 1.99.1, cargo-nextest 0.9.150), with this PR merged onto current main (336243e). It merges cleanly, and it works.

  • invalid_copy_count_is_refused fails on main and passes with the fix.
  • cargo xtask ci passes all 7 steps (2621 tests) with this PR merged onto main together with #2287, #2301, #2314 and #2335, which I tested at the same time, and #2198 so the gate runs on Windows.

One inconsistency to decide on: this refuses "count": null, and the test requires that. The array commands treat null as "use the default": ARRAYEDIT's count_param does, and so does #2399's count_or for ARRAYRECT and ARRAYPOLAR (None | Some(Value::Null) => Ok(default)). A client that sends null for "not given" would get 1 copy from COPY's default today and an error with this PR, while the array commands accept it. Matching them (None | Some(Value::Null) => 1) would make the JSON commands agree. Either way the error message is clear.

@echelon

echelon commented Oct 30, 2026

Copy link
Copy Markdown
Member

Landed — thank you @Silvertree2010 for this contribution to CADCraft! If you haven't already, come join us on Discord: https://discord.gg/artcraft

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.

4 participants