Joel Stevick

The ask · ASK 8 · stage 7 of 7

Answer with sources

ASK 8 builds the sources from the chunks the released units cited, in order of first citation, and each source carries the stored passage text, never the model's words. It records the answer in the questions table as one text, then yields done, with the whole answer, its sources and its timings, followed by the recorded sentinel. asText() produces that one-text form, and the MCP tool ask_documents and the canary call it too.

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

answering(): ASK 8

segment · lines 837–855View on GitHub

Builds the sources, records the answer and yields done.

Calls asText() answering(): the generator's contract, timer and record() Worker router

  // ASK 8 · the answer with its sources, in order of first citation  // The passage is the stored text it was given, read by its id: never the model's words.

The stage, and its rule Never runs

WhatThe comment names ASK 8, its order (first citation) and its rule. A source's passage is the text of the chunk object that the check matched by id at ASK 6–7, never the model's words. The comment's "read by its id" is loose wording, since nothing is read at this point; chunk.text is already on the matched object.

WhyA citation shows the visitor what the answer rests on. If the passage were the model's own quotation, it could be paraphrased or invented. It is the text the model was given, built from stored chunks (several joined, for a widened passage), under an id the check matched to a given chunk. The check also found every figure in the sentence in its cited passages or in the question.

An answer card: the question "How long must the dome be open before the first exposure, and why?", three sentences of answer, and under them the source line "observatory-handbook.pdf · page 2" with a "Show the passage" button.
A cited answer from the sample PDF. Three checked sentences come first. Under them the page draws the sources from the done event, one line per file and page, each with a button that shows the cited passage. 2026-11-29, the live demo at joelstevick.com/demo in Chrome. The live page is newer than the pinned commit, so its file selector, trash icons and indexing note are not in the code shown here.
  const cited = [...new Set(shown.flatMap((unit) => unit.cited))]

Unique cited chunks, in order Query time

WhatflatMap concatenates each shown unit's cited chunks in release order, and a Set removes repeats, keeping each chunk's first position.

WhyA Set keeps insertion order, so the first source is the first chunk the first released unit cited. Deduplication is by object identity. Every cited chunk is an object from the given passages, because checkUnits matches ids against them, so a chunk cited by three sentences is one object and one source. A held unit released together with the next unit shares that unit's cited chunks.

  const sources = cited.map((chunk) => ({    id: chunk.id, file: chunk.file, page: chunk.page, ...(chunk.lastPage !== undefined && chunk.lastPage > chunk.page ? { last_page: chunk.lastPage } : {}), passage: chunk.text,  }))

One source per cited passage Query time

WhatEach source carries the chunk's id, file and page, its text as passage, and last_page when the passage ends on a later page.

WhyWhen widening (ASK 5) joined a chunk with its neighbours, the given passage is one object with the joined text and the page span. The source therefore shows the whole passage the model read, with its page range. last_page is left out when the passage lies on one page. The demo page groups the sources by file and page, and links each to the PDF at that page when the file is open in the visitor's browser.

  // `items` goes to the page only when the answer is a list, so a prose answer's body is as before.  // The recorded answer is one text either way (`asText`), since /admin and the golden-set  // export read the `questions.answer` column as text.  const prose = shown.filter((unit) => unit.kind !== 'item').map((unit) => unit.text).join(' ')  const items = shown.filter((unit) => unit.kind === 'item').map((unit) => unit.text)

Prose and items Query time

Whatprose joins the text of every lead-in and sentence unit with a space. items is the text of every item unit, in order.

WhyA list answer is a lead-in plus items, and the demo page draws the items as bullets. A prose answer has no items, so its body has the same shape it had before lists existed.

  const ok = await record({ answer: asText(prose, items), sources, checked: true, fallback: failed === null ? null : 'stopped', first_ms: firstMs })

Record the answer Query time

Whatrecord() inserts the question's row with the answer as one text (asText), the sources as JSON with their passages, checked = 1, fallback 'stopped' when a later unit failed and null otherwise, ms (the time so far), first_ms, keep (1 when the request carries consent) and via.

WhyOnly checked rows come back as history when the demo page reloads. They bring these stored passages with them, so "Show the passage" never needs the chunks again. A row kept with consent (keep = 1) survives the idle purge, which deletes only questions asked without consent (keep = 0). A stopped answer counts as checked, because every sentence shown in it passed.

  // The ask ends with the whole answer, then whether its row was written.  yield { type: 'done', answer: prose, ...(items.length > 0 ? { items } : {}), sources, found: true, left, first_ms: firstMs, ms: Date.now() - started }  yield { type: 'recorded', ok }}

done, then recorded Query time

WhatYields done, with items only when the answer is a list and ms as the total time, then the recorded sentinel.

Whydone is the last AskEvent of an answer that was shown. On the demo page it draws the sources and the number of questions left. In the JSON rendering it becomes the response body. The insert runs before done is yielded, so when a consumer sees done the row has been written or the write has failed, and recorded reports which.

asText()

function · lines 856–862View on GitHub

An answer as one text: the prose, then one "- " line per item.

Called by answering(): ASK 8 build(): TOOL 2 ask_documents runCanary() The canary

/** * An answer as one text, for the `questions` table: the prose, or the list's lead-in followed by * one "- " line per item, as a reader of /admin or of a golden-set export would write it. */

The stored form of an answer Never runs

WhatThe doc comment says what the questions table keeps: the prose, or a list's lead-in followed by one "- " line per item.

Why/admin and the golden-set export read questions.answer as plain text. fromText() in app.ts is the inverse of asText(), and rebuilds the demo page's history from the stored text.

export function asText(answer: string, items: string[]): string {  return [answer.trim(), ...items.map((item) => `- ${item.trim()}`)].filter((line) => line.length > 0).join('\n')}

Join with newlines Query time

WhatThe trimmed prose comes first, then one "- " line per trimmed item. An empty prose line is dropped, and the lines are joined with \n.

WhyAn empty lead-in leaves no blank first line. An item line always holds at least "- ", so the filter never drops an item. The MCP tool ask_documents applies asText() to the done event it reads from the stream, and the canary applies it to its JSON body, so the recorded answer, the MCP tool and the canary all turn an answer into the same text.