Skip to content

Superseded By #357 — Do Not Merge - #356

Closed
jaywedgeworth22 wants to merge 4 commits into
mainfrom
codex/cc-205-reset-hotfix
Closed

jaywedgeworth22 wants to merge 4 commits into
mainfrom
codex/cc-205-reset-hotfix

Conversation

@jaywedgeworth22

@jaywedgeworth22 jaywedgeworth22 commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Refs CC-205

This PR stops false weekly reset alerts when a provider corrects a deadline forward without restoring quota. In Sources/QuotaCore/ResetAlarmTracker.swift, a reused window ID whose cadence (periodSeconds) changes beyond resetDriftTolerance now starts a new baseline instead of being compared across periods. A large forward jump in resetAt now also requires quota evidence — a rise above riseEpsilon or a passed end with no readable percentage — to count as a reset, while a smaller jump with a climb above earlyResetRiseMargin still counts; corrections with flat or falling quota now adopt the corrected end without emitting an alarm, but the later true scheduled rollover is still reported as .newPeriod.

Tests added in this change, not reported as run: updated testAMidCycleEventThatFellIsNeitherARefillNorAPlanChange to expect no event for a future-end correction with falling quota and a later valid rollover, plus new testPassedMiniMaxDeadlineCorrectionWithUnchangedQuotaDoesNotAlertAndValidRolloverDoes, testReusedWindowIdWithChangedCadenceStartsANewBaseline, and testPreEndResetTimeCorrectionWithoutQuotaRiseIsNotAnAlarm. Runtime and deployment verification is unverified from the provided changes.

Open automated review findings, described as reported and not resolved: medium issue at ResetAlarmTracker.swift:504 for a scheduled rollover missed with first measured reading after nil history, and medium issue at ResetAlarmTracker.swift:472 for cadence rebaseline dropping known window identity on unstamped reading.

@linear-code

linear-code Bot commented Nov 3, 2026

Copy link
Copy Markdown

CC-205

@kody-ai

kody-ai Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Code Review Completed! 🔥

The code review was successfully completed based on your current configurations.

Kody Guide: Usage and Configuration
Interacting with Kody
  • Request a Review: Ask Kody to review your PR manually by adding a comment with the @kody start-review command at the root of your PR.

  • Validate Business Logic: Ask Kody to validate your code against business rules by adding a comment with the @kody -v business-logic command.

  • Provide Feedback: Help Kody learn and improve by reacting to its comments with a 👍 for helpful suggestions or a 👎 if improvements are needed.

Current Kody Configuration
Review Options

The following review options are enabled or disabled:

Options Enabled
Bug ✅
Performance ✅
Security ✅
Business Logic ✅

Access your configuration settings here.

​

Comment on lines +474 to 484
let previousPeriod = window.periodSeconds,
abs(observedPeriod - previousPeriod) > ResetAlarmPolicy.resetDriftTolerance {
state.windows[key] = firstReading()
return nil
}

if let period = observation.periodSeconds { window.periodSeconds = period }

let readAt = observedAt ?? now
var isReset = false
var isMidWindow = 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.

Bug: Timestamp-derived cadence changes across varying-length periods (such as 30 vs 31-day months) trigger window rebaselining, dropping genuine rollover alerts.
Severity: MEDIUM

Suggested Fix

Allow tolerance for expected calendar variations or normalize timestamp-derived periods into standard calendar-month intervals before comparing cadence changes against resetDriftTolerance.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: Sources/QuotaCore/ResetAlarmTracker.swift#L462-L484

Potential issue: The cadence-change guard rebaselines the window and returns nil
whenever `observation.periodSeconds` differs from stored `periodSeconds` by more than 15
minutes. Some quota readers, such as Grok subscriptions, dynamically calculate period
tokens and durations from start and end timestamps (e.g. 30 days vs 31 days). When month
lengths change across consecutive periods, the difference exceeds `resetDriftTolerance`.
This causes `advance()` to reset the baseline via `firstReading()` instead of processing
a rollover transition, permanently dropping genuine `.newPeriod` rollover alerts and
resetting `minimumRemaining`.

Did we get this right? 👍 / 👎 to inform future reviews.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor
Autofix run https://github.com/Simple-With-Us/CodeCaps/actions/runs/41678489457 started for `sentry[bot]`'s tagged request. Owner/sentry request: exempt from the per-bot cap (owner rule 2026-10-20).

Comment on lines +504 to +509
let rose = reading.flatMap { current in
window.lastRemaining.map { current > $0 + ResetAlarmPolicy.riseEpsilon }
} ?? false
let resetHasQuotaEvidence = rose || (hasPassed && reading == nil)
if (jump > required && resetHasQuotaEvidence)
|| (jump > ResetAlarmPolicy.resetDriftTolerance && climbed) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

kody code-review Bug medium

Scheduled rollover missed with first measured reading after nil history

The check suppresses genuine resets because rose stays false when window.lastRemaining is nil, so resetHasQuotaEvidence blocks the hasPassed path. Treat missing window.lastRemaining as quota evidence when reading is present.

