# sticky-model

`pi-dent-sticky-model` keeps the previous session's model and thinking level across `/new`. pi
falls back to the configured `defaultProvider`/`defaultModel` for a new session; this extension
reapplies what the previous session used instead. It never changes that configuration itself.

The pi API this spec relies on: the `session_start` extension event carries `reason`
(`"startup" | "reload" | "new" | "resume" | "fork"`) and, for `"new"`, `"resume"` and `"fork"`,
a `previousSessionFile` path; the session context exposes a model registry with
`find(provider, modelId)`; and the extension API offers `setModel(model)` — returning `false`
when the model's provider has no working authentication — and
`setThinkingLevel(level)`, which the host clamps to the model's capabilities.

## 1. Trigger {#trigger}

On `session_start`, the extension MUST act only when both hold:

- `reason` is `"new"`, and
- `previousSessionFile` is a non-empty string.

For any other `reason` (`"startup"`, `"reload"`, `"resume"`, `"fork"`) or a missing
`previousSessionFile`, the extension MUST NOT read the previous session, MUST NOT change the
model or the thinking level, and MUST NOT show a message. When the previous session carries no
sticky state ([reading](#reading)), the extension MUST leave the model and thinking level at
their defaults and show no message.

## 2. Sticky state {#sticky-state}

The previous session file is a JSONL transcript: one JSON object per line, in append order. The
sticky state is the last two entries of these types, each taken independently:

| Entry type              | Fields relied on      | Holds                                    |
| ----------------------- | --------------------- | ---------------------------------------- |
| `model_change`          | `provider`, `modelId` | The model the session last switched to.  |
| `thinking_level_change` | `thinkingLevel`       | The thinking level the session last set. |

"Last" means the last matching line in file order. An entry of one of these types whose relied-on
fields are missing, of the wrong type, or an empty `provider`/`modelId` string, is not sticky
state and is skipped. A line that is not valid JSON, an empty line, and any other entry type are
ignored. Both entry types are independent: either may be present without the other.

## 3. Reading {#reading}

The extension reads [the sticky state](#sticky-state) from `previousSessionFile` only. A file
that cannot be read (missing, unreadable) yields no sticky state: the extension MUST NOT show a
message and MUST NOT change the model or thinking level. A read MUST NOT throw out of the
extension: no `session_start` handler failure may abort the session start.

## 4. Applying {#applying}

When there is sticky state, the extension applies it in this order:

1. **Model.** `provider` and `modelId` are looked up in the model registry with
   `find(provider, modelId)`. When the registry has no such model, the extension MUST skip the
   model silently. When it has one, the extension calls `setModel(model)`; when that returns
   `false`, the extension MUST notify `No auth for <provider>/<modelId>` with severity `error`,
   where `<provider>` and `<modelId>` are the entry's fields verbatim.
2. **Thinking level.** A `thinkingLevel` of `"off"`, `"minimal"`, `"low"`, `"medium"`, `"high"`,
   `"xhigh"` or `"max"` is applied with `setThinkingLevel(level)`; any other value is skipped.
   The level MUST be applied after the model, so the host clamps it to that model's
   capabilities. Without a sticky model entry, a valid level is still applied to the session's
   default model.

> Note (non-normative): the silent skip for an unknown model keeps noise down for models that
> are merely not enabled in the new session; the auth failure is reported because the user
> explicitly asked for that model.

## 5. Settings {#settings}

The extension MUST NOT write the configuration: `defaultProvider`, `defaultModel` and any other
setting keep their values. `setModel` and `setThinkingLevel` record their change in the new
session's history only, which is what makes the state sticky across `/new` without becoming the
startup default.

## Specification gaps

- A `model_change` entry whose `provider`/`modelId` name a model the registry offers but that
  fails for another reason than missing authentication is unspecified: the host's `setModel`
  contract has one failure signal (`false`), reported as [no auth](#applying).
- Entries of the two types with equal validity in file order are independent and both apply;
  no further conflict rule is specified.
