Repository navigation
fix: list same-second changes newest first on SQLite - #744
Conversation
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
|
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8
⏳ Review waiting on checksThe current head
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
left a comment
There was a problem hiding this comment.
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_TIMESTAMPin 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 acreated_at, and the uuid4idtie-break listed them in random order. Adding a Python-sidedefault=utcnow(microseconds) to everycreated_atcolumn while keeping the server default fixes ordering and — more importantly — makes cross-table comparisons (AutomationRun.created_at > max(AutomationDisableEvent.created_at)inutils/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 DESCtie-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_aton SQLite, order among themselves byid(stable, not time-ordered), and the feature-map gotchas now describe exactly that, including the still-rawautomation_service_metadatainserts. No backfill is needed or claimed. conversations.py(latest run per subject) anddispatcher.py(oldest PENDING) still order oncreated_atalone, 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 checkandruff format --checkon 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 withtype: fix, review request toall-hands-botpending, 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
|
🚀 Released in 1.33.0. |
I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.
Summary
GET /v1/{id}/changesorders bycreated_at DESC, id DESC, butcreated_atcame only fromserver_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 uuid4idtie-break listed them in random order (the flakyF05.changesbullet). The automations, runs, uploads and webhooks lists had the same problem. Everycreated_atcolumn is now also written from Python (utcnow, microseconds), the clockupdated_atalready 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=utcnowon everycreated_atcolumn (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 comparescreated_atacross tables (see Notes).server_defaultis kept, so DDL and Alembic are unchanged.router.py), uploads list (uploads.py) and webhooks list (webhook_router.py) now addid DESCaftercreated_at DESC, like the automations and changes lists already do, so offset pages stay stable on exact ties./changesdocstring and comment describe the ordering.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/changesorder 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.sleep 1.xworkarounds fromF04.list,F05.listandF10.list; rewrote the "one-second precision" gotchas in F04, F05, F07 and F10. The F05 gotcha covers/changesordering and legacy whole-second rows.Evidence
/changestest 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 --freshat 7f21bc1: 14 pass, 0 fail.F04.listandF10.listwith the sleeps removed,--repeat 3: 3/3 pass each.map run --file F05 --fresh: the only fail isF05.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 -xat 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.utils/unhealthy.py, a regression introduced by the first commit) is fixed in 7f21bc1 by applying the Python default to everycreated_at.Notes
utils/unhealthy.pyfiltersAutomationRun.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:/changes: fixed by the Python default; both already had anidtie-break.id DESCtie-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) andutils/templates.py(oldest template match): fixed by the Python default; none pages.watchdog.py(a run's deadline anchor) andutils/cron.py(first reference time) compare against Python times, now from the same clock.Rows written before this change keep whole-second
created_aton SQLite and order among themselves byid(stable, not time order); there is nothing to backfill from. On PostgreSQL,created_atnow uses the app clock at flush time instead of the transaction start time.🤖 Generated with Claude Code
https://claude.ai/code/session_013ArwwM9ABBZYP9fcggw4R8