WRITE · TRACE · UNDERSTAND

Verify citation spans against versioned source digests

Sources have unique id,tenant,version,text,sha256 (UTF-8 digest).

Read lesson: Verify citation spans against versioned source digests →

Your experiment

Sources have unique id,tenant,version,text,sha256 (UTF-8 digest). Each claim has text and citations containing source,start,end,quote. For each claim return ok and sorted unique errors. No citations gives missing. For each citation check known ID, exact tenant/version, recomputed source digest, integer code-point range 0<=start<end<=len(text), and exact quoted substring, in that order. Errors are id:unknown,scope,digest,range,quote. Duplicate source IDs raise ValueError. This validates provenance structure and literal spans, never semantic entailment.

Python runs in your browser. Your draft stays on this device.

What to look for

Review source approval, version changes, Unicode offset conventions and claim support. Literal span matching is not an answer-faithfulness classifier.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Verify citation spans against versioned source digests works

The idea

A citation is only useful if a reader can open the source and find the quoted words. verify_provenance checks each citation in layers, from basic to specific: the source id is known, it belongs to the caller's tenant and the expected version, its text still hashes to the recorded SHA-256 digest, the start and end offsets form a valid range, and the characters in that range equal the quote exactly.

The order matters because each layer only makes sense once the earlier ones hold. Comparing a quote against text that has changed since it was indexed proves nothing. The check is also literal. It shows that the quoted span exists in that version of that source, and it says nothing about whether the span supports the claim.

How it works

  1. Raise ValueError if two sources share an id, then index the sources by id.
  2. For each claim, record missing when it has no citations.
  3. For each citation, record the source id plus :unknown if it is not indexed, or :scope if its tenant or version differs from the caller's.
  4. Recompute the SHA-256 of the source text encoded as UTF-8 and record :digest if it differs from the stored sha256.
  5. Record :range unless start and end are integers with 0 <= start < end <= len(text), otherwise :quote if the slice differs from quote.
  6. Report each claim as ok or not, with its errors sorted and deduplicated.

What it costs

Time
O(citations · text size)
Space
O(sources + errors)

This answer rehashes the whole source text for every citation that reaches the digest step, so caching one digest per source would save most of the work.

Where it is used

  • npm records an integrity hash for every package in package-lock.json and verifies downloaded packages against it.
  • The W3C Web Annotation Data Model identifies a passage with a TextQuoteSelector for its exact text and a TextPositionSelector for its character offsets.
  • Wikipedia's permanent links carry an oldid revision number, so a reference can point at one fixed version of an article.

Watch out for

  • Trusting the stored sha256 without recomputing it from the text, which lets edited content pass under an old digest.
  • Testing the quote with quote in text instead of the slice at start:end: the offsets could then point somewhere else entirely.
  • Measuring offsets in UTF-8 bytes: the Lab uses Python string positions, which count code points, and the two differ for non-ASCII text.