{"record":{"id":"4d165deece95c30d","repo":"666ghj/MiroFish","slug":"reconstruction-dates-must-be-strictly-increasing","errorCode":null,"errorMessage":"reconstruction dates must be strictly increasing","messagePattern":"reconstruction dates must be strictly increasing","errorType":"exception","errorClass":"StarHistoryError","httpStatus":null,"severity":"error","filePath":"scripts/star_history.py","lineNumber":415,"sourceCode":"    previous_stars = 0\n    for index, raw_point in enumerate(daily):\n        if not isinstance(raw_point, dict):\n            raise StarHistoryError(\"reconstruction point must be an object\")\n        _expect_keys(raw_point, {\"date\", \"stars\"}, \"reconstruction point\")\n        raw_date = raw_point[\"date\"]\n        if not isinstance(raw_date, str):\n            raise StarHistoryError(\"reconstruction date must be a string\")\n        try:\n            point_day = date.fromisoformat(raw_date)\n        except ValueError as exc:\n            raise StarHistoryError(\"reconstruction date is invalid\") from exc\n        if point_day.isoformat() != raw_date:\n            raise StarHistoryError(\"reconstruction date is not canonical\")\n        stars = _strict_non_negative_int(raw_point[\"stars\"], \"reconstruction stars\")\n        if index == 0 and stars <= 0:\n            raise StarHistoryError(\"first reconstruction point must have stars\")\n        if previous_day is not None and point_day <= previous_day:\n            raise StarHistoryError(\"reconstruction dates must be strictly increasing\")\n        if index > 0 and stars <= previous_stars:\n            raise StarHistoryError(\"reconstruction stars must be strictly increasing\")\n        if point_day >= generated_at.date():\n            raise StarHistoryError(\"reconstruction must contain only completed UTC dates\")\n        previous_day = point_day\n        previous_stars = stars\n\n    snapshots = state[\"snapshots\"]\n    if not isinstance(snapshots, list):\n        raise StarHistoryError(\"snapshots must be a list\")\n    previous_snapshot: datetime | None = None\n    first_snapshot: datetime | None = None\n    for raw_snapshot in snapshots:\n        if not isinstance(raw_snapshot, dict):\n            raise StarHistoryError(\"snapshot must be an object\")\n        _expect_keys(raw_snapshot, {\"at\", \"stars\"}, \"snapshot\")\n        snapshot_at = _parse_state_timestamp(raw_snapshot[\"at\"], \"snapshot.at\")\n        _strict_non_negative_int(raw_snapshot[\"stars\"], \"snapshot stars\")","sourceCodeStart":397,"sourceCodeEnd":433,"githubUrl":"https://github.com/666ghj/MiroFish/blob/b5b53acc57189a4a42e44a23e149dc655c98fe82/scripts/star_history.py#L397-L433","documentation":"Raised by validate_state() when daily points are not strictly increasing by date — i.e. a date is <= the previous date (duplicates and backwards ordering both fail). Strict ordering is required because each point is a cumulative star total for that UTC day.","triggerScenarios":"validate_state(state) where daily contains two points with the same date, or the list is unsorted (e.g. sorted by stars or built from an unordered dict).","commonSituations":"Building daily from a date-keyed dict without sorting by date; merging two state files by concatenation; deduplication logic that leaves duplicate dates.","solutions":["Sort daily by date ascending and deduplicate dates before writing state","When merging, keep the point with the correct cumulative value for each date","Add a unit test that asserts strict date monotonicity on generated state"],"exampleFix":"# before\n\"daily\": [{\"date\": \"2024-01-02\", ...}, {\"date\": \"2024-01-01\", ...}]\n# after\npoints = sorted(points, key=lambda p: p[\"date\"])  # dedupe first, then write","handlingStrategy":"validation","validationCode":"daily.sort(key=lambda p: p[\"date\"])\ndedup = {p[\"date\"]: p for p in daily}\ndaily = [dedup[d] for d in sorted(dedup)]","typeGuard":"def dates_strictly_increasing(daily: list) -> bool:\n    return all(\n        daily[i][\"date\"] < daily[i + 1][\"date\"] for i in range(len(daily) - 1)\n    )","tryCatchPattern":"try:\n    validate_state(state)\nexcept StarHistoryError as exc:\n    if \"reconstruction dates must be strictly increasing\" in str(exc):\n        # sort + dedupe by date, then re-validate\n        ...","preventionTips":["Always sort daily by date before writing state","Never merge daily arrays by concatenation","Deduplicate on date when combining sources"],"tags":["validation","sorting","state-file"],"backgroundTag":null,"analyzedSha":"b5b53acc57189a4a42e44a23e149dc655c98fe82","analyzedAt":"2026-08-14T22:29:33.146Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}