# Degrading

A hotty program runs anywhere a standard terminal program runs, rendering
surfaces only when a hotty host is present
([§14](https://hotty.neuroplast.io/docs/protocol#14-degradation)). On other terminals it renders to the
character grid, and without a terminal it writes plain data. The program
detects its environment at startup and runs the same application logic in
all three modes.

## Three renditions

Every hotty program supports three output modes, which the SDKs expose
explicitly ([SDK design §2.5](https://hotty.neuroplast.io/docs/sdk-design#25-three-renditions)):

| where it runs | what it shows | how it knows |
| --- | --- | --- |
| a terminal that is a host | surfaces | detection finds a host it can use |
| a terminal that is not a host | cells | detection says no |
| no terminal: a cron job, a CI log | plain data | opening the terminal fails |

For example, a CLI status command renders a styled card on a hotty host,
aligned text in a standard terminal, and plain data when run without a TTY.
In cell mode, the program sends no hotty sequences after the initial query.

An SDK opens the process's controlling terminal directly (`/dev/tty` or the
platform console) regardless of where stdout is redirected
([SDK design §4.1](https://hotty.neuroplast.io/docs/sdk-design#41-terminal)). It falls back to plain data
only when no controlling terminal is available.

## Is there a host?

At startup, the program sends a hotty query immediately followed by a
standard terminal query ([§4](https://hotty.neuroplast.io/docs/protocol#4-detection-and-capabilities)):

```text
program → ESC ] 7279 ; a=q:n=1:v=0.9,0.10 ST  CSI c
```

`CSI c` requests Primary Device Attributes (DA1), which every terminal
answers. Because a hotty host answers `a=q` before answering DA1, response
order determines whether a host is present:

```text
host     → ESC ] 7279 ; a=ok:n=1:re=q ; <the capabilities> ST  CSI ? 62 ; 22 c
terminal → CSI ? 62 ; 22 c
```

If a hotty reply arrives before DA1, the terminal is a host and the reply
carries its capabilities. If DA1 arrives alone, the terminal is not a host,
and the program falls back after a short grace period instead of waiting for
a full timeout.

Because a DA1 response might belong to an earlier query from the program or
a previous command, the detector waits briefly after an unaccompanied DA1.
The SDK detector waits 150 ms after DA1 for a hotty reply, and times out
completely after 1500 ms if nothing responds
([SDK design §3.8](https://hotty.neuroplast.io/docs/sdk-design#38-detector)).

The detector distinguishes two states: **decided** (it knows whether a host
is present) and **done** (all expected detection responses have been
consumed). A framework adapter can start rendering as soon as it is decided;
a program about to hand off the terminal waits until detection is done.

## Versions

The query lists the protocol versions the program supports, and the host
selects the highest mutually supported version. If no version overlaps, the
host replies with `EVERSION` and lists its supported versions in `detail`:

```text
program → ESC ] 7279 ; a=q:n=1:v=0.9,0.10 ST  CSI c
host    → ESC ] 7279 ; a=err:n=1:re=q ; {"code": "EVERSION", "detail": "0.8"} ST  CSI ? 62 ; 22 c
```

The program treats `EVERSION` as no host and falls back to cell rendering.
It does the same if the capabilities payload fails to decode or names an
unrequested version. Prior to 1.0, protocol versions may change message
semantics, so mismatched versions never interoperate
([§15](https://hotty.neuroplast.io/docs/protocol#15-versioning-and-extensions)).

Current SDKs support 0.9 and 0.10, so programs built with them work on hosts
running either version and fall back to cells on 0.8 hosts. The `detail`
field allows a program to inform the user which versions the terminal
supports. Compare versions as numbers, not as strings: 0.10 comes after 0.9.

A session speaks the version the host answered in. A program that lists only
0.9 hears 0.9's events from a 0.10 host, and a 0.10 program on a 0.9 host
hears none of 0.10's: no gestures, no `wheel`, no `pointer`, and one finger's
drag at a time ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#in-a-09-session)). Offer
another way to zoom and pan there, such as keys or buttons.

## Features

A 0.10 host implements the full 0.10 specification except for explicitly
optional features ([§4](https://hotty.neuroplast.io/docs/protocol#4-detection-and-capabilities)). The
`features` list in the capabilities payload advertises which optional
features are available so programs can adapt when one is absent:

| feature | without it, the program |
| --- | --- |
| `passthrough` | keeps its surfaces off the cells whose pointer it needs |
| `hover` | clears what it lit under the pointer on the next key or press, since it never hears the pointer leave |
| `count` | offers another way to do what a double click would |
| `menu` | shows its menu itself, in its cells or on a surface |
| `backdrop` | draws in its cells what a backdrop would have shown under them: borders, gutters |

Ignore unrecognized feature names, as future versions may introduce new
ones. Cell-level edit commands require the `edit` capability
([Keyboard and focus](https://hotty.neuroplast.io/docs/concepts/keyboard#edit-commands-in-the-cells)). Touch
regions require no feature check: hosts without touch support accept
`a=touch` and ignore it. Nor do the kind of pointer, gestures, and the wheel:
a host without a touchscreen or a touchpad simply never sends them.

The capabilities payload may also identify the host software and its version.
Use these fields only to work around known bugs in specific host versions,
never as a substitute for checking capability fields.

## A late answer

A program that fell back to cell mode can still upgrade to surfaces if it
requested a late reply ([§4](https://hotty.neuroplast.io/docs/protocol#4-detection-and-capabilities)).
For example, a multiplexer may start a pane with no terminal attached (such
as after a reboot): initial detection finds no host, but a host terminal may
attach later.

```text
program  → ESC ] 7279 ; a=q:n=1:v=0.9,0.10:late=1 ST  CSI c
terminal → CSI ? 62 ; 22 c                                    (no host yet: the program draws in cells)
host     → ESC ] 7279 ; a=ok:n=1:re=q ; <the capabilities> ST   (later: a host attached)
```

- **Intermediaries** (such as multiplexers) may store one pending query per
  pane and answer it once a host attaches.
- **The program** handles the late reply as a normal host detection if the
  capabilities are usable, and can begin rendering surfaces from that point.
- **Before exiting**, the program cancels its pending query with `a=q:q=2`
  so a late reply is not delivered to the next process reading the terminal.

SDKs expose this via a `Late` option. When a late reply arrives, a framework
session switches to surface mode and triggers a re-layout
([SDK design §4.3](https://hotty.neuroplast.io/docs/sdk-design#43-session-a-frameworks-adapter)).

## The polyfill

A polyfill provides hotty host support on terminals that support inline
images ([§14](https://hotty.neuroplast.io/docs/protocol#14-degradation)). It sits on the PTY between
the program and the terminal, forwarding all non-hotty bytes, answering the
hotty query, rendering each surface locally, and displaying the result via
the kitty graphics protocol.

No changes are required in the program—even over SSH—because the polyfill
runs locally beside the terminal. Rasterized surfaces give up some benefits
of native hosts: text inside surfaces cannot be selected, and images lose
sharpness when zoomed. `hotty run` is a built-in polyfill
([Choose a host](https://hotty.neuroplast.io/docs/hosts#any-terminal-with-kitty-graphics)).

Because a polyfill reads keyboard input as raw terminal bytes, it knows the
character produced by a key but not its physical key position. Keybindings
that must work under a polyfill should bind character names (`ctrl+w`)
rather than physical keys (`ctrl+key_w`)
([Keyboard and focus](https://hotty.neuroplast.io/docs/concepts/keyboard#two-names-for-one-press)). An SDK can
report whether the terminal supports kitty graphics so the program can
suggest running under the polyfill.

## What a program does in each case

- **On a host:** Inspects capabilities, renders surfaces, and processes
  events. Before exiting, it detaches any surfaces left on screen and drains
  pending command replies
  ([SDK design §2.6](https://hotty.neuroplast.io/docs/sdk-design#26-leave-the-terminal-clean)).
- **In cells:** Renders the interface as terminal text and emits no hotty
  sequences beyond the startup query. SDK text fields use the same editing
  behavior and keybindings in both modes so keyboard muscle memory carries
  over ([SDK design §4.6](https://hotty.neuroplast.io/docs/sdk-design#46-field)).
- **Without a terminal:** Outputs plain data.

The application logic stays the same across all three modes; only the
rendering layer changes.
