Skip to content

fix: refetch an aws-control bill fetched in an earlier UTC month - #47905

Open
winnythedev wants to merge 1 commit into
mainfrom
fix/aws-control-costs-month-rollover
Open

winnythedev wants to merge 1 commit into
mainfrom
fix/aws-control-costs-month-rollover

Conversation

@winnythedev

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: On the first day of a month, AWS Control's Usage & costs shows this month's spend instead of last month's whole bill.

The AWS Control bill is cached for 24 hours. A copy fetched late on the last day of a month was still served on the 1st with fresh: true, so "This month" and the Overview's "Month to date" tile showed the finished month's total, with no "Cached figures" caption, for up to a day. The page never asks for a refresh, so the person could not get the right figure.

Why it matters

Anyone who opens Usage & costs on the 1st of a month (UTC) after a late fetch the evening before sees a figure that is wrong by a whole month's spend and is labelled as current. This is the wheel copy's half of the fix: the app repository takes the same change first.

Not a goal

  • Changing the consent checks, profiles, accounts, the ?refresh=1 route or the page.
  • Dropping or shortening the daily cache.
  • Changing the adoption pin for aws-control.

What changed (motivation → approach → change)

  • Symptom: on the 1st, "This month" shows last month's whole bill.
  • Root cause: the bill is month-to-date (fetch_month_costs asks Cost Explorer for the spend since the first of the current UTC month), but costs.is_fresh judged a cached copy only by its age.
  • Change, in src/kiro_crew/apps/builtins/aws_control/backend/costs.py:
    • is_fresh: a cached bill fetched in another UTC month than now is not fresh, whatever its age. Otherwise the 24-hour rule stands. The month is read from the stamp converted to UTC, and a timezone-less stamp is still read as UTC.
    • fetch_month_costs reads the clock once, for both the query's period and the fetchedAt stamp. Before, it read it again after the Cost Explorer call (up to 60 s), so a call ending just past midnight on the last day would stamp last month's bill with the new month, and the new rule would still serve it.

The cost is at most one extra Cost Explorer query a month per account (about $0.01, per the module docstring), on the first read after the UTC month turns.

aws-control is pinned in src/kiro_crew/apps/adoption-pins.json, so the fix landed in the app repository first (PR #24, merged):

App repo: KiroCrewCaptain/kirocrew-app-aws-control@b8efc3f

The two costs.py files are the same from is_fresh down. The test is the app's, with two differences: its autouse fixture patches through _floor_monkeypatch (this repository's semgrep rule), and two comments state the stamps in words rather than as ISO dates (the comment-history gate reads a date as history).

Backwards compatibility

Compatible: the cache file's shape is unchanged; a cached bill from an earlier month is fetched again once, and every other cached bill is served exactly as before.

Removes nothing: no key, route, field or record is removed; a stale cached bill stays on disk and is overwritten by the next fetch.

Tests

