# NORMALISATION-v1

Normalisation version 1 of this record: every rule, in the order it runs, by
which the bytes a publisher served become the text whose SHA-256 is
written as an observation's `content_hash`, with test vectors. Two rule sets
produce such a digest, and this version states both: the **document** rule
set, which reduces a page or a PDF file, and the **register** rule set, which
reduces a public register published as HTML. This version holds
the rules in force from the first commit of the document collector.

The rules are stated here in prose, and their machine-read parts in the fenced
`json` blocks: the identity, the rule values, the register substitutions and
the vectors. Only the blocks are checked against code. A plant in
`make gx-test` (`scripts/plants/b/test_normalisation_doc.py`) runs the code held in
git at commit `db6add9d4` over every vector of this document, and requires every
text and every digest to be reproduced and the rule values stated here to be
the values that code holds.

## Identity

```json
{
 "block": "identity",
 "normalisation_version": 1,
 "in_force_from": {
  "commit": "db6add9d4",
  "date": "2026-07-28"
 },
 "register_from": {
  "commit": "e25df1609",
  "date": "2026-07-28"
 },
 "profiles": null,
 "extractor_version": null
}
```

## How the number 1 is derived

A version is one rule era in git. The commits that touch `scripts/observe.py`,
`scripts/observe_api.py` or `docs/referents/chrome-exclusions.json` are read
oldest first, and an era begins at the first commit holding the document rule
set and at each later commit where any of three things differs from the commit
before it:

- the code of the document rule set's functions (`looks_like_pdf`,
  `extract_pdf_text`, `normalise`, `digest`, `apply_chrome_exclusions`,
  `_chrome_rules`, and `_observed_text`, which fixes the order they run in),
  with comments and docstrings not read;
- its rule values (the volatile patterns, the tag and whitespace rules, the
  PDF signature, the window it is sought in and the read cap) or its host
  exclusion rules;
- the substitutions of a register rule set that already existed.

`normalisation.rule_eras()` derives the eras, and the plant requires one
document for each, numbered in that order. Four eras are derived:

| Version | First commit | Its date | What differs from the version before |
|---|---|---|---|
| v1 | `db6add9d4` | 2026-07-28 | the first commit holding the document rule set |
| v2 | `9bf3274` | 2026-09-07 | the text of a PDF file is extracted and reduced |
| v3 | `263cd57b0` | 2026-09-07 | a new tag rule, and the first host exclusions (one host) |
| v4 | `4472b7800` | 2026-09-14 | a host exclusion for a second host |

A commit's date in this repository names the day it landed and no time of day,
so v2 and v3 began on the same day. The register rule set first appears at
commit `e25df1609`, dated 2026-07-28, inside v1, and begins no version: no
register digest was taken under any earlier rule. Its four substitutions have
not changed since, and every version states them.

The profile stamp, `normalization_profile`, was written into the collectors by
commit `cfd970684`, dated 2026-10-04, while v4 was in force, and a line first
carries it on 2026-10-05. A version is therefore dated by the commit at which
its rules first ran, never by the day a stamp first named them.

## Lines reduced under this version

No line written under v1 states the rules that reduced it: v1 lines carry no
`normalization_profile`, no `extractor_version` and no `normalisation_version`.
A line dated from 2026-07-28 to 2026-09-06 was written while v1 was the rule
set committed on master. A line dated 2026-09-07 may have been reduced under v1, v2 or v3,
and the line does not say which. The store is append-only, so nothing states
it on those lines afterwards.

## Where the rules ran

The document rule set is read here from `scripts/observe.py` as it stood at
commit `db6add9d4`: its reduction functions, its rule values, and the exclusions
file of the same commit. The document collector of that time, `observe_one`,
hashed the text they returned. The register rule set is the four substitutions
of `observe_api.poll_html_register`, as committed at `e25df1609`.

## The document rule set, for a markup body

0. **Decode** (in `observe.fetch`, before any rule here). The body's bytes are
   decoded with the charset of the response's `Content-Type`, or UTF-8 where it
   names none, and bytes that do not decode are dropped. The vectors below start
   from the decoded text.
