Skip to content

Commit 81e6392

Browse files
RaviTejaKNTSclaude
andcommitted
Lock review and release rows in ID order to match claims
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8c2ab44 commit 81e6392

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

‎scripts/self-host-apps/apply-candidate-review.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ export async function applyReviews(client, reviews, dryRun = false) {
385385
if (new Set(ids).size !== ids.length) throw new Error("Repeated candidate_id in review batch.");
386386
await client.query(dryRun ? "begin read only" : "begin");
387387
try {
388+
// Lock in ID order, the same order claims and dedupe use, so batches in any order can't deadlock with them.
389+
if (!dryRun) {
390+
await client.query("select id from public.self_host_app_candidates where id = any($1::uuid[]) order by id for no key update", [ids.filter(Boolean)]);
391+
}
388392
for (const review of reviews) results.push(await applyReview(client, review, dryRun));
389393
await client.query(dryRun ? "rollback" : "commit");
390394
return results;

‎scripts/self-host-apps/release-candidate-batch.mjs‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ async function main() {
2121
await withClient(async (client) => {
2222
const result = await client.query(
2323
`
24-
update public.self_host_app_candidates
24+
with locked as (
25+
select id from public.self_host_app_candidates
26+
where id = any($1::uuid[]) and status = 'enriching'
27+
order by id
28+
for no key update
29+
)
30+
update public.self_host_app_candidates candidate
2531
set status = $2, updated_at = now()
26-
where id = any($1::uuid[])
27-
and status = 'enriching'
28-
returning id
32+
from locked
33+
where candidate.id = locked.id
34+
and candidate.status = 'enriching'
35+
returning candidate.id
2936
`,
3037
[ids, targetStatus],
3138
);

‎tests/self-host-apps/chosen-candidates.test.mjs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ test("review dry run checks all ten reviewed records using only reads", async ()
108108
assert.equal(queries.at(-1), "rollback");
109109
});
110110

111+
test("review apply locks candidates in ID order before its first write", async () => {
112+
const queries = [];
113+
const later = "00000000-0000-4000-8000-000000000002";
114+
const earlier = "00000000-0000-4000-8000-000000000001";
115+
const client = { async query(sql, params) {
116+
queries.push({ sql, params });
117+
if (sql.includes("self_host_app_candidates") && sql.startsWith("select *")) return { rows: [{ id: params[0], status: "enriching" }] };
118+
return { rows: [], rowCount: 1 };
119+
} };
120+
await applyReviews(client, [
121+
{ candidate_id: later, decision: "reject", reason: "test" },
122+
{ candidate_id: earlier, decision: "reject", reason: "test" },
123+
]);
124+
assert.equal(queries[0].sql, "begin");
125+
assert.match(queries[1].sql, /where id = any\(\$1::uuid\[\]\) order by id for no key update$/);
126+
assert.deepEqual(queries[1].params[0], [later, earlier]);
127+
assert.equal(queries.at(-1).sql, "commit");
128+
});
129+
111130
test("review preview rejects unclaimed candidates and rolls back", async () => {
112131
const queries = [];
113132
const client = { async query(sql) {

0 commit comments

Comments
 (0)