# openloops.session_page

A session, rendered as one page a person can read: pure, script-free, private by rule.

ADR-016. [`session_page()`](#openloops.session_page.session_page) takes the records of one transcript (already loaded) and
returns one self-contained HTML document: one block per turn of the main thread, the
prompt and then every text the session wrote in that turn. It reads no file and writes
none; [`openloops.tools.page()`](openloops.tools.html.md#openloops.tools.page) and `ol page` do the reading and the writing.

**The anchor.** Every turn is an element whose `id` is `t-<id of the prompt record>`
(the id `openloops.exchanges.Exchange.uuid` carries), with any character outside
`A-Za-z0-9_-` replaced by `_`. [`turn_anchor()`](#openloops.session_page.turn_anchor) builds it, so a consumer links to
`<page address>#t-<id>` without guessing. The turn the address points at is
highlighted by the browser’s own `:target` rule; `focus=` additionally gives that
turn the class `focus`, for a page opened without a fragment.

**What the page holds.** The prompt (who it came from: a person, another session by
name, or tooling; the sentences that ask something are marked), then the session’s texts
in order, and each tool call as one line (the tool’s name and what it was pointed at).
Left out: tool results, thinking, sub-agent threads and text the tooling injected. A
compaction summary is shown and labelled as one.

**Credentials.** Text is cleaned *before* anything is cut: a field holding a
credential-shaped string is replaced whole by a notice naming the pattern class, never
the text, and the page says how many passages were withheld. That is
[`openloops.dashboard.Sanitizer`](openloops.dashboard.html.md#openloops.dashboard.Sanitizer)’s behaviour, not [`openloops.egress.scrub()`](openloops.egress.html.md#openloops.egress.scrub)’s
(which refuses a whole document on the first match, wrong for a long session).

**A rendered session is private data** (ADR-016, rule 4): the words of a session, for the
person who ran it. Nothing here publishes or uploads a page, and a page is never a
fixture, a docs example or an attachment to an issue.

```pycon
>>> records = [
...     {"type": "user", "uuid": "p1", "timestamp": "T1",
...      "message": {"content": "Why is CI slow?"}},
...     {"type": "assistant", "uuid": "a1", "timestamp": "T2",
...      "message": {"content": [{"type": "text", "text": "The cache was cold."}]}},
... ]
>>> html = session_page(records, focus="p1")
>>> turn_anchor("p1"), 'id="t-p1"' in html, "<script" in html
('t-p1', True, False)
```

### Module Attributes

| [`ANCHOR_PREFIX`](#openloops.session_page.ANCHOR_PREFIX)        | What every turn's `id` starts with; the rest is the prompt record's id.               |
|-----------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| [`DFLT_MAX_BYTES`](#openloops.session_page.DFLT_MAX_BYTES)       | Over this many characters of rendered turns, the page keeps the turns around `focus`. |
| [`DFLT_MAX_FIELD_CHARS`](#openloops.session_page.DFLT_MAX_FIELD_CHARS) | A single text, prompt or tool line longer than this is cut (after it is cleaned).     |

### Functions

| [`session_page`](#openloops.session_page.session_page)(records, \*[, focus, sanitizer, ...])   | One self-contained HTML document for a transcript's records.    |
|-------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------|
| [`turn_anchor`](#openloops.session_page.turn_anchor)(prompt_uuid)                             | The `id` of the turn whose prompt record has this id: `t-<id>`. |

### openloops.session_page.ANCHOR_PREFIX *= 't-'*

What every turn’s `id` starts with; the rest is the prompt record’s id.

### openloops.session_page.DFLT_MAX_BYTES *= 400000*

Over this many characters of rendered turns, the page keeps the turns around `focus`.

### openloops.session_page.DFLT_MAX_FIELD_CHARS *= 20000*

A single text, prompt or tool line longer than this is cut (after it is cleaned).

### openloops.session_page.session_page(records, , focus=None, sanitizer=None, key='', max_bytes=400000, max_field_chars=20000)

One self-contained HTML document for a transcript’s records. Pure.

`focus` is the id of a turn’s prompt record (`Exchange.uuid`; a leading `t-` is
accepted too). That turn gets the class `focus` and, when the page is over
`max_bytes` of rendered turns, is the centre of the turns kept: the rest are
replaced by a count of what was left out. With no `focus` the latest turns are
kept. `key` names the session in the header.

`sanitizer=` is the seam (default: a fresh [`openloops.dashboard.Sanitizer`](openloops.dashboard.html.md#openloops.dashboard.Sanitizer),
so each page counts its own withheld passages); any object with `text(str) -> str`
(escaped) and a `withheld` list substitutes.

No script, no external request, no event handler. See the module docstring for the
anchor format, what is left out, and why a rendered session is private data.

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)

### openloops.session_page.turn_anchor(prompt_uuid)

The `id` of the turn whose prompt record has this id: `t-<id>`.

A consumer’s link is `<page address>#` + this. Characters outside `A-Za-z0-9_-`
become `_` (a record id is a UUID in practice, so nothing changes).

```pycon
>>> turn_anchor("0f3a-91")
't-0f3a-91'
```

* **Return type:**
  [`str`](https://docs.python.org/3/builtins/stdtypes.html#str)
