Skip to content

fix(server): inline LIMIT/OFFSET so TiDB Cloud accepts paged queries - #251

Merged
i098 merged 2 commits into
mainfrom
fm/sanctum-tidb-jobs
Nov 24, 2026
Merged

i098 merged 2 commits into
mainfrom
fm/sanctum-tidb-jobs

Conversation

@i098

@i098 i098 commented Nov 24, 2026 •

Copy link
Copy Markdown
Owner

Intent

Uhh tidb cloud starter + a d1 clone so we can switch over once free runs out
Launch: hosted Sanctum app back up and proven live

Context: production runs on TiDB Cloud Starter since #244 (main b48350f). Six server tests fail against TiDB and pass on MySQL 8.4: #245 (job ledger tests: claims each due job exactly once, fences completion by lease token, keeps renewing through a database outage, runs handlers and renews leases; action tests: lists one meeting's receipts in offset pages, records unknown when the job ceiling cuts off a submission). It is not known whether production jobs can be claimed two times on TiDB.

What Changed

  • server/src/db.ts: on a database without FULLTEXT (TiDB), the translated connection wrapper now passes each safe non-negative integer LIMIT ? or OFFSET ? bound as text in the SQL. TiDB rejects a prepared LIMIT ?. The parameter leaves the list, and other placeholders stay bound. This fixes paged queries such as a meeting's action receipts (fix(server): job ledger and action tests fail against TiDB Cloud #245).
  • Tests and config: server/vitest.config.ts reads the test timeout from SANCTUM_TEST_TIMEOUT_MS (default 30000), for a database across the internet. server/tests/actions.test.ts raises the worker ceilingMs from 300 to a CEILING_MS of 3000, because each statement to TiDB Cloud takes about 33 ms.
  • Docs: docs/operations.md describes the TiDB LIMIT ? handling and the new timeout variable. changelog.d/245.fixed.md adds a changelog entry.

Risk Assessment

✅ Low: The code change is a narrow TiDB-only rewrite of LIMIT/OFFSET placeholders plus a test-only ceiling and timeout. Whether the job-ledger tests and the double-claim question are settled on TiDB stays unverified until a TiDB run.

Testing

The jobs and actions suites passed 22 of 22 on target code against both a disposable local TiDB v8.5.0 and MySQL 8.4.3, so there is no regression off TiDB. The offset-pages test fails on the base db.ts and passes with the change, which proves the LIMIT/OFFSET inlining. The other five tests from issue 245 pass locally but also passed at base for the one I re-ran (the claims test). I did not re-run the other four on base. Their Cloud failures are not reproduced here, and the double-claim question stays open until a run on TiDB Cloud Starter. The change has no UI, so there are no screenshots.

  • Live validation: ✅ go - 5 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Meeting action receipts list in offset pages on TiDB (LIMIT/OFFSET bounds inlined) ✅ pass live The 'lists one meeting's receipts … in offset pages' test passes on the target commit and fails with 'Incorrect arguments to LIMIT' on the base db.ts, both against local TiDB v8.5.0.