test/test_aws_control_costs_month_rollover.py (new; the clock is frozen through the module's own dt binding):

  • A bill fetched at 23:00 UTC on the last day, read at 10:00 UTC on the 1st through GET /costs/{account}, is fetched again.
  • A bill fetched earlier this month is still served from the cache, fresh: true, with no Cost Explorer call.
  • is_fresh on last month's bill is false.
  • The month is read in UTC: 05:00+10:00 on the 1st is still the previous month, and 23:30-05:00 on the 30th is already the next one.
  • The year rollover behaves the same.
  • A fetch that starts at 23:59:50 on the last day and whose Cost Explorer call returns after midnight is stamped in the month it asked for.

On main 5 of the 6 fail (the first with assert 312.4 == 0.5) and the same-month cache case passes; with this change all 6 pass, and so does test/test_aws_control_routes_costs_library.py.

Local gates on Linux x86_64 (Python 3.12): black, isort, flake8, the comment-history, brand, agent-SDK-boundary and file-line-cap checks, the flake and wait ratchets, and semgrep scan --config semgrep/ --baseline-commit <base> --error (1.78.0) all pass. scripts/local-gate.py --base refs/remotes/upstream/main --tree-scans: 39933 + 6693 related and 31553 + 14273 tree-scan tests pass; 13 fail, all because a child Python started with a stripped environment cannot load libffi.so.6 on that machine (test_crew_log_*, test_mcp_footprint_stub_probe, test_managed_launcher_restart, test_display_battery_rules_version, test_crew_image_build_plan, test_browser_harness_supervisor). None touch AWS Control. mypy src/kiro_crew reports 6 errors in files this PR does not touch (missing library stubs on that machine, wecom/client.py). The frontend set did not run (no website/node_modules); the diff has no frontend change.

Proof: PROVEN test/test_aws_control_costs_month_rollover.py::test_last_months_bill_is_refetched_on_the_first_day, test/test_aws_control_costs_month_rollover.py::test_a_bill_from_last_month_is_stale_for_is_fresh, test/test_aws_control_costs_month_rollover.py::test_the_month_is_read_in_utc_not_in_the_stamp_offset, test/test_aws_control_costs_month_rollover.py::test_the_year_rollover_reads_the_same_way, test/test_aws_control_costs_month_rollover.py::test_a_fetch_that_straddles_the_month_turn_is_stamped_in_the_month_it_asked_for (head a53ef81)

Manual verification

N/A: no gateway was started and no real AWS call was made. Cost Explorer is reached only through the _checked seam, which the tests patch.

Real-machine verification

Machine: own computer, Linux x86_64, Python 3.12
Before (main d72eba4, the base the fix was written on): python -m pytest -n0 test/test_aws_control_costs_month_rollover.py -> 5 failed, 1 passed
After (head a53ef81): python -m pytest -n0 test/test_aws_control_costs_month_rollover.py test/test_aws_control_routes_costs_library.py -> 44 passed
Cleanup: own computer, nothing to delete

No live run was done: the fix is verified by pytest on Linux.

Related Issues

KiroCrewCaptain/kirocrew-app-aws-control#23

App PR (merged): KiroCrewCaptain/kirocrew-app-aws-control#24

Sibling sweep

Swept: git grep -n "periodStart|monthToDate|is_fresh" -- src/kiro_crew
Sibling: src/kiro_crew/apps/builtins/aws_control/backend/costs.py -- changed here
Sibling: src/kiro_crew/apps/builtins/aws_control/backend/routes_parts/accounts.py -- not affected -- calls is_fresh and serves what it decides

Pattern harvest

Not generalizable: the defect is one cache that judged a month-to-date figure only by age; no other cache in the tree holds a figure scoped to a calendar period.

Review (multi-model interrogate)

Two independent reviewers read the full diff against CI's own lane contracts and found nothing; a final re-check of the head found nothing in the diff. Two more reviews were not counted because the AGENTS.md line count each stated did not match the file.

  • Acted on: a re-check noted that the app PR has merged, so the App repo: line names its merge commit.
  • Dismissed (low, discovery only): on the 1st, if Cost Explorer fails or consent is missing, the route still returns last month's cached bill, now labelled fresh: false. That fallback is unchanged code outside this fix, and the page captions it as cached.
  • Noted: fetchedAt is now stamped when the query starts, so a cached bill expires up to 60 s sooner. That is the intended trade.

Checklist

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

The AWS Control bill is a month-to-date figure: fetch_month_costs asks
Cost Explorer for the spend since the first of the current UTC month.
is_fresh judged a cached copy only by its age (24 hours), so a copy
fetched late on the last day of a month was still fresh on the 1st, and
Usage & costs showed the finished month's whole bill as "This month",
with no cached caption, until the cache aged out.

A cached bill fetched in another UTC month than now is no longer fresh,
whatever its age; otherwise the 24-hour rule stands. The month is read
from the stamp converted to UTC. fetch_month_costs now reads the clock
once for the query and the stamp, so a Cost Explorer call that ends past
midnight on the last day no longer stamps last month's bill with the
new month.

This is the wheel copy's half of the app repository's fix for
KiroCrewCaptain/kirocrew-app-aws-control#23
@winnythedev
winnythedev requested review from a team and KiroCrewCaptain as code owners December 5, 2026 21:06
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision a53ef8184f071b03212c6a83b2295bebe7e1a6db touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of a53ef8184f071b03212c6a83b2295bebe7e1a6db — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: a small, reversible fix to a real cache bug, landed in the app repository first as the adoption pin requires.

Notes

  • Comparing the cached periodStart with the current UTC month would test the bill's own period directly. The fetchedAt stamp would no longer stand in for it, and the single-clock-read change would not be needed.

[DESIGN-REVIEWED] a53ef81

Description read

[DESCRIPTION-READ] c46fd210458416bb643acc81c86b61854764ae72d57fa11e43b95bf0ac91a58b

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of a53ef8184f071b03212c6a83b2295bebe7e1a6db — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

The fix works. The cache already stores the month in periodStart, so checking that would make the single-clock-read change unnecessary.

Notes

  • Item 2 — oversized: the diff reads the bill's month back out of fetchedAt, which is why it needs "One clock read for the query and the stamp". The cache already records the queried month as "periodStart": start.isoformat(), written on base. A grep for periodStart in src/kiro_crew finds 1 writer and 0 readers.
    Subtraction: in is_fresh, compare cached["periodStart"] with the current UTC month's first day and drop the fetched_utc month comparison. The fetch_month_costs clock-read change and its straddle test can then go back to base.

What this change ships

Inventory (3 items) — 2 justified

Intent: on the 1st of a month, Usage & costs shows this month's spend, not last month's whole bill. This is a FIX.

  1. A cached bill from an earlier UTC month is fetched again, however young it is — justified
  2. The bill's fetchedAt stamp now records when the query started, not when it returned — oversized, periodStart already records the month
  3. A new test file pins month and year rollover and the UTC reading of the stamp — justified

[FIRST-PRINCIPLES-REVIEWED] a53ef81

Description read

[DESCRIPTION-READ] e3a95d59cd2418e2b545b8780934223c9a2fedf8e286c42830a4f591bf8e2e2e

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed a53ef8184f071b03212c6a83b2295bebe7e1a6db — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] a53ef81

Verdict parsed from the review's SHA-scoped output markers for commit a53ef8184f071b03212c6a83b2295bebe7e1a6db.

False positive or not applicable? A repository writer can comment:
/ai-review override fable a53ef8184f071b03212c6a83b2295bebe7e1a6db false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of a53ef8184f071b03212c6a83b2295bebe7e1a6db and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a53ef81

False positive or not applicable? A repository writer can comment:
/ai-review override gpt a53ef8184f071b03212c6a83b2295bebe7e1a6db false-positive: <reason, 20 to 500 characters>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: checking Automated validation is still running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant