Skip to content

fix: list same-second changes newest first on SQLite - #744

Merged
enyst merged 4 commits into
mainfrom
claude/project-thread-aesue3-change-order
Nov 11, 2026
Merged

enyst merged 4 commits into
mainfrom
claude/project-thread-aesue3-change-order

Conversation

@enyst

@enyst enyst commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.

Summary

GET /v1/{id}/changes orders by created_at DESC, id DESC, but created_at came only from server_default=CURRENT_TIMESTAMP. SQLite stores that in whole seconds (and PostgreSQL fixes it at transaction start), so a create and an immediate PATCH tied, and the uuid4 id tie-break listed them in random order (the flaky F05.changes bullet). The automations, runs, uploads and webhooks lists had the same problem. Every created_at column is now also written from Python (utcnow, microseconds), the clock updated_at already uses. The server default stays, so there is no schema change, and the response shape does not change.

Issue Number

Fixes #739

Changes

  • models.py: default=utcnow on every created_at column (automations, runs, uploads, webhooks, change events, drafts, disable events, service metadata, KV meta, KV, git sync state and org config). One rule for all tables, because some code compares created_at across tables (see Notes). server_default is kept, so DDL and Alembic are unchanged.
  • Runs list (router.py), uploads list (uploads.py) and webhooks list (webhook_router.py) now add id DESC after created_at DESC, like the automations and changes lists already do, so offset pages stay stable on exact ties.
  • The /changes docstring and comment describe the ordering.
  • Tests:
    • tests/test_change_events.py::test_same_second_changes_list_newest_first_on_sqlite: a create and four updates on SQLite, checking the endpoint's sort.
    • tests/test_router.py::...::test_changes_endpoint_lists_back_to_back_changes_newest_first: three PATCHes, full /changes order on PostgreSQL.
    • tests/test_unhealthy_automations.py::test_runs_from_the_same_second_as_the_disable_stay_before_the_boundary: failures and a disable in the same second do not count again after a re-enable.
  • Feature map: removed the sleep 1.x workarounds from F04.list, F05.list and F10.list; rewrote the "one-second precision" gotchas in F04, F05, F07 and F10. The F05 gotcha covers /changes ordering and legacy whole-second rows.

Evidence

  • Unit regressions, baseline (origin/main 1b571fd) vs branch: the SQLite /changes test fails 3/3 on baseline; the PostgreSQL router test fails on baseline (2/4 and 4/4 in two independent runs); both pass on the branch. The unhealthy-boundary test fails 3/3 at the first commit of this PR and passes at 7f21bc1.
  • control-automation map run --file F05 --only F05.changes --fresh --repeat 5: baseline 2 fail / 3 pass (wrong order, the reported race); branch 5/5 pass, and 3/3 again at 7f21bc1.
  • map run --file F14 --fresh at 7f21bc1: 14 pass, 0 fail. F04.list and F10.list with the sleeps removed, --repeat 3: 3/3 pass each.
  • map run --file F05 --fresh: the only fail is F05.tarball-path-errors, map drift on main fixed in docs(verify-automation): correct map drift in F03, F05 and F11 #738.
  • uv run pytest -q -x at 7f21bc1: 2888 passed, 7 skipped. tests/test_verify_automation_skill.py: 59 passed.
  • pre-commit run --files <changed>: ruff format, ruff lint, pycodestyle and pyright pass.
  • Independent review: its finding (runs in microseconds compared against whole-second disable events in utils/unhealthy.py, a regression introduced by the first commit) is fixed in 7f21bc1 by applying the Python default to every created_at.

Notes

utils/unhealthy.py filters AutomationRun.created_at > max(AutomationDisableEvent.created_at). With runs in microseconds and disable events in whole seconds, runs from earlier in the same second as a disable would count again after a re-enable, so every table now uses the same clock.

