Skip to content

fix: Optimal P increase always names a higher P - #247

Draft
nerdCopter wants to merge 2 commits into
masterfrom
fix/small-p-gain-step
Draft

nerdCopter wants to merge 2 commits into
masterfrom
fix/small-p-gain-step

Conversation

@nerdCopter

@nerdCopter nerdCopter commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

AI Generated pull-request

Summary

safe_scaled_p truncates, so a 5% step on P below 20 (or a 10% step on P below 10) scaled to the same integer and the recommendation read "Increase P to ". New safe_raised_p returns the scaled value but at least P+1. It serves the three Increase paths without a changed-value guard: Td significantly slower with low noise, Td moderately slower with low or moderate noise, and unknown noise with Td slower. The result names a higher P for every P below u32::MAX (at u32::MAX no higher value exists; real P gains are far below that). Results for P of 20 and above are unchanged. The on-target low-noise path keeps plain scaling, because its own conservative > current_p check must still be able to reject a step with no headroom.

Console, report.md and the PNG legend share one recommendation, so all three are covered.

Refs #242

Gaps left

  • The console delta uses i32 (optimal_p_estimation.rs ~:655) and could overflow near u32::MAX; not reachable with real gains, left unchanged.
  • IT 242 line "Peaks read about 0.035 above PIDtoolbox": no change in this PR.

Test plan

  • cargo clippy --all-targets --all-features -- -D warnings, cargo fmt --all, cargo test, cargo build --release pass.
  • New tests from the definition: P 0, 1, 9, 10, 19, 20, 21, 60 and u32::MAX; on-target P 10 and 19 stay without an increase.
  • One real log (P of 20 or above): Step Response PNG md5, report.md and console identical to master.
  • CodeRabbit local: 0 findings on the first head. Remote Ollama raised one point on the on-target path; it was valid and is fixed in the second commit.
  • Not verified: a real log with P below 20 rendered end to end. Whether very small P passes the Td-target step is unchecked.

nerdCopter and others added 2 commits November 23, 2026 09:03
safe_scaled_p truncates, so P below 20 with a 5% step (below 10 with
10%) scaled to the same integer and the Increase recommendation named
the current P. Add safe_raised_p, which returns at least P+1 for
increases, and use it in the four Increase paths. Decrease path and
P >= 20 results are unchanged. Add boundary tests at P 0, 1, 9, 10, 19,
20, 21 and u32::MAX saturation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KoqKWwCzFDgbyFBKx3fnAe
The on-target, low-noise case checks conservative > current_p to decide
whether a whole-integer step exists. Raising the step to P+1 there made the
check always true and added increases for small P that were not recommended
before. It uses safe_scaled_p again. The Td-slower and unknown-noise paths
keep safe_raised_p. A test covers P 10 and P 19 on target.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KoqKWwCzFDgbyFBKx3fnAe
@nerdCopter

Copy link
Copy Markdown
Owner Author

@coderabbitai — AI generated comment

Analysis requested on PR 247 (src/data_analysis/optimal_p_estimation.rs):

  1. Does safe_raised_p (max(scaled, base + 1), saturating) give the right result for every base from 0 to u32::MAX, and for both multipliers (5% and 10%)?
  2. The on-target, low-noise path keeps safe_scaled_p so its conservative > current_p check still rejects a step that truncates to the same P. Is any other caller of safe_scaled_p for an Increase left without a changed-value guard?
  3. Does the PR body match the code: three paths use safe_raised_p, results for P of 20 and above are unchanged?

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

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

Next included review available in 28 minutes.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 680d57bf-575c-4836-bcdb-2228b5b0cf2a
Commits

Reviewing files that changed from the base of the PR and between 24e0770 and 4d52a8a.

Files selected for processing: 1

  • src/data_analysis/optimal_p_estimation.rs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tip

For best results, initiate chat on the files or code changes.

Analysis chain

Script executed:

#!/bin/bash
set -uo pipefail
printf '%s\n' '=== Locate requested file ==='
fd -t f -g 'optimal_p_estimation.rs'
printf '%s\n' '=== File outline ==='
ast-grep outline src/data_analysis/optimal_p_estimation.rs --items all
printf '%s\n' '=== Requested file contents ==='
cat -n src/data_analysis/optimal_p_estimation.rs
printf '%s\n' '=== Helper references across repository ==='
rg -n -C 3 '\b(safe_scaled_p|safe_raised_p)\b' .
printf '%s\n' '=== PR diff ==='
gh pr diff 247 --repo nerdCopter/blackbox_analyzer

Length of output: 65685


Script executed:

#!/bin/bash
set -uo pipefail
printf '%s\n' '=== Multiplier definitions and other uses ==='
rg -n -C 3 '\bP_HEADROOM_(MODERATE|CONSERVATIVE)_MULTIPLIER\b' .
printf '%s\n' '=== Increase formatting and delta calculation ==='
rg -n -C 4 'PRecommendation::Increase|conservative_p as i32|current_p as i32' src
printf '%s\n' '=== Independent arithmetic boundary checks ==='
python3 - <<'PY'
M = 2**32 - 1
for mult in (1.05, 1.10):
    print(f'multiplier={mult}')
    for base in (0, 1, 9, 10, 19, 20, 21, 60, 2**31 - 1, M - 1, M):
        scaled = min(M, int(base * mult))
        raised = max(scaled, min(M, base + 1))
        print(f'  base={base}, scaled={scaled}, raised={raised}, greater={raised > base}')
PY

Length of output: 16268


Script executed:

#!/bin/bash
set -uo pipefail
printf '%s\n' '=== Report structure and recommendation formatting ==='
ast-grep outline src/report.rs --items all --match 'report|Report'
cat -n src/report.rs | sed -n '410,455p'
printf '%s\n' '=== Console and plot delta calculations ==='
cat -n src/data_analysis/optimal_p_estimation.rs | sed -n '640,670p'
cat -n src/plot_functions/plot_step_response.rs | sed -n '488,515p'
printf '%s\n' '=== P input definitions ==='
rg -n -C 3 'struct AxisPid|\bcurrent_p\b|pub p:\s*u32' src/data_input/pid_metadata.rs | head -n 110

Length of output: 7771


@nerdCopter

  1. The helper meets the saturating formula, with one limit. For both 1.05 and 1.10, safe_raised_p returns a value greater than base for every base < u32::MAX. At u32::MAX, it returns the same value because no larger u32 exists. The three unguarded paths still emit Increase at that limit, so “always names a higher P” is not true for the full u32 range. See src/data_analysis/optimal_p_estimation.rs, Line 71.

  2. No other direct safe_scaled_p increase path lacks a guard. The on-target, low-noise path checks conservative > current_p at Line 458. The only other direct call produces a Decrease.

  3. The PR body matches the code, with the limit above. Three call sites use safe_raised_p. For P values of 20 or above, the result matches the previous scaling result, including at saturation. For P below 20, the 5% path raises a truncated same-value result to P+1. The 10% path does so below P 10.

The existing maximum-value test checks the helper with 1.05, but it does not check the recommendation variant or 1.10. If the higher-P guarantee must include every u32 input, the increase paths need an explicit u32::MAX fallback.

There is also a pre-existing full-range output limitation: the console subtracts P values as i32 at Line 655. Values that cross the i32 boundary can overflow when overflow checks are enabled. The report and PNG legend use i64 for this delta.

I checked representative boundaries and reviewed the arithmetic; I did not run the repository tests or verify a low-P real log end to end.

You are interacting with an AI system.

@nerdCopter

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor
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.

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