testRequests.ts
@file Every statement this Worker runs against test_requests and…
Overview
@file Every statement this Worker runs against test_requests and
test_request_candidates. Same rule as rigs.ts: no SQL built by
concatenation, every value a bound parameter.
used by test-request.ts test-result.ts scheduled.ts
API
Fexport async function createTestRequest(db: D1Database | undefined, req: NewTestRequest): Promise<void>
Creates the request row and every candidate row in one D1 batch, so a
request never exists with a partial candidate list. pinged_awake is
seeded from awake itself: an awake candidate is pinged the moment the
request is posted, so there is nothing left to mark after the fact.
handler · calls need, runFexport async function claim(db: D1Database | undefined, requestId: string, userId: string): Promise<boolean>
Atomic first-accept-wins: a single UPDATE with the guard in its own WHERE clause rather than a read-then-write, so two simultaneous Accept clicks cannot both succeed (same pattern as cooldown.ts's checkMatrixCooldown). Returns whether this call was the one that claimed it.
componentHandler · calls need, runFexport async function getRequestByThreadId(db: D1Database | undefined, threadId: string): Promise<TestRequestRow | null>
/test-result runs inside the claim thread, not the queue channel, so it
has to work backwards from "which request does this thread belong to".
handler · calls need, runFexport async function markDone(db: D1Database | undefined, requestId: string): Promise<boolean>
Atomic claimed->done guard, same first-writer-wins shape as claim() —
a double /test-result submission (a retry, a duplicate interaction
delivery) writes at most one validation row per call site that checks
this return value.
handler · calls need, runFexport async function pendingUnescalated(db: D1Database | undefined, cutoffMs: number): Promise<TestRequestRow[]>
Requests that have sat pending, unescalated, since before cutoffMs —
the scheduled sweep's candidate set for waking the asleep half of the
candidate list.
runEscalationSweep · calls need, runFexport async function markEscalated(db: D1Database | undefined, requestId: string, now: number, pingedUserIds: readonly string[]): Promise<void>
Marks the request escalated and every named candidate as pinged, in one batch — the sweep must not re-ping the same asleep candidates on its next tick just because one write succeeded and the other did not.
runEscalationSweep · calls need, runUndocumented (6)
RoutingUnavailable.constructor, need, run, setThread, getRequest, asleepUnpingedCandidates