Joel Stevick

Ingestion · INGEST 2 · stage 2 of 7

Claim the file name

upload() inserts the file's row before it writes anything else; the row's key is (demo_id, name), so the insert claims the name in the demo account. The stage also defines forget, the DELETE that releases the claim.

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

segment · lines 292–305View on GitHub

Inserts the file row as a claim on the name, and defines how to release it.

Called by upload(): INGEST 3 Check the limitsCalls json() Shared helpers

  // INGEST 2 · claim the file name (one row per file)  // The file's row first: its primary key is what stops two uploads of one name at once.

The row first Never runs

WhatThe stage marker, and a comment explaining why the file row is written first.

WhyThe claim comes before the limit check (INGEST 3) and before any chunk is written, so a duplicate is refused before it takes any room or stores any text.

  try {    await env.DB.prepare('INSERT INTO files (demo_id, name, pages, chars, skipped) VALUES (?, ?, ?, ?, ?)')      .bind(demo.id, name, pageCount, chars, JSON.stringify(skipped)).run()

INSERT INTO files: claim the name Ingestion

WhatOne INSERT INTO files, bound to the demo account's id, the name, pageCount, chars and skipped as a JSON array.

WhyA single INSERT against PRIMARY KEY (demo_id, name) is atomic, so of two concurrent uploads of x.pdf exactly one inserts. A SELECT-then-INSERT check would let both see no row and both go on to store chunks. The row records pages and chars so that a later removal gives back exactly that much room.

table: filesPRIMARY KEY (demo_id, name)skipped: JSON array of page numbers (JSON.stringify(skipped))

  } catch (error) {    // A second upload of the same name hits the primary key and is refused; any other error is a real failure.    if (String(error).includes('UNIQUE')) return json(409, { error: `${name} is already uploaded.` })    throw error  }

Duplicate name: 409 Ingestion

WhatWhen the error's text contains UNIQUE (SQLite's "UNIQUE constraint failed"), the upload is answered 409 "<name> is already uploaded." Any other error is rethrown.

WhyA visitor who wants to replace a file removes it first; a silent overwrite would leave the old file's chunks and vectors behind. On the upload route, a rethrown error reaches createApp's fetch, which answers 500 "Something went wrong. Please try again in a minute." Nothing has been written at that point.

  // Undoes INGEST 2, for when INGEST 3 finds no room.  const forget = () => env.DB.prepare('DELETE FROM files WHERE demo_id = ? AND name = ?').bind(demo.id, name).run()

forget: release the claim Ingestion

Whatforget is a closure that deletes this file's row. It is defined here, and INGEST 3 calls it when the file does not fit.

WhyThe claim holds until forget runs. In that window a concurrent upload of the same name is answered 409, even if this upload is then refused for lack of room.