1. **Volatile content** (`_VOLATILE`). The fourteen patterns listed under
   `volatile` in the rule values, in that order, each match replaced by one
   space. They run over the markup, before any tag is removed, and are the same
   fourteen in every version.
2. **Tags** (`_TAG`, `<[^>]+>`). From a `<` to the next `>`, whatever lies
   between, is replaced by one space: a `>` inside a quoted attribute value ends
   the tag (D11), and a `<` in prose begins one that runs to the next `>` (D12).
3. **Character references** (`html.unescape`). Named and numeric references are
   replaced by the characters they name, after tags are removed.
4. **Whitespace** (`_WS`). Every run of whitespace, Unicode whitespace
   included, becomes one space, and the text is trimmed at both ends.
5. **Digest** (`digest`). SHA-256 over the text's UTF-8 bytes, written as 64
   lower-case hexadecimal characters.

No host exclusion exists in this version (D15, D16).

## The document rule set, for a PDF body

None. In this version a body beginning `%PDF-` is decoded as step 0 decodes
any body and reduced by the markup steps above, as vector P0 states: no text is
extracted from the file.

## When a digest is taken

The classification that decides whether a reduction's text is given a digest is
not part of the reduction and is not identified by any version. It is stated in
NORMALISATION-v4 for the code of v4, and is not restated for this one.

## Document rule values

The rule values the code of this version holds, in the shape
`observe.normalization_rule_values()` returns them now: the patterns of
`_VOLATILE` with their replacements, the tag and whitespace rules, the PDF
signature and the window it is sought in (null where the version has none),
the host exclusion rules as `_chrome_rules` reads them (empty where the version
has none), and, under `missing`, the reduction functions the version does not
define. Each pattern is written with its flags as Python's `re` module stores
them: 32 is Unicode matching, 34 adds case-insensitive matching, 48 adds
dot-matches-newline, and 50 adds both.

```json
{
 "block": "document-rule-values",
 "values": {
  "volatile": [
   [
    "<script.*?</script>",
    50,
    " "
   ],
   [
    "<style.*?</style>",
    50,
    " "
   ],
   [
    "<noscript.*?</noscript>",
    50,
    " "
   ],
   [
    "<!--.*?-->",
    48,
    " "
   ],
   [
    "<(nav|header|footer|aside)\\b.*?</\\1>",
    50,
    " "
   ],
   [
    "\\b(?:csrf|nonce|session|token|sid|jsessionid|viewstate)[-_a-z]*[\"\\s:=]+[A-Za-z0-9_\\-+/=]{8,}",
    34,
    " "
   ],
   [
    "\\?(?:v|ver|version|cb|t|ts|rev|_)=[A-Za-z0-9._-]+",
    34,
    " "
   ],
   [
    "\\b[a-f0-9]{32,64}\\b",
    32,
    " "
   ],
   [
    "\\b(?:ga|gtm|utm_[a-z]+|_gid|_ga|fbclid|gclid)\\b[^\\s\\\"']*",
    34,
    " "
   ],
   [
    "\\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\\b",
    34,
    " "
   ],
   [
    "(?:printed|generated|retrieved|accessed|downloaded|as of)\\s+(?:on\\s+)?\\d{1,2}[ /-][A-Za-z0-9]{1,9}[ /-]\\d{2,4}",
    34,
    " "
   ],
   [
    "\\b\\d{1,2}:\\d{2}(?::\\d{2})?\\s*(?:AM|PM|UTC|GMT|CET|CEST)?\\b",
    34,
    " "
   ],
   [
    "\\b\\d[\\d,]*\\s+(?:views?|visitors?|downloads?|hits)\\b",
    34,
    " "
   ],
   [
    "\\b\\d+\\s+(?:seconds?|minutes?|hours?|days?)\\s+ago\\b",
    34,
    " "
   ]
  ],
  "tag": [
   "<[^>]+>",
   32
  ],
  "whitespace": [
   "\\s+",
   32
  ],
  "pdf_signature": null,
  "pdf_signature_window": null,
  "exclusions": [],
  "missing": [
   "looks_like_pdf",
   "extract_pdf_text",
   "apply_chrome_exclusions",
   "_chrome_rules"
  ]
 }
}
```

## The register rule set

