Skip to content

Fix: regional trigger threshold proximity for lt/lte operators#3656

Open
FayezBast wants to merge 1 commit into
mainfrom
fix/bugs-fixing
Open

Fix: regional trigger threshold proximity for lt/lte operators#3656
FayezBast wants to merge 1 commit into
mainfrom
fix/bugs-fixing

Conversation

@FayezBast
Copy link
Copy Markdown
Collaborator

Summary

Fixes regional trigger threshold proximity detection for lt/lte operators. The previous helper only worked correctly for upward thresholds (gt/gte), which could hide near-breach regional intelligence triggers that move downward toward a threshold.

Adds focused regression coverage for lt, lte, gte, negative thresholds, and dormant delta_* operators.

Type of change

  • Bug fix

Affected areas

  • Other: Regional snapshot trigger evaluation

Checklist

  • No API keys or secrets committed
  • TypeScript compiles without errors (npm run typecheck)

@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
worldmonitor Ready Ready Preview, Comment May 10, 2026 10:06pm

Request Review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 10, 2026

Greptile Summary

This PR fixes isCloseToThreshold to be operator-aware so that lt/lte triggers correctly enter "watching" state when a value is approaching the threshold from above. The old ratio-based logic only worked for upward (gt/gte) thresholds; downward thresholds always fell through to dormant.

  • trigger-evaluator.mjs: replaces the ratio heuristic with an absolute band (Math.abs(target) * 0.2) and dispatches on the operator, returning the correct pre-breach window for both upward and downward thresholds. delta_* operators remain dormant. The function is now exported so it can be tested directly.
  • regional-snapshot.test.mjs: adds a focused isCloseToThreshold describe block covering lt, lte, gte, negative thresholds, and delta_* operators. The implementation and boundary conditions are correct across all cases reviewed.

Confidence Score: 4/5

Safe to merge — the bug fix is logically correct, watching windows are mutually exclusive from the active check, and the new tests cover the previously broken paths.

The operator-aware band logic is correct for all four comparison operators and for negative thresholds. The only gap is that gt with a positive threshold has no direct assertion — it is only implicitly covered by the negative-threshold test and by sharing a switch branch with gte.

tests/regional-snapshot.test.mjs — the gt positive-threshold case is not directly asserted; worth adding a line to the existing test for completeness.

Important Files Changed

Filename Overview
scripts/regional-snapshot/trigger-evaluator.mjs Rewrites isCloseToThreshold to be operator-aware, correctly producing a pre-breach watching window above the target for lt/lte. Band is now calculated as Math.abs(target)*0.2. Function is exported for direct unit testing.
tests/regional-snapshot.test.mjs Adds a new isCloseToThreshold describe block covering lt, lte, gte, negative thresholds, and delta_* operators. gt positive-threshold case is covered only via the negative-threshold test; a direct positive gt assertion would round out coverage.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[evaluateTriggers] --> B{resolveMetric}
    B -- null --> C[dormant]
    B -- value --> D{evaluateThreshold}
    D -- true --> E[active]
    D -- false --> F{isCloseToThreshold}
    F --> G{operator?}
    G -- gt/gte --> H["value < target AND value >= target - band"]
    G -- lt/lte --> I["value > target AND value <= target + band"]
    G -- delta_gt/delta_lt --> J[false]
    G -- default --> J
    H -- true --> K[watching]
    I -- true --> K
    H -- false --> C
    I -- false --> C
    J --> C

    style E fill:#f44,color:#fff
    style K fill:#fa0,color:#000
    style C fill:#888,color:#fff
Loading

Reviews (1): Last reviewed commit: "Fix: regional trigger threshold proximit..." | Re-trigger Greptile

Comment on lines +425 to +428
it('keeps gt/gte watching semantics for positive thresholds', () => {
assert.equal(isCloseToThreshold(0.85, { operator: 'gte', value: 1.0 }), true);
assert.equal(isCloseToThreshold(1.0, { operator: 'gte', value: 1.0 }), false);
});
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.

P2 The gt operator is never tested against a positive threshold in isolation — only gte is used in the "keeps gt/gte watching semantics" test, and gt with a negative target is exercised in the negative-threshold test. Since gt and gte share the same switch branch this is low risk, but a single positive-gt assertion would close the gap and guard against any future divergence between the two cases.

Suggested change
it('keeps gt/gte watching semantics for positive thresholds', () => {
assert.equal(isCloseToThreshold(0.85, { operator: 'gte', value: 1.0 }), true);
assert.equal(isCloseToThreshold(1.0, { operator: 'gte', value: 1.0 }), false);
});
it('keeps gt/gte watching semantics for positive thresholds', () => {
assert.equal(isCloseToThreshold(0.85, { operator: 'gte', value: 1.0 }), true);
assert.equal(isCloseToThreshold(1.0, { operator: 'gte', value: 1.0 }), false);
assert.equal(isCloseToThreshold(0.85, { operator: 'gt', value: 1.0 }), true);
assert.equal(isCloseToThreshold(1.0, { operator: 'gt', value: 1.0 }), false);
});

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