Repository navigation
Order pending work by commit and snapshot timestamps - #220
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
commit: |
|
One thing to note here is that reverting to an older version would require a migration to convert segments into their old format, - which could be hard to get right, but could make some assumptions about the maximum segment a user would have previously used, or migrate based on creation time of the documents or something..? |
reeceyang
left a comment
There was a problem hiding this comment.
When users migrate to this version, would work that was originally scheduled to run in the future get executed immediately?
4e3cbd1 to
99c41ba
Compare
d55a95d to
b619fc5
Compare
|
@cubic review |
@ianmacartney I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
All reported issues were addressed across 26 files
Not reviewed (too large): example/benchmarks/2026-09-08.json (~5,865 lines) - if these are generated or fixture files, add them to ignored paths to exclude them from future reviews.
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
All reported issues were addressed across 17 files (changes from recent commits).
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
90bf8f7 to
0b0b60e
Compare
|
I split out the pendingStart batching into a stacked PR |
There was a problem hiding this comment.
All reported issues were addressed across 20 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
2ccceca to
ba7eb18
Compare
| // (1) Out-of-order entries left at the boundary stamp. Nothing at or above | ||
| // the cursor is read, so a bulk enqueue sharing this stamp costs nothing. |
There was a problem hiding this comment.
which cursor is meant in
Nothing at or above the cursor is read, so a bulk enqueue sharing this stamp costs nothing.
It looks like we do read at least one doc at or above the sweep cursor?
There was a problem hiding this comment.
We start at the sweepCursor for the scanTs field, but we only read up to incomingCursor for the segment, since anything greater than that will get picked up by the normal incomingCursor query.
So if sweep is at 3, incomingCursor is at 10, the queries are:
1. scanTs == sweepCursor && segment < incomingCursor
2. scanTs ≥ sweepCursor
3. segment ≥ incomingCursor
(1) is an optimization for the case where a huge transaction lands in a single scanTs. Instead of scanning over the whole transaction, once we land in the middle of it, the next iteration will only consider the section of it that is actually actionable, before moving on to the next scanTs.
| // ──────────────────────────────────────────────────────────────────── | ||
|
|
||
| describe("getBatch", () => { | ||
| it("waits for wall time when the snapshot clock is ahead", async () => { |
There was a problem hiding this comment.
this scenario is not possible in practice, right?
There was a problem hiding this comment.
it was possible before, but I believe the Date.now() change should be out soon/already. And it can happen in convex-test pretty often, since Date.now() is fixed for tests, but each transaction bumps the db clock
| expect(o.running.map((r) => r.workId)).toEqual([scheduledId]); | ||
| }); | ||
|
|
||
| it("starts scheduled work once it's due, without ever rewriting it", async () => { |
There was a problem hiding this comment.
we don't ever rewrite scheduled work now, right? (unless it's being upgraded from a legacy entry?)
ba7eb18 to
98666b0
Compare
65c31bd to
65184eb
Compare
65184eb to
155405d
Compare
0287944 to
93fee9e
Compare
ddd0ed3 to
469ad37
Compare
fb5a6d9 to
68a0983
Compare
68a0983 to
dc2793c
Compare
dc2793c to
f48a486
Compare
There was a problem hiding this comment.
3 issues found across 20 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="src/component/loop.ts">
<violation number="1" location="src/component/loop.ts:551">
P2: `handleCancelation` deletes a selected cancellation without rechecking that it still exists. Cleanup can remove it between `getBatch` and `run`, causing the mutation to throw and delaying the whole batch; restore the point-read guard.</violation>
</file>
<file name="src/component/lib.ts">
<violation number="1" location="src/component/lib.ts:225">
P3: This reports legacy jobs that were already running at deployment as `pending` for their entire remaining execution. Preserve the `internalState.running` check when `pendingStartId` is absent, returning `running` for active legacy work.</violation>
</file>
<file name="src/component/danger.ts">
<violation number="1" location="src/component/danger.ts:54">
P2: `clearOldWork` pings the batch worker once for every legacy work row, even though subsequent pings are no-ops while the loop runs. Kick once per cleanup batch to avoid redundant component calls and slow large legacy cleanups.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| const jobs: CompleteJob[] = [...toCancel]; | ||
| await Promise.all( | ||
| canceled.map(async ({ pendingId, workId }) => { | ||
| await ctx.db.delete("pendingCancelation", pendingId); |
There was a problem hiding this comment.
P2: handleCancelation deletes a selected cancellation without rechecking that it still exists. Cleanup can remove it between getBatch and run, causing the mutation to throw and delaying the whole batch; restore the point-read guard.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/component/loop.ts, line 551:
<comment>`handleCancelation` deletes a selected cancellation without rechecking that it still exists. Cleanup can remove it between `getBatch` and `run`, causing the mutation to throw and delaying the whole batch; restore the point-read guard.</comment>
<file context>
@@ -453,43 +544,35 @@ async function handleCancelation(
+ const jobs: CompleteJob[] = [...toCancel];
+ await Promise.all(
+ canceled.map(async ({ pendingId, workId }) => {
+ await ctx.db.delete("pendingCancelation", pendingId);
+ if (canceledWork.has(workId)) {
+ console.error(`[main] ${workId} already canceled`);
</file context>
| await ctx.db.delete("pendingCancelation", pendingId); | |
| const pending = await ctx.db.get("pendingCancelation", pendingId); | |
| if (!pending) return; | |
| await ctx.db.delete("pendingCancelation", pendingId); |
| .unique(); | ||
| if (entry.pendingStartId === undefined) { | ||
| // The upgrade pass removes orphaned legacy queue entries. | ||
| await kickMainLoop(ctx, "kick"); |
There was a problem hiding this comment.
P2: clearOldWork pings the batch worker once for every legacy work row, even though subsequent pings are no-ops while the loop runs. Kick once per cleanup batch to avoid redundant component calls and slow large legacy cleanups.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/component/danger.ts, line 54:
<comment>`clearOldWork` pings the batch worker once for every legacy work row, even though subsequent pings are no-ops while the loop runs. Kick once per cleanup batch to avoid redundant component calls and slow large legacy cleanups.</comment>
<file context>
@@ -60,20 +44,16 @@ export const clearOldWork = internalMutation({
- .unique();
+ if (entry.pendingStartId === undefined) {
+ // The upgrade pass removes orphaned legacy queue entries.
+ await kickMainLoop(ctx, "kick");
+ }
+ const pendingStart = await findPendingStart(ctx, entry);
</file context>
| .withIndex("workId", (q) => q.eq("workId", id)) | ||
| .unique(); | ||
| if (pendingStart) { | ||
| if (work.pendingStartId === undefined) { |
There was a problem hiding this comment.
P3: This reports legacy jobs that were already running at deployment as pending for their entire remaining execution. Preserve the internalState.running check when pendingStartId is absent, returning running for active legacy work.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/component/lib.ts, line 225:
<comment>This reports legacy jobs that were already running at deployment as `pending` for their entire remaining execution. Preserve the `internalState.running` check when `pendingStartId` is absent, returning `running` for active legacy work.</comment>
<file context>
@@ -208,11 +222,13 @@ async function statusHandler(ctx: QueryCtx, { id }: { id: Id<"work"> }) {
- .withIndex("workId", (q) => q.eq("workId", id))
- .unique();
- if (pendingStart) {
+ if (work.pendingStartId === undefined) {
+ // During migration, legacy work without a queue pointer reports pending,
+ // even if already running. This temporary approximation lasts until the
</file context>

Order ready work, completions, and cancellations by commit timestamp. A sweep finds scheduled enqueues that commit behind the incoming cursor, which stays bounded by the transaction snapshot.
Upgrade legacy keys and pointers before scanning, preserving their original time ordering. Requires Convex 1.43.
Validation: 162 tests pass; build, typecheck, lint, and changed-file formatting pass.