Job ledger claims each due job exactly once across concurrent workers (local TiDB only) ✅ pass live The test passes on local TiDB v8.5.0 and on MySQL 8.4.3. It also passed on the base db.ts locally, so the reported failure and the double-claim risk are not reproduced here.
Job ledger fences by lease token, runs handlers and renews leases, and keeps renewing through a database outage (local TiDB only) ✅ pass live All 11 jobs.test.ts tests pass on local TiDB v8.5.0 and on MySQL 8.4.3. The reported Cloud failures were not reproduced here.
A job ceiling cut-off during action submission records unknown and never resubmits, with the 3 s ceiling ✅ pass live The actions.test.ts test passes on local TiDB v8.5.0 and on MySQL 8.4.3. All 11 actions tests pass on both.
No regression on MySQL 8.4: the same suites on a real MySQL 8.4.3 server ✅ pass live mysql-8.4.3-jobs-actions-run.txt: 22 of 22 passed, with SANCTUM_TEST_TIMEOUT_MS=180000 set for a slow disk.
Suites on TiDB Cloud Starter, including the job-ledger failures and the double-claim question from issue 245 ⏸️ untested no I have no TiDB Cloud Starter credentials and made no network connection to any cluster. To run it, set SANCTUM_TEST_MYSQL_URL to a Starter account allowed to CREATE/DROP DATABASE and `SANCTUM_TEST_M…
Adversarial: negative or non-integer bound values stay as bound parameters (not inlined) ⏸️ untested no inlineBounds is not exported and the suites only pass valid integer bounds. I did not add a test that checks the source text. A listing test that passes a negative or fractional bound against TiDB w…
Evidence: Jobs and actions suites on TiDB v8.5.0 with the fix: 22 of 22 passed
    at PoolConnection.handlePacket (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:616:34)
    at PacketParser.onPacket (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:148:12)
    at PacketParser.executeStart (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/packet_parser.js:86:16)
    at Socket.<anonymous> (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:156:25)
    at Socket.emit (node:events:514:20)
    at addChunk (node:internal/streams/readable:605:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:556:3)
    at sql.transaction {
  [cause]: Error: Table 'sanctum_t_0409ce887b84.jobs' doesn't exist
      at Packet.asError (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/packets/packet.js:930:17)
      at Prepare.execute (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/commands/command.js:29:26)
      at PoolConnection.handlePacket (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:616:34)
      at PacketParser.onPacket (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:148:12)
      at PacketParser.executeStart (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/packet_parser.js:86:16)
      at Socket.<anonymous> (/sdc-disk/moved/nm-worktrees/.treehouse_firstmate-8bf1b0_3_firstmate_projects_sanctum/01M865C566GW5EG9473Q8EGX96/node_modules/mysql2/lib/base/connection.js:156:25)
      at Socket.emit (node:events:514:20)
      at addChunk (node:internal/streams/readable:605:12)
      at readableAddChunkPushByteMode (node:internal/streams/readable:556:3)
      at Socket.Readable.push (node:internal/streams/readable:436:5)
}"

 ✓ |server| tests/jobs.test.ts (11 tests) 145746ms
   ✓ job ledger > coalesces active work keys per workspace and keeps finished history  14782ms
   ✓ job ledger > claims each due job exactly once across concurrent workers  14208ms
   ✓ job ledger > fences completion by lease token and generation after an expired lease is reclaimed  13976ms
   ✓ job ledger > retries retryable failures with backoff up to max_attempts and fails others at once  13877ms
   ✓ job ledger > parks paused jobs without spending an attempt and resumes them  14110ms
   ✓ job ledger > re-arms a running job so the latest payload runs after the current run  13681ms
   ✓ job ledger > runs handlers, renews leases, re-checks requesters and records defects  18634ms
   ✓ job ledger > stops a handler whose lease was taken over and writes nothing for it  12745ms
   ✓ job ledger > fails a hung handler at its ceiling retryably, interrupts it and frees its slot  13043ms
   ✓ job ledger > keeps renewing, claiming and sweeping through a database outage  16442ms
 ✓ |server| tests/actions.test.ts (11 tests) 162134ms
   ✓ action gateway > executes a granted action once with a truthful receipt and a stable provider key  14923ms
   ✓ action gateway > keeps an over-budget provider result as an artifact the receipt references  14108ms
   ✓ action gateway > deduplicates repeated requests and rejects a reused key with different content  14334ms
   ✓ action gateway > lists one meeting's receipts oldest first, only to their requester and admins, in offset pages  14605ms
   ✓ action gateway > refuses requests no active grant covers  14762ms
   ✓ action gateway > cancels a queued action whose grant is revoked before the worker submits it  14059ms
   ✓ action gateway > records a timeout after upstream success as unknown, never replays it, and lets a person resolve it  14425ms
   ✓ action gateway > records unknown when the provider does not answer in time, then settles from the late answer  13444ms
   ✓ action gateway > records unknown when the job ceiling cuts off a submission and never resubmits it  17260ms
   ✓ action gateway > marks a definite provider rejection failed without retrying  14432ms
   ✓ action gateway > lets only the human account owner grant, and hides receipts across principals and workspaces  15736ms

 Test Files  2 passed (2)
      Tests  22 passed (22)
   Start at  07:00:53
   Duration  170.07s (transform 2.20s, setup 0ms, collect 10.32s, tests 307.88s, environment 1ms, prepare 506ms)
Evidence: Jobs and actions suites on MySQL 8.4.3: 22 of 22 passed
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
  [cause]: Error: Table 'sanctum_t_9251ae28a7cc.jobs' doesn't exist
 ✓ |server| tests/jobs.test.ts (11 tests) 150947ms
   ✓ job ledger > coalesces active work keys per workspace and keeps finished history  20074ms
   ✓ job ledger > claims each due job exactly once across concurrent workers  11624ms
   ✓ job ledger > fences completion by lease token and generation after an expired lease is reclaimed  11605ms
   ✓ job ledger > retries retryable failures with backoff up to max_attempts and fails others at once  16107ms
   ✓ job ledger > parks paused jobs without spending an attempt and resumes them  15021ms
   ✓ job ledger > re-arms a running job so the latest payload runs after the current run  13979ms
   ✓ job ledger > runs handlers, renews leases, re-checks requesters and records defects  18883ms
   ✓ job ledger > stops a handler whose lease was taken over and writes nothing for it  12582ms
   ✓ job ledger > fails a hung handler at its ceiling retryably, interrupts it and frees its slot  15093ms
   ✓ job ledger > keeps renewing, claiming and sweeping through a database outage  15764ms
 ✓ |server| tests/actions.test.ts (11 tests) 161107ms
   ✓ action gateway > executes a granted action once with a truthful receipt and a stable provider key  19530ms
   ✓ action gateway > keeps an over-budget provider result as an artifact the receipt references  11012ms
   ✓ action gateway > deduplicates repeated requests and rejects a reused key with different content  11631ms
   ✓ action gateway > lists one meeting's receipts oldest first, only to their requester and admins, in offset pages  14458ms
   ✓ action gateway > refuses requests no active grant covers  15145ms
   ✓ action gateway > cancels a queued action whose grant is revoked before the worker submits it  13683ms
   ✓ action gateway > records a timeout after upstream success as unknown, never replays it, and lets a person resolve it  13144ms
   ✓ action gateway > records unknown when the provider does not answer in time, then settles from the late answer  17653ms
   ✓ action gateway > records unknown when the job ceiling cuts off a submission and never resubmits it  17722ms
   ✓ action gateway > marks a definite provider rejection failed without retrying  14122ms
   ✓ action gateway > lets only the human account owner grant, and hides receipts across principals and workspaces  12978ms
 Test Files  2 passed (2)
      Tests  22 passed (22)
  • Outcome: ⚠️ 1 warning across 1 run (20m5s)

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ server/vitest.config.ts:8 - This change does not prove the intent's job-ledger and double-claim questions. claimJob uses a literal LIMIT 1, so inlineBounds never touches it. The AND status = &#39;pending&#39; plus affectedRows guard that stops a double claim is already in base (6fd0a52). For the four job-ledger tests (jobs.test.ts:75, :100, :208, :301), the only lever here is the opt-in SANCTUM_TEST_TIMEOUT_MS. The comment says migrating one test database takes 50-110 s on TiDB Cloud. The 30 s test timeout would cover that migration, so a timeout-only failure is plausible. I could not show it without running tests. The changelog names only the LIMIT fix. The env var appears only in this config file, not in docs/operations.md. Closing fix(server): job ledger and action tests fail against TiDB Cloud #245 and the double-claim question needs a run against TiDB with the variable set; the changelog and the operations docs should also mention the override.
⚠️ **Test** - 1 warning
  • ⚠️ Whether production jobs can be claimed twice on TiDB Cloud Starter is still open. The 'claims each due job exactly once' and 'fences/renews/outage' job-ledger tests also passed on the base commit's db.ts against local TiDB v8.5.0. Local TiDB has no ~33 ms round trip, so those six failures were not reproduced here. Only the LIMIT inlining for offset pages was shown to fix a failure. The vitest timeout and ceiling changes are not shown to fix the job-ledger failures on Cloud. To close the question, run the suites with SANCTUM_TEST_MYSQL_URL and SANCTUM_TEST_MYSQL_CA_CERT set for a Starter cluster, with SANCTUM_TEST_TIMEOUT_MS raised, and add a double-claim check against that cluster.
  • Live validation: ✅ go - 5 of 7 scenarios driven live against the product
Scenario Result Live Evidence
Meeting action receipts list in offset pages on TiDB (LIMIT/OFFSET bounds inlined) ✅ pass live The 'lists one meeting's receipts … in offset pages' test passes on the target commit and fails with 'Incorrect arguments to LIMIT' on the base db.ts, both against local TiDB v8.5.0.
Job ledger claims each due job exactly once across concurrent workers (local TiDB only) ✅ pass live The test passes on local TiDB v8.5.0 and on MySQL 8.4.3. It also passed on the base db.ts locally, so the reported failure and the double-claim risk are not reproduced here.
Job ledger fences by lease token, runs handlers and renews leases, and keeps renewing through a database outage (local TiDB only) ✅ pass live All 11 jobs.test.ts tests pass on local TiDB v8.5.0 and on MySQL 8.4.3. The reported Cloud failures were not reproduced here.
A job ceiling cut-off during action submission records unknown and never resubmits, with the 3 s ceiling ✅ pass live The actions.test.ts test passes on local TiDB v8.5.0 and on MySQL 8.4.3. All 11 actions tests pass on both.
No regression on MySQL 8.4: the same suites on a real MySQL 8.4.3 server ✅ pass live mysql-8.4.3-jobs-actions-run.txt: 22 of 22 passed, with SANCTUM_TEST_TIMEOUT_MS=180000 set for a slow disk.
Suites on TiDB Cloud Starter, including the job-ledger failures and the double-claim question from issue 245 ⏸️ untested no I have no TiDB Cloud Starter credentials and made no network connection to any cluster. To run it, set SANCTUM_TEST_MYSQL_URL to a Starter account allowed to CREATE/DROP DATABASE and `SANCTUM_TEST_M…
Adversarial: negative or non-integer bound values stay as bound parameters (not inlined) ⏸️ untested no inlineBounds is not exported and the suites only pass valid integer bounds. I did not add a test that checks the source text. A listing test that passes a negative or fractional bound against TiDB w…
  • Local tidb-server v8.5.0 (unistore), SET GLOBAL time_zone=&#39;+00:00&#39;: npx vitest run tests/jobs.test.ts tests/actions.test.ts on the target commit, 22 of 22 passed
  • With server/src/db.ts restored to the base commit and -t &#34;claims each due job|offset pages&#34; on the same TiDB: the offset-pages test failed with ER_WRONG_ARGUMENTS 'Incorrect arguments to LIMIT'. The claims test passed. db.ts was restored afterwards.
  • Portable MySQL 8.4.3 tarball (libaio taken from a downloaded deb into the temp dir), started with relaxed fsync settings on port 3399 with --default-time-zone=+00:00, then the same suites with SANCTUM_TEST_TIMEOUT_MS=180000: 22 of 22 passed. A first run at the default 30 s timeout failed 15 tests because migrating each test database took about 28 s on this slow disk. I treat that as a setup problem, not a product failure.
  • The 'Table ... jobs doesn't exist' log lines also appear on MySQL 8.4.3, so they are not TiDB-specific. They look like a worker outliving suite teardown, and the tests still pass.
  • Cleanup: stopped the processes I started (TiDB and mysqld), removed the downloaded binaries and temp data. git status is clean.
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

