Joel Stevick

The ask · ASK 2 · stage 2 of 7

Reserve a question

Before any model is called, the ask reserves one of the ten questions of the visitor's demo account (their row in the demos table) with a single conditional UPDATE in D1. The new count gives the number of questions left, and the ask continues to retrieval.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/app.ts6 / 6 lines on this page explainedlines 552-557 of 1124; the rest are on other stage pages
In the browser
runs in the visitor's browser, on the demo page
Ingestion
runs when a PDF's text is uploaded
Query time
runs when the Worker answers a question or a search_documents call from the MCP server, and in the request handling every route shares, in the Worker and in the site's functions, which uploads and removals pass through too
MCP request
runs on a request to the MCP server or its OAuth endpoints
Site request
runs on a site request that is not a question, an upload, an MCP call or the cron: a page load, a session read, the demo page's /api/grants calls, an admin read, a file's removal
Scheduled
runs in the Worker's daily cron job
Eval run
runs when the eval harness runs on a developer's machine
MCP host
runs in an MCP host outside the Worker: the search loop example, on a developer's machine (npm run search) or in its test
Module load
runs once, when the module is loaded
Type only
erased by the TypeScript compiler; never runs
Never runs
a comment, a licence or markup with no behaviour of its own

Pinned to joelstevick-demo-rag-pipeline at 6a9c6c7.

demo/src/app.ts The demo Worker.

ask(): ASK 2

segment · lines 552–557View on GitHub

Reserves one question atomically.

Calls json() Shared helpers

  // ASK 2 · reserve one of the demo's ten questions  // The reservation is the limit: one statement, so two asks for the last question cannot both pass.  const reserved = await env.DB.prepare("UPDATE demos SET asks_used = asks_used + 1, used_at = datetime('now') WHERE id = ? AND asks_used < ? RETURNING asks_used")    .bind(demo.id, LIMITS.asks).first<{ asks_used: number }>()

Check and increment in one statement Query time

WhatOne D1 prepared statement adds 1 to asks_used and sets used_at, only where the demo account's row still has asks_used below LIMITS.asks (10). RETURNING gives back the new asks_used, and .first() returns that row, or null when the WHERE matched nothing.

WhyA SELECT followed by an UPDATE would let two asks both read 9 and both pass. One statement leaves no gap between the check and the increment. D1 runs it atomically, so only one of two concurrent asks for the last question matches asks_used < 10. RETURNING returns the new count without a second query. used_at records that the account is in use, and the idle purge (IDLE_DAYS = 10) reads it. The reservation comes before any model call. It is undone only if the answer model fails before anything is shown (app.ts 805), and a fallback still counts as a question.

LIMITS.asks = 10table: demos

  if (reserved === null) return json(429, { error: "You've used your ten questions.", left: 0 })

No question left: 429 Query time

WhatWhen no row came back, the ask answers 429 with the error and left: 0 in JSON.

WhyThe page checks for 429 Too Many Requests and, when it sees one, replaces the form with the offer instead of showing an error line. When a row came back, the ask continues. app.ts 566 passes LIMITS.asks − reserved.asks_used to answering() as left, the count after this question. The done and fallback events carry that count, and the error event, which gives the question back, carries left + 1.

status 429left: 0