Queries that order or bound by created_at:

  • Automations list and /changes: fixed by the Python default; both already had an id tie-break.
  • Runs, uploads and webhooks lists: fixed by the Python default, plus an id DESC tie-break.
  • conversations.py (latest run for a subject), utils/unhealthy.py (recent runs, the disable boundary, success since a bound), dispatcher.py (oldest PENDING first) and utils/templates.py (oldest template match): fixed by the Python default; none pages.
  • watchdog.py (a run's deadline anchor) and utils/cron.py (first reference time) compare against Python times, now from the same clock.

Rows written before this change keep whole-second created_at on SQLite and order among themselves by id (stable, not time order); there is nothing to backfill from. On PostgreSQL, created_at now uses the app clock at flush time instead of the transaction start time.

🤖 Generated with Claude Code

https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8

created_at on automations, runs, uploads, webhooks and change events
came only from CURRENT_TIMESTAMP, which SQLite stores in whole seconds
(and PostgreSQL fixes at transaction start). Back-to-back writes tied,
and the uuid4 tie-break shuffled them, so GET /v1/{id}/changes could
list a create before the update that followed it.

Write created_at from Python (utcnow, microseconds) on those five
tables, keeping the server default; no schema change. Add an id
tie-break to the runs, uploads and webhooks lists so pages stay stable
on exact ties, and drop the sleeps the verify-automation map used to
work around the one-second precision.

Fixes #739

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
@enyst enyst self-assigned this Nov 11, 2026
@github-actions github-actions Bot added the type: fix A bug fix label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

The unhealthy-automation check compares run created_at against the last
AutomationDisableEvent.created_at. Runs now carry microseconds while the
disable event kept SQLite's whole-second CURRENT_TIMESTAMP, so runs from
earlier in the same second as a disable counted as post-re-enable
history and the automation re-disabled below the threshold.

Give every created_at column default=utcnow (drafts, disable events,
service metadata, KV, git sync state and org config), so all of them
share one clock, and add a same-second regression test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
@enyst
enyst requested a review from all-hands-bot November 11, 2026 22:46
@all-hands-bot

Copy link
Copy Markdown
Contributor

⏳ Review waiting on checks

The current head c9f7778817ee still has checks that have not finished, so no review conversation was started:

  • Docker
  • PR Artifacts
  • PR Description Check
  • Run tests
  • ci
  • pr

No action is needed. The scheduled scan retries once every check on the head reports a conclusion.

This is an automated check - no AI was used to generate this comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8

@all-hands-bot all-hands-bot left a comment

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.

This review was posted by an AI agent (OpenHands).

Scope

The change is squarely in this repository's ownership: created_at handling for automation, run, upload, webhook and change-event rows, plus tie-breaks on three list endpoints of this service. No cross-repository contract changes — response shapes, DDL and Alembic state are unchanged (server_default retained, so create_all/migrations stay identical). No product or architecture decision is needed; the review continues on technical grounds.

Assessment

The root cause is correctly diagnosed and the fix is coherent:

  • SQLite materializes CURRENT_TIMESTAMP in whole seconds and PostgreSQL pins it to transaction start, so a create and an immediate PATCH (or a run and a disable event) could share a created_at, and the uuid4 id tie-break listed them in random order. Adding a Python-side default=utcnow (microseconds) to every created_at column while keeping the server default fixes ordering and — more importantly — makes cross-table comparisons (AutomationRun.created_at > max(AutomationDisableEvent.created_at) in utils/unhealthy.py) use one clock. Applying it to all tables in one commit was the right call: a runs-only fix would have reintroduced a mixed-precision boundary bug at the re-enable check.
  • id DESC tie-breaks added to the runs, uploads and webhooks lists match the automations and changes lists and satisfy the repository's pagination guidance (unique tie-breaker, stable offset pages).
  • Legacy rows are handled honestly: they keep whole-second created_at on SQLite, order among themselves by id (stable, not time-ordered), and the feature-map gotchas now describe exactly that, including the still-raw automation_service_metadata inserts. No backfill is needed or claimed.
  • conversations.py (latest run per subject) and dispatcher.py (oldest PENDING) still order on created_at alone, but neither paginates and both are disclosed in the PR notes; unchanged pre-existing behavior, not a merge risk.

Verification

  • tests/test_change_events.py + tests/test_unhealthy_automations.py: 29 passed, including the two new same-second tests, stable across 5 repeated runs.
  • tests/test_verify_automation_skill.py: 59 passed (feature-map consistency).
  • ruff check and ruff format --check on all changed Python files: clean.
  • The PostgreSQL-backed router test could not be executed in this workspace (no Docker daemon); the mechanism under test is the client-side default, which is backend-independent, and both new tests run on SQLite where the bug was observable.
  • Re-checked current PR state before posting: head is still c9f7778817eeac15f5ab903f0b57921b6dd44bcf, PR is open with type: fix, review request to all-hands-bot pending, no prior reviews, and linked issue #739 is open (bug, ready-for-dev, priority:low). CI reports no checks for this head.

No material correctness, security, or compatibility findings.

✅ APPROVED

@enyst
enyst merged commit c647c4b into main Nov 11, 2026
11 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.33.0 Shipped in 1.33.0 label Nov 17, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.33.0.

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

Labels

released: 1.33.0 Shipped in 1.33.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: GET /v1/{id}/changes returns same-second changes in random order on SQLite

2 participants