Prompt for LLM
File Sources/QuotaCore/ResetAlarmTracker.swift, lines 504-509:

Scheduled rollover missed with first measured reading after nil history

Scheduled rollover with first measured percentage after nil history never fires because rose is false when lastRemaining is nil. Genuine hasPassed jump with reading set but no prior baseline is suppressed, missing the reset the old jump>required caught. Treat missing lastRemaining as quota evidence for the hasPassed jump path, e.g. rose when window.lastRemaining is nil and reading is non-nil.

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

Comment on lines +472 to +478
// A producer may reuse a window ID while switching its cadence.  Its reset time and remaining percentage no longer share the old baseline.  Keep the new reading as a baseline instead of comparing across periods.
if let observedPeriod = observation.periodSeconds,
let previousPeriod = window.periodSeconds,
abs(observedPeriod - previousPeriod) > ResetAlarmPolicy.resetDriftTolerance {
state.windows[key] = firstReading()
return nil
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

kody code-review Bug medium

Cadence rebaseline drops known window identity on unstamped reading

The rebaseline discards the stored account binding because firstReading() carries nil for observation.identity, letting a later different account merge into the same window. Preserve window.identity when observation.identity is nil.

Prompt for LLM
File Sources/QuotaCore/ResetAlarmTracker.swift, lines 472-478:

Cadence rebaseline drops known window identity on unstamped reading

Cadence rebaseline replaces the window with firstReading() carrying observation.identity, wiping a known window.identity when the new reading is unstamped. Loses the account binding the identity branch preserves and lets a later different account be folded into the same window. Preserve window.identity when observation.identity is nil during the rebaseline.

Suggested code:

// A producer may reuse a window ID while switching its cadence. Its reset time and remaining percentage no longer share the old baseline. Keep the new reading as a baseline instead of comparing across periods.
if let observedPeriod = observation.periodSeconds,
   let previousPeriod = window.periodSeconds,
   abs(observedPeriod - previousPeriod) > ResetAlarmPolicy.resetDriftTolerance {
    var baseline = firstReading()
    if baseline.identity == nil { baseline.identity = window.identity }
    state.windows[key] = baseline
    return nil
}

Talk to Kody by mentioning @kody

Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.

​

​

@swu-codex

swu-codex Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

The 73-test reset fix is incorporated in #357 on current main, with broader MiniMax correction and idle rollover regressions under review.  This hotfix branch would trigger the old shared-machine-identity release workflow on merge, so it must not merge.  Please follow #357 for the final corrected implementation.

@swu-codex swu-codex Bot changed the title Stop False Weekly Reset Alerts From Corrected Deadlines Superseded By #357 — Do Not Merge Nov 3, 2026
@jaywedgeworth22
jaywedgeworth22 marked this pull request as draft November 4, 2026 04:58
@jaywedgeworth22
jaywedgeworth22 marked this pull request as ready for review November 4, 2026 07:22
@jaywedgeworth22
jaywedgeworth22 enabled auto-merge (squash) November 4, 2026 07:22
@swu-echo
swu-echo Bot marked this pull request as draft November 4, 2026 18:37
@swu-wise
swu-wise Bot marked this pull request as ready for review November 6, 2026 03:43
Comment on lines +504 to 510
let rose = reading.flatMap { current in
window.lastRemaining.map { current > $0 + ResetAlarmPolicy.riseEpsilon }
} ?? false
let resetHasQuotaEvidence = rose || (hasPassed && reading == nil)
if (jump > required && resetHasQuotaEvidence)
|| (jump > ResetAlarmPolicy.resetDriftTolerance && climbed) {
isReset = true

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.

Bug: Genuine period rollovers without quota increases or with nil lastRemaining fail resetHasQuotaEvidence, silently shifting the window without triggering .newPeriod.
Severity: MEDIUM

Suggested Fix

Refine resetHasQuotaEvidence so that a passed window deadline (hasPassed) with a forward jump can still qualify as a reset when lastRemaining is nil or when usage is already at maximum (e.g., reading == 100), or separate stale deadline corrections from period rollover detection.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: Sources/QuotaCore/ResetAlarmTracker.swift#L504-L510

Potential issue: In `ResetAlarmTracker.swift`, `resetHasQuotaEvidence` is set to `rose
|| (hasPassed && reading == nil)`, where `rose` requires `reading > lastRemaining +
riseEpsilon` (with `riseEpsilon = 1`) and is false when `lastRemaining` is `nil`. For a
window whose period has ended (`hasPassed`), if the remaining quota remained unchanged
(e.g., 100% to 100%), increased by <= 1 point (due to rounding), or if `lastRemaining`
was `nil`, the rollover is not detected as a reset. It silently updates the window end
date without firing `.newPeriod` or re-baselining `minimumRemaining`.

@jaywedgeworth22
jaywedgeworth22 marked this pull request as draft November 7, 2026 20:16
@jaywedgeworth22

Copy link
Copy Markdown
Member Author

Closed as fully superseded by #357 (the titles said so), per the owner's standing superseded-PR rule and converted-to-draft earlier today.  Reopen if #357 drops this scope.

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