`observe_api.register_normalise`, over a body read as UTF-8 with undecodable
bytes dropped (at most 3,000,000 bytes are read). Four substitutions, in order,
each match replaced by one space, then the text is trimmed; the digest is
SHA-256 over the text's UTF-8 bytes. A register reading below 400 characters is
recorded as thin and carries no digest.

This rule set differs from the document rule set, and vectors R2 and R3 state
two of the differences: a tag ends at its first `>`, quoted or not, and no
character reference is unescaped.

```json
{
 "block": "register-substitutions",
 "then": "trim both ends",
 "substitutions": [
  {
   "pattern": "<script.*?</script>|<style.*?</style>|<!--.*?-->",
   "flags": "re.S | re.I",
   "replacement": " "
  },
  {
   "pattern": "<[^>]+>",
   "flags": "",
   "replacement": " "
  },
  {
   "pattern": "[?&](?:t|ts|_|v|cb|sid|jsessionid)=[A-Za-z0-9._-]+",
   "flags": "",
   "replacement": " "
  },
  {
   "pattern": "\\s+",
   "flags": "",
   "replacement": " "
  }
 ]
}
```

## Vectors

Each input is hand-written for this document and is not taken from any
publisher's page. `text` is the text the rule set yields and `sha256` is the
SHA-256 of that text's UTF-8 bytes. A document vector names the address it is
read as answered from; no rule of this version reads it.

