Skip to content

fix: validate nudge numeric config fields - #100

Open
Beatrice0377 wants to merge 1 commit into
ranxianglei:masterfrom
Beatrice0377:2026-08-21_validate-nudge-config
Open

fix: validate nudge numeric config fields#100
Beatrice0377 wants to merge 1 commit into
ranxianglei:masterfrom
Beatrice0377:2026-08-21_validate-nudge-config

Conversation

@Beatrice0377

Copy link
Copy Markdown

Problem

validateConfig() does not sanity-check several numeric nudge configuration fields added over time.

Invalid overrides such as a negative growth floor, growthCap < growthFloor, or an out-of-range emergency threshold are currently accepted and can make nudge thresholds behave unexpectedly without any validation warning.

Non-finite values such as NaN or Infinity can also bypass ordinary numeric comparisons and propagate into the adaptive-growth calculations.

Root Cause

The newer nudge numeric fields were added without corresponding checks in validateConfig().

The existing validator covers model context limits, tier trigger ordering, truncation thresholds, and some nudge threshold relationships, but not:

  • growthRatio
  • growthFloor
  • growthCap
  • minGrowthFloor
  • minGrowthRatio
  • emergencyThresholdPct

Fix

Add validation for the six fields above:

  • Require all six values to be finite numbers.
  • Require growthRatio, growthFloor, minGrowthFloor, and minGrowthRatio to be non-negative.
  • Require growthCap >= growthFloor.
  • Require emergencyThresholdPct to be in (0, 1].

The validator keeps its existing behavior of returning validation errors; no runtime path is changed to throw.

Scope / Non-goals

Tests

Added regression coverage for:

  • Negative nudge growth values.
  • NaN / Infinity inputs.
  • growthCap < growthFloor.
  • growthCap == growthFloor.
  • Zero-valued non-negative fields.
  • emergencyThresholdPct boundaries.
  • Compatibility for finite growth ratios above 1.

Validation:

  • Targeted config tests: 18/18 passed.
  • Full test suite: 400/400 passed.
  • npm run typecheck: passed.
  • npm run build: passed.
  • git diff --check: passed.

Compatibility

Valid existing configurations continue to pass validation.

The change only adds warnings for invalid configuration values; it does not alter nudge calculation behavior for valid inputs.

Fixes #46

@ranxianglei ranxianglei left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Review: ✅ Approve — 2 independent agent reviews, both approve. Nits below are optional follow-ups, none blocking.

Verified locally on the PR branch (claims all check out):

  • npm run typecheck
  • npm test → 400/400 ✅ (matches PR claim)
  • npm run build
  • CI green (pr-validation, test 22/24); mergeable; single commit; scope confirmed: src/config.ts (+29/−1), tests/sync-config.test.ts (+108)

Correctness

  • Implements all six checks from #46, and adds finiteness (NaN/Infinity) checks beyond the issue's suggestion — good hardening, since NaN silently bypasses </> comparisons.
  • The Number.isFinite(growthFloor) guard at src/config.ts:92 avoids a confusing "growthCap must be >= growthFloor" cascade when the floor itself is non-finite. Probed: growthFloor: Infinity → single clear error; growthFloor: NaN, growthCap: -1 → root-cause error only.
  • growthCap >= 0 isn't checked directly but holds transitively (cap ≥ floor ≥ 0). Negative cap with valid floor is caught via the cap-vs-floor relation.
  • emergencyThresholdPct domain (0, 1] is correct: usage >= emergencyThresholdPct (src/compress.ts:967) means 0 would fire every turn and >1 never fires — exactly the nonsense values #46 wanted flagged.
  • Semantics vs resolveAdaptiveGrowth (src/compress.ts:922-928): with cap < floor, Math.min(cap, Math.max(floor, …)) silently returns cap — the validator now surfaces that misconfiguration instead of masking it.
  • Defaults (growthFloor=50000, growthCap=50000, emergencyThresholdPct=0.95) validate clean; also probed defaultConfig(2_000_000) to rule out limit-dependent breakage.
  • No consumer risk: validateConfig output is only console.warn'd (src/compress.ts:327-329), never thrown; pure additive checks, no behavior change for valid configs.

Nits (optional)

  1. No explicit negative-growthCap test (growthCap: -1 with a valid floor) — tests only cover cap=99/floor=100.
  2. growthCap: NaN untested (only Infinity, same !Number.isFinite branch — cosmetic).
  3. emergencyThresholdPct: -0.5 untested (0 and 1.01 already hit both branches — cosmetic).
  4. Follow-up candidate (explicitly out of scope here): tier2GrowthMultiplier, frequency, iterationThreshold, and min/max pct ranges remain unvalidated.

The keyof Pick<NudgeConfig, …> typing (src/config.ts:75-80) is a nice touch — keeps the field list refactor-safe. Thanks!

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.

validateConfig() does not sanity-check the newer nudge numeric fields

2 participants