Joel Stevick

Ingestion · INGEST 3 · stage 3 of 7

Check the limits

One conditional UPDATE checks the demo account's three upload limits (5 files, 300 pages, 500,000 characters) and takes the room in the same statement, returning the account's new upload count. The upload then continues to chunking, and room.uploads is kept for INGEST 7.

Runs in: Demo Worker, the private Worker behind the DEMO service binding, demo/src/. New to Cloudflare? How these fit together
demo/src/app.ts22 / 22 lines on this page explainedlines 306-327 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.

upload(): INGEST 3

segment · lines 306–327View on GitHub

Checks and takes room in the demo account's limits in one statement.

Calls json() Shared helpers upload(): INGEST 2 Claim the file name

  // INGEST 3 · check the demo's limits (files, pages, characters)  // Every limit in one statement, so two uploads at once cannot both fit in the last of the room.

Every limit in one statement Never runs

WhatThe stage marker, and a comment explaining why the check is a single statement.

WhyA read-then-write check (SELECT the totals, compare them in JavaScript, then UPDATE) would let two concurrent uploads read the same totals, both pass, and together exceed the limit.

  const room = await env.DB.prepare(    `UPDATE demos SET files = files + 1, pages = pages + ?1, chars = chars + ?2, uploads = uploads + 1, used_at = datetime('now'), cleared_at = NULL     WHERE id = ?3 AND files < ?4 AND pages + ?1 <= ?5 AND chars + ?2 <= ?6 RETURNING uploads`,  ).bind(pageCount, chars, demo.id, LIMITS.files, LIMITS.pages, LIMITS.chars).first<{ uploads: number }>()

The conditional UPDATE Ingestion

WhatOne UPDATE of the demo account's row whose WHERE clause holds the three limits and whose SET adds this file to the totals, ending in RETURNING uploads. Numbered parameters let pageCount (?1) and chars (?2) appear in both the SET and the WHERE clause.

WhySQLite runs one UPDATE atomically. The WHERE clause is evaluated against the row's current totals and the increments are applied in the same step, so of two uploads competing for the last of the room, only one matches. RETURNING uploads gives the count after this upload. INGEST 7 compares it with 1, so ordinarily only the account's first stored upload sends the email; a failed store at INGEST 5 takes the count back down. Setting used_at restarts the 10-day idle clock. The daily purge re-checks that clock inside its own delete transaction, so an upload that lands during a purge keeps all the account's files and chunks; vectors the purge had already deleted are gone, and those chunks are found by BM25 alone. Setting cleared_at to NULL removes the page's "index was deleted" notice once new documents arrive.

LIMITS.files = 5 (files < 5 before this one)LIMITS.pages = 300LIMITS.chars = 500_000IDLE_DAYS = 10

  if (room === null) {    // No room: the file row is deleted, then the demo is re-read to say which limit this file would pass.    await forget()    const now = (await env.DB.prepare('SELECT files, pages, chars FROM demos WHERE id = ?').bind(demo.id).first<Demo>()) ?? demo

No room: release the name, re-read the totals Ingestion

WhatWhen the UPDATE returns no row, forget() deletes the file row from INGEST 2. A SELECT then reads the account's current totals, falling back to the row the router read.

WhyThe UPDATE changed none of the account's totals, so the file row is the only thing to undo. The totals are re-read because the router's copy may be stale, and they are used only to word the message.

    // The message names the first limit this file would pass.    const limit = now.files >= LIMITS.files      ? `The demo takes at most ${LIMITS.files} PDFs.`      : now.pages + pageCount > LIMITS.pages        ? `The demo takes at most ${LIMITS.pages} pages in all, and ${name} would take it to ${now.pages + pageCount}.`        : `The demo takes at most ${LIMITS.chars / 1000} KB of text in all, and ${name} would take it past that.`

Name the first limit passed Ingestion

WhatFiles are checked first (already 5), then pages (the current pages plus pageCount over 300, with the total the file would reach). The character limit is the remaining case, given as LIMITS.chars / 1000 KB.

WhyThe message tells the visitor which limit to work around. The last branch is a default with no check of its own, so if a concurrent removal freed room between the UPDATE and the re-read, the message names the character limit although no limit is now passed. "500 KB" names the 500,000 limit, which counts JavaScript string length (UTF-16 code units), not bytes.

LIMITS.chars / 1000 = 500 ("KB")

    return json(413, { error: `${limit} ${name} was not added.` })  }

413: name the limit, add nothing Ingestion

WhatAnswers 413 with the limit's sentence followed by "<name> was not added."

Why413 (Content Too Large) marks the refusal as one of size. The upload leaves no file row, no chunks and no change to the account's totals.