```json
{
 "block": "vectors",
 "family": "document",
 "vectors": [
  {
   "id": "D0",
   "what": "an empty body",
   "url": "https://publisher.example/document",
   "input": "",
   "text": "",
   "sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  },
  {
   "id": "D1",
   "what": "script, style, noscript and comment removed",
   "url": "https://publisher.example/document",
   "input": "<p>Annex 1</p><script>var t=1;</script><style>p{}</style><noscript>x</noscript><!-- c --><p>Scope</p>",
   "text": "Annex 1 Scope",
   "sha256": "e57962e28a9df11d6cbdba23bac24ea1153a2d2f44ac85ae5d1d0b7fa3b5c903"
  },
  {
   "id": "D2",
   "what": "nav, header, footer and aside removed",
   "url": "https://publisher.example/document",
   "input": "<header>Menu</header><nav>Home</nav><p>Section 4</p><aside>Note</aside><footer>Contact</footer>",
   "text": "Section 4",
   "sha256": "6752a6387220ae2cc874a52b1e21c448787da63f4a78b0b927ffbcb79a952405"
  },
  {
   "id": "D3",
   "what": "a session token removed",
   "url": "https://publisher.example/document",
   "input": "<p>session: Ab12Cd34Ef56</p><p>Body</p>",
   "text": "Body",
   "sha256": "6ccaa6415b5ee449e3c5c716f57b4608ebe3b780f5aa6b077bc5d8eee5d961f9"
  },
  {
   "id": "D4",
   "what": "a cache buster removed",
   "url": "https://publisher.example/document",
   "input": "<p>Guide?v=20260101 text</p>",
   "text": "Guide text",
   "sha256": "d3ebfa5ad2fc97fee4fbffadafb4c7ca286df68a62424245cbc7ca46271a877d"
  },
  {
   "id": "D5",
   "what": "a bare digest of 32 hex characters removed",
   "url": "https://publisher.example/document",
   "input": "<p>File 0123456789abcdef0123456789abcdef final</p>",
   "text": "File final",
   "sha256": "1f269c680c7a833643c5de22092e02f711eaf71b226ef89127069c1b4f7a685f"
  },
  {
   "id": "D6",
   "what": "an analytics parameter removed",
   "url": "https://publisher.example/document",
   "input": "<p>ref utm_source=feed end</p>",
   "text": "ref end",
   "sha256": "8b691a8ceb0dc3aa9a5a7f9f730885764d76b71933f9c9d8cb21612912d73a72"
  },
  {
   "id": "D7",
   "what": "a UUID removed",
   "url": "https://publisher.example/document",
   "input": "<p>id 123e4567-e89b-12d3-a456-426614174000 x</p>",
   "text": "id x",
   "sha256": "3cd36c717c9083bec0575e9ff5c07c51e950e1e01328627e5b1bef202df9ff32"
  },
  {
   "id": "D8",
   "what": "a printed-on date removed",
   "url": "https://publisher.example/document",
   "input": "<p>Rev 3 printed on 3 May 2026 end</p>",
   "text": "Rev 3 end",
   "sha256": "966353381c7ebc5d183620a9344223eeb27a596095619b30a57af7ddb1192148"
  },
  {
   "id": "D9",
   "what": "a clock removed",
   "url": "https://publisher.example/document",
   "input": "<p>Updated 00:00 UTC daily</p>",
   "text": "Updated daily",
   "sha256": "a6aa90be2003df17c48f6e2adb5e7140036eec0a479fcb9c7bfe25f88bab9e5e"
  },
  {
   "id": "D10",
   "what": "a counter and a relative time removed",
   "url": "https://publisher.example/document",
   "input": "<p>1,204 views and 3 days ago</p>",
   "text": "and",
   "sha256": "6201111b83a0cb5b0922cb37cc442b9a40e24e3b1ce100a4bb204f4c63fd2ac0"
  },
  {
   "id": "D11",
   "what": "a tag ends at its first greater-than sign, quoted or not",
   "url": "https://publisher.example/document",
   "input": "<button data-action=\"click->search#go\">Find</button><p>Part 11</p>",
   "text": "search#go\">Find Part 11",
   "sha256": "b43854c8af06d085c5787431174e461f493483fa9caa57371627e0753649d596"
  },
  {
   "id": "D12",
   "what": "a less-than sign in prose begins a tag that runs to the next greater-than sign",
   "url": "https://publisher.example/document",
   "input": "<p>if a < b then</p>",
   "text": "if a",
   "sha256": "4585e38c27d8bf9d7e69e1f8baa322ea641b2ad899f8337eabb3c3a2efb22971"
  },
  {
   "id": "D13",
   "what": "character references unescaped after tags are removed",
   "url": "https://publisher.example/document",
   "input": "<p>R&amp;D &lt;draft&gt; &#8211; v2</p>",
   "text": "R&D <draft> – v2",
   "sha256": "14351575a76d2145798ebaa35462d2910c879bd985a5b7c4a759e5b0ccb2b949"
  },
  {
   "id": "D14",
   "what": "whitespace collapsed and trimmed",
   "url": "https://publisher.example/document",
   "input": "<p>  Line\n\tone   two </p>",
   "text": "Line one two",
   "sha256": "0e1dccf1fa5c1cae95dae8eaba7915678711564d19299496eb24c5cfb7eefa53"
  },
  {
   "id": "D15",
   "what": "no host exclusion is applied",
   "url": "https://www.ecfr.gov/current/title-21/part-11",
   "input": "<p>Title 21 was last amended 1/02/2026 Part 11 text</p>",
   "text": "Title 21 was last amended 1/02/2026 Part 11 text",
   "sha256": "3c9c095e09cdf9ce820b7f38df194df36c9b56e310560d50a59dcd761a4f3f34"
  },
  {
   "id": "D16",
   "what": "no host exclusion is applied, and markup kept the volatile rule from reaching the date",
   "url": "https://ecfr.gov/current/title-21",
   "input": "<p>up to date as of <b>1/02/2026</b> Body</p>",
   "text": "up to date as of 1/02/2026 Body",
   "sha256": "f7da77d9370e1564008e348764b8534b7a6ce307e21f34ff062dad61c1edbb97"
  },
  {
   "id": "D17",
   "what": "a host exclusion not applied on another host",
   "url": "https://publisher.example/document",
   "input": "<p>Title 21 was last amended 1/02/2026 Part 11 text</p>",
   "text": "Title 21 was last amended 1/02/2026 Part 11 text",
   "sha256": "3c9c095e09cdf9ce820b7f38df194df36c9b56e310560d50a59dcd761a4f3f34"
  },
  {
   "id": "D18",
   "what": "the volatile token rule removes part of a meta tag and the tag rule removes the rest",
   "url": "https://www.gov.uk/guidance/x",
   "input": "<meta name=\"csp-nonce\" content=\"QUJDREVGR0hJSktMTU5PUA==\" /><p>Guidance</p>",
   "text": "Guidance",
   "sha256": "10af9928ba801e7d223b816e783edf7c6d77d3bf5390ae16616eb6a5a1a195a8"
  },
  {
   "id": "D19",
   "what": "the volatile rule removes an as-of date before the host exclusion is applied",
   "url": "https://www.ecfr.gov/current/title-21",
   "input": "<p>up to date as of 1/02/2026 Body</p>",
   "text": "up to date Body",
   "sha256": "8c1adc4ec90aff99643f5116ee10f8ccfd4968defea453fc3e77c7240d44fb40"
  },
  {
   "id": "D20",
   "what": "a no-break space from a character reference collapsed with the other whitespace",
   "url": "https://publisher.example/document",
   "input": "<p>a&nbsp;&nbsp;b</p>",
   "text": "a b",
   "sha256": "c8687a08aa5d6ed2044328fa6a697ab8e96dc34291e8c2034ae8c38e6fcc6d65"
  },
  {
   "id": "D21",
   "what": "a meta tag written as character references is unescaped and kept",
   "url": "https://www.gov.uk/guidance/x",
   "input": "<p>&lt;meta name=&quot;csp-nonce&quot;QUJDREVGR0hJSktMTU5PUA==&quot; /&gt; Guidance</p>",
   "text": "<meta name=\"csp-nonce\"QUJDREVGR0hJSktMTU5PUA==\" /> Guidance",
   "sha256": "a2b33e30c2eedde1c0c106abfa3ab8d41826daef0d8c1c355ba6ca6d1cf94314"
  },
  {
   "id": "P0",
   "what": "a body beginning %PDF- is decoded and reduced as markup text",
   "url": "https://publisher.example/document",
   "input": "%PDF-1.4 (Scope) Tj",
   "text": "%PDF-1.4 (Scope) Tj",
   "sha256": "4ea8eb8c790c109efcddb93c0937fc87d98d5e28dcbe7e0e7f22b6adbb71211f"
  }
 ]
}
```