Closes #245

Before and after (TiDB Cloud Starter, us-east-1, median round trip 32.9 ms)

Before: the six tests from #245 failed on Cloud. After: all six pass on Cloud, and the full server suite passes on MySQL 8.4 (513 of 513).

  • Cloud evidence: data/sanctum-tidb-jobs/tidb-six-final.log (six tests, 453 s): claims once 87 s, fences 83 s, runs handlers 74 s, keeps renewing 58 s, offset pages 50 s, job ceiling 75 s. Earlier Cloud runs: tidb-six-1.log, tidb-six-2.log, tidb-six-3.log, tidb-six-4.log. MySQL proof: mysql-full-final.log. All in data/sanctum-tidb-jobs/.
  • Local TiDB v8.5.0 does not reproduce the failures (no WAN latency), as the test step says. The Cloud log is the evidence.
  • Which change fixed which failure:
    • Offset pages and "runs handlers, renews leases, re-checks requesters": real TiDB difference, a prepared LIMIT ? is rejected. Fixed by the db.ts inlining.
    • Job ceiling cut-off: latency. About 12 statements run before the submission is recorded, which at 33 ms exceeds the 300 ms ceiling. Fixed by the 3 s test ceiling.
    • Claims exactly once, fences by lease token, keeps renewing: latency only. Migrating one test database takes 50–110 s on Cloud, over the 30 s timeout. They passed with SANCTUM_TEST_TIMEOUT_MS raised; no code change. The claim test returned 12 distinct claims from 4 concurrent workers. No claim or lease defect was found; the base claim already guards with status = 'pending' (fix(server): run migrations, search and upserts on TiDB Cloud #244).
  • ponytail-review: one round, nothing to cut. Keep: inlineBounds stays in db.ts (the one place every statement passes), and it counts ? characters, so a ? in a SQL string literal would misalign it.

@i098 i098 changed the title fix(server): inline LIMIT/OFFSET bounds so TiDB Cloud accepts paged queries fix(server): inline LIMIT/OFFSET so TiDB Cloud accepts paged queries Nov 24, 2026

@i098 i098 left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

no-mistakes run 01M865C566GW5EG9473Q8EGX96, validated head 9adf984.

Checked: intent, rebase, review, test, document, lint, push, PR, CI.

  • review (info): the change does not itself prove the job-ledger and double-claim questions. Resolved: Cloud evidence is tidb-six-final.log (six tests on TiDB Cloud Starter); no claim or lease defect found, the status = 'pending' claim guard is in base (#244).
  • test warning test-1 (ask-user): double claim on Cloud still open, local TiDB does not reproduce. Resolved by firstmate approval, citing the Cloud log above.
  • ci finding ci-1 (ask-user): PR title 75 chars over the 72 limit. Resolved: title renamed to 68 chars, Commit standard re-ran and passed.
  • Final CI: all checks pass, including Application checks. The Pages jobs are skipped.

@i098
i098 merged commit 3212940 into main Nov 24, 2026
30 of 38 checks passed
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.

fix(server): job ledger and action tests fail against TiDB Cloud

1 participant