```json
{
 "block": "vectors",
 "family": "register",
 "vectors": [
  {
   "id": "R1",
   "what": "script, style and comment removed",
   "input": "<p>EudraGMDP</p><script>x</script><!-- c --><style>s</style><p>List</p>",
   "text": "EudraGMDP List",
   "sha256": "c9ce904afc5d175713fd3cf4cf5442ea097a31def0660b77efcc7b4744278abc"
  },
  {
   "id": "R2",
   "what": "a tag ends at its first greater-than sign",
   "input": "<a data-x=\"a>b\">Link</a>",
   "text": "b\">Link",
   "sha256": "a174a25f3887ff4371ab54c860f1a1c2b4646379385f485d1e3d0adddba3d54f"
  },
  {
   "id": "R3",
   "what": "a query token removed and character references kept",
   "input": "<p>Index?ts=1700000000 page &amp; more</p>",
   "text": "Index page &amp; more",
   "sha256": "c3867059704bf5f047f1e482442420f20ef8b36c2d777d83332df926725572ad"
  },
  {
   "id": "R4",
   "what": "whitespace collapsed and trimmed",
   "input": "  a \n b ",
   "text": "a b",
   "sha256": "c8687a08aa5d6ed2044328fa6a697ab8e96dc34291e8c2034ae8c38e6fcc6d65"
  },
  {
   "id": "R5",
   "what": "adjacent elements kept apart by the space each removed tag leaves",
   "input": "<td>Site</td><td>Status</td>",
   "text": "Site Status",
   "sha256": "3ce83e18855a0bb50ac44b1bd95040ecc756ff1cc6fae67e42fa84fd8edee3ae"
  }
 ]
}
```

## The version after this one

v2 begins at commit `9bf3274`, dated 2026-09-07. This document is kept as it stands.

## What this document does not state

- The decode of markup in `observe.fetch` is stated, not tested: the vectors
  begin from decoded text.
- `html.unescape`, the `re` module and the PDF libraries belong to the runtime.
  The plant runs this version's code under today's runtime, which may not be
  the runtime that ran it then; a line of this version names no runtime.
- The fetch and the classification that decided whether a digest was taken.
